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 5c860e29 2018-03-12 stsp
42 53ccebc2 2019-07-30 stsp #include "got_version.h"
43 f42b1b34 2018-03-12 stsp #include "got_error.h"
44 f42b1b34 2018-03-12 stsp #include "got_object.h"
45 5261c201 2018-04-01 stsp #include "got_reference.h"
46 f42b1b34 2018-03-12 stsp #include "got_repository.h"
47 1dd54920 2019-05-11 stsp #include "got_path.h"
48 e6209546 2019-08-22 stsp #include "got_cancel.h"
49 c09a553d 2018-03-12 stsp #include "got_worktree.h"
50 79109fed 2018-03-27 stsp #include "got_diff.h"
51 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
52 6f23baec 2020-03-18 stsp #include "got_fetch.h"
53 404c43c4 2018-06-21 stsp #include "got_blame.h"
54 63219cd2 2019-01-04 stsp #include "got_privsep.h"
55 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
56 5c860e29 2018-03-12 stsp
57 5c860e29 2018-03-12 stsp #ifndef nitems
58 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 5c860e29 2018-03-12 stsp #endif
60 99437157 2018-11-11 stsp
61 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
62 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static void
65 99437157 2018-11-11 stsp catch_sigint(int signo)
66 99437157 2018-11-11 stsp {
67 99437157 2018-11-11 stsp sigint_received = 1;
68 99437157 2018-11-11 stsp }
69 99437157 2018-11-11 stsp
70 99437157 2018-11-11 stsp static void
71 99437157 2018-11-11 stsp catch_sigpipe(int signo)
72 99437157 2018-11-11 stsp {
73 99437157 2018-11-11 stsp sigpipe_received = 1;
74 99437157 2018-11-11 stsp }
75 5c860e29 2018-03-12 stsp
76 99437157 2018-11-11 stsp
77 8cfb4057 2019-07-09 stsp struct got_cmd {
78 5c860e29 2018-03-12 stsp const char *cmd_name;
79 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
80 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
81 97b3a7be 2019-07-09 stsp const char *cmd_alias;
82 5c860e29 2018-03-12 stsp };
83 5c860e29 2018-03-12 stsp
84 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
85 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
86 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
88 93658fb9 2020-03-18 stsp __dead static void usage_clone(void);
89 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
90 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
91 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
92 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
93 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
94 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
95 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
96 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
97 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
98 d00136be 2019-03-26 stsp __dead static void usage_add(void);
99 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
100 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
101 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
102 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
103 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
104 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
105 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
106 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
107 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
108 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
109 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
110 5c860e29 2018-03-12 stsp
111 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
112 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
113 93658fb9 2020-03-18 stsp static const struct got_error* cmd_clone(int, char *[]);
114 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
115 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
117 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
118 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
119 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
120 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
121 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
122 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
123 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
124 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
125 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
126 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
127 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
128 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
129 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
130 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
131 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
132 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
133 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
134 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
135 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
136 5c860e29 2018-03-12 stsp
137 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
138 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
139 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
140 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
141 93658fb9 2020-03-18 stsp { "clone", cmd_clone, usage_clone, "cl" },
142 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
143 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
144 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
145 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
146 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
147 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
148 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
149 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
150 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
151 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
152 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
153 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
154 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
155 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
156 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
157 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
158 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
159 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
160 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
161 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
162 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
163 5c860e29 2018-03-12 stsp };
164 ce5b7c56 2019-07-09 stsp
165 ce5b7c56 2019-07-09 stsp static void
166 ce5b7c56 2019-07-09 stsp list_commands(void)
167 ce5b7c56 2019-07-09 stsp {
168 ce5b7c56 2019-07-09 stsp int i;
169 ce5b7c56 2019-07-09 stsp
170 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
171 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
172 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
173 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
174 ce5b7c56 2019-07-09 stsp }
175 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
176 ce5b7c56 2019-07-09 stsp }
177 5c860e29 2018-03-12 stsp
178 5c860e29 2018-03-12 stsp int
179 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
180 5c860e29 2018-03-12 stsp {
181 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
182 5c860e29 2018-03-12 stsp unsigned int i;
183 5c860e29 2018-03-12 stsp int ch;
184 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
185 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
186 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
187 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
188 83cd27f8 2020-01-13 stsp };
189 5c860e29 2018-03-12 stsp
190 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
191 5c860e29 2018-03-12 stsp
192 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
193 5c860e29 2018-03-12 stsp switch (ch) {
194 1b6b95a8 2018-03-12 stsp case 'h':
195 1b6b95a8 2018-03-12 stsp hflag = 1;
196 53ccebc2 2019-07-30 stsp break;
197 53ccebc2 2019-07-30 stsp case 'V':
198 53ccebc2 2019-07-30 stsp Vflag = 1;
199 1b6b95a8 2018-03-12 stsp break;
200 5c860e29 2018-03-12 stsp default:
201 ce5b7c56 2019-07-09 stsp usage(hflag);
202 5c860e29 2018-03-12 stsp /* NOTREACHED */
203 5c860e29 2018-03-12 stsp }
204 5c860e29 2018-03-12 stsp }
205 5c860e29 2018-03-12 stsp
206 5c860e29 2018-03-12 stsp argc -= optind;
207 5c860e29 2018-03-12 stsp argv += optind;
208 1e70621d 2018-03-27 stsp optind = 0;
209 53ccebc2 2019-07-30 stsp
210 53ccebc2 2019-07-30 stsp if (Vflag) {
211 53ccebc2 2019-07-30 stsp got_version_print_str();
212 53ccebc2 2019-07-30 stsp return 1;
213 53ccebc2 2019-07-30 stsp }
214 5c860e29 2018-03-12 stsp
215 5c860e29 2018-03-12 stsp if (argc <= 0)
216 ce5b7c56 2019-07-09 stsp usage(hflag);
217 5c860e29 2018-03-12 stsp
218 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
219 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
220 99437157 2018-11-11 stsp
221 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
222 d7d4f210 2018-03-12 stsp const struct got_error *error;
223 d7d4f210 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
225 5c860e29 2018-03-12 stsp
226 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
227 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
228 5c860e29 2018-03-12 stsp continue;
229 5c860e29 2018-03-12 stsp
230 1b6b95a8 2018-03-12 stsp if (hflag)
231 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
232 1b6b95a8 2018-03-12 stsp
233 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
234 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
235 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
236 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
237 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
238 70015d7a 2019-11-08 stsp !(sigint_received &&
239 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
240 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
241 d7d4f210 2018-03-12 stsp return 1;
242 d7d4f210 2018-03-12 stsp }
243 d7d4f210 2018-03-12 stsp
244 d7d4f210 2018-03-12 stsp return 0;
245 5c860e29 2018-03-12 stsp }
246 5c860e29 2018-03-12 stsp
247 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
248 ce5b7c56 2019-07-09 stsp list_commands();
249 5c860e29 2018-03-12 stsp return 1;
250 5c860e29 2018-03-12 stsp }
251 5c860e29 2018-03-12 stsp
252 4ed7e80c 2018-05-20 stsp __dead static void
253 ce5b7c56 2019-07-09 stsp usage(int hflag)
254 5c860e29 2018-03-12 stsp {
255 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
256 53ccebc2 2019-07-30 stsp getprogname());
257 ce5b7c56 2019-07-09 stsp if (hflag)
258 ce5b7c56 2019-07-09 stsp list_commands();
259 5c860e29 2018-03-12 stsp exit(1);
260 5c860e29 2018-03-12 stsp }
261 5c860e29 2018-03-12 stsp
262 0266afb7 2019-01-04 stsp static const struct got_error *
263 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
264 e2ba3d07 2019-05-13 stsp {
265 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
266 e2ba3d07 2019-05-13 stsp const char *editor;
267 8920fa04 2019-08-18 stsp
268 8920fa04 2019-08-18 stsp *abspath = NULL;
269 e2ba3d07 2019-05-13 stsp
270 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
271 e2ba3d07 2019-05-13 stsp if (editor == NULL)
272 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
273 e2ba3d07 2019-05-13 stsp
274 0ee7065d 2019-05-13 stsp if (editor) {
275 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
276 0ee7065d 2019-05-13 stsp if (err)
277 0ee7065d 2019-05-13 stsp return err;
278 0ee7065d 2019-05-13 stsp }
279 e2ba3d07 2019-05-13 stsp
280 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
281 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
282 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
283 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
284 0ee7065d 2019-05-13 stsp }
285 0ee7065d 2019-05-13 stsp
286 e2ba3d07 2019-05-13 stsp return NULL;
287 e2ba3d07 2019-05-13 stsp }
288 e2ba3d07 2019-05-13 stsp
289 e2ba3d07 2019-05-13 stsp static const struct got_error *
290 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
291 c530dc23 2019-07-23 stsp const char *worktree_path)
292 0266afb7 2019-01-04 stsp {
293 163ce85a 2019-05-13 stsp const struct got_error *err;
294 0266afb7 2019-01-04 stsp
295 37c06ea4 2019-07-15 stsp #ifdef PROFILE
296 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
297 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
298 37c06ea4 2019-07-15 stsp #endif
299 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
300 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
301 0266afb7 2019-01-04 stsp
302 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
304 0266afb7 2019-01-04 stsp
305 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
306 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
307 0266afb7 2019-01-04 stsp
308 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
309 163ce85a 2019-05-13 stsp if (err != NULL)
310 163ce85a 2019-05-13 stsp return err;
311 0266afb7 2019-01-04 stsp
312 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
313 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
314 0266afb7 2019-01-04 stsp
315 0266afb7 2019-01-04 stsp return NULL;
316 2c7829a4 2019-06-17 stsp }
317 2c7829a4 2019-06-17 stsp
318 2c7829a4 2019-06-17 stsp __dead static void
319 2c7829a4 2019-06-17 stsp usage_init(void)
320 2c7829a4 2019-06-17 stsp {
321 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
322 2c7829a4 2019-06-17 stsp exit(1);
323 2c7829a4 2019-06-17 stsp }
324 2c7829a4 2019-06-17 stsp
325 2c7829a4 2019-06-17 stsp static const struct got_error *
326 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
327 2c7829a4 2019-06-17 stsp {
328 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
329 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
330 2c7829a4 2019-06-17 stsp int ch;
331 2c7829a4 2019-06-17 stsp
332 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
333 2c7829a4 2019-06-17 stsp switch (ch) {
334 2c7829a4 2019-06-17 stsp default:
335 2c7829a4 2019-06-17 stsp usage_init();
336 2c7829a4 2019-06-17 stsp /* NOTREACHED */
337 2c7829a4 2019-06-17 stsp }
338 2c7829a4 2019-06-17 stsp }
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp argc -= optind;
341 2c7829a4 2019-06-17 stsp argv += optind;
342 2c7829a4 2019-06-17 stsp
343 2c7829a4 2019-06-17 stsp #ifndef PROFILE
344 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
345 2c7829a4 2019-06-17 stsp err(1, "pledge");
346 2c7829a4 2019-06-17 stsp #endif
347 2c7829a4 2019-06-17 stsp if (argc != 1)
348 bc20e173 2019-06-17 stsp usage_init();
349 2c7829a4 2019-06-17 stsp
350 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
351 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
352 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
353 2c7829a4 2019-06-17 stsp
354 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
355 2c7829a4 2019-06-17 stsp
356 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
357 2c7829a4 2019-06-17 stsp if (error &&
358 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
359 2c7829a4 2019-06-17 stsp goto done;
360 2c7829a4 2019-06-17 stsp
361 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
362 2c7829a4 2019-06-17 stsp if (error)
363 2c7829a4 2019-06-17 stsp goto done;
364 2c7829a4 2019-06-17 stsp
365 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
366 3ce1b845 2019-07-15 stsp done:
367 3ce1b845 2019-07-15 stsp free(repo_path);
368 3ce1b845 2019-07-15 stsp return error;
369 3ce1b845 2019-07-15 stsp }
370 3ce1b845 2019-07-15 stsp
371 3ce1b845 2019-07-15 stsp __dead static void
372 3ce1b845 2019-07-15 stsp usage_import(void)
373 3ce1b845 2019-07-15 stsp {
374 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
375 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
376 3ce1b845 2019-07-15 stsp exit(1);
377 3ce1b845 2019-07-15 stsp }
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp int
380 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
381 3ce1b845 2019-07-15 stsp {
382 3ce1b845 2019-07-15 stsp pid_t pid;
383 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
384 3ce1b845 2019-07-15 stsp int st = -1;
385 3ce1b845 2019-07-15 stsp
386 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
387 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
388 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
389 3ce1b845 2019-07-15 stsp
390 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
391 3ce1b845 2019-07-15 stsp case -1:
392 3ce1b845 2019-07-15 stsp goto doneediting;
393 3ce1b845 2019-07-15 stsp case 0:
394 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
395 3ce1b845 2019-07-15 stsp _exit(127);
396 3ce1b845 2019-07-15 stsp }
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
399 3ce1b845 2019-07-15 stsp if (errno != EINTR)
400 3ce1b845 2019-07-15 stsp break;
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp doneediting:
403 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
404 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
405 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
408 3ce1b845 2019-07-15 stsp errno = EINTR;
409 3ce1b845 2019-07-15 stsp return -1;
410 3ce1b845 2019-07-15 stsp }
411 3ce1b845 2019-07-15 stsp
412 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
413 3ce1b845 2019-07-15 stsp }
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp static const struct got_error *
416 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
417 3ce1b845 2019-07-15 stsp const char *initial_content)
418 3ce1b845 2019-07-15 stsp {
419 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
420 3ce1b845 2019-07-15 stsp char buf[1024];
421 3ce1b845 2019-07-15 stsp struct stat st, st2;
422 3ce1b845 2019-07-15 stsp FILE *fp;
423 3ce1b845 2019-07-15 stsp int content_changed = 0;
424 3ce1b845 2019-07-15 stsp size_t len;
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp *logmsg = NULL;
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
430 3ce1b845 2019-07-15 stsp
431 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
432 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
435 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
436 3ce1b845 2019-07-15 stsp
437 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
438 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
439 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
440 3ce1b845 2019-07-15 stsp
441 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
442 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
443 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
444 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
445 3ce1b845 2019-07-15 stsp len = 0;
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
448 3ce1b845 2019-07-15 stsp if (fp == NULL) {
449 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
450 3ce1b845 2019-07-15 stsp goto done;
451 3ce1b845 2019-07-15 stsp }
452 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
453 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
454 3ce1b845 2019-07-15 stsp content_changed = 1;
455 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
456 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
457 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
458 3ce1b845 2019-07-15 stsp }
459 3ce1b845 2019-07-15 stsp fclose(fp);
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
462 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
463 3ce1b845 2019-07-15 stsp len--;
464 3ce1b845 2019-07-15 stsp }
465 3ce1b845 2019-07-15 stsp
466 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
467 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
468 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
469 3ce1b845 2019-07-15 stsp done:
470 3ce1b845 2019-07-15 stsp if (err) {
471 3ce1b845 2019-07-15 stsp free(*logmsg);
472 3ce1b845 2019-07-15 stsp *logmsg = NULL;
473 3ce1b845 2019-07-15 stsp }
474 3ce1b845 2019-07-15 stsp return err;
475 3ce1b845 2019-07-15 stsp }
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
479 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
480 3ce1b845 2019-07-15 stsp {
481 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
482 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
483 3ce1b845 2019-07-15 stsp int fd;
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
486 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
487 3ce1b845 2019-07-15 stsp branch_name) == -1)
488 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
489 3ce1b845 2019-07-15 stsp
490 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
491 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
492 3ce1b845 2019-07-15 stsp if (err)
493 3ce1b845 2019-07-15 stsp goto done;
494 3ce1b845 2019-07-15 stsp
495 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
496 3ce1b845 2019-07-15 stsp close(fd);
497 3ce1b845 2019-07-15 stsp
498 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
499 3ce1b845 2019-07-15 stsp done:
500 3ce1b845 2019-07-15 stsp free(initial_content);
501 3ce1b845 2019-07-15 stsp return err;
502 3ce1b845 2019-07-15 stsp }
503 3ce1b845 2019-07-15 stsp
504 3ce1b845 2019-07-15 stsp static const struct got_error *
505 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
506 3ce1b845 2019-07-15 stsp {
507 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
508 3ce1b845 2019-07-15 stsp return NULL;
509 3ce1b845 2019-07-15 stsp }
510 3ce1b845 2019-07-15 stsp
511 3ce1b845 2019-07-15 stsp static const struct got_error *
512 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
513 84792843 2019-08-09 stsp {
514 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
515 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
516 84792843 2019-08-09 stsp
517 84792843 2019-08-09 stsp *author = NULL;
518 aba9c984 2019-09-08 stsp
519 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
524 aba9c984 2019-09-08 stsp return NULL;
525 aba9c984 2019-09-08 stsp }
526 84792843 2019-08-09 stsp
527 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
528 84792843 2019-08-09 stsp if (got_author == NULL) {
529 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
530 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
531 c9956ddf 2019-09-08 stsp if (name && email) {
532 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
533 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
534 c9956ddf 2019-09-08 stsp return NULL;
535 c9956ddf 2019-09-08 stsp }
536 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
537 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
538 84792843 2019-08-09 stsp }
539 84792843 2019-08-09 stsp
540 aba9c984 2019-09-08 stsp *author = strdup(got_author);
541 aba9c984 2019-09-08 stsp if (*author == NULL)
542 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
543 84792843 2019-08-09 stsp
544 84792843 2019-08-09 stsp /*
545 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
546 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
547 84792843 2019-08-09 stsp */
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '<') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 aba9c984 2019-09-08 stsp goto done;
553 aba9c984 2019-09-08 stsp }
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
555 84792843 2019-08-09 stsp got_author++;
556 aba9c984 2019-09-08 stsp if (*got_author != '@') {
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 aba9c984 2019-09-08 stsp goto done;
559 aba9c984 2019-09-08 stsp }
560 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
561 84792843 2019-08-09 stsp got_author++;
562 84792843 2019-08-09 stsp if (*got_author != '>')
563 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
564 aba9c984 2019-09-08 stsp done:
565 aba9c984 2019-09-08 stsp if (err) {
566 aba9c984 2019-09-08 stsp free(*author);
567 aba9c984 2019-09-08 stsp *author = NULL;
568 aba9c984 2019-09-08 stsp }
569 aba9c984 2019-09-08 stsp return err;
570 c9956ddf 2019-09-08 stsp }
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp static const struct got_error *
573 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
574 c9956ddf 2019-09-08 stsp {
575 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
576 c9956ddf 2019-09-08 stsp
577 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
578 c9956ddf 2019-09-08 stsp if (homedir) {
579 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
580 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
581 c9956ddf 2019-09-08 stsp
582 c9956ddf 2019-09-08 stsp }
583 c9956ddf 2019-09-08 stsp return NULL;
584 84792843 2019-08-09 stsp }
585 84792843 2019-08-09 stsp
586 84792843 2019-08-09 stsp static const struct got_error *
587 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
588 3ce1b845 2019-07-15 stsp {
589 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
590 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
591 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
592 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
593 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
594 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
595 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
596 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
597 3ce1b845 2019-07-15 stsp int ch;
598 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
599 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
600 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
601 3ce1b845 2019-07-15 stsp
602 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
603 3ce1b845 2019-07-15 stsp
604 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
605 3ce1b845 2019-07-15 stsp switch (ch) {
606 3ce1b845 2019-07-15 stsp case 'b':
607 3ce1b845 2019-07-15 stsp branch_name = optarg;
608 3ce1b845 2019-07-15 stsp break;
609 3ce1b845 2019-07-15 stsp case 'm':
610 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
611 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
612 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
613 3ce1b845 2019-07-15 stsp goto done;
614 3ce1b845 2019-07-15 stsp }
615 3ce1b845 2019-07-15 stsp break;
616 3ce1b845 2019-07-15 stsp case 'r':
617 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
618 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
619 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
620 9ba1d308 2019-10-21 stsp optarg);
621 3ce1b845 2019-07-15 stsp goto done;
622 3ce1b845 2019-07-15 stsp }
623 3ce1b845 2019-07-15 stsp break;
624 3ce1b845 2019-07-15 stsp case 'I':
625 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
626 3ce1b845 2019-07-15 stsp break;
627 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
628 3ce1b845 2019-07-15 stsp NULL);
629 3ce1b845 2019-07-15 stsp if (error)
630 3ce1b845 2019-07-15 stsp goto done;
631 3ce1b845 2019-07-15 stsp break;
632 3ce1b845 2019-07-15 stsp default:
633 b2b65d18 2019-08-22 stsp usage_import();
634 3ce1b845 2019-07-15 stsp /* NOTREACHED */
635 3ce1b845 2019-07-15 stsp }
636 3ce1b845 2019-07-15 stsp }
637 3ce1b845 2019-07-15 stsp
638 3ce1b845 2019-07-15 stsp argc -= optind;
639 3ce1b845 2019-07-15 stsp argv += optind;
640 3ce1b845 2019-07-15 stsp
641 3ce1b845 2019-07-15 stsp #ifndef PROFILE
642 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
643 aba9c984 2019-09-08 stsp "unveil",
644 3ce1b845 2019-07-15 stsp NULL) == -1)
645 3ce1b845 2019-07-15 stsp err(1, "pledge");
646 3ce1b845 2019-07-15 stsp #endif
647 3ce1b845 2019-07-15 stsp if (argc != 1)
648 3ce1b845 2019-07-15 stsp usage_import();
649 2c7829a4 2019-06-17 stsp
650 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
651 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
652 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
653 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
654 3ce1b845 2019-07-15 stsp }
655 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
656 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
657 c9956ddf 2019-09-08 stsp if (error)
658 c9956ddf 2019-09-08 stsp goto done;
659 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
660 3ce1b845 2019-07-15 stsp if (error)
661 3ce1b845 2019-07-15 stsp goto done;
662 aba9c984 2019-09-08 stsp
663 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
664 aba9c984 2019-09-08 stsp if (error)
665 aba9c984 2019-09-08 stsp return error;
666 e560b7e0 2019-11-28 stsp
667 e560b7e0 2019-11-28 stsp /*
668 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
669 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
670 e560b7e0 2019-11-28 stsp * an unintended typo.
671 e560b7e0 2019-11-28 stsp */
672 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
673 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
674 3ce1b845 2019-07-15 stsp
675 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
676 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
677 3ce1b845 2019-07-15 stsp goto done;
678 3ce1b845 2019-07-15 stsp }
679 3ce1b845 2019-07-15 stsp
680 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
681 3ce1b845 2019-07-15 stsp if (error) {
682 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
683 3ce1b845 2019-07-15 stsp goto done;
684 3ce1b845 2019-07-15 stsp } else {
685 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
686 3ce1b845 2019-07-15 stsp "import target branch already exists");
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp }
689 3ce1b845 2019-07-15 stsp
690 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
691 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
692 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
693 3ce1b845 2019-07-15 stsp goto done;
694 3ce1b845 2019-07-15 stsp }
695 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
696 3ce1b845 2019-07-15 stsp
697 3ce1b845 2019-07-15 stsp /*
698 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
699 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
700 3ce1b845 2019-07-15 stsp */
701 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
702 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
703 3ce1b845 2019-07-15 stsp if (error)
704 3ce1b845 2019-07-15 stsp goto done;
705 8e158b01 2019-09-22 stsp free(logmsg);
706 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
707 ef293bdd 2019-10-21 stsp path_dir, refname);
708 ef293bdd 2019-10-21 stsp if (error) {
709 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
710 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
712 3ce1b845 2019-07-15 stsp goto done;
713 ef293bdd 2019-10-21 stsp }
714 3ce1b845 2019-07-15 stsp }
715 3ce1b845 2019-07-15 stsp
716 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
717 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
718 ef293bdd 2019-10-21 stsp if (logmsg_path)
719 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
720 3ce1b845 2019-07-15 stsp goto done;
721 ef293bdd 2019-10-21 stsp }
722 3ce1b845 2019-07-15 stsp
723 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 ef293bdd 2019-10-21 stsp goto done;
728 ef293bdd 2019-10-21 stsp }
729 ef293bdd 2019-10-21 stsp
730 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
731 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
732 ef293bdd 2019-10-21 stsp if (error) {
733 ef293bdd 2019-10-21 stsp if (logmsg_path)
734 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
735 3ce1b845 2019-07-15 stsp goto done;
736 ef293bdd 2019-10-21 stsp }
737 3ce1b845 2019-07-15 stsp
738 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
739 ef293bdd 2019-10-21 stsp if (error) {
740 ef293bdd 2019-10-21 stsp if (logmsg_path)
741 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
742 3ce1b845 2019-07-15 stsp goto done;
743 ef293bdd 2019-10-21 stsp }
744 3ce1b845 2019-07-15 stsp
745 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
746 ef293bdd 2019-10-21 stsp if (error) {
747 ef293bdd 2019-10-21 stsp if (logmsg_path)
748 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
749 3ce1b845 2019-07-15 stsp goto done;
750 ef293bdd 2019-10-21 stsp }
751 3ce1b845 2019-07-15 stsp
752 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
753 ef293bdd 2019-10-21 stsp if (error) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp goto done;
757 ef293bdd 2019-10-21 stsp }
758 3ce1b845 2019-07-15 stsp
759 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
760 3ce1b845 2019-07-15 stsp if (error) {
761 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
764 3ce1b845 2019-07-15 stsp goto done;
765 ef293bdd 2019-10-21 stsp }
766 3ce1b845 2019-07-15 stsp
767 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
768 3ce1b845 2019-07-15 stsp branch_ref);
769 ef293bdd 2019-10-21 stsp if (error) {
770 ef293bdd 2019-10-21 stsp if (logmsg_path)
771 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
772 3ce1b845 2019-07-15 stsp goto done;
773 ef293bdd 2019-10-21 stsp }
774 3ce1b845 2019-07-15 stsp
775 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
776 ef293bdd 2019-10-21 stsp if (error) {
777 ef293bdd 2019-10-21 stsp if (logmsg_path)
778 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
779 3ce1b845 2019-07-15 stsp goto done;
780 ef293bdd 2019-10-21 stsp }
781 3ce1b845 2019-07-15 stsp }
782 3ce1b845 2019-07-15 stsp
783 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
784 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
785 2c7829a4 2019-06-17 stsp done:
786 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
787 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
788 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
789 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
790 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
791 8e158b01 2019-09-22 stsp free(logmsg);
792 ef293bdd 2019-10-21 stsp free(logmsg_path);
793 2c7829a4 2019-06-17 stsp free(repo_path);
794 3ce1b845 2019-07-15 stsp free(editor);
795 3ce1b845 2019-07-15 stsp free(refname);
796 3ce1b845 2019-07-15 stsp free(new_commit_id);
797 3ce1b845 2019-07-15 stsp free(id_str);
798 aba9c984 2019-09-08 stsp free(author);
799 c9956ddf 2019-09-08 stsp free(gitconfig_path);
800 3ce1b845 2019-07-15 stsp if (branch_ref)
801 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
802 3ce1b845 2019-07-15 stsp if (head_ref)
803 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
804 2c7829a4 2019-06-17 stsp return error;
805 93658fb9 2020-03-18 stsp }
806 93658fb9 2020-03-18 stsp
807 93658fb9 2020-03-18 stsp __dead static void
808 93658fb9 2020-03-18 stsp usage_clone(void)
809 93658fb9 2020-03-18 stsp {
810 93658fb9 2020-03-18 stsp fprintf(stderr, "usage: %s clone repo-url\n", getprogname());
811 93658fb9 2020-03-18 stsp exit(1);
812 0266afb7 2019-01-04 stsp }
813 0266afb7 2019-01-04 stsp
814 4ed7e80c 2018-05-20 stsp __dead static void
815 c09a553d 2018-03-12 stsp usage_checkout(void)
816 c09a553d 2018-03-12 stsp {
817 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
818 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
819 c09a553d 2018-03-12 stsp exit(1);
820 92a684f4 2018-03-12 stsp }
821 92a684f4 2018-03-12 stsp
822 7f47418f 2019-12-20 stsp static void
823 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
824 7f47418f 2019-12-20 stsp {
825 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
826 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
827 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
828 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
829 7f47418f 2019-12-20 stsp getprogname());
830 7f47418f 2019-12-20 stsp }
831 7f47418f 2019-12-20 stsp
832 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
833 7f47418f 2019-12-20 stsp const char *worktree_path;
834 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
835 7f47418f 2019-12-20 stsp };
836 7f47418f 2019-12-20 stsp
837 1ee397ad 2019-07-12 stsp static const struct got_error *
838 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
839 92a684f4 2018-03-12 stsp {
840 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
841 a484d721 2019-06-10 stsp
842 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
843 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
844 7f47418f 2019-12-20 stsp return NULL;
845 7f47418f 2019-12-20 stsp
846 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
847 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
848 1ee397ad 2019-07-12 stsp return NULL;
849 7f47418f 2019-12-20 stsp }
850 92a684f4 2018-03-12 stsp
851 92a684f4 2018-03-12 stsp while (path[0] == '/')
852 92a684f4 2018-03-12 stsp path++;
853 92a684f4 2018-03-12 stsp
854 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
855 1ee397ad 2019-07-12 stsp return NULL;
856 99437157 2018-11-11 stsp }
857 99437157 2018-11-11 stsp
858 99437157 2018-11-11 stsp static const struct got_error *
859 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
860 99437157 2018-11-11 stsp {
861 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
862 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
863 99437157 2018-11-11 stsp return NULL;
864 8069f636 2019-01-12 stsp }
865 8069f636 2019-01-12 stsp
866 8069f636 2019-01-12 stsp static const struct got_error *
867 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
868 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
869 3aef623b 2019-10-15 stsp struct got_repository *repo)
870 8069f636 2019-01-12 stsp {
871 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
872 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
873 8069f636 2019-01-12 stsp
874 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
875 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
876 36a38700 2019-05-10 stsp if (err)
877 36a38700 2019-05-10 stsp return err;
878 8069f636 2019-01-12 stsp
879 d5bea539 2019-05-13 stsp if (yca_id == NULL)
880 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
881 8069f636 2019-01-12 stsp
882 d5bea539 2019-05-13 stsp /*
883 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
884 d5bea539 2019-05-13 stsp * and the work tree's base commit.
885 d5bea539 2019-05-13 stsp *
886 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
887 d5bea539 2019-05-13 stsp *
888 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
889 d5bea539 2019-05-13 stsp * \ /
890 d5bea539 2019-05-13 stsp * C E
891 d5bea539 2019-05-13 stsp * \ /
892 d5bea539 2019-05-13 stsp * B (yca)
893 d5bea539 2019-05-13 stsp * |
894 d5bea539 2019-05-13 stsp * A
895 d5bea539 2019-05-13 stsp *
896 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
897 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
898 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
899 d5bea539 2019-05-13 stsp */
900 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
901 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
902 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
903 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
904 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
905 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
906 8069f636 2019-01-12 stsp
907 d5bea539 2019-05-13 stsp free(yca_id);
908 d5bea539 2019-05-13 stsp return NULL;
909 c09a553d 2018-03-12 stsp }
910 a367ff0f 2019-05-14 stsp
911 a367ff0f 2019-05-14 stsp static const struct got_error *
912 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
913 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
914 a51a74b3 2019-07-27 stsp struct got_repository *repo)
915 a367ff0f 2019-05-14 stsp {
916 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
917 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
918 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
919 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
920 a367ff0f 2019-05-14 stsp
921 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
922 a367ff0f 2019-05-14 stsp if (err)
923 a51a74b3 2019-07-27 stsp goto done;
924 a51a74b3 2019-07-27 stsp
925 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
926 a51a74b3 2019-07-27 stsp is_same_branch = 1;
927 a51a74b3 2019-07-27 stsp goto done;
928 a51a74b3 2019-07-27 stsp }
929 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
930 a51a74b3 2019-07-27 stsp is_same_branch = 1;
931 a367ff0f 2019-05-14 stsp goto done;
932 a51a74b3 2019-07-27 stsp }
933 a367ff0f 2019-05-14 stsp
934 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
935 a367ff0f 2019-05-14 stsp if (err)
936 a367ff0f 2019-05-14 stsp goto done;
937 a367ff0f 2019-05-14 stsp
938 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
939 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
940 a367ff0f 2019-05-14 stsp if (err)
941 a367ff0f 2019-05-14 stsp goto done;
942 a367ff0f 2019-05-14 stsp
943 a367ff0f 2019-05-14 stsp for (;;) {
944 a367ff0f 2019-05-14 stsp struct got_object_id *id;
945 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
946 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
947 a367ff0f 2019-05-14 stsp if (err) {
948 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
949 a367ff0f 2019-05-14 stsp err = NULL;
950 ee780d5c 2020-01-04 stsp break;
951 a367ff0f 2019-05-14 stsp }
952 c09a553d 2018-03-12 stsp
953 a367ff0f 2019-05-14 stsp if (id) {
954 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
955 a51a74b3 2019-07-27 stsp break;
956 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
957 a367ff0f 2019-05-14 stsp is_same_branch = 1;
958 a367ff0f 2019-05-14 stsp break;
959 a367ff0f 2019-05-14 stsp }
960 a367ff0f 2019-05-14 stsp }
961 a367ff0f 2019-05-14 stsp }
962 a367ff0f 2019-05-14 stsp done:
963 a367ff0f 2019-05-14 stsp if (graph)
964 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
965 a367ff0f 2019-05-14 stsp free(head_commit_id);
966 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
967 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
968 a367ff0f 2019-05-14 stsp return err;
969 93658fb9 2020-03-18 stsp }
970 93658fb9 2020-03-18 stsp
971 93658fb9 2020-03-18 stsp static const struct got_error *
972 531c3985 2020-03-18 stsp fetch_progress(void *arg, const char *message)
973 531c3985 2020-03-18 stsp {
974 531c3985 2020-03-18 stsp char *servername = arg;
975 531c3985 2020-03-18 stsp printf("\rserver %s: %s", servername, message);
976 531c3985 2020-03-18 stsp fflush(stdout);
977 531c3985 2020-03-18 stsp return NULL;
978 531c3985 2020-03-18 stsp }
979 531c3985 2020-03-18 stsp
980 531c3985 2020-03-18 stsp static const struct got_error *
981 93658fb9 2020-03-18 stsp cmd_clone(int argc, char *argv[])
982 93658fb9 2020-03-18 stsp {
983 09838ffc 2020-03-18 stsp const struct got_error *err = NULL;
984 09838ffc 2020-03-18 stsp const char *uri, *branch_filter, *dirname;
985 09838ffc 2020-03-18 stsp char *proto, *host, *port, *repo_name, *server_path;
986 d9b4d0c0 2020-03-18 stsp char *default_destdir = NULL, *id_str = NULL;
987 bb64b798 2020-03-18 stsp const char *repo_path;
988 bb64b798 2020-03-18 stsp struct got_repository *repo = NULL;
989 d9b4d0c0 2020-03-18 stsp struct got_pathlist_head refs, symrefs;
990 d9b4d0c0 2020-03-18 stsp struct got_pathlist_entry *pe;
991 d9b4d0c0 2020-03-18 stsp struct got_object_id *pack_hash = NULL;
992 20eb36d0 2020-03-18 stsp int ch, fetchfd = -1;
993 93658fb9 2020-03-18 stsp
994 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&refs);
995 d9b4d0c0 2020-03-18 stsp TAILQ_INIT(&symrefs);
996 d9b4d0c0 2020-03-18 stsp
997 93658fb9 2020-03-18 stsp while ((ch = getopt(argc, argv, "b:")) != -1) {
998 93658fb9 2020-03-18 stsp switch (ch) {
999 93658fb9 2020-03-18 stsp case 'b':
1000 93658fb9 2020-03-18 stsp branch_filter = optarg;
1001 93658fb9 2020-03-18 stsp break;
1002 93658fb9 2020-03-18 stsp default:
1003 93658fb9 2020-03-18 stsp usage_clone();
1004 93658fb9 2020-03-18 stsp break;
1005 93658fb9 2020-03-18 stsp }
1006 93658fb9 2020-03-18 stsp }
1007 93658fb9 2020-03-18 stsp argc -= optind;
1008 93658fb9 2020-03-18 stsp argv += optind;
1009 93658fb9 2020-03-18 stsp uri = argv[0];
1010 93658fb9 2020-03-18 stsp if(argc == 1)
1011 93658fb9 2020-03-18 stsp dirname = NULL;
1012 93658fb9 2020-03-18 stsp else if(argc == 2)
1013 93658fb9 2020-03-18 stsp dirname = argv[1];
1014 93658fb9 2020-03-18 stsp else
1015 93658fb9 2020-03-18 stsp usage_clone();
1016 09838ffc 2020-03-18 stsp
1017 09838ffc 2020-03-18 stsp err = got_fetch_parse_uri(&proto, &host, &port, &server_path,
1018 09838ffc 2020-03-18 stsp &repo_name, argv[0]);
1019 20eb36d0 2020-03-18 stsp if (err)
1020 20eb36d0 2020-03-18 stsp goto done;
1021 20eb36d0 2020-03-18 stsp
1022 20eb36d0 2020-03-18 stsp err = got_fetch_connect(&fetchfd, proto, host, port, server_path);
1023 09838ffc 2020-03-18 stsp if (err)
1024 09838ffc 2020-03-18 stsp goto done;
1025 09838ffc 2020-03-18 stsp
1026 bb64b798 2020-03-18 stsp if (dirname == NULL) {
1027 bb64b798 2020-03-18 stsp if (asprintf(&default_destdir, "%s.git", repo_name) == -1) {
1028 bb64b798 2020-03-18 stsp err = got_error_from_errno("asprintf");
1029 bb64b798 2020-03-18 stsp goto done;
1030 bb64b798 2020-03-18 stsp }
1031 bb64b798 2020-03-18 stsp repo_path = default_destdir;
1032 bb64b798 2020-03-18 stsp } else
1033 bb64b798 2020-03-18 stsp repo_path = dirname;
1034 bb64b798 2020-03-18 stsp
1035 bb64b798 2020-03-18 stsp err = got_path_mkdir(repo_path);
1036 bb64b798 2020-03-18 stsp if (err)
1037 bb64b798 2020-03-18 stsp goto done;
1038 bb64b798 2020-03-18 stsp
1039 bb64b798 2020-03-18 stsp err = got_repo_init(repo_path);
1040 bb64b798 2020-03-18 stsp if (err != NULL)
1041 bb64b798 2020-03-18 stsp goto done;
1042 bb64b798 2020-03-18 stsp
1043 bb64b798 2020-03-18 stsp err = got_repo_open(&repo, repo_path, NULL);
1044 bb64b798 2020-03-18 stsp if (err)
1045 bb64b798 2020-03-18 stsp goto done;
1046 bb64b798 2020-03-18 stsp
1047 07e52fce 2020-03-18 stsp err = got_fetch_pack(&pack_hash, &refs, &symrefs, fetchfd,
1048 531c3985 2020-03-18 stsp repo, fetch_progress, host);
1049 d9b4d0c0 2020-03-18 stsp if (err)
1050 d9b4d0c0 2020-03-18 stsp goto done;
1051 d9b4d0c0 2020-03-18 stsp
1052 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, pack_hash);
1053 d9b4d0c0 2020-03-18 stsp if (err)
1054 d9b4d0c0 2020-03-18 stsp goto done;
1055 d9b4d0c0 2020-03-18 stsp printf("Fetched %s.pack\n", id_str);
1056 d9b4d0c0 2020-03-18 stsp free(id_str);
1057 d9b4d0c0 2020-03-18 stsp
1058 d9b4d0c0 2020-03-18 stsp /* Set up references provided with the pack file. */
1059 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1060 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1061 d9b4d0c0 2020-03-18 stsp struct got_object_id *id = pe->data;
1062 d9b4d0c0 2020-03-18 stsp struct got_reference *ref;
1063 d9b4d0c0 2020-03-18 stsp
1064 d9b4d0c0 2020-03-18 stsp err = got_object_id_str(&id_str, id);
1065 d9b4d0c0 2020-03-18 stsp if (err)
1066 d9b4d0c0 2020-03-18 stsp goto done;
1067 d9b4d0c0 2020-03-18 stsp
1068 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc(&ref, refname, id);
1069 d9b4d0c0 2020-03-18 stsp if (err) {
1070 d9b4d0c0 2020-03-18 stsp free(id_str);
1071 d9b4d0c0 2020-03-18 stsp goto done;
1072 d9b4d0c0 2020-03-18 stsp }
1073 d9b4d0c0 2020-03-18 stsp
1074 d9b4d0c0 2020-03-18 stsp printf("%s: %s\n", got_ref_get_name(ref), id_str);
1075 d9b4d0c0 2020-03-18 stsp free(id_str);
1076 d9b4d0c0 2020-03-18 stsp err = got_ref_write(ref, repo);
1077 d9b4d0c0 2020-03-18 stsp got_ref_close(ref);
1078 d9b4d0c0 2020-03-18 stsp if (err)
1079 d9b4d0c0 2020-03-18 stsp goto done;
1080 d9b4d0c0 2020-03-18 stsp }
1081 d9b4d0c0 2020-03-18 stsp
1082 d9b4d0c0 2020-03-18 stsp /* Set the HEAD reference if the server provided one. */
1083 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1084 d9b4d0c0 2020-03-18 stsp struct got_reference *symref, *target_ref;
1085 d9b4d0c0 2020-03-18 stsp const char *refname = pe->path;
1086 d9b4d0c0 2020-03-18 stsp const char *target = pe->data;
1087 d9b4d0c0 2020-03-18 stsp
1088 d9b4d0c0 2020-03-18 stsp if (strcmp(refname, GOT_REF_HEAD) != 0)
1089 d9b4d0c0 2020-03-18 stsp continue;
1090 d9b4d0c0 2020-03-18 stsp
1091 d9b4d0c0 2020-03-18 stsp err = got_ref_open(&target_ref, repo, target, 0);
1092 d9b4d0c0 2020-03-18 stsp if (err) {
1093 d9b4d0c0 2020-03-18 stsp if (err->code == GOT_ERR_NOT_REF)
1094 d9b4d0c0 2020-03-18 stsp continue;
1095 d9b4d0c0 2020-03-18 stsp goto done;
1096 d9b4d0c0 2020-03-18 stsp }
1097 d9b4d0c0 2020-03-18 stsp
1098 d9b4d0c0 2020-03-18 stsp err = got_ref_alloc_symref(&symref, GOT_REF_HEAD, target_ref);
1099 d9b4d0c0 2020-03-18 stsp got_ref_close(target_ref);
1100 d9b4d0c0 2020-03-18 stsp if (err)
1101 d9b4d0c0 2020-03-18 stsp goto done;
1102 d9b4d0c0 2020-03-18 stsp
1103 d9b4d0c0 2020-03-18 stsp printf("Setting %s to %s\n", GOT_REF_HEAD,
1104 d9b4d0c0 2020-03-18 stsp got_ref_get_symref_target(symref));
1105 d9b4d0c0 2020-03-18 stsp
1106 d9b4d0c0 2020-03-18 stsp err = got_ref_write(symref, repo);
1107 d9b4d0c0 2020-03-18 stsp got_ref_close(symref);
1108 d9b4d0c0 2020-03-18 stsp break;
1109 d9b4d0c0 2020-03-18 stsp }
1110 d9b4d0c0 2020-03-18 stsp
1111 09838ffc 2020-03-18 stsp done:
1112 20eb36d0 2020-03-18 stsp if (fetchfd != -1 && close(fetchfd) == -1 && err == NULL)
1113 20eb36d0 2020-03-18 stsp err = got_error_from_errno("close");
1114 bb64b798 2020-03-18 stsp if (repo)
1115 bb64b798 2020-03-18 stsp got_repo_close(repo);
1116 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &refs, entry) {
1117 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1118 d9b4d0c0 2020-03-18 stsp free(pe->data);
1119 d9b4d0c0 2020-03-18 stsp }
1120 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&refs);
1121 d9b4d0c0 2020-03-18 stsp TAILQ_FOREACH(pe, &symrefs, entry) {
1122 d9b4d0c0 2020-03-18 stsp free((void *)pe->path);
1123 d9b4d0c0 2020-03-18 stsp free(pe->data);
1124 d9b4d0c0 2020-03-18 stsp }
1125 d9b4d0c0 2020-03-18 stsp got_pathlist_free(&symrefs);
1126 d9b4d0c0 2020-03-18 stsp free(pack_hash);
1127 09838ffc 2020-03-18 stsp free(proto);
1128 09838ffc 2020-03-18 stsp free(host);
1129 09838ffc 2020-03-18 stsp free(port);
1130 09838ffc 2020-03-18 stsp free(server_path);
1131 09838ffc 2020-03-18 stsp free(repo_name);
1132 bb64b798 2020-03-18 stsp free(default_destdir);
1133 09838ffc 2020-03-18 stsp return err;
1134 a367ff0f 2019-05-14 stsp }
1135 8069f636 2019-01-12 stsp
1136 4ed7e80c 2018-05-20 stsp static const struct got_error *
1137 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
1138 4b6c9460 2020-03-05 stsp {
1139 4b6c9460 2020-03-05 stsp static char msg[512];
1140 4b6c9460 2020-03-05 stsp const char *branch_name;
1141 4b6c9460 2020-03-05 stsp
1142 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
1143 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
1144 4b6c9460 2020-03-05 stsp else
1145 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
1146 4b6c9460 2020-03-05 stsp
1147 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
1148 4b6c9460 2020-03-05 stsp branch_name += 11;
1149 4b6c9460 2020-03-05 stsp
1150 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
1151 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
1152 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
1153 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
1154 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
1155 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
1156 4b6c9460 2020-03-05 stsp
1157 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
1158 4b6c9460 2020-03-05 stsp }
1159 4b6c9460 2020-03-05 stsp
1160 4b6c9460 2020-03-05 stsp static const struct got_error *
1161 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
1162 c09a553d 2018-03-12 stsp {
1163 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
1164 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
1165 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
1166 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
1167 c09a553d 2018-03-12 stsp char *repo_path = NULL;
1168 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
1169 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
1170 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1171 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1172 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
1173 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1174 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1175 f2ea84fa 2019-07-27 stsp
1176 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1177 c09a553d 2018-03-12 stsp
1178 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1179 0bb8a95e 2018-03-12 stsp switch (ch) {
1180 08573d5b 2019-05-14 stsp case 'b':
1181 08573d5b 2019-05-14 stsp branch_name = optarg;
1182 08573d5b 2019-05-14 stsp break;
1183 8069f636 2019-01-12 stsp case 'c':
1184 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1185 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1186 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1187 bb51a5b4 2020-01-13 stsp break;
1188 bb51a5b4 2020-01-13 stsp case 'E':
1189 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1190 8069f636 2019-01-12 stsp break;
1191 0bb8a95e 2018-03-12 stsp case 'p':
1192 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1193 0bb8a95e 2018-03-12 stsp break;
1194 0bb8a95e 2018-03-12 stsp default:
1195 2deda0b9 2019-03-07 stsp usage_checkout();
1196 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1197 0bb8a95e 2018-03-12 stsp }
1198 0bb8a95e 2018-03-12 stsp }
1199 0bb8a95e 2018-03-12 stsp
1200 0bb8a95e 2018-03-12 stsp argc -= optind;
1201 0bb8a95e 2018-03-12 stsp argv += optind;
1202 0bb8a95e 2018-03-12 stsp
1203 6715a751 2018-03-16 stsp #ifndef PROFILE
1204 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1205 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1206 c09a553d 2018-03-12 stsp err(1, "pledge");
1207 6715a751 2018-03-16 stsp #endif
1208 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1209 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1210 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1211 76089277 2018-04-01 stsp if (repo_path == NULL)
1212 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1213 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1214 76089277 2018-04-01 stsp if (cwd == NULL) {
1215 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1216 76089277 2018-04-01 stsp goto done;
1217 76089277 2018-04-01 stsp }
1218 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1219 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1220 230a42bd 2019-05-11 jcs if (base == NULL) {
1221 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1222 230a42bd 2019-05-11 jcs path_prefix);
1223 230a42bd 2019-05-11 jcs goto done;
1224 230a42bd 2019-05-11 jcs }
1225 230a42bd 2019-05-11 jcs } else {
1226 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1227 230a42bd 2019-05-11 jcs if (base == NULL) {
1228 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1229 230a42bd 2019-05-11 jcs repo_path);
1230 230a42bd 2019-05-11 jcs goto done;
1231 230a42bd 2019-05-11 jcs }
1232 76089277 2018-04-01 stsp }
1233 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1234 c09a553d 2018-03-12 stsp if (dotgit)
1235 c09a553d 2018-03-12 stsp *dotgit = '\0';
1236 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1237 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1238 c09a553d 2018-03-12 stsp free(cwd);
1239 76089277 2018-04-01 stsp goto done;
1240 c09a553d 2018-03-12 stsp }
1241 c09a553d 2018-03-12 stsp free(cwd);
1242 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1243 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1244 76089277 2018-04-01 stsp if (repo_path == NULL) {
1245 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1246 76089277 2018-04-01 stsp goto done;
1247 76089277 2018-04-01 stsp }
1248 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1249 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1250 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1251 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1252 b4b3a7dd 2019-07-22 stsp argv[1]);
1253 b4b3a7dd 2019-07-22 stsp goto done;
1254 b4b3a7dd 2019-07-22 stsp }
1255 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1256 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1257 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1258 b4b3a7dd 2019-07-22 stsp goto done;
1259 b4b3a7dd 2019-07-22 stsp }
1260 76089277 2018-04-01 stsp }
1261 c09a553d 2018-03-12 stsp } else
1262 c09a553d 2018-03-12 stsp usage_checkout();
1263 c09a553d 2018-03-12 stsp
1264 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1265 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1266 13bfb272 2019-05-10 jcs
1267 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1268 c09a553d 2018-03-12 stsp if (error != NULL)
1269 c02c541e 2019-03-29 stsp goto done;
1270 c02c541e 2019-03-29 stsp
1271 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1272 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1273 c530dc23 2019-07-23 stsp if (error) {
1274 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1275 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1276 c530dc23 2019-07-23 stsp goto done;
1277 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1278 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1279 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1280 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1281 c530dc23 2019-07-23 stsp goto done;
1282 c530dc23 2019-07-23 stsp }
1283 c530dc23 2019-07-23 stsp }
1284 c530dc23 2019-07-23 stsp
1285 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1286 c02c541e 2019-03-29 stsp if (error)
1287 c09a553d 2018-03-12 stsp goto done;
1288 8069f636 2019-01-12 stsp
1289 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1290 c09a553d 2018-03-12 stsp if (error != NULL)
1291 c09a553d 2018-03-12 stsp goto done;
1292 c09a553d 2018-03-12 stsp
1293 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1294 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1295 c09a553d 2018-03-12 stsp goto done;
1296 c09a553d 2018-03-12 stsp
1297 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1298 c09a553d 2018-03-12 stsp if (error != NULL)
1299 c09a553d 2018-03-12 stsp goto done;
1300 c09a553d 2018-03-12 stsp
1301 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1302 e5dc7198 2018-12-29 stsp path_prefix);
1303 e5dc7198 2018-12-29 stsp if (error != NULL)
1304 e5dc7198 2018-12-29 stsp goto done;
1305 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1306 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1307 49520a32 2018-12-29 stsp goto done;
1308 49520a32 2018-12-29 stsp }
1309 49520a32 2018-12-29 stsp
1310 8069f636 2019-01-12 stsp if (commit_id_str) {
1311 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1312 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1313 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1314 30837e32 2019-07-25 stsp if (error)
1315 8069f636 2019-01-12 stsp goto done;
1316 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1317 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1318 8069f636 2019-01-12 stsp if (error != NULL) {
1319 8069f636 2019-01-12 stsp free(commit_id);
1320 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1321 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1322 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1323 4b6c9460 2020-03-05 stsp }
1324 8069f636 2019-01-12 stsp goto done;
1325 8069f636 2019-01-12 stsp }
1326 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1327 4b6c9460 2020-03-05 stsp if (error) {
1328 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1329 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1330 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1331 4b6c9460 2020-03-05 stsp }
1332 45d344f6 2019-05-14 stsp goto done;
1333 4b6c9460 2020-03-05 stsp }
1334 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1335 8069f636 2019-01-12 stsp commit_id);
1336 8069f636 2019-01-12 stsp free(commit_id);
1337 8069f636 2019-01-12 stsp if (error)
1338 8069f636 2019-01-12 stsp goto done;
1339 8069f636 2019-01-12 stsp }
1340 8069f636 2019-01-12 stsp
1341 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1342 f2ea84fa 2019-07-27 stsp if (error)
1343 f2ea84fa 2019-07-27 stsp goto done;
1344 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1345 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1346 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1347 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1348 c09a553d 2018-03-12 stsp if (error != NULL)
1349 c09a553d 2018-03-12 stsp goto done;
1350 c09a553d 2018-03-12 stsp
1351 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1352 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1353 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1354 c09a553d 2018-03-12 stsp done:
1355 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1356 8069f636 2019-01-12 stsp free(commit_id_str);
1357 76089277 2018-04-01 stsp free(repo_path);
1358 507dc3bb 2018-12-29 stsp free(worktree_path);
1359 507dc3bb 2018-12-29 stsp return error;
1360 507dc3bb 2018-12-29 stsp }
1361 507dc3bb 2018-12-29 stsp
1362 507dc3bb 2018-12-29 stsp __dead static void
1363 507dc3bb 2018-12-29 stsp usage_update(void)
1364 507dc3bb 2018-12-29 stsp {
1365 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1366 507dc3bb 2018-12-29 stsp getprogname());
1367 507dc3bb 2018-12-29 stsp exit(1);
1368 507dc3bb 2018-12-29 stsp }
1369 507dc3bb 2018-12-29 stsp
1370 1ee397ad 2019-07-12 stsp static const struct got_error *
1371 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1372 507dc3bb 2018-12-29 stsp {
1373 784955db 2019-01-12 stsp int *did_something = arg;
1374 784955db 2019-01-12 stsp
1375 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1376 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1377 1ee397ad 2019-07-12 stsp return NULL;
1378 507dc3bb 2018-12-29 stsp
1379 1545c615 2019-02-10 stsp *did_something = 1;
1380 a484d721 2019-06-10 stsp
1381 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1382 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1383 1ee397ad 2019-07-12 stsp return NULL;
1384 a484d721 2019-06-10 stsp
1385 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1386 507dc3bb 2018-12-29 stsp path++;
1387 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1388 1ee397ad 2019-07-12 stsp return NULL;
1389 be7061eb 2018-12-30 stsp }
1390 be7061eb 2018-12-30 stsp
1391 be7061eb 2018-12-30 stsp static const struct got_error *
1392 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1393 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1394 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1395 a1fb16d8 2019-05-24 stsp {
1396 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1397 a1fb16d8 2019-05-24 stsp char *base_id_str;
1398 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1399 a1fb16d8 2019-05-24 stsp
1400 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1401 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1402 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1403 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1404 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1405 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1406 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1407 a1fb16d8 2019-05-24 stsp }
1408 a1fb16d8 2019-05-24 stsp
1409 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1410 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1411 a1fb16d8 2019-05-24 stsp if (err) {
1412 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1413 a1fb16d8 2019-05-24 stsp return err;
1414 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1415 a1fb16d8 2019-05-24 stsp }
1416 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1417 a1fb16d8 2019-05-24 stsp return NULL;
1418 a1fb16d8 2019-05-24 stsp
1419 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1420 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1421 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1422 a1fb16d8 2019-05-24 stsp if (err)
1423 a1fb16d8 2019-05-24 stsp return err;
1424 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1425 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1426 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1427 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1428 0ebf8283 2019-07-24 stsp return NULL;
1429 0ebf8283 2019-07-24 stsp }
1430 0ebf8283 2019-07-24 stsp
1431 0ebf8283 2019-07-24 stsp static const struct got_error *
1432 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1433 0ebf8283 2019-07-24 stsp {
1434 0ebf8283 2019-07-24 stsp const struct got_error *err;
1435 0ebf8283 2019-07-24 stsp int in_progress;
1436 0ebf8283 2019-07-24 stsp
1437 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1438 0ebf8283 2019-07-24 stsp if (err)
1439 0ebf8283 2019-07-24 stsp return err;
1440 0ebf8283 2019-07-24 stsp if (in_progress)
1441 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1442 0ebf8283 2019-07-24 stsp
1443 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1444 0ebf8283 2019-07-24 stsp if (err)
1445 0ebf8283 2019-07-24 stsp return err;
1446 0ebf8283 2019-07-24 stsp if (in_progress)
1447 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1448 0ebf8283 2019-07-24 stsp
1449 a1fb16d8 2019-05-24 stsp return NULL;
1450 a5edda0a 2019-07-27 stsp }
1451 a5edda0a 2019-07-27 stsp
1452 a5edda0a 2019-07-27 stsp static const struct got_error *
1453 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1454 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1455 a5edda0a 2019-07-27 stsp {
1456 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1457 a5edda0a 2019-07-27 stsp char *path;
1458 a5edda0a 2019-07-27 stsp int i;
1459 a5edda0a 2019-07-27 stsp
1460 a5edda0a 2019-07-27 stsp if (argc == 0) {
1461 a5edda0a 2019-07-27 stsp path = strdup("");
1462 a5edda0a 2019-07-27 stsp if (path == NULL)
1463 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1464 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1465 a5edda0a 2019-07-27 stsp }
1466 a5edda0a 2019-07-27 stsp
1467 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1468 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1469 a5edda0a 2019-07-27 stsp if (err)
1470 a5edda0a 2019-07-27 stsp break;
1471 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1472 a5edda0a 2019-07-27 stsp if (err) {
1473 a5edda0a 2019-07-27 stsp free(path);
1474 a5edda0a 2019-07-27 stsp break;
1475 a5edda0a 2019-07-27 stsp }
1476 a5edda0a 2019-07-27 stsp }
1477 a5edda0a 2019-07-27 stsp
1478 a5edda0a 2019-07-27 stsp return err;
1479 a1fb16d8 2019-05-24 stsp }
1480 a1fb16d8 2019-05-24 stsp
1481 a1fb16d8 2019-05-24 stsp static const struct got_error *
1482 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1483 507dc3bb 2018-12-29 stsp {
1484 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1485 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1486 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1487 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1488 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1489 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1490 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1491 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1492 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1493 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1494 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1495 507dc3bb 2018-12-29 stsp
1496 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1497 f2ea84fa 2019-07-27 stsp
1498 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1499 507dc3bb 2018-12-29 stsp switch (ch) {
1500 024e9686 2019-05-14 stsp case 'b':
1501 024e9686 2019-05-14 stsp branch_name = optarg;
1502 024e9686 2019-05-14 stsp break;
1503 507dc3bb 2018-12-29 stsp case 'c':
1504 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1505 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1506 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1507 507dc3bb 2018-12-29 stsp break;
1508 507dc3bb 2018-12-29 stsp default:
1509 2deda0b9 2019-03-07 stsp usage_update();
1510 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1511 507dc3bb 2018-12-29 stsp }
1512 507dc3bb 2018-12-29 stsp }
1513 507dc3bb 2018-12-29 stsp
1514 507dc3bb 2018-12-29 stsp argc -= optind;
1515 507dc3bb 2018-12-29 stsp argv += optind;
1516 507dc3bb 2018-12-29 stsp
1517 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1518 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1519 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1520 507dc3bb 2018-12-29 stsp err(1, "pledge");
1521 507dc3bb 2018-12-29 stsp #endif
1522 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1523 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1524 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1525 c4cdcb68 2019-04-03 stsp goto done;
1526 c4cdcb68 2019-04-03 stsp }
1527 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1528 7d5807f4 2019-07-11 stsp if (error)
1529 7d5807f4 2019-07-11 stsp goto done;
1530 7d5807f4 2019-07-11 stsp
1531 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1532 f2ea84fa 2019-07-27 stsp if (error)
1533 f2ea84fa 2019-07-27 stsp goto done;
1534 507dc3bb 2018-12-29 stsp
1535 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1536 c9956ddf 2019-09-08 stsp NULL);
1537 507dc3bb 2018-12-29 stsp if (error != NULL)
1538 507dc3bb 2018-12-29 stsp goto done;
1539 507dc3bb 2018-12-29 stsp
1540 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1541 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1542 087fb88c 2019-08-04 stsp if (error)
1543 087fb88c 2019-08-04 stsp goto done;
1544 087fb88c 2019-08-04 stsp
1545 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1546 0266afb7 2019-01-04 stsp if (error)
1547 0266afb7 2019-01-04 stsp goto done;
1548 0266afb7 2019-01-04 stsp
1549 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1550 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1551 024e9686 2019-05-14 stsp if (error != NULL)
1552 024e9686 2019-05-14 stsp goto done;
1553 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1554 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1555 9c4b8182 2019-01-02 stsp if (error != NULL)
1556 9c4b8182 2019-01-02 stsp goto done;
1557 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1558 507dc3bb 2018-12-29 stsp if (error != NULL)
1559 507dc3bb 2018-12-29 stsp goto done;
1560 507dc3bb 2018-12-29 stsp } else {
1561 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1562 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1563 04f57cb3 2019-07-25 stsp free(commit_id_str);
1564 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1565 30837e32 2019-07-25 stsp if (error)
1566 a297e751 2019-07-11 stsp goto done;
1567 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1568 a297e751 2019-07-11 stsp if (error)
1569 507dc3bb 2018-12-29 stsp goto done;
1570 507dc3bb 2018-12-29 stsp }
1571 35c965b2 2018-12-29 stsp
1572 a1fb16d8 2019-05-24 stsp if (branch_name) {
1573 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1574 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1575 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1576 f2ea84fa 2019-07-27 stsp continue;
1577 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1578 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1579 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1580 024e9686 2019-05-14 stsp goto done;
1581 024e9686 2019-05-14 stsp }
1582 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1583 024e9686 2019-05-14 stsp if (error)
1584 024e9686 2019-05-14 stsp goto done;
1585 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1586 3aef623b 2019-10-15 stsp repo);
1587 a367ff0f 2019-05-14 stsp free(head_commit_id);
1588 024e9686 2019-05-14 stsp if (error != NULL)
1589 024e9686 2019-05-14 stsp goto done;
1590 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1591 a367ff0f 2019-05-14 stsp if (error)
1592 a367ff0f 2019-05-14 stsp goto done;
1593 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1594 024e9686 2019-05-14 stsp if (error)
1595 024e9686 2019-05-14 stsp goto done;
1596 024e9686 2019-05-14 stsp } else {
1597 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1598 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1599 a367ff0f 2019-05-14 stsp if (error != NULL) {
1600 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1601 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1602 024e9686 2019-05-14 stsp goto done;
1603 a367ff0f 2019-05-14 stsp }
1604 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1605 a367ff0f 2019-05-14 stsp if (error)
1606 a367ff0f 2019-05-14 stsp goto done;
1607 024e9686 2019-05-14 stsp }
1608 507dc3bb 2018-12-29 stsp
1609 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1610 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1611 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1612 507dc3bb 2018-12-29 stsp commit_id);
1613 507dc3bb 2018-12-29 stsp if (error)
1614 507dc3bb 2018-12-29 stsp goto done;
1615 507dc3bb 2018-12-29 stsp }
1616 507dc3bb 2018-12-29 stsp
1617 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1618 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1619 507dc3bb 2018-12-29 stsp if (error != NULL)
1620 507dc3bb 2018-12-29 stsp goto done;
1621 9c4b8182 2019-01-02 stsp
1622 784955db 2019-01-12 stsp if (did_something)
1623 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1624 784955db 2019-01-12 stsp else
1625 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1626 507dc3bb 2018-12-29 stsp done:
1627 c09a553d 2018-03-12 stsp free(worktree_path);
1628 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1629 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1630 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1631 507dc3bb 2018-12-29 stsp free(commit_id);
1632 9c4b8182 2019-01-02 stsp free(commit_id_str);
1633 c09a553d 2018-03-12 stsp return error;
1634 c09a553d 2018-03-12 stsp }
1635 c09a553d 2018-03-12 stsp
1636 f42b1b34 2018-03-12 stsp static const struct got_error *
1637 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1638 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1639 63035f9f 2019-10-06 stsp struct got_repository *repo)
1640 5c860e29 2018-03-12 stsp {
1641 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1642 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1643 79109fed 2018-03-27 stsp
1644 44392932 2019-08-25 stsp if (blob_id1) {
1645 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1646 44392932 2019-08-25 stsp if (err)
1647 44392932 2019-08-25 stsp goto done;
1648 44392932 2019-08-25 stsp }
1649 79109fed 2018-03-27 stsp
1650 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1651 44392932 2019-08-25 stsp if (err)
1652 44392932 2019-08-25 stsp goto done;
1653 79109fed 2018-03-27 stsp
1654 44392932 2019-08-25 stsp while (path[0] == '/')
1655 44392932 2019-08-25 stsp path++;
1656 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1657 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1658 44392932 2019-08-25 stsp done:
1659 44392932 2019-08-25 stsp if (blob1)
1660 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1661 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1662 44392932 2019-08-25 stsp return err;
1663 44392932 2019-08-25 stsp }
1664 44392932 2019-08-25 stsp
1665 44392932 2019-08-25 stsp static const struct got_error *
1666 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1667 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1668 63035f9f 2019-10-06 stsp struct got_repository *repo)
1669 44392932 2019-08-25 stsp {
1670 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1671 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1672 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1673 44392932 2019-08-25 stsp
1674 44392932 2019-08-25 stsp if (tree_id1) {
1675 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1676 79109fed 2018-03-27 stsp if (err)
1677 44392932 2019-08-25 stsp goto done;
1678 79109fed 2018-03-27 stsp }
1679 79109fed 2018-03-27 stsp
1680 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1681 0f2b3dca 2018-12-22 stsp if (err)
1682 0f2b3dca 2018-12-22 stsp goto done;
1683 0f2b3dca 2018-12-22 stsp
1684 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1685 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1686 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1687 44392932 2019-08-25 stsp while (path[0] == '/')
1688 44392932 2019-08-25 stsp path++;
1689 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1690 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1691 0f2b3dca 2018-12-22 stsp done:
1692 79109fed 2018-03-27 stsp if (tree1)
1693 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1694 366e0a5f 2019-10-10 stsp if (tree2)
1695 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1696 44392932 2019-08-25 stsp return err;
1697 44392932 2019-08-25 stsp }
1698 44392932 2019-08-25 stsp
1699 44392932 2019-08-25 stsp static const struct got_error *
1700 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1701 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1702 44392932 2019-08-25 stsp {
1703 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1704 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1705 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1706 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1707 44392932 2019-08-25 stsp struct got_object_qid *qid;
1708 44392932 2019-08-25 stsp
1709 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1710 44392932 2019-08-25 stsp if (qid != NULL) {
1711 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1712 44392932 2019-08-25 stsp qid->id);
1713 44392932 2019-08-25 stsp if (err)
1714 44392932 2019-08-25 stsp return err;
1715 44392932 2019-08-25 stsp }
1716 44392932 2019-08-25 stsp
1717 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1718 44392932 2019-08-25 stsp int obj_type;
1719 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1720 44392932 2019-08-25 stsp if (err)
1721 44392932 2019-08-25 stsp goto done;
1722 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1723 44392932 2019-08-25 stsp if (err) {
1724 44392932 2019-08-25 stsp free(obj_id2);
1725 44392932 2019-08-25 stsp goto done;
1726 44392932 2019-08-25 stsp }
1727 44392932 2019-08-25 stsp if (pcommit) {
1728 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1729 44392932 2019-08-25 stsp qid->id, path);
1730 44392932 2019-08-25 stsp if (err) {
1731 44392932 2019-08-25 stsp free(obj_id2);
1732 44392932 2019-08-25 stsp goto done;
1733 44392932 2019-08-25 stsp }
1734 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1735 44392932 2019-08-25 stsp if (err) {
1736 44392932 2019-08-25 stsp free(obj_id2);
1737 44392932 2019-08-25 stsp goto done;
1738 44392932 2019-08-25 stsp }
1739 44392932 2019-08-25 stsp }
1740 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1741 44392932 2019-08-25 stsp if (err) {
1742 44392932 2019-08-25 stsp free(obj_id2);
1743 44392932 2019-08-25 stsp goto done;
1744 44392932 2019-08-25 stsp }
1745 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1746 44392932 2019-08-25 stsp switch (obj_type) {
1747 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1748 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1749 63035f9f 2019-10-06 stsp 0, repo);
1750 44392932 2019-08-25 stsp break;
1751 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1752 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1753 63035f9f 2019-10-06 stsp 0, repo);
1754 44392932 2019-08-25 stsp break;
1755 44392932 2019-08-25 stsp default:
1756 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1757 44392932 2019-08-25 stsp break;
1758 44392932 2019-08-25 stsp }
1759 44392932 2019-08-25 stsp free(obj_id1);
1760 44392932 2019-08-25 stsp free(obj_id2);
1761 44392932 2019-08-25 stsp } else {
1762 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1763 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1764 44392932 2019-08-25 stsp if (err)
1765 44392932 2019-08-25 stsp goto done;
1766 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1767 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1768 44392932 2019-08-25 stsp if (err)
1769 44392932 2019-08-25 stsp goto done;
1770 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1771 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1772 44392932 2019-08-25 stsp }
1773 44392932 2019-08-25 stsp done:
1774 0f2b3dca 2018-12-22 stsp free(id_str1);
1775 0f2b3dca 2018-12-22 stsp free(id_str2);
1776 44392932 2019-08-25 stsp if (pcommit)
1777 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1778 79109fed 2018-03-27 stsp return err;
1779 79109fed 2018-03-27 stsp }
1780 79109fed 2018-03-27 stsp
1781 4bb494d5 2018-06-16 stsp static char *
1782 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1783 6c281f94 2018-06-11 stsp {
1784 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1785 09867e48 2019-08-13 stsp char *p, *s;
1786 09867e48 2019-08-13 stsp
1787 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1788 09867e48 2019-08-13 stsp if (tm == NULL)
1789 09867e48 2019-08-13 stsp return NULL;
1790 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1791 09867e48 2019-08-13 stsp if (s == NULL)
1792 09867e48 2019-08-13 stsp return NULL;
1793 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1794 6c281f94 2018-06-11 stsp if (p)
1795 6c281f94 2018-06-11 stsp *p = '\0';
1796 6c281f94 2018-06-11 stsp return s;
1797 6c281f94 2018-06-11 stsp }
1798 dc424a06 2019-08-07 stsp
1799 6841bf13 2019-11-29 kn static const struct got_error *
1800 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1801 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1802 6841bf13 2019-11-29 kn {
1803 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1804 6841bf13 2019-11-29 kn regmatch_t regmatch;
1805 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1806 6841bf13 2019-11-29 kn
1807 6841bf13 2019-11-29 kn *have_match = 0;
1808 6841bf13 2019-11-29 kn
1809 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1810 6841bf13 2019-11-29 kn if (err)
1811 6841bf13 2019-11-29 kn return err;
1812 6841bf13 2019-11-29 kn
1813 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1814 6841bf13 2019-11-29 kn if (err)
1815 6841bf13 2019-11-29 kn goto done;
1816 6841bf13 2019-11-29 kn
1817 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1818 6841bf13 2019-11-29 kn *have_match = 1;
1819 6841bf13 2019-11-29 kn done:
1820 6841bf13 2019-11-29 kn free(id_str);
1821 6841bf13 2019-11-29 kn free(logmsg);
1822 6841bf13 2019-11-29 kn return err;
1823 6841bf13 2019-11-29 kn }
1824 6841bf13 2019-11-29 kn
1825 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1826 6c281f94 2018-06-11 stsp
1827 79109fed 2018-03-27 stsp static const struct got_error *
1828 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1829 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1830 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1831 79109fed 2018-03-27 stsp {
1832 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1833 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1834 6c281f94 2018-06-11 stsp char datebuf[26];
1835 45d799e2 2018-12-23 stsp time_t committer_time;
1836 45d799e2 2018-12-23 stsp const char *author, *committer;
1837 199a4027 2019-02-02 stsp char *refs_str = NULL;
1838 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1839 5c860e29 2018-03-12 stsp
1840 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1841 199a4027 2019-02-02 stsp char *s;
1842 199a4027 2019-02-02 stsp const char *name;
1843 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1844 a436ad14 2019-08-13 stsp int cmp;
1845 a436ad14 2019-08-13 stsp
1846 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1847 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1848 d9498b20 2019-02-05 stsp continue;
1849 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1850 199a4027 2019-02-02 stsp name += 5;
1851 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1852 7143d404 2019-03-12 stsp continue;
1853 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1854 e34f9ed6 2019-02-02 stsp name += 6;
1855 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1856 141c2bff 2019-02-04 stsp name += 8;
1857 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1858 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1859 5d844a1e 2019-08-13 stsp if (err) {
1860 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1861 5d844a1e 2019-08-13 stsp return err;
1862 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1863 5d844a1e 2019-08-13 stsp err = NULL;
1864 5d844a1e 2019-08-13 stsp tag = NULL;
1865 5d844a1e 2019-08-13 stsp }
1866 a436ad14 2019-08-13 stsp }
1867 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1868 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1869 a436ad14 2019-08-13 stsp if (tag)
1870 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1871 a436ad14 2019-08-13 stsp if (cmp != 0)
1872 a436ad14 2019-08-13 stsp continue;
1873 199a4027 2019-02-02 stsp s = refs_str;
1874 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1875 199a4027 2019-02-02 stsp name) == -1) {
1876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1877 199a4027 2019-02-02 stsp free(s);
1878 34ca4898 2019-08-13 stsp return err;
1879 199a4027 2019-02-02 stsp }
1880 199a4027 2019-02-02 stsp free(s);
1881 199a4027 2019-02-02 stsp }
1882 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1883 8bf5b3c9 2018-03-17 stsp if (err)
1884 8bf5b3c9 2018-03-17 stsp return err;
1885 788c352e 2018-06-16 stsp
1886 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1887 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1888 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1889 832c249c 2018-06-10 stsp free(id_str);
1890 d3d493d7 2019-02-21 stsp id_str = NULL;
1891 d3d493d7 2019-02-21 stsp free(refs_str);
1892 d3d493d7 2019-02-21 stsp refs_str = NULL;
1893 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1894 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1895 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1896 09867e48 2019-08-13 stsp if (datestr)
1897 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1898 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1899 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1900 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1901 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1902 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1903 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1904 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1905 3fe1abad 2018-06-10 stsp int n = 1;
1906 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1907 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1908 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1909 a0603db2 2018-06-10 stsp if (err)
1910 a0603db2 2018-06-10 stsp return err;
1911 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1912 a0603db2 2018-06-10 stsp free(id_str);
1913 a0603db2 2018-06-10 stsp }
1914 a0603db2 2018-06-10 stsp }
1915 832c249c 2018-06-10 stsp
1916 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1917 5943eee2 2019-08-13 stsp if (err)
1918 5943eee2 2019-08-13 stsp return err;
1919 8bf5b3c9 2018-03-17 stsp
1920 621015ac 2018-07-23 stsp logmsg = logmsg0;
1921 832c249c 2018-06-10 stsp do {
1922 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1923 832c249c 2018-06-10 stsp if (line)
1924 832c249c 2018-06-10 stsp printf(" %s\n", line);
1925 832c249c 2018-06-10 stsp } while (line);
1926 621015ac 2018-07-23 stsp free(logmsg0);
1927 832c249c 2018-06-10 stsp
1928 971751ac 2018-03-27 stsp if (show_patch) {
1929 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1930 971751ac 2018-03-27 stsp if (err == 0)
1931 971751ac 2018-03-27 stsp printf("\n");
1932 971751ac 2018-03-27 stsp }
1933 07862c20 2018-09-15 stsp
1934 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1935 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1936 07862c20 2018-09-15 stsp return err;
1937 f42b1b34 2018-03-12 stsp }
1938 5c860e29 2018-03-12 stsp
1939 f42b1b34 2018-03-12 stsp static const struct got_error *
1940 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1941 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1942 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1943 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1944 f42b1b34 2018-03-12 stsp {
1945 f42b1b34 2018-03-12 stsp const struct got_error *err;
1946 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1947 6841bf13 2019-11-29 kn regex_t regex;
1948 6841bf13 2019-11-29 kn int have_match;
1949 372ccdbb 2018-06-10 stsp
1950 6841bf13 2019-11-29 kn if (search_pattern &&
1951 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1952 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1953 6841bf13 2019-11-29 kn
1954 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1955 f42b1b34 2018-03-12 stsp if (err)
1956 f42b1b34 2018-03-12 stsp return err;
1957 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1958 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1959 372ccdbb 2018-06-10 stsp if (err)
1960 fcc85cad 2018-07-22 stsp goto done;
1961 656b1f76 2019-05-11 jcs for (;;) {
1962 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1963 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1964 8bf5b3c9 2018-03-17 stsp
1965 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1966 84453469 2018-11-11 stsp break;
1967 84453469 2018-11-11 stsp
1968 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1969 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1970 372ccdbb 2018-06-10 stsp if (err) {
1971 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1972 9ba79e04 2018-06-11 stsp err = NULL;
1973 ee780d5c 2020-01-04 stsp break;
1974 7e665116 2018-04-02 stsp }
1975 b43fbaa0 2018-06-11 stsp if (id == NULL)
1976 7e665116 2018-04-02 stsp break;
1977 b43fbaa0 2018-06-11 stsp
1978 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1979 b43fbaa0 2018-06-11 stsp if (err)
1980 fcc85cad 2018-07-22 stsp break;
1981 6841bf13 2019-11-29 kn
1982 6841bf13 2019-11-29 kn if (search_pattern) {
1983 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1984 6841bf13 2019-11-29 kn if (err) {
1985 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1986 6841bf13 2019-11-29 kn break;
1987 6841bf13 2019-11-29 kn }
1988 6841bf13 2019-11-29 kn if (have_match == 0) {
1989 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1990 6841bf13 2019-11-29 kn continue;
1991 6841bf13 2019-11-29 kn }
1992 6841bf13 2019-11-29 kn }
1993 6841bf13 2019-11-29 kn
1994 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1995 44392932 2019-08-25 stsp diff_context, refs);
1996 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1997 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1998 7e665116 2018-04-02 stsp break;
1999 31cedeaf 2018-09-15 stsp }
2000 fcc85cad 2018-07-22 stsp done:
2001 6841bf13 2019-11-29 kn if (search_pattern)
2002 6841bf13 2019-11-29 kn regfree(&regex);
2003 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
2004 f42b1b34 2018-03-12 stsp return err;
2005 f42b1b34 2018-03-12 stsp }
2006 5c860e29 2018-03-12 stsp
2007 4ed7e80c 2018-05-20 stsp __dead static void
2008 6f3d1eb0 2018-03-12 stsp usage_log(void)
2009 6f3d1eb0 2018-03-12 stsp {
2010 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
2011 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
2012 6f3d1eb0 2018-03-12 stsp exit(1);
2013 b1ebc001 2019-08-13 stsp }
2014 b1ebc001 2019-08-13 stsp
2015 b1ebc001 2019-08-13 stsp static int
2016 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
2017 b1ebc001 2019-08-13 stsp {
2018 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
2019 b1ebc001 2019-08-13 stsp long long n;
2020 b1ebc001 2019-08-13 stsp const char *errstr;
2021 b1ebc001 2019-08-13 stsp
2022 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
2023 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
2024 b1ebc001 2019-08-13 stsp return 0;
2025 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
2026 b1ebc001 2019-08-13 stsp if (errstr != NULL)
2027 b1ebc001 2019-08-13 stsp return 0;
2028 b1ebc001 2019-08-13 stsp return n;
2029 6f3d1eb0 2018-03-12 stsp }
2030 6f3d1eb0 2018-03-12 stsp
2031 4ed7e80c 2018-05-20 stsp static const struct got_error *
2032 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
2033 f42b1b34 2018-03-12 stsp {
2034 f42b1b34 2018-03-12 stsp const struct got_error *error;
2035 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
2036 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
2037 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
2038 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
2039 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
2040 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
2041 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
2042 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
2043 64a96a6d 2018-04-01 stsp const char *errstr;
2044 199a4027 2019-02-02 stsp struct got_reflist_head refs;
2045 1b3893a2 2019-03-18 stsp
2046 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
2047 5c860e29 2018-03-12 stsp
2048 6715a751 2018-03-16 stsp #ifndef PROFILE
2049 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2050 6098196c 2019-01-04 stsp NULL)
2051 ad242220 2018-09-08 stsp == -1)
2052 f42b1b34 2018-03-12 stsp err(1, "pledge");
2053 6715a751 2018-03-16 stsp #endif
2054 79109fed 2018-03-27 stsp
2055 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
2056 b1ebc001 2019-08-13 stsp
2057 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
2058 79109fed 2018-03-27 stsp switch (ch) {
2059 79109fed 2018-03-27 stsp case 'p':
2060 79109fed 2018-03-27 stsp show_patch = 1;
2061 d142fc45 2018-04-01 stsp break;
2062 d142fc45 2018-04-01 stsp case 'c':
2063 d142fc45 2018-04-01 stsp start_commit = optarg;
2064 64a96a6d 2018-04-01 stsp break;
2065 c0cc5c62 2018-10-18 stsp case 'C':
2066 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2067 4a8520aa 2018-10-18 stsp &errstr);
2068 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2069 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2070 c0cc5c62 2018-10-18 stsp break;
2071 64a96a6d 2018-04-01 stsp case 'l':
2072 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
2073 64a96a6d 2018-04-01 stsp if (errstr != NULL)
2074 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
2075 cc54c501 2019-07-15 stsp break;
2076 48c8c60d 2020-01-27 stsp case 'b':
2077 48c8c60d 2020-01-27 stsp log_branches = 1;
2078 a0603db2 2018-06-10 stsp break;
2079 04ca23f4 2018-07-16 stsp case 'r':
2080 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
2081 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
2082 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2083 9ba1d308 2019-10-21 stsp optarg);
2084 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2085 04ca23f4 2018-07-16 stsp break;
2086 6841bf13 2019-11-29 kn case 's':
2087 6841bf13 2019-11-29 kn search_pattern = optarg;
2088 6841bf13 2019-11-29 kn break;
2089 79109fed 2018-03-27 stsp default:
2090 2deda0b9 2019-03-07 stsp usage_log();
2091 79109fed 2018-03-27 stsp /* NOTREACHED */
2092 79109fed 2018-03-27 stsp }
2093 79109fed 2018-03-27 stsp }
2094 79109fed 2018-03-27 stsp
2095 79109fed 2018-03-27 stsp argc -= optind;
2096 79109fed 2018-03-27 stsp argv += optind;
2097 f42b1b34 2018-03-12 stsp
2098 dc1edbfa 2019-11-29 kn if (diff_context == -1)
2099 dc1edbfa 2019-11-29 kn diff_context = 3;
2100 dc1edbfa 2019-11-29 kn else if (!show_patch)
2101 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
2102 dc1edbfa 2019-11-29 kn
2103 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
2104 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
2105 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2106 04ca23f4 2018-07-16 stsp goto done;
2107 04ca23f4 2018-07-16 stsp }
2108 cffc0aa4 2019-02-05 stsp
2109 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2110 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2111 cffc0aa4 2019-02-05 stsp goto done;
2112 cffc0aa4 2019-02-05 stsp error = NULL;
2113 cffc0aa4 2019-02-05 stsp
2114 e7301579 2019-03-18 stsp if (argc == 0) {
2115 cbd1af7a 2019-03-18 stsp path = strdup("");
2116 e7301579 2019-03-18 stsp if (path == NULL) {
2117 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2118 cbd1af7a 2019-03-18 stsp goto done;
2119 e7301579 2019-03-18 stsp }
2120 e7301579 2019-03-18 stsp } else if (argc == 1) {
2121 e7301579 2019-03-18 stsp if (worktree) {
2122 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2123 e7301579 2019-03-18 stsp argv[0]);
2124 e7301579 2019-03-18 stsp if (error)
2125 e7301579 2019-03-18 stsp goto done;
2126 e7301579 2019-03-18 stsp } else {
2127 e7301579 2019-03-18 stsp path = strdup(argv[0]);
2128 e7301579 2019-03-18 stsp if (path == NULL) {
2129 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2130 e7301579 2019-03-18 stsp goto done;
2131 e7301579 2019-03-18 stsp }
2132 e7301579 2019-03-18 stsp }
2133 cbd1af7a 2019-03-18 stsp } else
2134 cbd1af7a 2019-03-18 stsp usage_log();
2135 cbd1af7a 2019-03-18 stsp
2136 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
2137 5486daa2 2019-05-11 stsp repo_path = worktree ?
2138 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
2139 5486daa2 2019-05-11 stsp }
2140 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
2141 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2142 cffc0aa4 2019-02-05 stsp goto done;
2143 04ca23f4 2018-07-16 stsp }
2144 6098196c 2019-01-04 stsp
2145 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2146 d7d4f210 2018-03-12 stsp if (error != NULL)
2147 04ca23f4 2018-07-16 stsp goto done;
2148 f42b1b34 2018-03-12 stsp
2149 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2150 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2151 c02c541e 2019-03-29 stsp if (error)
2152 c02c541e 2019-03-29 stsp goto done;
2153 c02c541e 2019-03-29 stsp
2154 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
2155 3235492e 2018-04-01 stsp struct got_reference *head_ref;
2156 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
2157 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
2158 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
2159 3235492e 2018-04-01 stsp if (error != NULL)
2160 3235492e 2018-04-01 stsp return error;
2161 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
2162 3235492e 2018-04-01 stsp got_ref_close(head_ref);
2163 3235492e 2018-04-01 stsp if (error != NULL)
2164 3235492e 2018-04-01 stsp return error;
2165 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2166 3235492e 2018-04-01 stsp } else {
2167 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
2168 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
2169 3235492e 2018-04-01 stsp if (error == NULL) {
2170 1caad61b 2019-02-01 stsp int obj_type;
2171 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
2172 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
2173 d1f2edc9 2018-06-13 stsp if (error != NULL)
2174 1caad61b 2019-02-01 stsp goto done;
2175 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
2176 1caad61b 2019-02-01 stsp if (error != NULL)
2177 1caad61b 2019-02-01 stsp goto done;
2178 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2179 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2180 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2181 1caad61b 2019-02-01 stsp if (error != NULL)
2182 1caad61b 2019-02-01 stsp goto done;
2183 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2184 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2185 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2186 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2187 1caad61b 2019-02-01 stsp goto done;
2188 1caad61b 2019-02-01 stsp }
2189 1caad61b 2019-02-01 stsp free(id);
2190 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2191 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2192 1caad61b 2019-02-01 stsp if (id == NULL)
2193 638f9024 2019-05-13 stsp error = got_error_from_errno(
2194 230a42bd 2019-05-11 jcs "got_object_id_dup");
2195 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2196 1caad61b 2019-02-01 stsp if (error)
2197 1caad61b 2019-02-01 stsp goto done;
2198 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2199 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2200 1caad61b 2019-02-01 stsp goto done;
2201 1caad61b 2019-02-01 stsp }
2202 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2203 d1f2edc9 2018-06-13 stsp if (error != NULL)
2204 1caad61b 2019-02-01 stsp goto done;
2205 d1f2edc9 2018-06-13 stsp }
2206 15a94983 2018-12-23 stsp if (commit == NULL) {
2207 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2208 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2209 d1f2edc9 2018-06-13 stsp if (error != NULL)
2210 d1f2edc9 2018-06-13 stsp return error;
2211 3235492e 2018-04-01 stsp }
2212 3235492e 2018-04-01 stsp }
2213 d7d4f210 2018-03-12 stsp if (error != NULL)
2214 04ca23f4 2018-07-16 stsp goto done;
2215 04ca23f4 2018-07-16 stsp
2216 deeabeae 2019-08-27 stsp if (worktree) {
2217 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2218 deeabeae 2019-08-27 stsp char *p;
2219 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2220 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2221 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2222 deeabeae 2019-08-27 stsp goto done;
2223 deeabeae 2019-08-27 stsp }
2224 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2225 deeabeae 2019-08-27 stsp free(p);
2226 deeabeae 2019-08-27 stsp } else
2227 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2228 04ca23f4 2018-07-16 stsp if (error != NULL)
2229 04ca23f4 2018-07-16 stsp goto done;
2230 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2231 04ca23f4 2018-07-16 stsp free(path);
2232 04ca23f4 2018-07-16 stsp path = in_repo_path;
2233 04ca23f4 2018-07-16 stsp }
2234 199a4027 2019-02-02 stsp
2235 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2236 199a4027 2019-02-02 stsp if (error)
2237 199a4027 2019-02-02 stsp goto done;
2238 04ca23f4 2018-07-16 stsp
2239 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2240 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2241 04ca23f4 2018-07-16 stsp done:
2242 04ca23f4 2018-07-16 stsp free(path);
2243 04ca23f4 2018-07-16 stsp free(repo_path);
2244 04ca23f4 2018-07-16 stsp free(cwd);
2245 f42b1b34 2018-03-12 stsp free(id);
2246 cffc0aa4 2019-02-05 stsp if (worktree)
2247 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2248 ad242220 2018-09-08 stsp if (repo) {
2249 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2250 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2251 ad242220 2018-09-08 stsp if (error == NULL)
2252 ad242220 2018-09-08 stsp error = repo_error;
2253 ad242220 2018-09-08 stsp }
2254 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2255 8bf5b3c9 2018-03-17 stsp return error;
2256 5c860e29 2018-03-12 stsp }
2257 5c860e29 2018-03-12 stsp
2258 4ed7e80c 2018-05-20 stsp __dead static void
2259 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2260 3f8b7d6a 2018-04-01 stsp {
2261 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2262 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2263 3f8b7d6a 2018-04-01 stsp exit(1);
2264 b00d56cd 2018-04-01 stsp }
2265 b00d56cd 2018-04-01 stsp
2266 b72f483a 2019-02-05 stsp struct print_diff_arg {
2267 b72f483a 2019-02-05 stsp struct got_repository *repo;
2268 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2269 b72f483a 2019-02-05 stsp int diff_context;
2270 3fc0c068 2019-02-10 stsp const char *id_str;
2271 3fc0c068 2019-02-10 stsp int header_shown;
2272 98eaaa12 2019-08-03 stsp int diff_staged;
2273 63035f9f 2019-10-06 stsp int ignore_whitespace;
2274 b72f483a 2019-02-05 stsp };
2275 b72f483a 2019-02-05 stsp
2276 4ed7e80c 2018-05-20 stsp static const struct got_error *
2277 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2278 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2279 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2280 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2281 b72f483a 2019-02-05 stsp {
2282 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2283 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2284 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2285 12463d8b 2019-12-13 stsp int fd = -1;
2286 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2287 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2288 b72f483a 2019-02-05 stsp struct stat sb;
2289 b72f483a 2019-02-05 stsp
2290 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2291 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2292 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2293 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2294 98eaaa12 2019-08-03 stsp return NULL;
2295 98eaaa12 2019-08-03 stsp } else {
2296 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2297 98eaaa12 2019-08-03 stsp return NULL;
2298 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2299 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2300 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2301 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2302 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2303 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2304 98eaaa12 2019-08-03 stsp return NULL;
2305 98eaaa12 2019-08-03 stsp }
2306 3fc0c068 2019-02-10 stsp
2307 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2308 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2309 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2310 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2311 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2312 3fc0c068 2019-02-10 stsp }
2313 b72f483a 2019-02-05 stsp
2314 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2315 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2316 98eaaa12 2019-08-03 stsp switch (staged_status) {
2317 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2318 98eaaa12 2019-08-03 stsp label1 = path;
2319 98eaaa12 2019-08-03 stsp label2 = path;
2320 98eaaa12 2019-08-03 stsp break;
2321 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2322 98eaaa12 2019-08-03 stsp label2 = path;
2323 98eaaa12 2019-08-03 stsp break;
2324 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2325 98eaaa12 2019-08-03 stsp label1 = path;
2326 98eaaa12 2019-08-03 stsp break;
2327 98eaaa12 2019-08-03 stsp default:
2328 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2329 98eaaa12 2019-08-03 stsp }
2330 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2331 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2332 63035f9f 2019-10-06 stsp a->repo, stdout);
2333 98eaaa12 2019-08-03 stsp }
2334 98eaaa12 2019-08-03 stsp
2335 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2336 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2337 4ce46740 2019-08-08 stsp char *id_str;
2338 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2339 408b4ebc 2019-08-03 stsp 8192);
2340 4ce46740 2019-08-08 stsp if (err)
2341 4ce46740 2019-08-08 stsp goto done;
2342 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2343 4ce46740 2019-08-08 stsp if (err)
2344 4ce46740 2019-08-08 stsp goto done;
2345 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2346 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2347 4ce46740 2019-08-08 stsp free(id_str);
2348 4ce46740 2019-08-08 stsp goto done;
2349 4ce46740 2019-08-08 stsp }
2350 4ce46740 2019-08-08 stsp free(id_str);
2351 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2352 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2353 4ce46740 2019-08-08 stsp if (err)
2354 4ce46740 2019-08-08 stsp goto done;
2355 4ce46740 2019-08-08 stsp }
2356 d00136be 2019-03-26 stsp
2357 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2358 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2359 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2360 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2361 2ec1f75b 2019-03-26 stsp goto done;
2362 2ec1f75b 2019-03-26 stsp }
2363 b72f483a 2019-02-05 stsp
2364 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2365 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2366 12463d8b 2019-12-13 stsp if (fd == -1) {
2367 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2368 12463d8b 2019-12-13 stsp goto done;
2369 12463d8b 2019-12-13 stsp }
2370 12463d8b 2019-12-13 stsp } else {
2371 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2372 12463d8b 2019-12-13 stsp if (fd == -1) {
2373 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2374 12463d8b 2019-12-13 stsp goto done;
2375 12463d8b 2019-12-13 stsp }
2376 12463d8b 2019-12-13 stsp }
2377 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2378 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2379 2ec1f75b 2019-03-26 stsp goto done;
2380 2ec1f75b 2019-03-26 stsp }
2381 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2382 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2383 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2384 2ec1f75b 2019-03-26 stsp goto done;
2385 2ec1f75b 2019-03-26 stsp }
2386 12463d8b 2019-12-13 stsp fd = -1;
2387 2ec1f75b 2019-03-26 stsp } else
2388 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2389 b72f483a 2019-02-05 stsp
2390 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2391 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2392 b72f483a 2019-02-05 stsp done:
2393 b72f483a 2019-02-05 stsp if (blob1)
2394 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2395 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2396 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2397 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2398 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2399 b72f483a 2019-02-05 stsp free(abspath);
2400 927df6b7 2019-02-10 stsp return err;
2401 927df6b7 2019-02-10 stsp }
2402 927df6b7 2019-02-10 stsp
2403 927df6b7 2019-02-10 stsp static const struct got_error *
2404 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2405 b00d56cd 2018-04-01 stsp {
2406 b00d56cd 2018-04-01 stsp const struct got_error *error;
2407 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2408 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2409 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2410 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2411 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2412 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2413 15a94983 2018-12-23 stsp int type1, type2;
2414 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2415 c0cc5c62 2018-10-18 stsp const char *errstr;
2416 927df6b7 2019-02-10 stsp char *path = NULL;
2417 b00d56cd 2018-04-01 stsp
2418 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2419 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2420 25eccc22 2019-01-04 stsp NULL) == -1)
2421 b00d56cd 2018-04-01 stsp err(1, "pledge");
2422 b00d56cd 2018-04-01 stsp #endif
2423 b00d56cd 2018-04-01 stsp
2424 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2425 b00d56cd 2018-04-01 stsp switch (ch) {
2426 c0cc5c62 2018-10-18 stsp case 'C':
2427 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2428 dfcab68b 2019-11-29 kn &errstr);
2429 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2430 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2431 c0cc5c62 2018-10-18 stsp break;
2432 b72f483a 2019-02-05 stsp case 'r':
2433 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2434 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2435 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2436 9ba1d308 2019-10-21 stsp optarg);
2437 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2438 b72f483a 2019-02-05 stsp break;
2439 98eaaa12 2019-08-03 stsp case 's':
2440 98eaaa12 2019-08-03 stsp diff_staged = 1;
2441 63035f9f 2019-10-06 stsp break;
2442 63035f9f 2019-10-06 stsp case 'w':
2443 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2444 98eaaa12 2019-08-03 stsp break;
2445 b00d56cd 2018-04-01 stsp default:
2446 2deda0b9 2019-03-07 stsp usage_diff();
2447 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2448 b00d56cd 2018-04-01 stsp }
2449 b00d56cd 2018-04-01 stsp }
2450 b00d56cd 2018-04-01 stsp
2451 b00d56cd 2018-04-01 stsp argc -= optind;
2452 b00d56cd 2018-04-01 stsp argv += optind;
2453 b00d56cd 2018-04-01 stsp
2454 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2455 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2456 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2457 b72f483a 2019-02-05 stsp goto done;
2458 b72f483a 2019-02-05 stsp }
2459 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2460 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2461 927df6b7 2019-02-10 stsp goto done;
2462 927df6b7 2019-02-10 stsp if (argc <= 1) {
2463 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2464 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2465 927df6b7 2019-02-10 stsp goto done;
2466 927df6b7 2019-02-10 stsp }
2467 b72f483a 2019-02-05 stsp if (repo_path)
2468 b72f483a 2019-02-05 stsp errx(1,
2469 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2470 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2471 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2473 927df6b7 2019-02-10 stsp goto done;
2474 927df6b7 2019-02-10 stsp }
2475 927df6b7 2019-02-10 stsp if (argc == 1) {
2476 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2477 cbd1af7a 2019-03-18 stsp argv[0]);
2478 927df6b7 2019-02-10 stsp if (error)
2479 927df6b7 2019-02-10 stsp goto done;
2480 927df6b7 2019-02-10 stsp } else {
2481 927df6b7 2019-02-10 stsp path = strdup("");
2482 927df6b7 2019-02-10 stsp if (path == NULL) {
2483 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2484 927df6b7 2019-02-10 stsp goto done;
2485 927df6b7 2019-02-10 stsp }
2486 927df6b7 2019-02-10 stsp }
2487 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2488 98eaaa12 2019-08-03 stsp if (diff_staged)
2489 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2490 98eaaa12 2019-08-03 stsp "objects in repository");
2491 15a94983 2018-12-23 stsp id_str1 = argv[0];
2492 15a94983 2018-12-23 stsp id_str2 = argv[1];
2493 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2494 30db809c 2019-06-05 stsp repo_path =
2495 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2496 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2497 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2498 30db809c 2019-06-05 stsp goto done;
2499 30db809c 2019-06-05 stsp }
2500 30db809c 2019-06-05 stsp }
2501 b00d56cd 2018-04-01 stsp } else
2502 b00d56cd 2018-04-01 stsp usage_diff();
2503 25eccc22 2019-01-04 stsp
2504 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2505 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2506 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2507 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2508 b72f483a 2019-02-05 stsp }
2509 b00d56cd 2018-04-01 stsp
2510 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2511 76089277 2018-04-01 stsp free(repo_path);
2512 b00d56cd 2018-04-01 stsp if (error != NULL)
2513 b00d56cd 2018-04-01 stsp goto done;
2514 b00d56cd 2018-04-01 stsp
2515 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2516 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2517 c02c541e 2019-03-29 stsp if (error)
2518 c02c541e 2019-03-29 stsp goto done;
2519 c02c541e 2019-03-29 stsp
2520 30db809c 2019-06-05 stsp if (argc <= 1) {
2521 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2522 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2523 b72f483a 2019-02-05 stsp char *id_str;
2524 72ea6654 2019-07-27 stsp
2525 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2526 72ea6654 2019-07-27 stsp
2527 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2528 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2529 b72f483a 2019-02-05 stsp if (error)
2530 b72f483a 2019-02-05 stsp goto done;
2531 b72f483a 2019-02-05 stsp arg.repo = repo;
2532 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2533 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2534 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2535 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2536 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2537 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2538 b72f483a 2019-02-05 stsp
2539 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2540 72ea6654 2019-07-27 stsp if (error)
2541 72ea6654 2019-07-27 stsp goto done;
2542 72ea6654 2019-07-27 stsp
2543 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2544 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2545 b72f483a 2019-02-05 stsp free(id_str);
2546 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2547 b72f483a 2019-02-05 stsp goto done;
2548 b72f483a 2019-02-05 stsp }
2549 b72f483a 2019-02-05 stsp
2550 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2551 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2552 0adb7196 2019-08-11 stsp if (error)
2553 0adb7196 2019-08-11 stsp goto done;
2554 b00d56cd 2018-04-01 stsp
2555 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2556 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2557 0adb7196 2019-08-11 stsp if (error)
2558 0adb7196 2019-08-11 stsp goto done;
2559 b00d56cd 2018-04-01 stsp
2560 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2561 15a94983 2018-12-23 stsp if (error)
2562 15a94983 2018-12-23 stsp goto done;
2563 15a94983 2018-12-23 stsp
2564 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2565 15a94983 2018-12-23 stsp if (error)
2566 15a94983 2018-12-23 stsp goto done;
2567 15a94983 2018-12-23 stsp
2568 15a94983 2018-12-23 stsp if (type1 != type2) {
2569 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2570 b00d56cd 2018-04-01 stsp goto done;
2571 b00d56cd 2018-04-01 stsp }
2572 b00d56cd 2018-04-01 stsp
2573 15a94983 2018-12-23 stsp switch (type1) {
2574 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2575 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2576 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2577 b00d56cd 2018-04-01 stsp break;
2578 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2579 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2580 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2581 b00d56cd 2018-04-01 stsp break;
2582 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2583 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2584 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2585 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2586 b00d56cd 2018-04-01 stsp break;
2587 b00d56cd 2018-04-01 stsp default:
2588 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2589 b00d56cd 2018-04-01 stsp }
2590 b00d56cd 2018-04-01 stsp done:
2591 5e70831e 2019-05-28 stsp free(label1);
2592 5e70831e 2019-05-28 stsp free(label2);
2593 15a94983 2018-12-23 stsp free(id1);
2594 15a94983 2018-12-23 stsp free(id2);
2595 927df6b7 2019-02-10 stsp free(path);
2596 b72f483a 2019-02-05 stsp if (worktree)
2597 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2598 ad242220 2018-09-08 stsp if (repo) {
2599 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2600 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2601 ad242220 2018-09-08 stsp if (error == NULL)
2602 ad242220 2018-09-08 stsp error = repo_error;
2603 ad242220 2018-09-08 stsp }
2604 b00d56cd 2018-04-01 stsp return error;
2605 404c43c4 2018-06-21 stsp }
2606 404c43c4 2018-06-21 stsp
2607 404c43c4 2018-06-21 stsp __dead static void
2608 404c43c4 2018-06-21 stsp usage_blame(void)
2609 404c43c4 2018-06-21 stsp {
2610 9270e621 2019-02-05 stsp fprintf(stderr,
2611 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2612 404c43c4 2018-06-21 stsp getprogname());
2613 404c43c4 2018-06-21 stsp exit(1);
2614 b00d56cd 2018-04-01 stsp }
2615 b00d56cd 2018-04-01 stsp
2616 28315671 2019-08-14 stsp struct blame_line {
2617 28315671 2019-08-14 stsp int annotated;
2618 28315671 2019-08-14 stsp char *id_str;
2619 82f6abb8 2019-08-14 stsp char *committer;
2620 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2621 28315671 2019-08-14 stsp };
2622 28315671 2019-08-14 stsp
2623 28315671 2019-08-14 stsp struct blame_cb_args {
2624 28315671 2019-08-14 stsp struct blame_line *lines;
2625 28315671 2019-08-14 stsp int nlines;
2626 7ef28ff8 2019-08-14 stsp int nlines_prec;
2627 28315671 2019-08-14 stsp int lineno_cur;
2628 28315671 2019-08-14 stsp off_t *line_offsets;
2629 28315671 2019-08-14 stsp FILE *f;
2630 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2631 28315671 2019-08-14 stsp };
2632 28315671 2019-08-14 stsp
2633 404c43c4 2018-06-21 stsp static const struct got_error *
2634 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2635 28315671 2019-08-14 stsp {
2636 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2637 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2638 28315671 2019-08-14 stsp struct blame_line *bline;
2639 82f6abb8 2019-08-14 stsp char *line = NULL;
2640 28315671 2019-08-14 stsp size_t linesize = 0;
2641 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2642 28315671 2019-08-14 stsp off_t offset;
2643 bcb49d15 2019-08-14 stsp struct tm tm;
2644 bcb49d15 2019-08-14 stsp time_t committer_time;
2645 28315671 2019-08-14 stsp
2646 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2647 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2648 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2649 28315671 2019-08-14 stsp
2650 28315671 2019-08-14 stsp if (sigint_received)
2651 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2652 28315671 2019-08-14 stsp
2653 2839f8b9 2019-08-18 stsp if (lineno == -1)
2654 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2655 2839f8b9 2019-08-18 stsp
2656 28315671 2019-08-14 stsp /* Annotate this line. */
2657 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2658 28315671 2019-08-14 stsp if (bline->annotated)
2659 28315671 2019-08-14 stsp return NULL;
2660 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2661 28315671 2019-08-14 stsp if (err)
2662 28315671 2019-08-14 stsp return err;
2663 82f6abb8 2019-08-14 stsp
2664 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2665 82f6abb8 2019-08-14 stsp if (err)
2666 82f6abb8 2019-08-14 stsp goto done;
2667 82f6abb8 2019-08-14 stsp
2668 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2669 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2670 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2671 bcb49d15 2019-08-14 stsp goto done;
2672 bcb49d15 2019-08-14 stsp }
2673 bcb49d15 2019-08-14 stsp
2674 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2675 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2676 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2677 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2678 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2679 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2680 82f6abb8 2019-08-14 stsp goto done;
2681 82f6abb8 2019-08-14 stsp }
2682 28315671 2019-08-14 stsp bline->annotated = 1;
2683 28315671 2019-08-14 stsp
2684 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2685 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2686 28315671 2019-08-14 stsp if (!bline->annotated)
2687 82f6abb8 2019-08-14 stsp goto done;
2688 28315671 2019-08-14 stsp
2689 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2690 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2691 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2692 82f6abb8 2019-08-14 stsp goto done;
2693 82f6abb8 2019-08-14 stsp }
2694 28315671 2019-08-14 stsp
2695 28315671 2019-08-14 stsp while (bline->annotated) {
2696 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2697 82f6abb8 2019-08-14 stsp size_t len;
2698 82f6abb8 2019-08-14 stsp
2699 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2700 28315671 2019-08-14 stsp if (ferror(a->f))
2701 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2702 28315671 2019-08-14 stsp break;
2703 28315671 2019-08-14 stsp }
2704 82f6abb8 2019-08-14 stsp
2705 82f6abb8 2019-08-14 stsp committer = bline->committer;
2706 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2707 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2708 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2709 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2710 82f6abb8 2019-08-14 stsp if (at)
2711 82f6abb8 2019-08-14 stsp *at = '\0';
2712 82f6abb8 2019-08-14 stsp len = strlen(committer);
2713 82f6abb8 2019-08-14 stsp if (len >= 9)
2714 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2715 28315671 2019-08-14 stsp
2716 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2717 28315671 2019-08-14 stsp if (nl)
2718 28315671 2019-08-14 stsp *nl = '\0';
2719 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2720 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2721 28315671 2019-08-14 stsp
2722 28315671 2019-08-14 stsp a->lineno_cur++;
2723 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2724 28315671 2019-08-14 stsp }
2725 82f6abb8 2019-08-14 stsp done:
2726 82f6abb8 2019-08-14 stsp if (commit)
2727 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2728 28315671 2019-08-14 stsp free(line);
2729 28315671 2019-08-14 stsp return err;
2730 28315671 2019-08-14 stsp }
2731 28315671 2019-08-14 stsp
2732 28315671 2019-08-14 stsp static const struct got_error *
2733 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2734 404c43c4 2018-06-21 stsp {
2735 404c43c4 2018-06-21 stsp const struct got_error *error;
2736 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2737 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2738 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2739 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2740 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2741 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2742 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2743 28315671 2019-08-14 stsp struct blame_cb_args bca;
2744 28315671 2019-08-14 stsp int ch, obj_type, i;
2745 b02560ec 2019-08-19 stsp size_t filesize;
2746 90f3c347 2019-08-19 stsp
2747 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2748 404c43c4 2018-06-21 stsp
2749 404c43c4 2018-06-21 stsp #ifndef PROFILE
2750 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2751 36e2fb66 2019-01-04 stsp NULL) == -1)
2752 404c43c4 2018-06-21 stsp err(1, "pledge");
2753 404c43c4 2018-06-21 stsp #endif
2754 404c43c4 2018-06-21 stsp
2755 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2756 404c43c4 2018-06-21 stsp switch (ch) {
2757 404c43c4 2018-06-21 stsp case 'c':
2758 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2759 404c43c4 2018-06-21 stsp break;
2760 66bea077 2018-08-02 stsp case 'r':
2761 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2762 66bea077 2018-08-02 stsp if (repo_path == NULL)
2763 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2764 9ba1d308 2019-10-21 stsp optarg);
2765 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2766 66bea077 2018-08-02 stsp break;
2767 404c43c4 2018-06-21 stsp default:
2768 2deda0b9 2019-03-07 stsp usage_blame();
2769 404c43c4 2018-06-21 stsp /* NOTREACHED */
2770 404c43c4 2018-06-21 stsp }
2771 404c43c4 2018-06-21 stsp }
2772 404c43c4 2018-06-21 stsp
2773 404c43c4 2018-06-21 stsp argc -= optind;
2774 404c43c4 2018-06-21 stsp argv += optind;
2775 404c43c4 2018-06-21 stsp
2776 a39318fd 2018-08-02 stsp if (argc == 1)
2777 404c43c4 2018-06-21 stsp path = argv[0];
2778 a39318fd 2018-08-02 stsp else
2779 404c43c4 2018-06-21 stsp usage_blame();
2780 404c43c4 2018-06-21 stsp
2781 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2782 66bea077 2018-08-02 stsp if (cwd == NULL) {
2783 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2784 66bea077 2018-08-02 stsp goto done;
2785 66bea077 2018-08-02 stsp }
2786 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2787 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2788 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2789 66bea077 2018-08-02 stsp goto done;
2790 0c06baac 2019-02-05 stsp else
2791 0c06baac 2019-02-05 stsp error = NULL;
2792 0c06baac 2019-02-05 stsp if (worktree) {
2793 0c06baac 2019-02-05 stsp repo_path =
2794 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2795 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2796 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2797 0c06baac 2019-02-05 stsp if (error)
2798 0c06baac 2019-02-05 stsp goto done;
2799 0c06baac 2019-02-05 stsp } else {
2800 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2801 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2802 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2803 0c06baac 2019-02-05 stsp goto done;
2804 0c06baac 2019-02-05 stsp }
2805 66bea077 2018-08-02 stsp }
2806 66bea077 2018-08-02 stsp }
2807 36e2fb66 2019-01-04 stsp
2808 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2809 c02c541e 2019-03-29 stsp if (error != NULL)
2810 36e2fb66 2019-01-04 stsp goto done;
2811 66bea077 2018-08-02 stsp
2812 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2813 c02c541e 2019-03-29 stsp if (error)
2814 404c43c4 2018-06-21 stsp goto done;
2815 404c43c4 2018-06-21 stsp
2816 0c06baac 2019-02-05 stsp if (worktree) {
2817 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2818 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2819 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2820 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2821 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2822 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2823 6efaaa2d 2019-02-05 stsp path) == -1) {
2824 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2825 0c06baac 2019-02-05 stsp goto done;
2826 0c06baac 2019-02-05 stsp }
2827 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2828 0c06baac 2019-02-05 stsp free(p);
2829 0c06baac 2019-02-05 stsp } else {
2830 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2831 0c06baac 2019-02-05 stsp }
2832 0c06baac 2019-02-05 stsp if (error)
2833 66bea077 2018-08-02 stsp goto done;
2834 66bea077 2018-08-02 stsp
2835 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2836 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2837 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2838 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2839 404c43c4 2018-06-21 stsp if (error != NULL)
2840 66bea077 2018-08-02 stsp goto done;
2841 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2842 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2843 404c43c4 2018-06-21 stsp if (error != NULL)
2844 66bea077 2018-08-02 stsp goto done;
2845 404c43c4 2018-06-21 stsp } else {
2846 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2847 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2848 30837e32 2019-07-25 stsp if (error)
2849 66bea077 2018-08-02 stsp goto done;
2850 404c43c4 2018-06-21 stsp }
2851 404c43c4 2018-06-21 stsp
2852 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2853 28315671 2019-08-14 stsp if (error)
2854 28315671 2019-08-14 stsp goto done;
2855 28315671 2019-08-14 stsp
2856 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2857 28315671 2019-08-14 stsp if (error)
2858 28315671 2019-08-14 stsp goto done;
2859 28315671 2019-08-14 stsp
2860 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2861 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2862 28315671 2019-08-14 stsp goto done;
2863 28315671 2019-08-14 stsp }
2864 28315671 2019-08-14 stsp
2865 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2866 28315671 2019-08-14 stsp if (error)
2867 28315671 2019-08-14 stsp goto done;
2868 28315671 2019-08-14 stsp bca.f = got_opentemp();
2869 28315671 2019-08-14 stsp if (bca.f == NULL) {
2870 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2871 28315671 2019-08-14 stsp goto done;
2872 28315671 2019-08-14 stsp }
2873 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2874 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2875 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2876 28315671 2019-08-14 stsp goto done;
2877 28315671 2019-08-14 stsp
2878 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2879 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2880 b02560ec 2019-08-19 stsp bca.nlines--;
2881 b02560ec 2019-08-19 stsp
2882 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2883 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2884 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2885 28315671 2019-08-14 stsp goto done;
2886 28315671 2019-08-14 stsp }
2887 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2888 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2889 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2890 7ef28ff8 2019-08-14 stsp while (i > 0) {
2891 7ef28ff8 2019-08-14 stsp i /= 10;
2892 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2893 7ef28ff8 2019-08-14 stsp }
2894 82f6abb8 2019-08-14 stsp bca.repo = repo;
2895 7ef28ff8 2019-08-14 stsp
2896 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2897 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2898 404c43c4 2018-06-21 stsp done:
2899 66bea077 2018-08-02 stsp free(in_repo_path);
2900 66bea077 2018-08-02 stsp free(repo_path);
2901 66bea077 2018-08-02 stsp free(cwd);
2902 404c43c4 2018-06-21 stsp free(commit_id);
2903 28315671 2019-08-14 stsp free(obj_id);
2904 28315671 2019-08-14 stsp if (blob)
2905 28315671 2019-08-14 stsp got_object_blob_close(blob);
2906 0c06baac 2019-02-05 stsp if (worktree)
2907 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2908 ad242220 2018-09-08 stsp if (repo) {
2909 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2910 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2911 ad242220 2018-09-08 stsp if (error == NULL)
2912 ad242220 2018-09-08 stsp error = repo_error;
2913 ad242220 2018-09-08 stsp }
2914 3affba96 2019-09-22 stsp if (bca.lines) {
2915 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2916 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2917 3affba96 2019-09-22 stsp free(bline->id_str);
2918 3affba96 2019-09-22 stsp free(bline->committer);
2919 3affba96 2019-09-22 stsp }
2920 3affba96 2019-09-22 stsp free(bca.lines);
2921 28315671 2019-08-14 stsp }
2922 28315671 2019-08-14 stsp free(bca.line_offsets);
2923 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2924 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2925 404c43c4 2018-06-21 stsp return error;
2926 5de5890b 2018-10-18 stsp }
2927 5de5890b 2018-10-18 stsp
2928 5de5890b 2018-10-18 stsp __dead static void
2929 5de5890b 2018-10-18 stsp usage_tree(void)
2930 5de5890b 2018-10-18 stsp {
2931 c1669e2e 2019-01-09 stsp fprintf(stderr,
2932 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2933 5de5890b 2018-10-18 stsp getprogname());
2934 5de5890b 2018-10-18 stsp exit(1);
2935 5de5890b 2018-10-18 stsp }
2936 5de5890b 2018-10-18 stsp
2937 c1669e2e 2019-01-09 stsp static void
2938 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2939 c1669e2e 2019-01-09 stsp const char *root_path)
2940 c1669e2e 2019-01-09 stsp {
2941 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2942 848d6979 2019-08-12 stsp const char *modestr = "";
2943 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2944 5de5890b 2018-10-18 stsp
2945 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2946 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2947 c1669e2e 2019-01-09 stsp path++;
2948 c1669e2e 2019-01-09 stsp
2949 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2950 63c5ca5d 2019-08-24 stsp modestr = "$";
2951 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2952 848d6979 2019-08-12 stsp modestr = "@";
2953 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2954 848d6979 2019-08-12 stsp modestr = "/";
2955 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2956 848d6979 2019-08-12 stsp modestr = "*";
2957 848d6979 2019-08-12 stsp
2958 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2959 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2960 c1669e2e 2019-01-09 stsp }
2961 c1669e2e 2019-01-09 stsp
2962 5de5890b 2018-10-18 stsp static const struct got_error *
2963 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2964 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2965 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2966 5de5890b 2018-10-18 stsp {
2967 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2968 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2969 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2970 56e0773d 2019-11-28 stsp int nentries, i;
2971 5de5890b 2018-10-18 stsp
2972 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2973 5de5890b 2018-10-18 stsp if (err)
2974 5de5890b 2018-10-18 stsp goto done;
2975 5de5890b 2018-10-18 stsp
2976 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2977 5de5890b 2018-10-18 stsp if (err)
2978 5de5890b 2018-10-18 stsp goto done;
2979 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2980 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2981 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2982 5de5890b 2018-10-18 stsp char *id = NULL;
2983 84453469 2018-11-11 stsp
2984 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2985 84453469 2018-11-11 stsp break;
2986 84453469 2018-11-11 stsp
2987 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2988 5de5890b 2018-10-18 stsp if (show_ids) {
2989 5de5890b 2018-10-18 stsp char *id_str;
2990 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2991 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2992 5de5890b 2018-10-18 stsp if (err)
2993 5de5890b 2018-10-18 stsp goto done;
2994 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2995 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2996 5de5890b 2018-10-18 stsp free(id_str);
2997 5de5890b 2018-10-18 stsp goto done;
2998 5de5890b 2018-10-18 stsp }
2999 5de5890b 2018-10-18 stsp free(id_str);
3000 5de5890b 2018-10-18 stsp }
3001 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
3002 5de5890b 2018-10-18 stsp free(id);
3003 c1669e2e 2019-01-09 stsp
3004 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
3005 c1669e2e 2019-01-09 stsp char *child_path;
3006 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
3007 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
3008 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
3009 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
3010 c1669e2e 2019-01-09 stsp goto done;
3011 c1669e2e 2019-01-09 stsp }
3012 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
3013 c1669e2e 2019-01-09 stsp root_path, repo);
3014 c1669e2e 2019-01-09 stsp free(child_path);
3015 c1669e2e 2019-01-09 stsp if (err)
3016 c1669e2e 2019-01-09 stsp goto done;
3017 c1669e2e 2019-01-09 stsp }
3018 5de5890b 2018-10-18 stsp }
3019 5de5890b 2018-10-18 stsp done:
3020 5de5890b 2018-10-18 stsp if (tree)
3021 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
3022 5de5890b 2018-10-18 stsp free(tree_id);
3023 5de5890b 2018-10-18 stsp return err;
3024 404c43c4 2018-06-21 stsp }
3025 404c43c4 2018-06-21 stsp
3026 5de5890b 2018-10-18 stsp static const struct got_error *
3027 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
3028 5de5890b 2018-10-18 stsp {
3029 5de5890b 2018-10-18 stsp const struct got_error *error;
3030 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
3031 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
3032 7a2c19d6 2019-02-05 stsp const char *path;
3033 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
3034 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
3035 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
3036 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
3037 5de5890b 2018-10-18 stsp int ch;
3038 5de5890b 2018-10-18 stsp
3039 5de5890b 2018-10-18 stsp #ifndef PROFILE
3040 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3041 0f8d269b 2019-01-04 stsp NULL) == -1)
3042 5de5890b 2018-10-18 stsp err(1, "pledge");
3043 5de5890b 2018-10-18 stsp #endif
3044 5de5890b 2018-10-18 stsp
3045 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
3046 5de5890b 2018-10-18 stsp switch (ch) {
3047 5de5890b 2018-10-18 stsp case 'c':
3048 5de5890b 2018-10-18 stsp commit_id_str = optarg;
3049 5de5890b 2018-10-18 stsp break;
3050 5de5890b 2018-10-18 stsp case 'r':
3051 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
3052 5de5890b 2018-10-18 stsp if (repo_path == NULL)
3053 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3054 9ba1d308 2019-10-21 stsp optarg);
3055 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3056 5de5890b 2018-10-18 stsp break;
3057 5de5890b 2018-10-18 stsp case 'i':
3058 5de5890b 2018-10-18 stsp show_ids = 1;
3059 5de5890b 2018-10-18 stsp break;
3060 c1669e2e 2019-01-09 stsp case 'R':
3061 c1669e2e 2019-01-09 stsp recurse = 1;
3062 c1669e2e 2019-01-09 stsp break;
3063 5de5890b 2018-10-18 stsp default:
3064 2deda0b9 2019-03-07 stsp usage_tree();
3065 5de5890b 2018-10-18 stsp /* NOTREACHED */
3066 5de5890b 2018-10-18 stsp }
3067 5de5890b 2018-10-18 stsp }
3068 5de5890b 2018-10-18 stsp
3069 5de5890b 2018-10-18 stsp argc -= optind;
3070 5de5890b 2018-10-18 stsp argv += optind;
3071 5de5890b 2018-10-18 stsp
3072 5de5890b 2018-10-18 stsp if (argc == 1)
3073 5de5890b 2018-10-18 stsp path = argv[0];
3074 5de5890b 2018-10-18 stsp else if (argc > 1)
3075 5de5890b 2018-10-18 stsp usage_tree();
3076 5de5890b 2018-10-18 stsp else
3077 7a2c19d6 2019-02-05 stsp path = NULL;
3078 7a2c19d6 2019-02-05 stsp
3079 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
3080 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
3081 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3082 9bf7a39b 2019-02-05 stsp goto done;
3083 9bf7a39b 2019-02-05 stsp }
3084 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
3085 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
3086 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3087 7a2c19d6 2019-02-05 stsp goto done;
3088 7a2c19d6 2019-02-05 stsp else
3089 7a2c19d6 2019-02-05 stsp error = NULL;
3090 7a2c19d6 2019-02-05 stsp if (worktree) {
3091 7a2c19d6 2019-02-05 stsp repo_path =
3092 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
3093 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
3094 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3095 7a2c19d6 2019-02-05 stsp if (error)
3096 7a2c19d6 2019-02-05 stsp goto done;
3097 7a2c19d6 2019-02-05 stsp } else {
3098 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
3099 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
3100 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3101 7a2c19d6 2019-02-05 stsp goto done;
3102 7a2c19d6 2019-02-05 stsp }
3103 5de5890b 2018-10-18 stsp }
3104 5de5890b 2018-10-18 stsp }
3105 5de5890b 2018-10-18 stsp
3106 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3107 5de5890b 2018-10-18 stsp if (error != NULL)
3108 c02c541e 2019-03-29 stsp goto done;
3109 c02c541e 2019-03-29 stsp
3110 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3111 c02c541e 2019-03-29 stsp if (error)
3112 5de5890b 2018-10-18 stsp goto done;
3113 5de5890b 2018-10-18 stsp
3114 9bf7a39b 2019-02-05 stsp if (path == NULL) {
3115 9bf7a39b 2019-02-05 stsp if (worktree) {
3116 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
3117 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
3118 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3119 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3120 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3121 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3122 9bf7a39b 2019-02-05 stsp goto done;
3123 9bf7a39b 2019-02-05 stsp }
3124 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
3125 9bf7a39b 2019-02-05 stsp free(p);
3126 9bf7a39b 2019-02-05 stsp if (error)
3127 9bf7a39b 2019-02-05 stsp goto done;
3128 9bf7a39b 2019-02-05 stsp } else
3129 9bf7a39b 2019-02-05 stsp path = "/";
3130 9bf7a39b 2019-02-05 stsp }
3131 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3132 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3133 9bf7a39b 2019-02-05 stsp if (error != NULL)
3134 9bf7a39b 2019-02-05 stsp goto done;
3135 9bf7a39b 2019-02-05 stsp }
3136 5de5890b 2018-10-18 stsp
3137 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3138 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3139 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3140 5de5890b 2018-10-18 stsp if (error != NULL)
3141 5de5890b 2018-10-18 stsp goto done;
3142 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3143 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3144 5de5890b 2018-10-18 stsp if (error != NULL)
3145 5de5890b 2018-10-18 stsp goto done;
3146 5de5890b 2018-10-18 stsp } else {
3147 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3148 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
3149 30837e32 2019-07-25 stsp if (error)
3150 5de5890b 2018-10-18 stsp goto done;
3151 5de5890b 2018-10-18 stsp }
3152 5de5890b 2018-10-18 stsp
3153 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3154 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3155 5de5890b 2018-10-18 stsp done:
3156 5de5890b 2018-10-18 stsp free(in_repo_path);
3157 5de5890b 2018-10-18 stsp free(repo_path);
3158 5de5890b 2018-10-18 stsp free(cwd);
3159 5de5890b 2018-10-18 stsp free(commit_id);
3160 7a2c19d6 2019-02-05 stsp if (worktree)
3161 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3162 5de5890b 2018-10-18 stsp if (repo) {
3163 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3164 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3165 5de5890b 2018-10-18 stsp if (error == NULL)
3166 5de5890b 2018-10-18 stsp error = repo_error;
3167 5de5890b 2018-10-18 stsp }
3168 5de5890b 2018-10-18 stsp return error;
3169 5de5890b 2018-10-18 stsp }
3170 5de5890b 2018-10-18 stsp
3171 6bad629b 2019-02-04 stsp __dead static void
3172 6bad629b 2019-02-04 stsp usage_status(void)
3173 6bad629b 2019-02-04 stsp {
3174 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3175 6bad629b 2019-02-04 stsp exit(1);
3176 6bad629b 2019-02-04 stsp }
3177 5c860e29 2018-03-12 stsp
3178 b72f483a 2019-02-05 stsp static const struct got_error *
3179 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3180 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3181 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3182 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3183 6bad629b 2019-02-04 stsp {
3184 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3185 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3186 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3187 b72f483a 2019-02-05 stsp return NULL;
3188 6bad629b 2019-02-04 stsp }
3189 5c860e29 2018-03-12 stsp
3190 6bad629b 2019-02-04 stsp static const struct got_error *
3191 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3192 6bad629b 2019-02-04 stsp {
3193 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3194 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3195 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3196 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3197 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3198 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3199 a5edda0a 2019-07-27 stsp int ch;
3200 5c860e29 2018-03-12 stsp
3201 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3202 72ea6654 2019-07-27 stsp
3203 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3204 6bad629b 2019-02-04 stsp switch (ch) {
3205 5c860e29 2018-03-12 stsp default:
3206 2deda0b9 2019-03-07 stsp usage_status();
3207 6bad629b 2019-02-04 stsp /* NOTREACHED */
3208 5c860e29 2018-03-12 stsp }
3209 5c860e29 2018-03-12 stsp }
3210 5c860e29 2018-03-12 stsp
3211 6bad629b 2019-02-04 stsp argc -= optind;
3212 6bad629b 2019-02-04 stsp argv += optind;
3213 5c860e29 2018-03-12 stsp
3214 6bad629b 2019-02-04 stsp #ifndef PROFILE
3215 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3216 6bad629b 2019-02-04 stsp NULL) == -1)
3217 6bad629b 2019-02-04 stsp err(1, "pledge");
3218 f42b1b34 2018-03-12 stsp #endif
3219 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3220 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3221 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3222 927df6b7 2019-02-10 stsp goto done;
3223 927df6b7 2019-02-10 stsp }
3224 927df6b7 2019-02-10 stsp
3225 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3226 927df6b7 2019-02-10 stsp if (error != NULL)
3227 a5edda0a 2019-07-27 stsp goto done;
3228 6bad629b 2019-02-04 stsp
3229 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3230 c9956ddf 2019-09-08 stsp NULL);
3231 6bad629b 2019-02-04 stsp if (error != NULL)
3232 6bad629b 2019-02-04 stsp goto done;
3233 6bad629b 2019-02-04 stsp
3234 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3235 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3236 087fb88c 2019-08-04 stsp if (error)
3237 087fb88c 2019-08-04 stsp goto done;
3238 087fb88c 2019-08-04 stsp
3239 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3240 6bad629b 2019-02-04 stsp if (error)
3241 6bad629b 2019-02-04 stsp goto done;
3242 6bad629b 2019-02-04 stsp
3243 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3244 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3245 6bad629b 2019-02-04 stsp done:
3246 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3247 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3248 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3249 927df6b7 2019-02-10 stsp free(cwd);
3250 d0eebce4 2019-03-11 stsp return error;
3251 d0eebce4 2019-03-11 stsp }
3252 d0eebce4 2019-03-11 stsp
3253 d0eebce4 2019-03-11 stsp __dead static void
3254 d0eebce4 2019-03-11 stsp usage_ref(void)
3255 d0eebce4 2019-03-11 stsp {
3256 d0eebce4 2019-03-11 stsp fprintf(stderr,
3257 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3258 d0eebce4 2019-03-11 stsp getprogname());
3259 d0eebce4 2019-03-11 stsp exit(1);
3260 d0eebce4 2019-03-11 stsp }
3261 d0eebce4 2019-03-11 stsp
3262 d0eebce4 2019-03-11 stsp static const struct got_error *
3263 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3264 d0eebce4 2019-03-11 stsp {
3265 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3266 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3267 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3268 d0eebce4 2019-03-11 stsp
3269 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3270 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3271 d0eebce4 2019-03-11 stsp if (err)
3272 d0eebce4 2019-03-11 stsp return err;
3273 d0eebce4 2019-03-11 stsp
3274 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3275 d0eebce4 2019-03-11 stsp char *refstr;
3276 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3277 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3278 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3279 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3280 d0eebce4 2019-03-11 stsp free(refstr);
3281 d0eebce4 2019-03-11 stsp }
3282 d0eebce4 2019-03-11 stsp
3283 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3284 d0eebce4 2019-03-11 stsp return NULL;
3285 d0eebce4 2019-03-11 stsp }
3286 d0eebce4 2019-03-11 stsp
3287 d0eebce4 2019-03-11 stsp static const struct got_error *
3288 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3289 d0eebce4 2019-03-11 stsp {
3290 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3291 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3292 d0eebce4 2019-03-11 stsp
3293 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3294 d0eebce4 2019-03-11 stsp if (err)
3295 d0eebce4 2019-03-11 stsp return err;
3296 d0eebce4 2019-03-11 stsp
3297 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3298 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3299 d0eebce4 2019-03-11 stsp return err;
3300 d0eebce4 2019-03-11 stsp }
3301 d0eebce4 2019-03-11 stsp
3302 d0eebce4 2019-03-11 stsp static const struct got_error *
3303 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3304 d0eebce4 2019-03-11 stsp {
3305 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3306 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3307 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3308 d1644381 2019-07-14 stsp
3309 d1644381 2019-07-14 stsp /*
3310 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3311 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3312 d1644381 2019-07-14 stsp * an unintended typo.
3313 d1644381 2019-07-14 stsp */
3314 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3315 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3316 d0eebce4 2019-03-11 stsp
3317 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3318 dd88155e 2019-06-29 stsp repo);
3319 d83d9d5c 2019-05-13 stsp if (err) {
3320 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3321 d0eebce4 2019-03-11 stsp
3322 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3323 d83d9d5c 2019-05-13 stsp return err;
3324 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3325 d83d9d5c 2019-05-13 stsp if (err)
3326 d83d9d5c 2019-05-13 stsp return err;
3327 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3328 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3329 d83d9d5c 2019-05-13 stsp if (err)
3330 d83d9d5c 2019-05-13 stsp return err;
3331 d83d9d5c 2019-05-13 stsp }
3332 d83d9d5c 2019-05-13 stsp
3333 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3334 d0eebce4 2019-03-11 stsp if (err)
3335 d0eebce4 2019-03-11 stsp goto done;
3336 d0eebce4 2019-03-11 stsp
3337 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3338 d0eebce4 2019-03-11 stsp done:
3339 d0eebce4 2019-03-11 stsp if (ref)
3340 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3341 d0eebce4 2019-03-11 stsp free(id);
3342 d1c1ae5f 2019-08-12 stsp return err;
3343 d1c1ae5f 2019-08-12 stsp }
3344 d1c1ae5f 2019-08-12 stsp
3345 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3346 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3347 d1c1ae5f 2019-08-12 stsp {
3348 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3349 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3350 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3351 d1c1ae5f 2019-08-12 stsp
3352 d1c1ae5f 2019-08-12 stsp /*
3353 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3354 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3355 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3356 d1c1ae5f 2019-08-12 stsp */
3357 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3358 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3359 d1c1ae5f 2019-08-12 stsp
3360 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3361 d1c1ae5f 2019-08-12 stsp if (err)
3362 d1c1ae5f 2019-08-12 stsp return err;
3363 d1c1ae5f 2019-08-12 stsp
3364 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3365 d1c1ae5f 2019-08-12 stsp if (err)
3366 d1c1ae5f 2019-08-12 stsp goto done;
3367 d1c1ae5f 2019-08-12 stsp
3368 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3369 d1c1ae5f 2019-08-12 stsp done:
3370 d1c1ae5f 2019-08-12 stsp if (target_ref)
3371 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3372 d1c1ae5f 2019-08-12 stsp if (ref)
3373 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3374 d0eebce4 2019-03-11 stsp return err;
3375 d0eebce4 2019-03-11 stsp }
3376 d0eebce4 2019-03-11 stsp
3377 d0eebce4 2019-03-11 stsp static const struct got_error *
3378 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3379 d0eebce4 2019-03-11 stsp {
3380 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3381 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3382 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3383 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3384 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3385 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3386 d0eebce4 2019-03-11 stsp
3387 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3388 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3389 d0eebce4 2019-03-11 stsp switch (ch) {
3390 d0eebce4 2019-03-11 stsp case 'd':
3391 d0eebce4 2019-03-11 stsp delref = optarg;
3392 d0eebce4 2019-03-11 stsp break;
3393 d0eebce4 2019-03-11 stsp case 'r':
3394 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3395 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3396 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3397 9ba1d308 2019-10-21 stsp optarg);
3398 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3399 d0eebce4 2019-03-11 stsp break;
3400 d0eebce4 2019-03-11 stsp case 'l':
3401 d0eebce4 2019-03-11 stsp do_list = 1;
3402 d1c1ae5f 2019-08-12 stsp break;
3403 d1c1ae5f 2019-08-12 stsp case 's':
3404 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3405 d0eebce4 2019-03-11 stsp break;
3406 d0eebce4 2019-03-11 stsp default:
3407 d0eebce4 2019-03-11 stsp usage_ref();
3408 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3409 d0eebce4 2019-03-11 stsp }
3410 d0eebce4 2019-03-11 stsp }
3411 d0eebce4 2019-03-11 stsp
3412 d0eebce4 2019-03-11 stsp if (do_list && delref)
3413 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3414 d0eebce4 2019-03-11 stsp
3415 d0eebce4 2019-03-11 stsp argc -= optind;
3416 d0eebce4 2019-03-11 stsp argv += optind;
3417 d0eebce4 2019-03-11 stsp
3418 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3419 d1c1ae5f 2019-08-12 stsp if (create_symref)
3420 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3421 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3422 d0eebce4 2019-03-11 stsp if (argc > 0)
3423 d0eebce4 2019-03-11 stsp usage_ref();
3424 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3425 d0eebce4 2019-03-11 stsp usage_ref();
3426 d0eebce4 2019-03-11 stsp
3427 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3428 e0b57350 2019-03-12 stsp if (do_list) {
3429 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3430 e0b57350 2019-03-12 stsp NULL) == -1)
3431 e0b57350 2019-03-12 stsp err(1, "pledge");
3432 e0b57350 2019-03-12 stsp } else {
3433 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3434 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3435 e0b57350 2019-03-12 stsp err(1, "pledge");
3436 e0b57350 2019-03-12 stsp }
3437 d0eebce4 2019-03-11 stsp #endif
3438 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3439 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3440 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3441 d0eebce4 2019-03-11 stsp goto done;
3442 d0eebce4 2019-03-11 stsp }
3443 d0eebce4 2019-03-11 stsp
3444 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3445 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3446 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3447 d0eebce4 2019-03-11 stsp goto done;
3448 d0eebce4 2019-03-11 stsp else
3449 d0eebce4 2019-03-11 stsp error = NULL;
3450 d0eebce4 2019-03-11 stsp if (worktree) {
3451 d0eebce4 2019-03-11 stsp repo_path =
3452 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3453 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3454 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3455 d0eebce4 2019-03-11 stsp if (error)
3456 d0eebce4 2019-03-11 stsp goto done;
3457 d0eebce4 2019-03-11 stsp } else {
3458 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3459 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3460 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3461 d0eebce4 2019-03-11 stsp goto done;
3462 d0eebce4 2019-03-11 stsp }
3463 d0eebce4 2019-03-11 stsp }
3464 d0eebce4 2019-03-11 stsp }
3465 d0eebce4 2019-03-11 stsp
3466 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3467 d0eebce4 2019-03-11 stsp if (error != NULL)
3468 d0eebce4 2019-03-11 stsp goto done;
3469 d0eebce4 2019-03-11 stsp
3470 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3471 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3472 c02c541e 2019-03-29 stsp if (error)
3473 c02c541e 2019-03-29 stsp goto done;
3474 c02c541e 2019-03-29 stsp
3475 d0eebce4 2019-03-11 stsp if (do_list)
3476 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3477 d0eebce4 2019-03-11 stsp else if (delref)
3478 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3479 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3480 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3481 d0eebce4 2019-03-11 stsp else
3482 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3483 4e759de4 2019-06-26 stsp done:
3484 4e759de4 2019-06-26 stsp if (repo)
3485 4e759de4 2019-06-26 stsp got_repo_close(repo);
3486 4e759de4 2019-06-26 stsp if (worktree)
3487 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3488 4e759de4 2019-06-26 stsp free(cwd);
3489 4e759de4 2019-06-26 stsp free(repo_path);
3490 4e759de4 2019-06-26 stsp return error;
3491 4e759de4 2019-06-26 stsp }
3492 4e759de4 2019-06-26 stsp
3493 4e759de4 2019-06-26 stsp __dead static void
3494 4e759de4 2019-06-26 stsp usage_branch(void)
3495 4e759de4 2019-06-26 stsp {
3496 4e759de4 2019-06-26 stsp fprintf(stderr,
3497 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3498 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3499 4e759de4 2019-06-26 stsp exit(1);
3500 4e759de4 2019-06-26 stsp }
3501 4e759de4 2019-06-26 stsp
3502 4e759de4 2019-06-26 stsp static const struct got_error *
3503 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3504 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3505 4e99b47e 2019-10-04 stsp {
3506 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3507 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3508 4e99b47e 2019-10-04 stsp char *refstr;
3509 4e99b47e 2019-10-04 stsp
3510 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3511 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3512 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3513 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3514 4e99b47e 2019-10-04 stsp
3515 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3516 4e99b47e 2019-10-04 stsp if (err)
3517 4e99b47e 2019-10-04 stsp return err;
3518 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3519 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3520 4e99b47e 2019-10-04 stsp marker = "* ";
3521 4e99b47e 2019-10-04 stsp else
3522 4e99b47e 2019-10-04 stsp marker = "~ ";
3523 4e99b47e 2019-10-04 stsp free(id);
3524 4e99b47e 2019-10-04 stsp }
3525 4e99b47e 2019-10-04 stsp
3526 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3527 4e99b47e 2019-10-04 stsp refname += 11;
3528 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3529 4e99b47e 2019-10-04 stsp refname += 18;
3530 4e99b47e 2019-10-04 stsp
3531 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3532 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3533 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3534 4e99b47e 2019-10-04 stsp
3535 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3536 4e99b47e 2019-10-04 stsp free(refstr);
3537 4e99b47e 2019-10-04 stsp return NULL;
3538 4e99b47e 2019-10-04 stsp }
3539 4e99b47e 2019-10-04 stsp
3540 4e99b47e 2019-10-04 stsp static const struct got_error *
3541 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3542 ad89fa31 2019-10-04 stsp {
3543 ad89fa31 2019-10-04 stsp const char *refname;
3544 ad89fa31 2019-10-04 stsp
3545 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3546 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3547 ad89fa31 2019-10-04 stsp
3548 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3549 ad89fa31 2019-10-04 stsp
3550 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3551 ad89fa31 2019-10-04 stsp refname += 11;
3552 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3553 ad89fa31 2019-10-04 stsp refname += 18;
3554 ad89fa31 2019-10-04 stsp
3555 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3556 ad89fa31 2019-10-04 stsp
3557 ad89fa31 2019-10-04 stsp return NULL;
3558 ad89fa31 2019-10-04 stsp }
3559 ad89fa31 2019-10-04 stsp
3560 ad89fa31 2019-10-04 stsp static const struct got_error *
3561 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3562 4e759de4 2019-06-26 stsp {
3563 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3564 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3565 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3566 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3567 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3568 4e759de4 2019-06-26 stsp
3569 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3570 ba882ee3 2019-07-11 stsp
3571 4e99b47e 2019-10-04 stsp if (worktree) {
3572 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3573 4e99b47e 2019-10-04 stsp worktree);
3574 4e99b47e 2019-10-04 stsp if (err)
3575 4e99b47e 2019-10-04 stsp return err;
3576 4e99b47e 2019-10-04 stsp
3577 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3578 4e99b47e 2019-10-04 stsp worktree);
3579 4e99b47e 2019-10-04 stsp if (err)
3580 4e99b47e 2019-10-04 stsp return err;
3581 4e99b47e 2019-10-04 stsp
3582 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3583 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3584 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3585 4e99b47e 2019-10-04 stsp if (err)
3586 4e99b47e 2019-10-04 stsp return err;
3587 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3588 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3589 4e99b47e 2019-10-04 stsp }
3590 4e99b47e 2019-10-04 stsp }
3591 4e99b47e 2019-10-04 stsp
3592 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3593 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3594 4e759de4 2019-06-26 stsp if (err)
3595 4e759de4 2019-06-26 stsp return err;
3596 4e759de4 2019-06-26 stsp
3597 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3598 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3599 4e759de4 2019-06-26 stsp
3600 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3601 4e759de4 2019-06-26 stsp return NULL;
3602 4e759de4 2019-06-26 stsp }
3603 4e759de4 2019-06-26 stsp
3604 4e759de4 2019-06-26 stsp static const struct got_error *
3605 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3606 45cd4e47 2019-08-25 stsp const char *branch_name)
3607 4e759de4 2019-06-26 stsp {
3608 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3609 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3610 4e759de4 2019-06-26 stsp char *refname;
3611 4e759de4 2019-06-26 stsp
3612 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3613 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3614 4e759de4 2019-06-26 stsp
3615 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3616 4e759de4 2019-06-26 stsp if (err)
3617 4e759de4 2019-06-26 stsp goto done;
3618 4e759de4 2019-06-26 stsp
3619 45cd4e47 2019-08-25 stsp if (worktree &&
3620 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3621 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3622 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3623 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3624 45cd4e47 2019-08-25 stsp goto done;
3625 45cd4e47 2019-08-25 stsp }
3626 45cd4e47 2019-08-25 stsp
3627 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3628 4e759de4 2019-06-26 stsp done:
3629 45cd4e47 2019-08-25 stsp if (ref)
3630 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3631 4e759de4 2019-06-26 stsp free(refname);
3632 4e759de4 2019-06-26 stsp return err;
3633 4e759de4 2019-06-26 stsp }
3634 4e759de4 2019-06-26 stsp
3635 4e759de4 2019-06-26 stsp static const struct got_error *
3636 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3637 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3638 4e759de4 2019-06-26 stsp {
3639 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3640 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3641 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3642 d3f84d51 2019-07-11 stsp
3643 d3f84d51 2019-07-11 stsp /*
3644 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3645 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3646 d3f84d51 2019-07-11 stsp * an unintended typo.
3647 d3f84d51 2019-07-11 stsp */
3648 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3649 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3650 4e759de4 2019-06-26 stsp
3651 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3652 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3653 4e759de4 2019-06-26 stsp goto done;
3654 4e759de4 2019-06-26 stsp }
3655 4e759de4 2019-06-26 stsp
3656 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3657 4e759de4 2019-06-26 stsp if (err == NULL) {
3658 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3659 4e759de4 2019-06-26 stsp goto done;
3660 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3661 4e759de4 2019-06-26 stsp goto done;
3662 4e759de4 2019-06-26 stsp
3663 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3664 4e759de4 2019-06-26 stsp if (err)
3665 4e759de4 2019-06-26 stsp goto done;
3666 4e759de4 2019-06-26 stsp
3667 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3668 d0eebce4 2019-03-11 stsp done:
3669 4e759de4 2019-06-26 stsp if (ref)
3670 4e759de4 2019-06-26 stsp got_ref_close(ref);
3671 4e759de4 2019-06-26 stsp free(base_refname);
3672 4e759de4 2019-06-26 stsp free(refname);
3673 4e759de4 2019-06-26 stsp return err;
3674 4e759de4 2019-06-26 stsp }
3675 4e759de4 2019-06-26 stsp
3676 4e759de4 2019-06-26 stsp static const struct got_error *
3677 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3678 4e759de4 2019-06-26 stsp {
3679 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3680 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3681 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3682 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3683 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3684 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3685 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3686 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3687 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3688 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3689 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3690 4e759de4 2019-06-26 stsp
3691 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3692 da76fce2 2020-02-24 stsp
3693 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3694 4e759de4 2019-06-26 stsp switch (ch) {
3695 a74f7e83 2019-11-10 stsp case 'c':
3696 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3697 a74f7e83 2019-11-10 stsp break;
3698 4e759de4 2019-06-26 stsp case 'd':
3699 4e759de4 2019-06-26 stsp delref = optarg;
3700 4e759de4 2019-06-26 stsp break;
3701 4e759de4 2019-06-26 stsp case 'r':
3702 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3703 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3704 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3705 9ba1d308 2019-10-21 stsp optarg);
3706 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3707 4e759de4 2019-06-26 stsp break;
3708 4e759de4 2019-06-26 stsp case 'l':
3709 4e759de4 2019-06-26 stsp do_list = 1;
3710 da76fce2 2020-02-24 stsp break;
3711 da76fce2 2020-02-24 stsp case 'n':
3712 da76fce2 2020-02-24 stsp do_update = 0;
3713 4e759de4 2019-06-26 stsp break;
3714 4e759de4 2019-06-26 stsp default:
3715 4e759de4 2019-06-26 stsp usage_branch();
3716 4e759de4 2019-06-26 stsp /* NOTREACHED */
3717 4e759de4 2019-06-26 stsp }
3718 4e759de4 2019-06-26 stsp }
3719 4e759de4 2019-06-26 stsp
3720 4e759de4 2019-06-26 stsp if (do_list && delref)
3721 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3722 4e759de4 2019-06-26 stsp
3723 4e759de4 2019-06-26 stsp argc -= optind;
3724 4e759de4 2019-06-26 stsp argv += optind;
3725 ad89fa31 2019-10-04 stsp
3726 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3727 ad89fa31 2019-10-04 stsp do_show = 1;
3728 4e759de4 2019-06-26 stsp
3729 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3730 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3731 a74f7e83 2019-11-10 stsp
3732 4e759de4 2019-06-26 stsp if (do_list || delref) {
3733 4e759de4 2019-06-26 stsp if (argc > 0)
3734 4e759de4 2019-06-26 stsp usage_branch();
3735 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3736 4e759de4 2019-06-26 stsp usage_branch();
3737 4e759de4 2019-06-26 stsp
3738 4e759de4 2019-06-26 stsp #ifndef PROFILE
3739 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3740 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3741 4e759de4 2019-06-26 stsp NULL) == -1)
3742 4e759de4 2019-06-26 stsp err(1, "pledge");
3743 4e759de4 2019-06-26 stsp } else {
3744 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3745 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3746 4e759de4 2019-06-26 stsp err(1, "pledge");
3747 4e759de4 2019-06-26 stsp }
3748 4e759de4 2019-06-26 stsp #endif
3749 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3750 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3751 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3752 4e759de4 2019-06-26 stsp goto done;
3753 4e759de4 2019-06-26 stsp }
3754 4e759de4 2019-06-26 stsp
3755 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3756 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3757 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3758 4e759de4 2019-06-26 stsp goto done;
3759 4e759de4 2019-06-26 stsp else
3760 4e759de4 2019-06-26 stsp error = NULL;
3761 4e759de4 2019-06-26 stsp if (worktree) {
3762 4e759de4 2019-06-26 stsp repo_path =
3763 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3764 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3765 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3766 4e759de4 2019-06-26 stsp if (error)
3767 4e759de4 2019-06-26 stsp goto done;
3768 4e759de4 2019-06-26 stsp } else {
3769 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3770 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3771 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3772 4e759de4 2019-06-26 stsp goto done;
3773 4e759de4 2019-06-26 stsp }
3774 4e759de4 2019-06-26 stsp }
3775 4e759de4 2019-06-26 stsp }
3776 4e759de4 2019-06-26 stsp
3777 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3778 4e759de4 2019-06-26 stsp if (error != NULL)
3779 4e759de4 2019-06-26 stsp goto done;
3780 4e759de4 2019-06-26 stsp
3781 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3782 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3783 4e759de4 2019-06-26 stsp if (error)
3784 4e759de4 2019-06-26 stsp goto done;
3785 4e759de4 2019-06-26 stsp
3786 ad89fa31 2019-10-04 stsp if (do_show)
3787 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3788 ad89fa31 2019-10-04 stsp else if (do_list)
3789 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3790 4e759de4 2019-06-26 stsp else if (delref)
3791 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3792 4e759de4 2019-06-26 stsp else {
3793 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3794 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3795 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3796 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3797 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3798 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3799 a74f7e83 2019-11-10 stsp if (error)
3800 a74f7e83 2019-11-10 stsp goto done;
3801 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3802 da76fce2 2020-02-24 stsp if (error)
3803 da76fce2 2020-02-24 stsp goto done;
3804 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3805 da76fce2 2020-02-24 stsp int did_something = 0;
3806 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3807 da76fce2 2020-02-24 stsp
3808 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3809 da76fce2 2020-02-24 stsp if (error)
3810 da76fce2 2020-02-24 stsp goto done;
3811 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3812 da76fce2 2020-02-24 stsp worktree);
3813 da76fce2 2020-02-24 stsp if (error)
3814 da76fce2 2020-02-24 stsp goto done;
3815 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3816 da76fce2 2020-02-24 stsp == -1) {
3817 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3818 da76fce2 2020-02-24 stsp goto done;
3819 da76fce2 2020-02-24 stsp }
3820 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3821 da76fce2 2020-02-24 stsp free(branch_refname);
3822 da76fce2 2020-02-24 stsp if (error)
3823 da76fce2 2020-02-24 stsp goto done;
3824 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3825 da76fce2 2020-02-24 stsp repo);
3826 da76fce2 2020-02-24 stsp if (error)
3827 da76fce2 2020-02-24 stsp goto done;
3828 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3829 da76fce2 2020-02-24 stsp commit_id);
3830 da76fce2 2020-02-24 stsp if (error)
3831 da76fce2 2020-02-24 stsp goto done;
3832 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3833 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3834 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3835 da76fce2 2020-02-24 stsp if (error)
3836 da76fce2 2020-02-24 stsp goto done;
3837 da76fce2 2020-02-24 stsp if (did_something)
3838 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3839 da76fce2 2020-02-24 stsp }
3840 4e759de4 2019-06-26 stsp }
3841 4e759de4 2019-06-26 stsp done:
3842 da76fce2 2020-02-24 stsp if (ref)
3843 da76fce2 2020-02-24 stsp got_ref_close(ref);
3844 d0eebce4 2019-03-11 stsp if (repo)
3845 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3846 d0eebce4 2019-03-11 stsp if (worktree)
3847 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3848 d0eebce4 2019-03-11 stsp free(cwd);
3849 d0eebce4 2019-03-11 stsp free(repo_path);
3850 da76fce2 2020-02-24 stsp free(commit_id);
3851 da76fce2 2020-02-24 stsp free(commit_id_str);
3852 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3853 da76fce2 2020-02-24 stsp free((char *)pe->path);
3854 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3855 d00136be 2019-03-26 stsp return error;
3856 d00136be 2019-03-26 stsp }
3857 d00136be 2019-03-26 stsp
3858 8e7bd50a 2019-08-22 stsp
3859 d00136be 2019-03-26 stsp __dead static void
3860 8e7bd50a 2019-08-22 stsp usage_tag(void)
3861 8e7bd50a 2019-08-22 stsp {
3862 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3863 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3864 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3865 8e7bd50a 2019-08-22 stsp exit(1);
3866 b8bad2ba 2019-08-23 stsp }
3867 b8bad2ba 2019-08-23 stsp
3868 b8bad2ba 2019-08-23 stsp #if 0
3869 b8bad2ba 2019-08-23 stsp static const struct got_error *
3870 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3871 b8bad2ba 2019-08-23 stsp {
3872 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3873 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3874 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3875 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3876 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3877 b8bad2ba 2019-08-23 stsp
3878 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3879 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3880 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3881 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3882 b8bad2ba 2019-08-23 stsp if (err)
3883 b8bad2ba 2019-08-23 stsp return err;
3884 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3885 b8bad2ba 2019-08-23 stsp continue;
3886 b8bad2ba 2019-08-23 stsp } else {
3887 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3888 b8bad2ba 2019-08-23 stsp if (err)
3889 b8bad2ba 2019-08-23 stsp break;
3890 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3891 b8bad2ba 2019-08-23 stsp free(re_id);
3892 b8bad2ba 2019-08-23 stsp if (err)
3893 b8bad2ba 2019-08-23 stsp break;
3894 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3895 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3896 b8bad2ba 2019-08-23 stsp }
3897 b8bad2ba 2019-08-23 stsp
3898 b8bad2ba 2019-08-23 stsp while (se) {
3899 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3900 b8bad2ba 2019-08-23 stsp if (err)
3901 b8bad2ba 2019-08-23 stsp break;
3902 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3903 b8bad2ba 2019-08-23 stsp free(se_id);
3904 b8bad2ba 2019-08-23 stsp if (err)
3905 b8bad2ba 2019-08-23 stsp break;
3906 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3907 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3908 b8bad2ba 2019-08-23 stsp
3909 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3910 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3911 b8bad2ba 2019-08-23 stsp if (err)
3912 b8bad2ba 2019-08-23 stsp return err;
3913 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3914 b8bad2ba 2019-08-23 stsp break;
3915 b8bad2ba 2019-08-23 stsp }
3916 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3917 b8bad2ba 2019-08-23 stsp continue;
3918 b8bad2ba 2019-08-23 stsp }
3919 b8bad2ba 2019-08-23 stsp }
3920 b8bad2ba 2019-08-23 stsp done:
3921 b8bad2ba 2019-08-23 stsp return err;
3922 8e7bd50a 2019-08-22 stsp }
3923 b8bad2ba 2019-08-23 stsp #endif
3924 b8bad2ba 2019-08-23 stsp
3925 b8bad2ba 2019-08-23 stsp static const struct got_error *
3926 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3927 8e7bd50a 2019-08-22 stsp {
3928 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3929 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3930 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3931 8e7bd50a 2019-08-22 stsp
3932 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3933 8e7bd50a 2019-08-22 stsp
3934 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3935 8e7bd50a 2019-08-22 stsp if (err)
3936 8e7bd50a 2019-08-22 stsp return err;
3937 8e7bd50a 2019-08-22 stsp
3938 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3939 8e7bd50a 2019-08-22 stsp const char *refname;
3940 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3941 8e7bd50a 2019-08-22 stsp char datebuf[26];
3942 d4efa91b 2020-01-14 stsp const char *tagger;
3943 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3944 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3945 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3946 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3947 8e7bd50a 2019-08-22 stsp
3948 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3949 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3950 8e7bd50a 2019-08-22 stsp continue;
3951 8e7bd50a 2019-08-22 stsp refname += 10;
3952 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3953 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3954 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3955 8e7bd50a 2019-08-22 stsp break;
3956 8e7bd50a 2019-08-22 stsp }
3957 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3958 8e7bd50a 2019-08-22 stsp free(refstr);
3959 8e7bd50a 2019-08-22 stsp
3960 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3961 8e7bd50a 2019-08-22 stsp if (err)
3962 8e7bd50a 2019-08-22 stsp break;
3963 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3964 d4efa91b 2020-01-14 stsp if (err) {
3965 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3966 d4efa91b 2020-01-14 stsp free(id);
3967 d4efa91b 2020-01-14 stsp break;
3968 d4efa91b 2020-01-14 stsp }
3969 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3970 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3971 d4efa91b 2020-01-14 stsp if (err) {
3972 d4efa91b 2020-01-14 stsp free(id);
3973 d4efa91b 2020-01-14 stsp break;
3974 d4efa91b 2020-01-14 stsp }
3975 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3976 d4efa91b 2020-01-14 stsp tagger_time =
3977 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3978 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3979 d4efa91b 2020-01-14 stsp free(id);
3980 d4efa91b 2020-01-14 stsp if (err)
3981 d4efa91b 2020-01-14 stsp break;
3982 d4efa91b 2020-01-14 stsp } else {
3983 d4efa91b 2020-01-14 stsp free(id);
3984 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3985 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3986 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3987 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3988 d4efa91b 2020-01-14 stsp if (err)
3989 d4efa91b 2020-01-14 stsp break;
3990 d4efa91b 2020-01-14 stsp }
3991 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3992 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3993 2417344c 2019-08-23 stsp if (datestr)
3994 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3995 d4efa91b 2020-01-14 stsp if (commit)
3996 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3997 d4efa91b 2020-01-14 stsp else {
3998 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3999 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
4000 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
4001 d4efa91b 2020-01-14 stsp id_str);
4002 d4efa91b 2020-01-14 stsp break;
4003 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
4004 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
4005 d4efa91b 2020-01-14 stsp id_str);
4006 d4efa91b 2020-01-14 stsp break;
4007 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
4008 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
4009 d4efa91b 2020-01-14 stsp id_str);
4010 d4efa91b 2020-01-14 stsp break;
4011 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
4012 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
4013 d4efa91b 2020-01-14 stsp id_str);
4014 d4efa91b 2020-01-14 stsp break;
4015 d4efa91b 2020-01-14 stsp default:
4016 d4efa91b 2020-01-14 stsp break;
4017 d4efa91b 2020-01-14 stsp }
4018 8e7bd50a 2019-08-22 stsp }
4019 8e7bd50a 2019-08-22 stsp free(id_str);
4020 d4efa91b 2020-01-14 stsp if (commit) {
4021 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
4022 d4efa91b 2020-01-14 stsp if (err)
4023 d4efa91b 2020-01-14 stsp break;
4024 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
4025 d4efa91b 2020-01-14 stsp } else {
4026 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
4027 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
4028 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
4029 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
4030 d4efa91b 2020-01-14 stsp break;
4031 d4efa91b 2020-01-14 stsp }
4032 8e7bd50a 2019-08-22 stsp }
4033 8e7bd50a 2019-08-22 stsp
4034 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
4035 8e7bd50a 2019-08-22 stsp do {
4036 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
4037 8e7bd50a 2019-08-22 stsp if (line)
4038 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
4039 8e7bd50a 2019-08-22 stsp } while (line);
4040 8e7bd50a 2019-08-22 stsp free(tagmsg0);
4041 8e7bd50a 2019-08-22 stsp }
4042 8e7bd50a 2019-08-22 stsp
4043 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
4044 8e7bd50a 2019-08-22 stsp return NULL;
4045 8e7bd50a 2019-08-22 stsp }
4046 8e7bd50a 2019-08-22 stsp
4047 8e7bd50a 2019-08-22 stsp static const struct got_error *
4048 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
4049 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
4050 8e7bd50a 2019-08-22 stsp {
4051 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4052 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
4053 f372d5cd 2019-10-21 stsp char *editor = NULL;
4054 8e7bd50a 2019-08-22 stsp int fd = -1;
4055 8e7bd50a 2019-08-22 stsp
4056 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
4057 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4058 8e7bd50a 2019-08-22 stsp goto done;
4059 8e7bd50a 2019-08-22 stsp }
4060 8e7bd50a 2019-08-22 stsp
4061 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
4062 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
4063 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4064 8e7bd50a 2019-08-22 stsp goto done;
4065 8e7bd50a 2019-08-22 stsp }
4066 8e7bd50a 2019-08-22 stsp
4067 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
4068 8e7bd50a 2019-08-22 stsp if (err)
4069 8e7bd50a 2019-08-22 stsp goto done;
4070 8e7bd50a 2019-08-22 stsp
4071 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
4072 8e7bd50a 2019-08-22 stsp close(fd);
4073 8e7bd50a 2019-08-22 stsp
4074 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
4075 8e7bd50a 2019-08-22 stsp if (err)
4076 8e7bd50a 2019-08-22 stsp goto done;
4077 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
4078 8e7bd50a 2019-08-22 stsp done:
4079 8e7bd50a 2019-08-22 stsp free(initial_content);
4080 8e7bd50a 2019-08-22 stsp free(template);
4081 8e7bd50a 2019-08-22 stsp free(editor);
4082 8e7bd50a 2019-08-22 stsp
4083 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
4084 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4085 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
4086 8e7bd50a 2019-08-22 stsp if (err) {
4087 8e7bd50a 2019-08-22 stsp free(*tagmsg);
4088 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
4089 8e7bd50a 2019-08-22 stsp }
4090 8e7bd50a 2019-08-22 stsp }
4091 8e7bd50a 2019-08-22 stsp return err;
4092 8e7bd50a 2019-08-22 stsp }
4093 8e7bd50a 2019-08-22 stsp
4094 8e7bd50a 2019-08-22 stsp static const struct got_error *
4095 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
4096 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
4097 8e7bd50a 2019-08-22 stsp {
4098 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
4099 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
4100 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
4101 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
4102 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
4103 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
4104 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
4105 8e7bd50a 2019-08-22 stsp
4106 8e7bd50a 2019-08-22 stsp /*
4107 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
4108 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
4109 8e7bd50a 2019-08-22 stsp * an unintended typo.
4110 8e7bd50a 2019-08-22 stsp */
4111 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
4112 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
4113 8e7bd50a 2019-08-22 stsp
4114 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
4115 8e7bd50a 2019-08-22 stsp if (err)
4116 8e7bd50a 2019-08-22 stsp return err;
4117 8e7bd50a 2019-08-22 stsp
4118 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
4119 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
4120 8e7bd50a 2019-08-22 stsp if (err)
4121 8e7bd50a 2019-08-22 stsp goto done;
4122 8e7bd50a 2019-08-22 stsp
4123 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
4124 8e7bd50a 2019-08-22 stsp if (err)
4125 8e7bd50a 2019-08-22 stsp goto done;
4126 8e7bd50a 2019-08-22 stsp
4127 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
4128 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
4129 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
4130 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
4131 8e7bd50a 2019-08-22 stsp goto done;
4132 8e7bd50a 2019-08-22 stsp }
4133 8e7bd50a 2019-08-22 stsp tag_name += 10;
4134 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
4135 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
4136 8e7bd50a 2019-08-22 stsp goto done;
4137 8e7bd50a 2019-08-22 stsp }
4138 8e7bd50a 2019-08-22 stsp
4139 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
4140 8e7bd50a 2019-08-22 stsp if (err == NULL) {
4141 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
4142 8e7bd50a 2019-08-22 stsp goto done;
4143 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
4144 8e7bd50a 2019-08-22 stsp goto done;
4145 8e7bd50a 2019-08-22 stsp
4146 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
4147 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
4148 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
4149 f372d5cd 2019-10-21 stsp if (err) {
4150 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4151 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
4152 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4153 8e7bd50a 2019-08-22 stsp goto done;
4154 f372d5cd 2019-10-21 stsp }
4155 8e7bd50a 2019-08-22 stsp }
4156 8e7bd50a 2019-08-22 stsp
4157 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
4158 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
4159 f372d5cd 2019-10-21 stsp if (err) {
4160 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4161 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4162 8e7bd50a 2019-08-22 stsp goto done;
4163 f372d5cd 2019-10-21 stsp }
4164 8e7bd50a 2019-08-22 stsp
4165 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4166 f372d5cd 2019-10-21 stsp if (err) {
4167 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4168 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4169 8e7bd50a 2019-08-22 stsp goto done;
4170 f372d5cd 2019-10-21 stsp }
4171 8e7bd50a 2019-08-22 stsp
4172 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4173 f372d5cd 2019-10-21 stsp if (err) {
4174 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4175 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4176 f372d5cd 2019-10-21 stsp goto done;
4177 f372d5cd 2019-10-21 stsp }
4178 8e7bd50a 2019-08-22 stsp
4179 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4180 f372d5cd 2019-10-21 stsp if (err) {
4181 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4182 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4183 f372d5cd 2019-10-21 stsp goto done;
4184 8e7bd50a 2019-08-22 stsp }
4185 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4186 8e7bd50a 2019-08-22 stsp done:
4187 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4188 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4189 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4190 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4191 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4192 f372d5cd 2019-10-21 stsp free(tag_id_str);
4193 8e7bd50a 2019-08-22 stsp if (ref)
4194 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4195 8e7bd50a 2019-08-22 stsp free(commit_id);
4196 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4197 8e7bd50a 2019-08-22 stsp free(refname);
4198 8e7bd50a 2019-08-22 stsp free(tagmsg);
4199 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4200 aba9c984 2019-09-08 stsp free(tagger);
4201 8e7bd50a 2019-08-22 stsp return err;
4202 8e7bd50a 2019-08-22 stsp }
4203 8e7bd50a 2019-08-22 stsp
4204 8e7bd50a 2019-08-22 stsp static const struct got_error *
4205 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4206 8e7bd50a 2019-08-22 stsp {
4207 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4208 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4209 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4210 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4211 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4212 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4213 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4214 8e7bd50a 2019-08-22 stsp
4215 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4216 8e7bd50a 2019-08-22 stsp switch (ch) {
4217 80106605 2020-02-24 stsp case 'c':
4218 80106605 2020-02-24 stsp commit_id_arg = optarg;
4219 80106605 2020-02-24 stsp break;
4220 8e7bd50a 2019-08-22 stsp case 'm':
4221 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4222 8e7bd50a 2019-08-22 stsp break;
4223 8e7bd50a 2019-08-22 stsp case 'r':
4224 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4225 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4226 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4227 9ba1d308 2019-10-21 stsp optarg);
4228 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4229 8e7bd50a 2019-08-22 stsp break;
4230 8e7bd50a 2019-08-22 stsp case 'l':
4231 8e7bd50a 2019-08-22 stsp do_list = 1;
4232 8e7bd50a 2019-08-22 stsp break;
4233 8e7bd50a 2019-08-22 stsp default:
4234 8e7bd50a 2019-08-22 stsp usage_tag();
4235 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4236 8e7bd50a 2019-08-22 stsp }
4237 8e7bd50a 2019-08-22 stsp }
4238 8e7bd50a 2019-08-22 stsp
4239 8e7bd50a 2019-08-22 stsp argc -= optind;
4240 8e7bd50a 2019-08-22 stsp argv += optind;
4241 8e7bd50a 2019-08-22 stsp
4242 8e7bd50a 2019-08-22 stsp if (do_list) {
4243 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4244 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4245 8e7bd50a 2019-08-22 stsp if (tagmsg)
4246 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4247 8e7bd50a 2019-08-22 stsp if (argc > 0)
4248 8e7bd50a 2019-08-22 stsp usage_tag();
4249 80106605 2020-02-24 stsp } else if (argc != 1)
4250 8e7bd50a 2019-08-22 stsp usage_tag();
4251 80106605 2020-02-24 stsp
4252 a2887370 2019-08-23 stsp tag_name = argv[0];
4253 8e7bd50a 2019-08-22 stsp
4254 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4255 8e7bd50a 2019-08-22 stsp if (do_list) {
4256 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4257 8e7bd50a 2019-08-22 stsp NULL) == -1)
4258 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4259 8e7bd50a 2019-08-22 stsp } else {
4260 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4261 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4262 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4263 8e7bd50a 2019-08-22 stsp }
4264 8e7bd50a 2019-08-22 stsp #endif
4265 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4266 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4267 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4268 8e7bd50a 2019-08-22 stsp goto done;
4269 8e7bd50a 2019-08-22 stsp }
4270 8e7bd50a 2019-08-22 stsp
4271 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4272 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4273 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4274 8e7bd50a 2019-08-22 stsp goto done;
4275 8e7bd50a 2019-08-22 stsp else
4276 8e7bd50a 2019-08-22 stsp error = NULL;
4277 8e7bd50a 2019-08-22 stsp if (worktree) {
4278 8e7bd50a 2019-08-22 stsp repo_path =
4279 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4280 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4281 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4282 8e7bd50a 2019-08-22 stsp if (error)
4283 8e7bd50a 2019-08-22 stsp goto done;
4284 8e7bd50a 2019-08-22 stsp } else {
4285 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4286 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4287 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4288 8e7bd50a 2019-08-22 stsp goto done;
4289 8e7bd50a 2019-08-22 stsp }
4290 8e7bd50a 2019-08-22 stsp }
4291 8e7bd50a 2019-08-22 stsp }
4292 8e7bd50a 2019-08-22 stsp
4293 8e7bd50a 2019-08-22 stsp if (do_list) {
4294 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4295 c9956ddf 2019-09-08 stsp if (error != NULL)
4296 c9956ddf 2019-09-08 stsp goto done;
4297 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4298 8e7bd50a 2019-08-22 stsp if (error)
4299 8e7bd50a 2019-08-22 stsp goto done;
4300 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4301 8e7bd50a 2019-08-22 stsp } else {
4302 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4303 c9956ddf 2019-09-08 stsp if (error)
4304 c9956ddf 2019-09-08 stsp goto done;
4305 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4306 c9956ddf 2019-09-08 stsp if (error != NULL)
4307 c9956ddf 2019-09-08 stsp goto done;
4308 c9956ddf 2019-09-08 stsp
4309 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4310 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4311 8e7bd50a 2019-08-22 stsp if (error)
4312 8e7bd50a 2019-08-22 stsp goto done;
4313 8e7bd50a 2019-08-22 stsp }
4314 8e7bd50a 2019-08-22 stsp
4315 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4316 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4317 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4318 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4319 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4320 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4321 8e7bd50a 2019-08-22 stsp if (error)
4322 8e7bd50a 2019-08-22 stsp goto done;
4323 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4324 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4325 8e7bd50a 2019-08-22 stsp if (error)
4326 8e7bd50a 2019-08-22 stsp goto done;
4327 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4328 8e7bd50a 2019-08-22 stsp free(commit_id);
4329 8e7bd50a 2019-08-22 stsp if (error)
4330 8e7bd50a 2019-08-22 stsp goto done;
4331 8e7bd50a 2019-08-22 stsp }
4332 8e7bd50a 2019-08-22 stsp
4333 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4334 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4335 8e7bd50a 2019-08-22 stsp }
4336 8e7bd50a 2019-08-22 stsp done:
4337 8e7bd50a 2019-08-22 stsp if (repo)
4338 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4339 8e7bd50a 2019-08-22 stsp if (worktree)
4340 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4341 8e7bd50a 2019-08-22 stsp free(cwd);
4342 8e7bd50a 2019-08-22 stsp free(repo_path);
4343 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4344 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4345 8e7bd50a 2019-08-22 stsp return error;
4346 8e7bd50a 2019-08-22 stsp }
4347 8e7bd50a 2019-08-22 stsp
4348 8e7bd50a 2019-08-22 stsp __dead static void
4349 d00136be 2019-03-26 stsp usage_add(void)
4350 d00136be 2019-03-26 stsp {
4351 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4352 022fae89 2019-12-06 tracey getprogname());
4353 d00136be 2019-03-26 stsp exit(1);
4354 d00136be 2019-03-26 stsp }
4355 d00136be 2019-03-26 stsp
4356 d00136be 2019-03-26 stsp static const struct got_error *
4357 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4358 4e68cba3 2019-11-23 stsp {
4359 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4360 4e68cba3 2019-11-23 stsp path++;
4361 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4362 4e68cba3 2019-11-23 stsp return NULL;
4363 4e68cba3 2019-11-23 stsp }
4364 4e68cba3 2019-11-23 stsp
4365 4e68cba3 2019-11-23 stsp static const struct got_error *
4366 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4367 d00136be 2019-03-26 stsp {
4368 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4369 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4370 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4371 1dd54920 2019-05-11 stsp char *cwd = NULL;
4372 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4373 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4374 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4375 1dd54920 2019-05-11 stsp
4376 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4377 d00136be 2019-03-26 stsp
4378 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4379 d00136be 2019-03-26 stsp switch (ch) {
4380 022fae89 2019-12-06 tracey case 'I':
4381 022fae89 2019-12-06 tracey no_ignores = 1;
4382 022fae89 2019-12-06 tracey break;
4383 4e68cba3 2019-11-23 stsp case 'R':
4384 4e68cba3 2019-11-23 stsp can_recurse = 1;
4385 4e68cba3 2019-11-23 stsp break;
4386 d00136be 2019-03-26 stsp default:
4387 d00136be 2019-03-26 stsp usage_add();
4388 d00136be 2019-03-26 stsp /* NOTREACHED */
4389 d00136be 2019-03-26 stsp }
4390 d00136be 2019-03-26 stsp }
4391 d00136be 2019-03-26 stsp
4392 d00136be 2019-03-26 stsp argc -= optind;
4393 d00136be 2019-03-26 stsp argv += optind;
4394 d00136be 2019-03-26 stsp
4395 43012d58 2019-07-14 stsp #ifndef PROFILE
4396 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4397 43012d58 2019-07-14 stsp NULL) == -1)
4398 43012d58 2019-07-14 stsp err(1, "pledge");
4399 43012d58 2019-07-14 stsp #endif
4400 723c305c 2019-05-11 jcs if (argc < 1)
4401 d00136be 2019-03-26 stsp usage_add();
4402 d00136be 2019-03-26 stsp
4403 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4404 d00136be 2019-03-26 stsp if (cwd == NULL) {
4405 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4406 d00136be 2019-03-26 stsp goto done;
4407 d00136be 2019-03-26 stsp }
4408 723c305c 2019-05-11 jcs
4409 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4410 d00136be 2019-03-26 stsp if (error)
4411 d00136be 2019-03-26 stsp goto done;
4412 d00136be 2019-03-26 stsp
4413 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4414 c9956ddf 2019-09-08 stsp NULL);
4415 031a5338 2019-03-26 stsp if (error != NULL)
4416 031a5338 2019-03-26 stsp goto done;
4417 031a5338 2019-03-26 stsp
4418 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4419 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4420 d00136be 2019-03-26 stsp if (error)
4421 d00136be 2019-03-26 stsp goto done;
4422 d00136be 2019-03-26 stsp
4423 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4424 6d022e97 2019-08-04 stsp if (error)
4425 022fae89 2019-12-06 tracey goto done;
4426 022fae89 2019-12-06 tracey
4427 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4428 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4429 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4430 6d022e97 2019-08-04 stsp goto done;
4431 723c305c 2019-05-11 jcs
4432 022fae89 2019-12-06 tracey }
4433 022fae89 2019-12-06 tracey
4434 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4435 4e68cba3 2019-11-23 stsp char *ondisk_path;
4436 4e68cba3 2019-11-23 stsp struct stat sb;
4437 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4438 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4439 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4440 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4441 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4442 4e68cba3 2019-11-23 stsp goto done;
4443 4e68cba3 2019-11-23 stsp }
4444 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4445 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4446 4e68cba3 2019-11-23 stsp free(ondisk_path);
4447 4e68cba3 2019-11-23 stsp continue;
4448 4e68cba3 2019-11-23 stsp }
4449 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4450 4e68cba3 2019-11-23 stsp ondisk_path);
4451 4e68cba3 2019-11-23 stsp free(ondisk_path);
4452 4e68cba3 2019-11-23 stsp goto done;
4453 4e68cba3 2019-11-23 stsp }
4454 4e68cba3 2019-11-23 stsp free(ondisk_path);
4455 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4456 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4457 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4458 4e68cba3 2019-11-23 stsp goto done;
4459 4e68cba3 2019-11-23 stsp }
4460 4e68cba3 2019-11-23 stsp }
4461 4e68cba3 2019-11-23 stsp }
4462 022fae89 2019-12-06 tracey
4463 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4464 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4465 d00136be 2019-03-26 stsp done:
4466 031a5338 2019-03-26 stsp if (repo)
4467 031a5338 2019-03-26 stsp got_repo_close(repo);
4468 d00136be 2019-03-26 stsp if (worktree)
4469 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4470 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4471 1dd54920 2019-05-11 stsp free((char *)pe->path);
4472 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4473 2ec1f75b 2019-03-26 stsp free(cwd);
4474 2ec1f75b 2019-03-26 stsp return error;
4475 2ec1f75b 2019-03-26 stsp }
4476 2ec1f75b 2019-03-26 stsp
4477 2ec1f75b 2019-03-26 stsp __dead static void
4478 648e4ef7 2019-07-09 stsp usage_remove(void)
4479 2ec1f75b 2019-03-26 stsp {
4480 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4481 f2a9dc41 2019-12-13 tracey getprogname());
4482 2ec1f75b 2019-03-26 stsp exit(1);
4483 2a06fe5f 2019-08-24 stsp }
4484 2a06fe5f 2019-08-24 stsp
4485 2a06fe5f 2019-08-24 stsp static const struct got_error *
4486 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4487 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4488 2a06fe5f 2019-08-24 stsp {
4489 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4490 f2a9dc41 2019-12-13 tracey path++;
4491 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4492 2a06fe5f 2019-08-24 stsp return NULL;
4493 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4494 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4495 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4496 2a06fe5f 2019-08-24 stsp return NULL;
4497 2ec1f75b 2019-03-26 stsp }
4498 2ec1f75b 2019-03-26 stsp
4499 2ec1f75b 2019-03-26 stsp static const struct got_error *
4500 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4501 2ec1f75b 2019-03-26 stsp {
4502 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4503 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4504 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4505 17ed4618 2019-06-02 stsp char *cwd = NULL;
4506 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4507 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4508 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4509 2ec1f75b 2019-03-26 stsp
4510 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4511 17ed4618 2019-06-02 stsp
4512 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4513 2ec1f75b 2019-03-26 stsp switch (ch) {
4514 2ec1f75b 2019-03-26 stsp case 'f':
4515 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4516 2ec1f75b 2019-03-26 stsp break;
4517 70e3e7f5 2019-12-13 tracey case 'k':
4518 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4519 70e3e7f5 2019-12-13 tracey break;
4520 f2a9dc41 2019-12-13 tracey case 'R':
4521 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4522 f2a9dc41 2019-12-13 tracey break;
4523 2ec1f75b 2019-03-26 stsp default:
4524 f2a9dc41 2019-12-13 tracey usage_remove();
4525 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4526 2ec1f75b 2019-03-26 stsp }
4527 2ec1f75b 2019-03-26 stsp }
4528 2ec1f75b 2019-03-26 stsp
4529 2ec1f75b 2019-03-26 stsp argc -= optind;
4530 2ec1f75b 2019-03-26 stsp argv += optind;
4531 2ec1f75b 2019-03-26 stsp
4532 43012d58 2019-07-14 stsp #ifndef PROFILE
4533 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4534 43012d58 2019-07-14 stsp NULL) == -1)
4535 43012d58 2019-07-14 stsp err(1, "pledge");
4536 43012d58 2019-07-14 stsp #endif
4537 17ed4618 2019-06-02 stsp if (argc < 1)
4538 648e4ef7 2019-07-09 stsp usage_remove();
4539 2ec1f75b 2019-03-26 stsp
4540 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4541 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4542 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4543 2ec1f75b 2019-03-26 stsp goto done;
4544 2ec1f75b 2019-03-26 stsp }
4545 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4546 2ec1f75b 2019-03-26 stsp if (error)
4547 2ec1f75b 2019-03-26 stsp goto done;
4548 2ec1f75b 2019-03-26 stsp
4549 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4550 c9956ddf 2019-09-08 stsp NULL);
4551 2af4a041 2019-05-11 jcs if (error)
4552 2ec1f75b 2019-03-26 stsp goto done;
4553 2ec1f75b 2019-03-26 stsp
4554 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4555 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4556 2ec1f75b 2019-03-26 stsp if (error)
4557 2ec1f75b 2019-03-26 stsp goto done;
4558 2ec1f75b 2019-03-26 stsp
4559 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4560 6d022e97 2019-08-04 stsp if (error)
4561 6d022e97 2019-08-04 stsp goto done;
4562 17ed4618 2019-06-02 stsp
4563 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4564 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4565 f2a9dc41 2019-12-13 tracey struct stat sb;
4566 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4567 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4568 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4569 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4570 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4571 f2a9dc41 2019-12-13 tracey goto done;
4572 f2a9dc41 2019-12-13 tracey }
4573 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4574 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4575 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4576 f2a9dc41 2019-12-13 tracey continue;
4577 f2a9dc41 2019-12-13 tracey }
4578 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4579 f2a9dc41 2019-12-13 tracey ondisk_path);
4580 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4581 f2a9dc41 2019-12-13 tracey goto done;
4582 f2a9dc41 2019-12-13 tracey }
4583 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4584 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4585 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4586 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4587 f2a9dc41 2019-12-13 tracey goto done;
4588 f2a9dc41 2019-12-13 tracey }
4589 f2a9dc41 2019-12-13 tracey }
4590 f2a9dc41 2019-12-13 tracey }
4591 f2a9dc41 2019-12-13 tracey
4592 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4593 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4594 a129376b 2019-03-28 stsp done:
4595 a129376b 2019-03-28 stsp if (repo)
4596 a129376b 2019-03-28 stsp got_repo_close(repo);
4597 a129376b 2019-03-28 stsp if (worktree)
4598 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4599 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4600 17ed4618 2019-06-02 stsp free((char *)pe->path);
4601 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4602 a129376b 2019-03-28 stsp free(cwd);
4603 a129376b 2019-03-28 stsp return error;
4604 a129376b 2019-03-28 stsp }
4605 a129376b 2019-03-28 stsp
4606 a129376b 2019-03-28 stsp __dead static void
4607 a129376b 2019-03-28 stsp usage_revert(void)
4608 a129376b 2019-03-28 stsp {
4609 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4610 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4611 a129376b 2019-03-28 stsp exit(1);
4612 a129376b 2019-03-28 stsp }
4613 a129376b 2019-03-28 stsp
4614 1ee397ad 2019-07-12 stsp static const struct got_error *
4615 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4616 a129376b 2019-03-28 stsp {
4617 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4618 fb9704af 2020-01-27 stsp return NULL;
4619 fb9704af 2020-01-27 stsp
4620 a129376b 2019-03-28 stsp while (path[0] == '/')
4621 a129376b 2019-03-28 stsp path++;
4622 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4623 33aa809d 2019-08-08 stsp return NULL;
4624 33aa809d 2019-08-08 stsp }
4625 33aa809d 2019-08-08 stsp
4626 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4627 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4628 33aa809d 2019-08-08 stsp const char *action;
4629 33aa809d 2019-08-08 stsp };
4630 33aa809d 2019-08-08 stsp
4631 33aa809d 2019-08-08 stsp static const struct got_error *
4632 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4633 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4634 33aa809d 2019-08-08 stsp {
4635 33aa809d 2019-08-08 stsp char *line = NULL;
4636 33aa809d 2019-08-08 stsp size_t linesize = 0;
4637 33aa809d 2019-08-08 stsp ssize_t linelen;
4638 33aa809d 2019-08-08 stsp
4639 33aa809d 2019-08-08 stsp switch (status) {
4640 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4641 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4642 33aa809d 2019-08-08 stsp break;
4643 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4644 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4645 33aa809d 2019-08-08 stsp break;
4646 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4647 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4648 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4649 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4650 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4651 33aa809d 2019-08-08 stsp printf("%s", line);
4652 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4653 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4654 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4655 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4656 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4657 33aa809d 2019-08-08 stsp break;
4658 33aa809d 2019-08-08 stsp default:
4659 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4660 33aa809d 2019-08-08 stsp }
4661 33aa809d 2019-08-08 stsp
4662 33aa809d 2019-08-08 stsp return NULL;
4663 33aa809d 2019-08-08 stsp }
4664 33aa809d 2019-08-08 stsp
4665 33aa809d 2019-08-08 stsp static const struct got_error *
4666 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4667 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4668 33aa809d 2019-08-08 stsp {
4669 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4670 33aa809d 2019-08-08 stsp char *line = NULL;
4671 33aa809d 2019-08-08 stsp size_t linesize = 0;
4672 33aa809d 2019-08-08 stsp ssize_t linelen;
4673 33aa809d 2019-08-08 stsp int resp = ' ';
4674 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4675 33aa809d 2019-08-08 stsp
4676 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4677 33aa809d 2019-08-08 stsp
4678 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4679 33aa809d 2019-08-08 stsp char *nl;
4680 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4681 33aa809d 2019-08-08 stsp a->action);
4682 33aa809d 2019-08-08 stsp if (err)
4683 33aa809d 2019-08-08 stsp return err;
4684 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4685 33aa809d 2019-08-08 stsp if (linelen == -1) {
4686 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4687 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4688 33aa809d 2019-08-08 stsp return NULL;
4689 33aa809d 2019-08-08 stsp }
4690 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4691 33aa809d 2019-08-08 stsp if (nl)
4692 33aa809d 2019-08-08 stsp *nl = '\0';
4693 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4694 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4695 33aa809d 2019-08-08 stsp printf("y\n");
4696 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4697 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4698 33aa809d 2019-08-08 stsp printf("n\n");
4699 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4700 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4701 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4702 33aa809d 2019-08-08 stsp printf("q\n");
4703 33aa809d 2019-08-08 stsp } else
4704 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4705 33aa809d 2019-08-08 stsp free(line);
4706 33aa809d 2019-08-08 stsp return NULL;
4707 33aa809d 2019-08-08 stsp }
4708 33aa809d 2019-08-08 stsp
4709 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4710 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4711 33aa809d 2019-08-08 stsp a->action);
4712 33aa809d 2019-08-08 stsp if (err)
4713 33aa809d 2019-08-08 stsp return err;
4714 33aa809d 2019-08-08 stsp resp = getchar();
4715 33aa809d 2019-08-08 stsp if (resp == '\n')
4716 33aa809d 2019-08-08 stsp resp = getchar();
4717 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4718 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4719 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4720 33aa809d 2019-08-08 stsp resp = ' ';
4721 33aa809d 2019-08-08 stsp }
4722 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4723 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4724 33aa809d 2019-08-08 stsp resp = ' ';
4725 33aa809d 2019-08-08 stsp }
4726 33aa809d 2019-08-08 stsp }
4727 33aa809d 2019-08-08 stsp
4728 33aa809d 2019-08-08 stsp if (resp == 'y')
4729 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4730 33aa809d 2019-08-08 stsp else if (resp == 'n')
4731 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4732 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4733 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4734 33aa809d 2019-08-08 stsp
4735 1ee397ad 2019-07-12 stsp return NULL;
4736 a129376b 2019-03-28 stsp }
4737 a129376b 2019-03-28 stsp
4738 33aa809d 2019-08-08 stsp
4739 a129376b 2019-03-28 stsp static const struct got_error *
4740 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4741 a129376b 2019-03-28 stsp {
4742 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4743 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4744 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4745 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4746 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4747 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4748 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4749 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4750 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4751 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4752 a129376b 2019-03-28 stsp
4753 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4754 e20a8b6f 2019-06-04 stsp
4755 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4756 a129376b 2019-03-28 stsp switch (ch) {
4757 33aa809d 2019-08-08 stsp case 'p':
4758 33aa809d 2019-08-08 stsp pflag = 1;
4759 33aa809d 2019-08-08 stsp break;
4760 33aa809d 2019-08-08 stsp case 'F':
4761 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4762 33aa809d 2019-08-08 stsp break;
4763 0f6d7415 2019-08-08 stsp case 'R':
4764 0f6d7415 2019-08-08 stsp can_recurse = 1;
4765 0f6d7415 2019-08-08 stsp break;
4766 a129376b 2019-03-28 stsp default:
4767 a129376b 2019-03-28 stsp usage_revert();
4768 a129376b 2019-03-28 stsp /* NOTREACHED */
4769 a129376b 2019-03-28 stsp }
4770 a129376b 2019-03-28 stsp }
4771 a129376b 2019-03-28 stsp
4772 a129376b 2019-03-28 stsp argc -= optind;
4773 a129376b 2019-03-28 stsp argv += optind;
4774 a129376b 2019-03-28 stsp
4775 43012d58 2019-07-14 stsp #ifndef PROFILE
4776 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4777 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4778 43012d58 2019-07-14 stsp err(1, "pledge");
4779 43012d58 2019-07-14 stsp #endif
4780 e20a8b6f 2019-06-04 stsp if (argc < 1)
4781 a129376b 2019-03-28 stsp usage_revert();
4782 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4783 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4784 a129376b 2019-03-28 stsp
4785 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4786 a129376b 2019-03-28 stsp if (cwd == NULL) {
4787 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4788 a129376b 2019-03-28 stsp goto done;
4789 a129376b 2019-03-28 stsp }
4790 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4791 2ec1f75b 2019-03-26 stsp if (error)
4792 2ec1f75b 2019-03-26 stsp goto done;
4793 a129376b 2019-03-28 stsp
4794 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4795 c9956ddf 2019-09-08 stsp NULL);
4796 a129376b 2019-03-28 stsp if (error != NULL)
4797 a129376b 2019-03-28 stsp goto done;
4798 a129376b 2019-03-28 stsp
4799 33aa809d 2019-08-08 stsp if (patch_script_path) {
4800 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4801 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4802 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4803 33aa809d 2019-08-08 stsp patch_script_path);
4804 33aa809d 2019-08-08 stsp goto done;
4805 33aa809d 2019-08-08 stsp }
4806 33aa809d 2019-08-08 stsp }
4807 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4808 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4809 a129376b 2019-03-28 stsp if (error)
4810 a129376b 2019-03-28 stsp goto done;
4811 a129376b 2019-03-28 stsp
4812 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4813 6d022e97 2019-08-04 stsp if (error)
4814 6d022e97 2019-08-04 stsp goto done;
4815 00db391e 2019-08-03 stsp
4816 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4817 0f6d7415 2019-08-08 stsp char *ondisk_path;
4818 0f6d7415 2019-08-08 stsp struct stat sb;
4819 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4820 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4821 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4822 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4823 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4824 0f6d7415 2019-08-08 stsp goto done;
4825 0f6d7415 2019-08-08 stsp }
4826 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4827 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4828 0f6d7415 2019-08-08 stsp free(ondisk_path);
4829 0f6d7415 2019-08-08 stsp continue;
4830 0f6d7415 2019-08-08 stsp }
4831 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4832 0f6d7415 2019-08-08 stsp ondisk_path);
4833 0f6d7415 2019-08-08 stsp free(ondisk_path);
4834 0f6d7415 2019-08-08 stsp goto done;
4835 0f6d7415 2019-08-08 stsp }
4836 0f6d7415 2019-08-08 stsp free(ondisk_path);
4837 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4838 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4839 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4840 0f6d7415 2019-08-08 stsp goto done;
4841 0f6d7415 2019-08-08 stsp }
4842 0f6d7415 2019-08-08 stsp }
4843 0f6d7415 2019-08-08 stsp }
4844 0f6d7415 2019-08-08 stsp
4845 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4846 33aa809d 2019-08-08 stsp cpa.action = "revert";
4847 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4848 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4849 2ec1f75b 2019-03-26 stsp done:
4850 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4851 33aa809d 2019-08-08 stsp error == NULL)
4852 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4853 2ec1f75b 2019-03-26 stsp if (repo)
4854 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4855 2ec1f75b 2019-03-26 stsp if (worktree)
4856 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4857 2ec1f75b 2019-03-26 stsp free(path);
4858 d00136be 2019-03-26 stsp free(cwd);
4859 6bad629b 2019-02-04 stsp return error;
4860 c4296144 2019-05-09 stsp }
4861 c4296144 2019-05-09 stsp
4862 c4296144 2019-05-09 stsp __dead static void
4863 c4296144 2019-05-09 stsp usage_commit(void)
4864 c4296144 2019-05-09 stsp {
4865 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4866 5c1e53bc 2019-07-28 stsp getprogname());
4867 c4296144 2019-05-09 stsp exit(1);
4868 33ad4cbe 2019-05-12 jcs }
4869 33ad4cbe 2019-05-12 jcs
4870 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4871 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4872 e2ba3d07 2019-05-13 stsp const char *editor;
4873 e0870e44 2019-05-13 stsp const char *worktree_path;
4874 76d98825 2019-06-03 stsp const char *branch_name;
4875 314a6357 2019-05-13 stsp const char *repo_path;
4876 e0870e44 2019-05-13 stsp char *logmsg_path;
4877 e2ba3d07 2019-05-13 stsp
4878 e2ba3d07 2019-05-13 stsp };
4879 e0870e44 2019-05-13 stsp
4880 33ad4cbe 2019-05-12 jcs static const struct got_error *
4881 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4882 33ad4cbe 2019-05-12 jcs void *arg)
4883 33ad4cbe 2019-05-12 jcs {
4884 76d98825 2019-06-03 stsp char *initial_content = NULL;
4885 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4886 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4887 e0870e44 2019-05-13 stsp char *template = NULL;
4888 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4889 3ce1b845 2019-07-15 stsp int fd;
4890 33ad4cbe 2019-05-12 jcs size_t len;
4891 33ad4cbe 2019-05-12 jcs
4892 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4893 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4894 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4895 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4896 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4897 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4898 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4899 33ad4cbe 2019-05-12 jcs return NULL;
4900 33ad4cbe 2019-05-12 jcs }
4901 33ad4cbe 2019-05-12 jcs
4902 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4903 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4904 76d98825 2019-06-03 stsp
4905 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4906 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4907 76d98825 2019-06-03 stsp a->branch_name) == -1)
4908 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4909 e0870e44 2019-05-13 stsp
4910 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4911 793c30b5 2019-05-13 stsp if (err)
4912 e0870e44 2019-05-13 stsp goto done;
4913 33ad4cbe 2019-05-12 jcs
4914 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4915 33ad4cbe 2019-05-12 jcs
4916 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4917 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4918 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4919 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4920 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4921 33ad4cbe 2019-05-12 jcs }
4922 33ad4cbe 2019-05-12 jcs close(fd);
4923 33ad4cbe 2019-05-12 jcs
4924 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4925 33ad4cbe 2019-05-12 jcs done:
4926 76d98825 2019-06-03 stsp free(initial_content);
4927 e0870e44 2019-05-13 stsp free(template);
4928 314a6357 2019-05-13 stsp
4929 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4930 3ce1b845 2019-07-15 stsp if (err == NULL) {
4931 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4932 3ce1b845 2019-07-15 stsp if (err) {
4933 3ce1b845 2019-07-15 stsp free(*logmsg);
4934 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4935 3ce1b845 2019-07-15 stsp }
4936 3ce1b845 2019-07-15 stsp }
4937 33ad4cbe 2019-05-12 jcs return err;
4938 6bad629b 2019-02-04 stsp }
4939 c4296144 2019-05-09 stsp
4940 c4296144 2019-05-09 stsp static const struct got_error *
4941 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4942 c4296144 2019-05-09 stsp {
4943 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4944 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4945 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4946 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4947 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4948 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4949 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4950 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4951 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4952 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4953 5c1e53bc 2019-07-28 stsp
4954 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4955 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4956 c4296144 2019-05-09 stsp
4957 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4958 c4296144 2019-05-09 stsp switch (ch) {
4959 c4296144 2019-05-09 stsp case 'm':
4960 c4296144 2019-05-09 stsp logmsg = optarg;
4961 c4296144 2019-05-09 stsp break;
4962 c4296144 2019-05-09 stsp default:
4963 c4296144 2019-05-09 stsp usage_commit();
4964 c4296144 2019-05-09 stsp /* NOTREACHED */
4965 c4296144 2019-05-09 stsp }
4966 c4296144 2019-05-09 stsp }
4967 c4296144 2019-05-09 stsp
4968 c4296144 2019-05-09 stsp argc -= optind;
4969 c4296144 2019-05-09 stsp argv += optind;
4970 c4296144 2019-05-09 stsp
4971 43012d58 2019-07-14 stsp #ifndef PROFILE
4972 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4973 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4974 43012d58 2019-07-14 stsp err(1, "pledge");
4975 43012d58 2019-07-14 stsp #endif
4976 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4977 c4296144 2019-05-09 stsp if (cwd == NULL) {
4978 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4979 c4296144 2019-05-09 stsp goto done;
4980 c4296144 2019-05-09 stsp }
4981 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4982 7d5807f4 2019-07-11 stsp if (error)
4983 7d5807f4 2019-07-11 stsp goto done;
4984 7d5807f4 2019-07-11 stsp
4985 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4986 c4296144 2019-05-09 stsp if (error)
4987 a698f62e 2019-07-25 stsp goto done;
4988 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4989 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4990 c4296144 2019-05-09 stsp goto done;
4991 a698f62e 2019-07-25 stsp }
4992 5c1e53bc 2019-07-28 stsp
4993 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4994 916f288c 2019-07-30 stsp worktree);
4995 5c1e53bc 2019-07-28 stsp if (error)
4996 5c1e53bc 2019-07-28 stsp goto done;
4997 c4296144 2019-05-09 stsp
4998 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4999 c9956ddf 2019-09-08 stsp if (error)
5000 c9956ddf 2019-09-08 stsp goto done;
5001 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5002 c9956ddf 2019-09-08 stsp gitconfig_path);
5003 c4296144 2019-05-09 stsp if (error != NULL)
5004 c4296144 2019-05-09 stsp goto done;
5005 c4296144 2019-05-09 stsp
5006 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
5007 aba9c984 2019-09-08 stsp if (error)
5008 aba9c984 2019-09-08 stsp return error;
5009 aba9c984 2019-09-08 stsp
5010 314a6357 2019-05-13 stsp /*
5011 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
5012 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
5013 314a6357 2019-05-13 stsp */
5014 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
5015 314a6357 2019-05-13 stsp error = get_editor(&editor);
5016 314a6357 2019-05-13 stsp else
5017 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5018 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5019 c4296144 2019-05-09 stsp if (error)
5020 c4296144 2019-05-09 stsp goto done;
5021 c4296144 2019-05-09 stsp
5022 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
5023 087fb88c 2019-08-04 stsp if (error)
5024 087fb88c 2019-08-04 stsp goto done;
5025 087fb88c 2019-08-04 stsp
5026 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
5027 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
5028 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
5029 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
5030 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
5031 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
5032 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
5033 916f288c 2019-07-30 stsp goto done;
5034 916f288c 2019-07-30 stsp }
5035 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
5036 916f288c 2019-07-30 stsp }
5037 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
5038 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
5039 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
5040 e0870e44 2019-05-13 stsp if (error) {
5041 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
5042 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
5043 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
5044 c4296144 2019-05-09 stsp goto done;
5045 e0870e44 2019-05-13 stsp }
5046 c4296144 2019-05-09 stsp
5047 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
5048 c4296144 2019-05-09 stsp if (error)
5049 c4296144 2019-05-09 stsp goto done;
5050 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
5051 c4296144 2019-05-09 stsp done:
5052 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
5053 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
5054 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
5055 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
5056 7266f21f 2019-10-21 stsp error == NULL)
5057 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
5058 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
5059 c4296144 2019-05-09 stsp if (repo)
5060 c4296144 2019-05-09 stsp got_repo_close(repo);
5061 c4296144 2019-05-09 stsp if (worktree)
5062 c4296144 2019-05-09 stsp got_worktree_close(worktree);
5063 c4296144 2019-05-09 stsp free(cwd);
5064 c4296144 2019-05-09 stsp free(id_str);
5065 c9956ddf 2019-09-08 stsp free(gitconfig_path);
5066 e2ba3d07 2019-05-13 stsp free(editor);
5067 aba9c984 2019-09-08 stsp free(author);
5068 234035bc 2019-06-01 stsp return error;
5069 234035bc 2019-06-01 stsp }
5070 234035bc 2019-06-01 stsp
5071 234035bc 2019-06-01 stsp __dead static void
5072 234035bc 2019-06-01 stsp usage_cherrypick(void)
5073 234035bc 2019-06-01 stsp {
5074 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
5075 234035bc 2019-06-01 stsp exit(1);
5076 234035bc 2019-06-01 stsp }
5077 234035bc 2019-06-01 stsp
5078 234035bc 2019-06-01 stsp static const struct got_error *
5079 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
5080 234035bc 2019-06-01 stsp {
5081 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
5082 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
5083 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
5084 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
5085 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
5086 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
5087 234035bc 2019-06-01 stsp struct got_object_qid *pid;
5088 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
5089 234035bc 2019-06-01 stsp int ch, did_something = 0;
5090 234035bc 2019-06-01 stsp
5091 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5092 234035bc 2019-06-01 stsp switch (ch) {
5093 234035bc 2019-06-01 stsp default:
5094 234035bc 2019-06-01 stsp usage_cherrypick();
5095 234035bc 2019-06-01 stsp /* NOTREACHED */
5096 234035bc 2019-06-01 stsp }
5097 234035bc 2019-06-01 stsp }
5098 234035bc 2019-06-01 stsp
5099 234035bc 2019-06-01 stsp argc -= optind;
5100 234035bc 2019-06-01 stsp argv += optind;
5101 234035bc 2019-06-01 stsp
5102 43012d58 2019-07-14 stsp #ifndef PROFILE
5103 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5104 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5105 43012d58 2019-07-14 stsp err(1, "pledge");
5106 43012d58 2019-07-14 stsp #endif
5107 234035bc 2019-06-01 stsp if (argc != 1)
5108 234035bc 2019-06-01 stsp usage_cherrypick();
5109 234035bc 2019-06-01 stsp
5110 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
5111 234035bc 2019-06-01 stsp if (cwd == NULL) {
5112 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
5113 234035bc 2019-06-01 stsp goto done;
5114 234035bc 2019-06-01 stsp }
5115 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
5116 234035bc 2019-06-01 stsp if (error)
5117 234035bc 2019-06-01 stsp goto done;
5118 234035bc 2019-06-01 stsp
5119 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5120 c9956ddf 2019-09-08 stsp NULL);
5121 234035bc 2019-06-01 stsp if (error != NULL)
5122 234035bc 2019-06-01 stsp goto done;
5123 234035bc 2019-06-01 stsp
5124 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5125 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5126 234035bc 2019-06-01 stsp if (error)
5127 234035bc 2019-06-01 stsp goto done;
5128 234035bc 2019-06-01 stsp
5129 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5130 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5131 234035bc 2019-06-01 stsp if (error != NULL) {
5132 234035bc 2019-06-01 stsp struct got_reference *ref;
5133 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5134 234035bc 2019-06-01 stsp goto done;
5135 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5136 234035bc 2019-06-01 stsp if (error != NULL)
5137 234035bc 2019-06-01 stsp goto done;
5138 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
5139 234035bc 2019-06-01 stsp got_ref_close(ref);
5140 234035bc 2019-06-01 stsp if (error != NULL)
5141 234035bc 2019-06-01 stsp goto done;
5142 234035bc 2019-06-01 stsp }
5143 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
5144 234035bc 2019-06-01 stsp if (error)
5145 234035bc 2019-06-01 stsp goto done;
5146 234035bc 2019-06-01 stsp
5147 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
5148 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
5149 234035bc 2019-06-01 stsp if (error != NULL)
5150 234035bc 2019-06-01 stsp goto done;
5151 234035bc 2019-06-01 stsp
5152 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5153 234035bc 2019-06-01 stsp if (error) {
5154 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
5155 234035bc 2019-06-01 stsp goto done;
5156 234035bc 2019-06-01 stsp error = NULL;
5157 234035bc 2019-06-01 stsp } else {
5158 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
5159 234035bc 2019-06-01 stsp goto done;
5160 234035bc 2019-06-01 stsp }
5161 234035bc 2019-06-01 stsp
5162 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5163 234035bc 2019-06-01 stsp if (error)
5164 234035bc 2019-06-01 stsp goto done;
5165 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5166 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5167 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5168 03415a1a 2019-06-02 stsp NULL);
5169 234035bc 2019-06-01 stsp if (error != NULL)
5170 234035bc 2019-06-01 stsp goto done;
5171 234035bc 2019-06-01 stsp
5172 234035bc 2019-06-01 stsp if (did_something)
5173 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5174 234035bc 2019-06-01 stsp done:
5175 234035bc 2019-06-01 stsp if (commit)
5176 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5177 234035bc 2019-06-01 stsp free(commit_id_str);
5178 234035bc 2019-06-01 stsp if (head_ref)
5179 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5180 234035bc 2019-06-01 stsp if (worktree)
5181 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5182 234035bc 2019-06-01 stsp if (repo)
5183 234035bc 2019-06-01 stsp got_repo_close(repo);
5184 c4296144 2019-05-09 stsp return error;
5185 c4296144 2019-05-09 stsp }
5186 5ef14e63 2019-06-02 stsp
5187 5ef14e63 2019-06-02 stsp __dead static void
5188 5ef14e63 2019-06-02 stsp usage_backout(void)
5189 5ef14e63 2019-06-02 stsp {
5190 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5191 5ef14e63 2019-06-02 stsp exit(1);
5192 5ef14e63 2019-06-02 stsp }
5193 5ef14e63 2019-06-02 stsp
5194 5ef14e63 2019-06-02 stsp static const struct got_error *
5195 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5196 5ef14e63 2019-06-02 stsp {
5197 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5198 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5199 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5200 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5201 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5202 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5203 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5204 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5205 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5206 5ef14e63 2019-06-02 stsp
5207 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5208 5ef14e63 2019-06-02 stsp switch (ch) {
5209 5ef14e63 2019-06-02 stsp default:
5210 5ef14e63 2019-06-02 stsp usage_backout();
5211 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5212 5ef14e63 2019-06-02 stsp }
5213 5ef14e63 2019-06-02 stsp }
5214 5ef14e63 2019-06-02 stsp
5215 5ef14e63 2019-06-02 stsp argc -= optind;
5216 5ef14e63 2019-06-02 stsp argv += optind;
5217 5ef14e63 2019-06-02 stsp
5218 43012d58 2019-07-14 stsp #ifndef PROFILE
5219 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5220 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5221 43012d58 2019-07-14 stsp err(1, "pledge");
5222 43012d58 2019-07-14 stsp #endif
5223 5ef14e63 2019-06-02 stsp if (argc != 1)
5224 5ef14e63 2019-06-02 stsp usage_backout();
5225 5ef14e63 2019-06-02 stsp
5226 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5227 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5228 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5229 5ef14e63 2019-06-02 stsp goto done;
5230 5ef14e63 2019-06-02 stsp }
5231 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5232 5ef14e63 2019-06-02 stsp if (error)
5233 5ef14e63 2019-06-02 stsp goto done;
5234 5ef14e63 2019-06-02 stsp
5235 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5236 c9956ddf 2019-09-08 stsp NULL);
5237 5ef14e63 2019-06-02 stsp if (error != NULL)
5238 5ef14e63 2019-06-02 stsp goto done;
5239 5ef14e63 2019-06-02 stsp
5240 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5241 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5242 5ef14e63 2019-06-02 stsp if (error)
5243 5ef14e63 2019-06-02 stsp goto done;
5244 5ef14e63 2019-06-02 stsp
5245 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5246 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5247 5ef14e63 2019-06-02 stsp if (error != NULL) {
5248 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5249 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5250 5ef14e63 2019-06-02 stsp goto done;
5251 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5252 5ef14e63 2019-06-02 stsp if (error != NULL)
5253 5ef14e63 2019-06-02 stsp goto done;
5254 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5255 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5256 5ef14e63 2019-06-02 stsp if (error != NULL)
5257 5ef14e63 2019-06-02 stsp goto done;
5258 5ef14e63 2019-06-02 stsp }
5259 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5260 5ef14e63 2019-06-02 stsp if (error)
5261 5ef14e63 2019-06-02 stsp goto done;
5262 5ef14e63 2019-06-02 stsp
5263 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5264 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5265 5ef14e63 2019-06-02 stsp if (error != NULL)
5266 5ef14e63 2019-06-02 stsp goto done;
5267 5ef14e63 2019-06-02 stsp
5268 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5269 5ef14e63 2019-06-02 stsp if (error)
5270 5ef14e63 2019-06-02 stsp goto done;
5271 5ef14e63 2019-06-02 stsp
5272 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5273 5ef14e63 2019-06-02 stsp if (error)
5274 5ef14e63 2019-06-02 stsp goto done;
5275 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5276 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5277 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5278 5ef14e63 2019-06-02 stsp goto done;
5279 5ef14e63 2019-06-02 stsp }
5280 5ef14e63 2019-06-02 stsp
5281 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5282 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5283 5ef14e63 2019-06-02 stsp if (error != NULL)
5284 5ef14e63 2019-06-02 stsp goto done;
5285 5ef14e63 2019-06-02 stsp
5286 5ef14e63 2019-06-02 stsp if (did_something)
5287 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5288 5ef14e63 2019-06-02 stsp done:
5289 5ef14e63 2019-06-02 stsp if (commit)
5290 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5291 5ef14e63 2019-06-02 stsp free(commit_id_str);
5292 5ef14e63 2019-06-02 stsp if (head_ref)
5293 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5294 818c7501 2019-07-11 stsp if (worktree)
5295 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5296 818c7501 2019-07-11 stsp if (repo)
5297 818c7501 2019-07-11 stsp got_repo_close(repo);
5298 818c7501 2019-07-11 stsp return error;
5299 818c7501 2019-07-11 stsp }
5300 818c7501 2019-07-11 stsp
5301 818c7501 2019-07-11 stsp __dead static void
5302 818c7501 2019-07-11 stsp usage_rebase(void)
5303 818c7501 2019-07-11 stsp {
5304 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5305 af54c8f8 2019-07-11 stsp getprogname());
5306 818c7501 2019-07-11 stsp exit(1);
5307 0ebf8283 2019-07-24 stsp }
5308 0ebf8283 2019-07-24 stsp
5309 0ebf8283 2019-07-24 stsp void
5310 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5311 0ebf8283 2019-07-24 stsp {
5312 0ebf8283 2019-07-24 stsp char *nl;
5313 0ebf8283 2019-07-24 stsp size_t len;
5314 0ebf8283 2019-07-24 stsp
5315 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5316 0ebf8283 2019-07-24 stsp if (len > limit)
5317 0ebf8283 2019-07-24 stsp len = limit;
5318 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5319 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5320 0ebf8283 2019-07-24 stsp if (nl)
5321 0ebf8283 2019-07-24 stsp *nl = '\0';
5322 0ebf8283 2019-07-24 stsp }
5323 0ebf8283 2019-07-24 stsp
5324 0ebf8283 2019-07-24 stsp static const struct got_error *
5325 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5326 0ebf8283 2019-07-24 stsp {
5327 5943eee2 2019-08-13 stsp const struct got_error *err;
5328 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5329 5943eee2 2019-08-13 stsp const char *s;
5330 0ebf8283 2019-07-24 stsp
5331 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5332 5943eee2 2019-08-13 stsp if (err)
5333 5943eee2 2019-08-13 stsp return err;
5334 0ebf8283 2019-07-24 stsp
5335 5943eee2 2019-08-13 stsp s = logmsg0;
5336 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5337 5943eee2 2019-08-13 stsp s++;
5338 5943eee2 2019-08-13 stsp
5339 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5340 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5341 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5342 5943eee2 2019-08-13 stsp goto done;
5343 5943eee2 2019-08-13 stsp }
5344 0ebf8283 2019-07-24 stsp
5345 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5346 5943eee2 2019-08-13 stsp done:
5347 5943eee2 2019-08-13 stsp free(logmsg0);
5348 a0ea4fc0 2020-02-28 stsp return err;
5349 a0ea4fc0 2020-02-28 stsp }
5350 a0ea4fc0 2020-02-28 stsp
5351 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5352 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5353 a0ea4fc0 2020-02-28 stsp {
5354 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5355 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5356 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5357 a0ea4fc0 2020-02-28 stsp
5358 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5359 a0ea4fc0 2020-02-28 stsp if (err)
5360 a0ea4fc0 2020-02-28 stsp return err;
5361 a0ea4fc0 2020-02-28 stsp
5362 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5363 a0ea4fc0 2020-02-28 stsp if (err)
5364 a0ea4fc0 2020-02-28 stsp goto done;
5365 a0ea4fc0 2020-02-28 stsp
5366 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5367 a0ea4fc0 2020-02-28 stsp
5368 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5369 a0ea4fc0 2020-02-28 stsp if (err)
5370 a0ea4fc0 2020-02-28 stsp goto done;
5371 a0ea4fc0 2020-02-28 stsp
5372 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5373 a0ea4fc0 2020-02-28 stsp done:
5374 a0ea4fc0 2020-02-28 stsp free(id_str);
5375 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5376 a0ea4fc0 2020-02-28 stsp free(logmsg);
5377 5943eee2 2019-08-13 stsp return err;
5378 818c7501 2019-07-11 stsp }
5379 818c7501 2019-07-11 stsp
5380 818c7501 2019-07-11 stsp static const struct got_error *
5381 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5382 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5383 818c7501 2019-07-11 stsp {
5384 818c7501 2019-07-11 stsp const struct got_error *err;
5385 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5386 818c7501 2019-07-11 stsp
5387 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5388 818c7501 2019-07-11 stsp if (err)
5389 818c7501 2019-07-11 stsp goto done;
5390 818c7501 2019-07-11 stsp
5391 ff0d2220 2019-07-11 stsp if (new_id) {
5392 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5393 ff0d2220 2019-07-11 stsp if (err)
5394 ff0d2220 2019-07-11 stsp goto done;
5395 ff0d2220 2019-07-11 stsp }
5396 818c7501 2019-07-11 stsp
5397 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5398 ff0d2220 2019-07-11 stsp if (new_id_str)
5399 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5400 0ebf8283 2019-07-24 stsp
5401 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5402 0ebf8283 2019-07-24 stsp if (err)
5403 0ebf8283 2019-07-24 stsp goto done;
5404 0ebf8283 2019-07-24 stsp
5405 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5406 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5407 818c7501 2019-07-11 stsp done:
5408 818c7501 2019-07-11 stsp free(old_id_str);
5409 818c7501 2019-07-11 stsp free(new_id_str);
5410 272a1371 2020-02-28 stsp free(logmsg);
5411 818c7501 2019-07-11 stsp return err;
5412 818c7501 2019-07-11 stsp }
5413 818c7501 2019-07-11 stsp
5414 1ee397ad 2019-07-12 stsp static const struct got_error *
5415 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5416 818c7501 2019-07-11 stsp {
5417 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5418 818c7501 2019-07-11 stsp
5419 818c7501 2019-07-11 stsp while (path[0] == '/')
5420 818c7501 2019-07-11 stsp path++;
5421 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5422 818c7501 2019-07-11 stsp
5423 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5424 1ee397ad 2019-07-12 stsp return NULL;
5425 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5426 818c7501 2019-07-11 stsp *rebase_status = status;
5427 1ee397ad 2019-07-12 stsp return NULL;
5428 818c7501 2019-07-11 stsp }
5429 818c7501 2019-07-11 stsp
5430 818c7501 2019-07-11 stsp static const struct got_error *
5431 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5432 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5433 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5434 818c7501 2019-07-11 stsp {
5435 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5436 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5437 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5438 818c7501 2019-07-11 stsp }
5439 818c7501 2019-07-11 stsp
5440 818c7501 2019-07-11 stsp static const struct got_error *
5441 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5442 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5443 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5444 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5445 ff0d2220 2019-07-11 stsp {
5446 ff0d2220 2019-07-11 stsp const struct got_error *error;
5447 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5448 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5449 ff0d2220 2019-07-11 stsp
5450 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5451 ff0d2220 2019-07-11 stsp if (error)
5452 ff0d2220 2019-07-11 stsp return error;
5453 ff0d2220 2019-07-11 stsp
5454 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5455 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5456 ff0d2220 2019-07-11 stsp if (error) {
5457 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5458 ff0d2220 2019-07-11 stsp goto done;
5459 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5460 ff0d2220 2019-07-11 stsp } else {
5461 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5462 ff0d2220 2019-07-11 stsp free(new_commit_id);
5463 ff0d2220 2019-07-11 stsp }
5464 ff0d2220 2019-07-11 stsp done:
5465 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5466 ff0d2220 2019-07-11 stsp return error;
5467 64c6d990 2019-07-11 stsp }
5468 64c6d990 2019-07-11 stsp
5469 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5470 64c6d990 2019-07-11 stsp const char *path_prefix;
5471 64c6d990 2019-07-11 stsp size_t len;
5472 8ca9bd68 2019-07-25 stsp int errcode;
5473 64c6d990 2019-07-11 stsp };
5474 64c6d990 2019-07-11 stsp
5475 64c6d990 2019-07-11 stsp static const struct got_error *
5476 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5477 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5478 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5479 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5480 64c6d990 2019-07-11 stsp {
5481 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5482 64c6d990 2019-07-11 stsp
5483 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5484 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5485 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5486 64c6d990 2019-07-11 stsp
5487 64c6d990 2019-07-11 stsp return NULL;
5488 64c6d990 2019-07-11 stsp }
5489 64c6d990 2019-07-11 stsp
5490 64c6d990 2019-07-11 stsp static const struct got_error *
5491 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5492 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5493 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5494 64c6d990 2019-07-11 stsp {
5495 64c6d990 2019-07-11 stsp const struct got_error *err;
5496 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5497 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5498 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5499 64c6d990 2019-07-11 stsp
5500 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5501 64c6d990 2019-07-11 stsp return NULL;
5502 64c6d990 2019-07-11 stsp
5503 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5504 64c6d990 2019-07-11 stsp if (err)
5505 64c6d990 2019-07-11 stsp goto done;
5506 64c6d990 2019-07-11 stsp
5507 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5508 64c6d990 2019-07-11 stsp if (err)
5509 64c6d990 2019-07-11 stsp goto done;
5510 64c6d990 2019-07-11 stsp
5511 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5512 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5513 64c6d990 2019-07-11 stsp if (err)
5514 64c6d990 2019-07-11 stsp goto done;
5515 64c6d990 2019-07-11 stsp
5516 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5517 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5518 64c6d990 2019-07-11 stsp if (err)
5519 64c6d990 2019-07-11 stsp goto done;
5520 64c6d990 2019-07-11 stsp
5521 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5522 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5523 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5524 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5525 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5526 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5527 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5528 64c6d990 2019-07-11 stsp done:
5529 64c6d990 2019-07-11 stsp if (tree1)
5530 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5531 64c6d990 2019-07-11 stsp if (tree2)
5532 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5533 64c6d990 2019-07-11 stsp if (commit)
5534 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5535 64c6d990 2019-07-11 stsp if (parent_commit)
5536 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5537 64c6d990 2019-07-11 stsp return err;
5538 ff0d2220 2019-07-11 stsp }
5539 ff0d2220 2019-07-11 stsp
5540 ff0d2220 2019-07-11 stsp static const struct got_error *
5541 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5542 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5543 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5544 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5545 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5546 af61c510 2019-07-19 stsp {
5547 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5548 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5549 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5550 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5551 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5552 af61c510 2019-07-19 stsp
5553 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5554 af61c510 2019-07-19 stsp if (err)
5555 af61c510 2019-07-19 stsp return err;
5556 af61c510 2019-07-19 stsp
5557 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5558 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5559 af61c510 2019-07-19 stsp if (err)
5560 af61c510 2019-07-19 stsp goto done;
5561 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5562 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5563 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5564 af61c510 2019-07-19 stsp if (err) {
5565 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5566 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5567 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5568 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5569 af61c510 2019-07-19 stsp "been reached?!?");
5570 ee780d5c 2020-01-04 stsp }
5571 ee780d5c 2020-01-04 stsp goto done;
5572 af61c510 2019-07-19 stsp } else {
5573 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5574 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5575 af61c510 2019-07-19 stsp if (err)
5576 af61c510 2019-07-19 stsp goto done;
5577 af61c510 2019-07-19 stsp
5578 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5579 af61c510 2019-07-19 stsp if (err)
5580 af61c510 2019-07-19 stsp goto done;
5581 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5582 af61c510 2019-07-19 stsp commit_id = parent_id;
5583 af61c510 2019-07-19 stsp }
5584 af61c510 2019-07-19 stsp }
5585 af61c510 2019-07-19 stsp done:
5586 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5587 af61c510 2019-07-19 stsp return err;
5588 af61c510 2019-07-19 stsp }
5589 af61c510 2019-07-19 stsp
5590 af61c510 2019-07-19 stsp static const struct got_error *
5591 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5592 818c7501 2019-07-11 stsp {
5593 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5594 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5595 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5596 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5597 818c7501 2019-07-11 stsp char *cwd = NULL;
5598 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5599 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5600 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5601 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5602 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5603 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5604 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5605 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5606 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5607 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5608 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5609 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5610 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5611 818c7501 2019-07-11 stsp
5612 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5613 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5614 818c7501 2019-07-11 stsp
5615 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5616 818c7501 2019-07-11 stsp switch (ch) {
5617 818c7501 2019-07-11 stsp case 'a':
5618 818c7501 2019-07-11 stsp abort_rebase = 1;
5619 818c7501 2019-07-11 stsp break;
5620 818c7501 2019-07-11 stsp case 'c':
5621 818c7501 2019-07-11 stsp continue_rebase = 1;
5622 818c7501 2019-07-11 stsp break;
5623 818c7501 2019-07-11 stsp default:
5624 818c7501 2019-07-11 stsp usage_rebase();
5625 818c7501 2019-07-11 stsp /* NOTREACHED */
5626 818c7501 2019-07-11 stsp }
5627 818c7501 2019-07-11 stsp }
5628 818c7501 2019-07-11 stsp
5629 818c7501 2019-07-11 stsp argc -= optind;
5630 818c7501 2019-07-11 stsp argv += optind;
5631 818c7501 2019-07-11 stsp
5632 43012d58 2019-07-14 stsp #ifndef PROFILE
5633 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5634 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5635 43012d58 2019-07-14 stsp err(1, "pledge");
5636 43012d58 2019-07-14 stsp #endif
5637 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5638 818c7501 2019-07-11 stsp usage_rebase();
5639 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5640 818c7501 2019-07-11 stsp if (argc != 0)
5641 818c7501 2019-07-11 stsp usage_rebase();
5642 818c7501 2019-07-11 stsp } else if (argc != 1)
5643 818c7501 2019-07-11 stsp usage_rebase();
5644 818c7501 2019-07-11 stsp
5645 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5646 818c7501 2019-07-11 stsp if (cwd == NULL) {
5647 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5648 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp }
5650 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5651 818c7501 2019-07-11 stsp if (error)
5652 818c7501 2019-07-11 stsp goto done;
5653 818c7501 2019-07-11 stsp
5654 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5655 c9956ddf 2019-09-08 stsp NULL);
5656 818c7501 2019-07-11 stsp if (error != NULL)
5657 818c7501 2019-07-11 stsp goto done;
5658 818c7501 2019-07-11 stsp
5659 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5660 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5661 7ef62c4e 2020-02-24 stsp if (error)
5662 7ef62c4e 2020-02-24 stsp goto done;
5663 7ef62c4e 2020-02-24 stsp
5664 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5665 7ef62c4e 2020-02-24 stsp worktree);
5666 818c7501 2019-07-11 stsp if (error)
5667 7ef62c4e 2020-02-24 stsp goto done;
5668 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5669 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5670 818c7501 2019-07-11 stsp goto done;
5671 7ef62c4e 2020-02-24 stsp }
5672 818c7501 2019-07-11 stsp
5673 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5674 818c7501 2019-07-11 stsp if (error)
5675 818c7501 2019-07-11 stsp goto done;
5676 818c7501 2019-07-11 stsp
5677 f6794adc 2019-07-23 stsp if (abort_rebase) {
5678 818c7501 2019-07-11 stsp int did_something;
5679 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5680 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5681 f6794adc 2019-07-23 stsp goto done;
5682 f6794adc 2019-07-23 stsp }
5683 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5684 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5685 3e3a69f1 2019-07-25 stsp worktree, repo);
5686 818c7501 2019-07-11 stsp if (error)
5687 818c7501 2019-07-11 stsp goto done;
5688 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5689 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5690 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5691 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5692 818c7501 2019-07-11 stsp if (error)
5693 818c7501 2019-07-11 stsp goto done;
5694 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5695 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5696 818c7501 2019-07-11 stsp }
5697 818c7501 2019-07-11 stsp
5698 818c7501 2019-07-11 stsp if (continue_rebase) {
5699 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5700 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5701 f6794adc 2019-07-23 stsp goto done;
5702 f6794adc 2019-07-23 stsp }
5703 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5704 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5705 3e3a69f1 2019-07-25 stsp worktree, repo);
5706 818c7501 2019-07-11 stsp if (error)
5707 818c7501 2019-07-11 stsp goto done;
5708 818c7501 2019-07-11 stsp
5709 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5710 01757395 2019-07-12 stsp resume_commit_id, repo);
5711 818c7501 2019-07-11 stsp if (error)
5712 818c7501 2019-07-11 stsp goto done;
5713 818c7501 2019-07-11 stsp
5714 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5715 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5716 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5717 818c7501 2019-07-11 stsp goto done;
5718 818c7501 2019-07-11 stsp }
5719 818c7501 2019-07-11 stsp } else {
5720 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5721 818c7501 2019-07-11 stsp if (error != NULL)
5722 818c7501 2019-07-11 stsp goto done;
5723 ff0d2220 2019-07-11 stsp }
5724 818c7501 2019-07-11 stsp
5725 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5726 ff0d2220 2019-07-11 stsp if (error)
5727 ff0d2220 2019-07-11 stsp goto done;
5728 ff0d2220 2019-07-11 stsp
5729 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5730 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5731 a51a74b3 2019-07-27 stsp
5732 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5733 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5734 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5735 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5736 818c7501 2019-07-11 stsp if (error)
5737 818c7501 2019-07-11 stsp goto done;
5738 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5739 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5740 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5741 818c7501 2019-07-11 stsp "with work tree's branch");
5742 818c7501 2019-07-11 stsp goto done;
5743 818c7501 2019-07-11 stsp }
5744 818c7501 2019-07-11 stsp
5745 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5746 a51a74b3 2019-07-27 stsp if (error) {
5747 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5748 a51a74b3 2019-07-27 stsp goto done;
5749 a51a74b3 2019-07-27 stsp error = NULL;
5750 a51a74b3 2019-07-27 stsp } else {
5751 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5752 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5753 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5754 a51a74b3 2019-07-27 stsp goto done;
5755 a51a74b3 2019-07-27 stsp }
5756 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5757 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5758 818c7501 2019-07-11 stsp if (error)
5759 818c7501 2019-07-11 stsp goto done;
5760 818c7501 2019-07-11 stsp }
5761 818c7501 2019-07-11 stsp
5762 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5763 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5764 818c7501 2019-07-11 stsp if (error)
5765 818c7501 2019-07-11 stsp goto done;
5766 818c7501 2019-07-11 stsp
5767 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5768 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5769 fc66b545 2019-08-12 stsp if (pid == NULL) {
5770 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5771 fc66b545 2019-08-12 stsp int did_something;
5772 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5773 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5774 fc66b545 2019-08-12 stsp &did_something);
5775 fc66b545 2019-08-12 stsp if (error)
5776 fc66b545 2019-08-12 stsp goto done;
5777 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5778 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5779 fc66b545 2019-08-12 stsp }
5780 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5781 fc66b545 2019-08-12 stsp goto done;
5782 fc66b545 2019-08-12 stsp }
5783 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5784 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5785 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5786 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5787 818c7501 2019-07-11 stsp commit = NULL;
5788 818c7501 2019-07-11 stsp if (error)
5789 818c7501 2019-07-11 stsp goto done;
5790 64c6d990 2019-07-11 stsp
5791 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5792 38b0338b 2019-11-29 stsp if (continue_rebase) {
5793 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5794 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5795 38b0338b 2019-11-29 stsp goto done;
5796 38b0338b 2019-11-29 stsp } else {
5797 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5798 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5799 38b0338b 2019-11-29 stsp char *id_str;
5800 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5801 38b0338b 2019-11-29 stsp new_base_branch);
5802 38b0338b 2019-11-29 stsp if (error)
5803 38b0338b 2019-11-29 stsp goto done;
5804 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5805 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5806 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5807 38b0338b 2019-11-29 stsp free(id_str);
5808 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5809 38b0338b 2019-11-29 stsp new_head_commit_id);
5810 38b0338b 2019-11-29 stsp if (error)
5811 38b0338b 2019-11-29 stsp goto done;
5812 38b0338b 2019-11-29 stsp }
5813 818c7501 2019-07-11 stsp }
5814 818c7501 2019-07-11 stsp
5815 818c7501 2019-07-11 stsp pid = NULL;
5816 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5817 818c7501 2019-07-11 stsp commit_id = qid->id;
5818 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5819 818c7501 2019-07-11 stsp pid = qid;
5820 818c7501 2019-07-11 stsp
5821 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5822 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5823 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5824 818c7501 2019-07-11 stsp if (error)
5825 818c7501 2019-07-11 stsp goto done;
5826 818c7501 2019-07-11 stsp
5827 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5828 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5829 a0ea4fc0 2020-02-28 stsp if (error)
5830 a0ea4fc0 2020-02-28 stsp goto done;
5831 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5832 818c7501 2019-07-11 stsp break;
5833 01757395 2019-07-12 stsp }
5834 818c7501 2019-07-11 stsp
5835 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5836 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5837 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5838 818c7501 2019-07-11 stsp if (error)
5839 818c7501 2019-07-11 stsp goto done;
5840 818c7501 2019-07-11 stsp }
5841 818c7501 2019-07-11 stsp
5842 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5843 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5844 818c7501 2019-07-11 stsp if (error)
5845 818c7501 2019-07-11 stsp goto done;
5846 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5847 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5848 818c7501 2019-07-11 stsp } else
5849 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5850 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5851 818c7501 2019-07-11 stsp done:
5852 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5853 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5854 818c7501 2019-07-11 stsp free(resume_commit_id);
5855 818c7501 2019-07-11 stsp free(yca_id);
5856 818c7501 2019-07-11 stsp if (commit)
5857 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5858 818c7501 2019-07-11 stsp if (branch)
5859 818c7501 2019-07-11 stsp got_ref_close(branch);
5860 818c7501 2019-07-11 stsp if (new_base_branch)
5861 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5862 818c7501 2019-07-11 stsp if (tmp_branch)
5863 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5864 5ef14e63 2019-06-02 stsp if (worktree)
5865 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5866 5ef14e63 2019-06-02 stsp if (repo)
5867 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5868 5ef14e63 2019-06-02 stsp return error;
5869 0ebf8283 2019-07-24 stsp }
5870 0ebf8283 2019-07-24 stsp
5871 0ebf8283 2019-07-24 stsp __dead static void
5872 0ebf8283 2019-07-24 stsp usage_histedit(void)
5873 0ebf8283 2019-07-24 stsp {
5874 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5875 0ebf8283 2019-07-24 stsp getprogname());
5876 0ebf8283 2019-07-24 stsp exit(1);
5877 0ebf8283 2019-07-24 stsp }
5878 0ebf8283 2019-07-24 stsp
5879 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5880 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5881 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5882 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5883 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5884 0ebf8283 2019-07-24 stsp
5885 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5886 0ebf8283 2019-07-24 stsp unsigned char code;
5887 0ebf8283 2019-07-24 stsp const char *name;
5888 0ebf8283 2019-07-24 stsp const char *desc;
5889 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5890 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5891 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5892 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5893 82997472 2020-01-29 stsp "be used" },
5894 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5895 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5896 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5897 0ebf8283 2019-07-24 stsp };
5898 0ebf8283 2019-07-24 stsp
5899 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5900 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5901 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5902 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5903 0ebf8283 2019-07-24 stsp char *logmsg;
5904 0ebf8283 2019-07-24 stsp };
5905 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5906 0ebf8283 2019-07-24 stsp
5907 0ebf8283 2019-07-24 stsp static const struct got_error *
5908 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5909 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5910 0ebf8283 2019-07-24 stsp {
5911 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5912 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5913 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5914 8138f3e1 2019-08-11 stsp int n;
5915 0ebf8283 2019-07-24 stsp
5916 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5917 0ebf8283 2019-07-24 stsp if (err)
5918 0ebf8283 2019-07-24 stsp goto done;
5919 0ebf8283 2019-07-24 stsp
5920 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5921 0ebf8283 2019-07-24 stsp if (err)
5922 0ebf8283 2019-07-24 stsp goto done;
5923 0ebf8283 2019-07-24 stsp
5924 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5925 0ebf8283 2019-07-24 stsp if (err)
5926 0ebf8283 2019-07-24 stsp goto done;
5927 0ebf8283 2019-07-24 stsp
5928 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5929 0ebf8283 2019-07-24 stsp if (n < 0)
5930 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5931 0ebf8283 2019-07-24 stsp done:
5932 0ebf8283 2019-07-24 stsp if (commit)
5933 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5934 0ebf8283 2019-07-24 stsp free(id_str);
5935 0ebf8283 2019-07-24 stsp free(logmsg);
5936 0ebf8283 2019-07-24 stsp return err;
5937 0ebf8283 2019-07-24 stsp }
5938 0ebf8283 2019-07-24 stsp
5939 0ebf8283 2019-07-24 stsp static const struct got_error *
5940 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5941 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5942 0ebf8283 2019-07-24 stsp {
5943 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5944 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5945 0ebf8283 2019-07-24 stsp
5946 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5947 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5948 0ebf8283 2019-07-24 stsp
5949 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5950 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5951 0ebf8283 2019-07-24 stsp f, repo);
5952 0ebf8283 2019-07-24 stsp if (err)
5953 0ebf8283 2019-07-24 stsp break;
5954 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5955 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5956 083957f4 2020-02-24 stsp if (n < 0) {
5957 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5958 083957f4 2020-02-24 stsp break;
5959 083957f4 2020-02-24 stsp }
5960 083957f4 2020-02-24 stsp }
5961 0ebf8283 2019-07-24 stsp }
5962 0ebf8283 2019-07-24 stsp
5963 0ebf8283 2019-07-24 stsp return err;
5964 0ebf8283 2019-07-24 stsp }
5965 0ebf8283 2019-07-24 stsp
5966 0ebf8283 2019-07-24 stsp static const struct got_error *
5967 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5968 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5969 0ebf8283 2019-07-24 stsp {
5970 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5971 0ebf8283 2019-07-24 stsp int n, i;
5972 514f2ffe 2020-01-29 stsp char *id_str;
5973 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5974 514f2ffe 2020-01-29 stsp
5975 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5976 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5977 514f2ffe 2020-01-29 stsp if (err)
5978 514f2ffe 2020-01-29 stsp return err;
5979 514f2ffe 2020-01-29 stsp
5980 514f2ffe 2020-01-29 stsp n = fprintf(f,
5981 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5982 514f2ffe 2020-01-29 stsp "# commit %s\n"
5983 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5984 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5985 514f2ffe 2020-01-29 stsp if (n < 0) {
5986 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5987 514f2ffe 2020-01-29 stsp goto done;
5988 514f2ffe 2020-01-29 stsp }
5989 0ebf8283 2019-07-24 stsp
5990 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5991 514f2ffe 2020-01-29 stsp if (n < 0) {
5992 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5993 514f2ffe 2020-01-29 stsp goto done;
5994 514f2ffe 2020-01-29 stsp }
5995 0ebf8283 2019-07-24 stsp
5996 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5997 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5998 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5999 0ebf8283 2019-07-24 stsp cmd->desc);
6000 0ebf8283 2019-07-24 stsp if (n < 0) {
6001 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6002 0ebf8283 2019-07-24 stsp break;
6003 0ebf8283 2019-07-24 stsp }
6004 0ebf8283 2019-07-24 stsp }
6005 514f2ffe 2020-01-29 stsp done:
6006 514f2ffe 2020-01-29 stsp free(id_str);
6007 0ebf8283 2019-07-24 stsp return err;
6008 0ebf8283 2019-07-24 stsp }
6009 0ebf8283 2019-07-24 stsp
6010 0ebf8283 2019-07-24 stsp static const struct got_error *
6011 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
6012 0ebf8283 2019-07-24 stsp {
6013 0ebf8283 2019-07-24 stsp static char msg[42];
6014 0ebf8283 2019-07-24 stsp int ret;
6015 0ebf8283 2019-07-24 stsp
6016 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
6017 0ebf8283 2019-07-24 stsp lineno);
6018 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
6019 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
6020 0ebf8283 2019-07-24 stsp
6021 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
6022 0ebf8283 2019-07-24 stsp }
6023 0ebf8283 2019-07-24 stsp
6024 0ebf8283 2019-07-24 stsp static const struct got_error *
6025 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
6026 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
6027 0ebf8283 2019-07-24 stsp {
6028 0ebf8283 2019-07-24 stsp const struct got_error *err;
6029 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
6030 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
6031 0ebf8283 2019-07-24 stsp
6032 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6033 0ebf8283 2019-07-24 stsp if (err)
6034 0ebf8283 2019-07-24 stsp return err;
6035 0ebf8283 2019-07-24 stsp
6036 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
6037 0ebf8283 2019-07-24 stsp if (err)
6038 0ebf8283 2019-07-24 stsp goto done;
6039 0ebf8283 2019-07-24 stsp
6040 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
6041 5943eee2 2019-08-13 stsp if (err)
6042 5943eee2 2019-08-13 stsp goto done;
6043 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
6044 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
6045 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
6046 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6047 0ebf8283 2019-07-24 stsp }
6048 0ebf8283 2019-07-24 stsp done:
6049 0ebf8283 2019-07-24 stsp if (folded_commit)
6050 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
6051 0ebf8283 2019-07-24 stsp free(id_str);
6052 5943eee2 2019-08-13 stsp free(folded_logmsg);
6053 0ebf8283 2019-07-24 stsp return err;
6054 0ebf8283 2019-07-24 stsp }
6055 0ebf8283 2019-07-24 stsp
6056 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
6057 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
6058 0ebf8283 2019-07-24 stsp {
6059 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
6060 0ebf8283 2019-07-24 stsp
6061 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
6062 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
6063 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
6064 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
6065 3f9de99f 2019-07-24 stsp folded = prev;
6066 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
6067 0ebf8283 2019-07-24 stsp }
6068 0ebf8283 2019-07-24 stsp
6069 0ebf8283 2019-07-24 stsp return folded;
6070 0ebf8283 2019-07-24 stsp }
6071 0ebf8283 2019-07-24 stsp
6072 0ebf8283 2019-07-24 stsp static const struct got_error *
6073 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
6074 0ebf8283 2019-07-24 stsp struct got_repository *repo)
6075 0ebf8283 2019-07-24 stsp {
6076 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
6077 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
6078 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6079 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6080 0ebf8283 2019-07-24 stsp int fd;
6081 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
6082 0ebf8283 2019-07-24 stsp
6083 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6084 0ebf8283 2019-07-24 stsp if (err)
6085 0ebf8283 2019-07-24 stsp return err;
6086 0ebf8283 2019-07-24 stsp
6087 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
6088 0ebf8283 2019-07-24 stsp if (folded) {
6089 0ebf8283 2019-07-24 stsp while (folded != hle) {
6090 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
6091 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6092 3f9de99f 2019-07-24 stsp continue;
6093 3f9de99f 2019-07-24 stsp }
6094 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
6095 0ebf8283 2019-07-24 stsp logmsg, repo);
6096 0ebf8283 2019-07-24 stsp if (err)
6097 0ebf8283 2019-07-24 stsp goto done;
6098 0ebf8283 2019-07-24 stsp free(logmsg);
6099 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6100 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
6101 0ebf8283 2019-07-24 stsp }
6102 0ebf8283 2019-07-24 stsp }
6103 0ebf8283 2019-07-24 stsp
6104 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6105 0ebf8283 2019-07-24 stsp if (err)
6106 0ebf8283 2019-07-24 stsp goto done;
6107 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
6108 5943eee2 2019-08-13 stsp if (err)
6109 5943eee2 2019-08-13 stsp goto done;
6110 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
6111 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
6112 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
6113 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
6114 0ebf8283 2019-07-24 stsp goto done;
6115 0ebf8283 2019-07-24 stsp }
6116 0ebf8283 2019-07-24 stsp free(logmsg);
6117 0ebf8283 2019-07-24 stsp logmsg = new_msg;
6118 0ebf8283 2019-07-24 stsp
6119 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
6120 0ebf8283 2019-07-24 stsp if (err)
6121 0ebf8283 2019-07-24 stsp goto done;
6122 0ebf8283 2019-07-24 stsp
6123 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
6124 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
6125 0ebf8283 2019-07-24 stsp if (err)
6126 0ebf8283 2019-07-24 stsp goto done;
6127 0ebf8283 2019-07-24 stsp
6128 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
6129 0ebf8283 2019-07-24 stsp close(fd);
6130 0ebf8283 2019-07-24 stsp
6131 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6132 0ebf8283 2019-07-24 stsp if (err)
6133 0ebf8283 2019-07-24 stsp goto done;
6134 0ebf8283 2019-07-24 stsp
6135 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
6136 0ebf8283 2019-07-24 stsp if (err) {
6137 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
6138 0ebf8283 2019-07-24 stsp goto done;
6139 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
6140 0ebf8283 2019-07-24 stsp }
6141 0ebf8283 2019-07-24 stsp done:
6142 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
6143 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
6144 0ebf8283 2019-07-24 stsp free(logmsg_path);
6145 0ebf8283 2019-07-24 stsp free(logmsg);
6146 5943eee2 2019-08-13 stsp free(orig_logmsg);
6147 0ebf8283 2019-07-24 stsp free(editor);
6148 0ebf8283 2019-07-24 stsp if (commit)
6149 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6150 0ebf8283 2019-07-24 stsp return err;
6151 5ef14e63 2019-06-02 stsp }
6152 0ebf8283 2019-07-24 stsp
6153 0ebf8283 2019-07-24 stsp static const struct got_error *
6154 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
6155 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
6156 0ebf8283 2019-07-24 stsp {
6157 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6158 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
6159 0ebf8283 2019-07-24 stsp size_t size;
6160 0ebf8283 2019-07-24 stsp ssize_t len;
6161 0ebf8283 2019-07-24 stsp int lineno = 0, i;
6162 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
6163 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
6164 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
6165 0ebf8283 2019-07-24 stsp
6166 0ebf8283 2019-07-24 stsp for (;;) {
6167 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
6168 0ebf8283 2019-07-24 stsp if (len == -1) {
6169 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
6170 0ebf8283 2019-07-24 stsp if (feof(f))
6171 0ebf8283 2019-07-24 stsp break;
6172 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
6173 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
6174 0ebf8283 2019-07-24 stsp break;
6175 0ebf8283 2019-07-24 stsp }
6176 0ebf8283 2019-07-24 stsp lineno++;
6177 0ebf8283 2019-07-24 stsp p = line;
6178 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6179 0ebf8283 2019-07-24 stsp p++;
6180 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6181 0ebf8283 2019-07-24 stsp free(line);
6182 0ebf8283 2019-07-24 stsp line = NULL;
6183 0ebf8283 2019-07-24 stsp continue;
6184 0ebf8283 2019-07-24 stsp }
6185 0ebf8283 2019-07-24 stsp cmd = NULL;
6186 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6187 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6188 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6189 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6190 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6191 0ebf8283 2019-07-24 stsp break;
6192 0ebf8283 2019-07-24 stsp }
6193 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6194 0ebf8283 2019-07-24 stsp p++;
6195 0ebf8283 2019-07-24 stsp break;
6196 0ebf8283 2019-07-24 stsp }
6197 0ebf8283 2019-07-24 stsp }
6198 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6199 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6200 0ebf8283 2019-07-24 stsp break;
6201 0ebf8283 2019-07-24 stsp }
6202 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6203 0ebf8283 2019-07-24 stsp p++;
6204 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6205 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6206 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6207 0ebf8283 2019-07-24 stsp break;
6208 0ebf8283 2019-07-24 stsp }
6209 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6210 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6211 0ebf8283 2019-07-24 stsp if (err)
6212 0ebf8283 2019-07-24 stsp break;
6213 0ebf8283 2019-07-24 stsp } else {
6214 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6215 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6216 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6217 0ebf8283 2019-07-24 stsp break;
6218 0ebf8283 2019-07-24 stsp }
6219 0ebf8283 2019-07-24 stsp }
6220 0ebf8283 2019-07-24 stsp free(line);
6221 0ebf8283 2019-07-24 stsp line = NULL;
6222 0ebf8283 2019-07-24 stsp continue;
6223 0ebf8283 2019-07-24 stsp } else {
6224 0ebf8283 2019-07-24 stsp end = p;
6225 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6226 0ebf8283 2019-07-24 stsp end++;
6227 0ebf8283 2019-07-24 stsp *end = '\0';
6228 0ebf8283 2019-07-24 stsp
6229 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6230 0ebf8283 2019-07-24 stsp if (err) {
6231 0ebf8283 2019-07-24 stsp /* override error code */
6232 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6233 0ebf8283 2019-07-24 stsp break;
6234 0ebf8283 2019-07-24 stsp }
6235 0ebf8283 2019-07-24 stsp }
6236 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6237 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6238 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6239 0ebf8283 2019-07-24 stsp break;
6240 0ebf8283 2019-07-24 stsp }
6241 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6242 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6243 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6244 0ebf8283 2019-07-24 stsp commit_id = NULL;
6245 0ebf8283 2019-07-24 stsp free(line);
6246 0ebf8283 2019-07-24 stsp line = NULL;
6247 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6248 0ebf8283 2019-07-24 stsp }
6249 0ebf8283 2019-07-24 stsp
6250 0ebf8283 2019-07-24 stsp free(line);
6251 0ebf8283 2019-07-24 stsp free(commit_id);
6252 0ebf8283 2019-07-24 stsp return err;
6253 0ebf8283 2019-07-24 stsp }
6254 0ebf8283 2019-07-24 stsp
6255 0ebf8283 2019-07-24 stsp static const struct got_error *
6256 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6257 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6258 bfce7f83 2019-07-27 stsp {
6259 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6260 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6261 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6262 5b87815e 2020-03-05 stsp static char msg[92];
6263 bfce7f83 2019-07-27 stsp char *id_str;
6264 bfce7f83 2019-07-27 stsp
6265 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6266 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6267 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6268 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6269 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6270 5b87815e 2020-03-05 stsp
6271 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6272 5b87815e 2020-03-05 stsp struct got_histedit_list_entry *hle2;
6273 5b87815e 2020-03-05 stsp TAILQ_FOREACH(hle2, histedit_cmds, entry) {
6274 5b87815e 2020-03-05 stsp if (hle == hle2)
6275 5b87815e 2020-03-05 stsp continue;
6276 5b87815e 2020-03-05 stsp if (got_object_id_cmp(hle->commit_id,
6277 5b87815e 2020-03-05 stsp hle2->commit_id) != 0)
6278 5b87815e 2020-03-05 stsp continue;
6279 5b87815e 2020-03-05 stsp err = got_object_id_str(&id_str, hle->commit_id);
6280 5b87815e 2020-03-05 stsp if (err)
6281 5b87815e 2020-03-05 stsp return err;
6282 5b87815e 2020-03-05 stsp snprintf(msg, sizeof(msg), "commit %s is listed "
6283 5b87815e 2020-03-05 stsp "more than once in histedit script", id_str);
6284 5b87815e 2020-03-05 stsp free(id_str);
6285 5b87815e 2020-03-05 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6286 5b87815e 2020-03-05 stsp }
6287 5b87815e 2020-03-05 stsp }
6288 bfce7f83 2019-07-27 stsp
6289 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6290 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6291 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6292 bfce7f83 2019-07-27 stsp break;
6293 bfce7f83 2019-07-27 stsp }
6294 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6295 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6296 bfce7f83 2019-07-27 stsp if (err)
6297 bfce7f83 2019-07-27 stsp return err;
6298 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6299 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6300 bfce7f83 2019-07-27 stsp free(id_str);
6301 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6302 bfce7f83 2019-07-27 stsp }
6303 bfce7f83 2019-07-27 stsp }
6304 bfce7f83 2019-07-27 stsp
6305 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6306 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6307 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6308 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6309 bfce7f83 2019-07-27 stsp
6310 bfce7f83 2019-07-27 stsp return NULL;
6311 bfce7f83 2019-07-27 stsp }
6312 bfce7f83 2019-07-27 stsp
6313 bfce7f83 2019-07-27 stsp static const struct got_error *
6314 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6315 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6316 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6317 0ebf8283 2019-07-24 stsp {
6318 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6319 0ebf8283 2019-07-24 stsp char *editor;
6320 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6321 0ebf8283 2019-07-24 stsp
6322 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6323 0ebf8283 2019-07-24 stsp if (err)
6324 0ebf8283 2019-07-24 stsp return err;
6325 0ebf8283 2019-07-24 stsp
6326 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6327 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6328 0ebf8283 2019-07-24 stsp goto done;
6329 0ebf8283 2019-07-24 stsp }
6330 0ebf8283 2019-07-24 stsp
6331 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6332 0ebf8283 2019-07-24 stsp if (f == NULL) {
6333 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6334 0ebf8283 2019-07-24 stsp goto done;
6335 0ebf8283 2019-07-24 stsp }
6336 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6337 bfce7f83 2019-07-27 stsp if (err)
6338 bfce7f83 2019-07-27 stsp goto done;
6339 bfce7f83 2019-07-27 stsp
6340 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6341 0ebf8283 2019-07-24 stsp done:
6342 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6343 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6344 0ebf8283 2019-07-24 stsp free(editor);
6345 0ebf8283 2019-07-24 stsp return err;
6346 0ebf8283 2019-07-24 stsp }
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp static const struct got_error *
6349 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6350 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6351 514f2ffe 2020-01-29 stsp struct got_repository *);
6352 0ebf8283 2019-07-24 stsp
6353 0ebf8283 2019-07-24 stsp static const struct got_error *
6354 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6355 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6356 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6357 0ebf8283 2019-07-24 stsp {
6358 0ebf8283 2019-07-24 stsp const struct got_error *err;
6359 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6360 0ebf8283 2019-07-24 stsp char *path = NULL;
6361 0ebf8283 2019-07-24 stsp
6362 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6363 0ebf8283 2019-07-24 stsp if (err)
6364 0ebf8283 2019-07-24 stsp return err;
6365 0ebf8283 2019-07-24 stsp
6366 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6367 0ebf8283 2019-07-24 stsp if (err)
6368 0ebf8283 2019-07-24 stsp goto done;
6369 0ebf8283 2019-07-24 stsp
6370 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6371 0ebf8283 2019-07-24 stsp if (err)
6372 0ebf8283 2019-07-24 stsp goto done;
6373 0ebf8283 2019-07-24 stsp
6374 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6375 083957f4 2020-02-24 stsp rewind(f);
6376 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6377 083957f4 2020-02-24 stsp } else {
6378 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6379 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6380 0ebf8283 2019-07-24 stsp goto done;
6381 083957f4 2020-02-24 stsp }
6382 083957f4 2020-02-24 stsp f = NULL;
6383 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6384 083957f4 2020-02-24 stsp if (err) {
6385 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6386 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6387 083957f4 2020-02-24 stsp goto done;
6388 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6389 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6390 083957f4 2020-02-24 stsp }
6391 0ebf8283 2019-07-24 stsp }
6392 0ebf8283 2019-07-24 stsp done:
6393 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6394 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6395 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6396 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6397 0ebf8283 2019-07-24 stsp free(path);
6398 0ebf8283 2019-07-24 stsp return err;
6399 0ebf8283 2019-07-24 stsp }
6400 0ebf8283 2019-07-24 stsp
6401 0ebf8283 2019-07-24 stsp static const struct got_error *
6402 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6403 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6404 0ebf8283 2019-07-24 stsp {
6405 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6406 0ebf8283 2019-07-24 stsp char *path = NULL;
6407 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6408 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6409 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6410 0ebf8283 2019-07-24 stsp
6411 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6412 0ebf8283 2019-07-24 stsp if (err)
6413 0ebf8283 2019-07-24 stsp return err;
6414 0ebf8283 2019-07-24 stsp
6415 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6416 0ebf8283 2019-07-24 stsp if (f == NULL) {
6417 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6418 0ebf8283 2019-07-24 stsp goto done;
6419 0ebf8283 2019-07-24 stsp }
6420 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6421 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6422 0ebf8283 2019-07-24 stsp repo);
6423 0ebf8283 2019-07-24 stsp if (err)
6424 0ebf8283 2019-07-24 stsp break;
6425 0ebf8283 2019-07-24 stsp
6426 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6427 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6428 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6429 0ebf8283 2019-07-24 stsp if (n < 0) {
6430 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6431 0ebf8283 2019-07-24 stsp break;
6432 0ebf8283 2019-07-24 stsp }
6433 0ebf8283 2019-07-24 stsp }
6434 0ebf8283 2019-07-24 stsp }
6435 0ebf8283 2019-07-24 stsp done:
6436 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6437 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6438 0ebf8283 2019-07-24 stsp free(path);
6439 0ebf8283 2019-07-24 stsp if (commit)
6440 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6441 0ebf8283 2019-07-24 stsp return err;
6442 0ebf8283 2019-07-24 stsp }
6443 0ebf8283 2019-07-24 stsp
6444 bfce7f83 2019-07-27 stsp void
6445 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6446 bfce7f83 2019-07-27 stsp {
6447 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6448 bfce7f83 2019-07-27 stsp
6449 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6450 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6451 bfce7f83 2019-07-27 stsp free(hle);
6452 bfce7f83 2019-07-27 stsp }
6453 bfce7f83 2019-07-27 stsp }
6454 bfce7f83 2019-07-27 stsp
6455 0ebf8283 2019-07-24 stsp static const struct got_error *
6456 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6457 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6458 0ebf8283 2019-07-24 stsp {
6459 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6460 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6461 0ebf8283 2019-07-24 stsp
6462 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6463 0ebf8283 2019-07-24 stsp if (f == NULL) {
6464 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6465 0ebf8283 2019-07-24 stsp goto done;
6466 0ebf8283 2019-07-24 stsp }
6467 0ebf8283 2019-07-24 stsp
6468 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6469 0ebf8283 2019-07-24 stsp done:
6470 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6471 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6472 0ebf8283 2019-07-24 stsp return err;
6473 0ebf8283 2019-07-24 stsp }
6474 0ebf8283 2019-07-24 stsp
6475 0ebf8283 2019-07-24 stsp static const struct got_error *
6476 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6477 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6478 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6479 0ebf8283 2019-07-24 stsp {
6480 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6481 0ebf8283 2019-07-24 stsp int resp = ' ';
6482 0ebf8283 2019-07-24 stsp
6483 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6484 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6485 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6486 0ebf8283 2019-07-24 stsp resp = getchar();
6487 a61a4414 2019-08-07 stsp if (resp == '\n')
6488 a61a4414 2019-08-07 stsp resp = getchar();
6489 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6490 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6491 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6492 bfce7f83 2019-07-27 stsp repo);
6493 426ebf2e 2019-07-25 stsp if (err) {
6494 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6495 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6496 426ebf2e 2019-07-25 stsp break;
6497 bfce7f83 2019-07-27 stsp prev_err = err;
6498 426ebf2e 2019-07-25 stsp resp = ' ';
6499 426ebf2e 2019-07-25 stsp continue;
6500 426ebf2e 2019-07-25 stsp }
6501 bfce7f83 2019-07-27 stsp break;
6502 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6503 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6504 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6505 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6506 426ebf2e 2019-07-25 stsp if (err) {
6507 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6508 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6509 426ebf2e 2019-07-25 stsp break;
6510 bfce7f83 2019-07-27 stsp prev_err = err;
6511 426ebf2e 2019-07-25 stsp resp = ' ';
6512 426ebf2e 2019-07-25 stsp continue;
6513 426ebf2e 2019-07-25 stsp }
6514 bfce7f83 2019-07-27 stsp break;
6515 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6516 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6517 0ebf8283 2019-07-24 stsp break;
6518 426ebf2e 2019-07-25 stsp } else
6519 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6520 0ebf8283 2019-07-24 stsp }
6521 0ebf8283 2019-07-24 stsp
6522 0ebf8283 2019-07-24 stsp return err;
6523 0ebf8283 2019-07-24 stsp }
6524 0ebf8283 2019-07-24 stsp
6525 0ebf8283 2019-07-24 stsp static const struct got_error *
6526 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6527 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6528 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6529 0ebf8283 2019-07-24 stsp {
6530 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6531 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6532 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6533 3e3a69f1 2019-07-25 stsp branch, repo);
6534 0ebf8283 2019-07-24 stsp }
6535 0ebf8283 2019-07-24 stsp
6536 0ebf8283 2019-07-24 stsp static const struct got_error *
6537 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6538 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6539 0ebf8283 2019-07-24 stsp {
6540 0ebf8283 2019-07-24 stsp const struct got_error *err;
6541 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6542 0ebf8283 2019-07-24 stsp
6543 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6544 0ebf8283 2019-07-24 stsp if (err)
6545 0ebf8283 2019-07-24 stsp goto done;
6546 0ebf8283 2019-07-24 stsp
6547 0ebf8283 2019-07-24 stsp if (new_id) {
6548 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6549 0ebf8283 2019-07-24 stsp if (err)
6550 0ebf8283 2019-07-24 stsp goto done;
6551 0ebf8283 2019-07-24 stsp }
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6554 0ebf8283 2019-07-24 stsp if (new_id_str)
6555 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6556 0ebf8283 2019-07-24 stsp
6557 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6558 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6559 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6560 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6561 0ebf8283 2019-07-24 stsp goto done;
6562 0ebf8283 2019-07-24 stsp }
6563 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6564 0ebf8283 2019-07-24 stsp } else {
6565 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6566 0ebf8283 2019-07-24 stsp if (err)
6567 0ebf8283 2019-07-24 stsp goto done;
6568 0ebf8283 2019-07-24 stsp }
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6571 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6572 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6573 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6574 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6575 0ebf8283 2019-07-24 stsp break;
6576 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6577 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6578 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6579 0ebf8283 2019-07-24 stsp logmsg);
6580 0ebf8283 2019-07-24 stsp break;
6581 0ebf8283 2019-07-24 stsp default:
6582 0ebf8283 2019-07-24 stsp break;
6583 0ebf8283 2019-07-24 stsp }
6584 0ebf8283 2019-07-24 stsp done:
6585 0ebf8283 2019-07-24 stsp free(old_id_str);
6586 0ebf8283 2019-07-24 stsp free(new_id_str);
6587 0ebf8283 2019-07-24 stsp return err;
6588 0ebf8283 2019-07-24 stsp }
6589 0ebf8283 2019-07-24 stsp
6590 0ebf8283 2019-07-24 stsp static const struct got_error *
6591 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6592 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6593 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6594 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6595 0ebf8283 2019-07-24 stsp {
6596 0ebf8283 2019-07-24 stsp const struct got_error *err;
6597 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6598 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6599 0ebf8283 2019-07-24 stsp
6600 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6601 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6602 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6603 0ebf8283 2019-07-24 stsp if (err)
6604 0ebf8283 2019-07-24 stsp return err;
6605 0ebf8283 2019-07-24 stsp }
6606 0ebf8283 2019-07-24 stsp
6607 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6608 0ebf8283 2019-07-24 stsp if (err)
6609 0ebf8283 2019-07-24 stsp return err;
6610 0ebf8283 2019-07-24 stsp
6611 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6612 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6613 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6614 0ebf8283 2019-07-24 stsp if (err) {
6615 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6616 0ebf8283 2019-07-24 stsp goto done;
6617 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6618 0ebf8283 2019-07-24 stsp } else {
6619 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6620 0ebf8283 2019-07-24 stsp free(new_commit_id);
6621 0ebf8283 2019-07-24 stsp }
6622 0ebf8283 2019-07-24 stsp done:
6623 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6624 0ebf8283 2019-07-24 stsp return err;
6625 0ebf8283 2019-07-24 stsp }
6626 0ebf8283 2019-07-24 stsp
6627 0ebf8283 2019-07-24 stsp static const struct got_error *
6628 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6629 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6630 0ebf8283 2019-07-24 stsp {
6631 0ebf8283 2019-07-24 stsp const struct got_error *error;
6632 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6635 0ebf8283 2019-07-24 stsp repo);
6636 0ebf8283 2019-07-24 stsp if (error)
6637 0ebf8283 2019-07-24 stsp return error;
6638 0ebf8283 2019-07-24 stsp
6639 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6640 0ebf8283 2019-07-24 stsp if (error)
6641 0ebf8283 2019-07-24 stsp return error;
6642 0ebf8283 2019-07-24 stsp
6643 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6644 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6645 0ebf8283 2019-07-24 stsp return error;
6646 ab20a43a 2020-01-29 stsp }
6647 ab20a43a 2020-01-29 stsp
6648 ab20a43a 2020-01-29 stsp static const struct got_error *
6649 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6650 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6651 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6652 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6653 ab20a43a 2020-01-29 stsp {
6654 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6655 ab20a43a 2020-01-29 stsp
6656 ab20a43a 2020-01-29 stsp switch (status) {
6657 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6658 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6659 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6660 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6661 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6662 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6663 ab20a43a 2020-01-29 stsp default:
6664 ab20a43a 2020-01-29 stsp break;
6665 ab20a43a 2020-01-29 stsp }
6666 ab20a43a 2020-01-29 stsp
6667 ab20a43a 2020-01-29 stsp switch (staged_status) {
6668 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6669 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6670 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6671 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6672 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6673 ab20a43a 2020-01-29 stsp default:
6674 ab20a43a 2020-01-29 stsp break;
6675 ab20a43a 2020-01-29 stsp }
6676 ab20a43a 2020-01-29 stsp
6677 ab20a43a 2020-01-29 stsp return NULL;
6678 0ebf8283 2019-07-24 stsp }
6679 0ebf8283 2019-07-24 stsp
6680 0ebf8283 2019-07-24 stsp static const struct got_error *
6681 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6682 0ebf8283 2019-07-24 stsp {
6683 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6684 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6685 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6686 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6687 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6688 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6689 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6690 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6691 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6692 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6693 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6694 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6695 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6696 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6697 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6698 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6699 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6700 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6701 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6702 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6703 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6704 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6705 0ebf8283 2019-07-24 stsp
6706 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6707 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6708 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6709 0ebf8283 2019-07-24 stsp
6710 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6711 0ebf8283 2019-07-24 stsp switch (ch) {
6712 0ebf8283 2019-07-24 stsp case 'a':
6713 0ebf8283 2019-07-24 stsp abort_edit = 1;
6714 0ebf8283 2019-07-24 stsp break;
6715 0ebf8283 2019-07-24 stsp case 'c':
6716 0ebf8283 2019-07-24 stsp continue_edit = 1;
6717 0ebf8283 2019-07-24 stsp break;
6718 0ebf8283 2019-07-24 stsp case 'F':
6719 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6720 0ebf8283 2019-07-24 stsp break;
6721 083957f4 2020-02-24 stsp case 'm':
6722 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6723 083957f4 2020-02-24 stsp break;
6724 0ebf8283 2019-07-24 stsp default:
6725 0ebf8283 2019-07-24 stsp usage_histedit();
6726 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6727 0ebf8283 2019-07-24 stsp }
6728 0ebf8283 2019-07-24 stsp }
6729 0ebf8283 2019-07-24 stsp
6730 0ebf8283 2019-07-24 stsp argc -= optind;
6731 0ebf8283 2019-07-24 stsp argv += optind;
6732 0ebf8283 2019-07-24 stsp
6733 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6734 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6735 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6736 0ebf8283 2019-07-24 stsp err(1, "pledge");
6737 0ebf8283 2019-07-24 stsp #endif
6738 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6739 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6740 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6741 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6742 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6743 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6744 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6745 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6746 0ebf8283 2019-07-24 stsp if (argc != 0)
6747 0ebf8283 2019-07-24 stsp usage_histedit();
6748 0ebf8283 2019-07-24 stsp
6749 0ebf8283 2019-07-24 stsp /*
6750 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6751 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6752 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6753 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6754 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6755 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6756 0ebf8283 2019-07-24 stsp */
6757 0ebf8283 2019-07-24 stsp
6758 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6759 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6760 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6761 0ebf8283 2019-07-24 stsp goto done;
6762 0ebf8283 2019-07-24 stsp }
6763 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6764 0ebf8283 2019-07-24 stsp if (error)
6765 0ebf8283 2019-07-24 stsp goto done;
6766 0ebf8283 2019-07-24 stsp
6767 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6768 c9956ddf 2019-09-08 stsp NULL);
6769 0ebf8283 2019-07-24 stsp if (error != NULL)
6770 0ebf8283 2019-07-24 stsp goto done;
6771 0ebf8283 2019-07-24 stsp
6772 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6773 0ebf8283 2019-07-24 stsp if (error)
6774 0ebf8283 2019-07-24 stsp goto done;
6775 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6776 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6777 0ebf8283 2019-07-24 stsp goto done;
6778 0ebf8283 2019-07-24 stsp }
6779 0ebf8283 2019-07-24 stsp
6780 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6781 0ebf8283 2019-07-24 stsp if (error)
6782 0ebf8283 2019-07-24 stsp goto done;
6783 0ebf8283 2019-07-24 stsp
6784 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6785 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6786 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6787 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6788 083957f4 2020-02-24 stsp "before the -m option can be used");
6789 083957f4 2020-02-24 stsp goto done;
6790 083957f4 2020-02-24 stsp }
6791 083957f4 2020-02-24 stsp
6792 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6793 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6794 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6795 3e3a69f1 2019-07-25 stsp worktree, repo);
6796 0ebf8283 2019-07-24 stsp if (error)
6797 0ebf8283 2019-07-24 stsp goto done;
6798 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6799 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6800 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6801 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6802 0ebf8283 2019-07-24 stsp if (error)
6803 0ebf8283 2019-07-24 stsp goto done;
6804 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6805 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6806 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6807 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6808 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6809 0ebf8283 2019-07-24 stsp goto done;
6810 0ebf8283 2019-07-24 stsp }
6811 0ebf8283 2019-07-24 stsp
6812 0ebf8283 2019-07-24 stsp if (continue_edit) {
6813 0ebf8283 2019-07-24 stsp char *path;
6814 0ebf8283 2019-07-24 stsp
6815 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6816 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6817 0ebf8283 2019-07-24 stsp goto done;
6818 0ebf8283 2019-07-24 stsp }
6819 0ebf8283 2019-07-24 stsp
6820 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6821 0ebf8283 2019-07-24 stsp if (error)
6822 0ebf8283 2019-07-24 stsp goto done;
6823 0ebf8283 2019-07-24 stsp
6824 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6825 0ebf8283 2019-07-24 stsp free(path);
6826 0ebf8283 2019-07-24 stsp if (error)
6827 0ebf8283 2019-07-24 stsp goto done;
6828 0ebf8283 2019-07-24 stsp
6829 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6830 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6831 3e3a69f1 2019-07-25 stsp worktree, repo);
6832 0ebf8283 2019-07-24 stsp if (error)
6833 0ebf8283 2019-07-24 stsp goto done;
6834 0ebf8283 2019-07-24 stsp
6835 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6836 0ebf8283 2019-07-24 stsp if (error)
6837 0ebf8283 2019-07-24 stsp goto done;
6838 0ebf8283 2019-07-24 stsp
6839 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6840 0ebf8283 2019-07-24 stsp head_commit_id);
6841 0ebf8283 2019-07-24 stsp if (error)
6842 0ebf8283 2019-07-24 stsp goto done;
6843 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6844 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6845 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6846 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6847 3aac7cf7 2019-07-25 stsp goto done;
6848 3aac7cf7 2019-07-25 stsp }
6849 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6850 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6851 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6852 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6853 0ebf8283 2019-07-24 stsp commit = NULL;
6854 0ebf8283 2019-07-24 stsp if (error)
6855 0ebf8283 2019-07-24 stsp goto done;
6856 0ebf8283 2019-07-24 stsp } else {
6857 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6858 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6859 0ebf8283 2019-07-24 stsp goto done;
6860 0ebf8283 2019-07-24 stsp }
6861 0ebf8283 2019-07-24 stsp
6862 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6863 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6864 0ebf8283 2019-07-24 stsp if (error != NULL)
6865 0ebf8283 2019-07-24 stsp goto done;
6866 0ebf8283 2019-07-24 stsp
6867 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6868 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6869 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6870 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6871 c7d20a3f 2019-07-30 stsp goto done;
6872 c7d20a3f 2019-07-30 stsp }
6873 c7d20a3f 2019-07-30 stsp
6874 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6875 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6876 bfce7f83 2019-07-27 stsp branch = NULL;
6877 0ebf8283 2019-07-24 stsp if (error)
6878 0ebf8283 2019-07-24 stsp goto done;
6879 0ebf8283 2019-07-24 stsp
6880 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6881 0ebf8283 2019-07-24 stsp head_commit_id);
6882 0ebf8283 2019-07-24 stsp if (error)
6883 0ebf8283 2019-07-24 stsp goto done;
6884 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6885 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6886 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6887 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6888 3aac7cf7 2019-07-25 stsp goto done;
6889 3aac7cf7 2019-07-25 stsp }
6890 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6891 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6892 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6893 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6894 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6895 0ebf8283 2019-07-24 stsp commit = NULL;
6896 0ebf8283 2019-07-24 stsp if (error)
6897 0ebf8283 2019-07-24 stsp goto done;
6898 0ebf8283 2019-07-24 stsp
6899 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6900 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6901 c5996fff 2020-01-29 stsp goto done;
6902 c5996fff 2020-01-29 stsp }
6903 c5996fff 2020-01-29 stsp
6904 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6905 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6906 bfce7f83 2019-07-27 stsp if (error)
6907 bfce7f83 2019-07-27 stsp goto done;
6908 bfce7f83 2019-07-27 stsp
6909 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6910 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6911 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6912 6ba2bea2 2019-07-27 stsp if (error) {
6913 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6914 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6915 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6916 0ebf8283 2019-07-24 stsp goto done;
6917 6ba2bea2 2019-07-27 stsp }
6918 0ebf8283 2019-07-24 stsp } else {
6919 514f2ffe 2020-01-29 stsp const char *branch_name;
6920 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6921 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6922 514f2ffe 2020-01-29 stsp branch_name += 11;
6923 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6924 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6925 6ba2bea2 2019-07-27 stsp if (error) {
6926 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6927 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6928 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6929 0ebf8283 2019-07-24 stsp goto done;
6930 6ba2bea2 2019-07-27 stsp }
6931 0ebf8283 2019-07-24 stsp
6932 0ebf8283 2019-07-24 stsp }
6933 0ebf8283 2019-07-24 stsp
6934 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6935 0ebf8283 2019-07-24 stsp repo);
6936 6ba2bea2 2019-07-27 stsp if (error) {
6937 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6938 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6939 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6940 0ebf8283 2019-07-24 stsp goto done;
6941 6ba2bea2 2019-07-27 stsp }
6942 0ebf8283 2019-07-24 stsp
6943 0ebf8283 2019-07-24 stsp }
6944 0ebf8283 2019-07-24 stsp
6945 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6946 4ec14e60 2019-08-28 hiltjo if (error)
6947 0ebf8283 2019-07-24 stsp goto done;
6948 0ebf8283 2019-07-24 stsp
6949 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6950 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6951 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6952 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6953 0ebf8283 2019-07-24 stsp continue;
6954 0ebf8283 2019-07-24 stsp
6955 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6956 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6957 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6958 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6959 0ebf8283 2019-07-24 stsp repo);
6960 ab20a43a 2020-01-29 stsp if (error)
6961 ab20a43a 2020-01-29 stsp goto done;
6962 0ebf8283 2019-07-24 stsp } else {
6963 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6964 ab20a43a 2020-01-29 stsp int have_changes = 0;
6965 ab20a43a 2020-01-29 stsp
6966 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6967 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6968 ab20a43a 2020-01-29 stsp if (error)
6969 ab20a43a 2020-01-29 stsp goto done;
6970 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6971 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6972 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6973 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6974 ab20a43a 2020-01-29 stsp if (error) {
6975 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6976 ab20a43a 2020-01-29 stsp goto done;
6977 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6978 ab20a43a 2020-01-29 stsp goto done;
6979 ab20a43a 2020-01-29 stsp }
6980 ab20a43a 2020-01-29 stsp if (have_changes) {
6981 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6982 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6983 ab20a43a 2020-01-29 stsp if (error)
6984 ab20a43a 2020-01-29 stsp goto done;
6985 ab20a43a 2020-01-29 stsp } else {
6986 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6987 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6988 ab20a43a 2020-01-29 stsp if (error)
6989 ab20a43a 2020-01-29 stsp goto done;
6990 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6991 ab20a43a 2020-01-29 stsp hle, NULL);
6992 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6993 ab20a43a 2020-01-29 stsp commit = NULL;
6994 ab20a43a 2020-01-29 stsp if (error)
6995 ab20a43a 2020-01-29 stsp goto done;
6996 ab20a43a 2020-01-29 stsp }
6997 0ebf8283 2019-07-24 stsp }
6998 0ebf8283 2019-07-24 stsp continue;
6999 0ebf8283 2019-07-24 stsp }
7000 0ebf8283 2019-07-24 stsp
7001 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
7002 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7003 0ebf8283 2019-07-24 stsp if (error)
7004 0ebf8283 2019-07-24 stsp goto done;
7005 0ebf8283 2019-07-24 stsp continue;
7006 0ebf8283 2019-07-24 stsp }
7007 0ebf8283 2019-07-24 stsp
7008 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
7009 0ebf8283 2019-07-24 stsp hle->commit_id);
7010 0ebf8283 2019-07-24 stsp if (error)
7011 0ebf8283 2019-07-24 stsp goto done;
7012 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7013 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
7014 0ebf8283 2019-07-24 stsp
7015 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
7016 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
7017 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
7018 0ebf8283 2019-07-24 stsp if (error)
7019 0ebf8283 2019-07-24 stsp goto done;
7020 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7021 0ebf8283 2019-07-24 stsp commit = NULL;
7022 0ebf8283 2019-07-24 stsp
7023 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7024 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
7025 a0ea4fc0 2020-02-28 stsp repo);
7026 a0ea4fc0 2020-02-28 stsp if (error)
7027 a0ea4fc0 2020-02-28 stsp goto done;
7028 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7029 0ebf8283 2019-07-24 stsp break;
7030 0ebf8283 2019-07-24 stsp }
7031 0ebf8283 2019-07-24 stsp
7032 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
7033 0ebf8283 2019-07-24 stsp char *id_str;
7034 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
7035 0ebf8283 2019-07-24 stsp if (error)
7036 0ebf8283 2019-07-24 stsp goto done;
7037 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
7038 0ebf8283 2019-07-24 stsp id_str);
7039 0ebf8283 2019-07-24 stsp free(id_str);
7040 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7041 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
7042 3e3a69f1 2019-07-25 stsp fileindex);
7043 0ebf8283 2019-07-24 stsp goto done;
7044 0ebf8283 2019-07-24 stsp }
7045 0ebf8283 2019-07-24 stsp
7046 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
7047 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
7048 0ebf8283 2019-07-24 stsp if (error)
7049 0ebf8283 2019-07-24 stsp goto done;
7050 0ebf8283 2019-07-24 stsp continue;
7051 0ebf8283 2019-07-24 stsp }
7052 0ebf8283 2019-07-24 stsp
7053 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
7054 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
7055 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
7056 0ebf8283 2019-07-24 stsp if (error)
7057 0ebf8283 2019-07-24 stsp goto done;
7058 0ebf8283 2019-07-24 stsp }
7059 0ebf8283 2019-07-24 stsp
7060 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
7061 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
7062 0ebf8283 2019-07-24 stsp if (error)
7063 0ebf8283 2019-07-24 stsp goto done;
7064 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
7065 4cb8f8f3 2020-03-05 stsp "conflicts must be resolved before histedit can continue");
7066 0ebf8283 2019-07-24 stsp } else
7067 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
7068 3e3a69f1 2019-07-25 stsp branch, repo);
7069 0ebf8283 2019-07-24 stsp done:
7070 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
7071 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
7072 0ebf8283 2019-07-24 stsp free(head_commit_id);
7073 0ebf8283 2019-07-24 stsp free(base_commit_id);
7074 0ebf8283 2019-07-24 stsp free(resume_commit_id);
7075 0ebf8283 2019-07-24 stsp if (commit)
7076 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
7077 0ebf8283 2019-07-24 stsp if (branch)
7078 0ebf8283 2019-07-24 stsp got_ref_close(branch);
7079 0ebf8283 2019-07-24 stsp if (tmp_branch)
7080 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
7081 0ebf8283 2019-07-24 stsp if (worktree)
7082 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
7083 2822a352 2019-10-15 stsp if (repo)
7084 2822a352 2019-10-15 stsp got_repo_close(repo);
7085 2822a352 2019-10-15 stsp return error;
7086 2822a352 2019-10-15 stsp }
7087 2822a352 2019-10-15 stsp
7088 2822a352 2019-10-15 stsp __dead static void
7089 2822a352 2019-10-15 stsp usage_integrate(void)
7090 2822a352 2019-10-15 stsp {
7091 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
7092 2822a352 2019-10-15 stsp exit(1);
7093 2822a352 2019-10-15 stsp }
7094 2822a352 2019-10-15 stsp
7095 2822a352 2019-10-15 stsp static const struct got_error *
7096 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
7097 2822a352 2019-10-15 stsp {
7098 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
7099 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
7100 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
7101 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
7102 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
7103 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
7104 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
7105 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
7106 2822a352 2019-10-15 stsp int ch, did_something = 0;
7107 2822a352 2019-10-15 stsp
7108 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
7109 2822a352 2019-10-15 stsp switch (ch) {
7110 2822a352 2019-10-15 stsp default:
7111 2822a352 2019-10-15 stsp usage_integrate();
7112 2822a352 2019-10-15 stsp /* NOTREACHED */
7113 2822a352 2019-10-15 stsp }
7114 2822a352 2019-10-15 stsp }
7115 2822a352 2019-10-15 stsp
7116 2822a352 2019-10-15 stsp argc -= optind;
7117 2822a352 2019-10-15 stsp argv += optind;
7118 2822a352 2019-10-15 stsp
7119 2822a352 2019-10-15 stsp if (argc != 1)
7120 2822a352 2019-10-15 stsp usage_integrate();
7121 2822a352 2019-10-15 stsp branch_arg = argv[0];
7122 2822a352 2019-10-15 stsp
7123 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7124 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
7125 2822a352 2019-10-15 stsp err(1, "pledge");
7126 2822a352 2019-10-15 stsp
7127 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
7128 2822a352 2019-10-15 stsp if (cwd == NULL) {
7129 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
7130 2822a352 2019-10-15 stsp goto done;
7131 2822a352 2019-10-15 stsp }
7132 2822a352 2019-10-15 stsp
7133 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
7134 2822a352 2019-10-15 stsp if (error)
7135 2822a352 2019-10-15 stsp goto done;
7136 2822a352 2019-10-15 stsp
7137 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
7138 2822a352 2019-10-15 stsp if (error)
7139 2822a352 2019-10-15 stsp goto done;
7140 2822a352 2019-10-15 stsp
7141 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7142 2822a352 2019-10-15 stsp NULL);
7143 2822a352 2019-10-15 stsp if (error != NULL)
7144 2822a352 2019-10-15 stsp goto done;
7145 2822a352 2019-10-15 stsp
7146 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7147 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
7148 2822a352 2019-10-15 stsp if (error)
7149 2822a352 2019-10-15 stsp goto done;
7150 2822a352 2019-10-15 stsp
7151 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
7152 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
7153 2822a352 2019-10-15 stsp goto done;
7154 2822a352 2019-10-15 stsp }
7155 2822a352 2019-10-15 stsp
7156 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
7157 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
7158 2822a352 2019-10-15 stsp if (error)
7159 2822a352 2019-10-15 stsp goto done;
7160 2822a352 2019-10-15 stsp
7161 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
7162 2822a352 2019-10-15 stsp if (refname == NULL) {
7163 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7164 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7165 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7166 2822a352 2019-10-15 stsp goto done;
7167 2822a352 2019-10-15 stsp }
7168 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
7169 2822a352 2019-10-15 stsp if (base_refname == NULL) {
7170 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
7171 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7172 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7173 2822a352 2019-10-15 stsp goto done;
7174 2822a352 2019-10-15 stsp }
7175 2822a352 2019-10-15 stsp
7176 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
7177 2822a352 2019-10-15 stsp if (error)
7178 2822a352 2019-10-15 stsp goto done;
7179 2822a352 2019-10-15 stsp
7180 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
7181 2822a352 2019-10-15 stsp if (error)
7182 2822a352 2019-10-15 stsp goto done;
7183 2822a352 2019-10-15 stsp
7184 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
7185 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
7186 2822a352 2019-10-15 stsp "specified branch has already been integrated");
7187 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7188 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7189 2822a352 2019-10-15 stsp goto done;
7190 2822a352 2019-10-15 stsp }
7191 2822a352 2019-10-15 stsp
7192 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
7193 2822a352 2019-10-15 stsp if (error) {
7194 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7195 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7196 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7197 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7198 2822a352 2019-10-15 stsp goto done;
7199 2822a352 2019-10-15 stsp }
7200 2822a352 2019-10-15 stsp
7201 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7202 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7203 2822a352 2019-10-15 stsp check_cancelled, NULL);
7204 2822a352 2019-10-15 stsp if (error)
7205 2822a352 2019-10-15 stsp goto done;
7206 2822a352 2019-10-15 stsp
7207 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7208 2822a352 2019-10-15 stsp done:
7209 715dc77e 2019-08-03 stsp if (repo)
7210 715dc77e 2019-08-03 stsp got_repo_close(repo);
7211 2822a352 2019-10-15 stsp if (worktree)
7212 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7213 2822a352 2019-10-15 stsp free(cwd);
7214 2822a352 2019-10-15 stsp free(base_commit_id);
7215 2822a352 2019-10-15 stsp free(commit_id);
7216 2822a352 2019-10-15 stsp free(refname);
7217 2822a352 2019-10-15 stsp free(base_refname);
7218 715dc77e 2019-08-03 stsp return error;
7219 715dc77e 2019-08-03 stsp }
7220 715dc77e 2019-08-03 stsp
7221 715dc77e 2019-08-03 stsp __dead static void
7222 715dc77e 2019-08-03 stsp usage_stage(void)
7223 715dc77e 2019-08-03 stsp {
7224 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7225 f3055044 2019-08-07 stsp "[file-path ...]\n",
7226 715dc77e 2019-08-03 stsp getprogname());
7227 715dc77e 2019-08-03 stsp exit(1);
7228 715dc77e 2019-08-03 stsp }
7229 715dc77e 2019-08-03 stsp
7230 715dc77e 2019-08-03 stsp static const struct got_error *
7231 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7232 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7233 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7234 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7235 408b4ebc 2019-08-03 stsp {
7236 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7237 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7238 408b4ebc 2019-08-03 stsp
7239 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7240 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7241 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7242 408b4ebc 2019-08-03 stsp return NULL;
7243 408b4ebc 2019-08-03 stsp
7244 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7245 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7246 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7247 408b4ebc 2019-08-03 stsp else
7248 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7249 408b4ebc 2019-08-03 stsp if (err)
7250 408b4ebc 2019-08-03 stsp return err;
7251 408b4ebc 2019-08-03 stsp
7252 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7253 408b4ebc 2019-08-03 stsp free(id_str);
7254 dc424a06 2019-08-07 stsp return NULL;
7255 dc424a06 2019-08-07 stsp }
7256 2e1f37b0 2019-08-08 stsp
7257 dc424a06 2019-08-07 stsp static const struct got_error *
7258 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7259 715dc77e 2019-08-03 stsp {
7260 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7261 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7262 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7263 715dc77e 2019-08-03 stsp char *cwd = NULL;
7264 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7265 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7266 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7267 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7268 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7269 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7270 715dc77e 2019-08-03 stsp
7271 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7272 715dc77e 2019-08-03 stsp
7273 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7274 715dc77e 2019-08-03 stsp switch (ch) {
7275 408b4ebc 2019-08-03 stsp case 'l':
7276 408b4ebc 2019-08-03 stsp list_stage = 1;
7277 408b4ebc 2019-08-03 stsp break;
7278 dc424a06 2019-08-07 stsp case 'p':
7279 dc424a06 2019-08-07 stsp pflag = 1;
7280 dc424a06 2019-08-07 stsp break;
7281 dc424a06 2019-08-07 stsp case 'F':
7282 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7283 dc424a06 2019-08-07 stsp break;
7284 715dc77e 2019-08-03 stsp default:
7285 715dc77e 2019-08-03 stsp usage_stage();
7286 715dc77e 2019-08-03 stsp /* NOTREACHED */
7287 715dc77e 2019-08-03 stsp }
7288 715dc77e 2019-08-03 stsp }
7289 715dc77e 2019-08-03 stsp
7290 715dc77e 2019-08-03 stsp argc -= optind;
7291 715dc77e 2019-08-03 stsp argv += optind;
7292 715dc77e 2019-08-03 stsp
7293 715dc77e 2019-08-03 stsp #ifndef PROFILE
7294 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7295 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7296 715dc77e 2019-08-03 stsp err(1, "pledge");
7297 715dc77e 2019-08-03 stsp #endif
7298 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7299 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7300 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7301 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7302 715dc77e 2019-08-03 stsp
7303 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7304 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7305 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7306 715dc77e 2019-08-03 stsp goto done;
7307 715dc77e 2019-08-03 stsp }
7308 715dc77e 2019-08-03 stsp
7309 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7310 715dc77e 2019-08-03 stsp if (error)
7311 715dc77e 2019-08-03 stsp goto done;
7312 715dc77e 2019-08-03 stsp
7313 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7314 c9956ddf 2019-09-08 stsp NULL);
7315 715dc77e 2019-08-03 stsp if (error != NULL)
7316 715dc77e 2019-08-03 stsp goto done;
7317 715dc77e 2019-08-03 stsp
7318 dc424a06 2019-08-07 stsp if (patch_script_path) {
7319 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7320 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7321 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7322 dc424a06 2019-08-07 stsp patch_script_path);
7323 dc424a06 2019-08-07 stsp goto done;
7324 dc424a06 2019-08-07 stsp }
7325 dc424a06 2019-08-07 stsp }
7326 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7327 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7328 715dc77e 2019-08-03 stsp if (error)
7329 715dc77e 2019-08-03 stsp goto done;
7330 715dc77e 2019-08-03 stsp
7331 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7332 6d022e97 2019-08-04 stsp if (error)
7333 6d022e97 2019-08-04 stsp goto done;
7334 715dc77e 2019-08-03 stsp
7335 6d022e97 2019-08-04 stsp if (list_stage)
7336 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7337 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7338 2e1f37b0 2019-08-08 stsp else {
7339 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7340 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7341 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7342 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7343 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7344 2e1f37b0 2019-08-08 stsp }
7345 ad493afc 2019-08-03 stsp done:
7346 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7347 2e1f37b0 2019-08-08 stsp error == NULL)
7348 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7349 ad493afc 2019-08-03 stsp if (repo)
7350 ad493afc 2019-08-03 stsp got_repo_close(repo);
7351 ad493afc 2019-08-03 stsp if (worktree)
7352 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7353 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7354 ad493afc 2019-08-03 stsp free((char *)pe->path);
7355 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7356 ad493afc 2019-08-03 stsp free(cwd);
7357 ad493afc 2019-08-03 stsp return error;
7358 ad493afc 2019-08-03 stsp }
7359 ad493afc 2019-08-03 stsp
7360 ad493afc 2019-08-03 stsp __dead static void
7361 ad493afc 2019-08-03 stsp usage_unstage(void)
7362 ad493afc 2019-08-03 stsp {
7363 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7364 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7365 ad493afc 2019-08-03 stsp getprogname());
7366 ad493afc 2019-08-03 stsp exit(1);
7367 ad493afc 2019-08-03 stsp }
7368 ad493afc 2019-08-03 stsp
7369 ad493afc 2019-08-03 stsp
7370 ad493afc 2019-08-03 stsp static const struct got_error *
7371 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7372 ad493afc 2019-08-03 stsp {
7373 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7374 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7375 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7376 ad493afc 2019-08-03 stsp char *cwd = NULL;
7377 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7378 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7379 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7380 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7381 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7382 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7383 ad493afc 2019-08-03 stsp
7384 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7385 ad493afc 2019-08-03 stsp
7386 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7387 ad493afc 2019-08-03 stsp switch (ch) {
7388 2e1f37b0 2019-08-08 stsp case 'p':
7389 2e1f37b0 2019-08-08 stsp pflag = 1;
7390 2e1f37b0 2019-08-08 stsp break;
7391 2e1f37b0 2019-08-08 stsp case 'F':
7392 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7393 2e1f37b0 2019-08-08 stsp break;
7394 ad493afc 2019-08-03 stsp default:
7395 ad493afc 2019-08-03 stsp usage_unstage();
7396 ad493afc 2019-08-03 stsp /* NOTREACHED */
7397 ad493afc 2019-08-03 stsp }
7398 ad493afc 2019-08-03 stsp }
7399 ad493afc 2019-08-03 stsp
7400 ad493afc 2019-08-03 stsp argc -= optind;
7401 ad493afc 2019-08-03 stsp argv += optind;
7402 ad493afc 2019-08-03 stsp
7403 ad493afc 2019-08-03 stsp #ifndef PROFILE
7404 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7405 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7406 ad493afc 2019-08-03 stsp err(1, "pledge");
7407 ad493afc 2019-08-03 stsp #endif
7408 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7409 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7410 2e1f37b0 2019-08-08 stsp
7411 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7412 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7413 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7414 ad493afc 2019-08-03 stsp goto done;
7415 ad493afc 2019-08-03 stsp }
7416 ad493afc 2019-08-03 stsp
7417 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7418 ad493afc 2019-08-03 stsp if (error)
7419 ad493afc 2019-08-03 stsp goto done;
7420 ad493afc 2019-08-03 stsp
7421 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7422 c9956ddf 2019-09-08 stsp NULL);
7423 ad493afc 2019-08-03 stsp if (error != NULL)
7424 ad493afc 2019-08-03 stsp goto done;
7425 ad493afc 2019-08-03 stsp
7426 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7427 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7428 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7429 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7430 2e1f37b0 2019-08-08 stsp patch_script_path);
7431 2e1f37b0 2019-08-08 stsp goto done;
7432 2e1f37b0 2019-08-08 stsp }
7433 2e1f37b0 2019-08-08 stsp }
7434 2e1f37b0 2019-08-08 stsp
7435 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7436 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7437 ad493afc 2019-08-03 stsp if (error)
7438 ad493afc 2019-08-03 stsp goto done;
7439 ad493afc 2019-08-03 stsp
7440 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7441 ad493afc 2019-08-03 stsp if (error)
7442 ad493afc 2019-08-03 stsp goto done;
7443 ad493afc 2019-08-03 stsp
7444 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7445 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7446 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7447 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7448 715dc77e 2019-08-03 stsp done:
7449 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7450 2e1f37b0 2019-08-08 stsp error == NULL)
7451 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7452 0ebf8283 2019-07-24 stsp if (repo)
7453 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7454 715dc77e 2019-08-03 stsp if (worktree)
7455 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7456 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7457 715dc77e 2019-08-03 stsp free((char *)pe->path);
7458 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7459 715dc77e 2019-08-03 stsp free(cwd);
7460 01073a5d 2019-08-22 stsp return error;
7461 01073a5d 2019-08-22 stsp }
7462 01073a5d 2019-08-22 stsp
7463 01073a5d 2019-08-22 stsp __dead static void
7464 01073a5d 2019-08-22 stsp usage_cat(void)
7465 01073a5d 2019-08-22 stsp {
7466 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7467 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7468 01073a5d 2019-08-22 stsp exit(1);
7469 01073a5d 2019-08-22 stsp }
7470 01073a5d 2019-08-22 stsp
7471 01073a5d 2019-08-22 stsp static const struct got_error *
7472 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7473 01073a5d 2019-08-22 stsp {
7474 01073a5d 2019-08-22 stsp const struct got_error *err;
7475 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7476 01073a5d 2019-08-22 stsp
7477 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7478 01073a5d 2019-08-22 stsp if (err)
7479 01073a5d 2019-08-22 stsp return err;
7480 01073a5d 2019-08-22 stsp
7481 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7482 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7483 01073a5d 2019-08-22 stsp return err;
7484 01073a5d 2019-08-22 stsp }
7485 01073a5d 2019-08-22 stsp
7486 01073a5d 2019-08-22 stsp static const struct got_error *
7487 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7488 01073a5d 2019-08-22 stsp {
7489 01073a5d 2019-08-22 stsp const struct got_error *err;
7490 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7491 56e0773d 2019-11-28 stsp int nentries, i;
7492 01073a5d 2019-08-22 stsp
7493 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7494 01073a5d 2019-08-22 stsp if (err)
7495 01073a5d 2019-08-22 stsp return err;
7496 01073a5d 2019-08-22 stsp
7497 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7498 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7499 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7500 01073a5d 2019-08-22 stsp char *id_str;
7501 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7502 01073a5d 2019-08-22 stsp break;
7503 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7504 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7505 01073a5d 2019-08-22 stsp if (err)
7506 01073a5d 2019-08-22 stsp break;
7507 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7508 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7509 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7510 01073a5d 2019-08-22 stsp free(id_str);
7511 01073a5d 2019-08-22 stsp }
7512 01073a5d 2019-08-22 stsp
7513 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7514 01073a5d 2019-08-22 stsp return err;
7515 01073a5d 2019-08-22 stsp }
7516 01073a5d 2019-08-22 stsp
7517 01073a5d 2019-08-22 stsp static const struct got_error *
7518 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7519 01073a5d 2019-08-22 stsp {
7520 01073a5d 2019-08-22 stsp const struct got_error *err;
7521 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7522 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7523 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7524 01073a5d 2019-08-22 stsp char *id_str = NULL;
7525 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7526 01073a5d 2019-08-22 stsp
7527 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7528 01073a5d 2019-08-22 stsp if (err)
7529 01073a5d 2019-08-22 stsp return err;
7530 01073a5d 2019-08-22 stsp
7531 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7532 01073a5d 2019-08-22 stsp if (err)
7533 01073a5d 2019-08-22 stsp goto done;
7534 01073a5d 2019-08-22 stsp
7535 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7536 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7537 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7538 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7539 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7540 01073a5d 2019-08-22 stsp char *pid_str;
7541 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7542 01073a5d 2019-08-22 stsp if (err)
7543 01073a5d 2019-08-22 stsp goto done;
7544 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7545 01073a5d 2019-08-22 stsp free(pid_str);
7546 01073a5d 2019-08-22 stsp }
7547 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7548 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7549 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7550 01073a5d 2019-08-22 stsp
7551 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7552 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7553 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7554 01073a5d 2019-08-22 stsp
7555 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7556 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7557 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7558 01073a5d 2019-08-22 stsp done:
7559 01073a5d 2019-08-22 stsp free(id_str);
7560 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7561 01073a5d 2019-08-22 stsp return err;
7562 01073a5d 2019-08-22 stsp }
7563 01073a5d 2019-08-22 stsp
7564 01073a5d 2019-08-22 stsp static const struct got_error *
7565 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7566 01073a5d 2019-08-22 stsp {
7567 01073a5d 2019-08-22 stsp const struct got_error *err;
7568 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7569 01073a5d 2019-08-22 stsp char *id_str = NULL;
7570 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7571 01073a5d 2019-08-22 stsp
7572 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7573 01073a5d 2019-08-22 stsp if (err)
7574 01073a5d 2019-08-22 stsp return err;
7575 01073a5d 2019-08-22 stsp
7576 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7577 01073a5d 2019-08-22 stsp if (err)
7578 01073a5d 2019-08-22 stsp goto done;
7579 01073a5d 2019-08-22 stsp
7580 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7581 e15d5241 2019-08-22 stsp
7582 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7583 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7584 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7585 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7586 01073a5d 2019-08-22 stsp break;
7587 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7588 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7589 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7590 01073a5d 2019-08-22 stsp break;
7591 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7592 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7593 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7594 01073a5d 2019-08-22 stsp break;
7595 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7596 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7597 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7598 01073a5d 2019-08-22 stsp break;
7599 01073a5d 2019-08-22 stsp default:
7600 01073a5d 2019-08-22 stsp break;
7601 01073a5d 2019-08-22 stsp }
7602 01073a5d 2019-08-22 stsp
7603 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7604 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7605 e15d5241 2019-08-22 stsp
7606 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7607 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7608 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7609 01073a5d 2019-08-22 stsp
7610 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7611 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7612 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7613 01073a5d 2019-08-22 stsp done:
7614 01073a5d 2019-08-22 stsp free(id_str);
7615 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7616 01073a5d 2019-08-22 stsp return err;
7617 01073a5d 2019-08-22 stsp }
7618 01073a5d 2019-08-22 stsp
7619 01073a5d 2019-08-22 stsp static const struct got_error *
7620 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7621 01073a5d 2019-08-22 stsp {
7622 01073a5d 2019-08-22 stsp const struct got_error *error;
7623 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7624 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7625 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7626 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7627 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7628 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7629 01073a5d 2019-08-22 stsp
7630 01073a5d 2019-08-22 stsp #ifndef PROFILE
7631 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7632 01073a5d 2019-08-22 stsp NULL) == -1)
7633 01073a5d 2019-08-22 stsp err(1, "pledge");
7634 01073a5d 2019-08-22 stsp #endif
7635 01073a5d 2019-08-22 stsp
7636 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7637 01073a5d 2019-08-22 stsp switch (ch) {
7638 896e9b6f 2019-08-26 stsp case 'c':
7639 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7640 896e9b6f 2019-08-26 stsp break;
7641 01073a5d 2019-08-22 stsp case 'r':
7642 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7643 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7644 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7645 9ba1d308 2019-10-21 stsp optarg);
7646 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7647 01073a5d 2019-08-22 stsp break;
7648 896e9b6f 2019-08-26 stsp case 'P':
7649 896e9b6f 2019-08-26 stsp force_path = 1;
7650 896e9b6f 2019-08-26 stsp break;
7651 01073a5d 2019-08-22 stsp default:
7652 01073a5d 2019-08-22 stsp usage_cat();
7653 01073a5d 2019-08-22 stsp /* NOTREACHED */
7654 01073a5d 2019-08-22 stsp }
7655 01073a5d 2019-08-22 stsp }
7656 01073a5d 2019-08-22 stsp
7657 01073a5d 2019-08-22 stsp argc -= optind;
7658 01073a5d 2019-08-22 stsp argv += optind;
7659 01073a5d 2019-08-22 stsp
7660 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7661 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7662 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7663 01073a5d 2019-08-22 stsp goto done;
7664 01073a5d 2019-08-22 stsp }
7665 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7666 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7667 01073a5d 2019-08-22 stsp goto done;
7668 01073a5d 2019-08-22 stsp if (worktree) {
7669 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7670 01073a5d 2019-08-22 stsp repo_path = strdup(
7671 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7672 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7673 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7674 01073a5d 2019-08-22 stsp goto done;
7675 01073a5d 2019-08-22 stsp }
7676 01073a5d 2019-08-22 stsp }
7677 01073a5d 2019-08-22 stsp }
7678 01073a5d 2019-08-22 stsp
7679 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7680 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7681 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7682 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7683 01073a5d 2019-08-22 stsp }
7684 01073a5d 2019-08-22 stsp
7685 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7686 01073a5d 2019-08-22 stsp free(repo_path);
7687 01073a5d 2019-08-22 stsp if (error != NULL)
7688 01073a5d 2019-08-22 stsp goto done;
7689 01073a5d 2019-08-22 stsp
7690 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7691 896e9b6f 2019-08-26 stsp if (error)
7692 896e9b6f 2019-08-26 stsp goto done;
7693 896e9b6f 2019-08-26 stsp
7694 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7695 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7696 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7697 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7698 01073a5d 2019-08-22 stsp if (error)
7699 01073a5d 2019-08-22 stsp goto done;
7700 01073a5d 2019-08-22 stsp
7701 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7702 896e9b6f 2019-08-26 stsp if (force_path) {
7703 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7704 896e9b6f 2019-08-26 stsp argv[i]);
7705 896e9b6f 2019-08-26 stsp if (error)
7706 896e9b6f 2019-08-26 stsp break;
7707 896e9b6f 2019-08-26 stsp } else {
7708 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7709 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7710 896e9b6f 2019-08-26 stsp if (error) {
7711 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7712 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7713 896e9b6f 2019-08-26 stsp break;
7714 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7715 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7716 896e9b6f 2019-08-26 stsp if (error)
7717 896e9b6f 2019-08-26 stsp break;
7718 896e9b6f 2019-08-26 stsp }
7719 896e9b6f 2019-08-26 stsp }
7720 01073a5d 2019-08-22 stsp
7721 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7722 01073a5d 2019-08-22 stsp if (error)
7723 01073a5d 2019-08-22 stsp break;
7724 01073a5d 2019-08-22 stsp
7725 01073a5d 2019-08-22 stsp switch (obj_type) {
7726 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7727 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7728 01073a5d 2019-08-22 stsp break;
7729 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7730 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7731 01073a5d 2019-08-22 stsp break;
7732 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7733 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7734 01073a5d 2019-08-22 stsp break;
7735 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7736 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7737 01073a5d 2019-08-22 stsp break;
7738 01073a5d 2019-08-22 stsp default:
7739 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7740 01073a5d 2019-08-22 stsp break;
7741 01073a5d 2019-08-22 stsp }
7742 01073a5d 2019-08-22 stsp if (error)
7743 01073a5d 2019-08-22 stsp break;
7744 01073a5d 2019-08-22 stsp free(label);
7745 01073a5d 2019-08-22 stsp label = NULL;
7746 01073a5d 2019-08-22 stsp free(id);
7747 01073a5d 2019-08-22 stsp id = NULL;
7748 01073a5d 2019-08-22 stsp }
7749 01073a5d 2019-08-22 stsp done:
7750 01073a5d 2019-08-22 stsp free(label);
7751 01073a5d 2019-08-22 stsp free(id);
7752 896e9b6f 2019-08-26 stsp free(commit_id);
7753 01073a5d 2019-08-22 stsp if (worktree)
7754 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7755 01073a5d 2019-08-22 stsp if (repo) {
7756 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7757 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7758 01073a5d 2019-08-22 stsp if (error == NULL)
7759 01073a5d 2019-08-22 stsp error = repo_error;
7760 01073a5d 2019-08-22 stsp }
7761 0ebf8283 2019-07-24 stsp return error;
7762 0ebf8283 2019-07-24 stsp }