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 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 5c860e29 2018-03-12 stsp
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
139 5c860e29 2018-03-12 stsp
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
167 5c860e29 2018-03-12 stsp };
168 ce5b7c56 2019-07-09 stsp
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp {
172 ce5b7c56 2019-07-09 stsp int i;
173 ce5b7c56 2019-07-09 stsp
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
178 ce5b7c56 2019-07-09 stsp }
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 ce5b7c56 2019-07-09 stsp }
181 5c860e29 2018-03-12 stsp
182 5c860e29 2018-03-12 stsp int
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
184 5c860e29 2018-03-12 stsp {
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
187 5c860e29 2018-03-12 stsp int ch;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
192 83cd27f8 2020-01-13 stsp };
193 5c860e29 2018-03-12 stsp
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
195 5c860e29 2018-03-12 stsp
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
198 1b6b95a8 2018-03-12 stsp case 'h':
199 1b6b95a8 2018-03-12 stsp hflag = 1;
200 53ccebc2 2019-07-30 stsp break;
201 53ccebc2 2019-07-30 stsp case 'V':
202 53ccebc2 2019-07-30 stsp Vflag = 1;
203 1b6b95a8 2018-03-12 stsp break;
204 5c860e29 2018-03-12 stsp default:
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp }
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
213 53ccebc2 2019-07-30 stsp
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
216 53ccebc2 2019-07-30 stsp return 1;
217 53ccebc2 2019-07-30 stsp }
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
221 5c860e29 2018-03-12 stsp
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
224 99437157 2018-11-11 stsp
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
227 d7d4f210 2018-03-12 stsp
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
229 5c860e29 2018-03-12 stsp
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 5c860e29 2018-03-12 stsp continue;
233 5c860e29 2018-03-12 stsp
234 1b6b95a8 2018-03-12 stsp if (hflag)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
236 1b6b95a8 2018-03-12 stsp
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 d7d4f210 2018-03-12 stsp return 1;
246 d7d4f210 2018-03-12 stsp }
247 d7d4f210 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp return 0;
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
253 5c860e29 2018-03-12 stsp return 1;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
258 5c860e29 2018-03-12 stsp {
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
261 ce5b7c56 2019-07-09 stsp if (hflag)
262 ce5b7c56 2019-07-09 stsp list_commands();
263 5c860e29 2018-03-12 stsp exit(1);
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
268 e2ba3d07 2019-05-13 stsp {
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
271 8920fa04 2019-08-18 stsp
272 8920fa04 2019-08-18 stsp *abspath = NULL;
273 e2ba3d07 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
277 e2ba3d07 2019-05-13 stsp
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
280 0ee7065d 2019-05-13 stsp if (err)
281 0ee7065d 2019-05-13 stsp return err;
282 0ee7065d 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
288 0ee7065d 2019-05-13 stsp }
289 0ee7065d 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp return NULL;
291 e2ba3d07 2019-05-13 stsp }
292 e2ba3d07 2019-05-13 stsp
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
296 0266afb7 2019-01-04 stsp {
297 163ce85a 2019-05-13 stsp const struct got_error *err;
298 0266afb7 2019-01-04 stsp
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
302 37c06ea4 2019-07-15 stsp #endif
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
305 0266afb7 2019-01-04 stsp
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
308 0266afb7 2019-01-04 stsp
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
311 0266afb7 2019-01-04 stsp
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp return NULL;
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
324 2c7829a4 2019-06-17 stsp {
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp exit(1);
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
331 2c7829a4 2019-06-17 stsp {
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
334 2c7829a4 2019-06-17 stsp int ch;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
338 2c7829a4 2019-06-17 stsp default:
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp }
342 2c7829a4 2019-06-17 stsp }
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
350 2c7829a4 2019-06-17 stsp #endif
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp if (error)
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
370 3ce1b845 2019-07-15 stsp done:
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
373 3ce1b845 2019-07-15 stsp }
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
377 3ce1b845 2019-07-15 stsp {
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp exit(1);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp int
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
385 3ce1b845 2019-07-15 stsp {
386 3ce1b845 2019-07-15 stsp pid_t pid;
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
395 3ce1b845 2019-07-15 stsp case -1:
396 3ce1b845 2019-07-15 stsp goto doneediting;
397 3ce1b845 2019-07-15 stsp case 0:
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
400 3ce1b845 2019-07-15 stsp }
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
404 3ce1b845 2019-07-15 stsp break;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return -1;
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
422 3ce1b845 2019-07-15 stsp {
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
426 3ce1b845 2019-07-15 stsp FILE *fp;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
449 3ce1b845 2019-07-15 stsp len = 0;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
454 3ce1b845 2019-07-15 stsp goto done;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp fclose(fp);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp len--;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
473 3ce1b845 2019-07-15 stsp done:
474 3ce1b845 2019-07-15 stsp if (err) {
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp return err;
479 3ce1b845 2019-07-15 stsp }
480 3ce1b845 2019-07-15 stsp
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
484 3ce1b845 2019-07-15 stsp {
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
487 3ce1b845 2019-07-15 stsp int fd;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
493 3ce1b845 2019-07-15 stsp
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp if (err)
497 3ce1b845 2019-07-15 stsp goto done;
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
500 3ce1b845 2019-07-15 stsp close(fd);
501 3ce1b845 2019-07-15 stsp
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 3ce1b845 2019-07-15 stsp done:
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
510 3ce1b845 2019-07-15 stsp {
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
513 3ce1b845 2019-07-15 stsp }
514 3ce1b845 2019-07-15 stsp
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
517 84792843 2019-08-09 stsp {
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp *author = NULL;
522 aba9c984 2019-09-08 stsp
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
529 aba9c984 2019-09-08 stsp }
530 84792843 2019-08-09 stsp
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
539 c9956ddf 2019-09-08 stsp }
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
542 84792843 2019-08-09 stsp }
543 84792843 2019-08-09 stsp
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
547 84792843 2019-08-09 stsp
548 84792843 2019-08-09 stsp /*
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
551 84792843 2019-08-09 stsp */
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp goto done;
557 aba9c984 2019-09-08 stsp }
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 aba9c984 2019-09-08 stsp goto done;
563 aba9c984 2019-09-08 stsp }
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 aba9c984 2019-09-08 stsp done:
569 aba9c984 2019-09-08 stsp if (err) {
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
572 aba9c984 2019-09-08 stsp }
573 aba9c984 2019-09-08 stsp return err;
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
578 c9956ddf 2019-09-08 stsp {
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
580 c9956ddf 2019-09-08 stsp
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
585 c9956ddf 2019-09-08 stsp
586 c9956ddf 2019-09-08 stsp }
587 c9956ddf 2019-09-08 stsp return NULL;
588 84792843 2019-08-09 stsp }
589 84792843 2019-08-09 stsp
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
592 3ce1b845 2019-07-15 stsp {
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
601 3ce1b845 2019-07-15 stsp int ch;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
610 3ce1b845 2019-07-15 stsp case 'b':
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'm':
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp break;
620 3ce1b845 2019-07-15 stsp case 'r':
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
624 9ba1d308 2019-10-21 stsp optarg);
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp case 'I':
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
630 3ce1b845 2019-07-15 stsp break;
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
632 3ce1b845 2019-07-15 stsp NULL);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp break;
636 3ce1b845 2019-07-15 stsp default:
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp }
640 3ce1b845 2019-07-15 stsp }
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
644 3ce1b845 2019-07-15 stsp
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 aba9c984 2019-09-08 stsp "unveil",
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
650 3ce1b845 2019-07-15 stsp #endif
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
653 2c7829a4 2019-06-17 stsp
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
658 3ce1b845 2019-07-15 stsp }
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
661 c9956ddf 2019-09-08 stsp if (error)
662 c9956ddf 2019-09-08 stsp goto done;
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 3ce1b845 2019-07-15 stsp if (error)
665 3ce1b845 2019-07-15 stsp goto done;
666 aba9c984 2019-09-08 stsp
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
668 aba9c984 2019-09-08 stsp if (error)
669 aba9c984 2019-09-08 stsp return error;
670 e560b7e0 2019-11-28 stsp
671 e560b7e0 2019-11-28 stsp /*
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
675 e560b7e0 2019-11-28 stsp */
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp } else {
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp goto done;
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp }
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
700 3ce1b845 2019-07-15 stsp
701 3ce1b845 2019-07-15 stsp /*
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
704 3ce1b845 2019-07-15 stsp */
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
707 3ce1b845 2019-07-15 stsp if (error)
708 3ce1b845 2019-07-15 stsp goto done;
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 3ce1b845 2019-07-15 stsp goto done;
717 ef293bdd 2019-10-21 stsp }
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 ef293bdd 2019-10-21 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 ef293bdd 2019-10-21 stsp
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp goto done;
761 ef293bdd 2019-10-21 stsp }
762 3ce1b845 2019-07-15 stsp
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp goto done;
769 ef293bdd 2019-10-21 stsp }
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp goto done;
784 ef293bdd 2019-10-21 stsp }
785 3ce1b845 2019-07-15 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
789 2c7829a4 2019-06-17 stsp done:
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
809 93658fb9 2020-03-18 stsp }
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
813 93658fb9 2020-03-18 stsp {
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
890 4ba14133 2020-03-20 stsp {
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
893 4ba14133 2020-03-20 stsp
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
895 4ba14133 2020-03-20 stsp if (err)
896 4ba14133 2020-03-20 stsp return err;
897 4ba14133 2020-03-20 stsp
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
903 6338a6a1 2020-03-21 stsp }
904 4ba14133 2020-03-20 stsp return err;
905 4ba14133 2020-03-20 stsp }
906 4ba14133 2020-03-20 stsp
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
910 41b0de12 2020-03-21 stsp {
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
913 41b0de12 2020-03-21 stsp
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
917 41b0de12 2020-03-21 stsp
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
919 41b0de12 2020-03-21 stsp }
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
925 41b0de12 2020-03-21 stsp
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
927 41b0de12 2020-03-21 stsp if (err)
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
931 41b0de12 2020-03-21 stsp }
932 41b0de12 2020-03-21 stsp
933 41b0de12 2020-03-21 stsp return NULL;
934 6338a6a1 2020-03-21 stsp }
935 6338a6a1 2020-03-21 stsp
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
939 6338a6a1 2020-03-21 stsp {
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
943 6338a6a1 2020-03-21 stsp
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
945 6338a6a1 2020-03-21 stsp if (err)
946 6338a6a1 2020-03-21 stsp return err;
947 6338a6a1 2020-03-21 stsp
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
949 6338a6a1 2020-03-21 stsp if (err)
950 6338a6a1 2020-03-21 stsp goto done;
951 6338a6a1 2020-03-21 stsp
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
954 6338a6a1 2020-03-21 stsp
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
957 6338a6a1 2020-03-21 stsp done:
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
960 0e4002ca 2020-03-21 stsp }
961 0e4002ca 2020-03-21 stsp
962 0e4002ca 2020-03-21 stsp static int
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
964 0e4002ca 2020-03-21 stsp {
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
966 0e4002ca 2020-03-21 stsp return 0;
967 0e4002ca 2020-03-21 stsp refname += 5;
968 0e4002ca 2020-03-21 stsp
969 0e4002ca 2020-03-21 stsp /*
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
972 0e4002ca 2020-03-21 stsp */
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
974 0e4002ca 2020-03-21 stsp return 0;
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
976 0e4002ca 2020-03-21 stsp return 0;
977 0e4002ca 2020-03-21 stsp
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
980 0e4002ca 2020-03-21 stsp
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
983 0e4002ca 2020-03-21 stsp return 1;
984 0e4002ca 2020-03-21 stsp
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
987 41b0de12 2020-03-21 stsp }
988 41b0de12 2020-03-21 stsp
989 0e4002ca 2020-03-21 stsp static int
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
991 0e4002ca 2020-03-21 stsp {
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
993 0e4002ca 2020-03-21 stsp
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
996 0e4002ca 2020-03-21 stsp return 1;
997 0e4002ca 2020-03-21 stsp }
998 0e4002ca 2020-03-21 stsp
999 0e4002ca 2020-03-21 stsp return 0;
1000 0e4002ca 2020-03-21 stsp }
1001 0e4002ca 2020-03-21 stsp
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1005 0e4002ca 2020-03-21 stsp {
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1008 0e4002ca 2020-03-21 stsp
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1019 0e4002ca 2020-03-21 stsp }
1020 0e4002ca 2020-03-21 stsp
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1023 93658fb9 2020-03-18 stsp {
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1044 93658fb9 2020-03-18 stsp
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1049 d9b4d0c0 2020-03-18 stsp
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1052 659e7fbd 2020-03-20 stsp case 'a':
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1054 4ba14133 2020-03-20 stsp break;
1055 4ba14133 2020-03-20 stsp case 'b':
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1060 659e7fbd 2020-03-20 stsp break;
1061 41b0de12 2020-03-21 stsp case 'l':
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1063 41b0de12 2020-03-21 stsp break;
1064 469dd726 2020-03-20 stsp case 'm':
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1066 469dd726 2020-03-20 stsp break;
1067 68999b92 2020-03-18 stsp case 'v':
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1072 68999b92 2020-03-18 stsp break;
1073 68999b92 2020-03-18 stsp case 'q':
1074 68999b92 2020-03-18 stsp verbosity = -1;
1075 68999b92 2020-03-18 stsp break;
1076 0e4002ca 2020-03-21 stsp case 'R':
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1081 0e4002ca 2020-03-21 stsp break;
1082 93658fb9 2020-03-18 stsp default:
1083 93658fb9 2020-03-18 stsp usage_clone();
1084 93658fb9 2020-03-18 stsp break;
1085 93658fb9 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp }
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1089 39c64a6a 2020-03-18 stsp
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1103 41b0de12 2020-03-21 stsp }
1104 4ba14133 2020-03-20 stsp
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1106 9df6f38b 2020-03-18 stsp
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1111 93658fb9 2020-03-18 stsp else
1112 93658fb9 2020-03-18 stsp usage_clone();
1113 09838ffc 2020-03-18 stsp
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1118 09f63084 2020-03-20 stsp
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1124 09f63084 2020-03-20 stsp }
1125 09838ffc 2020-03-18 stsp
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1131 b46f3e71 2020-03-18 stsp #endif
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1138 b46f3e71 2020-03-18 stsp #endif
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp } else {
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1146 39c64a6a 2020-03-18 stsp }
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1151 bb64b798 2020-03-18 stsp }
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1153 bb64b798 2020-03-18 stsp } else
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1155 bb64b798 2020-03-18 stsp
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1160 bb64b798 2020-03-18 stsp
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1167 41b0de12 2020-03-21 stsp }
1168 bb64b798 2020-03-18 stsp
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1174 ee448f5f 2020-03-18 stsp }
1175 ee448f5f 2020-03-18 stsp }
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1179 f79e6490 2020-04-19 stsp
1180 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1181 f79e6490 2020-04-19 stsp printf("Connecting to %s%s%s\n", host,
1182 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1183 ee448f5f 2020-03-18 stsp
1184 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1185 9c52365f 2020-03-21 stsp server_path, verbosity);
1186 39c64a6a 2020-03-18 stsp if (error)
1187 bb64b798 2020-03-18 stsp goto done;
1188 bb64b798 2020-03-18 stsp
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1200 d9b4d0c0 2020-03-18 stsp
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1204 41b0de12 2020-03-21 stsp }
1205 41b0de12 2020-03-21 stsp
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1212 d9b4d0c0 2020-03-18 stsp
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1218 0e4002ca 2020-03-21 stsp
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1226 0e4002ca 2020-03-21 stsp continue;
1227 0e4002ca 2020-03-21 stsp }
1228 668a20f6 2020-03-18 stsp
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1232 d9b4d0c0 2020-03-18 stsp
1233 469dd726 2020-03-20 stsp if (mirror_references)
1234 469dd726 2020-03-20 stsp continue;
1235 469dd726 2020-03-20 stsp
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1237 7ebc0570 2020-03-18 stsp continue;
1238 7ebc0570 2020-03-18 stsp
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1244 7ebc0570 2020-03-18 stsp }
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1249 d9b4d0c0 2020-03-18 stsp }
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1257 d9b4d0c0 2020-03-18 stsp
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 d9b4d0c0 2020-03-18 stsp continue;
1260 d9b4d0c0 2020-03-18 stsp
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1265 d9b4d0c0 2020-03-18 stsp continue;
1266 55330abe 2020-03-20 stsp }
1267 d9b4d0c0 2020-03-18 stsp goto done;
1268 d9b4d0c0 2020-03-18 stsp }
1269 d9b4d0c0 2020-03-18 stsp
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1274 f298ae0f 2020-03-25 stsp
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1276 f298ae0f 2020-03-25 stsp continue;
1277 f298ae0f 2020-03-25 stsp
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1279 f298ae0f 2020-03-25 stsp continue;
1280 f298ae0f 2020-03-25 stsp
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1286 f298ae0f 2020-03-25 stsp }
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1293 f298ae0f 2020-03-25 stsp }
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1300 f298ae0f 2020-03-25 stsp continue;
1301 f298ae0f 2020-03-25 stsp }
1302 f298ae0f 2020-03-25 stsp goto done;
1303 f298ae0f 2020-03-25 stsp }
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1311 4ba14133 2020-03-20 stsp }
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1313 4ba14133 2020-03-20 stsp /*
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1317 4ba14133 2020-03-20 stsp */
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1321 d9b4d0c0 2020-03-18 stsp
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1326 4ba14133 2020-03-20 stsp continue;
1327 4ba14133 2020-03-20 stsp }
1328 4ba14133 2020-03-20 stsp goto done;
1329 4ba14133 2020-03-20 stsp }
1330 4ba14133 2020-03-20 stsp
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1336 4ba14133 2020-03-20 stsp break;
1337 4ba14133 2020-03-20 stsp }
1338 659e7fbd 2020-03-20 stsp }
1339 659e7fbd 2020-03-20 stsp
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1345 659e7fbd 2020-03-20 stsp }
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1350 b46f3e71 2020-03-18 stsp }
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1360 659e7fbd 2020-03-20 stsp }
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1370 659e7fbd 2020-03-20 stsp }
1371 659e7fbd 2020-03-20 stsp } else {
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1373 d715f13e 2020-03-19 stsp
1374 659e7fbd 2020-03-20 stsp /*
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1377 659e7fbd 2020-03-20 stsp */
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1382 659e7fbd 2020-03-20 stsp } else
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1393 659e7fbd 2020-03-20 stsp }
1394 659e7fbd 2020-03-20 stsp }
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1399 659e7fbd 2020-03-20 stsp }
1400 659e7fbd 2020-03-20 stsp
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1404 09838ffc 2020-03-18 stsp done:
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1410 9c52365f 2020-03-21 stsp }
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1415 bb64b798 2020-03-18 stsp if (repo)
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1422 d9b4d0c0 2020-03-18 stsp }
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1427 d9b4d0c0 2020-03-18 stsp }
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1441 7848a0e1 2020-03-19 stsp }
1442 7848a0e1 2020-03-19 stsp
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1446 7848a0e1 2020-03-19 stsp {
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1450 7848a0e1 2020-03-19 stsp
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1452 7848a0e1 2020-03-19 stsp if (err)
1453 7848a0e1 2020-03-19 stsp goto done;
1454 7848a0e1 2020-03-19 stsp
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1458 88609724 2020-03-21 stsp if (err)
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1465 db6d8ad8 2020-03-21 stsp }
1466 db6d8ad8 2020-03-21 stsp goto done;
1467 db6d8ad8 2020-03-21 stsp }
1468 db6d8ad8 2020-03-21 stsp
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1474 688f11b3 2020-03-21 stsp }
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1476 7848a0e1 2020-03-19 stsp if (err)
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1479 e8a967e0 2020-03-21 stsp if (err)
1480 e8a967e0 2020-03-21 stsp goto done;
1481 7848a0e1 2020-03-19 stsp } else {
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1483 7848a0e1 2020-03-19 stsp if (err)
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1487 6338a6a1 2020-03-21 stsp
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1489 6338a6a1 2020-03-21 stsp if (err)
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1492 6338a6a1 2020-03-21 stsp if (err)
1493 6338a6a1 2020-03-21 stsp goto done;
1494 7848a0e1 2020-03-19 stsp }
1495 6338a6a1 2020-03-21 stsp
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1499 7848a0e1 2020-03-19 stsp done:
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1503 2ab43947 2020-03-18 stsp }
1504 f1bcca34 2020-03-25 stsp
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1508 f1bcca34 2020-03-25 stsp {
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1511 bcf34b0e 2020-03-26 stsp int symref_is_locked = 0;
1512 f1bcca34 2020-03-25 stsp
1513 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1514 bcf34b0e 2020-03-26 stsp if (err) {
1515 bcf34b0e 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1516 bcf34b0e 2020-03-26 stsp return err;
1517 bcf34b0e 2020-03-26 stsp err = got_ref_alloc_symref(&symref, refname, target_ref);
1518 bcf34b0e 2020-03-26 stsp if (err)
1519 bcf34b0e 2020-03-26 stsp goto done;
1520 2ab43947 2020-03-18 stsp
1521 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1522 bcf34b0e 2020-03-26 stsp if (err)
1523 bcf34b0e 2020-03-26 stsp goto done;
1524 f1bcca34 2020-03-25 stsp
1525 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1526 bcf34b0e 2020-03-26 stsp printf("Created reference %s: %s\n",
1527 bcf34b0e 2020-03-26 stsp got_ref_get_name(symref),
1528 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1529 bcf34b0e 2020-03-26 stsp } else {
1530 bcf34b0e 2020-03-26 stsp symref_is_locked = 1;
1531 f1bcca34 2020-03-25 stsp
1532 bcf34b0e 2020-03-26 stsp if (strcmp(got_ref_get_symref_target(symref),
1533 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref)) == 0)
1534 bcf34b0e 2020-03-26 stsp goto done;
1535 bcf34b0e 2020-03-26 stsp
1536 bcf34b0e 2020-03-26 stsp err = got_ref_change_symref(symref,
1537 bcf34b0e 2020-03-26 stsp got_ref_get_name(target_ref));
1538 bcf34b0e 2020-03-26 stsp if (err)
1539 bcf34b0e 2020-03-26 stsp goto done;
1540 bcf34b0e 2020-03-26 stsp
1541 bcf34b0e 2020-03-26 stsp err = got_ref_write(symref, repo);
1542 bcf34b0e 2020-03-26 stsp if (err)
1543 bcf34b0e 2020-03-26 stsp goto done;
1544 bcf34b0e 2020-03-26 stsp
1545 bcf34b0e 2020-03-26 stsp if (verbosity >= 0)
1546 f4d0e3fb 2020-05-15 stsp printf("Updated %s: %s\n", got_ref_get_name(symref),
1547 bcf34b0e 2020-03-26 stsp got_ref_get_symref_target(symref));
1548 bcf34b0e 2020-03-26 stsp
1549 bcf34b0e 2020-03-26 stsp }
1550 f1bcca34 2020-03-25 stsp done:
1551 bcf34b0e 2020-03-26 stsp if (symref_is_locked) {
1552 bcf34b0e 2020-03-26 stsp unlock_err = got_ref_unlock(symref);
1553 bcf34b0e 2020-03-26 stsp if (unlock_err && err == NULL)
1554 bcf34b0e 2020-03-26 stsp err = unlock_err;
1555 bcf34b0e 2020-03-26 stsp }
1556 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1557 f1bcca34 2020-03-25 stsp return err;
1558 f1bcca34 2020-03-25 stsp }
1559 f1bcca34 2020-03-25 stsp
1560 2ab43947 2020-03-18 stsp __dead static void
1561 7848a0e1 2020-03-19 stsp usage_fetch(void)
1562 7848a0e1 2020-03-19 stsp {
1563 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1564 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1565 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1566 13f12b09 2020-03-21 stsp getprogname());
1567 7848a0e1 2020-03-19 stsp exit(1);
1568 7848a0e1 2020-03-19 stsp }
1569 7848a0e1 2020-03-19 stsp
1570 7848a0e1 2020-03-19 stsp static const struct got_error *
1571 3789fd73 2020-03-26 stsp delete_missing_ref(struct got_reference *ref,
1572 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1573 f21ec2f0 2020-03-21 stsp {
1574 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1575 3789fd73 2020-03-26 stsp struct got_object_id *id = NULL;
1576 3789fd73 2020-03-26 stsp char *id_str = NULL;
1577 3789fd73 2020-03-26 stsp
1578 3789fd73 2020-03-26 stsp if (got_ref_is_symbolic(ref)) {
1579 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1580 3789fd73 2020-03-26 stsp if (err)
1581 3789fd73 2020-03-26 stsp return err;
1582 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1583 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1584 3789fd73 2020-03-26 stsp got_ref_get_name(ref),
1585 3789fd73 2020-03-26 stsp got_ref_get_symref_target(ref));
1586 3789fd73 2020-03-26 stsp }
1587 3789fd73 2020-03-26 stsp } else {
1588 3789fd73 2020-03-26 stsp err = got_ref_resolve(&id, repo, ref);
1589 3789fd73 2020-03-26 stsp if (err)
1590 3789fd73 2020-03-26 stsp return err;
1591 3789fd73 2020-03-26 stsp err = got_object_id_str(&id_str, id);
1592 3789fd73 2020-03-26 stsp if (err)
1593 3789fd73 2020-03-26 stsp goto done;
1594 3789fd73 2020-03-26 stsp
1595 3789fd73 2020-03-26 stsp err = got_ref_delete(ref, repo);
1596 3789fd73 2020-03-26 stsp if (err)
1597 3789fd73 2020-03-26 stsp goto done;
1598 3789fd73 2020-03-26 stsp if (verbosity >= 0) {
1599 3789fd73 2020-03-26 stsp printf("Deleted reference %s: %s\n",
1600 3789fd73 2020-03-26 stsp got_ref_get_name(ref), id_str);
1601 3789fd73 2020-03-26 stsp }
1602 3789fd73 2020-03-26 stsp }
1603 3789fd73 2020-03-26 stsp done:
1604 3789fd73 2020-03-26 stsp free(id);
1605 3789fd73 2020-03-26 stsp free(id_str);
1606 3789fd73 2020-03-26 stsp return NULL;
1607 3789fd73 2020-03-26 stsp }
1608 3789fd73 2020-03-26 stsp
1609 3789fd73 2020-03-26 stsp static const struct got_error *
1610 3789fd73 2020-03-26 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1611 3789fd73 2020-03-26 stsp struct got_pathlist_head *their_symrefs, struct got_remote_repo *remote,
1612 3789fd73 2020-03-26 stsp int verbosity, struct got_repository *repo)
1613 3789fd73 2020-03-26 stsp {
1614 3789fd73 2020-03-26 stsp const struct got_error *err = NULL, *unlock_err;
1615 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1616 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1617 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1618 3789fd73 2020-03-26 stsp char *remote_namespace = NULL;
1619 3789fd73 2020-03-26 stsp char *local_refname = NULL;
1620 f21ec2f0 2020-03-21 stsp
1621 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1622 f21ec2f0 2020-03-21 stsp
1623 3789fd73 2020-03-26 stsp if (asprintf(&remote_namespace, "refs/remotes/%s/", remote->name)
1624 3789fd73 2020-03-26 stsp == -1)
1625 3789fd73 2020-03-26 stsp return got_error_from_errno("asprintf");
1626 3789fd73 2020-03-26 stsp
1627 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1628 f21ec2f0 2020-03-21 stsp if (err)
1629 3789fd73 2020-03-26 stsp goto done;
1630 f21ec2f0 2020-03-21 stsp
1631 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1632 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1633 f21ec2f0 2020-03-21 stsp
1634 3789fd73 2020-03-26 stsp if (!remote->mirror_references) {
1635 3789fd73 2020-03-26 stsp if (strncmp(refname, remote_namespace,
1636 3789fd73 2020-03-26 stsp strlen(remote_namespace)) == 0) {
1637 3789fd73 2020-03-26 stsp if (strcmp(refname + strlen(remote_namespace),
1638 3789fd73 2020-03-26 stsp GOT_REF_HEAD) == 0)
1639 3789fd73 2020-03-26 stsp continue;
1640 3789fd73 2020-03-26 stsp if (asprintf(&local_refname, "refs/heads/%s",
1641 3789fd73 2020-03-26 stsp refname + strlen(remote_namespace)) == -1) {
1642 3789fd73 2020-03-26 stsp err = got_error_from_errno("asprintf");
1643 3789fd73 2020-03-26 stsp goto done;
1644 3789fd73 2020-03-26 stsp }
1645 3789fd73 2020-03-26 stsp } else if (strncmp(refname, "refs/tags/", 10) != 0)
1646 3789fd73 2020-03-26 stsp continue;
1647 3789fd73 2020-03-26 stsp }
1648 f21ec2f0 2020-03-21 stsp
1649 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1650 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1651 f21ec2f0 2020-03-21 stsp break;
1652 f21ec2f0 2020-03-21 stsp }
1653 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1654 f21ec2f0 2020-03-21 stsp continue;
1655 f21ec2f0 2020-03-21 stsp
1656 3789fd73 2020-03-26 stsp TAILQ_FOREACH(pe, their_symrefs, entry) {
1657 3789fd73 2020-03-26 stsp if (strcmp(local_refname, pe->path) == 0)
1658 3789fd73 2020-03-26 stsp break;
1659 3789fd73 2020-03-26 stsp }
1660 3789fd73 2020-03-26 stsp if (pe != NULL)
1661 3789fd73 2020-03-26 stsp continue;
1662 f21ec2f0 2020-03-21 stsp
1663 3789fd73 2020-03-26 stsp err = delete_missing_ref(re->ref, verbosity, repo);
1664 f21ec2f0 2020-03-21 stsp if (err)
1665 f21ec2f0 2020-03-21 stsp break;
1666 3789fd73 2020-03-26 stsp
1667 3789fd73 2020-03-26 stsp if (local_refname) {
1668 3789fd73 2020-03-26 stsp struct got_reference *ref;
1669 3789fd73 2020-03-26 stsp err = got_ref_open(&ref, repo, local_refname, 1);
1670 3789fd73 2020-03-26 stsp if (err) {
1671 3789fd73 2020-03-26 stsp if (err->code != GOT_ERR_NOT_REF)
1672 3789fd73 2020-03-26 stsp break;
1673 3789fd73 2020-03-26 stsp free(local_refname);
1674 3789fd73 2020-03-26 stsp local_refname = NULL;
1675 3789fd73 2020-03-26 stsp continue;
1676 3789fd73 2020-03-26 stsp }
1677 3789fd73 2020-03-26 stsp err = delete_missing_ref(ref, verbosity, repo);
1678 3789fd73 2020-03-26 stsp if (err)
1679 3789fd73 2020-03-26 stsp break;
1680 3789fd73 2020-03-26 stsp unlock_err = got_ref_unlock(ref);
1681 3789fd73 2020-03-26 stsp got_ref_close(ref);
1682 3789fd73 2020-03-26 stsp if (unlock_err && err == NULL) {
1683 3789fd73 2020-03-26 stsp err = unlock_err;
1684 3789fd73 2020-03-26 stsp break;
1685 3789fd73 2020-03-26 stsp }
1686 3789fd73 2020-03-26 stsp
1687 3789fd73 2020-03-26 stsp free(local_refname);
1688 3789fd73 2020-03-26 stsp local_refname = NULL;
1689 6338a6a1 2020-03-21 stsp }
1690 f21ec2f0 2020-03-21 stsp }
1691 3789fd73 2020-03-26 stsp done:
1692 3789fd73 2020-03-26 stsp free(remote_namespace);
1693 3789fd73 2020-03-26 stsp free(local_refname);
1694 0e4002ca 2020-03-21 stsp return err;
1695 0e4002ca 2020-03-21 stsp }
1696 0e4002ca 2020-03-21 stsp
1697 0e4002ca 2020-03-21 stsp static const struct got_error *
1698 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1699 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1700 0e4002ca 2020-03-21 stsp {
1701 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1702 0e4002ca 2020-03-21 stsp char *remote_refname;
1703 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1704 0e4002ca 2020-03-21 stsp
1705 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1706 0e4002ca 2020-03-21 stsp refname += 5;
1707 0e4002ca 2020-03-21 stsp
1708 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1709 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1710 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1711 f21ec2f0 2020-03-21 stsp
1712 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1713 0e4002ca 2020-03-21 stsp if (err) {
1714 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1715 0e4002ca 2020-03-21 stsp goto done;
1716 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1717 0e4002ca 2020-03-21 stsp } else {
1718 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1719 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1720 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1721 9f142382 2020-03-21 stsp err = unlock_err;
1722 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1723 0e4002ca 2020-03-21 stsp }
1724 0e4002ca 2020-03-21 stsp done:
1725 0e4002ca 2020-03-21 stsp free(remote_refname);
1726 f21ec2f0 2020-03-21 stsp return err;
1727 f21ec2f0 2020-03-21 stsp }
1728 f21ec2f0 2020-03-21 stsp
1729 f21ec2f0 2020-03-21 stsp static const struct got_error *
1730 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1731 7848a0e1 2020-03-19 stsp {
1732 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1733 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1734 7848a0e1 2020-03-19 stsp const char *remote_name;
1735 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1736 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1737 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1738 7848a0e1 2020-03-19 stsp int nremotes;
1739 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1740 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1741 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1742 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1743 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1744 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1745 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1746 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1747 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1748 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1749 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1750 7848a0e1 2020-03-19 stsp
1751 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1752 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1753 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1754 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1755 7848a0e1 2020-03-19 stsp
1756 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1757 7848a0e1 2020-03-19 stsp switch (ch) {
1758 659e7fbd 2020-03-20 stsp case 'a':
1759 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1760 4ba14133 2020-03-20 stsp break;
1761 4ba14133 2020-03-20 stsp case 'b':
1762 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1763 4ba14133 2020-03-20 stsp optarg, NULL);
1764 4ba14133 2020-03-20 stsp if (error)
1765 4ba14133 2020-03-20 stsp return error;
1766 41b0de12 2020-03-21 stsp break;
1767 f21ec2f0 2020-03-21 stsp case 'd':
1768 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1769 f21ec2f0 2020-03-21 stsp break;
1770 41b0de12 2020-03-21 stsp case 'l':
1771 41b0de12 2020-03-21 stsp list_refs_only = 1;
1772 659e7fbd 2020-03-20 stsp break;
1773 7848a0e1 2020-03-19 stsp case 'r':
1774 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1775 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1776 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1777 7848a0e1 2020-03-19 stsp optarg);
1778 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1779 7848a0e1 2020-03-19 stsp break;
1780 db6d8ad8 2020-03-21 stsp case 't':
1781 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1782 db6d8ad8 2020-03-21 stsp break;
1783 7848a0e1 2020-03-19 stsp case 'v':
1784 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1785 7848a0e1 2020-03-19 stsp verbosity = 0;
1786 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1787 7848a0e1 2020-03-19 stsp verbosity++;
1788 7848a0e1 2020-03-19 stsp break;
1789 7848a0e1 2020-03-19 stsp case 'q':
1790 7848a0e1 2020-03-19 stsp verbosity = -1;
1791 7848a0e1 2020-03-19 stsp break;
1792 0e4002ca 2020-03-21 stsp case 'R':
1793 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1794 0e4002ca 2020-03-21 stsp optarg, NULL);
1795 0e4002ca 2020-03-21 stsp if (error)
1796 0e4002ca 2020-03-21 stsp return error;
1797 0e4002ca 2020-03-21 stsp break;
1798 7848a0e1 2020-03-19 stsp default:
1799 7848a0e1 2020-03-19 stsp usage_fetch();
1800 7848a0e1 2020-03-19 stsp break;
1801 7848a0e1 2020-03-19 stsp }
1802 7848a0e1 2020-03-19 stsp }
1803 7848a0e1 2020-03-19 stsp argc -= optind;
1804 7848a0e1 2020-03-19 stsp argv += optind;
1805 7848a0e1 2020-03-19 stsp
1806 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1807 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1808 41b0de12 2020-03-21 stsp if (list_refs_only) {
1809 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1810 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1811 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1812 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1813 f21ec2f0 2020-03-21 stsp if (delete_refs)
1814 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1815 41b0de12 2020-03-21 stsp if (verbosity == -1)
1816 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1817 41b0de12 2020-03-21 stsp }
1818 4ba14133 2020-03-20 stsp
1819 7848a0e1 2020-03-19 stsp if (argc == 0)
1820 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1821 7848a0e1 2020-03-19 stsp else if (argc == 1)
1822 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1823 7848a0e1 2020-03-19 stsp else
1824 7848a0e1 2020-03-19 stsp usage_fetch();
1825 7848a0e1 2020-03-19 stsp
1826 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1827 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1828 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1829 7848a0e1 2020-03-19 stsp goto done;
1830 7848a0e1 2020-03-19 stsp }
1831 7848a0e1 2020-03-19 stsp
1832 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1833 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1834 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1835 7848a0e1 2020-03-19 stsp goto done;
1836 7848a0e1 2020-03-19 stsp else
1837 7848a0e1 2020-03-19 stsp error = NULL;
1838 7848a0e1 2020-03-19 stsp if (worktree) {
1839 7848a0e1 2020-03-19 stsp repo_path =
1840 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1841 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1842 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1843 7848a0e1 2020-03-19 stsp if (error)
1844 7848a0e1 2020-03-19 stsp goto done;
1845 7848a0e1 2020-03-19 stsp } else {
1846 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1847 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1848 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1849 7848a0e1 2020-03-19 stsp goto done;
1850 7848a0e1 2020-03-19 stsp }
1851 7848a0e1 2020-03-19 stsp }
1852 7848a0e1 2020-03-19 stsp }
1853 7848a0e1 2020-03-19 stsp
1854 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1855 7848a0e1 2020-03-19 stsp if (error)
1856 7848a0e1 2020-03-19 stsp goto done;
1857 7848a0e1 2020-03-19 stsp
1858 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1859 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1860 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1861 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1862 7848a0e1 2020-03-19 stsp break;
1863 7848a0e1 2020-03-19 stsp }
1864 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1865 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1866 7848a0e1 2020-03-19 stsp goto done;
1867 7848a0e1 2020-03-19 stsp }
1868 7848a0e1 2020-03-19 stsp
1869 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1870 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1871 7848a0e1 2020-03-19 stsp if (error)
1872 7848a0e1 2020-03-19 stsp goto done;
1873 7848a0e1 2020-03-19 stsp
1874 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1875 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1876 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1877 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1878 7848a0e1 2020-03-19 stsp err(1, "pledge");
1879 7848a0e1 2020-03-19 stsp #endif
1880 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1881 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1882 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1883 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1884 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1885 7848a0e1 2020-03-19 stsp err(1, "pledge");
1886 7848a0e1 2020-03-19 stsp #endif
1887 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1888 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1889 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1890 7848a0e1 2020-03-19 stsp goto done;
1891 7848a0e1 2020-03-19 stsp } else {
1892 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1893 7848a0e1 2020-03-19 stsp goto done;
1894 7848a0e1 2020-03-19 stsp }
1895 7848a0e1 2020-03-19 stsp
1896 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1897 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1898 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1899 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1900 7848a0e1 2020-03-19 stsp goto done;
1901 7848a0e1 2020-03-19 stsp }
1902 7848a0e1 2020-03-19 stsp }
1903 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1904 7848a0e1 2020-03-19 stsp if (error)
1905 7848a0e1 2020-03-19 stsp goto done;
1906 f79e6490 2020-04-19 stsp
1907 f79e6490 2020-04-19 stsp if (verbosity >= 0)
1908 f79e6490 2020-04-19 stsp printf("Connecting to \"%s\" %s%s%s\n", remote->name, host,
1909 f79e6490 2020-04-19 stsp port ? ":" : "", port ? port : "");
1910 7848a0e1 2020-03-19 stsp
1911 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1912 9c52365f 2020-03-21 stsp server_path, verbosity);
1913 7848a0e1 2020-03-19 stsp if (error)
1914 7848a0e1 2020-03-19 stsp goto done;
1915 7848a0e1 2020-03-19 stsp
1916 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1917 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1918 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1919 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1920 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1921 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1922 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1923 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1924 7848a0e1 2020-03-19 stsp if (error)
1925 7848a0e1 2020-03-19 stsp goto done;
1926 7848a0e1 2020-03-19 stsp
1927 41b0de12 2020-03-21 stsp if (list_refs_only) {
1928 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1929 41b0de12 2020-03-21 stsp goto done;
1930 41b0de12 2020-03-21 stsp }
1931 41b0de12 2020-03-21 stsp
1932 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1933 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1934 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1935 bcf34b0e 2020-03-26 stsp } else if (verbosity >= 0) {
1936 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1937 984065c8 2020-03-19 stsp if (error)
1938 984065c8 2020-03-19 stsp goto done;
1939 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1940 984065c8 2020-03-19 stsp free(id_str);
1941 984065c8 2020-03-19 stsp id_str = NULL;
1942 984065c8 2020-03-19 stsp }
1943 7848a0e1 2020-03-19 stsp
1944 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1945 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1946 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1947 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1948 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1949 7848a0e1 2020-03-19 stsp char *remote_refname;
1950 7848a0e1 2020-03-19 stsp
1951 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1952 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1953 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1954 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1955 0e4002ca 2020-03-21 stsp if (error)
1956 0e4002ca 2020-03-21 stsp goto done;
1957 0e4002ca 2020-03-21 stsp continue;
1958 0e4002ca 2020-03-21 stsp }
1959 0e4002ca 2020-03-21 stsp
1960 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1961 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1962 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1963 7848a0e1 2020-03-19 stsp if (error) {
1964 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1965 7848a0e1 2020-03-19 stsp goto done;
1966 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1967 6338a6a1 2020-03-21 stsp repo);
1968 7848a0e1 2020-03-19 stsp if (error)
1969 7848a0e1 2020-03-19 stsp goto done;
1970 7848a0e1 2020-03-19 stsp } else {
1971 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1972 db6d8ad8 2020-03-21 stsp verbosity, repo);
1973 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1974 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1975 9f142382 2020-03-21 stsp error = unlock_err;
1976 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1977 7848a0e1 2020-03-19 stsp if (error)
1978 7848a0e1 2020-03-19 stsp goto done;
1979 7848a0e1 2020-03-19 stsp }
1980 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1981 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1982 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1983 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1984 7848a0e1 2020-03-19 stsp goto done;
1985 7848a0e1 2020-03-19 stsp }
1986 7848a0e1 2020-03-19 stsp
1987 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1988 7848a0e1 2020-03-19 stsp if (error) {
1989 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1990 7848a0e1 2020-03-19 stsp goto done;
1991 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1992 688f11b3 2020-03-21 stsp verbosity, repo);
1993 7848a0e1 2020-03-19 stsp if (error)
1994 7848a0e1 2020-03-19 stsp goto done;
1995 7848a0e1 2020-03-19 stsp } else {
1996 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1997 db6d8ad8 2020-03-21 stsp verbosity, repo);
1998 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1999 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2000 9f142382 2020-03-21 stsp error = unlock_err;
2001 7848a0e1 2020-03-19 stsp got_ref_close(ref);
2002 7848a0e1 2020-03-19 stsp if (error)
2003 7848a0e1 2020-03-19 stsp goto done;
2004 7848a0e1 2020-03-19 stsp }
2005 2ec30c80 2020-03-20 stsp
2006 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
2007 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
2008 2ec30c80 2020-03-20 stsp if (error) {
2009 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
2010 2ec30c80 2020-03-20 stsp goto done;
2011 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
2012 6338a6a1 2020-03-21 stsp repo);
2013 2ec30c80 2020-03-20 stsp if (error)
2014 2ec30c80 2020-03-20 stsp goto done;
2015 9f142382 2020-03-21 stsp } else {
2016 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
2017 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
2018 9f142382 2020-03-21 stsp error = unlock_err;
2019 2ec30c80 2020-03-20 stsp got_ref_close(ref);
2020 9f142382 2020-03-21 stsp }
2021 7848a0e1 2020-03-19 stsp }
2022 7848a0e1 2020-03-19 stsp }
2023 f1bcca34 2020-03-25 stsp if (delete_refs) {
2024 3789fd73 2020-03-26 stsp error = delete_missing_refs(&refs, &symrefs, remote,
2025 3789fd73 2020-03-26 stsp verbosity, repo);
2026 f1bcca34 2020-03-25 stsp if (error)
2027 f1bcca34 2020-03-25 stsp goto done;
2028 f1bcca34 2020-03-25 stsp }
2029 f1bcca34 2020-03-25 stsp
2030 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
2031 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
2032 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2033 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
2034 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
2035 f1bcca34 2020-03-25 stsp const char *target = pe->data;
2036 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
2037 f1bcca34 2020-03-25 stsp
2038 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
2039 f1bcca34 2020-03-25 stsp continue;
2040 f1bcca34 2020-03-25 stsp
2041 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
2042 f1bcca34 2020-03-25 stsp continue;
2043 f1bcca34 2020-03-25 stsp
2044 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
2045 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
2046 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2047 f1bcca34 2020-03-25 stsp goto done;
2048 f1bcca34 2020-03-25 stsp }
2049 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
2050 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
2051 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
2052 f1bcca34 2020-03-25 stsp free(remote_refname);
2053 f1bcca34 2020-03-25 stsp goto done;
2054 f1bcca34 2020-03-25 stsp }
2055 f1bcca34 2020-03-25 stsp
2056 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
2057 f1bcca34 2020-03-25 stsp 0);
2058 f1bcca34 2020-03-25 stsp if (error) {
2059 f1bcca34 2020-03-25 stsp free(remote_refname);
2060 f1bcca34 2020-03-25 stsp free(remote_target);
2061 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
2062 f1bcca34 2020-03-25 stsp error = NULL;
2063 f1bcca34 2020-03-25 stsp continue;
2064 f1bcca34 2020-03-25 stsp }
2065 f1bcca34 2020-03-25 stsp goto done;
2066 f1bcca34 2020-03-25 stsp }
2067 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
2068 f1bcca34 2020-03-25 stsp verbosity, repo);
2069 f1bcca34 2020-03-25 stsp free(remote_refname);
2070 f1bcca34 2020-03-25 stsp free(remote_target);
2071 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
2072 f1bcca34 2020-03-25 stsp if (error)
2073 f1bcca34 2020-03-25 stsp goto done;
2074 f1bcca34 2020-03-25 stsp }
2075 f1bcca34 2020-03-25 stsp }
2076 7848a0e1 2020-03-19 stsp done:
2077 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
2078 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
2079 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
2080 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
2081 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
2082 9c52365f 2020-03-21 stsp }
2083 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
2084 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
2085 7848a0e1 2020-03-19 stsp if (repo)
2086 7848a0e1 2020-03-19 stsp got_repo_close(repo);
2087 7848a0e1 2020-03-19 stsp if (worktree)
2088 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
2089 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
2090 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2091 7848a0e1 2020-03-19 stsp free(pe->data);
2092 7848a0e1 2020-03-19 stsp }
2093 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2094 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2095 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2096 7848a0e1 2020-03-19 stsp free(pe->data);
2097 7848a0e1 2020-03-19 stsp }
2098 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2099 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2100 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2101 7848a0e1 2020-03-19 stsp free(id_str);
2102 7848a0e1 2020-03-19 stsp free(cwd);
2103 7848a0e1 2020-03-19 stsp free(repo_path);
2104 7848a0e1 2020-03-19 stsp free(pack_hash);
2105 7848a0e1 2020-03-19 stsp free(proto);
2106 7848a0e1 2020-03-19 stsp free(host);
2107 7848a0e1 2020-03-19 stsp free(port);
2108 7848a0e1 2020-03-19 stsp free(server_path);
2109 7848a0e1 2020-03-19 stsp free(repo_name);
2110 7848a0e1 2020-03-19 stsp return error;
2111 7848a0e1 2020-03-19 stsp }
2112 7848a0e1 2020-03-19 stsp
2113 7848a0e1 2020-03-19 stsp
2114 7848a0e1 2020-03-19 stsp __dead static void
2115 2ab43947 2020-03-18 stsp usage_checkout(void)
2116 2ab43947 2020-03-18 stsp {
2117 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2118 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2119 2ab43947 2020-03-18 stsp exit(1);
2120 2ab43947 2020-03-18 stsp }
2121 2ab43947 2020-03-18 stsp
2122 2ab43947 2020-03-18 stsp static void
2123 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2124 2ab43947 2020-03-18 stsp {
2125 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2126 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2127 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2128 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2129 2ab43947 2020-03-18 stsp getprogname());
2130 2ab43947 2020-03-18 stsp }
2131 2ab43947 2020-03-18 stsp
2132 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2133 2ab43947 2020-03-18 stsp const char *worktree_path;
2134 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2135 2ab43947 2020-03-18 stsp };
2136 2ab43947 2020-03-18 stsp
2137 2ab43947 2020-03-18 stsp static const struct got_error *
2138 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2139 2ab43947 2020-03-18 stsp {
2140 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2141 2ab43947 2020-03-18 stsp
2142 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2143 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2144 2ab43947 2020-03-18 stsp return NULL;
2145 2ab43947 2020-03-18 stsp
2146 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2147 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2148 2ab43947 2020-03-18 stsp return NULL;
2149 2ab43947 2020-03-18 stsp }
2150 2ab43947 2020-03-18 stsp
2151 2ab43947 2020-03-18 stsp while (path[0] == '/')
2152 2ab43947 2020-03-18 stsp path++;
2153 2ab43947 2020-03-18 stsp
2154 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2155 2ab43947 2020-03-18 stsp return NULL;
2156 2ab43947 2020-03-18 stsp }
2157 2ab43947 2020-03-18 stsp
2158 2ab43947 2020-03-18 stsp static const struct got_error *
2159 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2160 2ab43947 2020-03-18 stsp {
2161 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2162 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2163 2ab43947 2020-03-18 stsp return NULL;
2164 2ab43947 2020-03-18 stsp }
2165 2ab43947 2020-03-18 stsp
2166 2ab43947 2020-03-18 stsp static const struct got_error *
2167 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2168 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2169 2ab43947 2020-03-18 stsp struct got_repository *repo)
2170 2ab43947 2020-03-18 stsp {
2171 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2172 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2173 2ab43947 2020-03-18 stsp
2174 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2175 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2176 2ab43947 2020-03-18 stsp if (err)
2177 2ab43947 2020-03-18 stsp return err;
2178 2ab43947 2020-03-18 stsp
2179 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2180 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2181 2ab43947 2020-03-18 stsp
2182 2ab43947 2020-03-18 stsp /*
2183 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2184 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2185 2ab43947 2020-03-18 stsp *
2186 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2187 2ab43947 2020-03-18 stsp *
2188 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2189 2ab43947 2020-03-18 stsp * \ /
2190 2ab43947 2020-03-18 stsp * C E
2191 2ab43947 2020-03-18 stsp * \ /
2192 2ab43947 2020-03-18 stsp * B (yca)
2193 2ab43947 2020-03-18 stsp * |
2194 2ab43947 2020-03-18 stsp * A
2195 2ab43947 2020-03-18 stsp *
2196 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2197 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2198 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2199 2ab43947 2020-03-18 stsp */
2200 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2201 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2202 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2203 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2204 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2205 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2206 2ab43947 2020-03-18 stsp
2207 2ab43947 2020-03-18 stsp free(yca_id);
2208 2ab43947 2020-03-18 stsp return NULL;
2209 2ab43947 2020-03-18 stsp }
2210 2ab43947 2020-03-18 stsp
2211 2ab43947 2020-03-18 stsp static const struct got_error *
2212 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2213 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2214 2ab43947 2020-03-18 stsp struct got_repository *repo)
2215 2ab43947 2020-03-18 stsp {
2216 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2217 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2218 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2219 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2220 2ab43947 2020-03-18 stsp
2221 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2222 2ab43947 2020-03-18 stsp if (err)
2223 2ab43947 2020-03-18 stsp goto done;
2224 2ab43947 2020-03-18 stsp
2225 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2226 2ab43947 2020-03-18 stsp is_same_branch = 1;
2227 2ab43947 2020-03-18 stsp goto done;
2228 2ab43947 2020-03-18 stsp }
2229 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2230 2ab43947 2020-03-18 stsp is_same_branch = 1;
2231 2ab43947 2020-03-18 stsp goto done;
2232 2ab43947 2020-03-18 stsp }
2233 2ab43947 2020-03-18 stsp
2234 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2235 2ab43947 2020-03-18 stsp if (err)
2236 2ab43947 2020-03-18 stsp goto done;
2237 2ab43947 2020-03-18 stsp
2238 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2239 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2240 2ab43947 2020-03-18 stsp if (err)
2241 2ab43947 2020-03-18 stsp goto done;
2242 2ab43947 2020-03-18 stsp
2243 2ab43947 2020-03-18 stsp for (;;) {
2244 2ab43947 2020-03-18 stsp struct got_object_id *id;
2245 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2246 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2247 2ab43947 2020-03-18 stsp if (err) {
2248 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2249 2ab43947 2020-03-18 stsp err = NULL;
2250 2ab43947 2020-03-18 stsp break;
2251 2ab43947 2020-03-18 stsp }
2252 2ab43947 2020-03-18 stsp
2253 2ab43947 2020-03-18 stsp if (id) {
2254 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2255 2ab43947 2020-03-18 stsp break;
2256 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2257 2ab43947 2020-03-18 stsp is_same_branch = 1;
2258 2ab43947 2020-03-18 stsp break;
2259 2ab43947 2020-03-18 stsp }
2260 2ab43947 2020-03-18 stsp }
2261 2ab43947 2020-03-18 stsp }
2262 2ab43947 2020-03-18 stsp done:
2263 2ab43947 2020-03-18 stsp if (graph)
2264 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2265 2ab43947 2020-03-18 stsp free(head_commit_id);
2266 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2267 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2268 2ab43947 2020-03-18 stsp return err;
2269 a367ff0f 2019-05-14 stsp }
2270 8069f636 2019-01-12 stsp
2271 4ed7e80c 2018-05-20 stsp static const struct got_error *
2272 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2273 4b6c9460 2020-03-05 stsp {
2274 4b6c9460 2020-03-05 stsp static char msg[512];
2275 4b6c9460 2020-03-05 stsp const char *branch_name;
2276 4b6c9460 2020-03-05 stsp
2277 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2278 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2279 4b6c9460 2020-03-05 stsp else
2280 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2281 4b6c9460 2020-03-05 stsp
2282 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2283 4b6c9460 2020-03-05 stsp branch_name += 11;
2284 4b6c9460 2020-03-05 stsp
2285 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2286 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2287 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2288 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2289 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2290 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2291 4b6c9460 2020-03-05 stsp
2292 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2293 4b6c9460 2020-03-05 stsp }
2294 4b6c9460 2020-03-05 stsp
2295 4b6c9460 2020-03-05 stsp static const struct got_error *
2296 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2297 c09a553d 2018-03-12 stsp {
2298 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2299 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2300 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2301 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2302 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2303 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2304 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2305 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2306 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2307 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2308 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2309 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2310 f2ea84fa 2019-07-27 stsp
2311 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2312 c09a553d 2018-03-12 stsp
2313 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2314 0bb8a95e 2018-03-12 stsp switch (ch) {
2315 08573d5b 2019-05-14 stsp case 'b':
2316 08573d5b 2019-05-14 stsp branch_name = optarg;
2317 08573d5b 2019-05-14 stsp break;
2318 8069f636 2019-01-12 stsp case 'c':
2319 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2320 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2321 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2322 bb51a5b4 2020-01-13 stsp break;
2323 bb51a5b4 2020-01-13 stsp case 'E':
2324 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2325 8069f636 2019-01-12 stsp break;
2326 0bb8a95e 2018-03-12 stsp case 'p':
2327 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2328 0bb8a95e 2018-03-12 stsp break;
2329 0bb8a95e 2018-03-12 stsp default:
2330 2deda0b9 2019-03-07 stsp usage_checkout();
2331 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2332 0bb8a95e 2018-03-12 stsp }
2333 0bb8a95e 2018-03-12 stsp }
2334 0bb8a95e 2018-03-12 stsp
2335 0bb8a95e 2018-03-12 stsp argc -= optind;
2336 0bb8a95e 2018-03-12 stsp argv += optind;
2337 0bb8a95e 2018-03-12 stsp
2338 6715a751 2018-03-16 stsp #ifndef PROFILE
2339 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2340 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2341 c09a553d 2018-03-12 stsp err(1, "pledge");
2342 6715a751 2018-03-16 stsp #endif
2343 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2344 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2345 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2346 76089277 2018-04-01 stsp if (repo_path == NULL)
2347 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2348 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2349 76089277 2018-04-01 stsp if (cwd == NULL) {
2350 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2351 76089277 2018-04-01 stsp goto done;
2352 76089277 2018-04-01 stsp }
2353 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2354 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2355 230a42bd 2019-05-11 jcs if (base == NULL) {
2356 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2357 230a42bd 2019-05-11 jcs path_prefix);
2358 230a42bd 2019-05-11 jcs goto done;
2359 230a42bd 2019-05-11 jcs }
2360 230a42bd 2019-05-11 jcs } else {
2361 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2362 230a42bd 2019-05-11 jcs if (base == NULL) {
2363 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2364 230a42bd 2019-05-11 jcs repo_path);
2365 230a42bd 2019-05-11 jcs goto done;
2366 230a42bd 2019-05-11 jcs }
2367 76089277 2018-04-01 stsp }
2368 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2369 c09a553d 2018-03-12 stsp if (dotgit)
2370 c09a553d 2018-03-12 stsp *dotgit = '\0';
2371 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2372 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2373 c09a553d 2018-03-12 stsp free(cwd);
2374 76089277 2018-04-01 stsp goto done;
2375 c09a553d 2018-03-12 stsp }
2376 c09a553d 2018-03-12 stsp free(cwd);
2377 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2378 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2379 76089277 2018-04-01 stsp if (repo_path == NULL) {
2380 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2381 76089277 2018-04-01 stsp goto done;
2382 76089277 2018-04-01 stsp }
2383 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2384 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2385 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2386 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2387 b4b3a7dd 2019-07-22 stsp argv[1]);
2388 b4b3a7dd 2019-07-22 stsp goto done;
2389 b4b3a7dd 2019-07-22 stsp }
2390 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2391 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2392 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2393 b4b3a7dd 2019-07-22 stsp goto done;
2394 b4b3a7dd 2019-07-22 stsp }
2395 76089277 2018-04-01 stsp }
2396 c09a553d 2018-03-12 stsp } else
2397 c09a553d 2018-03-12 stsp usage_checkout();
2398 c09a553d 2018-03-12 stsp
2399 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2400 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2401 13bfb272 2019-05-10 jcs
2402 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2403 c09a553d 2018-03-12 stsp if (error != NULL)
2404 c02c541e 2019-03-29 stsp goto done;
2405 c02c541e 2019-03-29 stsp
2406 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2407 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2408 c530dc23 2019-07-23 stsp if (error) {
2409 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2410 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2411 c530dc23 2019-07-23 stsp goto done;
2412 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2413 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2414 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2415 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2416 c530dc23 2019-07-23 stsp goto done;
2417 c530dc23 2019-07-23 stsp }
2418 c530dc23 2019-07-23 stsp }
2419 c530dc23 2019-07-23 stsp
2420 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2421 c02c541e 2019-03-29 stsp if (error)
2422 c09a553d 2018-03-12 stsp goto done;
2423 8069f636 2019-01-12 stsp
2424 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2425 c09a553d 2018-03-12 stsp if (error != NULL)
2426 c09a553d 2018-03-12 stsp goto done;
2427 c09a553d 2018-03-12 stsp
2428 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2429 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2430 c09a553d 2018-03-12 stsp goto done;
2431 c09a553d 2018-03-12 stsp
2432 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2433 c09a553d 2018-03-12 stsp if (error != NULL)
2434 c09a553d 2018-03-12 stsp goto done;
2435 c09a553d 2018-03-12 stsp
2436 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2437 e5dc7198 2018-12-29 stsp path_prefix);
2438 e5dc7198 2018-12-29 stsp if (error != NULL)
2439 e5dc7198 2018-12-29 stsp goto done;
2440 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2441 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2442 49520a32 2018-12-29 stsp goto done;
2443 49520a32 2018-12-29 stsp }
2444 49520a32 2018-12-29 stsp
2445 8069f636 2019-01-12 stsp if (commit_id_str) {
2446 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2447 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2448 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2449 30837e32 2019-07-25 stsp if (error)
2450 8069f636 2019-01-12 stsp goto done;
2451 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2452 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2453 8069f636 2019-01-12 stsp if (error != NULL) {
2454 8069f636 2019-01-12 stsp free(commit_id);
2455 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2456 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2457 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2458 4b6c9460 2020-03-05 stsp }
2459 8069f636 2019-01-12 stsp goto done;
2460 8069f636 2019-01-12 stsp }
2461 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2462 4b6c9460 2020-03-05 stsp if (error) {
2463 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2464 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2465 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2466 4b6c9460 2020-03-05 stsp }
2467 45d344f6 2019-05-14 stsp goto done;
2468 4b6c9460 2020-03-05 stsp }
2469 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2470 8069f636 2019-01-12 stsp commit_id);
2471 8069f636 2019-01-12 stsp free(commit_id);
2472 8069f636 2019-01-12 stsp if (error)
2473 8069f636 2019-01-12 stsp goto done;
2474 8069f636 2019-01-12 stsp }
2475 8069f636 2019-01-12 stsp
2476 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2477 f2ea84fa 2019-07-27 stsp if (error)
2478 f2ea84fa 2019-07-27 stsp goto done;
2479 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2480 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2481 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2482 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2483 c09a553d 2018-03-12 stsp if (error != NULL)
2484 c09a553d 2018-03-12 stsp goto done;
2485 c09a553d 2018-03-12 stsp
2486 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2487 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2488 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2489 c09a553d 2018-03-12 stsp done:
2490 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2491 8069f636 2019-01-12 stsp free(commit_id_str);
2492 76089277 2018-04-01 stsp free(repo_path);
2493 507dc3bb 2018-12-29 stsp free(worktree_path);
2494 507dc3bb 2018-12-29 stsp return error;
2495 9627c110 2020-04-18 stsp }
2496 9627c110 2020-04-18 stsp
2497 9627c110 2020-04-18 stsp struct got_update_progress_arg {
2498 9627c110 2020-04-18 stsp int did_something;
2499 9627c110 2020-04-18 stsp int conflicts;
2500 9627c110 2020-04-18 stsp int obstructed;
2501 9627c110 2020-04-18 stsp int not_updated;
2502 9627c110 2020-04-18 stsp };
2503 9627c110 2020-04-18 stsp
2504 9627c110 2020-04-18 stsp void
2505 9627c110 2020-04-18 stsp print_update_progress_stats(struct got_update_progress_arg *upa)
2506 9627c110 2020-04-18 stsp {
2507 9627c110 2020-04-18 stsp if (!upa->did_something)
2508 9627c110 2020-04-18 stsp return;
2509 9627c110 2020-04-18 stsp
2510 9627c110 2020-04-18 stsp if (upa->conflicts > 0)
2511 9627c110 2020-04-18 stsp printf("Files with new merge conflicts: %d\n", upa->conflicts);
2512 9627c110 2020-04-18 stsp if (upa->obstructed > 0)
2513 9627c110 2020-04-18 stsp printf("File paths obstructed by a non-regular file: %d\n",
2514 9627c110 2020-04-18 stsp upa->obstructed);
2515 9627c110 2020-04-18 stsp if (upa->not_updated > 0)
2516 9627c110 2020-04-18 stsp printf("Files not updated because of existing merge "
2517 9627c110 2020-04-18 stsp "conflicts: %d\n", upa->not_updated);
2518 507dc3bb 2018-12-29 stsp }
2519 507dc3bb 2018-12-29 stsp
2520 507dc3bb 2018-12-29 stsp __dead static void
2521 507dc3bb 2018-12-29 stsp usage_update(void)
2522 507dc3bb 2018-12-29 stsp {
2523 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2524 507dc3bb 2018-12-29 stsp getprogname());
2525 507dc3bb 2018-12-29 stsp exit(1);
2526 507dc3bb 2018-12-29 stsp }
2527 507dc3bb 2018-12-29 stsp
2528 1ee397ad 2019-07-12 stsp static const struct got_error *
2529 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2530 507dc3bb 2018-12-29 stsp {
2531 9627c110 2020-04-18 stsp struct got_update_progress_arg *upa = arg;
2532 784955db 2019-01-12 stsp
2533 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2534 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2535 1ee397ad 2019-07-12 stsp return NULL;
2536 507dc3bb 2018-12-29 stsp
2537 9627c110 2020-04-18 stsp upa->did_something = 1;
2538 a484d721 2019-06-10 stsp
2539 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2540 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2541 1ee397ad 2019-07-12 stsp return NULL;
2542 a484d721 2019-06-10 stsp
2543 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CONFLICT)
2544 9627c110 2020-04-18 stsp upa->conflicts++;
2545 9627c110 2020-04-18 stsp if (status == GOT_STATUS_OBSTRUCTED)
2546 9627c110 2020-04-18 stsp upa->obstructed++;
2547 9627c110 2020-04-18 stsp if (status == GOT_STATUS_CANNOT_UPDATE)
2548 9627c110 2020-04-18 stsp upa->not_updated++;
2549 9627c110 2020-04-18 stsp
2550 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2551 507dc3bb 2018-12-29 stsp path++;
2552 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2553 1ee397ad 2019-07-12 stsp return NULL;
2554 be7061eb 2018-12-30 stsp }
2555 be7061eb 2018-12-30 stsp
2556 be7061eb 2018-12-30 stsp static const struct got_error *
2557 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2558 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2559 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2560 a1fb16d8 2019-05-24 stsp {
2561 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2562 a1fb16d8 2019-05-24 stsp char *base_id_str;
2563 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2564 a1fb16d8 2019-05-24 stsp
2565 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2566 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2567 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2568 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2569 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2570 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2571 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2572 a1fb16d8 2019-05-24 stsp }
2573 a1fb16d8 2019-05-24 stsp
2574 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2575 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2576 a1fb16d8 2019-05-24 stsp if (err) {
2577 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2578 a1fb16d8 2019-05-24 stsp return err;
2579 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2580 a1fb16d8 2019-05-24 stsp }
2581 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2582 a1fb16d8 2019-05-24 stsp return NULL;
2583 a1fb16d8 2019-05-24 stsp
2584 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2585 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2586 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2587 a1fb16d8 2019-05-24 stsp if (err)
2588 a1fb16d8 2019-05-24 stsp return err;
2589 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2590 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2591 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2592 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2593 0ebf8283 2019-07-24 stsp return NULL;
2594 0ebf8283 2019-07-24 stsp }
2595 0ebf8283 2019-07-24 stsp
2596 0ebf8283 2019-07-24 stsp static const struct got_error *
2597 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2598 0ebf8283 2019-07-24 stsp {
2599 0ebf8283 2019-07-24 stsp const struct got_error *err;
2600 0ebf8283 2019-07-24 stsp int in_progress;
2601 0ebf8283 2019-07-24 stsp
2602 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2603 0ebf8283 2019-07-24 stsp if (err)
2604 0ebf8283 2019-07-24 stsp return err;
2605 0ebf8283 2019-07-24 stsp if (in_progress)
2606 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2607 0ebf8283 2019-07-24 stsp
2608 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2609 0ebf8283 2019-07-24 stsp if (err)
2610 0ebf8283 2019-07-24 stsp return err;
2611 0ebf8283 2019-07-24 stsp if (in_progress)
2612 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2613 0ebf8283 2019-07-24 stsp
2614 a1fb16d8 2019-05-24 stsp return NULL;
2615 a5edda0a 2019-07-27 stsp }
2616 a5edda0a 2019-07-27 stsp
2617 a5edda0a 2019-07-27 stsp static const struct got_error *
2618 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2619 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2620 a5edda0a 2019-07-27 stsp {
2621 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2622 a5edda0a 2019-07-27 stsp char *path;
2623 a5edda0a 2019-07-27 stsp int i;
2624 a5edda0a 2019-07-27 stsp
2625 a5edda0a 2019-07-27 stsp if (argc == 0) {
2626 a5edda0a 2019-07-27 stsp path = strdup("");
2627 a5edda0a 2019-07-27 stsp if (path == NULL)
2628 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2629 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2630 a5edda0a 2019-07-27 stsp }
2631 a5edda0a 2019-07-27 stsp
2632 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2633 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2634 a5edda0a 2019-07-27 stsp if (err)
2635 a5edda0a 2019-07-27 stsp break;
2636 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2637 a5edda0a 2019-07-27 stsp if (err) {
2638 a5edda0a 2019-07-27 stsp free(path);
2639 a5edda0a 2019-07-27 stsp break;
2640 a5edda0a 2019-07-27 stsp }
2641 a5edda0a 2019-07-27 stsp }
2642 a5edda0a 2019-07-27 stsp
2643 a5edda0a 2019-07-27 stsp return err;
2644 a1fb16d8 2019-05-24 stsp }
2645 a1fb16d8 2019-05-24 stsp
2646 a1fb16d8 2019-05-24 stsp static const struct got_error *
2647 fa51e947 2020-03-27 stsp wrap_not_worktree_error(const struct got_error *orig_err,
2648 fa51e947 2020-03-27 stsp const char *cmdname, const char *path)
2649 fa51e947 2020-03-27 stsp {
2650 fa51e947 2020-03-27 stsp const struct got_error *err;
2651 fa51e947 2020-03-27 stsp struct got_repository *repo;
2652 fa51e947 2020-03-27 stsp static char msg[512];
2653 fa51e947 2020-03-27 stsp
2654 fa51e947 2020-03-27 stsp err = got_repo_open(&repo, path, NULL);
2655 fa51e947 2020-03-27 stsp if (err)
2656 fa51e947 2020-03-27 stsp return orig_err;
2657 fa51e947 2020-03-27 stsp
2658 fa51e947 2020-03-27 stsp snprintf(msg, sizeof(msg),
2659 fa51e947 2020-03-27 stsp "'got %s' needs a work tree in addition to a git repository\n"
2660 fa51e947 2020-03-27 stsp "Work trees can be checked out from this Git repository with "
2661 fa51e947 2020-03-27 stsp "'got checkout'.\n"
2662 fa51e947 2020-03-27 stsp "The got(1) manual page contains more information.", cmdname);
2663 fa51e947 2020-03-27 stsp err = got_error_msg(GOT_ERR_NOT_WORKTREE, msg);
2664 fa51e947 2020-03-27 stsp got_repo_close(repo);
2665 fa51e947 2020-03-27 stsp return err;
2666 fa51e947 2020-03-27 stsp }
2667 fa51e947 2020-03-27 stsp
2668 fa51e947 2020-03-27 stsp static const struct got_error *
2669 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2670 507dc3bb 2018-12-29 stsp {
2671 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2672 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2673 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2674 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2675 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2676 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2677 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2678 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2679 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2680 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2681 9627c110 2020-04-18 stsp int ch;
2682 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
2683 507dc3bb 2018-12-29 stsp
2684 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2685 f2ea84fa 2019-07-27 stsp
2686 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2687 507dc3bb 2018-12-29 stsp switch (ch) {
2688 024e9686 2019-05-14 stsp case 'b':
2689 024e9686 2019-05-14 stsp branch_name = optarg;
2690 024e9686 2019-05-14 stsp break;
2691 507dc3bb 2018-12-29 stsp case 'c':
2692 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2693 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2694 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2695 507dc3bb 2018-12-29 stsp break;
2696 507dc3bb 2018-12-29 stsp default:
2697 2deda0b9 2019-03-07 stsp usage_update();
2698 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2699 507dc3bb 2018-12-29 stsp }
2700 507dc3bb 2018-12-29 stsp }
2701 507dc3bb 2018-12-29 stsp
2702 507dc3bb 2018-12-29 stsp argc -= optind;
2703 507dc3bb 2018-12-29 stsp argv += optind;
2704 507dc3bb 2018-12-29 stsp
2705 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2706 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2707 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2708 507dc3bb 2018-12-29 stsp err(1, "pledge");
2709 507dc3bb 2018-12-29 stsp #endif
2710 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2711 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2712 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2713 c4cdcb68 2019-04-03 stsp goto done;
2714 c4cdcb68 2019-04-03 stsp }
2715 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2716 fa51e947 2020-03-27 stsp if (error) {
2717 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
2718 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "update",
2719 fa51e947 2020-03-27 stsp worktree_path);
2720 7d5807f4 2019-07-11 stsp goto done;
2721 fa51e947 2020-03-27 stsp }
2722 7d5807f4 2019-07-11 stsp
2723 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2724 f2ea84fa 2019-07-27 stsp if (error)
2725 f2ea84fa 2019-07-27 stsp goto done;
2726 507dc3bb 2018-12-29 stsp
2727 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2728 c9956ddf 2019-09-08 stsp NULL);
2729 507dc3bb 2018-12-29 stsp if (error != NULL)
2730 507dc3bb 2018-12-29 stsp goto done;
2731 507dc3bb 2018-12-29 stsp
2732 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2733 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2734 087fb88c 2019-08-04 stsp if (error)
2735 087fb88c 2019-08-04 stsp goto done;
2736 087fb88c 2019-08-04 stsp
2737 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2738 0266afb7 2019-01-04 stsp if (error)
2739 0266afb7 2019-01-04 stsp goto done;
2740 0266afb7 2019-01-04 stsp
2741 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2742 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2743 024e9686 2019-05-14 stsp if (error != NULL)
2744 024e9686 2019-05-14 stsp goto done;
2745 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2746 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2747 9c4b8182 2019-01-02 stsp if (error != NULL)
2748 9c4b8182 2019-01-02 stsp goto done;
2749 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2750 507dc3bb 2018-12-29 stsp if (error != NULL)
2751 507dc3bb 2018-12-29 stsp goto done;
2752 507dc3bb 2018-12-29 stsp } else {
2753 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2754 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2755 04f57cb3 2019-07-25 stsp free(commit_id_str);
2756 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2757 30837e32 2019-07-25 stsp if (error)
2758 a297e751 2019-07-11 stsp goto done;
2759 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2760 a297e751 2019-07-11 stsp if (error)
2761 507dc3bb 2018-12-29 stsp goto done;
2762 507dc3bb 2018-12-29 stsp }
2763 35c965b2 2018-12-29 stsp
2764 a1fb16d8 2019-05-24 stsp if (branch_name) {
2765 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2766 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2767 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2768 f2ea84fa 2019-07-27 stsp continue;
2769 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2770 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2771 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2772 024e9686 2019-05-14 stsp goto done;
2773 024e9686 2019-05-14 stsp }
2774 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2775 024e9686 2019-05-14 stsp if (error)
2776 024e9686 2019-05-14 stsp goto done;
2777 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2778 3aef623b 2019-10-15 stsp repo);
2779 a367ff0f 2019-05-14 stsp free(head_commit_id);
2780 024e9686 2019-05-14 stsp if (error != NULL)
2781 024e9686 2019-05-14 stsp goto done;
2782 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2783 a367ff0f 2019-05-14 stsp if (error)
2784 a367ff0f 2019-05-14 stsp goto done;
2785 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2786 024e9686 2019-05-14 stsp if (error)
2787 024e9686 2019-05-14 stsp goto done;
2788 024e9686 2019-05-14 stsp } else {
2789 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2790 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2791 a367ff0f 2019-05-14 stsp if (error != NULL) {
2792 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2793 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2794 024e9686 2019-05-14 stsp goto done;
2795 a367ff0f 2019-05-14 stsp }
2796 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2797 a367ff0f 2019-05-14 stsp if (error)
2798 a367ff0f 2019-05-14 stsp goto done;
2799 024e9686 2019-05-14 stsp }
2800 507dc3bb 2018-12-29 stsp
2801 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2802 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2803 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2804 507dc3bb 2018-12-29 stsp commit_id);
2805 507dc3bb 2018-12-29 stsp if (error)
2806 507dc3bb 2018-12-29 stsp goto done;
2807 507dc3bb 2018-12-29 stsp }
2808 507dc3bb 2018-12-29 stsp
2809 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
2810 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2811 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
2812 507dc3bb 2018-12-29 stsp if (error != NULL)
2813 507dc3bb 2018-12-29 stsp goto done;
2814 9c4b8182 2019-01-02 stsp
2815 9627c110 2020-04-18 stsp if (upa.did_something)
2816 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2817 784955db 2019-01-12 stsp else
2818 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2819 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
2820 507dc3bb 2018-12-29 stsp done:
2821 c09a553d 2018-03-12 stsp free(worktree_path);
2822 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2823 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2824 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2825 507dc3bb 2018-12-29 stsp free(commit_id);
2826 9c4b8182 2019-01-02 stsp free(commit_id_str);
2827 c09a553d 2018-03-12 stsp return error;
2828 c09a553d 2018-03-12 stsp }
2829 c09a553d 2018-03-12 stsp
2830 f42b1b34 2018-03-12 stsp static const struct got_error *
2831 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2832 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2833 63035f9f 2019-10-06 stsp struct got_repository *repo)
2834 5c860e29 2018-03-12 stsp {
2835 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2836 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2837 79109fed 2018-03-27 stsp
2838 44392932 2019-08-25 stsp if (blob_id1) {
2839 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2840 44392932 2019-08-25 stsp if (err)
2841 44392932 2019-08-25 stsp goto done;
2842 44392932 2019-08-25 stsp }
2843 79109fed 2018-03-27 stsp
2844 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2845 44392932 2019-08-25 stsp if (err)
2846 44392932 2019-08-25 stsp goto done;
2847 79109fed 2018-03-27 stsp
2848 44392932 2019-08-25 stsp while (path[0] == '/')
2849 44392932 2019-08-25 stsp path++;
2850 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2851 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2852 44392932 2019-08-25 stsp done:
2853 44392932 2019-08-25 stsp if (blob1)
2854 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2855 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2856 44392932 2019-08-25 stsp return err;
2857 44392932 2019-08-25 stsp }
2858 44392932 2019-08-25 stsp
2859 44392932 2019-08-25 stsp static const struct got_error *
2860 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2861 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2862 63035f9f 2019-10-06 stsp struct got_repository *repo)
2863 44392932 2019-08-25 stsp {
2864 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2865 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2866 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2867 44392932 2019-08-25 stsp
2868 44392932 2019-08-25 stsp if (tree_id1) {
2869 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2870 79109fed 2018-03-27 stsp if (err)
2871 44392932 2019-08-25 stsp goto done;
2872 79109fed 2018-03-27 stsp }
2873 79109fed 2018-03-27 stsp
2874 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2875 0f2b3dca 2018-12-22 stsp if (err)
2876 0f2b3dca 2018-12-22 stsp goto done;
2877 0f2b3dca 2018-12-22 stsp
2878 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2879 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2880 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2881 44392932 2019-08-25 stsp while (path[0] == '/')
2882 44392932 2019-08-25 stsp path++;
2883 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2884 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2885 0f2b3dca 2018-12-22 stsp done:
2886 79109fed 2018-03-27 stsp if (tree1)
2887 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2888 366e0a5f 2019-10-10 stsp if (tree2)
2889 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2890 44392932 2019-08-25 stsp return err;
2891 44392932 2019-08-25 stsp }
2892 44392932 2019-08-25 stsp
2893 44392932 2019-08-25 stsp static const struct got_error *
2894 0208f208 2020-05-05 stsp get_changed_paths(struct got_pathlist_head *paths,
2895 0208f208 2020-05-05 stsp struct got_commit_object *commit, struct got_repository *repo)
2896 0208f208 2020-05-05 stsp {
2897 0208f208 2020-05-05 stsp const struct got_error *err = NULL;
2898 0208f208 2020-05-05 stsp struct got_object_id *tree_id1 = NULL, *tree_id2 = NULL;
2899 0208f208 2020-05-05 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2900 0208f208 2020-05-05 stsp struct got_object_qid *qid;
2901 0208f208 2020-05-05 stsp
2902 0208f208 2020-05-05 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2903 0208f208 2020-05-05 stsp if (qid != NULL) {
2904 0208f208 2020-05-05 stsp struct got_commit_object *pcommit;
2905 0208f208 2020-05-05 stsp err = got_object_open_as_commit(&pcommit, repo,
2906 0208f208 2020-05-05 stsp qid->id);
2907 0208f208 2020-05-05 stsp if (err)
2908 0208f208 2020-05-05 stsp return err;
2909 0208f208 2020-05-05 stsp
2910 0208f208 2020-05-05 stsp tree_id1 = got_object_commit_get_tree_id(pcommit);
2911 0208f208 2020-05-05 stsp got_object_commit_close(pcommit);
2912 0208f208 2020-05-05 stsp
2913 0208f208 2020-05-05 stsp }
2914 0208f208 2020-05-05 stsp
2915 0208f208 2020-05-05 stsp if (tree_id1) {
2916 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2917 0208f208 2020-05-05 stsp if (err)
2918 0208f208 2020-05-05 stsp goto done;
2919 0208f208 2020-05-05 stsp }
2920 0208f208 2020-05-05 stsp
2921 0208f208 2020-05-05 stsp tree_id2 = got_object_commit_get_tree_id(commit);
2922 0208f208 2020-05-05 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2923 0208f208 2020-05-05 stsp if (err)
2924 0208f208 2020-05-05 stsp goto done;
2925 0208f208 2020-05-05 stsp
2926 0208f208 2020-05-05 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
2927 0208f208 2020-05-05 stsp got_diff_tree_collect_changed_paths, paths, 0);
2928 0208f208 2020-05-05 stsp done:
2929 0208f208 2020-05-05 stsp if (tree1)
2930 0208f208 2020-05-05 stsp got_object_tree_close(tree1);
2931 0208f208 2020-05-05 stsp if (tree2)
2932 0208f208 2020-05-05 stsp got_object_tree_close(tree2);
2933 0208f208 2020-05-05 stsp return err;
2934 0208f208 2020-05-05 stsp }
2935 0208f208 2020-05-05 stsp
2936 0208f208 2020-05-05 stsp static const struct got_error *
2937 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2938 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2939 44392932 2019-08-25 stsp {
2940 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2941 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2942 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2943 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2944 44392932 2019-08-25 stsp struct got_object_qid *qid;
2945 44392932 2019-08-25 stsp
2946 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2947 44392932 2019-08-25 stsp if (qid != NULL) {
2948 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2949 44392932 2019-08-25 stsp qid->id);
2950 44392932 2019-08-25 stsp if (err)
2951 44392932 2019-08-25 stsp return err;
2952 44392932 2019-08-25 stsp }
2953 44392932 2019-08-25 stsp
2954 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2955 44392932 2019-08-25 stsp int obj_type;
2956 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2957 44392932 2019-08-25 stsp if (err)
2958 44392932 2019-08-25 stsp goto done;
2959 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2960 44392932 2019-08-25 stsp if (err) {
2961 44392932 2019-08-25 stsp free(obj_id2);
2962 44392932 2019-08-25 stsp goto done;
2963 44392932 2019-08-25 stsp }
2964 44392932 2019-08-25 stsp if (pcommit) {
2965 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2966 44392932 2019-08-25 stsp qid->id, path);
2967 44392932 2019-08-25 stsp if (err) {
2968 2e8c69d1 2020-05-04 stsp if (err->code != GOT_ERR_NO_TREE_ENTRY) {
2969 2e8c69d1 2020-05-04 stsp free(obj_id2);
2970 2e8c69d1 2020-05-04 stsp goto done;
2971 2e8c69d1 2020-05-04 stsp }
2972 2e8c69d1 2020-05-04 stsp } else {
2973 2e8c69d1 2020-05-04 stsp err = got_object_id_str(&id_str1, obj_id1);
2974 2e8c69d1 2020-05-04 stsp if (err) {
2975 2e8c69d1 2020-05-04 stsp free(obj_id2);
2976 2e8c69d1 2020-05-04 stsp goto done;
2977 2e8c69d1 2020-05-04 stsp }
2978 44392932 2019-08-25 stsp }
2979 44392932 2019-08-25 stsp }
2980 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2981 44392932 2019-08-25 stsp if (err) {
2982 44392932 2019-08-25 stsp free(obj_id2);
2983 44392932 2019-08-25 stsp goto done;
2984 44392932 2019-08-25 stsp }
2985 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2986 44392932 2019-08-25 stsp switch (obj_type) {
2987 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2988 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2989 63035f9f 2019-10-06 stsp 0, repo);
2990 44392932 2019-08-25 stsp break;
2991 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2992 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2993 63035f9f 2019-10-06 stsp 0, repo);
2994 44392932 2019-08-25 stsp break;
2995 44392932 2019-08-25 stsp default:
2996 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2997 44392932 2019-08-25 stsp break;
2998 44392932 2019-08-25 stsp }
2999 44392932 2019-08-25 stsp free(obj_id1);
3000 44392932 2019-08-25 stsp free(obj_id2);
3001 44392932 2019-08-25 stsp } else {
3002 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
3003 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
3004 44392932 2019-08-25 stsp if (err)
3005 44392932 2019-08-25 stsp goto done;
3006 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
3007 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
3008 44392932 2019-08-25 stsp if (err)
3009 44392932 2019-08-25 stsp goto done;
3010 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
3011 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
3012 44392932 2019-08-25 stsp }
3013 44392932 2019-08-25 stsp done:
3014 0f2b3dca 2018-12-22 stsp free(id_str1);
3015 0f2b3dca 2018-12-22 stsp free(id_str2);
3016 44392932 2019-08-25 stsp if (pcommit)
3017 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
3018 79109fed 2018-03-27 stsp return err;
3019 79109fed 2018-03-27 stsp }
3020 79109fed 2018-03-27 stsp
3021 4bb494d5 2018-06-16 stsp static char *
3022 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
3023 6c281f94 2018-06-11 stsp {
3024 09867e48 2019-08-13 stsp struct tm mytm, *tm;
3025 09867e48 2019-08-13 stsp char *p, *s;
3026 09867e48 2019-08-13 stsp
3027 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
3028 09867e48 2019-08-13 stsp if (tm == NULL)
3029 09867e48 2019-08-13 stsp return NULL;
3030 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
3031 09867e48 2019-08-13 stsp if (s == NULL)
3032 09867e48 2019-08-13 stsp return NULL;
3033 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
3034 6c281f94 2018-06-11 stsp if (p)
3035 6c281f94 2018-06-11 stsp *p = '\0';
3036 6c281f94 2018-06-11 stsp return s;
3037 6c281f94 2018-06-11 stsp }
3038 dc424a06 2019-08-07 stsp
3039 6841bf13 2019-11-29 kn static const struct got_error *
3040 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
3041 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
3042 6841bf13 2019-11-29 kn {
3043 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
3044 6841bf13 2019-11-29 kn regmatch_t regmatch;
3045 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
3046 6841bf13 2019-11-29 kn
3047 6841bf13 2019-11-29 kn *have_match = 0;
3048 6841bf13 2019-11-29 kn
3049 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
3050 6841bf13 2019-11-29 kn if (err)
3051 6841bf13 2019-11-29 kn return err;
3052 6841bf13 2019-11-29 kn
3053 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
3054 6841bf13 2019-11-29 kn if (err)
3055 6841bf13 2019-11-29 kn goto done;
3056 6841bf13 2019-11-29 kn
3057 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
3058 6841bf13 2019-11-29 kn *have_match = 1;
3059 6841bf13 2019-11-29 kn done:
3060 6841bf13 2019-11-29 kn free(id_str);
3061 6841bf13 2019-11-29 kn free(logmsg);
3062 6841bf13 2019-11-29 kn return err;
3063 6841bf13 2019-11-29 kn }
3064 6841bf13 2019-11-29 kn
3065 0208f208 2020-05-05 stsp static void
3066 0208f208 2020-05-05 stsp match_changed_paths(int *have_match, struct got_pathlist_head *changed_paths,
3067 0208f208 2020-05-05 stsp regex_t *regex)
3068 0208f208 2020-05-05 stsp {
3069 0208f208 2020-05-05 stsp regmatch_t regmatch;
3070 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3071 0208f208 2020-05-05 stsp
3072 0208f208 2020-05-05 stsp *have_match = 0;
3073 0208f208 2020-05-05 stsp
3074 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3075 0208f208 2020-05-05 stsp if (regexec(regex, pe->path, 1, &regmatch, 0) == 0) {
3076 0208f208 2020-05-05 stsp *have_match = 1;
3077 0208f208 2020-05-05 stsp break;
3078 0208f208 2020-05-05 stsp }
3079 0208f208 2020-05-05 stsp }
3080 0208f208 2020-05-05 stsp }
3081 0208f208 2020-05-05 stsp
3082 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
3083 6c281f94 2018-06-11 stsp
3084 79109fed 2018-03-27 stsp static const struct got_error *
3085 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
3086 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path,
3087 0208f208 2020-05-05 stsp struct got_pathlist_head *changed_paths, int show_patch,
3088 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
3089 79109fed 2018-03-27 stsp {
3090 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
3091 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
3092 6c281f94 2018-06-11 stsp char datebuf[26];
3093 45d799e2 2018-12-23 stsp time_t committer_time;
3094 45d799e2 2018-12-23 stsp const char *author, *committer;
3095 199a4027 2019-02-02 stsp char *refs_str = NULL;
3096 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
3097 5c860e29 2018-03-12 stsp
3098 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
3099 199a4027 2019-02-02 stsp char *s;
3100 199a4027 2019-02-02 stsp const char *name;
3101 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
3102 a436ad14 2019-08-13 stsp int cmp;
3103 a436ad14 2019-08-13 stsp
3104 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
3105 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
3106 d9498b20 2019-02-05 stsp continue;
3107 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
3108 199a4027 2019-02-02 stsp name += 5;
3109 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
3110 7143d404 2019-03-12 stsp continue;
3111 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
3112 e34f9ed6 2019-02-02 stsp name += 6;
3113 4343a07f 2020-04-24 stsp if (strncmp(name, "remotes/", 8) == 0) {
3114 141c2bff 2019-02-04 stsp name += 8;
3115 4343a07f 2020-04-24 stsp s = strstr(name, "/" GOT_REF_HEAD);
3116 4343a07f 2020-04-24 stsp if (s != NULL && s[strlen(s)] == '\0')
3117 4343a07f 2020-04-24 stsp continue;
3118 4343a07f 2020-04-24 stsp }
3119 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
3120 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
3121 5d844a1e 2019-08-13 stsp if (err) {
3122 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
3123 5d844a1e 2019-08-13 stsp return err;
3124 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
3125 5d844a1e 2019-08-13 stsp err = NULL;
3126 5d844a1e 2019-08-13 stsp tag = NULL;
3127 5d844a1e 2019-08-13 stsp }
3128 a436ad14 2019-08-13 stsp }
3129 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
3130 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
3131 a436ad14 2019-08-13 stsp if (tag)
3132 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
3133 a436ad14 2019-08-13 stsp if (cmp != 0)
3134 a436ad14 2019-08-13 stsp continue;
3135 199a4027 2019-02-02 stsp s = refs_str;
3136 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
3137 199a4027 2019-02-02 stsp name) == -1) {
3138 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3139 199a4027 2019-02-02 stsp free(s);
3140 34ca4898 2019-08-13 stsp return err;
3141 199a4027 2019-02-02 stsp }
3142 199a4027 2019-02-02 stsp free(s);
3143 199a4027 2019-02-02 stsp }
3144 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
3145 8bf5b3c9 2018-03-17 stsp if (err)
3146 8bf5b3c9 2018-03-17 stsp return err;
3147 788c352e 2018-06-16 stsp
3148 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
3149 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
3150 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
3151 832c249c 2018-06-10 stsp free(id_str);
3152 d3d493d7 2019-02-21 stsp id_str = NULL;
3153 d3d493d7 2019-02-21 stsp free(refs_str);
3154 d3d493d7 2019-02-21 stsp refs_str = NULL;
3155 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
3156 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
3157 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
3158 09867e48 2019-08-13 stsp if (datestr)
3159 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
3160 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
3161 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
3162 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
3163 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
3164 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
3165 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
3166 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
3167 3fe1abad 2018-06-10 stsp int n = 1;
3168 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3169 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
3170 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
3171 a0603db2 2018-06-10 stsp if (err)
3172 a0603db2 2018-06-10 stsp return err;
3173 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
3174 a0603db2 2018-06-10 stsp free(id_str);
3175 a0603db2 2018-06-10 stsp }
3176 a0603db2 2018-06-10 stsp }
3177 832c249c 2018-06-10 stsp
3178 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
3179 5943eee2 2019-08-13 stsp if (err)
3180 5943eee2 2019-08-13 stsp return err;
3181 8bf5b3c9 2018-03-17 stsp
3182 621015ac 2018-07-23 stsp logmsg = logmsg0;
3183 832c249c 2018-06-10 stsp do {
3184 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
3185 832c249c 2018-06-10 stsp if (line)
3186 832c249c 2018-06-10 stsp printf(" %s\n", line);
3187 832c249c 2018-06-10 stsp } while (line);
3188 621015ac 2018-07-23 stsp free(logmsg0);
3189 832c249c 2018-06-10 stsp
3190 0208f208 2020-05-05 stsp if (changed_paths) {
3191 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3192 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, changed_paths, entry) {
3193 0208f208 2020-05-05 stsp struct got_diff_changed_path *cp = pe->data;
3194 0208f208 2020-05-05 stsp printf(" %c %s\n", cp->status, pe->path);
3195 0208f208 2020-05-05 stsp }
3196 0208f208 2020-05-05 stsp printf("\n");
3197 0208f208 2020-05-05 stsp }
3198 971751ac 2018-03-27 stsp if (show_patch) {
3199 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
3200 971751ac 2018-03-27 stsp if (err == 0)
3201 971751ac 2018-03-27 stsp printf("\n");
3202 971751ac 2018-03-27 stsp }
3203 07862c20 2018-09-15 stsp
3204 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
3205 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
3206 07862c20 2018-09-15 stsp return err;
3207 f42b1b34 2018-03-12 stsp }
3208 5c860e29 2018-03-12 stsp
3209 f42b1b34 2018-03-12 stsp static const struct got_error *
3210 d1fe46f9 2020-04-18 stsp print_commits(struct got_object_id *root_id, struct got_object_id *end_id,
3211 0208f208 2020-05-05 stsp struct got_repository *repo, const char *path, int show_changed_paths,
3212 0208f208 2020-05-05 stsp int show_patch, const char *search_pattern, int diff_context, int limit,
3213 0208f208 2020-05-05 stsp int log_branches, int reverse_display_order, struct got_reflist_head *refs)
3214 f42b1b34 2018-03-12 stsp {
3215 f42b1b34 2018-03-12 stsp const struct got_error *err;
3216 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
3217 6841bf13 2019-11-29 kn regex_t regex;
3218 6841bf13 2019-11-29 kn int have_match;
3219 dbec59df 2020-04-18 stsp struct got_object_id_queue reversed_commits;
3220 dbec59df 2020-04-18 stsp struct got_object_qid *qid;
3221 dbec59df 2020-04-18 stsp struct got_commit_object *commit;
3222 0208f208 2020-05-05 stsp struct got_pathlist_head changed_paths;
3223 0208f208 2020-05-05 stsp struct got_pathlist_entry *pe;
3224 dbec59df 2020-04-18 stsp
3225 dbec59df 2020-04-18 stsp SIMPLEQ_INIT(&reversed_commits);
3226 0208f208 2020-05-05 stsp TAILQ_INIT(&changed_paths);
3227 372ccdbb 2018-06-10 stsp
3228 ccecc9fd 2020-04-18 stsp if (search_pattern && regcomp(&regex, search_pattern,
3229 ccecc9fd 2020-04-18 stsp REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
3230 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
3231 6841bf13 2019-11-29 kn
3232 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
3233 f42b1b34 2018-03-12 stsp if (err)
3234 f42b1b34 2018-03-12 stsp return err;
3235 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3236 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3237 372ccdbb 2018-06-10 stsp if (err)
3238 fcc85cad 2018-07-22 stsp goto done;
3239 656b1f76 2019-05-11 jcs for (;;) {
3240 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3241 8bf5b3c9 2018-03-17 stsp
3242 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3243 84453469 2018-11-11 stsp break;
3244 84453469 2018-11-11 stsp
3245 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3246 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3247 372ccdbb 2018-06-10 stsp if (err) {
3248 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3249 9ba79e04 2018-06-11 stsp err = NULL;
3250 ee780d5c 2020-01-04 stsp break;
3251 7e665116 2018-04-02 stsp }
3252 b43fbaa0 2018-06-11 stsp if (id == NULL)
3253 7e665116 2018-04-02 stsp break;
3254 b43fbaa0 2018-06-11 stsp
3255 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3256 b43fbaa0 2018-06-11 stsp if (err)
3257 fcc85cad 2018-07-22 stsp break;
3258 6841bf13 2019-11-29 kn
3259 0208f208 2020-05-05 stsp if (show_changed_paths) {
3260 0208f208 2020-05-05 stsp err = get_changed_paths(&changed_paths, commit, repo);
3261 0208f208 2020-05-05 stsp if (err)
3262 0208f208 2020-05-05 stsp break;
3263 0208f208 2020-05-05 stsp }
3264 0208f208 2020-05-05 stsp
3265 6841bf13 2019-11-29 kn if (search_pattern) {
3266 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3267 6841bf13 2019-11-29 kn if (err) {
3268 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3269 6841bf13 2019-11-29 kn break;
3270 6841bf13 2019-11-29 kn }
3271 0208f208 2020-05-05 stsp if (have_match == 0 && show_changed_paths)
3272 0208f208 2020-05-05 stsp match_changed_paths(&have_match,
3273 0208f208 2020-05-05 stsp &changed_paths, &regex);
3274 6841bf13 2019-11-29 kn if (have_match == 0) {
3275 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3276 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3277 0208f208 2020-05-05 stsp free((char *)pe->path);
3278 0208f208 2020-05-05 stsp free(pe->data);
3279 0208f208 2020-05-05 stsp }
3280 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3281 6841bf13 2019-11-29 kn continue;
3282 6841bf13 2019-11-29 kn }
3283 6841bf13 2019-11-29 kn }
3284 6841bf13 2019-11-29 kn
3285 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3286 dbec59df 2020-04-18 stsp err = got_object_qid_alloc(&qid, id);
3287 dbec59df 2020-04-18 stsp if (err)
3288 dbec59df 2020-04-18 stsp break;
3289 dbec59df 2020-04-18 stsp SIMPLEQ_INSERT_HEAD(&reversed_commits, qid, entry);
3290 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3291 dbec59df 2020-04-18 stsp } else {
3292 0208f208 2020-05-05 stsp err = print_commit(commit, id, repo, path,
3293 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3294 0208f208 2020-05-05 stsp show_patch, diff_context, refs);
3295 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3296 dbec59df 2020-04-18 stsp if (err)
3297 dbec59df 2020-04-18 stsp break;
3298 dbec59df 2020-04-18 stsp }
3299 dbec59df 2020-04-18 stsp if ((limit && --limit == 0) ||
3300 dbec59df 2020-04-18 stsp (end_id && got_object_id_cmp(id, end_id) == 0))
3301 7e665116 2018-04-02 stsp break;
3302 0208f208 2020-05-05 stsp
3303 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3304 0208f208 2020-05-05 stsp free((char *)pe->path);
3305 0208f208 2020-05-05 stsp free(pe->data);
3306 0208f208 2020-05-05 stsp }
3307 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3308 31cedeaf 2018-09-15 stsp }
3309 dbec59df 2020-04-18 stsp if (reverse_display_order) {
3310 dbec59df 2020-04-18 stsp SIMPLEQ_FOREACH(qid, &reversed_commits, entry) {
3311 dbec59df 2020-04-18 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
3312 dbec59df 2020-04-18 stsp if (err)
3313 dbec59df 2020-04-18 stsp break;
3314 dbec59df 2020-04-18 stsp err = print_commit(commit, qid->id, repo, path,
3315 0208f208 2020-05-05 stsp show_changed_paths ? &changed_paths : NULL,
3316 dbec59df 2020-04-18 stsp show_patch, diff_context, refs);
3317 dbec59df 2020-04-18 stsp got_object_commit_close(commit);
3318 dbec59df 2020-04-18 stsp if (err)
3319 dbec59df 2020-04-18 stsp break;
3320 dbec59df 2020-04-18 stsp }
3321 dbec59df 2020-04-18 stsp }
3322 fcc85cad 2018-07-22 stsp done:
3323 dbec59df 2020-04-18 stsp while (!SIMPLEQ_EMPTY(&reversed_commits)) {
3324 dbec59df 2020-04-18 stsp qid = SIMPLEQ_FIRST(&reversed_commits);
3325 dbec59df 2020-04-18 stsp SIMPLEQ_REMOVE_HEAD(&reversed_commits, entry);
3326 dbec59df 2020-04-18 stsp got_object_qid_free(qid);
3327 dbec59df 2020-04-18 stsp }
3328 0208f208 2020-05-05 stsp TAILQ_FOREACH(pe, &changed_paths, entry) {
3329 0208f208 2020-05-05 stsp free((char *)pe->path);
3330 0208f208 2020-05-05 stsp free(pe->data);
3331 0208f208 2020-05-05 stsp }
3332 0208f208 2020-05-05 stsp got_pathlist_free(&changed_paths);
3333 6841bf13 2019-11-29 kn if (search_pattern)
3334 6841bf13 2019-11-29 kn regfree(&regex);
3335 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3336 f42b1b34 2018-03-12 stsp return err;
3337 f42b1b34 2018-03-12 stsp }
3338 5c860e29 2018-03-12 stsp
3339 4ed7e80c 2018-05-20 stsp __dead static void
3340 6f3d1eb0 2018-03-12 stsp usage_log(void)
3341 6f3d1eb0 2018-03-12 stsp {
3342 d1fe46f9 2020-04-18 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] "
3343 0208f208 2020-05-05 stsp "[-p] [-P] [-x commit] [-s search-pattern] [-r repository-path] "
3344 0208f208 2020-05-05 stsp "[-R] [path]\n", getprogname());
3345 6f3d1eb0 2018-03-12 stsp exit(1);
3346 b1ebc001 2019-08-13 stsp }
3347 b1ebc001 2019-08-13 stsp
3348 b1ebc001 2019-08-13 stsp static int
3349 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3350 b1ebc001 2019-08-13 stsp {
3351 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3352 b1ebc001 2019-08-13 stsp long long n;
3353 b1ebc001 2019-08-13 stsp const char *errstr;
3354 b1ebc001 2019-08-13 stsp
3355 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3356 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3357 b1ebc001 2019-08-13 stsp return 0;
3358 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3359 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3360 b1ebc001 2019-08-13 stsp return 0;
3361 b1ebc001 2019-08-13 stsp return n;
3362 d1fe46f9 2020-04-18 stsp }
3363 d1fe46f9 2020-04-18 stsp
3364 d1fe46f9 2020-04-18 stsp static const struct got_error *
3365 d1fe46f9 2020-04-18 stsp resolve_commit_arg(struct got_object_id **id, const char *commit_arg,
3366 d1fe46f9 2020-04-18 stsp struct got_repository *repo)
3367 d1fe46f9 2020-04-18 stsp {
3368 d1fe46f9 2020-04-18 stsp const struct got_error *err = NULL;
3369 d1fe46f9 2020-04-18 stsp struct got_reference *ref;
3370 d1fe46f9 2020-04-18 stsp
3371 d1fe46f9 2020-04-18 stsp *id = NULL;
3372 d1fe46f9 2020-04-18 stsp
3373 d1fe46f9 2020-04-18 stsp err = got_ref_open(&ref, repo, commit_arg, 0);
3374 d1fe46f9 2020-04-18 stsp if (err == NULL) {
3375 d1fe46f9 2020-04-18 stsp int obj_type;
3376 d1fe46f9 2020-04-18 stsp err = got_ref_resolve(id, repo, ref);
3377 d1fe46f9 2020-04-18 stsp got_ref_close(ref);
3378 d1fe46f9 2020-04-18 stsp if (err)
3379 d1fe46f9 2020-04-18 stsp return err;
3380 d1fe46f9 2020-04-18 stsp err = got_object_get_type(&obj_type, repo, *id);
3381 d1fe46f9 2020-04-18 stsp if (err)
3382 d1fe46f9 2020-04-18 stsp return err;
3383 d1fe46f9 2020-04-18 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3384 d1fe46f9 2020-04-18 stsp struct got_tag_object *tag;
3385 d1fe46f9 2020-04-18 stsp err = got_object_open_as_tag(&tag, repo, *id);
3386 d1fe46f9 2020-04-18 stsp if (err)
3387 d1fe46f9 2020-04-18 stsp return err;
3388 d1fe46f9 2020-04-18 stsp if (got_object_tag_get_object_type(tag) !=
3389 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT) {
3390 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3391 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3392 d1fe46f9 2020-04-18 stsp }
3393 d1fe46f9 2020-04-18 stsp free(*id);
3394 d1fe46f9 2020-04-18 stsp *id = got_object_id_dup(
3395 d1fe46f9 2020-04-18 stsp got_object_tag_get_object_id(tag));
3396 d1fe46f9 2020-04-18 stsp if (*id == NULL)
3397 d1fe46f9 2020-04-18 stsp err = got_error_from_errno(
3398 d1fe46f9 2020-04-18 stsp "got_object_id_dup");
3399 d1fe46f9 2020-04-18 stsp got_object_tag_close(tag);
3400 d1fe46f9 2020-04-18 stsp if (err)
3401 d1fe46f9 2020-04-18 stsp return err;
3402 d1fe46f9 2020-04-18 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT)
3403 d1fe46f9 2020-04-18 stsp return got_error(GOT_ERR_OBJ_TYPE);
3404 d1fe46f9 2020-04-18 stsp } else {
3405 d1fe46f9 2020-04-18 stsp err = got_repo_match_object_id_prefix(id, commit_arg,
3406 d1fe46f9 2020-04-18 stsp GOT_OBJ_TYPE_COMMIT, repo);
3407 d1fe46f9 2020-04-18 stsp }
3408 d1fe46f9 2020-04-18 stsp
3409 d1fe46f9 2020-04-18 stsp return err;
3410 6f3d1eb0 2018-03-12 stsp }
3411 6f3d1eb0 2018-03-12 stsp
3412 4ed7e80c 2018-05-20 stsp static const struct got_error *
3413 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3414 f42b1b34 2018-03-12 stsp {
3415 f42b1b34 2018-03-12 stsp const struct got_error *error;
3416 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3417 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3418 d1fe46f9 2020-04-18 stsp struct got_object_id *start_id = NULL, *end_id = NULL;
3419 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3420 d1fe46f9 2020-04-18 stsp const char *start_commit = NULL, *end_commit = NULL;
3421 d1fe46f9 2020-04-18 stsp const char *search_pattern = NULL;
3422 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3423 0208f208 2020-05-05 stsp int show_changed_paths = 0, show_patch = 0, limit = 0, log_branches = 0;
3424 dbec59df 2020-04-18 stsp int reverse_display_order = 0;
3425 64a96a6d 2018-04-01 stsp const char *errstr;
3426 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3427 1b3893a2 2019-03-18 stsp
3428 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3429 5c860e29 2018-03-12 stsp
3430 6715a751 2018-03-16 stsp #ifndef PROFILE
3431 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3432 6098196c 2019-01-04 stsp NULL)
3433 ad242220 2018-09-08 stsp == -1)
3434 f42b1b34 2018-03-12 stsp err(1, "pledge");
3435 6715a751 2018-03-16 stsp #endif
3436 79109fed 2018-03-27 stsp
3437 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3438 b1ebc001 2019-08-13 stsp
3439 0208f208 2020-05-05 stsp while ((ch = getopt(argc, argv, "bpPc:C:l:r:Rs:x:")) != -1) {
3440 79109fed 2018-03-27 stsp switch (ch) {
3441 79109fed 2018-03-27 stsp case 'p':
3442 79109fed 2018-03-27 stsp show_patch = 1;
3443 d142fc45 2018-04-01 stsp break;
3444 0208f208 2020-05-05 stsp case 'P':
3445 0208f208 2020-05-05 stsp show_changed_paths = 1;
3446 0208f208 2020-05-05 stsp break;
3447 d142fc45 2018-04-01 stsp case 'c':
3448 d142fc45 2018-04-01 stsp start_commit = optarg;
3449 64a96a6d 2018-04-01 stsp break;
3450 c0cc5c62 2018-10-18 stsp case 'C':
3451 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3452 4a8520aa 2018-10-18 stsp &errstr);
3453 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3454 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3455 c0cc5c62 2018-10-18 stsp break;
3456 64a96a6d 2018-04-01 stsp case 'l':
3457 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3458 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3459 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3460 cc54c501 2019-07-15 stsp break;
3461 48c8c60d 2020-01-27 stsp case 'b':
3462 48c8c60d 2020-01-27 stsp log_branches = 1;
3463 a0603db2 2018-06-10 stsp break;
3464 04ca23f4 2018-07-16 stsp case 'r':
3465 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3466 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3467 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3468 9ba1d308 2019-10-21 stsp optarg);
3469 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3470 04ca23f4 2018-07-16 stsp break;
3471 dbec59df 2020-04-18 stsp case 'R':
3472 dbec59df 2020-04-18 stsp reverse_display_order = 1;
3473 dbec59df 2020-04-18 stsp break;
3474 6841bf13 2019-11-29 kn case 's':
3475 6841bf13 2019-11-29 kn search_pattern = optarg;
3476 6841bf13 2019-11-29 kn break;
3477 d1fe46f9 2020-04-18 stsp case 'x':
3478 d1fe46f9 2020-04-18 stsp end_commit = optarg;
3479 d1fe46f9 2020-04-18 stsp break;
3480 79109fed 2018-03-27 stsp default:
3481 2deda0b9 2019-03-07 stsp usage_log();
3482 79109fed 2018-03-27 stsp /* NOTREACHED */
3483 79109fed 2018-03-27 stsp }
3484 79109fed 2018-03-27 stsp }
3485 79109fed 2018-03-27 stsp
3486 79109fed 2018-03-27 stsp argc -= optind;
3487 79109fed 2018-03-27 stsp argv += optind;
3488 f42b1b34 2018-03-12 stsp
3489 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3490 dc1edbfa 2019-11-29 kn diff_context = 3;
3491 dc1edbfa 2019-11-29 kn else if (!show_patch)
3492 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3493 dc1edbfa 2019-11-29 kn
3494 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3495 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3496 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3497 04ca23f4 2018-07-16 stsp goto done;
3498 04ca23f4 2018-07-16 stsp }
3499 cffc0aa4 2019-02-05 stsp
3500 50f2fada 2020-04-24 stsp if (repo_path == NULL) {
3501 50f2fada 2020-04-24 stsp error = got_worktree_open(&worktree, cwd);
3502 50f2fada 2020-04-24 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3503 50f2fada 2020-04-24 stsp goto done;
3504 50f2fada 2020-04-24 stsp error = NULL;
3505 50f2fada 2020-04-24 stsp }
3506 cffc0aa4 2019-02-05 stsp
3507 e7301579 2019-03-18 stsp if (argc == 0) {
3508 cbd1af7a 2019-03-18 stsp path = strdup("");
3509 e7301579 2019-03-18 stsp if (path == NULL) {
3510 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3511 cbd1af7a 2019-03-18 stsp goto done;
3512 e7301579 2019-03-18 stsp }
3513 e7301579 2019-03-18 stsp } else if (argc == 1) {
3514 e7301579 2019-03-18 stsp if (worktree) {
3515 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3516 e7301579 2019-03-18 stsp argv[0]);
3517 e7301579 2019-03-18 stsp if (error)
3518 e7301579 2019-03-18 stsp goto done;
3519 e7301579 2019-03-18 stsp } else {
3520 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3521 e7301579 2019-03-18 stsp if (path == NULL) {
3522 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3523 e7301579 2019-03-18 stsp goto done;
3524 e7301579 2019-03-18 stsp }
3525 e7301579 2019-03-18 stsp }
3526 cbd1af7a 2019-03-18 stsp } else
3527 cbd1af7a 2019-03-18 stsp usage_log();
3528 cbd1af7a 2019-03-18 stsp
3529 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3530 5486daa2 2019-05-11 stsp repo_path = worktree ?
3531 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3532 5486daa2 2019-05-11 stsp }
3533 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3534 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3535 cffc0aa4 2019-02-05 stsp goto done;
3536 04ca23f4 2018-07-16 stsp }
3537 6098196c 2019-01-04 stsp
3538 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3539 d7d4f210 2018-03-12 stsp if (error != NULL)
3540 04ca23f4 2018-07-16 stsp goto done;
3541 f42b1b34 2018-03-12 stsp
3542 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3543 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3544 c02c541e 2019-03-29 stsp if (error)
3545 c02c541e 2019-03-29 stsp goto done;
3546 c02c541e 2019-03-29 stsp
3547 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3548 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3549 d1fe46f9 2020-04-18 stsp struct got_commit_object *commit = NULL;
3550 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3551 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3552 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3553 3235492e 2018-04-01 stsp if (error != NULL)
3554 d1fe46f9 2020-04-18 stsp goto done;
3555 d1fe46f9 2020-04-18 stsp error = got_ref_resolve(&start_id, repo, head_ref);
3556 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3557 3235492e 2018-04-01 stsp if (error != NULL)
3558 d1fe46f9 2020-04-18 stsp goto done;
3559 d1fe46f9 2020-04-18 stsp error = got_object_open_as_commit(&commit, repo,
3560 d1fe46f9 2020-04-18 stsp start_id);
3561 d1fe46f9 2020-04-18 stsp if (error != NULL)
3562 d1fe46f9 2020-04-18 stsp goto done;
3563 d1fe46f9 2020-04-18 stsp got_object_commit_close(commit);
3564 3235492e 2018-04-01 stsp } else {
3565 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&start_id, start_commit, repo);
3566 d1fe46f9 2020-04-18 stsp if (error != NULL)
3567 d1fe46f9 2020-04-18 stsp goto done;
3568 3235492e 2018-04-01 stsp }
3569 d1fe46f9 2020-04-18 stsp if (end_commit != NULL) {
3570 d1fe46f9 2020-04-18 stsp error = resolve_commit_arg(&end_id, end_commit, repo);
3571 d1fe46f9 2020-04-18 stsp if (error != NULL)
3572 d1fe46f9 2020-04-18 stsp goto done;
3573 d1fe46f9 2020-04-18 stsp }
3574 04ca23f4 2018-07-16 stsp
3575 deeabeae 2019-08-27 stsp if (worktree) {
3576 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3577 deeabeae 2019-08-27 stsp char *p;
3578 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3579 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3580 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3581 deeabeae 2019-08-27 stsp goto done;
3582 deeabeae 2019-08-27 stsp }
3583 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3584 deeabeae 2019-08-27 stsp free(p);
3585 deeabeae 2019-08-27 stsp } else
3586 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3587 04ca23f4 2018-07-16 stsp if (error != NULL)
3588 04ca23f4 2018-07-16 stsp goto done;
3589 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3590 04ca23f4 2018-07-16 stsp free(path);
3591 04ca23f4 2018-07-16 stsp path = in_repo_path;
3592 04ca23f4 2018-07-16 stsp }
3593 199a4027 2019-02-02 stsp
3594 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3595 199a4027 2019-02-02 stsp if (error)
3596 199a4027 2019-02-02 stsp goto done;
3597 04ca23f4 2018-07-16 stsp
3598 0208f208 2020-05-05 stsp error = print_commits(start_id, end_id, repo, path, show_changed_paths,
3599 0208f208 2020-05-05 stsp show_patch, search_pattern, diff_context, limit, log_branches,
3600 dbec59df 2020-04-18 stsp reverse_display_order, &refs);
3601 04ca23f4 2018-07-16 stsp done:
3602 04ca23f4 2018-07-16 stsp free(path);
3603 04ca23f4 2018-07-16 stsp free(repo_path);
3604 04ca23f4 2018-07-16 stsp free(cwd);
3605 cffc0aa4 2019-02-05 stsp if (worktree)
3606 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3607 ad242220 2018-09-08 stsp if (repo) {
3608 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3609 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3610 ad242220 2018-09-08 stsp if (error == NULL)
3611 ad242220 2018-09-08 stsp error = repo_error;
3612 ad242220 2018-09-08 stsp }
3613 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3614 8bf5b3c9 2018-03-17 stsp return error;
3615 5c860e29 2018-03-12 stsp }
3616 5c860e29 2018-03-12 stsp
3617 4ed7e80c 2018-05-20 stsp __dead static void
3618 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3619 3f8b7d6a 2018-04-01 stsp {
3620 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3621 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3622 3f8b7d6a 2018-04-01 stsp exit(1);
3623 b00d56cd 2018-04-01 stsp }
3624 b00d56cd 2018-04-01 stsp
3625 b72f483a 2019-02-05 stsp struct print_diff_arg {
3626 b72f483a 2019-02-05 stsp struct got_repository *repo;
3627 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3628 b72f483a 2019-02-05 stsp int diff_context;
3629 3fc0c068 2019-02-10 stsp const char *id_str;
3630 3fc0c068 2019-02-10 stsp int header_shown;
3631 98eaaa12 2019-08-03 stsp int diff_staged;
3632 63035f9f 2019-10-06 stsp int ignore_whitespace;
3633 b72f483a 2019-02-05 stsp };
3634 39449a05 2020-07-23 stsp
3635 39449a05 2020-07-23 stsp /*
3636 39449a05 2020-07-23 stsp * Create a file which contains the target path of a symlink so we can feed
3637 39449a05 2020-07-23 stsp * it as content to the diff engine.
3638 39449a05 2020-07-23 stsp */
3639 39449a05 2020-07-23 stsp static const struct got_error *
3640 39449a05 2020-07-23 stsp get_symlink_target_file(int *fd, int dirfd, const char *de_name,
3641 39449a05 2020-07-23 stsp const char *abspath)
3642 39449a05 2020-07-23 stsp {
3643 39449a05 2020-07-23 stsp const struct got_error *err = NULL;
3644 39449a05 2020-07-23 stsp char target_path[PATH_MAX];
3645 39449a05 2020-07-23 stsp ssize_t target_len, outlen;
3646 39449a05 2020-07-23 stsp
3647 39449a05 2020-07-23 stsp *fd = -1;
3648 39449a05 2020-07-23 stsp
3649 39449a05 2020-07-23 stsp if (dirfd != -1) {
3650 39449a05 2020-07-23 stsp target_len = readlinkat(dirfd, de_name, target_path, PATH_MAX);
3651 39449a05 2020-07-23 stsp if (target_len == -1)
3652 39449a05 2020-07-23 stsp return got_error_from_errno2("readlinkat", abspath);
3653 39449a05 2020-07-23 stsp } else {
3654 39449a05 2020-07-23 stsp target_len = readlink(abspath, target_path, PATH_MAX);
3655 39449a05 2020-07-23 stsp if (target_len == -1)
3656 39449a05 2020-07-23 stsp return got_error_from_errno2("readlink", abspath);
3657 39449a05 2020-07-23 stsp }
3658 39449a05 2020-07-23 stsp
3659 39449a05 2020-07-23 stsp *fd = got_opentempfd();
3660 39449a05 2020-07-23 stsp if (*fd == -1)
3661 39449a05 2020-07-23 stsp return got_error_from_errno("got_opentempfd");
3662 39449a05 2020-07-23 stsp
3663 39449a05 2020-07-23 stsp outlen = write(*fd, target_path, target_len);
3664 39449a05 2020-07-23 stsp if (outlen == -1) {
3665 39449a05 2020-07-23 stsp err = got_error_from_errno("got_opentempfd");
3666 39449a05 2020-07-23 stsp goto done;
3667 39449a05 2020-07-23 stsp }
3668 39449a05 2020-07-23 stsp
3669 39449a05 2020-07-23 stsp if (lseek(*fd, 0, SEEK_SET) == -1) {
3670 39449a05 2020-07-23 stsp err = got_error_from_errno2("lseek", abspath);
3671 39449a05 2020-07-23 stsp goto done;
3672 39449a05 2020-07-23 stsp }
3673 39449a05 2020-07-23 stsp done:
3674 39449a05 2020-07-23 stsp if (err) {
3675 39449a05 2020-07-23 stsp close(*fd);
3676 39449a05 2020-07-23 stsp *fd = -1;
3677 39449a05 2020-07-23 stsp }
3678 39449a05 2020-07-23 stsp return err;
3679 39449a05 2020-07-23 stsp }
3680 b72f483a 2019-02-05 stsp
3681 4ed7e80c 2018-05-20 stsp static const struct got_error *
3682 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3683 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3684 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3685 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3686 b72f483a 2019-02-05 stsp {
3687 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3688 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3689 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3690 12463d8b 2019-12-13 stsp int fd = -1;
3691 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3692 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3693 b72f483a 2019-02-05 stsp struct stat sb;
3694 b72f483a 2019-02-05 stsp
3695 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3696 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3697 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3698 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3699 98eaaa12 2019-08-03 stsp return NULL;
3700 98eaaa12 2019-08-03 stsp } else {
3701 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3702 98eaaa12 2019-08-03 stsp return NULL;
3703 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3704 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3705 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3706 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3707 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3708 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3709 98eaaa12 2019-08-03 stsp return NULL;
3710 98eaaa12 2019-08-03 stsp }
3711 3fc0c068 2019-02-10 stsp
3712 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3713 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3714 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3715 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3716 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3717 3fc0c068 2019-02-10 stsp }
3718 b72f483a 2019-02-05 stsp
3719 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3720 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3721 98eaaa12 2019-08-03 stsp switch (staged_status) {
3722 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3723 98eaaa12 2019-08-03 stsp label1 = path;
3724 98eaaa12 2019-08-03 stsp label2 = path;
3725 98eaaa12 2019-08-03 stsp break;
3726 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3727 98eaaa12 2019-08-03 stsp label2 = path;
3728 98eaaa12 2019-08-03 stsp break;
3729 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3730 98eaaa12 2019-08-03 stsp label1 = path;
3731 98eaaa12 2019-08-03 stsp break;
3732 98eaaa12 2019-08-03 stsp default:
3733 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3734 98eaaa12 2019-08-03 stsp }
3735 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3736 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3737 63035f9f 2019-10-06 stsp a->repo, stdout);
3738 98eaaa12 2019-08-03 stsp }
3739 98eaaa12 2019-08-03 stsp
3740 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3741 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3742 4ce46740 2019-08-08 stsp char *id_str;
3743 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3744 408b4ebc 2019-08-03 stsp 8192);
3745 4ce46740 2019-08-08 stsp if (err)
3746 4ce46740 2019-08-08 stsp goto done;
3747 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3748 4ce46740 2019-08-08 stsp if (err)
3749 4ce46740 2019-08-08 stsp goto done;
3750 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3751 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3752 4ce46740 2019-08-08 stsp free(id_str);
3753 4ce46740 2019-08-08 stsp goto done;
3754 4ce46740 2019-08-08 stsp }
3755 4ce46740 2019-08-08 stsp free(id_str);
3756 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3757 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3758 4ce46740 2019-08-08 stsp if (err)
3759 4ce46740 2019-08-08 stsp goto done;
3760 4ce46740 2019-08-08 stsp }
3761 d00136be 2019-03-26 stsp
3762 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3763 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3764 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3765 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3766 2ec1f75b 2019-03-26 stsp goto done;
3767 2ec1f75b 2019-03-26 stsp }
3768 b72f483a 2019-02-05 stsp
3769 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3770 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3771 12463d8b 2019-12-13 stsp if (fd == -1) {
3772 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3773 39449a05 2020-07-23 stsp err = got_error_from_errno2("openat",
3774 39449a05 2020-07-23 stsp abspath);
3775 39449a05 2020-07-23 stsp goto done;
3776 39449a05 2020-07-23 stsp }
3777 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3778 39449a05 2020-07-23 stsp de_name, abspath);
3779 39449a05 2020-07-23 stsp if (err)
3780 39449a05 2020-07-23 stsp goto done;
3781 12463d8b 2019-12-13 stsp }
3782 12463d8b 2019-12-13 stsp } else {
3783 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3784 12463d8b 2019-12-13 stsp if (fd == -1) {
3785 39449a05 2020-07-23 stsp if (errno != ELOOP) {
3786 39449a05 2020-07-23 stsp err = got_error_from_errno2("open",
3787 39449a05 2020-07-23 stsp abspath);
3788 39449a05 2020-07-23 stsp goto done;
3789 39449a05 2020-07-23 stsp }
3790 39449a05 2020-07-23 stsp err = get_symlink_target_file(&fd, dirfd,
3791 39449a05 2020-07-23 stsp de_name, abspath);
3792 39449a05 2020-07-23 stsp if (err)
3793 39449a05 2020-07-23 stsp goto done;
3794 12463d8b 2019-12-13 stsp }
3795 12463d8b 2019-12-13 stsp }
3796 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3797 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3798 2ec1f75b 2019-03-26 stsp goto done;
3799 2ec1f75b 2019-03-26 stsp }
3800 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3801 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3802 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3803 2ec1f75b 2019-03-26 stsp goto done;
3804 2ec1f75b 2019-03-26 stsp }
3805 12463d8b 2019-12-13 stsp fd = -1;
3806 2ec1f75b 2019-03-26 stsp } else
3807 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3808 b72f483a 2019-02-05 stsp
3809 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3810 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3811 b72f483a 2019-02-05 stsp done:
3812 b72f483a 2019-02-05 stsp if (blob1)
3813 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3814 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3815 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3816 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3817 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3818 b72f483a 2019-02-05 stsp free(abspath);
3819 927df6b7 2019-02-10 stsp return err;
3820 927df6b7 2019-02-10 stsp }
3821 927df6b7 2019-02-10 stsp
3822 927df6b7 2019-02-10 stsp static const struct got_error *
3823 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3824 b00d56cd 2018-04-01 stsp {
3825 b00d56cd 2018-04-01 stsp const struct got_error *error;
3826 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3827 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3828 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3829 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3830 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3831 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3832 15a94983 2018-12-23 stsp int type1, type2;
3833 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3834 c0cc5c62 2018-10-18 stsp const char *errstr;
3835 927df6b7 2019-02-10 stsp char *path = NULL;
3836 b00d56cd 2018-04-01 stsp
3837 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3838 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3839 25eccc22 2019-01-04 stsp NULL) == -1)
3840 b00d56cd 2018-04-01 stsp err(1, "pledge");
3841 b00d56cd 2018-04-01 stsp #endif
3842 b00d56cd 2018-04-01 stsp
3843 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3844 b00d56cd 2018-04-01 stsp switch (ch) {
3845 c0cc5c62 2018-10-18 stsp case 'C':
3846 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3847 dfcab68b 2019-11-29 kn &errstr);
3848 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3849 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3850 c0cc5c62 2018-10-18 stsp break;
3851 b72f483a 2019-02-05 stsp case 'r':
3852 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3853 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3854 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3855 9ba1d308 2019-10-21 stsp optarg);
3856 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3857 b72f483a 2019-02-05 stsp break;
3858 98eaaa12 2019-08-03 stsp case 's':
3859 98eaaa12 2019-08-03 stsp diff_staged = 1;
3860 63035f9f 2019-10-06 stsp break;
3861 63035f9f 2019-10-06 stsp case 'w':
3862 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3863 98eaaa12 2019-08-03 stsp break;
3864 b00d56cd 2018-04-01 stsp default:
3865 2deda0b9 2019-03-07 stsp usage_diff();
3866 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3867 b00d56cd 2018-04-01 stsp }
3868 b00d56cd 2018-04-01 stsp }
3869 b00d56cd 2018-04-01 stsp
3870 b00d56cd 2018-04-01 stsp argc -= optind;
3871 b00d56cd 2018-04-01 stsp argv += optind;
3872 b00d56cd 2018-04-01 stsp
3873 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3874 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3875 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3876 b72f483a 2019-02-05 stsp goto done;
3877 b72f483a 2019-02-05 stsp }
3878 927df6b7 2019-02-10 stsp if (argc <= 1) {
3879 b72f483a 2019-02-05 stsp if (repo_path)
3880 b72f483a 2019-02-05 stsp errx(1,
3881 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3882 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3883 fa51e947 2020-03-27 stsp if (error) {
3884 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
3885 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "diff",
3886 fa51e947 2020-03-27 stsp cwd);
3887 1f03b8da 2020-03-20 stsp goto done;
3888 fa51e947 2020-03-27 stsp }
3889 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3890 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3891 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3892 927df6b7 2019-02-10 stsp goto done;
3893 927df6b7 2019-02-10 stsp }
3894 927df6b7 2019-02-10 stsp if (argc == 1) {
3895 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3896 cbd1af7a 2019-03-18 stsp argv[0]);
3897 927df6b7 2019-02-10 stsp if (error)
3898 927df6b7 2019-02-10 stsp goto done;
3899 927df6b7 2019-02-10 stsp } else {
3900 927df6b7 2019-02-10 stsp path = strdup("");
3901 927df6b7 2019-02-10 stsp if (path == NULL) {
3902 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3903 927df6b7 2019-02-10 stsp goto done;
3904 927df6b7 2019-02-10 stsp }
3905 927df6b7 2019-02-10 stsp }
3906 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3907 98eaaa12 2019-08-03 stsp if (diff_staged)
3908 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3909 98eaaa12 2019-08-03 stsp "objects in repository");
3910 15a94983 2018-12-23 stsp id_str1 = argv[0];
3911 15a94983 2018-12-23 stsp id_str2 = argv[1];
3912 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3913 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3914 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3915 30db809c 2019-06-05 stsp goto done;
3916 1f03b8da 2020-03-20 stsp if (worktree) {
3917 1f03b8da 2020-03-20 stsp repo_path = strdup(
3918 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3919 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3920 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3921 1f03b8da 2020-03-20 stsp goto done;
3922 1f03b8da 2020-03-20 stsp }
3923 1f03b8da 2020-03-20 stsp } else {
3924 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3925 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3926 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3927 1f03b8da 2020-03-20 stsp goto done;
3928 1f03b8da 2020-03-20 stsp }
3929 30db809c 2019-06-05 stsp }
3930 30db809c 2019-06-05 stsp }
3931 b00d56cd 2018-04-01 stsp } else
3932 b00d56cd 2018-04-01 stsp usage_diff();
3933 b00d56cd 2018-04-01 stsp
3934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3935 76089277 2018-04-01 stsp free(repo_path);
3936 b00d56cd 2018-04-01 stsp if (error != NULL)
3937 b00d56cd 2018-04-01 stsp goto done;
3938 b00d56cd 2018-04-01 stsp
3939 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3940 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3941 c02c541e 2019-03-29 stsp if (error)
3942 c02c541e 2019-03-29 stsp goto done;
3943 c02c541e 2019-03-29 stsp
3944 30db809c 2019-06-05 stsp if (argc <= 1) {
3945 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3946 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3947 b72f483a 2019-02-05 stsp char *id_str;
3948 72ea6654 2019-07-27 stsp
3949 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3950 72ea6654 2019-07-27 stsp
3951 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3952 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3953 b72f483a 2019-02-05 stsp if (error)
3954 b72f483a 2019-02-05 stsp goto done;
3955 b72f483a 2019-02-05 stsp arg.repo = repo;
3956 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3957 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3958 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3959 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3960 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3961 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3962 b72f483a 2019-02-05 stsp
3963 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3964 72ea6654 2019-07-27 stsp if (error)
3965 72ea6654 2019-07-27 stsp goto done;
3966 72ea6654 2019-07-27 stsp
3967 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3968 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3969 b72f483a 2019-02-05 stsp free(id_str);
3970 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3971 b72f483a 2019-02-05 stsp goto done;
3972 b72f483a 2019-02-05 stsp }
3973 b72f483a 2019-02-05 stsp
3974 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3975 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3976 0adb7196 2019-08-11 stsp if (error)
3977 0adb7196 2019-08-11 stsp goto done;
3978 b00d56cd 2018-04-01 stsp
3979 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3980 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3981 0adb7196 2019-08-11 stsp if (error)
3982 0adb7196 2019-08-11 stsp goto done;
3983 b00d56cd 2018-04-01 stsp
3984 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3985 15a94983 2018-12-23 stsp if (error)
3986 15a94983 2018-12-23 stsp goto done;
3987 15a94983 2018-12-23 stsp
3988 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3989 15a94983 2018-12-23 stsp if (error)
3990 15a94983 2018-12-23 stsp goto done;
3991 15a94983 2018-12-23 stsp
3992 15a94983 2018-12-23 stsp if (type1 != type2) {
3993 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3994 b00d56cd 2018-04-01 stsp goto done;
3995 b00d56cd 2018-04-01 stsp }
3996 b00d56cd 2018-04-01 stsp
3997 15a94983 2018-12-23 stsp switch (type1) {
3998 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3999 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
4000 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4001 b00d56cd 2018-04-01 stsp break;
4002 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
4003 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
4004 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
4005 b00d56cd 2018-04-01 stsp break;
4006 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
4007 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
4008 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
4009 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
4010 b00d56cd 2018-04-01 stsp break;
4011 b00d56cd 2018-04-01 stsp default:
4012 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
4013 b00d56cd 2018-04-01 stsp }
4014 b00d56cd 2018-04-01 stsp done:
4015 5e70831e 2019-05-28 stsp free(label1);
4016 5e70831e 2019-05-28 stsp free(label2);
4017 15a94983 2018-12-23 stsp free(id1);
4018 15a94983 2018-12-23 stsp free(id2);
4019 927df6b7 2019-02-10 stsp free(path);
4020 b72f483a 2019-02-05 stsp if (worktree)
4021 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
4022 ad242220 2018-09-08 stsp if (repo) {
4023 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4024 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4025 ad242220 2018-09-08 stsp if (error == NULL)
4026 ad242220 2018-09-08 stsp error = repo_error;
4027 ad242220 2018-09-08 stsp }
4028 b00d56cd 2018-04-01 stsp return error;
4029 404c43c4 2018-06-21 stsp }
4030 404c43c4 2018-06-21 stsp
4031 404c43c4 2018-06-21 stsp __dead static void
4032 404c43c4 2018-06-21 stsp usage_blame(void)
4033 404c43c4 2018-06-21 stsp {
4034 9270e621 2019-02-05 stsp fprintf(stderr,
4035 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
4036 404c43c4 2018-06-21 stsp getprogname());
4037 404c43c4 2018-06-21 stsp exit(1);
4038 b00d56cd 2018-04-01 stsp }
4039 b00d56cd 2018-04-01 stsp
4040 28315671 2019-08-14 stsp struct blame_line {
4041 28315671 2019-08-14 stsp int annotated;
4042 28315671 2019-08-14 stsp char *id_str;
4043 82f6abb8 2019-08-14 stsp char *committer;
4044 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
4045 28315671 2019-08-14 stsp };
4046 28315671 2019-08-14 stsp
4047 28315671 2019-08-14 stsp struct blame_cb_args {
4048 28315671 2019-08-14 stsp struct blame_line *lines;
4049 28315671 2019-08-14 stsp int nlines;
4050 7ef28ff8 2019-08-14 stsp int nlines_prec;
4051 28315671 2019-08-14 stsp int lineno_cur;
4052 28315671 2019-08-14 stsp off_t *line_offsets;
4053 28315671 2019-08-14 stsp FILE *f;
4054 82f6abb8 2019-08-14 stsp struct got_repository *repo;
4055 28315671 2019-08-14 stsp };
4056 28315671 2019-08-14 stsp
4057 404c43c4 2018-06-21 stsp static const struct got_error *
4058 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
4059 28315671 2019-08-14 stsp {
4060 28315671 2019-08-14 stsp const struct got_error *err = NULL;
4061 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
4062 28315671 2019-08-14 stsp struct blame_line *bline;
4063 82f6abb8 2019-08-14 stsp char *line = NULL;
4064 28315671 2019-08-14 stsp size_t linesize = 0;
4065 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
4066 28315671 2019-08-14 stsp off_t offset;
4067 bcb49d15 2019-08-14 stsp struct tm tm;
4068 bcb49d15 2019-08-14 stsp time_t committer_time;
4069 28315671 2019-08-14 stsp
4070 28315671 2019-08-14 stsp if (nlines != a->nlines ||
4071 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
4072 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
4073 28315671 2019-08-14 stsp
4074 28315671 2019-08-14 stsp if (sigint_received)
4075 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
4076 28315671 2019-08-14 stsp
4077 2839f8b9 2019-08-18 stsp if (lineno == -1)
4078 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
4079 2839f8b9 2019-08-18 stsp
4080 28315671 2019-08-14 stsp /* Annotate this line. */
4081 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
4082 28315671 2019-08-14 stsp if (bline->annotated)
4083 28315671 2019-08-14 stsp return NULL;
4084 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
4085 28315671 2019-08-14 stsp if (err)
4086 28315671 2019-08-14 stsp return err;
4087 82f6abb8 2019-08-14 stsp
4088 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
4089 82f6abb8 2019-08-14 stsp if (err)
4090 82f6abb8 2019-08-14 stsp goto done;
4091 82f6abb8 2019-08-14 stsp
4092 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
4093 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
4094 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
4095 bcb49d15 2019-08-14 stsp goto done;
4096 bcb49d15 2019-08-14 stsp }
4097 bcb49d15 2019-08-14 stsp
4098 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
4099 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
4100 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
4101 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
4102 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
4103 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
4104 82f6abb8 2019-08-14 stsp goto done;
4105 82f6abb8 2019-08-14 stsp }
4106 28315671 2019-08-14 stsp bline->annotated = 1;
4107 28315671 2019-08-14 stsp
4108 28315671 2019-08-14 stsp /* Print lines annotated so far. */
4109 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4110 28315671 2019-08-14 stsp if (!bline->annotated)
4111 82f6abb8 2019-08-14 stsp goto done;
4112 28315671 2019-08-14 stsp
4113 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
4114 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
4115 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
4116 82f6abb8 2019-08-14 stsp goto done;
4117 82f6abb8 2019-08-14 stsp }
4118 28315671 2019-08-14 stsp
4119 28315671 2019-08-14 stsp while (bline->annotated) {
4120 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
4121 82f6abb8 2019-08-14 stsp size_t len;
4122 82f6abb8 2019-08-14 stsp
4123 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
4124 28315671 2019-08-14 stsp if (ferror(a->f))
4125 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
4126 28315671 2019-08-14 stsp break;
4127 28315671 2019-08-14 stsp }
4128 82f6abb8 2019-08-14 stsp
4129 82f6abb8 2019-08-14 stsp committer = bline->committer;
4130 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
4131 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
4132 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
4133 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
4134 82f6abb8 2019-08-14 stsp if (at)
4135 82f6abb8 2019-08-14 stsp *at = '\0';
4136 82f6abb8 2019-08-14 stsp len = strlen(committer);
4137 82f6abb8 2019-08-14 stsp if (len >= 9)
4138 82f6abb8 2019-08-14 stsp committer[8] = '\0';
4139 28315671 2019-08-14 stsp
4140 28315671 2019-08-14 stsp nl = strchr(line, '\n');
4141 28315671 2019-08-14 stsp if (nl)
4142 28315671 2019-08-14 stsp *nl = '\0';
4143 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
4144 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
4145 28315671 2019-08-14 stsp
4146 28315671 2019-08-14 stsp a->lineno_cur++;
4147 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
4148 28315671 2019-08-14 stsp }
4149 82f6abb8 2019-08-14 stsp done:
4150 82f6abb8 2019-08-14 stsp if (commit)
4151 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
4152 28315671 2019-08-14 stsp free(line);
4153 28315671 2019-08-14 stsp return err;
4154 28315671 2019-08-14 stsp }
4155 28315671 2019-08-14 stsp
4156 28315671 2019-08-14 stsp static const struct got_error *
4157 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
4158 404c43c4 2018-06-21 stsp {
4159 404c43c4 2018-06-21 stsp const struct got_error *error;
4160 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
4161 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
4162 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4163 0587e10c 2020-07-23 stsp char *link_target = NULL;
4164 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
4165 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
4166 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
4167 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
4168 28315671 2019-08-14 stsp struct blame_cb_args bca;
4169 28315671 2019-08-14 stsp int ch, obj_type, i;
4170 b02560ec 2019-08-19 stsp size_t filesize;
4171 90f3c347 2019-08-19 stsp
4172 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
4173 404c43c4 2018-06-21 stsp
4174 404c43c4 2018-06-21 stsp #ifndef PROFILE
4175 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4176 36e2fb66 2019-01-04 stsp NULL) == -1)
4177 404c43c4 2018-06-21 stsp err(1, "pledge");
4178 404c43c4 2018-06-21 stsp #endif
4179 404c43c4 2018-06-21 stsp
4180 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
4181 404c43c4 2018-06-21 stsp switch (ch) {
4182 404c43c4 2018-06-21 stsp case 'c':
4183 404c43c4 2018-06-21 stsp commit_id_str = optarg;
4184 404c43c4 2018-06-21 stsp break;
4185 66bea077 2018-08-02 stsp case 'r':
4186 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
4187 66bea077 2018-08-02 stsp if (repo_path == NULL)
4188 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4189 9ba1d308 2019-10-21 stsp optarg);
4190 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4191 66bea077 2018-08-02 stsp break;
4192 404c43c4 2018-06-21 stsp default:
4193 2deda0b9 2019-03-07 stsp usage_blame();
4194 404c43c4 2018-06-21 stsp /* NOTREACHED */
4195 404c43c4 2018-06-21 stsp }
4196 404c43c4 2018-06-21 stsp }
4197 404c43c4 2018-06-21 stsp
4198 404c43c4 2018-06-21 stsp argc -= optind;
4199 404c43c4 2018-06-21 stsp argv += optind;
4200 404c43c4 2018-06-21 stsp
4201 a39318fd 2018-08-02 stsp if (argc == 1)
4202 404c43c4 2018-06-21 stsp path = argv[0];
4203 a39318fd 2018-08-02 stsp else
4204 404c43c4 2018-06-21 stsp usage_blame();
4205 404c43c4 2018-06-21 stsp
4206 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
4207 66bea077 2018-08-02 stsp if (cwd == NULL) {
4208 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4209 66bea077 2018-08-02 stsp goto done;
4210 66bea077 2018-08-02 stsp }
4211 66bea077 2018-08-02 stsp if (repo_path == NULL) {
4212 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4213 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4214 66bea077 2018-08-02 stsp goto done;
4215 0c06baac 2019-02-05 stsp else
4216 0c06baac 2019-02-05 stsp error = NULL;
4217 0c06baac 2019-02-05 stsp if (worktree) {
4218 0c06baac 2019-02-05 stsp repo_path =
4219 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4220 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
4221 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4222 1f03b8da 2020-03-20 stsp if (error)
4223 1f03b8da 2020-03-20 stsp goto done;
4224 1f03b8da 2020-03-20 stsp }
4225 0c06baac 2019-02-05 stsp } else {
4226 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
4227 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
4228 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4229 0c06baac 2019-02-05 stsp goto done;
4230 0c06baac 2019-02-05 stsp }
4231 66bea077 2018-08-02 stsp }
4232 66bea077 2018-08-02 stsp }
4233 36e2fb66 2019-01-04 stsp
4234 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4235 c02c541e 2019-03-29 stsp if (error != NULL)
4236 36e2fb66 2019-01-04 stsp goto done;
4237 66bea077 2018-08-02 stsp
4238 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4239 c02c541e 2019-03-29 stsp if (error)
4240 404c43c4 2018-06-21 stsp goto done;
4241 404c43c4 2018-06-21 stsp
4242 0c06baac 2019-02-05 stsp if (worktree) {
4243 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
4244 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4245 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4246 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
4247 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
4248 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
4249 6efaaa2d 2019-02-05 stsp path) == -1) {
4250 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4251 0c06baac 2019-02-05 stsp goto done;
4252 0c06baac 2019-02-05 stsp }
4253 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4254 0c06baac 2019-02-05 stsp free(p);
4255 0c06baac 2019-02-05 stsp } else {
4256 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4257 0c06baac 2019-02-05 stsp }
4258 0c06baac 2019-02-05 stsp if (error)
4259 66bea077 2018-08-02 stsp goto done;
4260 66bea077 2018-08-02 stsp
4261 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
4262 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
4263 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
4264 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
4265 404c43c4 2018-06-21 stsp if (error != NULL)
4266 66bea077 2018-08-02 stsp goto done;
4267 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4268 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
4269 404c43c4 2018-06-21 stsp if (error != NULL)
4270 66bea077 2018-08-02 stsp goto done;
4271 404c43c4 2018-06-21 stsp } else {
4272 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4273 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4274 30837e32 2019-07-25 stsp if (error)
4275 66bea077 2018-08-02 stsp goto done;
4276 404c43c4 2018-06-21 stsp }
4277 404c43c4 2018-06-21 stsp
4278 0587e10c 2020-07-23 stsp error = got_object_resolve_symlinks(&link_target, in_repo_path,
4279 0587e10c 2020-07-23 stsp commit_id, repo);
4280 0587e10c 2020-07-23 stsp if (error)
4281 0587e10c 2020-07-23 stsp goto done;
4282 0587e10c 2020-07-23 stsp
4283 0587e10c 2020-07-23 stsp error = got_object_id_by_path(&obj_id, repo, commit_id,
4284 0587e10c 2020-07-23 stsp link_target ? link_target : in_repo_path);
4285 28315671 2019-08-14 stsp if (error)
4286 28315671 2019-08-14 stsp goto done;
4287 28315671 2019-08-14 stsp
4288 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
4289 28315671 2019-08-14 stsp if (error)
4290 28315671 2019-08-14 stsp goto done;
4291 28315671 2019-08-14 stsp
4292 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
4293 eb59b6d4 2020-07-23 stsp error = got_error_path(link_target ? link_target : in_repo_path,
4294 eb59b6d4 2020-07-23 stsp GOT_ERR_OBJ_TYPE);
4295 28315671 2019-08-14 stsp goto done;
4296 28315671 2019-08-14 stsp }
4297 28315671 2019-08-14 stsp
4298 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
4299 28315671 2019-08-14 stsp if (error)
4300 28315671 2019-08-14 stsp goto done;
4301 28315671 2019-08-14 stsp bca.f = got_opentemp();
4302 28315671 2019-08-14 stsp if (bca.f == NULL) {
4303 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
4304 28315671 2019-08-14 stsp goto done;
4305 28315671 2019-08-14 stsp }
4306 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
4307 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
4308 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
4309 28315671 2019-08-14 stsp goto done;
4310 28315671 2019-08-14 stsp
4311 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
4312 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
4313 b02560ec 2019-08-19 stsp bca.nlines--;
4314 b02560ec 2019-08-19 stsp
4315 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
4316 28315671 2019-08-14 stsp if (bca.lines == NULL) {
4317 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
4318 28315671 2019-08-14 stsp goto done;
4319 28315671 2019-08-14 stsp }
4320 28315671 2019-08-14 stsp bca.lineno_cur = 1;
4321 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
4322 7ef28ff8 2019-08-14 stsp i = bca.nlines;
4323 7ef28ff8 2019-08-14 stsp while (i > 0) {
4324 7ef28ff8 2019-08-14 stsp i /= 10;
4325 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
4326 7ef28ff8 2019-08-14 stsp }
4327 82f6abb8 2019-08-14 stsp bca.repo = repo;
4328 7ef28ff8 2019-08-14 stsp
4329 0587e10c 2020-07-23 stsp error = got_blame(link_target ? link_target : in_repo_path, commit_id,
4330 0587e10c 2020-07-23 stsp repo, blame_cb, &bca, check_cancelled, NULL);
4331 404c43c4 2018-06-21 stsp done:
4332 66bea077 2018-08-02 stsp free(in_repo_path);
4333 0587e10c 2020-07-23 stsp free(link_target);
4334 66bea077 2018-08-02 stsp free(repo_path);
4335 66bea077 2018-08-02 stsp free(cwd);
4336 404c43c4 2018-06-21 stsp free(commit_id);
4337 28315671 2019-08-14 stsp free(obj_id);
4338 28315671 2019-08-14 stsp if (blob)
4339 28315671 2019-08-14 stsp got_object_blob_close(blob);
4340 0c06baac 2019-02-05 stsp if (worktree)
4341 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
4342 ad242220 2018-09-08 stsp if (repo) {
4343 ad242220 2018-09-08 stsp const struct got_error *repo_error;
4344 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
4345 ad242220 2018-09-08 stsp if (error == NULL)
4346 ad242220 2018-09-08 stsp error = repo_error;
4347 ad242220 2018-09-08 stsp }
4348 3affba96 2019-09-22 stsp if (bca.lines) {
4349 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
4350 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
4351 3affba96 2019-09-22 stsp free(bline->id_str);
4352 3affba96 2019-09-22 stsp free(bline->committer);
4353 3affba96 2019-09-22 stsp }
4354 3affba96 2019-09-22 stsp free(bca.lines);
4355 28315671 2019-08-14 stsp }
4356 28315671 2019-08-14 stsp free(bca.line_offsets);
4357 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
4358 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
4359 404c43c4 2018-06-21 stsp return error;
4360 5de5890b 2018-10-18 stsp }
4361 5de5890b 2018-10-18 stsp
4362 5de5890b 2018-10-18 stsp __dead static void
4363 5de5890b 2018-10-18 stsp usage_tree(void)
4364 5de5890b 2018-10-18 stsp {
4365 c1669e2e 2019-01-09 stsp fprintf(stderr,
4366 5d58be12 2020-05-17 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] [path]\n",
4367 5de5890b 2018-10-18 stsp getprogname());
4368 5de5890b 2018-10-18 stsp exit(1);
4369 5de5890b 2018-10-18 stsp }
4370 5de5890b 2018-10-18 stsp
4371 0d6c6ee3 2020-05-20 stsp static const struct got_error *
4372 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
4373 0d6c6ee3 2020-05-20 stsp const char *root_path, struct got_repository *repo)
4374 c1669e2e 2019-01-09 stsp {
4375 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
4376 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
4377 848d6979 2019-08-12 stsp const char *modestr = "";
4378 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
4379 0d6c6ee3 2020-05-20 stsp char *link_target = NULL;
4380 5de5890b 2018-10-18 stsp
4381 c1669e2e 2019-01-09 stsp path += strlen(root_path);
4382 c1669e2e 2019-01-09 stsp while (path[0] == '/')
4383 c1669e2e 2019-01-09 stsp path++;
4384 c1669e2e 2019-01-09 stsp
4385 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
4386 63c5ca5d 2019-08-24 stsp modestr = "$";
4387 0d6c6ee3 2020-05-20 stsp else if (S_ISLNK(mode)) {
4388 0d6c6ee3 2020-05-20 stsp int i;
4389 0d6c6ee3 2020-05-20 stsp
4390 0d6c6ee3 2020-05-20 stsp err = got_tree_entry_get_symlink_target(&link_target, te, repo);
4391 0d6c6ee3 2020-05-20 stsp if (err)
4392 0d6c6ee3 2020-05-20 stsp return err;
4393 0d6c6ee3 2020-05-20 stsp for (i = 0; i < strlen(link_target); i++) {
4394 0d6c6ee3 2020-05-20 stsp if (!isprint((unsigned char)link_target[i]))
4395 0d6c6ee3 2020-05-20 stsp link_target[i] = '?';
4396 0d6c6ee3 2020-05-20 stsp }
4397 0d6c6ee3 2020-05-20 stsp
4398 848d6979 2019-08-12 stsp modestr = "@";
4399 0d6c6ee3 2020-05-20 stsp }
4400 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4401 848d6979 2019-08-12 stsp modestr = "/";
4402 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4403 848d6979 2019-08-12 stsp modestr = "*";
4404 848d6979 2019-08-12 stsp
4405 0d6c6ee3 2020-05-20 stsp printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
4406 0d6c6ee3 2020-05-20 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
4407 0d6c6ee3 2020-05-20 stsp link_target ? " -> ": "", link_target ? link_target : "");
4408 0d6c6ee3 2020-05-20 stsp
4409 0d6c6ee3 2020-05-20 stsp free(link_target);
4410 0d6c6ee3 2020-05-20 stsp return NULL;
4411 c1669e2e 2019-01-09 stsp }
4412 c1669e2e 2019-01-09 stsp
4413 5de5890b 2018-10-18 stsp static const struct got_error *
4414 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4415 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4416 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4417 5de5890b 2018-10-18 stsp {
4418 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4419 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4420 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4421 56e0773d 2019-11-28 stsp int nentries, i;
4422 5de5890b 2018-10-18 stsp
4423 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4424 5de5890b 2018-10-18 stsp if (err)
4425 5de5890b 2018-10-18 stsp goto done;
4426 5de5890b 2018-10-18 stsp
4427 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4428 5de5890b 2018-10-18 stsp if (err)
4429 5de5890b 2018-10-18 stsp goto done;
4430 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4431 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4432 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4433 5de5890b 2018-10-18 stsp char *id = NULL;
4434 84453469 2018-11-11 stsp
4435 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4436 84453469 2018-11-11 stsp break;
4437 84453469 2018-11-11 stsp
4438 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4439 5de5890b 2018-10-18 stsp if (show_ids) {
4440 5de5890b 2018-10-18 stsp char *id_str;
4441 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4442 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4443 5de5890b 2018-10-18 stsp if (err)
4444 5de5890b 2018-10-18 stsp goto done;
4445 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4446 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4447 5de5890b 2018-10-18 stsp free(id_str);
4448 5de5890b 2018-10-18 stsp goto done;
4449 5de5890b 2018-10-18 stsp }
4450 5de5890b 2018-10-18 stsp free(id_str);
4451 5de5890b 2018-10-18 stsp }
4452 0d6c6ee3 2020-05-20 stsp err = print_entry(te, id, path, root_path, repo);
4453 5de5890b 2018-10-18 stsp free(id);
4454 0d6c6ee3 2020-05-20 stsp if (err)
4455 0d6c6ee3 2020-05-20 stsp goto done;
4456 c1669e2e 2019-01-09 stsp
4457 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4458 c1669e2e 2019-01-09 stsp char *child_path;
4459 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4460 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4461 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4462 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4463 c1669e2e 2019-01-09 stsp goto done;
4464 c1669e2e 2019-01-09 stsp }
4465 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4466 c1669e2e 2019-01-09 stsp root_path, repo);
4467 c1669e2e 2019-01-09 stsp free(child_path);
4468 c1669e2e 2019-01-09 stsp if (err)
4469 c1669e2e 2019-01-09 stsp goto done;
4470 c1669e2e 2019-01-09 stsp }
4471 5de5890b 2018-10-18 stsp }
4472 5de5890b 2018-10-18 stsp done:
4473 5de5890b 2018-10-18 stsp if (tree)
4474 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4475 5de5890b 2018-10-18 stsp free(tree_id);
4476 5de5890b 2018-10-18 stsp return err;
4477 404c43c4 2018-06-21 stsp }
4478 404c43c4 2018-06-21 stsp
4479 5de5890b 2018-10-18 stsp static const struct got_error *
4480 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4481 5de5890b 2018-10-18 stsp {
4482 5de5890b 2018-10-18 stsp const struct got_error *error;
4483 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4484 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4485 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4486 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4487 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4488 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4489 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4490 5de5890b 2018-10-18 stsp int ch;
4491 5de5890b 2018-10-18 stsp
4492 5de5890b 2018-10-18 stsp #ifndef PROFILE
4493 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4494 0f8d269b 2019-01-04 stsp NULL) == -1)
4495 5de5890b 2018-10-18 stsp err(1, "pledge");
4496 5de5890b 2018-10-18 stsp #endif
4497 5de5890b 2018-10-18 stsp
4498 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4499 5de5890b 2018-10-18 stsp switch (ch) {
4500 5de5890b 2018-10-18 stsp case 'c':
4501 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4502 5de5890b 2018-10-18 stsp break;
4503 5de5890b 2018-10-18 stsp case 'r':
4504 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4505 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4506 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4507 9ba1d308 2019-10-21 stsp optarg);
4508 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4509 5de5890b 2018-10-18 stsp break;
4510 5de5890b 2018-10-18 stsp case 'i':
4511 5de5890b 2018-10-18 stsp show_ids = 1;
4512 5de5890b 2018-10-18 stsp break;
4513 c1669e2e 2019-01-09 stsp case 'R':
4514 c1669e2e 2019-01-09 stsp recurse = 1;
4515 c1669e2e 2019-01-09 stsp break;
4516 5de5890b 2018-10-18 stsp default:
4517 2deda0b9 2019-03-07 stsp usage_tree();
4518 5de5890b 2018-10-18 stsp /* NOTREACHED */
4519 5de5890b 2018-10-18 stsp }
4520 5de5890b 2018-10-18 stsp }
4521 5de5890b 2018-10-18 stsp
4522 5de5890b 2018-10-18 stsp argc -= optind;
4523 5de5890b 2018-10-18 stsp argv += optind;
4524 5de5890b 2018-10-18 stsp
4525 5de5890b 2018-10-18 stsp if (argc == 1)
4526 5de5890b 2018-10-18 stsp path = argv[0];
4527 5de5890b 2018-10-18 stsp else if (argc > 1)
4528 5de5890b 2018-10-18 stsp usage_tree();
4529 5de5890b 2018-10-18 stsp else
4530 7a2c19d6 2019-02-05 stsp path = NULL;
4531 7a2c19d6 2019-02-05 stsp
4532 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4533 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4534 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4535 9bf7a39b 2019-02-05 stsp goto done;
4536 9bf7a39b 2019-02-05 stsp }
4537 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4538 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4539 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4540 7a2c19d6 2019-02-05 stsp goto done;
4541 7a2c19d6 2019-02-05 stsp else
4542 7a2c19d6 2019-02-05 stsp error = NULL;
4543 7a2c19d6 2019-02-05 stsp if (worktree) {
4544 7a2c19d6 2019-02-05 stsp repo_path =
4545 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4546 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4547 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4548 7a2c19d6 2019-02-05 stsp if (error)
4549 7a2c19d6 2019-02-05 stsp goto done;
4550 7a2c19d6 2019-02-05 stsp } else {
4551 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4552 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4553 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4554 7a2c19d6 2019-02-05 stsp goto done;
4555 7a2c19d6 2019-02-05 stsp }
4556 5de5890b 2018-10-18 stsp }
4557 5de5890b 2018-10-18 stsp }
4558 5de5890b 2018-10-18 stsp
4559 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4560 5de5890b 2018-10-18 stsp if (error != NULL)
4561 c02c541e 2019-03-29 stsp goto done;
4562 c02c541e 2019-03-29 stsp
4563 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4564 c02c541e 2019-03-29 stsp if (error)
4565 5de5890b 2018-10-18 stsp goto done;
4566 5de5890b 2018-10-18 stsp
4567 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4568 9bf7a39b 2019-02-05 stsp if (worktree) {
4569 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4570 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4571 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4572 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4573 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4574 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4575 9bf7a39b 2019-02-05 stsp goto done;
4576 9bf7a39b 2019-02-05 stsp }
4577 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4578 9bf7a39b 2019-02-05 stsp free(p);
4579 9bf7a39b 2019-02-05 stsp if (error)
4580 9bf7a39b 2019-02-05 stsp goto done;
4581 9bf7a39b 2019-02-05 stsp } else
4582 9bf7a39b 2019-02-05 stsp path = "/";
4583 9bf7a39b 2019-02-05 stsp }
4584 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4585 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4586 9bf7a39b 2019-02-05 stsp if (error != NULL)
4587 9bf7a39b 2019-02-05 stsp goto done;
4588 9bf7a39b 2019-02-05 stsp }
4589 5de5890b 2018-10-18 stsp
4590 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4591 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4592 4e0a20a4 2020-03-23 tracey if (worktree)
4593 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4594 4e0a20a4 2020-03-23 tracey else
4595 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4596 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4597 5de5890b 2018-10-18 stsp if (error != NULL)
4598 5de5890b 2018-10-18 stsp goto done;
4599 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4600 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4601 5de5890b 2018-10-18 stsp if (error != NULL)
4602 5de5890b 2018-10-18 stsp goto done;
4603 5de5890b 2018-10-18 stsp } else {
4604 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4605 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4606 30837e32 2019-07-25 stsp if (error)
4607 5de5890b 2018-10-18 stsp goto done;
4608 5de5890b 2018-10-18 stsp }
4609 5de5890b 2018-10-18 stsp
4610 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4611 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4612 5de5890b 2018-10-18 stsp done:
4613 5de5890b 2018-10-18 stsp free(in_repo_path);
4614 5de5890b 2018-10-18 stsp free(repo_path);
4615 5de5890b 2018-10-18 stsp free(cwd);
4616 5de5890b 2018-10-18 stsp free(commit_id);
4617 7a2c19d6 2019-02-05 stsp if (worktree)
4618 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4619 5de5890b 2018-10-18 stsp if (repo) {
4620 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4621 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4622 5de5890b 2018-10-18 stsp if (error == NULL)
4623 5de5890b 2018-10-18 stsp error = repo_error;
4624 5de5890b 2018-10-18 stsp }
4625 5de5890b 2018-10-18 stsp return error;
4626 5de5890b 2018-10-18 stsp }
4627 5de5890b 2018-10-18 stsp
4628 6bad629b 2019-02-04 stsp __dead static void
4629 6bad629b 2019-02-04 stsp usage_status(void)
4630 6bad629b 2019-02-04 stsp {
4631 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4632 6bad629b 2019-02-04 stsp exit(1);
4633 6bad629b 2019-02-04 stsp }
4634 5c860e29 2018-03-12 stsp
4635 b72f483a 2019-02-05 stsp static const struct got_error *
4636 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4637 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4638 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4639 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4640 6bad629b 2019-02-04 stsp {
4641 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4642 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4643 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4644 b72f483a 2019-02-05 stsp return NULL;
4645 6bad629b 2019-02-04 stsp }
4646 5c860e29 2018-03-12 stsp
4647 6bad629b 2019-02-04 stsp static const struct got_error *
4648 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4649 6bad629b 2019-02-04 stsp {
4650 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4651 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4652 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4653 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4654 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4655 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4656 a5edda0a 2019-07-27 stsp int ch;
4657 5c860e29 2018-03-12 stsp
4658 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4659 72ea6654 2019-07-27 stsp
4660 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4661 6bad629b 2019-02-04 stsp switch (ch) {
4662 5c860e29 2018-03-12 stsp default:
4663 2deda0b9 2019-03-07 stsp usage_status();
4664 6bad629b 2019-02-04 stsp /* NOTREACHED */
4665 5c860e29 2018-03-12 stsp }
4666 5c860e29 2018-03-12 stsp }
4667 5c860e29 2018-03-12 stsp
4668 6bad629b 2019-02-04 stsp argc -= optind;
4669 6bad629b 2019-02-04 stsp argv += optind;
4670 5c860e29 2018-03-12 stsp
4671 6bad629b 2019-02-04 stsp #ifndef PROFILE
4672 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4673 6bad629b 2019-02-04 stsp NULL) == -1)
4674 6bad629b 2019-02-04 stsp err(1, "pledge");
4675 f42b1b34 2018-03-12 stsp #endif
4676 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4677 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4678 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4679 927df6b7 2019-02-10 stsp goto done;
4680 927df6b7 2019-02-10 stsp }
4681 927df6b7 2019-02-10 stsp
4682 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4683 fa51e947 2020-03-27 stsp if (error) {
4684 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
4685 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "status", cwd);
4686 a5edda0a 2019-07-27 stsp goto done;
4687 fa51e947 2020-03-27 stsp }
4688 6bad629b 2019-02-04 stsp
4689 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4690 c9956ddf 2019-09-08 stsp NULL);
4691 6bad629b 2019-02-04 stsp if (error != NULL)
4692 6bad629b 2019-02-04 stsp goto done;
4693 6bad629b 2019-02-04 stsp
4694 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4695 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4696 087fb88c 2019-08-04 stsp if (error)
4697 087fb88c 2019-08-04 stsp goto done;
4698 087fb88c 2019-08-04 stsp
4699 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4700 6bad629b 2019-02-04 stsp if (error)
4701 6bad629b 2019-02-04 stsp goto done;
4702 6bad629b 2019-02-04 stsp
4703 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4704 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4705 6bad629b 2019-02-04 stsp done:
4706 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4707 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4708 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4709 927df6b7 2019-02-10 stsp free(cwd);
4710 d0eebce4 2019-03-11 stsp return error;
4711 d0eebce4 2019-03-11 stsp }
4712 d0eebce4 2019-03-11 stsp
4713 d0eebce4 2019-03-11 stsp __dead static void
4714 d0eebce4 2019-03-11 stsp usage_ref(void)
4715 d0eebce4 2019-03-11 stsp {
4716 d0eebce4 2019-03-11 stsp fprintf(stderr,
4717 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4718 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4719 d0eebce4 2019-03-11 stsp getprogname());
4720 d0eebce4 2019-03-11 stsp exit(1);
4721 d0eebce4 2019-03-11 stsp }
4722 d0eebce4 2019-03-11 stsp
4723 d0eebce4 2019-03-11 stsp static const struct got_error *
4724 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4725 d0eebce4 2019-03-11 stsp {
4726 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4727 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4728 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4729 d0eebce4 2019-03-11 stsp
4730 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4731 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4732 d0eebce4 2019-03-11 stsp if (err)
4733 d0eebce4 2019-03-11 stsp return err;
4734 d0eebce4 2019-03-11 stsp
4735 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4736 d0eebce4 2019-03-11 stsp char *refstr;
4737 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4738 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4739 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4740 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4741 d0eebce4 2019-03-11 stsp free(refstr);
4742 d0eebce4 2019-03-11 stsp }
4743 d0eebce4 2019-03-11 stsp
4744 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4745 d0eebce4 2019-03-11 stsp return NULL;
4746 d0eebce4 2019-03-11 stsp }
4747 d0eebce4 2019-03-11 stsp
4748 d0eebce4 2019-03-11 stsp static const struct got_error *
4749 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4750 d0eebce4 2019-03-11 stsp {
4751 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4752 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4753 d0eebce4 2019-03-11 stsp
4754 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4755 d0eebce4 2019-03-11 stsp if (err)
4756 d0eebce4 2019-03-11 stsp return err;
4757 d0eebce4 2019-03-11 stsp
4758 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4759 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4760 d0eebce4 2019-03-11 stsp return err;
4761 d0eebce4 2019-03-11 stsp }
4762 d0eebce4 2019-03-11 stsp
4763 d0eebce4 2019-03-11 stsp static const struct got_error *
4764 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4765 d0eebce4 2019-03-11 stsp {
4766 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4767 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4768 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4769 d1644381 2019-07-14 stsp
4770 d1644381 2019-07-14 stsp /*
4771 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4772 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4773 d1644381 2019-07-14 stsp * an unintended typo.
4774 d1644381 2019-07-14 stsp */
4775 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4776 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4777 d0eebce4 2019-03-11 stsp
4778 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4779 dd88155e 2019-06-29 stsp repo);
4780 d83d9d5c 2019-05-13 stsp if (err) {
4781 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4782 d0eebce4 2019-03-11 stsp
4783 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4784 d83d9d5c 2019-05-13 stsp return err;
4785 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4786 d83d9d5c 2019-05-13 stsp if (err)
4787 d83d9d5c 2019-05-13 stsp return err;
4788 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4789 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4790 d83d9d5c 2019-05-13 stsp if (err)
4791 d83d9d5c 2019-05-13 stsp return err;
4792 d83d9d5c 2019-05-13 stsp }
4793 d83d9d5c 2019-05-13 stsp
4794 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4795 d0eebce4 2019-03-11 stsp if (err)
4796 d0eebce4 2019-03-11 stsp goto done;
4797 d0eebce4 2019-03-11 stsp
4798 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4799 d0eebce4 2019-03-11 stsp done:
4800 d0eebce4 2019-03-11 stsp if (ref)
4801 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4802 d0eebce4 2019-03-11 stsp free(id);
4803 d1c1ae5f 2019-08-12 stsp return err;
4804 d1c1ae5f 2019-08-12 stsp }
4805 d1c1ae5f 2019-08-12 stsp
4806 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4807 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4808 d1c1ae5f 2019-08-12 stsp {
4809 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4810 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4811 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4812 d1c1ae5f 2019-08-12 stsp
4813 d1c1ae5f 2019-08-12 stsp /*
4814 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4815 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4816 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4817 d1c1ae5f 2019-08-12 stsp */
4818 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4819 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4820 d1c1ae5f 2019-08-12 stsp
4821 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4822 d1c1ae5f 2019-08-12 stsp if (err)
4823 d1c1ae5f 2019-08-12 stsp return err;
4824 d1c1ae5f 2019-08-12 stsp
4825 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4826 d1c1ae5f 2019-08-12 stsp if (err)
4827 d1c1ae5f 2019-08-12 stsp goto done;
4828 d1c1ae5f 2019-08-12 stsp
4829 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4830 d1c1ae5f 2019-08-12 stsp done:
4831 d1c1ae5f 2019-08-12 stsp if (target_ref)
4832 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4833 d1c1ae5f 2019-08-12 stsp if (ref)
4834 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4835 d0eebce4 2019-03-11 stsp return err;
4836 d0eebce4 2019-03-11 stsp }
4837 d0eebce4 2019-03-11 stsp
4838 d0eebce4 2019-03-11 stsp static const struct got_error *
4839 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4840 d0eebce4 2019-03-11 stsp {
4841 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4842 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4843 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4844 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4845 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4846 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4847 b2070a3f 2020-03-22 stsp char *refname = NULL;
4848 d0eebce4 2019-03-11 stsp
4849 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4850 d0eebce4 2019-03-11 stsp switch (ch) {
4851 e31abbf2 2020-03-22 stsp case 'c':
4852 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4853 e31abbf2 2020-03-22 stsp break;
4854 d0eebce4 2019-03-11 stsp case 'd':
4855 e31abbf2 2020-03-22 stsp do_delete = 1;
4856 d0eebce4 2019-03-11 stsp break;
4857 d0eebce4 2019-03-11 stsp case 'r':
4858 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4859 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4860 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4861 9ba1d308 2019-10-21 stsp optarg);
4862 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4863 d0eebce4 2019-03-11 stsp break;
4864 d0eebce4 2019-03-11 stsp case 'l':
4865 d0eebce4 2019-03-11 stsp do_list = 1;
4866 d1c1ae5f 2019-08-12 stsp break;
4867 d1c1ae5f 2019-08-12 stsp case 's':
4868 e31abbf2 2020-03-22 stsp symref_target = optarg;
4869 d0eebce4 2019-03-11 stsp break;
4870 d0eebce4 2019-03-11 stsp default:
4871 d0eebce4 2019-03-11 stsp usage_ref();
4872 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4873 d0eebce4 2019-03-11 stsp }
4874 d0eebce4 2019-03-11 stsp }
4875 d0eebce4 2019-03-11 stsp
4876 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4877 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4878 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4879 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4880 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4881 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4882 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4883 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4884 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4885 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4886 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4887 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4888 d0eebce4 2019-03-11 stsp
4889 d0eebce4 2019-03-11 stsp argc -= optind;
4890 d0eebce4 2019-03-11 stsp argv += optind;
4891 d0eebce4 2019-03-11 stsp
4892 e31abbf2 2020-03-22 stsp if (do_list) {
4893 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4894 d0eebce4 2019-03-11 stsp usage_ref();
4895 b2070a3f 2020-03-22 stsp if (argc == 1) {
4896 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4897 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4898 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4899 b2070a3f 2020-03-22 stsp goto done;
4900 b2070a3f 2020-03-22 stsp }
4901 b2070a3f 2020-03-22 stsp }
4902 e31abbf2 2020-03-22 stsp } else {
4903 e31abbf2 2020-03-22 stsp if (argc != 1)
4904 e31abbf2 2020-03-22 stsp usage_ref();
4905 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4906 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4907 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4908 b2070a3f 2020-03-22 stsp goto done;
4909 b2070a3f 2020-03-22 stsp }
4910 e31abbf2 2020-03-22 stsp }
4911 b2070a3f 2020-03-22 stsp
4912 b2070a3f 2020-03-22 stsp if (refname)
4913 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4914 d0eebce4 2019-03-11 stsp
4915 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4916 e0b57350 2019-03-12 stsp if (do_list) {
4917 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4918 e0b57350 2019-03-12 stsp NULL) == -1)
4919 e0b57350 2019-03-12 stsp err(1, "pledge");
4920 e0b57350 2019-03-12 stsp } else {
4921 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4922 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4923 e0b57350 2019-03-12 stsp err(1, "pledge");
4924 e0b57350 2019-03-12 stsp }
4925 d0eebce4 2019-03-11 stsp #endif
4926 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4927 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4928 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4929 d0eebce4 2019-03-11 stsp goto done;
4930 d0eebce4 2019-03-11 stsp }
4931 d0eebce4 2019-03-11 stsp
4932 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4933 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4934 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4935 d0eebce4 2019-03-11 stsp goto done;
4936 d0eebce4 2019-03-11 stsp else
4937 d0eebce4 2019-03-11 stsp error = NULL;
4938 d0eebce4 2019-03-11 stsp if (worktree) {
4939 d0eebce4 2019-03-11 stsp repo_path =
4940 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4941 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4942 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4943 d0eebce4 2019-03-11 stsp if (error)
4944 d0eebce4 2019-03-11 stsp goto done;
4945 d0eebce4 2019-03-11 stsp } else {
4946 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4947 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4948 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4949 d0eebce4 2019-03-11 stsp goto done;
4950 d0eebce4 2019-03-11 stsp }
4951 d0eebce4 2019-03-11 stsp }
4952 d0eebce4 2019-03-11 stsp }
4953 d0eebce4 2019-03-11 stsp
4954 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4955 d0eebce4 2019-03-11 stsp if (error != NULL)
4956 d0eebce4 2019-03-11 stsp goto done;
4957 d0eebce4 2019-03-11 stsp
4958 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4959 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4960 c02c541e 2019-03-29 stsp if (error)
4961 c02c541e 2019-03-29 stsp goto done;
4962 c02c541e 2019-03-29 stsp
4963 d0eebce4 2019-03-11 stsp if (do_list)
4964 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4965 e31abbf2 2020-03-22 stsp else if (do_delete)
4966 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4967 e31abbf2 2020-03-22 stsp else if (symref_target)
4968 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4969 e31abbf2 2020-03-22 stsp else {
4970 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4971 e31abbf2 2020-03-22 stsp usage_ref();
4972 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4973 e31abbf2 2020-03-22 stsp }
4974 4e759de4 2019-06-26 stsp done:
4975 b2070a3f 2020-03-22 stsp free(refname);
4976 4e759de4 2019-06-26 stsp if (repo)
4977 4e759de4 2019-06-26 stsp got_repo_close(repo);
4978 4e759de4 2019-06-26 stsp if (worktree)
4979 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4980 4e759de4 2019-06-26 stsp free(cwd);
4981 4e759de4 2019-06-26 stsp free(repo_path);
4982 4e759de4 2019-06-26 stsp return error;
4983 4e759de4 2019-06-26 stsp }
4984 4e759de4 2019-06-26 stsp
4985 4e759de4 2019-06-26 stsp __dead static void
4986 4e759de4 2019-06-26 stsp usage_branch(void)
4987 4e759de4 2019-06-26 stsp {
4988 4e759de4 2019-06-26 stsp fprintf(stderr,
4989 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4990 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4991 4e759de4 2019-06-26 stsp exit(1);
4992 4e759de4 2019-06-26 stsp }
4993 4e759de4 2019-06-26 stsp
4994 4e759de4 2019-06-26 stsp static const struct got_error *
4995 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4996 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4997 4e99b47e 2019-10-04 stsp {
4998 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4999 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
5000 4e99b47e 2019-10-04 stsp char *refstr;
5001 4e99b47e 2019-10-04 stsp
5002 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
5003 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
5004 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
5005 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
5006 4e99b47e 2019-10-04 stsp
5007 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
5008 4e99b47e 2019-10-04 stsp if (err)
5009 4e99b47e 2019-10-04 stsp return err;
5010 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
5011 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
5012 4e99b47e 2019-10-04 stsp marker = "* ";
5013 4e99b47e 2019-10-04 stsp else
5014 4e99b47e 2019-10-04 stsp marker = "~ ";
5015 4e99b47e 2019-10-04 stsp free(id);
5016 4e99b47e 2019-10-04 stsp }
5017 4e99b47e 2019-10-04 stsp
5018 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5019 4e99b47e 2019-10-04 stsp refname += 11;
5020 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5021 4e99b47e 2019-10-04 stsp refname += 18;
5022 4e99b47e 2019-10-04 stsp
5023 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
5024 4e99b47e 2019-10-04 stsp if (refstr == NULL)
5025 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
5026 4e99b47e 2019-10-04 stsp
5027 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
5028 4e99b47e 2019-10-04 stsp free(refstr);
5029 4e99b47e 2019-10-04 stsp return NULL;
5030 4e99b47e 2019-10-04 stsp }
5031 4e99b47e 2019-10-04 stsp
5032 4e99b47e 2019-10-04 stsp static const struct got_error *
5033 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
5034 ad89fa31 2019-10-04 stsp {
5035 ad89fa31 2019-10-04 stsp const char *refname;
5036 ad89fa31 2019-10-04 stsp
5037 ad89fa31 2019-10-04 stsp if (worktree == NULL)
5038 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
5039 ad89fa31 2019-10-04 stsp
5040 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
5041 ad89fa31 2019-10-04 stsp
5042 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
5043 ad89fa31 2019-10-04 stsp refname += 11;
5044 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
5045 ad89fa31 2019-10-04 stsp refname += 18;
5046 ad89fa31 2019-10-04 stsp
5047 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
5048 ad89fa31 2019-10-04 stsp
5049 ad89fa31 2019-10-04 stsp return NULL;
5050 ad89fa31 2019-10-04 stsp }
5051 ad89fa31 2019-10-04 stsp
5052 ad89fa31 2019-10-04 stsp static const struct got_error *
5053 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
5054 4e759de4 2019-06-26 stsp {
5055 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
5056 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
5057 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
5058 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
5059 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
5060 4e759de4 2019-06-26 stsp
5061 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
5062 ba882ee3 2019-07-11 stsp
5063 4e99b47e 2019-10-04 stsp if (worktree) {
5064 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
5065 4e99b47e 2019-10-04 stsp worktree);
5066 4e99b47e 2019-10-04 stsp if (err)
5067 4e99b47e 2019-10-04 stsp return err;
5068 4e99b47e 2019-10-04 stsp
5069 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
5070 4e99b47e 2019-10-04 stsp worktree);
5071 4e99b47e 2019-10-04 stsp if (err)
5072 4e99b47e 2019-10-04 stsp return err;
5073 4e99b47e 2019-10-04 stsp
5074 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
5075 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
5076 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
5077 4e99b47e 2019-10-04 stsp if (err)
5078 4e99b47e 2019-10-04 stsp return err;
5079 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
5080 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
5081 4e99b47e 2019-10-04 stsp }
5082 4e99b47e 2019-10-04 stsp }
5083 4e99b47e 2019-10-04 stsp
5084 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
5085 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
5086 4e759de4 2019-06-26 stsp if (err)
5087 4e759de4 2019-06-26 stsp return err;
5088 4e759de4 2019-06-26 stsp
5089 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
5090 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
5091 4e759de4 2019-06-26 stsp
5092 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
5093 4e759de4 2019-06-26 stsp return NULL;
5094 4e759de4 2019-06-26 stsp }
5095 4e759de4 2019-06-26 stsp
5096 4e759de4 2019-06-26 stsp static const struct got_error *
5097 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
5098 45cd4e47 2019-08-25 stsp const char *branch_name)
5099 4e759de4 2019-06-26 stsp {
5100 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5101 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
5102 4e759de4 2019-06-26 stsp char *refname;
5103 4e759de4 2019-06-26 stsp
5104 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
5105 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
5106 4e759de4 2019-06-26 stsp
5107 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5108 4e759de4 2019-06-26 stsp if (err)
5109 4e759de4 2019-06-26 stsp goto done;
5110 4e759de4 2019-06-26 stsp
5111 45cd4e47 2019-08-25 stsp if (worktree &&
5112 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
5113 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
5114 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
5115 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
5116 45cd4e47 2019-08-25 stsp goto done;
5117 45cd4e47 2019-08-25 stsp }
5118 45cd4e47 2019-08-25 stsp
5119 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
5120 4e759de4 2019-06-26 stsp done:
5121 45cd4e47 2019-08-25 stsp if (ref)
5122 45cd4e47 2019-08-25 stsp got_ref_close(ref);
5123 4e759de4 2019-06-26 stsp free(refname);
5124 4e759de4 2019-06-26 stsp return err;
5125 4e759de4 2019-06-26 stsp }
5126 4e759de4 2019-06-26 stsp
5127 4e759de4 2019-06-26 stsp static const struct got_error *
5128 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
5129 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
5130 4e759de4 2019-06-26 stsp {
5131 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
5132 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
5133 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
5134 d3f84d51 2019-07-11 stsp
5135 d3f84d51 2019-07-11 stsp /*
5136 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
5137 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
5138 d3f84d51 2019-07-11 stsp * an unintended typo.
5139 d3f84d51 2019-07-11 stsp */
5140 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
5141 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
5142 4e759de4 2019-06-26 stsp
5143 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
5144 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
5145 4e759de4 2019-06-26 stsp goto done;
5146 4e759de4 2019-06-26 stsp }
5147 4e759de4 2019-06-26 stsp
5148 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
5149 4e759de4 2019-06-26 stsp if (err == NULL) {
5150 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
5151 4e759de4 2019-06-26 stsp goto done;
5152 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
5153 4e759de4 2019-06-26 stsp goto done;
5154 4e759de4 2019-06-26 stsp
5155 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
5156 4e759de4 2019-06-26 stsp if (err)
5157 4e759de4 2019-06-26 stsp goto done;
5158 4e759de4 2019-06-26 stsp
5159 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
5160 d0eebce4 2019-03-11 stsp done:
5161 4e759de4 2019-06-26 stsp if (ref)
5162 4e759de4 2019-06-26 stsp got_ref_close(ref);
5163 4e759de4 2019-06-26 stsp free(base_refname);
5164 4e759de4 2019-06-26 stsp free(refname);
5165 4e759de4 2019-06-26 stsp return err;
5166 4e759de4 2019-06-26 stsp }
5167 4e759de4 2019-06-26 stsp
5168 4e759de4 2019-06-26 stsp static const struct got_error *
5169 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
5170 4e759de4 2019-06-26 stsp {
5171 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
5172 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
5173 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
5174 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
5175 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
5176 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
5177 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
5178 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
5179 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
5180 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
5181 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
5182 4e759de4 2019-06-26 stsp
5183 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
5184 da76fce2 2020-02-24 stsp
5185 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
5186 4e759de4 2019-06-26 stsp switch (ch) {
5187 a74f7e83 2019-11-10 stsp case 'c':
5188 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
5189 a74f7e83 2019-11-10 stsp break;
5190 4e759de4 2019-06-26 stsp case 'd':
5191 4e759de4 2019-06-26 stsp delref = optarg;
5192 4e759de4 2019-06-26 stsp break;
5193 4e759de4 2019-06-26 stsp case 'r':
5194 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
5195 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5196 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5197 9ba1d308 2019-10-21 stsp optarg);
5198 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
5199 4e759de4 2019-06-26 stsp break;
5200 4e759de4 2019-06-26 stsp case 'l':
5201 4e759de4 2019-06-26 stsp do_list = 1;
5202 da76fce2 2020-02-24 stsp break;
5203 da76fce2 2020-02-24 stsp case 'n':
5204 da76fce2 2020-02-24 stsp do_update = 0;
5205 4e759de4 2019-06-26 stsp break;
5206 4e759de4 2019-06-26 stsp default:
5207 4e759de4 2019-06-26 stsp usage_branch();
5208 4e759de4 2019-06-26 stsp /* NOTREACHED */
5209 4e759de4 2019-06-26 stsp }
5210 4e759de4 2019-06-26 stsp }
5211 4e759de4 2019-06-26 stsp
5212 4e759de4 2019-06-26 stsp if (do_list && delref)
5213 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
5214 4e759de4 2019-06-26 stsp
5215 4e759de4 2019-06-26 stsp argc -= optind;
5216 4e759de4 2019-06-26 stsp argv += optind;
5217 ad89fa31 2019-10-04 stsp
5218 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
5219 ad89fa31 2019-10-04 stsp do_show = 1;
5220 4e759de4 2019-06-26 stsp
5221 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
5222 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
5223 a74f7e83 2019-11-10 stsp
5224 4e759de4 2019-06-26 stsp if (do_list || delref) {
5225 4e759de4 2019-06-26 stsp if (argc > 0)
5226 4e759de4 2019-06-26 stsp usage_branch();
5227 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
5228 4e759de4 2019-06-26 stsp usage_branch();
5229 4e759de4 2019-06-26 stsp
5230 4e759de4 2019-06-26 stsp #ifndef PROFILE
5231 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
5232 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5233 4e759de4 2019-06-26 stsp NULL) == -1)
5234 4e759de4 2019-06-26 stsp err(1, "pledge");
5235 4e759de4 2019-06-26 stsp } else {
5236 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5237 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
5238 4e759de4 2019-06-26 stsp err(1, "pledge");
5239 4e759de4 2019-06-26 stsp }
5240 4e759de4 2019-06-26 stsp #endif
5241 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
5242 4e759de4 2019-06-26 stsp if (cwd == NULL) {
5243 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
5244 4e759de4 2019-06-26 stsp goto done;
5245 4e759de4 2019-06-26 stsp }
5246 4e759de4 2019-06-26 stsp
5247 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5248 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
5249 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5250 4e759de4 2019-06-26 stsp goto done;
5251 4e759de4 2019-06-26 stsp else
5252 4e759de4 2019-06-26 stsp error = NULL;
5253 4e759de4 2019-06-26 stsp if (worktree) {
5254 4e759de4 2019-06-26 stsp repo_path =
5255 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
5256 4e759de4 2019-06-26 stsp if (repo_path == NULL)
5257 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5258 4e759de4 2019-06-26 stsp if (error)
5259 4e759de4 2019-06-26 stsp goto done;
5260 4e759de4 2019-06-26 stsp } else {
5261 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
5262 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
5263 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
5264 4e759de4 2019-06-26 stsp goto done;
5265 4e759de4 2019-06-26 stsp }
5266 4e759de4 2019-06-26 stsp }
5267 4e759de4 2019-06-26 stsp }
5268 4e759de4 2019-06-26 stsp
5269 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5270 4e759de4 2019-06-26 stsp if (error != NULL)
5271 4e759de4 2019-06-26 stsp goto done;
5272 4e759de4 2019-06-26 stsp
5273 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
5274 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
5275 4e759de4 2019-06-26 stsp if (error)
5276 4e759de4 2019-06-26 stsp goto done;
5277 4e759de4 2019-06-26 stsp
5278 ad89fa31 2019-10-04 stsp if (do_show)
5279 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
5280 ad89fa31 2019-10-04 stsp else if (do_list)
5281 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
5282 4e759de4 2019-06-26 stsp else if (delref)
5283 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
5284 4e759de4 2019-06-26 stsp else {
5285 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
5286 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
5287 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
5288 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
5289 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
5290 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
5291 a74f7e83 2019-11-10 stsp if (error)
5292 a74f7e83 2019-11-10 stsp goto done;
5293 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
5294 da76fce2 2020-02-24 stsp if (error)
5295 da76fce2 2020-02-24 stsp goto done;
5296 da76fce2 2020-02-24 stsp if (worktree && do_update) {
5297 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
5298 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
5299 da76fce2 2020-02-24 stsp
5300 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
5301 da76fce2 2020-02-24 stsp if (error)
5302 da76fce2 2020-02-24 stsp goto done;
5303 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
5304 da76fce2 2020-02-24 stsp worktree);
5305 da76fce2 2020-02-24 stsp if (error)
5306 da76fce2 2020-02-24 stsp goto done;
5307 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
5308 da76fce2 2020-02-24 stsp == -1) {
5309 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
5310 da76fce2 2020-02-24 stsp goto done;
5311 da76fce2 2020-02-24 stsp }
5312 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
5313 da76fce2 2020-02-24 stsp free(branch_refname);
5314 da76fce2 2020-02-24 stsp if (error)
5315 da76fce2 2020-02-24 stsp goto done;
5316 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
5317 da76fce2 2020-02-24 stsp repo);
5318 da76fce2 2020-02-24 stsp if (error)
5319 da76fce2 2020-02-24 stsp goto done;
5320 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
5321 da76fce2 2020-02-24 stsp commit_id);
5322 da76fce2 2020-02-24 stsp if (error)
5323 da76fce2 2020-02-24 stsp goto done;
5324 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
5325 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
5326 9627c110 2020-04-18 stsp repo, update_progress, &upa, check_cancelled,
5327 9627c110 2020-04-18 stsp NULL);
5328 da76fce2 2020-02-24 stsp if (error)
5329 da76fce2 2020-02-24 stsp goto done;
5330 9627c110 2020-04-18 stsp if (upa.did_something)
5331 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
5332 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
5333 da76fce2 2020-02-24 stsp }
5334 4e759de4 2019-06-26 stsp }
5335 4e759de4 2019-06-26 stsp done:
5336 da76fce2 2020-02-24 stsp if (ref)
5337 da76fce2 2020-02-24 stsp got_ref_close(ref);
5338 d0eebce4 2019-03-11 stsp if (repo)
5339 d0eebce4 2019-03-11 stsp got_repo_close(repo);
5340 d0eebce4 2019-03-11 stsp if (worktree)
5341 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
5342 d0eebce4 2019-03-11 stsp free(cwd);
5343 d0eebce4 2019-03-11 stsp free(repo_path);
5344 da76fce2 2020-02-24 stsp free(commit_id);
5345 da76fce2 2020-02-24 stsp free(commit_id_str);
5346 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
5347 da76fce2 2020-02-24 stsp free((char *)pe->path);
5348 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
5349 d00136be 2019-03-26 stsp return error;
5350 d00136be 2019-03-26 stsp }
5351 d00136be 2019-03-26 stsp
5352 8e7bd50a 2019-08-22 stsp
5353 d00136be 2019-03-26 stsp __dead static void
5354 8e7bd50a 2019-08-22 stsp usage_tag(void)
5355 8e7bd50a 2019-08-22 stsp {
5356 8e7bd50a 2019-08-22 stsp fprintf(stderr,
5357 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
5358 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
5359 8e7bd50a 2019-08-22 stsp exit(1);
5360 b8bad2ba 2019-08-23 stsp }
5361 b8bad2ba 2019-08-23 stsp
5362 b8bad2ba 2019-08-23 stsp #if 0
5363 b8bad2ba 2019-08-23 stsp static const struct got_error *
5364 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
5365 b8bad2ba 2019-08-23 stsp {
5366 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
5367 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
5368 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
5369 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
5370 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
5371 b8bad2ba 2019-08-23 stsp
5372 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
5373 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
5374 b8bad2ba 2019-08-23 stsp if (se == NULL) {
5375 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5376 b8bad2ba 2019-08-23 stsp if (err)
5377 b8bad2ba 2019-08-23 stsp return err;
5378 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
5379 b8bad2ba 2019-08-23 stsp continue;
5380 b8bad2ba 2019-08-23 stsp } else {
5381 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
5382 b8bad2ba 2019-08-23 stsp if (err)
5383 b8bad2ba 2019-08-23 stsp break;
5384 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
5385 b8bad2ba 2019-08-23 stsp free(re_id);
5386 b8bad2ba 2019-08-23 stsp if (err)
5387 b8bad2ba 2019-08-23 stsp break;
5388 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
5389 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
5390 b8bad2ba 2019-08-23 stsp }
5391 b8bad2ba 2019-08-23 stsp
5392 b8bad2ba 2019-08-23 stsp while (se) {
5393 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
5394 b8bad2ba 2019-08-23 stsp if (err)
5395 b8bad2ba 2019-08-23 stsp break;
5396 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
5397 b8bad2ba 2019-08-23 stsp free(se_id);
5398 b8bad2ba 2019-08-23 stsp if (err)
5399 b8bad2ba 2019-08-23 stsp break;
5400 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
5401 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
5402 b8bad2ba 2019-08-23 stsp
5403 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
5404 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
5405 b8bad2ba 2019-08-23 stsp if (err)
5406 b8bad2ba 2019-08-23 stsp return err;
5407 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
5408 b8bad2ba 2019-08-23 stsp break;
5409 b8bad2ba 2019-08-23 stsp }
5410 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
5411 b8bad2ba 2019-08-23 stsp continue;
5412 b8bad2ba 2019-08-23 stsp }
5413 b8bad2ba 2019-08-23 stsp }
5414 b8bad2ba 2019-08-23 stsp done:
5415 b8bad2ba 2019-08-23 stsp return err;
5416 8e7bd50a 2019-08-22 stsp }
5417 b8bad2ba 2019-08-23 stsp #endif
5418 b8bad2ba 2019-08-23 stsp
5419 b8bad2ba 2019-08-23 stsp static const struct got_error *
5420 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5421 8e7bd50a 2019-08-22 stsp {
5422 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5423 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5424 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5425 8e7bd50a 2019-08-22 stsp
5426 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5427 8e7bd50a 2019-08-22 stsp
5428 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5429 8e7bd50a 2019-08-22 stsp if (err)
5430 8e7bd50a 2019-08-22 stsp return err;
5431 8e7bd50a 2019-08-22 stsp
5432 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5433 8e7bd50a 2019-08-22 stsp const char *refname;
5434 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5435 8e7bd50a 2019-08-22 stsp char datebuf[26];
5436 d4efa91b 2020-01-14 stsp const char *tagger;
5437 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5438 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5439 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5440 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5441 8e7bd50a 2019-08-22 stsp
5442 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5443 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5444 8e7bd50a 2019-08-22 stsp continue;
5445 8e7bd50a 2019-08-22 stsp refname += 10;
5446 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5447 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5448 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5449 8e7bd50a 2019-08-22 stsp break;
5450 8e7bd50a 2019-08-22 stsp }
5451 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5452 8e7bd50a 2019-08-22 stsp free(refstr);
5453 8e7bd50a 2019-08-22 stsp
5454 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5455 8e7bd50a 2019-08-22 stsp if (err)
5456 8e7bd50a 2019-08-22 stsp break;
5457 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5458 d4efa91b 2020-01-14 stsp if (err) {
5459 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5460 d4efa91b 2020-01-14 stsp free(id);
5461 d4efa91b 2020-01-14 stsp break;
5462 d4efa91b 2020-01-14 stsp }
5463 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5464 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5465 d4efa91b 2020-01-14 stsp if (err) {
5466 d4efa91b 2020-01-14 stsp free(id);
5467 d4efa91b 2020-01-14 stsp break;
5468 d4efa91b 2020-01-14 stsp }
5469 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5470 d4efa91b 2020-01-14 stsp tagger_time =
5471 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5472 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5473 d4efa91b 2020-01-14 stsp free(id);
5474 d4efa91b 2020-01-14 stsp if (err)
5475 d4efa91b 2020-01-14 stsp break;
5476 d4efa91b 2020-01-14 stsp } else {
5477 d4efa91b 2020-01-14 stsp free(id);
5478 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5479 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5480 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5481 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5482 d4efa91b 2020-01-14 stsp if (err)
5483 d4efa91b 2020-01-14 stsp break;
5484 d4efa91b 2020-01-14 stsp }
5485 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5486 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5487 2417344c 2019-08-23 stsp if (datestr)
5488 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5489 d4efa91b 2020-01-14 stsp if (commit)
5490 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5491 d4efa91b 2020-01-14 stsp else {
5492 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5493 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5494 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5495 d4efa91b 2020-01-14 stsp id_str);
5496 d4efa91b 2020-01-14 stsp break;
5497 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5498 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5499 d4efa91b 2020-01-14 stsp id_str);
5500 d4efa91b 2020-01-14 stsp break;
5501 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5502 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5503 d4efa91b 2020-01-14 stsp id_str);
5504 d4efa91b 2020-01-14 stsp break;
5505 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5506 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5507 d4efa91b 2020-01-14 stsp id_str);
5508 d4efa91b 2020-01-14 stsp break;
5509 d4efa91b 2020-01-14 stsp default:
5510 d4efa91b 2020-01-14 stsp break;
5511 d4efa91b 2020-01-14 stsp }
5512 8e7bd50a 2019-08-22 stsp }
5513 8e7bd50a 2019-08-22 stsp free(id_str);
5514 d4efa91b 2020-01-14 stsp if (commit) {
5515 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5516 d4efa91b 2020-01-14 stsp if (err)
5517 d4efa91b 2020-01-14 stsp break;
5518 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5519 d4efa91b 2020-01-14 stsp } else {
5520 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5521 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5522 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5523 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5524 d4efa91b 2020-01-14 stsp break;
5525 d4efa91b 2020-01-14 stsp }
5526 8e7bd50a 2019-08-22 stsp }
5527 8e7bd50a 2019-08-22 stsp
5528 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5529 8e7bd50a 2019-08-22 stsp do {
5530 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5531 8e7bd50a 2019-08-22 stsp if (line)
5532 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5533 8e7bd50a 2019-08-22 stsp } while (line);
5534 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5535 8e7bd50a 2019-08-22 stsp }
5536 8e7bd50a 2019-08-22 stsp
5537 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5538 8e7bd50a 2019-08-22 stsp return NULL;
5539 8e7bd50a 2019-08-22 stsp }
5540 8e7bd50a 2019-08-22 stsp
5541 8e7bd50a 2019-08-22 stsp static const struct got_error *
5542 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5543 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5544 8e7bd50a 2019-08-22 stsp {
5545 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5546 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5547 f372d5cd 2019-10-21 stsp char *editor = NULL;
5548 8e7bd50a 2019-08-22 stsp int fd = -1;
5549 8e7bd50a 2019-08-22 stsp
5550 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5551 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5552 8e7bd50a 2019-08-22 stsp goto done;
5553 8e7bd50a 2019-08-22 stsp }
5554 8e7bd50a 2019-08-22 stsp
5555 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5556 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5557 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5558 8e7bd50a 2019-08-22 stsp goto done;
5559 8e7bd50a 2019-08-22 stsp }
5560 8e7bd50a 2019-08-22 stsp
5561 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5562 8e7bd50a 2019-08-22 stsp if (err)
5563 8e7bd50a 2019-08-22 stsp goto done;
5564 8e7bd50a 2019-08-22 stsp
5565 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5566 8e7bd50a 2019-08-22 stsp close(fd);
5567 8e7bd50a 2019-08-22 stsp
5568 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5569 8e7bd50a 2019-08-22 stsp if (err)
5570 8e7bd50a 2019-08-22 stsp goto done;
5571 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5572 8e7bd50a 2019-08-22 stsp done:
5573 8e7bd50a 2019-08-22 stsp free(initial_content);
5574 8e7bd50a 2019-08-22 stsp free(template);
5575 8e7bd50a 2019-08-22 stsp free(editor);
5576 8e7bd50a 2019-08-22 stsp
5577 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5578 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5579 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5580 8e7bd50a 2019-08-22 stsp if (err) {
5581 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5582 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5583 8e7bd50a 2019-08-22 stsp }
5584 8e7bd50a 2019-08-22 stsp }
5585 8e7bd50a 2019-08-22 stsp return err;
5586 8e7bd50a 2019-08-22 stsp }
5587 8e7bd50a 2019-08-22 stsp
5588 8e7bd50a 2019-08-22 stsp static const struct got_error *
5589 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5590 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5591 8e7bd50a 2019-08-22 stsp {
5592 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5593 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5594 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5595 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5596 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5597 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5598 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5599 8e7bd50a 2019-08-22 stsp
5600 8e7bd50a 2019-08-22 stsp /*
5601 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5602 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5603 8e7bd50a 2019-08-22 stsp * an unintended typo.
5604 8e7bd50a 2019-08-22 stsp */
5605 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5606 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5607 8e7bd50a 2019-08-22 stsp
5608 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5609 8e7bd50a 2019-08-22 stsp if (err)
5610 8e7bd50a 2019-08-22 stsp return err;
5611 8e7bd50a 2019-08-22 stsp
5612 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5613 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5614 8e7bd50a 2019-08-22 stsp if (err)
5615 8e7bd50a 2019-08-22 stsp goto done;
5616 8e7bd50a 2019-08-22 stsp
5617 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5618 8e7bd50a 2019-08-22 stsp if (err)
5619 8e7bd50a 2019-08-22 stsp goto done;
5620 8e7bd50a 2019-08-22 stsp
5621 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5622 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5623 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5624 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5625 8e7bd50a 2019-08-22 stsp goto done;
5626 8e7bd50a 2019-08-22 stsp }
5627 8e7bd50a 2019-08-22 stsp tag_name += 10;
5628 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5629 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5630 8e7bd50a 2019-08-22 stsp goto done;
5631 8e7bd50a 2019-08-22 stsp }
5632 8e7bd50a 2019-08-22 stsp
5633 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5634 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5635 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5636 8e7bd50a 2019-08-22 stsp goto done;
5637 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5638 8e7bd50a 2019-08-22 stsp goto done;
5639 8e7bd50a 2019-08-22 stsp
5640 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5641 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5642 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5643 f372d5cd 2019-10-21 stsp if (err) {
5644 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5645 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5646 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5647 8e7bd50a 2019-08-22 stsp goto done;
5648 f372d5cd 2019-10-21 stsp }
5649 8e7bd50a 2019-08-22 stsp }
5650 8e7bd50a 2019-08-22 stsp
5651 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5652 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5653 f372d5cd 2019-10-21 stsp if (err) {
5654 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5655 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5656 8e7bd50a 2019-08-22 stsp goto done;
5657 f372d5cd 2019-10-21 stsp }
5658 8e7bd50a 2019-08-22 stsp
5659 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5660 f372d5cd 2019-10-21 stsp if (err) {
5661 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5662 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5663 8e7bd50a 2019-08-22 stsp goto done;
5664 f372d5cd 2019-10-21 stsp }
5665 8e7bd50a 2019-08-22 stsp
5666 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5667 f372d5cd 2019-10-21 stsp if (err) {
5668 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5669 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5670 f372d5cd 2019-10-21 stsp goto done;
5671 f372d5cd 2019-10-21 stsp }
5672 8e7bd50a 2019-08-22 stsp
5673 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5674 f372d5cd 2019-10-21 stsp if (err) {
5675 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5676 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5677 f372d5cd 2019-10-21 stsp goto done;
5678 8e7bd50a 2019-08-22 stsp }
5679 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5680 8e7bd50a 2019-08-22 stsp done:
5681 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5682 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5683 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5684 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5685 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5686 f372d5cd 2019-10-21 stsp free(tag_id_str);
5687 8e7bd50a 2019-08-22 stsp if (ref)
5688 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5689 8e7bd50a 2019-08-22 stsp free(commit_id);
5690 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5691 8e7bd50a 2019-08-22 stsp free(refname);
5692 8e7bd50a 2019-08-22 stsp free(tagmsg);
5693 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5694 aba9c984 2019-09-08 stsp free(tagger);
5695 8e7bd50a 2019-08-22 stsp return err;
5696 8e7bd50a 2019-08-22 stsp }
5697 8e7bd50a 2019-08-22 stsp
5698 8e7bd50a 2019-08-22 stsp static const struct got_error *
5699 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5700 8e7bd50a 2019-08-22 stsp {
5701 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5702 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5703 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5704 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5705 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5706 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5707 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5708 8e7bd50a 2019-08-22 stsp
5709 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5710 8e7bd50a 2019-08-22 stsp switch (ch) {
5711 80106605 2020-02-24 stsp case 'c':
5712 80106605 2020-02-24 stsp commit_id_arg = optarg;
5713 80106605 2020-02-24 stsp break;
5714 8e7bd50a 2019-08-22 stsp case 'm':
5715 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5716 8e7bd50a 2019-08-22 stsp break;
5717 8e7bd50a 2019-08-22 stsp case 'r':
5718 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5719 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5720 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5721 9ba1d308 2019-10-21 stsp optarg);
5722 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5723 8e7bd50a 2019-08-22 stsp break;
5724 8e7bd50a 2019-08-22 stsp case 'l':
5725 8e7bd50a 2019-08-22 stsp do_list = 1;
5726 8e7bd50a 2019-08-22 stsp break;
5727 8e7bd50a 2019-08-22 stsp default:
5728 8e7bd50a 2019-08-22 stsp usage_tag();
5729 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5730 8e7bd50a 2019-08-22 stsp }
5731 8e7bd50a 2019-08-22 stsp }
5732 8e7bd50a 2019-08-22 stsp
5733 8e7bd50a 2019-08-22 stsp argc -= optind;
5734 8e7bd50a 2019-08-22 stsp argv += optind;
5735 8e7bd50a 2019-08-22 stsp
5736 8e7bd50a 2019-08-22 stsp if (do_list) {
5737 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5738 775ce909 2020-03-22 stsp errx(1,
5739 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5740 8e7bd50a 2019-08-22 stsp if (tagmsg)
5741 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5742 8e7bd50a 2019-08-22 stsp if (argc > 0)
5743 8e7bd50a 2019-08-22 stsp usage_tag();
5744 80106605 2020-02-24 stsp } else if (argc != 1)
5745 8e7bd50a 2019-08-22 stsp usage_tag();
5746 80106605 2020-02-24 stsp
5747 a2887370 2019-08-23 stsp tag_name = argv[0];
5748 8e7bd50a 2019-08-22 stsp
5749 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5750 8e7bd50a 2019-08-22 stsp if (do_list) {
5751 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5752 8e7bd50a 2019-08-22 stsp NULL) == -1)
5753 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5754 8e7bd50a 2019-08-22 stsp } else {
5755 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5756 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5757 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5758 8e7bd50a 2019-08-22 stsp }
5759 8e7bd50a 2019-08-22 stsp #endif
5760 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5761 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5762 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5763 8e7bd50a 2019-08-22 stsp goto done;
5764 8e7bd50a 2019-08-22 stsp }
5765 8e7bd50a 2019-08-22 stsp
5766 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5767 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5768 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5769 8e7bd50a 2019-08-22 stsp goto done;
5770 8e7bd50a 2019-08-22 stsp else
5771 8e7bd50a 2019-08-22 stsp error = NULL;
5772 8e7bd50a 2019-08-22 stsp if (worktree) {
5773 8e7bd50a 2019-08-22 stsp repo_path =
5774 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5775 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5776 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5777 8e7bd50a 2019-08-22 stsp if (error)
5778 8e7bd50a 2019-08-22 stsp goto done;
5779 8e7bd50a 2019-08-22 stsp } else {
5780 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5781 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5782 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5783 8e7bd50a 2019-08-22 stsp goto done;
5784 8e7bd50a 2019-08-22 stsp }
5785 8e7bd50a 2019-08-22 stsp }
5786 8e7bd50a 2019-08-22 stsp }
5787 8e7bd50a 2019-08-22 stsp
5788 8e7bd50a 2019-08-22 stsp if (do_list) {
5789 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5790 c9956ddf 2019-09-08 stsp if (error != NULL)
5791 c9956ddf 2019-09-08 stsp goto done;
5792 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5793 8e7bd50a 2019-08-22 stsp if (error)
5794 8e7bd50a 2019-08-22 stsp goto done;
5795 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5796 8e7bd50a 2019-08-22 stsp } else {
5797 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5798 c9956ddf 2019-09-08 stsp if (error)
5799 c9956ddf 2019-09-08 stsp goto done;
5800 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5801 c9956ddf 2019-09-08 stsp if (error != NULL)
5802 c9956ddf 2019-09-08 stsp goto done;
5803 c9956ddf 2019-09-08 stsp
5804 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5805 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5806 8e7bd50a 2019-08-22 stsp if (error)
5807 8e7bd50a 2019-08-22 stsp goto done;
5808 8e7bd50a 2019-08-22 stsp }
5809 8e7bd50a 2019-08-22 stsp
5810 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5811 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5812 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5813 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5814 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5815 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5816 8e7bd50a 2019-08-22 stsp if (error)
5817 8e7bd50a 2019-08-22 stsp goto done;
5818 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5819 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5820 8e7bd50a 2019-08-22 stsp if (error)
5821 8e7bd50a 2019-08-22 stsp goto done;
5822 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5823 8e7bd50a 2019-08-22 stsp free(commit_id);
5824 8e7bd50a 2019-08-22 stsp if (error)
5825 8e7bd50a 2019-08-22 stsp goto done;
5826 8e7bd50a 2019-08-22 stsp }
5827 8e7bd50a 2019-08-22 stsp
5828 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5829 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5830 8e7bd50a 2019-08-22 stsp }
5831 8e7bd50a 2019-08-22 stsp done:
5832 8e7bd50a 2019-08-22 stsp if (repo)
5833 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5834 8e7bd50a 2019-08-22 stsp if (worktree)
5835 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5836 8e7bd50a 2019-08-22 stsp free(cwd);
5837 8e7bd50a 2019-08-22 stsp free(repo_path);
5838 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5839 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5840 8e7bd50a 2019-08-22 stsp return error;
5841 8e7bd50a 2019-08-22 stsp }
5842 8e7bd50a 2019-08-22 stsp
5843 8e7bd50a 2019-08-22 stsp __dead static void
5844 d00136be 2019-03-26 stsp usage_add(void)
5845 d00136be 2019-03-26 stsp {
5846 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5847 022fae89 2019-12-06 tracey getprogname());
5848 d00136be 2019-03-26 stsp exit(1);
5849 d00136be 2019-03-26 stsp }
5850 d00136be 2019-03-26 stsp
5851 d00136be 2019-03-26 stsp static const struct got_error *
5852 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5853 4e68cba3 2019-11-23 stsp {
5854 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5855 4e68cba3 2019-11-23 stsp path++;
5856 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5857 4e68cba3 2019-11-23 stsp return NULL;
5858 4e68cba3 2019-11-23 stsp }
5859 4e68cba3 2019-11-23 stsp
5860 4e68cba3 2019-11-23 stsp static const struct got_error *
5861 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5862 d00136be 2019-03-26 stsp {
5863 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5864 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5865 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5866 1dd54920 2019-05-11 stsp char *cwd = NULL;
5867 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5868 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5869 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5870 1dd54920 2019-05-11 stsp
5871 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5872 d00136be 2019-03-26 stsp
5873 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5874 d00136be 2019-03-26 stsp switch (ch) {
5875 022fae89 2019-12-06 tracey case 'I':
5876 022fae89 2019-12-06 tracey no_ignores = 1;
5877 022fae89 2019-12-06 tracey break;
5878 4e68cba3 2019-11-23 stsp case 'R':
5879 4e68cba3 2019-11-23 stsp can_recurse = 1;
5880 4e68cba3 2019-11-23 stsp break;
5881 d00136be 2019-03-26 stsp default:
5882 d00136be 2019-03-26 stsp usage_add();
5883 d00136be 2019-03-26 stsp /* NOTREACHED */
5884 d00136be 2019-03-26 stsp }
5885 d00136be 2019-03-26 stsp }
5886 d00136be 2019-03-26 stsp
5887 d00136be 2019-03-26 stsp argc -= optind;
5888 d00136be 2019-03-26 stsp argv += optind;
5889 d00136be 2019-03-26 stsp
5890 43012d58 2019-07-14 stsp #ifndef PROFILE
5891 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5892 43012d58 2019-07-14 stsp NULL) == -1)
5893 43012d58 2019-07-14 stsp err(1, "pledge");
5894 43012d58 2019-07-14 stsp #endif
5895 723c305c 2019-05-11 jcs if (argc < 1)
5896 d00136be 2019-03-26 stsp usage_add();
5897 d00136be 2019-03-26 stsp
5898 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5899 d00136be 2019-03-26 stsp if (cwd == NULL) {
5900 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5901 d00136be 2019-03-26 stsp goto done;
5902 d00136be 2019-03-26 stsp }
5903 723c305c 2019-05-11 jcs
5904 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5905 fa51e947 2020-03-27 stsp if (error) {
5906 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
5907 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "add", cwd);
5908 d00136be 2019-03-26 stsp goto done;
5909 fa51e947 2020-03-27 stsp }
5910 d00136be 2019-03-26 stsp
5911 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5912 c9956ddf 2019-09-08 stsp NULL);
5913 031a5338 2019-03-26 stsp if (error != NULL)
5914 031a5338 2019-03-26 stsp goto done;
5915 031a5338 2019-03-26 stsp
5916 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5917 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5918 d00136be 2019-03-26 stsp if (error)
5919 d00136be 2019-03-26 stsp goto done;
5920 d00136be 2019-03-26 stsp
5921 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5922 6d022e97 2019-08-04 stsp if (error)
5923 022fae89 2019-12-06 tracey goto done;
5924 022fae89 2019-12-06 tracey
5925 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5926 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5927 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5928 6d022e97 2019-08-04 stsp goto done;
5929 723c305c 2019-05-11 jcs
5930 022fae89 2019-12-06 tracey }
5931 022fae89 2019-12-06 tracey
5932 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5933 4e68cba3 2019-11-23 stsp char *ondisk_path;
5934 4e68cba3 2019-11-23 stsp struct stat sb;
5935 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5936 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5937 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5938 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5939 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5940 4e68cba3 2019-11-23 stsp goto done;
5941 4e68cba3 2019-11-23 stsp }
5942 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5943 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5944 4e68cba3 2019-11-23 stsp free(ondisk_path);
5945 4e68cba3 2019-11-23 stsp continue;
5946 4e68cba3 2019-11-23 stsp }
5947 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5948 4e68cba3 2019-11-23 stsp ondisk_path);
5949 4e68cba3 2019-11-23 stsp free(ondisk_path);
5950 4e68cba3 2019-11-23 stsp goto done;
5951 4e68cba3 2019-11-23 stsp }
5952 4e68cba3 2019-11-23 stsp free(ondisk_path);
5953 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5954 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5955 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5956 4e68cba3 2019-11-23 stsp goto done;
5957 4e68cba3 2019-11-23 stsp }
5958 4e68cba3 2019-11-23 stsp }
5959 4e68cba3 2019-11-23 stsp }
5960 022fae89 2019-12-06 tracey
5961 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5962 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5963 d00136be 2019-03-26 stsp done:
5964 031a5338 2019-03-26 stsp if (repo)
5965 031a5338 2019-03-26 stsp got_repo_close(repo);
5966 d00136be 2019-03-26 stsp if (worktree)
5967 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5968 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5969 1dd54920 2019-05-11 stsp free((char *)pe->path);
5970 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5971 2ec1f75b 2019-03-26 stsp free(cwd);
5972 2ec1f75b 2019-03-26 stsp return error;
5973 2ec1f75b 2019-03-26 stsp }
5974 2ec1f75b 2019-03-26 stsp
5975 2ec1f75b 2019-03-26 stsp __dead static void
5976 648e4ef7 2019-07-09 stsp usage_remove(void)
5977 2ec1f75b 2019-03-26 stsp {
5978 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5979 f2a9dc41 2019-12-13 tracey getprogname());
5980 2ec1f75b 2019-03-26 stsp exit(1);
5981 2a06fe5f 2019-08-24 stsp }
5982 2a06fe5f 2019-08-24 stsp
5983 2a06fe5f 2019-08-24 stsp static const struct got_error *
5984 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5985 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5986 2a06fe5f 2019-08-24 stsp {
5987 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5988 f2a9dc41 2019-12-13 tracey path++;
5989 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5990 2a06fe5f 2019-08-24 stsp return NULL;
5991 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5992 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5993 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5994 2a06fe5f 2019-08-24 stsp return NULL;
5995 2ec1f75b 2019-03-26 stsp }
5996 2ec1f75b 2019-03-26 stsp
5997 2ec1f75b 2019-03-26 stsp static const struct got_error *
5998 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5999 2ec1f75b 2019-03-26 stsp {
6000 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
6001 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
6002 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
6003 17ed4618 2019-06-02 stsp char *cwd = NULL;
6004 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
6005 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
6006 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
6007 2ec1f75b 2019-03-26 stsp
6008 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
6009 17ed4618 2019-06-02 stsp
6010 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
6011 2ec1f75b 2019-03-26 stsp switch (ch) {
6012 2ec1f75b 2019-03-26 stsp case 'f':
6013 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
6014 2ec1f75b 2019-03-26 stsp break;
6015 70e3e7f5 2019-12-13 tracey case 'k':
6016 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
6017 70e3e7f5 2019-12-13 tracey break;
6018 f2a9dc41 2019-12-13 tracey case 'R':
6019 f2a9dc41 2019-12-13 tracey can_recurse = 1;
6020 f2a9dc41 2019-12-13 tracey break;
6021 2ec1f75b 2019-03-26 stsp default:
6022 f2a9dc41 2019-12-13 tracey usage_remove();
6023 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
6024 2ec1f75b 2019-03-26 stsp }
6025 2ec1f75b 2019-03-26 stsp }
6026 2ec1f75b 2019-03-26 stsp
6027 2ec1f75b 2019-03-26 stsp argc -= optind;
6028 2ec1f75b 2019-03-26 stsp argv += optind;
6029 2ec1f75b 2019-03-26 stsp
6030 43012d58 2019-07-14 stsp #ifndef PROFILE
6031 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
6032 43012d58 2019-07-14 stsp NULL) == -1)
6033 43012d58 2019-07-14 stsp err(1, "pledge");
6034 43012d58 2019-07-14 stsp #endif
6035 17ed4618 2019-06-02 stsp if (argc < 1)
6036 648e4ef7 2019-07-09 stsp usage_remove();
6037 2ec1f75b 2019-03-26 stsp
6038 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
6039 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
6040 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6041 2ec1f75b 2019-03-26 stsp goto done;
6042 2ec1f75b 2019-03-26 stsp }
6043 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
6044 fa51e947 2020-03-27 stsp if (error) {
6045 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6046 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "remove", cwd);
6047 2ec1f75b 2019-03-26 stsp goto done;
6048 fa51e947 2020-03-27 stsp }
6049 2ec1f75b 2019-03-26 stsp
6050 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6051 c9956ddf 2019-09-08 stsp NULL);
6052 2af4a041 2019-05-11 jcs if (error)
6053 2ec1f75b 2019-03-26 stsp goto done;
6054 2ec1f75b 2019-03-26 stsp
6055 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6056 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6057 2ec1f75b 2019-03-26 stsp if (error)
6058 2ec1f75b 2019-03-26 stsp goto done;
6059 2ec1f75b 2019-03-26 stsp
6060 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6061 6d022e97 2019-08-04 stsp if (error)
6062 6d022e97 2019-08-04 stsp goto done;
6063 17ed4618 2019-06-02 stsp
6064 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
6065 f2a9dc41 2019-12-13 tracey char *ondisk_path;
6066 f2a9dc41 2019-12-13 tracey struct stat sb;
6067 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
6068 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
6069 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
6070 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
6071 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
6072 f2a9dc41 2019-12-13 tracey goto done;
6073 f2a9dc41 2019-12-13 tracey }
6074 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
6075 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
6076 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6077 f2a9dc41 2019-12-13 tracey continue;
6078 f2a9dc41 2019-12-13 tracey }
6079 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
6080 f2a9dc41 2019-12-13 tracey ondisk_path);
6081 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6082 f2a9dc41 2019-12-13 tracey goto done;
6083 f2a9dc41 2019-12-13 tracey }
6084 f2a9dc41 2019-12-13 tracey free(ondisk_path);
6085 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
6086 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
6087 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
6088 f2a9dc41 2019-12-13 tracey goto done;
6089 f2a9dc41 2019-12-13 tracey }
6090 f2a9dc41 2019-12-13 tracey }
6091 f2a9dc41 2019-12-13 tracey }
6092 f2a9dc41 2019-12-13 tracey
6093 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
6094 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
6095 a129376b 2019-03-28 stsp done:
6096 a129376b 2019-03-28 stsp if (repo)
6097 a129376b 2019-03-28 stsp got_repo_close(repo);
6098 a129376b 2019-03-28 stsp if (worktree)
6099 a129376b 2019-03-28 stsp got_worktree_close(worktree);
6100 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
6101 17ed4618 2019-06-02 stsp free((char *)pe->path);
6102 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
6103 a129376b 2019-03-28 stsp free(cwd);
6104 a129376b 2019-03-28 stsp return error;
6105 a129376b 2019-03-28 stsp }
6106 a129376b 2019-03-28 stsp
6107 a129376b 2019-03-28 stsp __dead static void
6108 a129376b 2019-03-28 stsp usage_revert(void)
6109 a129376b 2019-03-28 stsp {
6110 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
6111 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
6112 a129376b 2019-03-28 stsp exit(1);
6113 a129376b 2019-03-28 stsp }
6114 a129376b 2019-03-28 stsp
6115 1ee397ad 2019-07-12 stsp static const struct got_error *
6116 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
6117 a129376b 2019-03-28 stsp {
6118 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
6119 fb9704af 2020-01-27 stsp return NULL;
6120 fb9704af 2020-01-27 stsp
6121 a129376b 2019-03-28 stsp while (path[0] == '/')
6122 a129376b 2019-03-28 stsp path++;
6123 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
6124 33aa809d 2019-08-08 stsp return NULL;
6125 33aa809d 2019-08-08 stsp }
6126 33aa809d 2019-08-08 stsp
6127 33aa809d 2019-08-08 stsp struct choose_patch_arg {
6128 33aa809d 2019-08-08 stsp FILE *patch_script_file;
6129 33aa809d 2019-08-08 stsp const char *action;
6130 33aa809d 2019-08-08 stsp };
6131 33aa809d 2019-08-08 stsp
6132 33aa809d 2019-08-08 stsp static const struct got_error *
6133 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
6134 33aa809d 2019-08-08 stsp int nchanges, const char *action)
6135 33aa809d 2019-08-08 stsp {
6136 33aa809d 2019-08-08 stsp char *line = NULL;
6137 33aa809d 2019-08-08 stsp size_t linesize = 0;
6138 33aa809d 2019-08-08 stsp ssize_t linelen;
6139 33aa809d 2019-08-08 stsp
6140 33aa809d 2019-08-08 stsp switch (status) {
6141 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
6142 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
6143 33aa809d 2019-08-08 stsp break;
6144 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
6145 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
6146 33aa809d 2019-08-08 stsp break;
6147 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
6148 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
6149 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
6150 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6151 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
6152 33aa809d 2019-08-08 stsp printf("%s", line);
6153 33aa809d 2019-08-08 stsp if (ferror(patch_file))
6154 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6155 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
6156 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
6157 33aa809d 2019-08-08 stsp path, n, nchanges, action);
6158 33aa809d 2019-08-08 stsp break;
6159 33aa809d 2019-08-08 stsp default:
6160 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
6161 33aa809d 2019-08-08 stsp }
6162 33aa809d 2019-08-08 stsp
6163 33aa809d 2019-08-08 stsp return NULL;
6164 33aa809d 2019-08-08 stsp }
6165 33aa809d 2019-08-08 stsp
6166 33aa809d 2019-08-08 stsp static const struct got_error *
6167 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
6168 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
6169 33aa809d 2019-08-08 stsp {
6170 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
6171 33aa809d 2019-08-08 stsp char *line = NULL;
6172 33aa809d 2019-08-08 stsp size_t linesize = 0;
6173 33aa809d 2019-08-08 stsp ssize_t linelen;
6174 33aa809d 2019-08-08 stsp int resp = ' ';
6175 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
6176 33aa809d 2019-08-08 stsp
6177 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
6178 33aa809d 2019-08-08 stsp
6179 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
6180 33aa809d 2019-08-08 stsp char *nl;
6181 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6182 33aa809d 2019-08-08 stsp a->action);
6183 33aa809d 2019-08-08 stsp if (err)
6184 33aa809d 2019-08-08 stsp return err;
6185 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
6186 33aa809d 2019-08-08 stsp if (linelen == -1) {
6187 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
6188 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
6189 33aa809d 2019-08-08 stsp return NULL;
6190 33aa809d 2019-08-08 stsp }
6191 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
6192 33aa809d 2019-08-08 stsp if (nl)
6193 33aa809d 2019-08-08 stsp *nl = '\0';
6194 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
6195 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6196 33aa809d 2019-08-08 stsp printf("y\n");
6197 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
6198 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6199 33aa809d 2019-08-08 stsp printf("n\n");
6200 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
6201 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
6202 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6203 33aa809d 2019-08-08 stsp printf("q\n");
6204 33aa809d 2019-08-08 stsp } else
6205 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
6206 33aa809d 2019-08-08 stsp free(line);
6207 33aa809d 2019-08-08 stsp return NULL;
6208 33aa809d 2019-08-08 stsp }
6209 33aa809d 2019-08-08 stsp
6210 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
6211 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
6212 33aa809d 2019-08-08 stsp a->action);
6213 33aa809d 2019-08-08 stsp if (err)
6214 33aa809d 2019-08-08 stsp return err;
6215 33aa809d 2019-08-08 stsp resp = getchar();
6216 33aa809d 2019-08-08 stsp if (resp == '\n')
6217 33aa809d 2019-08-08 stsp resp = getchar();
6218 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
6219 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
6220 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6221 33aa809d 2019-08-08 stsp resp = ' ';
6222 33aa809d 2019-08-08 stsp }
6223 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
6224 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
6225 33aa809d 2019-08-08 stsp resp = ' ';
6226 33aa809d 2019-08-08 stsp }
6227 33aa809d 2019-08-08 stsp }
6228 33aa809d 2019-08-08 stsp
6229 33aa809d 2019-08-08 stsp if (resp == 'y')
6230 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
6231 33aa809d 2019-08-08 stsp else if (resp == 'n')
6232 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
6233 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
6234 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
6235 33aa809d 2019-08-08 stsp
6236 1ee397ad 2019-07-12 stsp return NULL;
6237 a129376b 2019-03-28 stsp }
6238 a129376b 2019-03-28 stsp
6239 33aa809d 2019-08-08 stsp
6240 a129376b 2019-03-28 stsp static const struct got_error *
6241 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
6242 a129376b 2019-03-28 stsp {
6243 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
6244 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
6245 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
6246 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
6247 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
6248 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
6249 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
6250 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
6251 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
6252 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
6253 a129376b 2019-03-28 stsp
6254 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
6255 e20a8b6f 2019-06-04 stsp
6256 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
6257 a129376b 2019-03-28 stsp switch (ch) {
6258 33aa809d 2019-08-08 stsp case 'p':
6259 33aa809d 2019-08-08 stsp pflag = 1;
6260 33aa809d 2019-08-08 stsp break;
6261 33aa809d 2019-08-08 stsp case 'F':
6262 33aa809d 2019-08-08 stsp patch_script_path = optarg;
6263 33aa809d 2019-08-08 stsp break;
6264 0f6d7415 2019-08-08 stsp case 'R':
6265 0f6d7415 2019-08-08 stsp can_recurse = 1;
6266 0f6d7415 2019-08-08 stsp break;
6267 a129376b 2019-03-28 stsp default:
6268 a129376b 2019-03-28 stsp usage_revert();
6269 a129376b 2019-03-28 stsp /* NOTREACHED */
6270 a129376b 2019-03-28 stsp }
6271 a129376b 2019-03-28 stsp }
6272 a129376b 2019-03-28 stsp
6273 a129376b 2019-03-28 stsp argc -= optind;
6274 a129376b 2019-03-28 stsp argv += optind;
6275 a129376b 2019-03-28 stsp
6276 43012d58 2019-07-14 stsp #ifndef PROFILE
6277 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6278 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6279 43012d58 2019-07-14 stsp err(1, "pledge");
6280 43012d58 2019-07-14 stsp #endif
6281 e20a8b6f 2019-06-04 stsp if (argc < 1)
6282 a129376b 2019-03-28 stsp usage_revert();
6283 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
6284 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6285 a129376b 2019-03-28 stsp
6286 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
6287 a129376b 2019-03-28 stsp if (cwd == NULL) {
6288 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6289 a129376b 2019-03-28 stsp goto done;
6290 a129376b 2019-03-28 stsp }
6291 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
6292 fa51e947 2020-03-27 stsp if (error) {
6293 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6294 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "revert", cwd);
6295 2ec1f75b 2019-03-26 stsp goto done;
6296 fa51e947 2020-03-27 stsp }
6297 a129376b 2019-03-28 stsp
6298 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6299 c9956ddf 2019-09-08 stsp NULL);
6300 a129376b 2019-03-28 stsp if (error != NULL)
6301 a129376b 2019-03-28 stsp goto done;
6302 a129376b 2019-03-28 stsp
6303 33aa809d 2019-08-08 stsp if (patch_script_path) {
6304 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6305 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
6306 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
6307 33aa809d 2019-08-08 stsp patch_script_path);
6308 33aa809d 2019-08-08 stsp goto done;
6309 33aa809d 2019-08-08 stsp }
6310 33aa809d 2019-08-08 stsp }
6311 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
6312 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6313 a129376b 2019-03-28 stsp if (error)
6314 a129376b 2019-03-28 stsp goto done;
6315 a129376b 2019-03-28 stsp
6316 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6317 6d022e97 2019-08-04 stsp if (error)
6318 6d022e97 2019-08-04 stsp goto done;
6319 00db391e 2019-08-03 stsp
6320 0f6d7415 2019-08-08 stsp if (!can_recurse) {
6321 0f6d7415 2019-08-08 stsp char *ondisk_path;
6322 0f6d7415 2019-08-08 stsp struct stat sb;
6323 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
6324 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
6325 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
6326 0f6d7415 2019-08-08 stsp pe->path) == -1) {
6327 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
6328 0f6d7415 2019-08-08 stsp goto done;
6329 0f6d7415 2019-08-08 stsp }
6330 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
6331 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
6332 0f6d7415 2019-08-08 stsp free(ondisk_path);
6333 0f6d7415 2019-08-08 stsp continue;
6334 0f6d7415 2019-08-08 stsp }
6335 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
6336 0f6d7415 2019-08-08 stsp ondisk_path);
6337 0f6d7415 2019-08-08 stsp free(ondisk_path);
6338 0f6d7415 2019-08-08 stsp goto done;
6339 0f6d7415 2019-08-08 stsp }
6340 0f6d7415 2019-08-08 stsp free(ondisk_path);
6341 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
6342 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
6343 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
6344 0f6d7415 2019-08-08 stsp goto done;
6345 0f6d7415 2019-08-08 stsp }
6346 0f6d7415 2019-08-08 stsp }
6347 0f6d7415 2019-08-08 stsp }
6348 0f6d7415 2019-08-08 stsp
6349 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6350 33aa809d 2019-08-08 stsp cpa.action = "revert";
6351 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
6352 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6353 2ec1f75b 2019-03-26 stsp done:
6354 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6355 33aa809d 2019-08-08 stsp error == NULL)
6356 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6357 2ec1f75b 2019-03-26 stsp if (repo)
6358 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
6359 2ec1f75b 2019-03-26 stsp if (worktree)
6360 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
6361 2ec1f75b 2019-03-26 stsp free(path);
6362 d00136be 2019-03-26 stsp free(cwd);
6363 6bad629b 2019-02-04 stsp return error;
6364 c4296144 2019-05-09 stsp }
6365 c4296144 2019-05-09 stsp
6366 c4296144 2019-05-09 stsp __dead static void
6367 c4296144 2019-05-09 stsp usage_commit(void)
6368 c4296144 2019-05-09 stsp {
6369 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
6370 5c1e53bc 2019-07-28 stsp getprogname());
6371 c4296144 2019-05-09 stsp exit(1);
6372 33ad4cbe 2019-05-12 jcs }
6373 33ad4cbe 2019-05-12 jcs
6374 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
6375 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
6376 e2ba3d07 2019-05-13 stsp const char *editor;
6377 e0870e44 2019-05-13 stsp const char *worktree_path;
6378 76d98825 2019-06-03 stsp const char *branch_name;
6379 314a6357 2019-05-13 stsp const char *repo_path;
6380 e0870e44 2019-05-13 stsp char *logmsg_path;
6381 e2ba3d07 2019-05-13 stsp
6382 e2ba3d07 2019-05-13 stsp };
6383 e0870e44 2019-05-13 stsp
6384 33ad4cbe 2019-05-12 jcs static const struct got_error *
6385 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
6386 33ad4cbe 2019-05-12 jcs void *arg)
6387 33ad4cbe 2019-05-12 jcs {
6388 76d98825 2019-06-03 stsp char *initial_content = NULL;
6389 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
6390 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
6391 e0870e44 2019-05-13 stsp char *template = NULL;
6392 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
6393 3ce1b845 2019-07-15 stsp int fd;
6394 33ad4cbe 2019-05-12 jcs size_t len;
6395 33ad4cbe 2019-05-12 jcs
6396 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
6397 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
6398 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
6399 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
6400 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
6401 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
6402 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
6403 33ad4cbe 2019-05-12 jcs return NULL;
6404 33ad4cbe 2019-05-12 jcs }
6405 33ad4cbe 2019-05-12 jcs
6406 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
6407 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
6408 76d98825 2019-06-03 stsp
6409 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
6410 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
6411 76d98825 2019-06-03 stsp a->branch_name) == -1)
6412 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
6413 e0870e44 2019-05-13 stsp
6414 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
6415 793c30b5 2019-05-13 stsp if (err)
6416 e0870e44 2019-05-13 stsp goto done;
6417 33ad4cbe 2019-05-12 jcs
6418 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
6419 33ad4cbe 2019-05-12 jcs
6420 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6421 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6422 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6423 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6424 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6425 33ad4cbe 2019-05-12 jcs }
6426 33ad4cbe 2019-05-12 jcs close(fd);
6427 33ad4cbe 2019-05-12 jcs
6428 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6429 33ad4cbe 2019-05-12 jcs done:
6430 76d98825 2019-06-03 stsp free(initial_content);
6431 e0870e44 2019-05-13 stsp free(template);
6432 314a6357 2019-05-13 stsp
6433 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6434 3ce1b845 2019-07-15 stsp if (err == NULL) {
6435 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6436 3ce1b845 2019-07-15 stsp if (err) {
6437 3ce1b845 2019-07-15 stsp free(*logmsg);
6438 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6439 3ce1b845 2019-07-15 stsp }
6440 3ce1b845 2019-07-15 stsp }
6441 33ad4cbe 2019-05-12 jcs return err;
6442 6bad629b 2019-02-04 stsp }
6443 c4296144 2019-05-09 stsp
6444 c4296144 2019-05-09 stsp static const struct got_error *
6445 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6446 c4296144 2019-05-09 stsp {
6447 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6448 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6449 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6450 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6451 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6452 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6453 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6454 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6455 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6456 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6457 5c1e53bc 2019-07-28 stsp
6458 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6459 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6460 c4296144 2019-05-09 stsp
6461 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6462 c4296144 2019-05-09 stsp switch (ch) {
6463 c4296144 2019-05-09 stsp case 'm':
6464 c4296144 2019-05-09 stsp logmsg = optarg;
6465 c4296144 2019-05-09 stsp break;
6466 c4296144 2019-05-09 stsp default:
6467 c4296144 2019-05-09 stsp usage_commit();
6468 c4296144 2019-05-09 stsp /* NOTREACHED */
6469 c4296144 2019-05-09 stsp }
6470 c4296144 2019-05-09 stsp }
6471 c4296144 2019-05-09 stsp
6472 c4296144 2019-05-09 stsp argc -= optind;
6473 c4296144 2019-05-09 stsp argv += optind;
6474 c4296144 2019-05-09 stsp
6475 43012d58 2019-07-14 stsp #ifndef PROFILE
6476 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6477 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6478 43012d58 2019-07-14 stsp err(1, "pledge");
6479 43012d58 2019-07-14 stsp #endif
6480 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6481 c4296144 2019-05-09 stsp if (cwd == NULL) {
6482 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6483 c4296144 2019-05-09 stsp goto done;
6484 c4296144 2019-05-09 stsp }
6485 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6486 fa51e947 2020-03-27 stsp if (error) {
6487 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6488 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "commit", cwd);
6489 7d5807f4 2019-07-11 stsp goto done;
6490 fa51e947 2020-03-27 stsp }
6491 7d5807f4 2019-07-11 stsp
6492 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6493 c4296144 2019-05-09 stsp if (error)
6494 a698f62e 2019-07-25 stsp goto done;
6495 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6496 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6497 c4296144 2019-05-09 stsp goto done;
6498 a698f62e 2019-07-25 stsp }
6499 5c1e53bc 2019-07-28 stsp
6500 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6501 916f288c 2019-07-30 stsp worktree);
6502 5c1e53bc 2019-07-28 stsp if (error)
6503 5c1e53bc 2019-07-28 stsp goto done;
6504 c4296144 2019-05-09 stsp
6505 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6506 c9956ddf 2019-09-08 stsp if (error)
6507 c9956ddf 2019-09-08 stsp goto done;
6508 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6509 c9956ddf 2019-09-08 stsp gitconfig_path);
6510 c4296144 2019-05-09 stsp if (error != NULL)
6511 c4296144 2019-05-09 stsp goto done;
6512 c4296144 2019-05-09 stsp
6513 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6514 aba9c984 2019-09-08 stsp if (error)
6515 aba9c984 2019-09-08 stsp return error;
6516 aba9c984 2019-09-08 stsp
6517 314a6357 2019-05-13 stsp /*
6518 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6519 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6520 314a6357 2019-05-13 stsp */
6521 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6522 314a6357 2019-05-13 stsp error = get_editor(&editor);
6523 314a6357 2019-05-13 stsp else
6524 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6525 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6526 c4296144 2019-05-09 stsp if (error)
6527 c4296144 2019-05-09 stsp goto done;
6528 c4296144 2019-05-09 stsp
6529 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6530 087fb88c 2019-08-04 stsp if (error)
6531 087fb88c 2019-08-04 stsp goto done;
6532 087fb88c 2019-08-04 stsp
6533 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6534 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6535 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6536 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6537 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6538 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6539 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6540 916f288c 2019-07-30 stsp goto done;
6541 916f288c 2019-07-30 stsp }
6542 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6543 916f288c 2019-07-30 stsp }
6544 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6545 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6546 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6547 e0870e44 2019-05-13 stsp if (error) {
6548 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6549 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6550 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6551 c4296144 2019-05-09 stsp goto done;
6552 e0870e44 2019-05-13 stsp }
6553 c4296144 2019-05-09 stsp
6554 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6555 c4296144 2019-05-09 stsp if (error)
6556 c4296144 2019-05-09 stsp goto done;
6557 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6558 c4296144 2019-05-09 stsp done:
6559 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6560 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6561 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6562 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6563 7266f21f 2019-10-21 stsp error == NULL)
6564 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6565 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6566 c4296144 2019-05-09 stsp if (repo)
6567 c4296144 2019-05-09 stsp got_repo_close(repo);
6568 c4296144 2019-05-09 stsp if (worktree)
6569 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6570 c4296144 2019-05-09 stsp free(cwd);
6571 c4296144 2019-05-09 stsp free(id_str);
6572 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6573 e2ba3d07 2019-05-13 stsp free(editor);
6574 aba9c984 2019-09-08 stsp free(author);
6575 234035bc 2019-06-01 stsp return error;
6576 234035bc 2019-06-01 stsp }
6577 234035bc 2019-06-01 stsp
6578 234035bc 2019-06-01 stsp __dead static void
6579 234035bc 2019-06-01 stsp usage_cherrypick(void)
6580 234035bc 2019-06-01 stsp {
6581 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6582 234035bc 2019-06-01 stsp exit(1);
6583 234035bc 2019-06-01 stsp }
6584 234035bc 2019-06-01 stsp
6585 234035bc 2019-06-01 stsp static const struct got_error *
6586 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6587 234035bc 2019-06-01 stsp {
6588 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6589 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6590 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6591 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6592 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6593 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6594 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6595 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6596 9627c110 2020-04-18 stsp int ch;
6597 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6598 234035bc 2019-06-01 stsp
6599 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6600 234035bc 2019-06-01 stsp switch (ch) {
6601 234035bc 2019-06-01 stsp default:
6602 234035bc 2019-06-01 stsp usage_cherrypick();
6603 234035bc 2019-06-01 stsp /* NOTREACHED */
6604 234035bc 2019-06-01 stsp }
6605 234035bc 2019-06-01 stsp }
6606 234035bc 2019-06-01 stsp
6607 234035bc 2019-06-01 stsp argc -= optind;
6608 234035bc 2019-06-01 stsp argv += optind;
6609 234035bc 2019-06-01 stsp
6610 43012d58 2019-07-14 stsp #ifndef PROFILE
6611 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6612 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6613 43012d58 2019-07-14 stsp err(1, "pledge");
6614 43012d58 2019-07-14 stsp #endif
6615 234035bc 2019-06-01 stsp if (argc != 1)
6616 234035bc 2019-06-01 stsp usage_cherrypick();
6617 234035bc 2019-06-01 stsp
6618 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6619 234035bc 2019-06-01 stsp if (cwd == NULL) {
6620 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6621 234035bc 2019-06-01 stsp goto done;
6622 234035bc 2019-06-01 stsp }
6623 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6624 fa51e947 2020-03-27 stsp if (error) {
6625 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6626 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "cherrypick",
6627 fa51e947 2020-03-27 stsp cwd);
6628 234035bc 2019-06-01 stsp goto done;
6629 fa51e947 2020-03-27 stsp }
6630 234035bc 2019-06-01 stsp
6631 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6632 c9956ddf 2019-09-08 stsp NULL);
6633 234035bc 2019-06-01 stsp if (error != NULL)
6634 234035bc 2019-06-01 stsp goto done;
6635 234035bc 2019-06-01 stsp
6636 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6637 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6638 234035bc 2019-06-01 stsp if (error)
6639 234035bc 2019-06-01 stsp goto done;
6640 234035bc 2019-06-01 stsp
6641 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6642 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6643 234035bc 2019-06-01 stsp if (error != NULL) {
6644 234035bc 2019-06-01 stsp struct got_reference *ref;
6645 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6646 234035bc 2019-06-01 stsp goto done;
6647 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6648 234035bc 2019-06-01 stsp if (error != NULL)
6649 234035bc 2019-06-01 stsp goto done;
6650 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6651 234035bc 2019-06-01 stsp got_ref_close(ref);
6652 234035bc 2019-06-01 stsp if (error != NULL)
6653 234035bc 2019-06-01 stsp goto done;
6654 234035bc 2019-06-01 stsp }
6655 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6656 234035bc 2019-06-01 stsp if (error)
6657 234035bc 2019-06-01 stsp goto done;
6658 234035bc 2019-06-01 stsp
6659 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6660 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6661 234035bc 2019-06-01 stsp if (error != NULL)
6662 234035bc 2019-06-01 stsp goto done;
6663 234035bc 2019-06-01 stsp
6664 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6665 234035bc 2019-06-01 stsp if (error) {
6666 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6667 234035bc 2019-06-01 stsp goto done;
6668 234035bc 2019-06-01 stsp error = NULL;
6669 234035bc 2019-06-01 stsp } else {
6670 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6671 234035bc 2019-06-01 stsp goto done;
6672 234035bc 2019-06-01 stsp }
6673 234035bc 2019-06-01 stsp
6674 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6675 234035bc 2019-06-01 stsp if (error)
6676 234035bc 2019-06-01 stsp goto done;
6677 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6678 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6679 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6680 9627c110 2020-04-18 stsp commit_id, repo, update_progress, &upa, check_cancelled,
6681 03415a1a 2019-06-02 stsp NULL);
6682 234035bc 2019-06-01 stsp if (error != NULL)
6683 234035bc 2019-06-01 stsp goto done;
6684 234035bc 2019-06-01 stsp
6685 9627c110 2020-04-18 stsp if (upa.did_something)
6686 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6687 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6688 234035bc 2019-06-01 stsp done:
6689 234035bc 2019-06-01 stsp if (commit)
6690 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6691 234035bc 2019-06-01 stsp free(commit_id_str);
6692 234035bc 2019-06-01 stsp if (head_ref)
6693 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6694 234035bc 2019-06-01 stsp if (worktree)
6695 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6696 234035bc 2019-06-01 stsp if (repo)
6697 234035bc 2019-06-01 stsp got_repo_close(repo);
6698 c4296144 2019-05-09 stsp return error;
6699 c4296144 2019-05-09 stsp }
6700 5ef14e63 2019-06-02 stsp
6701 5ef14e63 2019-06-02 stsp __dead static void
6702 5ef14e63 2019-06-02 stsp usage_backout(void)
6703 5ef14e63 2019-06-02 stsp {
6704 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6705 5ef14e63 2019-06-02 stsp exit(1);
6706 5ef14e63 2019-06-02 stsp }
6707 5ef14e63 2019-06-02 stsp
6708 5ef14e63 2019-06-02 stsp static const struct got_error *
6709 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6710 5ef14e63 2019-06-02 stsp {
6711 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6712 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6713 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6714 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6715 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6716 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6717 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6718 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6719 9627c110 2020-04-18 stsp int ch;
6720 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
6721 5ef14e63 2019-06-02 stsp
6722 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6723 5ef14e63 2019-06-02 stsp switch (ch) {
6724 5ef14e63 2019-06-02 stsp default:
6725 5ef14e63 2019-06-02 stsp usage_backout();
6726 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6727 5ef14e63 2019-06-02 stsp }
6728 5ef14e63 2019-06-02 stsp }
6729 5ef14e63 2019-06-02 stsp
6730 5ef14e63 2019-06-02 stsp argc -= optind;
6731 5ef14e63 2019-06-02 stsp argv += optind;
6732 5ef14e63 2019-06-02 stsp
6733 43012d58 2019-07-14 stsp #ifndef PROFILE
6734 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6735 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6736 43012d58 2019-07-14 stsp err(1, "pledge");
6737 43012d58 2019-07-14 stsp #endif
6738 5ef14e63 2019-06-02 stsp if (argc != 1)
6739 5ef14e63 2019-06-02 stsp usage_backout();
6740 5ef14e63 2019-06-02 stsp
6741 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6742 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6743 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6744 5ef14e63 2019-06-02 stsp goto done;
6745 5ef14e63 2019-06-02 stsp }
6746 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6747 fa51e947 2020-03-27 stsp if (error) {
6748 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
6749 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "backout", cwd);
6750 5ef14e63 2019-06-02 stsp goto done;
6751 fa51e947 2020-03-27 stsp }
6752 5ef14e63 2019-06-02 stsp
6753 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6754 c9956ddf 2019-09-08 stsp NULL);
6755 5ef14e63 2019-06-02 stsp if (error != NULL)
6756 5ef14e63 2019-06-02 stsp goto done;
6757 5ef14e63 2019-06-02 stsp
6758 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6759 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6760 5ef14e63 2019-06-02 stsp if (error)
6761 5ef14e63 2019-06-02 stsp goto done;
6762 5ef14e63 2019-06-02 stsp
6763 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6764 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6765 5ef14e63 2019-06-02 stsp if (error != NULL) {
6766 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6767 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6768 5ef14e63 2019-06-02 stsp goto done;
6769 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6770 5ef14e63 2019-06-02 stsp if (error != NULL)
6771 5ef14e63 2019-06-02 stsp goto done;
6772 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6773 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6774 5ef14e63 2019-06-02 stsp if (error != NULL)
6775 5ef14e63 2019-06-02 stsp goto done;
6776 5ef14e63 2019-06-02 stsp }
6777 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6778 5ef14e63 2019-06-02 stsp if (error)
6779 5ef14e63 2019-06-02 stsp goto done;
6780 5ef14e63 2019-06-02 stsp
6781 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6782 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6783 5ef14e63 2019-06-02 stsp if (error != NULL)
6784 5ef14e63 2019-06-02 stsp goto done;
6785 5ef14e63 2019-06-02 stsp
6786 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6787 5ef14e63 2019-06-02 stsp if (error)
6788 5ef14e63 2019-06-02 stsp goto done;
6789 5ef14e63 2019-06-02 stsp
6790 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6791 5ef14e63 2019-06-02 stsp if (error)
6792 5ef14e63 2019-06-02 stsp goto done;
6793 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6794 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6795 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6796 5ef14e63 2019-06-02 stsp goto done;
6797 5ef14e63 2019-06-02 stsp }
6798 5ef14e63 2019-06-02 stsp
6799 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
6800 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6801 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
6802 5ef14e63 2019-06-02 stsp if (error != NULL)
6803 5ef14e63 2019-06-02 stsp goto done;
6804 5ef14e63 2019-06-02 stsp
6805 9627c110 2020-04-18 stsp if (upa.did_something)
6806 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6807 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
6808 5ef14e63 2019-06-02 stsp done:
6809 5ef14e63 2019-06-02 stsp if (commit)
6810 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6811 5ef14e63 2019-06-02 stsp free(commit_id_str);
6812 5ef14e63 2019-06-02 stsp if (head_ref)
6813 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6814 818c7501 2019-07-11 stsp if (worktree)
6815 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6816 818c7501 2019-07-11 stsp if (repo)
6817 818c7501 2019-07-11 stsp got_repo_close(repo);
6818 818c7501 2019-07-11 stsp return error;
6819 818c7501 2019-07-11 stsp }
6820 818c7501 2019-07-11 stsp
6821 818c7501 2019-07-11 stsp __dead static void
6822 818c7501 2019-07-11 stsp usage_rebase(void)
6823 818c7501 2019-07-11 stsp {
6824 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6825 af54c8f8 2019-07-11 stsp getprogname());
6826 818c7501 2019-07-11 stsp exit(1);
6827 0ebf8283 2019-07-24 stsp }
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp void
6830 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6831 0ebf8283 2019-07-24 stsp {
6832 0ebf8283 2019-07-24 stsp char *nl;
6833 0ebf8283 2019-07-24 stsp size_t len;
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6836 0ebf8283 2019-07-24 stsp if (len > limit)
6837 0ebf8283 2019-07-24 stsp len = limit;
6838 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6839 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6840 0ebf8283 2019-07-24 stsp if (nl)
6841 0ebf8283 2019-07-24 stsp *nl = '\0';
6842 0ebf8283 2019-07-24 stsp }
6843 0ebf8283 2019-07-24 stsp
6844 0ebf8283 2019-07-24 stsp static const struct got_error *
6845 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6846 0ebf8283 2019-07-24 stsp {
6847 5943eee2 2019-08-13 stsp const struct got_error *err;
6848 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6849 5943eee2 2019-08-13 stsp const char *s;
6850 0ebf8283 2019-07-24 stsp
6851 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6852 5943eee2 2019-08-13 stsp if (err)
6853 5943eee2 2019-08-13 stsp return err;
6854 0ebf8283 2019-07-24 stsp
6855 5943eee2 2019-08-13 stsp s = logmsg0;
6856 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6857 5943eee2 2019-08-13 stsp s++;
6858 5943eee2 2019-08-13 stsp
6859 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6860 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6861 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6862 5943eee2 2019-08-13 stsp goto done;
6863 5943eee2 2019-08-13 stsp }
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6866 5943eee2 2019-08-13 stsp done:
6867 5943eee2 2019-08-13 stsp free(logmsg0);
6868 a0ea4fc0 2020-02-28 stsp return err;
6869 a0ea4fc0 2020-02-28 stsp }
6870 a0ea4fc0 2020-02-28 stsp
6871 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6872 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6873 a0ea4fc0 2020-02-28 stsp {
6874 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6875 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6876 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6877 a0ea4fc0 2020-02-28 stsp
6878 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6879 a0ea4fc0 2020-02-28 stsp if (err)
6880 a0ea4fc0 2020-02-28 stsp return err;
6881 a0ea4fc0 2020-02-28 stsp
6882 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6883 a0ea4fc0 2020-02-28 stsp if (err)
6884 a0ea4fc0 2020-02-28 stsp goto done;
6885 a0ea4fc0 2020-02-28 stsp
6886 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6887 a0ea4fc0 2020-02-28 stsp
6888 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6889 a0ea4fc0 2020-02-28 stsp if (err)
6890 a0ea4fc0 2020-02-28 stsp goto done;
6891 a0ea4fc0 2020-02-28 stsp
6892 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6893 a0ea4fc0 2020-02-28 stsp done:
6894 a0ea4fc0 2020-02-28 stsp free(id_str);
6895 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6896 a0ea4fc0 2020-02-28 stsp free(logmsg);
6897 5943eee2 2019-08-13 stsp return err;
6898 818c7501 2019-07-11 stsp }
6899 818c7501 2019-07-11 stsp
6900 818c7501 2019-07-11 stsp static const struct got_error *
6901 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6902 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6903 818c7501 2019-07-11 stsp {
6904 818c7501 2019-07-11 stsp const struct got_error *err;
6905 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6906 818c7501 2019-07-11 stsp
6907 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6908 818c7501 2019-07-11 stsp if (err)
6909 818c7501 2019-07-11 stsp goto done;
6910 818c7501 2019-07-11 stsp
6911 ff0d2220 2019-07-11 stsp if (new_id) {
6912 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6913 ff0d2220 2019-07-11 stsp if (err)
6914 ff0d2220 2019-07-11 stsp goto done;
6915 ff0d2220 2019-07-11 stsp }
6916 818c7501 2019-07-11 stsp
6917 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6918 ff0d2220 2019-07-11 stsp if (new_id_str)
6919 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6920 0ebf8283 2019-07-24 stsp
6921 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6922 0ebf8283 2019-07-24 stsp if (err)
6923 0ebf8283 2019-07-24 stsp goto done;
6924 0ebf8283 2019-07-24 stsp
6925 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6926 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6927 818c7501 2019-07-11 stsp done:
6928 818c7501 2019-07-11 stsp free(old_id_str);
6929 818c7501 2019-07-11 stsp free(new_id_str);
6930 272a1371 2020-02-28 stsp free(logmsg);
6931 818c7501 2019-07-11 stsp return err;
6932 818c7501 2019-07-11 stsp }
6933 818c7501 2019-07-11 stsp
6934 1ee397ad 2019-07-12 stsp static const struct got_error *
6935 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6936 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6937 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6938 818c7501 2019-07-11 stsp {
6939 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6940 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6941 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6942 818c7501 2019-07-11 stsp }
6943 818c7501 2019-07-11 stsp
6944 818c7501 2019-07-11 stsp static const struct got_error *
6945 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6946 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6947 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6948 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6949 ff0d2220 2019-07-11 stsp {
6950 ff0d2220 2019-07-11 stsp const struct got_error *error;
6951 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6952 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6953 ff0d2220 2019-07-11 stsp
6954 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6955 ff0d2220 2019-07-11 stsp if (error)
6956 ff0d2220 2019-07-11 stsp return error;
6957 ff0d2220 2019-07-11 stsp
6958 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6959 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6960 ff0d2220 2019-07-11 stsp if (error) {
6961 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6962 ff0d2220 2019-07-11 stsp goto done;
6963 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6964 ff0d2220 2019-07-11 stsp } else {
6965 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6966 ff0d2220 2019-07-11 stsp free(new_commit_id);
6967 ff0d2220 2019-07-11 stsp }
6968 ff0d2220 2019-07-11 stsp done:
6969 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6970 ff0d2220 2019-07-11 stsp return error;
6971 64c6d990 2019-07-11 stsp }
6972 64c6d990 2019-07-11 stsp
6973 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6974 64c6d990 2019-07-11 stsp const char *path_prefix;
6975 64c6d990 2019-07-11 stsp size_t len;
6976 8ca9bd68 2019-07-25 stsp int errcode;
6977 64c6d990 2019-07-11 stsp };
6978 64c6d990 2019-07-11 stsp
6979 64c6d990 2019-07-11 stsp static const struct got_error *
6980 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6981 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6982 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6983 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6984 64c6d990 2019-07-11 stsp {
6985 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6986 64c6d990 2019-07-11 stsp
6987 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6988 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6989 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6990 64c6d990 2019-07-11 stsp
6991 64c6d990 2019-07-11 stsp return NULL;
6992 64c6d990 2019-07-11 stsp }
6993 64c6d990 2019-07-11 stsp
6994 64c6d990 2019-07-11 stsp static const struct got_error *
6995 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6996 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6997 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6998 64c6d990 2019-07-11 stsp {
6999 64c6d990 2019-07-11 stsp const struct got_error *err;
7000 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
7001 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
7002 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
7003 64c6d990 2019-07-11 stsp
7004 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
7005 64c6d990 2019-07-11 stsp return NULL;
7006 64c6d990 2019-07-11 stsp
7007 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7008 64c6d990 2019-07-11 stsp if (err)
7009 64c6d990 2019-07-11 stsp goto done;
7010 64c6d990 2019-07-11 stsp
7011 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
7012 64c6d990 2019-07-11 stsp if (err)
7013 64c6d990 2019-07-11 stsp goto done;
7014 64c6d990 2019-07-11 stsp
7015 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
7016 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
7017 64c6d990 2019-07-11 stsp if (err)
7018 64c6d990 2019-07-11 stsp goto done;
7019 64c6d990 2019-07-11 stsp
7020 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
7021 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
7022 64c6d990 2019-07-11 stsp if (err)
7023 64c6d990 2019-07-11 stsp goto done;
7024 64c6d990 2019-07-11 stsp
7025 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
7026 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
7027 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
7028 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
7029 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
7030 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
7031 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
7032 64c6d990 2019-07-11 stsp done:
7033 64c6d990 2019-07-11 stsp if (tree1)
7034 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
7035 64c6d990 2019-07-11 stsp if (tree2)
7036 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
7037 64c6d990 2019-07-11 stsp if (commit)
7038 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
7039 64c6d990 2019-07-11 stsp if (parent_commit)
7040 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
7041 64c6d990 2019-07-11 stsp return err;
7042 ff0d2220 2019-07-11 stsp }
7043 ff0d2220 2019-07-11 stsp
7044 ff0d2220 2019-07-11 stsp static const struct got_error *
7045 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
7046 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
7047 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
7048 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
7049 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
7050 af61c510 2019-07-19 stsp {
7051 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
7052 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
7053 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
7054 af61c510 2019-07-19 stsp struct got_object_qid *qid;
7055 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
7056 af61c510 2019-07-19 stsp
7057 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
7058 af61c510 2019-07-19 stsp if (err)
7059 af61c510 2019-07-19 stsp return err;
7060 af61c510 2019-07-19 stsp
7061 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
7062 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7063 af61c510 2019-07-19 stsp if (err)
7064 af61c510 2019-07-19 stsp goto done;
7065 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
7066 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
7067 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
7068 af61c510 2019-07-19 stsp if (err) {
7069 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
7070 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
7071 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
7072 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
7073 af61c510 2019-07-19 stsp "been reached?!?");
7074 ee780d5c 2020-01-04 stsp }
7075 ee780d5c 2020-01-04 stsp goto done;
7076 af61c510 2019-07-19 stsp } else {
7077 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
7078 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
7079 af61c510 2019-07-19 stsp if (err)
7080 af61c510 2019-07-19 stsp goto done;
7081 af61c510 2019-07-19 stsp
7082 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
7083 af61c510 2019-07-19 stsp if (err)
7084 af61c510 2019-07-19 stsp goto done;
7085 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
7086 af61c510 2019-07-19 stsp commit_id = parent_id;
7087 af61c510 2019-07-19 stsp }
7088 af61c510 2019-07-19 stsp }
7089 af61c510 2019-07-19 stsp done:
7090 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
7091 af61c510 2019-07-19 stsp return err;
7092 af61c510 2019-07-19 stsp }
7093 af61c510 2019-07-19 stsp
7094 af61c510 2019-07-19 stsp static const struct got_error *
7095 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
7096 818c7501 2019-07-11 stsp {
7097 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
7098 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
7099 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
7100 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7101 818c7501 2019-07-11 stsp char *cwd = NULL;
7102 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
7103 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
7104 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
7105 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
7106 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
7107 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
7108 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
7109 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
7110 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7111 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
7112 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
7113 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
7114 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
7115 818c7501 2019-07-11 stsp
7116 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
7117 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
7118 818c7501 2019-07-11 stsp
7119 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
7120 818c7501 2019-07-11 stsp switch (ch) {
7121 818c7501 2019-07-11 stsp case 'a':
7122 818c7501 2019-07-11 stsp abort_rebase = 1;
7123 818c7501 2019-07-11 stsp break;
7124 818c7501 2019-07-11 stsp case 'c':
7125 818c7501 2019-07-11 stsp continue_rebase = 1;
7126 818c7501 2019-07-11 stsp break;
7127 818c7501 2019-07-11 stsp default:
7128 818c7501 2019-07-11 stsp usage_rebase();
7129 818c7501 2019-07-11 stsp /* NOTREACHED */
7130 818c7501 2019-07-11 stsp }
7131 818c7501 2019-07-11 stsp }
7132 818c7501 2019-07-11 stsp
7133 818c7501 2019-07-11 stsp argc -= optind;
7134 818c7501 2019-07-11 stsp argv += optind;
7135 818c7501 2019-07-11 stsp
7136 43012d58 2019-07-14 stsp #ifndef PROFILE
7137 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7138 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
7139 43012d58 2019-07-14 stsp err(1, "pledge");
7140 43012d58 2019-07-14 stsp #endif
7141 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
7142 818c7501 2019-07-11 stsp usage_rebase();
7143 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
7144 818c7501 2019-07-11 stsp if (argc != 0)
7145 818c7501 2019-07-11 stsp usage_rebase();
7146 818c7501 2019-07-11 stsp } else if (argc != 1)
7147 818c7501 2019-07-11 stsp usage_rebase();
7148 818c7501 2019-07-11 stsp
7149 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
7150 818c7501 2019-07-11 stsp if (cwd == NULL) {
7151 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
7152 818c7501 2019-07-11 stsp goto done;
7153 818c7501 2019-07-11 stsp }
7154 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
7155 fa51e947 2020-03-27 stsp if (error) {
7156 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
7157 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "rebase", cwd);
7158 818c7501 2019-07-11 stsp goto done;
7159 fa51e947 2020-03-27 stsp }
7160 818c7501 2019-07-11 stsp
7161 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7162 c9956ddf 2019-09-08 stsp NULL);
7163 818c7501 2019-07-11 stsp if (error != NULL)
7164 818c7501 2019-07-11 stsp goto done;
7165 818c7501 2019-07-11 stsp
7166 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7167 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
7168 7ef62c4e 2020-02-24 stsp if (error)
7169 7ef62c4e 2020-02-24 stsp goto done;
7170 7ef62c4e 2020-02-24 stsp
7171 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
7172 7ef62c4e 2020-02-24 stsp worktree);
7173 818c7501 2019-07-11 stsp if (error)
7174 7ef62c4e 2020-02-24 stsp goto done;
7175 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
7176 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7177 818c7501 2019-07-11 stsp goto done;
7178 7ef62c4e 2020-02-24 stsp }
7179 818c7501 2019-07-11 stsp
7180 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7181 818c7501 2019-07-11 stsp if (error)
7182 818c7501 2019-07-11 stsp goto done;
7183 818c7501 2019-07-11 stsp
7184 f6794adc 2019-07-23 stsp if (abort_rebase) {
7185 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7186 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7187 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7188 f6794adc 2019-07-23 stsp goto done;
7189 f6794adc 2019-07-23 stsp }
7190 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7191 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7192 3e3a69f1 2019-07-25 stsp worktree, repo);
7193 818c7501 2019-07-11 stsp if (error)
7194 818c7501 2019-07-11 stsp goto done;
7195 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
7196 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
7197 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7198 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
7199 9627c110 2020-04-18 stsp new_base_branch, update_progress, &upa);
7200 818c7501 2019-07-11 stsp if (error)
7201 818c7501 2019-07-11 stsp goto done;
7202 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
7203 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7204 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
7205 818c7501 2019-07-11 stsp }
7206 818c7501 2019-07-11 stsp
7207 818c7501 2019-07-11 stsp if (continue_rebase) {
7208 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
7209 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
7210 f6794adc 2019-07-23 stsp goto done;
7211 f6794adc 2019-07-23 stsp }
7212 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
7213 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
7214 3e3a69f1 2019-07-25 stsp worktree, repo);
7215 818c7501 2019-07-11 stsp if (error)
7216 818c7501 2019-07-11 stsp goto done;
7217 818c7501 2019-07-11 stsp
7218 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
7219 01757395 2019-07-12 stsp resume_commit_id, repo);
7220 818c7501 2019-07-11 stsp if (error)
7221 818c7501 2019-07-11 stsp goto done;
7222 818c7501 2019-07-11 stsp
7223 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
7224 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
7225 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
7226 818c7501 2019-07-11 stsp goto done;
7227 818c7501 2019-07-11 stsp }
7228 818c7501 2019-07-11 stsp } else {
7229 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
7230 818c7501 2019-07-11 stsp if (error != NULL)
7231 818c7501 2019-07-11 stsp goto done;
7232 ff0d2220 2019-07-11 stsp }
7233 818c7501 2019-07-11 stsp
7234 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
7235 ff0d2220 2019-07-11 stsp if (error)
7236 ff0d2220 2019-07-11 stsp goto done;
7237 ff0d2220 2019-07-11 stsp
7238 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
7239 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
7240 a51a74b3 2019-07-27 stsp
7241 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
7242 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
7243 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
7244 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
7245 818c7501 2019-07-11 stsp if (error)
7246 818c7501 2019-07-11 stsp goto done;
7247 818c7501 2019-07-11 stsp if (yca_id == NULL) {
7248 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
7249 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
7250 818c7501 2019-07-11 stsp "with work tree's branch");
7251 818c7501 2019-07-11 stsp goto done;
7252 818c7501 2019-07-11 stsp }
7253 818c7501 2019-07-11 stsp
7254 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
7255 a51a74b3 2019-07-27 stsp if (error) {
7256 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
7257 a51a74b3 2019-07-27 stsp goto done;
7258 a51a74b3 2019-07-27 stsp error = NULL;
7259 a51a74b3 2019-07-27 stsp } else {
7260 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7261 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
7262 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
7263 a51a74b3 2019-07-27 stsp goto done;
7264 a51a74b3 2019-07-27 stsp }
7265 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
7266 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
7267 818c7501 2019-07-11 stsp if (error)
7268 818c7501 2019-07-11 stsp goto done;
7269 818c7501 2019-07-11 stsp }
7270 818c7501 2019-07-11 stsp
7271 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
7272 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
7273 818c7501 2019-07-11 stsp if (error)
7274 818c7501 2019-07-11 stsp goto done;
7275 818c7501 2019-07-11 stsp
7276 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7277 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
7278 fc66b545 2019-08-12 stsp if (pid == NULL) {
7279 fc66b545 2019-08-12 stsp if (!continue_rebase) {
7280 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7281 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7282 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
7283 9627c110 2020-04-18 stsp repo, new_base_branch, update_progress, &upa);
7284 fc66b545 2019-08-12 stsp if (error)
7285 fc66b545 2019-08-12 stsp goto done;
7286 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
7287 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
7288 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7289 9627c110 2020-04-18 stsp
7290 fc66b545 2019-08-12 stsp }
7291 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
7292 fc66b545 2019-08-12 stsp goto done;
7293 fc66b545 2019-08-12 stsp }
7294 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
7295 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
7296 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
7297 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7298 818c7501 2019-07-11 stsp commit = NULL;
7299 818c7501 2019-07-11 stsp if (error)
7300 818c7501 2019-07-11 stsp goto done;
7301 64c6d990 2019-07-11 stsp
7302 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
7303 38b0338b 2019-11-29 stsp if (continue_rebase) {
7304 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
7305 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
7306 38b0338b 2019-11-29 stsp goto done;
7307 38b0338b 2019-11-29 stsp } else {
7308 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
7309 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
7310 38b0338b 2019-11-29 stsp char *id_str;
7311 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
7312 38b0338b 2019-11-29 stsp new_base_branch);
7313 38b0338b 2019-11-29 stsp if (error)
7314 38b0338b 2019-11-29 stsp goto done;
7315 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
7316 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
7317 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
7318 38b0338b 2019-11-29 stsp free(id_str);
7319 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
7320 38b0338b 2019-11-29 stsp new_head_commit_id);
7321 38b0338b 2019-11-29 stsp if (error)
7322 38b0338b 2019-11-29 stsp goto done;
7323 38b0338b 2019-11-29 stsp }
7324 818c7501 2019-07-11 stsp }
7325 818c7501 2019-07-11 stsp
7326 818c7501 2019-07-11 stsp pid = NULL;
7327 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
7328 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
7329 9627c110 2020-04-18 stsp
7330 818c7501 2019-07-11 stsp commit_id = qid->id;
7331 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
7332 818c7501 2019-07-11 stsp pid = qid;
7333 818c7501 2019-07-11 stsp
7334 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
7335 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
7336 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
7337 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
7338 818c7501 2019-07-11 stsp if (error)
7339 818c7501 2019-07-11 stsp goto done;
7340 9627c110 2020-04-18 stsp
7341 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
7342 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
7343 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
7344 818c7501 2019-07-11 stsp
7345 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7346 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
7347 a0ea4fc0 2020-02-28 stsp if (error)
7348 a0ea4fc0 2020-02-28 stsp goto done;
7349 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7350 818c7501 2019-07-11 stsp break;
7351 01757395 2019-07-12 stsp }
7352 818c7501 2019-07-11 stsp
7353 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
7354 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
7355 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7356 818c7501 2019-07-11 stsp if (error)
7357 818c7501 2019-07-11 stsp goto done;
7358 818c7501 2019-07-11 stsp }
7359 818c7501 2019-07-11 stsp
7360 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7361 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
7362 818c7501 2019-07-11 stsp if (error)
7363 818c7501 2019-07-11 stsp goto done;
7364 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7365 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
7366 818c7501 2019-07-11 stsp } else
7367 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
7368 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
7369 818c7501 2019-07-11 stsp done:
7370 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
7371 818c7501 2019-07-11 stsp free(branch_head_commit_id);
7372 818c7501 2019-07-11 stsp free(resume_commit_id);
7373 818c7501 2019-07-11 stsp free(yca_id);
7374 818c7501 2019-07-11 stsp if (commit)
7375 818c7501 2019-07-11 stsp got_object_commit_close(commit);
7376 818c7501 2019-07-11 stsp if (branch)
7377 818c7501 2019-07-11 stsp got_ref_close(branch);
7378 818c7501 2019-07-11 stsp if (new_base_branch)
7379 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
7380 818c7501 2019-07-11 stsp if (tmp_branch)
7381 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
7382 5ef14e63 2019-06-02 stsp if (worktree)
7383 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
7384 5ef14e63 2019-06-02 stsp if (repo)
7385 5ef14e63 2019-06-02 stsp got_repo_close(repo);
7386 5ef14e63 2019-06-02 stsp return error;
7387 0ebf8283 2019-07-24 stsp }
7388 0ebf8283 2019-07-24 stsp
7389 0ebf8283 2019-07-24 stsp __dead static void
7390 0ebf8283 2019-07-24 stsp usage_histedit(void)
7391 0ebf8283 2019-07-24 stsp {
7392 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
7393 0ebf8283 2019-07-24 stsp getprogname());
7394 0ebf8283 2019-07-24 stsp exit(1);
7395 0ebf8283 2019-07-24 stsp }
7396 0ebf8283 2019-07-24 stsp
7397 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
7398 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
7399 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
7400 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
7401 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
7402 0ebf8283 2019-07-24 stsp
7403 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
7404 0ebf8283 2019-07-24 stsp unsigned char code;
7405 0ebf8283 2019-07-24 stsp const char *name;
7406 0ebf8283 2019-07-24 stsp const char *desc;
7407 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
7408 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
7409 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
7410 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
7411 82997472 2020-01-29 stsp "be used" },
7412 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
7413 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
7414 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
7415 0ebf8283 2019-07-24 stsp };
7416 0ebf8283 2019-07-24 stsp
7417 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
7418 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
7419 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
7420 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7421 0ebf8283 2019-07-24 stsp char *logmsg;
7422 0ebf8283 2019-07-24 stsp };
7423 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
7424 0ebf8283 2019-07-24 stsp
7425 0ebf8283 2019-07-24 stsp static const struct got_error *
7426 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
7427 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7428 0ebf8283 2019-07-24 stsp {
7429 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7430 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
7431 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7432 8138f3e1 2019-08-11 stsp int n;
7433 0ebf8283 2019-07-24 stsp
7434 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7435 0ebf8283 2019-07-24 stsp if (err)
7436 0ebf8283 2019-07-24 stsp goto done;
7437 0ebf8283 2019-07-24 stsp
7438 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7439 0ebf8283 2019-07-24 stsp if (err)
7440 0ebf8283 2019-07-24 stsp goto done;
7441 0ebf8283 2019-07-24 stsp
7442 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7443 0ebf8283 2019-07-24 stsp if (err)
7444 0ebf8283 2019-07-24 stsp goto done;
7445 0ebf8283 2019-07-24 stsp
7446 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7447 0ebf8283 2019-07-24 stsp if (n < 0)
7448 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7449 0ebf8283 2019-07-24 stsp done:
7450 0ebf8283 2019-07-24 stsp if (commit)
7451 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7452 0ebf8283 2019-07-24 stsp free(id_str);
7453 0ebf8283 2019-07-24 stsp free(logmsg);
7454 0ebf8283 2019-07-24 stsp return err;
7455 0ebf8283 2019-07-24 stsp }
7456 0ebf8283 2019-07-24 stsp
7457 0ebf8283 2019-07-24 stsp static const struct got_error *
7458 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7459 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7460 0ebf8283 2019-07-24 stsp {
7461 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7462 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7463 0ebf8283 2019-07-24 stsp
7464 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7465 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7466 0ebf8283 2019-07-24 stsp
7467 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7468 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7469 0ebf8283 2019-07-24 stsp f, repo);
7470 0ebf8283 2019-07-24 stsp if (err)
7471 0ebf8283 2019-07-24 stsp break;
7472 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7473 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7474 083957f4 2020-02-24 stsp if (n < 0) {
7475 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7476 083957f4 2020-02-24 stsp break;
7477 083957f4 2020-02-24 stsp }
7478 083957f4 2020-02-24 stsp }
7479 0ebf8283 2019-07-24 stsp }
7480 0ebf8283 2019-07-24 stsp
7481 0ebf8283 2019-07-24 stsp return err;
7482 0ebf8283 2019-07-24 stsp }
7483 0ebf8283 2019-07-24 stsp
7484 0ebf8283 2019-07-24 stsp static const struct got_error *
7485 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7486 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7487 0ebf8283 2019-07-24 stsp {
7488 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7489 0ebf8283 2019-07-24 stsp int n, i;
7490 514f2ffe 2020-01-29 stsp char *id_str;
7491 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7492 514f2ffe 2020-01-29 stsp
7493 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7494 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7495 514f2ffe 2020-01-29 stsp if (err)
7496 514f2ffe 2020-01-29 stsp return err;
7497 514f2ffe 2020-01-29 stsp
7498 514f2ffe 2020-01-29 stsp n = fprintf(f,
7499 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7500 514f2ffe 2020-01-29 stsp "# commit %s\n"
7501 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7502 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7503 514f2ffe 2020-01-29 stsp if (n < 0) {
7504 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7505 514f2ffe 2020-01-29 stsp goto done;
7506 514f2ffe 2020-01-29 stsp }
7507 0ebf8283 2019-07-24 stsp
7508 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7509 514f2ffe 2020-01-29 stsp if (n < 0) {
7510 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7511 514f2ffe 2020-01-29 stsp goto done;
7512 514f2ffe 2020-01-29 stsp }
7513 0ebf8283 2019-07-24 stsp
7514 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7515 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7516 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7517 0ebf8283 2019-07-24 stsp cmd->desc);
7518 0ebf8283 2019-07-24 stsp if (n < 0) {
7519 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7520 0ebf8283 2019-07-24 stsp break;
7521 0ebf8283 2019-07-24 stsp }
7522 0ebf8283 2019-07-24 stsp }
7523 514f2ffe 2020-01-29 stsp done:
7524 514f2ffe 2020-01-29 stsp free(id_str);
7525 0ebf8283 2019-07-24 stsp return err;
7526 0ebf8283 2019-07-24 stsp }
7527 0ebf8283 2019-07-24 stsp
7528 0ebf8283 2019-07-24 stsp static const struct got_error *
7529 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7530 0ebf8283 2019-07-24 stsp {
7531 0ebf8283 2019-07-24 stsp static char msg[42];
7532 0ebf8283 2019-07-24 stsp int ret;
7533 0ebf8283 2019-07-24 stsp
7534 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7535 0ebf8283 2019-07-24 stsp lineno);
7536 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7537 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7538 0ebf8283 2019-07-24 stsp
7539 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7540 0ebf8283 2019-07-24 stsp }
7541 0ebf8283 2019-07-24 stsp
7542 0ebf8283 2019-07-24 stsp static const struct got_error *
7543 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7544 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7545 0ebf8283 2019-07-24 stsp {
7546 0ebf8283 2019-07-24 stsp const struct got_error *err;
7547 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7548 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7549 0ebf8283 2019-07-24 stsp
7550 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7551 0ebf8283 2019-07-24 stsp if (err)
7552 0ebf8283 2019-07-24 stsp return err;
7553 0ebf8283 2019-07-24 stsp
7554 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7555 0ebf8283 2019-07-24 stsp if (err)
7556 0ebf8283 2019-07-24 stsp goto done;
7557 0ebf8283 2019-07-24 stsp
7558 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7559 5943eee2 2019-08-13 stsp if (err)
7560 5943eee2 2019-08-13 stsp goto done;
7561 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7562 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7563 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7564 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7565 0ebf8283 2019-07-24 stsp }
7566 0ebf8283 2019-07-24 stsp done:
7567 0ebf8283 2019-07-24 stsp if (folded_commit)
7568 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7569 0ebf8283 2019-07-24 stsp free(id_str);
7570 5943eee2 2019-08-13 stsp free(folded_logmsg);
7571 0ebf8283 2019-07-24 stsp return err;
7572 0ebf8283 2019-07-24 stsp }
7573 0ebf8283 2019-07-24 stsp
7574 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7575 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7576 0ebf8283 2019-07-24 stsp {
7577 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7578 0ebf8283 2019-07-24 stsp
7579 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7580 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7581 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7582 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7583 3f9de99f 2019-07-24 stsp folded = prev;
7584 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7585 0ebf8283 2019-07-24 stsp }
7586 0ebf8283 2019-07-24 stsp
7587 0ebf8283 2019-07-24 stsp return folded;
7588 0ebf8283 2019-07-24 stsp }
7589 0ebf8283 2019-07-24 stsp
7590 0ebf8283 2019-07-24 stsp static const struct got_error *
7591 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7592 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7593 0ebf8283 2019-07-24 stsp {
7594 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7595 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7596 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7597 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7598 0ebf8283 2019-07-24 stsp int fd;
7599 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7600 0ebf8283 2019-07-24 stsp
7601 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7602 0ebf8283 2019-07-24 stsp if (err)
7603 0ebf8283 2019-07-24 stsp return err;
7604 0ebf8283 2019-07-24 stsp
7605 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7606 0ebf8283 2019-07-24 stsp if (folded) {
7607 0ebf8283 2019-07-24 stsp while (folded != hle) {
7608 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7609 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7610 3f9de99f 2019-07-24 stsp continue;
7611 3f9de99f 2019-07-24 stsp }
7612 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7613 0ebf8283 2019-07-24 stsp logmsg, repo);
7614 0ebf8283 2019-07-24 stsp if (err)
7615 0ebf8283 2019-07-24 stsp goto done;
7616 0ebf8283 2019-07-24 stsp free(logmsg);
7617 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7618 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7619 0ebf8283 2019-07-24 stsp }
7620 0ebf8283 2019-07-24 stsp }
7621 0ebf8283 2019-07-24 stsp
7622 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7623 0ebf8283 2019-07-24 stsp if (err)
7624 0ebf8283 2019-07-24 stsp goto done;
7625 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7626 5943eee2 2019-08-13 stsp if (err)
7627 5943eee2 2019-08-13 stsp goto done;
7628 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7629 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7630 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7631 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7632 0ebf8283 2019-07-24 stsp goto done;
7633 0ebf8283 2019-07-24 stsp }
7634 0ebf8283 2019-07-24 stsp free(logmsg);
7635 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7636 0ebf8283 2019-07-24 stsp
7637 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7638 0ebf8283 2019-07-24 stsp if (err)
7639 0ebf8283 2019-07-24 stsp goto done;
7640 0ebf8283 2019-07-24 stsp
7641 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7642 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7643 0ebf8283 2019-07-24 stsp if (err)
7644 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp
7646 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7647 0ebf8283 2019-07-24 stsp close(fd);
7648 0ebf8283 2019-07-24 stsp
7649 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7650 0ebf8283 2019-07-24 stsp if (err)
7651 0ebf8283 2019-07-24 stsp goto done;
7652 0ebf8283 2019-07-24 stsp
7653 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7654 0ebf8283 2019-07-24 stsp if (err) {
7655 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7656 0ebf8283 2019-07-24 stsp goto done;
7657 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7658 0ebf8283 2019-07-24 stsp }
7659 0ebf8283 2019-07-24 stsp done:
7660 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7661 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7662 0ebf8283 2019-07-24 stsp free(logmsg_path);
7663 0ebf8283 2019-07-24 stsp free(logmsg);
7664 5943eee2 2019-08-13 stsp free(orig_logmsg);
7665 0ebf8283 2019-07-24 stsp free(editor);
7666 0ebf8283 2019-07-24 stsp if (commit)
7667 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7668 0ebf8283 2019-07-24 stsp return err;
7669 5ef14e63 2019-06-02 stsp }
7670 0ebf8283 2019-07-24 stsp
7671 0ebf8283 2019-07-24 stsp static const struct got_error *
7672 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7673 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7674 0ebf8283 2019-07-24 stsp {
7675 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7676 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7677 0ebf8283 2019-07-24 stsp size_t size;
7678 0ebf8283 2019-07-24 stsp ssize_t len;
7679 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7680 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7681 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7682 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7683 0ebf8283 2019-07-24 stsp
7684 0ebf8283 2019-07-24 stsp for (;;) {
7685 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7686 0ebf8283 2019-07-24 stsp if (len == -1) {
7687 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7688 0ebf8283 2019-07-24 stsp if (feof(f))
7689 0ebf8283 2019-07-24 stsp break;
7690 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7691 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7692 0ebf8283 2019-07-24 stsp break;
7693 0ebf8283 2019-07-24 stsp }
7694 0ebf8283 2019-07-24 stsp lineno++;
7695 0ebf8283 2019-07-24 stsp p = line;
7696 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7697 0ebf8283 2019-07-24 stsp p++;
7698 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7699 0ebf8283 2019-07-24 stsp free(line);
7700 0ebf8283 2019-07-24 stsp line = NULL;
7701 0ebf8283 2019-07-24 stsp continue;
7702 0ebf8283 2019-07-24 stsp }
7703 0ebf8283 2019-07-24 stsp cmd = NULL;
7704 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7705 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7706 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7707 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7708 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7709 0ebf8283 2019-07-24 stsp break;
7710 0ebf8283 2019-07-24 stsp }
7711 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7712 0ebf8283 2019-07-24 stsp p++;
7713 0ebf8283 2019-07-24 stsp break;
7714 0ebf8283 2019-07-24 stsp }
7715 0ebf8283 2019-07-24 stsp }
7716 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7717 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7718 0ebf8283 2019-07-24 stsp break;
7719 0ebf8283 2019-07-24 stsp }
7720 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7721 0ebf8283 2019-07-24 stsp p++;
7722 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7723 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7724 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7725 0ebf8283 2019-07-24 stsp break;
7726 0ebf8283 2019-07-24 stsp }
7727 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7728 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7729 0ebf8283 2019-07-24 stsp if (err)
7730 0ebf8283 2019-07-24 stsp break;
7731 0ebf8283 2019-07-24 stsp } else {
7732 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7733 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7734 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7735 0ebf8283 2019-07-24 stsp break;
7736 0ebf8283 2019-07-24 stsp }
7737 0ebf8283 2019-07-24 stsp }
7738 0ebf8283 2019-07-24 stsp free(line);
7739 0ebf8283 2019-07-24 stsp line = NULL;
7740 0ebf8283 2019-07-24 stsp continue;
7741 0ebf8283 2019-07-24 stsp } else {
7742 0ebf8283 2019-07-24 stsp end = p;
7743 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7744 0ebf8283 2019-07-24 stsp end++;
7745 0ebf8283 2019-07-24 stsp *end = '\0';
7746 0ebf8283 2019-07-24 stsp
7747 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7748 0ebf8283 2019-07-24 stsp if (err) {
7749 0ebf8283 2019-07-24 stsp /* override error code */
7750 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7751 0ebf8283 2019-07-24 stsp break;
7752 0ebf8283 2019-07-24 stsp }
7753 0ebf8283 2019-07-24 stsp }
7754 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7755 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7756 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7757 0ebf8283 2019-07-24 stsp break;
7758 0ebf8283 2019-07-24 stsp }
7759 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7760 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7761 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7762 0ebf8283 2019-07-24 stsp commit_id = NULL;
7763 0ebf8283 2019-07-24 stsp free(line);
7764 0ebf8283 2019-07-24 stsp line = NULL;
7765 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7766 0ebf8283 2019-07-24 stsp }
7767 0ebf8283 2019-07-24 stsp
7768 0ebf8283 2019-07-24 stsp free(line);
7769 0ebf8283 2019-07-24 stsp free(commit_id);
7770 0ebf8283 2019-07-24 stsp return err;
7771 0ebf8283 2019-07-24 stsp }
7772 0ebf8283 2019-07-24 stsp
7773 0ebf8283 2019-07-24 stsp static const struct got_error *
7774 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7775 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7776 bfce7f83 2019-07-27 stsp {
7777 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7778 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7779 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7780 5b87815e 2020-03-05 stsp static char msg[92];
7781 bfce7f83 2019-07-27 stsp char *id_str;
7782 bfce7f83 2019-07-27 stsp
7783 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7784 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7785 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7786 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7787 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7788 5b87815e 2020-03-05 stsp
7789 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7790 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7791 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7792 5b87815e 2020-03-05 stsp if (hle == hle2)
7793 5b87815e 2020-03-05 stsp continue;
7794 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7795 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7796 5b87815e 2020-03-05 stsp continue;
7797 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7798 5b87815e 2020-03-05 stsp if (err)
7799 5b87815e 2020-03-05 stsp return err;
7800 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7801 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7802 5b87815e 2020-03-05 stsp free(id_str);
7803 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7804 5b87815e 2020-03-05 stsp }
7805 5b87815e 2020-03-05 stsp }
7806 bfce7f83 2019-07-27 stsp
7807 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7808 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7809 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7810 bfce7f83 2019-07-27 stsp break;
7811 bfce7f83 2019-07-27 stsp }
7812 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7813 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7814 bfce7f83 2019-07-27 stsp if (err)
7815 bfce7f83 2019-07-27 stsp return err;
7816 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7817 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7818 bfce7f83 2019-07-27 stsp free(id_str);
7819 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7820 bfce7f83 2019-07-27 stsp }
7821 bfce7f83 2019-07-27 stsp }
7822 bfce7f83 2019-07-27 stsp
7823 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7824 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7825 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7826 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7827 bfce7f83 2019-07-27 stsp
7828 bfce7f83 2019-07-27 stsp return NULL;
7829 bfce7f83 2019-07-27 stsp }
7830 bfce7f83 2019-07-27 stsp
7831 bfce7f83 2019-07-27 stsp static const struct got_error *
7832 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7833 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7834 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7835 0ebf8283 2019-07-24 stsp {
7836 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7837 0ebf8283 2019-07-24 stsp char *editor;
7838 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7839 0ebf8283 2019-07-24 stsp
7840 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7841 0ebf8283 2019-07-24 stsp if (err)
7842 0ebf8283 2019-07-24 stsp return err;
7843 0ebf8283 2019-07-24 stsp
7844 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7845 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7846 0ebf8283 2019-07-24 stsp goto done;
7847 0ebf8283 2019-07-24 stsp }
7848 0ebf8283 2019-07-24 stsp
7849 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7850 0ebf8283 2019-07-24 stsp if (f == NULL) {
7851 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7852 0ebf8283 2019-07-24 stsp goto done;
7853 0ebf8283 2019-07-24 stsp }
7854 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7855 bfce7f83 2019-07-27 stsp if (err)
7856 bfce7f83 2019-07-27 stsp goto done;
7857 bfce7f83 2019-07-27 stsp
7858 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7859 0ebf8283 2019-07-24 stsp done:
7860 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7861 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7862 0ebf8283 2019-07-24 stsp free(editor);
7863 0ebf8283 2019-07-24 stsp return err;
7864 0ebf8283 2019-07-24 stsp }
7865 0ebf8283 2019-07-24 stsp
7866 0ebf8283 2019-07-24 stsp static const struct got_error *
7867 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7868 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7869 514f2ffe 2020-01-29 stsp struct got_repository *);
7870 0ebf8283 2019-07-24 stsp
7871 0ebf8283 2019-07-24 stsp static const struct got_error *
7872 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7873 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7874 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7875 0ebf8283 2019-07-24 stsp {
7876 0ebf8283 2019-07-24 stsp const struct got_error *err;
7877 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7878 0ebf8283 2019-07-24 stsp char *path = NULL;
7879 0ebf8283 2019-07-24 stsp
7880 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7881 0ebf8283 2019-07-24 stsp if (err)
7882 0ebf8283 2019-07-24 stsp return err;
7883 0ebf8283 2019-07-24 stsp
7884 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7885 0ebf8283 2019-07-24 stsp if (err)
7886 0ebf8283 2019-07-24 stsp goto done;
7887 0ebf8283 2019-07-24 stsp
7888 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7889 0ebf8283 2019-07-24 stsp if (err)
7890 0ebf8283 2019-07-24 stsp goto done;
7891 0ebf8283 2019-07-24 stsp
7892 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7893 083957f4 2020-02-24 stsp rewind(f);
7894 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7895 083957f4 2020-02-24 stsp } else {
7896 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7897 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7898 0ebf8283 2019-07-24 stsp goto done;
7899 083957f4 2020-02-24 stsp }
7900 083957f4 2020-02-24 stsp f = NULL;
7901 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7902 083957f4 2020-02-24 stsp if (err) {
7903 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7904 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7905 083957f4 2020-02-24 stsp goto done;
7906 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7907 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7908 083957f4 2020-02-24 stsp }
7909 0ebf8283 2019-07-24 stsp }
7910 0ebf8283 2019-07-24 stsp done:
7911 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7912 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7913 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7914 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7915 0ebf8283 2019-07-24 stsp free(path);
7916 0ebf8283 2019-07-24 stsp return err;
7917 0ebf8283 2019-07-24 stsp }
7918 0ebf8283 2019-07-24 stsp
7919 0ebf8283 2019-07-24 stsp static const struct got_error *
7920 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7921 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7922 0ebf8283 2019-07-24 stsp {
7923 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7924 0ebf8283 2019-07-24 stsp char *path = NULL;
7925 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7926 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7927 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7928 0ebf8283 2019-07-24 stsp
7929 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7930 0ebf8283 2019-07-24 stsp if (err)
7931 0ebf8283 2019-07-24 stsp return err;
7932 0ebf8283 2019-07-24 stsp
7933 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7934 0ebf8283 2019-07-24 stsp if (f == NULL) {
7935 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7936 0ebf8283 2019-07-24 stsp goto done;
7937 0ebf8283 2019-07-24 stsp }
7938 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7939 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7940 0ebf8283 2019-07-24 stsp repo);
7941 0ebf8283 2019-07-24 stsp if (err)
7942 0ebf8283 2019-07-24 stsp break;
7943 0ebf8283 2019-07-24 stsp
7944 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7945 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7946 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7947 0ebf8283 2019-07-24 stsp if (n < 0) {
7948 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7949 0ebf8283 2019-07-24 stsp break;
7950 0ebf8283 2019-07-24 stsp }
7951 0ebf8283 2019-07-24 stsp }
7952 0ebf8283 2019-07-24 stsp }
7953 0ebf8283 2019-07-24 stsp done:
7954 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7955 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7956 0ebf8283 2019-07-24 stsp free(path);
7957 0ebf8283 2019-07-24 stsp if (commit)
7958 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7959 0ebf8283 2019-07-24 stsp return err;
7960 0ebf8283 2019-07-24 stsp }
7961 0ebf8283 2019-07-24 stsp
7962 bfce7f83 2019-07-27 stsp void
7963 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7964 bfce7f83 2019-07-27 stsp {
7965 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7966 bfce7f83 2019-07-27 stsp
7967 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7968 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7969 bfce7f83 2019-07-27 stsp free(hle);
7970 bfce7f83 2019-07-27 stsp }
7971 bfce7f83 2019-07-27 stsp }
7972 bfce7f83 2019-07-27 stsp
7973 0ebf8283 2019-07-24 stsp static const struct got_error *
7974 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7975 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7976 0ebf8283 2019-07-24 stsp {
7977 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7978 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7979 0ebf8283 2019-07-24 stsp
7980 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7981 0ebf8283 2019-07-24 stsp if (f == NULL) {
7982 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7983 0ebf8283 2019-07-24 stsp goto done;
7984 0ebf8283 2019-07-24 stsp }
7985 0ebf8283 2019-07-24 stsp
7986 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7987 0ebf8283 2019-07-24 stsp done:
7988 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7989 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7990 0ebf8283 2019-07-24 stsp return err;
7991 0ebf8283 2019-07-24 stsp }
7992 0ebf8283 2019-07-24 stsp
7993 0ebf8283 2019-07-24 stsp static const struct got_error *
7994 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7995 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7996 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7997 0ebf8283 2019-07-24 stsp {
7998 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7999 0ebf8283 2019-07-24 stsp int resp = ' ';
8000 0ebf8283 2019-07-24 stsp
8001 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
8002 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
8003 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
8004 0ebf8283 2019-07-24 stsp resp = getchar();
8005 a61a4414 2019-08-07 stsp if (resp == '\n')
8006 a61a4414 2019-08-07 stsp resp = getchar();
8007 426ebf2e 2019-07-25 stsp if (resp == 'c') {
8008 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8009 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
8010 bfce7f83 2019-07-27 stsp repo);
8011 426ebf2e 2019-07-25 stsp if (err) {
8012 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8013 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8014 426ebf2e 2019-07-25 stsp break;
8015 bfce7f83 2019-07-27 stsp prev_err = err;
8016 426ebf2e 2019-07-25 stsp resp = ' ';
8017 426ebf2e 2019-07-25 stsp continue;
8018 426ebf2e 2019-07-25 stsp }
8019 bfce7f83 2019-07-27 stsp break;
8020 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
8021 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
8022 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
8023 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
8024 426ebf2e 2019-07-25 stsp if (err) {
8025 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
8026 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
8027 426ebf2e 2019-07-25 stsp break;
8028 bfce7f83 2019-07-27 stsp prev_err = err;
8029 426ebf2e 2019-07-25 stsp resp = ' ';
8030 426ebf2e 2019-07-25 stsp continue;
8031 426ebf2e 2019-07-25 stsp }
8032 bfce7f83 2019-07-27 stsp break;
8033 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
8034 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
8035 0ebf8283 2019-07-24 stsp break;
8036 426ebf2e 2019-07-25 stsp } else
8037 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
8038 0ebf8283 2019-07-24 stsp }
8039 0ebf8283 2019-07-24 stsp
8040 0ebf8283 2019-07-24 stsp return err;
8041 0ebf8283 2019-07-24 stsp }
8042 0ebf8283 2019-07-24 stsp
8043 0ebf8283 2019-07-24 stsp static const struct got_error *
8044 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
8045 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
8046 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
8047 0ebf8283 2019-07-24 stsp {
8048 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8049 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8050 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
8051 3e3a69f1 2019-07-25 stsp branch, repo);
8052 0ebf8283 2019-07-24 stsp }
8053 0ebf8283 2019-07-24 stsp
8054 0ebf8283 2019-07-24 stsp static const struct got_error *
8055 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
8056 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
8057 0ebf8283 2019-07-24 stsp {
8058 0ebf8283 2019-07-24 stsp const struct got_error *err;
8059 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
8060 0ebf8283 2019-07-24 stsp
8061 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
8062 0ebf8283 2019-07-24 stsp if (err)
8063 0ebf8283 2019-07-24 stsp goto done;
8064 0ebf8283 2019-07-24 stsp
8065 0ebf8283 2019-07-24 stsp if (new_id) {
8066 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
8067 0ebf8283 2019-07-24 stsp if (err)
8068 0ebf8283 2019-07-24 stsp goto done;
8069 0ebf8283 2019-07-24 stsp }
8070 0ebf8283 2019-07-24 stsp
8071 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
8072 0ebf8283 2019-07-24 stsp if (new_id_str)
8073 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
8074 0ebf8283 2019-07-24 stsp
8075 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
8076 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
8077 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
8078 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
8079 0ebf8283 2019-07-24 stsp goto done;
8080 0ebf8283 2019-07-24 stsp }
8081 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
8082 0ebf8283 2019-07-24 stsp } else {
8083 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
8084 0ebf8283 2019-07-24 stsp if (err)
8085 0ebf8283 2019-07-24 stsp goto done;
8086 0ebf8283 2019-07-24 stsp }
8087 0ebf8283 2019-07-24 stsp
8088 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
8089 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
8090 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
8091 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
8092 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
8093 0ebf8283 2019-07-24 stsp break;
8094 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
8095 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
8096 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
8097 0ebf8283 2019-07-24 stsp logmsg);
8098 0ebf8283 2019-07-24 stsp break;
8099 0ebf8283 2019-07-24 stsp default:
8100 0ebf8283 2019-07-24 stsp break;
8101 0ebf8283 2019-07-24 stsp }
8102 0ebf8283 2019-07-24 stsp done:
8103 0ebf8283 2019-07-24 stsp free(old_id_str);
8104 0ebf8283 2019-07-24 stsp free(new_id_str);
8105 0ebf8283 2019-07-24 stsp return err;
8106 0ebf8283 2019-07-24 stsp }
8107 0ebf8283 2019-07-24 stsp
8108 0ebf8283 2019-07-24 stsp static const struct got_error *
8109 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
8110 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
8111 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
8112 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
8113 0ebf8283 2019-07-24 stsp {
8114 0ebf8283 2019-07-24 stsp const struct got_error *err;
8115 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8116 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
8117 0ebf8283 2019-07-24 stsp
8118 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
8119 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
8120 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
8121 0ebf8283 2019-07-24 stsp if (err)
8122 0ebf8283 2019-07-24 stsp return err;
8123 0ebf8283 2019-07-24 stsp }
8124 0ebf8283 2019-07-24 stsp
8125 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
8126 0ebf8283 2019-07-24 stsp if (err)
8127 0ebf8283 2019-07-24 stsp return err;
8128 0ebf8283 2019-07-24 stsp
8129 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
8130 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
8131 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
8132 0ebf8283 2019-07-24 stsp if (err) {
8133 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
8134 0ebf8283 2019-07-24 stsp goto done;
8135 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
8136 0ebf8283 2019-07-24 stsp } else {
8137 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
8138 0ebf8283 2019-07-24 stsp free(new_commit_id);
8139 0ebf8283 2019-07-24 stsp }
8140 0ebf8283 2019-07-24 stsp done:
8141 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8142 0ebf8283 2019-07-24 stsp return err;
8143 0ebf8283 2019-07-24 stsp }
8144 0ebf8283 2019-07-24 stsp
8145 0ebf8283 2019-07-24 stsp static const struct got_error *
8146 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
8147 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
8148 0ebf8283 2019-07-24 stsp {
8149 0ebf8283 2019-07-24 stsp const struct got_error *error;
8150 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
8151 0ebf8283 2019-07-24 stsp
8152 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
8153 0ebf8283 2019-07-24 stsp repo);
8154 0ebf8283 2019-07-24 stsp if (error)
8155 0ebf8283 2019-07-24 stsp return error;
8156 0ebf8283 2019-07-24 stsp
8157 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
8158 0ebf8283 2019-07-24 stsp if (error)
8159 0ebf8283 2019-07-24 stsp return error;
8160 0ebf8283 2019-07-24 stsp
8161 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
8162 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8163 0ebf8283 2019-07-24 stsp return error;
8164 ab20a43a 2020-01-29 stsp }
8165 ab20a43a 2020-01-29 stsp
8166 ab20a43a 2020-01-29 stsp static const struct got_error *
8167 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
8168 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
8169 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
8170 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
8171 ab20a43a 2020-01-29 stsp {
8172 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
8173 ab20a43a 2020-01-29 stsp
8174 ab20a43a 2020-01-29 stsp switch (status) {
8175 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8176 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8177 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8178 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
8179 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8180 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8181 ab20a43a 2020-01-29 stsp default:
8182 ab20a43a 2020-01-29 stsp break;
8183 ab20a43a 2020-01-29 stsp }
8184 ab20a43a 2020-01-29 stsp
8185 ab20a43a 2020-01-29 stsp switch (staged_status) {
8186 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
8187 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
8188 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
8189 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
8190 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
8191 ab20a43a 2020-01-29 stsp default:
8192 ab20a43a 2020-01-29 stsp break;
8193 ab20a43a 2020-01-29 stsp }
8194 ab20a43a 2020-01-29 stsp
8195 ab20a43a 2020-01-29 stsp return NULL;
8196 0ebf8283 2019-07-24 stsp }
8197 0ebf8283 2019-07-24 stsp
8198 0ebf8283 2019-07-24 stsp static const struct got_error *
8199 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
8200 0ebf8283 2019-07-24 stsp {
8201 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
8202 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
8203 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
8204 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
8205 0ebf8283 2019-07-24 stsp char *cwd = NULL;
8206 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
8207 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
8208 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
8209 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
8210 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
8211 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
8212 9627c110 2020-04-18 stsp int ch, rebase_in_progress = 0;
8213 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8214 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
8215 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
8216 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
8217 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
8218 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
8219 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
8220 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
8221 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
8222 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
8223 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
8224 0ebf8283 2019-07-24 stsp
8225 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
8226 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
8227 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
8228 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8229 0ebf8283 2019-07-24 stsp
8230 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
8231 0ebf8283 2019-07-24 stsp switch (ch) {
8232 0ebf8283 2019-07-24 stsp case 'a':
8233 0ebf8283 2019-07-24 stsp abort_edit = 1;
8234 0ebf8283 2019-07-24 stsp break;
8235 0ebf8283 2019-07-24 stsp case 'c':
8236 0ebf8283 2019-07-24 stsp continue_edit = 1;
8237 0ebf8283 2019-07-24 stsp break;
8238 0ebf8283 2019-07-24 stsp case 'F':
8239 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
8240 0ebf8283 2019-07-24 stsp break;
8241 083957f4 2020-02-24 stsp case 'm':
8242 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
8243 083957f4 2020-02-24 stsp break;
8244 0ebf8283 2019-07-24 stsp default:
8245 0ebf8283 2019-07-24 stsp usage_histedit();
8246 0ebf8283 2019-07-24 stsp /* NOTREACHED */
8247 0ebf8283 2019-07-24 stsp }
8248 0ebf8283 2019-07-24 stsp }
8249 0ebf8283 2019-07-24 stsp
8250 0ebf8283 2019-07-24 stsp argc -= optind;
8251 0ebf8283 2019-07-24 stsp argv += optind;
8252 0ebf8283 2019-07-24 stsp
8253 0ebf8283 2019-07-24 stsp #ifndef PROFILE
8254 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8255 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
8256 0ebf8283 2019-07-24 stsp err(1, "pledge");
8257 0ebf8283 2019-07-24 stsp #endif
8258 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
8259 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
8260 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
8261 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
8262 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
8263 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
8264 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
8265 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
8266 0ebf8283 2019-07-24 stsp if (argc != 0)
8267 0ebf8283 2019-07-24 stsp usage_histedit();
8268 0ebf8283 2019-07-24 stsp
8269 0ebf8283 2019-07-24 stsp /*
8270 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
8271 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
8272 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
8273 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
8274 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
8275 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
8276 0ebf8283 2019-07-24 stsp */
8277 0ebf8283 2019-07-24 stsp
8278 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
8279 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
8280 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
8281 0ebf8283 2019-07-24 stsp goto done;
8282 0ebf8283 2019-07-24 stsp }
8283 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
8284 fa51e947 2020-03-27 stsp if (error) {
8285 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8286 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "histedit", cwd);
8287 0ebf8283 2019-07-24 stsp goto done;
8288 fa51e947 2020-03-27 stsp }
8289 0ebf8283 2019-07-24 stsp
8290 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8291 c9956ddf 2019-09-08 stsp NULL);
8292 0ebf8283 2019-07-24 stsp if (error != NULL)
8293 0ebf8283 2019-07-24 stsp goto done;
8294 0ebf8283 2019-07-24 stsp
8295 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
8296 0ebf8283 2019-07-24 stsp if (error)
8297 0ebf8283 2019-07-24 stsp goto done;
8298 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
8299 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
8300 0ebf8283 2019-07-24 stsp goto done;
8301 0ebf8283 2019-07-24 stsp }
8302 0ebf8283 2019-07-24 stsp
8303 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
8304 0ebf8283 2019-07-24 stsp if (error)
8305 0ebf8283 2019-07-24 stsp goto done;
8306 0ebf8283 2019-07-24 stsp
8307 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
8308 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
8309 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
8310 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
8311 083957f4 2020-02-24 stsp "before the -m option can be used");
8312 083957f4 2020-02-24 stsp goto done;
8313 083957f4 2020-02-24 stsp }
8314 083957f4 2020-02-24 stsp
8315 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
8316 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8317 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8318 3e3a69f1 2019-07-25 stsp worktree, repo);
8319 0ebf8283 2019-07-24 stsp if (error)
8320 0ebf8283 2019-07-24 stsp goto done;
8321 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
8322 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8323 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
8324 9627c110 2020-04-18 stsp branch, base_commit_id, update_progress, &upa);
8325 0ebf8283 2019-07-24 stsp if (error)
8326 0ebf8283 2019-07-24 stsp goto done;
8327 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
8328 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
8329 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8330 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
8331 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
8332 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8333 0ebf8283 2019-07-24 stsp goto done;
8334 0ebf8283 2019-07-24 stsp }
8335 0ebf8283 2019-07-24 stsp
8336 0ebf8283 2019-07-24 stsp if (continue_edit) {
8337 0ebf8283 2019-07-24 stsp char *path;
8338 0ebf8283 2019-07-24 stsp
8339 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
8340 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
8341 0ebf8283 2019-07-24 stsp goto done;
8342 0ebf8283 2019-07-24 stsp }
8343 0ebf8283 2019-07-24 stsp
8344 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
8345 0ebf8283 2019-07-24 stsp if (error)
8346 0ebf8283 2019-07-24 stsp goto done;
8347 0ebf8283 2019-07-24 stsp
8348 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
8349 0ebf8283 2019-07-24 stsp free(path);
8350 0ebf8283 2019-07-24 stsp if (error)
8351 0ebf8283 2019-07-24 stsp goto done;
8352 0ebf8283 2019-07-24 stsp
8353 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
8354 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
8355 3e3a69f1 2019-07-25 stsp worktree, repo);
8356 0ebf8283 2019-07-24 stsp if (error)
8357 0ebf8283 2019-07-24 stsp goto done;
8358 0ebf8283 2019-07-24 stsp
8359 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8360 0ebf8283 2019-07-24 stsp if (error)
8361 0ebf8283 2019-07-24 stsp goto done;
8362 0ebf8283 2019-07-24 stsp
8363 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8364 0ebf8283 2019-07-24 stsp head_commit_id);
8365 0ebf8283 2019-07-24 stsp if (error)
8366 0ebf8283 2019-07-24 stsp goto done;
8367 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8368 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8369 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8370 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8371 3aac7cf7 2019-07-25 stsp goto done;
8372 3aac7cf7 2019-07-25 stsp }
8373 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8374 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
8375 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8376 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8377 0ebf8283 2019-07-24 stsp commit = NULL;
8378 0ebf8283 2019-07-24 stsp if (error)
8379 0ebf8283 2019-07-24 stsp goto done;
8380 0ebf8283 2019-07-24 stsp } else {
8381 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
8382 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
8383 0ebf8283 2019-07-24 stsp goto done;
8384 0ebf8283 2019-07-24 stsp }
8385 0ebf8283 2019-07-24 stsp
8386 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
8387 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
8388 0ebf8283 2019-07-24 stsp if (error != NULL)
8389 0ebf8283 2019-07-24 stsp goto done;
8390 0ebf8283 2019-07-24 stsp
8391 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
8392 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
8393 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
8394 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
8395 c7d20a3f 2019-07-30 stsp goto done;
8396 c7d20a3f 2019-07-30 stsp }
8397 c7d20a3f 2019-07-30 stsp
8398 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
8399 bfce7f83 2019-07-27 stsp got_ref_close(branch);
8400 bfce7f83 2019-07-27 stsp branch = NULL;
8401 0ebf8283 2019-07-24 stsp if (error)
8402 0ebf8283 2019-07-24 stsp goto done;
8403 0ebf8283 2019-07-24 stsp
8404 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8405 0ebf8283 2019-07-24 stsp head_commit_id);
8406 0ebf8283 2019-07-24 stsp if (error)
8407 0ebf8283 2019-07-24 stsp goto done;
8408 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8409 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8410 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
8411 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8412 3aac7cf7 2019-07-25 stsp goto done;
8413 3aac7cf7 2019-07-25 stsp }
8414 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
8415 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
8416 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
8417 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
8418 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8419 0ebf8283 2019-07-24 stsp commit = NULL;
8420 0ebf8283 2019-07-24 stsp if (error)
8421 0ebf8283 2019-07-24 stsp goto done;
8422 0ebf8283 2019-07-24 stsp
8423 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
8424 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
8425 c5996fff 2020-01-29 stsp goto done;
8426 c5996fff 2020-01-29 stsp }
8427 c5996fff 2020-01-29 stsp
8428 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
8429 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
8430 bfce7f83 2019-07-27 stsp if (error)
8431 bfce7f83 2019-07-27 stsp goto done;
8432 bfce7f83 2019-07-27 stsp
8433 0ebf8283 2019-07-24 stsp if (edit_script_path) {
8434 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
8435 0ebf8283 2019-07-24 stsp edit_script_path, repo);
8436 6ba2bea2 2019-07-27 stsp if (error) {
8437 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8438 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8439 9627c110 2020-04-18 stsp update_progress, &upa);
8440 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8441 0ebf8283 2019-07-24 stsp goto done;
8442 6ba2bea2 2019-07-27 stsp }
8443 0ebf8283 2019-07-24 stsp } else {
8444 514f2ffe 2020-01-29 stsp const char *branch_name;
8445 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8446 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8447 514f2ffe 2020-01-29 stsp branch_name += 11;
8448 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8449 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8450 6ba2bea2 2019-07-27 stsp if (error) {
8451 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8452 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8453 9627c110 2020-04-18 stsp update_progress, &upa);
8454 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8455 0ebf8283 2019-07-24 stsp goto done;
8456 6ba2bea2 2019-07-27 stsp }
8457 0ebf8283 2019-07-24 stsp
8458 0ebf8283 2019-07-24 stsp }
8459 0ebf8283 2019-07-24 stsp
8460 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8461 0ebf8283 2019-07-24 stsp repo);
8462 6ba2bea2 2019-07-27 stsp if (error) {
8463 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8464 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8465 9627c110 2020-04-18 stsp update_progress, &upa);
8466 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8467 0ebf8283 2019-07-24 stsp goto done;
8468 6ba2bea2 2019-07-27 stsp }
8469 0ebf8283 2019-07-24 stsp
8470 0ebf8283 2019-07-24 stsp }
8471 0ebf8283 2019-07-24 stsp
8472 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8473 4ec14e60 2019-08-28 hiltjo if (error)
8474 0ebf8283 2019-07-24 stsp goto done;
8475 0ebf8283 2019-07-24 stsp
8476 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8477 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8478 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8479 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8480 0ebf8283 2019-07-24 stsp continue;
8481 0ebf8283 2019-07-24 stsp
8482 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8483 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8484 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8485 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8486 0ebf8283 2019-07-24 stsp repo);
8487 ab20a43a 2020-01-29 stsp if (error)
8488 ab20a43a 2020-01-29 stsp goto done;
8489 0ebf8283 2019-07-24 stsp } else {
8490 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8491 ab20a43a 2020-01-29 stsp int have_changes = 0;
8492 ab20a43a 2020-01-29 stsp
8493 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8494 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8495 ab20a43a 2020-01-29 stsp if (error)
8496 ab20a43a 2020-01-29 stsp goto done;
8497 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8498 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8499 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8500 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8501 ab20a43a 2020-01-29 stsp if (error) {
8502 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8503 ab20a43a 2020-01-29 stsp goto done;
8504 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8505 ab20a43a 2020-01-29 stsp goto done;
8506 ab20a43a 2020-01-29 stsp }
8507 ab20a43a 2020-01-29 stsp if (have_changes) {
8508 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8509 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8510 ab20a43a 2020-01-29 stsp if (error)
8511 ab20a43a 2020-01-29 stsp goto done;
8512 ab20a43a 2020-01-29 stsp } else {
8513 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8514 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8515 ab20a43a 2020-01-29 stsp if (error)
8516 ab20a43a 2020-01-29 stsp goto done;
8517 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8518 ab20a43a 2020-01-29 stsp hle, NULL);
8519 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8520 ab20a43a 2020-01-29 stsp commit = NULL;
8521 ab20a43a 2020-01-29 stsp if (error)
8522 ab20a43a 2020-01-29 stsp goto done;
8523 ab20a43a 2020-01-29 stsp }
8524 0ebf8283 2019-07-24 stsp }
8525 0ebf8283 2019-07-24 stsp continue;
8526 0ebf8283 2019-07-24 stsp }
8527 0ebf8283 2019-07-24 stsp
8528 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8529 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8530 0ebf8283 2019-07-24 stsp if (error)
8531 0ebf8283 2019-07-24 stsp goto done;
8532 0ebf8283 2019-07-24 stsp continue;
8533 0ebf8283 2019-07-24 stsp }
8534 0ebf8283 2019-07-24 stsp
8535 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8536 0ebf8283 2019-07-24 stsp hle->commit_id);
8537 0ebf8283 2019-07-24 stsp if (error)
8538 0ebf8283 2019-07-24 stsp goto done;
8539 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8540 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8541 0ebf8283 2019-07-24 stsp
8542 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8543 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8544 9627c110 2020-04-18 stsp update_progress, &upa, check_cancelled, NULL);
8545 0ebf8283 2019-07-24 stsp if (error)
8546 0ebf8283 2019-07-24 stsp goto done;
8547 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8548 0ebf8283 2019-07-24 stsp commit = NULL;
8549 0ebf8283 2019-07-24 stsp
8550 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8551 9627c110 2020-04-18 stsp if (upa.conflicts > 0)
8552 9627c110 2020-04-18 stsp rebase_status = GOT_STATUS_CONFLICT;
8553 9627c110 2020-04-18 stsp
8554 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8555 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8556 a0ea4fc0 2020-02-28 stsp repo);
8557 a0ea4fc0 2020-02-28 stsp if (error)
8558 a0ea4fc0 2020-02-28 stsp goto done;
8559 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8560 0ebf8283 2019-07-24 stsp break;
8561 0ebf8283 2019-07-24 stsp }
8562 0ebf8283 2019-07-24 stsp
8563 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8564 0ebf8283 2019-07-24 stsp char *id_str;
8565 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8566 0ebf8283 2019-07-24 stsp if (error)
8567 0ebf8283 2019-07-24 stsp goto done;
8568 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8569 0ebf8283 2019-07-24 stsp id_str);
8570 0ebf8283 2019-07-24 stsp free(id_str);
8571 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8572 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8573 3e3a69f1 2019-07-25 stsp fileindex);
8574 0ebf8283 2019-07-24 stsp goto done;
8575 0ebf8283 2019-07-24 stsp }
8576 0ebf8283 2019-07-24 stsp
8577 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8578 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8579 0ebf8283 2019-07-24 stsp if (error)
8580 0ebf8283 2019-07-24 stsp goto done;
8581 0ebf8283 2019-07-24 stsp continue;
8582 0ebf8283 2019-07-24 stsp }
8583 0ebf8283 2019-07-24 stsp
8584 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8585 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8586 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8587 0ebf8283 2019-07-24 stsp if (error)
8588 0ebf8283 2019-07-24 stsp goto done;
8589 0ebf8283 2019-07-24 stsp }
8590 0ebf8283 2019-07-24 stsp
8591 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8592 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8593 0ebf8283 2019-07-24 stsp if (error)
8594 0ebf8283 2019-07-24 stsp goto done;
8595 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8596 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8597 0ebf8283 2019-07-24 stsp } else
8598 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8599 3e3a69f1 2019-07-25 stsp branch, repo);
8600 0ebf8283 2019-07-24 stsp done:
8601 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8602 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8603 0ebf8283 2019-07-24 stsp free(head_commit_id);
8604 0ebf8283 2019-07-24 stsp free(base_commit_id);
8605 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8606 0ebf8283 2019-07-24 stsp if (commit)
8607 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8608 0ebf8283 2019-07-24 stsp if (branch)
8609 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8610 0ebf8283 2019-07-24 stsp if (tmp_branch)
8611 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8612 0ebf8283 2019-07-24 stsp if (worktree)
8613 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8614 2822a352 2019-10-15 stsp if (repo)
8615 2822a352 2019-10-15 stsp got_repo_close(repo);
8616 2822a352 2019-10-15 stsp return error;
8617 2822a352 2019-10-15 stsp }
8618 2822a352 2019-10-15 stsp
8619 2822a352 2019-10-15 stsp __dead static void
8620 2822a352 2019-10-15 stsp usage_integrate(void)
8621 2822a352 2019-10-15 stsp {
8622 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8623 2822a352 2019-10-15 stsp exit(1);
8624 2822a352 2019-10-15 stsp }
8625 2822a352 2019-10-15 stsp
8626 2822a352 2019-10-15 stsp static const struct got_error *
8627 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8628 2822a352 2019-10-15 stsp {
8629 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8630 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8631 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8632 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8633 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8634 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8635 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8636 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8637 9627c110 2020-04-18 stsp int ch;
8638 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8639 2822a352 2019-10-15 stsp
8640 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8641 2822a352 2019-10-15 stsp switch (ch) {
8642 2822a352 2019-10-15 stsp default:
8643 2822a352 2019-10-15 stsp usage_integrate();
8644 2822a352 2019-10-15 stsp /* NOTREACHED */
8645 2822a352 2019-10-15 stsp }
8646 2822a352 2019-10-15 stsp }
8647 2822a352 2019-10-15 stsp
8648 2822a352 2019-10-15 stsp argc -= optind;
8649 2822a352 2019-10-15 stsp argv += optind;
8650 2822a352 2019-10-15 stsp
8651 2822a352 2019-10-15 stsp if (argc != 1)
8652 2822a352 2019-10-15 stsp usage_integrate();
8653 2822a352 2019-10-15 stsp branch_arg = argv[0];
8654 2822a352 2019-10-15 stsp
8655 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8656 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8657 2822a352 2019-10-15 stsp err(1, "pledge");
8658 2822a352 2019-10-15 stsp
8659 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8660 2822a352 2019-10-15 stsp if (cwd == NULL) {
8661 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8662 2822a352 2019-10-15 stsp goto done;
8663 2822a352 2019-10-15 stsp }
8664 2822a352 2019-10-15 stsp
8665 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8666 fa51e947 2020-03-27 stsp if (error) {
8667 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8668 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "integrate",
8669 fa51e947 2020-03-27 stsp cwd);
8670 2822a352 2019-10-15 stsp goto done;
8671 fa51e947 2020-03-27 stsp }
8672 2822a352 2019-10-15 stsp
8673 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8674 2822a352 2019-10-15 stsp if (error)
8675 2822a352 2019-10-15 stsp goto done;
8676 2822a352 2019-10-15 stsp
8677 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8678 2822a352 2019-10-15 stsp NULL);
8679 2822a352 2019-10-15 stsp if (error != NULL)
8680 2822a352 2019-10-15 stsp goto done;
8681 2822a352 2019-10-15 stsp
8682 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8683 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8684 2822a352 2019-10-15 stsp if (error)
8685 2822a352 2019-10-15 stsp goto done;
8686 2822a352 2019-10-15 stsp
8687 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8688 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8689 2822a352 2019-10-15 stsp goto done;
8690 2822a352 2019-10-15 stsp }
8691 2822a352 2019-10-15 stsp
8692 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8693 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8694 2822a352 2019-10-15 stsp if (error)
8695 2822a352 2019-10-15 stsp goto done;
8696 2822a352 2019-10-15 stsp
8697 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8698 2822a352 2019-10-15 stsp if (refname == NULL) {
8699 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8700 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8701 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8702 2822a352 2019-10-15 stsp goto done;
8703 2822a352 2019-10-15 stsp }
8704 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8705 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8706 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8707 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8708 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8709 2822a352 2019-10-15 stsp goto done;
8710 2822a352 2019-10-15 stsp }
8711 2822a352 2019-10-15 stsp
8712 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8713 2822a352 2019-10-15 stsp if (error)
8714 2822a352 2019-10-15 stsp goto done;
8715 2822a352 2019-10-15 stsp
8716 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8717 2822a352 2019-10-15 stsp if (error)
8718 2822a352 2019-10-15 stsp goto done;
8719 2822a352 2019-10-15 stsp
8720 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8721 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8722 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8723 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8724 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8725 2822a352 2019-10-15 stsp goto done;
8726 2822a352 2019-10-15 stsp }
8727 2822a352 2019-10-15 stsp
8728 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8729 2822a352 2019-10-15 stsp if (error) {
8730 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8731 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8732 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8733 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8734 2822a352 2019-10-15 stsp goto done;
8735 2822a352 2019-10-15 stsp }
8736 2822a352 2019-10-15 stsp
8737 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8738 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8739 9627c110 2020-04-18 stsp branch_ref, base_branch_ref, update_progress, &upa,
8740 2822a352 2019-10-15 stsp check_cancelled, NULL);
8741 2822a352 2019-10-15 stsp if (error)
8742 2822a352 2019-10-15 stsp goto done;
8743 2822a352 2019-10-15 stsp
8744 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8745 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8746 2822a352 2019-10-15 stsp done:
8747 715dc77e 2019-08-03 stsp if (repo)
8748 715dc77e 2019-08-03 stsp got_repo_close(repo);
8749 2822a352 2019-10-15 stsp if (worktree)
8750 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8751 2822a352 2019-10-15 stsp free(cwd);
8752 2822a352 2019-10-15 stsp free(base_commit_id);
8753 2822a352 2019-10-15 stsp free(commit_id);
8754 2822a352 2019-10-15 stsp free(refname);
8755 2822a352 2019-10-15 stsp free(base_refname);
8756 715dc77e 2019-08-03 stsp return error;
8757 715dc77e 2019-08-03 stsp }
8758 715dc77e 2019-08-03 stsp
8759 715dc77e 2019-08-03 stsp __dead static void
8760 715dc77e 2019-08-03 stsp usage_stage(void)
8761 715dc77e 2019-08-03 stsp {
8762 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8763 f3055044 2019-08-07 stsp "[file-path ...]\n",
8764 715dc77e 2019-08-03 stsp getprogname());
8765 715dc77e 2019-08-03 stsp exit(1);
8766 715dc77e 2019-08-03 stsp }
8767 715dc77e 2019-08-03 stsp
8768 715dc77e 2019-08-03 stsp static const struct got_error *
8769 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8770 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8771 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8772 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8773 408b4ebc 2019-08-03 stsp {
8774 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8775 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8776 408b4ebc 2019-08-03 stsp
8777 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8778 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8779 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8780 408b4ebc 2019-08-03 stsp return NULL;
8781 408b4ebc 2019-08-03 stsp
8782 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8783 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8784 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8785 408b4ebc 2019-08-03 stsp else
8786 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8787 408b4ebc 2019-08-03 stsp if (err)
8788 408b4ebc 2019-08-03 stsp return err;
8789 408b4ebc 2019-08-03 stsp
8790 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8791 408b4ebc 2019-08-03 stsp free(id_str);
8792 dc424a06 2019-08-07 stsp return NULL;
8793 dc424a06 2019-08-07 stsp }
8794 2e1f37b0 2019-08-08 stsp
8795 dc424a06 2019-08-07 stsp static const struct got_error *
8796 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8797 715dc77e 2019-08-03 stsp {
8798 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8799 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8800 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8801 715dc77e 2019-08-03 stsp char *cwd = NULL;
8802 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8803 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8804 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8805 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8806 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8807 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8808 715dc77e 2019-08-03 stsp
8809 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8810 715dc77e 2019-08-03 stsp
8811 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8812 715dc77e 2019-08-03 stsp switch (ch) {
8813 408b4ebc 2019-08-03 stsp case 'l':
8814 408b4ebc 2019-08-03 stsp list_stage = 1;
8815 408b4ebc 2019-08-03 stsp break;
8816 dc424a06 2019-08-07 stsp case 'p':
8817 dc424a06 2019-08-07 stsp pflag = 1;
8818 dc424a06 2019-08-07 stsp break;
8819 dc424a06 2019-08-07 stsp case 'F':
8820 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8821 dc424a06 2019-08-07 stsp break;
8822 715dc77e 2019-08-03 stsp default:
8823 715dc77e 2019-08-03 stsp usage_stage();
8824 715dc77e 2019-08-03 stsp /* NOTREACHED */
8825 715dc77e 2019-08-03 stsp }
8826 715dc77e 2019-08-03 stsp }
8827 715dc77e 2019-08-03 stsp
8828 715dc77e 2019-08-03 stsp argc -= optind;
8829 715dc77e 2019-08-03 stsp argv += optind;
8830 715dc77e 2019-08-03 stsp
8831 715dc77e 2019-08-03 stsp #ifndef PROFILE
8832 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8833 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8834 715dc77e 2019-08-03 stsp err(1, "pledge");
8835 715dc77e 2019-08-03 stsp #endif
8836 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8837 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8838 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8839 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8840 715dc77e 2019-08-03 stsp
8841 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8842 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8843 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8844 715dc77e 2019-08-03 stsp goto done;
8845 715dc77e 2019-08-03 stsp }
8846 715dc77e 2019-08-03 stsp
8847 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8848 fa51e947 2020-03-27 stsp if (error) {
8849 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8850 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "stage", cwd);
8851 715dc77e 2019-08-03 stsp goto done;
8852 fa51e947 2020-03-27 stsp }
8853 715dc77e 2019-08-03 stsp
8854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8855 c9956ddf 2019-09-08 stsp NULL);
8856 715dc77e 2019-08-03 stsp if (error != NULL)
8857 715dc77e 2019-08-03 stsp goto done;
8858 715dc77e 2019-08-03 stsp
8859 dc424a06 2019-08-07 stsp if (patch_script_path) {
8860 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8861 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8862 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8863 dc424a06 2019-08-07 stsp patch_script_path);
8864 dc424a06 2019-08-07 stsp goto done;
8865 dc424a06 2019-08-07 stsp }
8866 dc424a06 2019-08-07 stsp }
8867 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8868 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8869 715dc77e 2019-08-03 stsp if (error)
8870 715dc77e 2019-08-03 stsp goto done;
8871 715dc77e 2019-08-03 stsp
8872 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8873 6d022e97 2019-08-04 stsp if (error)
8874 6d022e97 2019-08-04 stsp goto done;
8875 715dc77e 2019-08-03 stsp
8876 6d022e97 2019-08-04 stsp if (list_stage)
8877 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8878 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8879 2e1f37b0 2019-08-08 stsp else {
8880 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8881 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8882 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8883 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8884 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8885 2e1f37b0 2019-08-08 stsp }
8886 ad493afc 2019-08-03 stsp done:
8887 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8888 2e1f37b0 2019-08-08 stsp error == NULL)
8889 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8890 ad493afc 2019-08-03 stsp if (repo)
8891 ad493afc 2019-08-03 stsp got_repo_close(repo);
8892 ad493afc 2019-08-03 stsp if (worktree)
8893 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8894 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8895 ad493afc 2019-08-03 stsp free((char *)pe->path);
8896 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8897 ad493afc 2019-08-03 stsp free(cwd);
8898 ad493afc 2019-08-03 stsp return error;
8899 ad493afc 2019-08-03 stsp }
8900 ad493afc 2019-08-03 stsp
8901 ad493afc 2019-08-03 stsp __dead static void
8902 ad493afc 2019-08-03 stsp usage_unstage(void)
8903 ad493afc 2019-08-03 stsp {
8904 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8905 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8906 ad493afc 2019-08-03 stsp getprogname());
8907 ad493afc 2019-08-03 stsp exit(1);
8908 ad493afc 2019-08-03 stsp }
8909 ad493afc 2019-08-03 stsp
8910 ad493afc 2019-08-03 stsp
8911 ad493afc 2019-08-03 stsp static const struct got_error *
8912 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8913 ad493afc 2019-08-03 stsp {
8914 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8915 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8916 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8917 ad493afc 2019-08-03 stsp char *cwd = NULL;
8918 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8919 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8920 9627c110 2020-04-18 stsp int ch, pflag = 0;
8921 9627c110 2020-04-18 stsp struct got_update_progress_arg upa;
8922 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8923 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8924 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8925 ad493afc 2019-08-03 stsp
8926 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8927 ad493afc 2019-08-03 stsp
8928 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8929 ad493afc 2019-08-03 stsp switch (ch) {
8930 2e1f37b0 2019-08-08 stsp case 'p':
8931 2e1f37b0 2019-08-08 stsp pflag = 1;
8932 2e1f37b0 2019-08-08 stsp break;
8933 2e1f37b0 2019-08-08 stsp case 'F':
8934 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8935 2e1f37b0 2019-08-08 stsp break;
8936 ad493afc 2019-08-03 stsp default:
8937 ad493afc 2019-08-03 stsp usage_unstage();
8938 ad493afc 2019-08-03 stsp /* NOTREACHED */
8939 ad493afc 2019-08-03 stsp }
8940 ad493afc 2019-08-03 stsp }
8941 ad493afc 2019-08-03 stsp
8942 ad493afc 2019-08-03 stsp argc -= optind;
8943 ad493afc 2019-08-03 stsp argv += optind;
8944 ad493afc 2019-08-03 stsp
8945 ad493afc 2019-08-03 stsp #ifndef PROFILE
8946 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8947 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8948 ad493afc 2019-08-03 stsp err(1, "pledge");
8949 ad493afc 2019-08-03 stsp #endif
8950 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8951 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8952 2e1f37b0 2019-08-08 stsp
8953 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8954 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8955 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8956 ad493afc 2019-08-03 stsp goto done;
8957 ad493afc 2019-08-03 stsp }
8958 ad493afc 2019-08-03 stsp
8959 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8960 fa51e947 2020-03-27 stsp if (error) {
8961 fa51e947 2020-03-27 stsp if (error->code == GOT_ERR_NOT_WORKTREE)
8962 fa51e947 2020-03-27 stsp error = wrap_not_worktree_error(error, "unstage", cwd);
8963 ad493afc 2019-08-03 stsp goto done;
8964 fa51e947 2020-03-27 stsp }
8965 ad493afc 2019-08-03 stsp
8966 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8967 c9956ddf 2019-09-08 stsp NULL);
8968 ad493afc 2019-08-03 stsp if (error != NULL)
8969 ad493afc 2019-08-03 stsp goto done;
8970 ad493afc 2019-08-03 stsp
8971 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8972 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8973 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8974 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8975 2e1f37b0 2019-08-08 stsp patch_script_path);
8976 2e1f37b0 2019-08-08 stsp goto done;
8977 2e1f37b0 2019-08-08 stsp }
8978 2e1f37b0 2019-08-08 stsp }
8979 2e1f37b0 2019-08-08 stsp
8980 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8981 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8982 ad493afc 2019-08-03 stsp if (error)
8983 ad493afc 2019-08-03 stsp goto done;
8984 ad493afc 2019-08-03 stsp
8985 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8986 ad493afc 2019-08-03 stsp if (error)
8987 ad493afc 2019-08-03 stsp goto done;
8988 ad493afc 2019-08-03 stsp
8989 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8990 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8991 9627c110 2020-04-18 stsp memset(&upa, 0, sizeof(upa));
8992 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8993 9627c110 2020-04-18 stsp &upa, pflag ? choose_patch : NULL, &cpa, repo);
8994 9627c110 2020-04-18 stsp if (!error)
8995 9627c110 2020-04-18 stsp print_update_progress_stats(&upa);
8996 715dc77e 2019-08-03 stsp done:
8997 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8998 2e1f37b0 2019-08-08 stsp error == NULL)
8999 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
9000 0ebf8283 2019-07-24 stsp if (repo)
9001 0ebf8283 2019-07-24 stsp got_repo_close(repo);
9002 715dc77e 2019-08-03 stsp if (worktree)
9003 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
9004 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
9005 715dc77e 2019-08-03 stsp free((char *)pe->path);
9006 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
9007 715dc77e 2019-08-03 stsp free(cwd);
9008 01073a5d 2019-08-22 stsp return error;
9009 01073a5d 2019-08-22 stsp }
9010 01073a5d 2019-08-22 stsp
9011 01073a5d 2019-08-22 stsp __dead static void
9012 01073a5d 2019-08-22 stsp usage_cat(void)
9013 01073a5d 2019-08-22 stsp {
9014 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
9015 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
9016 01073a5d 2019-08-22 stsp exit(1);
9017 01073a5d 2019-08-22 stsp }
9018 01073a5d 2019-08-22 stsp
9019 01073a5d 2019-08-22 stsp static const struct got_error *
9020 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9021 01073a5d 2019-08-22 stsp {
9022 01073a5d 2019-08-22 stsp const struct got_error *err;
9023 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
9024 01073a5d 2019-08-22 stsp
9025 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
9026 01073a5d 2019-08-22 stsp if (err)
9027 01073a5d 2019-08-22 stsp return err;
9028 01073a5d 2019-08-22 stsp
9029 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
9030 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
9031 01073a5d 2019-08-22 stsp return err;
9032 01073a5d 2019-08-22 stsp }
9033 01073a5d 2019-08-22 stsp
9034 01073a5d 2019-08-22 stsp static const struct got_error *
9035 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9036 01073a5d 2019-08-22 stsp {
9037 01073a5d 2019-08-22 stsp const struct got_error *err;
9038 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
9039 56e0773d 2019-11-28 stsp int nentries, i;
9040 01073a5d 2019-08-22 stsp
9041 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
9042 01073a5d 2019-08-22 stsp if (err)
9043 01073a5d 2019-08-22 stsp return err;
9044 01073a5d 2019-08-22 stsp
9045 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
9046 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
9047 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
9048 01073a5d 2019-08-22 stsp char *id_str;
9049 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
9050 01073a5d 2019-08-22 stsp break;
9051 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
9052 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
9053 01073a5d 2019-08-22 stsp if (err)
9054 01073a5d 2019-08-22 stsp break;
9055 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
9056 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
9057 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
9058 01073a5d 2019-08-22 stsp free(id_str);
9059 01073a5d 2019-08-22 stsp }
9060 01073a5d 2019-08-22 stsp
9061 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
9062 01073a5d 2019-08-22 stsp return err;
9063 01073a5d 2019-08-22 stsp }
9064 01073a5d 2019-08-22 stsp
9065 01073a5d 2019-08-22 stsp static const struct got_error *
9066 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9067 01073a5d 2019-08-22 stsp {
9068 01073a5d 2019-08-22 stsp const struct got_error *err;
9069 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
9070 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
9071 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
9072 01073a5d 2019-08-22 stsp char *id_str = NULL;
9073 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
9074 01073a5d 2019-08-22 stsp
9075 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
9076 01073a5d 2019-08-22 stsp if (err)
9077 01073a5d 2019-08-22 stsp return err;
9078 01073a5d 2019-08-22 stsp
9079 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
9080 01073a5d 2019-08-22 stsp if (err)
9081 01073a5d 2019-08-22 stsp goto done;
9082 01073a5d 2019-08-22 stsp
9083 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
9084 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
9085 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
9086 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
9087 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
9088 01073a5d 2019-08-22 stsp char *pid_str;
9089 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
9090 01073a5d 2019-08-22 stsp if (err)
9091 01073a5d 2019-08-22 stsp goto done;
9092 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
9093 01073a5d 2019-08-22 stsp free(pid_str);
9094 01073a5d 2019-08-22 stsp }
9095 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
9096 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9097 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
9098 01073a5d 2019-08-22 stsp
9099 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
9100 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
9101 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
9102 01073a5d 2019-08-22 stsp
9103 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
9104 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
9105 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
9106 01073a5d 2019-08-22 stsp done:
9107 01073a5d 2019-08-22 stsp free(id_str);
9108 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
9109 01073a5d 2019-08-22 stsp return err;
9110 01073a5d 2019-08-22 stsp }
9111 01073a5d 2019-08-22 stsp
9112 01073a5d 2019-08-22 stsp static const struct got_error *
9113 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
9114 01073a5d 2019-08-22 stsp {
9115 01073a5d 2019-08-22 stsp const struct got_error *err;
9116 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
9117 01073a5d 2019-08-22 stsp char *id_str = NULL;
9118 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
9119 01073a5d 2019-08-22 stsp
9120 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
9121 01073a5d 2019-08-22 stsp if (err)
9122 01073a5d 2019-08-22 stsp return err;
9123 01073a5d 2019-08-22 stsp
9124 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
9125 01073a5d 2019-08-22 stsp if (err)
9126 01073a5d 2019-08-22 stsp goto done;
9127 01073a5d 2019-08-22 stsp
9128 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
9129 e15d5241 2019-08-22 stsp
9130 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
9131 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9132 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9133 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
9134 01073a5d 2019-08-22 stsp break;
9135 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9136 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9137 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
9138 01073a5d 2019-08-22 stsp break;
9139 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9140 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9141 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
9142 01073a5d 2019-08-22 stsp break;
9143 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9144 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
9145 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
9146 01073a5d 2019-08-22 stsp break;
9147 01073a5d 2019-08-22 stsp default:
9148 01073a5d 2019-08-22 stsp break;
9149 01073a5d 2019-08-22 stsp }
9150 01073a5d 2019-08-22 stsp
9151 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
9152 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
9153 e15d5241 2019-08-22 stsp
9154 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
9155 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
9156 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
9157 01073a5d 2019-08-22 stsp
9158 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
9159 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
9160 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
9161 01073a5d 2019-08-22 stsp done:
9162 01073a5d 2019-08-22 stsp free(id_str);
9163 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
9164 01073a5d 2019-08-22 stsp return err;
9165 01073a5d 2019-08-22 stsp }
9166 01073a5d 2019-08-22 stsp
9167 01073a5d 2019-08-22 stsp static const struct got_error *
9168 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
9169 01073a5d 2019-08-22 stsp {
9170 01073a5d 2019-08-22 stsp const struct got_error *error;
9171 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
9172 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
9173 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
9174 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
9175 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
9176 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
9177 01073a5d 2019-08-22 stsp
9178 01073a5d 2019-08-22 stsp #ifndef PROFILE
9179 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
9180 01073a5d 2019-08-22 stsp NULL) == -1)
9181 01073a5d 2019-08-22 stsp err(1, "pledge");
9182 01073a5d 2019-08-22 stsp #endif
9183 01073a5d 2019-08-22 stsp
9184 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
9185 01073a5d 2019-08-22 stsp switch (ch) {
9186 896e9b6f 2019-08-26 stsp case 'c':
9187 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
9188 896e9b6f 2019-08-26 stsp break;
9189 01073a5d 2019-08-22 stsp case 'r':
9190 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
9191 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9192 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
9193 9ba1d308 2019-10-21 stsp optarg);
9194 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
9195 01073a5d 2019-08-22 stsp break;
9196 896e9b6f 2019-08-26 stsp case 'P':
9197 896e9b6f 2019-08-26 stsp force_path = 1;
9198 896e9b6f 2019-08-26 stsp break;
9199 01073a5d 2019-08-22 stsp default:
9200 01073a5d 2019-08-22 stsp usage_cat();
9201 01073a5d 2019-08-22 stsp /* NOTREACHED */
9202 01073a5d 2019-08-22 stsp }
9203 01073a5d 2019-08-22 stsp }
9204 01073a5d 2019-08-22 stsp
9205 01073a5d 2019-08-22 stsp argc -= optind;
9206 01073a5d 2019-08-22 stsp argv += optind;
9207 01073a5d 2019-08-22 stsp
9208 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
9209 01073a5d 2019-08-22 stsp if (cwd == NULL) {
9210 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
9211 01073a5d 2019-08-22 stsp goto done;
9212 01073a5d 2019-08-22 stsp }
9213 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
9214 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
9215 01073a5d 2019-08-22 stsp goto done;
9216 01073a5d 2019-08-22 stsp if (worktree) {
9217 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9218 01073a5d 2019-08-22 stsp repo_path = strdup(
9219 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
9220 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9221 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
9222 01073a5d 2019-08-22 stsp goto done;
9223 01073a5d 2019-08-22 stsp }
9224 01073a5d 2019-08-22 stsp }
9225 01073a5d 2019-08-22 stsp }
9226 01073a5d 2019-08-22 stsp
9227 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
9228 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
9229 01073a5d 2019-08-22 stsp if (repo_path == NULL)
9230 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
9231 01073a5d 2019-08-22 stsp }
9232 01073a5d 2019-08-22 stsp
9233 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
9234 01073a5d 2019-08-22 stsp free(repo_path);
9235 01073a5d 2019-08-22 stsp if (error != NULL)
9236 01073a5d 2019-08-22 stsp goto done;
9237 01073a5d 2019-08-22 stsp
9238 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
9239 896e9b6f 2019-08-26 stsp if (error)
9240 896e9b6f 2019-08-26 stsp goto done;
9241 896e9b6f 2019-08-26 stsp
9242 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
9243 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
9244 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
9245 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
9246 01073a5d 2019-08-22 stsp if (error)
9247 01073a5d 2019-08-22 stsp goto done;
9248 01073a5d 2019-08-22 stsp
9249 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
9250 896e9b6f 2019-08-26 stsp if (force_path) {
9251 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
9252 896e9b6f 2019-08-26 stsp argv[i]);
9253 896e9b6f 2019-08-26 stsp if (error)
9254 896e9b6f 2019-08-26 stsp break;
9255 896e9b6f 2019-08-26 stsp } else {
9256 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
9257 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
9258 896e9b6f 2019-08-26 stsp if (error) {
9259 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
9260 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
9261 896e9b6f 2019-08-26 stsp break;
9262 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
9263 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
9264 896e9b6f 2019-08-26 stsp if (error)
9265 896e9b6f 2019-08-26 stsp break;
9266 896e9b6f 2019-08-26 stsp }
9267 896e9b6f 2019-08-26 stsp }
9268 01073a5d 2019-08-22 stsp
9269 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
9270 01073a5d 2019-08-22 stsp if (error)
9271 01073a5d 2019-08-22 stsp break;
9272 01073a5d 2019-08-22 stsp
9273 01073a5d 2019-08-22 stsp switch (obj_type) {
9274 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
9275 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
9276 01073a5d 2019-08-22 stsp break;
9277 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
9278 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
9279 01073a5d 2019-08-22 stsp break;
9280 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
9281 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
9282 01073a5d 2019-08-22 stsp break;
9283 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
9284 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
9285 01073a5d 2019-08-22 stsp break;
9286 01073a5d 2019-08-22 stsp default:
9287 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
9288 01073a5d 2019-08-22 stsp break;
9289 01073a5d 2019-08-22 stsp }
9290 01073a5d 2019-08-22 stsp if (error)
9291 01073a5d 2019-08-22 stsp break;
9292 01073a5d 2019-08-22 stsp free(label);
9293 01073a5d 2019-08-22 stsp label = NULL;
9294 01073a5d 2019-08-22 stsp free(id);
9295 01073a5d 2019-08-22 stsp id = NULL;
9296 01073a5d 2019-08-22 stsp }
9297 01073a5d 2019-08-22 stsp done:
9298 01073a5d 2019-08-22 stsp free(label);
9299 01073a5d 2019-08-22 stsp free(id);
9300 896e9b6f 2019-08-26 stsp free(commit_id);
9301 01073a5d 2019-08-22 stsp if (worktree)
9302 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
9303 01073a5d 2019-08-22 stsp if (repo) {
9304 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
9305 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
9306 01073a5d 2019-08-22 stsp if (error == NULL)
9307 01073a5d 2019-08-22 stsp error = repo_error;
9308 01073a5d 2019-08-22 stsp }
9309 0ebf8283 2019-07-24 stsp return error;
9310 0ebf8283 2019-07-24 stsp }