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 4ba14133 2020-03-20 stsp "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 4ba14133 2020-03-20 stsp create_head_ref(struct got_reference *target_ref, struct got_repository *repo)
889 4ba14133 2020-03-20 stsp {
890 4ba14133 2020-03-20 stsp const struct got_error *err;
891 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
892 4ba14133 2020-03-20 stsp
893 4ba14133 2020-03-20 stsp err = got_ref_alloc_symref(&head_symref, GOT_REF_HEAD, target_ref);
894 4ba14133 2020-03-20 stsp if (err)
895 4ba14133 2020-03-20 stsp return err;
896 4ba14133 2020-03-20 stsp
897 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
898 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
899 4ba14133 2020-03-20 stsp return err;
900 4ba14133 2020-03-20 stsp }
901 4ba14133 2020-03-20 stsp
902 4ba14133 2020-03-20 stsp static const struct got_error *
903 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
904 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
905 41b0de12 2020-03-21 stsp {
906 41b0de12 2020-03-21 stsp const struct got_error *err;
907 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
908 41b0de12 2020-03-21 stsp
909 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
910 41b0de12 2020-03-21 stsp const char *refname = pe->path;
911 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
912 41b0de12 2020-03-21 stsp
913 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
914 41b0de12 2020-03-21 stsp }
915 41b0de12 2020-03-21 stsp
916 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
917 41b0de12 2020-03-21 stsp const char *refname = pe->path;
918 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
919 41b0de12 2020-03-21 stsp char *id_str;
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
922 41b0de12 2020-03-21 stsp if (err)
923 41b0de12 2020-03-21 stsp return err;
924 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
925 41b0de12 2020-03-21 stsp free(id_str);
926 41b0de12 2020-03-21 stsp }
927 41b0de12 2020-03-21 stsp
928 41b0de12 2020-03-21 stsp return NULL;
929 41b0de12 2020-03-21 stsp }
930 41b0de12 2020-03-21 stsp
931 41b0de12 2020-03-21 stsp static const struct got_error *
932 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
933 93658fb9 2020-03-18 stsp {
934 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
935 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
936 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
937 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
938 bb64b798 2020-03-18 stsp const char *repo_path;
939 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
940 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
941 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
942 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
943 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
944 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
945 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
946 b46f3e71 2020-03-18 stsp char *git_url = NULL;
947 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
948 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
949 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
950 b46f3e71 2020-03-18 stsp ssize_t n;
951 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
952 41b0de12 2020-03-21 stsp int list_refs_only = 0;
953 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
954 93658fb9 2020-03-18 stsp
955 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
956 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
957 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
958 d9b4d0c0 2020-03-18 stsp
959 41b0de12 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvq")) != -1) {
960 93658fb9 2020-03-18 stsp switch (ch) {
961 659e7fbd 2020-03-20 stsp case 'a':
962 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
963 4ba14133 2020-03-20 stsp break;
964 4ba14133 2020-03-20 stsp case 'b':
965 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
966 4ba14133 2020-03-20 stsp optarg, NULL);
967 4ba14133 2020-03-20 stsp if (error)
968 4ba14133 2020-03-20 stsp return error;
969 659e7fbd 2020-03-20 stsp break;
970 41b0de12 2020-03-21 stsp case 'l':
971 41b0de12 2020-03-21 stsp list_refs_only = 1;
972 41b0de12 2020-03-21 stsp break;
973 469dd726 2020-03-20 stsp case 'm':
974 469dd726 2020-03-20 stsp mirror_references = 1;
975 469dd726 2020-03-20 stsp break;
976 68999b92 2020-03-18 stsp case 'v':
977 68999b92 2020-03-18 stsp if (verbosity < 0)
978 68999b92 2020-03-18 stsp verbosity = 0;
979 68999b92 2020-03-18 stsp else if (verbosity < 3)
980 68999b92 2020-03-18 stsp verbosity++;
981 68999b92 2020-03-18 stsp break;
982 68999b92 2020-03-18 stsp case 'q':
983 68999b92 2020-03-18 stsp verbosity = -1;
984 68999b92 2020-03-18 stsp break;
985 93658fb9 2020-03-18 stsp default:
986 93658fb9 2020-03-18 stsp usage_clone();
987 93658fb9 2020-03-18 stsp break;
988 93658fb9 2020-03-18 stsp }
989 93658fb9 2020-03-18 stsp }
990 93658fb9 2020-03-18 stsp argc -= optind;
991 93658fb9 2020-03-18 stsp argv += optind;
992 39c64a6a 2020-03-18 stsp
993 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
994 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
995 41b0de12 2020-03-21 stsp if (list_refs_only) {
996 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
997 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
998 41b0de12 2020-03-21 stsp if (fetch_all_branches)
999 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1000 41b0de12 2020-03-21 stsp if (mirror_references)
1001 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1002 41b0de12 2020-03-21 stsp if (verbosity == -1)
1003 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1004 41b0de12 2020-03-21 stsp }
1005 4ba14133 2020-03-20 stsp
1006 93658fb9 2020-03-18 stsp uri = argv[0];
1007 9df6f38b 2020-03-18 stsp
1008 9df6f38b 2020-03-18 stsp if (argc == 1)
1009 93658fb9 2020-03-18 stsp dirname = NULL;
1010 9df6f38b 2020-03-18 stsp else if (argc == 2)
1011 93658fb9 2020-03-18 stsp dirname = argv[1];
1012 93658fb9 2020-03-18 stsp else
1013 93658fb9 2020-03-18 stsp usage_clone();
1014 09838ffc 2020-03-18 stsp
1015 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1016 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1017 39c64a6a 2020-03-18 stsp if (error)
1018 09f63084 2020-03-20 stsp goto done;
1019 09f63084 2020-03-20 stsp
1020 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1021 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1022 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1023 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1024 09838ffc 2020-03-18 stsp goto done;
1025 09f63084 2020-03-20 stsp }
1026 09838ffc 2020-03-18 stsp
1027 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1028 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1029 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1030 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1031 39c64a6a 2020-03-18 stsp err(1, "pledge");
1032 b46f3e71 2020-03-18 stsp #endif
1033 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1034 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1035 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1036 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1037 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1038 39c64a6a 2020-03-18 stsp err(1, "pledge");
1039 b46f3e71 2020-03-18 stsp #endif
1040 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1041 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1042 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1043 39c64a6a 2020-03-18 stsp goto done;
1044 39c64a6a 2020-03-18 stsp } else {
1045 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1046 39c64a6a 2020-03-18 stsp goto done;
1047 39c64a6a 2020-03-18 stsp }
1048 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1049 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1050 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1051 bb64b798 2020-03-18 stsp goto done;
1052 bb64b798 2020-03-18 stsp }
1053 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1054 bb64b798 2020-03-18 stsp } else
1055 bb64b798 2020-03-18 stsp repo_path = dirname;
1056 bb64b798 2020-03-18 stsp
1057 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1058 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1059 41b0de12 2020-03-21 stsp if (error)
1060 41b0de12 2020-03-21 stsp goto done;
1061 bb64b798 2020-03-18 stsp
1062 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1063 41b0de12 2020-03-21 stsp if (error)
1064 41b0de12 2020-03-21 stsp goto done;
1065 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1066 41b0de12 2020-03-21 stsp if (error)
1067 41b0de12 2020-03-21 stsp goto done;
1068 41b0de12 2020-03-21 stsp }
1069 bb64b798 2020-03-18 stsp
1070 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1071 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1072 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1073 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1074 ee448f5f 2020-03-18 stsp goto done;
1075 ee448f5f 2020-03-18 stsp }
1076 ee448f5f 2020-03-18 stsp }
1077 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1078 ee448f5f 2020-03-18 stsp if (error)
1079 ee448f5f 2020-03-18 stsp goto done;
1080 ee448f5f 2020-03-18 stsp
1081 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1082 9c52365f 2020-03-21 stsp server_path, verbosity);
1083 39c64a6a 2020-03-18 stsp if (error)
1084 bb64b798 2020-03-18 stsp goto done;
1085 294dfefd 2020-03-18 stsp
1086 68999b92 2020-03-18 stsp if (verbosity >= 0)
1087 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1088 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1089 bb64b798 2020-03-18 stsp
1090 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1091 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1092 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1093 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1094 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1095 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1096 41b0de12 2020-03-21 stsp fetch_all_branches, &wanted_branches, list_refs_only,
1097 41b0de12 2020-03-21 stsp fetchfd, repo, fetch_progress, &fpa);
1098 39c64a6a 2020-03-18 stsp if (error)
1099 d9b4d0c0 2020-03-18 stsp goto done;
1100 d9b4d0c0 2020-03-18 stsp
1101 41b0de12 2020-03-21 stsp if (list_refs_only) {
1102 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1103 41b0de12 2020-03-21 stsp goto done;
1104 41b0de12 2020-03-21 stsp }
1105 41b0de12 2020-03-21 stsp
1106 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1107 39c64a6a 2020-03-18 stsp if (error)
1108 d9b4d0c0 2020-03-18 stsp goto done;
1109 68999b92 2020-03-18 stsp if (verbosity >= 0)
1110 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1111 d9b4d0c0 2020-03-18 stsp free(id_str);
1112 d9b4d0c0 2020-03-18 stsp
1113 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1114 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1115 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1116 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1117 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1118 7ebc0570 2020-03-18 stsp char *remote_refname;
1119 668a20f6 2020-03-18 stsp
1120 39c64a6a 2020-03-18 stsp error = got_ref_alloc(&ref, refname, id);
1121 39c64a6a 2020-03-18 stsp if (error)
1122 d9b4d0c0 2020-03-18 stsp goto done;
1123 7ebc0570 2020-03-18 stsp error = got_ref_write(ref, repo);
1124 7ebc0570 2020-03-18 stsp got_ref_close(ref);
1125 7ebc0570 2020-03-18 stsp if (error)
1126 7ebc0570 2020-03-18 stsp goto done;
1127 d9b4d0c0 2020-03-18 stsp
1128 469dd726 2020-03-20 stsp if (mirror_references)
1129 469dd726 2020-03-20 stsp continue;
1130 469dd726 2020-03-20 stsp
1131 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1132 7ebc0570 2020-03-18 stsp continue;
1133 7ebc0570 2020-03-18 stsp
1134 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1135 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1136 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1137 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1138 7ebc0570 2020-03-18 stsp goto done;
1139 7ebc0570 2020-03-18 stsp }
1140 7ebc0570 2020-03-18 stsp error = got_ref_alloc(&ref, remote_refname, id);
1141 39c64a6a 2020-03-18 stsp if (error)
1142 d9b4d0c0 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp error = got_ref_write(ref, repo);
1144 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1145 39c64a6a 2020-03-18 stsp if (error)
1146 d9b4d0c0 2020-03-18 stsp goto done;
1147 d9b4d0c0 2020-03-18 stsp }
1148 d9b4d0c0 2020-03-18 stsp
1149 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1150 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1151 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1152 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1153 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1154 d9b4d0c0 2020-03-18 stsp
1155 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1156 d9b4d0c0 2020-03-18 stsp continue;
1157 d9b4d0c0 2020-03-18 stsp
1158 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1159 39c64a6a 2020-03-18 stsp if (error) {
1160 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1161 55330abe 2020-03-20 stsp error = NULL;
1162 d9b4d0c0 2020-03-18 stsp continue;
1163 55330abe 2020-03-20 stsp }
1164 d9b4d0c0 2020-03-18 stsp goto done;
1165 d9b4d0c0 2020-03-18 stsp }
1166 d9b4d0c0 2020-03-18 stsp
1167 4ba14133 2020-03-20 stsp if (verbosity >= 0)
1168 4ba14133 2020-03-20 stsp printf("Setting %s to %s\n", refname, target);
1169 4ba14133 2020-03-20 stsp error = create_head_ref(target_ref, repo);
1170 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1171 39c64a6a 2020-03-18 stsp if (error)
1172 d9b4d0c0 2020-03-18 stsp goto done;
1173 4ba14133 2020-03-20 stsp }
1174 4ba14133 2020-03-20 stsp if (pe == NULL) {
1175 4ba14133 2020-03-20 stsp /*
1176 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1177 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1178 4ba14133 2020-03-20 stsp * which could be fetched instead.
1179 4ba14133 2020-03-20 stsp */
1180 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1181 4ba14133 2020-03-20 stsp const char *target = pe->path;
1182 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1183 d9b4d0c0 2020-03-18 stsp
1184 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1185 4ba14133 2020-03-20 stsp if (error) {
1186 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1187 4ba14133 2020-03-20 stsp error = NULL;
1188 4ba14133 2020-03-20 stsp continue;
1189 4ba14133 2020-03-20 stsp }
1190 4ba14133 2020-03-20 stsp goto done;
1191 4ba14133 2020-03-20 stsp }
1192 4ba14133 2020-03-20 stsp
1193 4ba14133 2020-03-20 stsp if (verbosity >= 0)
1194 4ba14133 2020-03-20 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1195 4ba14133 2020-03-20 stsp got_ref_get_name(target_ref));
1196 4ba14133 2020-03-20 stsp error = create_head_ref(target_ref, repo);
1197 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1198 4ba14133 2020-03-20 stsp if (error)
1199 4ba14133 2020-03-20 stsp goto done;
1200 4ba14133 2020-03-20 stsp break;
1201 4ba14133 2020-03-20 stsp }
1202 659e7fbd 2020-03-20 stsp }
1203 659e7fbd 2020-03-20 stsp
1204 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1205 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1206 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1207 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1208 659e7fbd 2020-03-20 stsp goto done;
1209 659e7fbd 2020-03-20 stsp }
1210 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1211 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1212 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1213 659e7fbd 2020-03-20 stsp goto done;
1214 b46f3e71 2020-03-18 stsp }
1215 659e7fbd 2020-03-20 stsp if (mirror_references) {
1216 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1217 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1218 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1219 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1220 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1221 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1222 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1223 659e7fbd 2020-03-20 stsp goto done;
1224 659e7fbd 2020-03-20 stsp }
1225 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1226 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1227 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1228 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1229 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1230 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1231 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1232 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1233 659e7fbd 2020-03-20 stsp goto done;
1234 659e7fbd 2020-03-20 stsp }
1235 659e7fbd 2020-03-20 stsp } else {
1236 659e7fbd 2020-03-20 stsp const char *branchname;
1237 d715f13e 2020-03-19 stsp
1238 659e7fbd 2020-03-20 stsp /*
1239 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1240 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1241 659e7fbd 2020-03-20 stsp */
1242 659e7fbd 2020-03-20 stsp if (head_symref) {
1243 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1244 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1245 659e7fbd 2020-03-20 stsp branchname += 11;
1246 659e7fbd 2020-03-20 stsp } else
1247 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1248 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1249 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1250 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1251 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1252 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1253 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1254 659e7fbd 2020-03-20 stsp branchname) == -1) {
1255 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1256 659e7fbd 2020-03-20 stsp goto done;
1257 659e7fbd 2020-03-20 stsp }
1258 659e7fbd 2020-03-20 stsp }
1259 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1260 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1261 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1262 659e7fbd 2020-03-20 stsp goto done;
1263 659e7fbd 2020-03-20 stsp }
1264 659e7fbd 2020-03-20 stsp
1265 659e7fbd 2020-03-20 stsp
1266 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1267 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1268 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1269 09838ffc 2020-03-18 stsp done:
1270 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1271 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1272 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1273 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1274 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1275 9c52365f 2020-03-21 stsp }
1276 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1277 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1278 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1279 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1280 bb64b798 2020-03-18 stsp if (repo)
1281 bb64b798 2020-03-18 stsp got_repo_close(repo);
1282 659e7fbd 2020-03-20 stsp if (head_symref)
1283 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1284 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1285 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1286 d9b4d0c0 2020-03-18 stsp free(pe->data);
1287 d9b4d0c0 2020-03-18 stsp }
1288 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1289 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1290 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1291 d9b4d0c0 2020-03-18 stsp free(pe->data);
1292 d9b4d0c0 2020-03-18 stsp }
1293 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1294 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1295 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1296 09838ffc 2020-03-18 stsp free(proto);
1297 09838ffc 2020-03-18 stsp free(host);
1298 09838ffc 2020-03-18 stsp free(port);
1299 09838ffc 2020-03-18 stsp free(server_path);
1300 09838ffc 2020-03-18 stsp free(repo_name);
1301 bb64b798 2020-03-18 stsp free(default_destdir);
1302 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1303 b46f3e71 2020-03-18 stsp free(git_url);
1304 39c64a6a 2020-03-18 stsp return error;
1305 7848a0e1 2020-03-19 stsp }
1306 7848a0e1 2020-03-19 stsp
1307 7848a0e1 2020-03-19 stsp static const struct got_error *
1308 7848a0e1 2020-03-19 stsp create_ref(const char *refname, struct got_object_id *id,
1309 688f11b3 2020-03-21 stsp const char *id_str, int verbosity, struct got_repository *repo)
1310 7848a0e1 2020-03-19 stsp {
1311 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1312 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1313 7848a0e1 2020-03-19 stsp
1314 688f11b3 2020-03-21 stsp if (verbosity >= 0)
1315 688f11b3 2020-03-21 stsp printf("Creating %s: %s\n", refname, id_str);
1316 7848a0e1 2020-03-19 stsp
1317 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&ref, refname, id);
1318 7848a0e1 2020-03-19 stsp if (err)
1319 7848a0e1 2020-03-19 stsp return err;
1320 7848a0e1 2020-03-19 stsp
1321 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1322 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1323 7848a0e1 2020-03-19 stsp return err;
1324 7848a0e1 2020-03-19 stsp }
1325 7848a0e1 2020-03-19 stsp
1326 7848a0e1 2020-03-19 stsp static const struct got_error *
1327 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1328 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1329 7848a0e1 2020-03-19 stsp {
1330 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1331 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1332 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1333 7848a0e1 2020-03-19 stsp
1334 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1335 7848a0e1 2020-03-19 stsp if (err)
1336 7848a0e1 2020-03-19 stsp goto done;
1337 7848a0e1 2020-03-19 stsp
1338 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1339 7848a0e1 2020-03-19 stsp struct got_reference *new_ref;
1340 7848a0e1 2020-03-19 stsp err = got_ref_alloc(&new_ref, got_ref_get_name(ref), new_id);
1341 7848a0e1 2020-03-19 stsp if (err)
1342 7848a0e1 2020-03-19 stsp goto done;
1343 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1344 688f11b3 2020-03-21 stsp printf("Deleting symbolic reference %s -> %s\n",
1345 688f11b3 2020-03-21 stsp got_ref_get_name(ref),
1346 688f11b3 2020-03-21 stsp got_ref_get_symref_target(ref));
1347 688f11b3 2020-03-21 stsp }
1348 7848a0e1 2020-03-19 stsp err = got_ref_delete(ref, repo);
1349 7848a0e1 2020-03-19 stsp if (err)
1350 7848a0e1 2020-03-19 stsp goto done;
1351 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1352 688f11b3 2020-03-21 stsp printf("Setting %s to %s\n", got_ref_get_name(ref),
1353 688f11b3 2020-03-21 stsp new_id_str);
1354 688f11b3 2020-03-21 stsp }
1355 7848a0e1 2020-03-19 stsp err = got_ref_write(new_ref, repo);
1356 7848a0e1 2020-03-19 stsp if (err)
1357 7848a0e1 2020-03-19 stsp goto done;
1358 7848a0e1 2020-03-19 stsp } else {
1359 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1360 7848a0e1 2020-03-19 stsp if (err)
1361 7848a0e1 2020-03-19 stsp goto done;
1362 7848a0e1 2020-03-19 stsp if (got_object_id_cmp(old_id, new_id) != 0) {
1363 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1364 688f11b3 2020-03-21 stsp printf("Setting %s to %s\n",
1365 688f11b3 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1366 688f11b3 2020-03-21 stsp }
1367 7848a0e1 2020-03-19 stsp err = got_ref_change_ref(ref, new_id);
1368 7848a0e1 2020-03-19 stsp if (err)
1369 7848a0e1 2020-03-19 stsp goto done;
1370 7848a0e1 2020-03-19 stsp err = got_ref_write(ref, repo);
1371 7848a0e1 2020-03-19 stsp if (err)
1372 7848a0e1 2020-03-19 stsp goto done;
1373 7848a0e1 2020-03-19 stsp }
1374 7848a0e1 2020-03-19 stsp }
1375 7848a0e1 2020-03-19 stsp done:
1376 7848a0e1 2020-03-19 stsp free(old_id);
1377 7848a0e1 2020-03-19 stsp free(new_id_str);
1378 7848a0e1 2020-03-19 stsp return err;
1379 2ab43947 2020-03-18 stsp }
1380 2ab43947 2020-03-18 stsp
1381 2ab43947 2020-03-18 stsp __dead static void
1382 7848a0e1 2020-03-19 stsp usage_fetch(void)
1383 7848a0e1 2020-03-19 stsp {
1384 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1385 13f12b09 2020-03-21 stsp "[-r repository-path] [-q] [-v] [remote-repository-name]\n",
1386 13f12b09 2020-03-21 stsp getprogname());
1387 7848a0e1 2020-03-19 stsp exit(1);
1388 7848a0e1 2020-03-19 stsp }
1389 7848a0e1 2020-03-19 stsp
1390 7848a0e1 2020-03-19 stsp static const struct got_error *
1391 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1392 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1393 f21ec2f0 2020-03-21 stsp {
1394 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1395 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1396 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1397 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1398 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1399 f21ec2f0 2020-03-21 stsp char *id_str;
1400 f21ec2f0 2020-03-21 stsp
1401 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1402 f21ec2f0 2020-03-21 stsp
1403 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1404 f21ec2f0 2020-03-21 stsp if (err)
1405 f21ec2f0 2020-03-21 stsp return err;
1406 f21ec2f0 2020-03-21 stsp
1407 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1408 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1409 f21ec2f0 2020-03-21 stsp
1410 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1411 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1412 f21ec2f0 2020-03-21 stsp continue;
1413 f21ec2f0 2020-03-21 stsp
1414 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1415 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1416 f21ec2f0 2020-03-21 stsp break;
1417 f21ec2f0 2020-03-21 stsp }
1418 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1419 f21ec2f0 2020-03-21 stsp continue;
1420 f21ec2f0 2020-03-21 stsp
1421 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1422 f21ec2f0 2020-03-21 stsp if (err)
1423 f21ec2f0 2020-03-21 stsp break;
1424 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1425 f21ec2f0 2020-03-21 stsp free(id);
1426 f21ec2f0 2020-03-21 stsp if (err)
1427 f21ec2f0 2020-03-21 stsp break;
1428 f21ec2f0 2020-03-21 stsp
1429 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1430 688f11b3 2020-03-21 stsp printf("Deleting %s: %s\n",
1431 688f11b3 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1432 688f11b3 2020-03-21 stsp }
1433 f21ec2f0 2020-03-21 stsp free(id_str);
1434 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1435 f21ec2f0 2020-03-21 stsp if (err)
1436 f21ec2f0 2020-03-21 stsp break;
1437 f21ec2f0 2020-03-21 stsp }
1438 f21ec2f0 2020-03-21 stsp
1439 f21ec2f0 2020-03-21 stsp return err;
1440 f21ec2f0 2020-03-21 stsp }
1441 f21ec2f0 2020-03-21 stsp
1442 f21ec2f0 2020-03-21 stsp static const struct got_error *
1443 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1444 7848a0e1 2020-03-19 stsp {
1445 7848a0e1 2020-03-19 stsp const struct got_error *error = NULL;
1446 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1447 7848a0e1 2020-03-19 stsp const char *remote_name;
1448 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1449 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1450 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1451 7848a0e1 2020-03-19 stsp int nremotes;
1452 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1453 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1454 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1455 4ba14133 2020-03-20 stsp struct got_pathlist_head refs, symrefs, wanted_branches;
1456 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1457 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1458 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1459 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1460 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1461 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1462 f21ec2f0 2020-03-21 stsp int delete_refs = 0;
1463 7848a0e1 2020-03-19 stsp
1464 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1465 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1466 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1467 7848a0e1 2020-03-19 stsp
1468 f21ec2f0 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:vq")) != -1) {
1469 7848a0e1 2020-03-19 stsp switch (ch) {
1470 659e7fbd 2020-03-20 stsp case 'a':
1471 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1472 4ba14133 2020-03-20 stsp break;
1473 4ba14133 2020-03-20 stsp case 'b':
1474 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1475 4ba14133 2020-03-20 stsp optarg, NULL);
1476 4ba14133 2020-03-20 stsp if (error)
1477 4ba14133 2020-03-20 stsp return error;
1478 41b0de12 2020-03-21 stsp break;
1479 f21ec2f0 2020-03-21 stsp case 'd':
1480 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1481 f21ec2f0 2020-03-21 stsp break;
1482 41b0de12 2020-03-21 stsp case 'l':
1483 41b0de12 2020-03-21 stsp list_refs_only = 1;
1484 659e7fbd 2020-03-20 stsp break;
1485 7848a0e1 2020-03-19 stsp case 'r':
1486 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1487 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1488 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1489 7848a0e1 2020-03-19 stsp optarg);
1490 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1491 7848a0e1 2020-03-19 stsp break;
1492 7848a0e1 2020-03-19 stsp case 'v':
1493 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1494 7848a0e1 2020-03-19 stsp verbosity = 0;
1495 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1496 7848a0e1 2020-03-19 stsp verbosity++;
1497 7848a0e1 2020-03-19 stsp break;
1498 7848a0e1 2020-03-19 stsp case 'q':
1499 7848a0e1 2020-03-19 stsp verbosity = -1;
1500 7848a0e1 2020-03-19 stsp break;
1501 7848a0e1 2020-03-19 stsp default:
1502 7848a0e1 2020-03-19 stsp usage_fetch();
1503 7848a0e1 2020-03-19 stsp break;
1504 7848a0e1 2020-03-19 stsp }
1505 7848a0e1 2020-03-19 stsp }
1506 7848a0e1 2020-03-19 stsp argc -= optind;
1507 7848a0e1 2020-03-19 stsp argv += optind;
1508 7848a0e1 2020-03-19 stsp
1509 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1510 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1511 41b0de12 2020-03-21 stsp if (list_refs_only) {
1512 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1513 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1514 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1515 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1516 f21ec2f0 2020-03-21 stsp if (delete_refs)
1517 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1518 41b0de12 2020-03-21 stsp if (verbosity == -1)
1519 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1520 41b0de12 2020-03-21 stsp }
1521 4ba14133 2020-03-20 stsp
1522 7848a0e1 2020-03-19 stsp if (argc == 0)
1523 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1524 7848a0e1 2020-03-19 stsp else if (argc == 1)
1525 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1526 7848a0e1 2020-03-19 stsp else
1527 7848a0e1 2020-03-19 stsp usage_fetch();
1528 7848a0e1 2020-03-19 stsp
1529 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1530 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1531 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1532 7848a0e1 2020-03-19 stsp goto done;
1533 7848a0e1 2020-03-19 stsp }
1534 7848a0e1 2020-03-19 stsp
1535 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1536 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1537 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1538 7848a0e1 2020-03-19 stsp goto done;
1539 7848a0e1 2020-03-19 stsp else
1540 7848a0e1 2020-03-19 stsp error = NULL;
1541 7848a0e1 2020-03-19 stsp if (worktree) {
1542 7848a0e1 2020-03-19 stsp repo_path =
1543 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1544 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1545 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1546 7848a0e1 2020-03-19 stsp if (error)
1547 7848a0e1 2020-03-19 stsp goto done;
1548 7848a0e1 2020-03-19 stsp } else {
1549 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1550 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1551 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1552 7848a0e1 2020-03-19 stsp goto done;
1553 7848a0e1 2020-03-19 stsp }
1554 7848a0e1 2020-03-19 stsp }
1555 7848a0e1 2020-03-19 stsp }
1556 7848a0e1 2020-03-19 stsp
1557 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1558 7848a0e1 2020-03-19 stsp if (error)
1559 7848a0e1 2020-03-19 stsp goto done;
1560 7848a0e1 2020-03-19 stsp
1561 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1562 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1563 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1564 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1565 7848a0e1 2020-03-19 stsp break;
1566 7848a0e1 2020-03-19 stsp }
1567 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1568 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1569 7848a0e1 2020-03-19 stsp goto done;
1570 7848a0e1 2020-03-19 stsp }
1571 7848a0e1 2020-03-19 stsp
1572 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1573 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1574 7848a0e1 2020-03-19 stsp if (error)
1575 7848a0e1 2020-03-19 stsp goto done;
1576 7848a0e1 2020-03-19 stsp
1577 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1578 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1579 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1580 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1581 7848a0e1 2020-03-19 stsp err(1, "pledge");
1582 7848a0e1 2020-03-19 stsp #endif
1583 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1584 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1585 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1586 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1587 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1588 7848a0e1 2020-03-19 stsp err(1, "pledge");
1589 7848a0e1 2020-03-19 stsp #endif
1590 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1591 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1592 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1593 7848a0e1 2020-03-19 stsp goto done;
1594 7848a0e1 2020-03-19 stsp } else {
1595 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1596 7848a0e1 2020-03-19 stsp goto done;
1597 7848a0e1 2020-03-19 stsp }
1598 7848a0e1 2020-03-19 stsp
1599 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1600 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1601 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1602 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1603 7848a0e1 2020-03-19 stsp goto done;
1604 7848a0e1 2020-03-19 stsp }
1605 7848a0e1 2020-03-19 stsp }
1606 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1607 7848a0e1 2020-03-19 stsp if (error)
1608 7848a0e1 2020-03-19 stsp goto done;
1609 7848a0e1 2020-03-19 stsp
1610 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1611 9c52365f 2020-03-21 stsp server_path, verbosity);
1612 7848a0e1 2020-03-19 stsp if (error)
1613 7848a0e1 2020-03-19 stsp goto done;
1614 7848a0e1 2020-03-19 stsp
1615 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1616 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1617 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1618 7848a0e1 2020-03-19 stsp
1619 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1620 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1621 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1622 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1623 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1624 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1625 41b0de12 2020-03-21 stsp list_refs_only, fetchfd, repo, fetch_progress, &fpa);
1626 7848a0e1 2020-03-19 stsp if (error)
1627 7848a0e1 2020-03-19 stsp goto done;
1628 7848a0e1 2020-03-19 stsp
1629 41b0de12 2020-03-21 stsp if (list_refs_only) {
1630 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1631 41b0de12 2020-03-21 stsp goto done;
1632 41b0de12 2020-03-21 stsp }
1633 41b0de12 2020-03-21 stsp
1634 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1635 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1636 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1637 f21ec2f0 2020-03-21 stsp if (delete_refs)
1638 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1639 7848a0e1 2020-03-19 stsp goto done;
1640 7848a0e1 2020-03-19 stsp }
1641 7848a0e1 2020-03-19 stsp
1642 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1643 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1644 984065c8 2020-03-19 stsp if (error)
1645 984065c8 2020-03-19 stsp goto done;
1646 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1647 984065c8 2020-03-19 stsp free(id_str);
1648 984065c8 2020-03-19 stsp id_str = NULL;
1649 984065c8 2020-03-19 stsp }
1650 7848a0e1 2020-03-19 stsp
1651 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1652 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1653 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1654 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1655 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1656 7848a0e1 2020-03-19 stsp char *remote_refname;
1657 fed0b873 2020-03-20 stsp
1658 7848a0e1 2020-03-19 stsp error = got_object_id_str(&id_str, id);
1659 7848a0e1 2020-03-19 stsp if (error)
1660 7848a0e1 2020-03-19 stsp goto done;
1661 7848a0e1 2020-03-19 stsp
1662 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1663 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1664 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, refname, 0);
1665 7848a0e1 2020-03-19 stsp if (error) {
1666 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1667 7848a0e1 2020-03-19 stsp goto done;
1668 688f11b3 2020-03-21 stsp error = create_ref(refname, id, id_str,
1669 688f11b3 2020-03-21 stsp verbosity, repo);
1670 7848a0e1 2020-03-19 stsp if (error)
1671 7848a0e1 2020-03-19 stsp goto done;
1672 7848a0e1 2020-03-19 stsp } else {
1673 688f11b3 2020-03-21 stsp error = update_ref(ref, id, verbosity, repo);
1674 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1675 7848a0e1 2020-03-19 stsp if (error)
1676 7848a0e1 2020-03-19 stsp goto done;
1677 7848a0e1 2020-03-19 stsp }
1678 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1679 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1680 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1681 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1682 7848a0e1 2020-03-19 stsp goto done;
1683 7848a0e1 2020-03-19 stsp }
1684 7848a0e1 2020-03-19 stsp
1685 7848a0e1 2020-03-19 stsp error = got_ref_open(&ref, repo, remote_refname, 0);
1686 7848a0e1 2020-03-19 stsp if (error) {
1687 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1688 7848a0e1 2020-03-19 stsp goto done;
1689 e5083482 2020-03-20 stsp error = create_ref(remote_refname, id, id_str,
1690 688f11b3 2020-03-21 stsp verbosity, repo);
1691 7848a0e1 2020-03-19 stsp if (error)
1692 7848a0e1 2020-03-19 stsp goto done;
1693 7848a0e1 2020-03-19 stsp } else {
1694 688f11b3 2020-03-21 stsp error = update_ref(ref, id, verbosity, repo);
1695 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1696 7848a0e1 2020-03-19 stsp if (error)
1697 7848a0e1 2020-03-19 stsp goto done;
1698 7848a0e1 2020-03-19 stsp }
1699 2ec30c80 2020-03-20 stsp
1700 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1701 2ec30c80 2020-03-20 stsp error = got_ref_open(&ref, repo, refname, 0);
1702 2ec30c80 2020-03-20 stsp if (error) {
1703 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1704 2ec30c80 2020-03-20 stsp goto done;
1705 688f11b3 2020-03-21 stsp error = create_ref(refname, id, id_str,
1706 688f11b3 2020-03-21 stsp verbosity, repo);
1707 2ec30c80 2020-03-20 stsp if (error)
1708 2ec30c80 2020-03-20 stsp goto done;
1709 2ec30c80 2020-03-20 stsp } else
1710 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1711 7848a0e1 2020-03-19 stsp }
1712 7848a0e1 2020-03-19 stsp free(id_str);
1713 7848a0e1 2020-03-19 stsp id_str = NULL;
1714 7848a0e1 2020-03-19 stsp }
1715 f21ec2f0 2020-03-21 stsp if (delete_refs)
1716 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1717 7848a0e1 2020-03-19 stsp done:
1718 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1719 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1720 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1721 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1722 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1723 9c52365f 2020-03-21 stsp }
1724 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1725 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1726 7848a0e1 2020-03-19 stsp if (repo)
1727 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1728 7848a0e1 2020-03-19 stsp if (worktree)
1729 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1730 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1731 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1732 7848a0e1 2020-03-19 stsp free(pe->data);
1733 7848a0e1 2020-03-19 stsp }
1734 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
1735 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1736 7848a0e1 2020-03-19 stsp free((void *)pe->path);
1737 7848a0e1 2020-03-19 stsp free(pe->data);
1738 7848a0e1 2020-03-19 stsp }
1739 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
1740 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1741 7848a0e1 2020-03-19 stsp free(id_str);
1742 7848a0e1 2020-03-19 stsp free(cwd);
1743 7848a0e1 2020-03-19 stsp free(repo_path);
1744 7848a0e1 2020-03-19 stsp free(pack_hash);
1745 7848a0e1 2020-03-19 stsp free(proto);
1746 7848a0e1 2020-03-19 stsp free(host);
1747 7848a0e1 2020-03-19 stsp free(port);
1748 7848a0e1 2020-03-19 stsp free(server_path);
1749 7848a0e1 2020-03-19 stsp free(repo_name);
1750 7848a0e1 2020-03-19 stsp return error;
1751 7848a0e1 2020-03-19 stsp }
1752 7848a0e1 2020-03-19 stsp
1753 7848a0e1 2020-03-19 stsp
1754 7848a0e1 2020-03-19 stsp __dead static void
1755 2ab43947 2020-03-18 stsp usage_checkout(void)
1756 2ab43947 2020-03-18 stsp {
1757 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
1758 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
1759 2ab43947 2020-03-18 stsp exit(1);
1760 2ab43947 2020-03-18 stsp }
1761 2ab43947 2020-03-18 stsp
1762 2ab43947 2020-03-18 stsp static void
1763 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
1764 2ab43947 2020-03-18 stsp {
1765 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
1766 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
1767 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
1768 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
1769 2ab43947 2020-03-18 stsp getprogname());
1770 2ab43947 2020-03-18 stsp }
1771 2ab43947 2020-03-18 stsp
1772 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
1773 2ab43947 2020-03-18 stsp const char *worktree_path;
1774 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
1775 2ab43947 2020-03-18 stsp };
1776 2ab43947 2020-03-18 stsp
1777 2ab43947 2020-03-18 stsp static const struct got_error *
1778 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
1779 2ab43947 2020-03-18 stsp {
1780 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
1781 2ab43947 2020-03-18 stsp
1782 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
1783 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
1784 2ab43947 2020-03-18 stsp return NULL;
1785 2ab43947 2020-03-18 stsp
1786 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
1787 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
1788 2ab43947 2020-03-18 stsp return NULL;
1789 2ab43947 2020-03-18 stsp }
1790 2ab43947 2020-03-18 stsp
1791 2ab43947 2020-03-18 stsp while (path[0] == '/')
1792 2ab43947 2020-03-18 stsp path++;
1793 2ab43947 2020-03-18 stsp
1794 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
1795 2ab43947 2020-03-18 stsp return NULL;
1796 2ab43947 2020-03-18 stsp }
1797 2ab43947 2020-03-18 stsp
1798 2ab43947 2020-03-18 stsp static const struct got_error *
1799 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
1800 2ab43947 2020-03-18 stsp {
1801 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
1802 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
1803 2ab43947 2020-03-18 stsp return NULL;
1804 2ab43947 2020-03-18 stsp }
1805 2ab43947 2020-03-18 stsp
1806 2ab43947 2020-03-18 stsp static const struct got_error *
1807 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
1808 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
1809 2ab43947 2020-03-18 stsp struct got_repository *repo)
1810 2ab43947 2020-03-18 stsp {
1811 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1812 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
1813 2ab43947 2020-03-18 stsp
1814 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
1815 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
1816 2ab43947 2020-03-18 stsp if (err)
1817 2ab43947 2020-03-18 stsp return err;
1818 2ab43947 2020-03-18 stsp
1819 2ab43947 2020-03-18 stsp if (yca_id == NULL)
1820 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1821 2ab43947 2020-03-18 stsp
1822 2ab43947 2020-03-18 stsp /*
1823 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
1824 2ab43947 2020-03-18 stsp * and the work tree's base commit.
1825 2ab43947 2020-03-18 stsp *
1826 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
1827 2ab43947 2020-03-18 stsp *
1828 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
1829 2ab43947 2020-03-18 stsp * \ /
1830 2ab43947 2020-03-18 stsp * C E
1831 2ab43947 2020-03-18 stsp * \ /
1832 2ab43947 2020-03-18 stsp * B (yca)
1833 2ab43947 2020-03-18 stsp * |
1834 2ab43947 2020-03-18 stsp * A
1835 2ab43947 2020-03-18 stsp *
1836 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
1837 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
1838 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
1839 2ab43947 2020-03-18 stsp */
1840 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
1841 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
1842 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1843 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
1844 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
1845 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
1846 2ab43947 2020-03-18 stsp
1847 2ab43947 2020-03-18 stsp free(yca_id);
1848 2ab43947 2020-03-18 stsp return NULL;
1849 2ab43947 2020-03-18 stsp }
1850 2ab43947 2020-03-18 stsp
1851 2ab43947 2020-03-18 stsp static const struct got_error *
1852 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
1853 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
1854 2ab43947 2020-03-18 stsp struct got_repository *repo)
1855 2ab43947 2020-03-18 stsp {
1856 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
1857 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
1858 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
1859 2ab43947 2020-03-18 stsp int is_same_branch = 0;
1860 2ab43947 2020-03-18 stsp
1861 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
1862 2ab43947 2020-03-18 stsp if (err)
1863 2ab43947 2020-03-18 stsp goto done;
1864 2ab43947 2020-03-18 stsp
1865 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
1866 2ab43947 2020-03-18 stsp is_same_branch = 1;
1867 2ab43947 2020-03-18 stsp goto done;
1868 2ab43947 2020-03-18 stsp }
1869 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
1870 2ab43947 2020-03-18 stsp is_same_branch = 1;
1871 2ab43947 2020-03-18 stsp goto done;
1872 2ab43947 2020-03-18 stsp }
1873 2ab43947 2020-03-18 stsp
1874 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
1875 2ab43947 2020-03-18 stsp if (err)
1876 2ab43947 2020-03-18 stsp goto done;
1877 2ab43947 2020-03-18 stsp
1878 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
1879 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1880 2ab43947 2020-03-18 stsp if (err)
1881 2ab43947 2020-03-18 stsp goto done;
1882 2ab43947 2020-03-18 stsp
1883 2ab43947 2020-03-18 stsp for (;;) {
1884 2ab43947 2020-03-18 stsp struct got_object_id *id;
1885 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1886 2ab43947 2020-03-18 stsp check_cancelled, NULL);
1887 2ab43947 2020-03-18 stsp if (err) {
1888 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1889 2ab43947 2020-03-18 stsp err = NULL;
1890 2ab43947 2020-03-18 stsp break;
1891 2ab43947 2020-03-18 stsp }
1892 2ab43947 2020-03-18 stsp
1893 2ab43947 2020-03-18 stsp if (id) {
1894 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
1895 2ab43947 2020-03-18 stsp break;
1896 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
1897 2ab43947 2020-03-18 stsp is_same_branch = 1;
1898 2ab43947 2020-03-18 stsp break;
1899 2ab43947 2020-03-18 stsp }
1900 2ab43947 2020-03-18 stsp }
1901 2ab43947 2020-03-18 stsp }
1902 2ab43947 2020-03-18 stsp done:
1903 2ab43947 2020-03-18 stsp if (graph)
1904 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
1905 2ab43947 2020-03-18 stsp free(head_commit_id);
1906 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
1907 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
1908 2ab43947 2020-03-18 stsp return err;
1909 a367ff0f 2019-05-14 stsp }
1910 8069f636 2019-01-12 stsp
1911 4ed7e80c 2018-05-20 stsp static const struct got_error *
1912 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1913 4b6c9460 2020-03-05 stsp {
1914 4b6c9460 2020-03-05 stsp static char msg[512];
1915 4b6c9460 2020-03-05 stsp const char *branch_name;
1916 4b6c9460 2020-03-05 stsp
1917 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1918 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1919 4b6c9460 2020-03-05 stsp else
1920 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1921 4b6c9460 2020-03-05 stsp
1922 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1923 4b6c9460 2020-03-05 stsp branch_name += 11;
1924 4b6c9460 2020-03-05 stsp
1925 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1926 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1927 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1928 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1929 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1930 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1931 4b6c9460 2020-03-05 stsp
1932 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1933 4b6c9460 2020-03-05 stsp }
1934 4b6c9460 2020-03-05 stsp
1935 4b6c9460 2020-03-05 stsp static const struct got_error *
1936 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1937 c09a553d 2018-03-12 stsp {
1938 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1939 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1940 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1941 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1942 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1943 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1944 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1945 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1946 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1947 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1948 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1949 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1950 f2ea84fa 2019-07-27 stsp
1951 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1952 c09a553d 2018-03-12 stsp
1953 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1954 0bb8a95e 2018-03-12 stsp switch (ch) {
1955 08573d5b 2019-05-14 stsp case 'b':
1956 08573d5b 2019-05-14 stsp branch_name = optarg;
1957 08573d5b 2019-05-14 stsp break;
1958 8069f636 2019-01-12 stsp case 'c':
1959 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1960 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1961 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1962 bb51a5b4 2020-01-13 stsp break;
1963 bb51a5b4 2020-01-13 stsp case 'E':
1964 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1965 8069f636 2019-01-12 stsp break;
1966 0bb8a95e 2018-03-12 stsp case 'p':
1967 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1968 0bb8a95e 2018-03-12 stsp break;
1969 0bb8a95e 2018-03-12 stsp default:
1970 2deda0b9 2019-03-07 stsp usage_checkout();
1971 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1972 0bb8a95e 2018-03-12 stsp }
1973 0bb8a95e 2018-03-12 stsp }
1974 0bb8a95e 2018-03-12 stsp
1975 0bb8a95e 2018-03-12 stsp argc -= optind;
1976 0bb8a95e 2018-03-12 stsp argv += optind;
1977 0bb8a95e 2018-03-12 stsp
1978 6715a751 2018-03-16 stsp #ifndef PROFILE
1979 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1980 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1981 c09a553d 2018-03-12 stsp err(1, "pledge");
1982 6715a751 2018-03-16 stsp #endif
1983 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1984 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1985 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1986 76089277 2018-04-01 stsp if (repo_path == NULL)
1987 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1988 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1989 76089277 2018-04-01 stsp if (cwd == NULL) {
1990 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1991 76089277 2018-04-01 stsp goto done;
1992 76089277 2018-04-01 stsp }
1993 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1994 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1995 230a42bd 2019-05-11 jcs if (base == NULL) {
1996 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1997 230a42bd 2019-05-11 jcs path_prefix);
1998 230a42bd 2019-05-11 jcs goto done;
1999 230a42bd 2019-05-11 jcs }
2000 230a42bd 2019-05-11 jcs } else {
2001 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2002 230a42bd 2019-05-11 jcs if (base == NULL) {
2003 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2004 230a42bd 2019-05-11 jcs repo_path);
2005 230a42bd 2019-05-11 jcs goto done;
2006 230a42bd 2019-05-11 jcs }
2007 76089277 2018-04-01 stsp }
2008 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2009 c09a553d 2018-03-12 stsp if (dotgit)
2010 c09a553d 2018-03-12 stsp *dotgit = '\0';
2011 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2012 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2013 c09a553d 2018-03-12 stsp free(cwd);
2014 76089277 2018-04-01 stsp goto done;
2015 c09a553d 2018-03-12 stsp }
2016 c09a553d 2018-03-12 stsp free(cwd);
2017 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2018 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2019 76089277 2018-04-01 stsp if (repo_path == NULL) {
2020 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2021 76089277 2018-04-01 stsp goto done;
2022 76089277 2018-04-01 stsp }
2023 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2024 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2025 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2026 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2027 b4b3a7dd 2019-07-22 stsp argv[1]);
2028 b4b3a7dd 2019-07-22 stsp goto done;
2029 b4b3a7dd 2019-07-22 stsp }
2030 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2031 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2032 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2033 b4b3a7dd 2019-07-22 stsp goto done;
2034 b4b3a7dd 2019-07-22 stsp }
2035 76089277 2018-04-01 stsp }
2036 c09a553d 2018-03-12 stsp } else
2037 c09a553d 2018-03-12 stsp usage_checkout();
2038 c09a553d 2018-03-12 stsp
2039 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2040 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2041 13bfb272 2019-05-10 jcs
2042 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2043 c09a553d 2018-03-12 stsp if (error != NULL)
2044 c02c541e 2019-03-29 stsp goto done;
2045 c02c541e 2019-03-29 stsp
2046 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2047 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2048 c530dc23 2019-07-23 stsp if (error) {
2049 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2050 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2051 c530dc23 2019-07-23 stsp goto done;
2052 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2053 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2054 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2055 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2056 c530dc23 2019-07-23 stsp goto done;
2057 c530dc23 2019-07-23 stsp }
2058 c530dc23 2019-07-23 stsp }
2059 c530dc23 2019-07-23 stsp
2060 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2061 c02c541e 2019-03-29 stsp if (error)
2062 c09a553d 2018-03-12 stsp goto done;
2063 8069f636 2019-01-12 stsp
2064 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2065 c09a553d 2018-03-12 stsp if (error != NULL)
2066 c09a553d 2018-03-12 stsp goto done;
2067 c09a553d 2018-03-12 stsp
2068 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2069 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2070 c09a553d 2018-03-12 stsp goto done;
2071 c09a553d 2018-03-12 stsp
2072 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2073 c09a553d 2018-03-12 stsp if (error != NULL)
2074 c09a553d 2018-03-12 stsp goto done;
2075 c09a553d 2018-03-12 stsp
2076 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2077 e5dc7198 2018-12-29 stsp path_prefix);
2078 e5dc7198 2018-12-29 stsp if (error != NULL)
2079 e5dc7198 2018-12-29 stsp goto done;
2080 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2081 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2082 49520a32 2018-12-29 stsp goto done;
2083 49520a32 2018-12-29 stsp }
2084 49520a32 2018-12-29 stsp
2085 8069f636 2019-01-12 stsp if (commit_id_str) {
2086 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2087 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2088 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2089 30837e32 2019-07-25 stsp if (error)
2090 8069f636 2019-01-12 stsp goto done;
2091 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2092 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2093 8069f636 2019-01-12 stsp if (error != NULL) {
2094 8069f636 2019-01-12 stsp free(commit_id);
2095 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2096 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2097 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2098 4b6c9460 2020-03-05 stsp }
2099 8069f636 2019-01-12 stsp goto done;
2100 8069f636 2019-01-12 stsp }
2101 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2102 4b6c9460 2020-03-05 stsp if (error) {
2103 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2104 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2105 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2106 4b6c9460 2020-03-05 stsp }
2107 45d344f6 2019-05-14 stsp goto done;
2108 4b6c9460 2020-03-05 stsp }
2109 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2110 8069f636 2019-01-12 stsp commit_id);
2111 8069f636 2019-01-12 stsp free(commit_id);
2112 8069f636 2019-01-12 stsp if (error)
2113 8069f636 2019-01-12 stsp goto done;
2114 8069f636 2019-01-12 stsp }
2115 8069f636 2019-01-12 stsp
2116 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2117 f2ea84fa 2019-07-27 stsp if (error)
2118 f2ea84fa 2019-07-27 stsp goto done;
2119 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2120 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2121 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2122 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2123 c09a553d 2018-03-12 stsp if (error != NULL)
2124 c09a553d 2018-03-12 stsp goto done;
2125 c09a553d 2018-03-12 stsp
2126 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2127 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2128 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2129 c09a553d 2018-03-12 stsp done:
2130 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2131 8069f636 2019-01-12 stsp free(commit_id_str);
2132 76089277 2018-04-01 stsp free(repo_path);
2133 507dc3bb 2018-12-29 stsp free(worktree_path);
2134 507dc3bb 2018-12-29 stsp return error;
2135 507dc3bb 2018-12-29 stsp }
2136 507dc3bb 2018-12-29 stsp
2137 507dc3bb 2018-12-29 stsp __dead static void
2138 507dc3bb 2018-12-29 stsp usage_update(void)
2139 507dc3bb 2018-12-29 stsp {
2140 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2141 507dc3bb 2018-12-29 stsp getprogname());
2142 507dc3bb 2018-12-29 stsp exit(1);
2143 507dc3bb 2018-12-29 stsp }
2144 507dc3bb 2018-12-29 stsp
2145 1ee397ad 2019-07-12 stsp static const struct got_error *
2146 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2147 507dc3bb 2018-12-29 stsp {
2148 784955db 2019-01-12 stsp int *did_something = arg;
2149 784955db 2019-01-12 stsp
2150 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2151 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2152 1ee397ad 2019-07-12 stsp return NULL;
2153 507dc3bb 2018-12-29 stsp
2154 1545c615 2019-02-10 stsp *did_something = 1;
2155 a484d721 2019-06-10 stsp
2156 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2157 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2158 1ee397ad 2019-07-12 stsp return NULL;
2159 a484d721 2019-06-10 stsp
2160 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2161 507dc3bb 2018-12-29 stsp path++;
2162 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2163 1ee397ad 2019-07-12 stsp return NULL;
2164 be7061eb 2018-12-30 stsp }
2165 be7061eb 2018-12-30 stsp
2166 be7061eb 2018-12-30 stsp static const struct got_error *
2167 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2168 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2169 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2170 a1fb16d8 2019-05-24 stsp {
2171 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2172 a1fb16d8 2019-05-24 stsp char *base_id_str;
2173 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2174 a1fb16d8 2019-05-24 stsp
2175 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2176 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2177 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2178 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2179 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2180 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2181 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2182 a1fb16d8 2019-05-24 stsp }
2183 a1fb16d8 2019-05-24 stsp
2184 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2185 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2186 a1fb16d8 2019-05-24 stsp if (err) {
2187 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2188 a1fb16d8 2019-05-24 stsp return err;
2189 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2190 a1fb16d8 2019-05-24 stsp }
2191 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2192 a1fb16d8 2019-05-24 stsp return NULL;
2193 a1fb16d8 2019-05-24 stsp
2194 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2195 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2196 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2197 a1fb16d8 2019-05-24 stsp if (err)
2198 a1fb16d8 2019-05-24 stsp return err;
2199 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2200 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2201 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2202 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2203 0ebf8283 2019-07-24 stsp return NULL;
2204 0ebf8283 2019-07-24 stsp }
2205 0ebf8283 2019-07-24 stsp
2206 0ebf8283 2019-07-24 stsp static const struct got_error *
2207 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2208 0ebf8283 2019-07-24 stsp {
2209 0ebf8283 2019-07-24 stsp const struct got_error *err;
2210 0ebf8283 2019-07-24 stsp int in_progress;
2211 0ebf8283 2019-07-24 stsp
2212 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2213 0ebf8283 2019-07-24 stsp if (err)
2214 0ebf8283 2019-07-24 stsp return err;
2215 0ebf8283 2019-07-24 stsp if (in_progress)
2216 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2217 0ebf8283 2019-07-24 stsp
2218 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2219 0ebf8283 2019-07-24 stsp if (err)
2220 0ebf8283 2019-07-24 stsp return err;
2221 0ebf8283 2019-07-24 stsp if (in_progress)
2222 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2223 0ebf8283 2019-07-24 stsp
2224 a1fb16d8 2019-05-24 stsp return NULL;
2225 a5edda0a 2019-07-27 stsp }
2226 a5edda0a 2019-07-27 stsp
2227 a5edda0a 2019-07-27 stsp static const struct got_error *
2228 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2229 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2230 a5edda0a 2019-07-27 stsp {
2231 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2232 a5edda0a 2019-07-27 stsp char *path;
2233 a5edda0a 2019-07-27 stsp int i;
2234 a5edda0a 2019-07-27 stsp
2235 a5edda0a 2019-07-27 stsp if (argc == 0) {
2236 a5edda0a 2019-07-27 stsp path = strdup("");
2237 a5edda0a 2019-07-27 stsp if (path == NULL)
2238 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2239 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2240 a5edda0a 2019-07-27 stsp }
2241 a5edda0a 2019-07-27 stsp
2242 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2243 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2244 a5edda0a 2019-07-27 stsp if (err)
2245 a5edda0a 2019-07-27 stsp break;
2246 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2247 a5edda0a 2019-07-27 stsp if (err) {
2248 a5edda0a 2019-07-27 stsp free(path);
2249 a5edda0a 2019-07-27 stsp break;
2250 a5edda0a 2019-07-27 stsp }
2251 a5edda0a 2019-07-27 stsp }
2252 a5edda0a 2019-07-27 stsp
2253 a5edda0a 2019-07-27 stsp return err;
2254 a1fb16d8 2019-05-24 stsp }
2255 a1fb16d8 2019-05-24 stsp
2256 a1fb16d8 2019-05-24 stsp static const struct got_error *
2257 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2258 507dc3bb 2018-12-29 stsp {
2259 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2260 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2261 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2262 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2263 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2264 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2265 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2266 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2267 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2268 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2269 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2270 507dc3bb 2018-12-29 stsp
2271 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2272 f2ea84fa 2019-07-27 stsp
2273 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2274 507dc3bb 2018-12-29 stsp switch (ch) {
2275 024e9686 2019-05-14 stsp case 'b':
2276 024e9686 2019-05-14 stsp branch_name = optarg;
2277 024e9686 2019-05-14 stsp break;
2278 507dc3bb 2018-12-29 stsp case 'c':
2279 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2280 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2281 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2282 507dc3bb 2018-12-29 stsp break;
2283 507dc3bb 2018-12-29 stsp default:
2284 2deda0b9 2019-03-07 stsp usage_update();
2285 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2286 507dc3bb 2018-12-29 stsp }
2287 507dc3bb 2018-12-29 stsp }
2288 507dc3bb 2018-12-29 stsp
2289 507dc3bb 2018-12-29 stsp argc -= optind;
2290 507dc3bb 2018-12-29 stsp argv += optind;
2291 507dc3bb 2018-12-29 stsp
2292 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2293 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2294 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2295 507dc3bb 2018-12-29 stsp err(1, "pledge");
2296 507dc3bb 2018-12-29 stsp #endif
2297 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2298 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2299 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2300 c4cdcb68 2019-04-03 stsp goto done;
2301 c4cdcb68 2019-04-03 stsp }
2302 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2303 7d5807f4 2019-07-11 stsp if (error)
2304 7d5807f4 2019-07-11 stsp goto done;
2305 7d5807f4 2019-07-11 stsp
2306 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2307 f2ea84fa 2019-07-27 stsp if (error)
2308 f2ea84fa 2019-07-27 stsp goto done;
2309 507dc3bb 2018-12-29 stsp
2310 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2311 c9956ddf 2019-09-08 stsp NULL);
2312 507dc3bb 2018-12-29 stsp if (error != NULL)
2313 507dc3bb 2018-12-29 stsp goto done;
2314 507dc3bb 2018-12-29 stsp
2315 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2316 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2317 087fb88c 2019-08-04 stsp if (error)
2318 087fb88c 2019-08-04 stsp goto done;
2319 087fb88c 2019-08-04 stsp
2320 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2321 0266afb7 2019-01-04 stsp if (error)
2322 0266afb7 2019-01-04 stsp goto done;
2323 0266afb7 2019-01-04 stsp
2324 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2325 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2326 024e9686 2019-05-14 stsp if (error != NULL)
2327 024e9686 2019-05-14 stsp goto done;
2328 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2329 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2330 9c4b8182 2019-01-02 stsp if (error != NULL)
2331 9c4b8182 2019-01-02 stsp goto done;
2332 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2333 507dc3bb 2018-12-29 stsp if (error != NULL)
2334 507dc3bb 2018-12-29 stsp goto done;
2335 507dc3bb 2018-12-29 stsp } else {
2336 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2337 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2338 04f57cb3 2019-07-25 stsp free(commit_id_str);
2339 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2340 30837e32 2019-07-25 stsp if (error)
2341 a297e751 2019-07-11 stsp goto done;
2342 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2343 a297e751 2019-07-11 stsp if (error)
2344 507dc3bb 2018-12-29 stsp goto done;
2345 507dc3bb 2018-12-29 stsp }
2346 35c965b2 2018-12-29 stsp
2347 a1fb16d8 2019-05-24 stsp if (branch_name) {
2348 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2349 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2350 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2351 f2ea84fa 2019-07-27 stsp continue;
2352 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2353 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2354 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2355 024e9686 2019-05-14 stsp goto done;
2356 024e9686 2019-05-14 stsp }
2357 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2358 024e9686 2019-05-14 stsp if (error)
2359 024e9686 2019-05-14 stsp goto done;
2360 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2361 3aef623b 2019-10-15 stsp repo);
2362 a367ff0f 2019-05-14 stsp free(head_commit_id);
2363 024e9686 2019-05-14 stsp if (error != NULL)
2364 024e9686 2019-05-14 stsp goto done;
2365 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2366 a367ff0f 2019-05-14 stsp if (error)
2367 a367ff0f 2019-05-14 stsp goto done;
2368 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2369 024e9686 2019-05-14 stsp if (error)
2370 024e9686 2019-05-14 stsp goto done;
2371 024e9686 2019-05-14 stsp } else {
2372 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2373 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2374 a367ff0f 2019-05-14 stsp if (error != NULL) {
2375 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2376 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2377 024e9686 2019-05-14 stsp goto done;
2378 a367ff0f 2019-05-14 stsp }
2379 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2380 a367ff0f 2019-05-14 stsp if (error)
2381 a367ff0f 2019-05-14 stsp goto done;
2382 024e9686 2019-05-14 stsp }
2383 507dc3bb 2018-12-29 stsp
2384 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2385 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2386 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2387 507dc3bb 2018-12-29 stsp commit_id);
2388 507dc3bb 2018-12-29 stsp if (error)
2389 507dc3bb 2018-12-29 stsp goto done;
2390 507dc3bb 2018-12-29 stsp }
2391 507dc3bb 2018-12-29 stsp
2392 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2393 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2394 507dc3bb 2018-12-29 stsp if (error != NULL)
2395 507dc3bb 2018-12-29 stsp goto done;
2396 9c4b8182 2019-01-02 stsp
2397 784955db 2019-01-12 stsp if (did_something)
2398 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2399 784955db 2019-01-12 stsp else
2400 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2401 507dc3bb 2018-12-29 stsp done:
2402 c09a553d 2018-03-12 stsp free(worktree_path);
2403 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2404 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2405 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2406 507dc3bb 2018-12-29 stsp free(commit_id);
2407 9c4b8182 2019-01-02 stsp free(commit_id_str);
2408 c09a553d 2018-03-12 stsp return error;
2409 c09a553d 2018-03-12 stsp }
2410 c09a553d 2018-03-12 stsp
2411 f42b1b34 2018-03-12 stsp static const struct got_error *
2412 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2413 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2414 63035f9f 2019-10-06 stsp struct got_repository *repo)
2415 5c860e29 2018-03-12 stsp {
2416 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2417 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2418 79109fed 2018-03-27 stsp
2419 44392932 2019-08-25 stsp if (blob_id1) {
2420 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2421 44392932 2019-08-25 stsp if (err)
2422 44392932 2019-08-25 stsp goto done;
2423 44392932 2019-08-25 stsp }
2424 79109fed 2018-03-27 stsp
2425 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2426 44392932 2019-08-25 stsp if (err)
2427 44392932 2019-08-25 stsp goto done;
2428 79109fed 2018-03-27 stsp
2429 44392932 2019-08-25 stsp while (path[0] == '/')
2430 44392932 2019-08-25 stsp path++;
2431 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2432 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2433 44392932 2019-08-25 stsp done:
2434 44392932 2019-08-25 stsp if (blob1)
2435 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2436 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2437 44392932 2019-08-25 stsp return err;
2438 44392932 2019-08-25 stsp }
2439 44392932 2019-08-25 stsp
2440 44392932 2019-08-25 stsp static const struct got_error *
2441 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2442 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2443 63035f9f 2019-10-06 stsp struct got_repository *repo)
2444 44392932 2019-08-25 stsp {
2445 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2446 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2447 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2448 44392932 2019-08-25 stsp
2449 44392932 2019-08-25 stsp if (tree_id1) {
2450 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2451 79109fed 2018-03-27 stsp if (err)
2452 44392932 2019-08-25 stsp goto done;
2453 79109fed 2018-03-27 stsp }
2454 79109fed 2018-03-27 stsp
2455 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2456 0f2b3dca 2018-12-22 stsp if (err)
2457 0f2b3dca 2018-12-22 stsp goto done;
2458 0f2b3dca 2018-12-22 stsp
2459 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2460 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2461 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2462 44392932 2019-08-25 stsp while (path[0] == '/')
2463 44392932 2019-08-25 stsp path++;
2464 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2465 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2466 0f2b3dca 2018-12-22 stsp done:
2467 79109fed 2018-03-27 stsp if (tree1)
2468 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2469 366e0a5f 2019-10-10 stsp if (tree2)
2470 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2471 44392932 2019-08-25 stsp return err;
2472 44392932 2019-08-25 stsp }
2473 44392932 2019-08-25 stsp
2474 44392932 2019-08-25 stsp static const struct got_error *
2475 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2476 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2477 44392932 2019-08-25 stsp {
2478 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2479 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2480 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2481 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2482 44392932 2019-08-25 stsp struct got_object_qid *qid;
2483 44392932 2019-08-25 stsp
2484 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2485 44392932 2019-08-25 stsp if (qid != NULL) {
2486 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2487 44392932 2019-08-25 stsp qid->id);
2488 44392932 2019-08-25 stsp if (err)
2489 44392932 2019-08-25 stsp return err;
2490 44392932 2019-08-25 stsp }
2491 44392932 2019-08-25 stsp
2492 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2493 44392932 2019-08-25 stsp int obj_type;
2494 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2495 44392932 2019-08-25 stsp if (err)
2496 44392932 2019-08-25 stsp goto done;
2497 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2498 44392932 2019-08-25 stsp if (err) {
2499 44392932 2019-08-25 stsp free(obj_id2);
2500 44392932 2019-08-25 stsp goto done;
2501 44392932 2019-08-25 stsp }
2502 44392932 2019-08-25 stsp if (pcommit) {
2503 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2504 44392932 2019-08-25 stsp qid->id, path);
2505 44392932 2019-08-25 stsp if (err) {
2506 44392932 2019-08-25 stsp free(obj_id2);
2507 44392932 2019-08-25 stsp goto done;
2508 44392932 2019-08-25 stsp }
2509 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2510 44392932 2019-08-25 stsp if (err) {
2511 44392932 2019-08-25 stsp free(obj_id2);
2512 44392932 2019-08-25 stsp goto done;
2513 44392932 2019-08-25 stsp }
2514 44392932 2019-08-25 stsp }
2515 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2516 44392932 2019-08-25 stsp if (err) {
2517 44392932 2019-08-25 stsp free(obj_id2);
2518 44392932 2019-08-25 stsp goto done;
2519 44392932 2019-08-25 stsp }
2520 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2521 44392932 2019-08-25 stsp switch (obj_type) {
2522 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2523 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2524 63035f9f 2019-10-06 stsp 0, repo);
2525 44392932 2019-08-25 stsp break;
2526 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2527 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2528 63035f9f 2019-10-06 stsp 0, repo);
2529 44392932 2019-08-25 stsp break;
2530 44392932 2019-08-25 stsp default:
2531 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2532 44392932 2019-08-25 stsp break;
2533 44392932 2019-08-25 stsp }
2534 44392932 2019-08-25 stsp free(obj_id1);
2535 44392932 2019-08-25 stsp free(obj_id2);
2536 44392932 2019-08-25 stsp } else {
2537 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2538 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2539 44392932 2019-08-25 stsp if (err)
2540 44392932 2019-08-25 stsp goto done;
2541 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2542 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2543 44392932 2019-08-25 stsp if (err)
2544 44392932 2019-08-25 stsp goto done;
2545 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2546 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2547 44392932 2019-08-25 stsp }
2548 44392932 2019-08-25 stsp done:
2549 0f2b3dca 2018-12-22 stsp free(id_str1);
2550 0f2b3dca 2018-12-22 stsp free(id_str2);
2551 44392932 2019-08-25 stsp if (pcommit)
2552 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2553 79109fed 2018-03-27 stsp return err;
2554 79109fed 2018-03-27 stsp }
2555 79109fed 2018-03-27 stsp
2556 4bb494d5 2018-06-16 stsp static char *
2557 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2558 6c281f94 2018-06-11 stsp {
2559 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2560 09867e48 2019-08-13 stsp char *p, *s;
2561 09867e48 2019-08-13 stsp
2562 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2563 09867e48 2019-08-13 stsp if (tm == NULL)
2564 09867e48 2019-08-13 stsp return NULL;
2565 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2566 09867e48 2019-08-13 stsp if (s == NULL)
2567 09867e48 2019-08-13 stsp return NULL;
2568 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2569 6c281f94 2018-06-11 stsp if (p)
2570 6c281f94 2018-06-11 stsp *p = '\0';
2571 6c281f94 2018-06-11 stsp return s;
2572 6c281f94 2018-06-11 stsp }
2573 dc424a06 2019-08-07 stsp
2574 6841bf13 2019-11-29 kn static const struct got_error *
2575 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2576 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2577 6841bf13 2019-11-29 kn {
2578 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2579 6841bf13 2019-11-29 kn regmatch_t regmatch;
2580 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2581 6841bf13 2019-11-29 kn
2582 6841bf13 2019-11-29 kn *have_match = 0;
2583 6841bf13 2019-11-29 kn
2584 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2585 6841bf13 2019-11-29 kn if (err)
2586 6841bf13 2019-11-29 kn return err;
2587 6841bf13 2019-11-29 kn
2588 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2589 6841bf13 2019-11-29 kn if (err)
2590 6841bf13 2019-11-29 kn goto done;
2591 6841bf13 2019-11-29 kn
2592 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2593 6841bf13 2019-11-29 kn *have_match = 1;
2594 6841bf13 2019-11-29 kn done:
2595 6841bf13 2019-11-29 kn free(id_str);
2596 6841bf13 2019-11-29 kn free(logmsg);
2597 6841bf13 2019-11-29 kn return err;
2598 6841bf13 2019-11-29 kn }
2599 6841bf13 2019-11-29 kn
2600 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2601 6c281f94 2018-06-11 stsp
2602 79109fed 2018-03-27 stsp static const struct got_error *
2603 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2604 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2605 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2606 79109fed 2018-03-27 stsp {
2607 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2608 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2609 6c281f94 2018-06-11 stsp char datebuf[26];
2610 45d799e2 2018-12-23 stsp time_t committer_time;
2611 45d799e2 2018-12-23 stsp const char *author, *committer;
2612 199a4027 2019-02-02 stsp char *refs_str = NULL;
2613 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2614 5c860e29 2018-03-12 stsp
2615 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2616 199a4027 2019-02-02 stsp char *s;
2617 199a4027 2019-02-02 stsp const char *name;
2618 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2619 a436ad14 2019-08-13 stsp int cmp;
2620 a436ad14 2019-08-13 stsp
2621 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2622 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2623 d9498b20 2019-02-05 stsp continue;
2624 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2625 199a4027 2019-02-02 stsp name += 5;
2626 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2627 7143d404 2019-03-12 stsp continue;
2628 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2629 e34f9ed6 2019-02-02 stsp name += 6;
2630 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2631 141c2bff 2019-02-04 stsp name += 8;
2632 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2633 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2634 5d844a1e 2019-08-13 stsp if (err) {
2635 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2636 5d844a1e 2019-08-13 stsp return err;
2637 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2638 5d844a1e 2019-08-13 stsp err = NULL;
2639 5d844a1e 2019-08-13 stsp tag = NULL;
2640 5d844a1e 2019-08-13 stsp }
2641 a436ad14 2019-08-13 stsp }
2642 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2643 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2644 a436ad14 2019-08-13 stsp if (tag)
2645 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2646 a436ad14 2019-08-13 stsp if (cmp != 0)
2647 a436ad14 2019-08-13 stsp continue;
2648 199a4027 2019-02-02 stsp s = refs_str;
2649 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2650 199a4027 2019-02-02 stsp name) == -1) {
2651 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2652 199a4027 2019-02-02 stsp free(s);
2653 34ca4898 2019-08-13 stsp return err;
2654 199a4027 2019-02-02 stsp }
2655 199a4027 2019-02-02 stsp free(s);
2656 199a4027 2019-02-02 stsp }
2657 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2658 8bf5b3c9 2018-03-17 stsp if (err)
2659 8bf5b3c9 2018-03-17 stsp return err;
2660 788c352e 2018-06-16 stsp
2661 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2662 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2663 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2664 832c249c 2018-06-10 stsp free(id_str);
2665 d3d493d7 2019-02-21 stsp id_str = NULL;
2666 d3d493d7 2019-02-21 stsp free(refs_str);
2667 d3d493d7 2019-02-21 stsp refs_str = NULL;
2668 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2669 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2670 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2671 09867e48 2019-08-13 stsp if (datestr)
2672 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2673 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2674 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2675 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2676 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2677 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2678 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2679 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2680 3fe1abad 2018-06-10 stsp int n = 1;
2681 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2682 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2683 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2684 a0603db2 2018-06-10 stsp if (err)
2685 a0603db2 2018-06-10 stsp return err;
2686 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2687 a0603db2 2018-06-10 stsp free(id_str);
2688 a0603db2 2018-06-10 stsp }
2689 a0603db2 2018-06-10 stsp }
2690 832c249c 2018-06-10 stsp
2691 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2692 5943eee2 2019-08-13 stsp if (err)
2693 5943eee2 2019-08-13 stsp return err;
2694 8bf5b3c9 2018-03-17 stsp
2695 621015ac 2018-07-23 stsp logmsg = logmsg0;
2696 832c249c 2018-06-10 stsp do {
2697 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2698 832c249c 2018-06-10 stsp if (line)
2699 832c249c 2018-06-10 stsp printf(" %s\n", line);
2700 832c249c 2018-06-10 stsp } while (line);
2701 621015ac 2018-07-23 stsp free(logmsg0);
2702 832c249c 2018-06-10 stsp
2703 971751ac 2018-03-27 stsp if (show_patch) {
2704 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2705 971751ac 2018-03-27 stsp if (err == 0)
2706 971751ac 2018-03-27 stsp printf("\n");
2707 971751ac 2018-03-27 stsp }
2708 07862c20 2018-09-15 stsp
2709 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2710 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2711 07862c20 2018-09-15 stsp return err;
2712 f42b1b34 2018-03-12 stsp }
2713 5c860e29 2018-03-12 stsp
2714 f42b1b34 2018-03-12 stsp static const struct got_error *
2715 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2716 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2717 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2718 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2719 f42b1b34 2018-03-12 stsp {
2720 f42b1b34 2018-03-12 stsp const struct got_error *err;
2721 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2722 6841bf13 2019-11-29 kn regex_t regex;
2723 6841bf13 2019-11-29 kn int have_match;
2724 372ccdbb 2018-06-10 stsp
2725 6841bf13 2019-11-29 kn if (search_pattern &&
2726 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2727 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2728 6841bf13 2019-11-29 kn
2729 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2730 f42b1b34 2018-03-12 stsp if (err)
2731 f42b1b34 2018-03-12 stsp return err;
2732 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
2733 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2734 372ccdbb 2018-06-10 stsp if (err)
2735 fcc85cad 2018-07-22 stsp goto done;
2736 656b1f76 2019-05-11 jcs for (;;) {
2737 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
2738 372ccdbb 2018-06-10 stsp struct got_object_id *id;
2739 8bf5b3c9 2018-03-17 stsp
2740 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2741 84453469 2018-11-11 stsp break;
2742 84453469 2018-11-11 stsp
2743 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2744 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
2745 372ccdbb 2018-06-10 stsp if (err) {
2746 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2747 9ba79e04 2018-06-11 stsp err = NULL;
2748 ee780d5c 2020-01-04 stsp break;
2749 7e665116 2018-04-02 stsp }
2750 b43fbaa0 2018-06-11 stsp if (id == NULL)
2751 7e665116 2018-04-02 stsp break;
2752 b43fbaa0 2018-06-11 stsp
2753 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
2754 b43fbaa0 2018-06-11 stsp if (err)
2755 fcc85cad 2018-07-22 stsp break;
2756 6841bf13 2019-11-29 kn
2757 6841bf13 2019-11-29 kn if (search_pattern) {
2758 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
2759 6841bf13 2019-11-29 kn if (err) {
2760 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2761 6841bf13 2019-11-29 kn break;
2762 6841bf13 2019-11-29 kn }
2763 6841bf13 2019-11-29 kn if (have_match == 0) {
2764 6841bf13 2019-11-29 kn got_object_commit_close(commit);
2765 6841bf13 2019-11-29 kn continue;
2766 6841bf13 2019-11-29 kn }
2767 6841bf13 2019-11-29 kn }
2768 6841bf13 2019-11-29 kn
2769 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
2770 44392932 2019-08-25 stsp diff_context, refs);
2771 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
2772 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
2773 7e665116 2018-04-02 stsp break;
2774 31cedeaf 2018-09-15 stsp }
2775 fcc85cad 2018-07-22 stsp done:
2776 6841bf13 2019-11-29 kn if (search_pattern)
2777 6841bf13 2019-11-29 kn regfree(&regex);
2778 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2779 f42b1b34 2018-03-12 stsp return err;
2780 f42b1b34 2018-03-12 stsp }
2781 5c860e29 2018-03-12 stsp
2782 4ed7e80c 2018-05-20 stsp __dead static void
2783 6f3d1eb0 2018-03-12 stsp usage_log(void)
2784 6f3d1eb0 2018-03-12 stsp {
2785 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2786 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2787 6f3d1eb0 2018-03-12 stsp exit(1);
2788 b1ebc001 2019-08-13 stsp }
2789 b1ebc001 2019-08-13 stsp
2790 b1ebc001 2019-08-13 stsp static int
2791 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2792 b1ebc001 2019-08-13 stsp {
2793 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2794 b1ebc001 2019-08-13 stsp long long n;
2795 b1ebc001 2019-08-13 stsp const char *errstr;
2796 b1ebc001 2019-08-13 stsp
2797 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2798 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2799 b1ebc001 2019-08-13 stsp return 0;
2800 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2801 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2802 b1ebc001 2019-08-13 stsp return 0;
2803 b1ebc001 2019-08-13 stsp return n;
2804 6f3d1eb0 2018-03-12 stsp }
2805 6f3d1eb0 2018-03-12 stsp
2806 4ed7e80c 2018-05-20 stsp static const struct got_error *
2807 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2808 f42b1b34 2018-03-12 stsp {
2809 f42b1b34 2018-03-12 stsp const struct got_error *error;
2810 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2811 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2812 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2813 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2814 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2815 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2816 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2817 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2818 64a96a6d 2018-04-01 stsp const char *errstr;
2819 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2820 1b3893a2 2019-03-18 stsp
2821 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2822 5c860e29 2018-03-12 stsp
2823 6715a751 2018-03-16 stsp #ifndef PROFILE
2824 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2825 6098196c 2019-01-04 stsp NULL)
2826 ad242220 2018-09-08 stsp == -1)
2827 f42b1b34 2018-03-12 stsp err(1, "pledge");
2828 6715a751 2018-03-16 stsp #endif
2829 79109fed 2018-03-27 stsp
2830 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2831 b1ebc001 2019-08-13 stsp
2832 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2833 79109fed 2018-03-27 stsp switch (ch) {
2834 79109fed 2018-03-27 stsp case 'p':
2835 79109fed 2018-03-27 stsp show_patch = 1;
2836 d142fc45 2018-04-01 stsp break;
2837 d142fc45 2018-04-01 stsp case 'c':
2838 d142fc45 2018-04-01 stsp start_commit = optarg;
2839 64a96a6d 2018-04-01 stsp break;
2840 c0cc5c62 2018-10-18 stsp case 'C':
2841 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2842 4a8520aa 2018-10-18 stsp &errstr);
2843 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2844 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2845 c0cc5c62 2018-10-18 stsp break;
2846 64a96a6d 2018-04-01 stsp case 'l':
2847 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2848 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2849 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2850 cc54c501 2019-07-15 stsp break;
2851 48c8c60d 2020-01-27 stsp case 'b':
2852 48c8c60d 2020-01-27 stsp log_branches = 1;
2853 a0603db2 2018-06-10 stsp break;
2854 04ca23f4 2018-07-16 stsp case 'r':
2855 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2856 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2857 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2858 9ba1d308 2019-10-21 stsp optarg);
2859 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2860 04ca23f4 2018-07-16 stsp break;
2861 6841bf13 2019-11-29 kn case 's':
2862 6841bf13 2019-11-29 kn search_pattern = optarg;
2863 6841bf13 2019-11-29 kn break;
2864 79109fed 2018-03-27 stsp default:
2865 2deda0b9 2019-03-07 stsp usage_log();
2866 79109fed 2018-03-27 stsp /* NOTREACHED */
2867 79109fed 2018-03-27 stsp }
2868 79109fed 2018-03-27 stsp }
2869 79109fed 2018-03-27 stsp
2870 79109fed 2018-03-27 stsp argc -= optind;
2871 79109fed 2018-03-27 stsp argv += optind;
2872 f42b1b34 2018-03-12 stsp
2873 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2874 dc1edbfa 2019-11-29 kn diff_context = 3;
2875 dc1edbfa 2019-11-29 kn else if (!show_patch)
2876 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2877 dc1edbfa 2019-11-29 kn
2878 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2879 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2880 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2881 04ca23f4 2018-07-16 stsp goto done;
2882 04ca23f4 2018-07-16 stsp }
2883 cffc0aa4 2019-02-05 stsp
2884 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2885 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2886 cffc0aa4 2019-02-05 stsp goto done;
2887 cffc0aa4 2019-02-05 stsp error = NULL;
2888 cffc0aa4 2019-02-05 stsp
2889 e7301579 2019-03-18 stsp if (argc == 0) {
2890 cbd1af7a 2019-03-18 stsp path = strdup("");
2891 e7301579 2019-03-18 stsp if (path == NULL) {
2892 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2893 cbd1af7a 2019-03-18 stsp goto done;
2894 e7301579 2019-03-18 stsp }
2895 e7301579 2019-03-18 stsp } else if (argc == 1) {
2896 e7301579 2019-03-18 stsp if (worktree) {
2897 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2898 e7301579 2019-03-18 stsp argv[0]);
2899 e7301579 2019-03-18 stsp if (error)
2900 e7301579 2019-03-18 stsp goto done;
2901 e7301579 2019-03-18 stsp } else {
2902 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2903 e7301579 2019-03-18 stsp if (path == NULL) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2905 e7301579 2019-03-18 stsp goto done;
2906 e7301579 2019-03-18 stsp }
2907 e7301579 2019-03-18 stsp }
2908 cbd1af7a 2019-03-18 stsp } else
2909 cbd1af7a 2019-03-18 stsp usage_log();
2910 cbd1af7a 2019-03-18 stsp
2911 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2912 5486daa2 2019-05-11 stsp repo_path = worktree ?
2913 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2914 5486daa2 2019-05-11 stsp }
2915 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2916 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2917 cffc0aa4 2019-02-05 stsp goto done;
2918 04ca23f4 2018-07-16 stsp }
2919 6098196c 2019-01-04 stsp
2920 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2921 d7d4f210 2018-03-12 stsp if (error != NULL)
2922 04ca23f4 2018-07-16 stsp goto done;
2923 f42b1b34 2018-03-12 stsp
2924 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2925 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2926 c02c541e 2019-03-29 stsp if (error)
2927 c02c541e 2019-03-29 stsp goto done;
2928 c02c541e 2019-03-29 stsp
2929 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2930 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2931 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2932 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2933 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2934 3235492e 2018-04-01 stsp if (error != NULL)
2935 3235492e 2018-04-01 stsp return error;
2936 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2937 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2938 3235492e 2018-04-01 stsp if (error != NULL)
2939 3235492e 2018-04-01 stsp return error;
2940 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2941 3235492e 2018-04-01 stsp } else {
2942 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2943 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2944 3235492e 2018-04-01 stsp if (error == NULL) {
2945 1caad61b 2019-02-01 stsp int obj_type;
2946 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2947 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2948 d1f2edc9 2018-06-13 stsp if (error != NULL)
2949 1caad61b 2019-02-01 stsp goto done;
2950 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2951 1caad61b 2019-02-01 stsp if (error != NULL)
2952 1caad61b 2019-02-01 stsp goto done;
2953 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2954 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2955 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2956 1caad61b 2019-02-01 stsp if (error != NULL)
2957 1caad61b 2019-02-01 stsp goto done;
2958 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2959 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2960 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2961 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2962 1caad61b 2019-02-01 stsp goto done;
2963 1caad61b 2019-02-01 stsp }
2964 1caad61b 2019-02-01 stsp free(id);
2965 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2966 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2967 1caad61b 2019-02-01 stsp if (id == NULL)
2968 638f9024 2019-05-13 stsp error = got_error_from_errno(
2969 230a42bd 2019-05-11 jcs "got_object_id_dup");
2970 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2971 1caad61b 2019-02-01 stsp if (error)
2972 1caad61b 2019-02-01 stsp goto done;
2973 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2974 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2975 1caad61b 2019-02-01 stsp goto done;
2976 1caad61b 2019-02-01 stsp }
2977 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2978 d1f2edc9 2018-06-13 stsp if (error != NULL)
2979 1caad61b 2019-02-01 stsp goto done;
2980 d1f2edc9 2018-06-13 stsp }
2981 15a94983 2018-12-23 stsp if (commit == NULL) {
2982 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2983 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2984 d1f2edc9 2018-06-13 stsp if (error != NULL)
2985 d1f2edc9 2018-06-13 stsp return error;
2986 3235492e 2018-04-01 stsp }
2987 3235492e 2018-04-01 stsp }
2988 d7d4f210 2018-03-12 stsp if (error != NULL)
2989 04ca23f4 2018-07-16 stsp goto done;
2990 04ca23f4 2018-07-16 stsp
2991 deeabeae 2019-08-27 stsp if (worktree) {
2992 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2993 deeabeae 2019-08-27 stsp char *p;
2994 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2995 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2996 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2997 deeabeae 2019-08-27 stsp goto done;
2998 deeabeae 2019-08-27 stsp }
2999 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3000 deeabeae 2019-08-27 stsp free(p);
3001 deeabeae 2019-08-27 stsp } else
3002 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3003 04ca23f4 2018-07-16 stsp if (error != NULL)
3004 04ca23f4 2018-07-16 stsp goto done;
3005 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3006 04ca23f4 2018-07-16 stsp free(path);
3007 04ca23f4 2018-07-16 stsp path = in_repo_path;
3008 04ca23f4 2018-07-16 stsp }
3009 199a4027 2019-02-02 stsp
3010 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3011 199a4027 2019-02-02 stsp if (error)
3012 199a4027 2019-02-02 stsp goto done;
3013 04ca23f4 2018-07-16 stsp
3014 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3015 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3016 04ca23f4 2018-07-16 stsp done:
3017 04ca23f4 2018-07-16 stsp free(path);
3018 04ca23f4 2018-07-16 stsp free(repo_path);
3019 04ca23f4 2018-07-16 stsp free(cwd);
3020 f42b1b34 2018-03-12 stsp free(id);
3021 cffc0aa4 2019-02-05 stsp if (worktree)
3022 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3023 ad242220 2018-09-08 stsp if (repo) {
3024 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3025 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3026 ad242220 2018-09-08 stsp if (error == NULL)
3027 ad242220 2018-09-08 stsp error = repo_error;
3028 ad242220 2018-09-08 stsp }
3029 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3030 8bf5b3c9 2018-03-17 stsp return error;
3031 5c860e29 2018-03-12 stsp }
3032 5c860e29 2018-03-12 stsp
3033 4ed7e80c 2018-05-20 stsp __dead static void
3034 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3035 3f8b7d6a 2018-04-01 stsp {
3036 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3037 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3038 3f8b7d6a 2018-04-01 stsp exit(1);
3039 b00d56cd 2018-04-01 stsp }
3040 b00d56cd 2018-04-01 stsp
3041 b72f483a 2019-02-05 stsp struct print_diff_arg {
3042 b72f483a 2019-02-05 stsp struct got_repository *repo;
3043 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3044 b72f483a 2019-02-05 stsp int diff_context;
3045 3fc0c068 2019-02-10 stsp const char *id_str;
3046 3fc0c068 2019-02-10 stsp int header_shown;
3047 98eaaa12 2019-08-03 stsp int diff_staged;
3048 63035f9f 2019-10-06 stsp int ignore_whitespace;
3049 b72f483a 2019-02-05 stsp };
3050 b72f483a 2019-02-05 stsp
3051 4ed7e80c 2018-05-20 stsp static const struct got_error *
3052 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3053 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3054 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3055 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3056 b72f483a 2019-02-05 stsp {
3057 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3058 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3059 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3060 12463d8b 2019-12-13 stsp int fd = -1;
3061 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3062 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3063 b72f483a 2019-02-05 stsp struct stat sb;
3064 b72f483a 2019-02-05 stsp
3065 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3066 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3067 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3068 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3069 98eaaa12 2019-08-03 stsp return NULL;
3070 98eaaa12 2019-08-03 stsp } else {
3071 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3072 98eaaa12 2019-08-03 stsp return NULL;
3073 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3074 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3075 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3076 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3077 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3078 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3079 98eaaa12 2019-08-03 stsp return NULL;
3080 98eaaa12 2019-08-03 stsp }
3081 3fc0c068 2019-02-10 stsp
3082 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3083 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3084 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3085 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3086 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3087 3fc0c068 2019-02-10 stsp }
3088 b72f483a 2019-02-05 stsp
3089 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3090 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3091 98eaaa12 2019-08-03 stsp switch (staged_status) {
3092 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3093 98eaaa12 2019-08-03 stsp label1 = path;
3094 98eaaa12 2019-08-03 stsp label2 = path;
3095 98eaaa12 2019-08-03 stsp break;
3096 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3097 98eaaa12 2019-08-03 stsp label2 = path;
3098 98eaaa12 2019-08-03 stsp break;
3099 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3100 98eaaa12 2019-08-03 stsp label1 = path;
3101 98eaaa12 2019-08-03 stsp break;
3102 98eaaa12 2019-08-03 stsp default:
3103 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3104 98eaaa12 2019-08-03 stsp }
3105 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3106 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3107 63035f9f 2019-10-06 stsp a->repo, stdout);
3108 98eaaa12 2019-08-03 stsp }
3109 98eaaa12 2019-08-03 stsp
3110 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3111 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3112 4ce46740 2019-08-08 stsp char *id_str;
3113 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3114 408b4ebc 2019-08-03 stsp 8192);
3115 4ce46740 2019-08-08 stsp if (err)
3116 4ce46740 2019-08-08 stsp goto done;
3117 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3118 4ce46740 2019-08-08 stsp if (err)
3119 4ce46740 2019-08-08 stsp goto done;
3120 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3121 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3122 4ce46740 2019-08-08 stsp free(id_str);
3123 4ce46740 2019-08-08 stsp goto done;
3124 4ce46740 2019-08-08 stsp }
3125 4ce46740 2019-08-08 stsp free(id_str);
3126 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3127 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3128 4ce46740 2019-08-08 stsp if (err)
3129 4ce46740 2019-08-08 stsp goto done;
3130 4ce46740 2019-08-08 stsp }
3131 d00136be 2019-03-26 stsp
3132 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3133 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3134 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3135 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3136 2ec1f75b 2019-03-26 stsp goto done;
3137 2ec1f75b 2019-03-26 stsp }
3138 b72f483a 2019-02-05 stsp
3139 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3140 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3141 12463d8b 2019-12-13 stsp if (fd == -1) {
3142 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3143 12463d8b 2019-12-13 stsp goto done;
3144 12463d8b 2019-12-13 stsp }
3145 12463d8b 2019-12-13 stsp } else {
3146 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3147 12463d8b 2019-12-13 stsp if (fd == -1) {
3148 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3149 12463d8b 2019-12-13 stsp goto done;
3150 12463d8b 2019-12-13 stsp }
3151 12463d8b 2019-12-13 stsp }
3152 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3153 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3154 2ec1f75b 2019-03-26 stsp goto done;
3155 2ec1f75b 2019-03-26 stsp }
3156 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3157 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3158 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3159 2ec1f75b 2019-03-26 stsp goto done;
3160 2ec1f75b 2019-03-26 stsp }
3161 12463d8b 2019-12-13 stsp fd = -1;
3162 2ec1f75b 2019-03-26 stsp } else
3163 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3164 b72f483a 2019-02-05 stsp
3165 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3166 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3167 b72f483a 2019-02-05 stsp done:
3168 b72f483a 2019-02-05 stsp if (blob1)
3169 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3170 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3171 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3172 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3173 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3174 b72f483a 2019-02-05 stsp free(abspath);
3175 927df6b7 2019-02-10 stsp return err;
3176 927df6b7 2019-02-10 stsp }
3177 927df6b7 2019-02-10 stsp
3178 927df6b7 2019-02-10 stsp static const struct got_error *
3179 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3180 b00d56cd 2018-04-01 stsp {
3181 b00d56cd 2018-04-01 stsp const struct got_error *error;
3182 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3183 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3184 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3185 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3186 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3187 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3188 15a94983 2018-12-23 stsp int type1, type2;
3189 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3190 c0cc5c62 2018-10-18 stsp const char *errstr;
3191 927df6b7 2019-02-10 stsp char *path = NULL;
3192 b00d56cd 2018-04-01 stsp
3193 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3194 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3195 25eccc22 2019-01-04 stsp NULL) == -1)
3196 b00d56cd 2018-04-01 stsp err(1, "pledge");
3197 b00d56cd 2018-04-01 stsp #endif
3198 b00d56cd 2018-04-01 stsp
3199 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3200 b00d56cd 2018-04-01 stsp switch (ch) {
3201 c0cc5c62 2018-10-18 stsp case 'C':
3202 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3203 dfcab68b 2019-11-29 kn &errstr);
3204 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3205 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3206 c0cc5c62 2018-10-18 stsp break;
3207 b72f483a 2019-02-05 stsp case 'r':
3208 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3209 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3210 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3211 9ba1d308 2019-10-21 stsp optarg);
3212 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3213 b72f483a 2019-02-05 stsp break;
3214 98eaaa12 2019-08-03 stsp case 's':
3215 98eaaa12 2019-08-03 stsp diff_staged = 1;
3216 63035f9f 2019-10-06 stsp break;
3217 63035f9f 2019-10-06 stsp case 'w':
3218 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3219 98eaaa12 2019-08-03 stsp break;
3220 b00d56cd 2018-04-01 stsp default:
3221 2deda0b9 2019-03-07 stsp usage_diff();
3222 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3223 b00d56cd 2018-04-01 stsp }
3224 b00d56cd 2018-04-01 stsp }
3225 b00d56cd 2018-04-01 stsp
3226 b00d56cd 2018-04-01 stsp argc -= optind;
3227 b00d56cd 2018-04-01 stsp argv += optind;
3228 b00d56cd 2018-04-01 stsp
3229 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3230 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3231 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3232 b72f483a 2019-02-05 stsp goto done;
3233 b72f483a 2019-02-05 stsp }
3234 927df6b7 2019-02-10 stsp if (argc <= 1) {
3235 b72f483a 2019-02-05 stsp if (repo_path)
3236 b72f483a 2019-02-05 stsp errx(1,
3237 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3238 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3239 1f03b8da 2020-03-20 stsp if (error)
3240 1f03b8da 2020-03-20 stsp goto done;
3241 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3242 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3243 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3244 927df6b7 2019-02-10 stsp goto done;
3245 927df6b7 2019-02-10 stsp }
3246 927df6b7 2019-02-10 stsp if (argc == 1) {
3247 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3248 cbd1af7a 2019-03-18 stsp argv[0]);
3249 927df6b7 2019-02-10 stsp if (error)
3250 927df6b7 2019-02-10 stsp goto done;
3251 927df6b7 2019-02-10 stsp } else {
3252 927df6b7 2019-02-10 stsp path = strdup("");
3253 927df6b7 2019-02-10 stsp if (path == NULL) {
3254 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3255 927df6b7 2019-02-10 stsp goto done;
3256 927df6b7 2019-02-10 stsp }
3257 927df6b7 2019-02-10 stsp }
3258 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3259 98eaaa12 2019-08-03 stsp if (diff_staged)
3260 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3261 98eaaa12 2019-08-03 stsp "objects in repository");
3262 15a94983 2018-12-23 stsp id_str1 = argv[0];
3263 15a94983 2018-12-23 stsp id_str2 = argv[1];
3264 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3265 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3266 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3267 30db809c 2019-06-05 stsp goto done;
3268 1f03b8da 2020-03-20 stsp if (worktree) {
3269 1f03b8da 2020-03-20 stsp repo_path = strdup(
3270 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3271 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3272 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3273 1f03b8da 2020-03-20 stsp goto done;
3274 1f03b8da 2020-03-20 stsp }
3275 1f03b8da 2020-03-20 stsp } else {
3276 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3277 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3278 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3279 1f03b8da 2020-03-20 stsp goto done;
3280 1f03b8da 2020-03-20 stsp }
3281 30db809c 2019-06-05 stsp }
3282 30db809c 2019-06-05 stsp }
3283 b00d56cd 2018-04-01 stsp } else
3284 b00d56cd 2018-04-01 stsp usage_diff();
3285 b00d56cd 2018-04-01 stsp
3286 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3287 76089277 2018-04-01 stsp free(repo_path);
3288 b00d56cd 2018-04-01 stsp if (error != NULL)
3289 b00d56cd 2018-04-01 stsp goto done;
3290 b00d56cd 2018-04-01 stsp
3291 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3292 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3293 c02c541e 2019-03-29 stsp if (error)
3294 c02c541e 2019-03-29 stsp goto done;
3295 c02c541e 2019-03-29 stsp
3296 30db809c 2019-06-05 stsp if (argc <= 1) {
3297 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3298 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3299 b72f483a 2019-02-05 stsp char *id_str;
3300 72ea6654 2019-07-27 stsp
3301 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3302 72ea6654 2019-07-27 stsp
3303 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3304 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3305 b72f483a 2019-02-05 stsp if (error)
3306 b72f483a 2019-02-05 stsp goto done;
3307 b72f483a 2019-02-05 stsp arg.repo = repo;
3308 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3309 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3310 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3311 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3312 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3313 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3314 b72f483a 2019-02-05 stsp
3315 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3316 72ea6654 2019-07-27 stsp if (error)
3317 72ea6654 2019-07-27 stsp goto done;
3318 72ea6654 2019-07-27 stsp
3319 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3320 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3321 b72f483a 2019-02-05 stsp free(id_str);
3322 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3323 b72f483a 2019-02-05 stsp goto done;
3324 b72f483a 2019-02-05 stsp }
3325 b72f483a 2019-02-05 stsp
3326 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3327 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3328 0adb7196 2019-08-11 stsp if (error)
3329 0adb7196 2019-08-11 stsp goto done;
3330 b00d56cd 2018-04-01 stsp
3331 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3332 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3333 0adb7196 2019-08-11 stsp if (error)
3334 0adb7196 2019-08-11 stsp goto done;
3335 b00d56cd 2018-04-01 stsp
3336 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3337 15a94983 2018-12-23 stsp if (error)
3338 15a94983 2018-12-23 stsp goto done;
3339 15a94983 2018-12-23 stsp
3340 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3341 15a94983 2018-12-23 stsp if (error)
3342 15a94983 2018-12-23 stsp goto done;
3343 15a94983 2018-12-23 stsp
3344 15a94983 2018-12-23 stsp if (type1 != type2) {
3345 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3346 b00d56cd 2018-04-01 stsp goto done;
3347 b00d56cd 2018-04-01 stsp }
3348 b00d56cd 2018-04-01 stsp
3349 15a94983 2018-12-23 stsp switch (type1) {
3350 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3351 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3352 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3353 b00d56cd 2018-04-01 stsp break;
3354 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3355 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3356 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3357 b00d56cd 2018-04-01 stsp break;
3358 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3359 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3360 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3361 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3362 b00d56cd 2018-04-01 stsp break;
3363 b00d56cd 2018-04-01 stsp default:
3364 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3365 b00d56cd 2018-04-01 stsp }
3366 b00d56cd 2018-04-01 stsp done:
3367 5e70831e 2019-05-28 stsp free(label1);
3368 5e70831e 2019-05-28 stsp free(label2);
3369 15a94983 2018-12-23 stsp free(id1);
3370 15a94983 2018-12-23 stsp free(id2);
3371 927df6b7 2019-02-10 stsp free(path);
3372 b72f483a 2019-02-05 stsp if (worktree)
3373 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3374 ad242220 2018-09-08 stsp if (repo) {
3375 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3376 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3377 ad242220 2018-09-08 stsp if (error == NULL)
3378 ad242220 2018-09-08 stsp error = repo_error;
3379 ad242220 2018-09-08 stsp }
3380 b00d56cd 2018-04-01 stsp return error;
3381 404c43c4 2018-06-21 stsp }
3382 404c43c4 2018-06-21 stsp
3383 404c43c4 2018-06-21 stsp __dead static void
3384 404c43c4 2018-06-21 stsp usage_blame(void)
3385 404c43c4 2018-06-21 stsp {
3386 9270e621 2019-02-05 stsp fprintf(stderr,
3387 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3388 404c43c4 2018-06-21 stsp getprogname());
3389 404c43c4 2018-06-21 stsp exit(1);
3390 b00d56cd 2018-04-01 stsp }
3391 b00d56cd 2018-04-01 stsp
3392 28315671 2019-08-14 stsp struct blame_line {
3393 28315671 2019-08-14 stsp int annotated;
3394 28315671 2019-08-14 stsp char *id_str;
3395 82f6abb8 2019-08-14 stsp char *committer;
3396 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3397 28315671 2019-08-14 stsp };
3398 28315671 2019-08-14 stsp
3399 28315671 2019-08-14 stsp struct blame_cb_args {
3400 28315671 2019-08-14 stsp struct blame_line *lines;
3401 28315671 2019-08-14 stsp int nlines;
3402 7ef28ff8 2019-08-14 stsp int nlines_prec;
3403 28315671 2019-08-14 stsp int lineno_cur;
3404 28315671 2019-08-14 stsp off_t *line_offsets;
3405 28315671 2019-08-14 stsp FILE *f;
3406 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3407 28315671 2019-08-14 stsp };
3408 28315671 2019-08-14 stsp
3409 404c43c4 2018-06-21 stsp static const struct got_error *
3410 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3411 28315671 2019-08-14 stsp {
3412 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3413 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3414 28315671 2019-08-14 stsp struct blame_line *bline;
3415 82f6abb8 2019-08-14 stsp char *line = NULL;
3416 28315671 2019-08-14 stsp size_t linesize = 0;
3417 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3418 28315671 2019-08-14 stsp off_t offset;
3419 bcb49d15 2019-08-14 stsp struct tm tm;
3420 bcb49d15 2019-08-14 stsp time_t committer_time;
3421 28315671 2019-08-14 stsp
3422 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3423 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3424 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3425 28315671 2019-08-14 stsp
3426 28315671 2019-08-14 stsp if (sigint_received)
3427 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3428 28315671 2019-08-14 stsp
3429 2839f8b9 2019-08-18 stsp if (lineno == -1)
3430 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3431 2839f8b9 2019-08-18 stsp
3432 28315671 2019-08-14 stsp /* Annotate this line. */
3433 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3434 28315671 2019-08-14 stsp if (bline->annotated)
3435 28315671 2019-08-14 stsp return NULL;
3436 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3437 28315671 2019-08-14 stsp if (err)
3438 28315671 2019-08-14 stsp return err;
3439 82f6abb8 2019-08-14 stsp
3440 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3441 82f6abb8 2019-08-14 stsp if (err)
3442 82f6abb8 2019-08-14 stsp goto done;
3443 82f6abb8 2019-08-14 stsp
3444 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3445 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3446 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3447 bcb49d15 2019-08-14 stsp goto done;
3448 bcb49d15 2019-08-14 stsp }
3449 bcb49d15 2019-08-14 stsp
3450 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3451 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3452 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3453 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3454 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3455 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3456 82f6abb8 2019-08-14 stsp goto done;
3457 82f6abb8 2019-08-14 stsp }
3458 28315671 2019-08-14 stsp bline->annotated = 1;
3459 28315671 2019-08-14 stsp
3460 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3461 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3462 28315671 2019-08-14 stsp if (!bline->annotated)
3463 82f6abb8 2019-08-14 stsp goto done;
3464 28315671 2019-08-14 stsp
3465 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3466 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3467 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3468 82f6abb8 2019-08-14 stsp goto done;
3469 82f6abb8 2019-08-14 stsp }
3470 28315671 2019-08-14 stsp
3471 28315671 2019-08-14 stsp while (bline->annotated) {
3472 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3473 82f6abb8 2019-08-14 stsp size_t len;
3474 82f6abb8 2019-08-14 stsp
3475 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3476 28315671 2019-08-14 stsp if (ferror(a->f))
3477 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3478 28315671 2019-08-14 stsp break;
3479 28315671 2019-08-14 stsp }
3480 82f6abb8 2019-08-14 stsp
3481 82f6abb8 2019-08-14 stsp committer = bline->committer;
3482 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3483 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3484 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3485 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3486 82f6abb8 2019-08-14 stsp if (at)
3487 82f6abb8 2019-08-14 stsp *at = '\0';
3488 82f6abb8 2019-08-14 stsp len = strlen(committer);
3489 82f6abb8 2019-08-14 stsp if (len >= 9)
3490 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3491 28315671 2019-08-14 stsp
3492 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3493 28315671 2019-08-14 stsp if (nl)
3494 28315671 2019-08-14 stsp *nl = '\0';
3495 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3496 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3497 28315671 2019-08-14 stsp
3498 28315671 2019-08-14 stsp a->lineno_cur++;
3499 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3500 28315671 2019-08-14 stsp }
3501 82f6abb8 2019-08-14 stsp done:
3502 82f6abb8 2019-08-14 stsp if (commit)
3503 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3504 28315671 2019-08-14 stsp free(line);
3505 28315671 2019-08-14 stsp return err;
3506 28315671 2019-08-14 stsp }
3507 28315671 2019-08-14 stsp
3508 28315671 2019-08-14 stsp static const struct got_error *
3509 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3510 404c43c4 2018-06-21 stsp {
3511 404c43c4 2018-06-21 stsp const struct got_error *error;
3512 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3513 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3514 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3515 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3516 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3517 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3518 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3519 28315671 2019-08-14 stsp struct blame_cb_args bca;
3520 28315671 2019-08-14 stsp int ch, obj_type, i;
3521 b02560ec 2019-08-19 stsp size_t filesize;
3522 90f3c347 2019-08-19 stsp
3523 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3524 404c43c4 2018-06-21 stsp
3525 404c43c4 2018-06-21 stsp #ifndef PROFILE
3526 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3527 36e2fb66 2019-01-04 stsp NULL) == -1)
3528 404c43c4 2018-06-21 stsp err(1, "pledge");
3529 404c43c4 2018-06-21 stsp #endif
3530 404c43c4 2018-06-21 stsp
3531 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3532 404c43c4 2018-06-21 stsp switch (ch) {
3533 404c43c4 2018-06-21 stsp case 'c':
3534 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3535 404c43c4 2018-06-21 stsp break;
3536 66bea077 2018-08-02 stsp case 'r':
3537 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3538 66bea077 2018-08-02 stsp if (repo_path == NULL)
3539 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3540 9ba1d308 2019-10-21 stsp optarg);
3541 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3542 66bea077 2018-08-02 stsp break;
3543 404c43c4 2018-06-21 stsp default:
3544 2deda0b9 2019-03-07 stsp usage_blame();
3545 404c43c4 2018-06-21 stsp /* NOTREACHED */
3546 404c43c4 2018-06-21 stsp }
3547 404c43c4 2018-06-21 stsp }
3548 404c43c4 2018-06-21 stsp
3549 404c43c4 2018-06-21 stsp argc -= optind;
3550 404c43c4 2018-06-21 stsp argv += optind;
3551 404c43c4 2018-06-21 stsp
3552 a39318fd 2018-08-02 stsp if (argc == 1)
3553 404c43c4 2018-06-21 stsp path = argv[0];
3554 a39318fd 2018-08-02 stsp else
3555 404c43c4 2018-06-21 stsp usage_blame();
3556 404c43c4 2018-06-21 stsp
3557 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3558 66bea077 2018-08-02 stsp if (cwd == NULL) {
3559 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3560 66bea077 2018-08-02 stsp goto done;
3561 66bea077 2018-08-02 stsp }
3562 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3563 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3564 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3565 66bea077 2018-08-02 stsp goto done;
3566 0c06baac 2019-02-05 stsp else
3567 0c06baac 2019-02-05 stsp error = NULL;
3568 0c06baac 2019-02-05 stsp if (worktree) {
3569 0c06baac 2019-02-05 stsp repo_path =
3570 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3571 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3572 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3573 1f03b8da 2020-03-20 stsp if (error)
3574 1f03b8da 2020-03-20 stsp goto done;
3575 1f03b8da 2020-03-20 stsp }
3576 0c06baac 2019-02-05 stsp } else {
3577 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3578 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3579 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3580 0c06baac 2019-02-05 stsp goto done;
3581 0c06baac 2019-02-05 stsp }
3582 66bea077 2018-08-02 stsp }
3583 66bea077 2018-08-02 stsp }
3584 36e2fb66 2019-01-04 stsp
3585 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3586 c02c541e 2019-03-29 stsp if (error != NULL)
3587 36e2fb66 2019-01-04 stsp goto done;
3588 66bea077 2018-08-02 stsp
3589 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3590 c02c541e 2019-03-29 stsp if (error)
3591 404c43c4 2018-06-21 stsp goto done;
3592 404c43c4 2018-06-21 stsp
3593 0c06baac 2019-02-05 stsp if (worktree) {
3594 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3595 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3596 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3597 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3598 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3599 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3600 6efaaa2d 2019-02-05 stsp path) == -1) {
3601 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3602 0c06baac 2019-02-05 stsp goto done;
3603 0c06baac 2019-02-05 stsp }
3604 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3605 0c06baac 2019-02-05 stsp free(p);
3606 0c06baac 2019-02-05 stsp } else {
3607 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3608 0c06baac 2019-02-05 stsp }
3609 0c06baac 2019-02-05 stsp if (error)
3610 66bea077 2018-08-02 stsp goto done;
3611 66bea077 2018-08-02 stsp
3612 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3613 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3614 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3615 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3616 404c43c4 2018-06-21 stsp if (error != NULL)
3617 66bea077 2018-08-02 stsp goto done;
3618 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3619 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3620 404c43c4 2018-06-21 stsp if (error != NULL)
3621 66bea077 2018-08-02 stsp goto done;
3622 404c43c4 2018-06-21 stsp } else {
3623 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3624 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3625 30837e32 2019-07-25 stsp if (error)
3626 66bea077 2018-08-02 stsp goto done;
3627 404c43c4 2018-06-21 stsp }
3628 404c43c4 2018-06-21 stsp
3629 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3630 28315671 2019-08-14 stsp if (error)
3631 28315671 2019-08-14 stsp goto done;
3632 28315671 2019-08-14 stsp
3633 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3634 28315671 2019-08-14 stsp if (error)
3635 28315671 2019-08-14 stsp goto done;
3636 28315671 2019-08-14 stsp
3637 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3638 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3639 28315671 2019-08-14 stsp goto done;
3640 28315671 2019-08-14 stsp }
3641 28315671 2019-08-14 stsp
3642 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3643 28315671 2019-08-14 stsp if (error)
3644 28315671 2019-08-14 stsp goto done;
3645 28315671 2019-08-14 stsp bca.f = got_opentemp();
3646 28315671 2019-08-14 stsp if (bca.f == NULL) {
3647 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3648 28315671 2019-08-14 stsp goto done;
3649 28315671 2019-08-14 stsp }
3650 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3651 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3652 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3653 28315671 2019-08-14 stsp goto done;
3654 28315671 2019-08-14 stsp
3655 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3656 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3657 b02560ec 2019-08-19 stsp bca.nlines--;
3658 b02560ec 2019-08-19 stsp
3659 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3660 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3661 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3662 28315671 2019-08-14 stsp goto done;
3663 28315671 2019-08-14 stsp }
3664 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3665 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3666 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3667 7ef28ff8 2019-08-14 stsp while (i > 0) {
3668 7ef28ff8 2019-08-14 stsp i /= 10;
3669 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3670 7ef28ff8 2019-08-14 stsp }
3671 82f6abb8 2019-08-14 stsp bca.repo = repo;
3672 7ef28ff8 2019-08-14 stsp
3673 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3674 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3675 404c43c4 2018-06-21 stsp done:
3676 66bea077 2018-08-02 stsp free(in_repo_path);
3677 66bea077 2018-08-02 stsp free(repo_path);
3678 66bea077 2018-08-02 stsp free(cwd);
3679 404c43c4 2018-06-21 stsp free(commit_id);
3680 28315671 2019-08-14 stsp free(obj_id);
3681 28315671 2019-08-14 stsp if (blob)
3682 28315671 2019-08-14 stsp got_object_blob_close(blob);
3683 0c06baac 2019-02-05 stsp if (worktree)
3684 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3685 ad242220 2018-09-08 stsp if (repo) {
3686 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3687 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3688 ad242220 2018-09-08 stsp if (error == NULL)
3689 ad242220 2018-09-08 stsp error = repo_error;
3690 ad242220 2018-09-08 stsp }
3691 3affba96 2019-09-22 stsp if (bca.lines) {
3692 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3693 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3694 3affba96 2019-09-22 stsp free(bline->id_str);
3695 3affba96 2019-09-22 stsp free(bline->committer);
3696 3affba96 2019-09-22 stsp }
3697 3affba96 2019-09-22 stsp free(bca.lines);
3698 28315671 2019-08-14 stsp }
3699 28315671 2019-08-14 stsp free(bca.line_offsets);
3700 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3701 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3702 404c43c4 2018-06-21 stsp return error;
3703 5de5890b 2018-10-18 stsp }
3704 5de5890b 2018-10-18 stsp
3705 5de5890b 2018-10-18 stsp __dead static void
3706 5de5890b 2018-10-18 stsp usage_tree(void)
3707 5de5890b 2018-10-18 stsp {
3708 c1669e2e 2019-01-09 stsp fprintf(stderr,
3709 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3710 5de5890b 2018-10-18 stsp getprogname());
3711 5de5890b 2018-10-18 stsp exit(1);
3712 5de5890b 2018-10-18 stsp }
3713 5de5890b 2018-10-18 stsp
3714 c1669e2e 2019-01-09 stsp static void
3715 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3716 c1669e2e 2019-01-09 stsp const char *root_path)
3717 c1669e2e 2019-01-09 stsp {
3718 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3719 848d6979 2019-08-12 stsp const char *modestr = "";
3720 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3721 5de5890b 2018-10-18 stsp
3722 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3723 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3724 c1669e2e 2019-01-09 stsp path++;
3725 c1669e2e 2019-01-09 stsp
3726 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3727 63c5ca5d 2019-08-24 stsp modestr = "$";
3728 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3729 848d6979 2019-08-12 stsp modestr = "@";
3730 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
3731 848d6979 2019-08-12 stsp modestr = "/";
3732 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
3733 848d6979 2019-08-12 stsp modestr = "*";
3734 848d6979 2019-08-12 stsp
3735 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
3736 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
3737 c1669e2e 2019-01-09 stsp }
3738 c1669e2e 2019-01-09 stsp
3739 5de5890b 2018-10-18 stsp static const struct got_error *
3740 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
3741 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
3742 c1669e2e 2019-01-09 stsp struct got_repository *repo)
3743 5de5890b 2018-10-18 stsp {
3744 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
3745 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
3746 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
3747 56e0773d 2019-11-28 stsp int nentries, i;
3748 5de5890b 2018-10-18 stsp
3749 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
3750 5de5890b 2018-10-18 stsp if (err)
3751 5de5890b 2018-10-18 stsp goto done;
3752 5de5890b 2018-10-18 stsp
3753 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
3754 5de5890b 2018-10-18 stsp if (err)
3755 5de5890b 2018-10-18 stsp goto done;
3756 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
3757 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
3758 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
3759 5de5890b 2018-10-18 stsp char *id = NULL;
3760 84453469 2018-11-11 stsp
3761 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3762 84453469 2018-11-11 stsp break;
3763 84453469 2018-11-11 stsp
3764 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
3765 5de5890b 2018-10-18 stsp if (show_ids) {
3766 5de5890b 2018-10-18 stsp char *id_str;
3767 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
3768 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
3769 5de5890b 2018-10-18 stsp if (err)
3770 5de5890b 2018-10-18 stsp goto done;
3771 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
3772 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3773 5de5890b 2018-10-18 stsp free(id_str);
3774 5de5890b 2018-10-18 stsp goto done;
3775 5de5890b 2018-10-18 stsp }
3776 5de5890b 2018-10-18 stsp free(id_str);
3777 5de5890b 2018-10-18 stsp }
3778 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3779 5de5890b 2018-10-18 stsp free(id);
3780 c1669e2e 2019-01-09 stsp
3781 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3782 c1669e2e 2019-01-09 stsp char *child_path;
3783 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3784 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3785 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3786 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3787 c1669e2e 2019-01-09 stsp goto done;
3788 c1669e2e 2019-01-09 stsp }
3789 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3790 c1669e2e 2019-01-09 stsp root_path, repo);
3791 c1669e2e 2019-01-09 stsp free(child_path);
3792 c1669e2e 2019-01-09 stsp if (err)
3793 c1669e2e 2019-01-09 stsp goto done;
3794 c1669e2e 2019-01-09 stsp }
3795 5de5890b 2018-10-18 stsp }
3796 5de5890b 2018-10-18 stsp done:
3797 5de5890b 2018-10-18 stsp if (tree)
3798 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3799 5de5890b 2018-10-18 stsp free(tree_id);
3800 5de5890b 2018-10-18 stsp return err;
3801 404c43c4 2018-06-21 stsp }
3802 404c43c4 2018-06-21 stsp
3803 5de5890b 2018-10-18 stsp static const struct got_error *
3804 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3805 5de5890b 2018-10-18 stsp {
3806 5de5890b 2018-10-18 stsp const struct got_error *error;
3807 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3808 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3809 7a2c19d6 2019-02-05 stsp const char *path;
3810 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3811 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3812 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3813 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3814 5de5890b 2018-10-18 stsp int ch;
3815 5de5890b 2018-10-18 stsp
3816 5de5890b 2018-10-18 stsp #ifndef PROFILE
3817 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3818 0f8d269b 2019-01-04 stsp NULL) == -1)
3819 5de5890b 2018-10-18 stsp err(1, "pledge");
3820 5de5890b 2018-10-18 stsp #endif
3821 5de5890b 2018-10-18 stsp
3822 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3823 5de5890b 2018-10-18 stsp switch (ch) {
3824 5de5890b 2018-10-18 stsp case 'c':
3825 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3826 5de5890b 2018-10-18 stsp break;
3827 5de5890b 2018-10-18 stsp case 'r':
3828 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3829 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3830 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3831 9ba1d308 2019-10-21 stsp optarg);
3832 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3833 5de5890b 2018-10-18 stsp break;
3834 5de5890b 2018-10-18 stsp case 'i':
3835 5de5890b 2018-10-18 stsp show_ids = 1;
3836 5de5890b 2018-10-18 stsp break;
3837 c1669e2e 2019-01-09 stsp case 'R':
3838 c1669e2e 2019-01-09 stsp recurse = 1;
3839 c1669e2e 2019-01-09 stsp break;
3840 5de5890b 2018-10-18 stsp default:
3841 2deda0b9 2019-03-07 stsp usage_tree();
3842 5de5890b 2018-10-18 stsp /* NOTREACHED */
3843 5de5890b 2018-10-18 stsp }
3844 5de5890b 2018-10-18 stsp }
3845 5de5890b 2018-10-18 stsp
3846 5de5890b 2018-10-18 stsp argc -= optind;
3847 5de5890b 2018-10-18 stsp argv += optind;
3848 5de5890b 2018-10-18 stsp
3849 5de5890b 2018-10-18 stsp if (argc == 1)
3850 5de5890b 2018-10-18 stsp path = argv[0];
3851 5de5890b 2018-10-18 stsp else if (argc > 1)
3852 5de5890b 2018-10-18 stsp usage_tree();
3853 5de5890b 2018-10-18 stsp else
3854 7a2c19d6 2019-02-05 stsp path = NULL;
3855 7a2c19d6 2019-02-05 stsp
3856 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3857 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3858 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3859 9bf7a39b 2019-02-05 stsp goto done;
3860 9bf7a39b 2019-02-05 stsp }
3861 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3862 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3863 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3864 7a2c19d6 2019-02-05 stsp goto done;
3865 7a2c19d6 2019-02-05 stsp else
3866 7a2c19d6 2019-02-05 stsp error = NULL;
3867 7a2c19d6 2019-02-05 stsp if (worktree) {
3868 7a2c19d6 2019-02-05 stsp repo_path =
3869 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3870 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3871 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3872 7a2c19d6 2019-02-05 stsp if (error)
3873 7a2c19d6 2019-02-05 stsp goto done;
3874 7a2c19d6 2019-02-05 stsp } else {
3875 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3876 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3877 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3878 7a2c19d6 2019-02-05 stsp goto done;
3879 7a2c19d6 2019-02-05 stsp }
3880 5de5890b 2018-10-18 stsp }
3881 5de5890b 2018-10-18 stsp }
3882 5de5890b 2018-10-18 stsp
3883 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3884 5de5890b 2018-10-18 stsp if (error != NULL)
3885 c02c541e 2019-03-29 stsp goto done;
3886 c02c541e 2019-03-29 stsp
3887 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3888 c02c541e 2019-03-29 stsp if (error)
3889 5de5890b 2018-10-18 stsp goto done;
3890 5de5890b 2018-10-18 stsp
3891 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3892 9bf7a39b 2019-02-05 stsp if (worktree) {
3893 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3894 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3895 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3896 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3897 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3898 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3899 9bf7a39b 2019-02-05 stsp goto done;
3900 9bf7a39b 2019-02-05 stsp }
3901 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3902 9bf7a39b 2019-02-05 stsp free(p);
3903 9bf7a39b 2019-02-05 stsp if (error)
3904 9bf7a39b 2019-02-05 stsp goto done;
3905 9bf7a39b 2019-02-05 stsp } else
3906 9bf7a39b 2019-02-05 stsp path = "/";
3907 9bf7a39b 2019-02-05 stsp }
3908 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3909 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3910 9bf7a39b 2019-02-05 stsp if (error != NULL)
3911 9bf7a39b 2019-02-05 stsp goto done;
3912 9bf7a39b 2019-02-05 stsp }
3913 5de5890b 2018-10-18 stsp
3914 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3915 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3916 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3917 5de5890b 2018-10-18 stsp if (error != NULL)
3918 5de5890b 2018-10-18 stsp goto done;
3919 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3920 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3921 5de5890b 2018-10-18 stsp if (error != NULL)
3922 5de5890b 2018-10-18 stsp goto done;
3923 5de5890b 2018-10-18 stsp } else {
3924 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3925 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3926 30837e32 2019-07-25 stsp if (error)
3927 5de5890b 2018-10-18 stsp goto done;
3928 5de5890b 2018-10-18 stsp }
3929 5de5890b 2018-10-18 stsp
3930 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3931 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3932 5de5890b 2018-10-18 stsp done:
3933 5de5890b 2018-10-18 stsp free(in_repo_path);
3934 5de5890b 2018-10-18 stsp free(repo_path);
3935 5de5890b 2018-10-18 stsp free(cwd);
3936 5de5890b 2018-10-18 stsp free(commit_id);
3937 7a2c19d6 2019-02-05 stsp if (worktree)
3938 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3939 5de5890b 2018-10-18 stsp if (repo) {
3940 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3941 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3942 5de5890b 2018-10-18 stsp if (error == NULL)
3943 5de5890b 2018-10-18 stsp error = repo_error;
3944 5de5890b 2018-10-18 stsp }
3945 5de5890b 2018-10-18 stsp return error;
3946 5de5890b 2018-10-18 stsp }
3947 5de5890b 2018-10-18 stsp
3948 6bad629b 2019-02-04 stsp __dead static void
3949 6bad629b 2019-02-04 stsp usage_status(void)
3950 6bad629b 2019-02-04 stsp {
3951 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3952 6bad629b 2019-02-04 stsp exit(1);
3953 6bad629b 2019-02-04 stsp }
3954 5c860e29 2018-03-12 stsp
3955 b72f483a 2019-02-05 stsp static const struct got_error *
3956 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3957 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3958 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3959 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3960 6bad629b 2019-02-04 stsp {
3961 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3962 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3963 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3964 b72f483a 2019-02-05 stsp return NULL;
3965 6bad629b 2019-02-04 stsp }
3966 5c860e29 2018-03-12 stsp
3967 6bad629b 2019-02-04 stsp static const struct got_error *
3968 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3969 6bad629b 2019-02-04 stsp {
3970 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3971 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3972 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3973 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3974 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3975 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3976 a5edda0a 2019-07-27 stsp int ch;
3977 5c860e29 2018-03-12 stsp
3978 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3979 72ea6654 2019-07-27 stsp
3980 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3981 6bad629b 2019-02-04 stsp switch (ch) {
3982 5c860e29 2018-03-12 stsp default:
3983 2deda0b9 2019-03-07 stsp usage_status();
3984 6bad629b 2019-02-04 stsp /* NOTREACHED */
3985 5c860e29 2018-03-12 stsp }
3986 5c860e29 2018-03-12 stsp }
3987 5c860e29 2018-03-12 stsp
3988 6bad629b 2019-02-04 stsp argc -= optind;
3989 6bad629b 2019-02-04 stsp argv += optind;
3990 5c860e29 2018-03-12 stsp
3991 6bad629b 2019-02-04 stsp #ifndef PROFILE
3992 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3993 6bad629b 2019-02-04 stsp NULL) == -1)
3994 6bad629b 2019-02-04 stsp err(1, "pledge");
3995 f42b1b34 2018-03-12 stsp #endif
3996 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3997 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3998 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3999 927df6b7 2019-02-10 stsp goto done;
4000 927df6b7 2019-02-10 stsp }
4001 927df6b7 2019-02-10 stsp
4002 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4003 927df6b7 2019-02-10 stsp if (error != NULL)
4004 a5edda0a 2019-07-27 stsp goto done;
4005 6bad629b 2019-02-04 stsp
4006 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4007 c9956ddf 2019-09-08 stsp NULL);
4008 6bad629b 2019-02-04 stsp if (error != NULL)
4009 6bad629b 2019-02-04 stsp goto done;
4010 6bad629b 2019-02-04 stsp
4011 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4012 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4013 087fb88c 2019-08-04 stsp if (error)
4014 087fb88c 2019-08-04 stsp goto done;
4015 087fb88c 2019-08-04 stsp
4016 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4017 6bad629b 2019-02-04 stsp if (error)
4018 6bad629b 2019-02-04 stsp goto done;
4019 6bad629b 2019-02-04 stsp
4020 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4021 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4022 6bad629b 2019-02-04 stsp done:
4023 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4024 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4025 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4026 927df6b7 2019-02-10 stsp free(cwd);
4027 d0eebce4 2019-03-11 stsp return error;
4028 d0eebce4 2019-03-11 stsp }
4029 d0eebce4 2019-03-11 stsp
4030 d0eebce4 2019-03-11 stsp __dead static void
4031 d0eebce4 2019-03-11 stsp usage_ref(void)
4032 d0eebce4 2019-03-11 stsp {
4033 d0eebce4 2019-03-11 stsp fprintf(stderr,
4034 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
4035 d0eebce4 2019-03-11 stsp getprogname());
4036 d0eebce4 2019-03-11 stsp exit(1);
4037 d0eebce4 2019-03-11 stsp }
4038 d0eebce4 2019-03-11 stsp
4039 d0eebce4 2019-03-11 stsp static const struct got_error *
4040 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
4041 d0eebce4 2019-03-11 stsp {
4042 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4043 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4044 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4045 d0eebce4 2019-03-11 stsp
4046 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4047 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
4048 d0eebce4 2019-03-11 stsp if (err)
4049 d0eebce4 2019-03-11 stsp return err;
4050 d0eebce4 2019-03-11 stsp
4051 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4052 d0eebce4 2019-03-11 stsp char *refstr;
4053 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4054 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4055 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4056 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4057 d0eebce4 2019-03-11 stsp free(refstr);
4058 d0eebce4 2019-03-11 stsp }
4059 d0eebce4 2019-03-11 stsp
4060 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4061 d0eebce4 2019-03-11 stsp return NULL;
4062 d0eebce4 2019-03-11 stsp }
4063 d0eebce4 2019-03-11 stsp
4064 d0eebce4 2019-03-11 stsp static const struct got_error *
4065 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4066 d0eebce4 2019-03-11 stsp {
4067 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4068 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4069 d0eebce4 2019-03-11 stsp
4070 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4071 d0eebce4 2019-03-11 stsp if (err)
4072 d0eebce4 2019-03-11 stsp return err;
4073 d0eebce4 2019-03-11 stsp
4074 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4075 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4076 d0eebce4 2019-03-11 stsp return err;
4077 d0eebce4 2019-03-11 stsp }
4078 d0eebce4 2019-03-11 stsp
4079 d0eebce4 2019-03-11 stsp static const struct got_error *
4080 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4081 d0eebce4 2019-03-11 stsp {
4082 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4083 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4084 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4085 d1644381 2019-07-14 stsp
4086 d1644381 2019-07-14 stsp /*
4087 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4088 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4089 d1644381 2019-07-14 stsp * an unintended typo.
4090 d1644381 2019-07-14 stsp */
4091 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4092 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4093 d0eebce4 2019-03-11 stsp
4094 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4095 dd88155e 2019-06-29 stsp repo);
4096 d83d9d5c 2019-05-13 stsp if (err) {
4097 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4098 d0eebce4 2019-03-11 stsp
4099 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4100 d83d9d5c 2019-05-13 stsp return err;
4101 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4102 d83d9d5c 2019-05-13 stsp if (err)
4103 d83d9d5c 2019-05-13 stsp return err;
4104 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4105 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4106 d83d9d5c 2019-05-13 stsp if (err)
4107 d83d9d5c 2019-05-13 stsp return err;
4108 d83d9d5c 2019-05-13 stsp }
4109 d83d9d5c 2019-05-13 stsp
4110 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4111 d0eebce4 2019-03-11 stsp if (err)
4112 d0eebce4 2019-03-11 stsp goto done;
4113 d0eebce4 2019-03-11 stsp
4114 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4115 d0eebce4 2019-03-11 stsp done:
4116 d0eebce4 2019-03-11 stsp if (ref)
4117 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4118 d0eebce4 2019-03-11 stsp free(id);
4119 d1c1ae5f 2019-08-12 stsp return err;
4120 d1c1ae5f 2019-08-12 stsp }
4121 d1c1ae5f 2019-08-12 stsp
4122 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4123 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4124 d1c1ae5f 2019-08-12 stsp {
4125 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4126 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4127 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4128 d1c1ae5f 2019-08-12 stsp
4129 d1c1ae5f 2019-08-12 stsp /*
4130 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4131 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4132 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4133 d1c1ae5f 2019-08-12 stsp */
4134 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4135 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4136 d1c1ae5f 2019-08-12 stsp
4137 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4138 d1c1ae5f 2019-08-12 stsp if (err)
4139 d1c1ae5f 2019-08-12 stsp return err;
4140 d1c1ae5f 2019-08-12 stsp
4141 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4142 d1c1ae5f 2019-08-12 stsp if (err)
4143 d1c1ae5f 2019-08-12 stsp goto done;
4144 d1c1ae5f 2019-08-12 stsp
4145 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4146 d1c1ae5f 2019-08-12 stsp done:
4147 d1c1ae5f 2019-08-12 stsp if (target_ref)
4148 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4149 d1c1ae5f 2019-08-12 stsp if (ref)
4150 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4151 d0eebce4 2019-03-11 stsp return err;
4152 d0eebce4 2019-03-11 stsp }
4153 d0eebce4 2019-03-11 stsp
4154 d0eebce4 2019-03-11 stsp static const struct got_error *
4155 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4156 d0eebce4 2019-03-11 stsp {
4157 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4158 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4159 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4160 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4161 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
4162 d0eebce4 2019-03-11 stsp const char *delref = NULL;
4163 d0eebce4 2019-03-11 stsp
4164 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
4165 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
4166 d0eebce4 2019-03-11 stsp switch (ch) {
4167 d0eebce4 2019-03-11 stsp case 'd':
4168 d0eebce4 2019-03-11 stsp delref = optarg;
4169 d0eebce4 2019-03-11 stsp break;
4170 d0eebce4 2019-03-11 stsp case 'r':
4171 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4172 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4173 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4174 9ba1d308 2019-10-21 stsp optarg);
4175 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4176 d0eebce4 2019-03-11 stsp break;
4177 d0eebce4 2019-03-11 stsp case 'l':
4178 d0eebce4 2019-03-11 stsp do_list = 1;
4179 d1c1ae5f 2019-08-12 stsp break;
4180 d1c1ae5f 2019-08-12 stsp case 's':
4181 d1c1ae5f 2019-08-12 stsp create_symref = 1;
4182 d0eebce4 2019-03-11 stsp break;
4183 d0eebce4 2019-03-11 stsp default:
4184 d0eebce4 2019-03-11 stsp usage_ref();
4185 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4186 d0eebce4 2019-03-11 stsp }
4187 d0eebce4 2019-03-11 stsp }
4188 d0eebce4 2019-03-11 stsp
4189 d0eebce4 2019-03-11 stsp if (do_list && delref)
4190 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
4191 d0eebce4 2019-03-11 stsp
4192 d0eebce4 2019-03-11 stsp argc -= optind;
4193 d0eebce4 2019-03-11 stsp argv += optind;
4194 d0eebce4 2019-03-11 stsp
4195 d0eebce4 2019-03-11 stsp if (do_list || delref) {
4196 d1c1ae5f 2019-08-12 stsp if (create_symref)
4197 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
4198 d1c1ae5f 2019-08-12 stsp "-l or -d options");
4199 d0eebce4 2019-03-11 stsp if (argc > 0)
4200 d0eebce4 2019-03-11 stsp usage_ref();
4201 d0eebce4 2019-03-11 stsp } else if (argc != 2)
4202 d0eebce4 2019-03-11 stsp usage_ref();
4203 d0eebce4 2019-03-11 stsp
4204 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4205 e0b57350 2019-03-12 stsp if (do_list) {
4206 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4207 e0b57350 2019-03-12 stsp NULL) == -1)
4208 e0b57350 2019-03-12 stsp err(1, "pledge");
4209 e0b57350 2019-03-12 stsp } else {
4210 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4211 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4212 e0b57350 2019-03-12 stsp err(1, "pledge");
4213 e0b57350 2019-03-12 stsp }
4214 d0eebce4 2019-03-11 stsp #endif
4215 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4216 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4217 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4218 d0eebce4 2019-03-11 stsp goto done;
4219 d0eebce4 2019-03-11 stsp }
4220 d0eebce4 2019-03-11 stsp
4221 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4222 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4223 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4224 d0eebce4 2019-03-11 stsp goto done;
4225 d0eebce4 2019-03-11 stsp else
4226 d0eebce4 2019-03-11 stsp error = NULL;
4227 d0eebce4 2019-03-11 stsp if (worktree) {
4228 d0eebce4 2019-03-11 stsp repo_path =
4229 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4230 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4231 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4232 d0eebce4 2019-03-11 stsp if (error)
4233 d0eebce4 2019-03-11 stsp goto done;
4234 d0eebce4 2019-03-11 stsp } else {
4235 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4236 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4237 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4238 d0eebce4 2019-03-11 stsp goto done;
4239 d0eebce4 2019-03-11 stsp }
4240 d0eebce4 2019-03-11 stsp }
4241 d0eebce4 2019-03-11 stsp }
4242 d0eebce4 2019-03-11 stsp
4243 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4244 d0eebce4 2019-03-11 stsp if (error != NULL)
4245 d0eebce4 2019-03-11 stsp goto done;
4246 d0eebce4 2019-03-11 stsp
4247 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4248 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4249 c02c541e 2019-03-29 stsp if (error)
4250 c02c541e 2019-03-29 stsp goto done;
4251 c02c541e 2019-03-29 stsp
4252 d0eebce4 2019-03-11 stsp if (do_list)
4253 d0eebce4 2019-03-11 stsp error = list_refs(repo);
4254 d0eebce4 2019-03-11 stsp else if (delref)
4255 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
4256 d1c1ae5f 2019-08-12 stsp else if (create_symref)
4257 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
4258 d0eebce4 2019-03-11 stsp else
4259 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
4260 4e759de4 2019-06-26 stsp done:
4261 4e759de4 2019-06-26 stsp if (repo)
4262 4e759de4 2019-06-26 stsp got_repo_close(repo);
4263 4e759de4 2019-06-26 stsp if (worktree)
4264 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4265 4e759de4 2019-06-26 stsp free(cwd);
4266 4e759de4 2019-06-26 stsp free(repo_path);
4267 4e759de4 2019-06-26 stsp return error;
4268 4e759de4 2019-06-26 stsp }
4269 4e759de4 2019-06-26 stsp
4270 4e759de4 2019-06-26 stsp __dead static void
4271 4e759de4 2019-06-26 stsp usage_branch(void)
4272 4e759de4 2019-06-26 stsp {
4273 4e759de4 2019-06-26 stsp fprintf(stderr,
4274 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4275 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4276 4e759de4 2019-06-26 stsp exit(1);
4277 4e759de4 2019-06-26 stsp }
4278 4e759de4 2019-06-26 stsp
4279 4e759de4 2019-06-26 stsp static const struct got_error *
4280 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4281 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4282 4e99b47e 2019-10-04 stsp {
4283 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4284 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4285 4e99b47e 2019-10-04 stsp char *refstr;
4286 4e99b47e 2019-10-04 stsp
4287 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4288 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4289 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4290 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4291 4e99b47e 2019-10-04 stsp
4292 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4293 4e99b47e 2019-10-04 stsp if (err)
4294 4e99b47e 2019-10-04 stsp return err;
4295 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4296 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4297 4e99b47e 2019-10-04 stsp marker = "* ";
4298 4e99b47e 2019-10-04 stsp else
4299 4e99b47e 2019-10-04 stsp marker = "~ ";
4300 4e99b47e 2019-10-04 stsp free(id);
4301 4e99b47e 2019-10-04 stsp }
4302 4e99b47e 2019-10-04 stsp
4303 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4304 4e99b47e 2019-10-04 stsp refname += 11;
4305 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4306 4e99b47e 2019-10-04 stsp refname += 18;
4307 4e99b47e 2019-10-04 stsp
4308 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4309 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4310 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4311 4e99b47e 2019-10-04 stsp
4312 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4313 4e99b47e 2019-10-04 stsp free(refstr);
4314 4e99b47e 2019-10-04 stsp return NULL;
4315 4e99b47e 2019-10-04 stsp }
4316 4e99b47e 2019-10-04 stsp
4317 4e99b47e 2019-10-04 stsp static const struct got_error *
4318 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4319 ad89fa31 2019-10-04 stsp {
4320 ad89fa31 2019-10-04 stsp const char *refname;
4321 ad89fa31 2019-10-04 stsp
4322 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4323 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4324 ad89fa31 2019-10-04 stsp
4325 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4326 ad89fa31 2019-10-04 stsp
4327 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4328 ad89fa31 2019-10-04 stsp refname += 11;
4329 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4330 ad89fa31 2019-10-04 stsp refname += 18;
4331 ad89fa31 2019-10-04 stsp
4332 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4333 ad89fa31 2019-10-04 stsp
4334 ad89fa31 2019-10-04 stsp return NULL;
4335 ad89fa31 2019-10-04 stsp }
4336 ad89fa31 2019-10-04 stsp
4337 ad89fa31 2019-10-04 stsp static const struct got_error *
4338 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4339 4e759de4 2019-06-26 stsp {
4340 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4341 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4342 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4343 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4344 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4345 4e759de4 2019-06-26 stsp
4346 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4347 ba882ee3 2019-07-11 stsp
4348 4e99b47e 2019-10-04 stsp if (worktree) {
4349 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4350 4e99b47e 2019-10-04 stsp worktree);
4351 4e99b47e 2019-10-04 stsp if (err)
4352 4e99b47e 2019-10-04 stsp return err;
4353 4e99b47e 2019-10-04 stsp
4354 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4355 4e99b47e 2019-10-04 stsp worktree);
4356 4e99b47e 2019-10-04 stsp if (err)
4357 4e99b47e 2019-10-04 stsp return err;
4358 4e99b47e 2019-10-04 stsp
4359 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4360 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4361 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4362 4e99b47e 2019-10-04 stsp if (err)
4363 4e99b47e 2019-10-04 stsp return err;
4364 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4365 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4366 4e99b47e 2019-10-04 stsp }
4367 4e99b47e 2019-10-04 stsp }
4368 4e99b47e 2019-10-04 stsp
4369 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4370 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4371 4e759de4 2019-06-26 stsp if (err)
4372 4e759de4 2019-06-26 stsp return err;
4373 4e759de4 2019-06-26 stsp
4374 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4375 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4376 4e759de4 2019-06-26 stsp
4377 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4378 4e759de4 2019-06-26 stsp return NULL;
4379 4e759de4 2019-06-26 stsp }
4380 4e759de4 2019-06-26 stsp
4381 4e759de4 2019-06-26 stsp static const struct got_error *
4382 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4383 45cd4e47 2019-08-25 stsp const char *branch_name)
4384 4e759de4 2019-06-26 stsp {
4385 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4386 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4387 4e759de4 2019-06-26 stsp char *refname;
4388 4e759de4 2019-06-26 stsp
4389 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4390 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4391 4e759de4 2019-06-26 stsp
4392 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4393 4e759de4 2019-06-26 stsp if (err)
4394 4e759de4 2019-06-26 stsp goto done;
4395 4e759de4 2019-06-26 stsp
4396 45cd4e47 2019-08-25 stsp if (worktree &&
4397 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4398 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4399 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4400 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4401 45cd4e47 2019-08-25 stsp goto done;
4402 45cd4e47 2019-08-25 stsp }
4403 45cd4e47 2019-08-25 stsp
4404 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4405 4e759de4 2019-06-26 stsp done:
4406 45cd4e47 2019-08-25 stsp if (ref)
4407 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4408 4e759de4 2019-06-26 stsp free(refname);
4409 4e759de4 2019-06-26 stsp return err;
4410 4e759de4 2019-06-26 stsp }
4411 4e759de4 2019-06-26 stsp
4412 4e759de4 2019-06-26 stsp static const struct got_error *
4413 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4414 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4415 4e759de4 2019-06-26 stsp {
4416 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4417 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4418 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4419 d3f84d51 2019-07-11 stsp
4420 d3f84d51 2019-07-11 stsp /*
4421 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4422 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4423 d3f84d51 2019-07-11 stsp * an unintended typo.
4424 d3f84d51 2019-07-11 stsp */
4425 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4426 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4427 4e759de4 2019-06-26 stsp
4428 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4429 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4430 4e759de4 2019-06-26 stsp goto done;
4431 4e759de4 2019-06-26 stsp }
4432 4e759de4 2019-06-26 stsp
4433 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4434 4e759de4 2019-06-26 stsp if (err == NULL) {
4435 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4436 4e759de4 2019-06-26 stsp goto done;
4437 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4438 4e759de4 2019-06-26 stsp goto done;
4439 4e759de4 2019-06-26 stsp
4440 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4441 4e759de4 2019-06-26 stsp if (err)
4442 4e759de4 2019-06-26 stsp goto done;
4443 4e759de4 2019-06-26 stsp
4444 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4445 d0eebce4 2019-03-11 stsp done:
4446 4e759de4 2019-06-26 stsp if (ref)
4447 4e759de4 2019-06-26 stsp got_ref_close(ref);
4448 4e759de4 2019-06-26 stsp free(base_refname);
4449 4e759de4 2019-06-26 stsp free(refname);
4450 4e759de4 2019-06-26 stsp return err;
4451 4e759de4 2019-06-26 stsp }
4452 4e759de4 2019-06-26 stsp
4453 4e759de4 2019-06-26 stsp static const struct got_error *
4454 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4455 4e759de4 2019-06-26 stsp {
4456 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4457 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4458 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4459 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4460 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4461 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4462 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4463 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4464 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4465 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4466 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4467 4e759de4 2019-06-26 stsp
4468 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4469 da76fce2 2020-02-24 stsp
4470 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4471 4e759de4 2019-06-26 stsp switch (ch) {
4472 a74f7e83 2019-11-10 stsp case 'c':
4473 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4474 a74f7e83 2019-11-10 stsp break;
4475 4e759de4 2019-06-26 stsp case 'd':
4476 4e759de4 2019-06-26 stsp delref = optarg;
4477 4e759de4 2019-06-26 stsp break;
4478 4e759de4 2019-06-26 stsp case 'r':
4479 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4480 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4481 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4482 9ba1d308 2019-10-21 stsp optarg);
4483 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4484 4e759de4 2019-06-26 stsp break;
4485 4e759de4 2019-06-26 stsp case 'l':
4486 4e759de4 2019-06-26 stsp do_list = 1;
4487 da76fce2 2020-02-24 stsp break;
4488 da76fce2 2020-02-24 stsp case 'n':
4489 da76fce2 2020-02-24 stsp do_update = 0;
4490 4e759de4 2019-06-26 stsp break;
4491 4e759de4 2019-06-26 stsp default:
4492 4e759de4 2019-06-26 stsp usage_branch();
4493 4e759de4 2019-06-26 stsp /* NOTREACHED */
4494 4e759de4 2019-06-26 stsp }
4495 4e759de4 2019-06-26 stsp }
4496 4e759de4 2019-06-26 stsp
4497 4e759de4 2019-06-26 stsp if (do_list && delref)
4498 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
4499 4e759de4 2019-06-26 stsp
4500 4e759de4 2019-06-26 stsp argc -= optind;
4501 4e759de4 2019-06-26 stsp argv += optind;
4502 ad89fa31 2019-10-04 stsp
4503 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4504 ad89fa31 2019-10-04 stsp do_show = 1;
4505 4e759de4 2019-06-26 stsp
4506 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4507 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4508 a74f7e83 2019-11-10 stsp
4509 4e759de4 2019-06-26 stsp if (do_list || delref) {
4510 4e759de4 2019-06-26 stsp if (argc > 0)
4511 4e759de4 2019-06-26 stsp usage_branch();
4512 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4513 4e759de4 2019-06-26 stsp usage_branch();
4514 4e759de4 2019-06-26 stsp
4515 4e759de4 2019-06-26 stsp #ifndef PROFILE
4516 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4517 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4518 4e759de4 2019-06-26 stsp NULL) == -1)
4519 4e759de4 2019-06-26 stsp err(1, "pledge");
4520 4e759de4 2019-06-26 stsp } else {
4521 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4522 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4523 4e759de4 2019-06-26 stsp err(1, "pledge");
4524 4e759de4 2019-06-26 stsp }
4525 4e759de4 2019-06-26 stsp #endif
4526 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4527 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4528 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4529 4e759de4 2019-06-26 stsp goto done;
4530 4e759de4 2019-06-26 stsp }
4531 4e759de4 2019-06-26 stsp
4532 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4533 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4534 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4535 4e759de4 2019-06-26 stsp goto done;
4536 4e759de4 2019-06-26 stsp else
4537 4e759de4 2019-06-26 stsp error = NULL;
4538 4e759de4 2019-06-26 stsp if (worktree) {
4539 4e759de4 2019-06-26 stsp repo_path =
4540 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4541 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4542 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4543 4e759de4 2019-06-26 stsp if (error)
4544 4e759de4 2019-06-26 stsp goto done;
4545 4e759de4 2019-06-26 stsp } else {
4546 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4547 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4548 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4549 4e759de4 2019-06-26 stsp goto done;
4550 4e759de4 2019-06-26 stsp }
4551 4e759de4 2019-06-26 stsp }
4552 4e759de4 2019-06-26 stsp }
4553 4e759de4 2019-06-26 stsp
4554 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4555 4e759de4 2019-06-26 stsp if (error != NULL)
4556 4e759de4 2019-06-26 stsp goto done;
4557 4e759de4 2019-06-26 stsp
4558 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4559 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4560 4e759de4 2019-06-26 stsp if (error)
4561 4e759de4 2019-06-26 stsp goto done;
4562 4e759de4 2019-06-26 stsp
4563 ad89fa31 2019-10-04 stsp if (do_show)
4564 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4565 ad89fa31 2019-10-04 stsp else if (do_list)
4566 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4567 4e759de4 2019-06-26 stsp else if (delref)
4568 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4569 4e759de4 2019-06-26 stsp else {
4570 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4571 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4572 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4573 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4574 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4575 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4576 a74f7e83 2019-11-10 stsp if (error)
4577 a74f7e83 2019-11-10 stsp goto done;
4578 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4579 da76fce2 2020-02-24 stsp if (error)
4580 da76fce2 2020-02-24 stsp goto done;
4581 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4582 da76fce2 2020-02-24 stsp int did_something = 0;
4583 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4584 da76fce2 2020-02-24 stsp
4585 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4586 da76fce2 2020-02-24 stsp if (error)
4587 da76fce2 2020-02-24 stsp goto done;
4588 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4589 da76fce2 2020-02-24 stsp worktree);
4590 da76fce2 2020-02-24 stsp if (error)
4591 da76fce2 2020-02-24 stsp goto done;
4592 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4593 da76fce2 2020-02-24 stsp == -1) {
4594 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4595 da76fce2 2020-02-24 stsp goto done;
4596 da76fce2 2020-02-24 stsp }
4597 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4598 da76fce2 2020-02-24 stsp free(branch_refname);
4599 da76fce2 2020-02-24 stsp if (error)
4600 da76fce2 2020-02-24 stsp goto done;
4601 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4602 da76fce2 2020-02-24 stsp repo);
4603 da76fce2 2020-02-24 stsp if (error)
4604 da76fce2 2020-02-24 stsp goto done;
4605 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4606 da76fce2 2020-02-24 stsp commit_id);
4607 da76fce2 2020-02-24 stsp if (error)
4608 da76fce2 2020-02-24 stsp goto done;
4609 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4610 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4611 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4612 da76fce2 2020-02-24 stsp if (error)
4613 da76fce2 2020-02-24 stsp goto done;
4614 da76fce2 2020-02-24 stsp if (did_something)
4615 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4616 da76fce2 2020-02-24 stsp }
4617 4e759de4 2019-06-26 stsp }
4618 4e759de4 2019-06-26 stsp done:
4619 da76fce2 2020-02-24 stsp if (ref)
4620 da76fce2 2020-02-24 stsp got_ref_close(ref);
4621 d0eebce4 2019-03-11 stsp if (repo)
4622 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4623 d0eebce4 2019-03-11 stsp if (worktree)
4624 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4625 d0eebce4 2019-03-11 stsp free(cwd);
4626 d0eebce4 2019-03-11 stsp free(repo_path);
4627 da76fce2 2020-02-24 stsp free(commit_id);
4628 da76fce2 2020-02-24 stsp free(commit_id_str);
4629 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4630 da76fce2 2020-02-24 stsp free((char *)pe->path);
4631 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4632 d00136be 2019-03-26 stsp return error;
4633 d00136be 2019-03-26 stsp }
4634 d00136be 2019-03-26 stsp
4635 8e7bd50a 2019-08-22 stsp
4636 d00136be 2019-03-26 stsp __dead static void
4637 8e7bd50a 2019-08-22 stsp usage_tag(void)
4638 8e7bd50a 2019-08-22 stsp {
4639 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4640 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4641 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4642 8e7bd50a 2019-08-22 stsp exit(1);
4643 b8bad2ba 2019-08-23 stsp }
4644 b8bad2ba 2019-08-23 stsp
4645 b8bad2ba 2019-08-23 stsp #if 0
4646 b8bad2ba 2019-08-23 stsp static const struct got_error *
4647 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4648 b8bad2ba 2019-08-23 stsp {
4649 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4650 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4651 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4652 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4653 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4654 b8bad2ba 2019-08-23 stsp
4655 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4656 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4657 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4658 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4659 b8bad2ba 2019-08-23 stsp if (err)
4660 b8bad2ba 2019-08-23 stsp return err;
4661 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4662 b8bad2ba 2019-08-23 stsp continue;
4663 b8bad2ba 2019-08-23 stsp } else {
4664 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4665 b8bad2ba 2019-08-23 stsp if (err)
4666 b8bad2ba 2019-08-23 stsp break;
4667 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4668 b8bad2ba 2019-08-23 stsp free(re_id);
4669 b8bad2ba 2019-08-23 stsp if (err)
4670 b8bad2ba 2019-08-23 stsp break;
4671 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4672 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4673 b8bad2ba 2019-08-23 stsp }
4674 b8bad2ba 2019-08-23 stsp
4675 b8bad2ba 2019-08-23 stsp while (se) {
4676 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4677 b8bad2ba 2019-08-23 stsp if (err)
4678 b8bad2ba 2019-08-23 stsp break;
4679 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4680 b8bad2ba 2019-08-23 stsp free(se_id);
4681 b8bad2ba 2019-08-23 stsp if (err)
4682 b8bad2ba 2019-08-23 stsp break;
4683 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4684 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4685 b8bad2ba 2019-08-23 stsp
4686 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4687 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4688 b8bad2ba 2019-08-23 stsp if (err)
4689 b8bad2ba 2019-08-23 stsp return err;
4690 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4691 b8bad2ba 2019-08-23 stsp break;
4692 b8bad2ba 2019-08-23 stsp }
4693 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4694 b8bad2ba 2019-08-23 stsp continue;
4695 b8bad2ba 2019-08-23 stsp }
4696 b8bad2ba 2019-08-23 stsp }
4697 b8bad2ba 2019-08-23 stsp done:
4698 b8bad2ba 2019-08-23 stsp return err;
4699 8e7bd50a 2019-08-22 stsp }
4700 b8bad2ba 2019-08-23 stsp #endif
4701 b8bad2ba 2019-08-23 stsp
4702 b8bad2ba 2019-08-23 stsp static const struct got_error *
4703 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
4704 8e7bd50a 2019-08-22 stsp {
4705 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
4706 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
4707 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
4708 8e7bd50a 2019-08-22 stsp
4709 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
4710 8e7bd50a 2019-08-22 stsp
4711 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
4712 8e7bd50a 2019-08-22 stsp if (err)
4713 8e7bd50a 2019-08-22 stsp return err;
4714 8e7bd50a 2019-08-22 stsp
4715 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4716 8e7bd50a 2019-08-22 stsp const char *refname;
4717 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
4718 8e7bd50a 2019-08-22 stsp char datebuf[26];
4719 d4efa91b 2020-01-14 stsp const char *tagger;
4720 8e7bd50a 2019-08-22 stsp time_t tagger_time;
4721 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
4722 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
4723 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
4724 8e7bd50a 2019-08-22 stsp
4725 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
4726 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
4727 8e7bd50a 2019-08-22 stsp continue;
4728 8e7bd50a 2019-08-22 stsp refname += 10;
4729 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
4730 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
4731 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
4732 8e7bd50a 2019-08-22 stsp break;
4733 8e7bd50a 2019-08-22 stsp }
4734 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
4735 8e7bd50a 2019-08-22 stsp free(refstr);
4736 8e7bd50a 2019-08-22 stsp
4737 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
4738 8e7bd50a 2019-08-22 stsp if (err)
4739 8e7bd50a 2019-08-22 stsp break;
4740 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
4741 d4efa91b 2020-01-14 stsp if (err) {
4742 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
4743 d4efa91b 2020-01-14 stsp free(id);
4744 d4efa91b 2020-01-14 stsp break;
4745 d4efa91b 2020-01-14 stsp }
4746 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
4747 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
4748 d4efa91b 2020-01-14 stsp if (err) {
4749 d4efa91b 2020-01-14 stsp free(id);
4750 d4efa91b 2020-01-14 stsp break;
4751 d4efa91b 2020-01-14 stsp }
4752 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
4753 d4efa91b 2020-01-14 stsp tagger_time =
4754 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
4755 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
4756 d4efa91b 2020-01-14 stsp free(id);
4757 d4efa91b 2020-01-14 stsp if (err)
4758 d4efa91b 2020-01-14 stsp break;
4759 d4efa91b 2020-01-14 stsp } else {
4760 d4efa91b 2020-01-14 stsp free(id);
4761 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
4762 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
4763 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
4764 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
4765 d4efa91b 2020-01-14 stsp if (err)
4766 d4efa91b 2020-01-14 stsp break;
4767 d4efa91b 2020-01-14 stsp }
4768 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
4769 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
4770 2417344c 2019-08-23 stsp if (datestr)
4771 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
4772 d4efa91b 2020-01-14 stsp if (commit)
4773 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
4774 d4efa91b 2020-01-14 stsp else {
4775 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
4776 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4777 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4778 d4efa91b 2020-01-14 stsp id_str);
4779 d4efa91b 2020-01-14 stsp break;
4780 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4781 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4782 d4efa91b 2020-01-14 stsp id_str);
4783 d4efa91b 2020-01-14 stsp break;
4784 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4785 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4786 d4efa91b 2020-01-14 stsp id_str);
4787 d4efa91b 2020-01-14 stsp break;
4788 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4789 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4790 d4efa91b 2020-01-14 stsp id_str);
4791 d4efa91b 2020-01-14 stsp break;
4792 d4efa91b 2020-01-14 stsp default:
4793 d4efa91b 2020-01-14 stsp break;
4794 d4efa91b 2020-01-14 stsp }
4795 8e7bd50a 2019-08-22 stsp }
4796 8e7bd50a 2019-08-22 stsp free(id_str);
4797 d4efa91b 2020-01-14 stsp if (commit) {
4798 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4799 d4efa91b 2020-01-14 stsp if (err)
4800 d4efa91b 2020-01-14 stsp break;
4801 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4802 d4efa91b 2020-01-14 stsp } else {
4803 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4804 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4805 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4806 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4807 d4efa91b 2020-01-14 stsp break;
4808 d4efa91b 2020-01-14 stsp }
4809 8e7bd50a 2019-08-22 stsp }
4810 8e7bd50a 2019-08-22 stsp
4811 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4812 8e7bd50a 2019-08-22 stsp do {
4813 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4814 8e7bd50a 2019-08-22 stsp if (line)
4815 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4816 8e7bd50a 2019-08-22 stsp } while (line);
4817 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4818 8e7bd50a 2019-08-22 stsp }
4819 8e7bd50a 2019-08-22 stsp
4820 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4821 8e7bd50a 2019-08-22 stsp return NULL;
4822 8e7bd50a 2019-08-22 stsp }
4823 8e7bd50a 2019-08-22 stsp
4824 8e7bd50a 2019-08-22 stsp static const struct got_error *
4825 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4826 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4827 8e7bd50a 2019-08-22 stsp {
4828 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4829 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4830 f372d5cd 2019-10-21 stsp char *editor = NULL;
4831 8e7bd50a 2019-08-22 stsp int fd = -1;
4832 8e7bd50a 2019-08-22 stsp
4833 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4834 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4835 8e7bd50a 2019-08-22 stsp goto done;
4836 8e7bd50a 2019-08-22 stsp }
4837 8e7bd50a 2019-08-22 stsp
4838 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4839 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4840 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4841 8e7bd50a 2019-08-22 stsp goto done;
4842 8e7bd50a 2019-08-22 stsp }
4843 8e7bd50a 2019-08-22 stsp
4844 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4845 8e7bd50a 2019-08-22 stsp if (err)
4846 8e7bd50a 2019-08-22 stsp goto done;
4847 8e7bd50a 2019-08-22 stsp
4848 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4849 8e7bd50a 2019-08-22 stsp close(fd);
4850 8e7bd50a 2019-08-22 stsp
4851 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4852 8e7bd50a 2019-08-22 stsp if (err)
4853 8e7bd50a 2019-08-22 stsp goto done;
4854 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4855 8e7bd50a 2019-08-22 stsp done:
4856 8e7bd50a 2019-08-22 stsp free(initial_content);
4857 8e7bd50a 2019-08-22 stsp free(template);
4858 8e7bd50a 2019-08-22 stsp free(editor);
4859 8e7bd50a 2019-08-22 stsp
4860 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4861 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4862 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4863 8e7bd50a 2019-08-22 stsp if (err) {
4864 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4865 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4866 8e7bd50a 2019-08-22 stsp }
4867 8e7bd50a 2019-08-22 stsp }
4868 8e7bd50a 2019-08-22 stsp return err;
4869 8e7bd50a 2019-08-22 stsp }
4870 8e7bd50a 2019-08-22 stsp
4871 8e7bd50a 2019-08-22 stsp static const struct got_error *
4872 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4873 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4874 8e7bd50a 2019-08-22 stsp {
4875 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4876 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4877 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4878 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4879 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4880 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4881 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4882 8e7bd50a 2019-08-22 stsp
4883 8e7bd50a 2019-08-22 stsp /*
4884 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4885 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4886 8e7bd50a 2019-08-22 stsp * an unintended typo.
4887 8e7bd50a 2019-08-22 stsp */
4888 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4889 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4890 8e7bd50a 2019-08-22 stsp
4891 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4892 8e7bd50a 2019-08-22 stsp if (err)
4893 8e7bd50a 2019-08-22 stsp return err;
4894 8e7bd50a 2019-08-22 stsp
4895 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4896 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4897 8e7bd50a 2019-08-22 stsp if (err)
4898 8e7bd50a 2019-08-22 stsp goto done;
4899 8e7bd50a 2019-08-22 stsp
4900 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4901 8e7bd50a 2019-08-22 stsp if (err)
4902 8e7bd50a 2019-08-22 stsp goto done;
4903 8e7bd50a 2019-08-22 stsp
4904 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4905 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4906 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4907 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4908 8e7bd50a 2019-08-22 stsp goto done;
4909 8e7bd50a 2019-08-22 stsp }
4910 8e7bd50a 2019-08-22 stsp tag_name += 10;
4911 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4912 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4913 8e7bd50a 2019-08-22 stsp goto done;
4914 8e7bd50a 2019-08-22 stsp }
4915 8e7bd50a 2019-08-22 stsp
4916 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4917 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4918 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4919 8e7bd50a 2019-08-22 stsp goto done;
4920 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4921 8e7bd50a 2019-08-22 stsp goto done;
4922 8e7bd50a 2019-08-22 stsp
4923 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4924 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4925 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4926 f372d5cd 2019-10-21 stsp if (err) {
4927 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4928 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4929 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4930 8e7bd50a 2019-08-22 stsp goto done;
4931 f372d5cd 2019-10-21 stsp }
4932 8e7bd50a 2019-08-22 stsp }
4933 8e7bd50a 2019-08-22 stsp
4934 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4935 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4936 f372d5cd 2019-10-21 stsp if (err) {
4937 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4938 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4939 8e7bd50a 2019-08-22 stsp goto done;
4940 f372d5cd 2019-10-21 stsp }
4941 8e7bd50a 2019-08-22 stsp
4942 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4943 f372d5cd 2019-10-21 stsp if (err) {
4944 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4945 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4946 8e7bd50a 2019-08-22 stsp goto done;
4947 f372d5cd 2019-10-21 stsp }
4948 8e7bd50a 2019-08-22 stsp
4949 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4950 f372d5cd 2019-10-21 stsp if (err) {
4951 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4952 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4953 f372d5cd 2019-10-21 stsp goto done;
4954 f372d5cd 2019-10-21 stsp }
4955 8e7bd50a 2019-08-22 stsp
4956 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4957 f372d5cd 2019-10-21 stsp if (err) {
4958 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4959 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4960 f372d5cd 2019-10-21 stsp goto done;
4961 8e7bd50a 2019-08-22 stsp }
4962 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4963 8e7bd50a 2019-08-22 stsp done:
4964 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4965 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4966 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4967 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4968 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4969 f372d5cd 2019-10-21 stsp free(tag_id_str);
4970 8e7bd50a 2019-08-22 stsp if (ref)
4971 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4972 8e7bd50a 2019-08-22 stsp free(commit_id);
4973 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4974 8e7bd50a 2019-08-22 stsp free(refname);
4975 8e7bd50a 2019-08-22 stsp free(tagmsg);
4976 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4977 aba9c984 2019-09-08 stsp free(tagger);
4978 8e7bd50a 2019-08-22 stsp return err;
4979 8e7bd50a 2019-08-22 stsp }
4980 8e7bd50a 2019-08-22 stsp
4981 8e7bd50a 2019-08-22 stsp static const struct got_error *
4982 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4983 8e7bd50a 2019-08-22 stsp {
4984 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4985 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4986 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4987 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4988 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4989 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4990 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4991 8e7bd50a 2019-08-22 stsp
4992 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4993 8e7bd50a 2019-08-22 stsp switch (ch) {
4994 80106605 2020-02-24 stsp case 'c':
4995 80106605 2020-02-24 stsp commit_id_arg = optarg;
4996 80106605 2020-02-24 stsp break;
4997 8e7bd50a 2019-08-22 stsp case 'm':
4998 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4999 8e7bd50a 2019-08-22 stsp break;
5000 8e7bd50a 2019-08-22 stsp case 'r':
5001 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5002 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5003 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5004 9ba1d308 2019-10-21 stsp optarg);
5005 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5006 8e7bd50a 2019-08-22 stsp break;
5007 8e7bd50a 2019-08-22 stsp case 'l':
5008 8e7bd50a 2019-08-22 stsp do_list = 1;
5009 8e7bd50a 2019-08-22 stsp break;
5010 8e7bd50a 2019-08-22 stsp default:
5011 8e7bd50a 2019-08-22 stsp usage_tag();
5012 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5013 8e7bd50a 2019-08-22 stsp }
5014 8e7bd50a 2019-08-22 stsp }
5015 8e7bd50a 2019-08-22 stsp
5016 8e7bd50a 2019-08-22 stsp argc -= optind;
5017 8e7bd50a 2019-08-22 stsp argv += optind;
5018 8e7bd50a 2019-08-22 stsp
5019 8e7bd50a 2019-08-22 stsp if (do_list) {
5020 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5021 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
5022 8e7bd50a 2019-08-22 stsp if (tagmsg)
5023 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5024 8e7bd50a 2019-08-22 stsp if (argc > 0)
5025 8e7bd50a 2019-08-22 stsp usage_tag();
5026 80106605 2020-02-24 stsp } else if (argc != 1)
5027 8e7bd50a 2019-08-22 stsp usage_tag();
5028 80106605 2020-02-24 stsp
5029 a2887370 2019-08-23 stsp tag_name = argv[0];
5030 8e7bd50a 2019-08-22 stsp
5031 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5032 8e7bd50a 2019-08-22 stsp if (do_list) {
5033 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5034 8e7bd50a 2019-08-22 stsp NULL) == -1)
5035 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5036 8e7bd50a 2019-08-22 stsp } else {
5037 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5038 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5039 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5040 8e7bd50a 2019-08-22 stsp }
5041 8e7bd50a 2019-08-22 stsp #endif
5042 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5043 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5044 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5045 8e7bd50a 2019-08-22 stsp goto done;
5046 8e7bd50a 2019-08-22 stsp }
5047 8e7bd50a 2019-08-22 stsp
5048 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5049 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5050 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5051 8e7bd50a 2019-08-22 stsp goto done;
5052 8e7bd50a 2019-08-22 stsp else
5053 8e7bd50a 2019-08-22 stsp error = NULL;
5054 8e7bd50a 2019-08-22 stsp if (worktree) {
5055 8e7bd50a 2019-08-22 stsp repo_path =
5056 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5057 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5058 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5059 8e7bd50a 2019-08-22 stsp if (error)
5060 8e7bd50a 2019-08-22 stsp goto done;
5061 8e7bd50a 2019-08-22 stsp } else {
5062 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5063 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5064 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5065 8e7bd50a 2019-08-22 stsp goto done;
5066 8e7bd50a 2019-08-22 stsp }
5067 8e7bd50a 2019-08-22 stsp }
5068 8e7bd50a 2019-08-22 stsp }
5069 8e7bd50a 2019-08-22 stsp
5070 8e7bd50a 2019-08-22 stsp if (do_list) {
5071 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5072 c9956ddf 2019-09-08 stsp if (error != NULL)
5073 c9956ddf 2019-09-08 stsp goto done;
5074 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5075 8e7bd50a 2019-08-22 stsp if (error)
5076 8e7bd50a 2019-08-22 stsp goto done;
5077 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5078 8e7bd50a 2019-08-22 stsp } else {
5079 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5080 c9956ddf 2019-09-08 stsp if (error)
5081 c9956ddf 2019-09-08 stsp goto done;
5082 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5083 c9956ddf 2019-09-08 stsp if (error != NULL)
5084 c9956ddf 2019-09-08 stsp goto done;
5085 c9956ddf 2019-09-08 stsp
5086 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5087 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5088 8e7bd50a 2019-08-22 stsp if (error)
5089 8e7bd50a 2019-08-22 stsp goto done;
5090 8e7bd50a 2019-08-22 stsp }
5091 8e7bd50a 2019-08-22 stsp
5092 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5093 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5094 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5095 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5096 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5097 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5098 8e7bd50a 2019-08-22 stsp if (error)
5099 8e7bd50a 2019-08-22 stsp goto done;
5100 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5101 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5102 8e7bd50a 2019-08-22 stsp if (error)
5103 8e7bd50a 2019-08-22 stsp goto done;
5104 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5105 8e7bd50a 2019-08-22 stsp free(commit_id);
5106 8e7bd50a 2019-08-22 stsp if (error)
5107 8e7bd50a 2019-08-22 stsp goto done;
5108 8e7bd50a 2019-08-22 stsp }
5109 8e7bd50a 2019-08-22 stsp
5110 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5111 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5112 8e7bd50a 2019-08-22 stsp }
5113 8e7bd50a 2019-08-22 stsp done:
5114 8e7bd50a 2019-08-22 stsp if (repo)
5115 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5116 8e7bd50a 2019-08-22 stsp if (worktree)
5117 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5118 8e7bd50a 2019-08-22 stsp free(cwd);
5119 8e7bd50a 2019-08-22 stsp free(repo_path);
5120 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5121 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5122 8e7bd50a 2019-08-22 stsp return error;
5123 8e7bd50a 2019-08-22 stsp }
5124 8e7bd50a 2019-08-22 stsp
5125 8e7bd50a 2019-08-22 stsp __dead static void
5126 d00136be 2019-03-26 stsp usage_add(void)
5127 d00136be 2019-03-26 stsp {
5128 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5129 022fae89 2019-12-06 tracey getprogname());
5130 d00136be 2019-03-26 stsp exit(1);
5131 d00136be 2019-03-26 stsp }
5132 d00136be 2019-03-26 stsp
5133 d00136be 2019-03-26 stsp static const struct got_error *
5134 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5135 4e68cba3 2019-11-23 stsp {
5136 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5137 4e68cba3 2019-11-23 stsp path++;
5138 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5139 4e68cba3 2019-11-23 stsp return NULL;
5140 4e68cba3 2019-11-23 stsp }
5141 4e68cba3 2019-11-23 stsp
5142 4e68cba3 2019-11-23 stsp static const struct got_error *
5143 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5144 d00136be 2019-03-26 stsp {
5145 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5146 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5147 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5148 1dd54920 2019-05-11 stsp char *cwd = NULL;
5149 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5150 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5151 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5152 1dd54920 2019-05-11 stsp
5153 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5154 d00136be 2019-03-26 stsp
5155 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5156 d00136be 2019-03-26 stsp switch (ch) {
5157 022fae89 2019-12-06 tracey case 'I':
5158 022fae89 2019-12-06 tracey no_ignores = 1;
5159 022fae89 2019-12-06 tracey break;
5160 4e68cba3 2019-11-23 stsp case 'R':
5161 4e68cba3 2019-11-23 stsp can_recurse = 1;
5162 4e68cba3 2019-11-23 stsp break;
5163 d00136be 2019-03-26 stsp default:
5164 d00136be 2019-03-26 stsp usage_add();
5165 d00136be 2019-03-26 stsp /* NOTREACHED */
5166 d00136be 2019-03-26 stsp }
5167 d00136be 2019-03-26 stsp }
5168 d00136be 2019-03-26 stsp
5169 d00136be 2019-03-26 stsp argc -= optind;
5170 d00136be 2019-03-26 stsp argv += optind;
5171 d00136be 2019-03-26 stsp
5172 43012d58 2019-07-14 stsp #ifndef PROFILE
5173 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5174 43012d58 2019-07-14 stsp NULL) == -1)
5175 43012d58 2019-07-14 stsp err(1, "pledge");
5176 43012d58 2019-07-14 stsp #endif
5177 723c305c 2019-05-11 jcs if (argc < 1)
5178 d00136be 2019-03-26 stsp usage_add();
5179 d00136be 2019-03-26 stsp
5180 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5181 d00136be 2019-03-26 stsp if (cwd == NULL) {
5182 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5183 d00136be 2019-03-26 stsp goto done;
5184 d00136be 2019-03-26 stsp }
5185 723c305c 2019-05-11 jcs
5186 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5187 d00136be 2019-03-26 stsp if (error)
5188 d00136be 2019-03-26 stsp goto done;
5189 d00136be 2019-03-26 stsp
5190 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5191 c9956ddf 2019-09-08 stsp NULL);
5192 031a5338 2019-03-26 stsp if (error != NULL)
5193 031a5338 2019-03-26 stsp goto done;
5194 031a5338 2019-03-26 stsp
5195 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5196 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5197 d00136be 2019-03-26 stsp if (error)
5198 d00136be 2019-03-26 stsp goto done;
5199 d00136be 2019-03-26 stsp
5200 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5201 6d022e97 2019-08-04 stsp if (error)
5202 022fae89 2019-12-06 tracey goto done;
5203 022fae89 2019-12-06 tracey
5204 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5205 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5206 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5207 6d022e97 2019-08-04 stsp goto done;
5208 723c305c 2019-05-11 jcs
5209 022fae89 2019-12-06 tracey }
5210 022fae89 2019-12-06 tracey
5211 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5212 4e68cba3 2019-11-23 stsp char *ondisk_path;
5213 4e68cba3 2019-11-23 stsp struct stat sb;
5214 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5215 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5216 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5217 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5218 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5219 4e68cba3 2019-11-23 stsp goto done;
5220 4e68cba3 2019-11-23 stsp }
5221 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5222 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5223 4e68cba3 2019-11-23 stsp free(ondisk_path);
5224 4e68cba3 2019-11-23 stsp continue;
5225 4e68cba3 2019-11-23 stsp }
5226 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5227 4e68cba3 2019-11-23 stsp ondisk_path);
5228 4e68cba3 2019-11-23 stsp free(ondisk_path);
5229 4e68cba3 2019-11-23 stsp goto done;
5230 4e68cba3 2019-11-23 stsp }
5231 4e68cba3 2019-11-23 stsp free(ondisk_path);
5232 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5233 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5234 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5235 4e68cba3 2019-11-23 stsp goto done;
5236 4e68cba3 2019-11-23 stsp }
5237 4e68cba3 2019-11-23 stsp }
5238 4e68cba3 2019-11-23 stsp }
5239 022fae89 2019-12-06 tracey
5240 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5241 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5242 d00136be 2019-03-26 stsp done:
5243 031a5338 2019-03-26 stsp if (repo)
5244 031a5338 2019-03-26 stsp got_repo_close(repo);
5245 d00136be 2019-03-26 stsp if (worktree)
5246 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5247 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5248 1dd54920 2019-05-11 stsp free((char *)pe->path);
5249 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5250 2ec1f75b 2019-03-26 stsp free(cwd);
5251 2ec1f75b 2019-03-26 stsp return error;
5252 2ec1f75b 2019-03-26 stsp }
5253 2ec1f75b 2019-03-26 stsp
5254 2ec1f75b 2019-03-26 stsp __dead static void
5255 648e4ef7 2019-07-09 stsp usage_remove(void)
5256 2ec1f75b 2019-03-26 stsp {
5257 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5258 f2a9dc41 2019-12-13 tracey getprogname());
5259 2ec1f75b 2019-03-26 stsp exit(1);
5260 2a06fe5f 2019-08-24 stsp }
5261 2a06fe5f 2019-08-24 stsp
5262 2a06fe5f 2019-08-24 stsp static const struct got_error *
5263 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5264 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5265 2a06fe5f 2019-08-24 stsp {
5266 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5267 f2a9dc41 2019-12-13 tracey path++;
5268 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5269 2a06fe5f 2019-08-24 stsp return NULL;
5270 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5271 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5272 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5273 2a06fe5f 2019-08-24 stsp return NULL;
5274 2ec1f75b 2019-03-26 stsp }
5275 2ec1f75b 2019-03-26 stsp
5276 2ec1f75b 2019-03-26 stsp static const struct got_error *
5277 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5278 2ec1f75b 2019-03-26 stsp {
5279 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5280 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5281 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5282 17ed4618 2019-06-02 stsp char *cwd = NULL;
5283 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5284 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5285 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5286 2ec1f75b 2019-03-26 stsp
5287 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5288 17ed4618 2019-06-02 stsp
5289 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5290 2ec1f75b 2019-03-26 stsp switch (ch) {
5291 2ec1f75b 2019-03-26 stsp case 'f':
5292 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5293 2ec1f75b 2019-03-26 stsp break;
5294 70e3e7f5 2019-12-13 tracey case 'k':
5295 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5296 70e3e7f5 2019-12-13 tracey break;
5297 f2a9dc41 2019-12-13 tracey case 'R':
5298 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5299 f2a9dc41 2019-12-13 tracey break;
5300 2ec1f75b 2019-03-26 stsp default:
5301 f2a9dc41 2019-12-13 tracey usage_remove();
5302 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5303 2ec1f75b 2019-03-26 stsp }
5304 2ec1f75b 2019-03-26 stsp }
5305 2ec1f75b 2019-03-26 stsp
5306 2ec1f75b 2019-03-26 stsp argc -= optind;
5307 2ec1f75b 2019-03-26 stsp argv += optind;
5308 2ec1f75b 2019-03-26 stsp
5309 43012d58 2019-07-14 stsp #ifndef PROFILE
5310 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5311 43012d58 2019-07-14 stsp NULL) == -1)
5312 43012d58 2019-07-14 stsp err(1, "pledge");
5313 43012d58 2019-07-14 stsp #endif
5314 17ed4618 2019-06-02 stsp if (argc < 1)
5315 648e4ef7 2019-07-09 stsp usage_remove();
5316 2ec1f75b 2019-03-26 stsp
5317 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5318 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5319 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5320 2ec1f75b 2019-03-26 stsp goto done;
5321 2ec1f75b 2019-03-26 stsp }
5322 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5323 2ec1f75b 2019-03-26 stsp if (error)
5324 2ec1f75b 2019-03-26 stsp goto done;
5325 2ec1f75b 2019-03-26 stsp
5326 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5327 c9956ddf 2019-09-08 stsp NULL);
5328 2af4a041 2019-05-11 jcs if (error)
5329 2ec1f75b 2019-03-26 stsp goto done;
5330 2ec1f75b 2019-03-26 stsp
5331 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5332 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5333 2ec1f75b 2019-03-26 stsp if (error)
5334 2ec1f75b 2019-03-26 stsp goto done;
5335 2ec1f75b 2019-03-26 stsp
5336 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5337 6d022e97 2019-08-04 stsp if (error)
5338 6d022e97 2019-08-04 stsp goto done;
5339 17ed4618 2019-06-02 stsp
5340 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5341 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5342 f2a9dc41 2019-12-13 tracey struct stat sb;
5343 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5344 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5345 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5346 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5347 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5348 f2a9dc41 2019-12-13 tracey goto done;
5349 f2a9dc41 2019-12-13 tracey }
5350 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5351 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5352 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5353 f2a9dc41 2019-12-13 tracey continue;
5354 f2a9dc41 2019-12-13 tracey }
5355 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5356 f2a9dc41 2019-12-13 tracey ondisk_path);
5357 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5358 f2a9dc41 2019-12-13 tracey goto done;
5359 f2a9dc41 2019-12-13 tracey }
5360 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5361 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5362 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5363 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5364 f2a9dc41 2019-12-13 tracey goto done;
5365 f2a9dc41 2019-12-13 tracey }
5366 f2a9dc41 2019-12-13 tracey }
5367 f2a9dc41 2019-12-13 tracey }
5368 f2a9dc41 2019-12-13 tracey
5369 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5370 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5371 a129376b 2019-03-28 stsp done:
5372 a129376b 2019-03-28 stsp if (repo)
5373 a129376b 2019-03-28 stsp got_repo_close(repo);
5374 a129376b 2019-03-28 stsp if (worktree)
5375 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5376 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5377 17ed4618 2019-06-02 stsp free((char *)pe->path);
5378 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5379 a129376b 2019-03-28 stsp free(cwd);
5380 a129376b 2019-03-28 stsp return error;
5381 a129376b 2019-03-28 stsp }
5382 a129376b 2019-03-28 stsp
5383 a129376b 2019-03-28 stsp __dead static void
5384 a129376b 2019-03-28 stsp usage_revert(void)
5385 a129376b 2019-03-28 stsp {
5386 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5387 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5388 a129376b 2019-03-28 stsp exit(1);
5389 a129376b 2019-03-28 stsp }
5390 a129376b 2019-03-28 stsp
5391 1ee397ad 2019-07-12 stsp static const struct got_error *
5392 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5393 a129376b 2019-03-28 stsp {
5394 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5395 fb9704af 2020-01-27 stsp return NULL;
5396 fb9704af 2020-01-27 stsp
5397 a129376b 2019-03-28 stsp while (path[0] == '/')
5398 a129376b 2019-03-28 stsp path++;
5399 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5400 33aa809d 2019-08-08 stsp return NULL;
5401 33aa809d 2019-08-08 stsp }
5402 33aa809d 2019-08-08 stsp
5403 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5404 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5405 33aa809d 2019-08-08 stsp const char *action;
5406 33aa809d 2019-08-08 stsp };
5407 33aa809d 2019-08-08 stsp
5408 33aa809d 2019-08-08 stsp static const struct got_error *
5409 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5410 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5411 33aa809d 2019-08-08 stsp {
5412 33aa809d 2019-08-08 stsp char *line = NULL;
5413 33aa809d 2019-08-08 stsp size_t linesize = 0;
5414 33aa809d 2019-08-08 stsp ssize_t linelen;
5415 33aa809d 2019-08-08 stsp
5416 33aa809d 2019-08-08 stsp switch (status) {
5417 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5418 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5419 33aa809d 2019-08-08 stsp break;
5420 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5421 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5422 33aa809d 2019-08-08 stsp break;
5423 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5424 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5425 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5426 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5427 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5428 33aa809d 2019-08-08 stsp printf("%s", line);
5429 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5430 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5431 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5432 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5433 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5434 33aa809d 2019-08-08 stsp break;
5435 33aa809d 2019-08-08 stsp default:
5436 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5437 33aa809d 2019-08-08 stsp }
5438 33aa809d 2019-08-08 stsp
5439 33aa809d 2019-08-08 stsp return NULL;
5440 33aa809d 2019-08-08 stsp }
5441 33aa809d 2019-08-08 stsp
5442 33aa809d 2019-08-08 stsp static const struct got_error *
5443 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5444 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5445 33aa809d 2019-08-08 stsp {
5446 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5447 33aa809d 2019-08-08 stsp char *line = NULL;
5448 33aa809d 2019-08-08 stsp size_t linesize = 0;
5449 33aa809d 2019-08-08 stsp ssize_t linelen;
5450 33aa809d 2019-08-08 stsp int resp = ' ';
5451 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5452 33aa809d 2019-08-08 stsp
5453 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5454 33aa809d 2019-08-08 stsp
5455 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5456 33aa809d 2019-08-08 stsp char *nl;
5457 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5458 33aa809d 2019-08-08 stsp a->action);
5459 33aa809d 2019-08-08 stsp if (err)
5460 33aa809d 2019-08-08 stsp return err;
5461 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5462 33aa809d 2019-08-08 stsp if (linelen == -1) {
5463 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5464 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5465 33aa809d 2019-08-08 stsp return NULL;
5466 33aa809d 2019-08-08 stsp }
5467 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5468 33aa809d 2019-08-08 stsp if (nl)
5469 33aa809d 2019-08-08 stsp *nl = '\0';
5470 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5471 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5472 33aa809d 2019-08-08 stsp printf("y\n");
5473 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5474 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5475 33aa809d 2019-08-08 stsp printf("n\n");
5476 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5477 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5478 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5479 33aa809d 2019-08-08 stsp printf("q\n");
5480 33aa809d 2019-08-08 stsp } else
5481 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5482 33aa809d 2019-08-08 stsp free(line);
5483 33aa809d 2019-08-08 stsp return NULL;
5484 33aa809d 2019-08-08 stsp }
5485 33aa809d 2019-08-08 stsp
5486 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5487 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5488 33aa809d 2019-08-08 stsp a->action);
5489 33aa809d 2019-08-08 stsp if (err)
5490 33aa809d 2019-08-08 stsp return err;
5491 33aa809d 2019-08-08 stsp resp = getchar();
5492 33aa809d 2019-08-08 stsp if (resp == '\n')
5493 33aa809d 2019-08-08 stsp resp = getchar();
5494 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5495 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5496 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5497 33aa809d 2019-08-08 stsp resp = ' ';
5498 33aa809d 2019-08-08 stsp }
5499 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5500 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5501 33aa809d 2019-08-08 stsp resp = ' ';
5502 33aa809d 2019-08-08 stsp }
5503 33aa809d 2019-08-08 stsp }
5504 33aa809d 2019-08-08 stsp
5505 33aa809d 2019-08-08 stsp if (resp == 'y')
5506 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5507 33aa809d 2019-08-08 stsp else if (resp == 'n')
5508 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5509 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5510 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5511 33aa809d 2019-08-08 stsp
5512 1ee397ad 2019-07-12 stsp return NULL;
5513 a129376b 2019-03-28 stsp }
5514 a129376b 2019-03-28 stsp
5515 33aa809d 2019-08-08 stsp
5516 a129376b 2019-03-28 stsp static const struct got_error *
5517 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5518 a129376b 2019-03-28 stsp {
5519 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5520 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5521 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5522 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5523 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5524 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5525 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5526 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5527 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5528 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5529 a129376b 2019-03-28 stsp
5530 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5531 e20a8b6f 2019-06-04 stsp
5532 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5533 a129376b 2019-03-28 stsp switch (ch) {
5534 33aa809d 2019-08-08 stsp case 'p':
5535 33aa809d 2019-08-08 stsp pflag = 1;
5536 33aa809d 2019-08-08 stsp break;
5537 33aa809d 2019-08-08 stsp case 'F':
5538 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5539 33aa809d 2019-08-08 stsp break;
5540 0f6d7415 2019-08-08 stsp case 'R':
5541 0f6d7415 2019-08-08 stsp can_recurse = 1;
5542 0f6d7415 2019-08-08 stsp break;
5543 a129376b 2019-03-28 stsp default:
5544 a129376b 2019-03-28 stsp usage_revert();
5545 a129376b 2019-03-28 stsp /* NOTREACHED */
5546 a129376b 2019-03-28 stsp }
5547 a129376b 2019-03-28 stsp }
5548 a129376b 2019-03-28 stsp
5549 a129376b 2019-03-28 stsp argc -= optind;
5550 a129376b 2019-03-28 stsp argv += optind;
5551 a129376b 2019-03-28 stsp
5552 43012d58 2019-07-14 stsp #ifndef PROFILE
5553 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5554 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5555 43012d58 2019-07-14 stsp err(1, "pledge");
5556 43012d58 2019-07-14 stsp #endif
5557 e20a8b6f 2019-06-04 stsp if (argc < 1)
5558 a129376b 2019-03-28 stsp usage_revert();
5559 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5560 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5561 a129376b 2019-03-28 stsp
5562 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5563 a129376b 2019-03-28 stsp if (cwd == NULL) {
5564 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5565 a129376b 2019-03-28 stsp goto done;
5566 a129376b 2019-03-28 stsp }
5567 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5568 2ec1f75b 2019-03-26 stsp if (error)
5569 2ec1f75b 2019-03-26 stsp goto done;
5570 a129376b 2019-03-28 stsp
5571 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5572 c9956ddf 2019-09-08 stsp NULL);
5573 a129376b 2019-03-28 stsp if (error != NULL)
5574 a129376b 2019-03-28 stsp goto done;
5575 a129376b 2019-03-28 stsp
5576 33aa809d 2019-08-08 stsp if (patch_script_path) {
5577 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5578 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5579 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5580 33aa809d 2019-08-08 stsp patch_script_path);
5581 33aa809d 2019-08-08 stsp goto done;
5582 33aa809d 2019-08-08 stsp }
5583 33aa809d 2019-08-08 stsp }
5584 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5585 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5586 a129376b 2019-03-28 stsp if (error)
5587 a129376b 2019-03-28 stsp goto done;
5588 a129376b 2019-03-28 stsp
5589 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5590 6d022e97 2019-08-04 stsp if (error)
5591 6d022e97 2019-08-04 stsp goto done;
5592 00db391e 2019-08-03 stsp
5593 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5594 0f6d7415 2019-08-08 stsp char *ondisk_path;
5595 0f6d7415 2019-08-08 stsp struct stat sb;
5596 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5597 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5598 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5599 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5600 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5601 0f6d7415 2019-08-08 stsp goto done;
5602 0f6d7415 2019-08-08 stsp }
5603 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5604 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5605 0f6d7415 2019-08-08 stsp free(ondisk_path);
5606 0f6d7415 2019-08-08 stsp continue;
5607 0f6d7415 2019-08-08 stsp }
5608 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5609 0f6d7415 2019-08-08 stsp ondisk_path);
5610 0f6d7415 2019-08-08 stsp free(ondisk_path);
5611 0f6d7415 2019-08-08 stsp goto done;
5612 0f6d7415 2019-08-08 stsp }
5613 0f6d7415 2019-08-08 stsp free(ondisk_path);
5614 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5615 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5616 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5617 0f6d7415 2019-08-08 stsp goto done;
5618 0f6d7415 2019-08-08 stsp }
5619 0f6d7415 2019-08-08 stsp }
5620 0f6d7415 2019-08-08 stsp }
5621 0f6d7415 2019-08-08 stsp
5622 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5623 33aa809d 2019-08-08 stsp cpa.action = "revert";
5624 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5625 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5626 2ec1f75b 2019-03-26 stsp done:
5627 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5628 33aa809d 2019-08-08 stsp error == NULL)
5629 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5630 2ec1f75b 2019-03-26 stsp if (repo)
5631 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5632 2ec1f75b 2019-03-26 stsp if (worktree)
5633 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5634 2ec1f75b 2019-03-26 stsp free(path);
5635 d00136be 2019-03-26 stsp free(cwd);
5636 6bad629b 2019-02-04 stsp return error;
5637 c4296144 2019-05-09 stsp }
5638 c4296144 2019-05-09 stsp
5639 c4296144 2019-05-09 stsp __dead static void
5640 c4296144 2019-05-09 stsp usage_commit(void)
5641 c4296144 2019-05-09 stsp {
5642 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5643 5c1e53bc 2019-07-28 stsp getprogname());
5644 c4296144 2019-05-09 stsp exit(1);
5645 33ad4cbe 2019-05-12 jcs }
5646 33ad4cbe 2019-05-12 jcs
5647 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5648 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5649 e2ba3d07 2019-05-13 stsp const char *editor;
5650 e0870e44 2019-05-13 stsp const char *worktree_path;
5651 76d98825 2019-06-03 stsp const char *branch_name;
5652 314a6357 2019-05-13 stsp const char *repo_path;
5653 e0870e44 2019-05-13 stsp char *logmsg_path;
5654 e2ba3d07 2019-05-13 stsp
5655 e2ba3d07 2019-05-13 stsp };
5656 e0870e44 2019-05-13 stsp
5657 33ad4cbe 2019-05-12 jcs static const struct got_error *
5658 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5659 33ad4cbe 2019-05-12 jcs void *arg)
5660 33ad4cbe 2019-05-12 jcs {
5661 76d98825 2019-06-03 stsp char *initial_content = NULL;
5662 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5663 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5664 e0870e44 2019-05-13 stsp char *template = NULL;
5665 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5666 3ce1b845 2019-07-15 stsp int fd;
5667 33ad4cbe 2019-05-12 jcs size_t len;
5668 33ad4cbe 2019-05-12 jcs
5669 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5670 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5671 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5672 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5673 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5674 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5675 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5676 33ad4cbe 2019-05-12 jcs return NULL;
5677 33ad4cbe 2019-05-12 jcs }
5678 33ad4cbe 2019-05-12 jcs
5679 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5680 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5681 76d98825 2019-06-03 stsp
5682 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5683 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5684 76d98825 2019-06-03 stsp a->branch_name) == -1)
5685 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5686 e0870e44 2019-05-13 stsp
5687 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5688 793c30b5 2019-05-13 stsp if (err)
5689 e0870e44 2019-05-13 stsp goto done;
5690 33ad4cbe 2019-05-12 jcs
5691 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5692 33ad4cbe 2019-05-12 jcs
5693 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
5694 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
5695 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
5696 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
5697 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
5698 33ad4cbe 2019-05-12 jcs }
5699 33ad4cbe 2019-05-12 jcs close(fd);
5700 33ad4cbe 2019-05-12 jcs
5701 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
5702 33ad4cbe 2019-05-12 jcs done:
5703 76d98825 2019-06-03 stsp free(initial_content);
5704 e0870e44 2019-05-13 stsp free(template);
5705 314a6357 2019-05-13 stsp
5706 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
5707 3ce1b845 2019-07-15 stsp if (err == NULL) {
5708 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
5709 3ce1b845 2019-07-15 stsp if (err) {
5710 3ce1b845 2019-07-15 stsp free(*logmsg);
5711 3ce1b845 2019-07-15 stsp *logmsg = NULL;
5712 3ce1b845 2019-07-15 stsp }
5713 3ce1b845 2019-07-15 stsp }
5714 33ad4cbe 2019-05-12 jcs return err;
5715 6bad629b 2019-02-04 stsp }
5716 c4296144 2019-05-09 stsp
5717 c4296144 2019-05-09 stsp static const struct got_error *
5718 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
5719 c4296144 2019-05-09 stsp {
5720 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
5721 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
5722 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
5723 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
5724 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
5725 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
5726 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
5727 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
5728 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
5729 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
5730 5c1e53bc 2019-07-28 stsp
5731 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
5732 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
5733 c4296144 2019-05-09 stsp
5734 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
5735 c4296144 2019-05-09 stsp switch (ch) {
5736 c4296144 2019-05-09 stsp case 'm':
5737 c4296144 2019-05-09 stsp logmsg = optarg;
5738 c4296144 2019-05-09 stsp break;
5739 c4296144 2019-05-09 stsp default:
5740 c4296144 2019-05-09 stsp usage_commit();
5741 c4296144 2019-05-09 stsp /* NOTREACHED */
5742 c4296144 2019-05-09 stsp }
5743 c4296144 2019-05-09 stsp }
5744 c4296144 2019-05-09 stsp
5745 c4296144 2019-05-09 stsp argc -= optind;
5746 c4296144 2019-05-09 stsp argv += optind;
5747 c4296144 2019-05-09 stsp
5748 43012d58 2019-07-14 stsp #ifndef PROFILE
5749 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5750 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5751 43012d58 2019-07-14 stsp err(1, "pledge");
5752 43012d58 2019-07-14 stsp #endif
5753 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
5754 c4296144 2019-05-09 stsp if (cwd == NULL) {
5755 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5756 c4296144 2019-05-09 stsp goto done;
5757 c4296144 2019-05-09 stsp }
5758 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
5759 7d5807f4 2019-07-11 stsp if (error)
5760 7d5807f4 2019-07-11 stsp goto done;
5761 7d5807f4 2019-07-11 stsp
5762 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5763 c4296144 2019-05-09 stsp if (error)
5764 a698f62e 2019-07-25 stsp goto done;
5765 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
5766 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
5767 c4296144 2019-05-09 stsp goto done;
5768 a698f62e 2019-07-25 stsp }
5769 5c1e53bc 2019-07-28 stsp
5770 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5771 916f288c 2019-07-30 stsp worktree);
5772 5c1e53bc 2019-07-28 stsp if (error)
5773 5c1e53bc 2019-07-28 stsp goto done;
5774 c4296144 2019-05-09 stsp
5775 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5776 c9956ddf 2019-09-08 stsp if (error)
5777 c9956ddf 2019-09-08 stsp goto done;
5778 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5779 c9956ddf 2019-09-08 stsp gitconfig_path);
5780 c4296144 2019-05-09 stsp if (error != NULL)
5781 c4296144 2019-05-09 stsp goto done;
5782 c4296144 2019-05-09 stsp
5783 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5784 aba9c984 2019-09-08 stsp if (error)
5785 aba9c984 2019-09-08 stsp return error;
5786 aba9c984 2019-09-08 stsp
5787 314a6357 2019-05-13 stsp /*
5788 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5789 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5790 314a6357 2019-05-13 stsp */
5791 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5792 314a6357 2019-05-13 stsp error = get_editor(&editor);
5793 314a6357 2019-05-13 stsp else
5794 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5795 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5796 c4296144 2019-05-09 stsp if (error)
5797 c4296144 2019-05-09 stsp goto done;
5798 c4296144 2019-05-09 stsp
5799 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5800 087fb88c 2019-08-04 stsp if (error)
5801 087fb88c 2019-08-04 stsp goto done;
5802 087fb88c 2019-08-04 stsp
5803 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5804 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5805 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5806 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5807 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5808 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5809 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5810 916f288c 2019-07-30 stsp goto done;
5811 916f288c 2019-07-30 stsp }
5812 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5813 916f288c 2019-07-30 stsp }
5814 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5815 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5816 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5817 e0870e44 2019-05-13 stsp if (error) {
5818 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5819 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5820 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5821 c4296144 2019-05-09 stsp goto done;
5822 e0870e44 2019-05-13 stsp }
5823 c4296144 2019-05-09 stsp
5824 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5825 c4296144 2019-05-09 stsp if (error)
5826 c4296144 2019-05-09 stsp goto done;
5827 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5828 c4296144 2019-05-09 stsp done:
5829 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5830 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5831 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5832 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5833 7266f21f 2019-10-21 stsp error == NULL)
5834 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5835 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5836 c4296144 2019-05-09 stsp if (repo)
5837 c4296144 2019-05-09 stsp got_repo_close(repo);
5838 c4296144 2019-05-09 stsp if (worktree)
5839 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5840 c4296144 2019-05-09 stsp free(cwd);
5841 c4296144 2019-05-09 stsp free(id_str);
5842 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5843 e2ba3d07 2019-05-13 stsp free(editor);
5844 aba9c984 2019-09-08 stsp free(author);
5845 234035bc 2019-06-01 stsp return error;
5846 234035bc 2019-06-01 stsp }
5847 234035bc 2019-06-01 stsp
5848 234035bc 2019-06-01 stsp __dead static void
5849 234035bc 2019-06-01 stsp usage_cherrypick(void)
5850 234035bc 2019-06-01 stsp {
5851 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5852 234035bc 2019-06-01 stsp exit(1);
5853 234035bc 2019-06-01 stsp }
5854 234035bc 2019-06-01 stsp
5855 234035bc 2019-06-01 stsp static const struct got_error *
5856 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5857 234035bc 2019-06-01 stsp {
5858 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5859 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5860 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5861 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5862 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5863 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5864 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5865 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5866 234035bc 2019-06-01 stsp int ch, did_something = 0;
5867 234035bc 2019-06-01 stsp
5868 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5869 234035bc 2019-06-01 stsp switch (ch) {
5870 234035bc 2019-06-01 stsp default:
5871 234035bc 2019-06-01 stsp usage_cherrypick();
5872 234035bc 2019-06-01 stsp /* NOTREACHED */
5873 234035bc 2019-06-01 stsp }
5874 234035bc 2019-06-01 stsp }
5875 234035bc 2019-06-01 stsp
5876 234035bc 2019-06-01 stsp argc -= optind;
5877 234035bc 2019-06-01 stsp argv += optind;
5878 234035bc 2019-06-01 stsp
5879 43012d58 2019-07-14 stsp #ifndef PROFILE
5880 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5881 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5882 43012d58 2019-07-14 stsp err(1, "pledge");
5883 43012d58 2019-07-14 stsp #endif
5884 234035bc 2019-06-01 stsp if (argc != 1)
5885 234035bc 2019-06-01 stsp usage_cherrypick();
5886 234035bc 2019-06-01 stsp
5887 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5888 234035bc 2019-06-01 stsp if (cwd == NULL) {
5889 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5890 234035bc 2019-06-01 stsp goto done;
5891 234035bc 2019-06-01 stsp }
5892 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5893 234035bc 2019-06-01 stsp if (error)
5894 234035bc 2019-06-01 stsp goto done;
5895 234035bc 2019-06-01 stsp
5896 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5897 c9956ddf 2019-09-08 stsp NULL);
5898 234035bc 2019-06-01 stsp if (error != NULL)
5899 234035bc 2019-06-01 stsp goto done;
5900 234035bc 2019-06-01 stsp
5901 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5902 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5903 234035bc 2019-06-01 stsp if (error)
5904 234035bc 2019-06-01 stsp goto done;
5905 234035bc 2019-06-01 stsp
5906 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5907 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5908 234035bc 2019-06-01 stsp if (error != NULL) {
5909 234035bc 2019-06-01 stsp struct got_reference *ref;
5910 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5911 234035bc 2019-06-01 stsp goto done;
5912 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5913 234035bc 2019-06-01 stsp if (error != NULL)
5914 234035bc 2019-06-01 stsp goto done;
5915 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5916 234035bc 2019-06-01 stsp got_ref_close(ref);
5917 234035bc 2019-06-01 stsp if (error != NULL)
5918 234035bc 2019-06-01 stsp goto done;
5919 234035bc 2019-06-01 stsp }
5920 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5921 234035bc 2019-06-01 stsp if (error)
5922 234035bc 2019-06-01 stsp goto done;
5923 234035bc 2019-06-01 stsp
5924 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5925 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5926 234035bc 2019-06-01 stsp if (error != NULL)
5927 234035bc 2019-06-01 stsp goto done;
5928 234035bc 2019-06-01 stsp
5929 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5930 234035bc 2019-06-01 stsp if (error) {
5931 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5932 234035bc 2019-06-01 stsp goto done;
5933 234035bc 2019-06-01 stsp error = NULL;
5934 234035bc 2019-06-01 stsp } else {
5935 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5936 234035bc 2019-06-01 stsp goto done;
5937 234035bc 2019-06-01 stsp }
5938 234035bc 2019-06-01 stsp
5939 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5940 234035bc 2019-06-01 stsp if (error)
5941 234035bc 2019-06-01 stsp goto done;
5942 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5943 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5944 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5945 03415a1a 2019-06-02 stsp NULL);
5946 234035bc 2019-06-01 stsp if (error != NULL)
5947 234035bc 2019-06-01 stsp goto done;
5948 234035bc 2019-06-01 stsp
5949 234035bc 2019-06-01 stsp if (did_something)
5950 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5951 234035bc 2019-06-01 stsp done:
5952 234035bc 2019-06-01 stsp if (commit)
5953 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5954 234035bc 2019-06-01 stsp free(commit_id_str);
5955 234035bc 2019-06-01 stsp if (head_ref)
5956 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5957 234035bc 2019-06-01 stsp if (worktree)
5958 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5959 234035bc 2019-06-01 stsp if (repo)
5960 234035bc 2019-06-01 stsp got_repo_close(repo);
5961 c4296144 2019-05-09 stsp return error;
5962 c4296144 2019-05-09 stsp }
5963 5ef14e63 2019-06-02 stsp
5964 5ef14e63 2019-06-02 stsp __dead static void
5965 5ef14e63 2019-06-02 stsp usage_backout(void)
5966 5ef14e63 2019-06-02 stsp {
5967 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5968 5ef14e63 2019-06-02 stsp exit(1);
5969 5ef14e63 2019-06-02 stsp }
5970 5ef14e63 2019-06-02 stsp
5971 5ef14e63 2019-06-02 stsp static const struct got_error *
5972 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5973 5ef14e63 2019-06-02 stsp {
5974 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5975 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5976 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5977 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5978 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5979 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5980 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5981 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5982 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5983 5ef14e63 2019-06-02 stsp
5984 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5985 5ef14e63 2019-06-02 stsp switch (ch) {
5986 5ef14e63 2019-06-02 stsp default:
5987 5ef14e63 2019-06-02 stsp usage_backout();
5988 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5989 5ef14e63 2019-06-02 stsp }
5990 5ef14e63 2019-06-02 stsp }
5991 5ef14e63 2019-06-02 stsp
5992 5ef14e63 2019-06-02 stsp argc -= optind;
5993 5ef14e63 2019-06-02 stsp argv += optind;
5994 5ef14e63 2019-06-02 stsp
5995 43012d58 2019-07-14 stsp #ifndef PROFILE
5996 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5997 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5998 43012d58 2019-07-14 stsp err(1, "pledge");
5999 43012d58 2019-07-14 stsp #endif
6000 5ef14e63 2019-06-02 stsp if (argc != 1)
6001 5ef14e63 2019-06-02 stsp usage_backout();
6002 5ef14e63 2019-06-02 stsp
6003 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6004 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6005 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6006 5ef14e63 2019-06-02 stsp goto done;
6007 5ef14e63 2019-06-02 stsp }
6008 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6009 5ef14e63 2019-06-02 stsp if (error)
6010 5ef14e63 2019-06-02 stsp goto done;
6011 5ef14e63 2019-06-02 stsp
6012 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6013 c9956ddf 2019-09-08 stsp NULL);
6014 5ef14e63 2019-06-02 stsp if (error != NULL)
6015 5ef14e63 2019-06-02 stsp goto done;
6016 5ef14e63 2019-06-02 stsp
6017 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6019 5ef14e63 2019-06-02 stsp if (error)
6020 5ef14e63 2019-06-02 stsp goto done;
6021 5ef14e63 2019-06-02 stsp
6022 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6023 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6024 5ef14e63 2019-06-02 stsp if (error != NULL) {
6025 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6026 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6027 5ef14e63 2019-06-02 stsp goto done;
6028 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6029 5ef14e63 2019-06-02 stsp if (error != NULL)
6030 5ef14e63 2019-06-02 stsp goto done;
6031 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6032 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6033 5ef14e63 2019-06-02 stsp if (error != NULL)
6034 5ef14e63 2019-06-02 stsp goto done;
6035 5ef14e63 2019-06-02 stsp }
6036 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6037 5ef14e63 2019-06-02 stsp if (error)
6038 5ef14e63 2019-06-02 stsp goto done;
6039 5ef14e63 2019-06-02 stsp
6040 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6041 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6042 5ef14e63 2019-06-02 stsp if (error != NULL)
6043 5ef14e63 2019-06-02 stsp goto done;
6044 5ef14e63 2019-06-02 stsp
6045 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6046 5ef14e63 2019-06-02 stsp if (error)
6047 5ef14e63 2019-06-02 stsp goto done;
6048 5ef14e63 2019-06-02 stsp
6049 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6050 5ef14e63 2019-06-02 stsp if (error)
6051 5ef14e63 2019-06-02 stsp goto done;
6052 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6053 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6054 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6055 5ef14e63 2019-06-02 stsp goto done;
6056 5ef14e63 2019-06-02 stsp }
6057 5ef14e63 2019-06-02 stsp
6058 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6059 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6060 5ef14e63 2019-06-02 stsp if (error != NULL)
6061 5ef14e63 2019-06-02 stsp goto done;
6062 5ef14e63 2019-06-02 stsp
6063 5ef14e63 2019-06-02 stsp if (did_something)
6064 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6065 5ef14e63 2019-06-02 stsp done:
6066 5ef14e63 2019-06-02 stsp if (commit)
6067 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6068 5ef14e63 2019-06-02 stsp free(commit_id_str);
6069 5ef14e63 2019-06-02 stsp if (head_ref)
6070 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6071 818c7501 2019-07-11 stsp if (worktree)
6072 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6073 818c7501 2019-07-11 stsp if (repo)
6074 818c7501 2019-07-11 stsp got_repo_close(repo);
6075 818c7501 2019-07-11 stsp return error;
6076 818c7501 2019-07-11 stsp }
6077 818c7501 2019-07-11 stsp
6078 818c7501 2019-07-11 stsp __dead static void
6079 818c7501 2019-07-11 stsp usage_rebase(void)
6080 818c7501 2019-07-11 stsp {
6081 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6082 af54c8f8 2019-07-11 stsp getprogname());
6083 818c7501 2019-07-11 stsp exit(1);
6084 0ebf8283 2019-07-24 stsp }
6085 0ebf8283 2019-07-24 stsp
6086 0ebf8283 2019-07-24 stsp void
6087 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6088 0ebf8283 2019-07-24 stsp {
6089 0ebf8283 2019-07-24 stsp char *nl;
6090 0ebf8283 2019-07-24 stsp size_t len;
6091 0ebf8283 2019-07-24 stsp
6092 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6093 0ebf8283 2019-07-24 stsp if (len > limit)
6094 0ebf8283 2019-07-24 stsp len = limit;
6095 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6096 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6097 0ebf8283 2019-07-24 stsp if (nl)
6098 0ebf8283 2019-07-24 stsp *nl = '\0';
6099 0ebf8283 2019-07-24 stsp }
6100 0ebf8283 2019-07-24 stsp
6101 0ebf8283 2019-07-24 stsp static const struct got_error *
6102 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6103 0ebf8283 2019-07-24 stsp {
6104 5943eee2 2019-08-13 stsp const struct got_error *err;
6105 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6106 5943eee2 2019-08-13 stsp const char *s;
6107 0ebf8283 2019-07-24 stsp
6108 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6109 5943eee2 2019-08-13 stsp if (err)
6110 5943eee2 2019-08-13 stsp return err;
6111 0ebf8283 2019-07-24 stsp
6112 5943eee2 2019-08-13 stsp s = logmsg0;
6113 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6114 5943eee2 2019-08-13 stsp s++;
6115 5943eee2 2019-08-13 stsp
6116 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6117 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6118 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6119 5943eee2 2019-08-13 stsp goto done;
6120 5943eee2 2019-08-13 stsp }
6121 0ebf8283 2019-07-24 stsp
6122 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6123 5943eee2 2019-08-13 stsp done:
6124 5943eee2 2019-08-13 stsp free(logmsg0);
6125 a0ea4fc0 2020-02-28 stsp return err;
6126 a0ea4fc0 2020-02-28 stsp }
6127 a0ea4fc0 2020-02-28 stsp
6128 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6129 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6130 a0ea4fc0 2020-02-28 stsp {
6131 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6132 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6133 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6134 a0ea4fc0 2020-02-28 stsp
6135 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6136 a0ea4fc0 2020-02-28 stsp if (err)
6137 a0ea4fc0 2020-02-28 stsp return err;
6138 a0ea4fc0 2020-02-28 stsp
6139 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6140 a0ea4fc0 2020-02-28 stsp if (err)
6141 a0ea4fc0 2020-02-28 stsp goto done;
6142 a0ea4fc0 2020-02-28 stsp
6143 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6144 a0ea4fc0 2020-02-28 stsp
6145 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6146 a0ea4fc0 2020-02-28 stsp if (err)
6147 a0ea4fc0 2020-02-28 stsp goto done;
6148 a0ea4fc0 2020-02-28 stsp
6149 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6150 a0ea4fc0 2020-02-28 stsp done:
6151 a0ea4fc0 2020-02-28 stsp free(id_str);
6152 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6153 a0ea4fc0 2020-02-28 stsp free(logmsg);
6154 5943eee2 2019-08-13 stsp return err;
6155 818c7501 2019-07-11 stsp }
6156 818c7501 2019-07-11 stsp
6157 818c7501 2019-07-11 stsp static const struct got_error *
6158 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6159 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6160 818c7501 2019-07-11 stsp {
6161 818c7501 2019-07-11 stsp const struct got_error *err;
6162 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6163 818c7501 2019-07-11 stsp
6164 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6165 818c7501 2019-07-11 stsp if (err)
6166 818c7501 2019-07-11 stsp goto done;
6167 818c7501 2019-07-11 stsp
6168 ff0d2220 2019-07-11 stsp if (new_id) {
6169 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6170 ff0d2220 2019-07-11 stsp if (err)
6171 ff0d2220 2019-07-11 stsp goto done;
6172 ff0d2220 2019-07-11 stsp }
6173 818c7501 2019-07-11 stsp
6174 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6175 ff0d2220 2019-07-11 stsp if (new_id_str)
6176 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6177 0ebf8283 2019-07-24 stsp
6178 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6179 0ebf8283 2019-07-24 stsp if (err)
6180 0ebf8283 2019-07-24 stsp goto done;
6181 0ebf8283 2019-07-24 stsp
6182 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6183 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6184 818c7501 2019-07-11 stsp done:
6185 818c7501 2019-07-11 stsp free(old_id_str);
6186 818c7501 2019-07-11 stsp free(new_id_str);
6187 272a1371 2020-02-28 stsp free(logmsg);
6188 818c7501 2019-07-11 stsp return err;
6189 818c7501 2019-07-11 stsp }
6190 818c7501 2019-07-11 stsp
6191 1ee397ad 2019-07-12 stsp static const struct got_error *
6192 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6193 818c7501 2019-07-11 stsp {
6194 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6195 818c7501 2019-07-11 stsp
6196 818c7501 2019-07-11 stsp while (path[0] == '/')
6197 818c7501 2019-07-11 stsp path++;
6198 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6199 818c7501 2019-07-11 stsp
6200 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6201 1ee397ad 2019-07-12 stsp return NULL;
6202 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6203 818c7501 2019-07-11 stsp *rebase_status = status;
6204 1ee397ad 2019-07-12 stsp return NULL;
6205 818c7501 2019-07-11 stsp }
6206 818c7501 2019-07-11 stsp
6207 818c7501 2019-07-11 stsp static const struct got_error *
6208 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6209 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6210 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6211 818c7501 2019-07-11 stsp {
6212 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6213 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6214 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6215 818c7501 2019-07-11 stsp }
6216 818c7501 2019-07-11 stsp
6217 818c7501 2019-07-11 stsp static const struct got_error *
6218 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6219 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6220 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6221 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6222 ff0d2220 2019-07-11 stsp {
6223 ff0d2220 2019-07-11 stsp const struct got_error *error;
6224 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6225 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6226 ff0d2220 2019-07-11 stsp
6227 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6228 ff0d2220 2019-07-11 stsp if (error)
6229 ff0d2220 2019-07-11 stsp return error;
6230 ff0d2220 2019-07-11 stsp
6231 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6232 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6233 ff0d2220 2019-07-11 stsp if (error) {
6234 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6235 ff0d2220 2019-07-11 stsp goto done;
6236 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6237 ff0d2220 2019-07-11 stsp } else {
6238 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6239 ff0d2220 2019-07-11 stsp free(new_commit_id);
6240 ff0d2220 2019-07-11 stsp }
6241 ff0d2220 2019-07-11 stsp done:
6242 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6243 ff0d2220 2019-07-11 stsp return error;
6244 64c6d990 2019-07-11 stsp }
6245 64c6d990 2019-07-11 stsp
6246 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6247 64c6d990 2019-07-11 stsp const char *path_prefix;
6248 64c6d990 2019-07-11 stsp size_t len;
6249 8ca9bd68 2019-07-25 stsp int errcode;
6250 64c6d990 2019-07-11 stsp };
6251 64c6d990 2019-07-11 stsp
6252 64c6d990 2019-07-11 stsp static const struct got_error *
6253 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6254 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6255 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6256 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6257 64c6d990 2019-07-11 stsp {
6258 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6259 64c6d990 2019-07-11 stsp
6260 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6261 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6262 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6263 64c6d990 2019-07-11 stsp
6264 64c6d990 2019-07-11 stsp return NULL;
6265 64c6d990 2019-07-11 stsp }
6266 64c6d990 2019-07-11 stsp
6267 64c6d990 2019-07-11 stsp static const struct got_error *
6268 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6269 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6270 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6271 64c6d990 2019-07-11 stsp {
6272 64c6d990 2019-07-11 stsp const struct got_error *err;
6273 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6274 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6275 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6276 64c6d990 2019-07-11 stsp
6277 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6278 64c6d990 2019-07-11 stsp return NULL;
6279 64c6d990 2019-07-11 stsp
6280 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6281 64c6d990 2019-07-11 stsp if (err)
6282 64c6d990 2019-07-11 stsp goto done;
6283 64c6d990 2019-07-11 stsp
6284 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6285 64c6d990 2019-07-11 stsp if (err)
6286 64c6d990 2019-07-11 stsp goto done;
6287 64c6d990 2019-07-11 stsp
6288 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6289 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6290 64c6d990 2019-07-11 stsp if (err)
6291 64c6d990 2019-07-11 stsp goto done;
6292 64c6d990 2019-07-11 stsp
6293 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6294 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6295 64c6d990 2019-07-11 stsp if (err)
6296 64c6d990 2019-07-11 stsp goto done;
6297 64c6d990 2019-07-11 stsp
6298 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6299 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6300 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6301 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6302 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6303 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6304 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6305 64c6d990 2019-07-11 stsp done:
6306 64c6d990 2019-07-11 stsp if (tree1)
6307 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6308 64c6d990 2019-07-11 stsp if (tree2)
6309 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6310 64c6d990 2019-07-11 stsp if (commit)
6311 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6312 64c6d990 2019-07-11 stsp if (parent_commit)
6313 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6314 64c6d990 2019-07-11 stsp return err;
6315 ff0d2220 2019-07-11 stsp }
6316 ff0d2220 2019-07-11 stsp
6317 ff0d2220 2019-07-11 stsp static const struct got_error *
6318 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6319 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6320 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6321 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6322 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6323 af61c510 2019-07-19 stsp {
6324 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6325 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6326 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6327 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6328 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6329 af61c510 2019-07-19 stsp
6330 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6331 af61c510 2019-07-19 stsp if (err)
6332 af61c510 2019-07-19 stsp return err;
6333 af61c510 2019-07-19 stsp
6334 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6335 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6336 af61c510 2019-07-19 stsp if (err)
6337 af61c510 2019-07-19 stsp goto done;
6338 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6339 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6340 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6341 af61c510 2019-07-19 stsp if (err) {
6342 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6343 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6344 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6345 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6346 af61c510 2019-07-19 stsp "been reached?!?");
6347 ee780d5c 2020-01-04 stsp }
6348 ee780d5c 2020-01-04 stsp goto done;
6349 af61c510 2019-07-19 stsp } else {
6350 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6351 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6352 af61c510 2019-07-19 stsp if (err)
6353 af61c510 2019-07-19 stsp goto done;
6354 af61c510 2019-07-19 stsp
6355 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6356 af61c510 2019-07-19 stsp if (err)
6357 af61c510 2019-07-19 stsp goto done;
6358 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6359 af61c510 2019-07-19 stsp commit_id = parent_id;
6360 af61c510 2019-07-19 stsp }
6361 af61c510 2019-07-19 stsp }
6362 af61c510 2019-07-19 stsp done:
6363 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6364 af61c510 2019-07-19 stsp return err;
6365 af61c510 2019-07-19 stsp }
6366 af61c510 2019-07-19 stsp
6367 af61c510 2019-07-19 stsp static const struct got_error *
6368 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6369 818c7501 2019-07-11 stsp {
6370 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6371 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6372 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6373 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6374 818c7501 2019-07-11 stsp char *cwd = NULL;
6375 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6376 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6377 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6378 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6379 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6380 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6381 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6382 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6383 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6384 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6385 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6386 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6387 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6388 818c7501 2019-07-11 stsp
6389 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6390 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6391 818c7501 2019-07-11 stsp
6392 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6393 818c7501 2019-07-11 stsp switch (ch) {
6394 818c7501 2019-07-11 stsp case 'a':
6395 818c7501 2019-07-11 stsp abort_rebase = 1;
6396 818c7501 2019-07-11 stsp break;
6397 818c7501 2019-07-11 stsp case 'c':
6398 818c7501 2019-07-11 stsp continue_rebase = 1;
6399 818c7501 2019-07-11 stsp break;
6400 818c7501 2019-07-11 stsp default:
6401 818c7501 2019-07-11 stsp usage_rebase();
6402 818c7501 2019-07-11 stsp /* NOTREACHED */
6403 818c7501 2019-07-11 stsp }
6404 818c7501 2019-07-11 stsp }
6405 818c7501 2019-07-11 stsp
6406 818c7501 2019-07-11 stsp argc -= optind;
6407 818c7501 2019-07-11 stsp argv += optind;
6408 818c7501 2019-07-11 stsp
6409 43012d58 2019-07-14 stsp #ifndef PROFILE
6410 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6411 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6412 43012d58 2019-07-14 stsp err(1, "pledge");
6413 43012d58 2019-07-14 stsp #endif
6414 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6415 818c7501 2019-07-11 stsp usage_rebase();
6416 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6417 818c7501 2019-07-11 stsp if (argc != 0)
6418 818c7501 2019-07-11 stsp usage_rebase();
6419 818c7501 2019-07-11 stsp } else if (argc != 1)
6420 818c7501 2019-07-11 stsp usage_rebase();
6421 818c7501 2019-07-11 stsp
6422 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6423 818c7501 2019-07-11 stsp if (cwd == NULL) {
6424 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6425 818c7501 2019-07-11 stsp goto done;
6426 818c7501 2019-07-11 stsp }
6427 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6428 818c7501 2019-07-11 stsp if (error)
6429 818c7501 2019-07-11 stsp goto done;
6430 818c7501 2019-07-11 stsp
6431 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6432 c9956ddf 2019-09-08 stsp NULL);
6433 818c7501 2019-07-11 stsp if (error != NULL)
6434 818c7501 2019-07-11 stsp goto done;
6435 818c7501 2019-07-11 stsp
6436 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6437 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6438 7ef62c4e 2020-02-24 stsp if (error)
6439 7ef62c4e 2020-02-24 stsp goto done;
6440 7ef62c4e 2020-02-24 stsp
6441 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6442 7ef62c4e 2020-02-24 stsp worktree);
6443 818c7501 2019-07-11 stsp if (error)
6444 7ef62c4e 2020-02-24 stsp goto done;
6445 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6446 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6447 818c7501 2019-07-11 stsp goto done;
6448 7ef62c4e 2020-02-24 stsp }
6449 818c7501 2019-07-11 stsp
6450 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6451 818c7501 2019-07-11 stsp if (error)
6452 818c7501 2019-07-11 stsp goto done;
6453 818c7501 2019-07-11 stsp
6454 f6794adc 2019-07-23 stsp if (abort_rebase) {
6455 818c7501 2019-07-11 stsp int did_something;
6456 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6457 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6458 f6794adc 2019-07-23 stsp goto done;
6459 f6794adc 2019-07-23 stsp }
6460 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6461 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6462 3e3a69f1 2019-07-25 stsp worktree, repo);
6463 818c7501 2019-07-11 stsp if (error)
6464 818c7501 2019-07-11 stsp goto done;
6465 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6466 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6467 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6468 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6469 818c7501 2019-07-11 stsp if (error)
6470 818c7501 2019-07-11 stsp goto done;
6471 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6472 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6473 818c7501 2019-07-11 stsp }
6474 818c7501 2019-07-11 stsp
6475 818c7501 2019-07-11 stsp if (continue_rebase) {
6476 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6477 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6478 f6794adc 2019-07-23 stsp goto done;
6479 f6794adc 2019-07-23 stsp }
6480 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6481 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6482 3e3a69f1 2019-07-25 stsp worktree, repo);
6483 818c7501 2019-07-11 stsp if (error)
6484 818c7501 2019-07-11 stsp goto done;
6485 818c7501 2019-07-11 stsp
6486 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6487 01757395 2019-07-12 stsp resume_commit_id, repo);
6488 818c7501 2019-07-11 stsp if (error)
6489 818c7501 2019-07-11 stsp goto done;
6490 818c7501 2019-07-11 stsp
6491 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6492 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6493 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6494 818c7501 2019-07-11 stsp goto done;
6495 818c7501 2019-07-11 stsp }
6496 818c7501 2019-07-11 stsp } else {
6497 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6498 818c7501 2019-07-11 stsp if (error != NULL)
6499 818c7501 2019-07-11 stsp goto done;
6500 ff0d2220 2019-07-11 stsp }
6501 818c7501 2019-07-11 stsp
6502 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6503 ff0d2220 2019-07-11 stsp if (error)
6504 ff0d2220 2019-07-11 stsp goto done;
6505 ff0d2220 2019-07-11 stsp
6506 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6507 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6508 a51a74b3 2019-07-27 stsp
6509 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6510 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6511 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6512 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6513 818c7501 2019-07-11 stsp if (error)
6514 818c7501 2019-07-11 stsp goto done;
6515 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6516 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6517 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6518 818c7501 2019-07-11 stsp "with work tree's branch");
6519 818c7501 2019-07-11 stsp goto done;
6520 818c7501 2019-07-11 stsp }
6521 818c7501 2019-07-11 stsp
6522 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6523 a51a74b3 2019-07-27 stsp if (error) {
6524 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6525 a51a74b3 2019-07-27 stsp goto done;
6526 a51a74b3 2019-07-27 stsp error = NULL;
6527 a51a74b3 2019-07-27 stsp } else {
6528 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6529 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6530 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6531 a51a74b3 2019-07-27 stsp goto done;
6532 a51a74b3 2019-07-27 stsp }
6533 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6534 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6535 818c7501 2019-07-11 stsp if (error)
6536 818c7501 2019-07-11 stsp goto done;
6537 818c7501 2019-07-11 stsp }
6538 818c7501 2019-07-11 stsp
6539 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6540 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6541 818c7501 2019-07-11 stsp if (error)
6542 818c7501 2019-07-11 stsp goto done;
6543 818c7501 2019-07-11 stsp
6544 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6545 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6546 fc66b545 2019-08-12 stsp if (pid == NULL) {
6547 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6548 fc66b545 2019-08-12 stsp int did_something;
6549 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6550 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6551 fc66b545 2019-08-12 stsp &did_something);
6552 fc66b545 2019-08-12 stsp if (error)
6553 fc66b545 2019-08-12 stsp goto done;
6554 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6555 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6556 fc66b545 2019-08-12 stsp }
6557 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6558 fc66b545 2019-08-12 stsp goto done;
6559 fc66b545 2019-08-12 stsp }
6560 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6561 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6562 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6563 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6564 818c7501 2019-07-11 stsp commit = NULL;
6565 818c7501 2019-07-11 stsp if (error)
6566 818c7501 2019-07-11 stsp goto done;
6567 64c6d990 2019-07-11 stsp
6568 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6569 38b0338b 2019-11-29 stsp if (continue_rebase) {
6570 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6571 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6572 38b0338b 2019-11-29 stsp goto done;
6573 38b0338b 2019-11-29 stsp } else {
6574 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6575 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6576 38b0338b 2019-11-29 stsp char *id_str;
6577 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6578 38b0338b 2019-11-29 stsp new_base_branch);
6579 38b0338b 2019-11-29 stsp if (error)
6580 38b0338b 2019-11-29 stsp goto done;
6581 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6582 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6583 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6584 38b0338b 2019-11-29 stsp free(id_str);
6585 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6586 38b0338b 2019-11-29 stsp new_head_commit_id);
6587 38b0338b 2019-11-29 stsp if (error)
6588 38b0338b 2019-11-29 stsp goto done;
6589 38b0338b 2019-11-29 stsp }
6590 818c7501 2019-07-11 stsp }
6591 818c7501 2019-07-11 stsp
6592 818c7501 2019-07-11 stsp pid = NULL;
6593 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6594 818c7501 2019-07-11 stsp commit_id = qid->id;
6595 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6596 818c7501 2019-07-11 stsp pid = qid;
6597 818c7501 2019-07-11 stsp
6598 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6599 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6600 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6601 818c7501 2019-07-11 stsp if (error)
6602 818c7501 2019-07-11 stsp goto done;
6603 818c7501 2019-07-11 stsp
6604 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6605 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6606 a0ea4fc0 2020-02-28 stsp if (error)
6607 a0ea4fc0 2020-02-28 stsp goto done;
6608 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6609 818c7501 2019-07-11 stsp break;
6610 01757395 2019-07-12 stsp }
6611 818c7501 2019-07-11 stsp
6612 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6613 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6614 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6615 818c7501 2019-07-11 stsp if (error)
6616 818c7501 2019-07-11 stsp goto done;
6617 818c7501 2019-07-11 stsp }
6618 818c7501 2019-07-11 stsp
6619 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6620 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6621 818c7501 2019-07-11 stsp if (error)
6622 818c7501 2019-07-11 stsp goto done;
6623 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6624 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6625 818c7501 2019-07-11 stsp } else
6626 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6627 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6628 818c7501 2019-07-11 stsp done:
6629 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6630 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6631 818c7501 2019-07-11 stsp free(resume_commit_id);
6632 818c7501 2019-07-11 stsp free(yca_id);
6633 818c7501 2019-07-11 stsp if (commit)
6634 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6635 818c7501 2019-07-11 stsp if (branch)
6636 818c7501 2019-07-11 stsp got_ref_close(branch);
6637 818c7501 2019-07-11 stsp if (new_base_branch)
6638 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6639 818c7501 2019-07-11 stsp if (tmp_branch)
6640 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6641 5ef14e63 2019-06-02 stsp if (worktree)
6642 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6643 5ef14e63 2019-06-02 stsp if (repo)
6644 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6645 5ef14e63 2019-06-02 stsp return error;
6646 0ebf8283 2019-07-24 stsp }
6647 0ebf8283 2019-07-24 stsp
6648 0ebf8283 2019-07-24 stsp __dead static void
6649 0ebf8283 2019-07-24 stsp usage_histedit(void)
6650 0ebf8283 2019-07-24 stsp {
6651 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6652 0ebf8283 2019-07-24 stsp getprogname());
6653 0ebf8283 2019-07-24 stsp exit(1);
6654 0ebf8283 2019-07-24 stsp }
6655 0ebf8283 2019-07-24 stsp
6656 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6657 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6658 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6659 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6660 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6661 0ebf8283 2019-07-24 stsp
6662 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6663 0ebf8283 2019-07-24 stsp unsigned char code;
6664 0ebf8283 2019-07-24 stsp const char *name;
6665 0ebf8283 2019-07-24 stsp const char *desc;
6666 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6667 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6668 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6669 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6670 82997472 2020-01-29 stsp "be used" },
6671 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6672 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6673 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6674 0ebf8283 2019-07-24 stsp };
6675 0ebf8283 2019-07-24 stsp
6676 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6677 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6678 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6679 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6680 0ebf8283 2019-07-24 stsp char *logmsg;
6681 0ebf8283 2019-07-24 stsp };
6682 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6683 0ebf8283 2019-07-24 stsp
6684 0ebf8283 2019-07-24 stsp static const struct got_error *
6685 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6686 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6687 0ebf8283 2019-07-24 stsp {
6688 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6689 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6690 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6691 8138f3e1 2019-08-11 stsp int n;
6692 0ebf8283 2019-07-24 stsp
6693 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6694 0ebf8283 2019-07-24 stsp if (err)
6695 0ebf8283 2019-07-24 stsp goto done;
6696 0ebf8283 2019-07-24 stsp
6697 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
6698 0ebf8283 2019-07-24 stsp if (err)
6699 0ebf8283 2019-07-24 stsp goto done;
6700 0ebf8283 2019-07-24 stsp
6701 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
6702 0ebf8283 2019-07-24 stsp if (err)
6703 0ebf8283 2019-07-24 stsp goto done;
6704 0ebf8283 2019-07-24 stsp
6705 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
6706 0ebf8283 2019-07-24 stsp if (n < 0)
6707 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6708 0ebf8283 2019-07-24 stsp done:
6709 0ebf8283 2019-07-24 stsp if (commit)
6710 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6711 0ebf8283 2019-07-24 stsp free(id_str);
6712 0ebf8283 2019-07-24 stsp free(logmsg);
6713 0ebf8283 2019-07-24 stsp return err;
6714 0ebf8283 2019-07-24 stsp }
6715 0ebf8283 2019-07-24 stsp
6716 0ebf8283 2019-07-24 stsp static const struct got_error *
6717 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
6718 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
6719 0ebf8283 2019-07-24 stsp {
6720 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6721 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
6722 0ebf8283 2019-07-24 stsp
6723 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
6724 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6725 0ebf8283 2019-07-24 stsp
6726 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6727 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
6728 0ebf8283 2019-07-24 stsp f, repo);
6729 0ebf8283 2019-07-24 stsp if (err)
6730 0ebf8283 2019-07-24 stsp break;
6731 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6732 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
6733 083957f4 2020-02-24 stsp if (n < 0) {
6734 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
6735 083957f4 2020-02-24 stsp break;
6736 083957f4 2020-02-24 stsp }
6737 083957f4 2020-02-24 stsp }
6738 0ebf8283 2019-07-24 stsp }
6739 0ebf8283 2019-07-24 stsp
6740 0ebf8283 2019-07-24 stsp return err;
6741 0ebf8283 2019-07-24 stsp }
6742 0ebf8283 2019-07-24 stsp
6743 0ebf8283 2019-07-24 stsp static const struct got_error *
6744 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
6745 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
6746 0ebf8283 2019-07-24 stsp {
6747 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6748 0ebf8283 2019-07-24 stsp int n, i;
6749 514f2ffe 2020-01-29 stsp char *id_str;
6750 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
6751 514f2ffe 2020-01-29 stsp
6752 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
6753 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
6754 514f2ffe 2020-01-29 stsp if (err)
6755 514f2ffe 2020-01-29 stsp return err;
6756 514f2ffe 2020-01-29 stsp
6757 514f2ffe 2020-01-29 stsp n = fprintf(f,
6758 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
6759 514f2ffe 2020-01-29 stsp "# commit %s\n"
6760 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
6761 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
6762 514f2ffe 2020-01-29 stsp if (n < 0) {
6763 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6764 514f2ffe 2020-01-29 stsp goto done;
6765 514f2ffe 2020-01-29 stsp }
6766 0ebf8283 2019-07-24 stsp
6767 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
6768 514f2ffe 2020-01-29 stsp if (n < 0) {
6769 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
6770 514f2ffe 2020-01-29 stsp goto done;
6771 514f2ffe 2020-01-29 stsp }
6772 0ebf8283 2019-07-24 stsp
6773 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6774 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
6775 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
6776 0ebf8283 2019-07-24 stsp cmd->desc);
6777 0ebf8283 2019-07-24 stsp if (n < 0) {
6778 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6779 0ebf8283 2019-07-24 stsp break;
6780 0ebf8283 2019-07-24 stsp }
6781 0ebf8283 2019-07-24 stsp }
6782 514f2ffe 2020-01-29 stsp done:
6783 514f2ffe 2020-01-29 stsp free(id_str);
6784 0ebf8283 2019-07-24 stsp return err;
6785 0ebf8283 2019-07-24 stsp }
6786 0ebf8283 2019-07-24 stsp
6787 0ebf8283 2019-07-24 stsp static const struct got_error *
6788 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6789 0ebf8283 2019-07-24 stsp {
6790 0ebf8283 2019-07-24 stsp static char msg[42];
6791 0ebf8283 2019-07-24 stsp int ret;
6792 0ebf8283 2019-07-24 stsp
6793 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6794 0ebf8283 2019-07-24 stsp lineno);
6795 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6796 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6797 0ebf8283 2019-07-24 stsp
6798 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6799 0ebf8283 2019-07-24 stsp }
6800 0ebf8283 2019-07-24 stsp
6801 0ebf8283 2019-07-24 stsp static const struct got_error *
6802 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6803 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6804 0ebf8283 2019-07-24 stsp {
6805 0ebf8283 2019-07-24 stsp const struct got_error *err;
6806 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6807 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6808 0ebf8283 2019-07-24 stsp
6809 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6810 0ebf8283 2019-07-24 stsp if (err)
6811 0ebf8283 2019-07-24 stsp return err;
6812 0ebf8283 2019-07-24 stsp
6813 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6814 0ebf8283 2019-07-24 stsp if (err)
6815 0ebf8283 2019-07-24 stsp goto done;
6816 0ebf8283 2019-07-24 stsp
6817 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6818 5943eee2 2019-08-13 stsp if (err)
6819 5943eee2 2019-08-13 stsp goto done;
6820 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6821 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6822 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6823 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6824 0ebf8283 2019-07-24 stsp }
6825 0ebf8283 2019-07-24 stsp done:
6826 0ebf8283 2019-07-24 stsp if (folded_commit)
6827 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6828 0ebf8283 2019-07-24 stsp free(id_str);
6829 5943eee2 2019-08-13 stsp free(folded_logmsg);
6830 0ebf8283 2019-07-24 stsp return err;
6831 0ebf8283 2019-07-24 stsp }
6832 0ebf8283 2019-07-24 stsp
6833 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6834 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6835 0ebf8283 2019-07-24 stsp {
6836 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6837 0ebf8283 2019-07-24 stsp
6838 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6839 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6840 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6841 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6842 3f9de99f 2019-07-24 stsp folded = prev;
6843 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6844 0ebf8283 2019-07-24 stsp }
6845 0ebf8283 2019-07-24 stsp
6846 0ebf8283 2019-07-24 stsp return folded;
6847 0ebf8283 2019-07-24 stsp }
6848 0ebf8283 2019-07-24 stsp
6849 0ebf8283 2019-07-24 stsp static const struct got_error *
6850 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6851 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6852 0ebf8283 2019-07-24 stsp {
6853 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6854 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6855 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6856 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6857 0ebf8283 2019-07-24 stsp int fd;
6858 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6859 0ebf8283 2019-07-24 stsp
6860 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6861 0ebf8283 2019-07-24 stsp if (err)
6862 0ebf8283 2019-07-24 stsp return err;
6863 0ebf8283 2019-07-24 stsp
6864 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6865 0ebf8283 2019-07-24 stsp if (folded) {
6866 0ebf8283 2019-07-24 stsp while (folded != hle) {
6867 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6868 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6869 3f9de99f 2019-07-24 stsp continue;
6870 3f9de99f 2019-07-24 stsp }
6871 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6872 0ebf8283 2019-07-24 stsp logmsg, repo);
6873 0ebf8283 2019-07-24 stsp if (err)
6874 0ebf8283 2019-07-24 stsp goto done;
6875 0ebf8283 2019-07-24 stsp free(logmsg);
6876 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6877 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6878 0ebf8283 2019-07-24 stsp }
6879 0ebf8283 2019-07-24 stsp }
6880 0ebf8283 2019-07-24 stsp
6881 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6882 0ebf8283 2019-07-24 stsp if (err)
6883 0ebf8283 2019-07-24 stsp goto done;
6884 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6885 5943eee2 2019-08-13 stsp if (err)
6886 5943eee2 2019-08-13 stsp goto done;
6887 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6888 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6889 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6890 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6891 0ebf8283 2019-07-24 stsp goto done;
6892 0ebf8283 2019-07-24 stsp }
6893 0ebf8283 2019-07-24 stsp free(logmsg);
6894 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6895 0ebf8283 2019-07-24 stsp
6896 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6897 0ebf8283 2019-07-24 stsp if (err)
6898 0ebf8283 2019-07-24 stsp goto done;
6899 0ebf8283 2019-07-24 stsp
6900 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6901 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6902 0ebf8283 2019-07-24 stsp if (err)
6903 0ebf8283 2019-07-24 stsp goto done;
6904 0ebf8283 2019-07-24 stsp
6905 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6906 0ebf8283 2019-07-24 stsp close(fd);
6907 0ebf8283 2019-07-24 stsp
6908 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6909 0ebf8283 2019-07-24 stsp if (err)
6910 0ebf8283 2019-07-24 stsp goto done;
6911 0ebf8283 2019-07-24 stsp
6912 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6913 0ebf8283 2019-07-24 stsp if (err) {
6914 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6915 0ebf8283 2019-07-24 stsp goto done;
6916 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6917 0ebf8283 2019-07-24 stsp }
6918 0ebf8283 2019-07-24 stsp done:
6919 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6920 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6921 0ebf8283 2019-07-24 stsp free(logmsg_path);
6922 0ebf8283 2019-07-24 stsp free(logmsg);
6923 5943eee2 2019-08-13 stsp free(orig_logmsg);
6924 0ebf8283 2019-07-24 stsp free(editor);
6925 0ebf8283 2019-07-24 stsp if (commit)
6926 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6927 0ebf8283 2019-07-24 stsp return err;
6928 5ef14e63 2019-06-02 stsp }
6929 0ebf8283 2019-07-24 stsp
6930 0ebf8283 2019-07-24 stsp static const struct got_error *
6931 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6932 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6933 0ebf8283 2019-07-24 stsp {
6934 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6935 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6936 0ebf8283 2019-07-24 stsp size_t size;
6937 0ebf8283 2019-07-24 stsp ssize_t len;
6938 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6939 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6940 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6941 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6942 0ebf8283 2019-07-24 stsp
6943 0ebf8283 2019-07-24 stsp for (;;) {
6944 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6945 0ebf8283 2019-07-24 stsp if (len == -1) {
6946 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6947 0ebf8283 2019-07-24 stsp if (feof(f))
6948 0ebf8283 2019-07-24 stsp break;
6949 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6950 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6951 0ebf8283 2019-07-24 stsp break;
6952 0ebf8283 2019-07-24 stsp }
6953 0ebf8283 2019-07-24 stsp lineno++;
6954 0ebf8283 2019-07-24 stsp p = line;
6955 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6956 0ebf8283 2019-07-24 stsp p++;
6957 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6958 0ebf8283 2019-07-24 stsp free(line);
6959 0ebf8283 2019-07-24 stsp line = NULL;
6960 0ebf8283 2019-07-24 stsp continue;
6961 0ebf8283 2019-07-24 stsp }
6962 0ebf8283 2019-07-24 stsp cmd = NULL;
6963 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6964 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6965 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6966 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6967 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6968 0ebf8283 2019-07-24 stsp break;
6969 0ebf8283 2019-07-24 stsp }
6970 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6971 0ebf8283 2019-07-24 stsp p++;
6972 0ebf8283 2019-07-24 stsp break;
6973 0ebf8283 2019-07-24 stsp }
6974 0ebf8283 2019-07-24 stsp }
6975 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6976 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6977 0ebf8283 2019-07-24 stsp break;
6978 0ebf8283 2019-07-24 stsp }
6979 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6980 0ebf8283 2019-07-24 stsp p++;
6981 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6982 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6983 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6984 0ebf8283 2019-07-24 stsp break;
6985 0ebf8283 2019-07-24 stsp }
6986 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6987 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6988 0ebf8283 2019-07-24 stsp if (err)
6989 0ebf8283 2019-07-24 stsp break;
6990 0ebf8283 2019-07-24 stsp } else {
6991 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6992 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6993 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6994 0ebf8283 2019-07-24 stsp break;
6995 0ebf8283 2019-07-24 stsp }
6996 0ebf8283 2019-07-24 stsp }
6997 0ebf8283 2019-07-24 stsp free(line);
6998 0ebf8283 2019-07-24 stsp line = NULL;
6999 0ebf8283 2019-07-24 stsp continue;
7000 0ebf8283 2019-07-24 stsp } else {
7001 0ebf8283 2019-07-24 stsp end = p;
7002 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7003 0ebf8283 2019-07-24 stsp end++;
7004 0ebf8283 2019-07-24 stsp *end = '\0';
7005 0ebf8283 2019-07-24 stsp
7006 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7007 0ebf8283 2019-07-24 stsp if (err) {
7008 0ebf8283 2019-07-24 stsp /* override error code */
7009 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7010 0ebf8283 2019-07-24 stsp break;
7011 0ebf8283 2019-07-24 stsp }
7012 0ebf8283 2019-07-24 stsp }
7013 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7014 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7015 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7016 0ebf8283 2019-07-24 stsp break;
7017 0ebf8283 2019-07-24 stsp }
7018 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7019 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7020 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7021 0ebf8283 2019-07-24 stsp commit_id = NULL;
7022 0ebf8283 2019-07-24 stsp free(line);
7023 0ebf8283 2019-07-24 stsp line = NULL;
7024 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7025 0ebf8283 2019-07-24 stsp }
7026 0ebf8283 2019-07-24 stsp
7027 0ebf8283 2019-07-24 stsp free(line);
7028 0ebf8283 2019-07-24 stsp free(commit_id);
7029 0ebf8283 2019-07-24 stsp return err;
7030 0ebf8283 2019-07-24 stsp }
7031 0ebf8283 2019-07-24 stsp
7032 0ebf8283 2019-07-24 stsp static const struct got_error *
7033 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7034 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7035 bfce7f83 2019-07-27 stsp {
7036 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7037 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7038 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7039 5b87815e 2020-03-05 stsp static char msg[92];
7040 bfce7f83 2019-07-27 stsp char *id_str;
7041 bfce7f83 2019-07-27 stsp
7042 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7043 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7044 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7045 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7046 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7047 5b87815e 2020-03-05 stsp
7048 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7049 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7050 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7051 5b87815e 2020-03-05 stsp if (hle == hle2)
7052 5b87815e 2020-03-05 stsp continue;
7053 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7054 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7055 5b87815e 2020-03-05 stsp continue;
7056 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7057 5b87815e 2020-03-05 stsp if (err)
7058 5b87815e 2020-03-05 stsp return err;
7059 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7060 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7061 5b87815e 2020-03-05 stsp free(id_str);
7062 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7063 5b87815e 2020-03-05 stsp }
7064 5b87815e 2020-03-05 stsp }
7065 bfce7f83 2019-07-27 stsp
7066 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7067 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7068 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7069 bfce7f83 2019-07-27 stsp break;
7070 bfce7f83 2019-07-27 stsp }
7071 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7072 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7073 bfce7f83 2019-07-27 stsp if (err)
7074 bfce7f83 2019-07-27 stsp return err;
7075 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7076 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7077 bfce7f83 2019-07-27 stsp free(id_str);
7078 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7079 bfce7f83 2019-07-27 stsp }
7080 bfce7f83 2019-07-27 stsp }
7081 bfce7f83 2019-07-27 stsp
7082 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7083 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7084 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7085 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7086 bfce7f83 2019-07-27 stsp
7087 bfce7f83 2019-07-27 stsp return NULL;
7088 bfce7f83 2019-07-27 stsp }
7089 bfce7f83 2019-07-27 stsp
7090 bfce7f83 2019-07-27 stsp static const struct got_error *
7091 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7092 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7093 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7094 0ebf8283 2019-07-24 stsp {
7095 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7096 0ebf8283 2019-07-24 stsp char *editor;
7097 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7098 0ebf8283 2019-07-24 stsp
7099 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7100 0ebf8283 2019-07-24 stsp if (err)
7101 0ebf8283 2019-07-24 stsp return err;
7102 0ebf8283 2019-07-24 stsp
7103 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7104 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7105 0ebf8283 2019-07-24 stsp goto done;
7106 0ebf8283 2019-07-24 stsp }
7107 0ebf8283 2019-07-24 stsp
7108 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7109 0ebf8283 2019-07-24 stsp if (f == NULL) {
7110 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7111 0ebf8283 2019-07-24 stsp goto done;
7112 0ebf8283 2019-07-24 stsp }
7113 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7114 bfce7f83 2019-07-27 stsp if (err)
7115 bfce7f83 2019-07-27 stsp goto done;
7116 bfce7f83 2019-07-27 stsp
7117 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7118 0ebf8283 2019-07-24 stsp done:
7119 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7120 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7121 0ebf8283 2019-07-24 stsp free(editor);
7122 0ebf8283 2019-07-24 stsp return err;
7123 0ebf8283 2019-07-24 stsp }
7124 0ebf8283 2019-07-24 stsp
7125 0ebf8283 2019-07-24 stsp static const struct got_error *
7126 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7127 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7128 514f2ffe 2020-01-29 stsp struct got_repository *);
7129 0ebf8283 2019-07-24 stsp
7130 0ebf8283 2019-07-24 stsp static const struct got_error *
7131 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7132 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7133 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7134 0ebf8283 2019-07-24 stsp {
7135 0ebf8283 2019-07-24 stsp const struct got_error *err;
7136 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7137 0ebf8283 2019-07-24 stsp char *path = NULL;
7138 0ebf8283 2019-07-24 stsp
7139 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7140 0ebf8283 2019-07-24 stsp if (err)
7141 0ebf8283 2019-07-24 stsp return err;
7142 0ebf8283 2019-07-24 stsp
7143 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7144 0ebf8283 2019-07-24 stsp if (err)
7145 0ebf8283 2019-07-24 stsp goto done;
7146 0ebf8283 2019-07-24 stsp
7147 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7148 0ebf8283 2019-07-24 stsp if (err)
7149 0ebf8283 2019-07-24 stsp goto done;
7150 0ebf8283 2019-07-24 stsp
7151 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7152 083957f4 2020-02-24 stsp rewind(f);
7153 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7154 083957f4 2020-02-24 stsp } else {
7155 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7156 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7157 0ebf8283 2019-07-24 stsp goto done;
7158 083957f4 2020-02-24 stsp }
7159 083957f4 2020-02-24 stsp f = NULL;
7160 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7161 083957f4 2020-02-24 stsp if (err) {
7162 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7163 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7164 083957f4 2020-02-24 stsp goto done;
7165 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7166 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7167 083957f4 2020-02-24 stsp }
7168 0ebf8283 2019-07-24 stsp }
7169 0ebf8283 2019-07-24 stsp done:
7170 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7171 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7172 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7173 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7174 0ebf8283 2019-07-24 stsp free(path);
7175 0ebf8283 2019-07-24 stsp return err;
7176 0ebf8283 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp
7178 0ebf8283 2019-07-24 stsp static const struct got_error *
7179 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7180 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7181 0ebf8283 2019-07-24 stsp {
7182 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7183 0ebf8283 2019-07-24 stsp char *path = NULL;
7184 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7185 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7186 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7187 0ebf8283 2019-07-24 stsp
7188 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7189 0ebf8283 2019-07-24 stsp if (err)
7190 0ebf8283 2019-07-24 stsp return err;
7191 0ebf8283 2019-07-24 stsp
7192 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7193 0ebf8283 2019-07-24 stsp if (f == NULL) {
7194 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7195 0ebf8283 2019-07-24 stsp goto done;
7196 0ebf8283 2019-07-24 stsp }
7197 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7198 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7199 0ebf8283 2019-07-24 stsp repo);
7200 0ebf8283 2019-07-24 stsp if (err)
7201 0ebf8283 2019-07-24 stsp break;
7202 0ebf8283 2019-07-24 stsp
7203 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7204 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7205 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7206 0ebf8283 2019-07-24 stsp if (n < 0) {
7207 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7208 0ebf8283 2019-07-24 stsp break;
7209 0ebf8283 2019-07-24 stsp }
7210 0ebf8283 2019-07-24 stsp }
7211 0ebf8283 2019-07-24 stsp }
7212 0ebf8283 2019-07-24 stsp done:
7213 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7214 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7215 0ebf8283 2019-07-24 stsp free(path);
7216 0ebf8283 2019-07-24 stsp if (commit)
7217 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7218 0ebf8283 2019-07-24 stsp return err;
7219 0ebf8283 2019-07-24 stsp }
7220 0ebf8283 2019-07-24 stsp
7221 bfce7f83 2019-07-27 stsp void
7222 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7223 bfce7f83 2019-07-27 stsp {
7224 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7225 bfce7f83 2019-07-27 stsp
7226 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7227 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7228 bfce7f83 2019-07-27 stsp free(hle);
7229 bfce7f83 2019-07-27 stsp }
7230 bfce7f83 2019-07-27 stsp }
7231 bfce7f83 2019-07-27 stsp
7232 0ebf8283 2019-07-24 stsp static const struct got_error *
7233 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7234 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7235 0ebf8283 2019-07-24 stsp {
7236 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7237 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7238 0ebf8283 2019-07-24 stsp
7239 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7240 0ebf8283 2019-07-24 stsp if (f == NULL) {
7241 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7242 0ebf8283 2019-07-24 stsp goto done;
7243 0ebf8283 2019-07-24 stsp }
7244 0ebf8283 2019-07-24 stsp
7245 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7246 0ebf8283 2019-07-24 stsp done:
7247 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7248 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7249 0ebf8283 2019-07-24 stsp return err;
7250 0ebf8283 2019-07-24 stsp }
7251 0ebf8283 2019-07-24 stsp
7252 0ebf8283 2019-07-24 stsp static const struct got_error *
7253 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7254 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7255 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7256 0ebf8283 2019-07-24 stsp {
7257 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7258 0ebf8283 2019-07-24 stsp int resp = ' ';
7259 0ebf8283 2019-07-24 stsp
7260 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7261 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7262 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7263 0ebf8283 2019-07-24 stsp resp = getchar();
7264 a61a4414 2019-08-07 stsp if (resp == '\n')
7265 a61a4414 2019-08-07 stsp resp = getchar();
7266 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7267 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7268 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7269 bfce7f83 2019-07-27 stsp repo);
7270 426ebf2e 2019-07-25 stsp if (err) {
7271 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7272 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7273 426ebf2e 2019-07-25 stsp break;
7274 bfce7f83 2019-07-27 stsp prev_err = err;
7275 426ebf2e 2019-07-25 stsp resp = ' ';
7276 426ebf2e 2019-07-25 stsp continue;
7277 426ebf2e 2019-07-25 stsp }
7278 bfce7f83 2019-07-27 stsp break;
7279 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7280 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7281 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7282 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7283 426ebf2e 2019-07-25 stsp if (err) {
7284 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7285 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7286 426ebf2e 2019-07-25 stsp break;
7287 bfce7f83 2019-07-27 stsp prev_err = err;
7288 426ebf2e 2019-07-25 stsp resp = ' ';
7289 426ebf2e 2019-07-25 stsp continue;
7290 426ebf2e 2019-07-25 stsp }
7291 bfce7f83 2019-07-27 stsp break;
7292 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7293 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7294 0ebf8283 2019-07-24 stsp break;
7295 426ebf2e 2019-07-25 stsp } else
7296 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7297 0ebf8283 2019-07-24 stsp }
7298 0ebf8283 2019-07-24 stsp
7299 0ebf8283 2019-07-24 stsp return err;
7300 0ebf8283 2019-07-24 stsp }
7301 0ebf8283 2019-07-24 stsp
7302 0ebf8283 2019-07-24 stsp static const struct got_error *
7303 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7304 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7305 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7306 0ebf8283 2019-07-24 stsp {
7307 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7308 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7309 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7310 3e3a69f1 2019-07-25 stsp branch, repo);
7311 0ebf8283 2019-07-24 stsp }
7312 0ebf8283 2019-07-24 stsp
7313 0ebf8283 2019-07-24 stsp static const struct got_error *
7314 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7315 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7316 0ebf8283 2019-07-24 stsp {
7317 0ebf8283 2019-07-24 stsp const struct got_error *err;
7318 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7319 0ebf8283 2019-07-24 stsp
7320 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7321 0ebf8283 2019-07-24 stsp if (err)
7322 0ebf8283 2019-07-24 stsp goto done;
7323 0ebf8283 2019-07-24 stsp
7324 0ebf8283 2019-07-24 stsp if (new_id) {
7325 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7326 0ebf8283 2019-07-24 stsp if (err)
7327 0ebf8283 2019-07-24 stsp goto done;
7328 0ebf8283 2019-07-24 stsp }
7329 0ebf8283 2019-07-24 stsp
7330 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7331 0ebf8283 2019-07-24 stsp if (new_id_str)
7332 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7333 0ebf8283 2019-07-24 stsp
7334 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7335 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7336 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7337 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7338 0ebf8283 2019-07-24 stsp goto done;
7339 0ebf8283 2019-07-24 stsp }
7340 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7341 0ebf8283 2019-07-24 stsp } else {
7342 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7343 0ebf8283 2019-07-24 stsp if (err)
7344 0ebf8283 2019-07-24 stsp goto done;
7345 0ebf8283 2019-07-24 stsp }
7346 0ebf8283 2019-07-24 stsp
7347 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7348 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7349 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7350 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7351 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7352 0ebf8283 2019-07-24 stsp break;
7353 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7354 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7355 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7356 0ebf8283 2019-07-24 stsp logmsg);
7357 0ebf8283 2019-07-24 stsp break;
7358 0ebf8283 2019-07-24 stsp default:
7359 0ebf8283 2019-07-24 stsp break;
7360 0ebf8283 2019-07-24 stsp }
7361 0ebf8283 2019-07-24 stsp done:
7362 0ebf8283 2019-07-24 stsp free(old_id_str);
7363 0ebf8283 2019-07-24 stsp free(new_id_str);
7364 0ebf8283 2019-07-24 stsp return err;
7365 0ebf8283 2019-07-24 stsp }
7366 0ebf8283 2019-07-24 stsp
7367 0ebf8283 2019-07-24 stsp static const struct got_error *
7368 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7369 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7370 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7371 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7372 0ebf8283 2019-07-24 stsp {
7373 0ebf8283 2019-07-24 stsp const struct got_error *err;
7374 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7375 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7376 0ebf8283 2019-07-24 stsp
7377 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7378 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7379 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7380 0ebf8283 2019-07-24 stsp if (err)
7381 0ebf8283 2019-07-24 stsp return err;
7382 0ebf8283 2019-07-24 stsp }
7383 0ebf8283 2019-07-24 stsp
7384 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7385 0ebf8283 2019-07-24 stsp if (err)
7386 0ebf8283 2019-07-24 stsp return err;
7387 0ebf8283 2019-07-24 stsp
7388 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7389 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7390 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7391 0ebf8283 2019-07-24 stsp if (err) {
7392 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7393 0ebf8283 2019-07-24 stsp goto done;
7394 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7395 0ebf8283 2019-07-24 stsp } else {
7396 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7397 0ebf8283 2019-07-24 stsp free(new_commit_id);
7398 0ebf8283 2019-07-24 stsp }
7399 0ebf8283 2019-07-24 stsp done:
7400 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7401 0ebf8283 2019-07-24 stsp return err;
7402 0ebf8283 2019-07-24 stsp }
7403 0ebf8283 2019-07-24 stsp
7404 0ebf8283 2019-07-24 stsp static const struct got_error *
7405 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7406 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7407 0ebf8283 2019-07-24 stsp {
7408 0ebf8283 2019-07-24 stsp const struct got_error *error;
7409 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7410 0ebf8283 2019-07-24 stsp
7411 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7412 0ebf8283 2019-07-24 stsp repo);
7413 0ebf8283 2019-07-24 stsp if (error)
7414 0ebf8283 2019-07-24 stsp return error;
7415 0ebf8283 2019-07-24 stsp
7416 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7417 0ebf8283 2019-07-24 stsp if (error)
7418 0ebf8283 2019-07-24 stsp return error;
7419 0ebf8283 2019-07-24 stsp
7420 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7421 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7422 0ebf8283 2019-07-24 stsp return error;
7423 ab20a43a 2020-01-29 stsp }
7424 ab20a43a 2020-01-29 stsp
7425 ab20a43a 2020-01-29 stsp static const struct got_error *
7426 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7427 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7428 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7429 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7430 ab20a43a 2020-01-29 stsp {
7431 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7432 ab20a43a 2020-01-29 stsp
7433 ab20a43a 2020-01-29 stsp switch (status) {
7434 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7435 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7436 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7437 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7438 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7439 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7440 ab20a43a 2020-01-29 stsp default:
7441 ab20a43a 2020-01-29 stsp break;
7442 ab20a43a 2020-01-29 stsp }
7443 ab20a43a 2020-01-29 stsp
7444 ab20a43a 2020-01-29 stsp switch (staged_status) {
7445 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7446 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7447 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7448 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7449 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7450 ab20a43a 2020-01-29 stsp default:
7451 ab20a43a 2020-01-29 stsp break;
7452 ab20a43a 2020-01-29 stsp }
7453 ab20a43a 2020-01-29 stsp
7454 ab20a43a 2020-01-29 stsp return NULL;
7455 0ebf8283 2019-07-24 stsp }
7456 0ebf8283 2019-07-24 stsp
7457 0ebf8283 2019-07-24 stsp static const struct got_error *
7458 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7459 0ebf8283 2019-07-24 stsp {
7460 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7461 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7462 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7463 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7464 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7465 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7466 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7467 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7468 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7469 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7470 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7471 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7472 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7473 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7474 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7475 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7476 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7477 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7478 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7479 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7480 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7481 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7482 0ebf8283 2019-07-24 stsp
7483 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7484 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7485 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7486 0ebf8283 2019-07-24 stsp
7487 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7488 0ebf8283 2019-07-24 stsp switch (ch) {
7489 0ebf8283 2019-07-24 stsp case 'a':
7490 0ebf8283 2019-07-24 stsp abort_edit = 1;
7491 0ebf8283 2019-07-24 stsp break;
7492 0ebf8283 2019-07-24 stsp case 'c':
7493 0ebf8283 2019-07-24 stsp continue_edit = 1;
7494 0ebf8283 2019-07-24 stsp break;
7495 0ebf8283 2019-07-24 stsp case 'F':
7496 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7497 0ebf8283 2019-07-24 stsp break;
7498 083957f4 2020-02-24 stsp case 'm':
7499 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7500 083957f4 2020-02-24 stsp break;
7501 0ebf8283 2019-07-24 stsp default:
7502 0ebf8283 2019-07-24 stsp usage_histedit();
7503 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7504 0ebf8283 2019-07-24 stsp }
7505 0ebf8283 2019-07-24 stsp }
7506 0ebf8283 2019-07-24 stsp
7507 0ebf8283 2019-07-24 stsp argc -= optind;
7508 0ebf8283 2019-07-24 stsp argv += optind;
7509 0ebf8283 2019-07-24 stsp
7510 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7511 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7512 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7513 0ebf8283 2019-07-24 stsp err(1, "pledge");
7514 0ebf8283 2019-07-24 stsp #endif
7515 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7516 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7517 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7518 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7519 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7520 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7521 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7522 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7523 0ebf8283 2019-07-24 stsp if (argc != 0)
7524 0ebf8283 2019-07-24 stsp usage_histedit();
7525 0ebf8283 2019-07-24 stsp
7526 0ebf8283 2019-07-24 stsp /*
7527 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7528 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7529 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7530 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7531 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7532 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7533 0ebf8283 2019-07-24 stsp */
7534 0ebf8283 2019-07-24 stsp
7535 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7536 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7537 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7538 0ebf8283 2019-07-24 stsp goto done;
7539 0ebf8283 2019-07-24 stsp }
7540 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7541 0ebf8283 2019-07-24 stsp if (error)
7542 0ebf8283 2019-07-24 stsp goto done;
7543 0ebf8283 2019-07-24 stsp
7544 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7545 c9956ddf 2019-09-08 stsp NULL);
7546 0ebf8283 2019-07-24 stsp if (error != NULL)
7547 0ebf8283 2019-07-24 stsp goto done;
7548 0ebf8283 2019-07-24 stsp
7549 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7550 0ebf8283 2019-07-24 stsp if (error)
7551 0ebf8283 2019-07-24 stsp goto done;
7552 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7553 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7554 0ebf8283 2019-07-24 stsp goto done;
7555 0ebf8283 2019-07-24 stsp }
7556 0ebf8283 2019-07-24 stsp
7557 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7558 0ebf8283 2019-07-24 stsp if (error)
7559 0ebf8283 2019-07-24 stsp goto done;
7560 0ebf8283 2019-07-24 stsp
7561 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7562 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7563 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7564 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7565 083957f4 2020-02-24 stsp "before the -m option can be used");
7566 083957f4 2020-02-24 stsp goto done;
7567 083957f4 2020-02-24 stsp }
7568 083957f4 2020-02-24 stsp
7569 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7570 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7571 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7572 3e3a69f1 2019-07-25 stsp worktree, repo);
7573 0ebf8283 2019-07-24 stsp if (error)
7574 0ebf8283 2019-07-24 stsp goto done;
7575 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7576 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7577 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7578 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7579 0ebf8283 2019-07-24 stsp if (error)
7580 0ebf8283 2019-07-24 stsp goto done;
7581 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7582 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7583 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7584 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7585 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7586 0ebf8283 2019-07-24 stsp goto done;
7587 0ebf8283 2019-07-24 stsp }
7588 0ebf8283 2019-07-24 stsp
7589 0ebf8283 2019-07-24 stsp if (continue_edit) {
7590 0ebf8283 2019-07-24 stsp char *path;
7591 0ebf8283 2019-07-24 stsp
7592 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7593 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7594 0ebf8283 2019-07-24 stsp goto done;
7595 0ebf8283 2019-07-24 stsp }
7596 0ebf8283 2019-07-24 stsp
7597 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7598 0ebf8283 2019-07-24 stsp if (error)
7599 0ebf8283 2019-07-24 stsp goto done;
7600 0ebf8283 2019-07-24 stsp
7601 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7602 0ebf8283 2019-07-24 stsp free(path);
7603 0ebf8283 2019-07-24 stsp if (error)
7604 0ebf8283 2019-07-24 stsp goto done;
7605 0ebf8283 2019-07-24 stsp
7606 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7607 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7608 3e3a69f1 2019-07-25 stsp worktree, repo);
7609 0ebf8283 2019-07-24 stsp if (error)
7610 0ebf8283 2019-07-24 stsp goto done;
7611 0ebf8283 2019-07-24 stsp
7612 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7613 0ebf8283 2019-07-24 stsp if (error)
7614 0ebf8283 2019-07-24 stsp goto done;
7615 0ebf8283 2019-07-24 stsp
7616 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7617 0ebf8283 2019-07-24 stsp head_commit_id);
7618 0ebf8283 2019-07-24 stsp if (error)
7619 0ebf8283 2019-07-24 stsp goto done;
7620 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7621 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7622 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7623 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7624 3aac7cf7 2019-07-25 stsp goto done;
7625 3aac7cf7 2019-07-25 stsp }
7626 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7627 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7628 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7629 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7630 0ebf8283 2019-07-24 stsp commit = NULL;
7631 0ebf8283 2019-07-24 stsp if (error)
7632 0ebf8283 2019-07-24 stsp goto done;
7633 0ebf8283 2019-07-24 stsp } else {
7634 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7635 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7636 0ebf8283 2019-07-24 stsp goto done;
7637 0ebf8283 2019-07-24 stsp }
7638 0ebf8283 2019-07-24 stsp
7639 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7640 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7641 0ebf8283 2019-07-24 stsp if (error != NULL)
7642 0ebf8283 2019-07-24 stsp goto done;
7643 0ebf8283 2019-07-24 stsp
7644 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7645 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7646 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7647 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7648 c7d20a3f 2019-07-30 stsp goto done;
7649 c7d20a3f 2019-07-30 stsp }
7650 c7d20a3f 2019-07-30 stsp
7651 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7652 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7653 bfce7f83 2019-07-27 stsp branch = NULL;
7654 0ebf8283 2019-07-24 stsp if (error)
7655 0ebf8283 2019-07-24 stsp goto done;
7656 0ebf8283 2019-07-24 stsp
7657 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7658 0ebf8283 2019-07-24 stsp head_commit_id);
7659 0ebf8283 2019-07-24 stsp if (error)
7660 0ebf8283 2019-07-24 stsp goto done;
7661 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7662 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7663 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7664 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7665 3aac7cf7 2019-07-25 stsp goto done;
7666 3aac7cf7 2019-07-25 stsp }
7667 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7668 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7669 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7670 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7671 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7672 0ebf8283 2019-07-24 stsp commit = NULL;
7673 0ebf8283 2019-07-24 stsp if (error)
7674 0ebf8283 2019-07-24 stsp goto done;
7675 0ebf8283 2019-07-24 stsp
7676 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7677 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7678 c5996fff 2020-01-29 stsp goto done;
7679 c5996fff 2020-01-29 stsp }
7680 c5996fff 2020-01-29 stsp
7681 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7682 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7683 bfce7f83 2019-07-27 stsp if (error)
7684 bfce7f83 2019-07-27 stsp goto done;
7685 bfce7f83 2019-07-27 stsp
7686 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7687 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7688 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7689 6ba2bea2 2019-07-27 stsp if (error) {
7690 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7691 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7692 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7693 0ebf8283 2019-07-24 stsp goto done;
7694 6ba2bea2 2019-07-27 stsp }
7695 0ebf8283 2019-07-24 stsp } else {
7696 514f2ffe 2020-01-29 stsp const char *branch_name;
7697 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
7698 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
7699 514f2ffe 2020-01-29 stsp branch_name += 11;
7700 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
7701 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
7702 6ba2bea2 2019-07-27 stsp if (error) {
7703 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7704 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7705 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7706 0ebf8283 2019-07-24 stsp goto done;
7707 6ba2bea2 2019-07-27 stsp }
7708 0ebf8283 2019-07-24 stsp
7709 0ebf8283 2019-07-24 stsp }
7710 0ebf8283 2019-07-24 stsp
7711 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
7712 0ebf8283 2019-07-24 stsp repo);
7713 6ba2bea2 2019-07-27 stsp if (error) {
7714 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7715 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7716 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7717 0ebf8283 2019-07-24 stsp goto done;
7718 6ba2bea2 2019-07-27 stsp }
7719 0ebf8283 2019-07-24 stsp
7720 0ebf8283 2019-07-24 stsp }
7721 0ebf8283 2019-07-24 stsp
7722 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
7723 4ec14e60 2019-08-28 hiltjo if (error)
7724 0ebf8283 2019-07-24 stsp goto done;
7725 0ebf8283 2019-07-24 stsp
7726 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
7727 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
7728 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
7729 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
7730 0ebf8283 2019-07-24 stsp continue;
7731 0ebf8283 2019-07-24 stsp
7732 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
7733 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
7734 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
7735 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
7736 0ebf8283 2019-07-24 stsp repo);
7737 ab20a43a 2020-01-29 stsp if (error)
7738 ab20a43a 2020-01-29 stsp goto done;
7739 0ebf8283 2019-07-24 stsp } else {
7740 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
7741 ab20a43a 2020-01-29 stsp int have_changes = 0;
7742 ab20a43a 2020-01-29 stsp
7743 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
7744 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
7745 ab20a43a 2020-01-29 stsp if (error)
7746 ab20a43a 2020-01-29 stsp goto done;
7747 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
7748 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
7749 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
7750 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
7751 ab20a43a 2020-01-29 stsp if (error) {
7752 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
7753 ab20a43a 2020-01-29 stsp goto done;
7754 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
7755 ab20a43a 2020-01-29 stsp goto done;
7756 ab20a43a 2020-01-29 stsp }
7757 ab20a43a 2020-01-29 stsp if (have_changes) {
7758 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
7759 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
7760 ab20a43a 2020-01-29 stsp if (error)
7761 ab20a43a 2020-01-29 stsp goto done;
7762 ab20a43a 2020-01-29 stsp } else {
7763 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
7764 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
7765 ab20a43a 2020-01-29 stsp if (error)
7766 ab20a43a 2020-01-29 stsp goto done;
7767 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
7768 ab20a43a 2020-01-29 stsp hle, NULL);
7769 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
7770 ab20a43a 2020-01-29 stsp commit = NULL;
7771 ab20a43a 2020-01-29 stsp if (error)
7772 ab20a43a 2020-01-29 stsp goto done;
7773 ab20a43a 2020-01-29 stsp }
7774 0ebf8283 2019-07-24 stsp }
7775 0ebf8283 2019-07-24 stsp continue;
7776 0ebf8283 2019-07-24 stsp }
7777 0ebf8283 2019-07-24 stsp
7778 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7779 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7780 0ebf8283 2019-07-24 stsp if (error)
7781 0ebf8283 2019-07-24 stsp goto done;
7782 0ebf8283 2019-07-24 stsp continue;
7783 0ebf8283 2019-07-24 stsp }
7784 0ebf8283 2019-07-24 stsp
7785 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7786 0ebf8283 2019-07-24 stsp hle->commit_id);
7787 0ebf8283 2019-07-24 stsp if (error)
7788 0ebf8283 2019-07-24 stsp goto done;
7789 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7790 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7791 0ebf8283 2019-07-24 stsp
7792 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7793 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7794 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7795 0ebf8283 2019-07-24 stsp if (error)
7796 0ebf8283 2019-07-24 stsp goto done;
7797 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7798 0ebf8283 2019-07-24 stsp commit = NULL;
7799 0ebf8283 2019-07-24 stsp
7800 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7801 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7802 a0ea4fc0 2020-02-28 stsp repo);
7803 a0ea4fc0 2020-02-28 stsp if (error)
7804 a0ea4fc0 2020-02-28 stsp goto done;
7805 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7806 0ebf8283 2019-07-24 stsp break;
7807 0ebf8283 2019-07-24 stsp }
7808 0ebf8283 2019-07-24 stsp
7809 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7810 0ebf8283 2019-07-24 stsp char *id_str;
7811 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7812 0ebf8283 2019-07-24 stsp if (error)
7813 0ebf8283 2019-07-24 stsp goto done;
7814 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7815 0ebf8283 2019-07-24 stsp id_str);
7816 0ebf8283 2019-07-24 stsp free(id_str);
7817 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7818 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7819 3e3a69f1 2019-07-25 stsp fileindex);
7820 0ebf8283 2019-07-24 stsp goto done;
7821 0ebf8283 2019-07-24 stsp }
7822 0ebf8283 2019-07-24 stsp
7823 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7824 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7825 0ebf8283 2019-07-24 stsp if (error)
7826 0ebf8283 2019-07-24 stsp goto done;
7827 0ebf8283 2019-07-24 stsp continue;
7828 0ebf8283 2019-07-24 stsp }
7829 0ebf8283 2019-07-24 stsp
7830 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7831 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7832 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7833 0ebf8283 2019-07-24 stsp if (error)
7834 0ebf8283 2019-07-24 stsp goto done;
7835 0ebf8283 2019-07-24 stsp }
7836 0ebf8283 2019-07-24 stsp
7837 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7838 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7839 0ebf8283 2019-07-24 stsp if (error)
7840 0ebf8283 2019-07-24 stsp goto done;
7841 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7842 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7843 0ebf8283 2019-07-24 stsp } else
7844 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7845 3e3a69f1 2019-07-25 stsp branch, repo);
7846 0ebf8283 2019-07-24 stsp done:
7847 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7848 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7849 0ebf8283 2019-07-24 stsp free(head_commit_id);
7850 0ebf8283 2019-07-24 stsp free(base_commit_id);
7851 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7852 0ebf8283 2019-07-24 stsp if (commit)
7853 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7854 0ebf8283 2019-07-24 stsp if (branch)
7855 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7856 0ebf8283 2019-07-24 stsp if (tmp_branch)
7857 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7858 0ebf8283 2019-07-24 stsp if (worktree)
7859 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7860 2822a352 2019-10-15 stsp if (repo)
7861 2822a352 2019-10-15 stsp got_repo_close(repo);
7862 2822a352 2019-10-15 stsp return error;
7863 2822a352 2019-10-15 stsp }
7864 2822a352 2019-10-15 stsp
7865 2822a352 2019-10-15 stsp __dead static void
7866 2822a352 2019-10-15 stsp usage_integrate(void)
7867 2822a352 2019-10-15 stsp {
7868 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7869 2822a352 2019-10-15 stsp exit(1);
7870 2822a352 2019-10-15 stsp }
7871 2822a352 2019-10-15 stsp
7872 2822a352 2019-10-15 stsp static const struct got_error *
7873 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7874 2822a352 2019-10-15 stsp {
7875 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7876 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7877 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7878 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7879 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7880 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7881 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7882 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7883 2822a352 2019-10-15 stsp int ch, did_something = 0;
7884 2822a352 2019-10-15 stsp
7885 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7886 2822a352 2019-10-15 stsp switch (ch) {
7887 2822a352 2019-10-15 stsp default:
7888 2822a352 2019-10-15 stsp usage_integrate();
7889 2822a352 2019-10-15 stsp /* NOTREACHED */
7890 2822a352 2019-10-15 stsp }
7891 2822a352 2019-10-15 stsp }
7892 2822a352 2019-10-15 stsp
7893 2822a352 2019-10-15 stsp argc -= optind;
7894 2822a352 2019-10-15 stsp argv += optind;
7895 2822a352 2019-10-15 stsp
7896 2822a352 2019-10-15 stsp if (argc != 1)
7897 2822a352 2019-10-15 stsp usage_integrate();
7898 2822a352 2019-10-15 stsp branch_arg = argv[0];
7899 2822a352 2019-10-15 stsp
7900 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7901 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7902 2822a352 2019-10-15 stsp err(1, "pledge");
7903 2822a352 2019-10-15 stsp
7904 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7905 2822a352 2019-10-15 stsp if (cwd == NULL) {
7906 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7907 2822a352 2019-10-15 stsp goto done;
7908 2822a352 2019-10-15 stsp }
7909 2822a352 2019-10-15 stsp
7910 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7911 2822a352 2019-10-15 stsp if (error)
7912 2822a352 2019-10-15 stsp goto done;
7913 2822a352 2019-10-15 stsp
7914 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7915 2822a352 2019-10-15 stsp if (error)
7916 2822a352 2019-10-15 stsp goto done;
7917 2822a352 2019-10-15 stsp
7918 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7919 2822a352 2019-10-15 stsp NULL);
7920 2822a352 2019-10-15 stsp if (error != NULL)
7921 2822a352 2019-10-15 stsp goto done;
7922 2822a352 2019-10-15 stsp
7923 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7924 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7925 2822a352 2019-10-15 stsp if (error)
7926 2822a352 2019-10-15 stsp goto done;
7927 2822a352 2019-10-15 stsp
7928 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7929 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7930 2822a352 2019-10-15 stsp goto done;
7931 2822a352 2019-10-15 stsp }
7932 2822a352 2019-10-15 stsp
7933 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7934 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7935 2822a352 2019-10-15 stsp if (error)
7936 2822a352 2019-10-15 stsp goto done;
7937 2822a352 2019-10-15 stsp
7938 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7939 2822a352 2019-10-15 stsp if (refname == NULL) {
7940 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7941 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7942 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7943 2822a352 2019-10-15 stsp goto done;
7944 2822a352 2019-10-15 stsp }
7945 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7946 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7947 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7948 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7949 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7950 2822a352 2019-10-15 stsp goto done;
7951 2822a352 2019-10-15 stsp }
7952 2822a352 2019-10-15 stsp
7953 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7954 2822a352 2019-10-15 stsp if (error)
7955 2822a352 2019-10-15 stsp goto done;
7956 2822a352 2019-10-15 stsp
7957 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7958 2822a352 2019-10-15 stsp if (error)
7959 2822a352 2019-10-15 stsp goto done;
7960 2822a352 2019-10-15 stsp
7961 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7962 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7963 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7964 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7965 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7966 2822a352 2019-10-15 stsp goto done;
7967 2822a352 2019-10-15 stsp }
7968 2822a352 2019-10-15 stsp
7969 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7970 2822a352 2019-10-15 stsp if (error) {
7971 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7972 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7973 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7974 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7975 2822a352 2019-10-15 stsp goto done;
7976 2822a352 2019-10-15 stsp }
7977 2822a352 2019-10-15 stsp
7978 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7979 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7980 2822a352 2019-10-15 stsp check_cancelled, NULL);
7981 2822a352 2019-10-15 stsp if (error)
7982 2822a352 2019-10-15 stsp goto done;
7983 2822a352 2019-10-15 stsp
7984 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7985 2822a352 2019-10-15 stsp done:
7986 715dc77e 2019-08-03 stsp if (repo)
7987 715dc77e 2019-08-03 stsp got_repo_close(repo);
7988 2822a352 2019-10-15 stsp if (worktree)
7989 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7990 2822a352 2019-10-15 stsp free(cwd);
7991 2822a352 2019-10-15 stsp free(base_commit_id);
7992 2822a352 2019-10-15 stsp free(commit_id);
7993 2822a352 2019-10-15 stsp free(refname);
7994 2822a352 2019-10-15 stsp free(base_refname);
7995 715dc77e 2019-08-03 stsp return error;
7996 715dc77e 2019-08-03 stsp }
7997 715dc77e 2019-08-03 stsp
7998 715dc77e 2019-08-03 stsp __dead static void
7999 715dc77e 2019-08-03 stsp usage_stage(void)
8000 715dc77e 2019-08-03 stsp {
8001 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8002 f3055044 2019-08-07 stsp "[file-path ...]\n",
8003 715dc77e 2019-08-03 stsp getprogname());
8004 715dc77e 2019-08-03 stsp exit(1);
8005 715dc77e 2019-08-03 stsp }
8006 715dc77e 2019-08-03 stsp
8007 715dc77e 2019-08-03 stsp static const struct got_error *
8008 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8009 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8010 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8011 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8012 408b4ebc 2019-08-03 stsp {
8013 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8014 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8015 408b4ebc 2019-08-03 stsp
8016 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8017 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8018 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8019 408b4ebc 2019-08-03 stsp return NULL;
8020 408b4ebc 2019-08-03 stsp
8021 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8022 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8023 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8024 408b4ebc 2019-08-03 stsp else
8025 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8026 408b4ebc 2019-08-03 stsp if (err)
8027 408b4ebc 2019-08-03 stsp return err;
8028 408b4ebc 2019-08-03 stsp
8029 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8030 408b4ebc 2019-08-03 stsp free(id_str);
8031 dc424a06 2019-08-07 stsp return NULL;
8032 dc424a06 2019-08-07 stsp }
8033 2e1f37b0 2019-08-08 stsp
8034 dc424a06 2019-08-07 stsp static const struct got_error *
8035 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8036 715dc77e 2019-08-03 stsp {
8037 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8038 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8039 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8040 715dc77e 2019-08-03 stsp char *cwd = NULL;
8041 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8042 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8043 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8044 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8045 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8046 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8047 715dc77e 2019-08-03 stsp
8048 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8049 715dc77e 2019-08-03 stsp
8050 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8051 715dc77e 2019-08-03 stsp switch (ch) {
8052 408b4ebc 2019-08-03 stsp case 'l':
8053 408b4ebc 2019-08-03 stsp list_stage = 1;
8054 408b4ebc 2019-08-03 stsp break;
8055 dc424a06 2019-08-07 stsp case 'p':
8056 dc424a06 2019-08-07 stsp pflag = 1;
8057 dc424a06 2019-08-07 stsp break;
8058 dc424a06 2019-08-07 stsp case 'F':
8059 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8060 dc424a06 2019-08-07 stsp break;
8061 715dc77e 2019-08-03 stsp default:
8062 715dc77e 2019-08-03 stsp usage_stage();
8063 715dc77e 2019-08-03 stsp /* NOTREACHED */
8064 715dc77e 2019-08-03 stsp }
8065 715dc77e 2019-08-03 stsp }
8066 715dc77e 2019-08-03 stsp
8067 715dc77e 2019-08-03 stsp argc -= optind;
8068 715dc77e 2019-08-03 stsp argv += optind;
8069 715dc77e 2019-08-03 stsp
8070 715dc77e 2019-08-03 stsp #ifndef PROFILE
8071 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8072 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8073 715dc77e 2019-08-03 stsp err(1, "pledge");
8074 715dc77e 2019-08-03 stsp #endif
8075 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8076 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8077 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8078 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8079 715dc77e 2019-08-03 stsp
8080 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8081 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8082 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8083 715dc77e 2019-08-03 stsp goto done;
8084 715dc77e 2019-08-03 stsp }
8085 715dc77e 2019-08-03 stsp
8086 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8087 715dc77e 2019-08-03 stsp if (error)
8088 715dc77e 2019-08-03 stsp goto done;
8089 715dc77e 2019-08-03 stsp
8090 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8091 c9956ddf 2019-09-08 stsp NULL);
8092 715dc77e 2019-08-03 stsp if (error != NULL)
8093 715dc77e 2019-08-03 stsp goto done;
8094 715dc77e 2019-08-03 stsp
8095 dc424a06 2019-08-07 stsp if (patch_script_path) {
8096 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8097 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8098 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8099 dc424a06 2019-08-07 stsp patch_script_path);
8100 dc424a06 2019-08-07 stsp goto done;
8101 dc424a06 2019-08-07 stsp }
8102 dc424a06 2019-08-07 stsp }
8103 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8104 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8105 715dc77e 2019-08-03 stsp if (error)
8106 715dc77e 2019-08-03 stsp goto done;
8107 715dc77e 2019-08-03 stsp
8108 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8109 6d022e97 2019-08-04 stsp if (error)
8110 6d022e97 2019-08-04 stsp goto done;
8111 715dc77e 2019-08-03 stsp
8112 6d022e97 2019-08-04 stsp if (list_stage)
8113 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8114 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8115 2e1f37b0 2019-08-08 stsp else {
8116 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8117 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8118 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8119 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8120 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8121 2e1f37b0 2019-08-08 stsp }
8122 ad493afc 2019-08-03 stsp done:
8123 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8124 2e1f37b0 2019-08-08 stsp error == NULL)
8125 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8126 ad493afc 2019-08-03 stsp if (repo)
8127 ad493afc 2019-08-03 stsp got_repo_close(repo);
8128 ad493afc 2019-08-03 stsp if (worktree)
8129 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8130 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8131 ad493afc 2019-08-03 stsp free((char *)pe->path);
8132 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8133 ad493afc 2019-08-03 stsp free(cwd);
8134 ad493afc 2019-08-03 stsp return error;
8135 ad493afc 2019-08-03 stsp }
8136 ad493afc 2019-08-03 stsp
8137 ad493afc 2019-08-03 stsp __dead static void
8138 ad493afc 2019-08-03 stsp usage_unstage(void)
8139 ad493afc 2019-08-03 stsp {
8140 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8141 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8142 ad493afc 2019-08-03 stsp getprogname());
8143 ad493afc 2019-08-03 stsp exit(1);
8144 ad493afc 2019-08-03 stsp }
8145 ad493afc 2019-08-03 stsp
8146 ad493afc 2019-08-03 stsp
8147 ad493afc 2019-08-03 stsp static const struct got_error *
8148 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8149 ad493afc 2019-08-03 stsp {
8150 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8151 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8152 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8153 ad493afc 2019-08-03 stsp char *cwd = NULL;
8154 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8155 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8156 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8157 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8158 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8159 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8160 ad493afc 2019-08-03 stsp
8161 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8162 ad493afc 2019-08-03 stsp
8163 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8164 ad493afc 2019-08-03 stsp switch (ch) {
8165 2e1f37b0 2019-08-08 stsp case 'p':
8166 2e1f37b0 2019-08-08 stsp pflag = 1;
8167 2e1f37b0 2019-08-08 stsp break;
8168 2e1f37b0 2019-08-08 stsp case 'F':
8169 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8170 2e1f37b0 2019-08-08 stsp break;
8171 ad493afc 2019-08-03 stsp default:
8172 ad493afc 2019-08-03 stsp usage_unstage();
8173 ad493afc 2019-08-03 stsp /* NOTREACHED */
8174 ad493afc 2019-08-03 stsp }
8175 ad493afc 2019-08-03 stsp }
8176 ad493afc 2019-08-03 stsp
8177 ad493afc 2019-08-03 stsp argc -= optind;
8178 ad493afc 2019-08-03 stsp argv += optind;
8179 ad493afc 2019-08-03 stsp
8180 ad493afc 2019-08-03 stsp #ifndef PROFILE
8181 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8182 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8183 ad493afc 2019-08-03 stsp err(1, "pledge");
8184 ad493afc 2019-08-03 stsp #endif
8185 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8186 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8187 2e1f37b0 2019-08-08 stsp
8188 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8189 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8190 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8191 ad493afc 2019-08-03 stsp goto done;
8192 ad493afc 2019-08-03 stsp }
8193 ad493afc 2019-08-03 stsp
8194 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8195 ad493afc 2019-08-03 stsp if (error)
8196 ad493afc 2019-08-03 stsp goto done;
8197 ad493afc 2019-08-03 stsp
8198 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8199 c9956ddf 2019-09-08 stsp NULL);
8200 ad493afc 2019-08-03 stsp if (error != NULL)
8201 ad493afc 2019-08-03 stsp goto done;
8202 ad493afc 2019-08-03 stsp
8203 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8204 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8205 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8206 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8207 2e1f37b0 2019-08-08 stsp patch_script_path);
8208 2e1f37b0 2019-08-08 stsp goto done;
8209 2e1f37b0 2019-08-08 stsp }
8210 2e1f37b0 2019-08-08 stsp }
8211 2e1f37b0 2019-08-08 stsp
8212 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8213 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8214 ad493afc 2019-08-03 stsp if (error)
8215 ad493afc 2019-08-03 stsp goto done;
8216 ad493afc 2019-08-03 stsp
8217 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8218 ad493afc 2019-08-03 stsp if (error)
8219 ad493afc 2019-08-03 stsp goto done;
8220 ad493afc 2019-08-03 stsp
8221 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8222 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8223 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8224 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8225 715dc77e 2019-08-03 stsp done:
8226 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8227 2e1f37b0 2019-08-08 stsp error == NULL)
8228 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8229 0ebf8283 2019-07-24 stsp if (repo)
8230 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8231 715dc77e 2019-08-03 stsp if (worktree)
8232 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8233 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8234 715dc77e 2019-08-03 stsp free((char *)pe->path);
8235 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8236 715dc77e 2019-08-03 stsp free(cwd);
8237 01073a5d 2019-08-22 stsp return error;
8238 01073a5d 2019-08-22 stsp }
8239 01073a5d 2019-08-22 stsp
8240 01073a5d 2019-08-22 stsp __dead static void
8241 01073a5d 2019-08-22 stsp usage_cat(void)
8242 01073a5d 2019-08-22 stsp {
8243 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8244 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8245 01073a5d 2019-08-22 stsp exit(1);
8246 01073a5d 2019-08-22 stsp }
8247 01073a5d 2019-08-22 stsp
8248 01073a5d 2019-08-22 stsp static const struct got_error *
8249 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8250 01073a5d 2019-08-22 stsp {
8251 01073a5d 2019-08-22 stsp const struct got_error *err;
8252 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8253 01073a5d 2019-08-22 stsp
8254 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8255 01073a5d 2019-08-22 stsp if (err)
8256 01073a5d 2019-08-22 stsp return err;
8257 01073a5d 2019-08-22 stsp
8258 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8259 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8260 01073a5d 2019-08-22 stsp return err;
8261 01073a5d 2019-08-22 stsp }
8262 01073a5d 2019-08-22 stsp
8263 01073a5d 2019-08-22 stsp static const struct got_error *
8264 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8265 01073a5d 2019-08-22 stsp {
8266 01073a5d 2019-08-22 stsp const struct got_error *err;
8267 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8268 56e0773d 2019-11-28 stsp int nentries, i;
8269 01073a5d 2019-08-22 stsp
8270 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8271 01073a5d 2019-08-22 stsp if (err)
8272 01073a5d 2019-08-22 stsp return err;
8273 01073a5d 2019-08-22 stsp
8274 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8275 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8276 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8277 01073a5d 2019-08-22 stsp char *id_str;
8278 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8279 01073a5d 2019-08-22 stsp break;
8280 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8281 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8282 01073a5d 2019-08-22 stsp if (err)
8283 01073a5d 2019-08-22 stsp break;
8284 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8285 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8286 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8287 01073a5d 2019-08-22 stsp free(id_str);
8288 01073a5d 2019-08-22 stsp }
8289 01073a5d 2019-08-22 stsp
8290 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8291 01073a5d 2019-08-22 stsp return err;
8292 01073a5d 2019-08-22 stsp }
8293 01073a5d 2019-08-22 stsp
8294 01073a5d 2019-08-22 stsp static const struct got_error *
8295 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8296 01073a5d 2019-08-22 stsp {
8297 01073a5d 2019-08-22 stsp const struct got_error *err;
8298 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8299 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8300 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8301 01073a5d 2019-08-22 stsp char *id_str = NULL;
8302 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8303 01073a5d 2019-08-22 stsp
8304 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8305 01073a5d 2019-08-22 stsp if (err)
8306 01073a5d 2019-08-22 stsp return err;
8307 01073a5d 2019-08-22 stsp
8308 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8309 01073a5d 2019-08-22 stsp if (err)
8310 01073a5d 2019-08-22 stsp goto done;
8311 01073a5d 2019-08-22 stsp
8312 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8313 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8314 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8315 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8316 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8317 01073a5d 2019-08-22 stsp char *pid_str;
8318 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8319 01073a5d 2019-08-22 stsp if (err)
8320 01073a5d 2019-08-22 stsp goto done;
8321 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8322 01073a5d 2019-08-22 stsp free(pid_str);
8323 01073a5d 2019-08-22 stsp }
8324 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8325 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8326 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8327 01073a5d 2019-08-22 stsp
8328 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8329 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8330 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8331 01073a5d 2019-08-22 stsp
8332 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8333 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8334 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8335 01073a5d 2019-08-22 stsp done:
8336 01073a5d 2019-08-22 stsp free(id_str);
8337 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8338 01073a5d 2019-08-22 stsp return err;
8339 01073a5d 2019-08-22 stsp }
8340 01073a5d 2019-08-22 stsp
8341 01073a5d 2019-08-22 stsp static const struct got_error *
8342 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8343 01073a5d 2019-08-22 stsp {
8344 01073a5d 2019-08-22 stsp const struct got_error *err;
8345 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8346 01073a5d 2019-08-22 stsp char *id_str = NULL;
8347 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8348 01073a5d 2019-08-22 stsp
8349 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8350 01073a5d 2019-08-22 stsp if (err)
8351 01073a5d 2019-08-22 stsp return err;
8352 01073a5d 2019-08-22 stsp
8353 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8354 01073a5d 2019-08-22 stsp if (err)
8355 01073a5d 2019-08-22 stsp goto done;
8356 01073a5d 2019-08-22 stsp
8357 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8358 e15d5241 2019-08-22 stsp
8359 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8360 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8361 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8362 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8363 01073a5d 2019-08-22 stsp break;
8364 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8365 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8366 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8367 01073a5d 2019-08-22 stsp break;
8368 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8369 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8370 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8371 01073a5d 2019-08-22 stsp break;
8372 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8373 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8374 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8375 01073a5d 2019-08-22 stsp break;
8376 01073a5d 2019-08-22 stsp default:
8377 01073a5d 2019-08-22 stsp break;
8378 01073a5d 2019-08-22 stsp }
8379 01073a5d 2019-08-22 stsp
8380 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8381 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8382 e15d5241 2019-08-22 stsp
8383 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8384 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8385 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8386 01073a5d 2019-08-22 stsp
8387 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8388 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8389 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8390 01073a5d 2019-08-22 stsp done:
8391 01073a5d 2019-08-22 stsp free(id_str);
8392 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8393 01073a5d 2019-08-22 stsp return err;
8394 01073a5d 2019-08-22 stsp }
8395 01073a5d 2019-08-22 stsp
8396 01073a5d 2019-08-22 stsp static const struct got_error *
8397 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8398 01073a5d 2019-08-22 stsp {
8399 01073a5d 2019-08-22 stsp const struct got_error *error;
8400 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8401 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8402 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8403 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8404 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8405 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8406 01073a5d 2019-08-22 stsp
8407 01073a5d 2019-08-22 stsp #ifndef PROFILE
8408 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8409 01073a5d 2019-08-22 stsp NULL) == -1)
8410 01073a5d 2019-08-22 stsp err(1, "pledge");
8411 01073a5d 2019-08-22 stsp #endif
8412 01073a5d 2019-08-22 stsp
8413 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8414 01073a5d 2019-08-22 stsp switch (ch) {
8415 896e9b6f 2019-08-26 stsp case 'c':
8416 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8417 896e9b6f 2019-08-26 stsp break;
8418 01073a5d 2019-08-22 stsp case 'r':
8419 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8420 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8421 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8422 9ba1d308 2019-10-21 stsp optarg);
8423 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8424 01073a5d 2019-08-22 stsp break;
8425 896e9b6f 2019-08-26 stsp case 'P':
8426 896e9b6f 2019-08-26 stsp force_path = 1;
8427 896e9b6f 2019-08-26 stsp break;
8428 01073a5d 2019-08-22 stsp default:
8429 01073a5d 2019-08-22 stsp usage_cat();
8430 01073a5d 2019-08-22 stsp /* NOTREACHED */
8431 01073a5d 2019-08-22 stsp }
8432 01073a5d 2019-08-22 stsp }
8433 01073a5d 2019-08-22 stsp
8434 01073a5d 2019-08-22 stsp argc -= optind;
8435 01073a5d 2019-08-22 stsp argv += optind;
8436 01073a5d 2019-08-22 stsp
8437 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8438 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8439 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8440 01073a5d 2019-08-22 stsp goto done;
8441 01073a5d 2019-08-22 stsp }
8442 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8443 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8444 01073a5d 2019-08-22 stsp goto done;
8445 01073a5d 2019-08-22 stsp if (worktree) {
8446 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8447 01073a5d 2019-08-22 stsp repo_path = strdup(
8448 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8449 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8450 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8451 01073a5d 2019-08-22 stsp goto done;
8452 01073a5d 2019-08-22 stsp }
8453 01073a5d 2019-08-22 stsp }
8454 01073a5d 2019-08-22 stsp }
8455 01073a5d 2019-08-22 stsp
8456 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8457 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8458 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8459 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8460 01073a5d 2019-08-22 stsp }
8461 01073a5d 2019-08-22 stsp
8462 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8463 01073a5d 2019-08-22 stsp free(repo_path);
8464 01073a5d 2019-08-22 stsp if (error != NULL)
8465 01073a5d 2019-08-22 stsp goto done;
8466 01073a5d 2019-08-22 stsp
8467 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8468 896e9b6f 2019-08-26 stsp if (error)
8469 896e9b6f 2019-08-26 stsp goto done;
8470 896e9b6f 2019-08-26 stsp
8471 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8472 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8473 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8474 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8475 01073a5d 2019-08-22 stsp if (error)
8476 01073a5d 2019-08-22 stsp goto done;
8477 01073a5d 2019-08-22 stsp
8478 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8479 896e9b6f 2019-08-26 stsp if (force_path) {
8480 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8481 896e9b6f 2019-08-26 stsp argv[i]);
8482 896e9b6f 2019-08-26 stsp if (error)
8483 896e9b6f 2019-08-26 stsp break;
8484 896e9b6f 2019-08-26 stsp } else {
8485 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8486 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8487 896e9b6f 2019-08-26 stsp if (error) {
8488 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8489 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8490 896e9b6f 2019-08-26 stsp break;
8491 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8492 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8493 896e9b6f 2019-08-26 stsp if (error)
8494 896e9b6f 2019-08-26 stsp break;
8495 896e9b6f 2019-08-26 stsp }
8496 896e9b6f 2019-08-26 stsp }
8497 01073a5d 2019-08-22 stsp
8498 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8499 01073a5d 2019-08-22 stsp if (error)
8500 01073a5d 2019-08-22 stsp break;
8501 01073a5d 2019-08-22 stsp
8502 01073a5d 2019-08-22 stsp switch (obj_type) {
8503 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8504 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8505 01073a5d 2019-08-22 stsp break;
8506 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8507 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8508 01073a5d 2019-08-22 stsp break;
8509 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8510 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8511 01073a5d 2019-08-22 stsp break;
8512 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8513 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8514 01073a5d 2019-08-22 stsp break;
8515 01073a5d 2019-08-22 stsp default:
8516 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8517 01073a5d 2019-08-22 stsp break;
8518 01073a5d 2019-08-22 stsp }
8519 01073a5d 2019-08-22 stsp if (error)
8520 01073a5d 2019-08-22 stsp break;
8521 01073a5d 2019-08-22 stsp free(label);
8522 01073a5d 2019-08-22 stsp label = NULL;
8523 01073a5d 2019-08-22 stsp free(id);
8524 01073a5d 2019-08-22 stsp id = NULL;
8525 01073a5d 2019-08-22 stsp }
8526 01073a5d 2019-08-22 stsp done:
8527 01073a5d 2019-08-22 stsp free(label);
8528 01073a5d 2019-08-22 stsp free(id);
8529 896e9b6f 2019-08-26 stsp free(commit_id);
8530 01073a5d 2019-08-22 stsp if (worktree)
8531 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8532 01073a5d 2019-08-22 stsp if (repo) {
8533 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8534 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8535 01073a5d 2019-08-22 stsp if (error == NULL)
8536 01073a5d 2019-08-22 stsp error = repo_error;
8537 01073a5d 2019-08-22 stsp }
8538 0ebf8283 2019-07-24 stsp return error;
8539 0ebf8283 2019-07-24 stsp }