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 f42b1b34 2018-03-12 stsp #include "got_error.h"
39 f42b1b34 2018-03-12 stsp #include "got_object.h"
40 5261c201 2018-04-01 stsp #include "got_reference.h"
41 f42b1b34 2018-03-12 stsp #include "got_repository.h"
42 1dd54920 2019-05-11 stsp #include "got_path.h"
43 c09a553d 2018-03-12 stsp #include "got_worktree.h"
44 79109fed 2018-03-27 stsp #include "got_diff.h"
45 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
46 404c43c4 2018-06-21 stsp #include "got_blame.h"
47 63219cd2 2019-01-04 stsp #include "got_privsep.h"
48 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
49 5c860e29 2018-03-12 stsp
50 5c860e29 2018-03-12 stsp #ifndef nitems
51 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
52 5c860e29 2018-03-12 stsp #endif
53 99437157 2018-11-11 stsp
54 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigint(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigint_received = 1;
61 99437157 2018-11-11 stsp }
62 99437157 2018-11-11 stsp
63 99437157 2018-11-11 stsp static void
64 99437157 2018-11-11 stsp catch_sigpipe(int signo)
65 99437157 2018-11-11 stsp {
66 99437157 2018-11-11 stsp sigpipe_received = 1;
67 99437157 2018-11-11 stsp }
68 5c860e29 2018-03-12 stsp
69 99437157 2018-11-11 stsp
70 8cfb4057 2019-07-09 stsp struct got_cmd {
71 5c860e29 2018-03-12 stsp const char *cmd_name;
72 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
73 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
74 97b3a7be 2019-07-09 stsp const char *cmd_alias;
75 5c860e29 2018-03-12 stsp };
76 5c860e29 2018-03-12 stsp
77 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
78 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
79 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
80 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
81 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
83 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
84 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
85 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
86 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
87 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
88 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
89 d00136be 2019-03-26 stsp __dead static void usage_add(void);
90 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
91 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
92 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
93 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
94 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
95 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
96 5c860e29 2018-03-12 stsp
97 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
98 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
99 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
100 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
101 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
102 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
103 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
104 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
105 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
106 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
107 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
108 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
109 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
110 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
111 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
112 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
113 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
114 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
115 5c860e29 2018-03-12 stsp
116 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
117 97b3a7be 2019-07-09 stsp { "init", cmd_init, usage_init, "" },
118 3ce1b845 2019-07-15 stsp { "import", cmd_import, usage_import, "" },
119 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
120 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
121 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
122 97b3a7be 2019-07-09 stsp { "diff", cmd_diff, usage_diff, "" },
123 97b3a7be 2019-07-09 stsp { "blame", cmd_blame, usage_blame, "" },
124 97b3a7be 2019-07-09 stsp { "tree", cmd_tree, usage_tree, "" },
125 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
126 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
127 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
128 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
129 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
130 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
131 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
132 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
133 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
134 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
135 5c860e29 2018-03-12 stsp };
136 ce5b7c56 2019-07-09 stsp
137 ce5b7c56 2019-07-09 stsp static void
138 ce5b7c56 2019-07-09 stsp list_commands(void)
139 ce5b7c56 2019-07-09 stsp {
140 ce5b7c56 2019-07-09 stsp int i;
141 ce5b7c56 2019-07-09 stsp
142 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
143 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
144 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
145 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
146 ce5b7c56 2019-07-09 stsp }
147 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
148 ce5b7c56 2019-07-09 stsp }
149 5c860e29 2018-03-12 stsp
150 5c860e29 2018-03-12 stsp int
151 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
152 5c860e29 2018-03-12 stsp {
153 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
154 5c860e29 2018-03-12 stsp unsigned int i;
155 5c860e29 2018-03-12 stsp int ch;
156 1b6b95a8 2018-03-12 stsp int hflag = 0;
157 5c860e29 2018-03-12 stsp
158 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
159 5c860e29 2018-03-12 stsp
160 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
161 5c860e29 2018-03-12 stsp switch (ch) {
162 1b6b95a8 2018-03-12 stsp case 'h':
163 1b6b95a8 2018-03-12 stsp hflag = 1;
164 1b6b95a8 2018-03-12 stsp break;
165 5c860e29 2018-03-12 stsp default:
166 ce5b7c56 2019-07-09 stsp usage(hflag);
167 5c860e29 2018-03-12 stsp /* NOTREACHED */
168 5c860e29 2018-03-12 stsp }
169 5c860e29 2018-03-12 stsp }
170 5c860e29 2018-03-12 stsp
171 5c860e29 2018-03-12 stsp argc -= optind;
172 5c860e29 2018-03-12 stsp argv += optind;
173 1e70621d 2018-03-27 stsp optind = 0;
174 5c860e29 2018-03-12 stsp
175 5c860e29 2018-03-12 stsp if (argc <= 0)
176 ce5b7c56 2019-07-09 stsp usage(hflag);
177 5c860e29 2018-03-12 stsp
178 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
179 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
180 99437157 2018-11-11 stsp
181 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
182 d7d4f210 2018-03-12 stsp const struct got_error *error;
183 d7d4f210 2018-03-12 stsp
184 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
185 5c860e29 2018-03-12 stsp
186 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
187 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
188 5c860e29 2018-03-12 stsp continue;
189 5c860e29 2018-03-12 stsp
190 1b6b95a8 2018-03-12 stsp if (hflag)
191 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
192 1b6b95a8 2018-03-12 stsp
193 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
194 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
195 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
196 d7d4f210 2018-03-12 stsp return 1;
197 d7d4f210 2018-03-12 stsp }
198 d7d4f210 2018-03-12 stsp
199 d7d4f210 2018-03-12 stsp return 0;
200 5c860e29 2018-03-12 stsp }
201 5c860e29 2018-03-12 stsp
202 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
203 ce5b7c56 2019-07-09 stsp list_commands();
204 5c860e29 2018-03-12 stsp return 1;
205 5c860e29 2018-03-12 stsp }
206 5c860e29 2018-03-12 stsp
207 4ed7e80c 2018-05-20 stsp __dead static void
208 ce5b7c56 2019-07-09 stsp usage(int hflag)
209 5c860e29 2018-03-12 stsp {
210 5e070240 2019-06-22 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n", getprogname());
211 ce5b7c56 2019-07-09 stsp if (hflag)
212 ce5b7c56 2019-07-09 stsp list_commands();
213 5c860e29 2018-03-12 stsp exit(1);
214 5c860e29 2018-03-12 stsp }
215 5c860e29 2018-03-12 stsp
216 0266afb7 2019-01-04 stsp static const struct got_error *
217 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
218 e2ba3d07 2019-05-13 stsp {
219 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
220 e2ba3d07 2019-05-13 stsp const char *editor;
221 e2ba3d07 2019-05-13 stsp
222 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
223 e2ba3d07 2019-05-13 stsp if (editor == NULL)
224 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
225 e2ba3d07 2019-05-13 stsp
226 0ee7065d 2019-05-13 stsp if (editor) {
227 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
228 0ee7065d 2019-05-13 stsp if (err)
229 0ee7065d 2019-05-13 stsp return err;
230 0ee7065d 2019-05-13 stsp }
231 e2ba3d07 2019-05-13 stsp
232 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
233 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
234 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
235 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
236 0ee7065d 2019-05-13 stsp }
237 0ee7065d 2019-05-13 stsp
238 e2ba3d07 2019-05-13 stsp return NULL;
239 e2ba3d07 2019-05-13 stsp }
240 e2ba3d07 2019-05-13 stsp
241 e2ba3d07 2019-05-13 stsp static const struct got_error *
242 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
243 314a6357 2019-05-13 stsp const char *worktree_path, int create_worktree)
244 0266afb7 2019-01-04 stsp {
245 163ce85a 2019-05-13 stsp const struct got_error *err;
246 0266afb7 2019-01-04 stsp
247 37c06ea4 2019-07-15 stsp #ifdef PROFILE
248 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
249 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
250 37c06ea4 2019-07-15 stsp #endif
251 a0937847 2019-05-11 stsp if (create_worktree) {
252 a0937847 2019-05-11 stsp /* Pre-create work tree path to avoid unveiling its parents. */
253 163ce85a 2019-05-13 stsp err = got_path_mkdir(worktree_path);
254 3c45a30a 2019-05-12 jcs
255 3c45a30a 2019-05-12 jcs if (errno == EEXIST) {
256 280f921b 2019-05-12 stsp if (got_path_dir_is_empty(worktree_path)) {
257 3c45a30a 2019-05-12 jcs errno = 0;
258 163ce85a 2019-05-13 stsp err = NULL;
259 3c45a30a 2019-05-12 jcs } else {
260 df056ada 2019-05-15 stsp err = got_error_path(worktree_path,
261 df056ada 2019-05-15 stsp GOT_ERR_DIR_NOT_EMPTY);
262 3c45a30a 2019-05-12 jcs }
263 3c45a30a 2019-05-12 jcs }
264 3c45a30a 2019-05-12 jcs
265 163ce85a 2019-05-13 stsp if (err && (err->code != GOT_ERR_ERRNO || errno != EISDIR))
266 163ce85a 2019-05-13 stsp return err;
267 a0937847 2019-05-11 stsp }
268 a0937847 2019-05-11 stsp
269 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
270 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
271 0266afb7 2019-01-04 stsp
272 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
273 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
274 0266afb7 2019-01-04 stsp
275 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
276 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
277 0266afb7 2019-01-04 stsp
278 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
279 163ce85a 2019-05-13 stsp if (err != NULL)
280 163ce85a 2019-05-13 stsp return err;
281 0266afb7 2019-01-04 stsp
282 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
284 0266afb7 2019-01-04 stsp
285 0266afb7 2019-01-04 stsp return NULL;
286 2c7829a4 2019-06-17 stsp }
287 2c7829a4 2019-06-17 stsp
288 2c7829a4 2019-06-17 stsp __dead static void
289 2c7829a4 2019-06-17 stsp usage_init(void)
290 2c7829a4 2019-06-17 stsp {
291 2c7829a4 2019-06-17 stsp fprintf(stderr, "usage: %s init path\n", getprogname());
292 2c7829a4 2019-06-17 stsp exit(1);
293 2c7829a4 2019-06-17 stsp }
294 2c7829a4 2019-06-17 stsp
295 2c7829a4 2019-06-17 stsp static const struct got_error *
296 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
297 2c7829a4 2019-06-17 stsp {
298 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
299 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
300 2c7829a4 2019-06-17 stsp int ch;
301 2c7829a4 2019-06-17 stsp
302 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
303 2c7829a4 2019-06-17 stsp switch (ch) {
304 2c7829a4 2019-06-17 stsp default:
305 2c7829a4 2019-06-17 stsp usage_init();
306 2c7829a4 2019-06-17 stsp /* NOTREACHED */
307 2c7829a4 2019-06-17 stsp }
308 2c7829a4 2019-06-17 stsp }
309 2c7829a4 2019-06-17 stsp
310 2c7829a4 2019-06-17 stsp argc -= optind;
311 2c7829a4 2019-06-17 stsp argv += optind;
312 2c7829a4 2019-06-17 stsp
313 2c7829a4 2019-06-17 stsp #ifndef PROFILE
314 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
315 2c7829a4 2019-06-17 stsp err(1, "pledge");
316 2c7829a4 2019-06-17 stsp #endif
317 2c7829a4 2019-06-17 stsp if (argc != 1)
318 bc20e173 2019-06-17 stsp usage_init();
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
321 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
322 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
323 2c7829a4 2019-06-17 stsp
324 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
327 2c7829a4 2019-06-17 stsp if (error &&
328 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
329 2c7829a4 2019-06-17 stsp goto done;
330 2c7829a4 2019-06-17 stsp
331 2c7829a4 2019-06-17 stsp error = apply_unveil(repo_path, 0, NULL, 0);
332 2c7829a4 2019-06-17 stsp if (error)
333 2c7829a4 2019-06-17 stsp goto done;
334 2c7829a4 2019-06-17 stsp
335 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
336 2c7829a4 2019-06-17 stsp if (error != NULL)
337 3ce1b845 2019-07-15 stsp goto done;
338 3ce1b845 2019-07-15 stsp
339 3ce1b845 2019-07-15 stsp done:
340 3ce1b845 2019-07-15 stsp free(repo_path);
341 3ce1b845 2019-07-15 stsp return error;
342 3ce1b845 2019-07-15 stsp }
343 3ce1b845 2019-07-15 stsp
344 3ce1b845 2019-07-15 stsp __dead static void
345 3ce1b845 2019-07-15 stsp usage_import(void)
346 3ce1b845 2019-07-15 stsp {
347 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
348 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
349 3ce1b845 2019-07-15 stsp exit(1);
350 3ce1b845 2019-07-15 stsp }
351 3ce1b845 2019-07-15 stsp
352 3ce1b845 2019-07-15 stsp int
353 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
354 3ce1b845 2019-07-15 stsp {
355 3ce1b845 2019-07-15 stsp pid_t pid;
356 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
357 3ce1b845 2019-07-15 stsp int st = -1;
358 3ce1b845 2019-07-15 stsp
359 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
360 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
361 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
362 3ce1b845 2019-07-15 stsp
363 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
364 3ce1b845 2019-07-15 stsp case -1:
365 3ce1b845 2019-07-15 stsp goto doneediting;
366 3ce1b845 2019-07-15 stsp case 0:
367 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
368 3ce1b845 2019-07-15 stsp _exit(127);
369 3ce1b845 2019-07-15 stsp }
370 3ce1b845 2019-07-15 stsp
371 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
372 3ce1b845 2019-07-15 stsp if (errno != EINTR)
373 3ce1b845 2019-07-15 stsp break;
374 3ce1b845 2019-07-15 stsp
375 3ce1b845 2019-07-15 stsp doneediting:
376 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
377 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
378 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
379 3ce1b845 2019-07-15 stsp
380 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
381 3ce1b845 2019-07-15 stsp errno = EINTR;
382 3ce1b845 2019-07-15 stsp return -1;
383 3ce1b845 2019-07-15 stsp }
384 3ce1b845 2019-07-15 stsp
385 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
386 3ce1b845 2019-07-15 stsp }
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp static const struct got_error *
389 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
390 3ce1b845 2019-07-15 stsp const char *initial_content)
391 3ce1b845 2019-07-15 stsp {
392 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
393 3ce1b845 2019-07-15 stsp char buf[1024];
394 3ce1b845 2019-07-15 stsp struct stat st, st2;
395 3ce1b845 2019-07-15 stsp FILE *fp;
396 3ce1b845 2019-07-15 stsp int content_changed = 0;
397 3ce1b845 2019-07-15 stsp size_t len;
398 3ce1b845 2019-07-15 stsp
399 3ce1b845 2019-07-15 stsp *logmsg = NULL;
400 3ce1b845 2019-07-15 stsp
401 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
402 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
403 3ce1b845 2019-07-15 stsp
404 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
405 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
408 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
411 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
412 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
413 3ce1b845 2019-07-15 stsp
414 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
415 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
416 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
417 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
418 3ce1b845 2019-07-15 stsp len = 0;
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
421 3ce1b845 2019-07-15 stsp if (fp == NULL) {
422 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
423 3ce1b845 2019-07-15 stsp goto done;
424 3ce1b845 2019-07-15 stsp }
425 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
426 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
427 3ce1b845 2019-07-15 stsp content_changed = 1;
428 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
429 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
430 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
431 3ce1b845 2019-07-15 stsp }
432 3ce1b845 2019-07-15 stsp fclose(fp);
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
435 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
436 3ce1b845 2019-07-15 stsp len--;
437 3ce1b845 2019-07-15 stsp }
438 3ce1b845 2019-07-15 stsp
439 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
440 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
441 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
442 3ce1b845 2019-07-15 stsp done:
443 3ce1b845 2019-07-15 stsp if (err) {
444 3ce1b845 2019-07-15 stsp free(*logmsg);
445 3ce1b845 2019-07-15 stsp *logmsg = NULL;
446 3ce1b845 2019-07-15 stsp }
447 3ce1b845 2019-07-15 stsp return err;
448 3ce1b845 2019-07-15 stsp }
449 3ce1b845 2019-07-15 stsp
450 3ce1b845 2019-07-15 stsp static const struct got_error *
451 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
452 3ce1b845 2019-07-15 stsp const char *branch_name)
453 3ce1b845 2019-07-15 stsp {
454 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
455 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
456 3ce1b845 2019-07-15 stsp int fd;
457 3ce1b845 2019-07-15 stsp
458 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
459 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
460 3ce1b845 2019-07-15 stsp branch_name) == -1)
461 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
462 3ce1b845 2019-07-15 stsp
463 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
464 3ce1b845 2019-07-15 stsp if (err)
465 3ce1b845 2019-07-15 stsp goto done;
466 3ce1b845 2019-07-15 stsp
467 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
468 3ce1b845 2019-07-15 stsp close(fd);
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
471 3ce1b845 2019-07-15 stsp done:
472 3ce1b845 2019-07-15 stsp free(initial_content);
473 3ce1b845 2019-07-15 stsp free(logmsg_path);
474 3ce1b845 2019-07-15 stsp return err;
475 3ce1b845 2019-07-15 stsp }
476 3ce1b845 2019-07-15 stsp
477 3ce1b845 2019-07-15 stsp static const struct got_error *
478 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
479 3ce1b845 2019-07-15 stsp {
480 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
481 3ce1b845 2019-07-15 stsp return NULL;
482 3ce1b845 2019-07-15 stsp }
483 3ce1b845 2019-07-15 stsp
484 3ce1b845 2019-07-15 stsp static const struct got_error *
485 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
486 3ce1b845 2019-07-15 stsp {
487 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
488 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
489 3ce1b845 2019-07-15 stsp char *editor = NULL;
490 3ce1b845 2019-07-15 stsp const char *got_author = getenv("GOT_AUTHOR");
491 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
492 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
493 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
494 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
495 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
496 3ce1b845 2019-07-15 stsp int ch;
497 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
498 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
499 3ce1b845 2019-07-15 stsp
500 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
501 3ce1b845 2019-07-15 stsp
502 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
503 3ce1b845 2019-07-15 stsp switch (ch) {
504 3ce1b845 2019-07-15 stsp case 'b':
505 3ce1b845 2019-07-15 stsp branch_name = optarg;
506 3ce1b845 2019-07-15 stsp break;
507 3ce1b845 2019-07-15 stsp case 'm':
508 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
509 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
510 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
511 3ce1b845 2019-07-15 stsp goto done;
512 3ce1b845 2019-07-15 stsp }
513 3ce1b845 2019-07-15 stsp break;
514 3ce1b845 2019-07-15 stsp case 'r':
515 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
516 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
517 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
518 3ce1b845 2019-07-15 stsp goto done;
519 3ce1b845 2019-07-15 stsp }
520 3ce1b845 2019-07-15 stsp break;
521 3ce1b845 2019-07-15 stsp case 'I':
522 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
523 3ce1b845 2019-07-15 stsp break;
524 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
525 3ce1b845 2019-07-15 stsp NULL);
526 3ce1b845 2019-07-15 stsp if (error)
527 3ce1b845 2019-07-15 stsp goto done;
528 3ce1b845 2019-07-15 stsp break;
529 3ce1b845 2019-07-15 stsp default:
530 3ce1b845 2019-07-15 stsp usage_init();
531 3ce1b845 2019-07-15 stsp /* NOTREACHED */
532 3ce1b845 2019-07-15 stsp }
533 3ce1b845 2019-07-15 stsp }
534 3ce1b845 2019-07-15 stsp
535 3ce1b845 2019-07-15 stsp argc -= optind;
536 3ce1b845 2019-07-15 stsp argv += optind;
537 3ce1b845 2019-07-15 stsp
538 3ce1b845 2019-07-15 stsp #ifndef PROFILE
539 3ce1b845 2019-07-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec unveil",
540 3ce1b845 2019-07-15 stsp NULL) == -1)
541 3ce1b845 2019-07-15 stsp err(1, "pledge");
542 3ce1b845 2019-07-15 stsp #endif
543 3ce1b845 2019-07-15 stsp if (argc != 1)
544 3ce1b845 2019-07-15 stsp usage_import();
545 3ce1b845 2019-07-15 stsp
546 3ce1b845 2019-07-15 stsp if (got_author == NULL) {
547 3ce1b845 2019-07-15 stsp /* TODO: Look current user up in password database */
548 3ce1b845 2019-07-15 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
549 2c7829a4 2019-06-17 stsp goto done;
550 3ce1b845 2019-07-15 stsp }
551 2c7829a4 2019-06-17 stsp
552 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
553 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
554 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
555 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
556 3ce1b845 2019-07-15 stsp }
557 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
558 3ce1b845 2019-07-15 stsp error = got_repo_open(&repo, repo_path);
559 3ce1b845 2019-07-15 stsp if (error)
560 3ce1b845 2019-07-15 stsp goto done;
561 3ce1b845 2019-07-15 stsp
562 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
563 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
564 3ce1b845 2019-07-15 stsp goto done;
565 3ce1b845 2019-07-15 stsp }
566 3ce1b845 2019-07-15 stsp
567 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
568 3ce1b845 2019-07-15 stsp if (error) {
569 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
570 3ce1b845 2019-07-15 stsp goto done;
571 3ce1b845 2019-07-15 stsp } else {
572 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
573 3ce1b845 2019-07-15 stsp "import target branch already exists");
574 3ce1b845 2019-07-15 stsp goto done;
575 3ce1b845 2019-07-15 stsp }
576 3ce1b845 2019-07-15 stsp
577 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
578 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
579 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
580 3ce1b845 2019-07-15 stsp goto done;
581 3ce1b845 2019-07-15 stsp }
582 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
583 3ce1b845 2019-07-15 stsp
584 3ce1b845 2019-07-15 stsp /*
585 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
586 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
587 3ce1b845 2019-07-15 stsp */
588 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
589 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
590 3ce1b845 2019-07-15 stsp if (error)
591 3ce1b845 2019-07-15 stsp goto done;
592 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
593 3ce1b845 2019-07-15 stsp if (error)
594 3ce1b845 2019-07-15 stsp goto done;
595 3ce1b845 2019-07-15 stsp }
596 3ce1b845 2019-07-15 stsp
597 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
598 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
599 3ce1b845 2019-07-15 stsp
600 3ce1b845 2019-07-15 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL, 0);
601 3ce1b845 2019-07-15 stsp if (error)
602 3ce1b845 2019-07-15 stsp goto done;
603 3ce1b845 2019-07-15 stsp
604 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
605 3ce1b845 2019-07-15 stsp got_author, &ignores, repo, import_progress, NULL);
606 3ce1b845 2019-07-15 stsp if (error)
607 3ce1b845 2019-07-15 stsp goto done;
608 3ce1b845 2019-07-15 stsp
609 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
610 3ce1b845 2019-07-15 stsp if (error)
611 3ce1b845 2019-07-15 stsp goto done;
612 3ce1b845 2019-07-15 stsp
613 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
614 3ce1b845 2019-07-15 stsp if (error)
615 3ce1b845 2019-07-15 stsp goto done;
616 3ce1b845 2019-07-15 stsp
617 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
618 3ce1b845 2019-07-15 stsp if (error)
619 3ce1b845 2019-07-15 stsp goto done;
620 3ce1b845 2019-07-15 stsp
621 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
622 3ce1b845 2019-07-15 stsp if (error) {
623 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
624 3ce1b845 2019-07-15 stsp goto done;
625 3ce1b845 2019-07-15 stsp
626 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
627 3ce1b845 2019-07-15 stsp branch_ref);
628 3ce1b845 2019-07-15 stsp if (error)
629 3ce1b845 2019-07-15 stsp goto done;
630 3ce1b845 2019-07-15 stsp
631 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
632 3ce1b845 2019-07-15 stsp if (error)
633 3ce1b845 2019-07-15 stsp goto done;
634 3ce1b845 2019-07-15 stsp }
635 3ce1b845 2019-07-15 stsp
636 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
637 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
638 2c7829a4 2019-06-17 stsp done:
639 2c7829a4 2019-06-17 stsp free(repo_path);
640 3ce1b845 2019-07-15 stsp free(editor);
641 3ce1b845 2019-07-15 stsp free(refname);
642 3ce1b845 2019-07-15 stsp free(new_commit_id);
643 3ce1b845 2019-07-15 stsp free(id_str);
644 3ce1b845 2019-07-15 stsp if (branch_ref)
645 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
646 3ce1b845 2019-07-15 stsp if (head_ref)
647 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
648 2c7829a4 2019-06-17 stsp return error;
649 0266afb7 2019-01-04 stsp }
650 0266afb7 2019-01-04 stsp
651 4ed7e80c 2018-05-20 stsp __dead static void
652 c09a553d 2018-03-12 stsp usage_checkout(void)
653 c09a553d 2018-03-12 stsp {
654 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
655 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
656 c09a553d 2018-03-12 stsp exit(1);
657 92a684f4 2018-03-12 stsp }
658 92a684f4 2018-03-12 stsp
659 1ee397ad 2019-07-12 stsp static const struct got_error *
660 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
661 92a684f4 2018-03-12 stsp {
662 92a684f4 2018-03-12 stsp char *worktree_path = arg;
663 a484d721 2019-06-10 stsp
664 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
665 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
666 1ee397ad 2019-07-12 stsp return NULL;
667 92a684f4 2018-03-12 stsp
668 92a684f4 2018-03-12 stsp while (path[0] == '/')
669 92a684f4 2018-03-12 stsp path++;
670 92a684f4 2018-03-12 stsp
671 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
672 1ee397ad 2019-07-12 stsp return NULL;
673 99437157 2018-11-11 stsp }
674 99437157 2018-11-11 stsp
675 99437157 2018-11-11 stsp static const struct got_error *
676 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
677 99437157 2018-11-11 stsp {
678 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
679 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
680 99437157 2018-11-11 stsp return NULL;
681 8069f636 2019-01-12 stsp }
682 8069f636 2019-01-12 stsp
683 8069f636 2019-01-12 stsp static const struct got_error *
684 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
685 024e9686 2019-05-14 stsp struct got_object_id *base_commit_id, struct got_repository *repo)
686 8069f636 2019-01-12 stsp {
687 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
688 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
689 8069f636 2019-01-12 stsp
690 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
691 d5bea539 2019-05-13 stsp commit_id, base_commit_id, repo);
692 36a38700 2019-05-10 stsp if (err)
693 36a38700 2019-05-10 stsp return err;
694 8069f636 2019-01-12 stsp
695 d5bea539 2019-05-13 stsp if (yca_id == NULL)
696 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
697 8069f636 2019-01-12 stsp
698 d5bea539 2019-05-13 stsp /*
699 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
700 d5bea539 2019-05-13 stsp * and the work tree's base commit.
701 d5bea539 2019-05-13 stsp *
702 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
703 d5bea539 2019-05-13 stsp *
704 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
705 d5bea539 2019-05-13 stsp * \ /
706 d5bea539 2019-05-13 stsp * C E
707 d5bea539 2019-05-13 stsp * \ /
708 d5bea539 2019-05-13 stsp * B (yca)
709 d5bea539 2019-05-13 stsp * |
710 d5bea539 2019-05-13 stsp * A
711 d5bea539 2019-05-13 stsp *
712 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
713 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
714 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
715 d5bea539 2019-05-13 stsp */
716 d5bea539 2019-05-13 stsp if (got_object_id_cmp(commit_id, yca_id) != 0 &&
717 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
718 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
719 8069f636 2019-01-12 stsp
720 d5bea539 2019-05-13 stsp free(yca_id);
721 d5bea539 2019-05-13 stsp return NULL;
722 c09a553d 2018-03-12 stsp }
723 a367ff0f 2019-05-14 stsp
724 a367ff0f 2019-05-14 stsp static const struct got_error *
725 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
726 a367ff0f 2019-05-14 stsp struct got_reference *head_ref, struct got_repository *repo)
727 a367ff0f 2019-05-14 stsp {
728 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
729 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
730 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
731 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
732 a367ff0f 2019-05-14 stsp
733 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
734 a367ff0f 2019-05-14 stsp if (err)
735 a367ff0f 2019-05-14 stsp goto done;
736 a367ff0f 2019-05-14 stsp
737 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
738 a367ff0f 2019-05-14 stsp if (err)
739 a367ff0f 2019-05-14 stsp goto done;
740 a367ff0f 2019-05-14 stsp
741 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo);
742 a367ff0f 2019-05-14 stsp if (err)
743 a367ff0f 2019-05-14 stsp goto done;
744 a367ff0f 2019-05-14 stsp
745 a367ff0f 2019-05-14 stsp for (;;) {
746 a367ff0f 2019-05-14 stsp struct got_object_id *id;
747 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
748 a367ff0f 2019-05-14 stsp if (err) {
749 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
750 a367ff0f 2019-05-14 stsp err = NULL;
751 a367ff0f 2019-05-14 stsp break;
752 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
753 a367ff0f 2019-05-14 stsp break;
754 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
755 a367ff0f 2019-05-14 stsp repo);
756 a367ff0f 2019-05-14 stsp if (err)
757 a367ff0f 2019-05-14 stsp break;
758 a367ff0f 2019-05-14 stsp }
759 c09a553d 2018-03-12 stsp
760 a367ff0f 2019-05-14 stsp if (id) {
761 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
762 a367ff0f 2019-05-14 stsp is_same_branch = 1;
763 a367ff0f 2019-05-14 stsp break;
764 a367ff0f 2019-05-14 stsp }
765 a367ff0f 2019-05-14 stsp }
766 a367ff0f 2019-05-14 stsp }
767 a367ff0f 2019-05-14 stsp done:
768 a367ff0f 2019-05-14 stsp if (graph)
769 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
770 a367ff0f 2019-05-14 stsp free(head_commit_id);
771 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
772 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
773 a367ff0f 2019-05-14 stsp return err;
774 a367ff0f 2019-05-14 stsp }
775 8069f636 2019-01-12 stsp
776 4ed7e80c 2018-05-20 stsp static const struct got_error *
777 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
778 c09a553d 2018-03-12 stsp {
779 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
780 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
781 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
782 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
783 c09a553d 2018-03-12 stsp char *repo_path = NULL;
784 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
785 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
786 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
787 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
788 72151b04 2019-05-11 stsp int ch, same_path_prefix;
789 c09a553d 2018-03-12 stsp
790 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
791 0bb8a95e 2018-03-12 stsp switch (ch) {
792 08573d5b 2019-05-14 stsp case 'b':
793 08573d5b 2019-05-14 stsp branch_name = optarg;
794 08573d5b 2019-05-14 stsp break;
795 8069f636 2019-01-12 stsp case 'c':
796 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
797 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
798 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
799 8069f636 2019-01-12 stsp break;
800 0bb8a95e 2018-03-12 stsp case 'p':
801 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
802 0bb8a95e 2018-03-12 stsp break;
803 0bb8a95e 2018-03-12 stsp default:
804 2deda0b9 2019-03-07 stsp usage_checkout();
805 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
806 0bb8a95e 2018-03-12 stsp }
807 0bb8a95e 2018-03-12 stsp }
808 0bb8a95e 2018-03-12 stsp
809 0bb8a95e 2018-03-12 stsp argc -= optind;
810 0bb8a95e 2018-03-12 stsp argv += optind;
811 0bb8a95e 2018-03-12 stsp
812 6715a751 2018-03-16 stsp #ifndef PROFILE
813 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
814 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
815 c09a553d 2018-03-12 stsp err(1, "pledge");
816 6715a751 2018-03-16 stsp #endif
817 0bb8a95e 2018-03-12 stsp if (argc == 1) {
818 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
819 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
820 76089277 2018-04-01 stsp if (repo_path == NULL)
821 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
822 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
823 76089277 2018-04-01 stsp if (cwd == NULL) {
824 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
825 76089277 2018-04-01 stsp goto done;
826 76089277 2018-04-01 stsp }
827 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
828 230a42bd 2019-05-11 jcs base = basename(path_prefix);
829 230a42bd 2019-05-11 jcs if (base == NULL) {
830 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
831 230a42bd 2019-05-11 jcs path_prefix);
832 230a42bd 2019-05-11 jcs goto done;
833 230a42bd 2019-05-11 jcs }
834 230a42bd 2019-05-11 jcs } else {
835 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
836 230a42bd 2019-05-11 jcs if (base == NULL) {
837 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
838 230a42bd 2019-05-11 jcs repo_path);
839 230a42bd 2019-05-11 jcs goto done;
840 230a42bd 2019-05-11 jcs }
841 76089277 2018-04-01 stsp }
842 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
843 c09a553d 2018-03-12 stsp if (dotgit)
844 c09a553d 2018-03-12 stsp *dotgit = '\0';
845 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
846 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
847 c09a553d 2018-03-12 stsp free(cwd);
848 76089277 2018-04-01 stsp goto done;
849 c09a553d 2018-03-12 stsp }
850 c09a553d 2018-03-12 stsp free(cwd);
851 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
852 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
853 76089277 2018-04-01 stsp if (repo_path == NULL) {
854 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
855 76089277 2018-04-01 stsp goto done;
856 76089277 2018-04-01 stsp }
857 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
858 76089277 2018-04-01 stsp if (worktree_path == NULL) {
859 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
860 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
861 b4b3a7dd 2019-07-22 stsp argv[1]);
862 b4b3a7dd 2019-07-22 stsp goto done;
863 b4b3a7dd 2019-07-22 stsp }
864 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
865 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
866 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
867 b4b3a7dd 2019-07-22 stsp goto done;
868 b4b3a7dd 2019-07-22 stsp }
869 76089277 2018-04-01 stsp }
870 c09a553d 2018-03-12 stsp } else
871 c09a553d 2018-03-12 stsp usage_checkout();
872 c09a553d 2018-03-12 stsp
873 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
874 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
875 13bfb272 2019-05-10 jcs
876 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
877 c09a553d 2018-03-12 stsp if (error != NULL)
878 c02c541e 2019-03-29 stsp goto done;
879 c02c541e 2019-03-29 stsp
880 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path, 1);
881 c02c541e 2019-03-29 stsp if (error)
882 c09a553d 2018-03-12 stsp goto done;
883 8069f636 2019-01-12 stsp
884 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
885 c09a553d 2018-03-12 stsp if (error != NULL)
886 c09a553d 2018-03-12 stsp goto done;
887 c09a553d 2018-03-12 stsp
888 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
889 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
890 c09a553d 2018-03-12 stsp goto done;
891 c09a553d 2018-03-12 stsp
892 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
893 c09a553d 2018-03-12 stsp if (error != NULL)
894 c09a553d 2018-03-12 stsp goto done;
895 c09a553d 2018-03-12 stsp
896 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
897 e5dc7198 2018-12-29 stsp path_prefix);
898 e5dc7198 2018-12-29 stsp if (error != NULL)
899 e5dc7198 2018-12-29 stsp goto done;
900 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
901 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
902 49520a32 2018-12-29 stsp goto done;
903 49520a32 2018-12-29 stsp }
904 49520a32 2018-12-29 stsp
905 8069f636 2019-01-12 stsp if (commit_id_str) {
906 8069f636 2019-01-12 stsp struct got_object_id *commit_id;
907 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
908 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
909 8069f636 2019-01-12 stsp if (error != NULL)
910 8069f636 2019-01-12 stsp goto done;
911 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
912 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
913 8069f636 2019-01-12 stsp if (error != NULL) {
914 8069f636 2019-01-12 stsp free(commit_id);
915 8069f636 2019-01-12 stsp goto done;
916 8069f636 2019-01-12 stsp }
917 45d344f6 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
918 45d344f6 2019-05-14 stsp if (error)
919 45d344f6 2019-05-14 stsp goto done;
920 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
921 8069f636 2019-01-12 stsp commit_id);
922 8069f636 2019-01-12 stsp free(commit_id);
923 8069f636 2019-01-12 stsp if (error)
924 8069f636 2019-01-12 stsp goto done;
925 8069f636 2019-01-12 stsp }
926 8069f636 2019-01-12 stsp
927 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, "", repo,
928 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
929 c09a553d 2018-03-12 stsp if (error != NULL)
930 c09a553d 2018-03-12 stsp goto done;
931 c09a553d 2018-03-12 stsp
932 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
933 c09a553d 2018-03-12 stsp
934 c09a553d 2018-03-12 stsp done:
935 8069f636 2019-01-12 stsp free(commit_id_str);
936 76089277 2018-04-01 stsp free(repo_path);
937 507dc3bb 2018-12-29 stsp free(worktree_path);
938 507dc3bb 2018-12-29 stsp return error;
939 507dc3bb 2018-12-29 stsp }
940 507dc3bb 2018-12-29 stsp
941 507dc3bb 2018-12-29 stsp __dead static void
942 507dc3bb 2018-12-29 stsp usage_update(void)
943 507dc3bb 2018-12-29 stsp {
944 024e9686 2019-05-14 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path]\n",
945 507dc3bb 2018-12-29 stsp getprogname());
946 507dc3bb 2018-12-29 stsp exit(1);
947 507dc3bb 2018-12-29 stsp }
948 507dc3bb 2018-12-29 stsp
949 1ee397ad 2019-07-12 stsp static const struct got_error *
950 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
951 507dc3bb 2018-12-29 stsp {
952 784955db 2019-01-12 stsp int *did_something = arg;
953 784955db 2019-01-12 stsp
954 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
955 1ee397ad 2019-07-12 stsp return NULL;
956 507dc3bb 2018-12-29 stsp
957 1545c615 2019-02-10 stsp *did_something = 1;
958 a484d721 2019-06-10 stsp
959 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
960 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
961 1ee397ad 2019-07-12 stsp return NULL;
962 a484d721 2019-06-10 stsp
963 507dc3bb 2018-12-29 stsp while (path[0] == '/')
964 507dc3bb 2018-12-29 stsp path++;
965 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
966 1ee397ad 2019-07-12 stsp return NULL;
967 be7061eb 2018-12-30 stsp }
968 be7061eb 2018-12-30 stsp
969 be7061eb 2018-12-30 stsp static const struct got_error *
970 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
971 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
972 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
973 a1fb16d8 2019-05-24 stsp {
974 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
975 a1fb16d8 2019-05-24 stsp char *base_id_str;
976 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
977 a1fb16d8 2019-05-24 stsp
978 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
979 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
980 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
981 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
982 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
983 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
984 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
985 a1fb16d8 2019-05-24 stsp }
986 a1fb16d8 2019-05-24 stsp
987 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
988 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree), repo);
989 a1fb16d8 2019-05-24 stsp if (err) {
990 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
991 a1fb16d8 2019-05-24 stsp return err;
992 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
993 a1fb16d8 2019-05-24 stsp }
994 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
995 a1fb16d8 2019-05-24 stsp return NULL;
996 a1fb16d8 2019-05-24 stsp
997 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
998 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
999 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1000 a1fb16d8 2019-05-24 stsp if (err)
1001 a1fb16d8 2019-05-24 stsp return err;
1002 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1003 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1004 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1005 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1006 a1fb16d8 2019-05-24 stsp return NULL;
1007 a1fb16d8 2019-05-24 stsp }
1008 a1fb16d8 2019-05-24 stsp
1009 a1fb16d8 2019-05-24 stsp static const struct got_error *
1010 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1011 507dc3bb 2018-12-29 stsp {
1012 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1013 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1014 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1015 c4cdcb68 2019-04-03 stsp char *worktree_path = NULL, *path = NULL;
1016 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1017 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1018 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1019 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1020 7d5807f4 2019-07-11 stsp int ch, did_something = 0, rebase_in_progress;
1021 507dc3bb 2018-12-29 stsp
1022 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1023 507dc3bb 2018-12-29 stsp switch (ch) {
1024 024e9686 2019-05-14 stsp case 'b':
1025 024e9686 2019-05-14 stsp branch_name = optarg;
1026 024e9686 2019-05-14 stsp break;
1027 507dc3bb 2018-12-29 stsp case 'c':
1028 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1029 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1030 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1031 507dc3bb 2018-12-29 stsp break;
1032 507dc3bb 2018-12-29 stsp default:
1033 2deda0b9 2019-03-07 stsp usage_update();
1034 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1035 507dc3bb 2018-12-29 stsp }
1036 507dc3bb 2018-12-29 stsp }
1037 507dc3bb 2018-12-29 stsp
1038 507dc3bb 2018-12-29 stsp argc -= optind;
1039 507dc3bb 2018-12-29 stsp argv += optind;
1040 507dc3bb 2018-12-29 stsp
1041 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1042 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1043 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1044 507dc3bb 2018-12-29 stsp err(1, "pledge");
1045 507dc3bb 2018-12-29 stsp #endif
1046 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1047 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1048 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1049 c4cdcb68 2019-04-03 stsp goto done;
1050 c4cdcb68 2019-04-03 stsp }
1051 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1052 7d5807f4 2019-07-11 stsp if (error)
1053 7d5807f4 2019-07-11 stsp goto done;
1054 7d5807f4 2019-07-11 stsp
1055 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
1056 c4cdcb68 2019-04-03 stsp if (error)
1057 c4cdcb68 2019-04-03 stsp goto done;
1058 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
1059 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
1060 7d5807f4 2019-07-11 stsp goto done;
1061 7d5807f4 2019-07-11 stsp }
1062 c4cdcb68 2019-04-03 stsp
1063 507dc3bb 2018-12-29 stsp if (argc == 0) {
1064 c4cdcb68 2019-04-03 stsp path = strdup("");
1065 c4cdcb68 2019-04-03 stsp if (path == NULL) {
1066 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1067 507dc3bb 2018-12-29 stsp goto done;
1068 507dc3bb 2018-12-29 stsp }
1069 507dc3bb 2018-12-29 stsp } else if (argc == 1) {
1070 c4cdcb68 2019-04-03 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
1071 c4cdcb68 2019-04-03 stsp if (error)
1072 507dc3bb 2018-12-29 stsp goto done;
1073 507dc3bb 2018-12-29 stsp } else
1074 507dc3bb 2018-12-29 stsp usage_update();
1075 507dc3bb 2018-12-29 stsp
1076 507dc3bb 2018-12-29 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
1077 507dc3bb 2018-12-29 stsp if (error != NULL)
1078 507dc3bb 2018-12-29 stsp goto done;
1079 507dc3bb 2018-12-29 stsp
1080 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1081 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
1082 0266afb7 2019-01-04 stsp if (error)
1083 0266afb7 2019-01-04 stsp goto done;
1084 0266afb7 2019-01-04 stsp
1085 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1086 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1087 024e9686 2019-05-14 stsp if (error != NULL)
1088 024e9686 2019-05-14 stsp goto done;
1089 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1090 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1091 9c4b8182 2019-01-02 stsp if (error != NULL)
1092 9c4b8182 2019-01-02 stsp goto done;
1093 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1094 507dc3bb 2018-12-29 stsp if (error != NULL)
1095 507dc3bb 2018-12-29 stsp goto done;
1096 507dc3bb 2018-12-29 stsp } else {
1097 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
1098 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
1099 507dc3bb 2018-12-29 stsp if (error != NULL)
1100 a297e751 2019-07-11 stsp goto done;
1101 a297e751 2019-07-11 stsp free(commit_id_str);
1102 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1103 a297e751 2019-07-11 stsp if (error)
1104 507dc3bb 2018-12-29 stsp goto done;
1105 507dc3bb 2018-12-29 stsp }
1106 35c965b2 2018-12-29 stsp
1107 a1fb16d8 2019-05-24 stsp if (branch_name) {
1108 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1109 024e9686 2019-05-14 stsp if (strlen(path) != 0) {
1110 a1fb16d8 2019-05-24 stsp fprintf(stderr, "%s: switching between branches "
1111 a1fb16d8 2019-05-24 stsp "requires that the entire work tree "
1112 024e9686 2019-05-14 stsp "gets updated, not just '%s'\n",
1113 024e9686 2019-05-14 stsp getprogname(), path);
1114 024e9686 2019-05-14 stsp error = got_error(GOT_ERR_BAD_PATH);
1115 024e9686 2019-05-14 stsp goto done;
1116 024e9686 2019-05-14 stsp }
1117 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1118 024e9686 2019-05-14 stsp if (error)
1119 024e9686 2019-05-14 stsp goto done;
1120 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id, head_commit_id, repo);
1121 a367ff0f 2019-05-14 stsp free(head_commit_id);
1122 024e9686 2019-05-14 stsp if (error != NULL)
1123 024e9686 2019-05-14 stsp goto done;
1124 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1125 a367ff0f 2019-05-14 stsp if (error)
1126 a367ff0f 2019-05-14 stsp goto done;
1127 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1128 024e9686 2019-05-14 stsp if (error)
1129 024e9686 2019-05-14 stsp goto done;
1130 024e9686 2019-05-14 stsp } else {
1131 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1132 024e9686 2019-05-14 stsp got_worktree_get_base_commit_id(worktree), repo);
1133 a367ff0f 2019-05-14 stsp if (error != NULL) {
1134 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1135 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1136 024e9686 2019-05-14 stsp goto done;
1137 a367ff0f 2019-05-14 stsp }
1138 a367ff0f 2019-05-14 stsp error = check_same_branch(commit_id, head_ref, repo);
1139 a367ff0f 2019-05-14 stsp if (error)
1140 a367ff0f 2019-05-14 stsp goto done;
1141 024e9686 2019-05-14 stsp }
1142 507dc3bb 2018-12-29 stsp
1143 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1144 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1145 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1146 507dc3bb 2018-12-29 stsp commit_id);
1147 507dc3bb 2018-12-29 stsp if (error)
1148 507dc3bb 2018-12-29 stsp goto done;
1149 507dc3bb 2018-12-29 stsp }
1150 507dc3bb 2018-12-29 stsp
1151 c4cdcb68 2019-04-03 stsp error = got_worktree_checkout_files(worktree, path, repo,
1152 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1153 507dc3bb 2018-12-29 stsp if (error != NULL)
1154 507dc3bb 2018-12-29 stsp goto done;
1155 9c4b8182 2019-01-02 stsp
1156 784955db 2019-01-12 stsp if (did_something)
1157 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1158 784955db 2019-01-12 stsp else
1159 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1160 507dc3bb 2018-12-29 stsp done:
1161 c09a553d 2018-03-12 stsp free(worktree_path);
1162 c4cdcb68 2019-04-03 stsp free(path);
1163 507dc3bb 2018-12-29 stsp free(commit_id);
1164 9c4b8182 2019-01-02 stsp free(commit_id_str);
1165 c09a553d 2018-03-12 stsp return error;
1166 c09a553d 2018-03-12 stsp }
1167 c09a553d 2018-03-12 stsp
1168 f42b1b34 2018-03-12 stsp static const struct got_error *
1169 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1170 c0cc5c62 2018-10-18 stsp int diff_context, struct got_repository *repo)
1171 5c860e29 2018-03-12 stsp {
1172 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1173 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
1174 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1175 0f2b3dca 2018-12-22 stsp char *id_str1 = NULL, *id_str2;
1176 aaa13589 2019-06-01 stsp struct got_diff_blob_output_unidiff_arg arg;
1177 79109fed 2018-03-27 stsp
1178 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree2, repo,
1179 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(commit));
1180 79109fed 2018-03-27 stsp if (err)
1181 79109fed 2018-03-27 stsp return err;
1182 79109fed 2018-03-27 stsp
1183 45d799e2 2018-12-23 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1184 79f35eb3 2018-06-11 stsp if (qid != NULL) {
1185 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
1186 79109fed 2018-03-27 stsp
1187 117e771c 2018-07-23 stsp err = got_object_open_as_commit(&pcommit, repo, qid->id);
1188 79109fed 2018-03-27 stsp if (err)
1189 79109fed 2018-03-27 stsp return err;
1190 79109fed 2018-03-27 stsp
1191 45d799e2 2018-12-23 stsp err = got_object_open_as_tree(&tree1, repo,
1192 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(pcommit));
1193 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
1194 0f2b3dca 2018-12-22 stsp if (err)
1195 0f2b3dca 2018-12-22 stsp return err;
1196 0f2b3dca 2018-12-22 stsp
1197 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str1, qid->id);
1198 79109fed 2018-03-27 stsp if (err)
1199 79109fed 2018-03-27 stsp return err;
1200 79109fed 2018-03-27 stsp }
1201 79109fed 2018-03-27 stsp
1202 0f2b3dca 2018-12-22 stsp err = got_object_id_str(&id_str2, id);
1203 0f2b3dca 2018-12-22 stsp if (err)
1204 0f2b3dca 2018-12-22 stsp goto done;
1205 0f2b3dca 2018-12-22 stsp
1206 56765ebb 2018-12-23 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1207 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1208 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1209 aaa13589 2019-06-01 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
1210 aaa13589 2019-06-01 stsp got_diff_blob_output_unidiff, &arg);
1211 0f2b3dca 2018-12-22 stsp done:
1212 79109fed 2018-03-27 stsp if (tree1)
1213 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1214 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
1215 0f2b3dca 2018-12-22 stsp free(id_str1);
1216 0f2b3dca 2018-12-22 stsp free(id_str2);
1217 79109fed 2018-03-27 stsp return err;
1218 79109fed 2018-03-27 stsp }
1219 79109fed 2018-03-27 stsp
1220 4bb494d5 2018-06-16 stsp static char *
1221 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1222 6c281f94 2018-06-11 stsp {
1223 6c281f94 2018-06-11 stsp char *p, *s = ctime_r(time, datebuf);
1224 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1225 6c281f94 2018-06-11 stsp if (p)
1226 6c281f94 2018-06-11 stsp *p = '\0';
1227 6c281f94 2018-06-11 stsp return s;
1228 6c281f94 2018-06-11 stsp }
1229 6c281f94 2018-06-11 stsp
1230 79109fed 2018-03-27 stsp static const struct got_error *
1231 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1232 199a4027 2019-02-02 stsp struct got_repository *repo, int show_patch, int diff_context,
1233 199a4027 2019-02-02 stsp struct got_reflist_head *refs)
1234 79109fed 2018-03-27 stsp {
1235 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1236 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1237 6c281f94 2018-06-11 stsp char datebuf[26];
1238 45d799e2 2018-12-23 stsp time_t committer_time;
1239 45d799e2 2018-12-23 stsp const char *author, *committer;
1240 199a4027 2019-02-02 stsp char *refs_str = NULL;
1241 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1242 5c860e29 2018-03-12 stsp
1243 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1244 199a4027 2019-02-02 stsp char *s;
1245 199a4027 2019-02-02 stsp const char *name;
1246 199a4027 2019-02-02 stsp if (got_object_id_cmp(re->id, id) != 0)
1247 199a4027 2019-02-02 stsp continue;
1248 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1249 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1250 d9498b20 2019-02-05 stsp continue;
1251 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1252 199a4027 2019-02-02 stsp name += 5;
1253 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1254 7143d404 2019-03-12 stsp continue;
1255 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1256 e34f9ed6 2019-02-02 stsp name += 6;
1257 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1258 141c2bff 2019-02-04 stsp name += 8;
1259 199a4027 2019-02-02 stsp s = refs_str;
1260 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1261 199a4027 2019-02-02 stsp name) == -1) {
1262 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1263 199a4027 2019-02-02 stsp free(s);
1264 199a4027 2019-02-02 stsp break;
1265 199a4027 2019-02-02 stsp }
1266 199a4027 2019-02-02 stsp free(s);
1267 199a4027 2019-02-02 stsp }
1268 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1269 8bf5b3c9 2018-03-17 stsp if (err)
1270 8bf5b3c9 2018-03-17 stsp return err;
1271 788c352e 2018-06-16 stsp
1272 33d869be 2018-12-22 stsp printf("-----------------------------------------------\n");
1273 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1274 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1275 832c249c 2018-06-10 stsp free(id_str);
1276 d3d493d7 2019-02-21 stsp id_str = NULL;
1277 d3d493d7 2019-02-21 stsp free(refs_str);
1278 d3d493d7 2019-02-21 stsp refs_str = NULL;
1279 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1280 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1281 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1282 dab5fe87 2018-09-14 stsp printf("date: %s UTC\n", datestr);
1283 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1284 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1285 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1286 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1287 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1288 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1289 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1290 3fe1abad 2018-06-10 stsp int n = 1;
1291 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1292 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1293 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1294 a0603db2 2018-06-10 stsp if (err)
1295 a0603db2 2018-06-10 stsp return err;
1296 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1297 a0603db2 2018-06-10 stsp free(id_str);
1298 a0603db2 2018-06-10 stsp }
1299 a0603db2 2018-06-10 stsp }
1300 832c249c 2018-06-10 stsp
1301 45d799e2 2018-12-23 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
1302 621015ac 2018-07-23 stsp if (logmsg0 == NULL)
1303 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1304 8bf5b3c9 2018-03-17 stsp
1305 621015ac 2018-07-23 stsp logmsg = logmsg0;
1306 832c249c 2018-06-10 stsp do {
1307 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1308 832c249c 2018-06-10 stsp if (line)
1309 832c249c 2018-06-10 stsp printf(" %s\n", line);
1310 832c249c 2018-06-10 stsp } while (line);
1311 621015ac 2018-07-23 stsp free(logmsg0);
1312 832c249c 2018-06-10 stsp
1313 971751ac 2018-03-27 stsp if (show_patch) {
1314 c0cc5c62 2018-10-18 stsp err = print_patch(commit, id, diff_context, repo);
1315 971751ac 2018-03-27 stsp if (err == 0)
1316 971751ac 2018-03-27 stsp printf("\n");
1317 971751ac 2018-03-27 stsp }
1318 07862c20 2018-09-15 stsp
1319 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1320 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1321 07862c20 2018-09-15 stsp return err;
1322 f42b1b34 2018-03-12 stsp }
1323 5c860e29 2018-03-12 stsp
1324 f42b1b34 2018-03-12 stsp static const struct got_error *
1325 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1326 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1327 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1328 f42b1b34 2018-03-12 stsp {
1329 f42b1b34 2018-03-12 stsp const struct got_error *err;
1330 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1331 372ccdbb 2018-06-10 stsp
1332 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1333 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1334 f42b1b34 2018-03-12 stsp if (err)
1335 f42b1b34 2018-03-12 stsp return err;
1336 31cedeaf 2018-09-15 stsp err = got_commit_graph_iter_start(graph, root_id, repo);
1337 372ccdbb 2018-06-10 stsp if (err)
1338 fcc85cad 2018-07-22 stsp goto done;
1339 656b1f76 2019-05-11 jcs for (;;) {
1340 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1341 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1342 8bf5b3c9 2018-03-17 stsp
1343 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1344 84453469 2018-11-11 stsp break;
1345 84453469 2018-11-11 stsp
1346 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1347 372ccdbb 2018-06-10 stsp if (err) {
1348 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1349 9ba79e04 2018-06-11 stsp err = NULL;
1350 9ba79e04 2018-06-11 stsp break;
1351 9ba79e04 2018-06-11 stsp }
1352 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1353 372ccdbb 2018-06-10 stsp break;
1354 31cedeaf 2018-09-15 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
1355 372ccdbb 2018-06-10 stsp if (err)
1356 372ccdbb 2018-06-10 stsp break;
1357 372ccdbb 2018-06-10 stsp else
1358 372ccdbb 2018-06-10 stsp continue;
1359 7e665116 2018-04-02 stsp }
1360 b43fbaa0 2018-06-11 stsp if (id == NULL)
1361 7e665116 2018-04-02 stsp break;
1362 b43fbaa0 2018-06-11 stsp
1363 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1364 b43fbaa0 2018-06-11 stsp if (err)
1365 fcc85cad 2018-07-22 stsp break;
1366 199a4027 2019-02-02 stsp err = print_commit(commit, id, repo, show_patch, diff_context,
1367 199a4027 2019-02-02 stsp refs);
1368 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1369 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1370 7e665116 2018-04-02 stsp break;
1371 31cedeaf 2018-09-15 stsp }
1372 fcc85cad 2018-07-22 stsp done:
1373 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1374 f42b1b34 2018-03-12 stsp return err;
1375 f42b1b34 2018-03-12 stsp }
1376 5c860e29 2018-03-12 stsp
1377 4ed7e80c 2018-05-20 stsp __dead static void
1378 6f3d1eb0 2018-03-12 stsp usage_log(void)
1379 6f3d1eb0 2018-03-12 stsp {
1380 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1381 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1382 6f3d1eb0 2018-03-12 stsp exit(1);
1383 6f3d1eb0 2018-03-12 stsp }
1384 6f3d1eb0 2018-03-12 stsp
1385 4ed7e80c 2018-05-20 stsp static const struct got_error *
1386 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1387 f42b1b34 2018-03-12 stsp {
1388 f42b1b34 2018-03-12 stsp const struct got_error *error;
1389 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1390 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1391 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1392 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1393 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1394 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1395 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1396 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1397 64a96a6d 2018-04-01 stsp const char *errstr;
1398 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1399 1b3893a2 2019-03-18 stsp
1400 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1401 5c860e29 2018-03-12 stsp
1402 6715a751 2018-03-16 stsp #ifndef PROFILE
1403 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1404 6098196c 2019-01-04 stsp NULL)
1405 ad242220 2018-09-08 stsp == -1)
1406 f42b1b34 2018-03-12 stsp err(1, "pledge");
1407 6715a751 2018-03-16 stsp #endif
1408 79109fed 2018-03-27 stsp
1409 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1410 79109fed 2018-03-27 stsp switch (ch) {
1411 79109fed 2018-03-27 stsp case 'p':
1412 79109fed 2018-03-27 stsp show_patch = 1;
1413 d142fc45 2018-04-01 stsp break;
1414 d142fc45 2018-04-01 stsp case 'c':
1415 d142fc45 2018-04-01 stsp start_commit = optarg;
1416 64a96a6d 2018-04-01 stsp break;
1417 c0cc5c62 2018-10-18 stsp case 'C':
1418 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1419 4a8520aa 2018-10-18 stsp &errstr);
1420 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1421 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1422 c0cc5c62 2018-10-18 stsp break;
1423 64a96a6d 2018-04-01 stsp case 'l':
1424 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
1425 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1426 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1427 cc54c501 2019-07-15 stsp break;
1428 cc54c501 2019-07-15 stsp case 'f':
1429 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1430 a0603db2 2018-06-10 stsp break;
1431 04ca23f4 2018-07-16 stsp case 'r':
1432 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1433 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1434 04ca23f4 2018-07-16 stsp err(1, "-r option");
1435 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1436 04ca23f4 2018-07-16 stsp break;
1437 79109fed 2018-03-27 stsp default:
1438 2deda0b9 2019-03-07 stsp usage_log();
1439 79109fed 2018-03-27 stsp /* NOTREACHED */
1440 79109fed 2018-03-27 stsp }
1441 79109fed 2018-03-27 stsp }
1442 79109fed 2018-03-27 stsp
1443 79109fed 2018-03-27 stsp argc -= optind;
1444 79109fed 2018-03-27 stsp argv += optind;
1445 f42b1b34 2018-03-12 stsp
1446 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1447 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1448 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1449 04ca23f4 2018-07-16 stsp goto done;
1450 04ca23f4 2018-07-16 stsp }
1451 cffc0aa4 2019-02-05 stsp
1452 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1453 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1454 cffc0aa4 2019-02-05 stsp goto done;
1455 cffc0aa4 2019-02-05 stsp error = NULL;
1456 cffc0aa4 2019-02-05 stsp
1457 e7301579 2019-03-18 stsp if (argc == 0) {
1458 cbd1af7a 2019-03-18 stsp path = strdup("");
1459 e7301579 2019-03-18 stsp if (path == NULL) {
1460 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1461 cbd1af7a 2019-03-18 stsp goto done;
1462 e7301579 2019-03-18 stsp }
1463 e7301579 2019-03-18 stsp } else if (argc == 1) {
1464 e7301579 2019-03-18 stsp if (worktree) {
1465 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1466 e7301579 2019-03-18 stsp argv[0]);
1467 e7301579 2019-03-18 stsp if (error)
1468 e7301579 2019-03-18 stsp goto done;
1469 e7301579 2019-03-18 stsp } else {
1470 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1471 e7301579 2019-03-18 stsp if (path == NULL) {
1472 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1473 e7301579 2019-03-18 stsp goto done;
1474 e7301579 2019-03-18 stsp }
1475 e7301579 2019-03-18 stsp }
1476 cbd1af7a 2019-03-18 stsp } else
1477 cbd1af7a 2019-03-18 stsp usage_log();
1478 cbd1af7a 2019-03-18 stsp
1479 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1480 5486daa2 2019-05-11 stsp repo_path = worktree ?
1481 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1482 5486daa2 2019-05-11 stsp }
1483 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1484 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1485 cffc0aa4 2019-02-05 stsp goto done;
1486 04ca23f4 2018-07-16 stsp }
1487 6098196c 2019-01-04 stsp
1488 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
1489 d7d4f210 2018-03-12 stsp if (error != NULL)
1490 04ca23f4 2018-07-16 stsp goto done;
1491 f42b1b34 2018-03-12 stsp
1492 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1493 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1494 c02c541e 2019-03-29 stsp if (error)
1495 c02c541e 2019-03-29 stsp goto done;
1496 c02c541e 2019-03-29 stsp
1497 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1498 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1499 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1500 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1501 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1502 3235492e 2018-04-01 stsp if (error != NULL)
1503 3235492e 2018-04-01 stsp return error;
1504 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1505 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1506 3235492e 2018-04-01 stsp if (error != NULL)
1507 3235492e 2018-04-01 stsp return error;
1508 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1509 3235492e 2018-04-01 stsp } else {
1510 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1511 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1512 3235492e 2018-04-01 stsp if (error == NULL) {
1513 1caad61b 2019-02-01 stsp int obj_type;
1514 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1515 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1516 d1f2edc9 2018-06-13 stsp if (error != NULL)
1517 1caad61b 2019-02-01 stsp goto done;
1518 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1519 1caad61b 2019-02-01 stsp if (error != NULL)
1520 1caad61b 2019-02-01 stsp goto done;
1521 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1522 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1523 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1524 1caad61b 2019-02-01 stsp if (error != NULL)
1525 1caad61b 2019-02-01 stsp goto done;
1526 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1527 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1528 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1529 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1530 1caad61b 2019-02-01 stsp goto done;
1531 1caad61b 2019-02-01 stsp }
1532 1caad61b 2019-02-01 stsp free(id);
1533 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1534 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1535 1caad61b 2019-02-01 stsp if (id == NULL)
1536 638f9024 2019-05-13 stsp error = got_error_from_errno(
1537 230a42bd 2019-05-11 jcs "got_object_id_dup");
1538 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1539 1caad61b 2019-02-01 stsp if (error)
1540 1caad61b 2019-02-01 stsp goto done;
1541 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1542 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1543 1caad61b 2019-02-01 stsp goto done;
1544 1caad61b 2019-02-01 stsp }
1545 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1546 d1f2edc9 2018-06-13 stsp if (error != NULL)
1547 1caad61b 2019-02-01 stsp goto done;
1548 d1f2edc9 2018-06-13 stsp }
1549 15a94983 2018-12-23 stsp if (commit == NULL) {
1550 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1551 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1552 d1f2edc9 2018-06-13 stsp if (error != NULL)
1553 d1f2edc9 2018-06-13 stsp return error;
1554 3235492e 2018-04-01 stsp }
1555 3235492e 2018-04-01 stsp }
1556 d7d4f210 2018-03-12 stsp if (error != NULL)
1557 04ca23f4 2018-07-16 stsp goto done;
1558 04ca23f4 2018-07-16 stsp
1559 23721109 2018-10-22 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1560 04ca23f4 2018-07-16 stsp if (error != NULL)
1561 04ca23f4 2018-07-16 stsp goto done;
1562 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1563 04ca23f4 2018-07-16 stsp free(path);
1564 04ca23f4 2018-07-16 stsp path = in_repo_path;
1565 04ca23f4 2018-07-16 stsp }
1566 199a4027 2019-02-02 stsp
1567 199a4027 2019-02-02 stsp error = got_ref_list(&refs, repo);
1568 199a4027 2019-02-02 stsp if (error)
1569 199a4027 2019-02-02 stsp goto done;
1570 04ca23f4 2018-07-16 stsp
1571 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1572 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1573 04ca23f4 2018-07-16 stsp done:
1574 04ca23f4 2018-07-16 stsp free(path);
1575 04ca23f4 2018-07-16 stsp free(repo_path);
1576 04ca23f4 2018-07-16 stsp free(cwd);
1577 f42b1b34 2018-03-12 stsp free(id);
1578 cffc0aa4 2019-02-05 stsp if (worktree)
1579 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1580 ad242220 2018-09-08 stsp if (repo) {
1581 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1582 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1583 ad242220 2018-09-08 stsp if (error == NULL)
1584 ad242220 2018-09-08 stsp error = repo_error;
1585 ad242220 2018-09-08 stsp }
1586 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1587 8bf5b3c9 2018-03-17 stsp return error;
1588 5c860e29 2018-03-12 stsp }
1589 5c860e29 2018-03-12 stsp
1590 4ed7e80c 2018-05-20 stsp __dead static void
1591 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1592 3f8b7d6a 2018-04-01 stsp {
1593 b72f483a 2019-02-05 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] "
1594 927df6b7 2019-02-10 stsp "[object1 object2 | path]\n", getprogname());
1595 3f8b7d6a 2018-04-01 stsp exit(1);
1596 b00d56cd 2018-04-01 stsp }
1597 b00d56cd 2018-04-01 stsp
1598 b72f483a 2019-02-05 stsp struct print_diff_arg {
1599 b72f483a 2019-02-05 stsp struct got_repository *repo;
1600 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1601 b72f483a 2019-02-05 stsp int diff_context;
1602 3fc0c068 2019-02-10 stsp const char *id_str;
1603 3fc0c068 2019-02-10 stsp int header_shown;
1604 b72f483a 2019-02-05 stsp };
1605 b72f483a 2019-02-05 stsp
1606 4ed7e80c 2018-05-20 stsp static const struct got_error *
1607 b72f483a 2019-02-05 stsp print_diff(void *arg, unsigned char status, const char *path,
1608 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
1609 b72f483a 2019-02-05 stsp {
1610 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1611 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1612 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1613 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1614 b72f483a 2019-02-05 stsp char *abspath = NULL;
1615 b72f483a 2019-02-05 stsp struct stat sb;
1616 b72f483a 2019-02-05 stsp
1617 2ec1f75b 2019-03-26 stsp if (status != GOT_STATUS_MODIFY && status != GOT_STATUS_ADD &&
1618 7154f6ce 2019-03-27 stsp status != GOT_STATUS_DELETE && status != GOT_STATUS_CONFLICT)
1619 b72f483a 2019-02-05 stsp return NULL;
1620 3fc0c068 2019-02-10 stsp
1621 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1622 3fc0c068 2019-02-10 stsp printf("diff %s %s\n", a->id_str,
1623 3fc0c068 2019-02-10 stsp got_worktree_get_root_path(a->worktree));
1624 3fc0c068 2019-02-10 stsp a->header_shown = 1;
1625 3fc0c068 2019-02-10 stsp }
1626 b72f483a 2019-02-05 stsp
1627 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_ADD) {
1628 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
1629 d00136be 2019-03-26 stsp if (err)
1630 d00136be 2019-03-26 stsp goto done;
1631 b72f483a 2019-02-05 stsp
1632 d00136be 2019-03-26 stsp }
1633 d00136be 2019-03-26 stsp
1634 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
1635 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
1636 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
1637 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1638 2ec1f75b 2019-03-26 stsp goto done;
1639 2ec1f75b 2019-03-26 stsp }
1640 b72f483a 2019-02-05 stsp
1641 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
1642 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
1643 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
1644 2ec1f75b 2019-03-26 stsp goto done;
1645 2ec1f75b 2019-03-26 stsp }
1646 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
1647 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
1648 2ec1f75b 2019-03-26 stsp goto done;
1649 2ec1f75b 2019-03-26 stsp }
1650 2ec1f75b 2019-03-26 stsp } else
1651 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
1652 b72f483a 2019-02-05 stsp
1653 b72f483a 2019-02-05 stsp err = got_diff_blob_file(blob1, f2, sb.st_size, path, a->diff_context,
1654 b72f483a 2019-02-05 stsp stdout);
1655 b72f483a 2019-02-05 stsp done:
1656 b72f483a 2019-02-05 stsp if (blob1)
1657 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
1658 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
1659 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1660 b72f483a 2019-02-05 stsp free(abspath);
1661 927df6b7 2019-02-10 stsp return err;
1662 927df6b7 2019-02-10 stsp }
1663 927df6b7 2019-02-10 stsp
1664 927df6b7 2019-02-10 stsp static const struct got_error *
1665 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
1666 b00d56cd 2018-04-01 stsp {
1667 b00d56cd 2018-04-01 stsp const struct got_error *error;
1668 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
1669 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
1670 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
1671 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
1672 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
1673 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
1674 15a94983 2018-12-23 stsp int type1, type2;
1675 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1676 c0cc5c62 2018-10-18 stsp const char *errstr;
1677 927df6b7 2019-02-10 stsp char *path = NULL;
1678 b00d56cd 2018-04-01 stsp
1679 b00d56cd 2018-04-01 stsp #ifndef PROFILE
1680 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1681 25eccc22 2019-01-04 stsp NULL) == -1)
1682 b00d56cd 2018-04-01 stsp err(1, "pledge");
1683 b00d56cd 2018-04-01 stsp #endif
1684 b00d56cd 2018-04-01 stsp
1685 b72f483a 2019-02-05 stsp while ((ch = getopt(argc, argv, "C:r:")) != -1) {
1686 b00d56cd 2018-04-01 stsp switch (ch) {
1687 c0cc5c62 2018-10-18 stsp case 'C':
1688 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
1689 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1690 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1691 c0cc5c62 2018-10-18 stsp break;
1692 b72f483a 2019-02-05 stsp case 'r':
1693 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
1694 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1695 b72f483a 2019-02-05 stsp err(1, "-r option");
1696 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1697 b72f483a 2019-02-05 stsp break;
1698 b00d56cd 2018-04-01 stsp default:
1699 2deda0b9 2019-03-07 stsp usage_diff();
1700 b00d56cd 2018-04-01 stsp /* NOTREACHED */
1701 b00d56cd 2018-04-01 stsp }
1702 b00d56cd 2018-04-01 stsp }
1703 b00d56cd 2018-04-01 stsp
1704 b00d56cd 2018-04-01 stsp argc -= optind;
1705 b00d56cd 2018-04-01 stsp argv += optind;
1706 b00d56cd 2018-04-01 stsp
1707 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
1708 b72f483a 2019-02-05 stsp if (cwd == NULL) {
1709 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1710 b72f483a 2019-02-05 stsp goto done;
1711 b72f483a 2019-02-05 stsp }
1712 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
1713 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1714 927df6b7 2019-02-10 stsp goto done;
1715 927df6b7 2019-02-10 stsp if (argc <= 1) {
1716 927df6b7 2019-02-10 stsp if (worktree == NULL) {
1717 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
1718 927df6b7 2019-02-10 stsp goto done;
1719 927df6b7 2019-02-10 stsp }
1720 b72f483a 2019-02-05 stsp if (repo_path)
1721 b72f483a 2019-02-05 stsp errx(1,
1722 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
1723 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
1724 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
1725 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1726 927df6b7 2019-02-10 stsp goto done;
1727 927df6b7 2019-02-10 stsp }
1728 927df6b7 2019-02-10 stsp if (argc == 1) {
1729 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1730 cbd1af7a 2019-03-18 stsp argv[0]);
1731 927df6b7 2019-02-10 stsp if (error)
1732 927df6b7 2019-02-10 stsp goto done;
1733 927df6b7 2019-02-10 stsp } else {
1734 927df6b7 2019-02-10 stsp path = strdup("");
1735 927df6b7 2019-02-10 stsp if (path == NULL) {
1736 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1737 927df6b7 2019-02-10 stsp goto done;
1738 927df6b7 2019-02-10 stsp }
1739 927df6b7 2019-02-10 stsp }
1740 b72f483a 2019-02-05 stsp } else if (argc == 2) {
1741 15a94983 2018-12-23 stsp id_str1 = argv[0];
1742 15a94983 2018-12-23 stsp id_str2 = argv[1];
1743 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
1744 30db809c 2019-06-05 stsp repo_path =
1745 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
1746 30db809c 2019-06-05 stsp if (repo_path == NULL) {
1747 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
1748 30db809c 2019-06-05 stsp goto done;
1749 30db809c 2019-06-05 stsp }
1750 30db809c 2019-06-05 stsp }
1751 b00d56cd 2018-04-01 stsp } else
1752 b00d56cd 2018-04-01 stsp usage_diff();
1753 25eccc22 2019-01-04 stsp
1754 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
1755 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
1756 b72f483a 2019-02-05 stsp if (repo_path == NULL)
1757 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
1758 b72f483a 2019-02-05 stsp }
1759 b00d56cd 2018-04-01 stsp
1760 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
1761 76089277 2018-04-01 stsp free(repo_path);
1762 b00d56cd 2018-04-01 stsp if (error != NULL)
1763 b00d56cd 2018-04-01 stsp goto done;
1764 b00d56cd 2018-04-01 stsp
1765 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1766 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
1767 c02c541e 2019-03-29 stsp if (error)
1768 c02c541e 2019-03-29 stsp goto done;
1769 c02c541e 2019-03-29 stsp
1770 30db809c 2019-06-05 stsp if (argc <= 1) {
1771 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
1772 b72f483a 2019-02-05 stsp char *id_str;
1773 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
1774 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
1775 b72f483a 2019-02-05 stsp if (error)
1776 b72f483a 2019-02-05 stsp goto done;
1777 b72f483a 2019-02-05 stsp arg.repo = repo;
1778 b72f483a 2019-02-05 stsp arg.worktree = worktree;
1779 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
1780 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
1781 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
1782 b72f483a 2019-02-05 stsp
1783 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_diff,
1784 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
1785 b72f483a 2019-02-05 stsp free(id_str);
1786 b72f483a 2019-02-05 stsp goto done;
1787 b72f483a 2019-02-05 stsp }
1788 b72f483a 2019-02-05 stsp
1789 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id1, id_str1,
1790 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1791 e02e74af 2019-05-28 stsp if (error) {
1792 e02e74af 2019-05-28 stsp struct got_reference *ref;
1793 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1794 e02e74af 2019-05-28 stsp goto done;
1795 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str1, 0);
1796 e02e74af 2019-05-28 stsp if (error != NULL)
1797 e02e74af 2019-05-28 stsp goto done;
1798 5e70831e 2019-05-28 stsp label1 = strdup(got_ref_get_name(ref));
1799 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1800 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1801 5e70831e 2019-05-28 stsp goto done;
1802 5e70831e 2019-05-28 stsp }
1803 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id1, repo, ref);
1804 e02e74af 2019-05-28 stsp got_ref_close(ref);
1805 e02e74af 2019-05-28 stsp if (error != NULL)
1806 5e70831e 2019-05-28 stsp goto done;
1807 5e70831e 2019-05-28 stsp } else {
1808 a297e751 2019-07-11 stsp error = got_object_id_str(&label1, id1);
1809 5e70831e 2019-05-28 stsp if (label1 == NULL) {
1810 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1811 e02e74af 2019-05-28 stsp goto done;
1812 5e70831e 2019-05-28 stsp }
1813 e02e74af 2019-05-28 stsp }
1814 b00d56cd 2018-04-01 stsp
1815 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&id2, id_str2,
1816 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_ANY, repo);
1817 e02e74af 2019-05-28 stsp if (error) {
1818 e02e74af 2019-05-28 stsp struct got_reference *ref;
1819 e02e74af 2019-05-28 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
1820 e02e74af 2019-05-28 stsp goto done;
1821 e02e74af 2019-05-28 stsp error = got_ref_open(&ref, repo, id_str2, 0);
1822 e02e74af 2019-05-28 stsp if (error != NULL)
1823 5e70831e 2019-05-28 stsp goto done;
1824 5e70831e 2019-05-28 stsp label2 = strdup(got_ref_get_name(ref));
1825 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1826 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1827 e02e74af 2019-05-28 stsp goto done;
1828 5e70831e 2019-05-28 stsp }
1829 e02e74af 2019-05-28 stsp error = got_ref_resolve(&id2, repo, ref);
1830 e02e74af 2019-05-28 stsp got_ref_close(ref);
1831 e02e74af 2019-05-28 stsp if (error != NULL)
1832 e02e74af 2019-05-28 stsp goto done;
1833 5e70831e 2019-05-28 stsp } else {
1834 a297e751 2019-07-11 stsp error = got_object_id_str(&label2, id2);
1835 5e70831e 2019-05-28 stsp if (label2 == NULL) {
1836 5e70831e 2019-05-28 stsp error = got_error_from_errno("strdup");
1837 5e70831e 2019-05-28 stsp goto done;
1838 5e70831e 2019-05-28 stsp }
1839 e02e74af 2019-05-28 stsp }
1840 b00d56cd 2018-04-01 stsp
1841 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
1842 15a94983 2018-12-23 stsp if (error)
1843 15a94983 2018-12-23 stsp goto done;
1844 15a94983 2018-12-23 stsp
1845 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
1846 15a94983 2018-12-23 stsp if (error)
1847 15a94983 2018-12-23 stsp goto done;
1848 15a94983 2018-12-23 stsp
1849 15a94983 2018-12-23 stsp if (type1 != type2) {
1850 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1851 b00d56cd 2018-04-01 stsp goto done;
1852 b00d56cd 2018-04-01 stsp }
1853 b00d56cd 2018-04-01 stsp
1854 15a94983 2018-12-23 stsp switch (type1) {
1855 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
1856 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
1857 54156555 2018-12-24 stsp diff_context, repo, stdout);
1858 b00d56cd 2018-04-01 stsp break;
1859 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
1860 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
1861 54156555 2018-12-24 stsp diff_context, repo, stdout);
1862 b00d56cd 2018-04-01 stsp break;
1863 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
1864 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
1865 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
1866 c0cc5c62 2018-10-18 stsp repo, stdout);
1867 b00d56cd 2018-04-01 stsp break;
1868 b00d56cd 2018-04-01 stsp default:
1869 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1870 b00d56cd 2018-04-01 stsp }
1871 b00d56cd 2018-04-01 stsp
1872 b00d56cd 2018-04-01 stsp done:
1873 5e70831e 2019-05-28 stsp free(label1);
1874 5e70831e 2019-05-28 stsp free(label2);
1875 15a94983 2018-12-23 stsp free(id1);
1876 15a94983 2018-12-23 stsp free(id2);
1877 927df6b7 2019-02-10 stsp free(path);
1878 b72f483a 2019-02-05 stsp if (worktree)
1879 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
1880 ad242220 2018-09-08 stsp if (repo) {
1881 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1882 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1883 ad242220 2018-09-08 stsp if (error == NULL)
1884 ad242220 2018-09-08 stsp error = repo_error;
1885 ad242220 2018-09-08 stsp }
1886 b00d56cd 2018-04-01 stsp return error;
1887 404c43c4 2018-06-21 stsp }
1888 404c43c4 2018-06-21 stsp
1889 404c43c4 2018-06-21 stsp __dead static void
1890 404c43c4 2018-06-21 stsp usage_blame(void)
1891 404c43c4 2018-06-21 stsp {
1892 9270e621 2019-02-05 stsp fprintf(stderr,
1893 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
1894 404c43c4 2018-06-21 stsp getprogname());
1895 404c43c4 2018-06-21 stsp exit(1);
1896 b00d56cd 2018-04-01 stsp }
1897 b00d56cd 2018-04-01 stsp
1898 404c43c4 2018-06-21 stsp static const struct got_error *
1899 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
1900 404c43c4 2018-06-21 stsp {
1901 404c43c4 2018-06-21 stsp const struct got_error *error;
1902 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
1903 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
1904 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
1905 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
1906 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
1907 404c43c4 2018-06-21 stsp int ch;
1908 404c43c4 2018-06-21 stsp
1909 404c43c4 2018-06-21 stsp #ifndef PROFILE
1910 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1911 36e2fb66 2019-01-04 stsp NULL) == -1)
1912 404c43c4 2018-06-21 stsp err(1, "pledge");
1913 404c43c4 2018-06-21 stsp #endif
1914 404c43c4 2018-06-21 stsp
1915 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
1916 404c43c4 2018-06-21 stsp switch (ch) {
1917 404c43c4 2018-06-21 stsp case 'c':
1918 404c43c4 2018-06-21 stsp commit_id_str = optarg;
1919 404c43c4 2018-06-21 stsp break;
1920 66bea077 2018-08-02 stsp case 'r':
1921 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
1922 66bea077 2018-08-02 stsp if (repo_path == NULL)
1923 66bea077 2018-08-02 stsp err(1, "-r option");
1924 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1925 66bea077 2018-08-02 stsp break;
1926 404c43c4 2018-06-21 stsp default:
1927 2deda0b9 2019-03-07 stsp usage_blame();
1928 404c43c4 2018-06-21 stsp /* NOTREACHED */
1929 404c43c4 2018-06-21 stsp }
1930 404c43c4 2018-06-21 stsp }
1931 404c43c4 2018-06-21 stsp
1932 404c43c4 2018-06-21 stsp argc -= optind;
1933 404c43c4 2018-06-21 stsp argv += optind;
1934 404c43c4 2018-06-21 stsp
1935 a39318fd 2018-08-02 stsp if (argc == 1)
1936 404c43c4 2018-06-21 stsp path = argv[0];
1937 a39318fd 2018-08-02 stsp else
1938 404c43c4 2018-06-21 stsp usage_blame();
1939 404c43c4 2018-06-21 stsp
1940 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
1941 66bea077 2018-08-02 stsp if (cwd == NULL) {
1942 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1943 66bea077 2018-08-02 stsp goto done;
1944 66bea077 2018-08-02 stsp }
1945 66bea077 2018-08-02 stsp if (repo_path == NULL) {
1946 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1947 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1948 66bea077 2018-08-02 stsp goto done;
1949 0c06baac 2019-02-05 stsp else
1950 0c06baac 2019-02-05 stsp error = NULL;
1951 0c06baac 2019-02-05 stsp if (worktree) {
1952 0c06baac 2019-02-05 stsp repo_path =
1953 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
1954 0c06baac 2019-02-05 stsp if (repo_path == NULL)
1955 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1956 0c06baac 2019-02-05 stsp if (error)
1957 0c06baac 2019-02-05 stsp goto done;
1958 0c06baac 2019-02-05 stsp } else {
1959 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
1960 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
1961 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1962 0c06baac 2019-02-05 stsp goto done;
1963 0c06baac 2019-02-05 stsp }
1964 66bea077 2018-08-02 stsp }
1965 66bea077 2018-08-02 stsp }
1966 36e2fb66 2019-01-04 stsp
1967 c02c541e 2019-03-29 stsp error = got_repo_open(&repo, repo_path);
1968 c02c541e 2019-03-29 stsp if (error != NULL)
1969 36e2fb66 2019-01-04 stsp goto done;
1970 66bea077 2018-08-02 stsp
1971 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
1972 c02c541e 2019-03-29 stsp if (error)
1973 404c43c4 2018-06-21 stsp goto done;
1974 404c43c4 2018-06-21 stsp
1975 0c06baac 2019-02-05 stsp if (worktree) {
1976 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1977 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
1978 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
1979 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
1980 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
1981 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
1982 6efaaa2d 2019-02-05 stsp path) == -1) {
1983 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1984 0c06baac 2019-02-05 stsp goto done;
1985 0c06baac 2019-02-05 stsp }
1986 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
1987 0c06baac 2019-02-05 stsp free(p);
1988 0c06baac 2019-02-05 stsp } else {
1989 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1990 0c06baac 2019-02-05 stsp }
1991 0c06baac 2019-02-05 stsp if (error)
1992 66bea077 2018-08-02 stsp goto done;
1993 66bea077 2018-08-02 stsp
1994 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
1995 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
1996 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
1997 404c43c4 2018-06-21 stsp if (error != NULL)
1998 66bea077 2018-08-02 stsp goto done;
1999 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2000 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2001 404c43c4 2018-06-21 stsp if (error != NULL)
2002 66bea077 2018-08-02 stsp goto done;
2003 404c43c4 2018-06-21 stsp } else {
2004 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
2005 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2006 404c43c4 2018-06-21 stsp if (error != NULL)
2007 66bea077 2018-08-02 stsp goto done;
2008 404c43c4 2018-06-21 stsp }
2009 404c43c4 2018-06-21 stsp
2010 66bea077 2018-08-02 stsp error = got_blame(in_repo_path, commit_id, repo, stdout);
2011 404c43c4 2018-06-21 stsp done:
2012 66bea077 2018-08-02 stsp free(in_repo_path);
2013 66bea077 2018-08-02 stsp free(repo_path);
2014 66bea077 2018-08-02 stsp free(cwd);
2015 404c43c4 2018-06-21 stsp free(commit_id);
2016 0c06baac 2019-02-05 stsp if (worktree)
2017 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2018 ad242220 2018-09-08 stsp if (repo) {
2019 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2020 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2021 ad242220 2018-09-08 stsp if (error == NULL)
2022 ad242220 2018-09-08 stsp error = repo_error;
2023 ad242220 2018-09-08 stsp }
2024 404c43c4 2018-06-21 stsp return error;
2025 5de5890b 2018-10-18 stsp }
2026 5de5890b 2018-10-18 stsp
2027 5de5890b 2018-10-18 stsp __dead static void
2028 5de5890b 2018-10-18 stsp usage_tree(void)
2029 5de5890b 2018-10-18 stsp {
2030 c1669e2e 2019-01-09 stsp fprintf(stderr,
2031 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2032 5de5890b 2018-10-18 stsp getprogname());
2033 5de5890b 2018-10-18 stsp exit(1);
2034 5de5890b 2018-10-18 stsp }
2035 5de5890b 2018-10-18 stsp
2036 c1669e2e 2019-01-09 stsp static void
2037 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2038 c1669e2e 2019-01-09 stsp const char *root_path)
2039 c1669e2e 2019-01-09 stsp {
2040 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2041 5de5890b 2018-10-18 stsp
2042 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2043 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2044 c1669e2e 2019-01-09 stsp path++;
2045 c1669e2e 2019-01-09 stsp
2046 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2047 d6e648b4 2019-02-10 stsp is_root_path ? "" : "/", te->name,
2048 d6e648b4 2019-02-10 stsp S_ISDIR(te->mode) ? "/" : ((te->mode & S_IXUSR) ? "*" : ""));
2049 c1669e2e 2019-01-09 stsp }
2050 c1669e2e 2019-01-09 stsp
2051 5de5890b 2018-10-18 stsp static const struct got_error *
2052 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2053 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2054 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2055 5de5890b 2018-10-18 stsp {
2056 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2057 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2058 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2059 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2060 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2061 5de5890b 2018-10-18 stsp
2062 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2063 5de5890b 2018-10-18 stsp if (err)
2064 5de5890b 2018-10-18 stsp goto done;
2065 5de5890b 2018-10-18 stsp
2066 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2067 5de5890b 2018-10-18 stsp if (err)
2068 5de5890b 2018-10-18 stsp goto done;
2069 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2070 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2071 5de5890b 2018-10-18 stsp while (te) {
2072 5de5890b 2018-10-18 stsp char *id = NULL;
2073 84453469 2018-11-11 stsp
2074 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2075 84453469 2018-11-11 stsp break;
2076 84453469 2018-11-11 stsp
2077 5de5890b 2018-10-18 stsp if (show_ids) {
2078 5de5890b 2018-10-18 stsp char *id_str;
2079 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2080 5de5890b 2018-10-18 stsp if (err)
2081 5de5890b 2018-10-18 stsp goto done;
2082 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2083 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2084 5de5890b 2018-10-18 stsp free(id_str);
2085 5de5890b 2018-10-18 stsp goto done;
2086 5de5890b 2018-10-18 stsp }
2087 5de5890b 2018-10-18 stsp free(id_str);
2088 5de5890b 2018-10-18 stsp }
2089 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2090 5de5890b 2018-10-18 stsp free(id);
2091 c1669e2e 2019-01-09 stsp
2092 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2093 c1669e2e 2019-01-09 stsp char *child_path;
2094 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2095 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2096 c1669e2e 2019-01-09 stsp te->name) == -1) {
2097 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2098 c1669e2e 2019-01-09 stsp goto done;
2099 c1669e2e 2019-01-09 stsp }
2100 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2101 c1669e2e 2019-01-09 stsp root_path, repo);
2102 c1669e2e 2019-01-09 stsp free(child_path);
2103 c1669e2e 2019-01-09 stsp if (err)
2104 c1669e2e 2019-01-09 stsp goto done;
2105 c1669e2e 2019-01-09 stsp }
2106 c1669e2e 2019-01-09 stsp
2107 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2108 5de5890b 2018-10-18 stsp }
2109 5de5890b 2018-10-18 stsp done:
2110 5de5890b 2018-10-18 stsp if (tree)
2111 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2112 5de5890b 2018-10-18 stsp free(tree_id);
2113 5de5890b 2018-10-18 stsp return err;
2114 404c43c4 2018-06-21 stsp }
2115 404c43c4 2018-06-21 stsp
2116 5de5890b 2018-10-18 stsp static const struct got_error *
2117 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2118 5de5890b 2018-10-18 stsp {
2119 5de5890b 2018-10-18 stsp const struct got_error *error;
2120 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2121 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2122 7a2c19d6 2019-02-05 stsp const char *path;
2123 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2124 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2125 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2126 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2127 5de5890b 2018-10-18 stsp int ch;
2128 5de5890b 2018-10-18 stsp
2129 5de5890b 2018-10-18 stsp #ifndef PROFILE
2130 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2131 0f8d269b 2019-01-04 stsp NULL) == -1)
2132 5de5890b 2018-10-18 stsp err(1, "pledge");
2133 5de5890b 2018-10-18 stsp #endif
2134 5de5890b 2018-10-18 stsp
2135 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2136 5de5890b 2018-10-18 stsp switch (ch) {
2137 5de5890b 2018-10-18 stsp case 'c':
2138 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2139 5de5890b 2018-10-18 stsp break;
2140 5de5890b 2018-10-18 stsp case 'r':
2141 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2142 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2143 5de5890b 2018-10-18 stsp err(1, "-r option");
2144 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2145 5de5890b 2018-10-18 stsp break;
2146 5de5890b 2018-10-18 stsp case 'i':
2147 5de5890b 2018-10-18 stsp show_ids = 1;
2148 5de5890b 2018-10-18 stsp break;
2149 c1669e2e 2019-01-09 stsp case 'R':
2150 c1669e2e 2019-01-09 stsp recurse = 1;
2151 c1669e2e 2019-01-09 stsp break;
2152 5de5890b 2018-10-18 stsp default:
2153 2deda0b9 2019-03-07 stsp usage_tree();
2154 5de5890b 2018-10-18 stsp /* NOTREACHED */
2155 5de5890b 2018-10-18 stsp }
2156 5de5890b 2018-10-18 stsp }
2157 5de5890b 2018-10-18 stsp
2158 5de5890b 2018-10-18 stsp argc -= optind;
2159 5de5890b 2018-10-18 stsp argv += optind;
2160 5de5890b 2018-10-18 stsp
2161 5de5890b 2018-10-18 stsp if (argc == 1)
2162 5de5890b 2018-10-18 stsp path = argv[0];
2163 5de5890b 2018-10-18 stsp else if (argc > 1)
2164 5de5890b 2018-10-18 stsp usage_tree();
2165 5de5890b 2018-10-18 stsp else
2166 7a2c19d6 2019-02-05 stsp path = NULL;
2167 7a2c19d6 2019-02-05 stsp
2168 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2169 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2170 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2171 9bf7a39b 2019-02-05 stsp goto done;
2172 9bf7a39b 2019-02-05 stsp }
2173 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2174 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2175 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2176 7a2c19d6 2019-02-05 stsp goto done;
2177 7a2c19d6 2019-02-05 stsp else
2178 7a2c19d6 2019-02-05 stsp error = NULL;
2179 7a2c19d6 2019-02-05 stsp if (worktree) {
2180 7a2c19d6 2019-02-05 stsp repo_path =
2181 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2182 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2183 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2184 7a2c19d6 2019-02-05 stsp if (error)
2185 7a2c19d6 2019-02-05 stsp goto done;
2186 7a2c19d6 2019-02-05 stsp } else {
2187 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2188 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2189 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2190 7a2c19d6 2019-02-05 stsp goto done;
2191 7a2c19d6 2019-02-05 stsp }
2192 5de5890b 2018-10-18 stsp }
2193 5de5890b 2018-10-18 stsp }
2194 5de5890b 2018-10-18 stsp
2195 5de5890b 2018-10-18 stsp error = got_repo_open(&repo, repo_path);
2196 5de5890b 2018-10-18 stsp if (error != NULL)
2197 c02c541e 2019-03-29 stsp goto done;
2198 c02c541e 2019-03-29 stsp
2199 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL, 0);
2200 c02c541e 2019-03-29 stsp if (error)
2201 5de5890b 2018-10-18 stsp goto done;
2202 5de5890b 2018-10-18 stsp
2203 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2204 9bf7a39b 2019-02-05 stsp if (worktree) {
2205 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2206 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2207 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2208 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2209 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2210 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2211 9bf7a39b 2019-02-05 stsp goto done;
2212 9bf7a39b 2019-02-05 stsp }
2213 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2214 9bf7a39b 2019-02-05 stsp free(p);
2215 9bf7a39b 2019-02-05 stsp if (error)
2216 9bf7a39b 2019-02-05 stsp goto done;
2217 9bf7a39b 2019-02-05 stsp } else
2218 9bf7a39b 2019-02-05 stsp path = "/";
2219 9bf7a39b 2019-02-05 stsp }
2220 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2221 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2222 9bf7a39b 2019-02-05 stsp if (error != NULL)
2223 9bf7a39b 2019-02-05 stsp goto done;
2224 9bf7a39b 2019-02-05 stsp }
2225 5de5890b 2018-10-18 stsp
2226 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2227 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2228 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2229 5de5890b 2018-10-18 stsp if (error != NULL)
2230 5de5890b 2018-10-18 stsp goto done;
2231 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2232 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2233 5de5890b 2018-10-18 stsp if (error != NULL)
2234 5de5890b 2018-10-18 stsp goto done;
2235 5de5890b 2018-10-18 stsp } else {
2236 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&commit_id,
2237 dd88155e 2019-06-29 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, repo);
2238 5de5890b 2018-10-18 stsp if (error != NULL)
2239 5de5890b 2018-10-18 stsp goto done;
2240 5de5890b 2018-10-18 stsp }
2241 5de5890b 2018-10-18 stsp
2242 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2243 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2244 5de5890b 2018-10-18 stsp done:
2245 5de5890b 2018-10-18 stsp free(in_repo_path);
2246 5de5890b 2018-10-18 stsp free(repo_path);
2247 5de5890b 2018-10-18 stsp free(cwd);
2248 5de5890b 2018-10-18 stsp free(commit_id);
2249 7a2c19d6 2019-02-05 stsp if (worktree)
2250 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2251 5de5890b 2018-10-18 stsp if (repo) {
2252 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2253 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2254 5de5890b 2018-10-18 stsp if (error == NULL)
2255 5de5890b 2018-10-18 stsp error = repo_error;
2256 5de5890b 2018-10-18 stsp }
2257 5de5890b 2018-10-18 stsp return error;
2258 5de5890b 2018-10-18 stsp }
2259 5de5890b 2018-10-18 stsp
2260 6bad629b 2019-02-04 stsp __dead static void
2261 6bad629b 2019-02-04 stsp usage_status(void)
2262 6bad629b 2019-02-04 stsp {
2263 927df6b7 2019-02-10 stsp fprintf(stderr, "usage: %s status [path]\n", getprogname());
2264 6bad629b 2019-02-04 stsp exit(1);
2265 6bad629b 2019-02-04 stsp }
2266 5c860e29 2018-03-12 stsp
2267 b72f483a 2019-02-05 stsp static const struct got_error *
2268 b72f483a 2019-02-05 stsp print_status(void *arg, unsigned char status, const char *path,
2269 016a88dd 2019-05-13 stsp struct got_object_id *blob_id, struct got_object_id *commit_id)
2270 6bad629b 2019-02-04 stsp {
2271 6bad629b 2019-02-04 stsp printf("%c %s\n", status, path);
2272 b72f483a 2019-02-05 stsp return NULL;
2273 6bad629b 2019-02-04 stsp }
2274 5c860e29 2018-03-12 stsp
2275 6bad629b 2019-02-04 stsp static const struct got_error *
2276 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2277 6bad629b 2019-02-04 stsp {
2278 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2279 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2280 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2281 927df6b7 2019-02-10 stsp char *cwd = NULL, *path = NULL;
2282 6bad629b 2019-02-04 stsp int ch;
2283 5c860e29 2018-03-12 stsp
2284 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2285 6bad629b 2019-02-04 stsp switch (ch) {
2286 5c860e29 2018-03-12 stsp default:
2287 2deda0b9 2019-03-07 stsp usage_status();
2288 6bad629b 2019-02-04 stsp /* NOTREACHED */
2289 5c860e29 2018-03-12 stsp }
2290 5c860e29 2018-03-12 stsp }
2291 5c860e29 2018-03-12 stsp
2292 6bad629b 2019-02-04 stsp argc -= optind;
2293 6bad629b 2019-02-04 stsp argv += optind;
2294 5c860e29 2018-03-12 stsp
2295 6bad629b 2019-02-04 stsp #ifndef PROFILE
2296 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2297 6bad629b 2019-02-04 stsp NULL) == -1)
2298 6bad629b 2019-02-04 stsp err(1, "pledge");
2299 f42b1b34 2018-03-12 stsp #endif
2300 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2301 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2302 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2303 927df6b7 2019-02-10 stsp goto done;
2304 927df6b7 2019-02-10 stsp }
2305 927df6b7 2019-02-10 stsp
2306 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2307 927df6b7 2019-02-10 stsp if (error != NULL)
2308 927df6b7 2019-02-10 stsp goto done;
2309 927df6b7 2019-02-10 stsp
2310 6bad629b 2019-02-04 stsp if (argc == 0) {
2311 927df6b7 2019-02-10 stsp path = strdup("");
2312 927df6b7 2019-02-10 stsp if (path == NULL) {
2313 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2314 6bad629b 2019-02-04 stsp goto done;
2315 6bad629b 2019-02-04 stsp }
2316 6bad629b 2019-02-04 stsp } else if (argc == 1) {
2317 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree, argv[0]);
2318 927df6b7 2019-02-10 stsp if (error)
2319 6bad629b 2019-02-04 stsp goto done;
2320 6bad629b 2019-02-04 stsp } else
2321 6bad629b 2019-02-04 stsp usage_status();
2322 6bad629b 2019-02-04 stsp
2323 6bad629b 2019-02-04 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2324 6bad629b 2019-02-04 stsp if (error != NULL)
2325 6bad629b 2019-02-04 stsp goto done;
2326 6bad629b 2019-02-04 stsp
2327 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2328 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2329 6bad629b 2019-02-04 stsp if (error)
2330 6bad629b 2019-02-04 stsp goto done;
2331 6bad629b 2019-02-04 stsp
2332 927df6b7 2019-02-10 stsp error = got_worktree_status(worktree, path, repo, print_status, NULL,
2333 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2334 6bad629b 2019-02-04 stsp done:
2335 927df6b7 2019-02-10 stsp free(cwd);
2336 927df6b7 2019-02-10 stsp free(path);
2337 d0eebce4 2019-03-11 stsp return error;
2338 d0eebce4 2019-03-11 stsp }
2339 d0eebce4 2019-03-11 stsp
2340 d0eebce4 2019-03-11 stsp __dead static void
2341 d0eebce4 2019-03-11 stsp usage_ref(void)
2342 d0eebce4 2019-03-11 stsp {
2343 d0eebce4 2019-03-11 stsp fprintf(stderr,
2344 d83d9d5c 2019-05-13 stsp "usage: %s ref [-r repository] -l | -d name | name target\n",
2345 d0eebce4 2019-03-11 stsp getprogname());
2346 d0eebce4 2019-03-11 stsp exit(1);
2347 d0eebce4 2019-03-11 stsp }
2348 d0eebce4 2019-03-11 stsp
2349 d0eebce4 2019-03-11 stsp static const struct got_error *
2350 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2351 d0eebce4 2019-03-11 stsp {
2352 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2353 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2354 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
2355 d0eebce4 2019-03-11 stsp
2356 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
2357 d0eebce4 2019-03-11 stsp err = got_ref_list(&refs, repo);
2358 d0eebce4 2019-03-11 stsp if (err)
2359 d0eebce4 2019-03-11 stsp return err;
2360 d0eebce4 2019-03-11 stsp
2361 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2362 d0eebce4 2019-03-11 stsp char *refstr;
2363 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
2364 d0eebce4 2019-03-11 stsp if (refstr == NULL)
2365 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
2366 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
2367 d0eebce4 2019-03-11 stsp free(refstr);
2368 d0eebce4 2019-03-11 stsp }
2369 d0eebce4 2019-03-11 stsp
2370 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2371 d0eebce4 2019-03-11 stsp return NULL;
2372 d0eebce4 2019-03-11 stsp }
2373 d0eebce4 2019-03-11 stsp
2374 d0eebce4 2019-03-11 stsp static const struct got_error *
2375 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
2376 d0eebce4 2019-03-11 stsp {
2377 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2378 d0eebce4 2019-03-11 stsp struct got_reference *ref;
2379 d0eebce4 2019-03-11 stsp
2380 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
2381 d0eebce4 2019-03-11 stsp if (err)
2382 d0eebce4 2019-03-11 stsp return err;
2383 d0eebce4 2019-03-11 stsp
2384 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
2385 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2386 d0eebce4 2019-03-11 stsp return err;
2387 d0eebce4 2019-03-11 stsp }
2388 d0eebce4 2019-03-11 stsp
2389 d0eebce4 2019-03-11 stsp static const struct got_error *
2390 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
2391 d0eebce4 2019-03-11 stsp {
2392 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
2393 d0eebce4 2019-03-11 stsp struct got_object_id *id;
2394 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
2395 d1644381 2019-07-14 stsp
2396 d1644381 2019-07-14 stsp /*
2397 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
2398 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
2399 d1644381 2019-07-14 stsp * an unintended typo.
2400 d1644381 2019-07-14 stsp */
2401 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
2402 d1644381 2019-07-14 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2403 d0eebce4 2019-03-11 stsp
2404 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
2405 dd88155e 2019-06-29 stsp repo);
2406 d83d9d5c 2019-05-13 stsp if (err) {
2407 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
2408 d0eebce4 2019-03-11 stsp
2409 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2410 d83d9d5c 2019-05-13 stsp return err;
2411 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
2412 d83d9d5c 2019-05-13 stsp if (err)
2413 d83d9d5c 2019-05-13 stsp return err;
2414 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
2415 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
2416 d83d9d5c 2019-05-13 stsp if (err)
2417 d83d9d5c 2019-05-13 stsp return err;
2418 d83d9d5c 2019-05-13 stsp }
2419 d83d9d5c 2019-05-13 stsp
2420 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
2421 d0eebce4 2019-03-11 stsp if (err)
2422 d0eebce4 2019-03-11 stsp goto done;
2423 d0eebce4 2019-03-11 stsp
2424 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
2425 d0eebce4 2019-03-11 stsp done:
2426 d0eebce4 2019-03-11 stsp if (ref)
2427 d0eebce4 2019-03-11 stsp got_ref_close(ref);
2428 d0eebce4 2019-03-11 stsp free(id);
2429 d0eebce4 2019-03-11 stsp return err;
2430 d0eebce4 2019-03-11 stsp }
2431 d0eebce4 2019-03-11 stsp
2432 d0eebce4 2019-03-11 stsp static const struct got_error *
2433 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
2434 d0eebce4 2019-03-11 stsp {
2435 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
2436 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
2437 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
2438 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
2439 d0eebce4 2019-03-11 stsp int ch, do_list = 0;
2440 d0eebce4 2019-03-11 stsp const char *delref = NULL;
2441 d0eebce4 2019-03-11 stsp
2442 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
2443 d0eebce4 2019-03-11 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2444 d0eebce4 2019-03-11 stsp switch (ch) {
2445 d0eebce4 2019-03-11 stsp case 'd':
2446 d0eebce4 2019-03-11 stsp delref = optarg;
2447 d0eebce4 2019-03-11 stsp break;
2448 d0eebce4 2019-03-11 stsp case 'r':
2449 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
2450 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2451 d0eebce4 2019-03-11 stsp err(1, "-r option");
2452 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2453 d0eebce4 2019-03-11 stsp break;
2454 d0eebce4 2019-03-11 stsp case 'l':
2455 d0eebce4 2019-03-11 stsp do_list = 1;
2456 d0eebce4 2019-03-11 stsp break;
2457 d0eebce4 2019-03-11 stsp default:
2458 d0eebce4 2019-03-11 stsp usage_ref();
2459 d0eebce4 2019-03-11 stsp /* NOTREACHED */
2460 d0eebce4 2019-03-11 stsp }
2461 d0eebce4 2019-03-11 stsp }
2462 d0eebce4 2019-03-11 stsp
2463 d0eebce4 2019-03-11 stsp if (do_list && delref)
2464 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
2465 d0eebce4 2019-03-11 stsp
2466 d0eebce4 2019-03-11 stsp argc -= optind;
2467 d0eebce4 2019-03-11 stsp argv += optind;
2468 d0eebce4 2019-03-11 stsp
2469 d0eebce4 2019-03-11 stsp if (do_list || delref) {
2470 d0eebce4 2019-03-11 stsp if (argc > 0)
2471 d0eebce4 2019-03-11 stsp usage_ref();
2472 d0eebce4 2019-03-11 stsp } else if (argc != 2)
2473 d0eebce4 2019-03-11 stsp usage_ref();
2474 d0eebce4 2019-03-11 stsp
2475 d0eebce4 2019-03-11 stsp #ifndef PROFILE
2476 e0b57350 2019-03-12 stsp if (do_list) {
2477 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2478 e0b57350 2019-03-12 stsp NULL) == -1)
2479 e0b57350 2019-03-12 stsp err(1, "pledge");
2480 e0b57350 2019-03-12 stsp } else {
2481 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2482 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
2483 e0b57350 2019-03-12 stsp err(1, "pledge");
2484 e0b57350 2019-03-12 stsp }
2485 d0eebce4 2019-03-11 stsp #endif
2486 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
2487 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
2488 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2489 d0eebce4 2019-03-11 stsp goto done;
2490 d0eebce4 2019-03-11 stsp }
2491 d0eebce4 2019-03-11 stsp
2492 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2493 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
2494 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2495 d0eebce4 2019-03-11 stsp goto done;
2496 d0eebce4 2019-03-11 stsp else
2497 d0eebce4 2019-03-11 stsp error = NULL;
2498 d0eebce4 2019-03-11 stsp if (worktree) {
2499 d0eebce4 2019-03-11 stsp repo_path =
2500 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
2501 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
2502 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2503 d0eebce4 2019-03-11 stsp if (error)
2504 d0eebce4 2019-03-11 stsp goto done;
2505 d0eebce4 2019-03-11 stsp } else {
2506 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
2507 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
2508 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2509 d0eebce4 2019-03-11 stsp goto done;
2510 d0eebce4 2019-03-11 stsp }
2511 d0eebce4 2019-03-11 stsp }
2512 d0eebce4 2019-03-11 stsp }
2513 d0eebce4 2019-03-11 stsp
2514 d0eebce4 2019-03-11 stsp error = got_repo_open(&repo, repo_path);
2515 d0eebce4 2019-03-11 stsp if (error != NULL)
2516 d0eebce4 2019-03-11 stsp goto done;
2517 d0eebce4 2019-03-11 stsp
2518 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2519 314a6357 2019-05-13 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2520 c02c541e 2019-03-29 stsp if (error)
2521 c02c541e 2019-03-29 stsp goto done;
2522 c02c541e 2019-03-29 stsp
2523 d0eebce4 2019-03-11 stsp if (do_list)
2524 d0eebce4 2019-03-11 stsp error = list_refs(repo);
2525 d0eebce4 2019-03-11 stsp else if (delref)
2526 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
2527 d0eebce4 2019-03-11 stsp else
2528 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
2529 4e759de4 2019-06-26 stsp done:
2530 4e759de4 2019-06-26 stsp if (repo)
2531 4e759de4 2019-06-26 stsp got_repo_close(repo);
2532 4e759de4 2019-06-26 stsp if (worktree)
2533 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
2534 4e759de4 2019-06-26 stsp free(cwd);
2535 4e759de4 2019-06-26 stsp free(repo_path);
2536 4e759de4 2019-06-26 stsp return error;
2537 4e759de4 2019-06-26 stsp }
2538 4e759de4 2019-06-26 stsp
2539 4e759de4 2019-06-26 stsp __dead static void
2540 4e759de4 2019-06-26 stsp usage_branch(void)
2541 4e759de4 2019-06-26 stsp {
2542 4e759de4 2019-06-26 stsp fprintf(stderr,
2543 4e759de4 2019-06-26 stsp "usage: %s branch [-r repository] -l | -d name | "
2544 4e759de4 2019-06-26 stsp "name [base-branch]\n", getprogname());
2545 4e759de4 2019-06-26 stsp exit(1);
2546 4e759de4 2019-06-26 stsp }
2547 4e759de4 2019-06-26 stsp
2548 4e759de4 2019-06-26 stsp static const struct got_error *
2549 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
2550 4e759de4 2019-06-26 stsp {
2551 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
2552 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
2553 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
2554 4e759de4 2019-06-26 stsp
2555 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
2556 ba882ee3 2019-07-11 stsp
2557 4e759de4 2019-06-26 stsp err = got_ref_list(&refs, repo);
2558 4e759de4 2019-06-26 stsp if (err)
2559 4e759de4 2019-06-26 stsp return err;
2560 4e759de4 2019-06-26 stsp
2561 4e759de4 2019-06-26 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
2562 ba882ee3 2019-07-11 stsp const char *refname, *marker = " ";
2563 4e759de4 2019-06-26 stsp char *refstr;
2564 4e759de4 2019-06-26 stsp refname = got_ref_get_name(re->ref);
2565 4e759de4 2019-06-26 stsp if (strncmp(refname, "refs/heads/", 11) != 0)
2566 4e759de4 2019-06-26 stsp continue;
2567 ba882ee3 2019-07-11 stsp if (worktree && strcmp(refname,
2568 ba882ee3 2019-07-11 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
2569 ba882ee3 2019-07-11 stsp struct got_object_id *id = NULL;
2570 ba882ee3 2019-07-11 stsp err = got_ref_resolve(&id, repo, re->ref);
2571 ba882ee3 2019-07-11 stsp if (err)
2572 ba882ee3 2019-07-11 stsp return err;
2573 ba882ee3 2019-07-11 stsp if (got_object_id_cmp(id,
2574 ba882ee3 2019-07-11 stsp got_worktree_get_base_commit_id(worktree)) == 0)
2575 ba882ee3 2019-07-11 stsp marker = "* ";
2576 ba882ee3 2019-07-11 stsp else
2577 ba882ee3 2019-07-11 stsp marker = "~ ";
2578 ba882ee3 2019-07-11 stsp free(id);
2579 ba882ee3 2019-07-11 stsp }
2580 4e759de4 2019-06-26 stsp refname += 11;
2581 4e759de4 2019-06-26 stsp refstr = got_ref_to_str(re->ref);
2582 4e759de4 2019-06-26 stsp if (refstr == NULL)
2583 4e759de4 2019-06-26 stsp return got_error_from_errno("got_ref_to_str");
2584 ba882ee3 2019-07-11 stsp printf("%s%s: %s\n", marker, refname, refstr);
2585 4e759de4 2019-06-26 stsp free(refstr);
2586 4e759de4 2019-06-26 stsp }
2587 4e759de4 2019-06-26 stsp
2588 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
2589 4e759de4 2019-06-26 stsp return NULL;
2590 4e759de4 2019-06-26 stsp }
2591 4e759de4 2019-06-26 stsp
2592 4e759de4 2019-06-26 stsp static const struct got_error *
2593 4e759de4 2019-06-26 stsp delete_branch(struct got_repository *repo, const char *branch_name)
2594 4e759de4 2019-06-26 stsp {
2595 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2596 4e759de4 2019-06-26 stsp struct got_reference *ref;
2597 4e759de4 2019-06-26 stsp char *refname;
2598 4e759de4 2019-06-26 stsp
2599 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
2600 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2601 4e759de4 2019-06-26 stsp
2602 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2603 4e759de4 2019-06-26 stsp if (err)
2604 4e759de4 2019-06-26 stsp goto done;
2605 4e759de4 2019-06-26 stsp
2606 4e759de4 2019-06-26 stsp err = got_ref_delete(ref, repo);
2607 4e759de4 2019-06-26 stsp got_ref_close(ref);
2608 4e759de4 2019-06-26 stsp done:
2609 4e759de4 2019-06-26 stsp free(refname);
2610 4e759de4 2019-06-26 stsp return err;
2611 4e759de4 2019-06-26 stsp }
2612 4e759de4 2019-06-26 stsp
2613 4e759de4 2019-06-26 stsp static const struct got_error *
2614 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
2615 4e759de4 2019-06-26 stsp const char *base_branch)
2616 4e759de4 2019-06-26 stsp {
2617 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
2618 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
2619 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
2620 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
2621 4e759de4 2019-06-26 stsp struct got_reference *base_ref;
2622 d3f84d51 2019-07-11 stsp
2623 d3f84d51 2019-07-11 stsp /*
2624 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
2625 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
2626 d3f84d51 2019-07-11 stsp * an unintended typo.
2627 d3f84d51 2019-07-11 stsp */
2628 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
2629 d3f84d51 2019-07-11 stsp return got_error(GOT_ERR_BAD_REF_NAME);
2630 4e759de4 2019-06-26 stsp
2631 4e759de4 2019-06-26 stsp if (strcmp(GOT_REF_HEAD, base_branch) == 0) {
2632 4e759de4 2019-06-26 stsp base_refname = strdup(GOT_REF_HEAD);
2633 4e759de4 2019-06-26 stsp if (base_refname == NULL)
2634 4e759de4 2019-06-26 stsp return got_error_from_errno("strdup");
2635 4e759de4 2019-06-26 stsp } else if (asprintf(&base_refname, "refs/heads/%s", base_branch) == -1)
2636 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
2637 4e759de4 2019-06-26 stsp
2638 4e759de4 2019-06-26 stsp err = got_ref_open(&base_ref, repo, base_refname, 0);
2639 4e759de4 2019-06-26 stsp if (err)
2640 4e759de4 2019-06-26 stsp goto done;
2641 4e759de4 2019-06-26 stsp err = got_ref_resolve(&id, repo, base_ref);
2642 4e759de4 2019-06-26 stsp got_ref_close(base_ref);
2643 4e759de4 2019-06-26 stsp if (err)
2644 4e759de4 2019-06-26 stsp goto done;
2645 4e759de4 2019-06-26 stsp
2646 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
2647 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
2648 4e759de4 2019-06-26 stsp goto done;
2649 4e759de4 2019-06-26 stsp }
2650 4e759de4 2019-06-26 stsp
2651 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
2652 4e759de4 2019-06-26 stsp if (err == NULL) {
2653 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
2654 4e759de4 2019-06-26 stsp goto done;
2655 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
2656 4e759de4 2019-06-26 stsp goto done;
2657 4e759de4 2019-06-26 stsp
2658 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
2659 4e759de4 2019-06-26 stsp if (err)
2660 4e759de4 2019-06-26 stsp goto done;
2661 4e759de4 2019-06-26 stsp
2662 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
2663 d0eebce4 2019-03-11 stsp done:
2664 4e759de4 2019-06-26 stsp if (ref)
2665 4e759de4 2019-06-26 stsp got_ref_close(ref);
2666 4e759de4 2019-06-26 stsp free(id);
2667 4e759de4 2019-06-26 stsp free(base_refname);
2668 4e759de4 2019-06-26 stsp free(refname);
2669 4e759de4 2019-06-26 stsp return err;
2670 4e759de4 2019-06-26 stsp }
2671 4e759de4 2019-06-26 stsp
2672 4e759de4 2019-06-26 stsp static const struct got_error *
2673 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
2674 4e759de4 2019-06-26 stsp {
2675 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
2676 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
2677 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
2678 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
2679 4e759de4 2019-06-26 stsp int ch, do_list = 0;
2680 4e759de4 2019-06-26 stsp const char *delref = NULL;
2681 4e759de4 2019-06-26 stsp
2682 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
2683 4e759de4 2019-06-26 stsp switch (ch) {
2684 4e759de4 2019-06-26 stsp case 'd':
2685 4e759de4 2019-06-26 stsp delref = optarg;
2686 4e759de4 2019-06-26 stsp break;
2687 4e759de4 2019-06-26 stsp case 'r':
2688 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
2689 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2690 4e759de4 2019-06-26 stsp err(1, "-r option");
2691 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
2692 4e759de4 2019-06-26 stsp break;
2693 4e759de4 2019-06-26 stsp case 'l':
2694 4e759de4 2019-06-26 stsp do_list = 1;
2695 4e759de4 2019-06-26 stsp break;
2696 4e759de4 2019-06-26 stsp default:
2697 4e759de4 2019-06-26 stsp usage_branch();
2698 4e759de4 2019-06-26 stsp /* NOTREACHED */
2699 4e759de4 2019-06-26 stsp }
2700 4e759de4 2019-06-26 stsp }
2701 4e759de4 2019-06-26 stsp
2702 4e759de4 2019-06-26 stsp if (do_list && delref)
2703 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
2704 4e759de4 2019-06-26 stsp
2705 4e759de4 2019-06-26 stsp argc -= optind;
2706 4e759de4 2019-06-26 stsp argv += optind;
2707 4e759de4 2019-06-26 stsp
2708 4e759de4 2019-06-26 stsp if (do_list || delref) {
2709 4e759de4 2019-06-26 stsp if (argc > 0)
2710 4e759de4 2019-06-26 stsp usage_branch();
2711 4e759de4 2019-06-26 stsp } else if (argc < 1 || argc > 2)
2712 4e759de4 2019-06-26 stsp usage_branch();
2713 4e759de4 2019-06-26 stsp
2714 4e759de4 2019-06-26 stsp #ifndef PROFILE
2715 4e759de4 2019-06-26 stsp if (do_list) {
2716 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
2717 4e759de4 2019-06-26 stsp NULL) == -1)
2718 4e759de4 2019-06-26 stsp err(1, "pledge");
2719 4e759de4 2019-06-26 stsp } else {
2720 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
2721 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
2722 4e759de4 2019-06-26 stsp err(1, "pledge");
2723 4e759de4 2019-06-26 stsp }
2724 4e759de4 2019-06-26 stsp #endif
2725 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
2726 4e759de4 2019-06-26 stsp if (cwd == NULL) {
2727 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
2728 4e759de4 2019-06-26 stsp goto done;
2729 4e759de4 2019-06-26 stsp }
2730 4e759de4 2019-06-26 stsp
2731 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2732 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
2733 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2734 4e759de4 2019-06-26 stsp goto done;
2735 4e759de4 2019-06-26 stsp else
2736 4e759de4 2019-06-26 stsp error = NULL;
2737 4e759de4 2019-06-26 stsp if (worktree) {
2738 4e759de4 2019-06-26 stsp repo_path =
2739 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
2740 4e759de4 2019-06-26 stsp if (repo_path == NULL)
2741 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2742 4e759de4 2019-06-26 stsp if (error)
2743 4e759de4 2019-06-26 stsp goto done;
2744 4e759de4 2019-06-26 stsp } else {
2745 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
2746 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
2747 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
2748 4e759de4 2019-06-26 stsp goto done;
2749 4e759de4 2019-06-26 stsp }
2750 4e759de4 2019-06-26 stsp }
2751 4e759de4 2019-06-26 stsp }
2752 4e759de4 2019-06-26 stsp
2753 4e759de4 2019-06-26 stsp error = got_repo_open(&repo, repo_path);
2754 4e759de4 2019-06-26 stsp if (error != NULL)
2755 4e759de4 2019-06-26 stsp goto done;
2756 4e759de4 2019-06-26 stsp
2757 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
2758 4e759de4 2019-06-26 stsp worktree ? got_worktree_get_root_path(worktree) : NULL, 0);
2759 4e759de4 2019-06-26 stsp if (error)
2760 4e759de4 2019-06-26 stsp goto done;
2761 4e759de4 2019-06-26 stsp
2762 4e759de4 2019-06-26 stsp if (do_list)
2763 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
2764 4e759de4 2019-06-26 stsp else if (delref)
2765 4e759de4 2019-06-26 stsp error = delete_branch(repo, delref);
2766 4e759de4 2019-06-26 stsp else {
2767 4e759de4 2019-06-26 stsp const char *base_branch;
2768 4e759de4 2019-06-26 stsp if (argc == 1) {
2769 4e759de4 2019-06-26 stsp base_branch = worktree ?
2770 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
2771 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
2772 4e759de4 2019-06-26 stsp } else
2773 4e759de4 2019-06-26 stsp base_branch = argv[1];
2774 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
2775 4e759de4 2019-06-26 stsp }
2776 4e759de4 2019-06-26 stsp done:
2777 d0eebce4 2019-03-11 stsp if (repo)
2778 d0eebce4 2019-03-11 stsp got_repo_close(repo);
2779 d0eebce4 2019-03-11 stsp if (worktree)
2780 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
2781 d0eebce4 2019-03-11 stsp free(cwd);
2782 d0eebce4 2019-03-11 stsp free(repo_path);
2783 d00136be 2019-03-26 stsp return error;
2784 d00136be 2019-03-26 stsp }
2785 d00136be 2019-03-26 stsp
2786 d00136be 2019-03-26 stsp __dead static void
2787 d00136be 2019-03-26 stsp usage_add(void)
2788 d00136be 2019-03-26 stsp {
2789 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
2790 d00136be 2019-03-26 stsp exit(1);
2791 d00136be 2019-03-26 stsp }
2792 d00136be 2019-03-26 stsp
2793 d00136be 2019-03-26 stsp static const struct got_error *
2794 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
2795 d00136be 2019-03-26 stsp {
2796 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
2797 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
2798 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
2799 1dd54920 2019-05-11 stsp char *cwd = NULL;
2800 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
2801 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
2802 723c305c 2019-05-11 jcs int ch, x;
2803 1dd54920 2019-05-11 stsp
2804 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
2805 d00136be 2019-03-26 stsp
2806 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2807 d00136be 2019-03-26 stsp switch (ch) {
2808 d00136be 2019-03-26 stsp default:
2809 d00136be 2019-03-26 stsp usage_add();
2810 d00136be 2019-03-26 stsp /* NOTREACHED */
2811 d00136be 2019-03-26 stsp }
2812 d00136be 2019-03-26 stsp }
2813 d00136be 2019-03-26 stsp
2814 d00136be 2019-03-26 stsp argc -= optind;
2815 d00136be 2019-03-26 stsp argv += optind;
2816 d00136be 2019-03-26 stsp
2817 43012d58 2019-07-14 stsp #ifndef PROFILE
2818 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2819 43012d58 2019-07-14 stsp NULL) == -1)
2820 43012d58 2019-07-14 stsp err(1, "pledge");
2821 43012d58 2019-07-14 stsp #endif
2822 723c305c 2019-05-11 jcs if (argc < 1)
2823 d00136be 2019-03-26 stsp usage_add();
2824 d00136be 2019-03-26 stsp
2825 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
2826 d00136be 2019-03-26 stsp if (cwd == NULL) {
2827 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2828 d00136be 2019-03-26 stsp goto done;
2829 d00136be 2019-03-26 stsp }
2830 723c305c 2019-05-11 jcs
2831 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2832 d00136be 2019-03-26 stsp if (error)
2833 d00136be 2019-03-26 stsp goto done;
2834 d00136be 2019-03-26 stsp
2835 031a5338 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2836 031a5338 2019-03-26 stsp if (error != NULL)
2837 031a5338 2019-03-26 stsp goto done;
2838 031a5338 2019-03-26 stsp
2839 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2840 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2841 d00136be 2019-03-26 stsp if (error)
2842 d00136be 2019-03-26 stsp goto done;
2843 d00136be 2019-03-26 stsp
2844 723c305c 2019-05-11 jcs for (x = 0; x < argc; x++) {
2845 1dd54920 2019-05-11 stsp char *path = realpath(argv[x], NULL);
2846 723c305c 2019-05-11 jcs if (path == NULL) {
2847 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[x]);
2848 723c305c 2019-05-11 jcs goto done;
2849 723c305c 2019-05-11 jcs }
2850 723c305c 2019-05-11 jcs
2851 723c305c 2019-05-11 jcs got_path_strip_trailing_slashes(path);
2852 1dd54920 2019-05-11 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2853 1dd54920 2019-05-11 stsp if (error) {
2854 1dd54920 2019-05-11 stsp free(path);
2855 723c305c 2019-05-11 jcs goto done;
2856 1dd54920 2019-05-11 stsp }
2857 723c305c 2019-05-11 jcs }
2858 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
2859 1dd54920 2019-05-11 stsp NULL, repo);
2860 d00136be 2019-03-26 stsp done:
2861 031a5338 2019-03-26 stsp if (repo)
2862 031a5338 2019-03-26 stsp got_repo_close(repo);
2863 d00136be 2019-03-26 stsp if (worktree)
2864 d00136be 2019-03-26 stsp got_worktree_close(worktree);
2865 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
2866 1dd54920 2019-05-11 stsp free((char *)pe->path);
2867 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
2868 2ec1f75b 2019-03-26 stsp free(cwd);
2869 2ec1f75b 2019-03-26 stsp return error;
2870 2ec1f75b 2019-03-26 stsp }
2871 2ec1f75b 2019-03-26 stsp
2872 2ec1f75b 2019-03-26 stsp __dead static void
2873 648e4ef7 2019-07-09 stsp usage_remove(void)
2874 2ec1f75b 2019-03-26 stsp {
2875 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
2876 2ec1f75b 2019-03-26 stsp exit(1);
2877 2ec1f75b 2019-03-26 stsp }
2878 2ec1f75b 2019-03-26 stsp
2879 2ec1f75b 2019-03-26 stsp static const struct got_error *
2880 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
2881 2ec1f75b 2019-03-26 stsp {
2882 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
2883 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
2884 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
2885 17ed4618 2019-06-02 stsp char *cwd = NULL;
2886 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
2887 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
2888 17ed4618 2019-06-02 stsp int ch, i, delete_local_mods = 0;
2889 2ec1f75b 2019-03-26 stsp
2890 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
2891 17ed4618 2019-06-02 stsp
2892 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
2893 2ec1f75b 2019-03-26 stsp switch (ch) {
2894 2ec1f75b 2019-03-26 stsp case 'f':
2895 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
2896 2ec1f75b 2019-03-26 stsp break;
2897 2ec1f75b 2019-03-26 stsp default:
2898 2ec1f75b 2019-03-26 stsp usage_add();
2899 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
2900 2ec1f75b 2019-03-26 stsp }
2901 2ec1f75b 2019-03-26 stsp }
2902 2ec1f75b 2019-03-26 stsp
2903 2ec1f75b 2019-03-26 stsp argc -= optind;
2904 2ec1f75b 2019-03-26 stsp argv += optind;
2905 2ec1f75b 2019-03-26 stsp
2906 43012d58 2019-07-14 stsp #ifndef PROFILE
2907 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2908 43012d58 2019-07-14 stsp NULL) == -1)
2909 43012d58 2019-07-14 stsp err(1, "pledge");
2910 43012d58 2019-07-14 stsp #endif
2911 17ed4618 2019-06-02 stsp if (argc < 1)
2912 648e4ef7 2019-07-09 stsp usage_remove();
2913 2ec1f75b 2019-03-26 stsp
2914 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
2915 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
2916 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2917 2ec1f75b 2019-03-26 stsp goto done;
2918 2ec1f75b 2019-03-26 stsp }
2919 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
2920 2ec1f75b 2019-03-26 stsp if (error)
2921 2ec1f75b 2019-03-26 stsp goto done;
2922 2ec1f75b 2019-03-26 stsp
2923 2ec1f75b 2019-03-26 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
2924 2af4a041 2019-05-11 jcs if (error)
2925 2ec1f75b 2019-03-26 stsp goto done;
2926 2ec1f75b 2019-03-26 stsp
2927 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2928 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
2929 2ec1f75b 2019-03-26 stsp if (error)
2930 2ec1f75b 2019-03-26 stsp goto done;
2931 2ec1f75b 2019-03-26 stsp
2932 17ed4618 2019-06-02 stsp for (i = 0; i < argc; i++) {
2933 17ed4618 2019-06-02 stsp char *path = realpath(argv[i], NULL);
2934 17ed4618 2019-06-02 stsp if (path == NULL) {
2935 17ed4618 2019-06-02 stsp error = got_error_from_errno2("realpath", argv[i]);
2936 17ed4618 2019-06-02 stsp goto done;
2937 17ed4618 2019-06-02 stsp }
2938 17ed4618 2019-06-02 stsp
2939 17ed4618 2019-06-02 stsp got_path_strip_trailing_slashes(path);
2940 17ed4618 2019-06-02 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
2941 17ed4618 2019-06-02 stsp if (error) {
2942 17ed4618 2019-06-02 stsp free(path);
2943 17ed4618 2019-06-02 stsp goto done;
2944 17ed4618 2019-06-02 stsp }
2945 17ed4618 2019-06-02 stsp }
2946 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
2947 17ed4618 2019-06-02 stsp delete_local_mods, print_status, NULL, repo);
2948 a129376b 2019-03-28 stsp if (error)
2949 a129376b 2019-03-28 stsp goto done;
2950 a129376b 2019-03-28 stsp done:
2951 a129376b 2019-03-28 stsp if (repo)
2952 a129376b 2019-03-28 stsp got_repo_close(repo);
2953 a129376b 2019-03-28 stsp if (worktree)
2954 a129376b 2019-03-28 stsp got_worktree_close(worktree);
2955 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
2956 17ed4618 2019-06-02 stsp free((char *)pe->path);
2957 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
2958 a129376b 2019-03-28 stsp free(cwd);
2959 a129376b 2019-03-28 stsp return error;
2960 a129376b 2019-03-28 stsp }
2961 a129376b 2019-03-28 stsp
2962 a129376b 2019-03-28 stsp __dead static void
2963 a129376b 2019-03-28 stsp usage_revert(void)
2964 a129376b 2019-03-28 stsp {
2965 e20a8b6f 2019-06-04 stsp fprintf(stderr, "usage: %s revert file-path ...\n", getprogname());
2966 a129376b 2019-03-28 stsp exit(1);
2967 a129376b 2019-03-28 stsp }
2968 a129376b 2019-03-28 stsp
2969 1ee397ad 2019-07-12 stsp static const struct got_error *
2970 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
2971 a129376b 2019-03-28 stsp {
2972 a129376b 2019-03-28 stsp while (path[0] == '/')
2973 a129376b 2019-03-28 stsp path++;
2974 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
2975 1ee397ad 2019-07-12 stsp return NULL;
2976 a129376b 2019-03-28 stsp }
2977 a129376b 2019-03-28 stsp
2978 a129376b 2019-03-28 stsp static const struct got_error *
2979 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
2980 a129376b 2019-03-28 stsp {
2981 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
2982 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
2983 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
2984 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
2985 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
2986 e20a8b6f 2019-06-04 stsp struct got_pathlist_entry *pe;
2987 e20a8b6f 2019-06-04 stsp int ch, i;
2988 a129376b 2019-03-28 stsp
2989 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
2990 e20a8b6f 2019-06-04 stsp
2991 a129376b 2019-03-28 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2992 a129376b 2019-03-28 stsp switch (ch) {
2993 a129376b 2019-03-28 stsp default:
2994 a129376b 2019-03-28 stsp usage_revert();
2995 a129376b 2019-03-28 stsp /* NOTREACHED */
2996 a129376b 2019-03-28 stsp }
2997 a129376b 2019-03-28 stsp }
2998 a129376b 2019-03-28 stsp
2999 a129376b 2019-03-28 stsp argc -= optind;
3000 a129376b 2019-03-28 stsp argv += optind;
3001 a129376b 2019-03-28 stsp
3002 43012d58 2019-07-14 stsp #ifndef PROFILE
3003 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3004 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3005 43012d58 2019-07-14 stsp err(1, "pledge");
3006 43012d58 2019-07-14 stsp #endif
3007 e20a8b6f 2019-06-04 stsp if (argc < 1)
3008 a129376b 2019-03-28 stsp usage_revert();
3009 a129376b 2019-03-28 stsp
3010 e20a8b6f 2019-06-04 stsp for (i = 0; i < argc; i++) {
3011 e20a8b6f 2019-06-04 stsp char *path = realpath(argv[i], NULL);
3012 e20a8b6f 2019-06-04 stsp if (path == NULL) {
3013 e20a8b6f 2019-06-04 stsp error = got_error_from_errno2("realpath", argv[i]);
3014 e20a8b6f 2019-06-04 stsp goto done;
3015 e20a8b6f 2019-06-04 stsp }
3016 e20a8b6f 2019-06-04 stsp
3017 e20a8b6f 2019-06-04 stsp got_path_strip_trailing_slashes(path);
3018 e20a8b6f 2019-06-04 stsp error = got_pathlist_insert(&pe, &paths, path, NULL);
3019 e20a8b6f 2019-06-04 stsp if (error) {
3020 e20a8b6f 2019-06-04 stsp free(path);
3021 e20a8b6f 2019-06-04 stsp goto done;
3022 e20a8b6f 2019-06-04 stsp }
3023 a129376b 2019-03-28 stsp }
3024 a129376b 2019-03-28 stsp
3025 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
3026 a129376b 2019-03-28 stsp if (cwd == NULL) {
3027 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3028 a129376b 2019-03-28 stsp goto done;
3029 a129376b 2019-03-28 stsp }
3030 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
3031 2ec1f75b 2019-03-26 stsp if (error)
3032 2ec1f75b 2019-03-26 stsp goto done;
3033 a129376b 2019-03-28 stsp
3034 a129376b 2019-03-28 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3035 a129376b 2019-03-28 stsp if (error != NULL)
3036 a129376b 2019-03-28 stsp goto done;
3037 a129376b 2019-03-28 stsp
3038 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3039 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
3040 a129376b 2019-03-28 stsp if (error)
3041 a129376b 2019-03-28 stsp goto done;
3042 a129376b 2019-03-28 stsp
3043 e20a8b6f 2019-06-04 stsp error = got_worktree_revert(worktree, &paths,
3044 a129376b 2019-03-28 stsp revert_progress, NULL, repo);
3045 a129376b 2019-03-28 stsp if (error)
3046 a129376b 2019-03-28 stsp goto done;
3047 2ec1f75b 2019-03-26 stsp done:
3048 2ec1f75b 2019-03-26 stsp if (repo)
3049 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
3050 2ec1f75b 2019-03-26 stsp if (worktree)
3051 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
3052 2ec1f75b 2019-03-26 stsp free(path);
3053 d00136be 2019-03-26 stsp free(cwd);
3054 6bad629b 2019-02-04 stsp return error;
3055 c4296144 2019-05-09 stsp }
3056 c4296144 2019-05-09 stsp
3057 c4296144 2019-05-09 stsp __dead static void
3058 c4296144 2019-05-09 stsp usage_commit(void)
3059 c4296144 2019-05-09 stsp {
3060 c6fc0acd 2019-05-09 stsp fprintf(stderr, "usage: %s commit [-m msg] file-path\n", getprogname());
3061 c4296144 2019-05-09 stsp exit(1);
3062 33ad4cbe 2019-05-12 jcs }
3063 33ad4cbe 2019-05-12 jcs
3064 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
3065 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
3066 e2ba3d07 2019-05-13 stsp const char *editor;
3067 e0870e44 2019-05-13 stsp const char *worktree_path;
3068 76d98825 2019-06-03 stsp const char *branch_name;
3069 314a6357 2019-05-13 stsp const char *repo_path;
3070 e0870e44 2019-05-13 stsp char *logmsg_path;
3071 e2ba3d07 2019-05-13 stsp
3072 e2ba3d07 2019-05-13 stsp };
3073 e0870e44 2019-05-13 stsp
3074 33ad4cbe 2019-05-12 jcs static const struct got_error *
3075 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
3076 33ad4cbe 2019-05-12 jcs void *arg)
3077 33ad4cbe 2019-05-12 jcs {
3078 76d98825 2019-06-03 stsp char *initial_content = NULL;
3079 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
3080 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
3081 e0870e44 2019-05-13 stsp char *template = NULL;
3082 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
3083 3ce1b845 2019-07-15 stsp int fd;
3084 33ad4cbe 2019-05-12 jcs size_t len;
3085 33ad4cbe 2019-05-12 jcs
3086 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
3087 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
3088 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
3089 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
3090 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
3091 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
3092 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
3093 33ad4cbe 2019-05-12 jcs return NULL;
3094 33ad4cbe 2019-05-12 jcs }
3095 33ad4cbe 2019-05-12 jcs
3096 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
3097 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
3098 76d98825 2019-06-03 stsp
3099 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
3100 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
3101 76d98825 2019-06-03 stsp a->branch_name) == -1)
3102 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
3103 e0870e44 2019-05-13 stsp
3104 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
3105 793c30b5 2019-05-13 stsp if (err)
3106 e0870e44 2019-05-13 stsp goto done;
3107 33ad4cbe 2019-05-12 jcs
3108 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
3109 33ad4cbe 2019-05-12 jcs
3110 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
3111 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
3112 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
3113 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
3114 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
3115 33ad4cbe 2019-05-12 jcs }
3116 33ad4cbe 2019-05-12 jcs close(fd);
3117 33ad4cbe 2019-05-12 jcs
3118 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
3119 33ad4cbe 2019-05-12 jcs done:
3120 3ce1b845 2019-07-15 stsp unlink(a->logmsg_path);
3121 3ce1b845 2019-07-15 stsp free(a->logmsg_path);
3122 76d98825 2019-06-03 stsp free(initial_content);
3123 e0870e44 2019-05-13 stsp free(template);
3124 314a6357 2019-05-13 stsp
3125 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
3126 3ce1b845 2019-07-15 stsp if (err == NULL) {
3127 314a6357 2019-05-13 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path, 0);
3128 3ce1b845 2019-07-15 stsp if (err) {
3129 3ce1b845 2019-07-15 stsp free(*logmsg);
3130 3ce1b845 2019-07-15 stsp *logmsg = NULL;
3131 3ce1b845 2019-07-15 stsp }
3132 3ce1b845 2019-07-15 stsp }
3133 33ad4cbe 2019-05-12 jcs return err;
3134 6bad629b 2019-02-04 stsp }
3135 c4296144 2019-05-09 stsp
3136 c4296144 2019-05-09 stsp static const struct got_error *
3137 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
3138 c4296144 2019-05-09 stsp {
3139 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
3140 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
3141 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
3142 c4296144 2019-05-09 stsp char *cwd = NULL, *path = NULL, *id_str = NULL;
3143 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
3144 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
3145 35bd8fed 2019-05-09 stsp const char *got_author = getenv("GOT_AUTHOR");
3146 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg cl_arg;
3147 e2ba3d07 2019-05-13 stsp char *editor = NULL;
3148 7d5807f4 2019-07-11 stsp int ch, rebase_in_progress;
3149 c4296144 2019-05-09 stsp
3150 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
3151 c4296144 2019-05-09 stsp switch (ch) {
3152 c4296144 2019-05-09 stsp case 'm':
3153 c4296144 2019-05-09 stsp logmsg = optarg;
3154 c4296144 2019-05-09 stsp break;
3155 c4296144 2019-05-09 stsp default:
3156 c4296144 2019-05-09 stsp usage_commit();
3157 c4296144 2019-05-09 stsp /* NOTREACHED */
3158 c4296144 2019-05-09 stsp }
3159 c4296144 2019-05-09 stsp }
3160 c4296144 2019-05-09 stsp
3161 c4296144 2019-05-09 stsp argc -= optind;
3162 c4296144 2019-05-09 stsp argv += optind;
3163 c4296144 2019-05-09 stsp
3164 43012d58 2019-07-14 stsp #ifndef PROFILE
3165 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3166 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3167 43012d58 2019-07-14 stsp err(1, "pledge");
3168 43012d58 2019-07-14 stsp #endif
3169 c4296144 2019-05-09 stsp if (argc == 1) {
3170 c4296144 2019-05-09 stsp path = realpath(argv[0], NULL);
3171 c4296144 2019-05-09 stsp if (path == NULL) {
3172 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
3173 c4296144 2019-05-09 stsp goto done;
3174 c4296144 2019-05-09 stsp }
3175 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(path);
3176 c4296144 2019-05-09 stsp } else if (argc != 0)
3177 c4296144 2019-05-09 stsp usage_commit();
3178 c4296144 2019-05-09 stsp
3179 35bd8fed 2019-05-09 stsp if (got_author == NULL) {
3180 35bd8fed 2019-05-09 stsp /* TODO: Look current user up in password database */
3181 35bd8fed 2019-05-09 stsp error = got_error(GOT_ERR_COMMIT_NO_AUTHOR);
3182 35bd8fed 2019-05-09 stsp goto done;
3183 35bd8fed 2019-05-09 stsp }
3184 c4296144 2019-05-09 stsp
3185 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
3186 c4296144 2019-05-09 stsp if (cwd == NULL) {
3187 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3188 c4296144 2019-05-09 stsp goto done;
3189 c4296144 2019-05-09 stsp }
3190 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
3191 7d5807f4 2019-07-11 stsp if (error)
3192 7d5807f4 2019-07-11 stsp goto done;
3193 7d5807f4 2019-07-11 stsp
3194 7d5807f4 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3195 c4296144 2019-05-09 stsp if (error)
3196 7d5807f4 2019-07-11 stsp goto done;
3197 7d5807f4 2019-07-11 stsp if (rebase_in_progress) {
3198 7d5807f4 2019-07-11 stsp error = got_error(GOT_ERR_REBASING);
3199 c4296144 2019-05-09 stsp goto done;
3200 7d5807f4 2019-07-11 stsp }
3201 c4296144 2019-05-09 stsp
3202 c4296144 2019-05-09 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3203 c4296144 2019-05-09 stsp if (error != NULL)
3204 c4296144 2019-05-09 stsp goto done;
3205 c4296144 2019-05-09 stsp
3206 314a6357 2019-05-13 stsp /*
3207 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
3208 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
3209 314a6357 2019-05-13 stsp */
3210 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
3211 314a6357 2019-05-13 stsp error = get_editor(&editor);
3212 314a6357 2019-05-13 stsp else
3213 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3214 314a6357 2019-05-13 stsp got_worktree_get_root_path(worktree), 0);
3215 c4296144 2019-05-09 stsp if (error)
3216 c4296144 2019-05-09 stsp goto done;
3217 c4296144 2019-05-09 stsp
3218 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
3219 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
3220 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
3221 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
3222 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "refs/", 5) == 0)
3223 76d98825 2019-06-03 stsp cl_arg.branch_name += 5;
3224 76d98825 2019-06-03 stsp if (strncmp(cl_arg.branch_name, "heads/", 6) == 0)
3225 76d98825 2019-06-03 stsp cl_arg.branch_name += 6;
3226 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
3227 e0870e44 2019-05-13 stsp cl_arg.logmsg_path = NULL;
3228 35bd8fed 2019-05-09 stsp error = got_worktree_commit(&id, worktree, path, got_author, NULL,
3229 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
3230 e0870e44 2019-05-13 stsp if (error) {
3231 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3232 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
3233 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
3234 c4296144 2019-05-09 stsp goto done;
3235 e0870e44 2019-05-13 stsp }
3236 c4296144 2019-05-09 stsp
3237 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
3238 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
3239 e0870e44 2019-05-13 stsp
3240 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
3241 c4296144 2019-05-09 stsp if (error)
3242 c4296144 2019-05-09 stsp goto done;
3243 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
3244 c4296144 2019-05-09 stsp done:
3245 c4296144 2019-05-09 stsp if (repo)
3246 c4296144 2019-05-09 stsp got_repo_close(repo);
3247 c4296144 2019-05-09 stsp if (worktree)
3248 c4296144 2019-05-09 stsp got_worktree_close(worktree);
3249 c4296144 2019-05-09 stsp free(path);
3250 c4296144 2019-05-09 stsp free(cwd);
3251 c4296144 2019-05-09 stsp free(id_str);
3252 e2ba3d07 2019-05-13 stsp free(editor);
3253 234035bc 2019-06-01 stsp return error;
3254 234035bc 2019-06-01 stsp }
3255 234035bc 2019-06-01 stsp
3256 234035bc 2019-06-01 stsp __dead static void
3257 234035bc 2019-06-01 stsp usage_cherrypick(void)
3258 234035bc 2019-06-01 stsp {
3259 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
3260 234035bc 2019-06-01 stsp exit(1);
3261 234035bc 2019-06-01 stsp }
3262 234035bc 2019-06-01 stsp
3263 234035bc 2019-06-01 stsp static const struct got_error *
3264 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
3265 234035bc 2019-06-01 stsp {
3266 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
3267 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
3268 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
3269 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
3270 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
3271 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
3272 234035bc 2019-06-01 stsp struct got_object_qid *pid;
3273 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
3274 234035bc 2019-06-01 stsp int ch, did_something = 0;
3275 234035bc 2019-06-01 stsp
3276 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3277 234035bc 2019-06-01 stsp switch (ch) {
3278 234035bc 2019-06-01 stsp default:
3279 234035bc 2019-06-01 stsp usage_cherrypick();
3280 234035bc 2019-06-01 stsp /* NOTREACHED */
3281 234035bc 2019-06-01 stsp }
3282 234035bc 2019-06-01 stsp }
3283 234035bc 2019-06-01 stsp
3284 234035bc 2019-06-01 stsp argc -= optind;
3285 234035bc 2019-06-01 stsp argv += optind;
3286 234035bc 2019-06-01 stsp
3287 43012d58 2019-07-14 stsp #ifndef PROFILE
3288 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3289 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3290 43012d58 2019-07-14 stsp err(1, "pledge");
3291 43012d58 2019-07-14 stsp #endif
3292 234035bc 2019-06-01 stsp if (argc != 1)
3293 234035bc 2019-06-01 stsp usage_cherrypick();
3294 234035bc 2019-06-01 stsp
3295 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
3296 234035bc 2019-06-01 stsp if (cwd == NULL) {
3297 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
3298 234035bc 2019-06-01 stsp goto done;
3299 234035bc 2019-06-01 stsp }
3300 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
3301 234035bc 2019-06-01 stsp if (error)
3302 234035bc 2019-06-01 stsp goto done;
3303 234035bc 2019-06-01 stsp
3304 234035bc 2019-06-01 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3305 234035bc 2019-06-01 stsp if (error != NULL)
3306 234035bc 2019-06-01 stsp goto done;
3307 234035bc 2019-06-01 stsp
3308 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3309 234035bc 2019-06-01 stsp got_worktree_get_root_path(worktree), 0);
3310 234035bc 2019-06-01 stsp if (error)
3311 234035bc 2019-06-01 stsp goto done;
3312 234035bc 2019-06-01 stsp
3313 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3314 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3315 234035bc 2019-06-01 stsp if (error != NULL) {
3316 234035bc 2019-06-01 stsp struct got_reference *ref;
3317 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3318 234035bc 2019-06-01 stsp goto done;
3319 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3320 234035bc 2019-06-01 stsp if (error != NULL)
3321 234035bc 2019-06-01 stsp goto done;
3322 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
3323 234035bc 2019-06-01 stsp got_ref_close(ref);
3324 234035bc 2019-06-01 stsp if (error != NULL)
3325 234035bc 2019-06-01 stsp goto done;
3326 234035bc 2019-06-01 stsp }
3327 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
3328 234035bc 2019-06-01 stsp if (error)
3329 234035bc 2019-06-01 stsp goto done;
3330 234035bc 2019-06-01 stsp
3331 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
3332 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
3333 234035bc 2019-06-01 stsp if (error != NULL)
3334 234035bc 2019-06-01 stsp goto done;
3335 234035bc 2019-06-01 stsp
3336 234035bc 2019-06-01 stsp error = check_same_branch(commit_id, head_ref, repo);
3337 234035bc 2019-06-01 stsp if (error) {
3338 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
3339 234035bc 2019-06-01 stsp goto done;
3340 234035bc 2019-06-01 stsp error = NULL;
3341 234035bc 2019-06-01 stsp } else {
3342 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
3343 234035bc 2019-06-01 stsp goto done;
3344 234035bc 2019-06-01 stsp }
3345 234035bc 2019-06-01 stsp
3346 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3347 234035bc 2019-06-01 stsp if (error)
3348 234035bc 2019-06-01 stsp goto done;
3349 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3350 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
3351 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
3352 03415a1a 2019-06-02 stsp NULL);
3353 234035bc 2019-06-01 stsp if (error != NULL)
3354 234035bc 2019-06-01 stsp goto done;
3355 234035bc 2019-06-01 stsp
3356 234035bc 2019-06-01 stsp if (did_something)
3357 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
3358 234035bc 2019-06-01 stsp done:
3359 234035bc 2019-06-01 stsp if (commit)
3360 234035bc 2019-06-01 stsp got_object_commit_close(commit);
3361 234035bc 2019-06-01 stsp free(commit_id_str);
3362 234035bc 2019-06-01 stsp if (head_ref)
3363 234035bc 2019-06-01 stsp got_ref_close(head_ref);
3364 234035bc 2019-06-01 stsp if (worktree)
3365 234035bc 2019-06-01 stsp got_worktree_close(worktree);
3366 234035bc 2019-06-01 stsp if (repo)
3367 234035bc 2019-06-01 stsp got_repo_close(repo);
3368 c4296144 2019-05-09 stsp return error;
3369 c4296144 2019-05-09 stsp }
3370 5ef14e63 2019-06-02 stsp
3371 5ef14e63 2019-06-02 stsp __dead static void
3372 5ef14e63 2019-06-02 stsp usage_backout(void)
3373 5ef14e63 2019-06-02 stsp {
3374 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
3375 5ef14e63 2019-06-02 stsp exit(1);
3376 5ef14e63 2019-06-02 stsp }
3377 5ef14e63 2019-06-02 stsp
3378 5ef14e63 2019-06-02 stsp static const struct got_error *
3379 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
3380 5ef14e63 2019-06-02 stsp {
3381 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
3382 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
3383 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
3384 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
3385 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
3386 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
3387 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
3388 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
3389 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
3390 5ef14e63 2019-06-02 stsp
3391 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3392 5ef14e63 2019-06-02 stsp switch (ch) {
3393 5ef14e63 2019-06-02 stsp default:
3394 5ef14e63 2019-06-02 stsp usage_backout();
3395 5ef14e63 2019-06-02 stsp /* NOTREACHED */
3396 5ef14e63 2019-06-02 stsp }
3397 5ef14e63 2019-06-02 stsp }
3398 5ef14e63 2019-06-02 stsp
3399 5ef14e63 2019-06-02 stsp argc -= optind;
3400 5ef14e63 2019-06-02 stsp argv += optind;
3401 5ef14e63 2019-06-02 stsp
3402 43012d58 2019-07-14 stsp #ifndef PROFILE
3403 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3404 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3405 43012d58 2019-07-14 stsp err(1, "pledge");
3406 43012d58 2019-07-14 stsp #endif
3407 5ef14e63 2019-06-02 stsp if (argc != 1)
3408 5ef14e63 2019-06-02 stsp usage_backout();
3409 5ef14e63 2019-06-02 stsp
3410 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
3411 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
3412 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
3413 5ef14e63 2019-06-02 stsp goto done;
3414 5ef14e63 2019-06-02 stsp }
3415 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
3416 5ef14e63 2019-06-02 stsp if (error)
3417 5ef14e63 2019-06-02 stsp goto done;
3418 5ef14e63 2019-06-02 stsp
3419 5ef14e63 2019-06-02 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3420 5ef14e63 2019-06-02 stsp if (error != NULL)
3421 5ef14e63 2019-06-02 stsp goto done;
3422 5ef14e63 2019-06-02 stsp
3423 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3424 5ef14e63 2019-06-02 stsp got_worktree_get_root_path(worktree), 0);
3425 5ef14e63 2019-06-02 stsp if (error)
3426 5ef14e63 2019-06-02 stsp goto done;
3427 5ef14e63 2019-06-02 stsp
3428 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
3429 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
3430 5ef14e63 2019-06-02 stsp if (error != NULL) {
3431 5ef14e63 2019-06-02 stsp struct got_reference *ref;
3432 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
3433 5ef14e63 2019-06-02 stsp goto done;
3434 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
3435 5ef14e63 2019-06-02 stsp if (error != NULL)
3436 5ef14e63 2019-06-02 stsp goto done;
3437 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
3438 5ef14e63 2019-06-02 stsp got_ref_close(ref);
3439 5ef14e63 2019-06-02 stsp if (error != NULL)
3440 5ef14e63 2019-06-02 stsp goto done;
3441 5ef14e63 2019-06-02 stsp }
3442 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
3443 5ef14e63 2019-06-02 stsp if (error)
3444 5ef14e63 2019-06-02 stsp goto done;
3445 5ef14e63 2019-06-02 stsp
3446 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
3447 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
3448 5ef14e63 2019-06-02 stsp if (error != NULL)
3449 5ef14e63 2019-06-02 stsp goto done;
3450 5ef14e63 2019-06-02 stsp
3451 5ef14e63 2019-06-02 stsp error = check_same_branch(commit_id, head_ref, repo);
3452 5ef14e63 2019-06-02 stsp if (error)
3453 5ef14e63 2019-06-02 stsp goto done;
3454 5ef14e63 2019-06-02 stsp
3455 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3456 5ef14e63 2019-06-02 stsp if (error)
3457 5ef14e63 2019-06-02 stsp goto done;
3458 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
3459 5ef14e63 2019-06-02 stsp if (pid == NULL) {
3460 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
3461 5ef14e63 2019-06-02 stsp goto done;
3462 5ef14e63 2019-06-02 stsp }
3463 5ef14e63 2019-06-02 stsp
3464 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
3465 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
3466 5ef14e63 2019-06-02 stsp if (error != NULL)
3467 5ef14e63 2019-06-02 stsp goto done;
3468 5ef14e63 2019-06-02 stsp
3469 5ef14e63 2019-06-02 stsp if (did_something)
3470 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
3471 5ef14e63 2019-06-02 stsp done:
3472 5ef14e63 2019-06-02 stsp if (commit)
3473 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
3474 5ef14e63 2019-06-02 stsp free(commit_id_str);
3475 5ef14e63 2019-06-02 stsp if (head_ref)
3476 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
3477 818c7501 2019-07-11 stsp if (worktree)
3478 818c7501 2019-07-11 stsp got_worktree_close(worktree);
3479 818c7501 2019-07-11 stsp if (repo)
3480 818c7501 2019-07-11 stsp got_repo_close(repo);
3481 818c7501 2019-07-11 stsp return error;
3482 818c7501 2019-07-11 stsp }
3483 818c7501 2019-07-11 stsp
3484 818c7501 2019-07-11 stsp __dead static void
3485 818c7501 2019-07-11 stsp usage_rebase(void)
3486 818c7501 2019-07-11 stsp {
3487 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
3488 af54c8f8 2019-07-11 stsp getprogname());
3489 818c7501 2019-07-11 stsp exit(1);
3490 818c7501 2019-07-11 stsp }
3491 818c7501 2019-07-11 stsp
3492 818c7501 2019-07-11 stsp static const struct got_error *
3493 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
3494 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
3495 818c7501 2019-07-11 stsp {
3496 818c7501 2019-07-11 stsp const struct got_error *err;
3497 818c7501 2019-07-11 stsp char *old_id_str = NULL, *new_id_str = NULL;
3498 818c7501 2019-07-11 stsp char *logmsg0 = NULL, *logmsg, *nl;
3499 818c7501 2019-07-11 stsp size_t len;
3500 818c7501 2019-07-11 stsp
3501 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
3502 818c7501 2019-07-11 stsp if (err)
3503 818c7501 2019-07-11 stsp goto done;
3504 818c7501 2019-07-11 stsp
3505 ff0d2220 2019-07-11 stsp if (new_id) {
3506 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
3507 ff0d2220 2019-07-11 stsp if (err)
3508 ff0d2220 2019-07-11 stsp goto done;
3509 ff0d2220 2019-07-11 stsp }
3510 818c7501 2019-07-11 stsp
3511 818c7501 2019-07-11 stsp logmsg0 = strdup(got_object_commit_get_logmsg(commit));
3512 818c7501 2019-07-11 stsp if (logmsg0 == NULL) {
3513 818c7501 2019-07-11 stsp err = got_error_from_errno("strdup");
3514 818c7501 2019-07-11 stsp goto done;
3515 818c7501 2019-07-11 stsp }
3516 818c7501 2019-07-11 stsp logmsg = logmsg0;
3517 818c7501 2019-07-11 stsp
3518 10796104 2019-07-11 stsp while (isspace((unsigned char)logmsg[0]))
3519 818c7501 2019-07-11 stsp logmsg++;
3520 818c7501 2019-07-11 stsp
3521 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
3522 ff0d2220 2019-07-11 stsp if (new_id_str)
3523 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
3524 818c7501 2019-07-11 stsp len = strlen(logmsg);
3525 818c7501 2019-07-11 stsp if (len > 42)
3526 818c7501 2019-07-11 stsp len = 42;
3527 818c7501 2019-07-11 stsp logmsg[len] = '\0';
3528 818c7501 2019-07-11 stsp nl = strchr(logmsg, '\n');
3529 818c7501 2019-07-11 stsp if (nl)
3530 818c7501 2019-07-11 stsp *nl = '\0';
3531 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
3532 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
3533 818c7501 2019-07-11 stsp done:
3534 818c7501 2019-07-11 stsp free(old_id_str);
3535 818c7501 2019-07-11 stsp free(new_id_str);
3536 818c7501 2019-07-11 stsp free(logmsg0);
3537 818c7501 2019-07-11 stsp return err;
3538 818c7501 2019-07-11 stsp }
3539 818c7501 2019-07-11 stsp
3540 1ee397ad 2019-07-12 stsp static const struct got_error *
3541 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
3542 818c7501 2019-07-11 stsp {
3543 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
3544 818c7501 2019-07-11 stsp
3545 818c7501 2019-07-11 stsp while (path[0] == '/')
3546 818c7501 2019-07-11 stsp path++;
3547 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
3548 818c7501 2019-07-11 stsp
3549 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
3550 1ee397ad 2019-07-12 stsp return NULL;
3551 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
3552 818c7501 2019-07-11 stsp *rebase_status = status;
3553 1ee397ad 2019-07-12 stsp return NULL;
3554 818c7501 2019-07-11 stsp }
3555 818c7501 2019-07-11 stsp
3556 818c7501 2019-07-11 stsp static const struct got_error *
3557 818c7501 2019-07-11 stsp rebase_complete(struct got_worktree *worktree, struct got_reference *branch,
3558 818c7501 2019-07-11 stsp struct got_reference *new_base_branch, struct got_reference *tmp_branch,
3559 818c7501 2019-07-11 stsp struct got_repository *repo)
3560 818c7501 2019-07-11 stsp {
3561 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
3562 818c7501 2019-07-11 stsp return got_worktree_rebase_complete(worktree,
3563 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
3564 818c7501 2019-07-11 stsp }
3565 818c7501 2019-07-11 stsp
3566 818c7501 2019-07-11 stsp static const struct got_error *
3567 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
3568 01757395 2019-07-12 stsp struct got_worktree *worktree, struct got_reference *tmp_branch,
3569 ff0d2220 2019-07-11 stsp struct got_object_id *commit_id, struct got_repository *repo)
3570 ff0d2220 2019-07-11 stsp {
3571 ff0d2220 2019-07-11 stsp const struct got_error *error;
3572 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
3573 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
3574 ff0d2220 2019-07-11 stsp
3575 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3576 ff0d2220 2019-07-11 stsp if (error)
3577 ff0d2220 2019-07-11 stsp return error;
3578 ff0d2220 2019-07-11 stsp
3579 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
3580 01757395 2019-07-12 stsp worktree, tmp_branch, commit, commit_id, repo);
3581 ff0d2220 2019-07-11 stsp if (error) {
3582 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
3583 ff0d2220 2019-07-11 stsp goto done;
3584 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
3585 ff0d2220 2019-07-11 stsp } else {
3586 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
3587 ff0d2220 2019-07-11 stsp free(new_commit_id);
3588 ff0d2220 2019-07-11 stsp }
3589 ff0d2220 2019-07-11 stsp done:
3590 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
3591 ff0d2220 2019-07-11 stsp return error;
3592 64c6d990 2019-07-11 stsp }
3593 64c6d990 2019-07-11 stsp
3594 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
3595 64c6d990 2019-07-11 stsp const char *path_prefix;
3596 64c6d990 2019-07-11 stsp size_t len;
3597 64c6d990 2019-07-11 stsp };
3598 64c6d990 2019-07-11 stsp
3599 64c6d990 2019-07-11 stsp static const struct got_error *
3600 64c6d990 2019-07-11 stsp check_path_prefix(void *arg, struct got_blob_object *blob1,
3601 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
3602 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
3603 64c6d990 2019-07-11 stsp struct got_repository *repo)
3604 64c6d990 2019-07-11 stsp {
3605 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
3606 64c6d990 2019-07-11 stsp
3607 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
3608 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
3609 64c6d990 2019-07-11 stsp return got_error(GOT_ERR_REBASE_PATH);
3610 64c6d990 2019-07-11 stsp
3611 64c6d990 2019-07-11 stsp return NULL;
3612 64c6d990 2019-07-11 stsp }
3613 64c6d990 2019-07-11 stsp
3614 64c6d990 2019-07-11 stsp static const struct got_error *
3615 64c6d990 2019-07-11 stsp rebase_check_path_prefix(struct got_object_id *parent_id,
3616 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
3617 64c6d990 2019-07-11 stsp struct got_repository *repo)
3618 64c6d990 2019-07-11 stsp {
3619 64c6d990 2019-07-11 stsp const struct got_error *err;
3620 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
3621 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
3622 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
3623 64c6d990 2019-07-11 stsp
3624 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
3625 64c6d990 2019-07-11 stsp return NULL;
3626 64c6d990 2019-07-11 stsp
3627 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
3628 64c6d990 2019-07-11 stsp if (err)
3629 64c6d990 2019-07-11 stsp goto done;
3630 64c6d990 2019-07-11 stsp
3631 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
3632 64c6d990 2019-07-11 stsp if (err)
3633 64c6d990 2019-07-11 stsp goto done;
3634 64c6d990 2019-07-11 stsp
3635 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
3636 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
3637 64c6d990 2019-07-11 stsp if (err)
3638 64c6d990 2019-07-11 stsp goto done;
3639 64c6d990 2019-07-11 stsp
3640 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
3641 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
3642 64c6d990 2019-07-11 stsp if (err)
3643 64c6d990 2019-07-11 stsp goto done;
3644 64c6d990 2019-07-11 stsp
3645 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
3646 64c6d990 2019-07-11 stsp cpp_arg.len = strlen(path_prefix);
3647 64c6d990 2019-07-11 stsp err = got_diff_tree(tree1, tree2, "", "", repo, check_path_prefix,
3648 64c6d990 2019-07-11 stsp &cpp_arg);
3649 64c6d990 2019-07-11 stsp done:
3650 64c6d990 2019-07-11 stsp if (tree1)
3651 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
3652 64c6d990 2019-07-11 stsp if (tree2)
3653 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
3654 64c6d990 2019-07-11 stsp if (commit)
3655 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
3656 64c6d990 2019-07-11 stsp if (parent_commit)
3657 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
3658 64c6d990 2019-07-11 stsp return err;
3659 ff0d2220 2019-07-11 stsp }
3660 ff0d2220 2019-07-11 stsp
3661 ff0d2220 2019-07-11 stsp static const struct got_error *
3662 af61c510 2019-07-19 stsp collect_commits_to_rebase(struct got_object_id_queue *commits,
3663 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
3664 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
3665 af61c510 2019-07-19 stsp const char *path_prefix, struct got_repository *repo)
3666 af61c510 2019-07-19 stsp {
3667 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
3668 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
3669 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
3670 af61c510 2019-07-19 stsp struct got_object_qid *qid;
3671 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
3672 af61c510 2019-07-19 stsp
3673 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
3674 af61c510 2019-07-19 stsp if (err)
3675 af61c510 2019-07-19 stsp return err;
3676 af61c510 2019-07-19 stsp
3677 af61c510 2019-07-19 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo);
3678 af61c510 2019-07-19 stsp if (err)
3679 af61c510 2019-07-19 stsp goto done;
3680 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
3681 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
3682 af61c510 2019-07-19 stsp if (err) {
3683 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
3684 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
3685 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
3686 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
3687 af61c510 2019-07-19 stsp "been reached?!?");
3688 af61c510 2019-07-19 stsp goto done;
3689 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
3690 af61c510 2019-07-19 stsp goto done;
3691 af61c510 2019-07-19 stsp err = got_commit_graph_fetch_commits(graph, 1, repo);
3692 af61c510 2019-07-19 stsp if (err)
3693 af61c510 2019-07-19 stsp goto done;
3694 af61c510 2019-07-19 stsp } else {
3695 af61c510 2019-07-19 stsp err = rebase_check_path_prefix(parent_id, commit_id,
3696 af61c510 2019-07-19 stsp path_prefix, repo);
3697 af61c510 2019-07-19 stsp if (err)
3698 af61c510 2019-07-19 stsp goto done;
3699 af61c510 2019-07-19 stsp
3700 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
3701 af61c510 2019-07-19 stsp if (err)
3702 af61c510 2019-07-19 stsp goto done;
3703 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
3704 af61c510 2019-07-19 stsp commit_id = parent_id;
3705 af61c510 2019-07-19 stsp }
3706 af61c510 2019-07-19 stsp }
3707 af61c510 2019-07-19 stsp done:
3708 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
3709 af61c510 2019-07-19 stsp return err;
3710 af61c510 2019-07-19 stsp }
3711 af61c510 2019-07-19 stsp
3712 af61c510 2019-07-19 stsp static const struct got_error *
3713 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
3714 818c7501 2019-07-11 stsp {
3715 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
3716 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
3717 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
3718 818c7501 2019-07-11 stsp char *cwd = NULL;
3719 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
3720 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
3721 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
3722 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
3723 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
3724 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
3725 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
3726 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
3727 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
3728 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
3729 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
3730 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
3731 818c7501 2019-07-11 stsp
3732 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
3733 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
3734 818c7501 2019-07-11 stsp
3735 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
3736 818c7501 2019-07-11 stsp switch (ch) {
3737 818c7501 2019-07-11 stsp case 'a':
3738 818c7501 2019-07-11 stsp abort_rebase = 1;
3739 818c7501 2019-07-11 stsp break;
3740 818c7501 2019-07-11 stsp case 'c':
3741 818c7501 2019-07-11 stsp continue_rebase = 1;
3742 818c7501 2019-07-11 stsp break;
3743 818c7501 2019-07-11 stsp default:
3744 818c7501 2019-07-11 stsp usage_rebase();
3745 818c7501 2019-07-11 stsp /* NOTREACHED */
3746 818c7501 2019-07-11 stsp }
3747 818c7501 2019-07-11 stsp }
3748 818c7501 2019-07-11 stsp
3749 818c7501 2019-07-11 stsp argc -= optind;
3750 818c7501 2019-07-11 stsp argv += optind;
3751 818c7501 2019-07-11 stsp
3752 43012d58 2019-07-14 stsp #ifndef PROFILE
3753 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
3754 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
3755 43012d58 2019-07-14 stsp err(1, "pledge");
3756 43012d58 2019-07-14 stsp #endif
3757 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
3758 818c7501 2019-07-11 stsp usage_rebase();
3759 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
3760 818c7501 2019-07-11 stsp if (argc != 0)
3761 818c7501 2019-07-11 stsp usage_rebase();
3762 818c7501 2019-07-11 stsp } else if (argc != 1)
3763 818c7501 2019-07-11 stsp usage_rebase();
3764 818c7501 2019-07-11 stsp
3765 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
3766 818c7501 2019-07-11 stsp if (cwd == NULL) {
3767 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
3768 818c7501 2019-07-11 stsp goto done;
3769 818c7501 2019-07-11 stsp }
3770 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
3771 818c7501 2019-07-11 stsp if (error)
3772 818c7501 2019-07-11 stsp goto done;
3773 818c7501 2019-07-11 stsp
3774 818c7501 2019-07-11 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree));
3775 818c7501 2019-07-11 stsp if (error != NULL)
3776 818c7501 2019-07-11 stsp goto done;
3777 818c7501 2019-07-11 stsp
3778 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
3779 818c7501 2019-07-11 stsp got_worktree_get_root_path(worktree), 0);
3780 818c7501 2019-07-11 stsp if (error)
3781 818c7501 2019-07-11 stsp goto done;
3782 818c7501 2019-07-11 stsp
3783 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
3784 818c7501 2019-07-11 stsp if (error)
3785 818c7501 2019-07-11 stsp goto done;
3786 818c7501 2019-07-11 stsp
3787 818c7501 2019-07-11 stsp if (rebase_in_progress && abort_rebase) {
3788 818c7501 2019-07-11 stsp int did_something;
3789 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3790 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3791 818c7501 2019-07-11 stsp if (error)
3792 818c7501 2019-07-11 stsp goto done;
3793 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
3794 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
3795 818c7501 2019-07-11 stsp error = got_worktree_rebase_abort(worktree, repo,
3796 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
3797 818c7501 2019-07-11 stsp if (error)
3798 818c7501 2019-07-11 stsp goto done;
3799 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
3800 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
3801 818c7501 2019-07-11 stsp } else if (abort_rebase) {
3802 818c7501 2019-07-11 stsp error = got_error(GOT_ERR_NOT_REBASING);
3803 818c7501 2019-07-11 stsp goto done;
3804 818c7501 2019-07-11 stsp }
3805 818c7501 2019-07-11 stsp
3806 818c7501 2019-07-11 stsp if (continue_rebase) {
3807 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
3808 818c7501 2019-07-11 stsp &new_base_branch, &tmp_branch, &branch, worktree, repo);
3809 818c7501 2019-07-11 stsp if (error)
3810 818c7501 2019-07-11 stsp goto done;
3811 818c7501 2019-07-11 stsp
3812 01757395 2019-07-12 stsp error = rebase_commit(NULL, worktree, tmp_branch,
3813 01757395 2019-07-12 stsp resume_commit_id, repo);
3814 818c7501 2019-07-11 stsp if (error)
3815 818c7501 2019-07-11 stsp goto done;
3816 818c7501 2019-07-11 stsp
3817 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
3818 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
3819 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
3820 818c7501 2019-07-11 stsp goto done;
3821 818c7501 2019-07-11 stsp }
3822 818c7501 2019-07-11 stsp } else {
3823 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
3824 818c7501 2019-07-11 stsp if (error != NULL)
3825 818c7501 2019-07-11 stsp goto done;
3826 818c7501 2019-07-11 stsp
3827 818c7501 2019-07-11 stsp error = check_same_branch(
3828 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree), branch, repo);
3829 818c7501 2019-07-11 stsp if (error) {
3830 818c7501 2019-07-11 stsp if (error->code != GOT_ERR_ANCESTRY)
3831 818c7501 2019-07-11 stsp goto done;
3832 818c7501 2019-07-11 stsp error = NULL;
3833 818c7501 2019-07-11 stsp } else {
3834 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
3835 818c7501 2019-07-11 stsp "specified branch resolves to a commit which "
3836 818c7501 2019-07-11 stsp "is already contained in work tree's branch");
3837 818c7501 2019-07-11 stsp goto done;
3838 818c7501 2019-07-11 stsp }
3839 ff0d2220 2019-07-11 stsp }
3840 818c7501 2019-07-11 stsp
3841 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
3842 ff0d2220 2019-07-11 stsp if (error)
3843 ff0d2220 2019-07-11 stsp goto done;
3844 ff0d2220 2019-07-11 stsp
3845 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
3846 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
3847 818c7501 2019-07-11 stsp got_worktree_get_base_commit_id(worktree),
3848 818c7501 2019-07-11 stsp branch_head_commit_id, repo);
3849 818c7501 2019-07-11 stsp if (error)
3850 818c7501 2019-07-11 stsp goto done;
3851 818c7501 2019-07-11 stsp if (yca_id == NULL) {
3852 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
3853 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
3854 818c7501 2019-07-11 stsp "with work tree's branch");
3855 818c7501 2019-07-11 stsp goto done;
3856 818c7501 2019-07-11 stsp }
3857 818c7501 2019-07-11 stsp
3858 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
3859 818c7501 2019-07-11 stsp &tmp_branch, worktree, branch, repo);
3860 818c7501 2019-07-11 stsp if (error)
3861 818c7501 2019-07-11 stsp goto done;
3862 818c7501 2019-07-11 stsp }
3863 818c7501 2019-07-11 stsp
3864 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
3865 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
3866 818c7501 2019-07-11 stsp if (error)
3867 818c7501 2019-07-11 stsp goto done;
3868 818c7501 2019-07-11 stsp
3869 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
3870 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
3871 af61c510 2019-07-19 stsp error = collect_commits_to_rebase(&commits, commit_id, pid->id,
3872 af61c510 2019-07-19 stsp yca_id, got_worktree_get_path_prefix(worktree), repo);
3873 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3874 818c7501 2019-07-11 stsp commit = NULL;
3875 818c7501 2019-07-11 stsp if (error)
3876 818c7501 2019-07-11 stsp goto done;
3877 64c6d990 2019-07-11 stsp
3878 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
3879 ff0d2220 2019-07-11 stsp if (continue_rebase)
3880 ff0d2220 2019-07-11 stsp error = rebase_complete(worktree, branch,
3881 ff0d2220 2019-07-11 stsp new_base_branch, tmp_branch, repo);
3882 ff0d2220 2019-07-11 stsp else
3883 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
3884 818c7501 2019-07-11 stsp goto done;
3885 818c7501 2019-07-11 stsp }
3886 818c7501 2019-07-11 stsp
3887 818c7501 2019-07-11 stsp pid = NULL;
3888 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
3889 818c7501 2019-07-11 stsp commit_id = qid->id;
3890 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
3891 818c7501 2019-07-11 stsp pid = qid;
3892 818c7501 2019-07-11 stsp
3893 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
3894 01757395 2019-07-12 stsp worktree, parent_id, commit_id, repo, rebase_progress,
3895 01757395 2019-07-12 stsp &rebase_status, check_cancelled, NULL);
3896 818c7501 2019-07-11 stsp if (error)
3897 818c7501 2019-07-11 stsp goto done;
3898 818c7501 2019-07-11 stsp
3899 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3900 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3901 818c7501 2019-07-11 stsp break;
3902 01757395 2019-07-12 stsp }
3903 818c7501 2019-07-11 stsp
3904 01757395 2019-07-12 stsp error = rebase_commit(&merged_paths, worktree, tmp_branch,
3905 01757395 2019-07-12 stsp commit_id, repo);
3906 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
3907 818c7501 2019-07-11 stsp if (error)
3908 818c7501 2019-07-11 stsp goto done;
3909 818c7501 2019-07-11 stsp }
3910 818c7501 2019-07-11 stsp
3911 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
3912 818c7501 2019-07-11 stsp error = got_worktree_rebase_postpone(worktree);
3913 818c7501 2019-07-11 stsp if (error)
3914 818c7501 2019-07-11 stsp goto done;
3915 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
3916 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
3917 818c7501 2019-07-11 stsp } else
3918 818c7501 2019-07-11 stsp error = rebase_complete(worktree, branch, new_base_branch,
3919 818c7501 2019-07-11 stsp tmp_branch, repo);
3920 818c7501 2019-07-11 stsp done:
3921 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
3922 818c7501 2019-07-11 stsp free(branch_head_commit_id);
3923 818c7501 2019-07-11 stsp free(resume_commit_id);
3924 818c7501 2019-07-11 stsp free(yca_id);
3925 818c7501 2019-07-11 stsp if (commit)
3926 818c7501 2019-07-11 stsp got_object_commit_close(commit);
3927 818c7501 2019-07-11 stsp if (branch)
3928 818c7501 2019-07-11 stsp got_ref_close(branch);
3929 818c7501 2019-07-11 stsp if (new_base_branch)
3930 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
3931 818c7501 2019-07-11 stsp if (tmp_branch)
3932 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
3933 5ef14e63 2019-06-02 stsp if (worktree)
3934 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
3935 5ef14e63 2019-06-02 stsp if (repo)
3936 5ef14e63 2019-06-02 stsp got_repo_close(repo);
3937 5ef14e63 2019-06-02 stsp return error;
3938 5ef14e63 2019-06-02 stsp }