Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
4 5c860e29 2018-03-12 stsp *
5 5c860e29 2018-03-12 stsp * Permission to use, copy, modify, and distribute this software for any
6 5c860e29 2018-03-12 stsp * purpose with or without fee is hereby granted, provided that the above
7 5c860e29 2018-03-12 stsp * copyright notice and this permission notice appear in all copies.
8 5c860e29 2018-03-12 stsp *
9 5c860e29 2018-03-12 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 5c860e29 2018-03-12 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 5c860e29 2018-03-12 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 5c860e29 2018-03-12 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 5c860e29 2018-03-12 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 5c860e29 2018-03-12 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 5c860e29 2018-03-12 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 5c860e29 2018-03-12 stsp */
17 5c860e29 2018-03-12 stsp
18 f42b1b34 2018-03-12 stsp #include <sys/queue.h>
19 64a96a6d 2018-04-01 stsp #include <sys/limits.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 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 5c860e29 2018-03-12 stsp
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 c09a553d 2018-03-12 stsp #include "got_worktree.h"
45 79109fed 2018-03-27 stsp #include "got_diff.h"
46 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
47 404c43c4 2018-06-21 stsp #include "got_blame.h"
48 63219cd2 2019-01-04 stsp #include "got_privsep.h"
49 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
50 5c860e29 2018-03-12 stsp
51 5c860e29 2018-03-12 stsp #ifndef nitems
52 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 5c860e29 2018-03-12 stsp #endif
54 99437157 2018-11-11 stsp
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
57 99437157 2018-11-11 stsp
58 99437157 2018-11-11 stsp static void
59 99437157 2018-11-11 stsp catch_sigint(int signo)
60 99437157 2018-11-11 stsp {
61 99437157 2018-11-11 stsp sigint_received = 1;
62 99437157 2018-11-11 stsp }
63 99437157 2018-11-11 stsp
64 99437157 2018-11-11 stsp static void
65 99437157 2018-11-11 stsp catch_sigpipe(int signo)
66 99437157 2018-11-11 stsp {
67 99437157 2018-11-11 stsp sigpipe_received = 1;
68 99437157 2018-11-11 stsp }
69 5c860e29 2018-03-12 stsp
70 99437157 2018-11-11 stsp
71 8cfb4057 2019-07-09 stsp struct got_cmd {
72 5c860e29 2018-03-12 stsp const char *cmd_name;
73 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
74 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
75 97b3a7be 2019-07-09 stsp const char *cmd_alias;
76 5c860e29 2018-03-12 stsp };
77 5c860e29 2018-03-12 stsp
78 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
79 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
80 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
81 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
82 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
85 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
86 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
87 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
88 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
89 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
90 d00136be 2019-03-26 stsp __dead static void usage_add(void);
91 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
92 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
93 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
94 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
95 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
96 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
97 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
98 5c860e29 2018-03-12 stsp
99 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
100 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
101 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
102 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
103 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
104 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
105 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
106 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
108 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
109 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
110 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
111 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
112 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
113 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
114 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
115 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
116 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
117 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
118 5c860e29 2018-03-12 stsp
119 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
120 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
121 3ce1b845 2019-07-15 stsp { "import", cmd_import, usage_import, "" },
122 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
123 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
124 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
125 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
126 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
127 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
128 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
129 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
130 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
131 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
132 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
133 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
134 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
135 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
136 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
137 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
138 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
139 5c860e29 2018-03-12 stsp };
140 ce5b7c56 2019-07-09 stsp
141 ce5b7c56 2019-07-09 stsp static void
142 ce5b7c56 2019-07-09 stsp list_commands(void)
143 ce5b7c56 2019-07-09 stsp {
144 ce5b7c56 2019-07-09 stsp int i;
145 ce5b7c56 2019-07-09 stsp
146 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
147 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
148 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
149 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
150 ce5b7c56 2019-07-09 stsp }
151 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
152 ce5b7c56 2019-07-09 stsp }
153 5c860e29 2018-03-12 stsp
154 5c860e29 2018-03-12 stsp int
155 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
156 5c860e29 2018-03-12 stsp {
157 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
158 5c860e29 2018-03-12 stsp unsigned int i;
159 5c860e29 2018-03-12 stsp int ch;
160 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
161 5c860e29 2018-03-12 stsp
162 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
163 5c860e29 2018-03-12 stsp
164 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
165 5c860e29 2018-03-12 stsp switch (ch) {
166 1b6b95a8 2018-03-12 stsp case 'h':
167 1b6b95a8 2018-03-12 stsp hflag = 1;
168 53ccebc2 2019-07-30 stsp break;
169 53ccebc2 2019-07-30 stsp case 'V':
170 53ccebc2 2019-07-30 stsp Vflag = 1;
171 1b6b95a8 2018-03-12 stsp break;
172 5c860e29 2018-03-12 stsp default:
173 ce5b7c56 2019-07-09 stsp usage(hflag);
174 5c860e29 2018-03-12 stsp /* NOTREACHED */
175 5c860e29 2018-03-12 stsp }
176 5c860e29 2018-03-12 stsp }
177 5c860e29 2018-03-12 stsp
178 5c860e29 2018-03-12 stsp argc -= optind;
179 5c860e29 2018-03-12 stsp argv += optind;
180 1e70621d 2018-03-27 stsp optind = 0;
181 53ccebc2 2019-07-30 stsp
182 53ccebc2 2019-07-30 stsp if (Vflag) {
183 53ccebc2 2019-07-30 stsp got_version_print_str();
184 53ccebc2 2019-07-30 stsp return 1;
185 53ccebc2 2019-07-30 stsp }
186 5c860e29 2018-03-12 stsp
187 5c860e29 2018-03-12 stsp if (argc <= 0)
188 ce5b7c56 2019-07-09 stsp usage(hflag);
189 5c860e29 2018-03-12 stsp
190 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
191 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
192 99437157 2018-11-11 stsp
193 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
194 d7d4f210 2018-03-12 stsp const struct got_error *error;
195 d7d4f210 2018-03-12 stsp
196 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
197 5c860e29 2018-03-12 stsp
198 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
199 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
200 5c860e29 2018-03-12 stsp continue;
201 5c860e29 2018-03-12 stsp
202 1b6b95a8 2018-03-12 stsp if (hflag)
203 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
204 1b6b95a8 2018-03-12 stsp
205 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
206 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
207 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
208 d7d4f210 2018-03-12 stsp return 1;
209 d7d4f210 2018-03-12 stsp }
210 d7d4f210 2018-03-12 stsp
211 d7d4f210 2018-03-12 stsp return 0;
212 5c860e29 2018-03-12 stsp }
213 5c860e29 2018-03-12 stsp
214 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
215 ce5b7c56 2019-07-09 stsp list_commands();
216 5c860e29 2018-03-12 stsp return 1;
217 5c860e29 2018-03-12 stsp }
218 5c860e29 2018-03-12 stsp
219 4ed7e80c 2018-05-20 stsp __dead static void
220 ce5b7c56 2019-07-09 stsp usage(int hflag)
221 5c860e29 2018-03-12 stsp {
222 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
223 53ccebc2 2019-07-30 stsp getprogname());
224 ce5b7c56 2019-07-09 stsp if (hflag)
225 ce5b7c56 2019-07-09 stsp list_commands();
226 5c860e29 2018-03-12 stsp exit(1);
227 5c860e29 2018-03-12 stsp }
228 5c860e29 2018-03-12 stsp
229 0266afb7 2019-01-04 stsp static const struct got_error *
230 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
231 e2ba3d07 2019-05-13 stsp {
232 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
233 e2ba3d07 2019-05-13 stsp const char *editor;
234 e2ba3d07 2019-05-13 stsp
235 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
236 e2ba3d07 2019-05-13 stsp if (editor == NULL)
237 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
238 e2ba3d07 2019-05-13 stsp
239 0ee7065d 2019-05-13 stsp if (editor) {
240 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
241 0ee7065d 2019-05-13 stsp if (err)
242 0ee7065d 2019-05-13 stsp return err;
243 0ee7065d 2019-05-13 stsp }
244 e2ba3d07 2019-05-13 stsp
245 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
246 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
247 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
248 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
249 0ee7065d 2019-05-13 stsp }
250 0ee7065d 2019-05-13 stsp
251 e2ba3d07 2019-05-13 stsp return NULL;
252 e2ba3d07 2019-05-13 stsp }
253 e2ba3d07 2019-05-13 stsp
254 e2ba3d07 2019-05-13 stsp static const struct got_error *
255 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
256 c530dc23 2019-07-23 stsp const char *worktree_path)
257 0266afb7 2019-01-04 stsp {
258 163ce85a 2019-05-13 stsp const struct got_error *err;
259 0266afb7 2019-01-04 stsp
260 37c06ea4 2019-07-15 stsp #ifdef PROFILE
261 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
262 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
263 37c06ea4 2019-07-15 stsp #endif
264 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
265 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
266 0266afb7 2019-01-04 stsp
267 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
268 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
269 0266afb7 2019-01-04 stsp
270 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
271 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
272 0266afb7 2019-01-04 stsp
273 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
274 163ce85a 2019-05-13 stsp if (err != NULL)
275 163ce85a 2019-05-13 stsp return err;
276 0266afb7 2019-01-04 stsp
277 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
278 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
279 0266afb7 2019-01-04 stsp
280 0266afb7 2019-01-04 stsp return NULL;
281 2c7829a4 2019-06-17 stsp }
282 2c7829a4 2019-06-17 stsp
283 2c7829a4 2019-06-17 stsp __dead static void
284 2c7829a4 2019-06-17 stsp usage_init(void)
285 2c7829a4 2019-06-17 stsp {
286 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
287 2c7829a4 2019-06-17 stsp exit(1);
288 2c7829a4 2019-06-17 stsp }
289 2c7829a4 2019-06-17 stsp
290 2c7829a4 2019-06-17 stsp static const struct got_error *
291 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
292 2c7829a4 2019-06-17 stsp {
293 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
294 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
295 2c7829a4 2019-06-17 stsp int ch;
296 2c7829a4 2019-06-17 stsp
297 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
298 2c7829a4 2019-06-17 stsp switch (ch) {
299 2c7829a4 2019-06-17 stsp default:
300 2c7829a4 2019-06-17 stsp usage_init();
301 2c7829a4 2019-06-17 stsp /* NOTREACHED */
302 2c7829a4 2019-06-17 stsp }
303 2c7829a4 2019-06-17 stsp }
304 2c7829a4 2019-06-17 stsp
305 2c7829a4 2019-06-17 stsp argc -= optind;
306 2c7829a4 2019-06-17 stsp argv += optind;
307 2c7829a4 2019-06-17 stsp
308 2c7829a4 2019-06-17 stsp #ifndef PROFILE
309 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
310 2c7829a4 2019-06-17 stsp err(1, "pledge");
311 2c7829a4 2019-06-17 stsp #endif
312 2c7829a4 2019-06-17 stsp if (argc != 1)
313 bc20e173 2019-06-17 stsp usage_init();
314 2c7829a4 2019-06-17 stsp
315 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
316 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
317 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
318 2c7829a4 2019-06-17 stsp
319 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
320 2c7829a4 2019-06-17 stsp
321 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
322 2c7829a4 2019-06-17 stsp if (error &&
323 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
324 2c7829a4 2019-06-17 stsp goto done;
325 2c7829a4 2019-06-17 stsp
326 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
327 2c7829a4 2019-06-17 stsp if (error)
328 2c7829a4 2019-06-17 stsp goto done;
329 2c7829a4 2019-06-17 stsp
330 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
331 2c7829a4 2019-06-17 stsp if (error != NULL)
332 3ce1b845 2019-07-15 stsp goto done;
333 3ce1b845 2019-07-15 stsp
334 3ce1b845 2019-07-15 stsp done:
335 3ce1b845 2019-07-15 stsp free(repo_path);
336 3ce1b845 2019-07-15 stsp return error;
337 3ce1b845 2019-07-15 stsp }
338 3ce1b845 2019-07-15 stsp
339 3ce1b845 2019-07-15 stsp __dead static void
340 3ce1b845 2019-07-15 stsp usage_import(void)
341 3ce1b845 2019-07-15 stsp {
342 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
343 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
344 3ce1b845 2019-07-15 stsp exit(1);
345 3ce1b845 2019-07-15 stsp }
346 3ce1b845 2019-07-15 stsp
347 3ce1b845 2019-07-15 stsp int
348 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
349 3ce1b845 2019-07-15 stsp {
350 3ce1b845 2019-07-15 stsp pid_t pid;
351 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
352 3ce1b845 2019-07-15 stsp int st = -1;
353 3ce1b845 2019-07-15 stsp
354 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
355 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
356 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
357 3ce1b845 2019-07-15 stsp
358 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
359 3ce1b845 2019-07-15 stsp case -1:
360 3ce1b845 2019-07-15 stsp goto doneediting;
361 3ce1b845 2019-07-15 stsp case 0:
362 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
363 3ce1b845 2019-07-15 stsp _exit(127);
364 3ce1b845 2019-07-15 stsp }
365 3ce1b845 2019-07-15 stsp
366 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
367 3ce1b845 2019-07-15 stsp if (errno != EINTR)
368 3ce1b845 2019-07-15 stsp break;
369 3ce1b845 2019-07-15 stsp
370 3ce1b845 2019-07-15 stsp doneediting:
371 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
372 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
373 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
376 3ce1b845 2019-07-15 stsp errno = EINTR;
377 3ce1b845 2019-07-15 stsp return -1;
378 3ce1b845 2019-07-15 stsp }
379 3ce1b845 2019-07-15 stsp
380 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
381 3ce1b845 2019-07-15 stsp }
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp static const struct got_error *
384 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
385 3ce1b845 2019-07-15 stsp const char *initial_content)
386 3ce1b845 2019-07-15 stsp {
387 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
388 3ce1b845 2019-07-15 stsp char buf[1024];
389 3ce1b845 2019-07-15 stsp struct stat st, st2;
390 3ce1b845 2019-07-15 stsp FILE *fp;
391 3ce1b845 2019-07-15 stsp int content_changed = 0;
392 3ce1b845 2019-07-15 stsp size_t len;
393 3ce1b845 2019-07-15 stsp
394 3ce1b845 2019-07-15 stsp *logmsg = NULL;
395 3ce1b845 2019-07-15 stsp
396 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
397 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
400 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
403 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
404 3ce1b845 2019-07-15 stsp
405 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
406 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
407 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
408 3ce1b845 2019-07-15 stsp
409 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
410 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
411 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
412 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
413 3ce1b845 2019-07-15 stsp len = 0;
414 3ce1b845 2019-07-15 stsp
415 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
416 3ce1b845 2019-07-15 stsp if (fp == NULL) {
417 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
418 3ce1b845 2019-07-15 stsp goto done;
419 3ce1b845 2019-07-15 stsp }
420 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
421 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
422 3ce1b845 2019-07-15 stsp content_changed = 1;
423 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
424 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
425 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
426 3ce1b845 2019-07-15 stsp }
427 3ce1b845 2019-07-15 stsp fclose(fp);
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
430 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
431 3ce1b845 2019-07-15 stsp len--;
432 3ce1b845 2019-07-15 stsp }
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
435 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
436 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
437 3ce1b845 2019-07-15 stsp done:
438 3ce1b845 2019-07-15 stsp if (err) {
439 3ce1b845 2019-07-15 stsp free(*logmsg);
440 3ce1b845 2019-07-15 stsp *logmsg = NULL;
441 3ce1b845 2019-07-15 stsp }
442 3ce1b845 2019-07-15 stsp return err;
443 3ce1b845 2019-07-15 stsp }
444 3ce1b845 2019-07-15 stsp
445 3ce1b845 2019-07-15 stsp static const struct got_error *
446 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
447 3ce1b845 2019-07-15 stsp const char *branch_name)
448 3ce1b845 2019-07-15 stsp {
449 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
450 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
451 3ce1b845 2019-07-15 stsp int fd;
452 3ce1b845 2019-07-15 stsp
453 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
454 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
455 3ce1b845 2019-07-15 stsp branch_name) == -1)
456 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
459 3ce1b845 2019-07-15 stsp if (err)
460 3ce1b845 2019-07-15 stsp goto done;
461 3ce1b845 2019-07-15 stsp
462 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
463 3ce1b845 2019-07-15 stsp close(fd);
464 3ce1b845 2019-07-15 stsp
465 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
466 3ce1b845 2019-07-15 stsp done:
467 3ce1b845 2019-07-15 stsp free(initial_content);
468 3ce1b845 2019-07-15 stsp free(logmsg_path);
469 3ce1b845 2019-07-15 stsp return err;
470 3ce1b845 2019-07-15 stsp }
471 3ce1b845 2019-07-15 stsp
472 3ce1b845 2019-07-15 stsp static const struct got_error *
473 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
474 3ce1b845 2019-07-15 stsp {
475 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
476 3ce1b845 2019-07-15 stsp return NULL;
477 3ce1b845 2019-07-15 stsp }
478 3ce1b845 2019-07-15 stsp
479 3ce1b845 2019-07-15 stsp static const struct got_error *
480 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
481 3ce1b845 2019-07-15 stsp {
482 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
483 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
484 3ce1b845 2019-07-15 stsp char *editor = NULL;
485 3ce1b845 2019-07-15 stsp const char *got_author = getenv("GOT_AUTHOR");
486 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
487 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
488 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
489 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
490 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
491 3ce1b845 2019-07-15 stsp int ch;
492 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
493 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
494 3ce1b845 2019-07-15 stsp
495 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
496 3ce1b845 2019-07-15 stsp
497 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
498 3ce1b845 2019-07-15 stsp switch (ch) {
499 3ce1b845 2019-07-15 stsp case 'b':
500 3ce1b845 2019-07-15 stsp branch_name = optarg;
501 3ce1b845 2019-07-15 stsp break;
502 3ce1b845 2019-07-15 stsp case 'm':
503 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
504 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
505 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
506 3ce1b845 2019-07-15 stsp goto done;
507 3ce1b845 2019-07-15 stsp }
508 3ce1b845 2019-07-15 stsp break;
509 3ce1b845 2019-07-15 stsp case 'r':
510 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
511 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
512 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
513 3ce1b845 2019-07-15 stsp goto done;
514 3ce1b845 2019-07-15 stsp }
515 3ce1b845 2019-07-15 stsp break;
516 3ce1b845 2019-07-15 stsp case 'I':
517 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
518 3ce1b845 2019-07-15 stsp break;
519 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
520 3ce1b845 2019-07-15 stsp NULL);
521 3ce1b845 2019-07-15 stsp if (error)
522 3ce1b845 2019-07-15 stsp goto done;
523 3ce1b845 2019-07-15 stsp break;
524 3ce1b845 2019-07-15 stsp default:
525 3ce1b845 2019-07-15 stsp usage_init();
526 3ce1b845 2019-07-15 stsp /* NOTREACHED */
527 3ce1b845 2019-07-15 stsp }
528 3ce1b845 2019-07-15 stsp }
529 3ce1b845 2019-07-15 stsp
530 3ce1b845 2019-07-15 stsp argc -= optind;
531 3ce1b845 2019-07-15 stsp argv += optind;
532 3ce1b845 2019-07-15 stsp
533 3ce1b845 2019-07-15 stsp #ifndef PROFILE
534 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
535 3ce1b845 2019-07-15 stsp NULL) == -1)
536 3ce1b845 2019-07-15 stsp err(1, "pledge");
537 3ce1b845 2019-07-15 stsp #endif
538 3ce1b845 2019-07-15 stsp if (argc != 1)
539 3ce1b845 2019-07-15 stsp usage_import();
540 3ce1b845 2019-07-15 stsp
541 3ce1b845 2019-07-15 stsp if (got_author == NULL) {
542 3ce1b845 2019-07-15 stsp /* TODO: Look current user up in password database */
543 3ce1b845 2019-07-15 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
544 2c7829a4 2019-06-17 stsp goto done;
545 3ce1b845 2019-07-15 stsp }
546 2c7829a4 2019-06-17 stsp
547 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
548 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
549 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
550 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
551 3ce1b845 2019-07-15 stsp }
552 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
553 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
554 3ce1b845 2019-07-15 stsp if (error)
555 3ce1b845 2019-07-15 stsp goto done;
556 3ce1b845 2019-07-15 stsp
557 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
558 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
559 3ce1b845 2019-07-15 stsp goto done;
560 3ce1b845 2019-07-15 stsp }
561 3ce1b845 2019-07-15 stsp
562 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
563 3ce1b845 2019-07-15 stsp if (error) {
564 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
565 3ce1b845 2019-07-15 stsp goto done;
566 3ce1b845 2019-07-15 stsp } else {
567 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
568 3ce1b845 2019-07-15 stsp "import target branch already exists");
569 3ce1b845 2019-07-15 stsp goto done;
570 3ce1b845 2019-07-15 stsp }
571 3ce1b845 2019-07-15 stsp
572 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
573 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
574 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
575 3ce1b845 2019-07-15 stsp goto done;
576 3ce1b845 2019-07-15 stsp }
577 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
578 3ce1b845 2019-07-15 stsp
579 3ce1b845 2019-07-15 stsp /*
580 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
581 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
582 3ce1b845 2019-07-15 stsp */
583 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
584 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
585 3ce1b845 2019-07-15 stsp if (error)
586 3ce1b845 2019-07-15 stsp goto done;
587 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
588 3ce1b845 2019-07-15 stsp if (error)
589 3ce1b845 2019-07-15 stsp goto done;
590 3ce1b845 2019-07-15 stsp }
591 3ce1b845 2019-07-15 stsp
592 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
593 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
594 3ce1b845 2019-07-15 stsp
595 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
596 3ce1b845 2019-07-15 stsp if (error)
597 3ce1b845 2019-07-15 stsp goto done;
598 3ce1b845 2019-07-15 stsp
599 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
600 3ce1b845 2019-07-15 stsp got_author, &ignores, repo, import_progress, NULL);
601 3ce1b845 2019-07-15 stsp if (error)
602 3ce1b845 2019-07-15 stsp goto done;
603 3ce1b845 2019-07-15 stsp
604 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
605 3ce1b845 2019-07-15 stsp if (error)
606 3ce1b845 2019-07-15 stsp goto done;
607 3ce1b845 2019-07-15 stsp
608 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
609 3ce1b845 2019-07-15 stsp if (error)
610 3ce1b845 2019-07-15 stsp goto done;
611 3ce1b845 2019-07-15 stsp
612 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
613 3ce1b845 2019-07-15 stsp if (error)
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp
616 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
617 3ce1b845 2019-07-15 stsp if (error) {
618 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
619 3ce1b845 2019-07-15 stsp goto done;
620 3ce1b845 2019-07-15 stsp
621 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
622 3ce1b845 2019-07-15 stsp branch_ref);
623 3ce1b845 2019-07-15 stsp if (error)
624 3ce1b845 2019-07-15 stsp goto done;
625 3ce1b845 2019-07-15 stsp
626 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
627 3ce1b845 2019-07-15 stsp if (error)
628 3ce1b845 2019-07-15 stsp goto done;
629 3ce1b845 2019-07-15 stsp }
630 3ce1b845 2019-07-15 stsp
631 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
632 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
633 2c7829a4 2019-06-17 stsp done:
634 2c7829a4 2019-06-17 stsp free(repo_path);
635 3ce1b845 2019-07-15 stsp free(editor);
636 3ce1b845 2019-07-15 stsp free(refname);
637 3ce1b845 2019-07-15 stsp free(new_commit_id);
638 3ce1b845 2019-07-15 stsp free(id_str);
639 3ce1b845 2019-07-15 stsp if (branch_ref)
640 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
641 3ce1b845 2019-07-15 stsp if (head_ref)
642 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
643 2c7829a4 2019-06-17 stsp return error;
644 0266afb7 2019-01-04 stsp }
645 0266afb7 2019-01-04 stsp
646 4ed7e80c 2018-05-20 stsp __dead static void
647 c09a553d 2018-03-12 stsp usage_checkout(void)
648 c09a553d 2018-03-12 stsp {
649 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
650 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
651 c09a553d 2018-03-12 stsp exit(1);
652 92a684f4 2018-03-12 stsp }
653 92a684f4 2018-03-12 stsp
654 1ee397ad 2019-07-12 stsp static const struct got_error *
655 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
656 92a684f4 2018-03-12 stsp {
657 92a684f4 2018-03-12 stsp char *worktree_path = arg;
658 a484d721 2019-06-10 stsp
659 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
660 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
661 1ee397ad 2019-07-12 stsp return NULL;
662 92a684f4 2018-03-12 stsp
663 92a684f4 2018-03-12 stsp while (path[0] == '/')
664 92a684f4 2018-03-12 stsp path++;
665 92a684f4 2018-03-12 stsp
666 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
667 1ee397ad 2019-07-12 stsp return NULL;
668 99437157 2018-11-11 stsp }
669 99437157 2018-11-11 stsp
670 99437157 2018-11-11 stsp static const struct got_error *
671 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
672 99437157 2018-11-11 stsp {
673 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
674 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
675 99437157 2018-11-11 stsp return NULL;
676 8069f636 2019-01-12 stsp }
677 8069f636 2019-01-12 stsp
678 8069f636 2019-01-12 stsp static const struct got_error *
679 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
680 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
681 8069f636 2019-01-12 stsp {
682 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
683 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
684 8069f636 2019-01-12 stsp
685 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
686 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
687 36a38700 2019-05-10 stsp if (err)
688 36a38700 2019-05-10 stsp return err;
689 8069f636 2019-01-12 stsp
690 d5bea539 2019-05-13 stsp if (yca_id == NULL)
691 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
692 8069f636 2019-01-12 stsp
693 d5bea539 2019-05-13 stsp /*
694 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
695 d5bea539 2019-05-13 stsp * and the work tree's base commit.
696 d5bea539 2019-05-13 stsp *
697 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
698 d5bea539 2019-05-13 stsp *
699 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
700 d5bea539 2019-05-13 stsp * \ /
701 d5bea539 2019-05-13 stsp * C E
702 d5bea539 2019-05-13 stsp * \ /
703 d5bea539 2019-05-13 stsp * B (yca)
704 d5bea539 2019-05-13 stsp * |
705 d5bea539 2019-05-13 stsp * A
706 d5bea539 2019-05-13 stsp *
707 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
708 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
709 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
710 d5bea539 2019-05-13 stsp */
711 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
712 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
713 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
714 8069f636 2019-01-12 stsp
715 d5bea539 2019-05-13 stsp free(yca_id);
716 d5bea539 2019-05-13 stsp return NULL;
717 c09a553d 2018-03-12 stsp }
718 a367ff0f 2019-05-14 stsp
719 a367ff0f 2019-05-14 stsp static const struct got_error *
720 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
721 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
722 a51a74b3 2019-07-27 stsp struct got_repository *repo)
723 a367ff0f 2019-05-14 stsp {
724 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
725 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
726 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
727 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
728 a367ff0f 2019-05-14 stsp
729 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
730 a367ff0f 2019-05-14 stsp if (err)
731 a51a74b3 2019-07-27 stsp goto done;
732 a51a74b3 2019-07-27 stsp
733 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
734 a51a74b3 2019-07-27 stsp is_same_branch = 1;
735 a51a74b3 2019-07-27 stsp goto done;
736 a51a74b3 2019-07-27 stsp }
737 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
738 a51a74b3 2019-07-27 stsp is_same_branch = 1;
739 a367ff0f 2019-05-14 stsp goto done;
740 a51a74b3 2019-07-27 stsp }
741 a367ff0f 2019-05-14 stsp
742 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
743 a367ff0f 2019-05-14 stsp if (err)
744 a367ff0f 2019-05-14 stsp goto done;
745 a367ff0f 2019-05-14 stsp
746 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
747 a367ff0f 2019-05-14 stsp if (err)
748 a367ff0f 2019-05-14 stsp goto done;
749 a367ff0f 2019-05-14 stsp
750 a367ff0f 2019-05-14 stsp for (;;) {
751 a367ff0f 2019-05-14 stsp struct got_object_id *id;
752 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
753 a367ff0f 2019-05-14 stsp if (err) {
754 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
755 a367ff0f 2019-05-14 stsp err = NULL;
756 a367ff0f 2019-05-14 stsp break;
757 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
758 a367ff0f 2019-05-14 stsp break;
759 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
760 a367ff0f 2019-05-14 stsp repo);
761 a367ff0f 2019-05-14 stsp if (err)
762 a367ff0f 2019-05-14 stsp break;
763 a367ff0f 2019-05-14 stsp }
764 c09a553d 2018-03-12 stsp
765 a367ff0f 2019-05-14 stsp if (id) {
766 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
767 a51a74b3 2019-07-27 stsp break;
768 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
769 a367ff0f 2019-05-14 stsp is_same_branch = 1;
770 a367ff0f 2019-05-14 stsp break;
771 a367ff0f 2019-05-14 stsp }
772 a367ff0f 2019-05-14 stsp }
773 a367ff0f 2019-05-14 stsp }
774 a367ff0f 2019-05-14 stsp done:
775 a367ff0f 2019-05-14 stsp if (graph)
776 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
777 a367ff0f 2019-05-14 stsp free(head_commit_id);
778 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
779 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
780 04f57cb3 2019-07-25 stsp return err;
781 04f57cb3 2019-07-25 stsp }
782 04f57cb3 2019-07-25 stsp
783 04f57cb3 2019-07-25 stsp static const struct got_error *
784 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
785 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
786 04f57cb3 2019-07-25 stsp {
787 04f57cb3 2019-07-25 stsp const struct got_error *err;
788 04f57cb3 2019-07-25 stsp struct got_reference *ref;
789 04f57cb3 2019-07-25 stsp
790 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
791 04f57cb3 2019-07-25 stsp if (err == NULL) {
792 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
793 04f57cb3 2019-07-25 stsp got_ref_close(ref);
794 04f57cb3 2019-07-25 stsp } else {
795 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
796 04f57cb3 2019-07-25 stsp return err;
797 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
798 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
799 04f57cb3 2019-07-25 stsp }
800 a367ff0f 2019-05-14 stsp return err;
801 a367ff0f 2019-05-14 stsp }
802 8069f636 2019-01-12 stsp
803 4ed7e80c 2018-05-20 stsp static const struct got_error *
804 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
805 c09a553d 2018-03-12 stsp {
806 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
807 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
808 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
809 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
810 c09a553d 2018-03-12 stsp char *repo_path = NULL;
811 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
812 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
813 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
814 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
815 72151b04 2019-05-11 stsp int ch, same_path_prefix;
816 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
817 f2ea84fa 2019-07-27 stsp
818 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
819 c09a553d 2018-03-12 stsp
820 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
821 0bb8a95e 2018-03-12 stsp switch (ch) {
822 08573d5b 2019-05-14 stsp case 'b':
823 08573d5b 2019-05-14 stsp branch_name = optarg;
824 08573d5b 2019-05-14 stsp break;
825 8069f636 2019-01-12 stsp case 'c':
826 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
827 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
828 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
829 8069f636 2019-01-12 stsp break;
830 0bb8a95e 2018-03-12 stsp case 'p':
831 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
832 0bb8a95e 2018-03-12 stsp break;
833 0bb8a95e 2018-03-12 stsp default:
834 2deda0b9 2019-03-07 stsp usage_checkout();
835 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
836 0bb8a95e 2018-03-12 stsp }
837 0bb8a95e 2018-03-12 stsp }
838 0bb8a95e 2018-03-12 stsp
839 0bb8a95e 2018-03-12 stsp argc -= optind;
840 0bb8a95e 2018-03-12 stsp argv += optind;
841 0bb8a95e 2018-03-12 stsp
842 6715a751 2018-03-16 stsp #ifndef PROFILE
843 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
844 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
845 c09a553d 2018-03-12 stsp err(1, "pledge");
846 6715a751 2018-03-16 stsp #endif
847 0bb8a95e 2018-03-12 stsp if (argc == 1) {
848 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
849 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
850 76089277 2018-04-01 stsp if (repo_path == NULL)
851 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
852 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
853 76089277 2018-04-01 stsp if (cwd == NULL) {
854 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
855 76089277 2018-04-01 stsp goto done;
856 76089277 2018-04-01 stsp }
857 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
858 230a42bd 2019-05-11 jcs base = basename(path_prefix);
859 230a42bd 2019-05-11 jcs if (base == NULL) {
860 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
861 230a42bd 2019-05-11 jcs path_prefix);
862 230a42bd 2019-05-11 jcs goto done;
863 230a42bd 2019-05-11 jcs }
864 230a42bd 2019-05-11 jcs } else {
865 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
866 230a42bd 2019-05-11 jcs if (base == NULL) {
867 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
868 230a42bd 2019-05-11 jcs repo_path);
869 230a42bd 2019-05-11 jcs goto done;
870 230a42bd 2019-05-11 jcs }
871 76089277 2018-04-01 stsp }
872 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
873 c09a553d 2018-03-12 stsp if (dotgit)
874 c09a553d 2018-03-12 stsp *dotgit = '\0';
875 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
876 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
877 c09a553d 2018-03-12 stsp free(cwd);
878 76089277 2018-04-01 stsp goto done;
879 c09a553d 2018-03-12 stsp }
880 c09a553d 2018-03-12 stsp free(cwd);
881 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
882 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
883 76089277 2018-04-01 stsp if (repo_path == NULL) {
884 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
885 76089277 2018-04-01 stsp goto done;
886 76089277 2018-04-01 stsp }
887 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
888 76089277 2018-04-01 stsp if (worktree_path == NULL) {
889 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
890 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
891 b4b3a7dd 2019-07-22 stsp argv[1]);
892 b4b3a7dd 2019-07-22 stsp goto done;
893 b4b3a7dd 2019-07-22 stsp }
894 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
895 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
896 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
897 b4b3a7dd 2019-07-22 stsp goto done;
898 b4b3a7dd 2019-07-22 stsp }
899 76089277 2018-04-01 stsp }
900 c09a553d 2018-03-12 stsp } else
901 c09a553d 2018-03-12 stsp usage_checkout();
902 c09a553d 2018-03-12 stsp
903 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
904 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
905 13bfb272 2019-05-10 jcs
906 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
907 c09a553d 2018-03-12 stsp if (error != NULL)
908 c02c541e 2019-03-29 stsp goto done;
909 c02c541e 2019-03-29 stsp
910 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
911 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
912 c530dc23 2019-07-23 stsp if (error) {
913 c530dc23 2019-07-23 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR))
914 c530dc23 2019-07-23 stsp goto done;
915 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
916 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
917 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
918 c530dc23 2019-07-23 stsp goto done;
919 c530dc23 2019-07-23 stsp }
920 c530dc23 2019-07-23 stsp }
921 c530dc23 2019-07-23 stsp
922 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
923 c02c541e 2019-03-29 stsp if (error)
924 c09a553d 2018-03-12 stsp goto done;
925 8069f636 2019-01-12 stsp
926 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
927 c09a553d 2018-03-12 stsp if (error != NULL)
928 c09a553d 2018-03-12 stsp goto done;
929 c09a553d 2018-03-12 stsp
930 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
931 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
932 c09a553d 2018-03-12 stsp goto done;
933 c09a553d 2018-03-12 stsp
934 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
935 c09a553d 2018-03-12 stsp if (error != NULL)
936 c09a553d 2018-03-12 stsp goto done;
937 c09a553d 2018-03-12 stsp
938 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
939 e5dc7198 2018-12-29 stsp path_prefix);
940 e5dc7198 2018-12-29 stsp if (error != NULL)
941 e5dc7198 2018-12-29 stsp goto done;
942 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
943 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
944 49520a32 2018-12-29 stsp goto done;
945 49520a32 2018-12-29 stsp }
946 49520a32 2018-12-29 stsp
947 8069f636 2019-01-12 stsp if (commit_id_str) {
948 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
949 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
950 30837e32 2019-07-25 stsp if (error)
951 8069f636 2019-01-12 stsp goto done;
952 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
953 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
954 8069f636 2019-01-12 stsp if (error != NULL) {
955 8069f636 2019-01-12 stsp free(commit_id);
956 8069f636 2019-01-12 stsp goto done;
957 8069f636 2019-01-12 stsp }
958 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
959 45d344f6 2019-05-14 stsp if (error)
960 45d344f6 2019-05-14 stsp goto done;
961 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
962 8069f636 2019-01-12 stsp commit_id);
963 8069f636 2019-01-12 stsp free(commit_id);
964 8069f636 2019-01-12 stsp if (error)
965 8069f636 2019-01-12 stsp goto done;
966 8069f636 2019-01-12 stsp }
967 8069f636 2019-01-12 stsp
968 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
969 f2ea84fa 2019-07-27 stsp if (error)
970 f2ea84fa 2019-07-27 stsp goto done;
971 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
972 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
973 c09a553d 2018-03-12 stsp if (error != NULL)
974 c09a553d 2018-03-12 stsp goto done;
975 c09a553d 2018-03-12 stsp
976 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
977 c09a553d 2018-03-12 stsp
978 c09a553d 2018-03-12 stsp done:
979 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
980 8069f636 2019-01-12 stsp free(commit_id_str);
981 76089277 2018-04-01 stsp free(repo_path);
982 507dc3bb 2018-12-29 stsp free(worktree_path);
983 507dc3bb 2018-12-29 stsp return error;
984 507dc3bb 2018-12-29 stsp }
985 507dc3bb 2018-12-29 stsp
986 507dc3bb 2018-12-29 stsp __dead static void
987 507dc3bb 2018-12-29 stsp usage_update(void)
988 507dc3bb 2018-12-29 stsp {
989 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
990 507dc3bb 2018-12-29 stsp getprogname());
991 507dc3bb 2018-12-29 stsp exit(1);
992 507dc3bb 2018-12-29 stsp }
993 507dc3bb 2018-12-29 stsp
994 1ee397ad 2019-07-12 stsp static const struct got_error *
995 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
996 507dc3bb 2018-12-29 stsp {
997 784955db 2019-01-12 stsp int *did_something = arg;
998 784955db 2019-01-12 stsp
999 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1000 1ee397ad 2019-07-12 stsp return NULL;
1001 507dc3bb 2018-12-29 stsp
1002 1545c615 2019-02-10 stsp *did_something = 1;
1003 a484d721 2019-06-10 stsp
1004 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1005 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1006 1ee397ad 2019-07-12 stsp return NULL;
1007 a484d721 2019-06-10 stsp
1008 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1009 507dc3bb 2018-12-29 stsp path++;
1010 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1011 1ee397ad 2019-07-12 stsp return NULL;
1012 be7061eb 2018-12-30 stsp }
1013 be7061eb 2018-12-30 stsp
1014 be7061eb 2018-12-30 stsp static const struct got_error *
1015 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1016 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1017 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1018 a1fb16d8 2019-05-24 stsp {
1019 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1020 a1fb16d8 2019-05-24 stsp char *base_id_str;
1021 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1022 a1fb16d8 2019-05-24 stsp
1023 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1024 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1025 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1026 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1027 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1028 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1029 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1030 a1fb16d8 2019-05-24 stsp }
1031 a1fb16d8 2019-05-24 stsp
1032 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1033 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
1034 a1fb16d8 2019-05-24 stsp if (err) {
1035 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1036 a1fb16d8 2019-05-24 stsp return err;
1037 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1038 a1fb16d8 2019-05-24 stsp }
1039 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1040 a1fb16d8 2019-05-24 stsp return NULL;
1041 a1fb16d8 2019-05-24 stsp
1042 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1043 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1044 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1045 a1fb16d8 2019-05-24 stsp if (err)
1046 a1fb16d8 2019-05-24 stsp return err;
1047 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1048 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1049 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1050 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1051 0ebf8283 2019-07-24 stsp return NULL;
1052 0ebf8283 2019-07-24 stsp }
1053 0ebf8283 2019-07-24 stsp
1054 0ebf8283 2019-07-24 stsp static const struct got_error *
1055 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1056 0ebf8283 2019-07-24 stsp {
1057 0ebf8283 2019-07-24 stsp const struct got_error *err;
1058 0ebf8283 2019-07-24 stsp int in_progress;
1059 0ebf8283 2019-07-24 stsp
1060 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1061 0ebf8283 2019-07-24 stsp if (err)
1062 0ebf8283 2019-07-24 stsp return err;
1063 0ebf8283 2019-07-24 stsp if (in_progress)
1064 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1065 0ebf8283 2019-07-24 stsp
1066 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1067 0ebf8283 2019-07-24 stsp if (err)
1068 0ebf8283 2019-07-24 stsp return err;
1069 0ebf8283 2019-07-24 stsp if (in_progress)
1070 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1071 0ebf8283 2019-07-24 stsp
1072 a1fb16d8 2019-05-24 stsp return NULL;
1073 a5edda0a 2019-07-27 stsp }
1074 a5edda0a 2019-07-27 stsp
1075 a5edda0a 2019-07-27 stsp static const struct got_error *
1076 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1077 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1078 a5edda0a 2019-07-27 stsp {
1079 a5edda0a 2019-07-27 stsp const struct got_error *err;
1080 a5edda0a 2019-07-27 stsp char *path;
1081 a5edda0a 2019-07-27 stsp int i;
1082 a5edda0a 2019-07-27 stsp
1083 a5edda0a 2019-07-27 stsp if (argc == 0) {
1084 a5edda0a 2019-07-27 stsp path = strdup("");
1085 a5edda0a 2019-07-27 stsp if (path == NULL)
1086 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1087 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1088 a5edda0a 2019-07-27 stsp }
1089 a5edda0a 2019-07-27 stsp
1090 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1091 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1092 a5edda0a 2019-07-27 stsp if (err)
1093 a5edda0a 2019-07-27 stsp break;
1094 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1095 a5edda0a 2019-07-27 stsp if (err) {
1096 a5edda0a 2019-07-27 stsp free(path);
1097 a5edda0a 2019-07-27 stsp break;
1098 a5edda0a 2019-07-27 stsp }
1099 a5edda0a 2019-07-27 stsp }
1100 a5edda0a 2019-07-27 stsp
1101 a5edda0a 2019-07-27 stsp return err;
1102 a1fb16d8 2019-05-24 stsp }
1103 a1fb16d8 2019-05-24 stsp
1104 a1fb16d8 2019-05-24 stsp static const struct got_error *
1105 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1106 507dc3bb 2018-12-29 stsp {
1107 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1108 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1109 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1110 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1111 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1112 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1113 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1114 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1115 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1116 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1117 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1118 507dc3bb 2018-12-29 stsp
1119 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1120 f2ea84fa 2019-07-27 stsp
1121 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1122 507dc3bb 2018-12-29 stsp switch (ch) {
1123 024e9686 2019-05-14 stsp case 'b':
1124 024e9686 2019-05-14 stsp branch_name = optarg;
1125 024e9686 2019-05-14 stsp break;
1126 507dc3bb 2018-12-29 stsp case 'c':
1127 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1128 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1129 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1130 507dc3bb 2018-12-29 stsp break;
1131 507dc3bb 2018-12-29 stsp default:
1132 2deda0b9 2019-03-07 stsp usage_update();
1133 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1134 507dc3bb 2018-12-29 stsp }
1135 507dc3bb 2018-12-29 stsp }
1136 507dc3bb 2018-12-29 stsp
1137 507dc3bb 2018-12-29 stsp argc -= optind;
1138 507dc3bb 2018-12-29 stsp argv += optind;
1139 507dc3bb 2018-12-29 stsp
1140 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1141 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1142 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1143 507dc3bb 2018-12-29 stsp err(1, "pledge");
1144 507dc3bb 2018-12-29 stsp #endif
1145 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1146 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1147 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1148 c4cdcb68 2019-04-03 stsp goto done;
1149 c4cdcb68 2019-04-03 stsp }
1150 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1151 7d5807f4 2019-07-11 stsp if (error)
1152 7d5807f4 2019-07-11 stsp goto done;
1153 7d5807f4 2019-07-11 stsp
1154 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1155 c4cdcb68 2019-04-03 stsp if (error)
1156 7d5807f4 2019-07-11 stsp goto done;
1157 c4cdcb68 2019-04-03 stsp
1158 f2ea84fa 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1159 f2ea84fa 2019-07-27 stsp if (error)
1160 f2ea84fa 2019-07-27 stsp goto done;
1161 507dc3bb 2018-12-29 stsp
1162 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1163 507dc3bb 2018-12-29 stsp if (error != NULL)
1164 507dc3bb 2018-12-29 stsp goto done;
1165 507dc3bb 2018-12-29 stsp
1166 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1167 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1168 0266afb7 2019-01-04 stsp if (error)
1169 0266afb7 2019-01-04 stsp goto done;
1170 0266afb7 2019-01-04 stsp
1171 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1172 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1173 024e9686 2019-05-14 stsp if (error != NULL)
1174 024e9686 2019-05-14 stsp goto done;
1175 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1176 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1177 9c4b8182 2019-01-02 stsp if (error != NULL)
1178 9c4b8182 2019-01-02 stsp goto done;
1179 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1180 507dc3bb 2018-12-29 stsp if (error != NULL)
1181 507dc3bb 2018-12-29 stsp goto done;
1182 507dc3bb 2018-12-29 stsp } else {
1183 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1184 04f57cb3 2019-07-25 stsp free(commit_id_str);
1185 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1186 30837e32 2019-07-25 stsp if (error)
1187 a297e751 2019-07-11 stsp goto done;
1188 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1189 a297e751 2019-07-11 stsp if (error)
1190 507dc3bb 2018-12-29 stsp goto done;
1191 507dc3bb 2018-12-29 stsp }
1192 35c965b2 2018-12-29 stsp
1193 a1fb16d8 2019-05-24 stsp if (branch_name) {
1194 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1195 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1196 f2ea84fa 2019-07-27 stsp if (strlen(pe->path) == 0)
1197 f2ea84fa 2019-07-27 stsp continue;
1198 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1199 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1200 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1201 024e9686 2019-05-14 stsp goto done;
1202 024e9686 2019-05-14 stsp }
1203 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1204 024e9686 2019-05-14 stsp if (error)
1205 024e9686 2019-05-14 stsp goto done;
1206 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1207 a367ff0f 2019-05-14 stsp free(head_commit_id);
1208 024e9686 2019-05-14 stsp if (error != NULL)
1209 024e9686 2019-05-14 stsp goto done;
1210 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1211 a367ff0f 2019-05-14 stsp if (error)
1212 a367ff0f 2019-05-14 stsp goto done;
1213 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1214 024e9686 2019-05-14 stsp if (error)
1215 024e9686 2019-05-14 stsp goto done;
1216 024e9686 2019-05-14 stsp } else {
1217 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1218 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1219 a367ff0f 2019-05-14 stsp if (error != NULL) {
1220 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1221 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1222 024e9686 2019-05-14 stsp goto done;
1223 a367ff0f 2019-05-14 stsp }
1224 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1225 a367ff0f 2019-05-14 stsp if (error)
1226 a367ff0f 2019-05-14 stsp goto done;
1227 024e9686 2019-05-14 stsp }
1228 507dc3bb 2018-12-29 stsp
1229 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1230 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1231 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1232 507dc3bb 2018-12-29 stsp commit_id);
1233 507dc3bb 2018-12-29 stsp if (error)
1234 507dc3bb 2018-12-29 stsp goto done;
1235 507dc3bb 2018-12-29 stsp }
1236 507dc3bb 2018-12-29 stsp
1237 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1238 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1239 507dc3bb 2018-12-29 stsp if (error != NULL)
1240 507dc3bb 2018-12-29 stsp goto done;
1241 9c4b8182 2019-01-02 stsp
1242 784955db 2019-01-12 stsp if (did_something)
1243 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1244 784955db 2019-01-12 stsp else
1245 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1246 507dc3bb 2018-12-29 stsp done:
1247 c09a553d 2018-03-12 stsp free(worktree_path);
1248 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1249 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1250 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1251 507dc3bb 2018-12-29 stsp free(commit_id);
1252 9c4b8182 2019-01-02 stsp free(commit_id_str);
1253 c09a553d 2018-03-12 stsp return error;
1254 c09a553d 2018-03-12 stsp }
1255 c09a553d 2018-03-12 stsp
1256 f42b1b34 2018-03-12 stsp static const struct got_error *
1257 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1258 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1259 5c860e29 2018-03-12 stsp {
1260 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1261 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1262 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1263 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1264 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1265 79109fed 2018-03-27 stsp
1266 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1267 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1268 79109fed 2018-03-27 stsp if (err)
1269 79109fed 2018-03-27 stsp return err;
1270 79109fed 2018-03-27 stsp
1271 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1272 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1273 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1274 79109fed 2018-03-27 stsp
1275 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1276 79109fed 2018-03-27 stsp if (err)
1277 79109fed 2018-03-27 stsp return err;
1278 79109fed 2018-03-27 stsp
1279 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1280 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1281 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1282 0f2b3dca 2018-12-22 stsp if (err)
1283 0f2b3dca 2018-12-22 stsp return err;
1284 0f2b3dca 2018-12-22 stsp
1285 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1286 79109fed 2018-03-27 stsp if (err)
1287 79109fed 2018-03-27 stsp return err;
1288 79109fed 2018-03-27 stsp }
1289 79109fed 2018-03-27 stsp
1290 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1291 0f2b3dca 2018-12-22 stsp if (err)
1292 0f2b3dca 2018-12-22 stsp goto done;
1293 0f2b3dca 2018-12-22 stsp
1294 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1295 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1296 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1297 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1298 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1299 0f2b3dca 2018-12-22 stsp done:
1300 79109fed 2018-03-27 stsp if (tree1)
1301 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1302 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1303 0f2b3dca 2018-12-22 stsp free(id_str1);
1304 0f2b3dca 2018-12-22 stsp free(id_str2);
1305 79109fed 2018-03-27 stsp return err;
1306 79109fed 2018-03-27 stsp }
1307 79109fed 2018-03-27 stsp
1308 4bb494d5 2018-06-16 stsp static char *
1309 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1310 6c281f94 2018-06-11 stsp {
1311 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1312 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1313 6c281f94 2018-06-11 stsp if (p)
1314 6c281f94 2018-06-11 stsp *p = '\0';
1315 6c281f94 2018-06-11 stsp return s;
1316 6c281f94 2018-06-11 stsp }
1317 6c281f94 2018-06-11 stsp
1318 79109fed 2018-03-27 stsp static const struct got_error *
1319 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1320 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1321 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1322 79109fed 2018-03-27 stsp {
1323 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1324 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1325 6c281f94 2018-06-11 stsp char datebuf[26];
1326 45d799e2 2018-12-23 stsp time_t committer_time;
1327 45d799e2 2018-12-23 stsp const char *author, *committer;
1328 199a4027 2019-02-02 stsp char *refs_str = NULL;
1329 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1330 5c860e29 2018-03-12 stsp
1331 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1332 199a4027 2019-02-02 stsp char *s;
1333 199a4027 2019-02-02 stsp const char *name;
1334 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1335 199a4027 2019-02-02 stsp continue;
1336 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1337 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1338 d9498b20 2019-02-05 stsp continue;
1339 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1340 199a4027 2019-02-02 stsp name += 5;
1341 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1342 7143d404 2019-03-12 stsp continue;
1343 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1344 e34f9ed6 2019-02-02 stsp name += 6;
1345 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1346 141c2bff 2019-02-04 stsp name += 8;
1347 199a4027 2019-02-02 stsp s = refs_str;
1348 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1349 199a4027 2019-02-02 stsp name) == -1) {
1350 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1351 199a4027 2019-02-02 stsp free(s);
1352 199a4027 2019-02-02 stsp break;
1353 199a4027 2019-02-02 stsp }
1354 199a4027 2019-02-02 stsp free(s);
1355 199a4027 2019-02-02 stsp }
1356 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1357 8bf5b3c9 2018-03-17 stsp if (err)
1358 8bf5b3c9 2018-03-17 stsp return err;
1359 788c352e 2018-06-16 stsp
1360 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1361 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1362 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1363 832c249c 2018-06-10 stsp free(id_str);
1364 d3d493d7 2019-02-21 stsp id_str = NULL;
1365 d3d493d7 2019-02-21 stsp free(refs_str);
1366 d3d493d7 2019-02-21 stsp refs_str = NULL;
1367 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1368 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1369 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1370 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1371 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1372 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1373 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1374 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1375 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1376 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1377 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1378 3fe1abad 2018-06-10 stsp int n = 1;
1379 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1380 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1381 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1382 a0603db2 2018-06-10 stsp if (err)
1383 a0603db2 2018-06-10 stsp return err;
1384 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1385 a0603db2 2018-06-10 stsp free(id_str);
1386 a0603db2 2018-06-10 stsp }
1387 a0603db2 2018-06-10 stsp }
1388 832c249c 2018-06-10 stsp
1389 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1390 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1391 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1392 8bf5b3c9 2018-03-17 stsp
1393 621015ac 2018-07-23 stsp logmsg = logmsg0;
1394 832c249c 2018-06-10 stsp do {
1395 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1396 832c249c 2018-06-10 stsp if (line)
1397 832c249c 2018-06-10 stsp printf(" %s\n", line);
1398 832c249c 2018-06-10 stsp } while (line);
1399 621015ac 2018-07-23 stsp free(logmsg0);
1400 832c249c 2018-06-10 stsp
1401 971751ac 2018-03-27 stsp if (show_patch) {
1402 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1403 971751ac 2018-03-27 stsp if (err == 0)
1404 971751ac 2018-03-27 stsp printf("\n");
1405 971751ac 2018-03-27 stsp }
1406 07862c20 2018-09-15 stsp
1407 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1408 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1409 07862c20 2018-09-15 stsp return err;
1410 f42b1b34 2018-03-12 stsp }
1411 5c860e29 2018-03-12 stsp
1412 f42b1b34 2018-03-12 stsp static const struct got_error *
1413 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1414 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1415 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1416 f42b1b34 2018-03-12 stsp {
1417 f42b1b34 2018-03-12 stsp const struct got_error *err;
1418 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1419 372ccdbb 2018-06-10 stsp
1420 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1421 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1422 f42b1b34 2018-03-12 stsp if (err)
1423 f42b1b34 2018-03-12 stsp return err;
1424 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1425 372ccdbb 2018-06-10 stsp if (err)
1426 fcc85cad 2018-07-22 stsp goto done;
1427 656b1f76 2019-05-11 jcs for (;;) {
1428 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1429 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1430 8bf5b3c9 2018-03-17 stsp
1431 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1432 84453469 2018-11-11 stsp break;
1433 84453469 2018-11-11 stsp
1434 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1435 372ccdbb 2018-06-10 stsp if (err) {
1436 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1437 9ba79e04 2018-06-11 stsp err = NULL;
1438 9ba79e04 2018-06-11 stsp break;
1439 9ba79e04 2018-06-11 stsp }
1440 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1441 372ccdbb 2018-06-10 stsp break;
1442 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1443 372ccdbb 2018-06-10 stsp if (err)
1444 372ccdbb 2018-06-10 stsp break;
1445 372ccdbb 2018-06-10 stsp else
1446 372ccdbb 2018-06-10 stsp continue;
1447 7e665116 2018-04-02 stsp }
1448 b43fbaa0 2018-06-11 stsp if (id == NULL)
1449 7e665116 2018-04-02 stsp break;
1450 b43fbaa0 2018-06-11 stsp
1451 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1452 b43fbaa0 2018-06-11 stsp if (err)
1453 fcc85cad 2018-07-22 stsp break;
1454 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1455 199a4027 2019-02-02 stsp refs);
1456 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1457 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1458 7e665116 2018-04-02 stsp break;
1459 31cedeaf 2018-09-15 stsp }
1460 fcc85cad 2018-07-22 stsp done:
1461 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1462 f42b1b34 2018-03-12 stsp return err;
1463 f42b1b34 2018-03-12 stsp }
1464 5c860e29 2018-03-12 stsp
1465 4ed7e80c 2018-05-20 stsp __dead static void
1466 6f3d1eb0 2018-03-12 stsp usage_log(void)
1467 6f3d1eb0 2018-03-12 stsp {
1468 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1469 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1470 6f3d1eb0 2018-03-12 stsp exit(1);
1471 6f3d1eb0 2018-03-12 stsp }
1472 6f3d1eb0 2018-03-12 stsp
1473 4ed7e80c 2018-05-20 stsp static const struct got_error *
1474 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1475 f42b1b34 2018-03-12 stsp {
1476 f42b1b34 2018-03-12 stsp const struct got_error *error;
1477 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1478 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1479 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1480 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1481 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1482 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1483 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1484 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1485 64a96a6d 2018-04-01 stsp const char *errstr;
1486 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1487 1b3893a2 2019-03-18 stsp
1488 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1489 5c860e29 2018-03-12 stsp
1490 6715a751 2018-03-16 stsp #ifndef PROFILE
1491 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1492 6098196c 2019-01-04 stsp NULL)
1493 ad242220 2018-09-08 stsp == -1)
1494 f42b1b34 2018-03-12 stsp err(1, "pledge");
1495 6715a751 2018-03-16 stsp #endif
1496 79109fed 2018-03-27 stsp
1497 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1498 79109fed 2018-03-27 stsp switch (ch) {
1499 79109fed 2018-03-27 stsp case 'p':
1500 79109fed 2018-03-27 stsp show_patch = 1;
1501 d142fc45 2018-04-01 stsp break;
1502 d142fc45 2018-04-01 stsp case 'c':
1503 d142fc45 2018-04-01 stsp start_commit = optarg;
1504 64a96a6d 2018-04-01 stsp break;
1505 c0cc5c62 2018-10-18 stsp case 'C':
1506 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1507 4a8520aa 2018-10-18 stsp &errstr);
1508 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1509 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1510 c0cc5c62 2018-10-18 stsp break;
1511 64a96a6d 2018-04-01 stsp case 'l':
1512 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1513 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1514 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1515 cc54c501 2019-07-15 stsp break;
1516 cc54c501 2019-07-15 stsp case 'f':
1517 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1518 a0603db2 2018-06-10 stsp break;
1519 04ca23f4 2018-07-16 stsp case 'r':
1520 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1521 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1522 04ca23f4 2018-07-16 stsp err(1, "-r option");
1523 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1524 04ca23f4 2018-07-16 stsp break;
1525 79109fed 2018-03-27 stsp default:
1526 2deda0b9 2019-03-07 stsp usage_log();
1527 79109fed 2018-03-27 stsp /* NOTREACHED */
1528 79109fed 2018-03-27 stsp }
1529 79109fed 2018-03-27 stsp }
1530 79109fed 2018-03-27 stsp
1531 79109fed 2018-03-27 stsp argc -= optind;
1532 79109fed 2018-03-27 stsp argv += optind;
1533 f42b1b34 2018-03-12 stsp
1534 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1535 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1536 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1537 04ca23f4 2018-07-16 stsp goto done;
1538 04ca23f4 2018-07-16 stsp }
1539 cffc0aa4 2019-02-05 stsp
1540 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1541 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1542 cffc0aa4 2019-02-05 stsp goto done;
1543 cffc0aa4 2019-02-05 stsp error = NULL;
1544 cffc0aa4 2019-02-05 stsp
1545 e7301579 2019-03-18 stsp if (argc == 0) {
1546 cbd1af7a 2019-03-18 stsp path = strdup("");
1547 e7301579 2019-03-18 stsp if (path == NULL) {
1548 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1549 cbd1af7a 2019-03-18 stsp goto done;
1550 e7301579 2019-03-18 stsp }
1551 e7301579 2019-03-18 stsp } else if (argc == 1) {
1552 e7301579 2019-03-18 stsp if (worktree) {
1553 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1554 e7301579 2019-03-18 stsp argv[0]);
1555 e7301579 2019-03-18 stsp if (error)
1556 e7301579 2019-03-18 stsp goto done;
1557 e7301579 2019-03-18 stsp } else {
1558 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1559 e7301579 2019-03-18 stsp if (path == NULL) {
1560 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1561 e7301579 2019-03-18 stsp goto done;
1562 e7301579 2019-03-18 stsp }
1563 e7301579 2019-03-18 stsp }
1564 cbd1af7a 2019-03-18 stsp } else
1565 cbd1af7a 2019-03-18 stsp usage_log();
1566 cbd1af7a 2019-03-18 stsp
1567 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1568 5486daa2 2019-05-11 stsp repo_path = worktree ?
1569 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1570 5486daa2 2019-05-11 stsp }
1571 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1572 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1573 cffc0aa4 2019-02-05 stsp goto done;
1574 04ca23f4 2018-07-16 stsp }
1575 6098196c 2019-01-04 stsp
1576 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1577 d7d4f210 2018-03-12 stsp if (error != NULL)
1578 04ca23f4 2018-07-16 stsp goto done;
1579 f42b1b34 2018-03-12 stsp
1580 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1581 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1582 c02c541e 2019-03-29 stsp if (error)
1583 c02c541e 2019-03-29 stsp goto done;
1584 c02c541e 2019-03-29 stsp
1585 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1586 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1587 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1588 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1589 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1590 3235492e 2018-04-01 stsp if (error != NULL)
1591 3235492e 2018-04-01 stsp return error;
1592 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1593 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1594 3235492e 2018-04-01 stsp if (error != NULL)
1595 3235492e 2018-04-01 stsp return error;
1596 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1597 3235492e 2018-04-01 stsp } else {
1598 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1599 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1600 3235492e 2018-04-01 stsp if (error == NULL) {
1601 1caad61b 2019-02-01 stsp int obj_type;
1602 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1603 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1604 d1f2edc9 2018-06-13 stsp if (error != NULL)
1605 1caad61b 2019-02-01 stsp goto done;
1606 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1607 1caad61b 2019-02-01 stsp if (error != NULL)
1608 1caad61b 2019-02-01 stsp goto done;
1609 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1610 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1611 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1612 1caad61b 2019-02-01 stsp if (error != NULL)
1613 1caad61b 2019-02-01 stsp goto done;
1614 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1615 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1616 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1617 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1618 1caad61b 2019-02-01 stsp goto done;
1619 1caad61b 2019-02-01 stsp }
1620 1caad61b 2019-02-01 stsp free(id);
1621 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1622 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1623 1caad61b 2019-02-01 stsp if (id == NULL)
1624 638f9024 2019-05-13 stsp error = got_error_from_errno(
1625 230a42bd 2019-05-11 jcs "got_object_id_dup");
1626 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1627 1caad61b 2019-02-01 stsp if (error)
1628 1caad61b 2019-02-01 stsp goto done;
1629 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1630 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1631 1caad61b 2019-02-01 stsp goto done;
1632 1caad61b 2019-02-01 stsp }
1633 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1634 d1f2edc9 2018-06-13 stsp if (error != NULL)
1635 1caad61b 2019-02-01 stsp goto done;
1636 d1f2edc9 2018-06-13 stsp }
1637 15a94983 2018-12-23 stsp if (commit == NULL) {
1638 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1639 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1640 d1f2edc9 2018-06-13 stsp if (error != NULL)
1641 d1f2edc9 2018-06-13 stsp return error;
1642 3235492e 2018-04-01 stsp }
1643 3235492e 2018-04-01 stsp }
1644 d7d4f210 2018-03-12 stsp if (error != NULL)
1645 04ca23f4 2018-07-16 stsp goto done;
1646 04ca23f4 2018-07-16 stsp
1647 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1648 04ca23f4 2018-07-16 stsp if (error != NULL)
1649 04ca23f4 2018-07-16 stsp goto done;
1650 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1651 04ca23f4 2018-07-16 stsp free(path);
1652 04ca23f4 2018-07-16 stsp path = in_repo_path;
1653 04ca23f4 2018-07-16 stsp }
1654 199a4027 2019-02-02 stsp
1655 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1656 199a4027 2019-02-02 stsp if (error)
1657 199a4027 2019-02-02 stsp goto done;
1658 04ca23f4 2018-07-16 stsp
1659 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1660 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1661 04ca23f4 2018-07-16 stsp done:
1662 04ca23f4 2018-07-16 stsp free(path);
1663 04ca23f4 2018-07-16 stsp free(repo_path);
1664 04ca23f4 2018-07-16 stsp free(cwd);
1665 f42b1b34 2018-03-12 stsp free(id);
1666 cffc0aa4 2019-02-05 stsp if (worktree)
1667 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1668 ad242220 2018-09-08 stsp if (repo) {
1669 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1670 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1671 ad242220 2018-09-08 stsp if (error == NULL)
1672 ad242220 2018-09-08 stsp error = repo_error;
1673 ad242220 2018-09-08 stsp }
1674 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1675 8bf5b3c9 2018-03-17 stsp return error;
1676 5c860e29 2018-03-12 stsp }
1677 5c860e29 2018-03-12 stsp
1678 4ed7e80c 2018-05-20 stsp __dead static void
1679 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1680 3f8b7d6a 2018-04-01 stsp {
1681 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1682 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1683 3f8b7d6a 2018-04-01 stsp exit(1);
1684 b00d56cd 2018-04-01 stsp }
1685 b00d56cd 2018-04-01 stsp
1686 b72f483a 2019-02-05 stsp struct print_diff_arg {
1687 b72f483a 2019-02-05 stsp struct got_repository *repo;
1688 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1689 b72f483a 2019-02-05 stsp int diff_context;
1690 3fc0c068 2019-02-10 stsp const char *id_str;
1691 3fc0c068 2019-02-10 stsp int header_shown;
1692 b72f483a 2019-02-05 stsp };
1693 b72f483a 2019-02-05 stsp
1694 4ed7e80c 2018-05-20 stsp static const struct got_error *
1695 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1696 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1697 b72f483a 2019-02-05 stsp {
1698 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1699 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1700 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1701 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1702 b72f483a 2019-02-05 stsp char *abspath = NULL;
1703 b72f483a 2019-02-05 stsp struct stat sb;
1704 b72f483a 2019-02-05 stsp
1705 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1706 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1707 b72f483a 2019-02-05 stsp return NULL;
1708 3fc0c068 2019-02-10 stsp
1709 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1710 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1711 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1712 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1713 3fc0c068 2019-02-10 stsp }
1714 b72f483a 2019-02-05 stsp
1715 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1716 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1717 d00136be 2019-03-26 stsp if (err)
1718 d00136be 2019-03-26 stsp goto done;
1719 b72f483a 2019-02-05 stsp
1720 d00136be 2019-03-26 stsp }
1721 d00136be 2019-03-26 stsp
1722 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1723 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1724 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1725 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1726 2ec1f75b 2019-03-26 stsp goto done;
1727 2ec1f75b 2019-03-26 stsp }
1728 b72f483a 2019-02-05 stsp
1729 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1730 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1731 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1732 2ec1f75b 2019-03-26 stsp goto done;
1733 2ec1f75b 2019-03-26 stsp }
1734 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1735 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1736 2ec1f75b 2019-03-26 stsp goto done;
1737 2ec1f75b 2019-03-26 stsp }
1738 2ec1f75b 2019-03-26 stsp } else
1739 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1740 b72f483a 2019-02-05 stsp
1741 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1742 b72f483a 2019-02-05 stsp stdout);
1743 b72f483a 2019-02-05 stsp done:
1744 b72f483a 2019-02-05 stsp if (blob1)
1745 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1746 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1747 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1748 b72f483a 2019-02-05 stsp free(abspath);
1749 927df6b7 2019-02-10 stsp return err;
1750 927df6b7 2019-02-10 stsp }
1751 927df6b7 2019-02-10 stsp
1752 927df6b7 2019-02-10 stsp static const struct got_error *
1753 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1754 b00d56cd 2018-04-01 stsp {
1755 b00d56cd 2018-04-01 stsp const struct got_error *error;
1756 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1757 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1758 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1759 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1760 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1761 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1762 15a94983 2018-12-23 stsp int type1, type2;
1763 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1764 c0cc5c62 2018-10-18 stsp const char *errstr;
1765 927df6b7 2019-02-10 stsp char *path = NULL;
1766 b00d56cd 2018-04-01 stsp
1767 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1768 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1769 25eccc22 2019-01-04 stsp NULL) == -1)
1770 b00d56cd 2018-04-01 stsp err(1, "pledge");
1771 b00d56cd 2018-04-01 stsp #endif
1772 b00d56cd 2018-04-01 stsp
1773 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1774 b00d56cd 2018-04-01 stsp switch (ch) {
1775 c0cc5c62 2018-10-18 stsp case 'C':
1776 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1777 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1778 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1779 c0cc5c62 2018-10-18 stsp break;
1780 b72f483a 2019-02-05 stsp case 'r':
1781 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1782 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1783 b72f483a 2019-02-05 stsp err(1, "-r option");
1784 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1785 b72f483a 2019-02-05 stsp break;
1786 b00d56cd 2018-04-01 stsp default:
1787 2deda0b9 2019-03-07 stsp usage_diff();
1788 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1789 b00d56cd 2018-04-01 stsp }
1790 b00d56cd 2018-04-01 stsp }
1791 b00d56cd 2018-04-01 stsp
1792 b00d56cd 2018-04-01 stsp argc -= optind;
1793 b00d56cd 2018-04-01 stsp argv += optind;
1794 b00d56cd 2018-04-01 stsp
1795 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1796 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1797 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1798 b72f483a 2019-02-05 stsp goto done;
1799 b72f483a 2019-02-05 stsp }
1800 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1801 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1802 927df6b7 2019-02-10 stsp goto done;
1803 927df6b7 2019-02-10 stsp if (argc <= 1) {
1804 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1805 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1806 927df6b7 2019-02-10 stsp goto done;
1807 927df6b7 2019-02-10 stsp }
1808 b72f483a 2019-02-05 stsp if (repo_path)
1809 b72f483a 2019-02-05 stsp errx(1,
1810 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1811 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1812 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1813 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1814 927df6b7 2019-02-10 stsp goto done;
1815 927df6b7 2019-02-10 stsp }
1816 927df6b7 2019-02-10 stsp if (argc == 1) {
1817 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1818 cbd1af7a 2019-03-18 stsp argv[0]);
1819 927df6b7 2019-02-10 stsp if (error)
1820 927df6b7 2019-02-10 stsp goto done;
1821 927df6b7 2019-02-10 stsp } else {
1822 927df6b7 2019-02-10 stsp path = strdup("");
1823 927df6b7 2019-02-10 stsp if (path == NULL) {
1824 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1825 927df6b7 2019-02-10 stsp goto done;
1826 927df6b7 2019-02-10 stsp }
1827 927df6b7 2019-02-10 stsp }
1828 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1829 15a94983 2018-12-23 stsp id_str1 = argv[0];
1830 15a94983 2018-12-23 stsp id_str2 = argv[1];
1831 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1832 30db809c 2019-06-05 stsp repo_path =
1833 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1834 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1835 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1836 30db809c 2019-06-05 stsp goto done;
1837 30db809c 2019-06-05 stsp }
1838 30db809c 2019-06-05 stsp }
1839 b00d56cd 2018-04-01 stsp } else
1840 b00d56cd 2018-04-01 stsp usage_diff();
1841 25eccc22 2019-01-04 stsp
1842 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1843 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1844 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1845 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1846 b72f483a 2019-02-05 stsp }
1847 b00d56cd 2018-04-01 stsp
1848 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1849 76089277 2018-04-01 stsp free(repo_path);
1850 b00d56cd 2018-04-01 stsp if (error != NULL)
1851 b00d56cd 2018-04-01 stsp goto done;
1852 b00d56cd 2018-04-01 stsp
1853 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1854 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1855 c02c541e 2019-03-29 stsp if (error)
1856 c02c541e 2019-03-29 stsp goto done;
1857 c02c541e 2019-03-29 stsp
1858 30db809c 2019-06-05 stsp if (argc <= 1) {
1859 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1860 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
1861 b72f483a 2019-02-05 stsp char *id_str;
1862 72ea6654 2019-07-27 stsp
1863 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
1864 72ea6654 2019-07-27 stsp
1865 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1866 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1867 b72f483a 2019-02-05 stsp if (error)
1868 b72f483a 2019-02-05 stsp goto done;
1869 b72f483a 2019-02-05 stsp arg.repo = repo;
1870 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1871 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1872 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1873 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1874 b72f483a 2019-02-05 stsp
1875 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
1876 72ea6654 2019-07-27 stsp if (error)
1877 72ea6654 2019-07-27 stsp goto done;
1878 72ea6654 2019-07-27 stsp
1879 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
1880 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1881 b72f483a 2019-02-05 stsp free(id_str);
1882 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
1883 b72f483a 2019-02-05 stsp goto done;
1884 b72f483a 2019-02-05 stsp }
1885 b72f483a 2019-02-05 stsp
1886 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1887 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1888 e02e74af 2019-05-28 stsp if (error) {
1889 e02e74af 2019-05-28 stsp struct got_reference *ref;
1890 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1891 e02e74af 2019-05-28 stsp goto done;
1892 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1893 e02e74af 2019-05-28 stsp if (error != NULL)
1894 e02e74af 2019-05-28 stsp goto done;
1895 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1896 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1897 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1898 5e70831e 2019-05-28 stsp goto done;
1899 5e70831e 2019-05-28 stsp }
1900 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1901 e02e74af 2019-05-28 stsp got_ref_close(ref);
1902 e02e74af 2019-05-28 stsp if (error != NULL)
1903 5e70831e 2019-05-28 stsp goto done;
1904 5e70831e 2019-05-28 stsp } else {
1905 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1906 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1907 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1908 e02e74af 2019-05-28 stsp goto done;
1909 5e70831e 2019-05-28 stsp }
1910 e02e74af 2019-05-28 stsp }
1911 b00d56cd 2018-04-01 stsp
1912 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1913 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1914 e02e74af 2019-05-28 stsp if (error) {
1915 e02e74af 2019-05-28 stsp struct got_reference *ref;
1916 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1917 e02e74af 2019-05-28 stsp goto done;
1918 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1919 e02e74af 2019-05-28 stsp if (error != NULL)
1920 5e70831e 2019-05-28 stsp goto done;
1921 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1922 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1923 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1924 e02e74af 2019-05-28 stsp goto done;
1925 5e70831e 2019-05-28 stsp }
1926 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1927 e02e74af 2019-05-28 stsp got_ref_close(ref);
1928 e02e74af 2019-05-28 stsp if (error != NULL)
1929 e02e74af 2019-05-28 stsp goto done;
1930 5e70831e 2019-05-28 stsp } else {
1931 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1932 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1933 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1934 5e70831e 2019-05-28 stsp goto done;
1935 5e70831e 2019-05-28 stsp }
1936 e02e74af 2019-05-28 stsp }
1937 b00d56cd 2018-04-01 stsp
1938 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1939 15a94983 2018-12-23 stsp if (error)
1940 15a94983 2018-12-23 stsp goto done;
1941 15a94983 2018-12-23 stsp
1942 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1943 15a94983 2018-12-23 stsp if (error)
1944 15a94983 2018-12-23 stsp goto done;
1945 15a94983 2018-12-23 stsp
1946 15a94983 2018-12-23 stsp if (type1 != type2) {
1947 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1948 b00d56cd 2018-04-01 stsp goto done;
1949 b00d56cd 2018-04-01 stsp }
1950 b00d56cd 2018-04-01 stsp
1951 15a94983 2018-12-23 stsp switch (type1) {
1952 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1953 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1954 54156555 2018-12-24 stsp diff_context, repo, stdout);
1955 b00d56cd 2018-04-01 stsp break;
1956 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1957 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1958 54156555 2018-12-24 stsp diff_context, repo, stdout);
1959 b00d56cd 2018-04-01 stsp break;
1960 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1961 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1962 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1963 c0cc5c62 2018-10-18 stsp repo, stdout);
1964 b00d56cd 2018-04-01 stsp break;
1965 b00d56cd 2018-04-01 stsp default:
1966 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1967 b00d56cd 2018-04-01 stsp }
1968 b00d56cd 2018-04-01 stsp
1969 b00d56cd 2018-04-01 stsp done:
1970 5e70831e 2019-05-28 stsp free(label1);
1971 5e70831e 2019-05-28 stsp free(label2);
1972 15a94983 2018-12-23 stsp free(id1);
1973 15a94983 2018-12-23 stsp free(id2);
1974 927df6b7 2019-02-10 stsp free(path);
1975 b72f483a 2019-02-05 stsp if (worktree)
1976 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1977 ad242220 2018-09-08 stsp if (repo) {
1978 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1979 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1980 ad242220 2018-09-08 stsp if (error == NULL)
1981 ad242220 2018-09-08 stsp error = repo_error;
1982 ad242220 2018-09-08 stsp }
1983 b00d56cd 2018-04-01 stsp return error;
1984 404c43c4 2018-06-21 stsp }
1985 404c43c4 2018-06-21 stsp
1986 404c43c4 2018-06-21 stsp __dead static void
1987 404c43c4 2018-06-21 stsp usage_blame(void)
1988 404c43c4 2018-06-21 stsp {
1989 9270e621 2019-02-05 stsp fprintf(stderr,
1990 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1991 404c43c4 2018-06-21 stsp getprogname());
1992 404c43c4 2018-06-21 stsp exit(1);
1993 b00d56cd 2018-04-01 stsp }
1994 b00d56cd 2018-04-01 stsp
1995 404c43c4 2018-06-21 stsp static const struct got_error *
1996 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1997 404c43c4 2018-06-21 stsp {
1998 404c43c4 2018-06-21 stsp const struct got_error *error;
1999 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2000 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2001 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2002 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2003 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2004 404c43c4 2018-06-21 stsp int ch;
2005 404c43c4 2018-06-21 stsp
2006 404c43c4 2018-06-21 stsp #ifndef PROFILE
2007 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2008 36e2fb66 2019-01-04 stsp NULL) == -1)
2009 404c43c4 2018-06-21 stsp err(1, "pledge");
2010 404c43c4 2018-06-21 stsp #endif
2011 404c43c4 2018-06-21 stsp
2012 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2013 404c43c4 2018-06-21 stsp switch (ch) {
2014 404c43c4 2018-06-21 stsp case 'c':
2015 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2016 404c43c4 2018-06-21 stsp break;
2017 66bea077 2018-08-02 stsp case 'r':
2018 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2019 66bea077 2018-08-02 stsp if (repo_path == NULL)
2020 66bea077 2018-08-02 stsp err(1, "-r option");
2021 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2022 66bea077 2018-08-02 stsp break;
2023 404c43c4 2018-06-21 stsp default:
2024 2deda0b9 2019-03-07 stsp usage_blame();
2025 404c43c4 2018-06-21 stsp /* NOTREACHED */
2026 404c43c4 2018-06-21 stsp }
2027 404c43c4 2018-06-21 stsp }
2028 404c43c4 2018-06-21 stsp
2029 404c43c4 2018-06-21 stsp argc -= optind;
2030 404c43c4 2018-06-21 stsp argv += optind;
2031 404c43c4 2018-06-21 stsp
2032 a39318fd 2018-08-02 stsp if (argc == 1)
2033 404c43c4 2018-06-21 stsp path = argv[0];
2034 a39318fd 2018-08-02 stsp else
2035 404c43c4 2018-06-21 stsp usage_blame();
2036 404c43c4 2018-06-21 stsp
2037 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2038 66bea077 2018-08-02 stsp if (cwd == NULL) {
2039 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2040 66bea077 2018-08-02 stsp goto done;
2041 66bea077 2018-08-02 stsp }
2042 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2043 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2044 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2045 66bea077 2018-08-02 stsp goto done;
2046 0c06baac 2019-02-05 stsp else
2047 0c06baac 2019-02-05 stsp error = NULL;
2048 0c06baac 2019-02-05 stsp if (worktree) {
2049 0c06baac 2019-02-05 stsp repo_path =
2050 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2051 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2052 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2053 0c06baac 2019-02-05 stsp if (error)
2054 0c06baac 2019-02-05 stsp goto done;
2055 0c06baac 2019-02-05 stsp } else {
2056 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2057 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2058 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2059 0c06baac 2019-02-05 stsp goto done;
2060 0c06baac 2019-02-05 stsp }
2061 66bea077 2018-08-02 stsp }
2062 66bea077 2018-08-02 stsp }
2063 36e2fb66 2019-01-04 stsp
2064 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
2065 c02c541e 2019-03-29 stsp if (error != NULL)
2066 36e2fb66 2019-01-04 stsp goto done;
2067 66bea077 2018-08-02 stsp
2068 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2069 c02c541e 2019-03-29 stsp if (error)
2070 404c43c4 2018-06-21 stsp goto done;
2071 404c43c4 2018-06-21 stsp
2072 0c06baac 2019-02-05 stsp if (worktree) {
2073 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2074 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2075 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2076 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2077 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2078 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2079 6efaaa2d 2019-02-05 stsp path) == -1) {
2080 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2081 0c06baac 2019-02-05 stsp goto done;
2082 0c06baac 2019-02-05 stsp }
2083 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2084 0c06baac 2019-02-05 stsp free(p);
2085 0c06baac 2019-02-05 stsp } else {
2086 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2087 0c06baac 2019-02-05 stsp }
2088 0c06baac 2019-02-05 stsp if (error)
2089 66bea077 2018-08-02 stsp goto done;
2090 66bea077 2018-08-02 stsp
2091 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2092 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2093 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2094 404c43c4 2018-06-21 stsp if (error != NULL)
2095 66bea077 2018-08-02 stsp goto done;
2096 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2097 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2098 404c43c4 2018-06-21 stsp if (error != NULL)
2099 66bea077 2018-08-02 stsp goto done;
2100 404c43c4 2018-06-21 stsp } else {
2101 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2102 30837e32 2019-07-25 stsp if (error)
2103 66bea077 2018-08-02 stsp goto done;
2104 404c43c4 2018-06-21 stsp }
2105 404c43c4 2018-06-21 stsp
2106 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2107 404c43c4 2018-06-21 stsp done:
2108 66bea077 2018-08-02 stsp free(in_repo_path);
2109 66bea077 2018-08-02 stsp free(repo_path);
2110 66bea077 2018-08-02 stsp free(cwd);
2111 404c43c4 2018-06-21 stsp free(commit_id);
2112 0c06baac 2019-02-05 stsp if (worktree)
2113 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2114 ad242220 2018-09-08 stsp if (repo) {
2115 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2116 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2117 ad242220 2018-09-08 stsp if (error == NULL)
2118 ad242220 2018-09-08 stsp error = repo_error;
2119 ad242220 2018-09-08 stsp }
2120 404c43c4 2018-06-21 stsp return error;
2121 5de5890b 2018-10-18 stsp }
2122 5de5890b 2018-10-18 stsp
2123 5de5890b 2018-10-18 stsp __dead static void
2124 5de5890b 2018-10-18 stsp usage_tree(void)
2125 5de5890b 2018-10-18 stsp {
2126 c1669e2e 2019-01-09 stsp fprintf(stderr,
2127 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2128 5de5890b 2018-10-18 stsp getprogname());
2129 5de5890b 2018-10-18 stsp exit(1);
2130 5de5890b 2018-10-18 stsp }
2131 5de5890b 2018-10-18 stsp
2132 c1669e2e 2019-01-09 stsp static void
2133 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2134 c1669e2e 2019-01-09 stsp const char *root_path)
2135 c1669e2e 2019-01-09 stsp {
2136 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2137 5de5890b 2018-10-18 stsp
2138 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2139 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2140 c1669e2e 2019-01-09 stsp path++;
2141 c1669e2e 2019-01-09 stsp
2142 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2143 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2144 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2145 c1669e2e 2019-01-09 stsp }
2146 c1669e2e 2019-01-09 stsp
2147 5de5890b 2018-10-18 stsp static const struct got_error *
2148 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2149 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2150 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2151 5de5890b 2018-10-18 stsp {
2152 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2153 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2154 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2155 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2156 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2157 5de5890b 2018-10-18 stsp
2158 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2159 5de5890b 2018-10-18 stsp if (err)
2160 5de5890b 2018-10-18 stsp goto done;
2161 5de5890b 2018-10-18 stsp
2162 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2163 5de5890b 2018-10-18 stsp if (err)
2164 5de5890b 2018-10-18 stsp goto done;
2165 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2166 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2167 5de5890b 2018-10-18 stsp while (te) {
2168 5de5890b 2018-10-18 stsp char *id = NULL;
2169 84453469 2018-11-11 stsp
2170 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2171 84453469 2018-11-11 stsp break;
2172 84453469 2018-11-11 stsp
2173 5de5890b 2018-10-18 stsp if (show_ids) {
2174 5de5890b 2018-10-18 stsp char *id_str;
2175 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2176 5de5890b 2018-10-18 stsp if (err)
2177 5de5890b 2018-10-18 stsp goto done;
2178 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2179 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2180 5de5890b 2018-10-18 stsp free(id_str);
2181 5de5890b 2018-10-18 stsp goto done;
2182 5de5890b 2018-10-18 stsp }
2183 5de5890b 2018-10-18 stsp free(id_str);
2184 5de5890b 2018-10-18 stsp }
2185 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2186 5de5890b 2018-10-18 stsp free(id);
2187 c1669e2e 2019-01-09 stsp
2188 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2189 c1669e2e 2019-01-09 stsp char *child_path;
2190 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2191 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2192 c1669e2e 2019-01-09 stsp te->name) == -1) {
2193 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2194 c1669e2e 2019-01-09 stsp goto done;
2195 c1669e2e 2019-01-09 stsp }
2196 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2197 c1669e2e 2019-01-09 stsp root_path, repo);
2198 c1669e2e 2019-01-09 stsp free(child_path);
2199 c1669e2e 2019-01-09 stsp if (err)
2200 c1669e2e 2019-01-09 stsp goto done;
2201 c1669e2e 2019-01-09 stsp }
2202 c1669e2e 2019-01-09 stsp
2203 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2204 5de5890b 2018-10-18 stsp }
2205 5de5890b 2018-10-18 stsp done:
2206 5de5890b 2018-10-18 stsp if (tree)
2207 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2208 5de5890b 2018-10-18 stsp free(tree_id);
2209 5de5890b 2018-10-18 stsp return err;
2210 404c43c4 2018-06-21 stsp }
2211 404c43c4 2018-06-21 stsp
2212 5de5890b 2018-10-18 stsp static const struct got_error *
2213 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2214 5de5890b 2018-10-18 stsp {
2215 5de5890b 2018-10-18 stsp const struct got_error *error;
2216 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2217 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2218 7a2c19d6 2019-02-05 stsp const char *path;
2219 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2220 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2221 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2222 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2223 5de5890b 2018-10-18 stsp int ch;
2224 5de5890b 2018-10-18 stsp
2225 5de5890b 2018-10-18 stsp #ifndef PROFILE
2226 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2227 0f8d269b 2019-01-04 stsp NULL) == -1)
2228 5de5890b 2018-10-18 stsp err(1, "pledge");
2229 5de5890b 2018-10-18 stsp #endif
2230 5de5890b 2018-10-18 stsp
2231 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2232 5de5890b 2018-10-18 stsp switch (ch) {
2233 5de5890b 2018-10-18 stsp case 'c':
2234 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2235 5de5890b 2018-10-18 stsp break;
2236 5de5890b 2018-10-18 stsp case 'r':
2237 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2238 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2239 5de5890b 2018-10-18 stsp err(1, "-r option");
2240 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2241 5de5890b 2018-10-18 stsp break;
2242 5de5890b 2018-10-18 stsp case 'i':
2243 5de5890b 2018-10-18 stsp show_ids = 1;
2244 5de5890b 2018-10-18 stsp break;
2245 c1669e2e 2019-01-09 stsp case 'R':
2246 c1669e2e 2019-01-09 stsp recurse = 1;
2247 c1669e2e 2019-01-09 stsp break;
2248 5de5890b 2018-10-18 stsp default:
2249 2deda0b9 2019-03-07 stsp usage_tree();
2250 5de5890b 2018-10-18 stsp /* NOTREACHED */
2251 5de5890b 2018-10-18 stsp }
2252 5de5890b 2018-10-18 stsp }
2253 5de5890b 2018-10-18 stsp
2254 5de5890b 2018-10-18 stsp argc -= optind;
2255 5de5890b 2018-10-18 stsp argv += optind;
2256 5de5890b 2018-10-18 stsp
2257 5de5890b 2018-10-18 stsp if (argc == 1)
2258 5de5890b 2018-10-18 stsp path = argv[0];
2259 5de5890b 2018-10-18 stsp else if (argc > 1)
2260 5de5890b 2018-10-18 stsp usage_tree();
2261 5de5890b 2018-10-18 stsp else
2262 7a2c19d6 2019-02-05 stsp path = NULL;
2263 7a2c19d6 2019-02-05 stsp
2264 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2265 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2266 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2267 9bf7a39b 2019-02-05 stsp goto done;
2268 9bf7a39b 2019-02-05 stsp }
2269 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2270 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2271 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2272 7a2c19d6 2019-02-05 stsp goto done;
2273 7a2c19d6 2019-02-05 stsp else
2274 7a2c19d6 2019-02-05 stsp error = NULL;
2275 7a2c19d6 2019-02-05 stsp if (worktree) {
2276 7a2c19d6 2019-02-05 stsp repo_path =
2277 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2278 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2279 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2280 7a2c19d6 2019-02-05 stsp if (error)
2281 7a2c19d6 2019-02-05 stsp goto done;
2282 7a2c19d6 2019-02-05 stsp } else {
2283 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2284 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2285 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2286 7a2c19d6 2019-02-05 stsp goto done;
2287 7a2c19d6 2019-02-05 stsp }
2288 5de5890b 2018-10-18 stsp }
2289 5de5890b 2018-10-18 stsp }
2290 5de5890b 2018-10-18 stsp
2291 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2292 5de5890b 2018-10-18 stsp if (error != NULL)
2293 c02c541e 2019-03-29 stsp goto done;
2294 c02c541e 2019-03-29 stsp
2295 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2296 c02c541e 2019-03-29 stsp if (error)
2297 5de5890b 2018-10-18 stsp goto done;
2298 5de5890b 2018-10-18 stsp
2299 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2300 9bf7a39b 2019-02-05 stsp if (worktree) {
2301 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2302 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2303 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2304 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2305 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2306 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2307 9bf7a39b 2019-02-05 stsp goto done;
2308 9bf7a39b 2019-02-05 stsp }
2309 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2310 9bf7a39b 2019-02-05 stsp free(p);
2311 9bf7a39b 2019-02-05 stsp if (error)
2312 9bf7a39b 2019-02-05 stsp goto done;
2313 9bf7a39b 2019-02-05 stsp } else
2314 9bf7a39b 2019-02-05 stsp path = "/";
2315 9bf7a39b 2019-02-05 stsp }
2316 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2317 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2318 9bf7a39b 2019-02-05 stsp if (error != NULL)
2319 9bf7a39b 2019-02-05 stsp goto done;
2320 9bf7a39b 2019-02-05 stsp }
2321 5de5890b 2018-10-18 stsp
2322 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2323 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2324 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2325 5de5890b 2018-10-18 stsp if (error != NULL)
2326 5de5890b 2018-10-18 stsp goto done;
2327 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2328 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2329 5de5890b 2018-10-18 stsp if (error != NULL)
2330 5de5890b 2018-10-18 stsp goto done;
2331 5de5890b 2018-10-18 stsp } else {
2332 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2333 30837e32 2019-07-25 stsp if (error)
2334 5de5890b 2018-10-18 stsp goto done;
2335 5de5890b 2018-10-18 stsp }
2336 5de5890b 2018-10-18 stsp
2337 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2338 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2339 5de5890b 2018-10-18 stsp done:
2340 5de5890b 2018-10-18 stsp free(in_repo_path);
2341 5de5890b 2018-10-18 stsp free(repo_path);
2342 5de5890b 2018-10-18 stsp free(cwd);
2343 5de5890b 2018-10-18 stsp free(commit_id);
2344 7a2c19d6 2019-02-05 stsp if (worktree)
2345 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2346 5de5890b 2018-10-18 stsp if (repo) {
2347 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2348 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2349 5de5890b 2018-10-18 stsp if (error == NULL)
2350 5de5890b 2018-10-18 stsp error = repo_error;
2351 5de5890b 2018-10-18 stsp }
2352 5de5890b 2018-10-18 stsp return error;
2353 5de5890b 2018-10-18 stsp }
2354 5de5890b 2018-10-18 stsp
2355 6bad629b 2019-02-04 stsp __dead static void
2356 6bad629b 2019-02-04 stsp usage_status(void)
2357 6bad629b 2019-02-04 stsp {
2358 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2359 6bad629b 2019-02-04 stsp exit(1);
2360 6bad629b 2019-02-04 stsp }
2361 5c860e29 2018-03-12 stsp
2362 b72f483a 2019-02-05 stsp static const struct got_error *
2363 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2364 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2365 6bad629b 2019-02-04 stsp {
2366 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2367 b72f483a 2019-02-05 stsp return NULL;
2368 6bad629b 2019-02-04 stsp }
2369 5c860e29 2018-03-12 stsp
2370 6bad629b 2019-02-04 stsp static const struct got_error *
2371 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2372 6bad629b 2019-02-04 stsp {
2373 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2374 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2375 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2376 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2377 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2378 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2379 a5edda0a 2019-07-27 stsp int ch;
2380 5c860e29 2018-03-12 stsp
2381 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2382 72ea6654 2019-07-27 stsp
2383 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2384 6bad629b 2019-02-04 stsp switch (ch) {
2385 5c860e29 2018-03-12 stsp default:
2386 2deda0b9 2019-03-07 stsp usage_status();
2387 6bad629b 2019-02-04 stsp /* NOTREACHED */
2388 5c860e29 2018-03-12 stsp }
2389 5c860e29 2018-03-12 stsp }
2390 5c860e29 2018-03-12 stsp
2391 6bad629b 2019-02-04 stsp argc -= optind;
2392 6bad629b 2019-02-04 stsp argv += optind;
2393 5c860e29 2018-03-12 stsp
2394 6bad629b 2019-02-04 stsp #ifndef PROFILE
2395 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2396 6bad629b 2019-02-04 stsp NULL) == -1)
2397 6bad629b 2019-02-04 stsp err(1, "pledge");
2398 f42b1b34 2018-03-12 stsp #endif
2399 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2400 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2401 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2402 927df6b7 2019-02-10 stsp goto done;
2403 927df6b7 2019-02-10 stsp }
2404 927df6b7 2019-02-10 stsp
2405 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2406 927df6b7 2019-02-10 stsp if (error != NULL)
2407 927df6b7 2019-02-10 stsp goto done;
2408 927df6b7 2019-02-10 stsp
2409 a5edda0a 2019-07-27 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2410 a5edda0a 2019-07-27 stsp if (error)
2411 a5edda0a 2019-07-27 stsp goto done;
2412 6bad629b 2019-02-04 stsp
2413 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2414 6bad629b 2019-02-04 stsp if (error != NULL)
2415 6bad629b 2019-02-04 stsp goto done;
2416 6bad629b 2019-02-04 stsp
2417 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2418 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2419 6bad629b 2019-02-04 stsp if (error)
2420 6bad629b 2019-02-04 stsp goto done;
2421 6bad629b 2019-02-04 stsp
2422 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2423 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2424 6bad629b 2019-02-04 stsp done:
2425 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2426 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2427 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2428 927df6b7 2019-02-10 stsp free(cwd);
2429 d0eebce4 2019-03-11 stsp return error;
2430 d0eebce4 2019-03-11 stsp }
2431 d0eebce4 2019-03-11 stsp
2432 d0eebce4 2019-03-11 stsp __dead static void
2433 d0eebce4 2019-03-11 stsp usage_ref(void)
2434 d0eebce4 2019-03-11 stsp {
2435 d0eebce4 2019-03-11 stsp fprintf(stderr,
2436 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2437 d0eebce4 2019-03-11 stsp getprogname());
2438 d0eebce4 2019-03-11 stsp exit(1);
2439 d0eebce4 2019-03-11 stsp }
2440 d0eebce4 2019-03-11 stsp
2441 d0eebce4 2019-03-11 stsp static const struct got_error *
2442 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2443 d0eebce4 2019-03-11 stsp {
2444 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2445 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2446 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2447 d0eebce4 2019-03-11 stsp
2448 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2449 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2450 d0eebce4 2019-03-11 stsp if (err)
2451 d0eebce4 2019-03-11 stsp return err;
2452 d0eebce4 2019-03-11 stsp
2453 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2454 d0eebce4 2019-03-11 stsp char *refstr;
2455 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2456 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2457 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2458 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2459 d0eebce4 2019-03-11 stsp free(refstr);
2460 d0eebce4 2019-03-11 stsp }
2461 d0eebce4 2019-03-11 stsp
2462 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2463 d0eebce4 2019-03-11 stsp return NULL;
2464 d0eebce4 2019-03-11 stsp }
2465 d0eebce4 2019-03-11 stsp
2466 d0eebce4 2019-03-11 stsp static const struct got_error *
2467 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2468 d0eebce4 2019-03-11 stsp {
2469 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2470 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2471 d0eebce4 2019-03-11 stsp
2472 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2473 d0eebce4 2019-03-11 stsp if (err)
2474 d0eebce4 2019-03-11 stsp return err;
2475 d0eebce4 2019-03-11 stsp
2476 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2477 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2478 d0eebce4 2019-03-11 stsp return err;
2479 d0eebce4 2019-03-11 stsp }
2480 d0eebce4 2019-03-11 stsp
2481 d0eebce4 2019-03-11 stsp static const struct got_error *
2482 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2483 d0eebce4 2019-03-11 stsp {
2484 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2485 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2486 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2487 d1644381 2019-07-14 stsp
2488 d1644381 2019-07-14 stsp /*
2489 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2490 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2491 d1644381 2019-07-14 stsp * an unintended typo.
2492 d1644381 2019-07-14 stsp */
2493 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2494 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2495 d0eebce4 2019-03-11 stsp
2496 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2497 dd88155e 2019-06-29 stsp repo);
2498 d83d9d5c 2019-05-13 stsp if (err) {
2499 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2500 d0eebce4 2019-03-11 stsp
2501 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2502 d83d9d5c 2019-05-13 stsp return err;
2503 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2504 d83d9d5c 2019-05-13 stsp if (err)
2505 d83d9d5c 2019-05-13 stsp return err;
2506 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2507 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2508 d83d9d5c 2019-05-13 stsp if (err)
2509 d83d9d5c 2019-05-13 stsp return err;
2510 d83d9d5c 2019-05-13 stsp }
2511 d83d9d5c 2019-05-13 stsp
2512 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2513 d0eebce4 2019-03-11 stsp if (err)
2514 d0eebce4 2019-03-11 stsp goto done;
2515 d0eebce4 2019-03-11 stsp
2516 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2517 d0eebce4 2019-03-11 stsp done:
2518 d0eebce4 2019-03-11 stsp if (ref)
2519 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2520 d0eebce4 2019-03-11 stsp free(id);
2521 d0eebce4 2019-03-11 stsp return err;
2522 d0eebce4 2019-03-11 stsp }
2523 d0eebce4 2019-03-11 stsp
2524 d0eebce4 2019-03-11 stsp static const struct got_error *
2525 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2526 d0eebce4 2019-03-11 stsp {
2527 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2528 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2529 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2530 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2531 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2532 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2533 d0eebce4 2019-03-11 stsp
2534 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2535 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2536 d0eebce4 2019-03-11 stsp switch (ch) {
2537 d0eebce4 2019-03-11 stsp case 'd':
2538 d0eebce4 2019-03-11 stsp delref = optarg;
2539 d0eebce4 2019-03-11 stsp break;
2540 d0eebce4 2019-03-11 stsp case 'r':
2541 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2542 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2543 d0eebce4 2019-03-11 stsp err(1, "-r option");
2544 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2545 d0eebce4 2019-03-11 stsp break;
2546 d0eebce4 2019-03-11 stsp case 'l':
2547 d0eebce4 2019-03-11 stsp do_list = 1;
2548 d0eebce4 2019-03-11 stsp break;
2549 d0eebce4 2019-03-11 stsp default:
2550 d0eebce4 2019-03-11 stsp usage_ref();
2551 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2552 d0eebce4 2019-03-11 stsp }
2553 d0eebce4 2019-03-11 stsp }
2554 d0eebce4 2019-03-11 stsp
2555 d0eebce4 2019-03-11 stsp if (do_list && delref)
2556 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2557 d0eebce4 2019-03-11 stsp
2558 d0eebce4 2019-03-11 stsp argc -= optind;
2559 d0eebce4 2019-03-11 stsp argv += optind;
2560 d0eebce4 2019-03-11 stsp
2561 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2562 d0eebce4 2019-03-11 stsp if (argc > 0)
2563 d0eebce4 2019-03-11 stsp usage_ref();
2564 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2565 d0eebce4 2019-03-11 stsp usage_ref();
2566 d0eebce4 2019-03-11 stsp
2567 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2568 e0b57350 2019-03-12 stsp if (do_list) {
2569 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2570 e0b57350 2019-03-12 stsp NULL) == -1)
2571 e0b57350 2019-03-12 stsp err(1, "pledge");
2572 e0b57350 2019-03-12 stsp } else {
2573 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2574 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2575 e0b57350 2019-03-12 stsp err(1, "pledge");
2576 e0b57350 2019-03-12 stsp }
2577 d0eebce4 2019-03-11 stsp #endif
2578 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2579 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2580 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2581 d0eebce4 2019-03-11 stsp goto done;
2582 d0eebce4 2019-03-11 stsp }
2583 d0eebce4 2019-03-11 stsp
2584 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2585 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2586 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2587 d0eebce4 2019-03-11 stsp goto done;
2588 d0eebce4 2019-03-11 stsp else
2589 d0eebce4 2019-03-11 stsp error = NULL;
2590 d0eebce4 2019-03-11 stsp if (worktree) {
2591 d0eebce4 2019-03-11 stsp repo_path =
2592 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2593 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2594 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2595 d0eebce4 2019-03-11 stsp if (error)
2596 d0eebce4 2019-03-11 stsp goto done;
2597 d0eebce4 2019-03-11 stsp } else {
2598 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2599 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2600 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2601 d0eebce4 2019-03-11 stsp goto done;
2602 d0eebce4 2019-03-11 stsp }
2603 d0eebce4 2019-03-11 stsp }
2604 d0eebce4 2019-03-11 stsp }
2605 d0eebce4 2019-03-11 stsp
2606 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2607 d0eebce4 2019-03-11 stsp if (error != NULL)
2608 d0eebce4 2019-03-11 stsp goto done;
2609 d0eebce4 2019-03-11 stsp
2610 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2611 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2612 c02c541e 2019-03-29 stsp if (error)
2613 c02c541e 2019-03-29 stsp goto done;
2614 c02c541e 2019-03-29 stsp
2615 d0eebce4 2019-03-11 stsp if (do_list)
2616 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2617 d0eebce4 2019-03-11 stsp else if (delref)
2618 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2619 d0eebce4 2019-03-11 stsp else
2620 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2621 4e759de4 2019-06-26 stsp done:
2622 4e759de4 2019-06-26 stsp if (repo)
2623 4e759de4 2019-06-26 stsp got_repo_close(repo);
2624 4e759de4 2019-06-26 stsp if (worktree)
2625 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2626 4e759de4 2019-06-26 stsp free(cwd);
2627 4e759de4 2019-06-26 stsp free(repo_path);
2628 4e759de4 2019-06-26 stsp return error;
2629 4e759de4 2019-06-26 stsp }
2630 4e759de4 2019-06-26 stsp
2631 4e759de4 2019-06-26 stsp __dead static void
2632 4e759de4 2019-06-26 stsp usage_branch(void)
2633 4e759de4 2019-06-26 stsp {
2634 4e759de4 2019-06-26 stsp fprintf(stderr,
2635 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2636 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2637 4e759de4 2019-06-26 stsp exit(1);
2638 4e759de4 2019-06-26 stsp }
2639 4e759de4 2019-06-26 stsp
2640 4e759de4 2019-06-26 stsp static const struct got_error *
2641 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2642 4e759de4 2019-06-26 stsp {
2643 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2644 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2645 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2646 4e759de4 2019-06-26 stsp
2647 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2648 ba882ee3 2019-07-11 stsp
2649 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2650 4e759de4 2019-06-26 stsp if (err)
2651 4e759de4 2019-06-26 stsp return err;
2652 4e759de4 2019-06-26 stsp
2653 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2654 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2655 4e759de4 2019-06-26 stsp char *refstr;
2656 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2657 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2658 4e759de4 2019-06-26 stsp continue;
2659 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2660 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2661 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2662 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2663 ba882ee3 2019-07-11 stsp if (err)
2664 ba882ee3 2019-07-11 stsp return err;
2665 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2666 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2667 ba882ee3 2019-07-11 stsp marker = "* ";
2668 ba882ee3 2019-07-11 stsp else
2669 ba882ee3 2019-07-11 stsp marker = "~ ";
2670 ba882ee3 2019-07-11 stsp free(id);
2671 ba882ee3 2019-07-11 stsp }
2672 4e759de4 2019-06-26 stsp refname += 11;
2673 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2674 4e759de4 2019-06-26 stsp if (refstr == NULL)
2675 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2676 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2677 4e759de4 2019-06-26 stsp free(refstr);
2678 4e759de4 2019-06-26 stsp }
2679 4e759de4 2019-06-26 stsp
2680 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2681 4e759de4 2019-06-26 stsp return NULL;
2682 4e759de4 2019-06-26 stsp }
2683 4e759de4 2019-06-26 stsp
2684 4e759de4 2019-06-26 stsp static const struct got_error *
2685 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2686 4e759de4 2019-06-26 stsp {
2687 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2688 4e759de4 2019-06-26 stsp struct got_reference *ref;
2689 4e759de4 2019-06-26 stsp char *refname;
2690 4e759de4 2019-06-26 stsp
2691 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2692 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2693 4e759de4 2019-06-26 stsp
2694 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2695 4e759de4 2019-06-26 stsp if (err)
2696 4e759de4 2019-06-26 stsp goto done;
2697 4e759de4 2019-06-26 stsp
2698 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2699 4e759de4 2019-06-26 stsp got_ref_close(ref);
2700 4e759de4 2019-06-26 stsp done:
2701 4e759de4 2019-06-26 stsp free(refname);
2702 4e759de4 2019-06-26 stsp return err;
2703 4e759de4 2019-06-26 stsp }
2704 4e759de4 2019-06-26 stsp
2705 4e759de4 2019-06-26 stsp static const struct got_error *
2706 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2707 4e759de4 2019-06-26 stsp const char *base_branch)
2708 4e759de4 2019-06-26 stsp {
2709 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2710 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2711 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2712 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2713 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2714 d3f84d51 2019-07-11 stsp
2715 d3f84d51 2019-07-11 stsp /*
2716 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2717 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2718 d3f84d51 2019-07-11 stsp * an unintended typo.
2719 d3f84d51 2019-07-11 stsp */
2720 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2721 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2722 4e759de4 2019-06-26 stsp
2723 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2724 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2725 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2726 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2727 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2728 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2729 4e759de4 2019-06-26 stsp
2730 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2731 4e759de4 2019-06-26 stsp if (err)
2732 4e759de4 2019-06-26 stsp goto done;
2733 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2734 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2735 4e759de4 2019-06-26 stsp if (err)
2736 4e759de4 2019-06-26 stsp goto done;
2737 4e759de4 2019-06-26 stsp
2738 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2739 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2740 4e759de4 2019-06-26 stsp goto done;
2741 4e759de4 2019-06-26 stsp }
2742 4e759de4 2019-06-26 stsp
2743 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2744 4e759de4 2019-06-26 stsp if (err == NULL) {
2745 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2746 4e759de4 2019-06-26 stsp goto done;
2747 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2748 4e759de4 2019-06-26 stsp goto done;
2749 4e759de4 2019-06-26 stsp
2750 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2751 4e759de4 2019-06-26 stsp if (err)
2752 4e759de4 2019-06-26 stsp goto done;
2753 4e759de4 2019-06-26 stsp
2754 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2755 d0eebce4 2019-03-11 stsp done:
2756 4e759de4 2019-06-26 stsp if (ref)
2757 4e759de4 2019-06-26 stsp got_ref_close(ref);
2758 4e759de4 2019-06-26 stsp free(id);
2759 4e759de4 2019-06-26 stsp free(base_refname);
2760 4e759de4 2019-06-26 stsp free(refname);
2761 4e759de4 2019-06-26 stsp return err;
2762 4e759de4 2019-06-26 stsp }
2763 4e759de4 2019-06-26 stsp
2764 4e759de4 2019-06-26 stsp static const struct got_error *
2765 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2766 4e759de4 2019-06-26 stsp {
2767 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2768 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2769 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2770 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2771 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2772 4e759de4 2019-06-26 stsp const char *delref = NULL;
2773 4e759de4 2019-06-26 stsp
2774 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2775 4e759de4 2019-06-26 stsp switch (ch) {
2776 4e759de4 2019-06-26 stsp case 'd':
2777 4e759de4 2019-06-26 stsp delref = optarg;
2778 4e759de4 2019-06-26 stsp break;
2779 4e759de4 2019-06-26 stsp case 'r':
2780 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2781 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2782 4e759de4 2019-06-26 stsp err(1, "-r option");
2783 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2784 4e759de4 2019-06-26 stsp break;
2785 4e759de4 2019-06-26 stsp case 'l':
2786 4e759de4 2019-06-26 stsp do_list = 1;
2787 4e759de4 2019-06-26 stsp break;
2788 4e759de4 2019-06-26 stsp default:
2789 4e759de4 2019-06-26 stsp usage_branch();
2790 4e759de4 2019-06-26 stsp /* NOTREACHED */
2791 4e759de4 2019-06-26 stsp }
2792 4e759de4 2019-06-26 stsp }
2793 4e759de4 2019-06-26 stsp
2794 4e759de4 2019-06-26 stsp if (do_list && delref)
2795 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2796 4e759de4 2019-06-26 stsp
2797 4e759de4 2019-06-26 stsp argc -= optind;
2798 4e759de4 2019-06-26 stsp argv += optind;
2799 4e759de4 2019-06-26 stsp
2800 4e759de4 2019-06-26 stsp if (do_list || delref) {
2801 4e759de4 2019-06-26 stsp if (argc > 0)
2802 4e759de4 2019-06-26 stsp usage_branch();
2803 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2804 4e759de4 2019-06-26 stsp usage_branch();
2805 4e759de4 2019-06-26 stsp
2806 4e759de4 2019-06-26 stsp #ifndef PROFILE
2807 4e759de4 2019-06-26 stsp if (do_list) {
2808 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2809 4e759de4 2019-06-26 stsp NULL) == -1)
2810 4e759de4 2019-06-26 stsp err(1, "pledge");
2811 4e759de4 2019-06-26 stsp } else {
2812 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2813 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2814 4e759de4 2019-06-26 stsp err(1, "pledge");
2815 4e759de4 2019-06-26 stsp }
2816 4e759de4 2019-06-26 stsp #endif
2817 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2818 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2819 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2820 4e759de4 2019-06-26 stsp goto done;
2821 4e759de4 2019-06-26 stsp }
2822 4e759de4 2019-06-26 stsp
2823 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2824 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2825 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2826 4e759de4 2019-06-26 stsp goto done;
2827 4e759de4 2019-06-26 stsp else
2828 4e759de4 2019-06-26 stsp error = NULL;
2829 4e759de4 2019-06-26 stsp if (worktree) {
2830 4e759de4 2019-06-26 stsp repo_path =
2831 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2832 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2833 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2834 4e759de4 2019-06-26 stsp if (error)
2835 4e759de4 2019-06-26 stsp goto done;
2836 4e759de4 2019-06-26 stsp } else {
2837 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2838 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2839 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2840 4e759de4 2019-06-26 stsp goto done;
2841 4e759de4 2019-06-26 stsp }
2842 4e759de4 2019-06-26 stsp }
2843 4e759de4 2019-06-26 stsp }
2844 4e759de4 2019-06-26 stsp
2845 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2846 4e759de4 2019-06-26 stsp if (error != NULL)
2847 4e759de4 2019-06-26 stsp goto done;
2848 4e759de4 2019-06-26 stsp
2849 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2850 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2851 4e759de4 2019-06-26 stsp if (error)
2852 4e759de4 2019-06-26 stsp goto done;
2853 4e759de4 2019-06-26 stsp
2854 4e759de4 2019-06-26 stsp if (do_list)
2855 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2856 4e759de4 2019-06-26 stsp else if (delref)
2857 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2858 4e759de4 2019-06-26 stsp else {
2859 4e759de4 2019-06-26 stsp const char *base_branch;
2860 4e759de4 2019-06-26 stsp if (argc == 1) {
2861 4e759de4 2019-06-26 stsp base_branch = worktree ?
2862 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2863 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2864 4e759de4 2019-06-26 stsp } else
2865 4e759de4 2019-06-26 stsp base_branch = argv[1];
2866 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2867 4e759de4 2019-06-26 stsp }
2868 4e759de4 2019-06-26 stsp done:
2869 d0eebce4 2019-03-11 stsp if (repo)
2870 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2871 d0eebce4 2019-03-11 stsp if (worktree)
2872 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2873 d0eebce4 2019-03-11 stsp free(cwd);
2874 d0eebce4 2019-03-11 stsp free(repo_path);
2875 d00136be 2019-03-26 stsp return error;
2876 d00136be 2019-03-26 stsp }
2877 d00136be 2019-03-26 stsp
2878 d00136be 2019-03-26 stsp __dead static void
2879 d00136be 2019-03-26 stsp usage_add(void)
2880 d00136be 2019-03-26 stsp {
2881 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2882 d00136be 2019-03-26 stsp exit(1);
2883 d00136be 2019-03-26 stsp }
2884 d00136be 2019-03-26 stsp
2885 d00136be 2019-03-26 stsp static const struct got_error *
2886 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2887 d00136be 2019-03-26 stsp {
2888 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2889 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2890 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2891 1dd54920 2019-05-11 stsp char *cwd = NULL;
2892 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2893 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2894 723c305c 2019-05-11 jcs int ch, x;
2895 1dd54920 2019-05-11 stsp
2896 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2897 d00136be 2019-03-26 stsp
2898 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2899 d00136be 2019-03-26 stsp switch (ch) {
2900 d00136be 2019-03-26 stsp default:
2901 d00136be 2019-03-26 stsp usage_add();
2902 d00136be 2019-03-26 stsp /* NOTREACHED */
2903 d00136be 2019-03-26 stsp }
2904 d00136be 2019-03-26 stsp }
2905 d00136be 2019-03-26 stsp
2906 d00136be 2019-03-26 stsp argc -= optind;
2907 d00136be 2019-03-26 stsp argv += optind;
2908 d00136be 2019-03-26 stsp
2909 43012d58 2019-07-14 stsp #ifndef PROFILE
2910 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2911 43012d58 2019-07-14 stsp NULL) == -1)
2912 43012d58 2019-07-14 stsp err(1, "pledge");
2913 43012d58 2019-07-14 stsp #endif
2914 723c305c 2019-05-11 jcs if (argc < 1)
2915 d00136be 2019-03-26 stsp usage_add();
2916 d00136be 2019-03-26 stsp
2917 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2918 d00136be 2019-03-26 stsp if (cwd == NULL) {
2919 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2920 d00136be 2019-03-26 stsp goto done;
2921 d00136be 2019-03-26 stsp }
2922 723c305c 2019-05-11 jcs
2923 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2924 d00136be 2019-03-26 stsp if (error)
2925 d00136be 2019-03-26 stsp goto done;
2926 d00136be 2019-03-26 stsp
2927 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2928 031a5338 2019-03-26 stsp if (error != NULL)
2929 031a5338 2019-03-26 stsp goto done;
2930 031a5338 2019-03-26 stsp
2931 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2932 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2933 d00136be 2019-03-26 stsp if (error)
2934 d00136be 2019-03-26 stsp goto done;
2935 d00136be 2019-03-26 stsp
2936 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2937 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2938 723c305c 2019-05-11 jcs if (path == NULL) {
2939 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2940 723c305c 2019-05-11 jcs goto done;
2941 723c305c 2019-05-11 jcs }
2942 723c305c 2019-05-11 jcs
2943 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2944 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2945 1dd54920 2019-05-11 stsp if (error) {
2946 1dd54920 2019-05-11 stsp free(path);
2947 723c305c 2019-05-11 jcs goto done;
2948 1dd54920 2019-05-11 stsp }
2949 723c305c 2019-05-11 jcs }
2950 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2951 1dd54920 2019-05-11 stsp NULL, repo);
2952 d00136be 2019-03-26 stsp done:
2953 031a5338 2019-03-26 stsp if (repo)
2954 031a5338 2019-03-26 stsp got_repo_close(repo);
2955 d00136be 2019-03-26 stsp if (worktree)
2956 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2957 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2958 1dd54920 2019-05-11 stsp free((char *)pe->path);
2959 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2960 2ec1f75b 2019-03-26 stsp free(cwd);
2961 2ec1f75b 2019-03-26 stsp return error;
2962 2ec1f75b 2019-03-26 stsp }
2963 2ec1f75b 2019-03-26 stsp
2964 2ec1f75b 2019-03-26 stsp __dead static void
2965 648e4ef7 2019-07-09 stsp usage_remove(void)
2966 2ec1f75b 2019-03-26 stsp {
2967 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2968 2ec1f75b 2019-03-26 stsp exit(1);
2969 2ec1f75b 2019-03-26 stsp }
2970 2ec1f75b 2019-03-26 stsp
2971 2ec1f75b 2019-03-26 stsp static const struct got_error *
2972 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2973 2ec1f75b 2019-03-26 stsp {
2974 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2975 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2976 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2977 17ed4618 2019-06-02 stsp char *cwd = NULL;
2978 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2979 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2980 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2981 2ec1f75b 2019-03-26 stsp
2982 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2983 17ed4618 2019-06-02 stsp
2984 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2985 2ec1f75b 2019-03-26 stsp switch (ch) {
2986 2ec1f75b 2019-03-26 stsp case 'f':
2987 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2988 2ec1f75b 2019-03-26 stsp break;
2989 2ec1f75b 2019-03-26 stsp default:
2990 2ec1f75b 2019-03-26 stsp usage_add();
2991 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2992 2ec1f75b 2019-03-26 stsp }
2993 2ec1f75b 2019-03-26 stsp }
2994 2ec1f75b 2019-03-26 stsp
2995 2ec1f75b 2019-03-26 stsp argc -= optind;
2996 2ec1f75b 2019-03-26 stsp argv += optind;
2997 2ec1f75b 2019-03-26 stsp
2998 43012d58 2019-07-14 stsp #ifndef PROFILE
2999 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3000 43012d58 2019-07-14 stsp NULL) == -1)
3001 43012d58 2019-07-14 stsp err(1, "pledge");
3002 43012d58 2019-07-14 stsp #endif
3003 17ed4618 2019-06-02 stsp if (argc < 1)
3004 648e4ef7 2019-07-09 stsp usage_remove();
3005 2ec1f75b 2019-03-26 stsp
3006 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
3007 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
3008 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3009 2ec1f75b 2019-03-26 stsp goto done;
3010 2ec1f75b 2019-03-26 stsp }
3011 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
3012 2ec1f75b 2019-03-26 stsp if (error)
3013 2ec1f75b 2019-03-26 stsp goto done;
3014 2ec1f75b 2019-03-26 stsp
3015 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3016 2af4a041 2019-05-11 jcs if (error)
3017 2ec1f75b 2019-03-26 stsp goto done;
3018 2ec1f75b 2019-03-26 stsp
3019 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3020 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3021 2ec1f75b 2019-03-26 stsp if (error)
3022 2ec1f75b 2019-03-26 stsp goto done;
3023 2ec1f75b 2019-03-26 stsp
3024 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
3025 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
3026 17ed4618 2019-06-02 stsp if (path == NULL) {
3027 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
3028 17ed4618 2019-06-02 stsp goto done;
3029 17ed4618 2019-06-02 stsp }
3030 17ed4618 2019-06-02 stsp
3031 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
3032 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3033 17ed4618 2019-06-02 stsp if (error) {
3034 17ed4618 2019-06-02 stsp free(path);
3035 17ed4618 2019-06-02 stsp goto done;
3036 17ed4618 2019-06-02 stsp }
3037 17ed4618 2019-06-02 stsp }
3038 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
3039 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
3040 a129376b 2019-03-28 stsp if (error)
3041 a129376b 2019-03-28 stsp goto done;
3042 a129376b 2019-03-28 stsp done:
3043 a129376b 2019-03-28 stsp if (repo)
3044 a129376b 2019-03-28 stsp got_repo_close(repo);
3045 a129376b 2019-03-28 stsp if (worktree)
3046 a129376b 2019-03-28 stsp got_worktree_close(worktree);
3047 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
3048 17ed4618 2019-06-02 stsp free((char *)pe->path);
3049 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
3050 a129376b 2019-03-28 stsp free(cwd);
3051 a129376b 2019-03-28 stsp return error;
3052 a129376b 2019-03-28 stsp }
3053 a129376b 2019-03-28 stsp
3054 a129376b 2019-03-28 stsp __dead static void
3055 a129376b 2019-03-28 stsp usage_revert(void)
3056 a129376b 2019-03-28 stsp {
3057 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
3058 a129376b 2019-03-28 stsp exit(1);
3059 a129376b 2019-03-28 stsp }
3060 a129376b 2019-03-28 stsp
3061 1ee397ad 2019-07-12 stsp static const struct got_error *
3062 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
3063 a129376b 2019-03-28 stsp {
3064 a129376b 2019-03-28 stsp while (path[0] == '/')
3065 a129376b 2019-03-28 stsp path++;
3066 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
3067 1ee397ad 2019-07-12 stsp return NULL;
3068 a129376b 2019-03-28 stsp }
3069 a129376b 2019-03-28 stsp
3070 a129376b 2019-03-28 stsp static const struct got_error *
3071 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
3072 a129376b 2019-03-28 stsp {
3073 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
3074 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
3075 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
3076 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
3077 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
3078 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
3079 e20a8b6f 2019-06-04 stsp int ch, i;
3080 a129376b 2019-03-28 stsp
3081 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
3082 e20a8b6f 2019-06-04 stsp
3083 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3084 a129376b 2019-03-28 stsp switch (ch) {
3085 a129376b 2019-03-28 stsp default:
3086 a129376b 2019-03-28 stsp usage_revert();
3087 a129376b 2019-03-28 stsp /* NOTREACHED */
3088 a129376b 2019-03-28 stsp }
3089 a129376b 2019-03-28 stsp }
3090 a129376b 2019-03-28 stsp
3091 a129376b 2019-03-28 stsp argc -= optind;
3092 a129376b 2019-03-28 stsp argv += optind;
3093 a129376b 2019-03-28 stsp
3094 43012d58 2019-07-14 stsp #ifndef PROFILE
3095 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3096 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3097 43012d58 2019-07-14 stsp err(1, "pledge");
3098 43012d58 2019-07-14 stsp #endif
3099 e20a8b6f 2019-06-04 stsp if (argc < 1)
3100 a129376b 2019-03-28 stsp usage_revert();
3101 a129376b 2019-03-28 stsp
3102 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3103 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3104 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3105 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3106 e20a8b6f 2019-06-04 stsp goto done;
3107 e20a8b6f 2019-06-04 stsp }
3108 e20a8b6f 2019-06-04 stsp
3109 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3110 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3111 e20a8b6f 2019-06-04 stsp if (error) {
3112 e20a8b6f 2019-06-04 stsp free(path);
3113 e20a8b6f 2019-06-04 stsp goto done;
3114 e20a8b6f 2019-06-04 stsp }
3115 a129376b 2019-03-28 stsp }
3116 a129376b 2019-03-28 stsp
3117 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3118 a129376b 2019-03-28 stsp if (cwd == NULL) {
3119 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3120 a129376b 2019-03-28 stsp goto done;
3121 a129376b 2019-03-28 stsp }
3122 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3123 2ec1f75b 2019-03-26 stsp if (error)
3124 2ec1f75b 2019-03-26 stsp goto done;
3125 a129376b 2019-03-28 stsp
3126 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3127 a129376b 2019-03-28 stsp if (error != NULL)
3128 a129376b 2019-03-28 stsp goto done;
3129 a129376b 2019-03-28 stsp
3130 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3131 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3132 a129376b 2019-03-28 stsp if (error)
3133 a129376b 2019-03-28 stsp goto done;
3134 a129376b 2019-03-28 stsp
3135 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3136 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3137 a129376b 2019-03-28 stsp if (error)
3138 a129376b 2019-03-28 stsp goto done;
3139 2ec1f75b 2019-03-26 stsp done:
3140 2ec1f75b 2019-03-26 stsp if (repo)
3141 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3142 2ec1f75b 2019-03-26 stsp if (worktree)
3143 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3144 2ec1f75b 2019-03-26 stsp free(path);
3145 d00136be 2019-03-26 stsp free(cwd);
3146 6bad629b 2019-02-04 stsp return error;
3147 c4296144 2019-05-09 stsp }
3148 c4296144 2019-05-09 stsp
3149 c4296144 2019-05-09 stsp __dead static void
3150 c4296144 2019-05-09 stsp usage_commit(void)
3151 c4296144 2019-05-09 stsp {
3152 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
3153 5c1e53bc 2019-07-28 stsp getprogname());
3154 c4296144 2019-05-09 stsp exit(1);
3155 33ad4cbe 2019-05-12 jcs }
3156 33ad4cbe 2019-05-12 jcs
3157 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3158 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3159 e2ba3d07 2019-05-13 stsp const char *editor;
3160 e0870e44 2019-05-13 stsp const char *worktree_path;
3161 76d98825 2019-06-03 stsp const char *branch_name;
3162 314a6357 2019-05-13 stsp const char *repo_path;
3163 e0870e44 2019-05-13 stsp char *logmsg_path;
3164 e2ba3d07 2019-05-13 stsp
3165 e2ba3d07 2019-05-13 stsp };
3166 e0870e44 2019-05-13 stsp
3167 33ad4cbe 2019-05-12 jcs static const struct got_error *
3168 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3169 33ad4cbe 2019-05-12 jcs void *arg)
3170 33ad4cbe 2019-05-12 jcs {
3171 76d98825 2019-06-03 stsp char *initial_content = NULL;
3172 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3173 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3174 e0870e44 2019-05-13 stsp char *template = NULL;
3175 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3176 3ce1b845 2019-07-15 stsp int fd;
3177 33ad4cbe 2019-05-12 jcs size_t len;
3178 33ad4cbe 2019-05-12 jcs
3179 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3180 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3181 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3182 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3183 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3184 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3185 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3186 33ad4cbe 2019-05-12 jcs return NULL;
3187 33ad4cbe 2019-05-12 jcs }
3188 33ad4cbe 2019-05-12 jcs
3189 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3190 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3191 76d98825 2019-06-03 stsp
3192 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3193 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3194 76d98825 2019-06-03 stsp a->branch_name) == -1)
3195 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3196 e0870e44 2019-05-13 stsp
3197 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3198 793c30b5 2019-05-13 stsp if (err)
3199 e0870e44 2019-05-13 stsp goto done;
3200 33ad4cbe 2019-05-12 jcs
3201 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3202 33ad4cbe 2019-05-12 jcs
3203 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3204 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3205 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3206 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3207 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3208 33ad4cbe 2019-05-12 jcs }
3209 33ad4cbe 2019-05-12 jcs close(fd);
3210 33ad4cbe 2019-05-12 jcs
3211 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3212 33ad4cbe 2019-05-12 jcs done:
3213 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3214 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3215 76d98825 2019-06-03 stsp free(initial_content);
3216 e0870e44 2019-05-13 stsp free(template);
3217 314a6357 2019-05-13 stsp
3218 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3219 3ce1b845 2019-07-15 stsp if (err == NULL) {
3220 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
3221 3ce1b845 2019-07-15 stsp if (err) {
3222 3ce1b845 2019-07-15 stsp free(*logmsg);
3223 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3224 3ce1b845 2019-07-15 stsp }
3225 3ce1b845 2019-07-15 stsp }
3226 33ad4cbe 2019-05-12 jcs return err;
3227 6bad629b 2019-02-04 stsp }
3228 c4296144 2019-05-09 stsp
3229 c4296144 2019-05-09 stsp static const struct got_error *
3230 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3231 c4296144 2019-05-09 stsp {
3232 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3233 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3234 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3235 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
3236 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3237 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3238 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3239 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3240 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3241 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
3242 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
3243 5c1e53bc 2019-07-28 stsp
3244 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
3245 c4296144 2019-05-09 stsp
3246 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3247 c4296144 2019-05-09 stsp switch (ch) {
3248 c4296144 2019-05-09 stsp case 'm':
3249 c4296144 2019-05-09 stsp logmsg = optarg;
3250 c4296144 2019-05-09 stsp break;
3251 c4296144 2019-05-09 stsp default:
3252 c4296144 2019-05-09 stsp usage_commit();
3253 c4296144 2019-05-09 stsp /* NOTREACHED */
3254 c4296144 2019-05-09 stsp }
3255 c4296144 2019-05-09 stsp }
3256 c4296144 2019-05-09 stsp
3257 c4296144 2019-05-09 stsp argc -= optind;
3258 c4296144 2019-05-09 stsp argv += optind;
3259 c4296144 2019-05-09 stsp
3260 43012d58 2019-07-14 stsp #ifndef PROFILE
3261 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3262 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3263 43012d58 2019-07-14 stsp err(1, "pledge");
3264 43012d58 2019-07-14 stsp #endif
3265 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3266 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3267 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3268 35bd8fed 2019-05-09 stsp goto done;
3269 35bd8fed 2019-05-09 stsp }
3270 c4296144 2019-05-09 stsp
3271 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3272 c4296144 2019-05-09 stsp if (cwd == NULL) {
3273 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3274 c4296144 2019-05-09 stsp goto done;
3275 c4296144 2019-05-09 stsp }
3276 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3277 7d5807f4 2019-07-11 stsp if (error)
3278 7d5807f4 2019-07-11 stsp goto done;
3279 7d5807f4 2019-07-11 stsp
3280 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3281 c4296144 2019-05-09 stsp if (error)
3282 a698f62e 2019-07-25 stsp goto done;
3283 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
3284 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
3285 c4296144 2019-05-09 stsp goto done;
3286 a698f62e 2019-07-25 stsp }
3287 5c1e53bc 2019-07-28 stsp
3288 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
3289 916f288c 2019-07-30 stsp worktree);
3290 916f288c 2019-07-30 stsp if (error)
3291 916f288c 2019-07-30 stsp goto done;
3292 916f288c 2019-07-30 stsp
3293 5c1e53bc 2019-07-28 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3294 5c1e53bc 2019-07-28 stsp if (error)
3295 5c1e53bc 2019-07-28 stsp goto done;
3296 c4296144 2019-05-09 stsp
3297 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3298 c4296144 2019-05-09 stsp if (error != NULL)
3299 c4296144 2019-05-09 stsp goto done;
3300 c4296144 2019-05-09 stsp
3301 314a6357 2019-05-13 stsp /*
3302 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3303 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3304 314a6357 2019-05-13 stsp */
3305 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3306 314a6357 2019-05-13 stsp error = get_editor(&editor);
3307 314a6357 2019-05-13 stsp else
3308 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3309 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3310 c4296144 2019-05-09 stsp if (error)
3311 c4296144 2019-05-09 stsp goto done;
3312 c4296144 2019-05-09 stsp
3313 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3314 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3315 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3316 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3317 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
3318 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
3319 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
3320 916f288c 2019-07-30 stsp goto done;
3321 916f288c 2019-07-30 stsp }
3322 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
3323 916f288c 2019-07-30 stsp }
3324 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3325 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3326 5c1e53bc 2019-07-28 stsp error = got_worktree_commit(&id, worktree, &paths, got_author, NULL,
3327 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3328 e0870e44 2019-05-13 stsp if (error) {
3329 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3330 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3331 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3332 c4296144 2019-05-09 stsp goto done;
3333 e0870e44 2019-05-13 stsp }
3334 c4296144 2019-05-09 stsp
3335 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3336 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3337 e0870e44 2019-05-13 stsp
3338 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3339 c4296144 2019-05-09 stsp if (error)
3340 c4296144 2019-05-09 stsp goto done;
3341 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3342 c4296144 2019-05-09 stsp done:
3343 c4296144 2019-05-09 stsp if (repo)
3344 c4296144 2019-05-09 stsp got_repo_close(repo);
3345 c4296144 2019-05-09 stsp if (worktree)
3346 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3347 c4296144 2019-05-09 stsp free(cwd);
3348 c4296144 2019-05-09 stsp free(id_str);
3349 e2ba3d07 2019-05-13 stsp free(editor);
3350 234035bc 2019-06-01 stsp return error;
3351 234035bc 2019-06-01 stsp }
3352 234035bc 2019-06-01 stsp
3353 234035bc 2019-06-01 stsp __dead static void
3354 234035bc 2019-06-01 stsp usage_cherrypick(void)
3355 234035bc 2019-06-01 stsp {
3356 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3357 234035bc 2019-06-01 stsp exit(1);
3358 234035bc 2019-06-01 stsp }
3359 234035bc 2019-06-01 stsp
3360 234035bc 2019-06-01 stsp static const struct got_error *
3361 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3362 234035bc 2019-06-01 stsp {
3363 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3364 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3365 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3366 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3367 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3368 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3369 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3370 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3371 234035bc 2019-06-01 stsp int ch, did_something = 0;
3372 234035bc 2019-06-01 stsp
3373 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3374 234035bc 2019-06-01 stsp switch (ch) {
3375 234035bc 2019-06-01 stsp default:
3376 234035bc 2019-06-01 stsp usage_cherrypick();
3377 234035bc 2019-06-01 stsp /* NOTREACHED */
3378 234035bc 2019-06-01 stsp }
3379 234035bc 2019-06-01 stsp }
3380 234035bc 2019-06-01 stsp
3381 234035bc 2019-06-01 stsp argc -= optind;
3382 234035bc 2019-06-01 stsp argv += optind;
3383 234035bc 2019-06-01 stsp
3384 43012d58 2019-07-14 stsp #ifndef PROFILE
3385 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3386 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3387 43012d58 2019-07-14 stsp err(1, "pledge");
3388 43012d58 2019-07-14 stsp #endif
3389 234035bc 2019-06-01 stsp if (argc != 1)
3390 234035bc 2019-06-01 stsp usage_cherrypick();
3391 234035bc 2019-06-01 stsp
3392 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3393 234035bc 2019-06-01 stsp if (cwd == NULL) {
3394 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3395 234035bc 2019-06-01 stsp goto done;
3396 234035bc 2019-06-01 stsp }
3397 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3398 234035bc 2019-06-01 stsp if (error)
3399 234035bc 2019-06-01 stsp goto done;
3400 234035bc 2019-06-01 stsp
3401 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3402 234035bc 2019-06-01 stsp if (error != NULL)
3403 234035bc 2019-06-01 stsp goto done;
3404 234035bc 2019-06-01 stsp
3405 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3406 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3407 234035bc 2019-06-01 stsp if (error)
3408 234035bc 2019-06-01 stsp goto done;
3409 234035bc 2019-06-01 stsp
3410 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3411 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3412 234035bc 2019-06-01 stsp if (error != NULL) {
3413 234035bc 2019-06-01 stsp struct got_reference *ref;
3414 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3415 234035bc 2019-06-01 stsp goto done;
3416 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3417 234035bc 2019-06-01 stsp if (error != NULL)
3418 234035bc 2019-06-01 stsp goto done;
3419 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3420 234035bc 2019-06-01 stsp got_ref_close(ref);
3421 234035bc 2019-06-01 stsp if (error != NULL)
3422 234035bc 2019-06-01 stsp goto done;
3423 234035bc 2019-06-01 stsp }
3424 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3425 234035bc 2019-06-01 stsp if (error)
3426 234035bc 2019-06-01 stsp goto done;
3427 234035bc 2019-06-01 stsp
3428 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3429 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3430 234035bc 2019-06-01 stsp if (error != NULL)
3431 234035bc 2019-06-01 stsp goto done;
3432 234035bc 2019-06-01 stsp
3433 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3434 234035bc 2019-06-01 stsp if (error) {
3435 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3436 234035bc 2019-06-01 stsp goto done;
3437 234035bc 2019-06-01 stsp error = NULL;
3438 234035bc 2019-06-01 stsp } else {
3439 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3440 234035bc 2019-06-01 stsp goto done;
3441 234035bc 2019-06-01 stsp }
3442 234035bc 2019-06-01 stsp
3443 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3444 234035bc 2019-06-01 stsp if (error)
3445 234035bc 2019-06-01 stsp goto done;
3446 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3447 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3448 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3449 03415a1a 2019-06-02 stsp NULL);
3450 234035bc 2019-06-01 stsp if (error != NULL)
3451 234035bc 2019-06-01 stsp goto done;
3452 234035bc 2019-06-01 stsp
3453 234035bc 2019-06-01 stsp if (did_something)
3454 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3455 234035bc 2019-06-01 stsp done:
3456 234035bc 2019-06-01 stsp if (commit)
3457 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3458 234035bc 2019-06-01 stsp free(commit_id_str);
3459 234035bc 2019-06-01 stsp if (head_ref)
3460 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3461 234035bc 2019-06-01 stsp if (worktree)
3462 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3463 234035bc 2019-06-01 stsp if (repo)
3464 234035bc 2019-06-01 stsp got_repo_close(repo);
3465 c4296144 2019-05-09 stsp return error;
3466 c4296144 2019-05-09 stsp }
3467 5ef14e63 2019-06-02 stsp
3468 5ef14e63 2019-06-02 stsp __dead static void
3469 5ef14e63 2019-06-02 stsp usage_backout(void)
3470 5ef14e63 2019-06-02 stsp {
3471 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3472 5ef14e63 2019-06-02 stsp exit(1);
3473 5ef14e63 2019-06-02 stsp }
3474 5ef14e63 2019-06-02 stsp
3475 5ef14e63 2019-06-02 stsp static const struct got_error *
3476 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3477 5ef14e63 2019-06-02 stsp {
3478 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3479 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3480 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3481 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3482 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3483 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3484 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3485 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3486 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3487 5ef14e63 2019-06-02 stsp
3488 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3489 5ef14e63 2019-06-02 stsp switch (ch) {
3490 5ef14e63 2019-06-02 stsp default:
3491 5ef14e63 2019-06-02 stsp usage_backout();
3492 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3493 5ef14e63 2019-06-02 stsp }
3494 5ef14e63 2019-06-02 stsp }
3495 5ef14e63 2019-06-02 stsp
3496 5ef14e63 2019-06-02 stsp argc -= optind;
3497 5ef14e63 2019-06-02 stsp argv += optind;
3498 5ef14e63 2019-06-02 stsp
3499 43012d58 2019-07-14 stsp #ifndef PROFILE
3500 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3501 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3502 43012d58 2019-07-14 stsp err(1, "pledge");
3503 43012d58 2019-07-14 stsp #endif
3504 5ef14e63 2019-06-02 stsp if (argc != 1)
3505 5ef14e63 2019-06-02 stsp usage_backout();
3506 5ef14e63 2019-06-02 stsp
3507 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3508 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3509 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3510 5ef14e63 2019-06-02 stsp goto done;
3511 5ef14e63 2019-06-02 stsp }
3512 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3513 5ef14e63 2019-06-02 stsp if (error)
3514 5ef14e63 2019-06-02 stsp goto done;
3515 5ef14e63 2019-06-02 stsp
3516 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3517 5ef14e63 2019-06-02 stsp if (error != NULL)
3518 5ef14e63 2019-06-02 stsp goto done;
3519 5ef14e63 2019-06-02 stsp
3520 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3521 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3522 5ef14e63 2019-06-02 stsp if (error)
3523 5ef14e63 2019-06-02 stsp goto done;
3524 5ef14e63 2019-06-02 stsp
3525 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3526 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3527 5ef14e63 2019-06-02 stsp if (error != NULL) {
3528 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3529 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3530 5ef14e63 2019-06-02 stsp goto done;
3531 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3532 5ef14e63 2019-06-02 stsp if (error != NULL)
3533 5ef14e63 2019-06-02 stsp goto done;
3534 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3535 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3536 5ef14e63 2019-06-02 stsp if (error != NULL)
3537 5ef14e63 2019-06-02 stsp goto done;
3538 5ef14e63 2019-06-02 stsp }
3539 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3540 5ef14e63 2019-06-02 stsp if (error)
3541 5ef14e63 2019-06-02 stsp goto done;
3542 5ef14e63 2019-06-02 stsp
3543 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3544 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3545 5ef14e63 2019-06-02 stsp if (error != NULL)
3546 5ef14e63 2019-06-02 stsp goto done;
3547 5ef14e63 2019-06-02 stsp
3548 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
3549 5ef14e63 2019-06-02 stsp if (error)
3550 5ef14e63 2019-06-02 stsp goto done;
3551 5ef14e63 2019-06-02 stsp
3552 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3553 5ef14e63 2019-06-02 stsp if (error)
3554 5ef14e63 2019-06-02 stsp goto done;
3555 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3556 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3557 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3558 5ef14e63 2019-06-02 stsp goto done;
3559 5ef14e63 2019-06-02 stsp }
3560 5ef14e63 2019-06-02 stsp
3561 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3562 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3563 5ef14e63 2019-06-02 stsp if (error != NULL)
3564 5ef14e63 2019-06-02 stsp goto done;
3565 5ef14e63 2019-06-02 stsp
3566 5ef14e63 2019-06-02 stsp if (did_something)
3567 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3568 5ef14e63 2019-06-02 stsp done:
3569 5ef14e63 2019-06-02 stsp if (commit)
3570 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3571 5ef14e63 2019-06-02 stsp free(commit_id_str);
3572 5ef14e63 2019-06-02 stsp if (head_ref)
3573 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3574 818c7501 2019-07-11 stsp if (worktree)
3575 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3576 818c7501 2019-07-11 stsp if (repo)
3577 818c7501 2019-07-11 stsp got_repo_close(repo);
3578 818c7501 2019-07-11 stsp return error;
3579 818c7501 2019-07-11 stsp }
3580 818c7501 2019-07-11 stsp
3581 818c7501 2019-07-11 stsp __dead static void
3582 818c7501 2019-07-11 stsp usage_rebase(void)
3583 818c7501 2019-07-11 stsp {
3584 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3585 af54c8f8 2019-07-11 stsp getprogname());
3586 818c7501 2019-07-11 stsp exit(1);
3587 0ebf8283 2019-07-24 stsp }
3588 0ebf8283 2019-07-24 stsp
3589 0ebf8283 2019-07-24 stsp void
3590 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
3591 0ebf8283 2019-07-24 stsp {
3592 0ebf8283 2019-07-24 stsp char *nl;
3593 0ebf8283 2019-07-24 stsp size_t len;
3594 0ebf8283 2019-07-24 stsp
3595 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
3596 0ebf8283 2019-07-24 stsp if (len > limit)
3597 0ebf8283 2019-07-24 stsp len = limit;
3598 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
3599 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
3600 0ebf8283 2019-07-24 stsp if (nl)
3601 0ebf8283 2019-07-24 stsp *nl = '\0';
3602 0ebf8283 2019-07-24 stsp }
3603 0ebf8283 2019-07-24 stsp
3604 0ebf8283 2019-07-24 stsp static const struct got_error *
3605 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
3606 0ebf8283 2019-07-24 stsp {
3607 0ebf8283 2019-07-24 stsp const char *logmsg0 = NULL;
3608 0ebf8283 2019-07-24 stsp
3609 0ebf8283 2019-07-24 stsp logmsg0 = got_object_commit_get_logmsg(commit);
3610 0ebf8283 2019-07-24 stsp
3611 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)logmsg0[0]))
3612 0ebf8283 2019-07-24 stsp logmsg0++;
3613 0ebf8283 2019-07-24 stsp
3614 0ebf8283 2019-07-24 stsp *logmsg = strdup(logmsg0);
3615 0ebf8283 2019-07-24 stsp if (*logmsg == NULL)
3616 0ebf8283 2019-07-24 stsp return got_error_from_errno("strdup");
3617 0ebf8283 2019-07-24 stsp
3618 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
3619 0ebf8283 2019-07-24 stsp return NULL;
3620 818c7501 2019-07-11 stsp }
3621 818c7501 2019-07-11 stsp
3622 818c7501 2019-07-11 stsp static const struct got_error *
3623 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3624 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3625 818c7501 2019-07-11 stsp {
3626 818c7501 2019-07-11 stsp const struct got_error *err;
3627 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
3628 818c7501 2019-07-11 stsp
3629 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3630 818c7501 2019-07-11 stsp if (err)
3631 818c7501 2019-07-11 stsp goto done;
3632 818c7501 2019-07-11 stsp
3633 ff0d2220 2019-07-11 stsp if (new_id) {
3634 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3635 ff0d2220 2019-07-11 stsp if (err)
3636 ff0d2220 2019-07-11 stsp goto done;
3637 ff0d2220 2019-07-11 stsp }
3638 818c7501 2019-07-11 stsp
3639 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3640 ff0d2220 2019-07-11 stsp if (new_id_str)
3641 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3642 0ebf8283 2019-07-24 stsp
3643 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
3644 0ebf8283 2019-07-24 stsp if (err)
3645 0ebf8283 2019-07-24 stsp goto done;
3646 0ebf8283 2019-07-24 stsp
3647 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3648 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3649 818c7501 2019-07-11 stsp done:
3650 818c7501 2019-07-11 stsp free(old_id_str);
3651 818c7501 2019-07-11 stsp free(new_id_str);
3652 818c7501 2019-07-11 stsp return err;
3653 818c7501 2019-07-11 stsp }
3654 818c7501 2019-07-11 stsp
3655 1ee397ad 2019-07-12 stsp static const struct got_error *
3656 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3657 818c7501 2019-07-11 stsp {
3658 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3659 818c7501 2019-07-11 stsp
3660 818c7501 2019-07-11 stsp while (path[0] == '/')
3661 818c7501 2019-07-11 stsp path++;
3662 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3663 818c7501 2019-07-11 stsp
3664 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3665 1ee397ad 2019-07-12 stsp return NULL;
3666 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3667 818c7501 2019-07-11 stsp *rebase_status = status;
3668 1ee397ad 2019-07-12 stsp return NULL;
3669 818c7501 2019-07-11 stsp }
3670 818c7501 2019-07-11 stsp
3671 818c7501 2019-07-11 stsp static const struct got_error *
3672 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
3673 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
3674 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
3675 818c7501 2019-07-11 stsp {
3676 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3677 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
3678 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3679 818c7501 2019-07-11 stsp }
3680 818c7501 2019-07-11 stsp
3681 818c7501 2019-07-11 stsp static const struct got_error *
3682 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3683 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
3684 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
3685 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
3686 ff0d2220 2019-07-11 stsp {
3687 ff0d2220 2019-07-11 stsp const struct got_error *error;
3688 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3689 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3690 ff0d2220 2019-07-11 stsp
3691 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3692 ff0d2220 2019-07-11 stsp if (error)
3693 ff0d2220 2019-07-11 stsp return error;
3694 ff0d2220 2019-07-11 stsp
3695 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3696 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
3697 ff0d2220 2019-07-11 stsp if (error) {
3698 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3699 ff0d2220 2019-07-11 stsp goto done;
3700 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3701 ff0d2220 2019-07-11 stsp } else {
3702 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3703 ff0d2220 2019-07-11 stsp free(new_commit_id);
3704 ff0d2220 2019-07-11 stsp }
3705 ff0d2220 2019-07-11 stsp done:
3706 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3707 ff0d2220 2019-07-11 stsp return error;
3708 64c6d990 2019-07-11 stsp }
3709 64c6d990 2019-07-11 stsp
3710 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3711 64c6d990 2019-07-11 stsp const char *path_prefix;
3712 64c6d990 2019-07-11 stsp size_t len;
3713 8ca9bd68 2019-07-25 stsp int errcode;
3714 64c6d990 2019-07-11 stsp };
3715 64c6d990 2019-07-11 stsp
3716 64c6d990 2019-07-11 stsp static const struct got_error *
3717 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
3718 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3719 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3720 64c6d990 2019-07-11 stsp struct got_repository *repo)
3721 64c6d990 2019-07-11 stsp {
3722 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3723 64c6d990 2019-07-11 stsp
3724 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3725 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3726 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
3727 64c6d990 2019-07-11 stsp
3728 64c6d990 2019-07-11 stsp return NULL;
3729 64c6d990 2019-07-11 stsp }
3730 64c6d990 2019-07-11 stsp
3731 64c6d990 2019-07-11 stsp static const struct got_error *
3732 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
3733 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3734 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
3735 64c6d990 2019-07-11 stsp {
3736 64c6d990 2019-07-11 stsp const struct got_error *err;
3737 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3738 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3739 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3740 64c6d990 2019-07-11 stsp
3741 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3742 64c6d990 2019-07-11 stsp return NULL;
3743 64c6d990 2019-07-11 stsp
3744 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3745 64c6d990 2019-07-11 stsp if (err)
3746 64c6d990 2019-07-11 stsp goto done;
3747 64c6d990 2019-07-11 stsp
3748 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3749 64c6d990 2019-07-11 stsp if (err)
3750 64c6d990 2019-07-11 stsp goto done;
3751 64c6d990 2019-07-11 stsp
3752 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3753 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3754 64c6d990 2019-07-11 stsp if (err)
3755 64c6d990 2019-07-11 stsp goto done;
3756 64c6d990 2019-07-11 stsp
3757 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3758 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3759 64c6d990 2019-07-11 stsp if (err)
3760 64c6d990 2019-07-11 stsp goto done;
3761 64c6d990 2019-07-11 stsp
3762 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3763 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
3764 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
3765 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
3766 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
3767 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
3768 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
3769 64c6d990 2019-07-11 stsp done:
3770 64c6d990 2019-07-11 stsp if (tree1)
3771 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3772 64c6d990 2019-07-11 stsp if (tree2)
3773 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3774 64c6d990 2019-07-11 stsp if (commit)
3775 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3776 64c6d990 2019-07-11 stsp if (parent_commit)
3777 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3778 64c6d990 2019-07-11 stsp return err;
3779 ff0d2220 2019-07-11 stsp }
3780 ff0d2220 2019-07-11 stsp
3781 ff0d2220 2019-07-11 stsp static const struct got_error *
3782 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
3783 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3784 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3785 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
3786 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
3787 af61c510 2019-07-19 stsp {
3788 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3789 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3790 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3791 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3792 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3793 af61c510 2019-07-19 stsp
3794 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3795 af61c510 2019-07-19 stsp if (err)
3796 af61c510 2019-07-19 stsp return err;
3797 af61c510 2019-07-19 stsp
3798 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3799 af61c510 2019-07-19 stsp if (err)
3800 af61c510 2019-07-19 stsp goto done;
3801 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3802 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3803 af61c510 2019-07-19 stsp if (err) {
3804 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3805 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3806 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3807 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3808 af61c510 2019-07-19 stsp "been reached?!?");
3809 af61c510 2019-07-19 stsp goto done;
3810 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3811 af61c510 2019-07-19 stsp goto done;
3812 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3813 af61c510 2019-07-19 stsp if (err)
3814 af61c510 2019-07-19 stsp goto done;
3815 af61c510 2019-07-19 stsp } else {
3816 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
3817 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
3818 af61c510 2019-07-19 stsp if (err)
3819 af61c510 2019-07-19 stsp goto done;
3820 af61c510 2019-07-19 stsp
3821 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3822 af61c510 2019-07-19 stsp if (err)
3823 af61c510 2019-07-19 stsp goto done;
3824 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3825 af61c510 2019-07-19 stsp commit_id = parent_id;
3826 af61c510 2019-07-19 stsp }
3827 af61c510 2019-07-19 stsp }
3828 af61c510 2019-07-19 stsp done:
3829 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3830 af61c510 2019-07-19 stsp return err;
3831 af61c510 2019-07-19 stsp }
3832 af61c510 2019-07-19 stsp
3833 af61c510 2019-07-19 stsp static const struct got_error *
3834 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3835 818c7501 2019-07-11 stsp {
3836 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3837 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3838 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3839 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
3840 818c7501 2019-07-11 stsp char *cwd = NULL;
3841 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3842 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3843 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3844 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3845 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3846 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3847 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3848 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3849 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3850 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3851 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3852 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3853 818c7501 2019-07-11 stsp
3854 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3855 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3856 818c7501 2019-07-11 stsp
3857 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3858 818c7501 2019-07-11 stsp switch (ch) {
3859 818c7501 2019-07-11 stsp case 'a':
3860 818c7501 2019-07-11 stsp abort_rebase = 1;
3861 818c7501 2019-07-11 stsp break;
3862 818c7501 2019-07-11 stsp case 'c':
3863 818c7501 2019-07-11 stsp continue_rebase = 1;
3864 818c7501 2019-07-11 stsp break;
3865 818c7501 2019-07-11 stsp default:
3866 818c7501 2019-07-11 stsp usage_rebase();
3867 818c7501 2019-07-11 stsp /* NOTREACHED */
3868 818c7501 2019-07-11 stsp }
3869 818c7501 2019-07-11 stsp }
3870 818c7501 2019-07-11 stsp
3871 818c7501 2019-07-11 stsp argc -= optind;
3872 818c7501 2019-07-11 stsp argv += optind;
3873 818c7501 2019-07-11 stsp
3874 43012d58 2019-07-14 stsp #ifndef PROFILE
3875 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3876 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3877 43012d58 2019-07-14 stsp err(1, "pledge");
3878 43012d58 2019-07-14 stsp #endif
3879 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3880 818c7501 2019-07-11 stsp usage_rebase();
3881 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3882 818c7501 2019-07-11 stsp if (argc != 0)
3883 818c7501 2019-07-11 stsp usage_rebase();
3884 818c7501 2019-07-11 stsp } else if (argc != 1)
3885 818c7501 2019-07-11 stsp usage_rebase();
3886 818c7501 2019-07-11 stsp
3887 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3888 818c7501 2019-07-11 stsp if (cwd == NULL) {
3889 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3890 818c7501 2019-07-11 stsp goto done;
3891 818c7501 2019-07-11 stsp }
3892 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3893 818c7501 2019-07-11 stsp if (error)
3894 818c7501 2019-07-11 stsp goto done;
3895 818c7501 2019-07-11 stsp
3896 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3897 818c7501 2019-07-11 stsp if (error != NULL)
3898 818c7501 2019-07-11 stsp goto done;
3899 818c7501 2019-07-11 stsp
3900 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3901 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3902 818c7501 2019-07-11 stsp if (error)
3903 818c7501 2019-07-11 stsp goto done;
3904 818c7501 2019-07-11 stsp
3905 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3906 818c7501 2019-07-11 stsp if (error)
3907 818c7501 2019-07-11 stsp goto done;
3908 818c7501 2019-07-11 stsp
3909 f6794adc 2019-07-23 stsp if (abort_rebase) {
3910 818c7501 2019-07-11 stsp int did_something;
3911 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3912 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3913 f6794adc 2019-07-23 stsp goto done;
3914 f6794adc 2019-07-23 stsp }
3915 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3916 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3917 3e3a69f1 2019-07-25 stsp worktree, repo);
3918 818c7501 2019-07-11 stsp if (error)
3919 818c7501 2019-07-11 stsp goto done;
3920 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3921 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3922 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
3923 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3924 818c7501 2019-07-11 stsp if (error)
3925 818c7501 2019-07-11 stsp goto done;
3926 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3927 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3928 818c7501 2019-07-11 stsp }
3929 818c7501 2019-07-11 stsp
3930 818c7501 2019-07-11 stsp if (continue_rebase) {
3931 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
3932 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
3933 f6794adc 2019-07-23 stsp goto done;
3934 f6794adc 2019-07-23 stsp }
3935 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3936 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
3937 3e3a69f1 2019-07-25 stsp worktree, repo);
3938 818c7501 2019-07-11 stsp if (error)
3939 818c7501 2019-07-11 stsp goto done;
3940 818c7501 2019-07-11 stsp
3941 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
3942 01757395 2019-07-12 stsp resume_commit_id, repo);
3943 818c7501 2019-07-11 stsp if (error)
3944 818c7501 2019-07-11 stsp goto done;
3945 818c7501 2019-07-11 stsp
3946 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3947 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3948 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3949 818c7501 2019-07-11 stsp goto done;
3950 818c7501 2019-07-11 stsp }
3951 818c7501 2019-07-11 stsp } else {
3952 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3953 818c7501 2019-07-11 stsp if (error != NULL)
3954 818c7501 2019-07-11 stsp goto done;
3955 ff0d2220 2019-07-11 stsp }
3956 818c7501 2019-07-11 stsp
3957 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3958 ff0d2220 2019-07-11 stsp if (error)
3959 ff0d2220 2019-07-11 stsp goto done;
3960 ff0d2220 2019-07-11 stsp
3961 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3962 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
3963 a51a74b3 2019-07-27 stsp
3964 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
3965 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3966 a51a74b3 2019-07-27 stsp base_commit_id, branch_head_commit_id, repo);
3967 818c7501 2019-07-11 stsp if (error)
3968 818c7501 2019-07-11 stsp goto done;
3969 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3970 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3971 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3972 818c7501 2019-07-11 stsp "with work tree's branch");
3973 818c7501 2019-07-11 stsp goto done;
3974 818c7501 2019-07-11 stsp }
3975 818c7501 2019-07-11 stsp
3976 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
3977 a51a74b3 2019-07-27 stsp if (error) {
3978 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
3979 a51a74b3 2019-07-27 stsp goto done;
3980 a51a74b3 2019-07-27 stsp error = NULL;
3981 a51a74b3 2019-07-27 stsp } else {
3982 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3983 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
3984 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
3985 a51a74b3 2019-07-27 stsp goto done;
3986 a51a74b3 2019-07-27 stsp }
3987 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3988 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
3989 818c7501 2019-07-11 stsp if (error)
3990 818c7501 2019-07-11 stsp goto done;
3991 818c7501 2019-07-11 stsp }
3992 818c7501 2019-07-11 stsp
3993 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3994 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3995 818c7501 2019-07-11 stsp if (error)
3996 818c7501 2019-07-11 stsp goto done;
3997 818c7501 2019-07-11 stsp
3998 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3999 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
4000 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
4001 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
4002 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
4003 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4004 818c7501 2019-07-11 stsp commit = NULL;
4005 818c7501 2019-07-11 stsp if (error)
4006 818c7501 2019-07-11 stsp goto done;
4007 64c6d990 2019-07-11 stsp
4008 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
4009 ff0d2220 2019-07-11 stsp if (continue_rebase)
4010 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
4011 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
4012 ff0d2220 2019-07-11 stsp else
4013 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
4014 818c7501 2019-07-11 stsp goto done;
4015 818c7501 2019-07-11 stsp }
4016 818c7501 2019-07-11 stsp
4017 818c7501 2019-07-11 stsp pid = NULL;
4018 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
4019 818c7501 2019-07-11 stsp commit_id = qid->id;
4020 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
4021 818c7501 2019-07-11 stsp pid = qid;
4022 818c7501 2019-07-11 stsp
4023 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
4024 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
4025 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
4026 818c7501 2019-07-11 stsp if (error)
4027 818c7501 2019-07-11 stsp goto done;
4028 818c7501 2019-07-11 stsp
4029 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4030 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4031 818c7501 2019-07-11 stsp break;
4032 01757395 2019-07-12 stsp }
4033 818c7501 2019-07-11 stsp
4034 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
4035 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
4036 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
4037 818c7501 2019-07-11 stsp if (error)
4038 818c7501 2019-07-11 stsp goto done;
4039 818c7501 2019-07-11 stsp }
4040 818c7501 2019-07-11 stsp
4041 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
4042 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
4043 818c7501 2019-07-11 stsp if (error)
4044 818c7501 2019-07-11 stsp goto done;
4045 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
4046 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
4047 818c7501 2019-07-11 stsp } else
4048 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
4049 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
4050 818c7501 2019-07-11 stsp done:
4051 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
4052 818c7501 2019-07-11 stsp free(branch_head_commit_id);
4053 818c7501 2019-07-11 stsp free(resume_commit_id);
4054 818c7501 2019-07-11 stsp free(yca_id);
4055 818c7501 2019-07-11 stsp if (commit)
4056 818c7501 2019-07-11 stsp got_object_commit_close(commit);
4057 818c7501 2019-07-11 stsp if (branch)
4058 818c7501 2019-07-11 stsp got_ref_close(branch);
4059 818c7501 2019-07-11 stsp if (new_base_branch)
4060 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
4061 818c7501 2019-07-11 stsp if (tmp_branch)
4062 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
4063 5ef14e63 2019-06-02 stsp if (worktree)
4064 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
4065 5ef14e63 2019-06-02 stsp if (repo)
4066 5ef14e63 2019-06-02 stsp got_repo_close(repo);
4067 5ef14e63 2019-06-02 stsp return error;
4068 0ebf8283 2019-07-24 stsp }
4069 0ebf8283 2019-07-24 stsp
4070 0ebf8283 2019-07-24 stsp __dead static void
4071 0ebf8283 2019-07-24 stsp usage_histedit(void)
4072 0ebf8283 2019-07-24 stsp {
4073 0ebf8283 2019-07-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F path]\n",
4074 0ebf8283 2019-07-24 stsp getprogname());
4075 0ebf8283 2019-07-24 stsp exit(1);
4076 0ebf8283 2019-07-24 stsp }
4077 0ebf8283 2019-07-24 stsp
4078 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
4079 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
4080 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
4081 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
4082 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
4083 0ebf8283 2019-07-24 stsp
4084 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
4085 0ebf8283 2019-07-24 stsp unsigned char code;
4086 0ebf8283 2019-07-24 stsp const char *name;
4087 0ebf8283 2019-07-24 stsp const char *desc;
4088 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
4089 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
4090 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
4091 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
4092 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
4093 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
4094 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
4095 0ebf8283 2019-07-24 stsp };
4096 0ebf8283 2019-07-24 stsp
4097 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
4098 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
4099 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
4100 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4101 0ebf8283 2019-07-24 stsp char *logmsg;
4102 0ebf8283 2019-07-24 stsp };
4103 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
4104 0ebf8283 2019-07-24 stsp
4105 0ebf8283 2019-07-24 stsp static const struct got_error *
4106 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
4107 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4108 0ebf8283 2019-07-24 stsp {
4109 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4110 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
4111 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4112 0ebf8283 2019-07-24 stsp size_t n;
4113 0ebf8283 2019-07-24 stsp
4114 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
4115 0ebf8283 2019-07-24 stsp if (err)
4116 0ebf8283 2019-07-24 stsp goto done;
4117 0ebf8283 2019-07-24 stsp
4118 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
4119 0ebf8283 2019-07-24 stsp if (err)
4120 0ebf8283 2019-07-24 stsp goto done;
4121 0ebf8283 2019-07-24 stsp
4122 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
4123 0ebf8283 2019-07-24 stsp if (err)
4124 0ebf8283 2019-07-24 stsp goto done;
4125 0ebf8283 2019-07-24 stsp
4126 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
4127 0ebf8283 2019-07-24 stsp if (n < 0)
4128 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4129 0ebf8283 2019-07-24 stsp done:
4130 0ebf8283 2019-07-24 stsp if (commit)
4131 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4132 0ebf8283 2019-07-24 stsp free(id_str);
4133 0ebf8283 2019-07-24 stsp free(logmsg);
4134 0ebf8283 2019-07-24 stsp return err;
4135 0ebf8283 2019-07-24 stsp }
4136 0ebf8283 2019-07-24 stsp
4137 0ebf8283 2019-07-24 stsp static const struct got_error *
4138 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
4139 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4140 0ebf8283 2019-07-24 stsp {
4141 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4142 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
4143 0ebf8283 2019-07-24 stsp
4144 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
4145 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
4146 0ebf8283 2019-07-24 stsp
4147 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4148 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
4149 0ebf8283 2019-07-24 stsp f, repo);
4150 0ebf8283 2019-07-24 stsp if (err)
4151 0ebf8283 2019-07-24 stsp break;
4152 0ebf8283 2019-07-24 stsp }
4153 0ebf8283 2019-07-24 stsp
4154 0ebf8283 2019-07-24 stsp return err;
4155 0ebf8283 2019-07-24 stsp }
4156 0ebf8283 2019-07-24 stsp
4157 0ebf8283 2019-07-24 stsp static const struct got_error *
4158 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
4159 0ebf8283 2019-07-24 stsp {
4160 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4161 0ebf8283 2019-07-24 stsp int n, i;
4162 0ebf8283 2019-07-24 stsp
4163 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
4164 0ebf8283 2019-07-24 stsp if (n < 0)
4165 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4166 0ebf8283 2019-07-24 stsp
4167 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4168 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
4169 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
4170 0ebf8283 2019-07-24 stsp cmd->desc);
4171 0ebf8283 2019-07-24 stsp if (n < 0) {
4172 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4173 0ebf8283 2019-07-24 stsp break;
4174 0ebf8283 2019-07-24 stsp }
4175 0ebf8283 2019-07-24 stsp }
4176 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
4177 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
4178 0ebf8283 2019-07-24 stsp if (n < 0)
4179 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
4180 0ebf8283 2019-07-24 stsp return err;
4181 0ebf8283 2019-07-24 stsp }
4182 0ebf8283 2019-07-24 stsp
4183 0ebf8283 2019-07-24 stsp static const struct got_error *
4184 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
4185 0ebf8283 2019-07-24 stsp {
4186 0ebf8283 2019-07-24 stsp static char msg[42];
4187 0ebf8283 2019-07-24 stsp int ret;
4188 0ebf8283 2019-07-24 stsp
4189 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
4190 0ebf8283 2019-07-24 stsp lineno);
4191 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
4192 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
4193 0ebf8283 2019-07-24 stsp
4194 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
4195 0ebf8283 2019-07-24 stsp }
4196 0ebf8283 2019-07-24 stsp
4197 0ebf8283 2019-07-24 stsp static const struct got_error *
4198 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
4199 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
4200 0ebf8283 2019-07-24 stsp {
4201 0ebf8283 2019-07-24 stsp const struct got_error *err;
4202 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
4203 0ebf8283 2019-07-24 stsp char *id_str;
4204 0ebf8283 2019-07-24 stsp
4205 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4206 0ebf8283 2019-07-24 stsp if (err)
4207 0ebf8283 2019-07-24 stsp return err;
4208 0ebf8283 2019-07-24 stsp
4209 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
4210 0ebf8283 2019-07-24 stsp if (err)
4211 0ebf8283 2019-07-24 stsp goto done;
4212 0ebf8283 2019-07-24 stsp
4213 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
4214 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
4215 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(folded_commit)) == -1) {
4216 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4217 0ebf8283 2019-07-24 stsp goto done;
4218 0ebf8283 2019-07-24 stsp }
4219 0ebf8283 2019-07-24 stsp done:
4220 0ebf8283 2019-07-24 stsp if (folded_commit)
4221 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
4222 0ebf8283 2019-07-24 stsp free(id_str);
4223 0ebf8283 2019-07-24 stsp return err;
4224 0ebf8283 2019-07-24 stsp }
4225 0ebf8283 2019-07-24 stsp
4226 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
4227 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
4228 0ebf8283 2019-07-24 stsp {
4229 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
4230 0ebf8283 2019-07-24 stsp
4231 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
4232 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
4233 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
4234 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
4235 3f9de99f 2019-07-24 stsp folded = prev;
4236 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
4237 0ebf8283 2019-07-24 stsp }
4238 0ebf8283 2019-07-24 stsp
4239 0ebf8283 2019-07-24 stsp return folded;
4240 0ebf8283 2019-07-24 stsp }
4241 0ebf8283 2019-07-24 stsp
4242 0ebf8283 2019-07-24 stsp static const struct got_error *
4243 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
4244 0ebf8283 2019-07-24 stsp struct got_repository *repo)
4245 0ebf8283 2019-07-24 stsp {
4246 0ebf8283 2019-07-24 stsp char *logmsg_path = NULL, *id_str = NULL;
4247 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
4248 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4249 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4250 0ebf8283 2019-07-24 stsp int fd;
4251 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
4252 0ebf8283 2019-07-24 stsp
4253 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4254 0ebf8283 2019-07-24 stsp if (err)
4255 0ebf8283 2019-07-24 stsp return err;
4256 0ebf8283 2019-07-24 stsp
4257 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
4258 0ebf8283 2019-07-24 stsp if (folded) {
4259 0ebf8283 2019-07-24 stsp while (folded != hle) {
4260 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
4261 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4262 3f9de99f 2019-07-24 stsp continue;
4263 3f9de99f 2019-07-24 stsp }
4264 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
4265 0ebf8283 2019-07-24 stsp logmsg, repo);
4266 0ebf8283 2019-07-24 stsp if (err)
4267 0ebf8283 2019-07-24 stsp goto done;
4268 0ebf8283 2019-07-24 stsp free(logmsg);
4269 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4270 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
4271 0ebf8283 2019-07-24 stsp }
4272 0ebf8283 2019-07-24 stsp }
4273 0ebf8283 2019-07-24 stsp
4274 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4275 0ebf8283 2019-07-24 stsp if (err)
4276 0ebf8283 2019-07-24 stsp goto done;
4277 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
4278 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
4279 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", id_str,
4280 0ebf8283 2019-07-24 stsp got_object_commit_get_logmsg(commit)) == -1) {
4281 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
4282 0ebf8283 2019-07-24 stsp goto done;
4283 0ebf8283 2019-07-24 stsp }
4284 0ebf8283 2019-07-24 stsp free(logmsg);
4285 0ebf8283 2019-07-24 stsp logmsg = new_msg;
4286 0ebf8283 2019-07-24 stsp
4287 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
4288 0ebf8283 2019-07-24 stsp if (err)
4289 0ebf8283 2019-07-24 stsp goto done;
4290 0ebf8283 2019-07-24 stsp
4291 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
4292 0ebf8283 2019-07-24 stsp if (err)
4293 0ebf8283 2019-07-24 stsp goto done;
4294 0ebf8283 2019-07-24 stsp
4295 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
4296 0ebf8283 2019-07-24 stsp close(fd);
4297 0ebf8283 2019-07-24 stsp
4298 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4299 0ebf8283 2019-07-24 stsp if (err)
4300 0ebf8283 2019-07-24 stsp goto done;
4301 0ebf8283 2019-07-24 stsp
4302 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
4303 0ebf8283 2019-07-24 stsp if (err) {
4304 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
4305 0ebf8283 2019-07-24 stsp goto done;
4306 0ebf8283 2019-07-24 stsp err = NULL;
4307 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(got_object_commit_get_logmsg(commit));
4308 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL)
4309 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4310 0ebf8283 2019-07-24 stsp }
4311 0ebf8283 2019-07-24 stsp done:
4312 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
4313 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
4314 0ebf8283 2019-07-24 stsp free(logmsg_path);
4315 0ebf8283 2019-07-24 stsp free(logmsg);
4316 0ebf8283 2019-07-24 stsp free(editor);
4317 0ebf8283 2019-07-24 stsp if (commit)
4318 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4319 0ebf8283 2019-07-24 stsp return err;
4320 5ef14e63 2019-06-02 stsp }
4321 0ebf8283 2019-07-24 stsp
4322 0ebf8283 2019-07-24 stsp static const struct got_error *
4323 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
4324 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
4325 0ebf8283 2019-07-24 stsp {
4326 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4327 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
4328 0ebf8283 2019-07-24 stsp size_t size;
4329 0ebf8283 2019-07-24 stsp ssize_t len;
4330 0ebf8283 2019-07-24 stsp int lineno = 0, i;
4331 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
4332 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
4333 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
4334 0ebf8283 2019-07-24 stsp
4335 0ebf8283 2019-07-24 stsp for (;;) {
4336 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
4337 0ebf8283 2019-07-24 stsp if (len == -1) {
4338 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
4339 0ebf8283 2019-07-24 stsp if (feof(f))
4340 0ebf8283 2019-07-24 stsp break;
4341 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
4342 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
4343 0ebf8283 2019-07-24 stsp break;
4344 0ebf8283 2019-07-24 stsp }
4345 0ebf8283 2019-07-24 stsp lineno++;
4346 0ebf8283 2019-07-24 stsp p = line;
4347 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4348 0ebf8283 2019-07-24 stsp p++;
4349 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
4350 0ebf8283 2019-07-24 stsp free(line);
4351 0ebf8283 2019-07-24 stsp line = NULL;
4352 0ebf8283 2019-07-24 stsp continue;
4353 0ebf8283 2019-07-24 stsp }
4354 0ebf8283 2019-07-24 stsp cmd = NULL;
4355 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
4356 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
4357 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
4358 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
4359 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
4360 0ebf8283 2019-07-24 stsp break;
4361 0ebf8283 2019-07-24 stsp }
4362 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
4363 0ebf8283 2019-07-24 stsp p++;
4364 0ebf8283 2019-07-24 stsp break;
4365 0ebf8283 2019-07-24 stsp }
4366 0ebf8283 2019-07-24 stsp }
4367 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
4368 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4369 0ebf8283 2019-07-24 stsp break;
4370 0ebf8283 2019-07-24 stsp }
4371 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
4372 0ebf8283 2019-07-24 stsp p++;
4373 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
4374 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
4375 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
4376 0ebf8283 2019-07-24 stsp break;
4377 0ebf8283 2019-07-24 stsp }
4378 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
4379 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4380 0ebf8283 2019-07-24 stsp if (err)
4381 0ebf8283 2019-07-24 stsp break;
4382 0ebf8283 2019-07-24 stsp } else {
4383 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
4384 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
4385 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4386 0ebf8283 2019-07-24 stsp break;
4387 0ebf8283 2019-07-24 stsp }
4388 0ebf8283 2019-07-24 stsp }
4389 0ebf8283 2019-07-24 stsp free(line);
4390 0ebf8283 2019-07-24 stsp line = NULL;
4391 0ebf8283 2019-07-24 stsp continue;
4392 0ebf8283 2019-07-24 stsp } else {
4393 0ebf8283 2019-07-24 stsp end = p;
4394 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
4395 0ebf8283 2019-07-24 stsp end++;
4396 0ebf8283 2019-07-24 stsp *end = '\0';
4397 0ebf8283 2019-07-24 stsp
4398 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
4399 0ebf8283 2019-07-24 stsp if (err) {
4400 0ebf8283 2019-07-24 stsp /* override error code */
4401 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
4402 0ebf8283 2019-07-24 stsp break;
4403 0ebf8283 2019-07-24 stsp }
4404 0ebf8283 2019-07-24 stsp }
4405 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
4406 0ebf8283 2019-07-24 stsp if (hle == NULL) {
4407 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
4408 0ebf8283 2019-07-24 stsp break;
4409 0ebf8283 2019-07-24 stsp }
4410 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
4411 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
4412 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
4413 0ebf8283 2019-07-24 stsp commit_id = NULL;
4414 0ebf8283 2019-07-24 stsp free(line);
4415 0ebf8283 2019-07-24 stsp line = NULL;
4416 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
4417 0ebf8283 2019-07-24 stsp }
4418 0ebf8283 2019-07-24 stsp
4419 0ebf8283 2019-07-24 stsp free(line);
4420 0ebf8283 2019-07-24 stsp free(commit_id);
4421 0ebf8283 2019-07-24 stsp return err;
4422 0ebf8283 2019-07-24 stsp }
4423 0ebf8283 2019-07-24 stsp
4424 0ebf8283 2019-07-24 stsp static const struct got_error *
4425 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
4426 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4427 bfce7f83 2019-07-27 stsp {
4428 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
4429 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
4430 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4431 bfce7f83 2019-07-27 stsp static char msg[80];
4432 bfce7f83 2019-07-27 stsp char *id_str;
4433 bfce7f83 2019-07-27 stsp
4434 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
4435 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
4436 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
4437 bfce7f83 2019-07-27 stsp
4438 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
4439 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4440 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
4441 bfce7f83 2019-07-27 stsp break;
4442 bfce7f83 2019-07-27 stsp }
4443 bfce7f83 2019-07-27 stsp if (hle == NULL) {
4444 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
4445 bfce7f83 2019-07-27 stsp if (err)
4446 bfce7f83 2019-07-27 stsp return err;
4447 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
4448 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
4449 bfce7f83 2019-07-27 stsp free(id_str);
4450 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
4451 bfce7f83 2019-07-27 stsp }
4452 bfce7f83 2019-07-27 stsp }
4453 bfce7f83 2019-07-27 stsp
4454 bfce7f83 2019-07-27 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD)
4455 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
4456 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
4457 bfce7f83 2019-07-27 stsp
4458 bfce7f83 2019-07-27 stsp return NULL;
4459 bfce7f83 2019-07-27 stsp }
4460 bfce7f83 2019-07-27 stsp
4461 bfce7f83 2019-07-27 stsp static const struct got_error *
4462 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
4463 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
4464 bfce7f83 2019-07-27 stsp struct got_repository *repo)
4465 0ebf8283 2019-07-24 stsp {
4466 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4467 0ebf8283 2019-07-24 stsp char *editor;
4468 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4469 0ebf8283 2019-07-24 stsp
4470 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
4471 0ebf8283 2019-07-24 stsp if (err)
4472 0ebf8283 2019-07-24 stsp return err;
4473 0ebf8283 2019-07-24 stsp
4474 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
4475 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
4476 0ebf8283 2019-07-24 stsp goto done;
4477 0ebf8283 2019-07-24 stsp }
4478 0ebf8283 2019-07-24 stsp
4479 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4480 0ebf8283 2019-07-24 stsp if (f == NULL) {
4481 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
4482 0ebf8283 2019-07-24 stsp goto done;
4483 0ebf8283 2019-07-24 stsp }
4484 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4485 bfce7f83 2019-07-27 stsp if (err)
4486 bfce7f83 2019-07-27 stsp goto done;
4487 bfce7f83 2019-07-27 stsp
4488 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
4489 0ebf8283 2019-07-24 stsp done:
4490 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4491 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4492 0ebf8283 2019-07-24 stsp free(editor);
4493 0ebf8283 2019-07-24 stsp return err;
4494 0ebf8283 2019-07-24 stsp }
4495 0ebf8283 2019-07-24 stsp
4496 0ebf8283 2019-07-24 stsp static const struct got_error *
4497 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
4498 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
4499 0ebf8283 2019-07-24 stsp
4500 0ebf8283 2019-07-24 stsp static const struct got_error *
4501 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
4502 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
4503 0ebf8283 2019-07-24 stsp {
4504 0ebf8283 2019-07-24 stsp const struct got_error *err;
4505 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4506 0ebf8283 2019-07-24 stsp char *path = NULL;
4507 0ebf8283 2019-07-24 stsp
4508 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
4509 0ebf8283 2019-07-24 stsp if (err)
4510 0ebf8283 2019-07-24 stsp return err;
4511 0ebf8283 2019-07-24 stsp
4512 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
4513 0ebf8283 2019-07-24 stsp if (err)
4514 0ebf8283 2019-07-24 stsp goto done;
4515 0ebf8283 2019-07-24 stsp
4516 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
4517 0ebf8283 2019-07-24 stsp if (err)
4518 0ebf8283 2019-07-24 stsp goto done;
4519 0ebf8283 2019-07-24 stsp
4520 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
4521 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4522 0ebf8283 2019-07-24 stsp goto done;
4523 0ebf8283 2019-07-24 stsp }
4524 0ebf8283 2019-07-24 stsp f = NULL;
4525 0ebf8283 2019-07-24 stsp
4526 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
4527 0ebf8283 2019-07-24 stsp if (err) {
4528 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4529 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4530 0ebf8283 2019-07-24 stsp goto done;
4531 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
4532 0ebf8283 2019-07-24 stsp commits, path, repo);
4533 0ebf8283 2019-07-24 stsp }
4534 0ebf8283 2019-07-24 stsp done:
4535 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4536 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4537 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
4538 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
4539 0ebf8283 2019-07-24 stsp free(path);
4540 0ebf8283 2019-07-24 stsp return err;
4541 0ebf8283 2019-07-24 stsp }
4542 0ebf8283 2019-07-24 stsp
4543 0ebf8283 2019-07-24 stsp static const struct got_error *
4544 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
4545 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4546 0ebf8283 2019-07-24 stsp {
4547 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4548 0ebf8283 2019-07-24 stsp char *path = NULL;
4549 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4550 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4551 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4552 0ebf8283 2019-07-24 stsp
4553 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
4554 0ebf8283 2019-07-24 stsp if (err)
4555 0ebf8283 2019-07-24 stsp return err;
4556 0ebf8283 2019-07-24 stsp
4557 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
4558 0ebf8283 2019-07-24 stsp if (f == NULL) {
4559 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4560 0ebf8283 2019-07-24 stsp goto done;
4561 0ebf8283 2019-07-24 stsp }
4562 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
4563 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
4564 0ebf8283 2019-07-24 stsp repo);
4565 0ebf8283 2019-07-24 stsp if (err)
4566 0ebf8283 2019-07-24 stsp break;
4567 0ebf8283 2019-07-24 stsp
4568 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4569 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
4570 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
4571 0ebf8283 2019-07-24 stsp if (n < 0) {
4572 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
4573 0ebf8283 2019-07-24 stsp break;
4574 0ebf8283 2019-07-24 stsp }
4575 0ebf8283 2019-07-24 stsp }
4576 0ebf8283 2019-07-24 stsp }
4577 0ebf8283 2019-07-24 stsp done:
4578 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4579 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4580 0ebf8283 2019-07-24 stsp free(path);
4581 0ebf8283 2019-07-24 stsp if (commit)
4582 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4583 0ebf8283 2019-07-24 stsp return err;
4584 0ebf8283 2019-07-24 stsp }
4585 0ebf8283 2019-07-24 stsp
4586 bfce7f83 2019-07-27 stsp void
4587 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
4588 bfce7f83 2019-07-27 stsp {
4589 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
4590 bfce7f83 2019-07-27 stsp
4591 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
4592 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
4593 bfce7f83 2019-07-27 stsp free(hle);
4594 bfce7f83 2019-07-27 stsp }
4595 bfce7f83 2019-07-27 stsp }
4596 bfce7f83 2019-07-27 stsp
4597 0ebf8283 2019-07-24 stsp static const struct got_error *
4598 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
4599 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4600 0ebf8283 2019-07-24 stsp {
4601 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
4602 0ebf8283 2019-07-24 stsp FILE *f = NULL;
4603 0ebf8283 2019-07-24 stsp
4604 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
4605 0ebf8283 2019-07-24 stsp if (f == NULL) {
4606 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
4607 0ebf8283 2019-07-24 stsp goto done;
4608 0ebf8283 2019-07-24 stsp }
4609 0ebf8283 2019-07-24 stsp
4610 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
4611 0ebf8283 2019-07-24 stsp done:
4612 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
4613 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
4614 0ebf8283 2019-07-24 stsp return err;
4615 0ebf8283 2019-07-24 stsp }
4616 0ebf8283 2019-07-24 stsp
4617 0ebf8283 2019-07-24 stsp static const struct got_error *
4618 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
4619 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
4620 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
4621 0ebf8283 2019-07-24 stsp {
4622 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
4623 0ebf8283 2019-07-24 stsp int resp = ' ';
4624 0ebf8283 2019-07-24 stsp
4625 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
4626 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
4627 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
4628 0ebf8283 2019-07-24 stsp resp = getchar();
4629 426ebf2e 2019-07-25 stsp if (resp == 'c') {
4630 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4631 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
4632 bfce7f83 2019-07-27 stsp repo);
4633 426ebf2e 2019-07-25 stsp if (err) {
4634 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4635 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4636 426ebf2e 2019-07-25 stsp break;
4637 bfce7f83 2019-07-27 stsp prev_err = err;
4638 426ebf2e 2019-07-25 stsp resp = ' ';
4639 426ebf2e 2019-07-25 stsp continue;
4640 426ebf2e 2019-07-25 stsp }
4641 bfce7f83 2019-07-27 stsp break;
4642 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
4643 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
4644 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
4645 0ebf8283 2019-07-24 stsp commits, repo);
4646 426ebf2e 2019-07-25 stsp if (err) {
4647 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
4648 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
4649 426ebf2e 2019-07-25 stsp break;
4650 bfce7f83 2019-07-27 stsp prev_err = err;
4651 426ebf2e 2019-07-25 stsp resp = ' ';
4652 426ebf2e 2019-07-25 stsp continue;
4653 426ebf2e 2019-07-25 stsp }
4654 bfce7f83 2019-07-27 stsp break;
4655 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
4656 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
4657 0ebf8283 2019-07-24 stsp break;
4658 426ebf2e 2019-07-25 stsp } else
4659 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
4660 0ebf8283 2019-07-24 stsp }
4661 0ebf8283 2019-07-24 stsp
4662 0ebf8283 2019-07-24 stsp return err;
4663 0ebf8283 2019-07-24 stsp }
4664 0ebf8283 2019-07-24 stsp
4665 0ebf8283 2019-07-24 stsp static const struct got_error *
4666 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
4667 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
4668 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
4669 0ebf8283 2019-07-24 stsp {
4670 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4671 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4672 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
4673 3e3a69f1 2019-07-25 stsp branch, repo);
4674 0ebf8283 2019-07-24 stsp }
4675 0ebf8283 2019-07-24 stsp
4676 0ebf8283 2019-07-24 stsp static const struct got_error *
4677 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
4678 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
4679 0ebf8283 2019-07-24 stsp {
4680 0ebf8283 2019-07-24 stsp const struct got_error *err;
4681 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4682 0ebf8283 2019-07-24 stsp
4683 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
4684 0ebf8283 2019-07-24 stsp if (err)
4685 0ebf8283 2019-07-24 stsp goto done;
4686 0ebf8283 2019-07-24 stsp
4687 0ebf8283 2019-07-24 stsp if (new_id) {
4688 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
4689 0ebf8283 2019-07-24 stsp if (err)
4690 0ebf8283 2019-07-24 stsp goto done;
4691 0ebf8283 2019-07-24 stsp }
4692 0ebf8283 2019-07-24 stsp
4693 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
4694 0ebf8283 2019-07-24 stsp if (new_id_str)
4695 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
4696 0ebf8283 2019-07-24 stsp
4697 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
4698 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
4699 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
4700 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
4701 0ebf8283 2019-07-24 stsp goto done;
4702 0ebf8283 2019-07-24 stsp }
4703 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
4704 0ebf8283 2019-07-24 stsp } else {
4705 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4706 0ebf8283 2019-07-24 stsp if (err)
4707 0ebf8283 2019-07-24 stsp goto done;
4708 0ebf8283 2019-07-24 stsp }
4709 0ebf8283 2019-07-24 stsp
4710 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
4711 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
4712 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
4713 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
4714 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4715 0ebf8283 2019-07-24 stsp break;
4716 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
4717 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
4718 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
4719 0ebf8283 2019-07-24 stsp logmsg);
4720 0ebf8283 2019-07-24 stsp break;
4721 0ebf8283 2019-07-24 stsp default:
4722 0ebf8283 2019-07-24 stsp break;
4723 0ebf8283 2019-07-24 stsp }
4724 0ebf8283 2019-07-24 stsp
4725 0ebf8283 2019-07-24 stsp done:
4726 0ebf8283 2019-07-24 stsp free(old_id_str);
4727 0ebf8283 2019-07-24 stsp free(new_id_str);
4728 0ebf8283 2019-07-24 stsp return err;
4729 0ebf8283 2019-07-24 stsp }
4730 0ebf8283 2019-07-24 stsp
4731 0ebf8283 2019-07-24 stsp static const struct got_error *
4732 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
4733 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4734 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
4735 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
4736 0ebf8283 2019-07-24 stsp {
4737 0ebf8283 2019-07-24 stsp const struct got_error *err;
4738 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4739 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
4740 0ebf8283 2019-07-24 stsp
4741 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
4742 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
4743 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
4744 0ebf8283 2019-07-24 stsp if (err)
4745 0ebf8283 2019-07-24 stsp return err;
4746 0ebf8283 2019-07-24 stsp }
4747 0ebf8283 2019-07-24 stsp
4748 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
4749 0ebf8283 2019-07-24 stsp if (err)
4750 0ebf8283 2019-07-24 stsp return err;
4751 0ebf8283 2019-07-24 stsp
4752 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
4753 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
4754 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
4755 0ebf8283 2019-07-24 stsp if (err) {
4756 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
4757 0ebf8283 2019-07-24 stsp goto done;
4758 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
4759 0ebf8283 2019-07-24 stsp } else {
4760 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
4761 0ebf8283 2019-07-24 stsp free(new_commit_id);
4762 0ebf8283 2019-07-24 stsp }
4763 0ebf8283 2019-07-24 stsp done:
4764 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4765 0ebf8283 2019-07-24 stsp return err;
4766 0ebf8283 2019-07-24 stsp }
4767 0ebf8283 2019-07-24 stsp
4768 0ebf8283 2019-07-24 stsp static const struct got_error *
4769 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
4770 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
4771 0ebf8283 2019-07-24 stsp {
4772 0ebf8283 2019-07-24 stsp const struct got_error *error;
4773 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
4774 0ebf8283 2019-07-24 stsp
4775 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
4776 0ebf8283 2019-07-24 stsp repo);
4777 0ebf8283 2019-07-24 stsp if (error)
4778 0ebf8283 2019-07-24 stsp return error;
4779 0ebf8283 2019-07-24 stsp
4780 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
4781 0ebf8283 2019-07-24 stsp if (error)
4782 0ebf8283 2019-07-24 stsp return error;
4783 0ebf8283 2019-07-24 stsp
4784 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
4785 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4786 0ebf8283 2019-07-24 stsp return error;
4787 0ebf8283 2019-07-24 stsp }
4788 0ebf8283 2019-07-24 stsp
4789 0ebf8283 2019-07-24 stsp static const struct got_error *
4790 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
4791 0ebf8283 2019-07-24 stsp {
4792 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
4793 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
4794 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
4795 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
4796 0ebf8283 2019-07-24 stsp char *cwd = NULL;
4797 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
4798 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
4799 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
4800 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
4801 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
4802 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
4803 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
4804 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
4805 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
4806 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
4807 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
4808 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
4809 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
4810 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
4811 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
4812 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
4813 0ebf8283 2019-07-24 stsp
4814 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
4815 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
4816 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
4817 0ebf8283 2019-07-24 stsp
4818 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
4819 0ebf8283 2019-07-24 stsp switch (ch) {
4820 0ebf8283 2019-07-24 stsp case 'a':
4821 0ebf8283 2019-07-24 stsp abort_edit = 1;
4822 0ebf8283 2019-07-24 stsp break;
4823 0ebf8283 2019-07-24 stsp case 'c':
4824 0ebf8283 2019-07-24 stsp continue_edit = 1;
4825 0ebf8283 2019-07-24 stsp break;
4826 0ebf8283 2019-07-24 stsp case 'F':
4827 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
4828 0ebf8283 2019-07-24 stsp break;
4829 0ebf8283 2019-07-24 stsp default:
4830 0ebf8283 2019-07-24 stsp usage_histedit();
4831 0ebf8283 2019-07-24 stsp /* NOTREACHED */
4832 0ebf8283 2019-07-24 stsp }
4833 0ebf8283 2019-07-24 stsp }
4834 0ebf8283 2019-07-24 stsp
4835 0ebf8283 2019-07-24 stsp argc -= optind;
4836 0ebf8283 2019-07-24 stsp argv += optind;
4837 0ebf8283 2019-07-24 stsp
4838 0ebf8283 2019-07-24 stsp #ifndef PROFILE
4839 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4840 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
4841 0ebf8283 2019-07-24 stsp err(1, "pledge");
4842 0ebf8283 2019-07-24 stsp #endif
4843 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
4844 0ebf8283 2019-07-24 stsp usage_histedit();
4845 0ebf8283 2019-07-24 stsp if (argc != 0)
4846 0ebf8283 2019-07-24 stsp usage_histedit();
4847 0ebf8283 2019-07-24 stsp
4848 0ebf8283 2019-07-24 stsp /*
4849 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
4850 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
4851 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
4852 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
4853 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
4854 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
4855 0ebf8283 2019-07-24 stsp */
4856 0ebf8283 2019-07-24 stsp
4857 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
4858 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
4859 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
4860 0ebf8283 2019-07-24 stsp goto done;
4861 0ebf8283 2019-07-24 stsp }
4862 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
4863 0ebf8283 2019-07-24 stsp if (error)
4864 0ebf8283 2019-07-24 stsp goto done;
4865 0ebf8283 2019-07-24 stsp
4866 0ebf8283 2019-07-24 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
4867 0ebf8283 2019-07-24 stsp if (error != NULL)
4868 0ebf8283 2019-07-24 stsp goto done;
4869 0ebf8283 2019-07-24 stsp
4870 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4871 0ebf8283 2019-07-24 stsp if (error)
4872 0ebf8283 2019-07-24 stsp goto done;
4873 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
4874 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
4875 0ebf8283 2019-07-24 stsp goto done;
4876 0ebf8283 2019-07-24 stsp }
4877 0ebf8283 2019-07-24 stsp
4878 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
4879 0ebf8283 2019-07-24 stsp if (error)
4880 0ebf8283 2019-07-24 stsp goto done;
4881 0ebf8283 2019-07-24 stsp
4882 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
4883 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4884 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4885 3e3a69f1 2019-07-25 stsp worktree, repo);
4886 0ebf8283 2019-07-24 stsp if (error)
4887 0ebf8283 2019-07-24 stsp goto done;
4888 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
4889 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4890 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
4891 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
4892 0ebf8283 2019-07-24 stsp if (error)
4893 0ebf8283 2019-07-24 stsp goto done;
4894 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
4895 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
4896 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
4897 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
4898 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4899 0ebf8283 2019-07-24 stsp goto done;
4900 0ebf8283 2019-07-24 stsp }
4901 0ebf8283 2019-07-24 stsp
4902 0ebf8283 2019-07-24 stsp if (continue_edit) {
4903 0ebf8283 2019-07-24 stsp char *path;
4904 0ebf8283 2019-07-24 stsp
4905 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
4906 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
4907 0ebf8283 2019-07-24 stsp goto done;
4908 0ebf8283 2019-07-24 stsp }
4909 0ebf8283 2019-07-24 stsp
4910 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
4911 0ebf8283 2019-07-24 stsp if (error)
4912 0ebf8283 2019-07-24 stsp goto done;
4913 0ebf8283 2019-07-24 stsp
4914 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
4915 0ebf8283 2019-07-24 stsp free(path);
4916 0ebf8283 2019-07-24 stsp if (error)
4917 0ebf8283 2019-07-24 stsp goto done;
4918 0ebf8283 2019-07-24 stsp
4919 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
4920 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
4921 3e3a69f1 2019-07-25 stsp worktree, repo);
4922 0ebf8283 2019-07-24 stsp if (error)
4923 0ebf8283 2019-07-24 stsp goto done;
4924 0ebf8283 2019-07-24 stsp
4925 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4926 0ebf8283 2019-07-24 stsp if (error)
4927 0ebf8283 2019-07-24 stsp goto done;
4928 0ebf8283 2019-07-24 stsp
4929 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4930 0ebf8283 2019-07-24 stsp head_commit_id);
4931 0ebf8283 2019-07-24 stsp if (error)
4932 0ebf8283 2019-07-24 stsp goto done;
4933 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4934 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4935 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4936 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4937 3aac7cf7 2019-07-25 stsp goto done;
4938 3aac7cf7 2019-07-25 stsp }
4939 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4940 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
4941 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4942 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4943 0ebf8283 2019-07-24 stsp commit = NULL;
4944 0ebf8283 2019-07-24 stsp if (error)
4945 0ebf8283 2019-07-24 stsp goto done;
4946 0ebf8283 2019-07-24 stsp } else {
4947 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
4948 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
4949 0ebf8283 2019-07-24 stsp goto done;
4950 0ebf8283 2019-07-24 stsp }
4951 0ebf8283 2019-07-24 stsp
4952 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
4953 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
4954 0ebf8283 2019-07-24 stsp if (error != NULL)
4955 0ebf8283 2019-07-24 stsp goto done;
4956 0ebf8283 2019-07-24 stsp
4957 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
4958 bfce7f83 2019-07-27 stsp got_ref_close(branch);
4959 bfce7f83 2019-07-27 stsp branch = NULL;
4960 0ebf8283 2019-07-24 stsp if (error)
4961 0ebf8283 2019-07-24 stsp goto done;
4962 0ebf8283 2019-07-24 stsp
4963 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
4964 0ebf8283 2019-07-24 stsp head_commit_id);
4965 0ebf8283 2019-07-24 stsp if (error)
4966 0ebf8283 2019-07-24 stsp goto done;
4967 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
4968 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
4969 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
4970 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
4971 3aac7cf7 2019-07-25 stsp goto done;
4972 3aac7cf7 2019-07-25 stsp }
4973 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
4974 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
4975 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
4976 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
4977 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
4978 0ebf8283 2019-07-24 stsp commit = NULL;
4979 0ebf8283 2019-07-24 stsp if (error)
4980 0ebf8283 2019-07-24 stsp goto done;
4981 0ebf8283 2019-07-24 stsp
4982 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
4983 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
4984 bfce7f83 2019-07-27 stsp if (error)
4985 bfce7f83 2019-07-27 stsp goto done;
4986 bfce7f83 2019-07-27 stsp
4987 0ebf8283 2019-07-24 stsp if (edit_script_path) {
4988 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
4989 0ebf8283 2019-07-24 stsp edit_script_path, repo);
4990 6ba2bea2 2019-07-27 stsp if (error) {
4991 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
4992 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
4993 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
4994 0ebf8283 2019-07-24 stsp goto done;
4995 6ba2bea2 2019-07-27 stsp }
4996 0ebf8283 2019-07-24 stsp } else {
4997 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
4998 0ebf8283 2019-07-24 stsp repo);
4999 6ba2bea2 2019-07-27 stsp if (error) {
5000 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5001 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5002 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5003 0ebf8283 2019-07-24 stsp goto done;
5004 6ba2bea2 2019-07-27 stsp }
5005 0ebf8283 2019-07-24 stsp
5006 0ebf8283 2019-07-24 stsp }
5007 0ebf8283 2019-07-24 stsp
5008 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
5009 0ebf8283 2019-07-24 stsp repo);
5010 6ba2bea2 2019-07-27 stsp if (error) {
5011 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
5012 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
5013 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
5014 0ebf8283 2019-07-24 stsp goto done;
5015 6ba2bea2 2019-07-27 stsp }
5016 0ebf8283 2019-07-24 stsp
5017 0ebf8283 2019-07-24 stsp }
5018 0ebf8283 2019-07-24 stsp
5019 0ebf8283 2019-07-24 stsp error = histedit_check_script(&histedit_cmds, &commits, repo);
5020 0ebf8283 2019-07-24 stsp if (error)
5021 0ebf8283 2019-07-24 stsp goto done;
5022 0ebf8283 2019-07-24 stsp
5023 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
5024 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
5025 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
5026 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
5027 0ebf8283 2019-07-24 stsp continue;
5028 0ebf8283 2019-07-24 stsp
5029 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
5030 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
5031 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
5032 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
5033 0ebf8283 2019-07-24 stsp repo);
5034 0ebf8283 2019-07-24 stsp } else {
5035 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
5036 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
5037 0ebf8283 2019-07-24 stsp }
5038 0ebf8283 2019-07-24 stsp if (error)
5039 0ebf8283 2019-07-24 stsp goto done;
5040 0ebf8283 2019-07-24 stsp continue;
5041 0ebf8283 2019-07-24 stsp }
5042 0ebf8283 2019-07-24 stsp
5043 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
5044 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5045 0ebf8283 2019-07-24 stsp if (error)
5046 0ebf8283 2019-07-24 stsp goto done;
5047 0ebf8283 2019-07-24 stsp continue;
5048 0ebf8283 2019-07-24 stsp }
5049 0ebf8283 2019-07-24 stsp
5050 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
5051 0ebf8283 2019-07-24 stsp hle->commit_id);
5052 0ebf8283 2019-07-24 stsp if (error)
5053 0ebf8283 2019-07-24 stsp goto done;
5054 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5055 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
5056 0ebf8283 2019-07-24 stsp
5057 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
5058 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
5059 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5060 0ebf8283 2019-07-24 stsp if (error)
5061 0ebf8283 2019-07-24 stsp goto done;
5062 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5063 0ebf8283 2019-07-24 stsp commit = NULL;
5064 0ebf8283 2019-07-24 stsp
5065 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5066 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5067 0ebf8283 2019-07-24 stsp break;
5068 0ebf8283 2019-07-24 stsp }
5069 0ebf8283 2019-07-24 stsp
5070 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
5071 0ebf8283 2019-07-24 stsp char *id_str;
5072 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
5073 0ebf8283 2019-07-24 stsp if (error)
5074 0ebf8283 2019-07-24 stsp goto done;
5075 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
5076 0ebf8283 2019-07-24 stsp id_str);
5077 0ebf8283 2019-07-24 stsp free(id_str);
5078 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5079 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
5080 3e3a69f1 2019-07-25 stsp fileindex);
5081 0ebf8283 2019-07-24 stsp goto done;
5082 0ebf8283 2019-07-24 stsp }
5083 0ebf8283 2019-07-24 stsp
5084 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
5085 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
5086 0ebf8283 2019-07-24 stsp if (error)
5087 0ebf8283 2019-07-24 stsp goto done;
5088 0ebf8283 2019-07-24 stsp continue;
5089 0ebf8283 2019-07-24 stsp }
5090 0ebf8283 2019-07-24 stsp
5091 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
5092 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
5093 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5094 0ebf8283 2019-07-24 stsp if (error)
5095 0ebf8283 2019-07-24 stsp goto done;
5096 0ebf8283 2019-07-24 stsp }
5097 0ebf8283 2019-07-24 stsp
5098 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5099 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
5100 0ebf8283 2019-07-24 stsp if (error)
5101 0ebf8283 2019-07-24 stsp goto done;
5102 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5103 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
5104 0ebf8283 2019-07-24 stsp } else
5105 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
5106 3e3a69f1 2019-07-25 stsp branch, repo);
5107 0ebf8283 2019-07-24 stsp done:
5108 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
5109 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
5110 0ebf8283 2019-07-24 stsp free(head_commit_id);
5111 0ebf8283 2019-07-24 stsp free(base_commit_id);
5112 0ebf8283 2019-07-24 stsp free(resume_commit_id);
5113 0ebf8283 2019-07-24 stsp if (commit)
5114 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5115 0ebf8283 2019-07-24 stsp if (branch)
5116 0ebf8283 2019-07-24 stsp got_ref_close(branch);
5117 0ebf8283 2019-07-24 stsp if (tmp_branch)
5118 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
5119 0ebf8283 2019-07-24 stsp if (worktree)
5120 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
5121 0ebf8283 2019-07-24 stsp if (repo)
5122 0ebf8283 2019-07-24 stsp got_repo_close(repo);
5123 0ebf8283 2019-07-24 stsp return error;
5124 0ebf8283 2019-07-24 stsp }