Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
4 93658fb9 2020-03-18 stsp * Copyright (c) 2020 Ori Bernstein <ori@openbsd.org>
5 5c860e29 2018-03-12 stsp *
6 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
7 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
8 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
9 5c860e29 2018-03-12 stsp *
10 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 5c860e29 2018-03-12 stsp */
18 5c860e29 2018-03-12 stsp
19 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
20 c0768b0f 2018-06-10 stsp #include <sys/types.h>
21 5de5890b 2018-10-18 stsp #include <sys/stat.h>
22 3c45a30a 2019-05-12 jcs #include <sys/param.h>
23 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
24 f42b1b34 2018-03-12 stsp
25 5c860e29 2018-03-12 stsp #include <err.h>
26 5c860e29 2018-03-12 stsp #include <errno.h>
27 12463d8b 2019-12-13 stsp #include <fcntl.h>
28 12ce7a6c 2019-08-12 stsp #include <limits.h>
29 5c860e29 2018-03-12 stsp #include <locale.h>
30 818c7501 2019-07-11 stsp #include <ctype.h>
31 99437157 2018-11-11 stsp #include <signal.h>
32 5c860e29 2018-03-12 stsp #include <stdio.h>
33 5c860e29 2018-03-12 stsp #include <stdlib.h>
34 5c860e29 2018-03-12 stsp #include <string.h>
35 5c860e29 2018-03-12 stsp #include <unistd.h>
36 c09a553d 2018-03-12 stsp #include <libgen.h>
37 c0768b0f 2018-06-10 stsp #include <time.h>
38 33ad4cbe 2019-05-12 jcs #include <paths.h>
39 6841bf13 2019-11-29 kn #include <regex.h>
40 83cd27f8 2020-01-13 stsp #include <getopt.h>
41 d2cdc636 2020-03-18 stsp #include <util.h>
42 5c860e29 2018-03-12 stsp
43 53ccebc2 2019-07-30 stsp #include "got_version.h"
44 f42b1b34 2018-03-12 stsp #include "got_error.h"
45 f42b1b34 2018-03-12 stsp #include "got_object.h"
46 5261c201 2018-04-01 stsp #include "got_reference.h"
47 f42b1b34 2018-03-12 stsp #include "got_repository.h"
48 1dd54920 2019-05-11 stsp #include "got_path.h"
49 e6209546 2019-08-22 stsp #include "got_cancel.h"
50 c09a553d 2018-03-12 stsp #include "got_worktree.h"
51 79109fed 2018-03-27 stsp #include "got_diff.h"
52 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
53 6f23baec 2020-03-18 stsp #include "got_fetch.h"
54 404c43c4 2018-06-21 stsp #include "got_blame.h"
55 63219cd2 2019-01-04 stsp #include "got_privsep.h"
56 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
57 5c860e29 2018-03-12 stsp
58 5c860e29 2018-03-12 stsp #ifndef nitems
59 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 5c860e29 2018-03-12 stsp #endif
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
63 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigint(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigint_received = 1;
69 99437157 2018-11-11 stsp }
70 99437157 2018-11-11 stsp
71 99437157 2018-11-11 stsp static void
72 99437157 2018-11-11 stsp catch_sigpipe(int signo)
73 99437157 2018-11-11 stsp {
74 99437157 2018-11-11 stsp sigpipe_received = 1;
75 99437157 2018-11-11 stsp }
76 5c860e29 2018-03-12 stsp
77 99437157 2018-11-11 stsp
78 8cfb4057 2019-07-09 stsp struct got_cmd {
79 5c860e29 2018-03-12 stsp const char *cmd_name;
80 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
81 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
82 97b3a7be 2019-07-09 stsp const char *cmd_alias;
83 5c860e29 2018-03-12 stsp };
84 5c860e29 2018-03-12 stsp
85 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
86 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
87 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 7848a0e1 2020-03-19 stsp __dead static void usage_fetch(void);
90 2ab43947 2020-03-18 stsp __dead static void usage_checkout(void);
91 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
92 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
93 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
94 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
95 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
96 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
97 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
98 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
99 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
100 d00136be 2019-03-26 stsp __dead static void usage_add(void);
101 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
102 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
103 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
104 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
105 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
106 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
107 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
108 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
109 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
110 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
111 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
112 5c860e29 2018-03-12 stsp
113 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
114 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
115 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
116 7848a0e1 2020-03-19 stsp static const struct got_error* cmd_fetch(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
118 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
119 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
121 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
122 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
123 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
124 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
125 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
126 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
127 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
128 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
129 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
130 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
131 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
132 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
133 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
134 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
135 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
136 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
137 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
138 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
139 5c860e29 2018-03-12 stsp
140 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
141 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
142 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
143 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
144 7848a0e1 2020-03-19 stsp { "fetch", cmd_fetch, usage_fetch, "fe" },
145 2ab43947 2020-03-18 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
146 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
147 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
148 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
149 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
150 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
151 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
152 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
153 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
154 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
155 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
156 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
157 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
158 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
159 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
160 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
161 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
162 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
163 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
164 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
165 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
166 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
167 5c860e29 2018-03-12 stsp };
168 ce5b7c56 2019-07-09 stsp
169 ce5b7c56 2019-07-09 stsp static void
170 ce5b7c56 2019-07-09 stsp list_commands(void)
171 ce5b7c56 2019-07-09 stsp {
172 ce5b7c56 2019-07-09 stsp int i;
173 ce5b7c56 2019-07-09 stsp
174 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
175 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
176 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
177 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
178 ce5b7c56 2019-07-09 stsp }
179 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
180 ce5b7c56 2019-07-09 stsp }
181 5c860e29 2018-03-12 stsp
182 5c860e29 2018-03-12 stsp int
183 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
184 5c860e29 2018-03-12 stsp {
185 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
186 5c860e29 2018-03-12 stsp unsigned int i;
187 5c860e29 2018-03-12 stsp int ch;
188 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
189 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
190 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
191 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
192 83cd27f8 2020-01-13 stsp };
193 5c860e29 2018-03-12 stsp
194 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
195 5c860e29 2018-03-12 stsp
196 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
197 5c860e29 2018-03-12 stsp switch (ch) {
198 1b6b95a8 2018-03-12 stsp case 'h':
199 1b6b95a8 2018-03-12 stsp hflag = 1;
200 53ccebc2 2019-07-30 stsp break;
201 53ccebc2 2019-07-30 stsp case 'V':
202 53ccebc2 2019-07-30 stsp Vflag = 1;
203 1b6b95a8 2018-03-12 stsp break;
204 5c860e29 2018-03-12 stsp default:
205 ce5b7c56 2019-07-09 stsp usage(hflag);
206 5c860e29 2018-03-12 stsp /* NOTREACHED */
207 5c860e29 2018-03-12 stsp }
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp argc -= optind;
211 5c860e29 2018-03-12 stsp argv += optind;
212 1e70621d 2018-03-27 stsp optind = 0;
213 53ccebc2 2019-07-30 stsp
214 53ccebc2 2019-07-30 stsp if (Vflag) {
215 53ccebc2 2019-07-30 stsp got_version_print_str();
216 53ccebc2 2019-07-30 stsp return 1;
217 53ccebc2 2019-07-30 stsp }
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp if (argc <= 0)
220 ce5b7c56 2019-07-09 stsp usage(hflag);
221 5c860e29 2018-03-12 stsp
222 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
223 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
224 99437157 2018-11-11 stsp
225 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
226 d7d4f210 2018-03-12 stsp const struct got_error *error;
227 d7d4f210 2018-03-12 stsp
228 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
229 5c860e29 2018-03-12 stsp
230 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
231 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
232 5c860e29 2018-03-12 stsp continue;
233 5c860e29 2018-03-12 stsp
234 1b6b95a8 2018-03-12 stsp if (hflag)
235 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
236 1b6b95a8 2018-03-12 stsp
237 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
238 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
239 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
240 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
241 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
242 70015d7a 2019-11-08 stsp !(sigint_received &&
243 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
244 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
245 d7d4f210 2018-03-12 stsp return 1;
246 d7d4f210 2018-03-12 stsp }
247 d7d4f210 2018-03-12 stsp
248 d7d4f210 2018-03-12 stsp return 0;
249 5c860e29 2018-03-12 stsp }
250 5c860e29 2018-03-12 stsp
251 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
252 ce5b7c56 2019-07-09 stsp list_commands();
253 5c860e29 2018-03-12 stsp return 1;
254 5c860e29 2018-03-12 stsp }
255 5c860e29 2018-03-12 stsp
256 4ed7e80c 2018-05-20 stsp __dead static void
257 ce5b7c56 2019-07-09 stsp usage(int hflag)
258 5c860e29 2018-03-12 stsp {
259 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
260 53ccebc2 2019-07-30 stsp getprogname());
261 ce5b7c56 2019-07-09 stsp if (hflag)
262 ce5b7c56 2019-07-09 stsp list_commands();
263 5c860e29 2018-03-12 stsp exit(1);
264 5c860e29 2018-03-12 stsp }
265 5c860e29 2018-03-12 stsp
266 0266afb7 2019-01-04 stsp static const struct got_error *
267 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
268 e2ba3d07 2019-05-13 stsp {
269 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
270 e2ba3d07 2019-05-13 stsp const char *editor;
271 8920fa04 2019-08-18 stsp
272 8920fa04 2019-08-18 stsp *abspath = NULL;
273 e2ba3d07 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
275 e2ba3d07 2019-05-13 stsp if (editor == NULL)
276 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
277 e2ba3d07 2019-05-13 stsp
278 0ee7065d 2019-05-13 stsp if (editor) {
279 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
280 0ee7065d 2019-05-13 stsp if (err)
281 0ee7065d 2019-05-13 stsp return err;
282 0ee7065d 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
285 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
286 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
287 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
288 0ee7065d 2019-05-13 stsp }
289 0ee7065d 2019-05-13 stsp
290 e2ba3d07 2019-05-13 stsp return NULL;
291 e2ba3d07 2019-05-13 stsp }
292 e2ba3d07 2019-05-13 stsp
293 e2ba3d07 2019-05-13 stsp static const struct got_error *
294 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
295 c530dc23 2019-07-23 stsp const char *worktree_path)
296 0266afb7 2019-01-04 stsp {
297 163ce85a 2019-05-13 stsp const struct got_error *err;
298 0266afb7 2019-01-04 stsp
299 37c06ea4 2019-07-15 stsp #ifdef PROFILE
300 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
301 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
302 37c06ea4 2019-07-15 stsp #endif
303 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
304 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
305 0266afb7 2019-01-04 stsp
306 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
307 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
308 0266afb7 2019-01-04 stsp
309 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
310 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
311 0266afb7 2019-01-04 stsp
312 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
313 163ce85a 2019-05-13 stsp if (err != NULL)
314 163ce85a 2019-05-13 stsp return err;
315 0266afb7 2019-01-04 stsp
316 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
317 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
318 0266afb7 2019-01-04 stsp
319 0266afb7 2019-01-04 stsp return NULL;
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp __dead static void
323 2c7829a4 2019-06-17 stsp usage_init(void)
324 2c7829a4 2019-06-17 stsp {
325 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
326 2c7829a4 2019-06-17 stsp exit(1);
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp
329 2c7829a4 2019-06-17 stsp static const struct got_error *
330 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
331 2c7829a4 2019-06-17 stsp {
332 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
333 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
334 2c7829a4 2019-06-17 stsp int ch;
335 2c7829a4 2019-06-17 stsp
336 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
337 2c7829a4 2019-06-17 stsp switch (ch) {
338 2c7829a4 2019-06-17 stsp default:
339 2c7829a4 2019-06-17 stsp usage_init();
340 2c7829a4 2019-06-17 stsp /* NOTREACHED */
341 2c7829a4 2019-06-17 stsp }
342 2c7829a4 2019-06-17 stsp }
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp argc -= optind;
345 2c7829a4 2019-06-17 stsp argv += optind;
346 2c7829a4 2019-06-17 stsp
347 2c7829a4 2019-06-17 stsp #ifndef PROFILE
348 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
349 2c7829a4 2019-06-17 stsp err(1, "pledge");
350 2c7829a4 2019-06-17 stsp #endif
351 2c7829a4 2019-06-17 stsp if (argc != 1)
352 bc20e173 2019-06-17 stsp usage_init();
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
355 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
356 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
357 2c7829a4 2019-06-17 stsp
358 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
361 2c7829a4 2019-06-17 stsp if (error &&
362 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
366 2c7829a4 2019-06-17 stsp if (error)
367 2c7829a4 2019-06-17 stsp goto done;
368 2c7829a4 2019-06-17 stsp
369 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
370 3ce1b845 2019-07-15 stsp done:
371 3ce1b845 2019-07-15 stsp free(repo_path);
372 3ce1b845 2019-07-15 stsp return error;
373 3ce1b845 2019-07-15 stsp }
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp __dead static void
376 3ce1b845 2019-07-15 stsp usage_import(void)
377 3ce1b845 2019-07-15 stsp {
378 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
379 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
380 3ce1b845 2019-07-15 stsp exit(1);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp int
384 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
385 3ce1b845 2019-07-15 stsp {
386 3ce1b845 2019-07-15 stsp pid_t pid;
387 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
388 3ce1b845 2019-07-15 stsp int st = -1;
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
391 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
392 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
395 3ce1b845 2019-07-15 stsp case -1:
396 3ce1b845 2019-07-15 stsp goto doneediting;
397 3ce1b845 2019-07-15 stsp case 0:
398 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
399 3ce1b845 2019-07-15 stsp _exit(127);
400 3ce1b845 2019-07-15 stsp }
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
403 3ce1b845 2019-07-15 stsp if (errno != EINTR)
404 3ce1b845 2019-07-15 stsp break;
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp doneediting:
407 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
408 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
409 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
410 3ce1b845 2019-07-15 stsp
411 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
412 3ce1b845 2019-07-15 stsp errno = EINTR;
413 3ce1b845 2019-07-15 stsp return -1;
414 3ce1b845 2019-07-15 stsp }
415 3ce1b845 2019-07-15 stsp
416 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
417 3ce1b845 2019-07-15 stsp }
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp static const struct got_error *
420 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
421 3ce1b845 2019-07-15 stsp const char *initial_content)
422 3ce1b845 2019-07-15 stsp {
423 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
424 3ce1b845 2019-07-15 stsp char buf[1024];
425 3ce1b845 2019-07-15 stsp struct stat st, st2;
426 3ce1b845 2019-07-15 stsp FILE *fp;
427 3ce1b845 2019-07-15 stsp int content_changed = 0;
428 3ce1b845 2019-07-15 stsp size_t len;
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp *logmsg = NULL;
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
433 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
434 3ce1b845 2019-07-15 stsp
435 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
439 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
442 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
443 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
446 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
447 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
448 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
449 3ce1b845 2019-07-15 stsp len = 0;
450 3ce1b845 2019-07-15 stsp
451 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
452 3ce1b845 2019-07-15 stsp if (fp == NULL) {
453 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
454 3ce1b845 2019-07-15 stsp goto done;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
457 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
458 3ce1b845 2019-07-15 stsp content_changed = 1;
459 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
460 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
461 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
462 3ce1b845 2019-07-15 stsp }
463 3ce1b845 2019-07-15 stsp fclose(fp);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
466 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
467 3ce1b845 2019-07-15 stsp len--;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
471 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
472 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
473 3ce1b845 2019-07-15 stsp done:
474 3ce1b845 2019-07-15 stsp if (err) {
475 3ce1b845 2019-07-15 stsp free(*logmsg);
476 3ce1b845 2019-07-15 stsp *logmsg = NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp return err;
479 3ce1b845 2019-07-15 stsp }
480 3ce1b845 2019-07-15 stsp
481 3ce1b845 2019-07-15 stsp static const struct got_error *
482 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
483 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
484 3ce1b845 2019-07-15 stsp {
485 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
486 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
487 3ce1b845 2019-07-15 stsp int fd;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
490 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
491 3ce1b845 2019-07-15 stsp branch_name) == -1)
492 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
493 3ce1b845 2019-07-15 stsp
494 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
495 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
496 3ce1b845 2019-07-15 stsp if (err)
497 3ce1b845 2019-07-15 stsp goto done;
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
500 3ce1b845 2019-07-15 stsp close(fd);
501 3ce1b845 2019-07-15 stsp
502 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
503 3ce1b845 2019-07-15 stsp done:
504 3ce1b845 2019-07-15 stsp free(initial_content);
505 3ce1b845 2019-07-15 stsp return err;
506 3ce1b845 2019-07-15 stsp }
507 3ce1b845 2019-07-15 stsp
508 3ce1b845 2019-07-15 stsp static const struct got_error *
509 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
510 3ce1b845 2019-07-15 stsp {
511 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
512 3ce1b845 2019-07-15 stsp return NULL;
513 3ce1b845 2019-07-15 stsp }
514 3ce1b845 2019-07-15 stsp
515 3ce1b845 2019-07-15 stsp static const struct got_error *
516 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
517 84792843 2019-08-09 stsp {
518 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
519 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp *author = NULL;
522 aba9c984 2019-09-08 stsp
523 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
528 aba9c984 2019-09-08 stsp return NULL;
529 aba9c984 2019-09-08 stsp }
530 84792843 2019-08-09 stsp
531 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
532 84792843 2019-08-09 stsp if (got_author == NULL) {
533 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
534 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
535 c9956ddf 2019-09-08 stsp if (name && email) {
536 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
537 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
538 c9956ddf 2019-09-08 stsp return NULL;
539 c9956ddf 2019-09-08 stsp }
540 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
541 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
542 84792843 2019-08-09 stsp }
543 84792843 2019-08-09 stsp
544 aba9c984 2019-09-08 stsp *author = strdup(got_author);
545 aba9c984 2019-09-08 stsp if (*author == NULL)
546 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
547 84792843 2019-08-09 stsp
548 84792843 2019-08-09 stsp /*
549 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
550 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
551 84792843 2019-08-09 stsp */
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
553 84792843 2019-08-09 stsp got_author++;
554 aba9c984 2019-09-08 stsp if (*got_author != '<') {
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp goto done;
557 aba9c984 2019-09-08 stsp }
558 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
559 84792843 2019-08-09 stsp got_author++;
560 aba9c984 2019-09-08 stsp if (*got_author != '@') {
561 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
562 aba9c984 2019-09-08 stsp goto done;
563 aba9c984 2019-09-08 stsp }
564 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
565 84792843 2019-08-09 stsp got_author++;
566 84792843 2019-08-09 stsp if (*got_author != '>')
567 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
568 aba9c984 2019-09-08 stsp done:
569 aba9c984 2019-09-08 stsp if (err) {
570 aba9c984 2019-09-08 stsp free(*author);
571 aba9c984 2019-09-08 stsp *author = NULL;
572 aba9c984 2019-09-08 stsp }
573 aba9c984 2019-09-08 stsp return err;
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp static const struct got_error *
577 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
578 c9956ddf 2019-09-08 stsp {
579 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
580 c9956ddf 2019-09-08 stsp
581 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
582 c9956ddf 2019-09-08 stsp if (homedir) {
583 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
584 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
585 c9956ddf 2019-09-08 stsp
586 c9956ddf 2019-09-08 stsp }
587 c9956ddf 2019-09-08 stsp return NULL;
588 84792843 2019-08-09 stsp }
589 84792843 2019-08-09 stsp
590 84792843 2019-08-09 stsp static const struct got_error *
591 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
592 3ce1b845 2019-07-15 stsp {
593 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
594 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
595 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
596 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
597 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
598 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
599 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
600 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
601 3ce1b845 2019-07-15 stsp int ch;
602 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
603 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
604 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
605 3ce1b845 2019-07-15 stsp
606 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
609 3ce1b845 2019-07-15 stsp switch (ch) {
610 3ce1b845 2019-07-15 stsp case 'b':
611 3ce1b845 2019-07-15 stsp branch_name = optarg;
612 3ce1b845 2019-07-15 stsp break;
613 3ce1b845 2019-07-15 stsp case 'm':
614 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
615 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
616 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
617 3ce1b845 2019-07-15 stsp goto done;
618 3ce1b845 2019-07-15 stsp }
619 3ce1b845 2019-07-15 stsp break;
620 3ce1b845 2019-07-15 stsp case 'r':
621 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
622 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
623 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
624 9ba1d308 2019-10-21 stsp optarg);
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp break;
628 3ce1b845 2019-07-15 stsp case 'I':
629 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
630 3ce1b845 2019-07-15 stsp break;
631 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
632 3ce1b845 2019-07-15 stsp NULL);
633 3ce1b845 2019-07-15 stsp if (error)
634 3ce1b845 2019-07-15 stsp goto done;
635 3ce1b845 2019-07-15 stsp break;
636 3ce1b845 2019-07-15 stsp default:
637 b2b65d18 2019-08-22 stsp usage_import();
638 3ce1b845 2019-07-15 stsp /* NOTREACHED */
639 3ce1b845 2019-07-15 stsp }
640 3ce1b845 2019-07-15 stsp }
641 3ce1b845 2019-07-15 stsp
642 3ce1b845 2019-07-15 stsp argc -= optind;
643 3ce1b845 2019-07-15 stsp argv += optind;
644 3ce1b845 2019-07-15 stsp
645 3ce1b845 2019-07-15 stsp #ifndef PROFILE
646 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
647 aba9c984 2019-09-08 stsp "unveil",
648 3ce1b845 2019-07-15 stsp NULL) == -1)
649 3ce1b845 2019-07-15 stsp err(1, "pledge");
650 3ce1b845 2019-07-15 stsp #endif
651 3ce1b845 2019-07-15 stsp if (argc != 1)
652 3ce1b845 2019-07-15 stsp usage_import();
653 2c7829a4 2019-06-17 stsp
654 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
655 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
656 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
657 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
658 3ce1b845 2019-07-15 stsp }
659 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
660 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
661 c9956ddf 2019-09-08 stsp if (error)
662 c9956ddf 2019-09-08 stsp goto done;
663 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
664 3ce1b845 2019-07-15 stsp if (error)
665 3ce1b845 2019-07-15 stsp goto done;
666 aba9c984 2019-09-08 stsp
667 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
668 aba9c984 2019-09-08 stsp if (error)
669 aba9c984 2019-09-08 stsp return error;
670 e560b7e0 2019-11-28 stsp
671 e560b7e0 2019-11-28 stsp /*
672 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
673 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
674 e560b7e0 2019-11-28 stsp * an unintended typo.
675 e560b7e0 2019-11-28 stsp */
676 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
677 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
680 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
685 3ce1b845 2019-07-15 stsp if (error) {
686 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp } else {
689 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
690 3ce1b845 2019-07-15 stsp "import target branch already exists");
691 3ce1b845 2019-07-15 stsp goto done;
692 3ce1b845 2019-07-15 stsp }
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
695 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
696 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp }
699 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
700 3ce1b845 2019-07-15 stsp
701 3ce1b845 2019-07-15 stsp /*
702 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
703 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
704 3ce1b845 2019-07-15 stsp */
705 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
706 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
707 3ce1b845 2019-07-15 stsp if (error)
708 3ce1b845 2019-07-15 stsp goto done;
709 8e158b01 2019-09-22 stsp free(logmsg);
710 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
711 ef293bdd 2019-10-21 stsp path_dir, refname);
712 ef293bdd 2019-10-21 stsp if (error) {
713 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
714 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
715 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
716 3ce1b845 2019-07-15 stsp goto done;
717 ef293bdd 2019-10-21 stsp }
718 3ce1b845 2019-07-15 stsp }
719 3ce1b845 2019-07-15 stsp
720 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
721 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 ef293bdd 2019-10-21 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 ef293bdd 2019-10-21 stsp
734 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
735 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
736 ef293bdd 2019-10-21 stsp if (error) {
737 ef293bdd 2019-10-21 stsp if (logmsg_path)
738 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
739 3ce1b845 2019-07-15 stsp goto done;
740 ef293bdd 2019-10-21 stsp }
741 3ce1b845 2019-07-15 stsp
742 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
743 ef293bdd 2019-10-21 stsp if (error) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
750 ef293bdd 2019-10-21 stsp if (error) {
751 ef293bdd 2019-10-21 stsp if (logmsg_path)
752 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
753 3ce1b845 2019-07-15 stsp goto done;
754 ef293bdd 2019-10-21 stsp }
755 3ce1b845 2019-07-15 stsp
756 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
757 ef293bdd 2019-10-21 stsp if (error) {
758 ef293bdd 2019-10-21 stsp if (logmsg_path)
759 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
760 3ce1b845 2019-07-15 stsp goto done;
761 ef293bdd 2019-10-21 stsp }
762 3ce1b845 2019-07-15 stsp
763 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
764 3ce1b845 2019-07-15 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
766 ef293bdd 2019-10-21 stsp if (logmsg_path)
767 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
768 3ce1b845 2019-07-15 stsp goto done;
769 ef293bdd 2019-10-21 stsp }
770 3ce1b845 2019-07-15 stsp
771 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
772 3ce1b845 2019-07-15 stsp branch_ref);
773 ef293bdd 2019-10-21 stsp if (error) {
774 ef293bdd 2019-10-21 stsp if (logmsg_path)
775 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
776 3ce1b845 2019-07-15 stsp goto done;
777 ef293bdd 2019-10-21 stsp }
778 3ce1b845 2019-07-15 stsp
779 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
780 ef293bdd 2019-10-21 stsp if (error) {
781 ef293bdd 2019-10-21 stsp if (logmsg_path)
782 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
783 3ce1b845 2019-07-15 stsp goto done;
784 ef293bdd 2019-10-21 stsp }
785 3ce1b845 2019-07-15 stsp }
786 3ce1b845 2019-07-15 stsp
787 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
788 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
789 2c7829a4 2019-06-17 stsp done:
790 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
791 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
792 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
793 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
794 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
795 8e158b01 2019-09-22 stsp free(logmsg);
796 ef293bdd 2019-10-21 stsp free(logmsg_path);
797 2c7829a4 2019-06-17 stsp free(repo_path);
798 3ce1b845 2019-07-15 stsp free(editor);
799 3ce1b845 2019-07-15 stsp free(refname);
800 3ce1b845 2019-07-15 stsp free(new_commit_id);
801 3ce1b845 2019-07-15 stsp free(id_str);
802 aba9c984 2019-09-08 stsp free(author);
803 c9956ddf 2019-09-08 stsp free(gitconfig_path);
804 3ce1b845 2019-07-15 stsp if (branch_ref)
805 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
806 3ce1b845 2019-07-15 stsp if (head_ref)
807 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
808 2c7829a4 2019-06-17 stsp return error;
809 93658fb9 2020-03-18 stsp }
810 93658fb9 2020-03-18 stsp
811 93658fb9 2020-03-18 stsp __dead static void
812 93658fb9 2020-03-18 stsp usage_clone(void)
813 93658fb9 2020-03-18 stsp {
814 13f12b09 2020-03-21 stsp fprintf(stderr, "usage: %s clone [-a] [-b branch] [-l] [-m] [-q] [-v] "
815 0e4002ca 2020-03-21 stsp "[-R reference] repository-url [directory]\n", getprogname());
816 93658fb9 2020-03-18 stsp exit(1);
817 93658fb9 2020-03-18 stsp }
818 892ac3b6 2020-03-18 stsp
819 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg {
820 892ac3b6 2020-03-18 stsp char last_scaled_size[FMT_SCALED_STRSIZE];
821 892ac3b6 2020-03-18 stsp int last_p_indexed;
822 892ac3b6 2020-03-18 stsp int last_p_resolved;
823 68999b92 2020-03-18 stsp int verbosity;
824 892ac3b6 2020-03-18 stsp };
825 93658fb9 2020-03-18 stsp
826 93658fb9 2020-03-18 stsp static const struct got_error *
827 baa9fea0 2020-03-18 stsp fetch_progress(void *arg, const char *message, off_t packfile_size,
828 668a20f6 2020-03-18 stsp int nobj_total, int nobj_indexed, int nobj_loose, int nobj_resolved)
829 531c3985 2020-03-18 stsp {
830 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg *a = arg;
831 892ac3b6 2020-03-18 stsp char scaled_size[FMT_SCALED_STRSIZE];
832 892ac3b6 2020-03-18 stsp int p_indexed, p_resolved;
833 892ac3b6 2020-03-18 stsp int print_size = 0, print_indexed = 0, print_resolved = 0;
834 b2409d58 2020-03-18 stsp
835 68999b92 2020-03-18 stsp if (a->verbosity < 0)
836 68999b92 2020-03-18 stsp return NULL;
837 68999b92 2020-03-18 stsp
838 fd843b58 2020-03-18 stsp if (message && message[0] != '\0') {
839 d2cdc636 2020-03-18 stsp printf("\rserver: %s", message);
840 892ac3b6 2020-03-18 stsp fflush(stdout);
841 12d1281e 2020-03-19 stsp return NULL;
842 b2409d58 2020-03-18 stsp }
843 b2409d58 2020-03-18 stsp
844 b2409d58 2020-03-18 stsp if (packfile_size > 0 || nobj_indexed > 0) {
845 892ac3b6 2020-03-18 stsp if (fmt_scaled(packfile_size, scaled_size) == 0 &&
846 892ac3b6 2020-03-18 stsp (a->last_scaled_size[0] == '\0' ||
847 892ac3b6 2020-03-18 stsp strcmp(scaled_size, a->last_scaled_size)) != 0) {
848 892ac3b6 2020-03-18 stsp print_size = 1;
849 892ac3b6 2020-03-18 stsp if (strlcpy(a->last_scaled_size, scaled_size,
850 892ac3b6 2020-03-18 stsp FMT_SCALED_STRSIZE) >= FMT_SCALED_STRSIZE)
851 892ac3b6 2020-03-18 stsp return got_error(GOT_ERR_NO_SPACE);
852 892ac3b6 2020-03-18 stsp }
853 61cc1a7a 2020-03-18 stsp if (nobj_indexed > 0) {
854 892ac3b6 2020-03-18 stsp p_indexed = (nobj_indexed * 100) / nobj_total;
855 892ac3b6 2020-03-18 stsp if (p_indexed != a->last_p_indexed) {
856 892ac3b6 2020-03-18 stsp a->last_p_indexed = p_indexed;
857 892ac3b6 2020-03-18 stsp print_indexed = 1;
858 892ac3b6 2020-03-18 stsp print_size = 1;
859 892ac3b6 2020-03-18 stsp }
860 61cc1a7a 2020-03-18 stsp }
861 61cc1a7a 2020-03-18 stsp if (nobj_resolved > 0) {
862 892ac3b6 2020-03-18 stsp p_resolved = (nobj_resolved * 100) /
863 892ac3b6 2020-03-18 stsp (nobj_total - nobj_loose);
864 892ac3b6 2020-03-18 stsp if (p_resolved != a->last_p_resolved) {
865 892ac3b6 2020-03-18 stsp a->last_p_resolved = p_resolved;
866 892ac3b6 2020-03-18 stsp print_resolved = 1;
867 892ac3b6 2020-03-18 stsp print_indexed = 1;
868 892ac3b6 2020-03-18 stsp print_size = 1;
869 892ac3b6 2020-03-18 stsp }
870 61cc1a7a 2020-03-18 stsp }
871 b2409d58 2020-03-18 stsp
872 d2cdc636 2020-03-18 stsp }
873 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
874 892ac3b6 2020-03-18 stsp printf("\r");
875 892ac3b6 2020-03-18 stsp if (print_size)
876 892ac3b6 2020-03-18 stsp printf("%*s fetched", FMT_SCALED_STRSIZE, scaled_size);
877 d715f13e 2020-03-19 stsp if (print_indexed)
878 892ac3b6 2020-03-18 stsp printf("; indexing %d%%", p_indexed);
879 d715f13e 2020-03-19 stsp if (print_resolved)
880 892ac3b6 2020-03-18 stsp printf("; resolving deltas %d%%", p_resolved);
881 892ac3b6 2020-03-18 stsp if (print_size || print_indexed || print_resolved)
882 892ac3b6 2020-03-18 stsp fflush(stdout);
883 892ac3b6 2020-03-18 stsp
884 531c3985 2020-03-18 stsp return NULL;
885 531c3985 2020-03-18 stsp }
886 531c3985 2020-03-18 stsp
887 531c3985 2020-03-18 stsp static const struct got_error *
888 f298ae0f 2020-03-25 stsp create_symref(const char *refname, struct got_reference *target_ref,
889 f298ae0f 2020-03-25 stsp int verbosity, struct got_repository *repo)
890 4ba14133 2020-03-20 stsp {
891 4ba14133 2020-03-20 stsp const struct got_error *err;
892 4ba14133 2020-03-20 stsp struct got_reference *head_symref;
893 4ba14133 2020-03-20 stsp
894 f298ae0f 2020-03-25 stsp err = got_ref_alloc_symref(&head_symref, refname, target_ref);
895 4ba14133 2020-03-20 stsp if (err)
896 4ba14133 2020-03-20 stsp return err;
897 4ba14133 2020-03-20 stsp
898 4ba14133 2020-03-20 stsp err = got_ref_write(head_symref, repo);
899 4ba14133 2020-03-20 stsp got_ref_close(head_symref);
900 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity > 0) {
901 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", GOT_REF_HEAD,
902 6338a6a1 2020-03-21 stsp got_ref_get_name(target_ref));
903 6338a6a1 2020-03-21 stsp }
904 4ba14133 2020-03-20 stsp return err;
905 4ba14133 2020-03-20 stsp }
906 4ba14133 2020-03-20 stsp
907 4ba14133 2020-03-20 stsp static const struct got_error *
908 41b0de12 2020-03-21 stsp list_remote_refs(struct got_pathlist_head *symrefs,
909 41b0de12 2020-03-21 stsp struct got_pathlist_head *refs)
910 41b0de12 2020-03-21 stsp {
911 41b0de12 2020-03-21 stsp const struct got_error *err;
912 41b0de12 2020-03-21 stsp struct got_pathlist_entry *pe;
913 41b0de12 2020-03-21 stsp
914 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, symrefs, entry) {
915 41b0de12 2020-03-21 stsp const char *refname = pe->path;
916 41b0de12 2020-03-21 stsp const char *targetref = pe->data;
917 41b0de12 2020-03-21 stsp
918 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, targetref);
919 41b0de12 2020-03-21 stsp }
920 41b0de12 2020-03-21 stsp
921 41b0de12 2020-03-21 stsp TAILQ_FOREACH(pe, refs, entry) {
922 41b0de12 2020-03-21 stsp const char *refname = pe->path;
923 41b0de12 2020-03-21 stsp struct got_object_id *id = pe->data;
924 41b0de12 2020-03-21 stsp char *id_str;
925 41b0de12 2020-03-21 stsp
926 41b0de12 2020-03-21 stsp err = got_object_id_str(&id_str, id);
927 41b0de12 2020-03-21 stsp if (err)
928 41b0de12 2020-03-21 stsp return err;
929 41b0de12 2020-03-21 stsp printf("%s: %s\n", refname, id_str);
930 41b0de12 2020-03-21 stsp free(id_str);
931 41b0de12 2020-03-21 stsp }
932 41b0de12 2020-03-21 stsp
933 41b0de12 2020-03-21 stsp return NULL;
934 6338a6a1 2020-03-21 stsp }
935 6338a6a1 2020-03-21 stsp
936 6338a6a1 2020-03-21 stsp static const struct got_error *
937 6338a6a1 2020-03-21 stsp create_ref(const char *refname, struct got_object_id *id,
938 6338a6a1 2020-03-21 stsp int verbosity, struct got_repository *repo)
939 6338a6a1 2020-03-21 stsp {
940 6338a6a1 2020-03-21 stsp const struct got_error *err = NULL;
941 6338a6a1 2020-03-21 stsp struct got_reference *ref;
942 6338a6a1 2020-03-21 stsp char *id_str;
943 6338a6a1 2020-03-21 stsp
944 6338a6a1 2020-03-21 stsp err = got_object_id_str(&id_str, id);
945 6338a6a1 2020-03-21 stsp if (err)
946 6338a6a1 2020-03-21 stsp return err;
947 6338a6a1 2020-03-21 stsp
948 6338a6a1 2020-03-21 stsp err = got_ref_alloc(&ref, refname, id);
949 6338a6a1 2020-03-21 stsp if (err)
950 6338a6a1 2020-03-21 stsp goto done;
951 6338a6a1 2020-03-21 stsp
952 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
953 6338a6a1 2020-03-21 stsp got_ref_close(ref);
954 6338a6a1 2020-03-21 stsp
955 6338a6a1 2020-03-21 stsp if (err == NULL && verbosity >= 0)
956 6338a6a1 2020-03-21 stsp printf("Created reference %s: %s\n", refname, id_str);
957 6338a6a1 2020-03-21 stsp done:
958 6338a6a1 2020-03-21 stsp free(id_str);
959 6338a6a1 2020-03-21 stsp return err;
960 0e4002ca 2020-03-21 stsp }
961 0e4002ca 2020-03-21 stsp
962 0e4002ca 2020-03-21 stsp static int
963 0e4002ca 2020-03-21 stsp match_wanted_ref(const char *refname, const char *wanted_ref)
964 0e4002ca 2020-03-21 stsp {
965 0e4002ca 2020-03-21 stsp if (strncmp(refname, "refs/", 5) != 0)
966 0e4002ca 2020-03-21 stsp return 0;
967 0e4002ca 2020-03-21 stsp refname += 5;
968 0e4002ca 2020-03-21 stsp
969 0e4002ca 2020-03-21 stsp /*
970 0e4002ca 2020-03-21 stsp * Prevent fetching of references that won't make any
971 0e4002ca 2020-03-21 stsp * sense outside of the remote repository's context.
972 0e4002ca 2020-03-21 stsp */
973 0e4002ca 2020-03-21 stsp if (strncmp(refname, "got/", 4) == 0)
974 0e4002ca 2020-03-21 stsp return 0;
975 0e4002ca 2020-03-21 stsp if (strncmp(refname, "remotes/", 8) == 0)
976 0e4002ca 2020-03-21 stsp return 0;
977 0e4002ca 2020-03-21 stsp
978 0e4002ca 2020-03-21 stsp if (strncmp(wanted_ref, "refs/", 5) == 0)
979 0e4002ca 2020-03-21 stsp wanted_ref += 5;
980 0e4002ca 2020-03-21 stsp
981 0e4002ca 2020-03-21 stsp /* Allow prefix match. */
982 0e4002ca 2020-03-21 stsp if (got_path_is_child(refname, wanted_ref, strlen(wanted_ref)))
983 0e4002ca 2020-03-21 stsp return 1;
984 0e4002ca 2020-03-21 stsp
985 0e4002ca 2020-03-21 stsp /* Allow exact match. */
986 0e4002ca 2020-03-21 stsp return (strcmp(refname, wanted_ref) == 0);
987 41b0de12 2020-03-21 stsp }
988 41b0de12 2020-03-21 stsp
989 0e4002ca 2020-03-21 stsp static int
990 0e4002ca 2020-03-21 stsp is_wanted_ref(struct got_pathlist_head *wanted_refs, const char *refname)
991 0e4002ca 2020-03-21 stsp {
992 0e4002ca 2020-03-21 stsp struct got_pathlist_entry *pe;
993 0e4002ca 2020-03-21 stsp
994 0e4002ca 2020-03-21 stsp TAILQ_FOREACH(pe, wanted_refs, entry) {
995 0e4002ca 2020-03-21 stsp if (match_wanted_ref(refname, pe->path))
996 0e4002ca 2020-03-21 stsp return 1;
997 0e4002ca 2020-03-21 stsp }
998 0e4002ca 2020-03-21 stsp
999 0e4002ca 2020-03-21 stsp return 0;
1000 0e4002ca 2020-03-21 stsp }
1001 0e4002ca 2020-03-21 stsp
1002 41b0de12 2020-03-21 stsp static const struct got_error *
1003 0e4002ca 2020-03-21 stsp create_wanted_ref(const char *refname, struct got_object_id *id,
1004 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1005 0e4002ca 2020-03-21 stsp {
1006 0e4002ca 2020-03-21 stsp const struct got_error *err;
1007 0e4002ca 2020-03-21 stsp char *remote_refname;
1008 0e4002ca 2020-03-21 stsp
1009 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1010 0e4002ca 2020-03-21 stsp refname += 5;
1011 0e4002ca 2020-03-21 stsp
1012 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1013 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1014 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1015 0e4002ca 2020-03-21 stsp
1016 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1017 0e4002ca 2020-03-21 stsp free(remote_refname);
1018 0e4002ca 2020-03-21 stsp return err;
1019 0e4002ca 2020-03-21 stsp }
1020 0e4002ca 2020-03-21 stsp
1021 0e4002ca 2020-03-21 stsp static const struct got_error *
1022 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
1023 93658fb9 2020-03-18 stsp {
1024 39c64a6a 2020-03-18 stsp const struct got_error *error = NULL;
1025 9df6f38b 2020-03-18 stsp const char *uri, *dirname;
1026 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
1027 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
1028 bb64b798 2020-03-18 stsp const char *repo_path;
1029 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
1030 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1031 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
1032 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
1033 9c52365f 2020-03-21 stsp int ch, fetchfd = -1, fetchstatus;
1034 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1035 892ac3b6 2020-03-18 stsp struct got_fetch_progress_arg fpa;
1036 b46f3e71 2020-03-18 stsp char *git_url = NULL;
1037 b46f3e71 2020-03-18 stsp char *gitconfig_path = NULL;
1038 b46f3e71 2020-03-18 stsp char *gitconfig = NULL;
1039 b46f3e71 2020-03-18 stsp FILE *gitconfig_file = NULL;
1040 b46f3e71 2020-03-18 stsp ssize_t n;
1041 659e7fbd 2020-03-20 stsp int verbosity = 0, fetch_all_branches = 0, mirror_references = 0;
1042 41b0de12 2020-03-21 stsp int list_refs_only = 0;
1043 659e7fbd 2020-03-20 stsp struct got_reference *head_symref = NULL;
1044 93658fb9 2020-03-18 stsp
1045 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
1046 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
1047 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1048 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1049 d9b4d0c0 2020-03-18 stsp
1050 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:lmvqR:")) != -1) {
1051 93658fb9 2020-03-18 stsp switch (ch) {
1052 659e7fbd 2020-03-20 stsp case 'a':
1053 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1054 4ba14133 2020-03-20 stsp break;
1055 4ba14133 2020-03-20 stsp case 'b':
1056 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1057 4ba14133 2020-03-20 stsp optarg, NULL);
1058 4ba14133 2020-03-20 stsp if (error)
1059 4ba14133 2020-03-20 stsp return error;
1060 659e7fbd 2020-03-20 stsp break;
1061 41b0de12 2020-03-21 stsp case 'l':
1062 41b0de12 2020-03-21 stsp list_refs_only = 1;
1063 41b0de12 2020-03-21 stsp break;
1064 469dd726 2020-03-20 stsp case 'm':
1065 469dd726 2020-03-20 stsp mirror_references = 1;
1066 469dd726 2020-03-20 stsp break;
1067 68999b92 2020-03-18 stsp case 'v':
1068 68999b92 2020-03-18 stsp if (verbosity < 0)
1069 68999b92 2020-03-18 stsp verbosity = 0;
1070 68999b92 2020-03-18 stsp else if (verbosity < 3)
1071 68999b92 2020-03-18 stsp verbosity++;
1072 68999b92 2020-03-18 stsp break;
1073 68999b92 2020-03-18 stsp case 'q':
1074 68999b92 2020-03-18 stsp verbosity = -1;
1075 68999b92 2020-03-18 stsp break;
1076 0e4002ca 2020-03-21 stsp case 'R':
1077 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1078 0e4002ca 2020-03-21 stsp optarg, NULL);
1079 0e4002ca 2020-03-21 stsp if (error)
1080 0e4002ca 2020-03-21 stsp return error;
1081 0e4002ca 2020-03-21 stsp break;
1082 93658fb9 2020-03-18 stsp default:
1083 93658fb9 2020-03-18 stsp usage_clone();
1084 93658fb9 2020-03-18 stsp break;
1085 93658fb9 2020-03-18 stsp }
1086 93658fb9 2020-03-18 stsp }
1087 93658fb9 2020-03-18 stsp argc -= optind;
1088 93658fb9 2020-03-18 stsp argv += optind;
1089 39c64a6a 2020-03-18 stsp
1090 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1091 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1092 41b0de12 2020-03-21 stsp if (list_refs_only) {
1093 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1094 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1095 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1096 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1097 41b0de12 2020-03-21 stsp if (mirror_references)
1098 41b0de12 2020-03-21 stsp errx(1, "-l and -m options are mutually exclusive");
1099 41b0de12 2020-03-21 stsp if (verbosity == -1)
1100 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1101 0e4002ca 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_refs))
1102 0e4002ca 2020-03-21 stsp errx(1, "-l and -R options are mutually exclusive");
1103 41b0de12 2020-03-21 stsp }
1104 4ba14133 2020-03-20 stsp
1105 93658fb9 2020-03-18 stsp uri = argv[0];
1106 9df6f38b 2020-03-18 stsp
1107 9df6f38b 2020-03-18 stsp if (argc == 1)
1108 93658fb9 2020-03-18 stsp dirname = NULL;
1109 9df6f38b 2020-03-18 stsp else if (argc == 2)
1110 93658fb9 2020-03-18 stsp dirname = argv[1];
1111 93658fb9 2020-03-18 stsp else
1112 93658fb9 2020-03-18 stsp usage_clone();
1113 09838ffc 2020-03-18 stsp
1114 39c64a6a 2020-03-18 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1115 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1116 39c64a6a 2020-03-18 stsp if (error)
1117 09f63084 2020-03-20 stsp goto done;
1118 09f63084 2020-03-20 stsp
1119 09f63084 2020-03-20 stsp if (asprintf(&git_url, "%s://%s%s%s%s%s", proto,
1120 09f63084 2020-03-20 stsp host, port ? ":" : "", port ? port : "",
1121 09f63084 2020-03-20 stsp server_path[0] != '/' ? "/" : "", server_path) == -1) {
1122 09f63084 2020-03-20 stsp error = got_error_from_errno("asprintf");
1123 09838ffc 2020-03-18 stsp goto done;
1124 09f63084 2020-03-20 stsp }
1125 09838ffc 2020-03-18 stsp
1126 39c64a6a 2020-03-18 stsp if (strcmp(proto, "git") == 0) {
1127 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1128 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1129 39c64a6a 2020-03-18 stsp "sendfd dns inet unveil", NULL) == -1)
1130 39c64a6a 2020-03-18 stsp err(1, "pledge");
1131 b46f3e71 2020-03-18 stsp #endif
1132 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1133 39c64a6a 2020-03-18 stsp strcmp(proto, "ssh") == 0) {
1134 b46f3e71 2020-03-18 stsp #ifndef PROFILE
1135 39c64a6a 2020-03-18 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1136 39c64a6a 2020-03-18 stsp "sendfd unveil", NULL) == -1)
1137 39c64a6a 2020-03-18 stsp err(1, "pledge");
1138 b46f3e71 2020-03-18 stsp #endif
1139 39c64a6a 2020-03-18 stsp } else if (strcmp(proto, "http") == 0 ||
1140 39c64a6a 2020-03-18 stsp strcmp(proto, "git+http") == 0) {
1141 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1142 39c64a6a 2020-03-18 stsp goto done;
1143 39c64a6a 2020-03-18 stsp } else {
1144 39c64a6a 2020-03-18 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1145 39c64a6a 2020-03-18 stsp goto done;
1146 39c64a6a 2020-03-18 stsp }
1147 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1148 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1149 39c64a6a 2020-03-18 stsp error = got_error_from_errno("asprintf");
1150 bb64b798 2020-03-18 stsp goto done;
1151 bb64b798 2020-03-18 stsp }
1152 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1153 bb64b798 2020-03-18 stsp } else
1154 bb64b798 2020-03-18 stsp repo_path = dirname;
1155 bb64b798 2020-03-18 stsp
1156 41b0de12 2020-03-21 stsp if (!list_refs_only) {
1157 41b0de12 2020-03-21 stsp error = got_path_mkdir(repo_path);
1158 41b0de12 2020-03-21 stsp if (error)
1159 41b0de12 2020-03-21 stsp goto done;
1160 bb64b798 2020-03-18 stsp
1161 41b0de12 2020-03-21 stsp error = got_repo_init(repo_path);
1162 41b0de12 2020-03-21 stsp if (error)
1163 41b0de12 2020-03-21 stsp goto done;
1164 41b0de12 2020-03-21 stsp error = got_repo_open(&repo, repo_path, NULL);
1165 41b0de12 2020-03-21 stsp if (error)
1166 41b0de12 2020-03-21 stsp goto done;
1167 41b0de12 2020-03-21 stsp }
1168 bb64b798 2020-03-18 stsp
1169 ee448f5f 2020-03-18 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1170 ee448f5f 2020-03-18 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1171 ee448f5f 2020-03-18 stsp error = got_error_from_errno2("unveil",
1172 ee448f5f 2020-03-18 stsp GOT_FETCH_PATH_SSH);
1173 ee448f5f 2020-03-18 stsp goto done;
1174 ee448f5f 2020-03-18 stsp }
1175 ee448f5f 2020-03-18 stsp }
1176 41b0de12 2020-03-21 stsp error = apply_unveil(repo ? got_repo_get_path(repo) : NULL, 0, NULL);
1177 ee448f5f 2020-03-18 stsp if (error)
1178 ee448f5f 2020-03-18 stsp goto done;
1179 ee448f5f 2020-03-18 stsp
1180 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1181 9c52365f 2020-03-21 stsp server_path, verbosity);
1182 39c64a6a 2020-03-18 stsp if (error)
1183 bb64b798 2020-03-18 stsp goto done;
1184 294dfefd 2020-03-18 stsp
1185 68999b92 2020-03-18 stsp if (verbosity >= 0)
1186 62a4c94c 2020-03-20 stsp printf("Connected to %s%s%s\n", host,
1187 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1188 bb64b798 2020-03-18 stsp
1189 892ac3b6 2020-03-18 stsp fpa.last_scaled_size[0] = '\0';
1190 892ac3b6 2020-03-18 stsp fpa.last_p_indexed = -1;
1191 892ac3b6 2020-03-18 stsp fpa.last_p_resolved = -1;
1192 68999b92 2020-03-18 stsp fpa.verbosity = verbosity;
1193 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs,
1194 469dd726 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, mirror_references,
1195 0e4002ca 2020-03-21 stsp fetch_all_branches, &wanted_branches, &wanted_refs,
1196 0e4002ca 2020-03-21 stsp list_refs_only, verbosity, fetchfd, repo,
1197 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1198 39c64a6a 2020-03-18 stsp if (error)
1199 d9b4d0c0 2020-03-18 stsp goto done;
1200 d9b4d0c0 2020-03-18 stsp
1201 41b0de12 2020-03-21 stsp if (list_refs_only) {
1202 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1203 41b0de12 2020-03-21 stsp goto done;
1204 41b0de12 2020-03-21 stsp }
1205 41b0de12 2020-03-21 stsp
1206 39c64a6a 2020-03-18 stsp error = got_object_id_str(&id_str, pack_hash);
1207 39c64a6a 2020-03-18 stsp if (error)
1208 d9b4d0c0 2020-03-18 stsp goto done;
1209 68999b92 2020-03-18 stsp if (verbosity >= 0)
1210 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1211 d9b4d0c0 2020-03-18 stsp free(id_str);
1212 d9b4d0c0 2020-03-18 stsp
1213 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1214 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1215 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1216 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1217 7ebc0570 2020-03-18 stsp char *remote_refname;
1218 0e4002ca 2020-03-21 stsp
1219 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1220 0e4002ca 2020-03-21 stsp !mirror_references) {
1221 0e4002ca 2020-03-21 stsp error = create_wanted_ref(refname, id,
1222 0e4002ca 2020-03-21 stsp GOT_FETCH_DEFAULT_REMOTE_NAME,
1223 0e4002ca 2020-03-21 stsp verbosity - 1, repo);
1224 0e4002ca 2020-03-21 stsp if (error)
1225 0e4002ca 2020-03-21 stsp goto done;
1226 0e4002ca 2020-03-21 stsp continue;
1227 0e4002ca 2020-03-21 stsp }
1228 668a20f6 2020-03-18 stsp
1229 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity - 1, repo);
1230 39c64a6a 2020-03-18 stsp if (error)
1231 d9b4d0c0 2020-03-18 stsp goto done;
1232 d9b4d0c0 2020-03-18 stsp
1233 469dd726 2020-03-20 stsp if (mirror_references)
1234 469dd726 2020-03-20 stsp continue;
1235 469dd726 2020-03-20 stsp
1236 7ebc0570 2020-03-18 stsp if (strncmp("refs/heads/", refname, 11) != 0)
1237 7ebc0570 2020-03-18 stsp continue;
1238 7ebc0570 2020-03-18 stsp
1239 7ebc0570 2020-03-18 stsp if (asprintf(&remote_refname,
1240 7ebc0570 2020-03-18 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1241 7ebc0570 2020-03-18 stsp refname + 11) == -1) {
1242 7ebc0570 2020-03-18 stsp error = got_error_from_errno("asprintf");
1243 7ebc0570 2020-03-18 stsp goto done;
1244 7ebc0570 2020-03-18 stsp }
1245 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id, verbosity - 1, repo);
1246 f298ae0f 2020-03-25 stsp free(remote_refname);
1247 39c64a6a 2020-03-18 stsp if (error)
1248 d9b4d0c0 2020-03-18 stsp goto done;
1249 d9b4d0c0 2020-03-18 stsp }
1250 d9b4d0c0 2020-03-18 stsp
1251 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1252 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1253 659e7fbd 2020-03-20 stsp struct got_reference *target_ref;
1254 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1255 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1256 f298ae0f 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1257 d9b4d0c0 2020-03-18 stsp
1258 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1259 d9b4d0c0 2020-03-18 stsp continue;
1260 d9b4d0c0 2020-03-18 stsp
1261 39c64a6a 2020-03-18 stsp error = got_ref_open(&target_ref, repo, target, 0);
1262 39c64a6a 2020-03-18 stsp if (error) {
1263 55330abe 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1264 55330abe 2020-03-20 stsp error = NULL;
1265 d9b4d0c0 2020-03-18 stsp continue;
1266 55330abe 2020-03-20 stsp }
1267 d9b4d0c0 2020-03-18 stsp goto done;
1268 d9b4d0c0 2020-03-18 stsp }
1269 d9b4d0c0 2020-03-18 stsp
1270 f298ae0f 2020-03-25 stsp error = create_symref(refname, target_ref, verbosity, repo);
1271 f298ae0f 2020-03-25 stsp got_ref_close(target_ref);
1272 f298ae0f 2020-03-25 stsp if (error)
1273 f298ae0f 2020-03-25 stsp goto done;
1274 f298ae0f 2020-03-25 stsp
1275 f298ae0f 2020-03-25 stsp if (mirror_references)
1276 f298ae0f 2020-03-25 stsp continue;
1277 f298ae0f 2020-03-25 stsp
1278 f298ae0f 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1279 f298ae0f 2020-03-25 stsp continue;
1280 f298ae0f 2020-03-25 stsp
1281 f298ae0f 2020-03-25 stsp if (asprintf(&remote_refname,
1282 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1283 f298ae0f 2020-03-25 stsp refname) == -1) {
1284 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1285 f298ae0f 2020-03-25 stsp goto done;
1286 f298ae0f 2020-03-25 stsp }
1287 f298ae0f 2020-03-25 stsp if (asprintf(&remote_target,
1288 f298ae0f 2020-03-25 stsp "refs/remotes/%s/%s", GOT_FETCH_DEFAULT_REMOTE_NAME,
1289 f298ae0f 2020-03-25 stsp target + 11) == -1) {
1290 f298ae0f 2020-03-25 stsp error = got_error_from_errno("asprintf");
1291 f298ae0f 2020-03-25 stsp free(remote_refname);
1292 f298ae0f 2020-03-25 stsp goto done;
1293 f298ae0f 2020-03-25 stsp }
1294 f298ae0f 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target, 0);
1295 f298ae0f 2020-03-25 stsp if (error) {
1296 f298ae0f 2020-03-25 stsp free(remote_refname);
1297 f298ae0f 2020-03-25 stsp free(remote_target);
1298 f298ae0f 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1299 f298ae0f 2020-03-25 stsp error = NULL;
1300 f298ae0f 2020-03-25 stsp continue;
1301 f298ae0f 2020-03-25 stsp }
1302 f298ae0f 2020-03-25 stsp goto done;
1303 f298ae0f 2020-03-25 stsp }
1304 f298ae0f 2020-03-25 stsp error = create_symref(remote_refname, target_ref,
1305 f298ae0f 2020-03-25 stsp verbosity - 1, repo);
1306 f298ae0f 2020-03-25 stsp free(remote_refname);
1307 f298ae0f 2020-03-25 stsp free(remote_target);
1308 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1309 39c64a6a 2020-03-18 stsp if (error)
1310 d9b4d0c0 2020-03-18 stsp goto done;
1311 4ba14133 2020-03-20 stsp }
1312 4ba14133 2020-03-20 stsp if (pe == NULL) {
1313 4ba14133 2020-03-20 stsp /*
1314 4ba14133 2020-03-20 stsp * We failed to set the HEAD reference. If we asked for
1315 4ba14133 2020-03-20 stsp * a set of wanted branches use the first of one of those
1316 4ba14133 2020-03-20 stsp * which could be fetched instead.
1317 4ba14133 2020-03-20 stsp */
1318 4ba14133 2020-03-20 stsp TAILQ_FOREACH(pe, &wanted_branches, entry) {
1319 4ba14133 2020-03-20 stsp const char *target = pe->path;
1320 4ba14133 2020-03-20 stsp struct got_reference *target_ref;
1321 d9b4d0c0 2020-03-18 stsp
1322 4ba14133 2020-03-20 stsp error = got_ref_open(&target_ref, repo, target, 0);
1323 4ba14133 2020-03-20 stsp if (error) {
1324 4ba14133 2020-03-20 stsp if (error->code == GOT_ERR_NOT_REF) {
1325 4ba14133 2020-03-20 stsp error = NULL;
1326 4ba14133 2020-03-20 stsp continue;
1327 4ba14133 2020-03-20 stsp }
1328 4ba14133 2020-03-20 stsp goto done;
1329 4ba14133 2020-03-20 stsp }
1330 4ba14133 2020-03-20 stsp
1331 f298ae0f 2020-03-25 stsp error = create_symref(GOT_REF_HEAD, target_ref,
1332 f298ae0f 2020-03-25 stsp verbosity, repo);
1333 4ba14133 2020-03-20 stsp got_ref_close(target_ref);
1334 4ba14133 2020-03-20 stsp if (error)
1335 4ba14133 2020-03-20 stsp goto done;
1336 4ba14133 2020-03-20 stsp break;
1337 4ba14133 2020-03-20 stsp }
1338 659e7fbd 2020-03-20 stsp }
1339 659e7fbd 2020-03-20 stsp
1340 659e7fbd 2020-03-20 stsp /* Create a config file git-fetch(1) can understand. */
1341 659e7fbd 2020-03-20 stsp gitconfig_path = got_repo_get_path_gitconfig(repo);
1342 659e7fbd 2020-03-20 stsp if (gitconfig_path == NULL) {
1343 659e7fbd 2020-03-20 stsp error = got_error_from_errno("got_repo_get_path_gitconfig");
1344 659e7fbd 2020-03-20 stsp goto done;
1345 659e7fbd 2020-03-20 stsp }
1346 659e7fbd 2020-03-20 stsp gitconfig_file = fopen(gitconfig_path, "a");
1347 659e7fbd 2020-03-20 stsp if (gitconfig_file == NULL) {
1348 659e7fbd 2020-03-20 stsp error = got_error_from_errno2("fopen", gitconfig_path);
1349 659e7fbd 2020-03-20 stsp goto done;
1350 b46f3e71 2020-03-18 stsp }
1351 659e7fbd 2020-03-20 stsp if (mirror_references) {
1352 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1353 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1354 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1355 8ceee112 2020-03-20 stsp "\tfetch = +refs/*:refs/*\n"
1356 659e7fbd 2020-03-20 stsp "\tmirror = true\n",
1357 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url) == -1) {
1358 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1359 659e7fbd 2020-03-20 stsp goto done;
1360 659e7fbd 2020-03-20 stsp }
1361 659e7fbd 2020-03-20 stsp } else if (fetch_all_branches) {
1362 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1363 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1364 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1365 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/*:refs/remotes/%s/*\n",
1366 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1367 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME) == -1) {
1368 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1369 659e7fbd 2020-03-20 stsp goto done;
1370 659e7fbd 2020-03-20 stsp }
1371 659e7fbd 2020-03-20 stsp } else {
1372 659e7fbd 2020-03-20 stsp const char *branchname;
1373 d715f13e 2020-03-19 stsp
1374 659e7fbd 2020-03-20 stsp /*
1375 659e7fbd 2020-03-20 stsp * If the server specified a default branch, use just that one.
1376 659e7fbd 2020-03-20 stsp * Otherwise fall back to fetching all branches on next fetch.
1377 659e7fbd 2020-03-20 stsp */
1378 659e7fbd 2020-03-20 stsp if (head_symref) {
1379 659e7fbd 2020-03-20 stsp branchname = got_ref_get_symref_target(head_symref);
1380 659e7fbd 2020-03-20 stsp if (strncmp(branchname, "refs/heads/", 11) == 0)
1381 659e7fbd 2020-03-20 stsp branchname += 11;
1382 659e7fbd 2020-03-20 stsp } else
1383 659e7fbd 2020-03-20 stsp branchname = "*"; /* fall back to all branches */
1384 659e7fbd 2020-03-20 stsp if (asprintf(&gitconfig,
1385 659e7fbd 2020-03-20 stsp "[remote \"%s\"]\n"
1386 659e7fbd 2020-03-20 stsp "\turl = %s\n"
1387 659e7fbd 2020-03-20 stsp "\tfetch = +refs/heads/%s:refs/remotes/%s/%s\n",
1388 659e7fbd 2020-03-20 stsp GOT_FETCH_DEFAULT_REMOTE_NAME, git_url,
1389 659e7fbd 2020-03-20 stsp branchname, GOT_FETCH_DEFAULT_REMOTE_NAME,
1390 659e7fbd 2020-03-20 stsp branchname) == -1) {
1391 659e7fbd 2020-03-20 stsp error = got_error_from_errno("asprintf");
1392 659e7fbd 2020-03-20 stsp goto done;
1393 659e7fbd 2020-03-20 stsp }
1394 659e7fbd 2020-03-20 stsp }
1395 659e7fbd 2020-03-20 stsp n = fwrite(gitconfig, 1, strlen(gitconfig), gitconfig_file);
1396 659e7fbd 2020-03-20 stsp if (n != strlen(gitconfig)) {
1397 659e7fbd 2020-03-20 stsp error = got_ferror(gitconfig_file, GOT_ERR_IO);
1398 659e7fbd 2020-03-20 stsp goto done;
1399 659e7fbd 2020-03-20 stsp }
1400 659e7fbd 2020-03-20 stsp
1401 d715f13e 2020-03-19 stsp if (verbosity >= 0)
1402 469dd726 2020-03-20 stsp printf("Created %s repository '%s'\n",
1403 469dd726 2020-03-20 stsp mirror_references ? "mirrored" : "cloned", repo_path);
1404 09838ffc 2020-03-18 stsp done:
1405 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1406 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1407 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1408 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1409 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1410 9c52365f 2020-03-21 stsp }
1411 39c64a6a 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1412 39c64a6a 2020-03-18 stsp error = got_error_from_errno("close");
1413 b46f3e71 2020-03-18 stsp if (gitconfig_file && fclose(gitconfig_file) == EOF && error == NULL)
1414 b46f3e71 2020-03-18 stsp error = got_error_from_errno("fclose");
1415 bb64b798 2020-03-18 stsp if (repo)
1416 bb64b798 2020-03-18 stsp got_repo_close(repo);
1417 659e7fbd 2020-03-20 stsp if (head_symref)
1418 659e7fbd 2020-03-20 stsp got_ref_close(head_symref);
1419 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1420 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1421 d9b4d0c0 2020-03-18 stsp free(pe->data);
1422 d9b4d0c0 2020-03-18 stsp }
1423 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1424 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1425 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1426 d9b4d0c0 2020-03-18 stsp free(pe->data);
1427 d9b4d0c0 2020-03-18 stsp }
1428 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1429 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
1430 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
1431 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1432 09838ffc 2020-03-18 stsp free(proto);
1433 09838ffc 2020-03-18 stsp free(host);
1434 09838ffc 2020-03-18 stsp free(port);
1435 09838ffc 2020-03-18 stsp free(server_path);
1436 09838ffc 2020-03-18 stsp free(repo_name);
1437 bb64b798 2020-03-18 stsp free(default_destdir);
1438 b46f3e71 2020-03-18 stsp free(gitconfig_path);
1439 b46f3e71 2020-03-18 stsp free(git_url);
1440 39c64a6a 2020-03-18 stsp return error;
1441 7848a0e1 2020-03-19 stsp }
1442 7848a0e1 2020-03-19 stsp
1443 7848a0e1 2020-03-19 stsp static const struct got_error *
1444 7848a0e1 2020-03-19 stsp update_ref(struct got_reference *ref, struct got_object_id *new_id,
1445 db6d8ad8 2020-03-21 stsp int replace_tags, int verbosity, struct got_repository *repo)
1446 7848a0e1 2020-03-19 stsp {
1447 7848a0e1 2020-03-19 stsp const struct got_error *err = NULL;
1448 7848a0e1 2020-03-19 stsp char *new_id_str = NULL;
1449 7848a0e1 2020-03-19 stsp struct got_object_id *old_id = NULL;
1450 7848a0e1 2020-03-19 stsp
1451 7848a0e1 2020-03-19 stsp err = got_object_id_str(&new_id_str, new_id);
1452 7848a0e1 2020-03-19 stsp if (err)
1453 7848a0e1 2020-03-19 stsp goto done;
1454 7848a0e1 2020-03-19 stsp
1455 db6d8ad8 2020-03-21 stsp if (!replace_tags &&
1456 db6d8ad8 2020-03-21 stsp strncmp(got_ref_get_name(ref), "refs/tags/", 10) == 0) {
1457 88609724 2020-03-21 stsp err = got_ref_resolve(&old_id, repo, ref);
1458 88609724 2020-03-21 stsp if (err)
1459 88609724 2020-03-21 stsp goto done;
1460 88609724 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1461 88609724 2020-03-21 stsp goto done;
1462 db6d8ad8 2020-03-21 stsp if (verbosity >= 0) {
1463 db6d8ad8 2020-03-21 stsp printf("Rejecting update of existing tag %s: %s\n",
1464 db6d8ad8 2020-03-21 stsp got_ref_get_name(ref), new_id_str);
1465 db6d8ad8 2020-03-21 stsp }
1466 db6d8ad8 2020-03-21 stsp goto done;
1467 db6d8ad8 2020-03-21 stsp }
1468 db6d8ad8 2020-03-21 stsp
1469 7848a0e1 2020-03-19 stsp if (got_ref_is_symbolic(ref)) {
1470 688f11b3 2020-03-21 stsp if (verbosity >= 0) {
1471 e8a967e0 2020-03-21 stsp printf("Replacing reference %s: %s\n",
1472 6338a6a1 2020-03-21 stsp got_ref_get_name(ref),
1473 6338a6a1 2020-03-21 stsp got_ref_get_symref_target(ref));
1474 688f11b3 2020-03-21 stsp }
1475 e8a967e0 2020-03-21 stsp err = got_ref_change_symref_to_ref(ref, new_id);
1476 7848a0e1 2020-03-19 stsp if (err)
1477 7848a0e1 2020-03-19 stsp goto done;
1478 e8a967e0 2020-03-21 stsp err = got_ref_write(ref, repo);
1479 e8a967e0 2020-03-21 stsp if (err)
1480 e8a967e0 2020-03-21 stsp goto done;
1481 7848a0e1 2020-03-19 stsp } else {
1482 7848a0e1 2020-03-19 stsp err = got_ref_resolve(&old_id, repo, ref);
1483 7848a0e1 2020-03-19 stsp if (err)
1484 7848a0e1 2020-03-19 stsp goto done;
1485 6338a6a1 2020-03-21 stsp if (got_object_id_cmp(old_id, new_id) == 0)
1486 6338a6a1 2020-03-21 stsp goto done;
1487 6338a6a1 2020-03-21 stsp
1488 6338a6a1 2020-03-21 stsp err = got_ref_change_ref(ref, new_id);
1489 6338a6a1 2020-03-21 stsp if (err)
1490 6338a6a1 2020-03-21 stsp goto done;
1491 6338a6a1 2020-03-21 stsp err = got_ref_write(ref, repo);
1492 6338a6a1 2020-03-21 stsp if (err)
1493 6338a6a1 2020-03-21 stsp goto done;
1494 7848a0e1 2020-03-19 stsp }
1495 6338a6a1 2020-03-21 stsp
1496 6338a6a1 2020-03-21 stsp if (verbosity >= 0)
1497 6338a6a1 2020-03-21 stsp printf("Updated reference %s: %s\n", got_ref_get_name(ref),
1498 6338a6a1 2020-03-21 stsp new_id_str);
1499 7848a0e1 2020-03-19 stsp done:
1500 7848a0e1 2020-03-19 stsp free(old_id);
1501 7848a0e1 2020-03-19 stsp free(new_id_str);
1502 7848a0e1 2020-03-19 stsp return err;
1503 2ab43947 2020-03-18 stsp }
1504 f1bcca34 2020-03-25 stsp
1505 f1bcca34 2020-03-25 stsp static const struct got_error *
1506 f1bcca34 2020-03-25 stsp update_symref(const char *refname, struct got_reference *target_ref,
1507 f1bcca34 2020-03-25 stsp int verbosity, struct got_repository *repo)
1508 f1bcca34 2020-03-25 stsp {
1509 f1bcca34 2020-03-25 stsp const struct got_error *err = NULL, *unlock_err;
1510 f1bcca34 2020-03-25 stsp struct got_reference *symref;
1511 f1bcca34 2020-03-25 stsp
1512 f1bcca34 2020-03-25 stsp err = got_ref_open(&symref, repo, refname, 1);
1513 f1bcca34 2020-03-25 stsp if (err)
1514 f1bcca34 2020-03-25 stsp return err;
1515 f1bcca34 2020-03-25 stsp
1516 f1bcca34 2020-03-25 stsp if (strcmp(got_ref_get_symref_target(symref),
1517 f1bcca34 2020-03-25 stsp got_ref_get_name(target_ref)) == 0)
1518 f1bcca34 2020-03-25 stsp goto done;
1519 2ab43947 2020-03-18 stsp
1520 f1bcca34 2020-03-25 stsp err = got_ref_change_symref(symref, got_ref_get_name(target_ref));
1521 f1bcca34 2020-03-25 stsp if (err)
1522 f1bcca34 2020-03-25 stsp goto done;
1523 f1bcca34 2020-03-25 stsp
1524 f1bcca34 2020-03-25 stsp err = got_ref_write(symref, repo);
1525 f1bcca34 2020-03-25 stsp if (err)
1526 f1bcca34 2020-03-25 stsp goto done;
1527 f1bcca34 2020-03-25 stsp
1528 f1bcca34 2020-03-25 stsp if (verbosity >= 0)
1529 f1bcca34 2020-03-25 stsp printf("Updated reference %s: %s\n", got_ref_get_name(symref),
1530 f1bcca34 2020-03-25 stsp got_ref_get_symref_target(symref));
1531 f1bcca34 2020-03-25 stsp done:
1532 f1bcca34 2020-03-25 stsp unlock_err = got_ref_unlock(symref);
1533 f1bcca34 2020-03-25 stsp if (unlock_err && err == NULL)
1534 f1bcca34 2020-03-25 stsp err = unlock_err;
1535 f1bcca34 2020-03-25 stsp got_ref_close(symref);
1536 f1bcca34 2020-03-25 stsp return err;
1537 f1bcca34 2020-03-25 stsp return NULL;
1538 f1bcca34 2020-03-25 stsp }
1539 f1bcca34 2020-03-25 stsp
1540 2ab43947 2020-03-18 stsp __dead static void
1541 7848a0e1 2020-03-19 stsp usage_fetch(void)
1542 7848a0e1 2020-03-19 stsp {
1543 f21ec2f0 2020-03-21 stsp fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
1544 0e4002ca 2020-03-21 stsp "[-r repository-path] [-t] [-q] [-v] [-R reference] "
1545 0e4002ca 2020-03-21 stsp "[remote-repository-name]\n",
1546 13f12b09 2020-03-21 stsp getprogname());
1547 7848a0e1 2020-03-19 stsp exit(1);
1548 7848a0e1 2020-03-19 stsp }
1549 7848a0e1 2020-03-19 stsp
1550 7848a0e1 2020-03-19 stsp static const struct got_error *
1551 f21ec2f0 2020-03-21 stsp delete_missing_refs(struct got_pathlist_head *their_refs,
1552 688f11b3 2020-03-21 stsp int verbosity, struct got_repository *repo)
1553 f21ec2f0 2020-03-21 stsp {
1554 f21ec2f0 2020-03-21 stsp const struct got_error *err = NULL;
1555 f21ec2f0 2020-03-21 stsp struct got_reflist_head my_refs;
1556 f21ec2f0 2020-03-21 stsp struct got_reflist_entry *re;
1557 f21ec2f0 2020-03-21 stsp struct got_pathlist_entry *pe;
1558 f21ec2f0 2020-03-21 stsp struct got_object_id *id;
1559 f21ec2f0 2020-03-21 stsp char *id_str;
1560 f21ec2f0 2020-03-21 stsp
1561 f21ec2f0 2020-03-21 stsp SIMPLEQ_INIT(&my_refs);
1562 f21ec2f0 2020-03-21 stsp
1563 f21ec2f0 2020-03-21 stsp err = got_ref_list(&my_refs, repo, NULL, got_ref_cmp_by_name, NULL);
1564 f21ec2f0 2020-03-21 stsp if (err)
1565 f21ec2f0 2020-03-21 stsp return err;
1566 f21ec2f0 2020-03-21 stsp
1567 f21ec2f0 2020-03-21 stsp SIMPLEQ_FOREACH(re, &my_refs, entry) {
1568 f21ec2f0 2020-03-21 stsp const char *refname = got_ref_get_name(re->ref);
1569 f21ec2f0 2020-03-21 stsp
1570 f21ec2f0 2020-03-21 stsp if (strncmp(refname, "refs/heads/", 11) != 0 &&
1571 f21ec2f0 2020-03-21 stsp strncmp(refname, "refs/tags/", 10) != 0)
1572 f21ec2f0 2020-03-21 stsp continue;
1573 f21ec2f0 2020-03-21 stsp
1574 f21ec2f0 2020-03-21 stsp TAILQ_FOREACH(pe, their_refs, entry) {
1575 f21ec2f0 2020-03-21 stsp if (strcmp(refname, pe->path) == 0)
1576 f21ec2f0 2020-03-21 stsp break;
1577 f21ec2f0 2020-03-21 stsp }
1578 f21ec2f0 2020-03-21 stsp if (pe != NULL)
1579 f21ec2f0 2020-03-21 stsp continue;
1580 f21ec2f0 2020-03-21 stsp
1581 f21ec2f0 2020-03-21 stsp err = got_ref_resolve(&id, repo, re->ref);
1582 f21ec2f0 2020-03-21 stsp if (err)
1583 f21ec2f0 2020-03-21 stsp break;
1584 f21ec2f0 2020-03-21 stsp err = got_object_id_str(&id_str, id);
1585 f21ec2f0 2020-03-21 stsp free(id);
1586 f21ec2f0 2020-03-21 stsp if (err)
1587 f21ec2f0 2020-03-21 stsp break;
1588 f21ec2f0 2020-03-21 stsp
1589 f21ec2f0 2020-03-21 stsp free(id_str);
1590 f21ec2f0 2020-03-21 stsp err = got_ref_delete(re->ref, repo);
1591 f21ec2f0 2020-03-21 stsp if (err)
1592 f21ec2f0 2020-03-21 stsp break;
1593 6338a6a1 2020-03-21 stsp if (verbosity >= 0) {
1594 6338a6a1 2020-03-21 stsp printf("Deleted reference %s: %s\n",
1595 6338a6a1 2020-03-21 stsp got_ref_get_name(re->ref), id_str);
1596 6338a6a1 2020-03-21 stsp }
1597 f21ec2f0 2020-03-21 stsp }
1598 0e4002ca 2020-03-21 stsp
1599 0e4002ca 2020-03-21 stsp return err;
1600 0e4002ca 2020-03-21 stsp }
1601 0e4002ca 2020-03-21 stsp
1602 0e4002ca 2020-03-21 stsp static const struct got_error *
1603 0e4002ca 2020-03-21 stsp update_wanted_ref(const char *refname, struct got_object_id *id,
1604 0e4002ca 2020-03-21 stsp const char *remote_repo_name, int verbosity, struct got_repository *repo)
1605 0e4002ca 2020-03-21 stsp {
1606 9f142382 2020-03-21 stsp const struct got_error *err, *unlock_err;
1607 0e4002ca 2020-03-21 stsp char *remote_refname;
1608 0e4002ca 2020-03-21 stsp struct got_reference *ref;
1609 0e4002ca 2020-03-21 stsp
1610 0e4002ca 2020-03-21 stsp if (strncmp("refs/", refname, 5) == 0)
1611 0e4002ca 2020-03-21 stsp refname += 5;
1612 0e4002ca 2020-03-21 stsp
1613 0e4002ca 2020-03-21 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1614 0e4002ca 2020-03-21 stsp remote_repo_name, refname) == -1)
1615 0e4002ca 2020-03-21 stsp return got_error_from_errno("asprintf");
1616 f21ec2f0 2020-03-21 stsp
1617 9f142382 2020-03-21 stsp err = got_ref_open(&ref, repo, remote_refname, 1);
1618 0e4002ca 2020-03-21 stsp if (err) {
1619 0e4002ca 2020-03-21 stsp if (err->code != GOT_ERR_NOT_REF)
1620 0e4002ca 2020-03-21 stsp goto done;
1621 0e4002ca 2020-03-21 stsp err = create_ref(remote_refname, id, verbosity, repo);
1622 0e4002ca 2020-03-21 stsp } else {
1623 0e4002ca 2020-03-21 stsp err = update_ref(ref, id, 0, verbosity, repo);
1624 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1625 9f142382 2020-03-21 stsp if (unlock_err && err == NULL)
1626 9f142382 2020-03-21 stsp err = unlock_err;
1627 0e4002ca 2020-03-21 stsp got_ref_close(ref);
1628 0e4002ca 2020-03-21 stsp }
1629 0e4002ca 2020-03-21 stsp done:
1630 0e4002ca 2020-03-21 stsp free(remote_refname);
1631 f21ec2f0 2020-03-21 stsp return err;
1632 f21ec2f0 2020-03-21 stsp }
1633 f21ec2f0 2020-03-21 stsp
1634 f21ec2f0 2020-03-21 stsp static const struct got_error *
1635 7848a0e1 2020-03-19 stsp cmd_fetch(int argc, char *argv[])
1636 7848a0e1 2020-03-19 stsp {
1637 9f142382 2020-03-21 stsp const struct got_error *error = NULL, *unlock_err;
1638 7848a0e1 2020-03-19 stsp char *cwd = NULL, *repo_path = NULL;
1639 7848a0e1 2020-03-19 stsp const char *remote_name;
1640 7848a0e1 2020-03-19 stsp char *proto = NULL, *host = NULL, *port = NULL;
1641 7848a0e1 2020-03-19 stsp char *repo_name = NULL, *server_path = NULL;
1642 7848a0e1 2020-03-19 stsp struct got_remote_repo *remotes, *remote = NULL;
1643 7848a0e1 2020-03-19 stsp int nremotes;
1644 7848a0e1 2020-03-19 stsp char *id_str = NULL;
1645 7848a0e1 2020-03-19 stsp struct got_repository *repo = NULL;
1646 7848a0e1 2020-03-19 stsp struct got_worktree *worktree = NULL;
1647 0e4002ca 2020-03-21 stsp struct got_pathlist_head refs, symrefs, wanted_branches, wanted_refs;
1648 7848a0e1 2020-03-19 stsp struct got_pathlist_entry *pe;
1649 7848a0e1 2020-03-19 stsp struct got_object_id *pack_hash = NULL;
1650 9c52365f 2020-03-21 stsp int i, ch, fetchfd = -1, fetchstatus;
1651 9c52365f 2020-03-21 stsp pid_t fetchpid = -1;
1652 7848a0e1 2020-03-19 stsp struct got_fetch_progress_arg fpa;
1653 41b0de12 2020-03-21 stsp int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
1654 db6d8ad8 2020-03-21 stsp int delete_refs = 0, replace_tags = 0;
1655 7848a0e1 2020-03-19 stsp
1656 7848a0e1 2020-03-19 stsp TAILQ_INIT(&refs);
1657 7848a0e1 2020-03-19 stsp TAILQ_INIT(&symrefs);
1658 4ba14133 2020-03-20 stsp TAILQ_INIT(&wanted_branches);
1659 0e4002ca 2020-03-21 stsp TAILQ_INIT(&wanted_refs);
1660 7848a0e1 2020-03-19 stsp
1661 0e4002ca 2020-03-21 stsp while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
1662 7848a0e1 2020-03-19 stsp switch (ch) {
1663 659e7fbd 2020-03-20 stsp case 'a':
1664 659e7fbd 2020-03-20 stsp fetch_all_branches = 1;
1665 4ba14133 2020-03-20 stsp break;
1666 4ba14133 2020-03-20 stsp case 'b':
1667 4ba14133 2020-03-20 stsp error = got_pathlist_append(&wanted_branches,
1668 4ba14133 2020-03-20 stsp optarg, NULL);
1669 4ba14133 2020-03-20 stsp if (error)
1670 4ba14133 2020-03-20 stsp return error;
1671 41b0de12 2020-03-21 stsp break;
1672 f21ec2f0 2020-03-21 stsp case 'd':
1673 f21ec2f0 2020-03-21 stsp delete_refs = 1;
1674 f21ec2f0 2020-03-21 stsp break;
1675 41b0de12 2020-03-21 stsp case 'l':
1676 41b0de12 2020-03-21 stsp list_refs_only = 1;
1677 659e7fbd 2020-03-20 stsp break;
1678 7848a0e1 2020-03-19 stsp case 'r':
1679 7848a0e1 2020-03-19 stsp repo_path = realpath(optarg, NULL);
1680 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1681 7848a0e1 2020-03-19 stsp return got_error_from_errno2("realpath",
1682 7848a0e1 2020-03-19 stsp optarg);
1683 7848a0e1 2020-03-19 stsp got_path_strip_trailing_slashes(repo_path);
1684 7848a0e1 2020-03-19 stsp break;
1685 db6d8ad8 2020-03-21 stsp case 't':
1686 db6d8ad8 2020-03-21 stsp replace_tags = 1;
1687 db6d8ad8 2020-03-21 stsp break;
1688 7848a0e1 2020-03-19 stsp case 'v':
1689 7848a0e1 2020-03-19 stsp if (verbosity < 0)
1690 7848a0e1 2020-03-19 stsp verbosity = 0;
1691 7848a0e1 2020-03-19 stsp else if (verbosity < 3)
1692 7848a0e1 2020-03-19 stsp verbosity++;
1693 7848a0e1 2020-03-19 stsp break;
1694 7848a0e1 2020-03-19 stsp case 'q':
1695 7848a0e1 2020-03-19 stsp verbosity = -1;
1696 7848a0e1 2020-03-19 stsp break;
1697 0e4002ca 2020-03-21 stsp case 'R':
1698 0e4002ca 2020-03-21 stsp error = got_pathlist_append(&wanted_refs,
1699 0e4002ca 2020-03-21 stsp optarg, NULL);
1700 0e4002ca 2020-03-21 stsp if (error)
1701 0e4002ca 2020-03-21 stsp return error;
1702 0e4002ca 2020-03-21 stsp break;
1703 7848a0e1 2020-03-19 stsp default:
1704 7848a0e1 2020-03-19 stsp usage_fetch();
1705 7848a0e1 2020-03-19 stsp break;
1706 7848a0e1 2020-03-19 stsp }
1707 7848a0e1 2020-03-19 stsp }
1708 7848a0e1 2020-03-19 stsp argc -= optind;
1709 7848a0e1 2020-03-19 stsp argv += optind;
1710 7848a0e1 2020-03-19 stsp
1711 4ba14133 2020-03-20 stsp if (fetch_all_branches && !TAILQ_EMPTY(&wanted_branches))
1712 41b0de12 2020-03-21 stsp errx(1, "-a and -b options are mutually exclusive");
1713 41b0de12 2020-03-21 stsp if (list_refs_only) {
1714 41b0de12 2020-03-21 stsp if (!TAILQ_EMPTY(&wanted_branches))
1715 41b0de12 2020-03-21 stsp errx(1, "-l and -b options are mutually exclusive");
1716 41b0de12 2020-03-21 stsp if (fetch_all_branches)
1717 41b0de12 2020-03-21 stsp errx(1, "-l and -a options are mutually exclusive");
1718 f21ec2f0 2020-03-21 stsp if (delete_refs)
1719 f21ec2f0 2020-03-21 stsp errx(1, "-l and -d options are mutually exclusive");
1720 41b0de12 2020-03-21 stsp if (verbosity == -1)
1721 41b0de12 2020-03-21 stsp errx(1, "-l and -q options are mutually exclusive");
1722 41b0de12 2020-03-21 stsp }
1723 4ba14133 2020-03-20 stsp
1724 7848a0e1 2020-03-19 stsp if (argc == 0)
1725 7848a0e1 2020-03-19 stsp remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
1726 7848a0e1 2020-03-19 stsp else if (argc == 1)
1727 7848a0e1 2020-03-19 stsp remote_name = argv[0];
1728 7848a0e1 2020-03-19 stsp else
1729 7848a0e1 2020-03-19 stsp usage_fetch();
1730 7848a0e1 2020-03-19 stsp
1731 7848a0e1 2020-03-19 stsp cwd = getcwd(NULL, 0);
1732 7848a0e1 2020-03-19 stsp if (cwd == NULL) {
1733 7848a0e1 2020-03-19 stsp error = got_error_from_errno("getcwd");
1734 7848a0e1 2020-03-19 stsp goto done;
1735 7848a0e1 2020-03-19 stsp }
1736 7848a0e1 2020-03-19 stsp
1737 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1738 7848a0e1 2020-03-19 stsp error = got_worktree_open(&worktree, cwd);
1739 7848a0e1 2020-03-19 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1740 7848a0e1 2020-03-19 stsp goto done;
1741 7848a0e1 2020-03-19 stsp else
1742 7848a0e1 2020-03-19 stsp error = NULL;
1743 7848a0e1 2020-03-19 stsp if (worktree) {
1744 7848a0e1 2020-03-19 stsp repo_path =
1745 7848a0e1 2020-03-19 stsp strdup(got_worktree_get_repo_path(worktree));
1746 7848a0e1 2020-03-19 stsp if (repo_path == NULL)
1747 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1748 7848a0e1 2020-03-19 stsp if (error)
1749 7848a0e1 2020-03-19 stsp goto done;
1750 7848a0e1 2020-03-19 stsp } else {
1751 7848a0e1 2020-03-19 stsp repo_path = strdup(cwd);
1752 7848a0e1 2020-03-19 stsp if (repo_path == NULL) {
1753 7848a0e1 2020-03-19 stsp error = got_error_from_errno("strdup");
1754 7848a0e1 2020-03-19 stsp goto done;
1755 7848a0e1 2020-03-19 stsp }
1756 7848a0e1 2020-03-19 stsp }
1757 7848a0e1 2020-03-19 stsp }
1758 7848a0e1 2020-03-19 stsp
1759 7848a0e1 2020-03-19 stsp error = got_repo_open(&repo, repo_path, NULL);
1760 7848a0e1 2020-03-19 stsp if (error)
1761 7848a0e1 2020-03-19 stsp goto done;
1762 7848a0e1 2020-03-19 stsp
1763 7848a0e1 2020-03-19 stsp got_repo_get_gitconfig_remotes(&nremotes, &remotes, repo);
1764 7848a0e1 2020-03-19 stsp for (i = 0; i < nremotes; i++) {
1765 7848a0e1 2020-03-19 stsp remote = &remotes[i];
1766 7848a0e1 2020-03-19 stsp if (strcmp(remote->name, remote_name) == 0)
1767 7848a0e1 2020-03-19 stsp break;
1768 7848a0e1 2020-03-19 stsp }
1769 7848a0e1 2020-03-19 stsp if (i == nremotes) {
1770 7848a0e1 2020-03-19 stsp error = got_error_path(remote_name, GOT_ERR_NO_REMOTE);
1771 7848a0e1 2020-03-19 stsp goto done;
1772 7848a0e1 2020-03-19 stsp }
1773 7848a0e1 2020-03-19 stsp
1774 7848a0e1 2020-03-19 stsp error = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1775 7848a0e1 2020-03-19 stsp &repo_name, remote->url);
1776 7848a0e1 2020-03-19 stsp if (error)
1777 7848a0e1 2020-03-19 stsp goto done;
1778 7848a0e1 2020-03-19 stsp
1779 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git") == 0) {
1780 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1781 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1782 7848a0e1 2020-03-19 stsp "sendfd dns inet unveil", NULL) == -1)
1783 7848a0e1 2020-03-19 stsp err(1, "pledge");
1784 7848a0e1 2020-03-19 stsp #endif
1785 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "git+ssh") == 0 ||
1786 7848a0e1 2020-03-19 stsp strcmp(proto, "ssh") == 0) {
1787 7848a0e1 2020-03-19 stsp #ifndef PROFILE
1788 7848a0e1 2020-03-19 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
1789 7848a0e1 2020-03-19 stsp "sendfd unveil", NULL) == -1)
1790 7848a0e1 2020-03-19 stsp err(1, "pledge");
1791 7848a0e1 2020-03-19 stsp #endif
1792 7848a0e1 2020-03-19 stsp } else if (strcmp(proto, "http") == 0 ||
1793 7848a0e1 2020-03-19 stsp strcmp(proto, "git+http") == 0) {
1794 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_NOT_IMPL);
1795 7848a0e1 2020-03-19 stsp goto done;
1796 7848a0e1 2020-03-19 stsp } else {
1797 7848a0e1 2020-03-19 stsp error = got_error_path(proto, GOT_ERR_BAD_PROTO);
1798 7848a0e1 2020-03-19 stsp goto done;
1799 7848a0e1 2020-03-19 stsp }
1800 7848a0e1 2020-03-19 stsp
1801 7848a0e1 2020-03-19 stsp if (strcmp(proto, "git+ssh") == 0 || strcmp(proto, "ssh") == 0) {
1802 7848a0e1 2020-03-19 stsp if (unveil(GOT_FETCH_PATH_SSH, "x") != 0) {
1803 7848a0e1 2020-03-19 stsp error = got_error_from_errno2("unveil",
1804 7848a0e1 2020-03-19 stsp GOT_FETCH_PATH_SSH);
1805 7848a0e1 2020-03-19 stsp goto done;
1806 7848a0e1 2020-03-19 stsp }
1807 7848a0e1 2020-03-19 stsp }
1808 7848a0e1 2020-03-19 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
1809 7848a0e1 2020-03-19 stsp if (error)
1810 7848a0e1 2020-03-19 stsp goto done;
1811 7848a0e1 2020-03-19 stsp
1812 9c52365f 2020-03-21 stsp error = got_fetch_connect(&fetchpid, &fetchfd, proto, host, port,
1813 9c52365f 2020-03-21 stsp server_path, verbosity);
1814 7848a0e1 2020-03-19 stsp if (error)
1815 7848a0e1 2020-03-19 stsp goto done;
1816 7848a0e1 2020-03-19 stsp
1817 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1818 62a4c94c 2020-03-20 stsp printf("Connected to \"%s\" %s%s%s\n", remote->name, host,
1819 62a4c94c 2020-03-20 stsp port ? ":" : "", port ? port : "");
1820 7848a0e1 2020-03-19 stsp
1821 7848a0e1 2020-03-19 stsp fpa.last_scaled_size[0] = '\0';
1822 7848a0e1 2020-03-19 stsp fpa.last_p_indexed = -1;
1823 7848a0e1 2020-03-19 stsp fpa.last_p_resolved = -1;
1824 7848a0e1 2020-03-19 stsp fpa.verbosity = verbosity;
1825 7848a0e1 2020-03-19 stsp error = got_fetch_pack(&pack_hash, &refs, &symrefs, remote->name,
1826 4ba14133 2020-03-20 stsp remote->mirror_references, fetch_all_branches, &wanted_branches,
1827 0e4002ca 2020-03-21 stsp &wanted_refs, list_refs_only, verbosity, fetchfd, repo,
1828 0e4002ca 2020-03-21 stsp fetch_progress, &fpa);
1829 7848a0e1 2020-03-19 stsp if (error)
1830 7848a0e1 2020-03-19 stsp goto done;
1831 7848a0e1 2020-03-19 stsp
1832 41b0de12 2020-03-21 stsp if (list_refs_only) {
1833 41b0de12 2020-03-21 stsp error = list_remote_refs(&symrefs, &refs);
1834 41b0de12 2020-03-21 stsp goto done;
1835 41b0de12 2020-03-21 stsp }
1836 41b0de12 2020-03-21 stsp
1837 7848a0e1 2020-03-19 stsp if (pack_hash == NULL) {
1838 7848a0e1 2020-03-19 stsp if (verbosity >= 0)
1839 7848a0e1 2020-03-19 stsp printf("Already up-to-date\n");
1840 f21ec2f0 2020-03-21 stsp if (delete_refs)
1841 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1842 7848a0e1 2020-03-19 stsp goto done;
1843 7848a0e1 2020-03-19 stsp }
1844 7848a0e1 2020-03-19 stsp
1845 984065c8 2020-03-19 stsp if (verbosity >= 0) {
1846 984065c8 2020-03-19 stsp error = got_object_id_str(&id_str, pack_hash);
1847 984065c8 2020-03-19 stsp if (error)
1848 984065c8 2020-03-19 stsp goto done;
1849 e69674d8 2020-03-19 stsp printf("\nFetched %s.pack\n", id_str);
1850 984065c8 2020-03-19 stsp free(id_str);
1851 984065c8 2020-03-19 stsp id_str = NULL;
1852 984065c8 2020-03-19 stsp }
1853 7848a0e1 2020-03-19 stsp
1854 7848a0e1 2020-03-19 stsp /* Update references provided with the pack file. */
1855 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1856 7848a0e1 2020-03-19 stsp const char *refname = pe->path;
1857 7848a0e1 2020-03-19 stsp struct got_object_id *id = pe->data;
1858 7848a0e1 2020-03-19 stsp struct got_reference *ref;
1859 7848a0e1 2020-03-19 stsp char *remote_refname;
1860 7848a0e1 2020-03-19 stsp
1861 0e4002ca 2020-03-21 stsp if (is_wanted_ref(&wanted_refs, refname) &&
1862 0e4002ca 2020-03-21 stsp !remote->mirror_references) {
1863 0e4002ca 2020-03-21 stsp error = update_wanted_ref(refname, id,
1864 0e4002ca 2020-03-21 stsp remote->name, verbosity, repo);
1865 0e4002ca 2020-03-21 stsp if (error)
1866 0e4002ca 2020-03-21 stsp goto done;
1867 0e4002ca 2020-03-21 stsp continue;
1868 0e4002ca 2020-03-21 stsp }
1869 0e4002ca 2020-03-21 stsp
1870 1510c839 2020-03-20 stsp if (remote->mirror_references ||
1871 1510c839 2020-03-20 stsp strncmp("refs/tags/", refname, 10) == 0) {
1872 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1873 7848a0e1 2020-03-19 stsp if (error) {
1874 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1875 7848a0e1 2020-03-19 stsp goto done;
1876 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1877 6338a6a1 2020-03-21 stsp repo);
1878 7848a0e1 2020-03-19 stsp if (error)
1879 7848a0e1 2020-03-19 stsp goto done;
1880 7848a0e1 2020-03-19 stsp } else {
1881 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1882 db6d8ad8 2020-03-21 stsp verbosity, repo);
1883 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1884 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1885 9f142382 2020-03-21 stsp error = unlock_err;
1886 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1887 7848a0e1 2020-03-19 stsp if (error)
1888 7848a0e1 2020-03-19 stsp goto done;
1889 7848a0e1 2020-03-19 stsp }
1890 7848a0e1 2020-03-19 stsp } else if (strncmp("refs/heads/", refname, 11) == 0) {
1891 7848a0e1 2020-03-19 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1892 7848a0e1 2020-03-19 stsp remote_name, refname + 11) == -1) {
1893 7848a0e1 2020-03-19 stsp error = got_error_from_errno("asprintf");
1894 7848a0e1 2020-03-19 stsp goto done;
1895 7848a0e1 2020-03-19 stsp }
1896 7848a0e1 2020-03-19 stsp
1897 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, remote_refname, 1);
1898 7848a0e1 2020-03-19 stsp if (error) {
1899 7848a0e1 2020-03-19 stsp if (error->code != GOT_ERR_NOT_REF)
1900 7848a0e1 2020-03-19 stsp goto done;
1901 6338a6a1 2020-03-21 stsp error = create_ref(remote_refname, id,
1902 688f11b3 2020-03-21 stsp verbosity, repo);
1903 7848a0e1 2020-03-19 stsp if (error)
1904 7848a0e1 2020-03-19 stsp goto done;
1905 7848a0e1 2020-03-19 stsp } else {
1906 db6d8ad8 2020-03-21 stsp error = update_ref(ref, id, replace_tags,
1907 db6d8ad8 2020-03-21 stsp verbosity, repo);
1908 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1909 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1910 9f142382 2020-03-21 stsp error = unlock_err;
1911 7848a0e1 2020-03-19 stsp got_ref_close(ref);
1912 7848a0e1 2020-03-19 stsp if (error)
1913 7848a0e1 2020-03-19 stsp goto done;
1914 7848a0e1 2020-03-19 stsp }
1915 2ec30c80 2020-03-20 stsp
1916 2ec30c80 2020-03-20 stsp /* Also create a local branch if none exists yet. */
1917 9f142382 2020-03-21 stsp error = got_ref_open(&ref, repo, refname, 1);
1918 2ec30c80 2020-03-20 stsp if (error) {
1919 2ec30c80 2020-03-20 stsp if (error->code != GOT_ERR_NOT_REF)
1920 2ec30c80 2020-03-20 stsp goto done;
1921 6338a6a1 2020-03-21 stsp error = create_ref(refname, id, verbosity,
1922 6338a6a1 2020-03-21 stsp repo);
1923 2ec30c80 2020-03-20 stsp if (error)
1924 2ec30c80 2020-03-20 stsp goto done;
1925 9f142382 2020-03-21 stsp } else {
1926 9f142382 2020-03-21 stsp unlock_err = got_ref_unlock(ref);
1927 9f142382 2020-03-21 stsp if (unlock_err && error == NULL)
1928 9f142382 2020-03-21 stsp error = unlock_err;
1929 2ec30c80 2020-03-20 stsp got_ref_close(ref);
1930 9f142382 2020-03-21 stsp }
1931 7848a0e1 2020-03-19 stsp }
1932 7848a0e1 2020-03-19 stsp }
1933 f1bcca34 2020-03-25 stsp if (delete_refs) {
1934 688f11b3 2020-03-21 stsp error = delete_missing_refs(&refs, verbosity, repo);
1935 f1bcca34 2020-03-25 stsp if (error)
1936 f1bcca34 2020-03-25 stsp goto done;
1937 f1bcca34 2020-03-25 stsp }
1938 f1bcca34 2020-03-25 stsp
1939 f1bcca34 2020-03-25 stsp if (!remote->mirror_references) {
1940 f1bcca34 2020-03-25 stsp /* Update remote HEAD reference if the server provided one. */
1941 f1bcca34 2020-03-25 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1942 f1bcca34 2020-03-25 stsp struct got_reference *target_ref;
1943 f1bcca34 2020-03-25 stsp const char *refname = pe->path;
1944 f1bcca34 2020-03-25 stsp const char *target = pe->data;
1945 f1bcca34 2020-03-25 stsp char *remote_refname = NULL, *remote_target = NULL;
1946 f1bcca34 2020-03-25 stsp
1947 f1bcca34 2020-03-25 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1948 f1bcca34 2020-03-25 stsp continue;
1949 f1bcca34 2020-03-25 stsp
1950 f1bcca34 2020-03-25 stsp if (strncmp("refs/heads/", target, 11) != 0)
1951 f1bcca34 2020-03-25 stsp continue;
1952 f1bcca34 2020-03-25 stsp
1953 f1bcca34 2020-03-25 stsp if (asprintf(&remote_refname, "refs/remotes/%s/%s",
1954 f1bcca34 2020-03-25 stsp remote->name, refname) == -1) {
1955 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
1956 f1bcca34 2020-03-25 stsp goto done;
1957 f1bcca34 2020-03-25 stsp }
1958 f1bcca34 2020-03-25 stsp if (asprintf(&remote_target, "refs/remotes/%s/%s",
1959 f1bcca34 2020-03-25 stsp remote->name, target + 11) == -1) {
1960 f1bcca34 2020-03-25 stsp error = got_error_from_errno("asprintf");
1961 f1bcca34 2020-03-25 stsp free(remote_refname);
1962 f1bcca34 2020-03-25 stsp goto done;
1963 f1bcca34 2020-03-25 stsp }
1964 f1bcca34 2020-03-25 stsp
1965 f1bcca34 2020-03-25 stsp error = got_ref_open(&target_ref, repo, remote_target,
1966 f1bcca34 2020-03-25 stsp 0);
1967 f1bcca34 2020-03-25 stsp if (error) {
1968 f1bcca34 2020-03-25 stsp free(remote_refname);
1969 f1bcca34 2020-03-25 stsp free(remote_target);
1970 f1bcca34 2020-03-25 stsp if (error->code == GOT_ERR_NOT_REF) {
1971 f1bcca34 2020-03-25 stsp error = NULL;
1972 f1bcca34 2020-03-25 stsp continue;
1973 f1bcca34 2020-03-25 stsp }
1974 f1bcca34 2020-03-25 stsp goto done;
1975 f1bcca34 2020-03-25 stsp }
1976 f1bcca34 2020-03-25 stsp error = update_symref(remote_refname, target_ref,
1977 f1bcca34 2020-03-25 stsp verbosity, repo);
1978 f1bcca34 2020-03-25 stsp free(remote_refname);
1979 f1bcca34 2020-03-25 stsp free(remote_target);
1980 f1bcca34 2020-03-25 stsp got_ref_close(target_ref);
1981 f1bcca34 2020-03-25 stsp if (error)
1982 f1bcca34 2020-03-25 stsp goto done;
1983 f1bcca34 2020-03-25 stsp }
1984 f1bcca34 2020-03-25 stsp }
1985 7848a0e1 2020-03-19 stsp done:
1986 9c52365f 2020-03-21 stsp if (fetchpid > 0) {
1987 9c52365f 2020-03-21 stsp if (kill(fetchpid, SIGTERM) == -1)
1988 9c52365f 2020-03-21 stsp error = got_error_from_errno("kill");
1989 9c52365f 2020-03-21 stsp if (waitpid(fetchpid, &fetchstatus, 0) == -1 && error == NULL)
1990 9c52365f 2020-03-21 stsp error = got_error_from_errno("waitpid");
1991 9c52365f 2020-03-21 stsp }
1992 7848a0e1 2020-03-19 stsp if (fetchfd != -1 && close(fetchfd) == -1 && error == NULL)
1993 7848a0e1 2020-03-19 stsp error = got_error_from_errno("close");
1994 7848a0e1 2020-03-19 stsp if (repo)
1995 7848a0e1 2020-03-19 stsp got_repo_close(repo);
1996 7848a0e1 2020-03-19 stsp if (worktree)
1997 7848a0e1 2020-03-19 stsp got_worktree_close(worktree);
1998 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &refs, entry) {
1999 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2000 7848a0e1 2020-03-19 stsp free(pe->data);
2001 7848a0e1 2020-03-19 stsp }
2002 7848a0e1 2020-03-19 stsp got_pathlist_free(&refs);
2003 7848a0e1 2020-03-19 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
2004 7848a0e1 2020-03-19 stsp free((void *)pe->path);
2005 7848a0e1 2020-03-19 stsp free(pe->data);
2006 7848a0e1 2020-03-19 stsp }
2007 7848a0e1 2020-03-19 stsp got_pathlist_free(&symrefs);
2008 4ba14133 2020-03-20 stsp got_pathlist_free(&wanted_branches);
2009 0e4002ca 2020-03-21 stsp got_pathlist_free(&wanted_refs);
2010 7848a0e1 2020-03-19 stsp free(id_str);
2011 7848a0e1 2020-03-19 stsp free(cwd);
2012 7848a0e1 2020-03-19 stsp free(repo_path);
2013 7848a0e1 2020-03-19 stsp free(pack_hash);
2014 7848a0e1 2020-03-19 stsp free(proto);
2015 7848a0e1 2020-03-19 stsp free(host);
2016 7848a0e1 2020-03-19 stsp free(port);
2017 7848a0e1 2020-03-19 stsp free(server_path);
2018 7848a0e1 2020-03-19 stsp free(repo_name);
2019 7848a0e1 2020-03-19 stsp return error;
2020 7848a0e1 2020-03-19 stsp }
2021 7848a0e1 2020-03-19 stsp
2022 7848a0e1 2020-03-19 stsp
2023 7848a0e1 2020-03-19 stsp __dead static void
2024 2ab43947 2020-03-18 stsp usage_checkout(void)
2025 2ab43947 2020-03-18 stsp {
2026 2ab43947 2020-03-18 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
2027 2ab43947 2020-03-18 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
2028 2ab43947 2020-03-18 stsp exit(1);
2029 2ab43947 2020-03-18 stsp }
2030 2ab43947 2020-03-18 stsp
2031 2ab43947 2020-03-18 stsp static void
2032 2ab43947 2020-03-18 stsp show_worktree_base_ref_warning(void)
2033 2ab43947 2020-03-18 stsp {
2034 2ab43947 2020-03-18 stsp fprintf(stderr, "%s: warning: could not create a reference "
2035 2ab43947 2020-03-18 stsp "to the work tree's base commit; the commit could be "
2036 2ab43947 2020-03-18 stsp "garbage-collected by Git; making the repository "
2037 2ab43947 2020-03-18 stsp "writable and running 'got update' will prevent this\n",
2038 2ab43947 2020-03-18 stsp getprogname());
2039 2ab43947 2020-03-18 stsp }
2040 2ab43947 2020-03-18 stsp
2041 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg {
2042 2ab43947 2020-03-18 stsp const char *worktree_path;
2043 2ab43947 2020-03-18 stsp int had_base_commit_ref_error;
2044 2ab43947 2020-03-18 stsp };
2045 2ab43947 2020-03-18 stsp
2046 2ab43947 2020-03-18 stsp static const struct got_error *
2047 2ab43947 2020-03-18 stsp checkout_progress(void *arg, unsigned char status, const char *path)
2048 2ab43947 2020-03-18 stsp {
2049 2ab43947 2020-03-18 stsp struct got_checkout_progress_arg *a = arg;
2050 2ab43947 2020-03-18 stsp
2051 2ab43947 2020-03-18 stsp /* Base commit bump happens silently. */
2052 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BUMP_BASE)
2053 2ab43947 2020-03-18 stsp return NULL;
2054 2ab43947 2020-03-18 stsp
2055 2ab43947 2020-03-18 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
2056 2ab43947 2020-03-18 stsp a->had_base_commit_ref_error = 1;
2057 2ab43947 2020-03-18 stsp return NULL;
2058 2ab43947 2020-03-18 stsp }
2059 2ab43947 2020-03-18 stsp
2060 2ab43947 2020-03-18 stsp while (path[0] == '/')
2061 2ab43947 2020-03-18 stsp path++;
2062 2ab43947 2020-03-18 stsp
2063 2ab43947 2020-03-18 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
2064 2ab43947 2020-03-18 stsp return NULL;
2065 2ab43947 2020-03-18 stsp }
2066 2ab43947 2020-03-18 stsp
2067 2ab43947 2020-03-18 stsp static const struct got_error *
2068 2ab43947 2020-03-18 stsp check_cancelled(void *arg)
2069 2ab43947 2020-03-18 stsp {
2070 2ab43947 2020-03-18 stsp if (sigint_received || sigpipe_received)
2071 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_CANCELLED);
2072 2ab43947 2020-03-18 stsp return NULL;
2073 2ab43947 2020-03-18 stsp }
2074 2ab43947 2020-03-18 stsp
2075 2ab43947 2020-03-18 stsp static const struct got_error *
2076 2ab43947 2020-03-18 stsp check_linear_ancestry(struct got_object_id *commit_id,
2077 2ab43947 2020-03-18 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
2078 2ab43947 2020-03-18 stsp struct got_repository *repo)
2079 2ab43947 2020-03-18 stsp {
2080 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2081 2ab43947 2020-03-18 stsp struct got_object_id *yca_id;
2082 2ab43947 2020-03-18 stsp
2083 2ab43947 2020-03-18 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
2084 2ab43947 2020-03-18 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
2085 2ab43947 2020-03-18 stsp if (err)
2086 2ab43947 2020-03-18 stsp return err;
2087 2ab43947 2020-03-18 stsp
2088 2ab43947 2020-03-18 stsp if (yca_id == NULL)
2089 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2090 2ab43947 2020-03-18 stsp
2091 2ab43947 2020-03-18 stsp /*
2092 2ab43947 2020-03-18 stsp * Require a straight line of history between the target commit
2093 2ab43947 2020-03-18 stsp * and the work tree's base commit.
2094 2ab43947 2020-03-18 stsp *
2095 2ab43947 2020-03-18 stsp * Non-linear situations such as this require a rebase:
2096 2ab43947 2020-03-18 stsp *
2097 2ab43947 2020-03-18 stsp * (commit) D F (base_commit)
2098 2ab43947 2020-03-18 stsp * \ /
2099 2ab43947 2020-03-18 stsp * C E
2100 2ab43947 2020-03-18 stsp * \ /
2101 2ab43947 2020-03-18 stsp * B (yca)
2102 2ab43947 2020-03-18 stsp * |
2103 2ab43947 2020-03-18 stsp * A
2104 2ab43947 2020-03-18 stsp *
2105 2ab43947 2020-03-18 stsp * 'got update' only handles linear cases:
2106 2ab43947 2020-03-18 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
2107 2ab43947 2020-03-18 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
2108 2ab43947 2020-03-18 stsp */
2109 2ab43947 2020-03-18 stsp if (allow_forwards_in_time_only) {
2110 2ab43947 2020-03-18 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
2111 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2112 2ab43947 2020-03-18 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
2113 2ab43947 2020-03-18 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
2114 2ab43947 2020-03-18 stsp return got_error(GOT_ERR_ANCESTRY);
2115 2ab43947 2020-03-18 stsp
2116 2ab43947 2020-03-18 stsp free(yca_id);
2117 2ab43947 2020-03-18 stsp return NULL;
2118 2ab43947 2020-03-18 stsp }
2119 2ab43947 2020-03-18 stsp
2120 2ab43947 2020-03-18 stsp static const struct got_error *
2121 2ab43947 2020-03-18 stsp check_same_branch(struct got_object_id *commit_id,
2122 2ab43947 2020-03-18 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
2123 2ab43947 2020-03-18 stsp struct got_repository *repo)
2124 2ab43947 2020-03-18 stsp {
2125 2ab43947 2020-03-18 stsp const struct got_error *err = NULL;
2126 2ab43947 2020-03-18 stsp struct got_commit_graph *graph = NULL;
2127 2ab43947 2020-03-18 stsp struct got_object_id *head_commit_id = NULL;
2128 2ab43947 2020-03-18 stsp int is_same_branch = 0;
2129 2ab43947 2020-03-18 stsp
2130 2ab43947 2020-03-18 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
2131 2ab43947 2020-03-18 stsp if (err)
2132 2ab43947 2020-03-18 stsp goto done;
2133 2ab43947 2020-03-18 stsp
2134 2ab43947 2020-03-18 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
2135 2ab43947 2020-03-18 stsp is_same_branch = 1;
2136 2ab43947 2020-03-18 stsp goto done;
2137 2ab43947 2020-03-18 stsp }
2138 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
2139 2ab43947 2020-03-18 stsp is_same_branch = 1;
2140 2ab43947 2020-03-18 stsp goto done;
2141 2ab43947 2020-03-18 stsp }
2142 2ab43947 2020-03-18 stsp
2143 2ab43947 2020-03-18 stsp err = got_commit_graph_open(&graph, "/", 1);
2144 2ab43947 2020-03-18 stsp if (err)
2145 2ab43947 2020-03-18 stsp goto done;
2146 2ab43947 2020-03-18 stsp
2147 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
2148 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2149 2ab43947 2020-03-18 stsp if (err)
2150 2ab43947 2020-03-18 stsp goto done;
2151 2ab43947 2020-03-18 stsp
2152 2ab43947 2020-03-18 stsp for (;;) {
2153 2ab43947 2020-03-18 stsp struct got_object_id *id;
2154 2ab43947 2020-03-18 stsp err = got_commit_graph_iter_next(&id, graph, repo,
2155 2ab43947 2020-03-18 stsp check_cancelled, NULL);
2156 2ab43947 2020-03-18 stsp if (err) {
2157 2ab43947 2020-03-18 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
2158 2ab43947 2020-03-18 stsp err = NULL;
2159 2ab43947 2020-03-18 stsp break;
2160 2ab43947 2020-03-18 stsp }
2161 2ab43947 2020-03-18 stsp
2162 2ab43947 2020-03-18 stsp if (id) {
2163 2ab43947 2020-03-18 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
2164 2ab43947 2020-03-18 stsp break;
2165 2ab43947 2020-03-18 stsp if (got_object_id_cmp(id, commit_id) == 0) {
2166 2ab43947 2020-03-18 stsp is_same_branch = 1;
2167 2ab43947 2020-03-18 stsp break;
2168 2ab43947 2020-03-18 stsp }
2169 2ab43947 2020-03-18 stsp }
2170 2ab43947 2020-03-18 stsp }
2171 2ab43947 2020-03-18 stsp done:
2172 2ab43947 2020-03-18 stsp if (graph)
2173 2ab43947 2020-03-18 stsp got_commit_graph_close(graph);
2174 2ab43947 2020-03-18 stsp free(head_commit_id);
2175 2ab43947 2020-03-18 stsp if (!err && !is_same_branch)
2176 2ab43947 2020-03-18 stsp err = got_error(GOT_ERR_ANCESTRY);
2177 2ab43947 2020-03-18 stsp return err;
2178 a367ff0f 2019-05-14 stsp }
2179 8069f636 2019-01-12 stsp
2180 4ed7e80c 2018-05-20 stsp static const struct got_error *
2181 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
2182 4b6c9460 2020-03-05 stsp {
2183 4b6c9460 2020-03-05 stsp static char msg[512];
2184 4b6c9460 2020-03-05 stsp const char *branch_name;
2185 4b6c9460 2020-03-05 stsp
2186 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
2187 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
2188 4b6c9460 2020-03-05 stsp else
2189 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
2190 4b6c9460 2020-03-05 stsp
2191 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
2192 4b6c9460 2020-03-05 stsp branch_name += 11;
2193 4b6c9460 2020-03-05 stsp
2194 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
2195 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
2196 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
2197 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
2198 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
2199 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
2200 4b6c9460 2020-03-05 stsp
2201 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
2202 4b6c9460 2020-03-05 stsp }
2203 4b6c9460 2020-03-05 stsp
2204 4b6c9460 2020-03-05 stsp static const struct got_error *
2205 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
2206 c09a553d 2018-03-12 stsp {
2207 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
2208 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
2209 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
2210 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
2211 c09a553d 2018-03-12 stsp char *repo_path = NULL;
2212 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
2213 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
2214 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
2215 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
2216 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
2217 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2218 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
2219 f2ea84fa 2019-07-27 stsp
2220 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2221 c09a553d 2018-03-12 stsp
2222 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
2223 0bb8a95e 2018-03-12 stsp switch (ch) {
2224 08573d5b 2019-05-14 stsp case 'b':
2225 08573d5b 2019-05-14 stsp branch_name = optarg;
2226 08573d5b 2019-05-14 stsp break;
2227 8069f636 2019-01-12 stsp case 'c':
2228 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
2229 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
2230 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2231 bb51a5b4 2020-01-13 stsp break;
2232 bb51a5b4 2020-01-13 stsp case 'E':
2233 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
2234 8069f636 2019-01-12 stsp break;
2235 0bb8a95e 2018-03-12 stsp case 'p':
2236 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
2237 0bb8a95e 2018-03-12 stsp break;
2238 0bb8a95e 2018-03-12 stsp default:
2239 2deda0b9 2019-03-07 stsp usage_checkout();
2240 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
2241 0bb8a95e 2018-03-12 stsp }
2242 0bb8a95e 2018-03-12 stsp }
2243 0bb8a95e 2018-03-12 stsp
2244 0bb8a95e 2018-03-12 stsp argc -= optind;
2245 0bb8a95e 2018-03-12 stsp argv += optind;
2246 0bb8a95e 2018-03-12 stsp
2247 6715a751 2018-03-16 stsp #ifndef PROFILE
2248 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2249 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2250 c09a553d 2018-03-12 stsp err(1, "pledge");
2251 6715a751 2018-03-16 stsp #endif
2252 0bb8a95e 2018-03-12 stsp if (argc == 1) {
2253 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
2254 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2255 76089277 2018-04-01 stsp if (repo_path == NULL)
2256 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
2257 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
2258 76089277 2018-04-01 stsp if (cwd == NULL) {
2259 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2260 76089277 2018-04-01 stsp goto done;
2261 76089277 2018-04-01 stsp }
2262 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
2263 230a42bd 2019-05-11 jcs base = basename(path_prefix);
2264 230a42bd 2019-05-11 jcs if (base == NULL) {
2265 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2266 230a42bd 2019-05-11 jcs path_prefix);
2267 230a42bd 2019-05-11 jcs goto done;
2268 230a42bd 2019-05-11 jcs }
2269 230a42bd 2019-05-11 jcs } else {
2270 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
2271 230a42bd 2019-05-11 jcs if (base == NULL) {
2272 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
2273 230a42bd 2019-05-11 jcs repo_path);
2274 230a42bd 2019-05-11 jcs goto done;
2275 230a42bd 2019-05-11 jcs }
2276 76089277 2018-04-01 stsp }
2277 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
2278 c09a553d 2018-03-12 stsp if (dotgit)
2279 c09a553d 2018-03-12 stsp *dotgit = '\0';
2280 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
2281 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2282 c09a553d 2018-03-12 stsp free(cwd);
2283 76089277 2018-04-01 stsp goto done;
2284 c09a553d 2018-03-12 stsp }
2285 c09a553d 2018-03-12 stsp free(cwd);
2286 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
2287 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
2288 76089277 2018-04-01 stsp if (repo_path == NULL) {
2289 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
2290 76089277 2018-04-01 stsp goto done;
2291 76089277 2018-04-01 stsp }
2292 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
2293 76089277 2018-04-01 stsp if (worktree_path == NULL) {
2294 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
2295 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
2296 b4b3a7dd 2019-07-22 stsp argv[1]);
2297 b4b3a7dd 2019-07-22 stsp goto done;
2298 b4b3a7dd 2019-07-22 stsp }
2299 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
2300 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
2301 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
2302 b4b3a7dd 2019-07-22 stsp goto done;
2303 b4b3a7dd 2019-07-22 stsp }
2304 76089277 2018-04-01 stsp }
2305 c09a553d 2018-03-12 stsp } else
2306 c09a553d 2018-03-12 stsp usage_checkout();
2307 c09a553d 2018-03-12 stsp
2308 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2309 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
2310 13bfb272 2019-05-10 jcs
2311 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2312 c09a553d 2018-03-12 stsp if (error != NULL)
2313 c02c541e 2019-03-29 stsp goto done;
2314 c02c541e 2019-03-29 stsp
2315 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
2316 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
2317 c530dc23 2019-07-23 stsp if (error) {
2318 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
2319 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2320 c530dc23 2019-07-23 stsp goto done;
2321 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
2322 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
2323 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
2324 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
2325 c530dc23 2019-07-23 stsp goto done;
2326 c530dc23 2019-07-23 stsp }
2327 c530dc23 2019-07-23 stsp }
2328 c530dc23 2019-07-23 stsp
2329 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
2330 c02c541e 2019-03-29 stsp if (error)
2331 c09a553d 2018-03-12 stsp goto done;
2332 8069f636 2019-01-12 stsp
2333 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
2334 c09a553d 2018-03-12 stsp if (error != NULL)
2335 c09a553d 2018-03-12 stsp goto done;
2336 c09a553d 2018-03-12 stsp
2337 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
2338 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
2339 c09a553d 2018-03-12 stsp goto done;
2340 c09a553d 2018-03-12 stsp
2341 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
2342 c09a553d 2018-03-12 stsp if (error != NULL)
2343 c09a553d 2018-03-12 stsp goto done;
2344 c09a553d 2018-03-12 stsp
2345 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
2346 e5dc7198 2018-12-29 stsp path_prefix);
2347 e5dc7198 2018-12-29 stsp if (error != NULL)
2348 e5dc7198 2018-12-29 stsp goto done;
2349 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
2350 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
2351 49520a32 2018-12-29 stsp goto done;
2352 49520a32 2018-12-29 stsp }
2353 49520a32 2018-12-29 stsp
2354 8069f636 2019-01-12 stsp if (commit_id_str) {
2355 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
2356 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2357 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2358 30837e32 2019-07-25 stsp if (error)
2359 8069f636 2019-01-12 stsp goto done;
2360 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2361 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2362 8069f636 2019-01-12 stsp if (error != NULL) {
2363 8069f636 2019-01-12 stsp free(commit_id);
2364 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2365 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2366 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2367 4b6c9460 2020-03-05 stsp }
2368 8069f636 2019-01-12 stsp goto done;
2369 8069f636 2019-01-12 stsp }
2370 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2371 4b6c9460 2020-03-05 stsp if (error) {
2372 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
2373 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
2374 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
2375 4b6c9460 2020-03-05 stsp }
2376 45d344f6 2019-05-14 stsp goto done;
2377 4b6c9460 2020-03-05 stsp }
2378 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2379 8069f636 2019-01-12 stsp commit_id);
2380 8069f636 2019-01-12 stsp free(commit_id);
2381 8069f636 2019-01-12 stsp if (error)
2382 8069f636 2019-01-12 stsp goto done;
2383 8069f636 2019-01-12 stsp }
2384 8069f636 2019-01-12 stsp
2385 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
2386 f2ea84fa 2019-07-27 stsp if (error)
2387 f2ea84fa 2019-07-27 stsp goto done;
2388 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
2389 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
2390 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2391 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
2392 c09a553d 2018-03-12 stsp if (error != NULL)
2393 c09a553d 2018-03-12 stsp goto done;
2394 c09a553d 2018-03-12 stsp
2395 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
2396 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
2397 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
2398 c09a553d 2018-03-12 stsp done:
2399 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2400 8069f636 2019-01-12 stsp free(commit_id_str);
2401 76089277 2018-04-01 stsp free(repo_path);
2402 507dc3bb 2018-12-29 stsp free(worktree_path);
2403 507dc3bb 2018-12-29 stsp return error;
2404 507dc3bb 2018-12-29 stsp }
2405 507dc3bb 2018-12-29 stsp
2406 507dc3bb 2018-12-29 stsp __dead static void
2407 507dc3bb 2018-12-29 stsp usage_update(void)
2408 507dc3bb 2018-12-29 stsp {
2409 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
2410 507dc3bb 2018-12-29 stsp getprogname());
2411 507dc3bb 2018-12-29 stsp exit(1);
2412 507dc3bb 2018-12-29 stsp }
2413 507dc3bb 2018-12-29 stsp
2414 1ee397ad 2019-07-12 stsp static const struct got_error *
2415 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
2416 507dc3bb 2018-12-29 stsp {
2417 784955db 2019-01-12 stsp int *did_something = arg;
2418 784955db 2019-01-12 stsp
2419 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
2420 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
2421 1ee397ad 2019-07-12 stsp return NULL;
2422 507dc3bb 2018-12-29 stsp
2423 1545c615 2019-02-10 stsp *did_something = 1;
2424 a484d721 2019-06-10 stsp
2425 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
2426 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
2427 1ee397ad 2019-07-12 stsp return NULL;
2428 a484d721 2019-06-10 stsp
2429 507dc3bb 2018-12-29 stsp while (path[0] == '/')
2430 507dc3bb 2018-12-29 stsp path++;
2431 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
2432 1ee397ad 2019-07-12 stsp return NULL;
2433 be7061eb 2018-12-30 stsp }
2434 be7061eb 2018-12-30 stsp
2435 be7061eb 2018-12-30 stsp static const struct got_error *
2436 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
2437 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
2438 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
2439 a1fb16d8 2019-05-24 stsp {
2440 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
2441 a1fb16d8 2019-05-24 stsp char *base_id_str;
2442 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
2443 a1fb16d8 2019-05-24 stsp
2444 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
2445 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
2446 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
2447 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
2448 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
2449 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
2450 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
2451 a1fb16d8 2019-05-24 stsp }
2452 a1fb16d8 2019-05-24 stsp
2453 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
2454 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2455 a1fb16d8 2019-05-24 stsp if (err) {
2456 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
2457 a1fb16d8 2019-05-24 stsp return err;
2458 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
2459 a1fb16d8 2019-05-24 stsp }
2460 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
2461 a1fb16d8 2019-05-24 stsp return NULL;
2462 a1fb16d8 2019-05-24 stsp
2463 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
2464 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
2465 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
2466 a1fb16d8 2019-05-24 stsp if (err)
2467 a1fb16d8 2019-05-24 stsp return err;
2468 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
2469 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2470 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
2471 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
2472 0ebf8283 2019-07-24 stsp return NULL;
2473 0ebf8283 2019-07-24 stsp }
2474 0ebf8283 2019-07-24 stsp
2475 0ebf8283 2019-07-24 stsp static const struct got_error *
2476 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
2477 0ebf8283 2019-07-24 stsp {
2478 0ebf8283 2019-07-24 stsp const struct got_error *err;
2479 0ebf8283 2019-07-24 stsp int in_progress;
2480 0ebf8283 2019-07-24 stsp
2481 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
2482 0ebf8283 2019-07-24 stsp if (err)
2483 0ebf8283 2019-07-24 stsp return err;
2484 0ebf8283 2019-07-24 stsp if (in_progress)
2485 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
2486 0ebf8283 2019-07-24 stsp
2487 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
2488 0ebf8283 2019-07-24 stsp if (err)
2489 0ebf8283 2019-07-24 stsp return err;
2490 0ebf8283 2019-07-24 stsp if (in_progress)
2491 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
2492 0ebf8283 2019-07-24 stsp
2493 a1fb16d8 2019-05-24 stsp return NULL;
2494 a5edda0a 2019-07-27 stsp }
2495 a5edda0a 2019-07-27 stsp
2496 a5edda0a 2019-07-27 stsp static const struct got_error *
2497 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
2498 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
2499 a5edda0a 2019-07-27 stsp {
2500 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
2501 a5edda0a 2019-07-27 stsp char *path;
2502 a5edda0a 2019-07-27 stsp int i;
2503 a5edda0a 2019-07-27 stsp
2504 a5edda0a 2019-07-27 stsp if (argc == 0) {
2505 a5edda0a 2019-07-27 stsp path = strdup("");
2506 a5edda0a 2019-07-27 stsp if (path == NULL)
2507 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
2508 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
2509 a5edda0a 2019-07-27 stsp }
2510 a5edda0a 2019-07-27 stsp
2511 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
2512 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
2513 a5edda0a 2019-07-27 stsp if (err)
2514 a5edda0a 2019-07-27 stsp break;
2515 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
2516 a5edda0a 2019-07-27 stsp if (err) {
2517 a5edda0a 2019-07-27 stsp free(path);
2518 a5edda0a 2019-07-27 stsp break;
2519 a5edda0a 2019-07-27 stsp }
2520 a5edda0a 2019-07-27 stsp }
2521 a5edda0a 2019-07-27 stsp
2522 a5edda0a 2019-07-27 stsp return err;
2523 a1fb16d8 2019-05-24 stsp }
2524 a1fb16d8 2019-05-24 stsp
2525 a1fb16d8 2019-05-24 stsp static const struct got_error *
2526 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
2527 507dc3bb 2018-12-29 stsp {
2528 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
2529 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
2530 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
2531 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
2532 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
2533 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
2534 024e9686 2019-05-14 stsp const char *branch_name = NULL;
2535 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
2536 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
2537 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
2538 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
2539 507dc3bb 2018-12-29 stsp
2540 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
2541 f2ea84fa 2019-07-27 stsp
2542 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
2543 507dc3bb 2018-12-29 stsp switch (ch) {
2544 024e9686 2019-05-14 stsp case 'b':
2545 024e9686 2019-05-14 stsp branch_name = optarg;
2546 024e9686 2019-05-14 stsp break;
2547 507dc3bb 2018-12-29 stsp case 'c':
2548 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
2549 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
2550 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
2551 507dc3bb 2018-12-29 stsp break;
2552 507dc3bb 2018-12-29 stsp default:
2553 2deda0b9 2019-03-07 stsp usage_update();
2554 507dc3bb 2018-12-29 stsp /* NOTREACHED */
2555 507dc3bb 2018-12-29 stsp }
2556 507dc3bb 2018-12-29 stsp }
2557 507dc3bb 2018-12-29 stsp
2558 507dc3bb 2018-12-29 stsp argc -= optind;
2559 507dc3bb 2018-12-29 stsp argv += optind;
2560 507dc3bb 2018-12-29 stsp
2561 507dc3bb 2018-12-29 stsp #ifndef PROFILE
2562 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
2563 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
2564 507dc3bb 2018-12-29 stsp err(1, "pledge");
2565 507dc3bb 2018-12-29 stsp #endif
2566 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
2567 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
2568 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2569 c4cdcb68 2019-04-03 stsp goto done;
2570 c4cdcb68 2019-04-03 stsp }
2571 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
2572 7d5807f4 2019-07-11 stsp if (error)
2573 7d5807f4 2019-07-11 stsp goto done;
2574 7d5807f4 2019-07-11 stsp
2575 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
2576 f2ea84fa 2019-07-27 stsp if (error)
2577 f2ea84fa 2019-07-27 stsp goto done;
2578 507dc3bb 2018-12-29 stsp
2579 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2580 c9956ddf 2019-09-08 stsp NULL);
2581 507dc3bb 2018-12-29 stsp if (error != NULL)
2582 507dc3bb 2018-12-29 stsp goto done;
2583 507dc3bb 2018-12-29 stsp
2584 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
2585 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2586 087fb88c 2019-08-04 stsp if (error)
2587 087fb88c 2019-08-04 stsp goto done;
2588 087fb88c 2019-08-04 stsp
2589 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2590 0266afb7 2019-01-04 stsp if (error)
2591 0266afb7 2019-01-04 stsp goto done;
2592 0266afb7 2019-01-04 stsp
2593 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
2594 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
2595 024e9686 2019-05-14 stsp if (error != NULL)
2596 024e9686 2019-05-14 stsp goto done;
2597 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
2598 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2599 9c4b8182 2019-01-02 stsp if (error != NULL)
2600 9c4b8182 2019-01-02 stsp goto done;
2601 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
2602 507dc3bb 2018-12-29 stsp if (error != NULL)
2603 507dc3bb 2018-12-29 stsp goto done;
2604 507dc3bb 2018-12-29 stsp } else {
2605 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2606 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2607 04f57cb3 2019-07-25 stsp free(commit_id_str);
2608 bb787f09 2019-07-25 stsp commit_id_str = NULL;
2609 30837e32 2019-07-25 stsp if (error)
2610 a297e751 2019-07-11 stsp goto done;
2611 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
2612 a297e751 2019-07-11 stsp if (error)
2613 507dc3bb 2018-12-29 stsp goto done;
2614 507dc3bb 2018-12-29 stsp }
2615 35c965b2 2018-12-29 stsp
2616 a1fb16d8 2019-05-24 stsp if (branch_name) {
2617 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
2618 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
2619 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
2620 f2ea84fa 2019-07-27 stsp continue;
2621 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
2622 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
2623 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
2624 024e9686 2019-05-14 stsp goto done;
2625 024e9686 2019-05-14 stsp }
2626 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
2627 024e9686 2019-05-14 stsp if (error)
2628 024e9686 2019-05-14 stsp goto done;
2629 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
2630 3aef623b 2019-10-15 stsp repo);
2631 a367ff0f 2019-05-14 stsp free(head_commit_id);
2632 024e9686 2019-05-14 stsp if (error != NULL)
2633 024e9686 2019-05-14 stsp goto done;
2634 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2635 a367ff0f 2019-05-14 stsp if (error)
2636 a367ff0f 2019-05-14 stsp goto done;
2637 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
2638 024e9686 2019-05-14 stsp if (error)
2639 024e9686 2019-05-14 stsp goto done;
2640 024e9686 2019-05-14 stsp } else {
2641 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
2642 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
2643 a367ff0f 2019-05-14 stsp if (error != NULL) {
2644 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
2645 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
2646 024e9686 2019-05-14 stsp goto done;
2647 a367ff0f 2019-05-14 stsp }
2648 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
2649 a367ff0f 2019-05-14 stsp if (error)
2650 a367ff0f 2019-05-14 stsp goto done;
2651 024e9686 2019-05-14 stsp }
2652 507dc3bb 2018-12-29 stsp
2653 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
2654 507dc3bb 2018-12-29 stsp commit_id) != 0) {
2655 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
2656 507dc3bb 2018-12-29 stsp commit_id);
2657 507dc3bb 2018-12-29 stsp if (error)
2658 507dc3bb 2018-12-29 stsp goto done;
2659 507dc3bb 2018-12-29 stsp }
2660 507dc3bb 2018-12-29 stsp
2661 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
2662 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
2663 507dc3bb 2018-12-29 stsp if (error != NULL)
2664 507dc3bb 2018-12-29 stsp goto done;
2665 9c4b8182 2019-01-02 stsp
2666 784955db 2019-01-12 stsp if (did_something)
2667 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
2668 784955db 2019-01-12 stsp else
2669 784955db 2019-01-12 stsp printf("Already up-to-date\n");
2670 507dc3bb 2018-12-29 stsp done:
2671 c09a553d 2018-03-12 stsp free(worktree_path);
2672 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2673 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
2674 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
2675 507dc3bb 2018-12-29 stsp free(commit_id);
2676 9c4b8182 2019-01-02 stsp free(commit_id_str);
2677 c09a553d 2018-03-12 stsp return error;
2678 c09a553d 2018-03-12 stsp }
2679 c09a553d 2018-03-12 stsp
2680 f42b1b34 2018-03-12 stsp static const struct got_error *
2681 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
2682 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2683 63035f9f 2019-10-06 stsp struct got_repository *repo)
2684 5c860e29 2018-03-12 stsp {
2685 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
2686 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
2687 79109fed 2018-03-27 stsp
2688 44392932 2019-08-25 stsp if (blob_id1) {
2689 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
2690 44392932 2019-08-25 stsp if (err)
2691 44392932 2019-08-25 stsp goto done;
2692 44392932 2019-08-25 stsp }
2693 79109fed 2018-03-27 stsp
2694 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
2695 44392932 2019-08-25 stsp if (err)
2696 44392932 2019-08-25 stsp goto done;
2697 79109fed 2018-03-27 stsp
2698 44392932 2019-08-25 stsp while (path[0] == '/')
2699 44392932 2019-08-25 stsp path++;
2700 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
2701 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
2702 44392932 2019-08-25 stsp done:
2703 44392932 2019-08-25 stsp if (blob1)
2704 44392932 2019-08-25 stsp got_object_blob_close(blob1);
2705 44392932 2019-08-25 stsp got_object_blob_close(blob2);
2706 44392932 2019-08-25 stsp return err;
2707 44392932 2019-08-25 stsp }
2708 44392932 2019-08-25 stsp
2709 44392932 2019-08-25 stsp static const struct got_error *
2710 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
2711 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
2712 63035f9f 2019-10-06 stsp struct got_repository *repo)
2713 44392932 2019-08-25 stsp {
2714 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2715 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
2716 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
2717 44392932 2019-08-25 stsp
2718 44392932 2019-08-25 stsp if (tree_id1) {
2719 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
2720 79109fed 2018-03-27 stsp if (err)
2721 44392932 2019-08-25 stsp goto done;
2722 79109fed 2018-03-27 stsp }
2723 79109fed 2018-03-27 stsp
2724 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
2725 0f2b3dca 2018-12-22 stsp if (err)
2726 0f2b3dca 2018-12-22 stsp goto done;
2727 0f2b3dca 2018-12-22 stsp
2728 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
2729 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2730 aaa13589 2019-06-01 stsp arg.outfile = stdout;
2731 44392932 2019-08-25 stsp while (path[0] == '/')
2732 44392932 2019-08-25 stsp path++;
2733 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
2734 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
2735 0f2b3dca 2018-12-22 stsp done:
2736 79109fed 2018-03-27 stsp if (tree1)
2737 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
2738 366e0a5f 2019-10-10 stsp if (tree2)
2739 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
2740 44392932 2019-08-25 stsp return err;
2741 44392932 2019-08-25 stsp }
2742 44392932 2019-08-25 stsp
2743 44392932 2019-08-25 stsp static const struct got_error *
2744 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
2745 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
2746 44392932 2019-08-25 stsp {
2747 44392932 2019-08-25 stsp const struct got_error *err = NULL;
2748 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
2749 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
2750 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
2751 44392932 2019-08-25 stsp struct got_object_qid *qid;
2752 44392932 2019-08-25 stsp
2753 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
2754 44392932 2019-08-25 stsp if (qid != NULL) {
2755 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
2756 44392932 2019-08-25 stsp qid->id);
2757 44392932 2019-08-25 stsp if (err)
2758 44392932 2019-08-25 stsp return err;
2759 44392932 2019-08-25 stsp }
2760 44392932 2019-08-25 stsp
2761 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
2762 44392932 2019-08-25 stsp int obj_type;
2763 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
2764 44392932 2019-08-25 stsp if (err)
2765 44392932 2019-08-25 stsp goto done;
2766 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2767 44392932 2019-08-25 stsp if (err) {
2768 44392932 2019-08-25 stsp free(obj_id2);
2769 44392932 2019-08-25 stsp goto done;
2770 44392932 2019-08-25 stsp }
2771 44392932 2019-08-25 stsp if (pcommit) {
2772 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
2773 44392932 2019-08-25 stsp qid->id, path);
2774 44392932 2019-08-25 stsp if (err) {
2775 44392932 2019-08-25 stsp free(obj_id2);
2776 44392932 2019-08-25 stsp goto done;
2777 44392932 2019-08-25 stsp }
2778 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2779 44392932 2019-08-25 stsp if (err) {
2780 44392932 2019-08-25 stsp free(obj_id2);
2781 44392932 2019-08-25 stsp goto done;
2782 44392932 2019-08-25 stsp }
2783 44392932 2019-08-25 stsp }
2784 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
2785 44392932 2019-08-25 stsp if (err) {
2786 44392932 2019-08-25 stsp free(obj_id2);
2787 44392932 2019-08-25 stsp goto done;
2788 44392932 2019-08-25 stsp }
2789 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2790 44392932 2019-08-25 stsp switch (obj_type) {
2791 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
2792 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
2793 63035f9f 2019-10-06 stsp 0, repo);
2794 44392932 2019-08-25 stsp break;
2795 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
2796 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
2797 63035f9f 2019-10-06 stsp 0, repo);
2798 44392932 2019-08-25 stsp break;
2799 44392932 2019-08-25 stsp default:
2800 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
2801 44392932 2019-08-25 stsp break;
2802 44392932 2019-08-25 stsp }
2803 44392932 2019-08-25 stsp free(obj_id1);
2804 44392932 2019-08-25 stsp free(obj_id2);
2805 44392932 2019-08-25 stsp } else {
2806 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
2807 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
2808 44392932 2019-08-25 stsp if (err)
2809 44392932 2019-08-25 stsp goto done;
2810 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
2811 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
2812 44392932 2019-08-25 stsp if (err)
2813 44392932 2019-08-25 stsp goto done;
2814 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
2815 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
2816 44392932 2019-08-25 stsp }
2817 44392932 2019-08-25 stsp done:
2818 0f2b3dca 2018-12-22 stsp free(id_str1);
2819 0f2b3dca 2018-12-22 stsp free(id_str2);
2820 44392932 2019-08-25 stsp if (pcommit)
2821 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
2822 79109fed 2018-03-27 stsp return err;
2823 79109fed 2018-03-27 stsp }
2824 79109fed 2018-03-27 stsp
2825 4bb494d5 2018-06-16 stsp static char *
2826 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
2827 6c281f94 2018-06-11 stsp {
2828 09867e48 2019-08-13 stsp struct tm mytm, *tm;
2829 09867e48 2019-08-13 stsp char *p, *s;
2830 09867e48 2019-08-13 stsp
2831 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
2832 09867e48 2019-08-13 stsp if (tm == NULL)
2833 09867e48 2019-08-13 stsp return NULL;
2834 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
2835 09867e48 2019-08-13 stsp if (s == NULL)
2836 09867e48 2019-08-13 stsp return NULL;
2837 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
2838 6c281f94 2018-06-11 stsp if (p)
2839 6c281f94 2018-06-11 stsp *p = '\0';
2840 6c281f94 2018-06-11 stsp return s;
2841 6c281f94 2018-06-11 stsp }
2842 dc424a06 2019-08-07 stsp
2843 6841bf13 2019-11-29 kn static const struct got_error *
2844 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
2845 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
2846 6841bf13 2019-11-29 kn {
2847 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
2848 6841bf13 2019-11-29 kn regmatch_t regmatch;
2849 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
2850 6841bf13 2019-11-29 kn
2851 6841bf13 2019-11-29 kn *have_match = 0;
2852 6841bf13 2019-11-29 kn
2853 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
2854 6841bf13 2019-11-29 kn if (err)
2855 6841bf13 2019-11-29 kn return err;
2856 6841bf13 2019-11-29 kn
2857 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
2858 6841bf13 2019-11-29 kn if (err)
2859 6841bf13 2019-11-29 kn goto done;
2860 6841bf13 2019-11-29 kn
2861 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
2862 6841bf13 2019-11-29 kn *have_match = 1;
2863 6841bf13 2019-11-29 kn done:
2864 6841bf13 2019-11-29 kn free(id_str);
2865 6841bf13 2019-11-29 kn free(logmsg);
2866 6841bf13 2019-11-29 kn return err;
2867 6841bf13 2019-11-29 kn }
2868 6841bf13 2019-11-29 kn
2869 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
2870 6c281f94 2018-06-11 stsp
2871 79109fed 2018-03-27 stsp static const struct got_error *
2872 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
2873 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
2874 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
2875 79109fed 2018-03-27 stsp {
2876 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
2877 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
2878 6c281f94 2018-06-11 stsp char datebuf[26];
2879 45d799e2 2018-12-23 stsp time_t committer_time;
2880 45d799e2 2018-12-23 stsp const char *author, *committer;
2881 199a4027 2019-02-02 stsp char *refs_str = NULL;
2882 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
2883 5c860e29 2018-03-12 stsp
2884 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
2885 199a4027 2019-02-02 stsp char *s;
2886 199a4027 2019-02-02 stsp const char *name;
2887 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
2888 a436ad14 2019-08-13 stsp int cmp;
2889 a436ad14 2019-08-13 stsp
2890 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
2891 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
2892 d9498b20 2019-02-05 stsp continue;
2893 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
2894 199a4027 2019-02-02 stsp name += 5;
2895 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
2896 7143d404 2019-03-12 stsp continue;
2897 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
2898 e34f9ed6 2019-02-02 stsp name += 6;
2899 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
2900 141c2bff 2019-02-04 stsp name += 8;
2901 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
2902 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
2903 5d844a1e 2019-08-13 stsp if (err) {
2904 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
2905 5d844a1e 2019-08-13 stsp return err;
2906 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
2907 5d844a1e 2019-08-13 stsp err = NULL;
2908 5d844a1e 2019-08-13 stsp tag = NULL;
2909 5d844a1e 2019-08-13 stsp }
2910 a436ad14 2019-08-13 stsp }
2911 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
2912 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
2913 a436ad14 2019-08-13 stsp if (tag)
2914 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
2915 a436ad14 2019-08-13 stsp if (cmp != 0)
2916 a436ad14 2019-08-13 stsp continue;
2917 199a4027 2019-02-02 stsp s = refs_str;
2918 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
2919 199a4027 2019-02-02 stsp name) == -1) {
2920 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2921 199a4027 2019-02-02 stsp free(s);
2922 34ca4898 2019-08-13 stsp return err;
2923 199a4027 2019-02-02 stsp }
2924 199a4027 2019-02-02 stsp free(s);
2925 199a4027 2019-02-02 stsp }
2926 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
2927 8bf5b3c9 2018-03-17 stsp if (err)
2928 8bf5b3c9 2018-03-17 stsp return err;
2929 788c352e 2018-06-16 stsp
2930 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
2931 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
2932 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
2933 832c249c 2018-06-10 stsp free(id_str);
2934 d3d493d7 2019-02-21 stsp id_str = NULL;
2935 d3d493d7 2019-02-21 stsp free(refs_str);
2936 d3d493d7 2019-02-21 stsp refs_str = NULL;
2937 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
2938 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
2939 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
2940 09867e48 2019-08-13 stsp if (datestr)
2941 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
2942 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
2943 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
2944 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
2945 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
2946 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
2947 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
2948 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
2949 3fe1abad 2018-06-10 stsp int n = 1;
2950 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
2951 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
2952 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
2953 a0603db2 2018-06-10 stsp if (err)
2954 a0603db2 2018-06-10 stsp return err;
2955 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
2956 a0603db2 2018-06-10 stsp free(id_str);
2957 a0603db2 2018-06-10 stsp }
2958 a0603db2 2018-06-10 stsp }
2959 832c249c 2018-06-10 stsp
2960 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
2961 5943eee2 2019-08-13 stsp if (err)
2962 5943eee2 2019-08-13 stsp return err;
2963 8bf5b3c9 2018-03-17 stsp
2964 621015ac 2018-07-23 stsp logmsg = logmsg0;
2965 832c249c 2018-06-10 stsp do {
2966 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
2967 832c249c 2018-06-10 stsp if (line)
2968 832c249c 2018-06-10 stsp printf(" %s\n", line);
2969 832c249c 2018-06-10 stsp } while (line);
2970 621015ac 2018-07-23 stsp free(logmsg0);
2971 832c249c 2018-06-10 stsp
2972 971751ac 2018-03-27 stsp if (show_patch) {
2973 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
2974 971751ac 2018-03-27 stsp if (err == 0)
2975 971751ac 2018-03-27 stsp printf("\n");
2976 971751ac 2018-03-27 stsp }
2977 07862c20 2018-09-15 stsp
2978 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
2979 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
2980 07862c20 2018-09-15 stsp return err;
2981 f42b1b34 2018-03-12 stsp }
2982 5c860e29 2018-03-12 stsp
2983 f42b1b34 2018-03-12 stsp static const struct got_error *
2984 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
2985 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
2986 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
2987 463a997a 2019-12-08 kn struct got_reflist_head *refs)
2988 f42b1b34 2018-03-12 stsp {
2989 f42b1b34 2018-03-12 stsp const struct got_error *err;
2990 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
2991 6841bf13 2019-11-29 kn regex_t regex;
2992 6841bf13 2019-11-29 kn int have_match;
2993 372ccdbb 2018-06-10 stsp
2994 6841bf13 2019-11-29 kn if (search_pattern &&
2995 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
2996 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
2997 6841bf13 2019-11-29 kn
2998 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
2999 f42b1b34 2018-03-12 stsp if (err)
3000 f42b1b34 2018-03-12 stsp return err;
3001 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
3002 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3003 372ccdbb 2018-06-10 stsp if (err)
3004 fcc85cad 2018-07-22 stsp goto done;
3005 656b1f76 2019-05-11 jcs for (;;) {
3006 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
3007 372ccdbb 2018-06-10 stsp struct got_object_id *id;
3008 8bf5b3c9 2018-03-17 stsp
3009 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
3010 84453469 2018-11-11 stsp break;
3011 84453469 2018-11-11 stsp
3012 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
3013 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
3014 372ccdbb 2018-06-10 stsp if (err) {
3015 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
3016 9ba79e04 2018-06-11 stsp err = NULL;
3017 ee780d5c 2020-01-04 stsp break;
3018 7e665116 2018-04-02 stsp }
3019 b43fbaa0 2018-06-11 stsp if (id == NULL)
3020 7e665116 2018-04-02 stsp break;
3021 b43fbaa0 2018-06-11 stsp
3022 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
3023 b43fbaa0 2018-06-11 stsp if (err)
3024 fcc85cad 2018-07-22 stsp break;
3025 6841bf13 2019-11-29 kn
3026 6841bf13 2019-11-29 kn if (search_pattern) {
3027 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
3028 6841bf13 2019-11-29 kn if (err) {
3029 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3030 6841bf13 2019-11-29 kn break;
3031 6841bf13 2019-11-29 kn }
3032 6841bf13 2019-11-29 kn if (have_match == 0) {
3033 6841bf13 2019-11-29 kn got_object_commit_close(commit);
3034 6841bf13 2019-11-29 kn continue;
3035 6841bf13 2019-11-29 kn }
3036 6841bf13 2019-11-29 kn }
3037 6841bf13 2019-11-29 kn
3038 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
3039 44392932 2019-08-25 stsp diff_context, refs);
3040 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
3041 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
3042 7e665116 2018-04-02 stsp break;
3043 31cedeaf 2018-09-15 stsp }
3044 fcc85cad 2018-07-22 stsp done:
3045 6841bf13 2019-11-29 kn if (search_pattern)
3046 6841bf13 2019-11-29 kn regfree(&regex);
3047 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
3048 f42b1b34 2018-03-12 stsp return err;
3049 f42b1b34 2018-03-12 stsp }
3050 5c860e29 2018-03-12 stsp
3051 4ed7e80c 2018-05-20 stsp __dead static void
3052 6f3d1eb0 2018-03-12 stsp usage_log(void)
3053 6f3d1eb0 2018-03-12 stsp {
3054 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
3055 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
3056 6f3d1eb0 2018-03-12 stsp exit(1);
3057 b1ebc001 2019-08-13 stsp }
3058 b1ebc001 2019-08-13 stsp
3059 b1ebc001 2019-08-13 stsp static int
3060 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
3061 b1ebc001 2019-08-13 stsp {
3062 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
3063 b1ebc001 2019-08-13 stsp long long n;
3064 b1ebc001 2019-08-13 stsp const char *errstr;
3065 b1ebc001 2019-08-13 stsp
3066 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
3067 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
3068 b1ebc001 2019-08-13 stsp return 0;
3069 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
3070 b1ebc001 2019-08-13 stsp if (errstr != NULL)
3071 b1ebc001 2019-08-13 stsp return 0;
3072 b1ebc001 2019-08-13 stsp return n;
3073 6f3d1eb0 2018-03-12 stsp }
3074 6f3d1eb0 2018-03-12 stsp
3075 4ed7e80c 2018-05-20 stsp static const struct got_error *
3076 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
3077 f42b1b34 2018-03-12 stsp {
3078 f42b1b34 2018-03-12 stsp const struct got_error *error;
3079 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
3080 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
3081 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
3082 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
3083 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
3084 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
3085 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
3086 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
3087 64a96a6d 2018-04-01 stsp const char *errstr;
3088 199a4027 2019-02-02 stsp struct got_reflist_head refs;
3089 1b3893a2 2019-03-18 stsp
3090 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
3091 5c860e29 2018-03-12 stsp
3092 6715a751 2018-03-16 stsp #ifndef PROFILE
3093 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3094 6098196c 2019-01-04 stsp NULL)
3095 ad242220 2018-09-08 stsp == -1)
3096 f42b1b34 2018-03-12 stsp err(1, "pledge");
3097 6715a751 2018-03-16 stsp #endif
3098 79109fed 2018-03-27 stsp
3099 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
3100 b1ebc001 2019-08-13 stsp
3101 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
3102 79109fed 2018-03-27 stsp switch (ch) {
3103 79109fed 2018-03-27 stsp case 'p':
3104 79109fed 2018-03-27 stsp show_patch = 1;
3105 d142fc45 2018-04-01 stsp break;
3106 d142fc45 2018-04-01 stsp case 'c':
3107 d142fc45 2018-04-01 stsp start_commit = optarg;
3108 64a96a6d 2018-04-01 stsp break;
3109 c0cc5c62 2018-10-18 stsp case 'C':
3110 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3111 4a8520aa 2018-10-18 stsp &errstr);
3112 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3113 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3114 c0cc5c62 2018-10-18 stsp break;
3115 64a96a6d 2018-04-01 stsp case 'l':
3116 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
3117 64a96a6d 2018-04-01 stsp if (errstr != NULL)
3118 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
3119 cc54c501 2019-07-15 stsp break;
3120 48c8c60d 2020-01-27 stsp case 'b':
3121 48c8c60d 2020-01-27 stsp log_branches = 1;
3122 a0603db2 2018-06-10 stsp break;
3123 04ca23f4 2018-07-16 stsp case 'r':
3124 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
3125 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
3126 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3127 9ba1d308 2019-10-21 stsp optarg);
3128 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3129 04ca23f4 2018-07-16 stsp break;
3130 6841bf13 2019-11-29 kn case 's':
3131 6841bf13 2019-11-29 kn search_pattern = optarg;
3132 6841bf13 2019-11-29 kn break;
3133 79109fed 2018-03-27 stsp default:
3134 2deda0b9 2019-03-07 stsp usage_log();
3135 79109fed 2018-03-27 stsp /* NOTREACHED */
3136 79109fed 2018-03-27 stsp }
3137 79109fed 2018-03-27 stsp }
3138 79109fed 2018-03-27 stsp
3139 79109fed 2018-03-27 stsp argc -= optind;
3140 79109fed 2018-03-27 stsp argv += optind;
3141 f42b1b34 2018-03-12 stsp
3142 dc1edbfa 2019-11-29 kn if (diff_context == -1)
3143 dc1edbfa 2019-11-29 kn diff_context = 3;
3144 dc1edbfa 2019-11-29 kn else if (!show_patch)
3145 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
3146 dc1edbfa 2019-11-29 kn
3147 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
3148 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
3149 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3150 04ca23f4 2018-07-16 stsp goto done;
3151 04ca23f4 2018-07-16 stsp }
3152 cffc0aa4 2019-02-05 stsp
3153 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3154 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3155 cffc0aa4 2019-02-05 stsp goto done;
3156 cffc0aa4 2019-02-05 stsp error = NULL;
3157 cffc0aa4 2019-02-05 stsp
3158 e7301579 2019-03-18 stsp if (argc == 0) {
3159 cbd1af7a 2019-03-18 stsp path = strdup("");
3160 e7301579 2019-03-18 stsp if (path == NULL) {
3161 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3162 cbd1af7a 2019-03-18 stsp goto done;
3163 e7301579 2019-03-18 stsp }
3164 e7301579 2019-03-18 stsp } else if (argc == 1) {
3165 e7301579 2019-03-18 stsp if (worktree) {
3166 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3167 e7301579 2019-03-18 stsp argv[0]);
3168 e7301579 2019-03-18 stsp if (error)
3169 e7301579 2019-03-18 stsp goto done;
3170 e7301579 2019-03-18 stsp } else {
3171 e7301579 2019-03-18 stsp path = strdup(argv[0]);
3172 e7301579 2019-03-18 stsp if (path == NULL) {
3173 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3174 e7301579 2019-03-18 stsp goto done;
3175 e7301579 2019-03-18 stsp }
3176 e7301579 2019-03-18 stsp }
3177 cbd1af7a 2019-03-18 stsp } else
3178 cbd1af7a 2019-03-18 stsp usage_log();
3179 cbd1af7a 2019-03-18 stsp
3180 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
3181 5486daa2 2019-05-11 stsp repo_path = worktree ?
3182 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
3183 5486daa2 2019-05-11 stsp }
3184 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
3185 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3186 cffc0aa4 2019-02-05 stsp goto done;
3187 04ca23f4 2018-07-16 stsp }
3188 6098196c 2019-01-04 stsp
3189 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3190 d7d4f210 2018-03-12 stsp if (error != NULL)
3191 04ca23f4 2018-07-16 stsp goto done;
3192 f42b1b34 2018-03-12 stsp
3193 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3194 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3195 c02c541e 2019-03-29 stsp if (error)
3196 c02c541e 2019-03-29 stsp goto done;
3197 c02c541e 2019-03-29 stsp
3198 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
3199 3235492e 2018-04-01 stsp struct got_reference *head_ref;
3200 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
3201 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3202 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
3203 3235492e 2018-04-01 stsp if (error != NULL)
3204 3235492e 2018-04-01 stsp return error;
3205 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
3206 3235492e 2018-04-01 stsp got_ref_close(head_ref);
3207 3235492e 2018-04-01 stsp if (error != NULL)
3208 3235492e 2018-04-01 stsp return error;
3209 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3210 3235492e 2018-04-01 stsp } else {
3211 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
3212 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
3213 3235492e 2018-04-01 stsp if (error == NULL) {
3214 1caad61b 2019-02-01 stsp int obj_type;
3215 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
3216 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
3217 d1f2edc9 2018-06-13 stsp if (error != NULL)
3218 1caad61b 2019-02-01 stsp goto done;
3219 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
3220 1caad61b 2019-02-01 stsp if (error != NULL)
3221 1caad61b 2019-02-01 stsp goto done;
3222 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
3223 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
3224 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
3225 1caad61b 2019-02-01 stsp if (error != NULL)
3226 1caad61b 2019-02-01 stsp goto done;
3227 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
3228 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
3229 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3230 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3231 1caad61b 2019-02-01 stsp goto done;
3232 1caad61b 2019-02-01 stsp }
3233 1caad61b 2019-02-01 stsp free(id);
3234 1caad61b 2019-02-01 stsp id = got_object_id_dup(
3235 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
3236 1caad61b 2019-02-01 stsp if (id == NULL)
3237 638f9024 2019-05-13 stsp error = got_error_from_errno(
3238 230a42bd 2019-05-11 jcs "got_object_id_dup");
3239 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
3240 1caad61b 2019-02-01 stsp if (error)
3241 1caad61b 2019-02-01 stsp goto done;
3242 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
3243 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3244 1caad61b 2019-02-01 stsp goto done;
3245 1caad61b 2019-02-01 stsp }
3246 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
3247 d1f2edc9 2018-06-13 stsp if (error != NULL)
3248 1caad61b 2019-02-01 stsp goto done;
3249 d1f2edc9 2018-06-13 stsp }
3250 15a94983 2018-12-23 stsp if (commit == NULL) {
3251 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
3252 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
3253 d1f2edc9 2018-06-13 stsp if (error != NULL)
3254 d1f2edc9 2018-06-13 stsp return error;
3255 3235492e 2018-04-01 stsp }
3256 3235492e 2018-04-01 stsp }
3257 d7d4f210 2018-03-12 stsp if (error != NULL)
3258 04ca23f4 2018-07-16 stsp goto done;
3259 04ca23f4 2018-07-16 stsp
3260 deeabeae 2019-08-27 stsp if (worktree) {
3261 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3262 deeabeae 2019-08-27 stsp char *p;
3263 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
3264 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
3265 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
3266 deeabeae 2019-08-27 stsp goto done;
3267 deeabeae 2019-08-27 stsp }
3268 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3269 deeabeae 2019-08-27 stsp free(p);
3270 deeabeae 2019-08-27 stsp } else
3271 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3272 04ca23f4 2018-07-16 stsp if (error != NULL)
3273 04ca23f4 2018-07-16 stsp goto done;
3274 04ca23f4 2018-07-16 stsp if (in_repo_path) {
3275 04ca23f4 2018-07-16 stsp free(path);
3276 04ca23f4 2018-07-16 stsp path = in_repo_path;
3277 04ca23f4 2018-07-16 stsp }
3278 199a4027 2019-02-02 stsp
3279 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3280 199a4027 2019-02-02 stsp if (error)
3281 199a4027 2019-02-02 stsp goto done;
3282 04ca23f4 2018-07-16 stsp
3283 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
3284 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
3285 04ca23f4 2018-07-16 stsp done:
3286 04ca23f4 2018-07-16 stsp free(path);
3287 04ca23f4 2018-07-16 stsp free(repo_path);
3288 04ca23f4 2018-07-16 stsp free(cwd);
3289 f42b1b34 2018-03-12 stsp free(id);
3290 cffc0aa4 2019-02-05 stsp if (worktree)
3291 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
3292 ad242220 2018-09-08 stsp if (repo) {
3293 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3294 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3295 ad242220 2018-09-08 stsp if (error == NULL)
3296 ad242220 2018-09-08 stsp error = repo_error;
3297 ad242220 2018-09-08 stsp }
3298 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3299 8bf5b3c9 2018-03-17 stsp return error;
3300 5c860e29 2018-03-12 stsp }
3301 5c860e29 2018-03-12 stsp
3302 4ed7e80c 2018-05-20 stsp __dead static void
3303 3f8b7d6a 2018-04-01 stsp usage_diff(void)
3304 3f8b7d6a 2018-04-01 stsp {
3305 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
3306 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
3307 3f8b7d6a 2018-04-01 stsp exit(1);
3308 b00d56cd 2018-04-01 stsp }
3309 b00d56cd 2018-04-01 stsp
3310 b72f483a 2019-02-05 stsp struct print_diff_arg {
3311 b72f483a 2019-02-05 stsp struct got_repository *repo;
3312 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
3313 b72f483a 2019-02-05 stsp int diff_context;
3314 3fc0c068 2019-02-10 stsp const char *id_str;
3315 3fc0c068 2019-02-10 stsp int header_shown;
3316 98eaaa12 2019-08-03 stsp int diff_staged;
3317 63035f9f 2019-10-06 stsp int ignore_whitespace;
3318 b72f483a 2019-02-05 stsp };
3319 b72f483a 2019-02-05 stsp
3320 4ed7e80c 2018-05-20 stsp static const struct got_error *
3321 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
3322 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3323 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3324 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3325 b72f483a 2019-02-05 stsp {
3326 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
3327 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
3328 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
3329 12463d8b 2019-12-13 stsp int fd = -1;
3330 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
3331 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
3332 b72f483a 2019-02-05 stsp struct stat sb;
3333 b72f483a 2019-02-05 stsp
3334 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3335 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
3336 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
3337 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
3338 98eaaa12 2019-08-03 stsp return NULL;
3339 98eaaa12 2019-08-03 stsp } else {
3340 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
3341 98eaaa12 2019-08-03 stsp return NULL;
3342 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
3343 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
3344 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
3345 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
3346 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
3347 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
3348 98eaaa12 2019-08-03 stsp return NULL;
3349 98eaaa12 2019-08-03 stsp }
3350 3fc0c068 2019-02-10 stsp
3351 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
3352 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
3353 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
3354 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
3355 3fc0c068 2019-02-10 stsp a->header_shown = 1;
3356 3fc0c068 2019-02-10 stsp }
3357 b72f483a 2019-02-05 stsp
3358 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
3359 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
3360 98eaaa12 2019-08-03 stsp switch (staged_status) {
3361 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
3362 98eaaa12 2019-08-03 stsp label1 = path;
3363 98eaaa12 2019-08-03 stsp label2 = path;
3364 98eaaa12 2019-08-03 stsp break;
3365 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
3366 98eaaa12 2019-08-03 stsp label2 = path;
3367 98eaaa12 2019-08-03 stsp break;
3368 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
3369 98eaaa12 2019-08-03 stsp label1 = path;
3370 98eaaa12 2019-08-03 stsp break;
3371 98eaaa12 2019-08-03 stsp default:
3372 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
3373 98eaaa12 2019-08-03 stsp }
3374 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
3375 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
3376 63035f9f 2019-10-06 stsp a->repo, stdout);
3377 98eaaa12 2019-08-03 stsp }
3378 98eaaa12 2019-08-03 stsp
3379 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
3380 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
3381 4ce46740 2019-08-08 stsp char *id_str;
3382 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
3383 408b4ebc 2019-08-03 stsp 8192);
3384 4ce46740 2019-08-08 stsp if (err)
3385 4ce46740 2019-08-08 stsp goto done;
3386 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
3387 4ce46740 2019-08-08 stsp if (err)
3388 4ce46740 2019-08-08 stsp goto done;
3389 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
3390 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
3391 4ce46740 2019-08-08 stsp free(id_str);
3392 4ce46740 2019-08-08 stsp goto done;
3393 4ce46740 2019-08-08 stsp }
3394 4ce46740 2019-08-08 stsp free(id_str);
3395 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
3396 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
3397 4ce46740 2019-08-08 stsp if (err)
3398 4ce46740 2019-08-08 stsp goto done;
3399 4ce46740 2019-08-08 stsp }
3400 d00136be 2019-03-26 stsp
3401 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
3402 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
3403 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
3404 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3405 2ec1f75b 2019-03-26 stsp goto done;
3406 2ec1f75b 2019-03-26 stsp }
3407 b72f483a 2019-02-05 stsp
3408 12463d8b 2019-12-13 stsp if (dirfd != -1) {
3409 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
3410 12463d8b 2019-12-13 stsp if (fd == -1) {
3411 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
3412 12463d8b 2019-12-13 stsp goto done;
3413 12463d8b 2019-12-13 stsp }
3414 12463d8b 2019-12-13 stsp } else {
3415 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
3416 12463d8b 2019-12-13 stsp if (fd == -1) {
3417 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
3418 12463d8b 2019-12-13 stsp goto done;
3419 12463d8b 2019-12-13 stsp }
3420 12463d8b 2019-12-13 stsp }
3421 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
3422 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
3423 2ec1f75b 2019-03-26 stsp goto done;
3424 2ec1f75b 2019-03-26 stsp }
3425 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
3426 12463d8b 2019-12-13 stsp if (f2 == NULL) {
3427 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
3428 2ec1f75b 2019-03-26 stsp goto done;
3429 2ec1f75b 2019-03-26 stsp }
3430 12463d8b 2019-12-13 stsp fd = -1;
3431 2ec1f75b 2019-03-26 stsp } else
3432 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
3433 b72f483a 2019-02-05 stsp
3434 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
3435 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
3436 b72f483a 2019-02-05 stsp done:
3437 b72f483a 2019-02-05 stsp if (blob1)
3438 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
3439 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
3440 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
3441 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
3442 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
3443 b72f483a 2019-02-05 stsp free(abspath);
3444 927df6b7 2019-02-10 stsp return err;
3445 927df6b7 2019-02-10 stsp }
3446 927df6b7 2019-02-10 stsp
3447 927df6b7 2019-02-10 stsp static const struct got_error *
3448 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
3449 b00d56cd 2018-04-01 stsp {
3450 b00d56cd 2018-04-01 stsp const struct got_error *error;
3451 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
3452 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
3453 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
3454 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
3455 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
3456 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
3457 15a94983 2018-12-23 stsp int type1, type2;
3458 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
3459 c0cc5c62 2018-10-18 stsp const char *errstr;
3460 927df6b7 2019-02-10 stsp char *path = NULL;
3461 b00d56cd 2018-04-01 stsp
3462 b00d56cd 2018-04-01 stsp #ifndef PROFILE
3463 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3464 25eccc22 2019-01-04 stsp NULL) == -1)
3465 b00d56cd 2018-04-01 stsp err(1, "pledge");
3466 b00d56cd 2018-04-01 stsp #endif
3467 b00d56cd 2018-04-01 stsp
3468 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
3469 b00d56cd 2018-04-01 stsp switch (ch) {
3470 c0cc5c62 2018-10-18 stsp case 'C':
3471 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
3472 dfcab68b 2019-11-29 kn &errstr);
3473 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
3474 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
3475 c0cc5c62 2018-10-18 stsp break;
3476 b72f483a 2019-02-05 stsp case 'r':
3477 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
3478 b72f483a 2019-02-05 stsp if (repo_path == NULL)
3479 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3480 9ba1d308 2019-10-21 stsp optarg);
3481 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3482 b72f483a 2019-02-05 stsp break;
3483 98eaaa12 2019-08-03 stsp case 's':
3484 98eaaa12 2019-08-03 stsp diff_staged = 1;
3485 63035f9f 2019-10-06 stsp break;
3486 63035f9f 2019-10-06 stsp case 'w':
3487 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
3488 98eaaa12 2019-08-03 stsp break;
3489 b00d56cd 2018-04-01 stsp default:
3490 2deda0b9 2019-03-07 stsp usage_diff();
3491 b00d56cd 2018-04-01 stsp /* NOTREACHED */
3492 b00d56cd 2018-04-01 stsp }
3493 b00d56cd 2018-04-01 stsp }
3494 b00d56cd 2018-04-01 stsp
3495 b00d56cd 2018-04-01 stsp argc -= optind;
3496 b00d56cd 2018-04-01 stsp argv += optind;
3497 b00d56cd 2018-04-01 stsp
3498 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
3499 b72f483a 2019-02-05 stsp if (cwd == NULL) {
3500 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3501 b72f483a 2019-02-05 stsp goto done;
3502 b72f483a 2019-02-05 stsp }
3503 927df6b7 2019-02-10 stsp if (argc <= 1) {
3504 b72f483a 2019-02-05 stsp if (repo_path)
3505 b72f483a 2019-02-05 stsp errx(1,
3506 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
3507 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3508 1f03b8da 2020-03-20 stsp if (error)
3509 1f03b8da 2020-03-20 stsp goto done;
3510 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
3511 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
3512 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3513 927df6b7 2019-02-10 stsp goto done;
3514 927df6b7 2019-02-10 stsp }
3515 927df6b7 2019-02-10 stsp if (argc == 1) {
3516 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
3517 cbd1af7a 2019-03-18 stsp argv[0]);
3518 927df6b7 2019-02-10 stsp if (error)
3519 927df6b7 2019-02-10 stsp goto done;
3520 927df6b7 2019-02-10 stsp } else {
3521 927df6b7 2019-02-10 stsp path = strdup("");
3522 927df6b7 2019-02-10 stsp if (path == NULL) {
3523 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3524 927df6b7 2019-02-10 stsp goto done;
3525 927df6b7 2019-02-10 stsp }
3526 927df6b7 2019-02-10 stsp }
3527 b72f483a 2019-02-05 stsp } else if (argc == 2) {
3528 98eaaa12 2019-08-03 stsp if (diff_staged)
3529 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
3530 98eaaa12 2019-08-03 stsp "objects in repository");
3531 15a94983 2018-12-23 stsp id_str1 = argv[0];
3532 15a94983 2018-12-23 stsp id_str2 = argv[1];
3533 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3534 1f03b8da 2020-03-20 stsp error = got_worktree_open(&worktree, cwd);
3535 1f03b8da 2020-03-20 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3536 30db809c 2019-06-05 stsp goto done;
3537 1f03b8da 2020-03-20 stsp if (worktree) {
3538 1f03b8da 2020-03-20 stsp repo_path = strdup(
3539 1f03b8da 2020-03-20 stsp got_worktree_get_repo_path(worktree));
3540 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3541 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3542 1f03b8da 2020-03-20 stsp goto done;
3543 1f03b8da 2020-03-20 stsp }
3544 1f03b8da 2020-03-20 stsp } else {
3545 1f03b8da 2020-03-20 stsp repo_path = strdup(cwd);
3546 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3547 1f03b8da 2020-03-20 stsp error = got_error_from_errno("strdup");
3548 1f03b8da 2020-03-20 stsp goto done;
3549 1f03b8da 2020-03-20 stsp }
3550 30db809c 2019-06-05 stsp }
3551 30db809c 2019-06-05 stsp }
3552 b00d56cd 2018-04-01 stsp } else
3553 b00d56cd 2018-04-01 stsp usage_diff();
3554 b00d56cd 2018-04-01 stsp
3555 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3556 76089277 2018-04-01 stsp free(repo_path);
3557 b00d56cd 2018-04-01 stsp if (error != NULL)
3558 b00d56cd 2018-04-01 stsp goto done;
3559 b00d56cd 2018-04-01 stsp
3560 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3561 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3562 c02c541e 2019-03-29 stsp if (error)
3563 c02c541e 2019-03-29 stsp goto done;
3564 c02c541e 2019-03-29 stsp
3565 30db809c 2019-06-05 stsp if (argc <= 1) {
3566 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
3567 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3568 b72f483a 2019-02-05 stsp char *id_str;
3569 72ea6654 2019-07-27 stsp
3570 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3571 72ea6654 2019-07-27 stsp
3572 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
3573 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
3574 b72f483a 2019-02-05 stsp if (error)
3575 b72f483a 2019-02-05 stsp goto done;
3576 b72f483a 2019-02-05 stsp arg.repo = repo;
3577 b72f483a 2019-02-05 stsp arg.worktree = worktree;
3578 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
3579 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
3580 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
3581 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
3582 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
3583 b72f483a 2019-02-05 stsp
3584 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
3585 72ea6654 2019-07-27 stsp if (error)
3586 72ea6654 2019-07-27 stsp goto done;
3587 72ea6654 2019-07-27 stsp
3588 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
3589 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
3590 b72f483a 2019-02-05 stsp free(id_str);
3591 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3592 b72f483a 2019-02-05 stsp goto done;
3593 b72f483a 2019-02-05 stsp }
3594 b72f483a 2019-02-05 stsp
3595 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
3596 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3597 0adb7196 2019-08-11 stsp if (error)
3598 0adb7196 2019-08-11 stsp goto done;
3599 b00d56cd 2018-04-01 stsp
3600 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
3601 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
3602 0adb7196 2019-08-11 stsp if (error)
3603 0adb7196 2019-08-11 stsp goto done;
3604 b00d56cd 2018-04-01 stsp
3605 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
3606 15a94983 2018-12-23 stsp if (error)
3607 15a94983 2018-12-23 stsp goto done;
3608 15a94983 2018-12-23 stsp
3609 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
3610 15a94983 2018-12-23 stsp if (error)
3611 15a94983 2018-12-23 stsp goto done;
3612 15a94983 2018-12-23 stsp
3613 15a94983 2018-12-23 stsp if (type1 != type2) {
3614 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3615 b00d56cd 2018-04-01 stsp goto done;
3616 b00d56cd 2018-04-01 stsp }
3617 b00d56cd 2018-04-01 stsp
3618 15a94983 2018-12-23 stsp switch (type1) {
3619 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
3620 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
3621 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3622 b00d56cd 2018-04-01 stsp break;
3623 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
3624 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
3625 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
3626 b00d56cd 2018-04-01 stsp break;
3627 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
3628 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
3629 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
3630 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
3631 b00d56cd 2018-04-01 stsp break;
3632 b00d56cd 2018-04-01 stsp default:
3633 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3634 b00d56cd 2018-04-01 stsp }
3635 b00d56cd 2018-04-01 stsp done:
3636 5e70831e 2019-05-28 stsp free(label1);
3637 5e70831e 2019-05-28 stsp free(label2);
3638 15a94983 2018-12-23 stsp free(id1);
3639 15a94983 2018-12-23 stsp free(id2);
3640 927df6b7 2019-02-10 stsp free(path);
3641 b72f483a 2019-02-05 stsp if (worktree)
3642 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
3643 ad242220 2018-09-08 stsp if (repo) {
3644 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3645 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3646 ad242220 2018-09-08 stsp if (error == NULL)
3647 ad242220 2018-09-08 stsp error = repo_error;
3648 ad242220 2018-09-08 stsp }
3649 b00d56cd 2018-04-01 stsp return error;
3650 404c43c4 2018-06-21 stsp }
3651 404c43c4 2018-06-21 stsp
3652 404c43c4 2018-06-21 stsp __dead static void
3653 404c43c4 2018-06-21 stsp usage_blame(void)
3654 404c43c4 2018-06-21 stsp {
3655 9270e621 2019-02-05 stsp fprintf(stderr,
3656 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
3657 404c43c4 2018-06-21 stsp getprogname());
3658 404c43c4 2018-06-21 stsp exit(1);
3659 b00d56cd 2018-04-01 stsp }
3660 b00d56cd 2018-04-01 stsp
3661 28315671 2019-08-14 stsp struct blame_line {
3662 28315671 2019-08-14 stsp int annotated;
3663 28315671 2019-08-14 stsp char *id_str;
3664 82f6abb8 2019-08-14 stsp char *committer;
3665 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
3666 28315671 2019-08-14 stsp };
3667 28315671 2019-08-14 stsp
3668 28315671 2019-08-14 stsp struct blame_cb_args {
3669 28315671 2019-08-14 stsp struct blame_line *lines;
3670 28315671 2019-08-14 stsp int nlines;
3671 7ef28ff8 2019-08-14 stsp int nlines_prec;
3672 28315671 2019-08-14 stsp int lineno_cur;
3673 28315671 2019-08-14 stsp off_t *line_offsets;
3674 28315671 2019-08-14 stsp FILE *f;
3675 82f6abb8 2019-08-14 stsp struct got_repository *repo;
3676 28315671 2019-08-14 stsp };
3677 28315671 2019-08-14 stsp
3678 404c43c4 2018-06-21 stsp static const struct got_error *
3679 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
3680 28315671 2019-08-14 stsp {
3681 28315671 2019-08-14 stsp const struct got_error *err = NULL;
3682 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
3683 28315671 2019-08-14 stsp struct blame_line *bline;
3684 82f6abb8 2019-08-14 stsp char *line = NULL;
3685 28315671 2019-08-14 stsp size_t linesize = 0;
3686 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
3687 28315671 2019-08-14 stsp off_t offset;
3688 bcb49d15 2019-08-14 stsp struct tm tm;
3689 bcb49d15 2019-08-14 stsp time_t committer_time;
3690 28315671 2019-08-14 stsp
3691 28315671 2019-08-14 stsp if (nlines != a->nlines ||
3692 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
3693 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
3694 28315671 2019-08-14 stsp
3695 28315671 2019-08-14 stsp if (sigint_received)
3696 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
3697 28315671 2019-08-14 stsp
3698 2839f8b9 2019-08-18 stsp if (lineno == -1)
3699 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
3700 2839f8b9 2019-08-18 stsp
3701 28315671 2019-08-14 stsp /* Annotate this line. */
3702 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
3703 28315671 2019-08-14 stsp if (bline->annotated)
3704 28315671 2019-08-14 stsp return NULL;
3705 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
3706 28315671 2019-08-14 stsp if (err)
3707 28315671 2019-08-14 stsp return err;
3708 82f6abb8 2019-08-14 stsp
3709 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
3710 82f6abb8 2019-08-14 stsp if (err)
3711 82f6abb8 2019-08-14 stsp goto done;
3712 82f6abb8 2019-08-14 stsp
3713 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
3714 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
3715 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
3716 bcb49d15 2019-08-14 stsp goto done;
3717 bcb49d15 2019-08-14 stsp }
3718 bcb49d15 2019-08-14 stsp
3719 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
3720 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
3721 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
3722 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
3723 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
3724 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
3725 82f6abb8 2019-08-14 stsp goto done;
3726 82f6abb8 2019-08-14 stsp }
3727 28315671 2019-08-14 stsp bline->annotated = 1;
3728 28315671 2019-08-14 stsp
3729 28315671 2019-08-14 stsp /* Print lines annotated so far. */
3730 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3731 28315671 2019-08-14 stsp if (!bline->annotated)
3732 82f6abb8 2019-08-14 stsp goto done;
3733 28315671 2019-08-14 stsp
3734 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
3735 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
3736 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
3737 82f6abb8 2019-08-14 stsp goto done;
3738 82f6abb8 2019-08-14 stsp }
3739 28315671 2019-08-14 stsp
3740 28315671 2019-08-14 stsp while (bline->annotated) {
3741 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
3742 82f6abb8 2019-08-14 stsp size_t len;
3743 82f6abb8 2019-08-14 stsp
3744 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
3745 28315671 2019-08-14 stsp if (ferror(a->f))
3746 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
3747 28315671 2019-08-14 stsp break;
3748 28315671 2019-08-14 stsp }
3749 82f6abb8 2019-08-14 stsp
3750 82f6abb8 2019-08-14 stsp committer = bline->committer;
3751 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
3752 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
3753 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
3754 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
3755 82f6abb8 2019-08-14 stsp if (at)
3756 82f6abb8 2019-08-14 stsp *at = '\0';
3757 82f6abb8 2019-08-14 stsp len = strlen(committer);
3758 82f6abb8 2019-08-14 stsp if (len >= 9)
3759 82f6abb8 2019-08-14 stsp committer[8] = '\0';
3760 28315671 2019-08-14 stsp
3761 28315671 2019-08-14 stsp nl = strchr(line, '\n');
3762 28315671 2019-08-14 stsp if (nl)
3763 28315671 2019-08-14 stsp *nl = '\0';
3764 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
3765 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
3766 28315671 2019-08-14 stsp
3767 28315671 2019-08-14 stsp a->lineno_cur++;
3768 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
3769 28315671 2019-08-14 stsp }
3770 82f6abb8 2019-08-14 stsp done:
3771 82f6abb8 2019-08-14 stsp if (commit)
3772 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
3773 28315671 2019-08-14 stsp free(line);
3774 28315671 2019-08-14 stsp return err;
3775 28315671 2019-08-14 stsp }
3776 28315671 2019-08-14 stsp
3777 28315671 2019-08-14 stsp static const struct got_error *
3778 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
3779 404c43c4 2018-06-21 stsp {
3780 404c43c4 2018-06-21 stsp const struct got_error *error;
3781 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
3782 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
3783 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3784 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
3785 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
3786 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
3787 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
3788 28315671 2019-08-14 stsp struct blame_cb_args bca;
3789 28315671 2019-08-14 stsp int ch, obj_type, i;
3790 b02560ec 2019-08-19 stsp size_t filesize;
3791 90f3c347 2019-08-19 stsp
3792 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
3793 404c43c4 2018-06-21 stsp
3794 404c43c4 2018-06-21 stsp #ifndef PROFILE
3795 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3796 36e2fb66 2019-01-04 stsp NULL) == -1)
3797 404c43c4 2018-06-21 stsp err(1, "pledge");
3798 404c43c4 2018-06-21 stsp #endif
3799 404c43c4 2018-06-21 stsp
3800 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
3801 404c43c4 2018-06-21 stsp switch (ch) {
3802 404c43c4 2018-06-21 stsp case 'c':
3803 404c43c4 2018-06-21 stsp commit_id_str = optarg;
3804 404c43c4 2018-06-21 stsp break;
3805 66bea077 2018-08-02 stsp case 'r':
3806 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
3807 66bea077 2018-08-02 stsp if (repo_path == NULL)
3808 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3809 9ba1d308 2019-10-21 stsp optarg);
3810 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3811 66bea077 2018-08-02 stsp break;
3812 404c43c4 2018-06-21 stsp default:
3813 2deda0b9 2019-03-07 stsp usage_blame();
3814 404c43c4 2018-06-21 stsp /* NOTREACHED */
3815 404c43c4 2018-06-21 stsp }
3816 404c43c4 2018-06-21 stsp }
3817 404c43c4 2018-06-21 stsp
3818 404c43c4 2018-06-21 stsp argc -= optind;
3819 404c43c4 2018-06-21 stsp argv += optind;
3820 404c43c4 2018-06-21 stsp
3821 a39318fd 2018-08-02 stsp if (argc == 1)
3822 404c43c4 2018-06-21 stsp path = argv[0];
3823 a39318fd 2018-08-02 stsp else
3824 404c43c4 2018-06-21 stsp usage_blame();
3825 404c43c4 2018-06-21 stsp
3826 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
3827 66bea077 2018-08-02 stsp if (cwd == NULL) {
3828 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3829 66bea077 2018-08-02 stsp goto done;
3830 66bea077 2018-08-02 stsp }
3831 66bea077 2018-08-02 stsp if (repo_path == NULL) {
3832 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3833 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3834 66bea077 2018-08-02 stsp goto done;
3835 0c06baac 2019-02-05 stsp else
3836 0c06baac 2019-02-05 stsp error = NULL;
3837 0c06baac 2019-02-05 stsp if (worktree) {
3838 0c06baac 2019-02-05 stsp repo_path =
3839 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3840 1f03b8da 2020-03-20 stsp if (repo_path == NULL) {
3841 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3842 1f03b8da 2020-03-20 stsp if (error)
3843 1f03b8da 2020-03-20 stsp goto done;
3844 1f03b8da 2020-03-20 stsp }
3845 0c06baac 2019-02-05 stsp } else {
3846 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
3847 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
3848 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3849 0c06baac 2019-02-05 stsp goto done;
3850 0c06baac 2019-02-05 stsp }
3851 66bea077 2018-08-02 stsp }
3852 66bea077 2018-08-02 stsp }
3853 36e2fb66 2019-01-04 stsp
3854 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3855 c02c541e 2019-03-29 stsp if (error != NULL)
3856 36e2fb66 2019-01-04 stsp goto done;
3857 66bea077 2018-08-02 stsp
3858 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3859 c02c541e 2019-03-29 stsp if (error)
3860 404c43c4 2018-06-21 stsp goto done;
3861 404c43c4 2018-06-21 stsp
3862 0c06baac 2019-02-05 stsp if (worktree) {
3863 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
3864 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3865 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3866 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
3867 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
3868 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
3869 6efaaa2d 2019-02-05 stsp path) == -1) {
3870 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3871 0c06baac 2019-02-05 stsp goto done;
3872 0c06baac 2019-02-05 stsp }
3873 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3874 0c06baac 2019-02-05 stsp free(p);
3875 0c06baac 2019-02-05 stsp } else {
3876 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3877 0c06baac 2019-02-05 stsp }
3878 0c06baac 2019-02-05 stsp if (error)
3879 66bea077 2018-08-02 stsp goto done;
3880 66bea077 2018-08-02 stsp
3881 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
3882 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
3883 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
3884 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
3885 404c43c4 2018-06-21 stsp if (error != NULL)
3886 66bea077 2018-08-02 stsp goto done;
3887 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3888 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
3889 404c43c4 2018-06-21 stsp if (error != NULL)
3890 66bea077 2018-08-02 stsp goto done;
3891 404c43c4 2018-06-21 stsp } else {
3892 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3893 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3894 30837e32 2019-07-25 stsp if (error)
3895 66bea077 2018-08-02 stsp goto done;
3896 404c43c4 2018-06-21 stsp }
3897 404c43c4 2018-06-21 stsp
3898 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
3899 28315671 2019-08-14 stsp if (error)
3900 28315671 2019-08-14 stsp goto done;
3901 28315671 2019-08-14 stsp
3902 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
3903 28315671 2019-08-14 stsp if (error)
3904 28315671 2019-08-14 stsp goto done;
3905 28315671 2019-08-14 stsp
3906 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
3907 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
3908 28315671 2019-08-14 stsp goto done;
3909 28315671 2019-08-14 stsp }
3910 28315671 2019-08-14 stsp
3911 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
3912 28315671 2019-08-14 stsp if (error)
3913 28315671 2019-08-14 stsp goto done;
3914 28315671 2019-08-14 stsp bca.f = got_opentemp();
3915 28315671 2019-08-14 stsp if (bca.f == NULL) {
3916 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
3917 28315671 2019-08-14 stsp goto done;
3918 28315671 2019-08-14 stsp }
3919 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
3920 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
3921 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
3922 28315671 2019-08-14 stsp goto done;
3923 28315671 2019-08-14 stsp
3924 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
3925 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
3926 b02560ec 2019-08-19 stsp bca.nlines--;
3927 b02560ec 2019-08-19 stsp
3928 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
3929 28315671 2019-08-14 stsp if (bca.lines == NULL) {
3930 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
3931 28315671 2019-08-14 stsp goto done;
3932 28315671 2019-08-14 stsp }
3933 28315671 2019-08-14 stsp bca.lineno_cur = 1;
3934 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
3935 7ef28ff8 2019-08-14 stsp i = bca.nlines;
3936 7ef28ff8 2019-08-14 stsp while (i > 0) {
3937 7ef28ff8 2019-08-14 stsp i /= 10;
3938 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
3939 7ef28ff8 2019-08-14 stsp }
3940 82f6abb8 2019-08-14 stsp bca.repo = repo;
3941 7ef28ff8 2019-08-14 stsp
3942 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
3943 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
3944 404c43c4 2018-06-21 stsp done:
3945 66bea077 2018-08-02 stsp free(in_repo_path);
3946 66bea077 2018-08-02 stsp free(repo_path);
3947 66bea077 2018-08-02 stsp free(cwd);
3948 404c43c4 2018-06-21 stsp free(commit_id);
3949 28315671 2019-08-14 stsp free(obj_id);
3950 28315671 2019-08-14 stsp if (blob)
3951 28315671 2019-08-14 stsp got_object_blob_close(blob);
3952 0c06baac 2019-02-05 stsp if (worktree)
3953 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
3954 ad242220 2018-09-08 stsp if (repo) {
3955 ad242220 2018-09-08 stsp const struct got_error *repo_error;
3956 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
3957 ad242220 2018-09-08 stsp if (error == NULL)
3958 ad242220 2018-09-08 stsp error = repo_error;
3959 ad242220 2018-09-08 stsp }
3960 3affba96 2019-09-22 stsp if (bca.lines) {
3961 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
3962 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
3963 3affba96 2019-09-22 stsp free(bline->id_str);
3964 3affba96 2019-09-22 stsp free(bline->committer);
3965 3affba96 2019-09-22 stsp }
3966 3affba96 2019-09-22 stsp free(bca.lines);
3967 28315671 2019-08-14 stsp }
3968 28315671 2019-08-14 stsp free(bca.line_offsets);
3969 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
3970 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
3971 404c43c4 2018-06-21 stsp return error;
3972 5de5890b 2018-10-18 stsp }
3973 5de5890b 2018-10-18 stsp
3974 5de5890b 2018-10-18 stsp __dead static void
3975 5de5890b 2018-10-18 stsp usage_tree(void)
3976 5de5890b 2018-10-18 stsp {
3977 c1669e2e 2019-01-09 stsp fprintf(stderr,
3978 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
3979 5de5890b 2018-10-18 stsp getprogname());
3980 5de5890b 2018-10-18 stsp exit(1);
3981 5de5890b 2018-10-18 stsp }
3982 5de5890b 2018-10-18 stsp
3983 c1669e2e 2019-01-09 stsp static void
3984 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
3985 c1669e2e 2019-01-09 stsp const char *root_path)
3986 c1669e2e 2019-01-09 stsp {
3987 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
3988 848d6979 2019-08-12 stsp const char *modestr = "";
3989 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
3990 5de5890b 2018-10-18 stsp
3991 c1669e2e 2019-01-09 stsp path += strlen(root_path);
3992 c1669e2e 2019-01-09 stsp while (path[0] == '/')
3993 c1669e2e 2019-01-09 stsp path++;
3994 c1669e2e 2019-01-09 stsp
3995 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
3996 63c5ca5d 2019-08-24 stsp modestr = "$";
3997 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
3998 848d6979 2019-08-12 stsp modestr = "@";
3999 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
4000 848d6979 2019-08-12 stsp modestr = "/";
4001 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
4002 848d6979 2019-08-12 stsp modestr = "*";
4003 848d6979 2019-08-12 stsp
4004 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
4005 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
4006 c1669e2e 2019-01-09 stsp }
4007 c1669e2e 2019-01-09 stsp
4008 5de5890b 2018-10-18 stsp static const struct got_error *
4009 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
4010 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
4011 c1669e2e 2019-01-09 stsp struct got_repository *repo)
4012 5de5890b 2018-10-18 stsp {
4013 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
4014 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
4015 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
4016 56e0773d 2019-11-28 stsp int nentries, i;
4017 5de5890b 2018-10-18 stsp
4018 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
4019 5de5890b 2018-10-18 stsp if (err)
4020 5de5890b 2018-10-18 stsp goto done;
4021 5de5890b 2018-10-18 stsp
4022 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
4023 5de5890b 2018-10-18 stsp if (err)
4024 5de5890b 2018-10-18 stsp goto done;
4025 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
4026 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
4027 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
4028 5de5890b 2018-10-18 stsp char *id = NULL;
4029 84453469 2018-11-11 stsp
4030 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
4031 84453469 2018-11-11 stsp break;
4032 84453469 2018-11-11 stsp
4033 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
4034 5de5890b 2018-10-18 stsp if (show_ids) {
4035 5de5890b 2018-10-18 stsp char *id_str;
4036 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
4037 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
4038 5de5890b 2018-10-18 stsp if (err)
4039 5de5890b 2018-10-18 stsp goto done;
4040 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
4041 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4042 5de5890b 2018-10-18 stsp free(id_str);
4043 5de5890b 2018-10-18 stsp goto done;
4044 5de5890b 2018-10-18 stsp }
4045 5de5890b 2018-10-18 stsp free(id_str);
4046 5de5890b 2018-10-18 stsp }
4047 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
4048 5de5890b 2018-10-18 stsp free(id);
4049 c1669e2e 2019-01-09 stsp
4050 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
4051 c1669e2e 2019-01-09 stsp char *child_path;
4052 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
4053 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
4054 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
4055 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
4056 c1669e2e 2019-01-09 stsp goto done;
4057 c1669e2e 2019-01-09 stsp }
4058 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
4059 c1669e2e 2019-01-09 stsp root_path, repo);
4060 c1669e2e 2019-01-09 stsp free(child_path);
4061 c1669e2e 2019-01-09 stsp if (err)
4062 c1669e2e 2019-01-09 stsp goto done;
4063 c1669e2e 2019-01-09 stsp }
4064 5de5890b 2018-10-18 stsp }
4065 5de5890b 2018-10-18 stsp done:
4066 5de5890b 2018-10-18 stsp if (tree)
4067 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
4068 5de5890b 2018-10-18 stsp free(tree_id);
4069 5de5890b 2018-10-18 stsp return err;
4070 404c43c4 2018-06-21 stsp }
4071 404c43c4 2018-06-21 stsp
4072 5de5890b 2018-10-18 stsp static const struct got_error *
4073 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
4074 5de5890b 2018-10-18 stsp {
4075 5de5890b 2018-10-18 stsp const struct got_error *error;
4076 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
4077 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
4078 4e0a20a4 2020-03-23 tracey const char *path, *refname = NULL;
4079 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
4080 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
4081 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
4082 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
4083 5de5890b 2018-10-18 stsp int ch;
4084 5de5890b 2018-10-18 stsp
4085 5de5890b 2018-10-18 stsp #ifndef PROFILE
4086 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4087 0f8d269b 2019-01-04 stsp NULL) == -1)
4088 5de5890b 2018-10-18 stsp err(1, "pledge");
4089 5de5890b 2018-10-18 stsp #endif
4090 5de5890b 2018-10-18 stsp
4091 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
4092 5de5890b 2018-10-18 stsp switch (ch) {
4093 5de5890b 2018-10-18 stsp case 'c':
4094 5de5890b 2018-10-18 stsp commit_id_str = optarg;
4095 5de5890b 2018-10-18 stsp break;
4096 5de5890b 2018-10-18 stsp case 'r':
4097 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
4098 5de5890b 2018-10-18 stsp if (repo_path == NULL)
4099 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4100 9ba1d308 2019-10-21 stsp optarg);
4101 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4102 5de5890b 2018-10-18 stsp break;
4103 5de5890b 2018-10-18 stsp case 'i':
4104 5de5890b 2018-10-18 stsp show_ids = 1;
4105 5de5890b 2018-10-18 stsp break;
4106 c1669e2e 2019-01-09 stsp case 'R':
4107 c1669e2e 2019-01-09 stsp recurse = 1;
4108 c1669e2e 2019-01-09 stsp break;
4109 5de5890b 2018-10-18 stsp default:
4110 2deda0b9 2019-03-07 stsp usage_tree();
4111 5de5890b 2018-10-18 stsp /* NOTREACHED */
4112 5de5890b 2018-10-18 stsp }
4113 5de5890b 2018-10-18 stsp }
4114 5de5890b 2018-10-18 stsp
4115 5de5890b 2018-10-18 stsp argc -= optind;
4116 5de5890b 2018-10-18 stsp argv += optind;
4117 5de5890b 2018-10-18 stsp
4118 5de5890b 2018-10-18 stsp if (argc == 1)
4119 5de5890b 2018-10-18 stsp path = argv[0];
4120 5de5890b 2018-10-18 stsp else if (argc > 1)
4121 5de5890b 2018-10-18 stsp usage_tree();
4122 5de5890b 2018-10-18 stsp else
4123 7a2c19d6 2019-02-05 stsp path = NULL;
4124 7a2c19d6 2019-02-05 stsp
4125 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
4126 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
4127 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4128 9bf7a39b 2019-02-05 stsp goto done;
4129 9bf7a39b 2019-02-05 stsp }
4130 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
4131 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
4132 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4133 7a2c19d6 2019-02-05 stsp goto done;
4134 7a2c19d6 2019-02-05 stsp else
4135 7a2c19d6 2019-02-05 stsp error = NULL;
4136 7a2c19d6 2019-02-05 stsp if (worktree) {
4137 7a2c19d6 2019-02-05 stsp repo_path =
4138 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
4139 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
4140 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4141 7a2c19d6 2019-02-05 stsp if (error)
4142 7a2c19d6 2019-02-05 stsp goto done;
4143 7a2c19d6 2019-02-05 stsp } else {
4144 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
4145 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
4146 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4147 7a2c19d6 2019-02-05 stsp goto done;
4148 7a2c19d6 2019-02-05 stsp }
4149 5de5890b 2018-10-18 stsp }
4150 5de5890b 2018-10-18 stsp }
4151 5de5890b 2018-10-18 stsp
4152 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4153 5de5890b 2018-10-18 stsp if (error != NULL)
4154 c02c541e 2019-03-29 stsp goto done;
4155 c02c541e 2019-03-29 stsp
4156 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4157 c02c541e 2019-03-29 stsp if (error)
4158 5de5890b 2018-10-18 stsp goto done;
4159 5de5890b 2018-10-18 stsp
4160 9bf7a39b 2019-02-05 stsp if (path == NULL) {
4161 9bf7a39b 2019-02-05 stsp if (worktree) {
4162 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
4163 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
4164 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
4165 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
4166 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
4167 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
4168 9bf7a39b 2019-02-05 stsp goto done;
4169 9bf7a39b 2019-02-05 stsp }
4170 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
4171 9bf7a39b 2019-02-05 stsp free(p);
4172 9bf7a39b 2019-02-05 stsp if (error)
4173 9bf7a39b 2019-02-05 stsp goto done;
4174 9bf7a39b 2019-02-05 stsp } else
4175 9bf7a39b 2019-02-05 stsp path = "/";
4176 9bf7a39b 2019-02-05 stsp }
4177 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
4178 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
4179 9bf7a39b 2019-02-05 stsp if (error != NULL)
4180 9bf7a39b 2019-02-05 stsp goto done;
4181 9bf7a39b 2019-02-05 stsp }
4182 5de5890b 2018-10-18 stsp
4183 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
4184 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
4185 4e0a20a4 2020-03-23 tracey if (worktree)
4186 4e0a20a4 2020-03-23 tracey refname = got_worktree_get_head_ref_name(worktree);
4187 4e0a20a4 2020-03-23 tracey else
4188 4e0a20a4 2020-03-23 tracey refname = GOT_REF_HEAD;
4189 4e0a20a4 2020-03-23 tracey error = got_ref_open(&head_ref, repo, refname, 0);
4190 5de5890b 2018-10-18 stsp if (error != NULL)
4191 5de5890b 2018-10-18 stsp goto done;
4192 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4193 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
4194 5de5890b 2018-10-18 stsp if (error != NULL)
4195 5de5890b 2018-10-18 stsp goto done;
4196 5de5890b 2018-10-18 stsp } else {
4197 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4198 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
4199 30837e32 2019-07-25 stsp if (error)
4200 5de5890b 2018-10-18 stsp goto done;
4201 5de5890b 2018-10-18 stsp }
4202 5de5890b 2018-10-18 stsp
4203 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
4204 c1669e2e 2019-01-09 stsp in_repo_path, repo);
4205 5de5890b 2018-10-18 stsp done:
4206 5de5890b 2018-10-18 stsp free(in_repo_path);
4207 5de5890b 2018-10-18 stsp free(repo_path);
4208 5de5890b 2018-10-18 stsp free(cwd);
4209 5de5890b 2018-10-18 stsp free(commit_id);
4210 7a2c19d6 2019-02-05 stsp if (worktree)
4211 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
4212 5de5890b 2018-10-18 stsp if (repo) {
4213 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
4214 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
4215 5de5890b 2018-10-18 stsp if (error == NULL)
4216 5de5890b 2018-10-18 stsp error = repo_error;
4217 5de5890b 2018-10-18 stsp }
4218 5de5890b 2018-10-18 stsp return error;
4219 5de5890b 2018-10-18 stsp }
4220 5de5890b 2018-10-18 stsp
4221 6bad629b 2019-02-04 stsp __dead static void
4222 6bad629b 2019-02-04 stsp usage_status(void)
4223 6bad629b 2019-02-04 stsp {
4224 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
4225 6bad629b 2019-02-04 stsp exit(1);
4226 6bad629b 2019-02-04 stsp }
4227 5c860e29 2018-03-12 stsp
4228 b72f483a 2019-02-05 stsp static const struct got_error *
4229 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
4230 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
4231 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
4232 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
4233 6bad629b 2019-02-04 stsp {
4234 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4235 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
4236 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
4237 b72f483a 2019-02-05 stsp return NULL;
4238 6bad629b 2019-02-04 stsp }
4239 5c860e29 2018-03-12 stsp
4240 6bad629b 2019-02-04 stsp static const struct got_error *
4241 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
4242 6bad629b 2019-02-04 stsp {
4243 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
4244 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
4245 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
4246 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
4247 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
4248 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
4249 a5edda0a 2019-07-27 stsp int ch;
4250 5c860e29 2018-03-12 stsp
4251 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
4252 72ea6654 2019-07-27 stsp
4253 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4254 6bad629b 2019-02-04 stsp switch (ch) {
4255 5c860e29 2018-03-12 stsp default:
4256 2deda0b9 2019-03-07 stsp usage_status();
4257 6bad629b 2019-02-04 stsp /* NOTREACHED */
4258 5c860e29 2018-03-12 stsp }
4259 5c860e29 2018-03-12 stsp }
4260 5c860e29 2018-03-12 stsp
4261 6bad629b 2019-02-04 stsp argc -= optind;
4262 6bad629b 2019-02-04 stsp argv += optind;
4263 5c860e29 2018-03-12 stsp
4264 6bad629b 2019-02-04 stsp #ifndef PROFILE
4265 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4266 6bad629b 2019-02-04 stsp NULL) == -1)
4267 6bad629b 2019-02-04 stsp err(1, "pledge");
4268 f42b1b34 2018-03-12 stsp #endif
4269 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
4270 927df6b7 2019-02-10 stsp if (cwd == NULL) {
4271 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4272 927df6b7 2019-02-10 stsp goto done;
4273 927df6b7 2019-02-10 stsp }
4274 927df6b7 2019-02-10 stsp
4275 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
4276 927df6b7 2019-02-10 stsp if (error != NULL)
4277 a5edda0a 2019-07-27 stsp goto done;
4278 6bad629b 2019-02-04 stsp
4279 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4280 c9956ddf 2019-09-08 stsp NULL);
4281 6bad629b 2019-02-04 stsp if (error != NULL)
4282 6bad629b 2019-02-04 stsp goto done;
4283 6bad629b 2019-02-04 stsp
4284 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4285 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4286 087fb88c 2019-08-04 stsp if (error)
4287 087fb88c 2019-08-04 stsp goto done;
4288 087fb88c 2019-08-04 stsp
4289 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4290 6bad629b 2019-02-04 stsp if (error)
4291 6bad629b 2019-02-04 stsp goto done;
4292 6bad629b 2019-02-04 stsp
4293 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
4294 6bad629b 2019-02-04 stsp check_cancelled, NULL);
4295 6bad629b 2019-02-04 stsp done:
4296 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
4297 a5edda0a 2019-07-27 stsp free((char *)pe->path);
4298 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
4299 927df6b7 2019-02-10 stsp free(cwd);
4300 d0eebce4 2019-03-11 stsp return error;
4301 d0eebce4 2019-03-11 stsp }
4302 d0eebce4 2019-03-11 stsp
4303 d0eebce4 2019-03-11 stsp __dead static void
4304 d0eebce4 2019-03-11 stsp usage_ref(void)
4305 d0eebce4 2019-03-11 stsp {
4306 d0eebce4 2019-03-11 stsp fprintf(stderr,
4307 e31abbf2 2020-03-22 stsp "usage: %s ref [-r repository] [-l] [-c object] [-s reference] "
4308 e31abbf2 2020-03-22 stsp "[-d] [name]\n",
4309 d0eebce4 2019-03-11 stsp getprogname());
4310 d0eebce4 2019-03-11 stsp exit(1);
4311 d0eebce4 2019-03-11 stsp }
4312 d0eebce4 2019-03-11 stsp
4313 d0eebce4 2019-03-11 stsp static const struct got_error *
4314 b2070a3f 2020-03-22 stsp list_refs(struct got_repository *repo, const char *refname)
4315 d0eebce4 2019-03-11 stsp {
4316 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
4317 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
4318 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
4319 d0eebce4 2019-03-11 stsp
4320 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
4321 b2070a3f 2020-03-22 stsp err = got_ref_list(&refs, repo, refname, got_ref_cmp_by_name, NULL);
4322 d0eebce4 2019-03-11 stsp if (err)
4323 d0eebce4 2019-03-11 stsp return err;
4324 d0eebce4 2019-03-11 stsp
4325 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
4326 d0eebce4 2019-03-11 stsp char *refstr;
4327 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
4328 d0eebce4 2019-03-11 stsp if (refstr == NULL)
4329 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
4330 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
4331 d0eebce4 2019-03-11 stsp free(refstr);
4332 d0eebce4 2019-03-11 stsp }
4333 d0eebce4 2019-03-11 stsp
4334 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
4335 d0eebce4 2019-03-11 stsp return NULL;
4336 d0eebce4 2019-03-11 stsp }
4337 d0eebce4 2019-03-11 stsp
4338 d0eebce4 2019-03-11 stsp static const struct got_error *
4339 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
4340 d0eebce4 2019-03-11 stsp {
4341 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4342 d0eebce4 2019-03-11 stsp struct got_reference *ref;
4343 d0eebce4 2019-03-11 stsp
4344 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
4345 d0eebce4 2019-03-11 stsp if (err)
4346 d0eebce4 2019-03-11 stsp return err;
4347 d0eebce4 2019-03-11 stsp
4348 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
4349 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4350 d0eebce4 2019-03-11 stsp return err;
4351 d0eebce4 2019-03-11 stsp }
4352 d0eebce4 2019-03-11 stsp
4353 d0eebce4 2019-03-11 stsp static const struct got_error *
4354 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
4355 d0eebce4 2019-03-11 stsp {
4356 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
4357 d0eebce4 2019-03-11 stsp struct got_object_id *id;
4358 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
4359 d1644381 2019-07-14 stsp
4360 d1644381 2019-07-14 stsp /*
4361 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4362 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
4363 d1644381 2019-07-14 stsp * an unintended typo.
4364 d1644381 2019-07-14 stsp */
4365 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4366 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4367 d0eebce4 2019-03-11 stsp
4368 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
4369 dd88155e 2019-06-29 stsp repo);
4370 d83d9d5c 2019-05-13 stsp if (err) {
4371 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
4372 d0eebce4 2019-03-11 stsp
4373 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
4374 d83d9d5c 2019-05-13 stsp return err;
4375 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
4376 d83d9d5c 2019-05-13 stsp if (err)
4377 d83d9d5c 2019-05-13 stsp return err;
4378 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
4379 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
4380 d83d9d5c 2019-05-13 stsp if (err)
4381 d83d9d5c 2019-05-13 stsp return err;
4382 d83d9d5c 2019-05-13 stsp }
4383 d83d9d5c 2019-05-13 stsp
4384 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
4385 d0eebce4 2019-03-11 stsp if (err)
4386 d0eebce4 2019-03-11 stsp goto done;
4387 d0eebce4 2019-03-11 stsp
4388 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
4389 d0eebce4 2019-03-11 stsp done:
4390 d0eebce4 2019-03-11 stsp if (ref)
4391 d0eebce4 2019-03-11 stsp got_ref_close(ref);
4392 d0eebce4 2019-03-11 stsp free(id);
4393 d1c1ae5f 2019-08-12 stsp return err;
4394 d1c1ae5f 2019-08-12 stsp }
4395 d1c1ae5f 2019-08-12 stsp
4396 d1c1ae5f 2019-08-12 stsp static const struct got_error *
4397 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
4398 d1c1ae5f 2019-08-12 stsp {
4399 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
4400 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
4401 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
4402 d1c1ae5f 2019-08-12 stsp
4403 d1c1ae5f 2019-08-12 stsp /*
4404 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
4405 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
4406 d1c1ae5f 2019-08-12 stsp * an unintended typo.
4407 d1c1ae5f 2019-08-12 stsp */
4408 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
4409 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
4410 d1c1ae5f 2019-08-12 stsp
4411 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
4412 d1c1ae5f 2019-08-12 stsp if (err)
4413 d1c1ae5f 2019-08-12 stsp return err;
4414 d1c1ae5f 2019-08-12 stsp
4415 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
4416 d1c1ae5f 2019-08-12 stsp if (err)
4417 d1c1ae5f 2019-08-12 stsp goto done;
4418 d1c1ae5f 2019-08-12 stsp
4419 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
4420 d1c1ae5f 2019-08-12 stsp done:
4421 d1c1ae5f 2019-08-12 stsp if (target_ref)
4422 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
4423 d1c1ae5f 2019-08-12 stsp if (ref)
4424 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
4425 d0eebce4 2019-03-11 stsp return err;
4426 d0eebce4 2019-03-11 stsp }
4427 d0eebce4 2019-03-11 stsp
4428 d0eebce4 2019-03-11 stsp static const struct got_error *
4429 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
4430 d0eebce4 2019-03-11 stsp {
4431 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
4432 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
4433 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
4434 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
4435 e31abbf2 2020-03-22 stsp int ch, do_list = 0, do_delete = 0;
4436 b2070a3f 2020-03-22 stsp const char *obj_arg = NULL, *symref_target= NULL;
4437 b2070a3f 2020-03-22 stsp char *refname = NULL;
4438 d0eebce4 2019-03-11 stsp
4439 e31abbf2 2020-03-22 stsp while ((ch = getopt(argc, argv, "c:dr:ls:")) != -1) {
4440 d0eebce4 2019-03-11 stsp switch (ch) {
4441 e31abbf2 2020-03-22 stsp case 'c':
4442 e31abbf2 2020-03-22 stsp obj_arg = optarg;
4443 e31abbf2 2020-03-22 stsp break;
4444 d0eebce4 2019-03-11 stsp case 'd':
4445 e31abbf2 2020-03-22 stsp do_delete = 1;
4446 d0eebce4 2019-03-11 stsp break;
4447 d0eebce4 2019-03-11 stsp case 'r':
4448 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
4449 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4450 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4451 9ba1d308 2019-10-21 stsp optarg);
4452 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
4453 d0eebce4 2019-03-11 stsp break;
4454 d0eebce4 2019-03-11 stsp case 'l':
4455 d0eebce4 2019-03-11 stsp do_list = 1;
4456 d1c1ae5f 2019-08-12 stsp break;
4457 d1c1ae5f 2019-08-12 stsp case 's':
4458 e31abbf2 2020-03-22 stsp symref_target = optarg;
4459 d0eebce4 2019-03-11 stsp break;
4460 d0eebce4 2019-03-11 stsp default:
4461 d0eebce4 2019-03-11 stsp usage_ref();
4462 d0eebce4 2019-03-11 stsp /* NOTREACHED */
4463 d0eebce4 2019-03-11 stsp }
4464 d0eebce4 2019-03-11 stsp }
4465 d0eebce4 2019-03-11 stsp
4466 e31abbf2 2020-03-22 stsp if (obj_arg && do_list)
4467 907f15e2 2020-03-22 stsp errx(1, "-c and -l options are mutually exclusive");
4468 e31abbf2 2020-03-22 stsp if (obj_arg && do_delete)
4469 907f15e2 2020-03-22 stsp errx(1, "-c and -d options are mutually exclusive");
4470 907f15e2 2020-03-22 stsp if (obj_arg && symref_target)
4471 907f15e2 2020-03-22 stsp errx(1, "-c and -s options are mutually exclusive");
4472 e31abbf2 2020-03-22 stsp if (symref_target && do_delete)
4473 907f15e2 2020-03-22 stsp errx(1, "-s and -d options are mutually exclusive");
4474 e31abbf2 2020-03-22 stsp if (symref_target && do_list)
4475 907f15e2 2020-03-22 stsp errx(1, "-s and -l options are mutually exclusive");
4476 e31abbf2 2020-03-22 stsp if (do_delete && do_list)
4477 907f15e2 2020-03-22 stsp errx(1, "-d and -l options are mutually exclusive");
4478 d0eebce4 2019-03-11 stsp
4479 d0eebce4 2019-03-11 stsp argc -= optind;
4480 d0eebce4 2019-03-11 stsp argv += optind;
4481 d0eebce4 2019-03-11 stsp
4482 e31abbf2 2020-03-22 stsp if (do_list) {
4483 b2070a3f 2020-03-22 stsp if (argc != 0 && argc != 1)
4484 d0eebce4 2019-03-11 stsp usage_ref();
4485 b2070a3f 2020-03-22 stsp if (argc == 1) {
4486 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4487 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4488 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4489 b2070a3f 2020-03-22 stsp goto done;
4490 b2070a3f 2020-03-22 stsp }
4491 b2070a3f 2020-03-22 stsp }
4492 e31abbf2 2020-03-22 stsp } else {
4493 e31abbf2 2020-03-22 stsp if (argc != 1)
4494 e31abbf2 2020-03-22 stsp usage_ref();
4495 b2070a3f 2020-03-22 stsp refname = strdup(argv[0]);
4496 b2070a3f 2020-03-22 stsp if (refname == NULL) {
4497 b2070a3f 2020-03-22 stsp error = got_error_from_errno("strdup");
4498 b2070a3f 2020-03-22 stsp goto done;
4499 b2070a3f 2020-03-22 stsp }
4500 e31abbf2 2020-03-22 stsp }
4501 b2070a3f 2020-03-22 stsp
4502 b2070a3f 2020-03-22 stsp if (refname)
4503 b2070a3f 2020-03-22 stsp got_path_strip_trailing_slashes(refname);
4504 d0eebce4 2019-03-11 stsp
4505 d0eebce4 2019-03-11 stsp #ifndef PROFILE
4506 e0b57350 2019-03-12 stsp if (do_list) {
4507 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4508 e0b57350 2019-03-12 stsp NULL) == -1)
4509 e0b57350 2019-03-12 stsp err(1, "pledge");
4510 e0b57350 2019-03-12 stsp } else {
4511 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4512 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
4513 e0b57350 2019-03-12 stsp err(1, "pledge");
4514 e0b57350 2019-03-12 stsp }
4515 d0eebce4 2019-03-11 stsp #endif
4516 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
4517 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
4518 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4519 d0eebce4 2019-03-11 stsp goto done;
4520 d0eebce4 2019-03-11 stsp }
4521 d0eebce4 2019-03-11 stsp
4522 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4523 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
4524 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4525 d0eebce4 2019-03-11 stsp goto done;
4526 d0eebce4 2019-03-11 stsp else
4527 d0eebce4 2019-03-11 stsp error = NULL;
4528 d0eebce4 2019-03-11 stsp if (worktree) {
4529 d0eebce4 2019-03-11 stsp repo_path =
4530 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
4531 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
4532 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4533 d0eebce4 2019-03-11 stsp if (error)
4534 d0eebce4 2019-03-11 stsp goto done;
4535 d0eebce4 2019-03-11 stsp } else {
4536 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
4537 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
4538 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
4539 d0eebce4 2019-03-11 stsp goto done;
4540 d0eebce4 2019-03-11 stsp }
4541 d0eebce4 2019-03-11 stsp }
4542 d0eebce4 2019-03-11 stsp }
4543 d0eebce4 2019-03-11 stsp
4544 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4545 d0eebce4 2019-03-11 stsp if (error != NULL)
4546 d0eebce4 2019-03-11 stsp goto done;
4547 d0eebce4 2019-03-11 stsp
4548 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4549 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4550 c02c541e 2019-03-29 stsp if (error)
4551 c02c541e 2019-03-29 stsp goto done;
4552 c02c541e 2019-03-29 stsp
4553 d0eebce4 2019-03-11 stsp if (do_list)
4554 b2070a3f 2020-03-22 stsp error = list_refs(repo, refname);
4555 e31abbf2 2020-03-22 stsp else if (do_delete)
4556 e31abbf2 2020-03-22 stsp error = delete_ref(repo, refname);
4557 e31abbf2 2020-03-22 stsp else if (symref_target)
4558 e31abbf2 2020-03-22 stsp error = add_symref(repo, refname, symref_target);
4559 e31abbf2 2020-03-22 stsp else {
4560 e31abbf2 2020-03-22 stsp if (obj_arg == NULL)
4561 e31abbf2 2020-03-22 stsp usage_ref();
4562 e31abbf2 2020-03-22 stsp error = add_ref(repo, refname, obj_arg);
4563 e31abbf2 2020-03-22 stsp }
4564 4e759de4 2019-06-26 stsp done:
4565 b2070a3f 2020-03-22 stsp free(refname);
4566 4e759de4 2019-06-26 stsp if (repo)
4567 4e759de4 2019-06-26 stsp got_repo_close(repo);
4568 4e759de4 2019-06-26 stsp if (worktree)
4569 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
4570 4e759de4 2019-06-26 stsp free(cwd);
4571 4e759de4 2019-06-26 stsp free(repo_path);
4572 4e759de4 2019-06-26 stsp return error;
4573 4e759de4 2019-06-26 stsp }
4574 4e759de4 2019-06-26 stsp
4575 4e759de4 2019-06-26 stsp __dead static void
4576 4e759de4 2019-06-26 stsp usage_branch(void)
4577 4e759de4 2019-06-26 stsp {
4578 4e759de4 2019-06-26 stsp fprintf(stderr,
4579 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
4580 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
4581 4e759de4 2019-06-26 stsp exit(1);
4582 4e759de4 2019-06-26 stsp }
4583 4e759de4 2019-06-26 stsp
4584 4e759de4 2019-06-26 stsp static const struct got_error *
4585 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
4586 4e99b47e 2019-10-04 stsp struct got_reference *ref)
4587 4e99b47e 2019-10-04 stsp {
4588 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
4589 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
4590 4e99b47e 2019-10-04 stsp char *refstr;
4591 4e99b47e 2019-10-04 stsp
4592 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
4593 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
4594 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
4595 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
4596 4e99b47e 2019-10-04 stsp
4597 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
4598 4e99b47e 2019-10-04 stsp if (err)
4599 4e99b47e 2019-10-04 stsp return err;
4600 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
4601 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
4602 4e99b47e 2019-10-04 stsp marker = "* ";
4603 4e99b47e 2019-10-04 stsp else
4604 4e99b47e 2019-10-04 stsp marker = "~ ";
4605 4e99b47e 2019-10-04 stsp free(id);
4606 4e99b47e 2019-10-04 stsp }
4607 4e99b47e 2019-10-04 stsp
4608 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4609 4e99b47e 2019-10-04 stsp refname += 11;
4610 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4611 4e99b47e 2019-10-04 stsp refname += 18;
4612 4e99b47e 2019-10-04 stsp
4613 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
4614 4e99b47e 2019-10-04 stsp if (refstr == NULL)
4615 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
4616 4e99b47e 2019-10-04 stsp
4617 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
4618 4e99b47e 2019-10-04 stsp free(refstr);
4619 4e99b47e 2019-10-04 stsp return NULL;
4620 4e99b47e 2019-10-04 stsp }
4621 4e99b47e 2019-10-04 stsp
4622 4e99b47e 2019-10-04 stsp static const struct got_error *
4623 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
4624 ad89fa31 2019-10-04 stsp {
4625 ad89fa31 2019-10-04 stsp const char *refname;
4626 ad89fa31 2019-10-04 stsp
4627 ad89fa31 2019-10-04 stsp if (worktree == NULL)
4628 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
4629 ad89fa31 2019-10-04 stsp
4630 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
4631 ad89fa31 2019-10-04 stsp
4632 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
4633 ad89fa31 2019-10-04 stsp refname += 11;
4634 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
4635 ad89fa31 2019-10-04 stsp refname += 18;
4636 ad89fa31 2019-10-04 stsp
4637 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
4638 ad89fa31 2019-10-04 stsp
4639 ad89fa31 2019-10-04 stsp return NULL;
4640 ad89fa31 2019-10-04 stsp }
4641 ad89fa31 2019-10-04 stsp
4642 ad89fa31 2019-10-04 stsp static const struct got_error *
4643 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
4644 4e759de4 2019-06-26 stsp {
4645 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
4646 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
4647 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
4648 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
4649 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
4650 4e759de4 2019-06-26 stsp
4651 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
4652 ba882ee3 2019-07-11 stsp
4653 4e99b47e 2019-10-04 stsp if (worktree) {
4654 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
4655 4e99b47e 2019-10-04 stsp worktree);
4656 4e99b47e 2019-10-04 stsp if (err)
4657 4e99b47e 2019-10-04 stsp return err;
4658 4e99b47e 2019-10-04 stsp
4659 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
4660 4e99b47e 2019-10-04 stsp worktree);
4661 4e99b47e 2019-10-04 stsp if (err)
4662 4e99b47e 2019-10-04 stsp return err;
4663 4e99b47e 2019-10-04 stsp
4664 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
4665 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
4666 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
4667 4e99b47e 2019-10-04 stsp if (err)
4668 4e99b47e 2019-10-04 stsp return err;
4669 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
4670 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
4671 4e99b47e 2019-10-04 stsp }
4672 4e99b47e 2019-10-04 stsp }
4673 4e99b47e 2019-10-04 stsp
4674 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
4675 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
4676 4e759de4 2019-06-26 stsp if (err)
4677 4e759de4 2019-06-26 stsp return err;
4678 4e759de4 2019-06-26 stsp
4679 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
4680 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
4681 4e759de4 2019-06-26 stsp
4682 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
4683 4e759de4 2019-06-26 stsp return NULL;
4684 4e759de4 2019-06-26 stsp }
4685 4e759de4 2019-06-26 stsp
4686 4e759de4 2019-06-26 stsp static const struct got_error *
4687 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
4688 45cd4e47 2019-08-25 stsp const char *branch_name)
4689 4e759de4 2019-06-26 stsp {
4690 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4691 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
4692 4e759de4 2019-06-26 stsp char *refname;
4693 4e759de4 2019-06-26 stsp
4694 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
4695 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
4696 4e759de4 2019-06-26 stsp
4697 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4698 4e759de4 2019-06-26 stsp if (err)
4699 4e759de4 2019-06-26 stsp goto done;
4700 4e759de4 2019-06-26 stsp
4701 45cd4e47 2019-08-25 stsp if (worktree &&
4702 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
4703 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
4704 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
4705 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
4706 45cd4e47 2019-08-25 stsp goto done;
4707 45cd4e47 2019-08-25 stsp }
4708 45cd4e47 2019-08-25 stsp
4709 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
4710 4e759de4 2019-06-26 stsp done:
4711 45cd4e47 2019-08-25 stsp if (ref)
4712 45cd4e47 2019-08-25 stsp got_ref_close(ref);
4713 4e759de4 2019-06-26 stsp free(refname);
4714 4e759de4 2019-06-26 stsp return err;
4715 4e759de4 2019-06-26 stsp }
4716 4e759de4 2019-06-26 stsp
4717 4e759de4 2019-06-26 stsp static const struct got_error *
4718 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
4719 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
4720 4e759de4 2019-06-26 stsp {
4721 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
4722 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
4723 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
4724 d3f84d51 2019-07-11 stsp
4725 d3f84d51 2019-07-11 stsp /*
4726 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
4727 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
4728 d3f84d51 2019-07-11 stsp * an unintended typo.
4729 d3f84d51 2019-07-11 stsp */
4730 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
4731 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
4732 4e759de4 2019-06-26 stsp
4733 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
4734 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
4735 4e759de4 2019-06-26 stsp goto done;
4736 4e759de4 2019-06-26 stsp }
4737 4e759de4 2019-06-26 stsp
4738 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
4739 4e759de4 2019-06-26 stsp if (err == NULL) {
4740 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
4741 4e759de4 2019-06-26 stsp goto done;
4742 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
4743 4e759de4 2019-06-26 stsp goto done;
4744 4e759de4 2019-06-26 stsp
4745 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
4746 4e759de4 2019-06-26 stsp if (err)
4747 4e759de4 2019-06-26 stsp goto done;
4748 4e759de4 2019-06-26 stsp
4749 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
4750 d0eebce4 2019-03-11 stsp done:
4751 4e759de4 2019-06-26 stsp if (ref)
4752 4e759de4 2019-06-26 stsp got_ref_close(ref);
4753 4e759de4 2019-06-26 stsp free(base_refname);
4754 4e759de4 2019-06-26 stsp free(refname);
4755 4e759de4 2019-06-26 stsp return err;
4756 4e759de4 2019-06-26 stsp }
4757 4e759de4 2019-06-26 stsp
4758 4e759de4 2019-06-26 stsp static const struct got_error *
4759 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
4760 4e759de4 2019-06-26 stsp {
4761 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
4762 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
4763 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
4764 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
4765 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
4766 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
4767 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
4768 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
4769 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
4770 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
4771 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
4772 4e759de4 2019-06-26 stsp
4773 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
4774 da76fce2 2020-02-24 stsp
4775 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
4776 4e759de4 2019-06-26 stsp switch (ch) {
4777 a74f7e83 2019-11-10 stsp case 'c':
4778 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
4779 a74f7e83 2019-11-10 stsp break;
4780 4e759de4 2019-06-26 stsp case 'd':
4781 4e759de4 2019-06-26 stsp delref = optarg;
4782 4e759de4 2019-06-26 stsp break;
4783 4e759de4 2019-06-26 stsp case 'r':
4784 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
4785 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4786 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4787 9ba1d308 2019-10-21 stsp optarg);
4788 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
4789 4e759de4 2019-06-26 stsp break;
4790 4e759de4 2019-06-26 stsp case 'l':
4791 4e759de4 2019-06-26 stsp do_list = 1;
4792 da76fce2 2020-02-24 stsp break;
4793 da76fce2 2020-02-24 stsp case 'n':
4794 da76fce2 2020-02-24 stsp do_update = 0;
4795 4e759de4 2019-06-26 stsp break;
4796 4e759de4 2019-06-26 stsp default:
4797 4e759de4 2019-06-26 stsp usage_branch();
4798 4e759de4 2019-06-26 stsp /* NOTREACHED */
4799 4e759de4 2019-06-26 stsp }
4800 4e759de4 2019-06-26 stsp }
4801 4e759de4 2019-06-26 stsp
4802 4e759de4 2019-06-26 stsp if (do_list && delref)
4803 907f15e2 2020-03-22 stsp errx(1, "-l and -d options are mutually exclusive");
4804 4e759de4 2019-06-26 stsp
4805 4e759de4 2019-06-26 stsp argc -= optind;
4806 4e759de4 2019-06-26 stsp argv += optind;
4807 ad89fa31 2019-10-04 stsp
4808 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
4809 ad89fa31 2019-10-04 stsp do_show = 1;
4810 4e759de4 2019-06-26 stsp
4811 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
4812 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
4813 a74f7e83 2019-11-10 stsp
4814 4e759de4 2019-06-26 stsp if (do_list || delref) {
4815 4e759de4 2019-06-26 stsp if (argc > 0)
4816 4e759de4 2019-06-26 stsp usage_branch();
4817 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
4818 4e759de4 2019-06-26 stsp usage_branch();
4819 4e759de4 2019-06-26 stsp
4820 4e759de4 2019-06-26 stsp #ifndef PROFILE
4821 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
4822 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4823 4e759de4 2019-06-26 stsp NULL) == -1)
4824 4e759de4 2019-06-26 stsp err(1, "pledge");
4825 4e759de4 2019-06-26 stsp } else {
4826 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4827 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
4828 4e759de4 2019-06-26 stsp err(1, "pledge");
4829 4e759de4 2019-06-26 stsp }
4830 4e759de4 2019-06-26 stsp #endif
4831 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
4832 4e759de4 2019-06-26 stsp if (cwd == NULL) {
4833 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
4834 4e759de4 2019-06-26 stsp goto done;
4835 4e759de4 2019-06-26 stsp }
4836 4e759de4 2019-06-26 stsp
4837 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4838 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
4839 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4840 4e759de4 2019-06-26 stsp goto done;
4841 4e759de4 2019-06-26 stsp else
4842 4e759de4 2019-06-26 stsp error = NULL;
4843 4e759de4 2019-06-26 stsp if (worktree) {
4844 4e759de4 2019-06-26 stsp repo_path =
4845 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
4846 4e759de4 2019-06-26 stsp if (repo_path == NULL)
4847 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4848 4e759de4 2019-06-26 stsp if (error)
4849 4e759de4 2019-06-26 stsp goto done;
4850 4e759de4 2019-06-26 stsp } else {
4851 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
4852 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
4853 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
4854 4e759de4 2019-06-26 stsp goto done;
4855 4e759de4 2019-06-26 stsp }
4856 4e759de4 2019-06-26 stsp }
4857 4e759de4 2019-06-26 stsp }
4858 4e759de4 2019-06-26 stsp
4859 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4860 4e759de4 2019-06-26 stsp if (error != NULL)
4861 4e759de4 2019-06-26 stsp goto done;
4862 4e759de4 2019-06-26 stsp
4863 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
4864 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
4865 4e759de4 2019-06-26 stsp if (error)
4866 4e759de4 2019-06-26 stsp goto done;
4867 4e759de4 2019-06-26 stsp
4868 ad89fa31 2019-10-04 stsp if (do_show)
4869 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
4870 ad89fa31 2019-10-04 stsp else if (do_list)
4871 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
4872 4e759de4 2019-06-26 stsp else if (delref)
4873 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
4874 4e759de4 2019-06-26 stsp else {
4875 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
4876 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
4877 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
4878 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
4879 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
4880 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
4881 a74f7e83 2019-11-10 stsp if (error)
4882 a74f7e83 2019-11-10 stsp goto done;
4883 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
4884 da76fce2 2020-02-24 stsp if (error)
4885 da76fce2 2020-02-24 stsp goto done;
4886 da76fce2 2020-02-24 stsp if (worktree && do_update) {
4887 da76fce2 2020-02-24 stsp int did_something = 0;
4888 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
4889 da76fce2 2020-02-24 stsp
4890 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
4891 da76fce2 2020-02-24 stsp if (error)
4892 da76fce2 2020-02-24 stsp goto done;
4893 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
4894 da76fce2 2020-02-24 stsp worktree);
4895 da76fce2 2020-02-24 stsp if (error)
4896 da76fce2 2020-02-24 stsp goto done;
4897 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
4898 da76fce2 2020-02-24 stsp == -1) {
4899 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
4900 da76fce2 2020-02-24 stsp goto done;
4901 da76fce2 2020-02-24 stsp }
4902 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
4903 da76fce2 2020-02-24 stsp free(branch_refname);
4904 da76fce2 2020-02-24 stsp if (error)
4905 da76fce2 2020-02-24 stsp goto done;
4906 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
4907 da76fce2 2020-02-24 stsp repo);
4908 da76fce2 2020-02-24 stsp if (error)
4909 da76fce2 2020-02-24 stsp goto done;
4910 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
4911 da76fce2 2020-02-24 stsp commit_id);
4912 da76fce2 2020-02-24 stsp if (error)
4913 da76fce2 2020-02-24 stsp goto done;
4914 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
4915 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
4916 da76fce2 2020-02-24 stsp check_cancelled, NULL);
4917 da76fce2 2020-02-24 stsp if (error)
4918 da76fce2 2020-02-24 stsp goto done;
4919 da76fce2 2020-02-24 stsp if (did_something)
4920 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
4921 da76fce2 2020-02-24 stsp }
4922 4e759de4 2019-06-26 stsp }
4923 4e759de4 2019-06-26 stsp done:
4924 da76fce2 2020-02-24 stsp if (ref)
4925 da76fce2 2020-02-24 stsp got_ref_close(ref);
4926 d0eebce4 2019-03-11 stsp if (repo)
4927 d0eebce4 2019-03-11 stsp got_repo_close(repo);
4928 d0eebce4 2019-03-11 stsp if (worktree)
4929 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
4930 d0eebce4 2019-03-11 stsp free(cwd);
4931 d0eebce4 2019-03-11 stsp free(repo_path);
4932 da76fce2 2020-02-24 stsp free(commit_id);
4933 da76fce2 2020-02-24 stsp free(commit_id_str);
4934 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
4935 da76fce2 2020-02-24 stsp free((char *)pe->path);
4936 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
4937 d00136be 2019-03-26 stsp return error;
4938 d00136be 2019-03-26 stsp }
4939 d00136be 2019-03-26 stsp
4940 8e7bd50a 2019-08-22 stsp
4941 d00136be 2019-03-26 stsp __dead static void
4942 8e7bd50a 2019-08-22 stsp usage_tag(void)
4943 8e7bd50a 2019-08-22 stsp {
4944 8e7bd50a 2019-08-22 stsp fprintf(stderr,
4945 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
4946 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
4947 8e7bd50a 2019-08-22 stsp exit(1);
4948 b8bad2ba 2019-08-23 stsp }
4949 b8bad2ba 2019-08-23 stsp
4950 b8bad2ba 2019-08-23 stsp #if 0
4951 b8bad2ba 2019-08-23 stsp static const struct got_error *
4952 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
4953 b8bad2ba 2019-08-23 stsp {
4954 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
4955 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
4956 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
4957 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
4958 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
4959 b8bad2ba 2019-08-23 stsp
4960 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
4961 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
4962 b8bad2ba 2019-08-23 stsp if (se == NULL) {
4963 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4964 b8bad2ba 2019-08-23 stsp if (err)
4965 b8bad2ba 2019-08-23 stsp return err;
4966 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
4967 b8bad2ba 2019-08-23 stsp continue;
4968 b8bad2ba 2019-08-23 stsp } else {
4969 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
4970 b8bad2ba 2019-08-23 stsp if (err)
4971 b8bad2ba 2019-08-23 stsp break;
4972 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
4973 b8bad2ba 2019-08-23 stsp free(re_id);
4974 b8bad2ba 2019-08-23 stsp if (err)
4975 b8bad2ba 2019-08-23 stsp break;
4976 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
4977 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
4978 b8bad2ba 2019-08-23 stsp }
4979 b8bad2ba 2019-08-23 stsp
4980 b8bad2ba 2019-08-23 stsp while (se) {
4981 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
4982 b8bad2ba 2019-08-23 stsp if (err)
4983 b8bad2ba 2019-08-23 stsp break;
4984 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
4985 b8bad2ba 2019-08-23 stsp free(se_id);
4986 b8bad2ba 2019-08-23 stsp if (err)
4987 b8bad2ba 2019-08-23 stsp break;
4988 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
4989 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
4990 b8bad2ba 2019-08-23 stsp
4991 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
4992 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
4993 b8bad2ba 2019-08-23 stsp if (err)
4994 b8bad2ba 2019-08-23 stsp return err;
4995 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
4996 b8bad2ba 2019-08-23 stsp break;
4997 b8bad2ba 2019-08-23 stsp }
4998 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
4999 b8bad2ba 2019-08-23 stsp continue;
5000 b8bad2ba 2019-08-23 stsp }
5001 b8bad2ba 2019-08-23 stsp }
5002 b8bad2ba 2019-08-23 stsp done:
5003 b8bad2ba 2019-08-23 stsp return err;
5004 8e7bd50a 2019-08-22 stsp }
5005 b8bad2ba 2019-08-23 stsp #endif
5006 b8bad2ba 2019-08-23 stsp
5007 b8bad2ba 2019-08-23 stsp static const struct got_error *
5008 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
5009 8e7bd50a 2019-08-22 stsp {
5010 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
5011 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
5012 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
5013 8e7bd50a 2019-08-22 stsp
5014 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
5015 8e7bd50a 2019-08-22 stsp
5016 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
5017 8e7bd50a 2019-08-22 stsp if (err)
5018 8e7bd50a 2019-08-22 stsp return err;
5019 8e7bd50a 2019-08-22 stsp
5020 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
5021 8e7bd50a 2019-08-22 stsp const char *refname;
5022 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
5023 8e7bd50a 2019-08-22 stsp char datebuf[26];
5024 d4efa91b 2020-01-14 stsp const char *tagger;
5025 8e7bd50a 2019-08-22 stsp time_t tagger_time;
5026 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
5027 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
5028 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
5029 8e7bd50a 2019-08-22 stsp
5030 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
5031 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
5032 8e7bd50a 2019-08-22 stsp continue;
5033 8e7bd50a 2019-08-22 stsp refname += 10;
5034 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
5035 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
5036 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
5037 8e7bd50a 2019-08-22 stsp break;
5038 8e7bd50a 2019-08-22 stsp }
5039 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
5040 8e7bd50a 2019-08-22 stsp free(refstr);
5041 8e7bd50a 2019-08-22 stsp
5042 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
5043 8e7bd50a 2019-08-22 stsp if (err)
5044 8e7bd50a 2019-08-22 stsp break;
5045 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
5046 d4efa91b 2020-01-14 stsp if (err) {
5047 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
5048 d4efa91b 2020-01-14 stsp free(id);
5049 d4efa91b 2020-01-14 stsp break;
5050 d4efa91b 2020-01-14 stsp }
5051 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
5052 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
5053 d4efa91b 2020-01-14 stsp if (err) {
5054 d4efa91b 2020-01-14 stsp free(id);
5055 d4efa91b 2020-01-14 stsp break;
5056 d4efa91b 2020-01-14 stsp }
5057 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
5058 d4efa91b 2020-01-14 stsp tagger_time =
5059 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
5060 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
5061 d4efa91b 2020-01-14 stsp free(id);
5062 d4efa91b 2020-01-14 stsp if (err)
5063 d4efa91b 2020-01-14 stsp break;
5064 d4efa91b 2020-01-14 stsp } else {
5065 d4efa91b 2020-01-14 stsp free(id);
5066 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
5067 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
5068 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
5069 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
5070 d4efa91b 2020-01-14 stsp if (err)
5071 d4efa91b 2020-01-14 stsp break;
5072 d4efa91b 2020-01-14 stsp }
5073 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
5074 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
5075 2417344c 2019-08-23 stsp if (datestr)
5076 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
5077 d4efa91b 2020-01-14 stsp if (commit)
5078 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
5079 d4efa91b 2020-01-14 stsp else {
5080 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
5081 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
5082 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
5083 d4efa91b 2020-01-14 stsp id_str);
5084 d4efa91b 2020-01-14 stsp break;
5085 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
5086 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
5087 d4efa91b 2020-01-14 stsp id_str);
5088 d4efa91b 2020-01-14 stsp break;
5089 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
5090 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
5091 d4efa91b 2020-01-14 stsp id_str);
5092 d4efa91b 2020-01-14 stsp break;
5093 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
5094 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
5095 d4efa91b 2020-01-14 stsp id_str);
5096 d4efa91b 2020-01-14 stsp break;
5097 d4efa91b 2020-01-14 stsp default:
5098 d4efa91b 2020-01-14 stsp break;
5099 d4efa91b 2020-01-14 stsp }
5100 8e7bd50a 2019-08-22 stsp }
5101 8e7bd50a 2019-08-22 stsp free(id_str);
5102 d4efa91b 2020-01-14 stsp if (commit) {
5103 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
5104 d4efa91b 2020-01-14 stsp if (err)
5105 d4efa91b 2020-01-14 stsp break;
5106 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
5107 d4efa91b 2020-01-14 stsp } else {
5108 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
5109 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
5110 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
5111 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
5112 d4efa91b 2020-01-14 stsp break;
5113 d4efa91b 2020-01-14 stsp }
5114 8e7bd50a 2019-08-22 stsp }
5115 8e7bd50a 2019-08-22 stsp
5116 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
5117 8e7bd50a 2019-08-22 stsp do {
5118 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
5119 8e7bd50a 2019-08-22 stsp if (line)
5120 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
5121 8e7bd50a 2019-08-22 stsp } while (line);
5122 8e7bd50a 2019-08-22 stsp free(tagmsg0);
5123 8e7bd50a 2019-08-22 stsp }
5124 8e7bd50a 2019-08-22 stsp
5125 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
5126 8e7bd50a 2019-08-22 stsp return NULL;
5127 8e7bd50a 2019-08-22 stsp }
5128 8e7bd50a 2019-08-22 stsp
5129 8e7bd50a 2019-08-22 stsp static const struct got_error *
5130 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
5131 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
5132 8e7bd50a 2019-08-22 stsp {
5133 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5134 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
5135 f372d5cd 2019-10-21 stsp char *editor = NULL;
5136 8e7bd50a 2019-08-22 stsp int fd = -1;
5137 8e7bd50a 2019-08-22 stsp
5138 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
5139 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5140 8e7bd50a 2019-08-22 stsp goto done;
5141 8e7bd50a 2019-08-22 stsp }
5142 8e7bd50a 2019-08-22 stsp
5143 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
5144 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
5145 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5146 8e7bd50a 2019-08-22 stsp goto done;
5147 8e7bd50a 2019-08-22 stsp }
5148 8e7bd50a 2019-08-22 stsp
5149 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
5150 8e7bd50a 2019-08-22 stsp if (err)
5151 8e7bd50a 2019-08-22 stsp goto done;
5152 8e7bd50a 2019-08-22 stsp
5153 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
5154 8e7bd50a 2019-08-22 stsp close(fd);
5155 8e7bd50a 2019-08-22 stsp
5156 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
5157 8e7bd50a 2019-08-22 stsp if (err)
5158 8e7bd50a 2019-08-22 stsp goto done;
5159 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
5160 8e7bd50a 2019-08-22 stsp done:
5161 8e7bd50a 2019-08-22 stsp free(initial_content);
5162 8e7bd50a 2019-08-22 stsp free(template);
5163 8e7bd50a 2019-08-22 stsp free(editor);
5164 8e7bd50a 2019-08-22 stsp
5165 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
5166 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5167 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
5168 8e7bd50a 2019-08-22 stsp if (err) {
5169 8e7bd50a 2019-08-22 stsp free(*tagmsg);
5170 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
5171 8e7bd50a 2019-08-22 stsp }
5172 8e7bd50a 2019-08-22 stsp }
5173 8e7bd50a 2019-08-22 stsp return err;
5174 8e7bd50a 2019-08-22 stsp }
5175 8e7bd50a 2019-08-22 stsp
5176 8e7bd50a 2019-08-22 stsp static const struct got_error *
5177 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
5178 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
5179 8e7bd50a 2019-08-22 stsp {
5180 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
5181 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
5182 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
5183 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
5184 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
5185 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
5186 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
5187 8e7bd50a 2019-08-22 stsp
5188 8e7bd50a 2019-08-22 stsp /*
5189 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
5190 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
5191 8e7bd50a 2019-08-22 stsp * an unintended typo.
5192 8e7bd50a 2019-08-22 stsp */
5193 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
5194 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
5195 8e7bd50a 2019-08-22 stsp
5196 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
5197 8e7bd50a 2019-08-22 stsp if (err)
5198 8e7bd50a 2019-08-22 stsp return err;
5199 8e7bd50a 2019-08-22 stsp
5200 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
5201 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
5202 8e7bd50a 2019-08-22 stsp if (err)
5203 8e7bd50a 2019-08-22 stsp goto done;
5204 8e7bd50a 2019-08-22 stsp
5205 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
5206 8e7bd50a 2019-08-22 stsp if (err)
5207 8e7bd50a 2019-08-22 stsp goto done;
5208 8e7bd50a 2019-08-22 stsp
5209 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
5210 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
5211 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
5212 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
5213 8e7bd50a 2019-08-22 stsp goto done;
5214 8e7bd50a 2019-08-22 stsp }
5215 8e7bd50a 2019-08-22 stsp tag_name += 10;
5216 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
5217 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
5218 8e7bd50a 2019-08-22 stsp goto done;
5219 8e7bd50a 2019-08-22 stsp }
5220 8e7bd50a 2019-08-22 stsp
5221 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
5222 8e7bd50a 2019-08-22 stsp if (err == NULL) {
5223 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
5224 8e7bd50a 2019-08-22 stsp goto done;
5225 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
5226 8e7bd50a 2019-08-22 stsp goto done;
5227 8e7bd50a 2019-08-22 stsp
5228 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
5229 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
5230 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
5231 f372d5cd 2019-10-21 stsp if (err) {
5232 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5233 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
5234 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5235 8e7bd50a 2019-08-22 stsp goto done;
5236 f372d5cd 2019-10-21 stsp }
5237 8e7bd50a 2019-08-22 stsp }
5238 8e7bd50a 2019-08-22 stsp
5239 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
5240 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
5241 f372d5cd 2019-10-21 stsp if (err) {
5242 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5243 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5244 8e7bd50a 2019-08-22 stsp goto done;
5245 f372d5cd 2019-10-21 stsp }
5246 8e7bd50a 2019-08-22 stsp
5247 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
5248 f372d5cd 2019-10-21 stsp if (err) {
5249 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5250 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5251 8e7bd50a 2019-08-22 stsp goto done;
5252 f372d5cd 2019-10-21 stsp }
5253 8e7bd50a 2019-08-22 stsp
5254 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
5255 f372d5cd 2019-10-21 stsp if (err) {
5256 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5257 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5258 f372d5cd 2019-10-21 stsp goto done;
5259 f372d5cd 2019-10-21 stsp }
5260 8e7bd50a 2019-08-22 stsp
5261 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
5262 f372d5cd 2019-10-21 stsp if (err) {
5263 f372d5cd 2019-10-21 stsp if (tagmsg_path)
5264 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
5265 f372d5cd 2019-10-21 stsp goto done;
5266 8e7bd50a 2019-08-22 stsp }
5267 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
5268 8e7bd50a 2019-08-22 stsp done:
5269 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
5270 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
5271 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
5272 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
5273 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
5274 f372d5cd 2019-10-21 stsp free(tag_id_str);
5275 8e7bd50a 2019-08-22 stsp if (ref)
5276 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
5277 8e7bd50a 2019-08-22 stsp free(commit_id);
5278 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5279 8e7bd50a 2019-08-22 stsp free(refname);
5280 8e7bd50a 2019-08-22 stsp free(tagmsg);
5281 f372d5cd 2019-10-21 stsp free(tagmsg_path);
5282 aba9c984 2019-09-08 stsp free(tagger);
5283 8e7bd50a 2019-08-22 stsp return err;
5284 8e7bd50a 2019-08-22 stsp }
5285 8e7bd50a 2019-08-22 stsp
5286 8e7bd50a 2019-08-22 stsp static const struct got_error *
5287 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
5288 8e7bd50a 2019-08-22 stsp {
5289 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
5290 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
5291 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
5292 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
5293 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
5294 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
5295 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
5296 8e7bd50a 2019-08-22 stsp
5297 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
5298 8e7bd50a 2019-08-22 stsp switch (ch) {
5299 80106605 2020-02-24 stsp case 'c':
5300 80106605 2020-02-24 stsp commit_id_arg = optarg;
5301 80106605 2020-02-24 stsp break;
5302 8e7bd50a 2019-08-22 stsp case 'm':
5303 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
5304 8e7bd50a 2019-08-22 stsp break;
5305 8e7bd50a 2019-08-22 stsp case 'r':
5306 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
5307 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5308 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
5309 9ba1d308 2019-10-21 stsp optarg);
5310 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
5311 8e7bd50a 2019-08-22 stsp break;
5312 8e7bd50a 2019-08-22 stsp case 'l':
5313 8e7bd50a 2019-08-22 stsp do_list = 1;
5314 8e7bd50a 2019-08-22 stsp break;
5315 8e7bd50a 2019-08-22 stsp default:
5316 8e7bd50a 2019-08-22 stsp usage_tag();
5317 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
5318 8e7bd50a 2019-08-22 stsp }
5319 8e7bd50a 2019-08-22 stsp }
5320 8e7bd50a 2019-08-22 stsp
5321 8e7bd50a 2019-08-22 stsp argc -= optind;
5322 8e7bd50a 2019-08-22 stsp argv += optind;
5323 8e7bd50a 2019-08-22 stsp
5324 8e7bd50a 2019-08-22 stsp if (do_list) {
5325 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
5326 775ce909 2020-03-22 stsp errx(1,
5327 775ce909 2020-03-22 stsp "-c option can only be used when creating a tag");
5328 8e7bd50a 2019-08-22 stsp if (tagmsg)
5329 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
5330 8e7bd50a 2019-08-22 stsp if (argc > 0)
5331 8e7bd50a 2019-08-22 stsp usage_tag();
5332 80106605 2020-02-24 stsp } else if (argc != 1)
5333 8e7bd50a 2019-08-22 stsp usage_tag();
5334 80106605 2020-02-24 stsp
5335 a2887370 2019-08-23 stsp tag_name = argv[0];
5336 8e7bd50a 2019-08-22 stsp
5337 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
5338 8e7bd50a 2019-08-22 stsp if (do_list) {
5339 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
5340 8e7bd50a 2019-08-22 stsp NULL) == -1)
5341 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5342 8e7bd50a 2019-08-22 stsp } else {
5343 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
5344 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
5345 8e7bd50a 2019-08-22 stsp err(1, "pledge");
5346 8e7bd50a 2019-08-22 stsp }
5347 8e7bd50a 2019-08-22 stsp #endif
5348 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
5349 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
5350 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
5351 8e7bd50a 2019-08-22 stsp goto done;
5352 8e7bd50a 2019-08-22 stsp }
5353 8e7bd50a 2019-08-22 stsp
5354 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5355 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
5356 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
5357 8e7bd50a 2019-08-22 stsp goto done;
5358 8e7bd50a 2019-08-22 stsp else
5359 8e7bd50a 2019-08-22 stsp error = NULL;
5360 8e7bd50a 2019-08-22 stsp if (worktree) {
5361 8e7bd50a 2019-08-22 stsp repo_path =
5362 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
5363 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
5364 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5365 8e7bd50a 2019-08-22 stsp if (error)
5366 8e7bd50a 2019-08-22 stsp goto done;
5367 8e7bd50a 2019-08-22 stsp } else {
5368 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
5369 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
5370 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
5371 8e7bd50a 2019-08-22 stsp goto done;
5372 8e7bd50a 2019-08-22 stsp }
5373 8e7bd50a 2019-08-22 stsp }
5374 8e7bd50a 2019-08-22 stsp }
5375 8e7bd50a 2019-08-22 stsp
5376 8e7bd50a 2019-08-22 stsp if (do_list) {
5377 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
5378 c9956ddf 2019-09-08 stsp if (error != NULL)
5379 c9956ddf 2019-09-08 stsp goto done;
5380 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5381 8e7bd50a 2019-08-22 stsp if (error)
5382 8e7bd50a 2019-08-22 stsp goto done;
5383 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
5384 8e7bd50a 2019-08-22 stsp } else {
5385 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
5386 c9956ddf 2019-09-08 stsp if (error)
5387 c9956ddf 2019-09-08 stsp goto done;
5388 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
5389 c9956ddf 2019-09-08 stsp if (error != NULL)
5390 c9956ddf 2019-09-08 stsp goto done;
5391 c9956ddf 2019-09-08 stsp
5392 8e7bd50a 2019-08-22 stsp if (tagmsg) {
5393 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
5394 8e7bd50a 2019-08-22 stsp if (error)
5395 8e7bd50a 2019-08-22 stsp goto done;
5396 8e7bd50a 2019-08-22 stsp }
5397 8e7bd50a 2019-08-22 stsp
5398 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
5399 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
5400 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
5401 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
5402 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
5403 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
5404 8e7bd50a 2019-08-22 stsp if (error)
5405 8e7bd50a 2019-08-22 stsp goto done;
5406 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
5407 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
5408 8e7bd50a 2019-08-22 stsp if (error)
5409 8e7bd50a 2019-08-22 stsp goto done;
5410 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
5411 8e7bd50a 2019-08-22 stsp free(commit_id);
5412 8e7bd50a 2019-08-22 stsp if (error)
5413 8e7bd50a 2019-08-22 stsp goto done;
5414 8e7bd50a 2019-08-22 stsp }
5415 8e7bd50a 2019-08-22 stsp
5416 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
5417 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
5418 8e7bd50a 2019-08-22 stsp }
5419 8e7bd50a 2019-08-22 stsp done:
5420 8e7bd50a 2019-08-22 stsp if (repo)
5421 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
5422 8e7bd50a 2019-08-22 stsp if (worktree)
5423 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
5424 8e7bd50a 2019-08-22 stsp free(cwd);
5425 8e7bd50a 2019-08-22 stsp free(repo_path);
5426 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5427 8e7bd50a 2019-08-22 stsp free(commit_id_str);
5428 8e7bd50a 2019-08-22 stsp return error;
5429 8e7bd50a 2019-08-22 stsp }
5430 8e7bd50a 2019-08-22 stsp
5431 8e7bd50a 2019-08-22 stsp __dead static void
5432 d00136be 2019-03-26 stsp usage_add(void)
5433 d00136be 2019-03-26 stsp {
5434 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
5435 022fae89 2019-12-06 tracey getprogname());
5436 d00136be 2019-03-26 stsp exit(1);
5437 d00136be 2019-03-26 stsp }
5438 d00136be 2019-03-26 stsp
5439 d00136be 2019-03-26 stsp static const struct got_error *
5440 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
5441 4e68cba3 2019-11-23 stsp {
5442 4e68cba3 2019-11-23 stsp while (path[0] == '/')
5443 4e68cba3 2019-11-23 stsp path++;
5444 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
5445 4e68cba3 2019-11-23 stsp return NULL;
5446 4e68cba3 2019-11-23 stsp }
5447 4e68cba3 2019-11-23 stsp
5448 4e68cba3 2019-11-23 stsp static const struct got_error *
5449 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
5450 d00136be 2019-03-26 stsp {
5451 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
5452 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
5453 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
5454 1dd54920 2019-05-11 stsp char *cwd = NULL;
5455 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
5456 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
5457 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
5458 1dd54920 2019-05-11 stsp
5459 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
5460 d00136be 2019-03-26 stsp
5461 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
5462 d00136be 2019-03-26 stsp switch (ch) {
5463 022fae89 2019-12-06 tracey case 'I':
5464 022fae89 2019-12-06 tracey no_ignores = 1;
5465 022fae89 2019-12-06 tracey break;
5466 4e68cba3 2019-11-23 stsp case 'R':
5467 4e68cba3 2019-11-23 stsp can_recurse = 1;
5468 4e68cba3 2019-11-23 stsp break;
5469 d00136be 2019-03-26 stsp default:
5470 d00136be 2019-03-26 stsp usage_add();
5471 d00136be 2019-03-26 stsp /* NOTREACHED */
5472 d00136be 2019-03-26 stsp }
5473 d00136be 2019-03-26 stsp }
5474 d00136be 2019-03-26 stsp
5475 d00136be 2019-03-26 stsp argc -= optind;
5476 d00136be 2019-03-26 stsp argv += optind;
5477 d00136be 2019-03-26 stsp
5478 43012d58 2019-07-14 stsp #ifndef PROFILE
5479 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5480 43012d58 2019-07-14 stsp NULL) == -1)
5481 43012d58 2019-07-14 stsp err(1, "pledge");
5482 43012d58 2019-07-14 stsp #endif
5483 723c305c 2019-05-11 jcs if (argc < 1)
5484 d00136be 2019-03-26 stsp usage_add();
5485 d00136be 2019-03-26 stsp
5486 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
5487 d00136be 2019-03-26 stsp if (cwd == NULL) {
5488 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5489 d00136be 2019-03-26 stsp goto done;
5490 d00136be 2019-03-26 stsp }
5491 723c305c 2019-05-11 jcs
5492 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5493 d00136be 2019-03-26 stsp if (error)
5494 d00136be 2019-03-26 stsp goto done;
5495 d00136be 2019-03-26 stsp
5496 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5497 c9956ddf 2019-09-08 stsp NULL);
5498 031a5338 2019-03-26 stsp if (error != NULL)
5499 031a5338 2019-03-26 stsp goto done;
5500 031a5338 2019-03-26 stsp
5501 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5502 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5503 d00136be 2019-03-26 stsp if (error)
5504 d00136be 2019-03-26 stsp goto done;
5505 d00136be 2019-03-26 stsp
5506 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5507 6d022e97 2019-08-04 stsp if (error)
5508 022fae89 2019-12-06 tracey goto done;
5509 022fae89 2019-12-06 tracey
5510 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
5511 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5512 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
5513 6d022e97 2019-08-04 stsp goto done;
5514 723c305c 2019-05-11 jcs
5515 022fae89 2019-12-06 tracey }
5516 022fae89 2019-12-06 tracey
5517 4e68cba3 2019-11-23 stsp if (!can_recurse) {
5518 4e68cba3 2019-11-23 stsp char *ondisk_path;
5519 4e68cba3 2019-11-23 stsp struct stat sb;
5520 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
5521 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
5522 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
5523 4e68cba3 2019-11-23 stsp pe->path) == -1) {
5524 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
5525 4e68cba3 2019-11-23 stsp goto done;
5526 4e68cba3 2019-11-23 stsp }
5527 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
5528 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
5529 4e68cba3 2019-11-23 stsp free(ondisk_path);
5530 4e68cba3 2019-11-23 stsp continue;
5531 4e68cba3 2019-11-23 stsp }
5532 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
5533 4e68cba3 2019-11-23 stsp ondisk_path);
5534 4e68cba3 2019-11-23 stsp free(ondisk_path);
5535 4e68cba3 2019-11-23 stsp goto done;
5536 4e68cba3 2019-11-23 stsp }
5537 4e68cba3 2019-11-23 stsp free(ondisk_path);
5538 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
5539 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5540 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
5541 4e68cba3 2019-11-23 stsp goto done;
5542 4e68cba3 2019-11-23 stsp }
5543 4e68cba3 2019-11-23 stsp }
5544 4e68cba3 2019-11-23 stsp }
5545 022fae89 2019-12-06 tracey
5546 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
5547 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
5548 d00136be 2019-03-26 stsp done:
5549 031a5338 2019-03-26 stsp if (repo)
5550 031a5338 2019-03-26 stsp got_repo_close(repo);
5551 d00136be 2019-03-26 stsp if (worktree)
5552 d00136be 2019-03-26 stsp got_worktree_close(worktree);
5553 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
5554 1dd54920 2019-05-11 stsp free((char *)pe->path);
5555 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
5556 2ec1f75b 2019-03-26 stsp free(cwd);
5557 2ec1f75b 2019-03-26 stsp return error;
5558 2ec1f75b 2019-03-26 stsp }
5559 2ec1f75b 2019-03-26 stsp
5560 2ec1f75b 2019-03-26 stsp __dead static void
5561 648e4ef7 2019-07-09 stsp usage_remove(void)
5562 2ec1f75b 2019-03-26 stsp {
5563 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
5564 f2a9dc41 2019-12-13 tracey getprogname());
5565 2ec1f75b 2019-03-26 stsp exit(1);
5566 2a06fe5f 2019-08-24 stsp }
5567 2a06fe5f 2019-08-24 stsp
5568 2a06fe5f 2019-08-24 stsp static const struct got_error *
5569 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
5570 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
5571 2a06fe5f 2019-08-24 stsp {
5572 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
5573 f2a9dc41 2019-12-13 tracey path++;
5574 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
5575 2a06fe5f 2019-08-24 stsp return NULL;
5576 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
5577 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
5578 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
5579 2a06fe5f 2019-08-24 stsp return NULL;
5580 2ec1f75b 2019-03-26 stsp }
5581 2ec1f75b 2019-03-26 stsp
5582 2ec1f75b 2019-03-26 stsp static const struct got_error *
5583 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
5584 2ec1f75b 2019-03-26 stsp {
5585 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
5586 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
5587 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
5588 17ed4618 2019-06-02 stsp char *cwd = NULL;
5589 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
5590 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
5591 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
5592 2ec1f75b 2019-03-26 stsp
5593 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
5594 17ed4618 2019-06-02 stsp
5595 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
5596 2ec1f75b 2019-03-26 stsp switch (ch) {
5597 2ec1f75b 2019-03-26 stsp case 'f':
5598 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
5599 2ec1f75b 2019-03-26 stsp break;
5600 70e3e7f5 2019-12-13 tracey case 'k':
5601 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
5602 70e3e7f5 2019-12-13 tracey break;
5603 f2a9dc41 2019-12-13 tracey case 'R':
5604 f2a9dc41 2019-12-13 tracey can_recurse = 1;
5605 f2a9dc41 2019-12-13 tracey break;
5606 2ec1f75b 2019-03-26 stsp default:
5607 f2a9dc41 2019-12-13 tracey usage_remove();
5608 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
5609 2ec1f75b 2019-03-26 stsp }
5610 2ec1f75b 2019-03-26 stsp }
5611 2ec1f75b 2019-03-26 stsp
5612 2ec1f75b 2019-03-26 stsp argc -= optind;
5613 2ec1f75b 2019-03-26 stsp argv += optind;
5614 2ec1f75b 2019-03-26 stsp
5615 43012d58 2019-07-14 stsp #ifndef PROFILE
5616 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
5617 43012d58 2019-07-14 stsp NULL) == -1)
5618 43012d58 2019-07-14 stsp err(1, "pledge");
5619 43012d58 2019-07-14 stsp #endif
5620 17ed4618 2019-06-02 stsp if (argc < 1)
5621 648e4ef7 2019-07-09 stsp usage_remove();
5622 2ec1f75b 2019-03-26 stsp
5623 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
5624 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
5625 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5626 2ec1f75b 2019-03-26 stsp goto done;
5627 2ec1f75b 2019-03-26 stsp }
5628 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
5629 2ec1f75b 2019-03-26 stsp if (error)
5630 2ec1f75b 2019-03-26 stsp goto done;
5631 2ec1f75b 2019-03-26 stsp
5632 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5633 c9956ddf 2019-09-08 stsp NULL);
5634 2af4a041 2019-05-11 jcs if (error)
5635 2ec1f75b 2019-03-26 stsp goto done;
5636 2ec1f75b 2019-03-26 stsp
5637 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5638 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5639 2ec1f75b 2019-03-26 stsp if (error)
5640 2ec1f75b 2019-03-26 stsp goto done;
5641 2ec1f75b 2019-03-26 stsp
5642 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5643 6d022e97 2019-08-04 stsp if (error)
5644 6d022e97 2019-08-04 stsp goto done;
5645 17ed4618 2019-06-02 stsp
5646 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
5647 f2a9dc41 2019-12-13 tracey char *ondisk_path;
5648 f2a9dc41 2019-12-13 tracey struct stat sb;
5649 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
5650 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
5651 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
5652 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
5653 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
5654 f2a9dc41 2019-12-13 tracey goto done;
5655 f2a9dc41 2019-12-13 tracey }
5656 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
5657 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
5658 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5659 f2a9dc41 2019-12-13 tracey continue;
5660 f2a9dc41 2019-12-13 tracey }
5661 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
5662 f2a9dc41 2019-12-13 tracey ondisk_path);
5663 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5664 f2a9dc41 2019-12-13 tracey goto done;
5665 f2a9dc41 2019-12-13 tracey }
5666 f2a9dc41 2019-12-13 tracey free(ondisk_path);
5667 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
5668 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
5669 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
5670 f2a9dc41 2019-12-13 tracey goto done;
5671 f2a9dc41 2019-12-13 tracey }
5672 f2a9dc41 2019-12-13 tracey }
5673 f2a9dc41 2019-12-13 tracey }
5674 f2a9dc41 2019-12-13 tracey
5675 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
5676 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
5677 a129376b 2019-03-28 stsp done:
5678 a129376b 2019-03-28 stsp if (repo)
5679 a129376b 2019-03-28 stsp got_repo_close(repo);
5680 a129376b 2019-03-28 stsp if (worktree)
5681 a129376b 2019-03-28 stsp got_worktree_close(worktree);
5682 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
5683 17ed4618 2019-06-02 stsp free((char *)pe->path);
5684 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
5685 a129376b 2019-03-28 stsp free(cwd);
5686 a129376b 2019-03-28 stsp return error;
5687 a129376b 2019-03-28 stsp }
5688 a129376b 2019-03-28 stsp
5689 a129376b 2019-03-28 stsp __dead static void
5690 a129376b 2019-03-28 stsp usage_revert(void)
5691 a129376b 2019-03-28 stsp {
5692 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
5693 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
5694 a129376b 2019-03-28 stsp exit(1);
5695 a129376b 2019-03-28 stsp }
5696 a129376b 2019-03-28 stsp
5697 1ee397ad 2019-07-12 stsp static const struct got_error *
5698 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
5699 a129376b 2019-03-28 stsp {
5700 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
5701 fb9704af 2020-01-27 stsp return NULL;
5702 fb9704af 2020-01-27 stsp
5703 a129376b 2019-03-28 stsp while (path[0] == '/')
5704 a129376b 2019-03-28 stsp path++;
5705 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
5706 33aa809d 2019-08-08 stsp return NULL;
5707 33aa809d 2019-08-08 stsp }
5708 33aa809d 2019-08-08 stsp
5709 33aa809d 2019-08-08 stsp struct choose_patch_arg {
5710 33aa809d 2019-08-08 stsp FILE *patch_script_file;
5711 33aa809d 2019-08-08 stsp const char *action;
5712 33aa809d 2019-08-08 stsp };
5713 33aa809d 2019-08-08 stsp
5714 33aa809d 2019-08-08 stsp static const struct got_error *
5715 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
5716 33aa809d 2019-08-08 stsp int nchanges, const char *action)
5717 33aa809d 2019-08-08 stsp {
5718 33aa809d 2019-08-08 stsp char *line = NULL;
5719 33aa809d 2019-08-08 stsp size_t linesize = 0;
5720 33aa809d 2019-08-08 stsp ssize_t linelen;
5721 33aa809d 2019-08-08 stsp
5722 33aa809d 2019-08-08 stsp switch (status) {
5723 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
5724 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
5725 33aa809d 2019-08-08 stsp break;
5726 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
5727 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
5728 33aa809d 2019-08-08 stsp break;
5729 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
5730 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
5731 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
5732 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5733 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
5734 33aa809d 2019-08-08 stsp printf("%s", line);
5735 33aa809d 2019-08-08 stsp if (ferror(patch_file))
5736 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5737 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
5738 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
5739 33aa809d 2019-08-08 stsp path, n, nchanges, action);
5740 33aa809d 2019-08-08 stsp break;
5741 33aa809d 2019-08-08 stsp default:
5742 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
5743 33aa809d 2019-08-08 stsp }
5744 33aa809d 2019-08-08 stsp
5745 33aa809d 2019-08-08 stsp return NULL;
5746 33aa809d 2019-08-08 stsp }
5747 33aa809d 2019-08-08 stsp
5748 33aa809d 2019-08-08 stsp static const struct got_error *
5749 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
5750 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
5751 33aa809d 2019-08-08 stsp {
5752 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
5753 33aa809d 2019-08-08 stsp char *line = NULL;
5754 33aa809d 2019-08-08 stsp size_t linesize = 0;
5755 33aa809d 2019-08-08 stsp ssize_t linelen;
5756 33aa809d 2019-08-08 stsp int resp = ' ';
5757 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
5758 33aa809d 2019-08-08 stsp
5759 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
5760 33aa809d 2019-08-08 stsp
5761 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
5762 33aa809d 2019-08-08 stsp char *nl;
5763 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5764 33aa809d 2019-08-08 stsp a->action);
5765 33aa809d 2019-08-08 stsp if (err)
5766 33aa809d 2019-08-08 stsp return err;
5767 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
5768 33aa809d 2019-08-08 stsp if (linelen == -1) {
5769 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
5770 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
5771 33aa809d 2019-08-08 stsp return NULL;
5772 33aa809d 2019-08-08 stsp }
5773 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
5774 33aa809d 2019-08-08 stsp if (nl)
5775 33aa809d 2019-08-08 stsp *nl = '\0';
5776 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
5777 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5778 33aa809d 2019-08-08 stsp printf("y\n");
5779 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
5780 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5781 33aa809d 2019-08-08 stsp printf("n\n");
5782 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
5783 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
5784 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5785 33aa809d 2019-08-08 stsp printf("q\n");
5786 33aa809d 2019-08-08 stsp } else
5787 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
5788 33aa809d 2019-08-08 stsp free(line);
5789 33aa809d 2019-08-08 stsp return NULL;
5790 33aa809d 2019-08-08 stsp }
5791 33aa809d 2019-08-08 stsp
5792 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
5793 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
5794 33aa809d 2019-08-08 stsp a->action);
5795 33aa809d 2019-08-08 stsp if (err)
5796 33aa809d 2019-08-08 stsp return err;
5797 33aa809d 2019-08-08 stsp resp = getchar();
5798 33aa809d 2019-08-08 stsp if (resp == '\n')
5799 33aa809d 2019-08-08 stsp resp = getchar();
5800 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
5801 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
5802 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5803 33aa809d 2019-08-08 stsp resp = ' ';
5804 33aa809d 2019-08-08 stsp }
5805 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
5806 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
5807 33aa809d 2019-08-08 stsp resp = ' ';
5808 33aa809d 2019-08-08 stsp }
5809 33aa809d 2019-08-08 stsp }
5810 33aa809d 2019-08-08 stsp
5811 33aa809d 2019-08-08 stsp if (resp == 'y')
5812 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
5813 33aa809d 2019-08-08 stsp else if (resp == 'n')
5814 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
5815 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
5816 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
5817 33aa809d 2019-08-08 stsp
5818 1ee397ad 2019-07-12 stsp return NULL;
5819 a129376b 2019-03-28 stsp }
5820 a129376b 2019-03-28 stsp
5821 33aa809d 2019-08-08 stsp
5822 a129376b 2019-03-28 stsp static const struct got_error *
5823 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
5824 a129376b 2019-03-28 stsp {
5825 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
5826 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
5827 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
5828 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
5829 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
5830 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
5831 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
5832 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
5833 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
5834 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
5835 a129376b 2019-03-28 stsp
5836 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
5837 e20a8b6f 2019-06-04 stsp
5838 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
5839 a129376b 2019-03-28 stsp switch (ch) {
5840 33aa809d 2019-08-08 stsp case 'p':
5841 33aa809d 2019-08-08 stsp pflag = 1;
5842 33aa809d 2019-08-08 stsp break;
5843 33aa809d 2019-08-08 stsp case 'F':
5844 33aa809d 2019-08-08 stsp patch_script_path = optarg;
5845 33aa809d 2019-08-08 stsp break;
5846 0f6d7415 2019-08-08 stsp case 'R':
5847 0f6d7415 2019-08-08 stsp can_recurse = 1;
5848 0f6d7415 2019-08-08 stsp break;
5849 a129376b 2019-03-28 stsp default:
5850 a129376b 2019-03-28 stsp usage_revert();
5851 a129376b 2019-03-28 stsp /* NOTREACHED */
5852 a129376b 2019-03-28 stsp }
5853 a129376b 2019-03-28 stsp }
5854 a129376b 2019-03-28 stsp
5855 a129376b 2019-03-28 stsp argc -= optind;
5856 a129376b 2019-03-28 stsp argv += optind;
5857 a129376b 2019-03-28 stsp
5858 43012d58 2019-07-14 stsp #ifndef PROFILE
5859 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5860 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5861 43012d58 2019-07-14 stsp err(1, "pledge");
5862 43012d58 2019-07-14 stsp #endif
5863 e20a8b6f 2019-06-04 stsp if (argc < 1)
5864 a129376b 2019-03-28 stsp usage_revert();
5865 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
5866 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
5867 a129376b 2019-03-28 stsp
5868 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
5869 a129376b 2019-03-28 stsp if (cwd == NULL) {
5870 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
5871 a129376b 2019-03-28 stsp goto done;
5872 a129376b 2019-03-28 stsp }
5873 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
5874 2ec1f75b 2019-03-26 stsp if (error)
5875 2ec1f75b 2019-03-26 stsp goto done;
5876 a129376b 2019-03-28 stsp
5877 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5878 c9956ddf 2019-09-08 stsp NULL);
5879 a129376b 2019-03-28 stsp if (error != NULL)
5880 a129376b 2019-03-28 stsp goto done;
5881 a129376b 2019-03-28 stsp
5882 33aa809d 2019-08-08 stsp if (patch_script_path) {
5883 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
5884 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
5885 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
5886 33aa809d 2019-08-08 stsp patch_script_path);
5887 33aa809d 2019-08-08 stsp goto done;
5888 33aa809d 2019-08-08 stsp }
5889 33aa809d 2019-08-08 stsp }
5890 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
5891 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5892 a129376b 2019-03-28 stsp if (error)
5893 a129376b 2019-03-28 stsp goto done;
5894 a129376b 2019-03-28 stsp
5895 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5896 6d022e97 2019-08-04 stsp if (error)
5897 6d022e97 2019-08-04 stsp goto done;
5898 00db391e 2019-08-03 stsp
5899 0f6d7415 2019-08-08 stsp if (!can_recurse) {
5900 0f6d7415 2019-08-08 stsp char *ondisk_path;
5901 0f6d7415 2019-08-08 stsp struct stat sb;
5902 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
5903 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
5904 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
5905 0f6d7415 2019-08-08 stsp pe->path) == -1) {
5906 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
5907 0f6d7415 2019-08-08 stsp goto done;
5908 0f6d7415 2019-08-08 stsp }
5909 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
5910 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
5911 0f6d7415 2019-08-08 stsp free(ondisk_path);
5912 0f6d7415 2019-08-08 stsp continue;
5913 0f6d7415 2019-08-08 stsp }
5914 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
5915 0f6d7415 2019-08-08 stsp ondisk_path);
5916 0f6d7415 2019-08-08 stsp free(ondisk_path);
5917 0f6d7415 2019-08-08 stsp goto done;
5918 0f6d7415 2019-08-08 stsp }
5919 0f6d7415 2019-08-08 stsp free(ondisk_path);
5920 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
5921 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
5922 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
5923 0f6d7415 2019-08-08 stsp goto done;
5924 0f6d7415 2019-08-08 stsp }
5925 0f6d7415 2019-08-08 stsp }
5926 0f6d7415 2019-08-08 stsp }
5927 0f6d7415 2019-08-08 stsp
5928 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
5929 33aa809d 2019-08-08 stsp cpa.action = "revert";
5930 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
5931 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
5932 2ec1f75b 2019-03-26 stsp done:
5933 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
5934 33aa809d 2019-08-08 stsp error == NULL)
5935 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
5936 2ec1f75b 2019-03-26 stsp if (repo)
5937 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
5938 2ec1f75b 2019-03-26 stsp if (worktree)
5939 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
5940 2ec1f75b 2019-03-26 stsp free(path);
5941 d00136be 2019-03-26 stsp free(cwd);
5942 6bad629b 2019-02-04 stsp return error;
5943 c4296144 2019-05-09 stsp }
5944 c4296144 2019-05-09 stsp
5945 c4296144 2019-05-09 stsp __dead static void
5946 c4296144 2019-05-09 stsp usage_commit(void)
5947 c4296144 2019-05-09 stsp {
5948 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
5949 5c1e53bc 2019-07-28 stsp getprogname());
5950 c4296144 2019-05-09 stsp exit(1);
5951 33ad4cbe 2019-05-12 jcs }
5952 33ad4cbe 2019-05-12 jcs
5953 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
5954 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
5955 e2ba3d07 2019-05-13 stsp const char *editor;
5956 e0870e44 2019-05-13 stsp const char *worktree_path;
5957 76d98825 2019-06-03 stsp const char *branch_name;
5958 314a6357 2019-05-13 stsp const char *repo_path;
5959 e0870e44 2019-05-13 stsp char *logmsg_path;
5960 e2ba3d07 2019-05-13 stsp
5961 e2ba3d07 2019-05-13 stsp };
5962 e0870e44 2019-05-13 stsp
5963 33ad4cbe 2019-05-12 jcs static const struct got_error *
5964 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
5965 33ad4cbe 2019-05-12 jcs void *arg)
5966 33ad4cbe 2019-05-12 jcs {
5967 76d98825 2019-06-03 stsp char *initial_content = NULL;
5968 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
5969 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
5970 e0870e44 2019-05-13 stsp char *template = NULL;
5971 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
5972 3ce1b845 2019-07-15 stsp int fd;
5973 33ad4cbe 2019-05-12 jcs size_t len;
5974 33ad4cbe 2019-05-12 jcs
5975 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
5976 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
5977 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
5978 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
5979 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
5980 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
5981 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
5982 33ad4cbe 2019-05-12 jcs return NULL;
5983 33ad4cbe 2019-05-12 jcs }
5984 33ad4cbe 2019-05-12 jcs
5985 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
5986 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
5987 76d98825 2019-06-03 stsp
5988 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
5989 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
5990 76d98825 2019-06-03 stsp a->branch_name) == -1)
5991 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
5992 e0870e44 2019-05-13 stsp
5993 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
5994 793c30b5 2019-05-13 stsp if (err)
5995 e0870e44 2019-05-13 stsp goto done;
5996 33ad4cbe 2019-05-12 jcs
5997 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
5998 33ad4cbe 2019-05-12 jcs
5999 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
6000 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
6001 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
6002 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
6003 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
6004 33ad4cbe 2019-05-12 jcs }
6005 33ad4cbe 2019-05-12 jcs close(fd);
6006 33ad4cbe 2019-05-12 jcs
6007 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
6008 33ad4cbe 2019-05-12 jcs done:
6009 76d98825 2019-06-03 stsp free(initial_content);
6010 e0870e44 2019-05-13 stsp free(template);
6011 314a6357 2019-05-13 stsp
6012 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
6013 3ce1b845 2019-07-15 stsp if (err == NULL) {
6014 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
6015 3ce1b845 2019-07-15 stsp if (err) {
6016 3ce1b845 2019-07-15 stsp free(*logmsg);
6017 3ce1b845 2019-07-15 stsp *logmsg = NULL;
6018 3ce1b845 2019-07-15 stsp }
6019 3ce1b845 2019-07-15 stsp }
6020 33ad4cbe 2019-05-12 jcs return err;
6021 6bad629b 2019-02-04 stsp }
6022 c4296144 2019-05-09 stsp
6023 c4296144 2019-05-09 stsp static const struct got_error *
6024 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
6025 c4296144 2019-05-09 stsp {
6026 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
6027 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
6028 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
6029 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
6030 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
6031 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
6032 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
6033 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
6034 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
6035 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
6036 5c1e53bc 2019-07-28 stsp
6037 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
6038 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
6039 c4296144 2019-05-09 stsp
6040 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
6041 c4296144 2019-05-09 stsp switch (ch) {
6042 c4296144 2019-05-09 stsp case 'm':
6043 c4296144 2019-05-09 stsp logmsg = optarg;
6044 c4296144 2019-05-09 stsp break;
6045 c4296144 2019-05-09 stsp default:
6046 c4296144 2019-05-09 stsp usage_commit();
6047 c4296144 2019-05-09 stsp /* NOTREACHED */
6048 c4296144 2019-05-09 stsp }
6049 c4296144 2019-05-09 stsp }
6050 c4296144 2019-05-09 stsp
6051 c4296144 2019-05-09 stsp argc -= optind;
6052 c4296144 2019-05-09 stsp argv += optind;
6053 c4296144 2019-05-09 stsp
6054 43012d58 2019-07-14 stsp #ifndef PROFILE
6055 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6056 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6057 43012d58 2019-07-14 stsp err(1, "pledge");
6058 43012d58 2019-07-14 stsp #endif
6059 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
6060 c4296144 2019-05-09 stsp if (cwd == NULL) {
6061 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
6062 c4296144 2019-05-09 stsp goto done;
6063 c4296144 2019-05-09 stsp }
6064 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
6065 7d5807f4 2019-07-11 stsp if (error)
6066 7d5807f4 2019-07-11 stsp goto done;
6067 7d5807f4 2019-07-11 stsp
6068 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6069 c4296144 2019-05-09 stsp if (error)
6070 a698f62e 2019-07-25 stsp goto done;
6071 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
6072 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
6073 c4296144 2019-05-09 stsp goto done;
6074 a698f62e 2019-07-25 stsp }
6075 5c1e53bc 2019-07-28 stsp
6076 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6077 916f288c 2019-07-30 stsp worktree);
6078 5c1e53bc 2019-07-28 stsp if (error)
6079 5c1e53bc 2019-07-28 stsp goto done;
6080 c4296144 2019-05-09 stsp
6081 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
6082 c9956ddf 2019-09-08 stsp if (error)
6083 c9956ddf 2019-09-08 stsp goto done;
6084 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6085 c9956ddf 2019-09-08 stsp gitconfig_path);
6086 c4296144 2019-05-09 stsp if (error != NULL)
6087 c4296144 2019-05-09 stsp goto done;
6088 c4296144 2019-05-09 stsp
6089 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
6090 aba9c984 2019-09-08 stsp if (error)
6091 aba9c984 2019-09-08 stsp return error;
6092 aba9c984 2019-09-08 stsp
6093 314a6357 2019-05-13 stsp /*
6094 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
6095 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
6096 314a6357 2019-05-13 stsp */
6097 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
6098 314a6357 2019-05-13 stsp error = get_editor(&editor);
6099 314a6357 2019-05-13 stsp else
6100 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6101 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6102 c4296144 2019-05-09 stsp if (error)
6103 c4296144 2019-05-09 stsp goto done;
6104 c4296144 2019-05-09 stsp
6105 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6106 087fb88c 2019-08-04 stsp if (error)
6107 087fb88c 2019-08-04 stsp goto done;
6108 087fb88c 2019-08-04 stsp
6109 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
6110 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
6111 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
6112 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
6113 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
6114 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
6115 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
6116 916f288c 2019-07-30 stsp goto done;
6117 916f288c 2019-07-30 stsp }
6118 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
6119 916f288c 2019-07-30 stsp }
6120 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
6121 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
6122 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
6123 e0870e44 2019-05-13 stsp if (error) {
6124 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
6125 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
6126 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
6127 c4296144 2019-05-09 stsp goto done;
6128 e0870e44 2019-05-13 stsp }
6129 c4296144 2019-05-09 stsp
6130 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
6131 c4296144 2019-05-09 stsp if (error)
6132 c4296144 2019-05-09 stsp goto done;
6133 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
6134 c4296144 2019-05-09 stsp done:
6135 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
6136 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
6137 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
6138 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
6139 7266f21f 2019-10-21 stsp error == NULL)
6140 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
6141 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
6142 c4296144 2019-05-09 stsp if (repo)
6143 c4296144 2019-05-09 stsp got_repo_close(repo);
6144 c4296144 2019-05-09 stsp if (worktree)
6145 c4296144 2019-05-09 stsp got_worktree_close(worktree);
6146 c4296144 2019-05-09 stsp free(cwd);
6147 c4296144 2019-05-09 stsp free(id_str);
6148 c9956ddf 2019-09-08 stsp free(gitconfig_path);
6149 e2ba3d07 2019-05-13 stsp free(editor);
6150 aba9c984 2019-09-08 stsp free(author);
6151 234035bc 2019-06-01 stsp return error;
6152 234035bc 2019-06-01 stsp }
6153 234035bc 2019-06-01 stsp
6154 234035bc 2019-06-01 stsp __dead static void
6155 234035bc 2019-06-01 stsp usage_cherrypick(void)
6156 234035bc 2019-06-01 stsp {
6157 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
6158 234035bc 2019-06-01 stsp exit(1);
6159 234035bc 2019-06-01 stsp }
6160 234035bc 2019-06-01 stsp
6161 234035bc 2019-06-01 stsp static const struct got_error *
6162 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
6163 234035bc 2019-06-01 stsp {
6164 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
6165 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
6166 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
6167 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
6168 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
6169 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
6170 234035bc 2019-06-01 stsp struct got_object_qid *pid;
6171 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
6172 234035bc 2019-06-01 stsp int ch, did_something = 0;
6173 234035bc 2019-06-01 stsp
6174 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6175 234035bc 2019-06-01 stsp switch (ch) {
6176 234035bc 2019-06-01 stsp default:
6177 234035bc 2019-06-01 stsp usage_cherrypick();
6178 234035bc 2019-06-01 stsp /* NOTREACHED */
6179 234035bc 2019-06-01 stsp }
6180 234035bc 2019-06-01 stsp }
6181 234035bc 2019-06-01 stsp
6182 234035bc 2019-06-01 stsp argc -= optind;
6183 234035bc 2019-06-01 stsp argv += optind;
6184 234035bc 2019-06-01 stsp
6185 43012d58 2019-07-14 stsp #ifndef PROFILE
6186 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6187 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6188 43012d58 2019-07-14 stsp err(1, "pledge");
6189 43012d58 2019-07-14 stsp #endif
6190 234035bc 2019-06-01 stsp if (argc != 1)
6191 234035bc 2019-06-01 stsp usage_cherrypick();
6192 234035bc 2019-06-01 stsp
6193 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
6194 234035bc 2019-06-01 stsp if (cwd == NULL) {
6195 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
6196 234035bc 2019-06-01 stsp goto done;
6197 234035bc 2019-06-01 stsp }
6198 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
6199 234035bc 2019-06-01 stsp if (error)
6200 234035bc 2019-06-01 stsp goto done;
6201 234035bc 2019-06-01 stsp
6202 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6203 c9956ddf 2019-09-08 stsp NULL);
6204 234035bc 2019-06-01 stsp if (error != NULL)
6205 234035bc 2019-06-01 stsp goto done;
6206 234035bc 2019-06-01 stsp
6207 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6208 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6209 234035bc 2019-06-01 stsp if (error)
6210 234035bc 2019-06-01 stsp goto done;
6211 234035bc 2019-06-01 stsp
6212 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6213 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6214 234035bc 2019-06-01 stsp if (error != NULL) {
6215 234035bc 2019-06-01 stsp struct got_reference *ref;
6216 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6217 234035bc 2019-06-01 stsp goto done;
6218 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6219 234035bc 2019-06-01 stsp if (error != NULL)
6220 234035bc 2019-06-01 stsp goto done;
6221 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
6222 234035bc 2019-06-01 stsp got_ref_close(ref);
6223 234035bc 2019-06-01 stsp if (error != NULL)
6224 234035bc 2019-06-01 stsp goto done;
6225 234035bc 2019-06-01 stsp }
6226 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
6227 234035bc 2019-06-01 stsp if (error)
6228 234035bc 2019-06-01 stsp goto done;
6229 234035bc 2019-06-01 stsp
6230 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
6231 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
6232 234035bc 2019-06-01 stsp if (error != NULL)
6233 234035bc 2019-06-01 stsp goto done;
6234 234035bc 2019-06-01 stsp
6235 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6236 234035bc 2019-06-01 stsp if (error) {
6237 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
6238 234035bc 2019-06-01 stsp goto done;
6239 234035bc 2019-06-01 stsp error = NULL;
6240 234035bc 2019-06-01 stsp } else {
6241 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
6242 234035bc 2019-06-01 stsp goto done;
6243 234035bc 2019-06-01 stsp }
6244 234035bc 2019-06-01 stsp
6245 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6246 234035bc 2019-06-01 stsp if (error)
6247 234035bc 2019-06-01 stsp goto done;
6248 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6249 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
6250 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
6251 03415a1a 2019-06-02 stsp NULL);
6252 234035bc 2019-06-01 stsp if (error != NULL)
6253 234035bc 2019-06-01 stsp goto done;
6254 234035bc 2019-06-01 stsp
6255 234035bc 2019-06-01 stsp if (did_something)
6256 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
6257 234035bc 2019-06-01 stsp done:
6258 234035bc 2019-06-01 stsp if (commit)
6259 234035bc 2019-06-01 stsp got_object_commit_close(commit);
6260 234035bc 2019-06-01 stsp free(commit_id_str);
6261 234035bc 2019-06-01 stsp if (head_ref)
6262 234035bc 2019-06-01 stsp got_ref_close(head_ref);
6263 234035bc 2019-06-01 stsp if (worktree)
6264 234035bc 2019-06-01 stsp got_worktree_close(worktree);
6265 234035bc 2019-06-01 stsp if (repo)
6266 234035bc 2019-06-01 stsp got_repo_close(repo);
6267 c4296144 2019-05-09 stsp return error;
6268 c4296144 2019-05-09 stsp }
6269 5ef14e63 2019-06-02 stsp
6270 5ef14e63 2019-06-02 stsp __dead static void
6271 5ef14e63 2019-06-02 stsp usage_backout(void)
6272 5ef14e63 2019-06-02 stsp {
6273 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
6274 5ef14e63 2019-06-02 stsp exit(1);
6275 5ef14e63 2019-06-02 stsp }
6276 5ef14e63 2019-06-02 stsp
6277 5ef14e63 2019-06-02 stsp static const struct got_error *
6278 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
6279 5ef14e63 2019-06-02 stsp {
6280 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
6281 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
6282 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
6283 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
6284 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
6285 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
6286 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
6287 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
6288 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
6289 5ef14e63 2019-06-02 stsp
6290 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6291 5ef14e63 2019-06-02 stsp switch (ch) {
6292 5ef14e63 2019-06-02 stsp default:
6293 5ef14e63 2019-06-02 stsp usage_backout();
6294 5ef14e63 2019-06-02 stsp /* NOTREACHED */
6295 5ef14e63 2019-06-02 stsp }
6296 5ef14e63 2019-06-02 stsp }
6297 5ef14e63 2019-06-02 stsp
6298 5ef14e63 2019-06-02 stsp argc -= optind;
6299 5ef14e63 2019-06-02 stsp argv += optind;
6300 5ef14e63 2019-06-02 stsp
6301 43012d58 2019-07-14 stsp #ifndef PROFILE
6302 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6303 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6304 43012d58 2019-07-14 stsp err(1, "pledge");
6305 43012d58 2019-07-14 stsp #endif
6306 5ef14e63 2019-06-02 stsp if (argc != 1)
6307 5ef14e63 2019-06-02 stsp usage_backout();
6308 5ef14e63 2019-06-02 stsp
6309 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
6310 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
6311 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
6312 5ef14e63 2019-06-02 stsp goto done;
6313 5ef14e63 2019-06-02 stsp }
6314 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
6315 5ef14e63 2019-06-02 stsp if (error)
6316 5ef14e63 2019-06-02 stsp goto done;
6317 5ef14e63 2019-06-02 stsp
6318 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6319 c9956ddf 2019-09-08 stsp NULL);
6320 5ef14e63 2019-06-02 stsp if (error != NULL)
6321 5ef14e63 2019-06-02 stsp goto done;
6322 5ef14e63 2019-06-02 stsp
6323 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6324 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6325 5ef14e63 2019-06-02 stsp if (error)
6326 5ef14e63 2019-06-02 stsp goto done;
6327 5ef14e63 2019-06-02 stsp
6328 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
6329 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
6330 5ef14e63 2019-06-02 stsp if (error != NULL) {
6331 5ef14e63 2019-06-02 stsp struct got_reference *ref;
6332 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
6333 5ef14e63 2019-06-02 stsp goto done;
6334 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
6335 5ef14e63 2019-06-02 stsp if (error != NULL)
6336 5ef14e63 2019-06-02 stsp goto done;
6337 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
6338 5ef14e63 2019-06-02 stsp got_ref_close(ref);
6339 5ef14e63 2019-06-02 stsp if (error != NULL)
6340 5ef14e63 2019-06-02 stsp goto done;
6341 5ef14e63 2019-06-02 stsp }
6342 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
6343 5ef14e63 2019-06-02 stsp if (error)
6344 5ef14e63 2019-06-02 stsp goto done;
6345 5ef14e63 2019-06-02 stsp
6346 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
6347 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
6348 5ef14e63 2019-06-02 stsp if (error != NULL)
6349 5ef14e63 2019-06-02 stsp goto done;
6350 5ef14e63 2019-06-02 stsp
6351 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
6352 5ef14e63 2019-06-02 stsp if (error)
6353 5ef14e63 2019-06-02 stsp goto done;
6354 5ef14e63 2019-06-02 stsp
6355 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6356 5ef14e63 2019-06-02 stsp if (error)
6357 5ef14e63 2019-06-02 stsp goto done;
6358 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
6359 5ef14e63 2019-06-02 stsp if (pid == NULL) {
6360 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
6361 5ef14e63 2019-06-02 stsp goto done;
6362 5ef14e63 2019-06-02 stsp }
6363 5ef14e63 2019-06-02 stsp
6364 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
6365 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
6366 5ef14e63 2019-06-02 stsp if (error != NULL)
6367 5ef14e63 2019-06-02 stsp goto done;
6368 5ef14e63 2019-06-02 stsp
6369 5ef14e63 2019-06-02 stsp if (did_something)
6370 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
6371 5ef14e63 2019-06-02 stsp done:
6372 5ef14e63 2019-06-02 stsp if (commit)
6373 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
6374 5ef14e63 2019-06-02 stsp free(commit_id_str);
6375 5ef14e63 2019-06-02 stsp if (head_ref)
6376 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
6377 818c7501 2019-07-11 stsp if (worktree)
6378 818c7501 2019-07-11 stsp got_worktree_close(worktree);
6379 818c7501 2019-07-11 stsp if (repo)
6380 818c7501 2019-07-11 stsp got_repo_close(repo);
6381 818c7501 2019-07-11 stsp return error;
6382 818c7501 2019-07-11 stsp }
6383 818c7501 2019-07-11 stsp
6384 818c7501 2019-07-11 stsp __dead static void
6385 818c7501 2019-07-11 stsp usage_rebase(void)
6386 818c7501 2019-07-11 stsp {
6387 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
6388 af54c8f8 2019-07-11 stsp getprogname());
6389 818c7501 2019-07-11 stsp exit(1);
6390 0ebf8283 2019-07-24 stsp }
6391 0ebf8283 2019-07-24 stsp
6392 0ebf8283 2019-07-24 stsp void
6393 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
6394 0ebf8283 2019-07-24 stsp {
6395 0ebf8283 2019-07-24 stsp char *nl;
6396 0ebf8283 2019-07-24 stsp size_t len;
6397 0ebf8283 2019-07-24 stsp
6398 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
6399 0ebf8283 2019-07-24 stsp if (len > limit)
6400 0ebf8283 2019-07-24 stsp len = limit;
6401 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
6402 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
6403 0ebf8283 2019-07-24 stsp if (nl)
6404 0ebf8283 2019-07-24 stsp *nl = '\0';
6405 0ebf8283 2019-07-24 stsp }
6406 0ebf8283 2019-07-24 stsp
6407 0ebf8283 2019-07-24 stsp static const struct got_error *
6408 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
6409 0ebf8283 2019-07-24 stsp {
6410 5943eee2 2019-08-13 stsp const struct got_error *err;
6411 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
6412 5943eee2 2019-08-13 stsp const char *s;
6413 0ebf8283 2019-07-24 stsp
6414 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
6415 5943eee2 2019-08-13 stsp if (err)
6416 5943eee2 2019-08-13 stsp return err;
6417 0ebf8283 2019-07-24 stsp
6418 5943eee2 2019-08-13 stsp s = logmsg0;
6419 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
6420 5943eee2 2019-08-13 stsp s++;
6421 5943eee2 2019-08-13 stsp
6422 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
6423 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
6424 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
6425 5943eee2 2019-08-13 stsp goto done;
6426 5943eee2 2019-08-13 stsp }
6427 0ebf8283 2019-07-24 stsp
6428 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
6429 5943eee2 2019-08-13 stsp done:
6430 5943eee2 2019-08-13 stsp free(logmsg0);
6431 a0ea4fc0 2020-02-28 stsp return err;
6432 a0ea4fc0 2020-02-28 stsp }
6433 a0ea4fc0 2020-02-28 stsp
6434 a0ea4fc0 2020-02-28 stsp static const struct got_error *
6435 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
6436 a0ea4fc0 2020-02-28 stsp {
6437 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
6438 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
6439 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
6440 a0ea4fc0 2020-02-28 stsp
6441 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
6442 a0ea4fc0 2020-02-28 stsp if (err)
6443 a0ea4fc0 2020-02-28 stsp return err;
6444 a0ea4fc0 2020-02-28 stsp
6445 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
6446 a0ea4fc0 2020-02-28 stsp if (err)
6447 a0ea4fc0 2020-02-28 stsp goto done;
6448 a0ea4fc0 2020-02-28 stsp
6449 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
6450 a0ea4fc0 2020-02-28 stsp
6451 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
6452 a0ea4fc0 2020-02-28 stsp if (err)
6453 a0ea4fc0 2020-02-28 stsp goto done;
6454 a0ea4fc0 2020-02-28 stsp
6455 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
6456 a0ea4fc0 2020-02-28 stsp done:
6457 a0ea4fc0 2020-02-28 stsp free(id_str);
6458 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
6459 a0ea4fc0 2020-02-28 stsp free(logmsg);
6460 5943eee2 2019-08-13 stsp return err;
6461 818c7501 2019-07-11 stsp }
6462 818c7501 2019-07-11 stsp
6463 818c7501 2019-07-11 stsp static const struct got_error *
6464 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
6465 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
6466 818c7501 2019-07-11 stsp {
6467 818c7501 2019-07-11 stsp const struct got_error *err;
6468 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6469 818c7501 2019-07-11 stsp
6470 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
6471 818c7501 2019-07-11 stsp if (err)
6472 818c7501 2019-07-11 stsp goto done;
6473 818c7501 2019-07-11 stsp
6474 ff0d2220 2019-07-11 stsp if (new_id) {
6475 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
6476 ff0d2220 2019-07-11 stsp if (err)
6477 ff0d2220 2019-07-11 stsp goto done;
6478 ff0d2220 2019-07-11 stsp }
6479 818c7501 2019-07-11 stsp
6480 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
6481 ff0d2220 2019-07-11 stsp if (new_id_str)
6482 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
6483 0ebf8283 2019-07-24 stsp
6484 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6485 0ebf8283 2019-07-24 stsp if (err)
6486 0ebf8283 2019-07-24 stsp goto done;
6487 0ebf8283 2019-07-24 stsp
6488 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
6489 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6490 818c7501 2019-07-11 stsp done:
6491 818c7501 2019-07-11 stsp free(old_id_str);
6492 818c7501 2019-07-11 stsp free(new_id_str);
6493 272a1371 2020-02-28 stsp free(logmsg);
6494 818c7501 2019-07-11 stsp return err;
6495 818c7501 2019-07-11 stsp }
6496 818c7501 2019-07-11 stsp
6497 1ee397ad 2019-07-12 stsp static const struct got_error *
6498 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
6499 818c7501 2019-07-11 stsp {
6500 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
6501 818c7501 2019-07-11 stsp
6502 818c7501 2019-07-11 stsp while (path[0] == '/')
6503 818c7501 2019-07-11 stsp path++;
6504 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
6505 818c7501 2019-07-11 stsp
6506 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
6507 1ee397ad 2019-07-12 stsp return NULL;
6508 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
6509 818c7501 2019-07-11 stsp *rebase_status = status;
6510 1ee397ad 2019-07-12 stsp return NULL;
6511 818c7501 2019-07-11 stsp }
6512 818c7501 2019-07-11 stsp
6513 818c7501 2019-07-11 stsp static const struct got_error *
6514 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
6515 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
6516 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
6517 818c7501 2019-07-11 stsp {
6518 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
6519 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
6520 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
6521 818c7501 2019-07-11 stsp }
6522 818c7501 2019-07-11 stsp
6523 818c7501 2019-07-11 stsp static const struct got_error *
6524 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
6525 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6526 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
6527 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
6528 ff0d2220 2019-07-11 stsp {
6529 ff0d2220 2019-07-11 stsp const struct got_error *error;
6530 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
6531 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
6532 ff0d2220 2019-07-11 stsp
6533 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6534 ff0d2220 2019-07-11 stsp if (error)
6535 ff0d2220 2019-07-11 stsp return error;
6536 ff0d2220 2019-07-11 stsp
6537 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
6538 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
6539 ff0d2220 2019-07-11 stsp if (error) {
6540 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
6541 ff0d2220 2019-07-11 stsp goto done;
6542 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
6543 ff0d2220 2019-07-11 stsp } else {
6544 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
6545 ff0d2220 2019-07-11 stsp free(new_commit_id);
6546 ff0d2220 2019-07-11 stsp }
6547 ff0d2220 2019-07-11 stsp done:
6548 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
6549 ff0d2220 2019-07-11 stsp return error;
6550 64c6d990 2019-07-11 stsp }
6551 64c6d990 2019-07-11 stsp
6552 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
6553 64c6d990 2019-07-11 stsp const char *path_prefix;
6554 64c6d990 2019-07-11 stsp size_t len;
6555 8ca9bd68 2019-07-25 stsp int errcode;
6556 64c6d990 2019-07-11 stsp };
6557 64c6d990 2019-07-11 stsp
6558 64c6d990 2019-07-11 stsp static const struct got_error *
6559 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
6560 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
6561 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
6562 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
6563 64c6d990 2019-07-11 stsp {
6564 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
6565 64c6d990 2019-07-11 stsp
6566 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
6567 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
6568 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
6569 64c6d990 2019-07-11 stsp
6570 64c6d990 2019-07-11 stsp return NULL;
6571 64c6d990 2019-07-11 stsp }
6572 64c6d990 2019-07-11 stsp
6573 64c6d990 2019-07-11 stsp static const struct got_error *
6574 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
6575 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
6576 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
6577 64c6d990 2019-07-11 stsp {
6578 64c6d990 2019-07-11 stsp const struct got_error *err;
6579 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
6580 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
6581 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
6582 64c6d990 2019-07-11 stsp
6583 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
6584 64c6d990 2019-07-11 stsp return NULL;
6585 64c6d990 2019-07-11 stsp
6586 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
6587 64c6d990 2019-07-11 stsp if (err)
6588 64c6d990 2019-07-11 stsp goto done;
6589 64c6d990 2019-07-11 stsp
6590 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
6591 64c6d990 2019-07-11 stsp if (err)
6592 64c6d990 2019-07-11 stsp goto done;
6593 64c6d990 2019-07-11 stsp
6594 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
6595 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
6596 64c6d990 2019-07-11 stsp if (err)
6597 64c6d990 2019-07-11 stsp goto done;
6598 64c6d990 2019-07-11 stsp
6599 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
6600 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
6601 64c6d990 2019-07-11 stsp if (err)
6602 64c6d990 2019-07-11 stsp goto done;
6603 64c6d990 2019-07-11 stsp
6604 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
6605 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
6606 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
6607 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
6608 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
6609 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
6610 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
6611 64c6d990 2019-07-11 stsp done:
6612 64c6d990 2019-07-11 stsp if (tree1)
6613 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
6614 64c6d990 2019-07-11 stsp if (tree2)
6615 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
6616 64c6d990 2019-07-11 stsp if (commit)
6617 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
6618 64c6d990 2019-07-11 stsp if (parent_commit)
6619 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
6620 64c6d990 2019-07-11 stsp return err;
6621 ff0d2220 2019-07-11 stsp }
6622 ff0d2220 2019-07-11 stsp
6623 ff0d2220 2019-07-11 stsp static const struct got_error *
6624 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
6625 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
6626 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
6627 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
6628 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
6629 af61c510 2019-07-19 stsp {
6630 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
6631 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
6632 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
6633 af61c510 2019-07-19 stsp struct got_object_qid *qid;
6634 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
6635 af61c510 2019-07-19 stsp
6636 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
6637 af61c510 2019-07-19 stsp if (err)
6638 af61c510 2019-07-19 stsp return err;
6639 af61c510 2019-07-19 stsp
6640 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
6641 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6642 af61c510 2019-07-19 stsp if (err)
6643 af61c510 2019-07-19 stsp goto done;
6644 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
6645 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
6646 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
6647 af61c510 2019-07-19 stsp if (err) {
6648 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
6649 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
6650 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
6651 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
6652 af61c510 2019-07-19 stsp "been reached?!?");
6653 ee780d5c 2020-01-04 stsp }
6654 ee780d5c 2020-01-04 stsp goto done;
6655 af61c510 2019-07-19 stsp } else {
6656 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
6657 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
6658 af61c510 2019-07-19 stsp if (err)
6659 af61c510 2019-07-19 stsp goto done;
6660 af61c510 2019-07-19 stsp
6661 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
6662 af61c510 2019-07-19 stsp if (err)
6663 af61c510 2019-07-19 stsp goto done;
6664 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
6665 af61c510 2019-07-19 stsp commit_id = parent_id;
6666 af61c510 2019-07-19 stsp }
6667 af61c510 2019-07-19 stsp }
6668 af61c510 2019-07-19 stsp done:
6669 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
6670 af61c510 2019-07-19 stsp return err;
6671 af61c510 2019-07-19 stsp }
6672 af61c510 2019-07-19 stsp
6673 af61c510 2019-07-19 stsp static const struct got_error *
6674 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
6675 818c7501 2019-07-11 stsp {
6676 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
6677 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
6678 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
6679 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6680 818c7501 2019-07-11 stsp char *cwd = NULL;
6681 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
6682 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
6683 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
6684 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
6685 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
6686 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
6687 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
6688 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
6689 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6690 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
6691 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
6692 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
6693 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
6694 818c7501 2019-07-11 stsp
6695 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
6696 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
6697 818c7501 2019-07-11 stsp
6698 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
6699 818c7501 2019-07-11 stsp switch (ch) {
6700 818c7501 2019-07-11 stsp case 'a':
6701 818c7501 2019-07-11 stsp abort_rebase = 1;
6702 818c7501 2019-07-11 stsp break;
6703 818c7501 2019-07-11 stsp case 'c':
6704 818c7501 2019-07-11 stsp continue_rebase = 1;
6705 818c7501 2019-07-11 stsp break;
6706 818c7501 2019-07-11 stsp default:
6707 818c7501 2019-07-11 stsp usage_rebase();
6708 818c7501 2019-07-11 stsp /* NOTREACHED */
6709 818c7501 2019-07-11 stsp }
6710 818c7501 2019-07-11 stsp }
6711 818c7501 2019-07-11 stsp
6712 818c7501 2019-07-11 stsp argc -= optind;
6713 818c7501 2019-07-11 stsp argv += optind;
6714 818c7501 2019-07-11 stsp
6715 43012d58 2019-07-14 stsp #ifndef PROFILE
6716 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6717 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
6718 43012d58 2019-07-14 stsp err(1, "pledge");
6719 43012d58 2019-07-14 stsp #endif
6720 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
6721 818c7501 2019-07-11 stsp usage_rebase();
6722 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
6723 818c7501 2019-07-11 stsp if (argc != 0)
6724 818c7501 2019-07-11 stsp usage_rebase();
6725 818c7501 2019-07-11 stsp } else if (argc != 1)
6726 818c7501 2019-07-11 stsp usage_rebase();
6727 818c7501 2019-07-11 stsp
6728 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
6729 818c7501 2019-07-11 stsp if (cwd == NULL) {
6730 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
6731 818c7501 2019-07-11 stsp goto done;
6732 818c7501 2019-07-11 stsp }
6733 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
6734 818c7501 2019-07-11 stsp if (error)
6735 818c7501 2019-07-11 stsp goto done;
6736 818c7501 2019-07-11 stsp
6737 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6738 c9956ddf 2019-09-08 stsp NULL);
6739 818c7501 2019-07-11 stsp if (error != NULL)
6740 818c7501 2019-07-11 stsp goto done;
6741 818c7501 2019-07-11 stsp
6742 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6743 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
6744 7ef62c4e 2020-02-24 stsp if (error)
6745 7ef62c4e 2020-02-24 stsp goto done;
6746 7ef62c4e 2020-02-24 stsp
6747 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
6748 7ef62c4e 2020-02-24 stsp worktree);
6749 818c7501 2019-07-11 stsp if (error)
6750 7ef62c4e 2020-02-24 stsp goto done;
6751 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
6752 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6753 818c7501 2019-07-11 stsp goto done;
6754 7ef62c4e 2020-02-24 stsp }
6755 818c7501 2019-07-11 stsp
6756 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6757 818c7501 2019-07-11 stsp if (error)
6758 818c7501 2019-07-11 stsp goto done;
6759 818c7501 2019-07-11 stsp
6760 f6794adc 2019-07-23 stsp if (abort_rebase) {
6761 818c7501 2019-07-11 stsp int did_something;
6762 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6763 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6764 f6794adc 2019-07-23 stsp goto done;
6765 f6794adc 2019-07-23 stsp }
6766 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6767 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6768 3e3a69f1 2019-07-25 stsp worktree, repo);
6769 818c7501 2019-07-11 stsp if (error)
6770 818c7501 2019-07-11 stsp goto done;
6771 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
6772 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
6773 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
6774 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
6775 818c7501 2019-07-11 stsp if (error)
6776 818c7501 2019-07-11 stsp goto done;
6777 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
6778 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
6779 818c7501 2019-07-11 stsp }
6780 818c7501 2019-07-11 stsp
6781 818c7501 2019-07-11 stsp if (continue_rebase) {
6782 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
6783 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
6784 f6794adc 2019-07-23 stsp goto done;
6785 f6794adc 2019-07-23 stsp }
6786 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
6787 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
6788 3e3a69f1 2019-07-25 stsp worktree, repo);
6789 818c7501 2019-07-11 stsp if (error)
6790 818c7501 2019-07-11 stsp goto done;
6791 818c7501 2019-07-11 stsp
6792 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
6793 01757395 2019-07-12 stsp resume_commit_id, repo);
6794 818c7501 2019-07-11 stsp if (error)
6795 818c7501 2019-07-11 stsp goto done;
6796 818c7501 2019-07-11 stsp
6797 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
6798 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
6799 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
6800 818c7501 2019-07-11 stsp goto done;
6801 818c7501 2019-07-11 stsp }
6802 818c7501 2019-07-11 stsp } else {
6803 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
6804 818c7501 2019-07-11 stsp if (error != NULL)
6805 818c7501 2019-07-11 stsp goto done;
6806 ff0d2220 2019-07-11 stsp }
6807 818c7501 2019-07-11 stsp
6808 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
6809 ff0d2220 2019-07-11 stsp if (error)
6810 ff0d2220 2019-07-11 stsp goto done;
6811 ff0d2220 2019-07-11 stsp
6812 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
6813 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
6814 a51a74b3 2019-07-27 stsp
6815 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
6816 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
6817 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
6818 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
6819 818c7501 2019-07-11 stsp if (error)
6820 818c7501 2019-07-11 stsp goto done;
6821 818c7501 2019-07-11 stsp if (yca_id == NULL) {
6822 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
6823 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
6824 818c7501 2019-07-11 stsp "with work tree's branch");
6825 818c7501 2019-07-11 stsp goto done;
6826 818c7501 2019-07-11 stsp }
6827 818c7501 2019-07-11 stsp
6828 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
6829 a51a74b3 2019-07-27 stsp if (error) {
6830 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
6831 a51a74b3 2019-07-27 stsp goto done;
6832 a51a74b3 2019-07-27 stsp error = NULL;
6833 a51a74b3 2019-07-27 stsp } else {
6834 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6835 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
6836 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
6837 a51a74b3 2019-07-27 stsp goto done;
6838 a51a74b3 2019-07-27 stsp }
6839 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
6840 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
6841 818c7501 2019-07-11 stsp if (error)
6842 818c7501 2019-07-11 stsp goto done;
6843 818c7501 2019-07-11 stsp }
6844 818c7501 2019-07-11 stsp
6845 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
6846 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
6847 818c7501 2019-07-11 stsp if (error)
6848 818c7501 2019-07-11 stsp goto done;
6849 818c7501 2019-07-11 stsp
6850 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6851 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
6852 fc66b545 2019-08-12 stsp if (pid == NULL) {
6853 fc66b545 2019-08-12 stsp if (!continue_rebase) {
6854 fc66b545 2019-08-12 stsp int did_something;
6855 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
6856 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
6857 fc66b545 2019-08-12 stsp &did_something);
6858 fc66b545 2019-08-12 stsp if (error)
6859 fc66b545 2019-08-12 stsp goto done;
6860 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
6861 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
6862 fc66b545 2019-08-12 stsp }
6863 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
6864 fc66b545 2019-08-12 stsp goto done;
6865 fc66b545 2019-08-12 stsp }
6866 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
6867 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
6868 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
6869 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6870 818c7501 2019-07-11 stsp commit = NULL;
6871 818c7501 2019-07-11 stsp if (error)
6872 818c7501 2019-07-11 stsp goto done;
6873 64c6d990 2019-07-11 stsp
6874 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
6875 38b0338b 2019-11-29 stsp if (continue_rebase) {
6876 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
6877 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
6878 38b0338b 2019-11-29 stsp goto done;
6879 38b0338b 2019-11-29 stsp } else {
6880 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
6881 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
6882 38b0338b 2019-11-29 stsp char *id_str;
6883 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
6884 38b0338b 2019-11-29 stsp new_base_branch);
6885 38b0338b 2019-11-29 stsp if (error)
6886 38b0338b 2019-11-29 stsp goto done;
6887 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
6888 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
6889 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
6890 38b0338b 2019-11-29 stsp free(id_str);
6891 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
6892 38b0338b 2019-11-29 stsp new_head_commit_id);
6893 38b0338b 2019-11-29 stsp if (error)
6894 38b0338b 2019-11-29 stsp goto done;
6895 38b0338b 2019-11-29 stsp }
6896 818c7501 2019-07-11 stsp }
6897 818c7501 2019-07-11 stsp
6898 818c7501 2019-07-11 stsp pid = NULL;
6899 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
6900 818c7501 2019-07-11 stsp commit_id = qid->id;
6901 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
6902 818c7501 2019-07-11 stsp pid = qid;
6903 818c7501 2019-07-11 stsp
6904 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
6905 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
6906 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6907 818c7501 2019-07-11 stsp if (error)
6908 818c7501 2019-07-11 stsp goto done;
6909 818c7501 2019-07-11 stsp
6910 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6911 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
6912 a0ea4fc0 2020-02-28 stsp if (error)
6913 a0ea4fc0 2020-02-28 stsp goto done;
6914 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6915 818c7501 2019-07-11 stsp break;
6916 01757395 2019-07-12 stsp }
6917 818c7501 2019-07-11 stsp
6918 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
6919 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
6920 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6921 818c7501 2019-07-11 stsp if (error)
6922 818c7501 2019-07-11 stsp goto done;
6923 818c7501 2019-07-11 stsp }
6924 818c7501 2019-07-11 stsp
6925 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6926 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
6927 818c7501 2019-07-11 stsp if (error)
6928 818c7501 2019-07-11 stsp goto done;
6929 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6930 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
6931 818c7501 2019-07-11 stsp } else
6932 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
6933 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
6934 818c7501 2019-07-11 stsp done:
6935 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
6936 818c7501 2019-07-11 stsp free(branch_head_commit_id);
6937 818c7501 2019-07-11 stsp free(resume_commit_id);
6938 818c7501 2019-07-11 stsp free(yca_id);
6939 818c7501 2019-07-11 stsp if (commit)
6940 818c7501 2019-07-11 stsp got_object_commit_close(commit);
6941 818c7501 2019-07-11 stsp if (branch)
6942 818c7501 2019-07-11 stsp got_ref_close(branch);
6943 818c7501 2019-07-11 stsp if (new_base_branch)
6944 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
6945 818c7501 2019-07-11 stsp if (tmp_branch)
6946 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
6947 5ef14e63 2019-06-02 stsp if (worktree)
6948 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
6949 5ef14e63 2019-06-02 stsp if (repo)
6950 5ef14e63 2019-06-02 stsp got_repo_close(repo);
6951 5ef14e63 2019-06-02 stsp return error;
6952 0ebf8283 2019-07-24 stsp }
6953 0ebf8283 2019-07-24 stsp
6954 0ebf8283 2019-07-24 stsp __dead static void
6955 0ebf8283 2019-07-24 stsp usage_histedit(void)
6956 0ebf8283 2019-07-24 stsp {
6957 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
6958 0ebf8283 2019-07-24 stsp getprogname());
6959 0ebf8283 2019-07-24 stsp exit(1);
6960 0ebf8283 2019-07-24 stsp }
6961 0ebf8283 2019-07-24 stsp
6962 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
6963 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
6964 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
6965 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
6966 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
6967 0ebf8283 2019-07-24 stsp
6968 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
6969 0ebf8283 2019-07-24 stsp unsigned char code;
6970 0ebf8283 2019-07-24 stsp const char *name;
6971 0ebf8283 2019-07-24 stsp const char *desc;
6972 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
6973 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
6974 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
6975 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
6976 82997472 2020-01-29 stsp "be used" },
6977 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
6978 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
6979 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
6980 0ebf8283 2019-07-24 stsp };
6981 0ebf8283 2019-07-24 stsp
6982 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
6983 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
6984 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
6985 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6986 0ebf8283 2019-07-24 stsp char *logmsg;
6987 0ebf8283 2019-07-24 stsp };
6988 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
6989 0ebf8283 2019-07-24 stsp
6990 0ebf8283 2019-07-24 stsp static const struct got_error *
6991 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
6992 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6993 0ebf8283 2019-07-24 stsp {
6994 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6995 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
6996 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6997 8138f3e1 2019-08-11 stsp int n;
6998 0ebf8283 2019-07-24 stsp
6999 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
7000 0ebf8283 2019-07-24 stsp if (err)
7001 0ebf8283 2019-07-24 stsp goto done;
7002 0ebf8283 2019-07-24 stsp
7003 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
7004 0ebf8283 2019-07-24 stsp if (err)
7005 0ebf8283 2019-07-24 stsp goto done;
7006 0ebf8283 2019-07-24 stsp
7007 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
7008 0ebf8283 2019-07-24 stsp if (err)
7009 0ebf8283 2019-07-24 stsp goto done;
7010 0ebf8283 2019-07-24 stsp
7011 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
7012 0ebf8283 2019-07-24 stsp if (n < 0)
7013 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7014 0ebf8283 2019-07-24 stsp done:
7015 0ebf8283 2019-07-24 stsp if (commit)
7016 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7017 0ebf8283 2019-07-24 stsp free(id_str);
7018 0ebf8283 2019-07-24 stsp free(logmsg);
7019 0ebf8283 2019-07-24 stsp return err;
7020 0ebf8283 2019-07-24 stsp }
7021 0ebf8283 2019-07-24 stsp
7022 0ebf8283 2019-07-24 stsp static const struct got_error *
7023 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
7024 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
7025 0ebf8283 2019-07-24 stsp {
7026 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7027 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
7028 0ebf8283 2019-07-24 stsp
7029 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
7030 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7031 0ebf8283 2019-07-24 stsp
7032 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7033 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
7034 0ebf8283 2019-07-24 stsp f, repo);
7035 0ebf8283 2019-07-24 stsp if (err)
7036 0ebf8283 2019-07-24 stsp break;
7037 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7038 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
7039 083957f4 2020-02-24 stsp if (n < 0) {
7040 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
7041 083957f4 2020-02-24 stsp break;
7042 083957f4 2020-02-24 stsp }
7043 083957f4 2020-02-24 stsp }
7044 0ebf8283 2019-07-24 stsp }
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp return err;
7047 0ebf8283 2019-07-24 stsp }
7048 0ebf8283 2019-07-24 stsp
7049 0ebf8283 2019-07-24 stsp static const struct got_error *
7050 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
7051 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
7052 0ebf8283 2019-07-24 stsp {
7053 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7054 0ebf8283 2019-07-24 stsp int n, i;
7055 514f2ffe 2020-01-29 stsp char *id_str;
7056 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
7057 514f2ffe 2020-01-29 stsp
7058 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
7059 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
7060 514f2ffe 2020-01-29 stsp if (err)
7061 514f2ffe 2020-01-29 stsp return err;
7062 514f2ffe 2020-01-29 stsp
7063 514f2ffe 2020-01-29 stsp n = fprintf(f,
7064 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
7065 514f2ffe 2020-01-29 stsp "# commit %s\n"
7066 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
7067 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
7068 514f2ffe 2020-01-29 stsp if (n < 0) {
7069 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7070 514f2ffe 2020-01-29 stsp goto done;
7071 514f2ffe 2020-01-29 stsp }
7072 0ebf8283 2019-07-24 stsp
7073 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
7074 514f2ffe 2020-01-29 stsp if (n < 0) {
7075 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
7076 514f2ffe 2020-01-29 stsp goto done;
7077 514f2ffe 2020-01-29 stsp }
7078 0ebf8283 2019-07-24 stsp
7079 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7080 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
7081 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
7082 0ebf8283 2019-07-24 stsp cmd->desc);
7083 0ebf8283 2019-07-24 stsp if (n < 0) {
7084 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7085 0ebf8283 2019-07-24 stsp break;
7086 0ebf8283 2019-07-24 stsp }
7087 0ebf8283 2019-07-24 stsp }
7088 514f2ffe 2020-01-29 stsp done:
7089 514f2ffe 2020-01-29 stsp free(id_str);
7090 0ebf8283 2019-07-24 stsp return err;
7091 0ebf8283 2019-07-24 stsp }
7092 0ebf8283 2019-07-24 stsp
7093 0ebf8283 2019-07-24 stsp static const struct got_error *
7094 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
7095 0ebf8283 2019-07-24 stsp {
7096 0ebf8283 2019-07-24 stsp static char msg[42];
7097 0ebf8283 2019-07-24 stsp int ret;
7098 0ebf8283 2019-07-24 stsp
7099 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
7100 0ebf8283 2019-07-24 stsp lineno);
7101 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
7102 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
7103 0ebf8283 2019-07-24 stsp
7104 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
7105 0ebf8283 2019-07-24 stsp }
7106 0ebf8283 2019-07-24 stsp
7107 0ebf8283 2019-07-24 stsp static const struct got_error *
7108 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
7109 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
7110 0ebf8283 2019-07-24 stsp {
7111 0ebf8283 2019-07-24 stsp const struct got_error *err;
7112 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
7113 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
7114 0ebf8283 2019-07-24 stsp
7115 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7116 0ebf8283 2019-07-24 stsp if (err)
7117 0ebf8283 2019-07-24 stsp return err;
7118 0ebf8283 2019-07-24 stsp
7119 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
7120 0ebf8283 2019-07-24 stsp if (err)
7121 0ebf8283 2019-07-24 stsp goto done;
7122 0ebf8283 2019-07-24 stsp
7123 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
7124 5943eee2 2019-08-13 stsp if (err)
7125 5943eee2 2019-08-13 stsp goto done;
7126 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
7127 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
7128 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
7129 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7130 0ebf8283 2019-07-24 stsp }
7131 0ebf8283 2019-07-24 stsp done:
7132 0ebf8283 2019-07-24 stsp if (folded_commit)
7133 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
7134 0ebf8283 2019-07-24 stsp free(id_str);
7135 5943eee2 2019-08-13 stsp free(folded_logmsg);
7136 0ebf8283 2019-07-24 stsp return err;
7137 0ebf8283 2019-07-24 stsp }
7138 0ebf8283 2019-07-24 stsp
7139 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
7140 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
7141 0ebf8283 2019-07-24 stsp {
7142 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
7143 0ebf8283 2019-07-24 stsp
7144 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
7145 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
7146 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
7147 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
7148 3f9de99f 2019-07-24 stsp folded = prev;
7149 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
7150 0ebf8283 2019-07-24 stsp }
7151 0ebf8283 2019-07-24 stsp
7152 0ebf8283 2019-07-24 stsp return folded;
7153 0ebf8283 2019-07-24 stsp }
7154 0ebf8283 2019-07-24 stsp
7155 0ebf8283 2019-07-24 stsp static const struct got_error *
7156 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
7157 0ebf8283 2019-07-24 stsp struct got_repository *repo)
7158 0ebf8283 2019-07-24 stsp {
7159 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
7160 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
7161 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7162 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7163 0ebf8283 2019-07-24 stsp int fd;
7164 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
7165 0ebf8283 2019-07-24 stsp
7166 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7167 0ebf8283 2019-07-24 stsp if (err)
7168 0ebf8283 2019-07-24 stsp return err;
7169 0ebf8283 2019-07-24 stsp
7170 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
7171 0ebf8283 2019-07-24 stsp if (folded) {
7172 0ebf8283 2019-07-24 stsp while (folded != hle) {
7173 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
7174 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7175 3f9de99f 2019-07-24 stsp continue;
7176 3f9de99f 2019-07-24 stsp }
7177 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
7178 0ebf8283 2019-07-24 stsp logmsg, repo);
7179 0ebf8283 2019-07-24 stsp if (err)
7180 0ebf8283 2019-07-24 stsp goto done;
7181 0ebf8283 2019-07-24 stsp free(logmsg);
7182 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7183 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
7184 0ebf8283 2019-07-24 stsp }
7185 0ebf8283 2019-07-24 stsp }
7186 0ebf8283 2019-07-24 stsp
7187 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7188 0ebf8283 2019-07-24 stsp if (err)
7189 0ebf8283 2019-07-24 stsp goto done;
7190 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
7191 5943eee2 2019-08-13 stsp if (err)
7192 5943eee2 2019-08-13 stsp goto done;
7193 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
7194 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
7195 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
7196 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
7197 0ebf8283 2019-07-24 stsp goto done;
7198 0ebf8283 2019-07-24 stsp }
7199 0ebf8283 2019-07-24 stsp free(logmsg);
7200 0ebf8283 2019-07-24 stsp logmsg = new_msg;
7201 0ebf8283 2019-07-24 stsp
7202 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
7203 0ebf8283 2019-07-24 stsp if (err)
7204 0ebf8283 2019-07-24 stsp goto done;
7205 0ebf8283 2019-07-24 stsp
7206 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
7207 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
7208 0ebf8283 2019-07-24 stsp if (err)
7209 0ebf8283 2019-07-24 stsp goto done;
7210 0ebf8283 2019-07-24 stsp
7211 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
7212 0ebf8283 2019-07-24 stsp close(fd);
7213 0ebf8283 2019-07-24 stsp
7214 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7215 0ebf8283 2019-07-24 stsp if (err)
7216 0ebf8283 2019-07-24 stsp goto done;
7217 0ebf8283 2019-07-24 stsp
7218 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
7219 0ebf8283 2019-07-24 stsp if (err) {
7220 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
7221 0ebf8283 2019-07-24 stsp goto done;
7222 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
7223 0ebf8283 2019-07-24 stsp }
7224 0ebf8283 2019-07-24 stsp done:
7225 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
7226 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
7227 0ebf8283 2019-07-24 stsp free(logmsg_path);
7228 0ebf8283 2019-07-24 stsp free(logmsg);
7229 5943eee2 2019-08-13 stsp free(orig_logmsg);
7230 0ebf8283 2019-07-24 stsp free(editor);
7231 0ebf8283 2019-07-24 stsp if (commit)
7232 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7233 0ebf8283 2019-07-24 stsp return err;
7234 5ef14e63 2019-06-02 stsp }
7235 0ebf8283 2019-07-24 stsp
7236 0ebf8283 2019-07-24 stsp static const struct got_error *
7237 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
7238 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
7239 0ebf8283 2019-07-24 stsp {
7240 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7241 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
7242 0ebf8283 2019-07-24 stsp size_t size;
7243 0ebf8283 2019-07-24 stsp ssize_t len;
7244 0ebf8283 2019-07-24 stsp int lineno = 0, i;
7245 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
7246 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
7247 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
7248 0ebf8283 2019-07-24 stsp
7249 0ebf8283 2019-07-24 stsp for (;;) {
7250 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
7251 0ebf8283 2019-07-24 stsp if (len == -1) {
7252 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
7253 0ebf8283 2019-07-24 stsp if (feof(f))
7254 0ebf8283 2019-07-24 stsp break;
7255 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
7256 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
7257 0ebf8283 2019-07-24 stsp break;
7258 0ebf8283 2019-07-24 stsp }
7259 0ebf8283 2019-07-24 stsp lineno++;
7260 0ebf8283 2019-07-24 stsp p = line;
7261 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7262 0ebf8283 2019-07-24 stsp p++;
7263 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
7264 0ebf8283 2019-07-24 stsp free(line);
7265 0ebf8283 2019-07-24 stsp line = NULL;
7266 0ebf8283 2019-07-24 stsp continue;
7267 0ebf8283 2019-07-24 stsp }
7268 0ebf8283 2019-07-24 stsp cmd = NULL;
7269 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
7270 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
7271 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
7272 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
7273 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
7274 0ebf8283 2019-07-24 stsp break;
7275 0ebf8283 2019-07-24 stsp }
7276 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
7277 0ebf8283 2019-07-24 stsp p++;
7278 0ebf8283 2019-07-24 stsp break;
7279 0ebf8283 2019-07-24 stsp }
7280 0ebf8283 2019-07-24 stsp }
7281 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
7282 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7283 0ebf8283 2019-07-24 stsp break;
7284 0ebf8283 2019-07-24 stsp }
7285 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
7286 0ebf8283 2019-07-24 stsp p++;
7287 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
7288 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
7289 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
7290 0ebf8283 2019-07-24 stsp break;
7291 0ebf8283 2019-07-24 stsp }
7292 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
7293 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7294 0ebf8283 2019-07-24 stsp if (err)
7295 0ebf8283 2019-07-24 stsp break;
7296 0ebf8283 2019-07-24 stsp } else {
7297 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
7298 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
7299 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7300 0ebf8283 2019-07-24 stsp break;
7301 0ebf8283 2019-07-24 stsp }
7302 0ebf8283 2019-07-24 stsp }
7303 0ebf8283 2019-07-24 stsp free(line);
7304 0ebf8283 2019-07-24 stsp line = NULL;
7305 0ebf8283 2019-07-24 stsp continue;
7306 0ebf8283 2019-07-24 stsp } else {
7307 0ebf8283 2019-07-24 stsp end = p;
7308 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
7309 0ebf8283 2019-07-24 stsp end++;
7310 0ebf8283 2019-07-24 stsp *end = '\0';
7311 0ebf8283 2019-07-24 stsp
7312 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
7313 0ebf8283 2019-07-24 stsp if (err) {
7314 0ebf8283 2019-07-24 stsp /* override error code */
7315 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
7316 0ebf8283 2019-07-24 stsp break;
7317 0ebf8283 2019-07-24 stsp }
7318 0ebf8283 2019-07-24 stsp }
7319 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
7320 0ebf8283 2019-07-24 stsp if (hle == NULL) {
7321 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
7322 0ebf8283 2019-07-24 stsp break;
7323 0ebf8283 2019-07-24 stsp }
7324 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
7325 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
7326 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
7327 0ebf8283 2019-07-24 stsp commit_id = NULL;
7328 0ebf8283 2019-07-24 stsp free(line);
7329 0ebf8283 2019-07-24 stsp line = NULL;
7330 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
7331 0ebf8283 2019-07-24 stsp }
7332 0ebf8283 2019-07-24 stsp
7333 0ebf8283 2019-07-24 stsp free(line);
7334 0ebf8283 2019-07-24 stsp free(commit_id);
7335 0ebf8283 2019-07-24 stsp return err;
7336 0ebf8283 2019-07-24 stsp }
7337 0ebf8283 2019-07-24 stsp
7338 0ebf8283 2019-07-24 stsp static const struct got_error *
7339 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
7340 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
7341 bfce7f83 2019-07-27 stsp {
7342 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
7343 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
7344 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7345 5b87815e 2020-03-05 stsp static char msg[92];
7346 bfce7f83 2019-07-27 stsp char *id_str;
7347 bfce7f83 2019-07-27 stsp
7348 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
7349 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
7350 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
7351 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
7352 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
7353 5b87815e 2020-03-05 stsp
7354 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7355 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
7356 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
7357 5b87815e 2020-03-05 stsp if (hle == hle2)
7358 5b87815e 2020-03-05 stsp continue;
7359 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
7360 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
7361 5b87815e 2020-03-05 stsp continue;
7362 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
7363 5b87815e 2020-03-05 stsp if (err)
7364 5b87815e 2020-03-05 stsp return err;
7365 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
7366 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
7367 5b87815e 2020-03-05 stsp free(id_str);
7368 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7369 5b87815e 2020-03-05 stsp }
7370 5b87815e 2020-03-05 stsp }
7371 bfce7f83 2019-07-27 stsp
7372 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
7373 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7374 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
7375 bfce7f83 2019-07-27 stsp break;
7376 bfce7f83 2019-07-27 stsp }
7377 bfce7f83 2019-07-27 stsp if (hle == NULL) {
7378 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
7379 bfce7f83 2019-07-27 stsp if (err)
7380 bfce7f83 2019-07-27 stsp return err;
7381 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
7382 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
7383 bfce7f83 2019-07-27 stsp free(id_str);
7384 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
7385 bfce7f83 2019-07-27 stsp }
7386 bfce7f83 2019-07-27 stsp }
7387 bfce7f83 2019-07-27 stsp
7388 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
7389 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
7390 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
7391 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
7392 bfce7f83 2019-07-27 stsp
7393 bfce7f83 2019-07-27 stsp return NULL;
7394 bfce7f83 2019-07-27 stsp }
7395 bfce7f83 2019-07-27 stsp
7396 bfce7f83 2019-07-27 stsp static const struct got_error *
7397 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
7398 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
7399 bfce7f83 2019-07-27 stsp struct got_repository *repo)
7400 0ebf8283 2019-07-24 stsp {
7401 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7402 0ebf8283 2019-07-24 stsp char *editor;
7403 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7404 0ebf8283 2019-07-24 stsp
7405 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
7406 0ebf8283 2019-07-24 stsp if (err)
7407 0ebf8283 2019-07-24 stsp return err;
7408 0ebf8283 2019-07-24 stsp
7409 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
7410 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
7411 0ebf8283 2019-07-24 stsp goto done;
7412 0ebf8283 2019-07-24 stsp }
7413 0ebf8283 2019-07-24 stsp
7414 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7415 0ebf8283 2019-07-24 stsp if (f == NULL) {
7416 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
7417 0ebf8283 2019-07-24 stsp goto done;
7418 0ebf8283 2019-07-24 stsp }
7419 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7420 bfce7f83 2019-07-27 stsp if (err)
7421 bfce7f83 2019-07-27 stsp goto done;
7422 bfce7f83 2019-07-27 stsp
7423 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
7424 0ebf8283 2019-07-24 stsp done:
7425 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7426 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7427 0ebf8283 2019-07-24 stsp free(editor);
7428 0ebf8283 2019-07-24 stsp return err;
7429 0ebf8283 2019-07-24 stsp }
7430 0ebf8283 2019-07-24 stsp
7431 0ebf8283 2019-07-24 stsp static const struct got_error *
7432 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
7433 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
7434 514f2ffe 2020-01-29 stsp struct got_repository *);
7435 0ebf8283 2019-07-24 stsp
7436 0ebf8283 2019-07-24 stsp static const struct got_error *
7437 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
7438 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
7439 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
7440 0ebf8283 2019-07-24 stsp {
7441 0ebf8283 2019-07-24 stsp const struct got_error *err;
7442 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7443 0ebf8283 2019-07-24 stsp char *path = NULL;
7444 0ebf8283 2019-07-24 stsp
7445 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
7446 0ebf8283 2019-07-24 stsp if (err)
7447 0ebf8283 2019-07-24 stsp return err;
7448 0ebf8283 2019-07-24 stsp
7449 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
7450 0ebf8283 2019-07-24 stsp if (err)
7451 0ebf8283 2019-07-24 stsp goto done;
7452 0ebf8283 2019-07-24 stsp
7453 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
7454 0ebf8283 2019-07-24 stsp if (err)
7455 0ebf8283 2019-07-24 stsp goto done;
7456 0ebf8283 2019-07-24 stsp
7457 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
7458 083957f4 2020-02-24 stsp rewind(f);
7459 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7460 083957f4 2020-02-24 stsp } else {
7461 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
7462 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
7463 0ebf8283 2019-07-24 stsp goto done;
7464 083957f4 2020-02-24 stsp }
7465 083957f4 2020-02-24 stsp f = NULL;
7466 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
7467 083957f4 2020-02-24 stsp if (err) {
7468 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7469 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7470 083957f4 2020-02-24 stsp goto done;
7471 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
7472 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
7473 083957f4 2020-02-24 stsp }
7474 0ebf8283 2019-07-24 stsp }
7475 0ebf8283 2019-07-24 stsp done:
7476 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7477 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7478 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
7479 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
7480 0ebf8283 2019-07-24 stsp free(path);
7481 0ebf8283 2019-07-24 stsp return err;
7482 0ebf8283 2019-07-24 stsp }
7483 0ebf8283 2019-07-24 stsp
7484 0ebf8283 2019-07-24 stsp static const struct got_error *
7485 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
7486 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7487 0ebf8283 2019-07-24 stsp {
7488 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7489 0ebf8283 2019-07-24 stsp char *path = NULL;
7490 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7491 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7492 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7493 0ebf8283 2019-07-24 stsp
7494 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
7495 0ebf8283 2019-07-24 stsp if (err)
7496 0ebf8283 2019-07-24 stsp return err;
7497 0ebf8283 2019-07-24 stsp
7498 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
7499 0ebf8283 2019-07-24 stsp if (f == NULL) {
7500 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7501 0ebf8283 2019-07-24 stsp goto done;
7502 0ebf8283 2019-07-24 stsp }
7503 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
7504 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
7505 0ebf8283 2019-07-24 stsp repo);
7506 0ebf8283 2019-07-24 stsp if (err)
7507 0ebf8283 2019-07-24 stsp break;
7508 0ebf8283 2019-07-24 stsp
7509 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7510 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
7511 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
7512 0ebf8283 2019-07-24 stsp if (n < 0) {
7513 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
7514 0ebf8283 2019-07-24 stsp break;
7515 0ebf8283 2019-07-24 stsp }
7516 0ebf8283 2019-07-24 stsp }
7517 0ebf8283 2019-07-24 stsp }
7518 0ebf8283 2019-07-24 stsp done:
7519 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7520 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7521 0ebf8283 2019-07-24 stsp free(path);
7522 0ebf8283 2019-07-24 stsp if (commit)
7523 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7524 0ebf8283 2019-07-24 stsp return err;
7525 0ebf8283 2019-07-24 stsp }
7526 0ebf8283 2019-07-24 stsp
7527 bfce7f83 2019-07-27 stsp void
7528 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
7529 bfce7f83 2019-07-27 stsp {
7530 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
7531 bfce7f83 2019-07-27 stsp
7532 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
7533 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
7534 bfce7f83 2019-07-27 stsp free(hle);
7535 bfce7f83 2019-07-27 stsp }
7536 bfce7f83 2019-07-27 stsp }
7537 bfce7f83 2019-07-27 stsp
7538 0ebf8283 2019-07-24 stsp static const struct got_error *
7539 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
7540 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
7541 0ebf8283 2019-07-24 stsp {
7542 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
7543 0ebf8283 2019-07-24 stsp FILE *f = NULL;
7544 0ebf8283 2019-07-24 stsp
7545 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
7546 0ebf8283 2019-07-24 stsp if (f == NULL) {
7547 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
7548 0ebf8283 2019-07-24 stsp goto done;
7549 0ebf8283 2019-07-24 stsp }
7550 0ebf8283 2019-07-24 stsp
7551 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
7552 0ebf8283 2019-07-24 stsp done:
7553 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
7554 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
7555 0ebf8283 2019-07-24 stsp return err;
7556 0ebf8283 2019-07-24 stsp }
7557 0ebf8283 2019-07-24 stsp
7558 0ebf8283 2019-07-24 stsp static const struct got_error *
7559 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
7560 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
7561 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
7562 0ebf8283 2019-07-24 stsp {
7563 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
7564 0ebf8283 2019-07-24 stsp int resp = ' ';
7565 0ebf8283 2019-07-24 stsp
7566 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
7567 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
7568 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
7569 0ebf8283 2019-07-24 stsp resp = getchar();
7570 a61a4414 2019-08-07 stsp if (resp == '\n')
7571 a61a4414 2019-08-07 stsp resp = getchar();
7572 426ebf2e 2019-07-25 stsp if (resp == 'c') {
7573 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7574 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
7575 bfce7f83 2019-07-27 stsp repo);
7576 426ebf2e 2019-07-25 stsp if (err) {
7577 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7578 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7579 426ebf2e 2019-07-25 stsp break;
7580 bfce7f83 2019-07-27 stsp prev_err = err;
7581 426ebf2e 2019-07-25 stsp resp = ' ';
7582 426ebf2e 2019-07-25 stsp continue;
7583 426ebf2e 2019-07-25 stsp }
7584 bfce7f83 2019-07-27 stsp break;
7585 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
7586 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
7587 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
7588 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
7589 426ebf2e 2019-07-25 stsp if (err) {
7590 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
7591 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
7592 426ebf2e 2019-07-25 stsp break;
7593 bfce7f83 2019-07-27 stsp prev_err = err;
7594 426ebf2e 2019-07-25 stsp resp = ' ';
7595 426ebf2e 2019-07-25 stsp continue;
7596 426ebf2e 2019-07-25 stsp }
7597 bfce7f83 2019-07-27 stsp break;
7598 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
7599 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
7600 0ebf8283 2019-07-24 stsp break;
7601 426ebf2e 2019-07-25 stsp } else
7602 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
7603 0ebf8283 2019-07-24 stsp }
7604 0ebf8283 2019-07-24 stsp
7605 0ebf8283 2019-07-24 stsp return err;
7606 0ebf8283 2019-07-24 stsp }
7607 0ebf8283 2019-07-24 stsp
7608 0ebf8283 2019-07-24 stsp static const struct got_error *
7609 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
7610 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
7611 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
7612 0ebf8283 2019-07-24 stsp {
7613 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7614 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7615 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
7616 3e3a69f1 2019-07-25 stsp branch, repo);
7617 0ebf8283 2019-07-24 stsp }
7618 0ebf8283 2019-07-24 stsp
7619 0ebf8283 2019-07-24 stsp static const struct got_error *
7620 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
7621 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
7622 0ebf8283 2019-07-24 stsp {
7623 0ebf8283 2019-07-24 stsp const struct got_error *err;
7624 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
7625 0ebf8283 2019-07-24 stsp
7626 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
7627 0ebf8283 2019-07-24 stsp if (err)
7628 0ebf8283 2019-07-24 stsp goto done;
7629 0ebf8283 2019-07-24 stsp
7630 0ebf8283 2019-07-24 stsp if (new_id) {
7631 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
7632 0ebf8283 2019-07-24 stsp if (err)
7633 0ebf8283 2019-07-24 stsp goto done;
7634 0ebf8283 2019-07-24 stsp }
7635 0ebf8283 2019-07-24 stsp
7636 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
7637 0ebf8283 2019-07-24 stsp if (new_id_str)
7638 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
7639 0ebf8283 2019-07-24 stsp
7640 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
7641 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
7642 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
7643 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
7644 0ebf8283 2019-07-24 stsp goto done;
7645 0ebf8283 2019-07-24 stsp }
7646 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
7647 0ebf8283 2019-07-24 stsp } else {
7648 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
7649 0ebf8283 2019-07-24 stsp if (err)
7650 0ebf8283 2019-07-24 stsp goto done;
7651 0ebf8283 2019-07-24 stsp }
7652 0ebf8283 2019-07-24 stsp
7653 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
7654 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
7655 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
7656 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
7657 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
7658 0ebf8283 2019-07-24 stsp break;
7659 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
7660 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
7661 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
7662 0ebf8283 2019-07-24 stsp logmsg);
7663 0ebf8283 2019-07-24 stsp break;
7664 0ebf8283 2019-07-24 stsp default:
7665 0ebf8283 2019-07-24 stsp break;
7666 0ebf8283 2019-07-24 stsp }
7667 0ebf8283 2019-07-24 stsp done:
7668 0ebf8283 2019-07-24 stsp free(old_id_str);
7669 0ebf8283 2019-07-24 stsp free(new_id_str);
7670 0ebf8283 2019-07-24 stsp return err;
7671 0ebf8283 2019-07-24 stsp }
7672 0ebf8283 2019-07-24 stsp
7673 0ebf8283 2019-07-24 stsp static const struct got_error *
7674 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
7675 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
7676 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
7677 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
7678 0ebf8283 2019-07-24 stsp {
7679 0ebf8283 2019-07-24 stsp const struct got_error *err;
7680 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7681 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
7682 0ebf8283 2019-07-24 stsp
7683 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
7684 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
7685 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
7686 0ebf8283 2019-07-24 stsp if (err)
7687 0ebf8283 2019-07-24 stsp return err;
7688 0ebf8283 2019-07-24 stsp }
7689 0ebf8283 2019-07-24 stsp
7690 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
7691 0ebf8283 2019-07-24 stsp if (err)
7692 0ebf8283 2019-07-24 stsp return err;
7693 0ebf8283 2019-07-24 stsp
7694 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
7695 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
7696 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
7697 0ebf8283 2019-07-24 stsp if (err) {
7698 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
7699 0ebf8283 2019-07-24 stsp goto done;
7700 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
7701 0ebf8283 2019-07-24 stsp } else {
7702 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
7703 0ebf8283 2019-07-24 stsp free(new_commit_id);
7704 0ebf8283 2019-07-24 stsp }
7705 0ebf8283 2019-07-24 stsp done:
7706 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7707 0ebf8283 2019-07-24 stsp return err;
7708 0ebf8283 2019-07-24 stsp }
7709 0ebf8283 2019-07-24 stsp
7710 0ebf8283 2019-07-24 stsp static const struct got_error *
7711 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
7712 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
7713 0ebf8283 2019-07-24 stsp {
7714 0ebf8283 2019-07-24 stsp const struct got_error *error;
7715 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
7716 0ebf8283 2019-07-24 stsp
7717 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
7718 0ebf8283 2019-07-24 stsp repo);
7719 0ebf8283 2019-07-24 stsp if (error)
7720 0ebf8283 2019-07-24 stsp return error;
7721 0ebf8283 2019-07-24 stsp
7722 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
7723 0ebf8283 2019-07-24 stsp if (error)
7724 0ebf8283 2019-07-24 stsp return error;
7725 0ebf8283 2019-07-24 stsp
7726 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
7727 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7728 0ebf8283 2019-07-24 stsp return error;
7729 ab20a43a 2020-01-29 stsp }
7730 ab20a43a 2020-01-29 stsp
7731 ab20a43a 2020-01-29 stsp static const struct got_error *
7732 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
7733 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
7734 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
7735 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
7736 ab20a43a 2020-01-29 stsp {
7737 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
7738 ab20a43a 2020-01-29 stsp
7739 ab20a43a 2020-01-29 stsp switch (status) {
7740 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7741 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7742 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7743 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
7744 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7745 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7746 ab20a43a 2020-01-29 stsp default:
7747 ab20a43a 2020-01-29 stsp break;
7748 ab20a43a 2020-01-29 stsp }
7749 ab20a43a 2020-01-29 stsp
7750 ab20a43a 2020-01-29 stsp switch (staged_status) {
7751 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
7752 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
7753 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
7754 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
7755 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
7756 ab20a43a 2020-01-29 stsp default:
7757 ab20a43a 2020-01-29 stsp break;
7758 ab20a43a 2020-01-29 stsp }
7759 ab20a43a 2020-01-29 stsp
7760 ab20a43a 2020-01-29 stsp return NULL;
7761 0ebf8283 2019-07-24 stsp }
7762 0ebf8283 2019-07-24 stsp
7763 0ebf8283 2019-07-24 stsp static const struct got_error *
7764 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
7765 0ebf8283 2019-07-24 stsp {
7766 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
7767 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
7768 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
7769 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
7770 0ebf8283 2019-07-24 stsp char *cwd = NULL;
7771 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
7772 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
7773 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
7774 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
7775 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
7776 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
7777 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
7778 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
7779 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
7780 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
7781 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
7782 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
7783 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
7784 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
7785 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
7786 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
7787 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
7788 0ebf8283 2019-07-24 stsp
7789 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
7790 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
7791 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
7792 0ebf8283 2019-07-24 stsp
7793 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
7794 0ebf8283 2019-07-24 stsp switch (ch) {
7795 0ebf8283 2019-07-24 stsp case 'a':
7796 0ebf8283 2019-07-24 stsp abort_edit = 1;
7797 0ebf8283 2019-07-24 stsp break;
7798 0ebf8283 2019-07-24 stsp case 'c':
7799 0ebf8283 2019-07-24 stsp continue_edit = 1;
7800 0ebf8283 2019-07-24 stsp break;
7801 0ebf8283 2019-07-24 stsp case 'F':
7802 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
7803 0ebf8283 2019-07-24 stsp break;
7804 083957f4 2020-02-24 stsp case 'm':
7805 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
7806 083957f4 2020-02-24 stsp break;
7807 0ebf8283 2019-07-24 stsp default:
7808 0ebf8283 2019-07-24 stsp usage_histedit();
7809 0ebf8283 2019-07-24 stsp /* NOTREACHED */
7810 0ebf8283 2019-07-24 stsp }
7811 0ebf8283 2019-07-24 stsp }
7812 0ebf8283 2019-07-24 stsp
7813 0ebf8283 2019-07-24 stsp argc -= optind;
7814 0ebf8283 2019-07-24 stsp argv += optind;
7815 0ebf8283 2019-07-24 stsp
7816 0ebf8283 2019-07-24 stsp #ifndef PROFILE
7817 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7818 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
7819 0ebf8283 2019-07-24 stsp err(1, "pledge");
7820 0ebf8283 2019-07-24 stsp #endif
7821 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
7822 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
7823 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
7824 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
7825 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
7826 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
7827 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
7828 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
7829 0ebf8283 2019-07-24 stsp if (argc != 0)
7830 0ebf8283 2019-07-24 stsp usage_histedit();
7831 0ebf8283 2019-07-24 stsp
7832 0ebf8283 2019-07-24 stsp /*
7833 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
7834 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
7835 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
7836 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
7837 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
7838 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
7839 0ebf8283 2019-07-24 stsp */
7840 0ebf8283 2019-07-24 stsp
7841 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
7842 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
7843 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
7844 0ebf8283 2019-07-24 stsp goto done;
7845 0ebf8283 2019-07-24 stsp }
7846 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
7847 0ebf8283 2019-07-24 stsp if (error)
7848 0ebf8283 2019-07-24 stsp goto done;
7849 0ebf8283 2019-07-24 stsp
7850 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7851 c9956ddf 2019-09-08 stsp NULL);
7852 0ebf8283 2019-07-24 stsp if (error != NULL)
7853 0ebf8283 2019-07-24 stsp goto done;
7854 0ebf8283 2019-07-24 stsp
7855 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
7856 0ebf8283 2019-07-24 stsp if (error)
7857 0ebf8283 2019-07-24 stsp goto done;
7858 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
7859 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
7860 0ebf8283 2019-07-24 stsp goto done;
7861 0ebf8283 2019-07-24 stsp }
7862 0ebf8283 2019-07-24 stsp
7863 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
7864 0ebf8283 2019-07-24 stsp if (error)
7865 0ebf8283 2019-07-24 stsp goto done;
7866 0ebf8283 2019-07-24 stsp
7867 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
7868 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
7869 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
7870 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
7871 083957f4 2020-02-24 stsp "before the -m option can be used");
7872 083957f4 2020-02-24 stsp goto done;
7873 083957f4 2020-02-24 stsp }
7874 083957f4 2020-02-24 stsp
7875 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
7876 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7877 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7878 3e3a69f1 2019-07-25 stsp worktree, repo);
7879 0ebf8283 2019-07-24 stsp if (error)
7880 0ebf8283 2019-07-24 stsp goto done;
7881 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
7882 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7883 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
7884 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
7885 0ebf8283 2019-07-24 stsp if (error)
7886 0ebf8283 2019-07-24 stsp goto done;
7887 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
7888 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
7889 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
7890 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
7891 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7892 0ebf8283 2019-07-24 stsp goto done;
7893 0ebf8283 2019-07-24 stsp }
7894 0ebf8283 2019-07-24 stsp
7895 0ebf8283 2019-07-24 stsp if (continue_edit) {
7896 0ebf8283 2019-07-24 stsp char *path;
7897 0ebf8283 2019-07-24 stsp
7898 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
7899 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
7900 0ebf8283 2019-07-24 stsp goto done;
7901 0ebf8283 2019-07-24 stsp }
7902 0ebf8283 2019-07-24 stsp
7903 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
7904 0ebf8283 2019-07-24 stsp if (error)
7905 0ebf8283 2019-07-24 stsp goto done;
7906 0ebf8283 2019-07-24 stsp
7907 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
7908 0ebf8283 2019-07-24 stsp free(path);
7909 0ebf8283 2019-07-24 stsp if (error)
7910 0ebf8283 2019-07-24 stsp goto done;
7911 0ebf8283 2019-07-24 stsp
7912 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
7913 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
7914 3e3a69f1 2019-07-25 stsp worktree, repo);
7915 0ebf8283 2019-07-24 stsp if (error)
7916 0ebf8283 2019-07-24 stsp goto done;
7917 0ebf8283 2019-07-24 stsp
7918 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7919 0ebf8283 2019-07-24 stsp if (error)
7920 0ebf8283 2019-07-24 stsp goto done;
7921 0ebf8283 2019-07-24 stsp
7922 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7923 0ebf8283 2019-07-24 stsp head_commit_id);
7924 0ebf8283 2019-07-24 stsp if (error)
7925 0ebf8283 2019-07-24 stsp goto done;
7926 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7927 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7928 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7929 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7930 3aac7cf7 2019-07-25 stsp goto done;
7931 3aac7cf7 2019-07-25 stsp }
7932 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7933 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
7934 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7935 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7936 0ebf8283 2019-07-24 stsp commit = NULL;
7937 0ebf8283 2019-07-24 stsp if (error)
7938 0ebf8283 2019-07-24 stsp goto done;
7939 0ebf8283 2019-07-24 stsp } else {
7940 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
7941 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
7942 0ebf8283 2019-07-24 stsp goto done;
7943 0ebf8283 2019-07-24 stsp }
7944 0ebf8283 2019-07-24 stsp
7945 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
7946 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
7947 0ebf8283 2019-07-24 stsp if (error != NULL)
7948 0ebf8283 2019-07-24 stsp goto done;
7949 0ebf8283 2019-07-24 stsp
7950 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
7951 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
7952 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
7953 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
7954 c7d20a3f 2019-07-30 stsp goto done;
7955 c7d20a3f 2019-07-30 stsp }
7956 c7d20a3f 2019-07-30 stsp
7957 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
7958 bfce7f83 2019-07-27 stsp got_ref_close(branch);
7959 bfce7f83 2019-07-27 stsp branch = NULL;
7960 0ebf8283 2019-07-24 stsp if (error)
7961 0ebf8283 2019-07-24 stsp goto done;
7962 0ebf8283 2019-07-24 stsp
7963 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7964 0ebf8283 2019-07-24 stsp head_commit_id);
7965 0ebf8283 2019-07-24 stsp if (error)
7966 0ebf8283 2019-07-24 stsp goto done;
7967 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7968 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7969 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
7970 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7971 3aac7cf7 2019-07-25 stsp goto done;
7972 3aac7cf7 2019-07-25 stsp }
7973 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
7974 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
7975 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
7976 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
7977 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7978 0ebf8283 2019-07-24 stsp commit = NULL;
7979 0ebf8283 2019-07-24 stsp if (error)
7980 0ebf8283 2019-07-24 stsp goto done;
7981 0ebf8283 2019-07-24 stsp
7982 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
7983 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
7984 c5996fff 2020-01-29 stsp goto done;
7985 c5996fff 2020-01-29 stsp }
7986 c5996fff 2020-01-29 stsp
7987 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
7988 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
7989 bfce7f83 2019-07-27 stsp if (error)
7990 bfce7f83 2019-07-27 stsp goto done;
7991 bfce7f83 2019-07-27 stsp
7992 0ebf8283 2019-07-24 stsp if (edit_script_path) {
7993 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
7994 0ebf8283 2019-07-24 stsp edit_script_path, repo);
7995 6ba2bea2 2019-07-27 stsp if (error) {
7996 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
7997 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
7998 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
7999 0ebf8283 2019-07-24 stsp goto done;
8000 6ba2bea2 2019-07-27 stsp }
8001 0ebf8283 2019-07-24 stsp } else {
8002 514f2ffe 2020-01-29 stsp const char *branch_name;
8003 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
8004 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
8005 514f2ffe 2020-01-29 stsp branch_name += 11;
8006 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
8007 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
8008 6ba2bea2 2019-07-27 stsp if (error) {
8009 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8010 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8011 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8012 0ebf8283 2019-07-24 stsp goto done;
8013 6ba2bea2 2019-07-27 stsp }
8014 0ebf8283 2019-07-24 stsp
8015 0ebf8283 2019-07-24 stsp }
8016 0ebf8283 2019-07-24 stsp
8017 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
8018 0ebf8283 2019-07-24 stsp repo);
8019 6ba2bea2 2019-07-27 stsp if (error) {
8020 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
8021 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
8022 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
8023 0ebf8283 2019-07-24 stsp goto done;
8024 6ba2bea2 2019-07-27 stsp }
8025 0ebf8283 2019-07-24 stsp
8026 0ebf8283 2019-07-24 stsp }
8027 0ebf8283 2019-07-24 stsp
8028 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
8029 4ec14e60 2019-08-28 hiltjo if (error)
8030 0ebf8283 2019-07-24 stsp goto done;
8031 0ebf8283 2019-07-24 stsp
8032 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
8033 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
8034 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
8035 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
8036 0ebf8283 2019-07-24 stsp continue;
8037 0ebf8283 2019-07-24 stsp
8038 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
8039 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
8040 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
8041 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
8042 0ebf8283 2019-07-24 stsp repo);
8043 ab20a43a 2020-01-29 stsp if (error)
8044 ab20a43a 2020-01-29 stsp goto done;
8045 0ebf8283 2019-07-24 stsp } else {
8046 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
8047 ab20a43a 2020-01-29 stsp int have_changes = 0;
8048 ab20a43a 2020-01-29 stsp
8049 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
8050 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
8051 ab20a43a 2020-01-29 stsp if (error)
8052 ab20a43a 2020-01-29 stsp goto done;
8053 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
8054 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
8055 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
8056 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
8057 ab20a43a 2020-01-29 stsp if (error) {
8058 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
8059 ab20a43a 2020-01-29 stsp goto done;
8060 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
8061 ab20a43a 2020-01-29 stsp goto done;
8062 ab20a43a 2020-01-29 stsp }
8063 ab20a43a 2020-01-29 stsp if (have_changes) {
8064 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
8065 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
8066 ab20a43a 2020-01-29 stsp if (error)
8067 ab20a43a 2020-01-29 stsp goto done;
8068 ab20a43a 2020-01-29 stsp } else {
8069 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
8070 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
8071 ab20a43a 2020-01-29 stsp if (error)
8072 ab20a43a 2020-01-29 stsp goto done;
8073 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
8074 ab20a43a 2020-01-29 stsp hle, NULL);
8075 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
8076 ab20a43a 2020-01-29 stsp commit = NULL;
8077 ab20a43a 2020-01-29 stsp if (error)
8078 ab20a43a 2020-01-29 stsp goto done;
8079 ab20a43a 2020-01-29 stsp }
8080 0ebf8283 2019-07-24 stsp }
8081 0ebf8283 2019-07-24 stsp continue;
8082 0ebf8283 2019-07-24 stsp }
8083 0ebf8283 2019-07-24 stsp
8084 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
8085 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8086 0ebf8283 2019-07-24 stsp if (error)
8087 0ebf8283 2019-07-24 stsp goto done;
8088 0ebf8283 2019-07-24 stsp continue;
8089 0ebf8283 2019-07-24 stsp }
8090 0ebf8283 2019-07-24 stsp
8091 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
8092 0ebf8283 2019-07-24 stsp hle->commit_id);
8093 0ebf8283 2019-07-24 stsp if (error)
8094 0ebf8283 2019-07-24 stsp goto done;
8095 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8096 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
8097 0ebf8283 2019-07-24 stsp
8098 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
8099 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
8100 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
8101 0ebf8283 2019-07-24 stsp if (error)
8102 0ebf8283 2019-07-24 stsp goto done;
8103 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8104 0ebf8283 2019-07-24 stsp commit = NULL;
8105 0ebf8283 2019-07-24 stsp
8106 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8107 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
8108 a0ea4fc0 2020-02-28 stsp repo);
8109 a0ea4fc0 2020-02-28 stsp if (error)
8110 a0ea4fc0 2020-02-28 stsp goto done;
8111 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8112 0ebf8283 2019-07-24 stsp break;
8113 0ebf8283 2019-07-24 stsp }
8114 0ebf8283 2019-07-24 stsp
8115 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
8116 0ebf8283 2019-07-24 stsp char *id_str;
8117 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
8118 0ebf8283 2019-07-24 stsp if (error)
8119 0ebf8283 2019-07-24 stsp goto done;
8120 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
8121 0ebf8283 2019-07-24 stsp id_str);
8122 0ebf8283 2019-07-24 stsp free(id_str);
8123 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8124 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
8125 3e3a69f1 2019-07-25 stsp fileindex);
8126 0ebf8283 2019-07-24 stsp goto done;
8127 0ebf8283 2019-07-24 stsp }
8128 0ebf8283 2019-07-24 stsp
8129 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
8130 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
8131 0ebf8283 2019-07-24 stsp if (error)
8132 0ebf8283 2019-07-24 stsp goto done;
8133 0ebf8283 2019-07-24 stsp continue;
8134 0ebf8283 2019-07-24 stsp }
8135 0ebf8283 2019-07-24 stsp
8136 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
8137 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
8138 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
8139 0ebf8283 2019-07-24 stsp if (error)
8140 0ebf8283 2019-07-24 stsp goto done;
8141 0ebf8283 2019-07-24 stsp }
8142 0ebf8283 2019-07-24 stsp
8143 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
8144 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
8145 0ebf8283 2019-07-24 stsp if (error)
8146 0ebf8283 2019-07-24 stsp goto done;
8147 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
8148 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
8149 0ebf8283 2019-07-24 stsp } else
8150 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
8151 3e3a69f1 2019-07-25 stsp branch, repo);
8152 0ebf8283 2019-07-24 stsp done:
8153 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
8154 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
8155 0ebf8283 2019-07-24 stsp free(head_commit_id);
8156 0ebf8283 2019-07-24 stsp free(base_commit_id);
8157 0ebf8283 2019-07-24 stsp free(resume_commit_id);
8158 0ebf8283 2019-07-24 stsp if (commit)
8159 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
8160 0ebf8283 2019-07-24 stsp if (branch)
8161 0ebf8283 2019-07-24 stsp got_ref_close(branch);
8162 0ebf8283 2019-07-24 stsp if (tmp_branch)
8163 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
8164 0ebf8283 2019-07-24 stsp if (worktree)
8165 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
8166 2822a352 2019-10-15 stsp if (repo)
8167 2822a352 2019-10-15 stsp got_repo_close(repo);
8168 2822a352 2019-10-15 stsp return error;
8169 2822a352 2019-10-15 stsp }
8170 2822a352 2019-10-15 stsp
8171 2822a352 2019-10-15 stsp __dead static void
8172 2822a352 2019-10-15 stsp usage_integrate(void)
8173 2822a352 2019-10-15 stsp {
8174 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
8175 2822a352 2019-10-15 stsp exit(1);
8176 2822a352 2019-10-15 stsp }
8177 2822a352 2019-10-15 stsp
8178 2822a352 2019-10-15 stsp static const struct got_error *
8179 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
8180 2822a352 2019-10-15 stsp {
8181 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
8182 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
8183 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
8184 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
8185 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
8186 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
8187 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
8188 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
8189 2822a352 2019-10-15 stsp int ch, did_something = 0;
8190 2822a352 2019-10-15 stsp
8191 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
8192 2822a352 2019-10-15 stsp switch (ch) {
8193 2822a352 2019-10-15 stsp default:
8194 2822a352 2019-10-15 stsp usage_integrate();
8195 2822a352 2019-10-15 stsp /* NOTREACHED */
8196 2822a352 2019-10-15 stsp }
8197 2822a352 2019-10-15 stsp }
8198 2822a352 2019-10-15 stsp
8199 2822a352 2019-10-15 stsp argc -= optind;
8200 2822a352 2019-10-15 stsp argv += optind;
8201 2822a352 2019-10-15 stsp
8202 2822a352 2019-10-15 stsp if (argc != 1)
8203 2822a352 2019-10-15 stsp usage_integrate();
8204 2822a352 2019-10-15 stsp branch_arg = argv[0];
8205 2822a352 2019-10-15 stsp
8206 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8207 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
8208 2822a352 2019-10-15 stsp err(1, "pledge");
8209 2822a352 2019-10-15 stsp
8210 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
8211 2822a352 2019-10-15 stsp if (cwd == NULL) {
8212 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
8213 2822a352 2019-10-15 stsp goto done;
8214 2822a352 2019-10-15 stsp }
8215 2822a352 2019-10-15 stsp
8216 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
8217 2822a352 2019-10-15 stsp if (error)
8218 2822a352 2019-10-15 stsp goto done;
8219 2822a352 2019-10-15 stsp
8220 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
8221 2822a352 2019-10-15 stsp if (error)
8222 2822a352 2019-10-15 stsp goto done;
8223 2822a352 2019-10-15 stsp
8224 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8225 2822a352 2019-10-15 stsp NULL);
8226 2822a352 2019-10-15 stsp if (error != NULL)
8227 2822a352 2019-10-15 stsp goto done;
8228 2822a352 2019-10-15 stsp
8229 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8230 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
8231 2822a352 2019-10-15 stsp if (error)
8232 2822a352 2019-10-15 stsp goto done;
8233 2822a352 2019-10-15 stsp
8234 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
8235 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
8236 2822a352 2019-10-15 stsp goto done;
8237 2822a352 2019-10-15 stsp }
8238 2822a352 2019-10-15 stsp
8239 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
8240 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
8241 2822a352 2019-10-15 stsp if (error)
8242 2822a352 2019-10-15 stsp goto done;
8243 2822a352 2019-10-15 stsp
8244 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
8245 2822a352 2019-10-15 stsp if (refname == NULL) {
8246 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8247 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8248 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8249 2822a352 2019-10-15 stsp goto done;
8250 2822a352 2019-10-15 stsp }
8251 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
8252 2822a352 2019-10-15 stsp if (base_refname == NULL) {
8253 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
8254 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8255 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8256 2822a352 2019-10-15 stsp goto done;
8257 2822a352 2019-10-15 stsp }
8258 2822a352 2019-10-15 stsp
8259 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
8260 2822a352 2019-10-15 stsp if (error)
8261 2822a352 2019-10-15 stsp goto done;
8262 2822a352 2019-10-15 stsp
8263 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
8264 2822a352 2019-10-15 stsp if (error)
8265 2822a352 2019-10-15 stsp goto done;
8266 2822a352 2019-10-15 stsp
8267 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
8268 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
8269 2822a352 2019-10-15 stsp "specified branch has already been integrated");
8270 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8271 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8272 2822a352 2019-10-15 stsp goto done;
8273 2822a352 2019-10-15 stsp }
8274 2822a352 2019-10-15 stsp
8275 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
8276 2822a352 2019-10-15 stsp if (error) {
8277 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
8278 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
8279 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
8280 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
8281 2822a352 2019-10-15 stsp goto done;
8282 2822a352 2019-10-15 stsp }
8283 2822a352 2019-10-15 stsp
8284 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
8285 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
8286 2822a352 2019-10-15 stsp check_cancelled, NULL);
8287 2822a352 2019-10-15 stsp if (error)
8288 2822a352 2019-10-15 stsp goto done;
8289 2822a352 2019-10-15 stsp
8290 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
8291 2822a352 2019-10-15 stsp done:
8292 715dc77e 2019-08-03 stsp if (repo)
8293 715dc77e 2019-08-03 stsp got_repo_close(repo);
8294 2822a352 2019-10-15 stsp if (worktree)
8295 2822a352 2019-10-15 stsp got_worktree_close(worktree);
8296 2822a352 2019-10-15 stsp free(cwd);
8297 2822a352 2019-10-15 stsp free(base_commit_id);
8298 2822a352 2019-10-15 stsp free(commit_id);
8299 2822a352 2019-10-15 stsp free(refname);
8300 2822a352 2019-10-15 stsp free(base_refname);
8301 715dc77e 2019-08-03 stsp return error;
8302 715dc77e 2019-08-03 stsp }
8303 715dc77e 2019-08-03 stsp
8304 715dc77e 2019-08-03 stsp __dead static void
8305 715dc77e 2019-08-03 stsp usage_stage(void)
8306 715dc77e 2019-08-03 stsp {
8307 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
8308 f3055044 2019-08-07 stsp "[file-path ...]\n",
8309 715dc77e 2019-08-03 stsp getprogname());
8310 715dc77e 2019-08-03 stsp exit(1);
8311 715dc77e 2019-08-03 stsp }
8312 715dc77e 2019-08-03 stsp
8313 715dc77e 2019-08-03 stsp static const struct got_error *
8314 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
8315 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
8316 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
8317 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
8318 408b4ebc 2019-08-03 stsp {
8319 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
8320 408b4ebc 2019-08-03 stsp char *id_str = NULL;
8321 408b4ebc 2019-08-03 stsp
8322 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
8323 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
8324 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
8325 408b4ebc 2019-08-03 stsp return NULL;
8326 408b4ebc 2019-08-03 stsp
8327 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
8328 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
8329 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
8330 408b4ebc 2019-08-03 stsp else
8331 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
8332 408b4ebc 2019-08-03 stsp if (err)
8333 408b4ebc 2019-08-03 stsp return err;
8334 408b4ebc 2019-08-03 stsp
8335 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
8336 408b4ebc 2019-08-03 stsp free(id_str);
8337 dc424a06 2019-08-07 stsp return NULL;
8338 dc424a06 2019-08-07 stsp }
8339 2e1f37b0 2019-08-08 stsp
8340 dc424a06 2019-08-07 stsp static const struct got_error *
8341 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
8342 715dc77e 2019-08-03 stsp {
8343 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
8344 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
8345 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
8346 715dc77e 2019-08-03 stsp char *cwd = NULL;
8347 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
8348 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
8349 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
8350 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
8351 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8352 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8353 715dc77e 2019-08-03 stsp
8354 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
8355 715dc77e 2019-08-03 stsp
8356 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
8357 715dc77e 2019-08-03 stsp switch (ch) {
8358 408b4ebc 2019-08-03 stsp case 'l':
8359 408b4ebc 2019-08-03 stsp list_stage = 1;
8360 408b4ebc 2019-08-03 stsp break;
8361 dc424a06 2019-08-07 stsp case 'p':
8362 dc424a06 2019-08-07 stsp pflag = 1;
8363 dc424a06 2019-08-07 stsp break;
8364 dc424a06 2019-08-07 stsp case 'F':
8365 dc424a06 2019-08-07 stsp patch_script_path = optarg;
8366 dc424a06 2019-08-07 stsp break;
8367 715dc77e 2019-08-03 stsp default:
8368 715dc77e 2019-08-03 stsp usage_stage();
8369 715dc77e 2019-08-03 stsp /* NOTREACHED */
8370 715dc77e 2019-08-03 stsp }
8371 715dc77e 2019-08-03 stsp }
8372 715dc77e 2019-08-03 stsp
8373 715dc77e 2019-08-03 stsp argc -= optind;
8374 715dc77e 2019-08-03 stsp argv += optind;
8375 715dc77e 2019-08-03 stsp
8376 715dc77e 2019-08-03 stsp #ifndef PROFILE
8377 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8378 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
8379 715dc77e 2019-08-03 stsp err(1, "pledge");
8380 715dc77e 2019-08-03 stsp #endif
8381 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
8382 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
8383 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
8384 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
8385 715dc77e 2019-08-03 stsp
8386 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
8387 715dc77e 2019-08-03 stsp if (cwd == NULL) {
8388 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
8389 715dc77e 2019-08-03 stsp goto done;
8390 715dc77e 2019-08-03 stsp }
8391 715dc77e 2019-08-03 stsp
8392 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8393 715dc77e 2019-08-03 stsp if (error)
8394 715dc77e 2019-08-03 stsp goto done;
8395 715dc77e 2019-08-03 stsp
8396 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8397 c9956ddf 2019-09-08 stsp NULL);
8398 715dc77e 2019-08-03 stsp if (error != NULL)
8399 715dc77e 2019-08-03 stsp goto done;
8400 715dc77e 2019-08-03 stsp
8401 dc424a06 2019-08-07 stsp if (patch_script_path) {
8402 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
8403 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
8404 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
8405 dc424a06 2019-08-07 stsp patch_script_path);
8406 dc424a06 2019-08-07 stsp goto done;
8407 dc424a06 2019-08-07 stsp }
8408 dc424a06 2019-08-07 stsp }
8409 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8410 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
8411 715dc77e 2019-08-03 stsp if (error)
8412 715dc77e 2019-08-03 stsp goto done;
8413 715dc77e 2019-08-03 stsp
8414 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8415 6d022e97 2019-08-04 stsp if (error)
8416 6d022e97 2019-08-04 stsp goto done;
8417 715dc77e 2019-08-03 stsp
8418 6d022e97 2019-08-04 stsp if (list_stage)
8419 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
8420 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
8421 2e1f37b0 2019-08-08 stsp else {
8422 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8423 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
8424 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
8425 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
8426 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
8427 2e1f37b0 2019-08-08 stsp }
8428 ad493afc 2019-08-03 stsp done:
8429 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8430 2e1f37b0 2019-08-08 stsp error == NULL)
8431 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8432 ad493afc 2019-08-03 stsp if (repo)
8433 ad493afc 2019-08-03 stsp got_repo_close(repo);
8434 ad493afc 2019-08-03 stsp if (worktree)
8435 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
8436 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8437 ad493afc 2019-08-03 stsp free((char *)pe->path);
8438 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
8439 ad493afc 2019-08-03 stsp free(cwd);
8440 ad493afc 2019-08-03 stsp return error;
8441 ad493afc 2019-08-03 stsp }
8442 ad493afc 2019-08-03 stsp
8443 ad493afc 2019-08-03 stsp __dead static void
8444 ad493afc 2019-08-03 stsp usage_unstage(void)
8445 ad493afc 2019-08-03 stsp {
8446 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
8447 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
8448 ad493afc 2019-08-03 stsp getprogname());
8449 ad493afc 2019-08-03 stsp exit(1);
8450 ad493afc 2019-08-03 stsp }
8451 ad493afc 2019-08-03 stsp
8452 ad493afc 2019-08-03 stsp
8453 ad493afc 2019-08-03 stsp static const struct got_error *
8454 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
8455 ad493afc 2019-08-03 stsp {
8456 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
8457 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
8458 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
8459 ad493afc 2019-08-03 stsp char *cwd = NULL;
8460 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
8461 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
8462 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
8463 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
8464 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
8465 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
8466 ad493afc 2019-08-03 stsp
8467 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
8468 ad493afc 2019-08-03 stsp
8469 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
8470 ad493afc 2019-08-03 stsp switch (ch) {
8471 2e1f37b0 2019-08-08 stsp case 'p':
8472 2e1f37b0 2019-08-08 stsp pflag = 1;
8473 2e1f37b0 2019-08-08 stsp break;
8474 2e1f37b0 2019-08-08 stsp case 'F':
8475 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
8476 2e1f37b0 2019-08-08 stsp break;
8477 ad493afc 2019-08-03 stsp default:
8478 ad493afc 2019-08-03 stsp usage_unstage();
8479 ad493afc 2019-08-03 stsp /* NOTREACHED */
8480 ad493afc 2019-08-03 stsp }
8481 ad493afc 2019-08-03 stsp }
8482 ad493afc 2019-08-03 stsp
8483 ad493afc 2019-08-03 stsp argc -= optind;
8484 ad493afc 2019-08-03 stsp argv += optind;
8485 ad493afc 2019-08-03 stsp
8486 ad493afc 2019-08-03 stsp #ifndef PROFILE
8487 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
8488 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
8489 ad493afc 2019-08-03 stsp err(1, "pledge");
8490 ad493afc 2019-08-03 stsp #endif
8491 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
8492 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
8493 2e1f37b0 2019-08-08 stsp
8494 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
8495 ad493afc 2019-08-03 stsp if (cwd == NULL) {
8496 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
8497 ad493afc 2019-08-03 stsp goto done;
8498 ad493afc 2019-08-03 stsp }
8499 ad493afc 2019-08-03 stsp
8500 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
8501 ad493afc 2019-08-03 stsp if (error)
8502 ad493afc 2019-08-03 stsp goto done;
8503 ad493afc 2019-08-03 stsp
8504 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
8505 c9956ddf 2019-09-08 stsp NULL);
8506 ad493afc 2019-08-03 stsp if (error != NULL)
8507 ad493afc 2019-08-03 stsp goto done;
8508 ad493afc 2019-08-03 stsp
8509 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
8510 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
8511 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
8512 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
8513 2e1f37b0 2019-08-08 stsp patch_script_path);
8514 2e1f37b0 2019-08-08 stsp goto done;
8515 2e1f37b0 2019-08-08 stsp }
8516 2e1f37b0 2019-08-08 stsp }
8517 2e1f37b0 2019-08-08 stsp
8518 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
8519 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
8520 ad493afc 2019-08-03 stsp if (error)
8521 ad493afc 2019-08-03 stsp goto done;
8522 ad493afc 2019-08-03 stsp
8523 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
8524 ad493afc 2019-08-03 stsp if (error)
8525 ad493afc 2019-08-03 stsp goto done;
8526 ad493afc 2019-08-03 stsp
8527 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
8528 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
8529 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
8530 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
8531 715dc77e 2019-08-03 stsp done:
8532 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
8533 2e1f37b0 2019-08-08 stsp error == NULL)
8534 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
8535 0ebf8283 2019-07-24 stsp if (repo)
8536 0ebf8283 2019-07-24 stsp got_repo_close(repo);
8537 715dc77e 2019-08-03 stsp if (worktree)
8538 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
8539 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
8540 715dc77e 2019-08-03 stsp free((char *)pe->path);
8541 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
8542 715dc77e 2019-08-03 stsp free(cwd);
8543 01073a5d 2019-08-22 stsp return error;
8544 01073a5d 2019-08-22 stsp }
8545 01073a5d 2019-08-22 stsp
8546 01073a5d 2019-08-22 stsp __dead static void
8547 01073a5d 2019-08-22 stsp usage_cat(void)
8548 01073a5d 2019-08-22 stsp {
8549 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
8550 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
8551 01073a5d 2019-08-22 stsp exit(1);
8552 01073a5d 2019-08-22 stsp }
8553 01073a5d 2019-08-22 stsp
8554 01073a5d 2019-08-22 stsp static const struct got_error *
8555 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8556 01073a5d 2019-08-22 stsp {
8557 01073a5d 2019-08-22 stsp const struct got_error *err;
8558 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
8559 01073a5d 2019-08-22 stsp
8560 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
8561 01073a5d 2019-08-22 stsp if (err)
8562 01073a5d 2019-08-22 stsp return err;
8563 01073a5d 2019-08-22 stsp
8564 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
8565 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
8566 01073a5d 2019-08-22 stsp return err;
8567 01073a5d 2019-08-22 stsp }
8568 01073a5d 2019-08-22 stsp
8569 01073a5d 2019-08-22 stsp static const struct got_error *
8570 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8571 01073a5d 2019-08-22 stsp {
8572 01073a5d 2019-08-22 stsp const struct got_error *err;
8573 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
8574 56e0773d 2019-11-28 stsp int nentries, i;
8575 01073a5d 2019-08-22 stsp
8576 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
8577 01073a5d 2019-08-22 stsp if (err)
8578 01073a5d 2019-08-22 stsp return err;
8579 01073a5d 2019-08-22 stsp
8580 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
8581 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
8582 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
8583 01073a5d 2019-08-22 stsp char *id_str;
8584 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
8585 01073a5d 2019-08-22 stsp break;
8586 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
8587 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
8588 01073a5d 2019-08-22 stsp if (err)
8589 01073a5d 2019-08-22 stsp break;
8590 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
8591 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
8592 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
8593 01073a5d 2019-08-22 stsp free(id_str);
8594 01073a5d 2019-08-22 stsp }
8595 01073a5d 2019-08-22 stsp
8596 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
8597 01073a5d 2019-08-22 stsp return err;
8598 01073a5d 2019-08-22 stsp }
8599 01073a5d 2019-08-22 stsp
8600 01073a5d 2019-08-22 stsp static const struct got_error *
8601 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8602 01073a5d 2019-08-22 stsp {
8603 01073a5d 2019-08-22 stsp const struct got_error *err;
8604 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
8605 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
8606 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
8607 01073a5d 2019-08-22 stsp char *id_str = NULL;
8608 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
8609 01073a5d 2019-08-22 stsp
8610 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
8611 01073a5d 2019-08-22 stsp if (err)
8612 01073a5d 2019-08-22 stsp return err;
8613 01073a5d 2019-08-22 stsp
8614 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
8615 01073a5d 2019-08-22 stsp if (err)
8616 01073a5d 2019-08-22 stsp goto done;
8617 01073a5d 2019-08-22 stsp
8618 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
8619 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
8620 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
8621 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
8622 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
8623 01073a5d 2019-08-22 stsp char *pid_str;
8624 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
8625 01073a5d 2019-08-22 stsp if (err)
8626 01073a5d 2019-08-22 stsp goto done;
8627 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
8628 01073a5d 2019-08-22 stsp free(pid_str);
8629 01073a5d 2019-08-22 stsp }
8630 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
8631 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8632 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
8633 01073a5d 2019-08-22 stsp
8634 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
8635 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
8636 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
8637 01073a5d 2019-08-22 stsp
8638 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
8639 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
8640 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
8641 01073a5d 2019-08-22 stsp done:
8642 01073a5d 2019-08-22 stsp free(id_str);
8643 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
8644 01073a5d 2019-08-22 stsp return err;
8645 01073a5d 2019-08-22 stsp }
8646 01073a5d 2019-08-22 stsp
8647 01073a5d 2019-08-22 stsp static const struct got_error *
8648 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
8649 01073a5d 2019-08-22 stsp {
8650 01073a5d 2019-08-22 stsp const struct got_error *err;
8651 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
8652 01073a5d 2019-08-22 stsp char *id_str = NULL;
8653 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
8654 01073a5d 2019-08-22 stsp
8655 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
8656 01073a5d 2019-08-22 stsp if (err)
8657 01073a5d 2019-08-22 stsp return err;
8658 01073a5d 2019-08-22 stsp
8659 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
8660 01073a5d 2019-08-22 stsp if (err)
8661 01073a5d 2019-08-22 stsp goto done;
8662 01073a5d 2019-08-22 stsp
8663 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
8664 e15d5241 2019-08-22 stsp
8665 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
8666 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8667 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8668 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
8669 01073a5d 2019-08-22 stsp break;
8670 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8671 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8672 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
8673 01073a5d 2019-08-22 stsp break;
8674 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8675 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8676 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
8677 01073a5d 2019-08-22 stsp break;
8678 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8679 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
8680 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
8681 01073a5d 2019-08-22 stsp break;
8682 01073a5d 2019-08-22 stsp default:
8683 01073a5d 2019-08-22 stsp break;
8684 01073a5d 2019-08-22 stsp }
8685 01073a5d 2019-08-22 stsp
8686 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
8687 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
8688 e15d5241 2019-08-22 stsp
8689 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
8690 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
8691 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
8692 01073a5d 2019-08-22 stsp
8693 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
8694 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
8695 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
8696 01073a5d 2019-08-22 stsp done:
8697 01073a5d 2019-08-22 stsp free(id_str);
8698 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
8699 01073a5d 2019-08-22 stsp return err;
8700 01073a5d 2019-08-22 stsp }
8701 01073a5d 2019-08-22 stsp
8702 01073a5d 2019-08-22 stsp static const struct got_error *
8703 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
8704 01073a5d 2019-08-22 stsp {
8705 01073a5d 2019-08-22 stsp const struct got_error *error;
8706 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
8707 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
8708 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
8709 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
8710 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
8711 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
8712 01073a5d 2019-08-22 stsp
8713 01073a5d 2019-08-22 stsp #ifndef PROFILE
8714 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
8715 01073a5d 2019-08-22 stsp NULL) == -1)
8716 01073a5d 2019-08-22 stsp err(1, "pledge");
8717 01073a5d 2019-08-22 stsp #endif
8718 01073a5d 2019-08-22 stsp
8719 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
8720 01073a5d 2019-08-22 stsp switch (ch) {
8721 896e9b6f 2019-08-26 stsp case 'c':
8722 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
8723 896e9b6f 2019-08-26 stsp break;
8724 01073a5d 2019-08-22 stsp case 'r':
8725 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
8726 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8727 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
8728 9ba1d308 2019-10-21 stsp optarg);
8729 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
8730 01073a5d 2019-08-22 stsp break;
8731 896e9b6f 2019-08-26 stsp case 'P':
8732 896e9b6f 2019-08-26 stsp force_path = 1;
8733 896e9b6f 2019-08-26 stsp break;
8734 01073a5d 2019-08-22 stsp default:
8735 01073a5d 2019-08-22 stsp usage_cat();
8736 01073a5d 2019-08-22 stsp /* NOTREACHED */
8737 01073a5d 2019-08-22 stsp }
8738 01073a5d 2019-08-22 stsp }
8739 01073a5d 2019-08-22 stsp
8740 01073a5d 2019-08-22 stsp argc -= optind;
8741 01073a5d 2019-08-22 stsp argv += optind;
8742 01073a5d 2019-08-22 stsp
8743 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
8744 01073a5d 2019-08-22 stsp if (cwd == NULL) {
8745 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
8746 01073a5d 2019-08-22 stsp goto done;
8747 01073a5d 2019-08-22 stsp }
8748 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
8749 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
8750 01073a5d 2019-08-22 stsp goto done;
8751 01073a5d 2019-08-22 stsp if (worktree) {
8752 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8753 01073a5d 2019-08-22 stsp repo_path = strdup(
8754 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
8755 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8756 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
8757 01073a5d 2019-08-22 stsp goto done;
8758 01073a5d 2019-08-22 stsp }
8759 01073a5d 2019-08-22 stsp }
8760 01073a5d 2019-08-22 stsp }
8761 01073a5d 2019-08-22 stsp
8762 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
8763 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
8764 01073a5d 2019-08-22 stsp if (repo_path == NULL)
8765 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
8766 01073a5d 2019-08-22 stsp }
8767 01073a5d 2019-08-22 stsp
8768 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
8769 01073a5d 2019-08-22 stsp free(repo_path);
8770 01073a5d 2019-08-22 stsp if (error != NULL)
8771 01073a5d 2019-08-22 stsp goto done;
8772 01073a5d 2019-08-22 stsp
8773 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
8774 896e9b6f 2019-08-26 stsp if (error)
8775 896e9b6f 2019-08-26 stsp goto done;
8776 896e9b6f 2019-08-26 stsp
8777 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
8778 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
8779 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
8780 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
8781 01073a5d 2019-08-22 stsp if (error)
8782 01073a5d 2019-08-22 stsp goto done;
8783 01073a5d 2019-08-22 stsp
8784 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
8785 896e9b6f 2019-08-26 stsp if (force_path) {
8786 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
8787 896e9b6f 2019-08-26 stsp argv[i]);
8788 896e9b6f 2019-08-26 stsp if (error)
8789 896e9b6f 2019-08-26 stsp break;
8790 896e9b6f 2019-08-26 stsp } else {
8791 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
8792 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
8793 896e9b6f 2019-08-26 stsp if (error) {
8794 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
8795 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
8796 896e9b6f 2019-08-26 stsp break;
8797 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
8798 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
8799 896e9b6f 2019-08-26 stsp if (error)
8800 896e9b6f 2019-08-26 stsp break;
8801 896e9b6f 2019-08-26 stsp }
8802 896e9b6f 2019-08-26 stsp }
8803 01073a5d 2019-08-22 stsp
8804 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
8805 01073a5d 2019-08-22 stsp if (error)
8806 01073a5d 2019-08-22 stsp break;
8807 01073a5d 2019-08-22 stsp
8808 01073a5d 2019-08-22 stsp switch (obj_type) {
8809 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
8810 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
8811 01073a5d 2019-08-22 stsp break;
8812 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
8813 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
8814 01073a5d 2019-08-22 stsp break;
8815 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
8816 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
8817 01073a5d 2019-08-22 stsp break;
8818 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
8819 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
8820 01073a5d 2019-08-22 stsp break;
8821 01073a5d 2019-08-22 stsp default:
8822 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
8823 01073a5d 2019-08-22 stsp break;
8824 01073a5d 2019-08-22 stsp }
8825 01073a5d 2019-08-22 stsp if (error)
8826 01073a5d 2019-08-22 stsp break;
8827 01073a5d 2019-08-22 stsp free(label);
8828 01073a5d 2019-08-22 stsp label = NULL;
8829 01073a5d 2019-08-22 stsp free(id);
8830 01073a5d 2019-08-22 stsp id = NULL;
8831 01073a5d 2019-08-22 stsp }
8832 01073a5d 2019-08-22 stsp done:
8833 01073a5d 2019-08-22 stsp free(label);
8834 01073a5d 2019-08-22 stsp free(id);
8835 896e9b6f 2019-08-26 stsp free(commit_id);
8836 01073a5d 2019-08-22 stsp if (worktree)
8837 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
8838 01073a5d 2019-08-22 stsp if (repo) {
8839 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
8840 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
8841 01073a5d 2019-08-22 stsp if (error == NULL)
8842 01073a5d 2019-08-22 stsp error = repo_error;
8843 01073a5d 2019-08-22 stsp }
8844 0ebf8283 2019-07-24 stsp return error;
8845 0ebf8283 2019-07-24 stsp }