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