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 c0768b0f 2018-06-10 stsp #include <sys/types.h>
20 5de5890b 2018-10-18 stsp #include <sys/stat.h>
21 3c45a30a 2019-05-12 jcs #include <sys/param.h>
22 33ad4cbe 2019-05-12 jcs #include <sys/wait.h>
23 f42b1b34 2018-03-12 stsp
24 5c860e29 2018-03-12 stsp #include <err.h>
25 5c860e29 2018-03-12 stsp #include <errno.h>
26 12ce7a6c 2019-08-12 stsp #include <limits.h>
27 5c860e29 2018-03-12 stsp #include <locale.h>
28 818c7501 2019-07-11 stsp #include <ctype.h>
29 99437157 2018-11-11 stsp #include <signal.h>
30 5c860e29 2018-03-12 stsp #include <stdio.h>
31 5c860e29 2018-03-12 stsp #include <stdlib.h>
32 5c860e29 2018-03-12 stsp #include <string.h>
33 5c860e29 2018-03-12 stsp #include <unistd.h>
34 c09a553d 2018-03-12 stsp #include <libgen.h>
35 c0768b0f 2018-06-10 stsp #include <time.h>
36 33ad4cbe 2019-05-12 jcs #include <paths.h>
37 5c860e29 2018-03-12 stsp
38 53ccebc2 2019-07-30 stsp #include "got_version.h"
39 f42b1b34 2018-03-12 stsp #include "got_error.h"
40 f42b1b34 2018-03-12 stsp #include "got_object.h"
41 5261c201 2018-04-01 stsp #include "got_reference.h"
42 f42b1b34 2018-03-12 stsp #include "got_repository.h"
43 1dd54920 2019-05-11 stsp #include "got_path.h"
44 e6209546 2019-08-22 stsp #include "got_cancel.h"
45 c09a553d 2018-03-12 stsp #include "got_worktree.h"
46 79109fed 2018-03-27 stsp #include "got_diff.h"
47 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
48 404c43c4 2018-06-21 stsp #include "got_blame.h"
49 63219cd2 2019-01-04 stsp #include "got_privsep.h"
50 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
51 5c860e29 2018-03-12 stsp
52 5c860e29 2018-03-12 stsp #ifndef nitems
53 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
54 5c860e29 2018-03-12 stsp #endif
55 99437157 2018-11-11 stsp
56 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
57 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
58 99437157 2018-11-11 stsp
59 99437157 2018-11-11 stsp static void
60 99437157 2018-11-11 stsp catch_sigint(int signo)
61 99437157 2018-11-11 stsp {
62 99437157 2018-11-11 stsp sigint_received = 1;
63 99437157 2018-11-11 stsp }
64 99437157 2018-11-11 stsp
65 99437157 2018-11-11 stsp static void
66 99437157 2018-11-11 stsp catch_sigpipe(int signo)
67 99437157 2018-11-11 stsp {
68 99437157 2018-11-11 stsp sigpipe_received = 1;
69 99437157 2018-11-11 stsp }
70 5c860e29 2018-03-12 stsp
71 99437157 2018-11-11 stsp
72 8cfb4057 2019-07-09 stsp struct got_cmd {
73 5c860e29 2018-03-12 stsp const char *cmd_name;
74 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
75 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
76 97b3a7be 2019-07-09 stsp const char *cmd_alias;
77 5c860e29 2018-03-12 stsp };
78 5c860e29 2018-03-12 stsp
79 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
80 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
81 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
82 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
83 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
86 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
87 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
88 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
89 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
90 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
91 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
92 d00136be 2019-03-26 stsp __dead static void usage_add(void);
93 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
94 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
95 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
96 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
97 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
98 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
99 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
100 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
101 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
102 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
103 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
104 5c860e29 2018-03-12 stsp
105 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
106 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
107 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
108 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
111 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
112 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
114 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
115 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
116 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
117 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
118 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
119 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
120 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
121 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
122 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
123 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
124 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
125 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
126 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
127 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
128 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
129 5c860e29 2018-03-12 stsp
130 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
131 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
132 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
133 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
134 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
135 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
136 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
137 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
138 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
139 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
140 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
141 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
142 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
143 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
144 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
145 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
146 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
147 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
148 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
149 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
150 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
151 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
152 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
153 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
154 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
155 5c860e29 2018-03-12 stsp };
156 ce5b7c56 2019-07-09 stsp
157 ce5b7c56 2019-07-09 stsp static void
158 ce5b7c56 2019-07-09 stsp list_commands(void)
159 ce5b7c56 2019-07-09 stsp {
160 ce5b7c56 2019-07-09 stsp int i;
161 ce5b7c56 2019-07-09 stsp
162 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
163 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
164 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
166 ce5b7c56 2019-07-09 stsp }
167 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
168 ce5b7c56 2019-07-09 stsp }
169 5c860e29 2018-03-12 stsp
170 5c860e29 2018-03-12 stsp int
171 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
172 5c860e29 2018-03-12 stsp {
173 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
174 5c860e29 2018-03-12 stsp unsigned int i;
175 5c860e29 2018-03-12 stsp int ch;
176 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
177 5c860e29 2018-03-12 stsp
178 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
179 5c860e29 2018-03-12 stsp
180 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
181 5c860e29 2018-03-12 stsp switch (ch) {
182 1b6b95a8 2018-03-12 stsp case 'h':
183 1b6b95a8 2018-03-12 stsp hflag = 1;
184 53ccebc2 2019-07-30 stsp break;
185 53ccebc2 2019-07-30 stsp case 'V':
186 53ccebc2 2019-07-30 stsp Vflag = 1;
187 1b6b95a8 2018-03-12 stsp break;
188 5c860e29 2018-03-12 stsp default:
189 ce5b7c56 2019-07-09 stsp usage(hflag);
190 5c860e29 2018-03-12 stsp /* NOTREACHED */
191 5c860e29 2018-03-12 stsp }
192 5c860e29 2018-03-12 stsp }
193 5c860e29 2018-03-12 stsp
194 5c860e29 2018-03-12 stsp argc -= optind;
195 5c860e29 2018-03-12 stsp argv += optind;
196 1e70621d 2018-03-27 stsp optind = 0;
197 53ccebc2 2019-07-30 stsp
198 53ccebc2 2019-07-30 stsp if (Vflag) {
199 53ccebc2 2019-07-30 stsp got_version_print_str();
200 53ccebc2 2019-07-30 stsp return 1;
201 53ccebc2 2019-07-30 stsp }
202 5c860e29 2018-03-12 stsp
203 5c860e29 2018-03-12 stsp if (argc <= 0)
204 ce5b7c56 2019-07-09 stsp usage(hflag);
205 5c860e29 2018-03-12 stsp
206 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
207 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
208 99437157 2018-11-11 stsp
209 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
210 d7d4f210 2018-03-12 stsp const struct got_error *error;
211 d7d4f210 2018-03-12 stsp
212 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
213 5c860e29 2018-03-12 stsp
214 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
215 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
216 5c860e29 2018-03-12 stsp continue;
217 5c860e29 2018-03-12 stsp
218 1b6b95a8 2018-03-12 stsp if (hflag)
219 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
220 1b6b95a8 2018-03-12 stsp
221 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
222 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
223 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
224 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
225 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
226 70015d7a 2019-11-08 stsp !(sigint_received &&
227 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
228 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
229 d7d4f210 2018-03-12 stsp return 1;
230 d7d4f210 2018-03-12 stsp }
231 d7d4f210 2018-03-12 stsp
232 d7d4f210 2018-03-12 stsp return 0;
233 5c860e29 2018-03-12 stsp }
234 5c860e29 2018-03-12 stsp
235 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
236 ce5b7c56 2019-07-09 stsp list_commands();
237 5c860e29 2018-03-12 stsp return 1;
238 5c860e29 2018-03-12 stsp }
239 5c860e29 2018-03-12 stsp
240 4ed7e80c 2018-05-20 stsp __dead static void
241 ce5b7c56 2019-07-09 stsp usage(int hflag)
242 5c860e29 2018-03-12 stsp {
243 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
244 53ccebc2 2019-07-30 stsp getprogname());
245 ce5b7c56 2019-07-09 stsp if (hflag)
246 ce5b7c56 2019-07-09 stsp list_commands();
247 5c860e29 2018-03-12 stsp exit(1);
248 5c860e29 2018-03-12 stsp }
249 5c860e29 2018-03-12 stsp
250 0266afb7 2019-01-04 stsp static const struct got_error *
251 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
252 e2ba3d07 2019-05-13 stsp {
253 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
254 e2ba3d07 2019-05-13 stsp const char *editor;
255 8920fa04 2019-08-18 stsp
256 8920fa04 2019-08-18 stsp *abspath = NULL;
257 e2ba3d07 2019-05-13 stsp
258 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
259 e2ba3d07 2019-05-13 stsp if (editor == NULL)
260 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
261 e2ba3d07 2019-05-13 stsp
262 0ee7065d 2019-05-13 stsp if (editor) {
263 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
264 0ee7065d 2019-05-13 stsp if (err)
265 0ee7065d 2019-05-13 stsp return err;
266 0ee7065d 2019-05-13 stsp }
267 e2ba3d07 2019-05-13 stsp
268 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
269 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
270 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
271 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
272 0ee7065d 2019-05-13 stsp }
273 0ee7065d 2019-05-13 stsp
274 e2ba3d07 2019-05-13 stsp return NULL;
275 e2ba3d07 2019-05-13 stsp }
276 e2ba3d07 2019-05-13 stsp
277 e2ba3d07 2019-05-13 stsp static const struct got_error *
278 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
279 c530dc23 2019-07-23 stsp const char *worktree_path)
280 0266afb7 2019-01-04 stsp {
281 163ce85a 2019-05-13 stsp const struct got_error *err;
282 0266afb7 2019-01-04 stsp
283 37c06ea4 2019-07-15 stsp #ifdef PROFILE
284 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
285 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
286 37c06ea4 2019-07-15 stsp #endif
287 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
288 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
289 0266afb7 2019-01-04 stsp
290 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
291 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
292 0266afb7 2019-01-04 stsp
293 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
294 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
295 0266afb7 2019-01-04 stsp
296 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
297 163ce85a 2019-05-13 stsp if (err != NULL)
298 163ce85a 2019-05-13 stsp return err;
299 0266afb7 2019-01-04 stsp
300 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
302 0266afb7 2019-01-04 stsp
303 0266afb7 2019-01-04 stsp return NULL;
304 2c7829a4 2019-06-17 stsp }
305 2c7829a4 2019-06-17 stsp
306 2c7829a4 2019-06-17 stsp __dead static void
307 2c7829a4 2019-06-17 stsp usage_init(void)
308 2c7829a4 2019-06-17 stsp {
309 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
310 2c7829a4 2019-06-17 stsp exit(1);
311 2c7829a4 2019-06-17 stsp }
312 2c7829a4 2019-06-17 stsp
313 2c7829a4 2019-06-17 stsp static const struct got_error *
314 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
315 2c7829a4 2019-06-17 stsp {
316 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
317 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
318 2c7829a4 2019-06-17 stsp int ch;
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
321 2c7829a4 2019-06-17 stsp switch (ch) {
322 2c7829a4 2019-06-17 stsp default:
323 2c7829a4 2019-06-17 stsp usage_init();
324 2c7829a4 2019-06-17 stsp /* NOTREACHED */
325 2c7829a4 2019-06-17 stsp }
326 2c7829a4 2019-06-17 stsp }
327 2c7829a4 2019-06-17 stsp
328 2c7829a4 2019-06-17 stsp argc -= optind;
329 2c7829a4 2019-06-17 stsp argv += optind;
330 2c7829a4 2019-06-17 stsp
331 2c7829a4 2019-06-17 stsp #ifndef PROFILE
332 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
333 2c7829a4 2019-06-17 stsp err(1, "pledge");
334 2c7829a4 2019-06-17 stsp #endif
335 2c7829a4 2019-06-17 stsp if (argc != 1)
336 bc20e173 2019-06-17 stsp usage_init();
337 2c7829a4 2019-06-17 stsp
338 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
339 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
340 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
341 2c7829a4 2019-06-17 stsp
342 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
345 2c7829a4 2019-06-17 stsp if (error &&
346 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
347 2c7829a4 2019-06-17 stsp goto done;
348 2c7829a4 2019-06-17 stsp
349 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
350 2c7829a4 2019-06-17 stsp if (error)
351 2c7829a4 2019-06-17 stsp goto done;
352 2c7829a4 2019-06-17 stsp
353 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
354 2c7829a4 2019-06-17 stsp if (error != NULL)
355 3ce1b845 2019-07-15 stsp goto done;
356 3ce1b845 2019-07-15 stsp
357 3ce1b845 2019-07-15 stsp done:
358 3ce1b845 2019-07-15 stsp free(repo_path);
359 3ce1b845 2019-07-15 stsp return error;
360 3ce1b845 2019-07-15 stsp }
361 3ce1b845 2019-07-15 stsp
362 3ce1b845 2019-07-15 stsp __dead static void
363 3ce1b845 2019-07-15 stsp usage_import(void)
364 3ce1b845 2019-07-15 stsp {
365 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
366 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
367 3ce1b845 2019-07-15 stsp exit(1);
368 3ce1b845 2019-07-15 stsp }
369 3ce1b845 2019-07-15 stsp
370 3ce1b845 2019-07-15 stsp int
371 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
372 3ce1b845 2019-07-15 stsp {
373 3ce1b845 2019-07-15 stsp pid_t pid;
374 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
375 3ce1b845 2019-07-15 stsp int st = -1;
376 3ce1b845 2019-07-15 stsp
377 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
378 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
379 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
380 3ce1b845 2019-07-15 stsp
381 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
382 3ce1b845 2019-07-15 stsp case -1:
383 3ce1b845 2019-07-15 stsp goto doneediting;
384 3ce1b845 2019-07-15 stsp case 0:
385 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
386 3ce1b845 2019-07-15 stsp _exit(127);
387 3ce1b845 2019-07-15 stsp }
388 3ce1b845 2019-07-15 stsp
389 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
390 3ce1b845 2019-07-15 stsp if (errno != EINTR)
391 3ce1b845 2019-07-15 stsp break;
392 3ce1b845 2019-07-15 stsp
393 3ce1b845 2019-07-15 stsp doneediting:
394 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
395 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
396 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
399 3ce1b845 2019-07-15 stsp errno = EINTR;
400 3ce1b845 2019-07-15 stsp return -1;
401 3ce1b845 2019-07-15 stsp }
402 3ce1b845 2019-07-15 stsp
403 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
404 3ce1b845 2019-07-15 stsp }
405 3ce1b845 2019-07-15 stsp
406 3ce1b845 2019-07-15 stsp static const struct got_error *
407 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
408 3ce1b845 2019-07-15 stsp const char *initial_content)
409 3ce1b845 2019-07-15 stsp {
410 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
411 3ce1b845 2019-07-15 stsp char buf[1024];
412 3ce1b845 2019-07-15 stsp struct stat st, st2;
413 3ce1b845 2019-07-15 stsp FILE *fp;
414 3ce1b845 2019-07-15 stsp int content_changed = 0;
415 3ce1b845 2019-07-15 stsp size_t len;
416 3ce1b845 2019-07-15 stsp
417 3ce1b845 2019-07-15 stsp *logmsg = NULL;
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
420 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
421 3ce1b845 2019-07-15 stsp
422 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
423 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
424 3ce1b845 2019-07-15 stsp
425 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
426 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
427 3ce1b845 2019-07-15 stsp
428 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
429 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
430 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
433 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
434 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
435 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
436 3ce1b845 2019-07-15 stsp len = 0;
437 3ce1b845 2019-07-15 stsp
438 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
439 3ce1b845 2019-07-15 stsp if (fp == NULL) {
440 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
441 3ce1b845 2019-07-15 stsp goto done;
442 3ce1b845 2019-07-15 stsp }
443 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
444 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
445 3ce1b845 2019-07-15 stsp content_changed = 1;
446 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
447 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
448 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
449 3ce1b845 2019-07-15 stsp }
450 3ce1b845 2019-07-15 stsp fclose(fp);
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
453 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
454 3ce1b845 2019-07-15 stsp len--;
455 3ce1b845 2019-07-15 stsp }
456 3ce1b845 2019-07-15 stsp
457 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
458 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
459 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
460 3ce1b845 2019-07-15 stsp done:
461 3ce1b845 2019-07-15 stsp if (err) {
462 3ce1b845 2019-07-15 stsp free(*logmsg);
463 3ce1b845 2019-07-15 stsp *logmsg = NULL;
464 3ce1b845 2019-07-15 stsp }
465 3ce1b845 2019-07-15 stsp return err;
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp
468 3ce1b845 2019-07-15 stsp static const struct got_error *
469 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
470 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
471 3ce1b845 2019-07-15 stsp {
472 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
473 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
474 3ce1b845 2019-07-15 stsp int fd;
475 3ce1b845 2019-07-15 stsp
476 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
477 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
478 3ce1b845 2019-07-15 stsp branch_name) == -1)
479 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
480 3ce1b845 2019-07-15 stsp
481 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
482 3ce1b845 2019-07-15 stsp if (err)
483 3ce1b845 2019-07-15 stsp goto done;
484 3ce1b845 2019-07-15 stsp
485 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
486 3ce1b845 2019-07-15 stsp close(fd);
487 3ce1b845 2019-07-15 stsp
488 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
489 3ce1b845 2019-07-15 stsp done:
490 3ce1b845 2019-07-15 stsp free(initial_content);
491 3ce1b845 2019-07-15 stsp return err;
492 3ce1b845 2019-07-15 stsp }
493 3ce1b845 2019-07-15 stsp
494 3ce1b845 2019-07-15 stsp static const struct got_error *
495 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
496 3ce1b845 2019-07-15 stsp {
497 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
498 3ce1b845 2019-07-15 stsp return NULL;
499 3ce1b845 2019-07-15 stsp }
500 3ce1b845 2019-07-15 stsp
501 3ce1b845 2019-07-15 stsp static const struct got_error *
502 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
503 84792843 2019-08-09 stsp {
504 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
505 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
506 84792843 2019-08-09 stsp
507 84792843 2019-08-09 stsp *author = NULL;
508 aba9c984 2019-09-08 stsp
509 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
510 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
511 c9956ddf 2019-09-08 stsp if (name && email) {
512 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
513 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
514 aba9c984 2019-09-08 stsp return NULL;
515 aba9c984 2019-09-08 stsp }
516 84792843 2019-08-09 stsp
517 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
518 84792843 2019-08-09 stsp if (got_author == NULL) {
519 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
520 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
521 c9956ddf 2019-09-08 stsp if (name && email) {
522 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
523 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
524 c9956ddf 2019-09-08 stsp return NULL;
525 c9956ddf 2019-09-08 stsp }
526 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
527 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
528 84792843 2019-08-09 stsp }
529 84792843 2019-08-09 stsp
530 aba9c984 2019-09-08 stsp *author = strdup(got_author);
531 aba9c984 2019-09-08 stsp if (*author == NULL)
532 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
533 84792843 2019-08-09 stsp
534 84792843 2019-08-09 stsp /*
535 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
536 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
537 84792843 2019-08-09 stsp */
538 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
539 84792843 2019-08-09 stsp got_author++;
540 aba9c984 2019-09-08 stsp if (*got_author != '<') {
541 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
542 aba9c984 2019-09-08 stsp goto done;
543 aba9c984 2019-09-08 stsp }
544 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
545 84792843 2019-08-09 stsp got_author++;
546 aba9c984 2019-09-08 stsp if (*got_author != '@') {
547 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
548 aba9c984 2019-09-08 stsp goto done;
549 aba9c984 2019-09-08 stsp }
550 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
551 84792843 2019-08-09 stsp got_author++;
552 84792843 2019-08-09 stsp if (*got_author != '>')
553 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
554 aba9c984 2019-09-08 stsp done:
555 aba9c984 2019-09-08 stsp if (err) {
556 aba9c984 2019-09-08 stsp free(*author);
557 aba9c984 2019-09-08 stsp *author = NULL;
558 aba9c984 2019-09-08 stsp }
559 aba9c984 2019-09-08 stsp return err;
560 c9956ddf 2019-09-08 stsp }
561 c9956ddf 2019-09-08 stsp
562 c9956ddf 2019-09-08 stsp static const struct got_error *
563 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
564 c9956ddf 2019-09-08 stsp {
565 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
566 c9956ddf 2019-09-08 stsp
567 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
568 c9956ddf 2019-09-08 stsp if (homedir) {
569 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
570 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp }
573 c9956ddf 2019-09-08 stsp return NULL;
574 84792843 2019-08-09 stsp }
575 84792843 2019-08-09 stsp
576 84792843 2019-08-09 stsp static const struct got_error *
577 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
578 3ce1b845 2019-07-15 stsp {
579 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
580 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
581 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
582 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
583 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
584 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
585 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
586 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
587 3ce1b845 2019-07-15 stsp int ch;
588 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
589 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
590 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
591 3ce1b845 2019-07-15 stsp
592 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
593 3ce1b845 2019-07-15 stsp
594 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
595 3ce1b845 2019-07-15 stsp switch (ch) {
596 3ce1b845 2019-07-15 stsp case 'b':
597 3ce1b845 2019-07-15 stsp branch_name = optarg;
598 3ce1b845 2019-07-15 stsp break;
599 3ce1b845 2019-07-15 stsp case 'm':
600 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
601 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
602 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
603 3ce1b845 2019-07-15 stsp goto done;
604 3ce1b845 2019-07-15 stsp }
605 3ce1b845 2019-07-15 stsp break;
606 3ce1b845 2019-07-15 stsp case 'r':
607 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
608 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
609 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
610 9ba1d308 2019-10-21 stsp optarg);
611 3ce1b845 2019-07-15 stsp goto done;
612 3ce1b845 2019-07-15 stsp }
613 3ce1b845 2019-07-15 stsp break;
614 3ce1b845 2019-07-15 stsp case 'I':
615 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
616 3ce1b845 2019-07-15 stsp break;
617 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
618 3ce1b845 2019-07-15 stsp NULL);
619 3ce1b845 2019-07-15 stsp if (error)
620 3ce1b845 2019-07-15 stsp goto done;
621 3ce1b845 2019-07-15 stsp break;
622 3ce1b845 2019-07-15 stsp default:
623 b2b65d18 2019-08-22 stsp usage_import();
624 3ce1b845 2019-07-15 stsp /* NOTREACHED */
625 3ce1b845 2019-07-15 stsp }
626 3ce1b845 2019-07-15 stsp }
627 3ce1b845 2019-07-15 stsp
628 3ce1b845 2019-07-15 stsp argc -= optind;
629 3ce1b845 2019-07-15 stsp argv += optind;
630 3ce1b845 2019-07-15 stsp
631 3ce1b845 2019-07-15 stsp #ifndef PROFILE
632 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
633 aba9c984 2019-09-08 stsp "unveil",
634 3ce1b845 2019-07-15 stsp NULL) == -1)
635 3ce1b845 2019-07-15 stsp err(1, "pledge");
636 3ce1b845 2019-07-15 stsp #endif
637 3ce1b845 2019-07-15 stsp if (argc != 1)
638 3ce1b845 2019-07-15 stsp usage_import();
639 2c7829a4 2019-06-17 stsp
640 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
641 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
642 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
643 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
644 3ce1b845 2019-07-15 stsp }
645 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
646 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
647 c9956ddf 2019-09-08 stsp if (error)
648 c9956ddf 2019-09-08 stsp goto done;
649 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
650 3ce1b845 2019-07-15 stsp if (error)
651 3ce1b845 2019-07-15 stsp goto done;
652 aba9c984 2019-09-08 stsp
653 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
654 aba9c984 2019-09-08 stsp if (error)
655 aba9c984 2019-09-08 stsp return error;
656 3ce1b845 2019-07-15 stsp
657 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
658 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
659 3ce1b845 2019-07-15 stsp goto done;
660 3ce1b845 2019-07-15 stsp }
661 3ce1b845 2019-07-15 stsp
662 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
663 3ce1b845 2019-07-15 stsp if (error) {
664 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
665 3ce1b845 2019-07-15 stsp goto done;
666 3ce1b845 2019-07-15 stsp } else {
667 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
668 3ce1b845 2019-07-15 stsp "import target branch already exists");
669 3ce1b845 2019-07-15 stsp goto done;
670 3ce1b845 2019-07-15 stsp }
671 3ce1b845 2019-07-15 stsp
672 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
673 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
674 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
675 3ce1b845 2019-07-15 stsp goto done;
676 3ce1b845 2019-07-15 stsp }
677 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
678 3ce1b845 2019-07-15 stsp
679 3ce1b845 2019-07-15 stsp /*
680 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
681 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
682 3ce1b845 2019-07-15 stsp */
683 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
684 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
685 3ce1b845 2019-07-15 stsp if (error)
686 3ce1b845 2019-07-15 stsp goto done;
687 8e158b01 2019-09-22 stsp free(logmsg);
688 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
689 ef293bdd 2019-10-21 stsp path_dir, refname);
690 ef293bdd 2019-10-21 stsp if (error) {
691 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
692 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
693 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
694 3ce1b845 2019-07-15 stsp goto done;
695 ef293bdd 2019-10-21 stsp }
696 3ce1b845 2019-07-15 stsp }
697 3ce1b845 2019-07-15 stsp
698 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
699 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
700 ef293bdd 2019-10-21 stsp if (logmsg_path)
701 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
702 3ce1b845 2019-07-15 stsp goto done;
703 ef293bdd 2019-10-21 stsp }
704 3ce1b845 2019-07-15 stsp
705 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
706 ef293bdd 2019-10-21 stsp if (error) {
707 ef293bdd 2019-10-21 stsp if (logmsg_path)
708 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
709 ef293bdd 2019-10-21 stsp goto done;
710 ef293bdd 2019-10-21 stsp }
711 ef293bdd 2019-10-21 stsp
712 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
713 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
714 ef293bdd 2019-10-21 stsp if (error) {
715 ef293bdd 2019-10-21 stsp if (logmsg_path)
716 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
717 3ce1b845 2019-07-15 stsp goto done;
718 ef293bdd 2019-10-21 stsp }
719 3ce1b845 2019-07-15 stsp
720 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
721 ef293bdd 2019-10-21 stsp if (error) {
722 ef293bdd 2019-10-21 stsp if (logmsg_path)
723 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
724 3ce1b845 2019-07-15 stsp goto done;
725 ef293bdd 2019-10-21 stsp }
726 3ce1b845 2019-07-15 stsp
727 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
728 ef293bdd 2019-10-21 stsp if (error) {
729 ef293bdd 2019-10-21 stsp if (logmsg_path)
730 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
731 3ce1b845 2019-07-15 stsp goto done;
732 ef293bdd 2019-10-21 stsp }
733 3ce1b845 2019-07-15 stsp
734 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
735 ef293bdd 2019-10-21 stsp if (error) {
736 ef293bdd 2019-10-21 stsp if (logmsg_path)
737 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
738 3ce1b845 2019-07-15 stsp goto done;
739 ef293bdd 2019-10-21 stsp }
740 3ce1b845 2019-07-15 stsp
741 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
742 3ce1b845 2019-07-15 stsp if (error) {
743 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
744 ef293bdd 2019-10-21 stsp if (logmsg_path)
745 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
746 3ce1b845 2019-07-15 stsp goto done;
747 ef293bdd 2019-10-21 stsp }
748 3ce1b845 2019-07-15 stsp
749 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
750 3ce1b845 2019-07-15 stsp branch_ref);
751 ef293bdd 2019-10-21 stsp if (error) {
752 ef293bdd 2019-10-21 stsp if (logmsg_path)
753 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
754 3ce1b845 2019-07-15 stsp goto done;
755 ef293bdd 2019-10-21 stsp }
756 3ce1b845 2019-07-15 stsp
757 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
758 ef293bdd 2019-10-21 stsp if (error) {
759 ef293bdd 2019-10-21 stsp if (logmsg_path)
760 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
761 3ce1b845 2019-07-15 stsp goto done;
762 ef293bdd 2019-10-21 stsp }
763 3ce1b845 2019-07-15 stsp }
764 3ce1b845 2019-07-15 stsp
765 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
766 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
767 2c7829a4 2019-06-17 stsp done:
768 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
769 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
770 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
771 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
772 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
773 8e158b01 2019-09-22 stsp free(logmsg);
774 ef293bdd 2019-10-21 stsp free(logmsg_path);
775 2c7829a4 2019-06-17 stsp free(repo_path);
776 3ce1b845 2019-07-15 stsp free(editor);
777 3ce1b845 2019-07-15 stsp free(refname);
778 3ce1b845 2019-07-15 stsp free(new_commit_id);
779 3ce1b845 2019-07-15 stsp free(id_str);
780 aba9c984 2019-09-08 stsp free(author);
781 c9956ddf 2019-09-08 stsp free(gitconfig_path);
782 3ce1b845 2019-07-15 stsp if (branch_ref)
783 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
784 3ce1b845 2019-07-15 stsp if (head_ref)
785 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
786 2c7829a4 2019-06-17 stsp return error;
787 0266afb7 2019-01-04 stsp }
788 0266afb7 2019-01-04 stsp
789 4ed7e80c 2018-05-20 stsp __dead static void
790 c09a553d 2018-03-12 stsp usage_checkout(void)
791 c09a553d 2018-03-12 stsp {
792 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
793 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
794 c09a553d 2018-03-12 stsp exit(1);
795 92a684f4 2018-03-12 stsp }
796 92a684f4 2018-03-12 stsp
797 1ee397ad 2019-07-12 stsp static const struct got_error *
798 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
799 92a684f4 2018-03-12 stsp {
800 92a684f4 2018-03-12 stsp char *worktree_path = arg;
801 a484d721 2019-06-10 stsp
802 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
803 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
804 1ee397ad 2019-07-12 stsp return NULL;
805 92a684f4 2018-03-12 stsp
806 92a684f4 2018-03-12 stsp while (path[0] == '/')
807 92a684f4 2018-03-12 stsp path++;
808 92a684f4 2018-03-12 stsp
809 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
810 1ee397ad 2019-07-12 stsp return NULL;
811 99437157 2018-11-11 stsp }
812 99437157 2018-11-11 stsp
813 99437157 2018-11-11 stsp static const struct got_error *
814 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
815 99437157 2018-11-11 stsp {
816 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
817 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
818 99437157 2018-11-11 stsp return NULL;
819 8069f636 2019-01-12 stsp }
820 8069f636 2019-01-12 stsp
821 8069f636 2019-01-12 stsp static const struct got_error *
822 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
823 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
824 3aef623b 2019-10-15 stsp struct got_repository *repo)
825 8069f636 2019-01-12 stsp {
826 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
827 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
828 8069f636 2019-01-12 stsp
829 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
830 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
831 36a38700 2019-05-10 stsp if (err)
832 36a38700 2019-05-10 stsp return err;
833 8069f636 2019-01-12 stsp
834 d5bea539 2019-05-13 stsp if (yca_id == NULL)
835 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
836 8069f636 2019-01-12 stsp
837 d5bea539 2019-05-13 stsp /*
838 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
839 d5bea539 2019-05-13 stsp * and the work tree's base commit.
840 d5bea539 2019-05-13 stsp *
841 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
842 d5bea539 2019-05-13 stsp *
843 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
844 d5bea539 2019-05-13 stsp * \ /
845 d5bea539 2019-05-13 stsp * C E
846 d5bea539 2019-05-13 stsp * \ /
847 d5bea539 2019-05-13 stsp * B (yca)
848 d5bea539 2019-05-13 stsp * |
849 d5bea539 2019-05-13 stsp * A
850 d5bea539 2019-05-13 stsp *
851 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
852 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
853 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
854 d5bea539 2019-05-13 stsp */
855 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
856 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
857 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
858 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
859 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
860 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
861 8069f636 2019-01-12 stsp
862 d5bea539 2019-05-13 stsp free(yca_id);
863 d5bea539 2019-05-13 stsp return NULL;
864 c09a553d 2018-03-12 stsp }
865 a367ff0f 2019-05-14 stsp
866 a367ff0f 2019-05-14 stsp static const struct got_error *
867 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
868 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
869 a51a74b3 2019-07-27 stsp struct got_repository *repo)
870 a367ff0f 2019-05-14 stsp {
871 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
872 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
873 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
874 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
875 a367ff0f 2019-05-14 stsp
876 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
877 a367ff0f 2019-05-14 stsp if (err)
878 a51a74b3 2019-07-27 stsp goto done;
879 a51a74b3 2019-07-27 stsp
880 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
881 a51a74b3 2019-07-27 stsp is_same_branch = 1;
882 a51a74b3 2019-07-27 stsp goto done;
883 a51a74b3 2019-07-27 stsp }
884 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
885 a51a74b3 2019-07-27 stsp is_same_branch = 1;
886 a367ff0f 2019-05-14 stsp goto done;
887 a51a74b3 2019-07-27 stsp }
888 a367ff0f 2019-05-14 stsp
889 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
890 a367ff0f 2019-05-14 stsp if (err)
891 a367ff0f 2019-05-14 stsp goto done;
892 a367ff0f 2019-05-14 stsp
893 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
894 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
895 a367ff0f 2019-05-14 stsp if (err)
896 a367ff0f 2019-05-14 stsp goto done;
897 a367ff0f 2019-05-14 stsp
898 a367ff0f 2019-05-14 stsp for (;;) {
899 a367ff0f 2019-05-14 stsp struct got_object_id *id;
900 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
901 a367ff0f 2019-05-14 stsp if (err) {
902 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
903 a367ff0f 2019-05-14 stsp err = NULL;
904 a367ff0f 2019-05-14 stsp break;
905 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
906 a367ff0f 2019-05-14 stsp break;
907 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
908 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
909 a367ff0f 2019-05-14 stsp if (err)
910 a367ff0f 2019-05-14 stsp break;
911 a367ff0f 2019-05-14 stsp }
912 c09a553d 2018-03-12 stsp
913 a367ff0f 2019-05-14 stsp if (id) {
914 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
915 a51a74b3 2019-07-27 stsp break;
916 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
917 a367ff0f 2019-05-14 stsp is_same_branch = 1;
918 a367ff0f 2019-05-14 stsp break;
919 a367ff0f 2019-05-14 stsp }
920 a367ff0f 2019-05-14 stsp }
921 a367ff0f 2019-05-14 stsp }
922 a367ff0f 2019-05-14 stsp done:
923 a367ff0f 2019-05-14 stsp if (graph)
924 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
925 a367ff0f 2019-05-14 stsp free(head_commit_id);
926 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
927 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
928 04f57cb3 2019-07-25 stsp return err;
929 04f57cb3 2019-07-25 stsp }
930 04f57cb3 2019-07-25 stsp
931 04f57cb3 2019-07-25 stsp static const struct got_error *
932 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
933 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
934 04f57cb3 2019-07-25 stsp {
935 04f57cb3 2019-07-25 stsp const struct got_error *err;
936 04f57cb3 2019-07-25 stsp struct got_reference *ref;
937 303e2782 2019-08-09 stsp struct got_tag_object *tag;
938 04f57cb3 2019-07-25 stsp
939 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
940 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
941 303e2782 2019-08-09 stsp if (err == NULL) {
942 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
943 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
944 303e2782 2019-08-09 stsp if (*commit_id == NULL)
945 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
946 303e2782 2019-08-09 stsp got_object_tag_close(tag);
947 303e2782 2019-08-09 stsp return err;
948 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
949 303e2782 2019-08-09 stsp return err;
950 303e2782 2019-08-09 stsp
951 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
952 04f57cb3 2019-07-25 stsp if (err == NULL) {
953 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
954 04f57cb3 2019-07-25 stsp got_ref_close(ref);
955 04f57cb3 2019-07-25 stsp } else {
956 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
957 04f57cb3 2019-07-25 stsp return err;
958 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
959 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
960 04f57cb3 2019-07-25 stsp }
961 a367ff0f 2019-05-14 stsp return err;
962 a367ff0f 2019-05-14 stsp }
963 8069f636 2019-01-12 stsp
964 4ed7e80c 2018-05-20 stsp static const struct got_error *
965 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
966 c09a553d 2018-03-12 stsp {
967 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
968 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
969 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
970 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
971 c09a553d 2018-03-12 stsp char *repo_path = NULL;
972 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
973 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
974 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
975 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
976 72151b04 2019-05-11 stsp int ch, same_path_prefix;
977 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
978 f2ea84fa 2019-07-27 stsp
979 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
980 c09a553d 2018-03-12 stsp
981 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
982 0bb8a95e 2018-03-12 stsp switch (ch) {
983 08573d5b 2019-05-14 stsp case 'b':
984 08573d5b 2019-05-14 stsp branch_name = optarg;
985 08573d5b 2019-05-14 stsp break;
986 8069f636 2019-01-12 stsp case 'c':
987 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
988 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
989 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
990 8069f636 2019-01-12 stsp break;
991 0bb8a95e 2018-03-12 stsp case 'p':
992 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
993 0bb8a95e 2018-03-12 stsp break;
994 0bb8a95e 2018-03-12 stsp default:
995 2deda0b9 2019-03-07 stsp usage_checkout();
996 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
997 0bb8a95e 2018-03-12 stsp }
998 0bb8a95e 2018-03-12 stsp }
999 0bb8a95e 2018-03-12 stsp
1000 0bb8a95e 2018-03-12 stsp argc -= optind;
1001 0bb8a95e 2018-03-12 stsp argv += optind;
1002 0bb8a95e 2018-03-12 stsp
1003 6715a751 2018-03-16 stsp #ifndef PROFILE
1004 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1005 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1006 c09a553d 2018-03-12 stsp err(1, "pledge");
1007 6715a751 2018-03-16 stsp #endif
1008 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1009 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1010 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1011 76089277 2018-04-01 stsp if (repo_path == NULL)
1012 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1013 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1014 76089277 2018-04-01 stsp if (cwd == NULL) {
1015 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1016 76089277 2018-04-01 stsp goto done;
1017 76089277 2018-04-01 stsp }
1018 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1019 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1020 230a42bd 2019-05-11 jcs if (base == NULL) {
1021 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1022 230a42bd 2019-05-11 jcs path_prefix);
1023 230a42bd 2019-05-11 jcs goto done;
1024 230a42bd 2019-05-11 jcs }
1025 230a42bd 2019-05-11 jcs } else {
1026 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1027 230a42bd 2019-05-11 jcs if (base == NULL) {
1028 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1029 230a42bd 2019-05-11 jcs repo_path);
1030 230a42bd 2019-05-11 jcs goto done;
1031 230a42bd 2019-05-11 jcs }
1032 76089277 2018-04-01 stsp }
1033 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1034 c09a553d 2018-03-12 stsp if (dotgit)
1035 c09a553d 2018-03-12 stsp *dotgit = '\0';
1036 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1037 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1038 c09a553d 2018-03-12 stsp free(cwd);
1039 76089277 2018-04-01 stsp goto done;
1040 c09a553d 2018-03-12 stsp }
1041 c09a553d 2018-03-12 stsp free(cwd);
1042 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1043 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1044 76089277 2018-04-01 stsp if (repo_path == NULL) {
1045 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1046 76089277 2018-04-01 stsp goto done;
1047 76089277 2018-04-01 stsp }
1048 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1049 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1050 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1051 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1052 b4b3a7dd 2019-07-22 stsp argv[1]);
1053 b4b3a7dd 2019-07-22 stsp goto done;
1054 b4b3a7dd 2019-07-22 stsp }
1055 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1056 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1057 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1058 b4b3a7dd 2019-07-22 stsp goto done;
1059 b4b3a7dd 2019-07-22 stsp }
1060 76089277 2018-04-01 stsp }
1061 c09a553d 2018-03-12 stsp } else
1062 c09a553d 2018-03-12 stsp usage_checkout();
1063 c09a553d 2018-03-12 stsp
1064 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1065 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1066 13bfb272 2019-05-10 jcs
1067 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1068 c09a553d 2018-03-12 stsp if (error != NULL)
1069 c02c541e 2019-03-29 stsp goto done;
1070 c02c541e 2019-03-29 stsp
1071 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1072 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1073 c530dc23 2019-07-23 stsp if (error) {
1074 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1075 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1076 c530dc23 2019-07-23 stsp goto done;
1077 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1078 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1079 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1080 c530dc23 2019-07-23 stsp goto done;
1081 c530dc23 2019-07-23 stsp }
1082 c530dc23 2019-07-23 stsp }
1083 c530dc23 2019-07-23 stsp
1084 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1085 c02c541e 2019-03-29 stsp if (error)
1086 c09a553d 2018-03-12 stsp goto done;
1087 8069f636 2019-01-12 stsp
1088 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1089 c09a553d 2018-03-12 stsp if (error != NULL)
1090 c09a553d 2018-03-12 stsp goto done;
1091 c09a553d 2018-03-12 stsp
1092 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1093 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1094 c09a553d 2018-03-12 stsp goto done;
1095 c09a553d 2018-03-12 stsp
1096 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1097 c09a553d 2018-03-12 stsp if (error != NULL)
1098 c09a553d 2018-03-12 stsp goto done;
1099 c09a553d 2018-03-12 stsp
1100 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1101 e5dc7198 2018-12-29 stsp path_prefix);
1102 e5dc7198 2018-12-29 stsp if (error != NULL)
1103 e5dc7198 2018-12-29 stsp goto done;
1104 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1105 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1106 49520a32 2018-12-29 stsp goto done;
1107 49520a32 2018-12-29 stsp }
1108 49520a32 2018-12-29 stsp
1109 8069f636 2019-01-12 stsp if (commit_id_str) {
1110 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1111 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1112 30837e32 2019-07-25 stsp if (error)
1113 8069f636 2019-01-12 stsp goto done;
1114 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1115 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1116 8069f636 2019-01-12 stsp if (error != NULL) {
1117 8069f636 2019-01-12 stsp free(commit_id);
1118 8069f636 2019-01-12 stsp goto done;
1119 8069f636 2019-01-12 stsp }
1120 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1121 45d344f6 2019-05-14 stsp if (error)
1122 45d344f6 2019-05-14 stsp goto done;
1123 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1124 8069f636 2019-01-12 stsp commit_id);
1125 8069f636 2019-01-12 stsp free(commit_id);
1126 8069f636 2019-01-12 stsp if (error)
1127 8069f636 2019-01-12 stsp goto done;
1128 8069f636 2019-01-12 stsp }
1129 8069f636 2019-01-12 stsp
1130 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1131 f2ea84fa 2019-07-27 stsp if (error)
1132 f2ea84fa 2019-07-27 stsp goto done;
1133 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1134 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1135 c09a553d 2018-03-12 stsp if (error != NULL)
1136 c09a553d 2018-03-12 stsp goto done;
1137 c09a553d 2018-03-12 stsp
1138 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1139 c09a553d 2018-03-12 stsp
1140 c09a553d 2018-03-12 stsp done:
1141 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1142 8069f636 2019-01-12 stsp free(commit_id_str);
1143 76089277 2018-04-01 stsp free(repo_path);
1144 507dc3bb 2018-12-29 stsp free(worktree_path);
1145 507dc3bb 2018-12-29 stsp return error;
1146 507dc3bb 2018-12-29 stsp }
1147 507dc3bb 2018-12-29 stsp
1148 507dc3bb 2018-12-29 stsp __dead static void
1149 507dc3bb 2018-12-29 stsp usage_update(void)
1150 507dc3bb 2018-12-29 stsp {
1151 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1152 507dc3bb 2018-12-29 stsp getprogname());
1153 507dc3bb 2018-12-29 stsp exit(1);
1154 507dc3bb 2018-12-29 stsp }
1155 507dc3bb 2018-12-29 stsp
1156 1ee397ad 2019-07-12 stsp static const struct got_error *
1157 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1158 507dc3bb 2018-12-29 stsp {
1159 784955db 2019-01-12 stsp int *did_something = arg;
1160 784955db 2019-01-12 stsp
1161 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1162 1ee397ad 2019-07-12 stsp return NULL;
1163 507dc3bb 2018-12-29 stsp
1164 1545c615 2019-02-10 stsp *did_something = 1;
1165 a484d721 2019-06-10 stsp
1166 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1167 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1168 1ee397ad 2019-07-12 stsp return NULL;
1169 a484d721 2019-06-10 stsp
1170 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1171 507dc3bb 2018-12-29 stsp path++;
1172 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1173 1ee397ad 2019-07-12 stsp return NULL;
1174 be7061eb 2018-12-30 stsp }
1175 be7061eb 2018-12-30 stsp
1176 be7061eb 2018-12-30 stsp static const struct got_error *
1177 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1178 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1179 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1180 a1fb16d8 2019-05-24 stsp {
1181 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1182 a1fb16d8 2019-05-24 stsp char *base_id_str;
1183 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1184 a1fb16d8 2019-05-24 stsp
1185 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1186 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1187 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1188 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1189 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1190 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1191 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1192 a1fb16d8 2019-05-24 stsp }
1193 a1fb16d8 2019-05-24 stsp
1194 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1195 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1196 a1fb16d8 2019-05-24 stsp if (err) {
1197 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1198 a1fb16d8 2019-05-24 stsp return err;
1199 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1200 a1fb16d8 2019-05-24 stsp }
1201 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1202 a1fb16d8 2019-05-24 stsp return NULL;
1203 a1fb16d8 2019-05-24 stsp
1204 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1205 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1206 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1207 a1fb16d8 2019-05-24 stsp if (err)
1208 a1fb16d8 2019-05-24 stsp return err;
1209 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1210 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1211 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1212 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1213 0ebf8283 2019-07-24 stsp return NULL;
1214 0ebf8283 2019-07-24 stsp }
1215 0ebf8283 2019-07-24 stsp
1216 0ebf8283 2019-07-24 stsp static const struct got_error *
1217 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1218 0ebf8283 2019-07-24 stsp {
1219 0ebf8283 2019-07-24 stsp const struct got_error *err;
1220 0ebf8283 2019-07-24 stsp int in_progress;
1221 0ebf8283 2019-07-24 stsp
1222 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1223 0ebf8283 2019-07-24 stsp if (err)
1224 0ebf8283 2019-07-24 stsp return err;
1225 0ebf8283 2019-07-24 stsp if (in_progress)
1226 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1227 0ebf8283 2019-07-24 stsp
1228 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1229 0ebf8283 2019-07-24 stsp if (err)
1230 0ebf8283 2019-07-24 stsp return err;
1231 0ebf8283 2019-07-24 stsp if (in_progress)
1232 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1233 0ebf8283 2019-07-24 stsp
1234 a1fb16d8 2019-05-24 stsp return NULL;
1235 a5edda0a 2019-07-27 stsp }
1236 a5edda0a 2019-07-27 stsp
1237 a5edda0a 2019-07-27 stsp static const struct got_error *
1238 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1239 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1240 a5edda0a 2019-07-27 stsp {
1241 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1242 a5edda0a 2019-07-27 stsp char *path;
1243 a5edda0a 2019-07-27 stsp int i;
1244 a5edda0a 2019-07-27 stsp
1245 a5edda0a 2019-07-27 stsp if (argc == 0) {
1246 a5edda0a 2019-07-27 stsp path = strdup("");
1247 a5edda0a 2019-07-27 stsp if (path == NULL)
1248 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1249 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1250 a5edda0a 2019-07-27 stsp }
1251 a5edda0a 2019-07-27 stsp
1252 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1253 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1254 a5edda0a 2019-07-27 stsp if (err)
1255 a5edda0a 2019-07-27 stsp break;
1256 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1257 a5edda0a 2019-07-27 stsp if (err) {
1258 a5edda0a 2019-07-27 stsp free(path);
1259 a5edda0a 2019-07-27 stsp break;
1260 a5edda0a 2019-07-27 stsp }
1261 a5edda0a 2019-07-27 stsp }
1262 a5edda0a 2019-07-27 stsp
1263 a5edda0a 2019-07-27 stsp return err;
1264 a1fb16d8 2019-05-24 stsp }
1265 a1fb16d8 2019-05-24 stsp
1266 a1fb16d8 2019-05-24 stsp static const struct got_error *
1267 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1268 507dc3bb 2018-12-29 stsp {
1269 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1270 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1271 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1272 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1273 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1274 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1275 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1276 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1277 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1278 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1279 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1280 507dc3bb 2018-12-29 stsp
1281 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1282 f2ea84fa 2019-07-27 stsp
1283 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1284 507dc3bb 2018-12-29 stsp switch (ch) {
1285 024e9686 2019-05-14 stsp case 'b':
1286 024e9686 2019-05-14 stsp branch_name = optarg;
1287 024e9686 2019-05-14 stsp break;
1288 507dc3bb 2018-12-29 stsp case 'c':
1289 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1290 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1291 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1292 507dc3bb 2018-12-29 stsp break;
1293 507dc3bb 2018-12-29 stsp default:
1294 2deda0b9 2019-03-07 stsp usage_update();
1295 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1296 507dc3bb 2018-12-29 stsp }
1297 507dc3bb 2018-12-29 stsp }
1298 507dc3bb 2018-12-29 stsp
1299 507dc3bb 2018-12-29 stsp argc -= optind;
1300 507dc3bb 2018-12-29 stsp argv += optind;
1301 507dc3bb 2018-12-29 stsp
1302 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1303 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1304 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1305 507dc3bb 2018-12-29 stsp err(1, "pledge");
1306 507dc3bb 2018-12-29 stsp #endif
1307 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1308 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1309 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1310 c4cdcb68 2019-04-03 stsp goto done;
1311 c4cdcb68 2019-04-03 stsp }
1312 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1313 7d5807f4 2019-07-11 stsp if (error)
1314 7d5807f4 2019-07-11 stsp goto done;
1315 7d5807f4 2019-07-11 stsp
1316 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1317 f2ea84fa 2019-07-27 stsp if (error)
1318 f2ea84fa 2019-07-27 stsp goto done;
1319 507dc3bb 2018-12-29 stsp
1320 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1321 c9956ddf 2019-09-08 stsp NULL);
1322 507dc3bb 2018-12-29 stsp if (error != NULL)
1323 507dc3bb 2018-12-29 stsp goto done;
1324 507dc3bb 2018-12-29 stsp
1325 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1326 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1327 087fb88c 2019-08-04 stsp if (error)
1328 087fb88c 2019-08-04 stsp goto done;
1329 087fb88c 2019-08-04 stsp
1330 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1331 0266afb7 2019-01-04 stsp if (error)
1332 0266afb7 2019-01-04 stsp goto done;
1333 0266afb7 2019-01-04 stsp
1334 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1335 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1336 024e9686 2019-05-14 stsp if (error != NULL)
1337 024e9686 2019-05-14 stsp goto done;
1338 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1339 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1340 9c4b8182 2019-01-02 stsp if (error != NULL)
1341 9c4b8182 2019-01-02 stsp goto done;
1342 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1343 507dc3bb 2018-12-29 stsp if (error != NULL)
1344 507dc3bb 2018-12-29 stsp goto done;
1345 507dc3bb 2018-12-29 stsp } else {
1346 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1347 04f57cb3 2019-07-25 stsp free(commit_id_str);
1348 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1349 30837e32 2019-07-25 stsp if (error)
1350 a297e751 2019-07-11 stsp goto done;
1351 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1352 a297e751 2019-07-11 stsp if (error)
1353 507dc3bb 2018-12-29 stsp goto done;
1354 507dc3bb 2018-12-29 stsp }
1355 35c965b2 2018-12-29 stsp
1356 a1fb16d8 2019-05-24 stsp if (branch_name) {
1357 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1358 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1359 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1360 f2ea84fa 2019-07-27 stsp continue;
1361 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1362 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1363 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1364 024e9686 2019-05-14 stsp goto done;
1365 024e9686 2019-05-14 stsp }
1366 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1367 024e9686 2019-05-14 stsp if (error)
1368 024e9686 2019-05-14 stsp goto done;
1369 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1370 3aef623b 2019-10-15 stsp repo);
1371 a367ff0f 2019-05-14 stsp free(head_commit_id);
1372 024e9686 2019-05-14 stsp if (error != NULL)
1373 024e9686 2019-05-14 stsp goto done;
1374 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1375 a367ff0f 2019-05-14 stsp if (error)
1376 a367ff0f 2019-05-14 stsp goto done;
1377 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1378 024e9686 2019-05-14 stsp if (error)
1379 024e9686 2019-05-14 stsp goto done;
1380 024e9686 2019-05-14 stsp } else {
1381 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1382 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1383 a367ff0f 2019-05-14 stsp if (error != NULL) {
1384 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1385 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1386 024e9686 2019-05-14 stsp goto done;
1387 a367ff0f 2019-05-14 stsp }
1388 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1389 a367ff0f 2019-05-14 stsp if (error)
1390 a367ff0f 2019-05-14 stsp goto done;
1391 024e9686 2019-05-14 stsp }
1392 507dc3bb 2018-12-29 stsp
1393 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1394 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1395 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1396 507dc3bb 2018-12-29 stsp commit_id);
1397 507dc3bb 2018-12-29 stsp if (error)
1398 507dc3bb 2018-12-29 stsp goto done;
1399 507dc3bb 2018-12-29 stsp }
1400 507dc3bb 2018-12-29 stsp
1401 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1402 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1403 507dc3bb 2018-12-29 stsp if (error != NULL)
1404 507dc3bb 2018-12-29 stsp goto done;
1405 9c4b8182 2019-01-02 stsp
1406 784955db 2019-01-12 stsp if (did_something)
1407 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1408 784955db 2019-01-12 stsp else
1409 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1410 507dc3bb 2018-12-29 stsp done:
1411 c09a553d 2018-03-12 stsp free(worktree_path);
1412 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1413 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1414 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1415 507dc3bb 2018-12-29 stsp free(commit_id);
1416 9c4b8182 2019-01-02 stsp free(commit_id_str);
1417 c09a553d 2018-03-12 stsp return error;
1418 c09a553d 2018-03-12 stsp }
1419 c09a553d 2018-03-12 stsp
1420 f42b1b34 2018-03-12 stsp static const struct got_error *
1421 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1422 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1423 63035f9f 2019-10-06 stsp struct got_repository *repo)
1424 5c860e29 2018-03-12 stsp {
1425 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1426 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1427 79109fed 2018-03-27 stsp
1428 44392932 2019-08-25 stsp if (blob_id1) {
1429 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1430 44392932 2019-08-25 stsp if (err)
1431 44392932 2019-08-25 stsp goto done;
1432 44392932 2019-08-25 stsp }
1433 79109fed 2018-03-27 stsp
1434 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1435 44392932 2019-08-25 stsp if (err)
1436 44392932 2019-08-25 stsp goto done;
1437 79109fed 2018-03-27 stsp
1438 44392932 2019-08-25 stsp while (path[0] == '/')
1439 44392932 2019-08-25 stsp path++;
1440 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1441 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1442 44392932 2019-08-25 stsp done:
1443 44392932 2019-08-25 stsp if (blob1)
1444 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1445 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1446 44392932 2019-08-25 stsp return err;
1447 44392932 2019-08-25 stsp }
1448 44392932 2019-08-25 stsp
1449 44392932 2019-08-25 stsp static const struct got_error *
1450 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1451 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1452 63035f9f 2019-10-06 stsp struct got_repository *repo)
1453 44392932 2019-08-25 stsp {
1454 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1455 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1456 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1457 44392932 2019-08-25 stsp
1458 44392932 2019-08-25 stsp if (tree_id1) {
1459 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1460 79109fed 2018-03-27 stsp if (err)
1461 44392932 2019-08-25 stsp goto done;
1462 79109fed 2018-03-27 stsp }
1463 79109fed 2018-03-27 stsp
1464 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1465 0f2b3dca 2018-12-22 stsp if (err)
1466 0f2b3dca 2018-12-22 stsp goto done;
1467 0f2b3dca 2018-12-22 stsp
1468 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1469 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1470 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1471 44392932 2019-08-25 stsp while (path[0] == '/')
1472 44392932 2019-08-25 stsp path++;
1473 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1474 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1475 0f2b3dca 2018-12-22 stsp done:
1476 79109fed 2018-03-27 stsp if (tree1)
1477 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1478 366e0a5f 2019-10-10 stsp if (tree2)
1479 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1480 44392932 2019-08-25 stsp return err;
1481 44392932 2019-08-25 stsp }
1482 44392932 2019-08-25 stsp
1483 44392932 2019-08-25 stsp static const struct got_error *
1484 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1485 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1486 44392932 2019-08-25 stsp {
1487 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1488 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1489 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1490 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1491 44392932 2019-08-25 stsp struct got_object_qid *qid;
1492 44392932 2019-08-25 stsp
1493 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1494 44392932 2019-08-25 stsp if (qid != NULL) {
1495 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1496 44392932 2019-08-25 stsp qid->id);
1497 44392932 2019-08-25 stsp if (err)
1498 44392932 2019-08-25 stsp return err;
1499 44392932 2019-08-25 stsp }
1500 44392932 2019-08-25 stsp
1501 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1502 44392932 2019-08-25 stsp int obj_type;
1503 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1504 44392932 2019-08-25 stsp if (err)
1505 44392932 2019-08-25 stsp goto done;
1506 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1507 44392932 2019-08-25 stsp if (err) {
1508 44392932 2019-08-25 stsp free(obj_id2);
1509 44392932 2019-08-25 stsp goto done;
1510 44392932 2019-08-25 stsp }
1511 44392932 2019-08-25 stsp if (pcommit) {
1512 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1513 44392932 2019-08-25 stsp qid->id, path);
1514 44392932 2019-08-25 stsp if (err) {
1515 44392932 2019-08-25 stsp free(obj_id2);
1516 44392932 2019-08-25 stsp goto done;
1517 44392932 2019-08-25 stsp }
1518 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1519 44392932 2019-08-25 stsp if (err) {
1520 44392932 2019-08-25 stsp free(obj_id2);
1521 44392932 2019-08-25 stsp goto done;
1522 44392932 2019-08-25 stsp }
1523 44392932 2019-08-25 stsp }
1524 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1525 44392932 2019-08-25 stsp if (err) {
1526 44392932 2019-08-25 stsp free(obj_id2);
1527 44392932 2019-08-25 stsp goto done;
1528 44392932 2019-08-25 stsp }
1529 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1530 44392932 2019-08-25 stsp switch (obj_type) {
1531 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1532 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1533 63035f9f 2019-10-06 stsp 0, repo);
1534 44392932 2019-08-25 stsp break;
1535 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1536 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1537 63035f9f 2019-10-06 stsp 0, repo);
1538 44392932 2019-08-25 stsp break;
1539 44392932 2019-08-25 stsp default:
1540 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1541 44392932 2019-08-25 stsp break;
1542 44392932 2019-08-25 stsp }
1543 44392932 2019-08-25 stsp free(obj_id1);
1544 44392932 2019-08-25 stsp free(obj_id2);
1545 44392932 2019-08-25 stsp } else {
1546 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1547 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1548 44392932 2019-08-25 stsp if (err)
1549 44392932 2019-08-25 stsp goto done;
1550 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1551 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1552 44392932 2019-08-25 stsp if (err)
1553 44392932 2019-08-25 stsp goto done;
1554 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1555 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1556 44392932 2019-08-25 stsp }
1557 44392932 2019-08-25 stsp
1558 44392932 2019-08-25 stsp done:
1559 0f2b3dca 2018-12-22 stsp free(id_str1);
1560 0f2b3dca 2018-12-22 stsp free(id_str2);
1561 44392932 2019-08-25 stsp if (pcommit)
1562 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1563 79109fed 2018-03-27 stsp return err;
1564 79109fed 2018-03-27 stsp }
1565 79109fed 2018-03-27 stsp
1566 4bb494d5 2018-06-16 stsp static char *
1567 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1568 6c281f94 2018-06-11 stsp {
1569 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1570 09867e48 2019-08-13 stsp char *p, *s;
1571 09867e48 2019-08-13 stsp
1572 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1573 09867e48 2019-08-13 stsp if (tm == NULL)
1574 09867e48 2019-08-13 stsp return NULL;
1575 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1576 09867e48 2019-08-13 stsp if (s == NULL)
1577 09867e48 2019-08-13 stsp return NULL;
1578 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1579 6c281f94 2018-06-11 stsp if (p)
1580 6c281f94 2018-06-11 stsp *p = '\0';
1581 6c281f94 2018-06-11 stsp return s;
1582 6c281f94 2018-06-11 stsp }
1583 dc424a06 2019-08-07 stsp
1584 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1585 6c281f94 2018-06-11 stsp
1586 79109fed 2018-03-27 stsp static const struct got_error *
1587 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1588 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1589 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1590 79109fed 2018-03-27 stsp {
1591 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1592 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1593 6c281f94 2018-06-11 stsp char datebuf[26];
1594 45d799e2 2018-12-23 stsp time_t committer_time;
1595 45d799e2 2018-12-23 stsp const char *author, *committer;
1596 199a4027 2019-02-02 stsp char *refs_str = NULL;
1597 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1598 5c860e29 2018-03-12 stsp
1599 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1600 199a4027 2019-02-02 stsp char *s;
1601 199a4027 2019-02-02 stsp const char *name;
1602 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1603 a436ad14 2019-08-13 stsp int cmp;
1604 a436ad14 2019-08-13 stsp
1605 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1606 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1607 d9498b20 2019-02-05 stsp continue;
1608 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1609 199a4027 2019-02-02 stsp name += 5;
1610 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1611 7143d404 2019-03-12 stsp continue;
1612 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1613 e34f9ed6 2019-02-02 stsp name += 6;
1614 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1615 141c2bff 2019-02-04 stsp name += 8;
1616 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1617 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1618 5d844a1e 2019-08-13 stsp if (err) {
1619 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1620 5d844a1e 2019-08-13 stsp return err;
1621 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1622 5d844a1e 2019-08-13 stsp err = NULL;
1623 5d844a1e 2019-08-13 stsp tag = NULL;
1624 5d844a1e 2019-08-13 stsp }
1625 a436ad14 2019-08-13 stsp }
1626 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1627 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1628 a436ad14 2019-08-13 stsp if (tag)
1629 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1630 a436ad14 2019-08-13 stsp if (cmp != 0)
1631 a436ad14 2019-08-13 stsp continue;
1632 199a4027 2019-02-02 stsp s = refs_str;
1633 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1634 199a4027 2019-02-02 stsp name) == -1) {
1635 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1636 199a4027 2019-02-02 stsp free(s);
1637 34ca4898 2019-08-13 stsp return err;
1638 199a4027 2019-02-02 stsp }
1639 199a4027 2019-02-02 stsp free(s);
1640 199a4027 2019-02-02 stsp }
1641 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1642 8bf5b3c9 2018-03-17 stsp if (err)
1643 8bf5b3c9 2018-03-17 stsp return err;
1644 788c352e 2018-06-16 stsp
1645 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1646 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1647 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1648 832c249c 2018-06-10 stsp free(id_str);
1649 d3d493d7 2019-02-21 stsp id_str = NULL;
1650 d3d493d7 2019-02-21 stsp free(refs_str);
1651 d3d493d7 2019-02-21 stsp refs_str = NULL;
1652 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1653 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1654 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1655 09867e48 2019-08-13 stsp if (datestr)
1656 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1657 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1658 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1659 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1660 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1661 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1662 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1663 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1664 3fe1abad 2018-06-10 stsp int n = 1;
1665 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1666 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1667 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1668 a0603db2 2018-06-10 stsp if (err)
1669 a0603db2 2018-06-10 stsp return err;
1670 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1671 a0603db2 2018-06-10 stsp free(id_str);
1672 a0603db2 2018-06-10 stsp }
1673 a0603db2 2018-06-10 stsp }
1674 832c249c 2018-06-10 stsp
1675 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1676 5943eee2 2019-08-13 stsp if (err)
1677 5943eee2 2019-08-13 stsp return err;
1678 8bf5b3c9 2018-03-17 stsp
1679 621015ac 2018-07-23 stsp logmsg = logmsg0;
1680 832c249c 2018-06-10 stsp do {
1681 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1682 832c249c 2018-06-10 stsp if (line)
1683 832c249c 2018-06-10 stsp printf(" %s\n", line);
1684 832c249c 2018-06-10 stsp } while (line);
1685 621015ac 2018-07-23 stsp free(logmsg0);
1686 832c249c 2018-06-10 stsp
1687 971751ac 2018-03-27 stsp if (show_patch) {
1688 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1689 971751ac 2018-03-27 stsp if (err == 0)
1690 971751ac 2018-03-27 stsp printf("\n");
1691 971751ac 2018-03-27 stsp }
1692 07862c20 2018-09-15 stsp
1693 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1694 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1695 07862c20 2018-09-15 stsp return err;
1696 f42b1b34 2018-03-12 stsp }
1697 5c860e29 2018-03-12 stsp
1698 f42b1b34 2018-03-12 stsp static const struct got_error *
1699 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1700 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1701 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1702 f42b1b34 2018-03-12 stsp {
1703 f42b1b34 2018-03-12 stsp const struct got_error *err;
1704 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1705 372ccdbb 2018-06-10 stsp
1706 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1707 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1708 f42b1b34 2018-03-12 stsp if (err)
1709 f42b1b34 2018-03-12 stsp return err;
1710 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1711 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1712 372ccdbb 2018-06-10 stsp if (err)
1713 fcc85cad 2018-07-22 stsp goto done;
1714 656b1f76 2019-05-11 jcs for (;;) {
1715 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1716 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1717 8bf5b3c9 2018-03-17 stsp
1718 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1719 84453469 2018-11-11 stsp break;
1720 84453469 2018-11-11 stsp
1721 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1722 372ccdbb 2018-06-10 stsp if (err) {
1723 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1724 9ba79e04 2018-06-11 stsp err = NULL;
1725 9ba79e04 2018-06-11 stsp break;
1726 9ba79e04 2018-06-11 stsp }
1727 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1728 372ccdbb 2018-06-10 stsp break;
1729 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1730 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1731 372ccdbb 2018-06-10 stsp if (err)
1732 372ccdbb 2018-06-10 stsp break;
1733 372ccdbb 2018-06-10 stsp else
1734 372ccdbb 2018-06-10 stsp continue;
1735 7e665116 2018-04-02 stsp }
1736 b43fbaa0 2018-06-11 stsp if (id == NULL)
1737 7e665116 2018-04-02 stsp break;
1738 b43fbaa0 2018-06-11 stsp
1739 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1740 b43fbaa0 2018-06-11 stsp if (err)
1741 fcc85cad 2018-07-22 stsp break;
1742 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1743 44392932 2019-08-25 stsp diff_context, refs);
1744 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1745 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1746 7e665116 2018-04-02 stsp break;
1747 31cedeaf 2018-09-15 stsp }
1748 fcc85cad 2018-07-22 stsp done:
1749 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1750 f42b1b34 2018-03-12 stsp return err;
1751 f42b1b34 2018-03-12 stsp }
1752 5c860e29 2018-03-12 stsp
1753 4ed7e80c 2018-05-20 stsp __dead static void
1754 6f3d1eb0 2018-03-12 stsp usage_log(void)
1755 6f3d1eb0 2018-03-12 stsp {
1756 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1757 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1758 6f3d1eb0 2018-03-12 stsp exit(1);
1759 b1ebc001 2019-08-13 stsp }
1760 b1ebc001 2019-08-13 stsp
1761 b1ebc001 2019-08-13 stsp static int
1762 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1763 b1ebc001 2019-08-13 stsp {
1764 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1765 b1ebc001 2019-08-13 stsp long long n;
1766 b1ebc001 2019-08-13 stsp const char *errstr;
1767 b1ebc001 2019-08-13 stsp
1768 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1769 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1770 b1ebc001 2019-08-13 stsp return 0;
1771 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1772 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1773 b1ebc001 2019-08-13 stsp return 0;
1774 b1ebc001 2019-08-13 stsp return n;
1775 6f3d1eb0 2018-03-12 stsp }
1776 6f3d1eb0 2018-03-12 stsp
1777 4ed7e80c 2018-05-20 stsp static const struct got_error *
1778 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1779 f42b1b34 2018-03-12 stsp {
1780 f42b1b34 2018-03-12 stsp const struct got_error *error;
1781 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1782 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1783 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1784 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1785 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1786 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1787 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1788 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1789 64a96a6d 2018-04-01 stsp const char *errstr;
1790 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1791 1b3893a2 2019-03-18 stsp
1792 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1793 5c860e29 2018-03-12 stsp
1794 6715a751 2018-03-16 stsp #ifndef PROFILE
1795 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1796 6098196c 2019-01-04 stsp NULL)
1797 ad242220 2018-09-08 stsp == -1)
1798 f42b1b34 2018-03-12 stsp err(1, "pledge");
1799 6715a751 2018-03-16 stsp #endif
1800 79109fed 2018-03-27 stsp
1801 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1802 b1ebc001 2019-08-13 stsp
1803 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1804 79109fed 2018-03-27 stsp switch (ch) {
1805 79109fed 2018-03-27 stsp case 'p':
1806 79109fed 2018-03-27 stsp show_patch = 1;
1807 d142fc45 2018-04-01 stsp break;
1808 d142fc45 2018-04-01 stsp case 'c':
1809 d142fc45 2018-04-01 stsp start_commit = optarg;
1810 64a96a6d 2018-04-01 stsp break;
1811 c0cc5c62 2018-10-18 stsp case 'C':
1812 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1813 4a8520aa 2018-10-18 stsp &errstr);
1814 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1815 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1816 c0cc5c62 2018-10-18 stsp break;
1817 64a96a6d 2018-04-01 stsp case 'l':
1818 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1819 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1820 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1821 cc54c501 2019-07-15 stsp break;
1822 cc54c501 2019-07-15 stsp case 'f':
1823 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1824 a0603db2 2018-06-10 stsp break;
1825 04ca23f4 2018-07-16 stsp case 'r':
1826 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1827 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1828 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1829 9ba1d308 2019-10-21 stsp optarg);
1830 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1831 04ca23f4 2018-07-16 stsp break;
1832 79109fed 2018-03-27 stsp default:
1833 2deda0b9 2019-03-07 stsp usage_log();
1834 79109fed 2018-03-27 stsp /* NOTREACHED */
1835 79109fed 2018-03-27 stsp }
1836 79109fed 2018-03-27 stsp }
1837 79109fed 2018-03-27 stsp
1838 79109fed 2018-03-27 stsp argc -= optind;
1839 79109fed 2018-03-27 stsp argv += optind;
1840 f42b1b34 2018-03-12 stsp
1841 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1842 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1843 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1844 04ca23f4 2018-07-16 stsp goto done;
1845 04ca23f4 2018-07-16 stsp }
1846 cffc0aa4 2019-02-05 stsp
1847 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1848 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1849 cffc0aa4 2019-02-05 stsp goto done;
1850 cffc0aa4 2019-02-05 stsp error = NULL;
1851 cffc0aa4 2019-02-05 stsp
1852 e7301579 2019-03-18 stsp if (argc == 0) {
1853 cbd1af7a 2019-03-18 stsp path = strdup("");
1854 e7301579 2019-03-18 stsp if (path == NULL) {
1855 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1856 cbd1af7a 2019-03-18 stsp goto done;
1857 e7301579 2019-03-18 stsp }
1858 e7301579 2019-03-18 stsp } else if (argc == 1) {
1859 e7301579 2019-03-18 stsp if (worktree) {
1860 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1861 e7301579 2019-03-18 stsp argv[0]);
1862 e7301579 2019-03-18 stsp if (error)
1863 e7301579 2019-03-18 stsp goto done;
1864 e7301579 2019-03-18 stsp } else {
1865 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1866 e7301579 2019-03-18 stsp if (path == NULL) {
1867 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1868 e7301579 2019-03-18 stsp goto done;
1869 e7301579 2019-03-18 stsp }
1870 e7301579 2019-03-18 stsp }
1871 cbd1af7a 2019-03-18 stsp } else
1872 cbd1af7a 2019-03-18 stsp usage_log();
1873 cbd1af7a 2019-03-18 stsp
1874 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1875 5486daa2 2019-05-11 stsp repo_path = worktree ?
1876 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1877 5486daa2 2019-05-11 stsp }
1878 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1879 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1880 cffc0aa4 2019-02-05 stsp goto done;
1881 04ca23f4 2018-07-16 stsp }
1882 6098196c 2019-01-04 stsp
1883 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1884 d7d4f210 2018-03-12 stsp if (error != NULL)
1885 04ca23f4 2018-07-16 stsp goto done;
1886 f42b1b34 2018-03-12 stsp
1887 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1888 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1889 c02c541e 2019-03-29 stsp if (error)
1890 c02c541e 2019-03-29 stsp goto done;
1891 c02c541e 2019-03-29 stsp
1892 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1893 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1894 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1895 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1896 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1897 3235492e 2018-04-01 stsp if (error != NULL)
1898 3235492e 2018-04-01 stsp return error;
1899 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1900 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1901 3235492e 2018-04-01 stsp if (error != NULL)
1902 3235492e 2018-04-01 stsp return error;
1903 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1904 3235492e 2018-04-01 stsp } else {
1905 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1906 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1907 3235492e 2018-04-01 stsp if (error == NULL) {
1908 1caad61b 2019-02-01 stsp int obj_type;
1909 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1910 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1911 d1f2edc9 2018-06-13 stsp if (error != NULL)
1912 1caad61b 2019-02-01 stsp goto done;
1913 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1914 1caad61b 2019-02-01 stsp if (error != NULL)
1915 1caad61b 2019-02-01 stsp goto done;
1916 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1917 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1918 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1919 1caad61b 2019-02-01 stsp if (error != NULL)
1920 1caad61b 2019-02-01 stsp goto done;
1921 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1922 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1923 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1924 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1925 1caad61b 2019-02-01 stsp goto done;
1926 1caad61b 2019-02-01 stsp }
1927 1caad61b 2019-02-01 stsp free(id);
1928 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1929 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1930 1caad61b 2019-02-01 stsp if (id == NULL)
1931 638f9024 2019-05-13 stsp error = got_error_from_errno(
1932 230a42bd 2019-05-11 jcs "got_object_id_dup");
1933 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1934 1caad61b 2019-02-01 stsp if (error)
1935 1caad61b 2019-02-01 stsp goto done;
1936 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1937 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1938 1caad61b 2019-02-01 stsp goto done;
1939 1caad61b 2019-02-01 stsp }
1940 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1941 d1f2edc9 2018-06-13 stsp if (error != NULL)
1942 1caad61b 2019-02-01 stsp goto done;
1943 d1f2edc9 2018-06-13 stsp }
1944 15a94983 2018-12-23 stsp if (commit == NULL) {
1945 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1946 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1947 d1f2edc9 2018-06-13 stsp if (error != NULL)
1948 d1f2edc9 2018-06-13 stsp return error;
1949 3235492e 2018-04-01 stsp }
1950 3235492e 2018-04-01 stsp }
1951 d7d4f210 2018-03-12 stsp if (error != NULL)
1952 04ca23f4 2018-07-16 stsp goto done;
1953 04ca23f4 2018-07-16 stsp
1954 deeabeae 2019-08-27 stsp if (worktree) {
1955 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1956 deeabeae 2019-08-27 stsp char *p;
1957 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
1958 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1959 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
1960 deeabeae 2019-08-27 stsp goto done;
1961 deeabeae 2019-08-27 stsp }
1962 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1963 deeabeae 2019-08-27 stsp free(p);
1964 deeabeae 2019-08-27 stsp } else
1965 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1966 04ca23f4 2018-07-16 stsp if (error != NULL)
1967 04ca23f4 2018-07-16 stsp goto done;
1968 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1969 04ca23f4 2018-07-16 stsp free(path);
1970 04ca23f4 2018-07-16 stsp path = in_repo_path;
1971 04ca23f4 2018-07-16 stsp }
1972 199a4027 2019-02-02 stsp
1973 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1974 199a4027 2019-02-02 stsp if (error)
1975 199a4027 2019-02-02 stsp goto done;
1976 04ca23f4 2018-07-16 stsp
1977 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1978 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1979 04ca23f4 2018-07-16 stsp done:
1980 04ca23f4 2018-07-16 stsp free(path);
1981 04ca23f4 2018-07-16 stsp free(repo_path);
1982 04ca23f4 2018-07-16 stsp free(cwd);
1983 f42b1b34 2018-03-12 stsp free(id);
1984 cffc0aa4 2019-02-05 stsp if (worktree)
1985 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1986 ad242220 2018-09-08 stsp if (repo) {
1987 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1988 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1989 ad242220 2018-09-08 stsp if (error == NULL)
1990 ad242220 2018-09-08 stsp error = repo_error;
1991 ad242220 2018-09-08 stsp }
1992 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1993 8bf5b3c9 2018-03-17 stsp return error;
1994 5c860e29 2018-03-12 stsp }
1995 5c860e29 2018-03-12 stsp
1996 4ed7e80c 2018-05-20 stsp __dead static void
1997 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1998 3f8b7d6a 2018-04-01 stsp {
1999 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2000 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2001 3f8b7d6a 2018-04-01 stsp exit(1);
2002 b00d56cd 2018-04-01 stsp }
2003 b00d56cd 2018-04-01 stsp
2004 b72f483a 2019-02-05 stsp struct print_diff_arg {
2005 b72f483a 2019-02-05 stsp struct got_repository *repo;
2006 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2007 b72f483a 2019-02-05 stsp int diff_context;
2008 3fc0c068 2019-02-10 stsp const char *id_str;
2009 3fc0c068 2019-02-10 stsp int header_shown;
2010 98eaaa12 2019-08-03 stsp int diff_staged;
2011 63035f9f 2019-10-06 stsp int ignore_whitespace;
2012 b72f483a 2019-02-05 stsp };
2013 b72f483a 2019-02-05 stsp
2014 4ed7e80c 2018-05-20 stsp static const struct got_error *
2015 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2016 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2017 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2018 b72f483a 2019-02-05 stsp {
2019 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2020 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2021 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2022 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2023 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2024 b72f483a 2019-02-05 stsp struct stat sb;
2025 b72f483a 2019-02-05 stsp
2026 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2027 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2028 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2029 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2030 98eaaa12 2019-08-03 stsp return NULL;
2031 98eaaa12 2019-08-03 stsp } else {
2032 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2033 98eaaa12 2019-08-03 stsp return NULL;
2034 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2035 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2036 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2037 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2038 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2039 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2040 98eaaa12 2019-08-03 stsp return NULL;
2041 98eaaa12 2019-08-03 stsp }
2042 3fc0c068 2019-02-10 stsp
2043 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2044 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2045 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2046 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2047 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2048 3fc0c068 2019-02-10 stsp }
2049 b72f483a 2019-02-05 stsp
2050 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2051 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2052 98eaaa12 2019-08-03 stsp switch (staged_status) {
2053 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2054 98eaaa12 2019-08-03 stsp label1 = path;
2055 98eaaa12 2019-08-03 stsp label2 = path;
2056 98eaaa12 2019-08-03 stsp break;
2057 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2058 98eaaa12 2019-08-03 stsp label2 = path;
2059 98eaaa12 2019-08-03 stsp break;
2060 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2061 98eaaa12 2019-08-03 stsp label1 = path;
2062 98eaaa12 2019-08-03 stsp break;
2063 98eaaa12 2019-08-03 stsp default:
2064 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2065 98eaaa12 2019-08-03 stsp }
2066 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2067 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2068 63035f9f 2019-10-06 stsp a->repo, stdout);
2069 98eaaa12 2019-08-03 stsp }
2070 98eaaa12 2019-08-03 stsp
2071 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2072 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2073 4ce46740 2019-08-08 stsp char *id_str;
2074 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2075 408b4ebc 2019-08-03 stsp 8192);
2076 4ce46740 2019-08-08 stsp if (err)
2077 4ce46740 2019-08-08 stsp goto done;
2078 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2079 4ce46740 2019-08-08 stsp if (err)
2080 4ce46740 2019-08-08 stsp goto done;
2081 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2082 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2083 4ce46740 2019-08-08 stsp free(id_str);
2084 4ce46740 2019-08-08 stsp goto done;
2085 4ce46740 2019-08-08 stsp }
2086 4ce46740 2019-08-08 stsp free(id_str);
2087 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2088 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2089 4ce46740 2019-08-08 stsp if (err)
2090 4ce46740 2019-08-08 stsp goto done;
2091 4ce46740 2019-08-08 stsp }
2092 d00136be 2019-03-26 stsp
2093 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2094 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2095 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2096 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2097 2ec1f75b 2019-03-26 stsp goto done;
2098 2ec1f75b 2019-03-26 stsp }
2099 b72f483a 2019-02-05 stsp
2100 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
2101 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
2102 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
2103 2ec1f75b 2019-03-26 stsp goto done;
2104 2ec1f75b 2019-03-26 stsp }
2105 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
2106 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
2107 2ec1f75b 2019-03-26 stsp goto done;
2108 2ec1f75b 2019-03-26 stsp }
2109 2ec1f75b 2019-03-26 stsp } else
2110 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2111 b72f483a 2019-02-05 stsp
2112 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2113 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2114 b72f483a 2019-02-05 stsp done:
2115 b72f483a 2019-02-05 stsp if (blob1)
2116 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2117 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2118 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2119 b72f483a 2019-02-05 stsp free(abspath);
2120 927df6b7 2019-02-10 stsp return err;
2121 927df6b7 2019-02-10 stsp }
2122 927df6b7 2019-02-10 stsp
2123 927df6b7 2019-02-10 stsp static const struct got_error *
2124 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2125 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2126 01073a5d 2019-08-22 stsp struct got_repository *repo)
2127 0adb7196 2019-08-11 stsp {
2128 0adb7196 2019-08-11 stsp const struct got_error *err;
2129 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2130 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2131 0adb7196 2019-08-11 stsp
2132 0adb7196 2019-08-11 stsp *id = NULL;
2133 0adb7196 2019-08-11 stsp *label = NULL;
2134 d24820bf 2019-08-11 stsp
2135 01073a5d 2019-08-22 stsp if (resolve_tags) {
2136 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2137 01073a5d 2019-08-22 stsp repo);
2138 01073a5d 2019-08-22 stsp if (err == NULL) {
2139 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2140 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2141 01073a5d 2019-08-22 stsp if (*id == NULL)
2142 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2143 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2144 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2145 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2146 32f0ab81 2019-08-28 hiltjo free(*id);
2147 01073a5d 2019-08-22 stsp *id = NULL;
2148 01073a5d 2019-08-22 stsp }
2149 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2150 01073a5d 2019-08-22 stsp return err;
2151 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2152 01073a5d 2019-08-22 stsp return err;
2153 01073a5d 2019-08-22 stsp }
2154 0adb7196 2019-08-11 stsp
2155 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2156 0adb7196 2019-08-11 stsp if (err) {
2157 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2158 0adb7196 2019-08-11 stsp return err;
2159 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2160 0adb7196 2019-08-11 stsp if (err != NULL)
2161 0adb7196 2019-08-11 stsp goto done;
2162 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2163 0adb7196 2019-08-11 stsp if (*label == NULL) {
2164 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2165 0adb7196 2019-08-11 stsp goto done;
2166 0adb7196 2019-08-11 stsp }
2167 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2168 0adb7196 2019-08-11 stsp } else {
2169 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2170 0adb7196 2019-08-11 stsp if (*label == NULL) {
2171 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2172 0adb7196 2019-08-11 stsp goto done;
2173 0adb7196 2019-08-11 stsp }
2174 0adb7196 2019-08-11 stsp }
2175 0adb7196 2019-08-11 stsp done:
2176 0adb7196 2019-08-11 stsp if (ref)
2177 0adb7196 2019-08-11 stsp got_ref_close(ref);
2178 0adb7196 2019-08-11 stsp return err;
2179 0adb7196 2019-08-11 stsp }
2180 0adb7196 2019-08-11 stsp
2181 0adb7196 2019-08-11 stsp
2182 0adb7196 2019-08-11 stsp static const struct got_error *
2183 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2184 b00d56cd 2018-04-01 stsp {
2185 b00d56cd 2018-04-01 stsp const struct got_error *error;
2186 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2187 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2188 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2189 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2190 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2191 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2192 15a94983 2018-12-23 stsp int type1, type2;
2193 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2194 c0cc5c62 2018-10-18 stsp const char *errstr;
2195 927df6b7 2019-02-10 stsp char *path = NULL;
2196 b00d56cd 2018-04-01 stsp
2197 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2198 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2199 25eccc22 2019-01-04 stsp NULL) == -1)
2200 b00d56cd 2018-04-01 stsp err(1, "pledge");
2201 b00d56cd 2018-04-01 stsp #endif
2202 b00d56cd 2018-04-01 stsp
2203 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2204 b00d56cd 2018-04-01 stsp switch (ch) {
2205 c0cc5c62 2018-10-18 stsp case 'C':
2206 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2207 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2208 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2209 c0cc5c62 2018-10-18 stsp break;
2210 b72f483a 2019-02-05 stsp case 'r':
2211 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2212 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2213 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2214 9ba1d308 2019-10-21 stsp optarg);
2215 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2216 b72f483a 2019-02-05 stsp break;
2217 98eaaa12 2019-08-03 stsp case 's':
2218 98eaaa12 2019-08-03 stsp diff_staged = 1;
2219 63035f9f 2019-10-06 stsp break;
2220 63035f9f 2019-10-06 stsp case 'w':
2221 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2222 98eaaa12 2019-08-03 stsp break;
2223 b00d56cd 2018-04-01 stsp default:
2224 2deda0b9 2019-03-07 stsp usage_diff();
2225 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2226 b00d56cd 2018-04-01 stsp }
2227 b00d56cd 2018-04-01 stsp }
2228 b00d56cd 2018-04-01 stsp
2229 b00d56cd 2018-04-01 stsp argc -= optind;
2230 b00d56cd 2018-04-01 stsp argv += optind;
2231 b00d56cd 2018-04-01 stsp
2232 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2233 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2234 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2235 b72f483a 2019-02-05 stsp goto done;
2236 b72f483a 2019-02-05 stsp }
2237 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2238 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2239 927df6b7 2019-02-10 stsp goto done;
2240 927df6b7 2019-02-10 stsp if (argc <= 1) {
2241 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2242 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2243 927df6b7 2019-02-10 stsp goto done;
2244 927df6b7 2019-02-10 stsp }
2245 b72f483a 2019-02-05 stsp if (repo_path)
2246 b72f483a 2019-02-05 stsp errx(1,
2247 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2248 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2249 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2250 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2251 927df6b7 2019-02-10 stsp goto done;
2252 927df6b7 2019-02-10 stsp }
2253 927df6b7 2019-02-10 stsp if (argc == 1) {
2254 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2255 cbd1af7a 2019-03-18 stsp argv[0]);
2256 927df6b7 2019-02-10 stsp if (error)
2257 927df6b7 2019-02-10 stsp goto done;
2258 927df6b7 2019-02-10 stsp } else {
2259 927df6b7 2019-02-10 stsp path = strdup("");
2260 927df6b7 2019-02-10 stsp if (path == NULL) {
2261 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2262 927df6b7 2019-02-10 stsp goto done;
2263 927df6b7 2019-02-10 stsp }
2264 927df6b7 2019-02-10 stsp }
2265 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2266 98eaaa12 2019-08-03 stsp if (diff_staged)
2267 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2268 98eaaa12 2019-08-03 stsp "objects in repository");
2269 15a94983 2018-12-23 stsp id_str1 = argv[0];
2270 15a94983 2018-12-23 stsp id_str2 = argv[1];
2271 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2272 30db809c 2019-06-05 stsp repo_path =
2273 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2274 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2275 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2276 30db809c 2019-06-05 stsp goto done;
2277 30db809c 2019-06-05 stsp }
2278 30db809c 2019-06-05 stsp }
2279 b00d56cd 2018-04-01 stsp } else
2280 b00d56cd 2018-04-01 stsp usage_diff();
2281 25eccc22 2019-01-04 stsp
2282 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2283 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2284 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2285 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2286 b72f483a 2019-02-05 stsp }
2287 b00d56cd 2018-04-01 stsp
2288 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2289 76089277 2018-04-01 stsp free(repo_path);
2290 b00d56cd 2018-04-01 stsp if (error != NULL)
2291 b00d56cd 2018-04-01 stsp goto done;
2292 b00d56cd 2018-04-01 stsp
2293 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2294 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2295 c02c541e 2019-03-29 stsp if (error)
2296 c02c541e 2019-03-29 stsp goto done;
2297 c02c541e 2019-03-29 stsp
2298 30db809c 2019-06-05 stsp if (argc <= 1) {
2299 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2300 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2301 b72f483a 2019-02-05 stsp char *id_str;
2302 72ea6654 2019-07-27 stsp
2303 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2304 72ea6654 2019-07-27 stsp
2305 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2306 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2307 b72f483a 2019-02-05 stsp if (error)
2308 b72f483a 2019-02-05 stsp goto done;
2309 b72f483a 2019-02-05 stsp arg.repo = repo;
2310 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2311 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2312 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2313 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2314 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2315 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2316 b72f483a 2019-02-05 stsp
2317 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2318 72ea6654 2019-07-27 stsp if (error)
2319 72ea6654 2019-07-27 stsp goto done;
2320 72ea6654 2019-07-27 stsp
2321 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2322 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2323 b72f483a 2019-02-05 stsp free(id_str);
2324 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2325 b72f483a 2019-02-05 stsp goto done;
2326 b72f483a 2019-02-05 stsp }
2327 b72f483a 2019-02-05 stsp
2328 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2329 01073a5d 2019-08-22 stsp repo);
2330 0adb7196 2019-08-11 stsp if (error)
2331 0adb7196 2019-08-11 stsp goto done;
2332 b00d56cd 2018-04-01 stsp
2333 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2334 01073a5d 2019-08-22 stsp repo);
2335 0adb7196 2019-08-11 stsp if (error)
2336 0adb7196 2019-08-11 stsp goto done;
2337 b00d56cd 2018-04-01 stsp
2338 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2339 15a94983 2018-12-23 stsp if (error)
2340 15a94983 2018-12-23 stsp goto done;
2341 15a94983 2018-12-23 stsp
2342 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2343 15a94983 2018-12-23 stsp if (error)
2344 15a94983 2018-12-23 stsp goto done;
2345 15a94983 2018-12-23 stsp
2346 15a94983 2018-12-23 stsp if (type1 != type2) {
2347 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2348 b00d56cd 2018-04-01 stsp goto done;
2349 b00d56cd 2018-04-01 stsp }
2350 b00d56cd 2018-04-01 stsp
2351 15a94983 2018-12-23 stsp switch (type1) {
2352 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2353 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2354 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2355 b00d56cd 2018-04-01 stsp break;
2356 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2357 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2358 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2359 b00d56cd 2018-04-01 stsp break;
2360 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2361 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2362 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2363 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2364 b00d56cd 2018-04-01 stsp break;
2365 b00d56cd 2018-04-01 stsp default:
2366 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2367 b00d56cd 2018-04-01 stsp }
2368 b00d56cd 2018-04-01 stsp
2369 b00d56cd 2018-04-01 stsp done:
2370 5e70831e 2019-05-28 stsp free(label1);
2371 5e70831e 2019-05-28 stsp free(label2);
2372 15a94983 2018-12-23 stsp free(id1);
2373 15a94983 2018-12-23 stsp free(id2);
2374 927df6b7 2019-02-10 stsp free(path);
2375 b72f483a 2019-02-05 stsp if (worktree)
2376 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2377 ad242220 2018-09-08 stsp if (repo) {
2378 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2379 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2380 ad242220 2018-09-08 stsp if (error == NULL)
2381 ad242220 2018-09-08 stsp error = repo_error;
2382 ad242220 2018-09-08 stsp }
2383 b00d56cd 2018-04-01 stsp return error;
2384 404c43c4 2018-06-21 stsp }
2385 404c43c4 2018-06-21 stsp
2386 404c43c4 2018-06-21 stsp __dead static void
2387 404c43c4 2018-06-21 stsp usage_blame(void)
2388 404c43c4 2018-06-21 stsp {
2389 9270e621 2019-02-05 stsp fprintf(stderr,
2390 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2391 404c43c4 2018-06-21 stsp getprogname());
2392 404c43c4 2018-06-21 stsp exit(1);
2393 b00d56cd 2018-04-01 stsp }
2394 b00d56cd 2018-04-01 stsp
2395 28315671 2019-08-14 stsp struct blame_line {
2396 28315671 2019-08-14 stsp int annotated;
2397 28315671 2019-08-14 stsp char *id_str;
2398 82f6abb8 2019-08-14 stsp char *committer;
2399 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2400 28315671 2019-08-14 stsp };
2401 28315671 2019-08-14 stsp
2402 28315671 2019-08-14 stsp struct blame_cb_args {
2403 28315671 2019-08-14 stsp struct blame_line *lines;
2404 28315671 2019-08-14 stsp int nlines;
2405 7ef28ff8 2019-08-14 stsp int nlines_prec;
2406 28315671 2019-08-14 stsp int lineno_cur;
2407 28315671 2019-08-14 stsp off_t *line_offsets;
2408 28315671 2019-08-14 stsp FILE *f;
2409 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2410 28315671 2019-08-14 stsp };
2411 28315671 2019-08-14 stsp
2412 404c43c4 2018-06-21 stsp static const struct got_error *
2413 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2414 28315671 2019-08-14 stsp {
2415 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2416 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2417 28315671 2019-08-14 stsp struct blame_line *bline;
2418 82f6abb8 2019-08-14 stsp char *line = NULL;
2419 28315671 2019-08-14 stsp size_t linesize = 0;
2420 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2421 28315671 2019-08-14 stsp off_t offset;
2422 bcb49d15 2019-08-14 stsp struct tm tm;
2423 bcb49d15 2019-08-14 stsp time_t committer_time;
2424 28315671 2019-08-14 stsp
2425 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2426 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2427 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2428 28315671 2019-08-14 stsp
2429 28315671 2019-08-14 stsp if (sigint_received)
2430 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2431 28315671 2019-08-14 stsp
2432 2839f8b9 2019-08-18 stsp if (lineno == -1)
2433 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2434 2839f8b9 2019-08-18 stsp
2435 28315671 2019-08-14 stsp /* Annotate this line. */
2436 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2437 28315671 2019-08-14 stsp if (bline->annotated)
2438 28315671 2019-08-14 stsp return NULL;
2439 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2440 28315671 2019-08-14 stsp if (err)
2441 28315671 2019-08-14 stsp return err;
2442 82f6abb8 2019-08-14 stsp
2443 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2444 82f6abb8 2019-08-14 stsp if (err)
2445 82f6abb8 2019-08-14 stsp goto done;
2446 82f6abb8 2019-08-14 stsp
2447 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2448 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2449 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2450 bcb49d15 2019-08-14 stsp goto done;
2451 bcb49d15 2019-08-14 stsp }
2452 bcb49d15 2019-08-14 stsp
2453 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2454 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2455 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2456 11db6024 2019-10-21 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G/%m/%d",
2457 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2458 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2459 82f6abb8 2019-08-14 stsp goto done;
2460 82f6abb8 2019-08-14 stsp }
2461 28315671 2019-08-14 stsp bline->annotated = 1;
2462 28315671 2019-08-14 stsp
2463 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2464 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2465 28315671 2019-08-14 stsp if (!bline->annotated)
2466 82f6abb8 2019-08-14 stsp goto done;
2467 28315671 2019-08-14 stsp
2468 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2469 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2470 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2471 82f6abb8 2019-08-14 stsp goto done;
2472 82f6abb8 2019-08-14 stsp }
2473 28315671 2019-08-14 stsp
2474 28315671 2019-08-14 stsp while (bline->annotated) {
2475 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2476 82f6abb8 2019-08-14 stsp size_t len;
2477 82f6abb8 2019-08-14 stsp
2478 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2479 28315671 2019-08-14 stsp if (ferror(a->f))
2480 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2481 28315671 2019-08-14 stsp break;
2482 28315671 2019-08-14 stsp }
2483 82f6abb8 2019-08-14 stsp
2484 82f6abb8 2019-08-14 stsp committer = bline->committer;
2485 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2486 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2487 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2488 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2489 82f6abb8 2019-08-14 stsp if (at)
2490 82f6abb8 2019-08-14 stsp *at = '\0';
2491 82f6abb8 2019-08-14 stsp len = strlen(committer);
2492 82f6abb8 2019-08-14 stsp if (len >= 9)
2493 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2494 28315671 2019-08-14 stsp
2495 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2496 28315671 2019-08-14 stsp if (nl)
2497 28315671 2019-08-14 stsp *nl = '\0';
2498 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2499 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2500 28315671 2019-08-14 stsp
2501 28315671 2019-08-14 stsp a->lineno_cur++;
2502 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2503 28315671 2019-08-14 stsp }
2504 82f6abb8 2019-08-14 stsp done:
2505 82f6abb8 2019-08-14 stsp if (commit)
2506 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2507 28315671 2019-08-14 stsp free(line);
2508 28315671 2019-08-14 stsp return err;
2509 28315671 2019-08-14 stsp }
2510 28315671 2019-08-14 stsp
2511 28315671 2019-08-14 stsp static const struct got_error *
2512 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2513 404c43c4 2018-06-21 stsp {
2514 404c43c4 2018-06-21 stsp const struct got_error *error;
2515 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2516 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2517 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2518 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2519 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2520 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2521 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2522 28315671 2019-08-14 stsp struct blame_cb_args bca;
2523 28315671 2019-08-14 stsp int ch, obj_type, i;
2524 b02560ec 2019-08-19 stsp size_t filesize;
2525 90f3c347 2019-08-19 stsp
2526 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2527 404c43c4 2018-06-21 stsp
2528 404c43c4 2018-06-21 stsp #ifndef PROFILE
2529 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2530 36e2fb66 2019-01-04 stsp NULL) == -1)
2531 404c43c4 2018-06-21 stsp err(1, "pledge");
2532 404c43c4 2018-06-21 stsp #endif
2533 404c43c4 2018-06-21 stsp
2534 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2535 404c43c4 2018-06-21 stsp switch (ch) {
2536 404c43c4 2018-06-21 stsp case 'c':
2537 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2538 404c43c4 2018-06-21 stsp break;
2539 66bea077 2018-08-02 stsp case 'r':
2540 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2541 66bea077 2018-08-02 stsp if (repo_path == NULL)
2542 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2543 9ba1d308 2019-10-21 stsp optarg);
2544 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2545 66bea077 2018-08-02 stsp break;
2546 404c43c4 2018-06-21 stsp default:
2547 2deda0b9 2019-03-07 stsp usage_blame();
2548 404c43c4 2018-06-21 stsp /* NOTREACHED */
2549 404c43c4 2018-06-21 stsp }
2550 404c43c4 2018-06-21 stsp }
2551 404c43c4 2018-06-21 stsp
2552 404c43c4 2018-06-21 stsp argc -= optind;
2553 404c43c4 2018-06-21 stsp argv += optind;
2554 404c43c4 2018-06-21 stsp
2555 a39318fd 2018-08-02 stsp if (argc == 1)
2556 404c43c4 2018-06-21 stsp path = argv[0];
2557 a39318fd 2018-08-02 stsp else
2558 404c43c4 2018-06-21 stsp usage_blame();
2559 404c43c4 2018-06-21 stsp
2560 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2561 66bea077 2018-08-02 stsp if (cwd == NULL) {
2562 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2563 66bea077 2018-08-02 stsp goto done;
2564 66bea077 2018-08-02 stsp }
2565 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2566 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2567 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2568 66bea077 2018-08-02 stsp goto done;
2569 0c06baac 2019-02-05 stsp else
2570 0c06baac 2019-02-05 stsp error = NULL;
2571 0c06baac 2019-02-05 stsp if (worktree) {
2572 0c06baac 2019-02-05 stsp repo_path =
2573 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2574 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2575 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2576 0c06baac 2019-02-05 stsp if (error)
2577 0c06baac 2019-02-05 stsp goto done;
2578 0c06baac 2019-02-05 stsp } else {
2579 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2580 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2581 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2582 0c06baac 2019-02-05 stsp goto done;
2583 0c06baac 2019-02-05 stsp }
2584 66bea077 2018-08-02 stsp }
2585 66bea077 2018-08-02 stsp }
2586 36e2fb66 2019-01-04 stsp
2587 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2588 c02c541e 2019-03-29 stsp if (error != NULL)
2589 36e2fb66 2019-01-04 stsp goto done;
2590 66bea077 2018-08-02 stsp
2591 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2592 c02c541e 2019-03-29 stsp if (error)
2593 404c43c4 2018-06-21 stsp goto done;
2594 404c43c4 2018-06-21 stsp
2595 0c06baac 2019-02-05 stsp if (worktree) {
2596 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2597 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2598 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2599 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2600 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2601 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2602 6efaaa2d 2019-02-05 stsp path) == -1) {
2603 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2604 0c06baac 2019-02-05 stsp goto done;
2605 0c06baac 2019-02-05 stsp }
2606 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2607 0c06baac 2019-02-05 stsp free(p);
2608 0c06baac 2019-02-05 stsp } else {
2609 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2610 0c06baac 2019-02-05 stsp }
2611 0c06baac 2019-02-05 stsp if (error)
2612 66bea077 2018-08-02 stsp goto done;
2613 66bea077 2018-08-02 stsp
2614 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2615 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2616 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2617 404c43c4 2018-06-21 stsp if (error != NULL)
2618 66bea077 2018-08-02 stsp goto done;
2619 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2620 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2621 404c43c4 2018-06-21 stsp if (error != NULL)
2622 66bea077 2018-08-02 stsp goto done;
2623 404c43c4 2018-06-21 stsp } else {
2624 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2625 30837e32 2019-07-25 stsp if (error)
2626 66bea077 2018-08-02 stsp goto done;
2627 404c43c4 2018-06-21 stsp }
2628 404c43c4 2018-06-21 stsp
2629 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2630 28315671 2019-08-14 stsp if (error)
2631 28315671 2019-08-14 stsp goto done;
2632 28315671 2019-08-14 stsp if (obj_id == NULL) {
2633 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2634 28315671 2019-08-14 stsp goto done;
2635 28315671 2019-08-14 stsp }
2636 28315671 2019-08-14 stsp
2637 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2638 28315671 2019-08-14 stsp if (error)
2639 28315671 2019-08-14 stsp goto done;
2640 28315671 2019-08-14 stsp
2641 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2642 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2643 28315671 2019-08-14 stsp goto done;
2644 28315671 2019-08-14 stsp }
2645 28315671 2019-08-14 stsp
2646 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2647 28315671 2019-08-14 stsp if (error)
2648 28315671 2019-08-14 stsp goto done;
2649 28315671 2019-08-14 stsp bca.f = got_opentemp();
2650 28315671 2019-08-14 stsp if (bca.f == NULL) {
2651 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2652 28315671 2019-08-14 stsp goto done;
2653 28315671 2019-08-14 stsp }
2654 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2655 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2656 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2657 28315671 2019-08-14 stsp goto done;
2658 28315671 2019-08-14 stsp
2659 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2660 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2661 b02560ec 2019-08-19 stsp bca.nlines--;
2662 b02560ec 2019-08-19 stsp
2663 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2664 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2665 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2666 28315671 2019-08-14 stsp goto done;
2667 28315671 2019-08-14 stsp }
2668 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2669 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2670 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2671 7ef28ff8 2019-08-14 stsp while (i > 0) {
2672 7ef28ff8 2019-08-14 stsp i /= 10;
2673 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2674 7ef28ff8 2019-08-14 stsp }
2675 82f6abb8 2019-08-14 stsp bca.repo = repo;
2676 7ef28ff8 2019-08-14 stsp
2677 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2678 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2679 28315671 2019-08-14 stsp if (error)
2680 28315671 2019-08-14 stsp goto done;
2681 404c43c4 2018-06-21 stsp done:
2682 66bea077 2018-08-02 stsp free(in_repo_path);
2683 66bea077 2018-08-02 stsp free(repo_path);
2684 66bea077 2018-08-02 stsp free(cwd);
2685 404c43c4 2018-06-21 stsp free(commit_id);
2686 28315671 2019-08-14 stsp free(obj_id);
2687 28315671 2019-08-14 stsp if (blob)
2688 28315671 2019-08-14 stsp got_object_blob_close(blob);
2689 0c06baac 2019-02-05 stsp if (worktree)
2690 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2691 ad242220 2018-09-08 stsp if (repo) {
2692 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2693 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2694 ad242220 2018-09-08 stsp if (error == NULL)
2695 ad242220 2018-09-08 stsp error = repo_error;
2696 ad242220 2018-09-08 stsp }
2697 3affba96 2019-09-22 stsp if (bca.lines) {
2698 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2699 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2700 3affba96 2019-09-22 stsp free(bline->id_str);
2701 3affba96 2019-09-22 stsp free(bline->committer);
2702 3affba96 2019-09-22 stsp }
2703 3affba96 2019-09-22 stsp free(bca.lines);
2704 28315671 2019-08-14 stsp }
2705 28315671 2019-08-14 stsp free(bca.line_offsets);
2706 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2707 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2708 404c43c4 2018-06-21 stsp return error;
2709 5de5890b 2018-10-18 stsp }
2710 5de5890b 2018-10-18 stsp
2711 5de5890b 2018-10-18 stsp __dead static void
2712 5de5890b 2018-10-18 stsp usage_tree(void)
2713 5de5890b 2018-10-18 stsp {
2714 c1669e2e 2019-01-09 stsp fprintf(stderr,
2715 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2716 5de5890b 2018-10-18 stsp getprogname());
2717 5de5890b 2018-10-18 stsp exit(1);
2718 5de5890b 2018-10-18 stsp }
2719 5de5890b 2018-10-18 stsp
2720 c1669e2e 2019-01-09 stsp static void
2721 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2722 c1669e2e 2019-01-09 stsp const char *root_path)
2723 c1669e2e 2019-01-09 stsp {
2724 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2725 848d6979 2019-08-12 stsp const char *modestr = "";
2726 5de5890b 2018-10-18 stsp
2727 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2728 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2729 c1669e2e 2019-01-09 stsp path++;
2730 c1669e2e 2019-01-09 stsp
2731 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2732 63c5ca5d 2019-08-24 stsp modestr = "$";
2733 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
2734 848d6979 2019-08-12 stsp modestr = "@";
2735 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2736 848d6979 2019-08-12 stsp modestr = "/";
2737 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2738 848d6979 2019-08-12 stsp modestr = "*";
2739 848d6979 2019-08-12 stsp
2740 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2741 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2742 c1669e2e 2019-01-09 stsp }
2743 c1669e2e 2019-01-09 stsp
2744 5de5890b 2018-10-18 stsp static const struct got_error *
2745 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2746 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2747 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2748 5de5890b 2018-10-18 stsp {
2749 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2750 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2751 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2752 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2753 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2754 5de5890b 2018-10-18 stsp
2755 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2756 5de5890b 2018-10-18 stsp if (err)
2757 5de5890b 2018-10-18 stsp goto done;
2758 5de5890b 2018-10-18 stsp
2759 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2760 5de5890b 2018-10-18 stsp if (err)
2761 5de5890b 2018-10-18 stsp goto done;
2762 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2763 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2764 5de5890b 2018-10-18 stsp while (te) {
2765 5de5890b 2018-10-18 stsp char *id = NULL;
2766 84453469 2018-11-11 stsp
2767 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2768 84453469 2018-11-11 stsp break;
2769 84453469 2018-11-11 stsp
2770 5de5890b 2018-10-18 stsp if (show_ids) {
2771 5de5890b 2018-10-18 stsp char *id_str;
2772 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2773 5de5890b 2018-10-18 stsp if (err)
2774 5de5890b 2018-10-18 stsp goto done;
2775 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2776 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2777 5de5890b 2018-10-18 stsp free(id_str);
2778 5de5890b 2018-10-18 stsp goto done;
2779 5de5890b 2018-10-18 stsp }
2780 5de5890b 2018-10-18 stsp free(id_str);
2781 5de5890b 2018-10-18 stsp }
2782 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2783 5de5890b 2018-10-18 stsp free(id);
2784 c1669e2e 2019-01-09 stsp
2785 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2786 c1669e2e 2019-01-09 stsp char *child_path;
2787 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2788 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2789 c1669e2e 2019-01-09 stsp te->name) == -1) {
2790 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2791 c1669e2e 2019-01-09 stsp goto done;
2792 c1669e2e 2019-01-09 stsp }
2793 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2794 c1669e2e 2019-01-09 stsp root_path, repo);
2795 c1669e2e 2019-01-09 stsp free(child_path);
2796 c1669e2e 2019-01-09 stsp if (err)
2797 c1669e2e 2019-01-09 stsp goto done;
2798 c1669e2e 2019-01-09 stsp }
2799 c1669e2e 2019-01-09 stsp
2800 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2801 5de5890b 2018-10-18 stsp }
2802 5de5890b 2018-10-18 stsp done:
2803 5de5890b 2018-10-18 stsp if (tree)
2804 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2805 5de5890b 2018-10-18 stsp free(tree_id);
2806 5de5890b 2018-10-18 stsp return err;
2807 404c43c4 2018-06-21 stsp }
2808 404c43c4 2018-06-21 stsp
2809 5de5890b 2018-10-18 stsp static const struct got_error *
2810 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2811 5de5890b 2018-10-18 stsp {
2812 5de5890b 2018-10-18 stsp const struct got_error *error;
2813 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2814 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2815 7a2c19d6 2019-02-05 stsp const char *path;
2816 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2817 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2818 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2819 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2820 5de5890b 2018-10-18 stsp int ch;
2821 5de5890b 2018-10-18 stsp
2822 5de5890b 2018-10-18 stsp #ifndef PROFILE
2823 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2824 0f8d269b 2019-01-04 stsp NULL) == -1)
2825 5de5890b 2018-10-18 stsp err(1, "pledge");
2826 5de5890b 2018-10-18 stsp #endif
2827 5de5890b 2018-10-18 stsp
2828 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2829 5de5890b 2018-10-18 stsp switch (ch) {
2830 5de5890b 2018-10-18 stsp case 'c':
2831 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2832 5de5890b 2018-10-18 stsp break;
2833 5de5890b 2018-10-18 stsp case 'r':
2834 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2835 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2836 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2837 9ba1d308 2019-10-21 stsp optarg);
2838 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2839 5de5890b 2018-10-18 stsp break;
2840 5de5890b 2018-10-18 stsp case 'i':
2841 5de5890b 2018-10-18 stsp show_ids = 1;
2842 5de5890b 2018-10-18 stsp break;
2843 c1669e2e 2019-01-09 stsp case 'R':
2844 c1669e2e 2019-01-09 stsp recurse = 1;
2845 c1669e2e 2019-01-09 stsp break;
2846 5de5890b 2018-10-18 stsp default:
2847 2deda0b9 2019-03-07 stsp usage_tree();
2848 5de5890b 2018-10-18 stsp /* NOTREACHED */
2849 5de5890b 2018-10-18 stsp }
2850 5de5890b 2018-10-18 stsp }
2851 5de5890b 2018-10-18 stsp
2852 5de5890b 2018-10-18 stsp argc -= optind;
2853 5de5890b 2018-10-18 stsp argv += optind;
2854 5de5890b 2018-10-18 stsp
2855 5de5890b 2018-10-18 stsp if (argc == 1)
2856 5de5890b 2018-10-18 stsp path = argv[0];
2857 5de5890b 2018-10-18 stsp else if (argc > 1)
2858 5de5890b 2018-10-18 stsp usage_tree();
2859 5de5890b 2018-10-18 stsp else
2860 7a2c19d6 2019-02-05 stsp path = NULL;
2861 7a2c19d6 2019-02-05 stsp
2862 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2863 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2864 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2865 9bf7a39b 2019-02-05 stsp goto done;
2866 9bf7a39b 2019-02-05 stsp }
2867 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2868 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2869 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2870 7a2c19d6 2019-02-05 stsp goto done;
2871 7a2c19d6 2019-02-05 stsp else
2872 7a2c19d6 2019-02-05 stsp error = NULL;
2873 7a2c19d6 2019-02-05 stsp if (worktree) {
2874 7a2c19d6 2019-02-05 stsp repo_path =
2875 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2876 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2877 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2878 7a2c19d6 2019-02-05 stsp if (error)
2879 7a2c19d6 2019-02-05 stsp goto done;
2880 7a2c19d6 2019-02-05 stsp } else {
2881 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2882 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2883 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2884 7a2c19d6 2019-02-05 stsp goto done;
2885 7a2c19d6 2019-02-05 stsp }
2886 5de5890b 2018-10-18 stsp }
2887 5de5890b 2018-10-18 stsp }
2888 5de5890b 2018-10-18 stsp
2889 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2890 5de5890b 2018-10-18 stsp if (error != NULL)
2891 c02c541e 2019-03-29 stsp goto done;
2892 c02c541e 2019-03-29 stsp
2893 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2894 c02c541e 2019-03-29 stsp if (error)
2895 5de5890b 2018-10-18 stsp goto done;
2896 5de5890b 2018-10-18 stsp
2897 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2898 9bf7a39b 2019-02-05 stsp if (worktree) {
2899 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2900 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2901 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2902 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2903 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2905 9bf7a39b 2019-02-05 stsp goto done;
2906 9bf7a39b 2019-02-05 stsp }
2907 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2908 9bf7a39b 2019-02-05 stsp free(p);
2909 9bf7a39b 2019-02-05 stsp if (error)
2910 9bf7a39b 2019-02-05 stsp goto done;
2911 9bf7a39b 2019-02-05 stsp } else
2912 9bf7a39b 2019-02-05 stsp path = "/";
2913 9bf7a39b 2019-02-05 stsp }
2914 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2915 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2916 9bf7a39b 2019-02-05 stsp if (error != NULL)
2917 9bf7a39b 2019-02-05 stsp goto done;
2918 9bf7a39b 2019-02-05 stsp }
2919 5de5890b 2018-10-18 stsp
2920 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2921 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2922 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2923 5de5890b 2018-10-18 stsp if (error != NULL)
2924 5de5890b 2018-10-18 stsp goto done;
2925 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2926 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2927 5de5890b 2018-10-18 stsp if (error != NULL)
2928 5de5890b 2018-10-18 stsp goto done;
2929 5de5890b 2018-10-18 stsp } else {
2930 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2931 30837e32 2019-07-25 stsp if (error)
2932 5de5890b 2018-10-18 stsp goto done;
2933 5de5890b 2018-10-18 stsp }
2934 5de5890b 2018-10-18 stsp
2935 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2936 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2937 5de5890b 2018-10-18 stsp done:
2938 5de5890b 2018-10-18 stsp free(in_repo_path);
2939 5de5890b 2018-10-18 stsp free(repo_path);
2940 5de5890b 2018-10-18 stsp free(cwd);
2941 5de5890b 2018-10-18 stsp free(commit_id);
2942 7a2c19d6 2019-02-05 stsp if (worktree)
2943 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2944 5de5890b 2018-10-18 stsp if (repo) {
2945 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2946 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2947 5de5890b 2018-10-18 stsp if (error == NULL)
2948 5de5890b 2018-10-18 stsp error = repo_error;
2949 5de5890b 2018-10-18 stsp }
2950 5de5890b 2018-10-18 stsp return error;
2951 5de5890b 2018-10-18 stsp }
2952 5de5890b 2018-10-18 stsp
2953 6bad629b 2019-02-04 stsp __dead static void
2954 6bad629b 2019-02-04 stsp usage_status(void)
2955 6bad629b 2019-02-04 stsp {
2956 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2957 6bad629b 2019-02-04 stsp exit(1);
2958 6bad629b 2019-02-04 stsp }
2959 5c860e29 2018-03-12 stsp
2960 b72f483a 2019-02-05 stsp static const struct got_error *
2961 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2962 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2963 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2964 6bad629b 2019-02-04 stsp {
2965 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2966 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2967 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2968 b72f483a 2019-02-05 stsp return NULL;
2969 6bad629b 2019-02-04 stsp }
2970 5c860e29 2018-03-12 stsp
2971 6bad629b 2019-02-04 stsp static const struct got_error *
2972 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2973 6bad629b 2019-02-04 stsp {
2974 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2975 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2976 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2977 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2978 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2979 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2980 a5edda0a 2019-07-27 stsp int ch;
2981 5c860e29 2018-03-12 stsp
2982 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2983 72ea6654 2019-07-27 stsp
2984 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2985 6bad629b 2019-02-04 stsp switch (ch) {
2986 5c860e29 2018-03-12 stsp default:
2987 2deda0b9 2019-03-07 stsp usage_status();
2988 6bad629b 2019-02-04 stsp /* NOTREACHED */
2989 5c860e29 2018-03-12 stsp }
2990 5c860e29 2018-03-12 stsp }
2991 5c860e29 2018-03-12 stsp
2992 6bad629b 2019-02-04 stsp argc -= optind;
2993 6bad629b 2019-02-04 stsp argv += optind;
2994 5c860e29 2018-03-12 stsp
2995 6bad629b 2019-02-04 stsp #ifndef PROFILE
2996 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2997 6bad629b 2019-02-04 stsp NULL) == -1)
2998 6bad629b 2019-02-04 stsp err(1, "pledge");
2999 f42b1b34 2018-03-12 stsp #endif
3000 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3001 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3002 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3003 927df6b7 2019-02-10 stsp goto done;
3004 927df6b7 2019-02-10 stsp }
3005 927df6b7 2019-02-10 stsp
3006 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3007 927df6b7 2019-02-10 stsp if (error != NULL)
3008 a5edda0a 2019-07-27 stsp goto done;
3009 6bad629b 2019-02-04 stsp
3010 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3011 c9956ddf 2019-09-08 stsp NULL);
3012 6bad629b 2019-02-04 stsp if (error != NULL)
3013 6bad629b 2019-02-04 stsp goto done;
3014 6bad629b 2019-02-04 stsp
3015 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3016 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3017 087fb88c 2019-08-04 stsp if (error)
3018 087fb88c 2019-08-04 stsp goto done;
3019 087fb88c 2019-08-04 stsp
3020 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3021 6bad629b 2019-02-04 stsp if (error)
3022 6bad629b 2019-02-04 stsp goto done;
3023 6bad629b 2019-02-04 stsp
3024 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3025 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3026 6bad629b 2019-02-04 stsp done:
3027 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3028 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3029 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3030 927df6b7 2019-02-10 stsp free(cwd);
3031 d0eebce4 2019-03-11 stsp return error;
3032 d0eebce4 2019-03-11 stsp }
3033 d0eebce4 2019-03-11 stsp
3034 d0eebce4 2019-03-11 stsp __dead static void
3035 d0eebce4 2019-03-11 stsp usage_ref(void)
3036 d0eebce4 2019-03-11 stsp {
3037 d0eebce4 2019-03-11 stsp fprintf(stderr,
3038 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3039 d0eebce4 2019-03-11 stsp getprogname());
3040 d0eebce4 2019-03-11 stsp exit(1);
3041 d0eebce4 2019-03-11 stsp }
3042 d0eebce4 2019-03-11 stsp
3043 d0eebce4 2019-03-11 stsp static const struct got_error *
3044 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3045 d0eebce4 2019-03-11 stsp {
3046 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3047 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3048 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3049 d0eebce4 2019-03-11 stsp
3050 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3051 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3052 d0eebce4 2019-03-11 stsp if (err)
3053 d0eebce4 2019-03-11 stsp return err;
3054 d0eebce4 2019-03-11 stsp
3055 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3056 d0eebce4 2019-03-11 stsp char *refstr;
3057 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3058 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3059 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3060 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3061 d0eebce4 2019-03-11 stsp free(refstr);
3062 d0eebce4 2019-03-11 stsp }
3063 d0eebce4 2019-03-11 stsp
3064 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3065 d0eebce4 2019-03-11 stsp return NULL;
3066 d0eebce4 2019-03-11 stsp }
3067 d0eebce4 2019-03-11 stsp
3068 d0eebce4 2019-03-11 stsp static const struct got_error *
3069 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3070 d0eebce4 2019-03-11 stsp {
3071 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3072 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3073 d0eebce4 2019-03-11 stsp
3074 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3075 d0eebce4 2019-03-11 stsp if (err)
3076 d0eebce4 2019-03-11 stsp return err;
3077 d0eebce4 2019-03-11 stsp
3078 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3079 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3080 d0eebce4 2019-03-11 stsp return err;
3081 d0eebce4 2019-03-11 stsp }
3082 d0eebce4 2019-03-11 stsp
3083 d0eebce4 2019-03-11 stsp static const struct got_error *
3084 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3085 d0eebce4 2019-03-11 stsp {
3086 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3087 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3088 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3089 d1644381 2019-07-14 stsp
3090 d1644381 2019-07-14 stsp /*
3091 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
3092 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3093 d1644381 2019-07-14 stsp * an unintended typo.
3094 d1644381 2019-07-14 stsp */
3095 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
3096 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3097 d0eebce4 2019-03-11 stsp
3098 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3099 dd88155e 2019-06-29 stsp repo);
3100 d83d9d5c 2019-05-13 stsp if (err) {
3101 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3102 d0eebce4 2019-03-11 stsp
3103 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3104 d83d9d5c 2019-05-13 stsp return err;
3105 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3106 d83d9d5c 2019-05-13 stsp if (err)
3107 d83d9d5c 2019-05-13 stsp return err;
3108 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3109 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3110 d83d9d5c 2019-05-13 stsp if (err)
3111 d83d9d5c 2019-05-13 stsp return err;
3112 d83d9d5c 2019-05-13 stsp }
3113 d83d9d5c 2019-05-13 stsp
3114 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3115 d0eebce4 2019-03-11 stsp if (err)
3116 d0eebce4 2019-03-11 stsp goto done;
3117 d0eebce4 2019-03-11 stsp
3118 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3119 d0eebce4 2019-03-11 stsp done:
3120 d0eebce4 2019-03-11 stsp if (ref)
3121 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3122 d0eebce4 2019-03-11 stsp free(id);
3123 d1c1ae5f 2019-08-12 stsp return err;
3124 d1c1ae5f 2019-08-12 stsp }
3125 d1c1ae5f 2019-08-12 stsp
3126 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3127 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3128 d1c1ae5f 2019-08-12 stsp {
3129 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3130 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3131 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3132 d1c1ae5f 2019-08-12 stsp
3133 d1c1ae5f 2019-08-12 stsp /*
3134 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
3135 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3136 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3137 d1c1ae5f 2019-08-12 stsp */
3138 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
3139 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3140 d1c1ae5f 2019-08-12 stsp
3141 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3142 d1c1ae5f 2019-08-12 stsp if (err)
3143 d1c1ae5f 2019-08-12 stsp return err;
3144 d1c1ae5f 2019-08-12 stsp
3145 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3146 d1c1ae5f 2019-08-12 stsp if (err)
3147 d1c1ae5f 2019-08-12 stsp goto done;
3148 d1c1ae5f 2019-08-12 stsp
3149 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3150 d1c1ae5f 2019-08-12 stsp done:
3151 d1c1ae5f 2019-08-12 stsp if (target_ref)
3152 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3153 d1c1ae5f 2019-08-12 stsp if (ref)
3154 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3155 d0eebce4 2019-03-11 stsp return err;
3156 d0eebce4 2019-03-11 stsp }
3157 d0eebce4 2019-03-11 stsp
3158 d0eebce4 2019-03-11 stsp static const struct got_error *
3159 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3160 d0eebce4 2019-03-11 stsp {
3161 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3162 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3163 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3164 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3165 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3166 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3167 d0eebce4 2019-03-11 stsp
3168 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3169 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3170 d0eebce4 2019-03-11 stsp switch (ch) {
3171 d0eebce4 2019-03-11 stsp case 'd':
3172 d0eebce4 2019-03-11 stsp delref = optarg;
3173 d0eebce4 2019-03-11 stsp break;
3174 d0eebce4 2019-03-11 stsp case 'r':
3175 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3176 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3177 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3178 9ba1d308 2019-10-21 stsp optarg);
3179 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3180 d0eebce4 2019-03-11 stsp break;
3181 d0eebce4 2019-03-11 stsp case 'l':
3182 d0eebce4 2019-03-11 stsp do_list = 1;
3183 d1c1ae5f 2019-08-12 stsp break;
3184 d1c1ae5f 2019-08-12 stsp case 's':
3185 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3186 d0eebce4 2019-03-11 stsp break;
3187 d0eebce4 2019-03-11 stsp default:
3188 d0eebce4 2019-03-11 stsp usage_ref();
3189 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3190 d0eebce4 2019-03-11 stsp }
3191 d0eebce4 2019-03-11 stsp }
3192 d0eebce4 2019-03-11 stsp
3193 d0eebce4 2019-03-11 stsp if (do_list && delref)
3194 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3195 d0eebce4 2019-03-11 stsp
3196 d0eebce4 2019-03-11 stsp argc -= optind;
3197 d0eebce4 2019-03-11 stsp argv += optind;
3198 d0eebce4 2019-03-11 stsp
3199 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3200 d1c1ae5f 2019-08-12 stsp if (create_symref)
3201 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3202 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3203 d0eebce4 2019-03-11 stsp if (argc > 0)
3204 d0eebce4 2019-03-11 stsp usage_ref();
3205 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3206 d0eebce4 2019-03-11 stsp usage_ref();
3207 d0eebce4 2019-03-11 stsp
3208 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3209 e0b57350 2019-03-12 stsp if (do_list) {
3210 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3211 e0b57350 2019-03-12 stsp NULL) == -1)
3212 e0b57350 2019-03-12 stsp err(1, "pledge");
3213 e0b57350 2019-03-12 stsp } else {
3214 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3215 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3216 e0b57350 2019-03-12 stsp err(1, "pledge");
3217 e0b57350 2019-03-12 stsp }
3218 d0eebce4 2019-03-11 stsp #endif
3219 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3220 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3221 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3222 d0eebce4 2019-03-11 stsp goto done;
3223 d0eebce4 2019-03-11 stsp }
3224 d0eebce4 2019-03-11 stsp
3225 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3226 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3227 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3228 d0eebce4 2019-03-11 stsp goto done;
3229 d0eebce4 2019-03-11 stsp else
3230 d0eebce4 2019-03-11 stsp error = NULL;
3231 d0eebce4 2019-03-11 stsp if (worktree) {
3232 d0eebce4 2019-03-11 stsp repo_path =
3233 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3234 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3235 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3236 d0eebce4 2019-03-11 stsp if (error)
3237 d0eebce4 2019-03-11 stsp goto done;
3238 d0eebce4 2019-03-11 stsp } else {
3239 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3240 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3241 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3242 d0eebce4 2019-03-11 stsp goto done;
3243 d0eebce4 2019-03-11 stsp }
3244 d0eebce4 2019-03-11 stsp }
3245 d0eebce4 2019-03-11 stsp }
3246 d0eebce4 2019-03-11 stsp
3247 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3248 d0eebce4 2019-03-11 stsp if (error != NULL)
3249 d0eebce4 2019-03-11 stsp goto done;
3250 d0eebce4 2019-03-11 stsp
3251 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3252 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3253 c02c541e 2019-03-29 stsp if (error)
3254 c02c541e 2019-03-29 stsp goto done;
3255 c02c541e 2019-03-29 stsp
3256 d0eebce4 2019-03-11 stsp if (do_list)
3257 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3258 d0eebce4 2019-03-11 stsp else if (delref)
3259 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3260 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3261 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3262 d0eebce4 2019-03-11 stsp else
3263 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3264 4e759de4 2019-06-26 stsp done:
3265 4e759de4 2019-06-26 stsp if (repo)
3266 4e759de4 2019-06-26 stsp got_repo_close(repo);
3267 4e759de4 2019-06-26 stsp if (worktree)
3268 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3269 4e759de4 2019-06-26 stsp free(cwd);
3270 4e759de4 2019-06-26 stsp free(repo_path);
3271 4e759de4 2019-06-26 stsp return error;
3272 4e759de4 2019-06-26 stsp }
3273 4e759de4 2019-06-26 stsp
3274 4e759de4 2019-06-26 stsp __dead static void
3275 4e759de4 2019-06-26 stsp usage_branch(void)
3276 4e759de4 2019-06-26 stsp {
3277 4e759de4 2019-06-26 stsp fprintf(stderr,
3278 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3279 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3280 4e759de4 2019-06-26 stsp exit(1);
3281 4e759de4 2019-06-26 stsp }
3282 4e759de4 2019-06-26 stsp
3283 4e759de4 2019-06-26 stsp static const struct got_error *
3284 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3285 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3286 4e99b47e 2019-10-04 stsp {
3287 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3288 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3289 4e99b47e 2019-10-04 stsp char *refstr;
3290 4e99b47e 2019-10-04 stsp
3291 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3292 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3293 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3294 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3295 4e99b47e 2019-10-04 stsp
3296 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3297 4e99b47e 2019-10-04 stsp if (err)
3298 4e99b47e 2019-10-04 stsp return err;
3299 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3300 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3301 4e99b47e 2019-10-04 stsp marker = "* ";
3302 4e99b47e 2019-10-04 stsp else
3303 4e99b47e 2019-10-04 stsp marker = "~ ";
3304 4e99b47e 2019-10-04 stsp free(id);
3305 4e99b47e 2019-10-04 stsp }
3306 4e99b47e 2019-10-04 stsp
3307 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3308 4e99b47e 2019-10-04 stsp refname += 11;
3309 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3310 4e99b47e 2019-10-04 stsp refname += 18;
3311 4e99b47e 2019-10-04 stsp
3312 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3313 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3314 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3315 4e99b47e 2019-10-04 stsp
3316 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3317 4e99b47e 2019-10-04 stsp free(refstr);
3318 4e99b47e 2019-10-04 stsp return NULL;
3319 4e99b47e 2019-10-04 stsp }
3320 4e99b47e 2019-10-04 stsp
3321 4e99b47e 2019-10-04 stsp static const struct got_error *
3322 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3323 ad89fa31 2019-10-04 stsp {
3324 ad89fa31 2019-10-04 stsp const char *refname;
3325 ad89fa31 2019-10-04 stsp
3326 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3327 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3328 ad89fa31 2019-10-04 stsp
3329 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3330 ad89fa31 2019-10-04 stsp
3331 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3332 ad89fa31 2019-10-04 stsp refname += 11;
3333 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3334 ad89fa31 2019-10-04 stsp refname += 18;
3335 ad89fa31 2019-10-04 stsp
3336 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3337 ad89fa31 2019-10-04 stsp
3338 ad89fa31 2019-10-04 stsp return NULL;
3339 ad89fa31 2019-10-04 stsp }
3340 ad89fa31 2019-10-04 stsp
3341 ad89fa31 2019-10-04 stsp static const struct got_error *
3342 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3343 4e759de4 2019-06-26 stsp {
3344 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3345 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3346 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3347 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3348 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3349 4e759de4 2019-06-26 stsp
3350 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3351 ba882ee3 2019-07-11 stsp
3352 4e99b47e 2019-10-04 stsp if (worktree) {
3353 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3354 4e99b47e 2019-10-04 stsp worktree);
3355 4e99b47e 2019-10-04 stsp if (err)
3356 4e99b47e 2019-10-04 stsp return err;
3357 4e99b47e 2019-10-04 stsp
3358 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3359 4e99b47e 2019-10-04 stsp worktree);
3360 4e99b47e 2019-10-04 stsp if (err)
3361 4e99b47e 2019-10-04 stsp return err;
3362 4e99b47e 2019-10-04 stsp
3363 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3364 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3365 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3366 4e99b47e 2019-10-04 stsp if (err)
3367 4e99b47e 2019-10-04 stsp return err;
3368 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3369 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3370 4e99b47e 2019-10-04 stsp }
3371 4e99b47e 2019-10-04 stsp }
3372 4e99b47e 2019-10-04 stsp
3373 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3374 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3375 4e759de4 2019-06-26 stsp if (err)
3376 4e759de4 2019-06-26 stsp return err;
3377 4e759de4 2019-06-26 stsp
3378 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3379 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3380 4e759de4 2019-06-26 stsp
3381 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3382 4e759de4 2019-06-26 stsp return NULL;
3383 4e759de4 2019-06-26 stsp }
3384 4e759de4 2019-06-26 stsp
3385 4e759de4 2019-06-26 stsp static const struct got_error *
3386 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3387 45cd4e47 2019-08-25 stsp const char *branch_name)
3388 4e759de4 2019-06-26 stsp {
3389 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3390 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3391 4e759de4 2019-06-26 stsp char *refname;
3392 4e759de4 2019-06-26 stsp
3393 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3394 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3395 4e759de4 2019-06-26 stsp
3396 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3397 4e759de4 2019-06-26 stsp if (err)
3398 4e759de4 2019-06-26 stsp goto done;
3399 4e759de4 2019-06-26 stsp
3400 45cd4e47 2019-08-25 stsp if (worktree &&
3401 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3402 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3403 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3404 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3405 45cd4e47 2019-08-25 stsp goto done;
3406 45cd4e47 2019-08-25 stsp }
3407 45cd4e47 2019-08-25 stsp
3408 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3409 4e759de4 2019-06-26 stsp done:
3410 45cd4e47 2019-08-25 stsp if (ref)
3411 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3412 4e759de4 2019-06-26 stsp free(refname);
3413 4e759de4 2019-06-26 stsp return err;
3414 4e759de4 2019-06-26 stsp }
3415 4e759de4 2019-06-26 stsp
3416 4e759de4 2019-06-26 stsp static const struct got_error *
3417 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3418 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3419 4e759de4 2019-06-26 stsp {
3420 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3421 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3422 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3423 d3f84d51 2019-07-11 stsp
3424 d3f84d51 2019-07-11 stsp /*
3425 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3426 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3427 d3f84d51 2019-07-11 stsp * an unintended typo.
3428 d3f84d51 2019-07-11 stsp */
3429 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3430 24b5452a 2019-10-09 stsp return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3431 4e759de4 2019-06-26 stsp
3432 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3433 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3434 4e759de4 2019-06-26 stsp goto done;
3435 4e759de4 2019-06-26 stsp }
3436 4e759de4 2019-06-26 stsp
3437 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3438 4e759de4 2019-06-26 stsp if (err == NULL) {
3439 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3440 4e759de4 2019-06-26 stsp goto done;
3441 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3442 4e759de4 2019-06-26 stsp goto done;
3443 4e759de4 2019-06-26 stsp
3444 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3445 4e759de4 2019-06-26 stsp if (err)
3446 4e759de4 2019-06-26 stsp goto done;
3447 4e759de4 2019-06-26 stsp
3448 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3449 d0eebce4 2019-03-11 stsp done:
3450 4e759de4 2019-06-26 stsp if (ref)
3451 4e759de4 2019-06-26 stsp got_ref_close(ref);
3452 4e759de4 2019-06-26 stsp free(base_refname);
3453 4e759de4 2019-06-26 stsp free(refname);
3454 4e759de4 2019-06-26 stsp return err;
3455 4e759de4 2019-06-26 stsp }
3456 4e759de4 2019-06-26 stsp
3457 4e759de4 2019-06-26 stsp static const struct got_error *
3458 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3459 4e759de4 2019-06-26 stsp {
3460 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3461 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3462 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3463 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3464 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3465 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3466 4e759de4 2019-06-26 stsp
3467 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3468 4e759de4 2019-06-26 stsp switch (ch) {
3469 a74f7e83 2019-11-10 stsp case 'c':
3470 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3471 a74f7e83 2019-11-10 stsp break;
3472 4e759de4 2019-06-26 stsp case 'd':
3473 4e759de4 2019-06-26 stsp delref = optarg;
3474 4e759de4 2019-06-26 stsp break;
3475 4e759de4 2019-06-26 stsp case 'r':
3476 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3477 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3478 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3479 9ba1d308 2019-10-21 stsp optarg);
3480 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3481 4e759de4 2019-06-26 stsp break;
3482 4e759de4 2019-06-26 stsp case 'l':
3483 4e759de4 2019-06-26 stsp do_list = 1;
3484 4e759de4 2019-06-26 stsp break;
3485 4e759de4 2019-06-26 stsp default:
3486 4e759de4 2019-06-26 stsp usage_branch();
3487 4e759de4 2019-06-26 stsp /* NOTREACHED */
3488 4e759de4 2019-06-26 stsp }
3489 4e759de4 2019-06-26 stsp }
3490 4e759de4 2019-06-26 stsp
3491 4e759de4 2019-06-26 stsp if (do_list && delref)
3492 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3493 4e759de4 2019-06-26 stsp
3494 4e759de4 2019-06-26 stsp argc -= optind;
3495 4e759de4 2019-06-26 stsp argv += optind;
3496 ad89fa31 2019-10-04 stsp
3497 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3498 ad89fa31 2019-10-04 stsp do_show = 1;
3499 4e759de4 2019-06-26 stsp
3500 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3501 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3502 a74f7e83 2019-11-10 stsp
3503 4e759de4 2019-06-26 stsp if (do_list || delref) {
3504 4e759de4 2019-06-26 stsp if (argc > 0)
3505 4e759de4 2019-06-26 stsp usage_branch();
3506 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3507 4e759de4 2019-06-26 stsp usage_branch();
3508 4e759de4 2019-06-26 stsp
3509 4e759de4 2019-06-26 stsp #ifndef PROFILE
3510 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3511 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3512 4e759de4 2019-06-26 stsp NULL) == -1)
3513 4e759de4 2019-06-26 stsp err(1, "pledge");
3514 4e759de4 2019-06-26 stsp } else {
3515 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3516 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3517 4e759de4 2019-06-26 stsp err(1, "pledge");
3518 4e759de4 2019-06-26 stsp }
3519 4e759de4 2019-06-26 stsp #endif
3520 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3521 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3522 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3523 4e759de4 2019-06-26 stsp goto done;
3524 4e759de4 2019-06-26 stsp }
3525 4e759de4 2019-06-26 stsp
3526 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3527 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3528 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3529 4e759de4 2019-06-26 stsp goto done;
3530 4e759de4 2019-06-26 stsp else
3531 4e759de4 2019-06-26 stsp error = NULL;
3532 4e759de4 2019-06-26 stsp if (worktree) {
3533 4e759de4 2019-06-26 stsp repo_path =
3534 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3535 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3536 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3537 4e759de4 2019-06-26 stsp if (error)
3538 4e759de4 2019-06-26 stsp goto done;
3539 4e759de4 2019-06-26 stsp } else {
3540 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3541 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3542 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3543 4e759de4 2019-06-26 stsp goto done;
3544 4e759de4 2019-06-26 stsp }
3545 4e759de4 2019-06-26 stsp }
3546 4e759de4 2019-06-26 stsp }
3547 4e759de4 2019-06-26 stsp
3548 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3549 4e759de4 2019-06-26 stsp if (error != NULL)
3550 4e759de4 2019-06-26 stsp goto done;
3551 4e759de4 2019-06-26 stsp
3552 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3553 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3554 4e759de4 2019-06-26 stsp if (error)
3555 4e759de4 2019-06-26 stsp goto done;
3556 4e759de4 2019-06-26 stsp
3557 ad89fa31 2019-10-04 stsp if (do_show)
3558 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3559 ad89fa31 2019-10-04 stsp else if (do_list)
3560 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3561 4e759de4 2019-06-26 stsp else if (delref)
3562 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3563 4e759de4 2019-06-26 stsp else {
3564 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3565 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3566 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3567 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3568 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3569 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3570 a74f7e83 2019-11-10 stsp if (error)
3571 a74f7e83 2019-11-10 stsp goto done;
3572 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3573 a74f7e83 2019-11-10 stsp free(commit_id);
3574 4e759de4 2019-06-26 stsp }
3575 4e759de4 2019-06-26 stsp done:
3576 d0eebce4 2019-03-11 stsp if (repo)
3577 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3578 d0eebce4 2019-03-11 stsp if (worktree)
3579 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3580 d0eebce4 2019-03-11 stsp free(cwd);
3581 d0eebce4 2019-03-11 stsp free(repo_path);
3582 d00136be 2019-03-26 stsp return error;
3583 d00136be 2019-03-26 stsp }
3584 d00136be 2019-03-26 stsp
3585 8e7bd50a 2019-08-22 stsp
3586 d00136be 2019-03-26 stsp __dead static void
3587 8e7bd50a 2019-08-22 stsp usage_tag(void)
3588 8e7bd50a 2019-08-22 stsp {
3589 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3590 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3591 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3592 8e7bd50a 2019-08-22 stsp exit(1);
3593 b8bad2ba 2019-08-23 stsp }
3594 b8bad2ba 2019-08-23 stsp
3595 b8bad2ba 2019-08-23 stsp #if 0
3596 b8bad2ba 2019-08-23 stsp static const struct got_error *
3597 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3598 b8bad2ba 2019-08-23 stsp {
3599 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3600 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3601 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3602 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3603 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3604 b8bad2ba 2019-08-23 stsp
3605 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3606 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3607 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3608 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3609 b8bad2ba 2019-08-23 stsp if (err)
3610 b8bad2ba 2019-08-23 stsp return err;
3611 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3612 b8bad2ba 2019-08-23 stsp continue;
3613 b8bad2ba 2019-08-23 stsp } else {
3614 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3615 b8bad2ba 2019-08-23 stsp if (err)
3616 b8bad2ba 2019-08-23 stsp break;
3617 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3618 b8bad2ba 2019-08-23 stsp free(re_id);
3619 b8bad2ba 2019-08-23 stsp if (err)
3620 b8bad2ba 2019-08-23 stsp break;
3621 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3622 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3623 b8bad2ba 2019-08-23 stsp }
3624 b8bad2ba 2019-08-23 stsp
3625 b8bad2ba 2019-08-23 stsp while (se) {
3626 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3627 b8bad2ba 2019-08-23 stsp if (err)
3628 b8bad2ba 2019-08-23 stsp break;
3629 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3630 b8bad2ba 2019-08-23 stsp free(se_id);
3631 b8bad2ba 2019-08-23 stsp if (err)
3632 b8bad2ba 2019-08-23 stsp break;
3633 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3634 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3635 b8bad2ba 2019-08-23 stsp
3636 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3637 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3638 b8bad2ba 2019-08-23 stsp if (err)
3639 b8bad2ba 2019-08-23 stsp return err;
3640 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3641 b8bad2ba 2019-08-23 stsp break;
3642 b8bad2ba 2019-08-23 stsp }
3643 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3644 b8bad2ba 2019-08-23 stsp continue;
3645 b8bad2ba 2019-08-23 stsp }
3646 b8bad2ba 2019-08-23 stsp }
3647 b8bad2ba 2019-08-23 stsp done:
3648 b8bad2ba 2019-08-23 stsp return err;
3649 8e7bd50a 2019-08-22 stsp }
3650 b8bad2ba 2019-08-23 stsp #endif
3651 8e7bd50a 2019-08-22 stsp
3652 8e7bd50a 2019-08-22 stsp static const struct got_error *
3653 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3654 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3655 b8bad2ba 2019-08-23 stsp {
3656 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3657 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3658 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3659 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3660 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3661 b8bad2ba 2019-08-23 stsp
3662 b8bad2ba 2019-08-23 stsp *cmp = 0;
3663 b8bad2ba 2019-08-23 stsp
3664 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3665 b8bad2ba 2019-08-23 stsp if (err)
3666 b8bad2ba 2019-08-23 stsp return err;
3667 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3668 b8bad2ba 2019-08-23 stsp if (err)
3669 b8bad2ba 2019-08-23 stsp goto done;
3670 b8bad2ba 2019-08-23 stsp
3671 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3672 b8bad2ba 2019-08-23 stsp if (err)
3673 b8bad2ba 2019-08-23 stsp goto done;
3674 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3675 b8bad2ba 2019-08-23 stsp if (err)
3676 b8bad2ba 2019-08-23 stsp goto done;
3677 b8bad2ba 2019-08-23 stsp
3678 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3679 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3680 b8bad2ba 2019-08-23 stsp
3681 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3682 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3683 b8bad2ba 2019-08-23 stsp *cmp = 1;
3684 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3685 b8bad2ba 2019-08-23 stsp *cmp = -1;
3686 b8bad2ba 2019-08-23 stsp else
3687 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3688 b8bad2ba 2019-08-23 stsp done:
3689 b8bad2ba 2019-08-23 stsp free(id1);
3690 b8bad2ba 2019-08-23 stsp free(id2);
3691 b8bad2ba 2019-08-23 stsp if (tag1)
3692 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3693 b8bad2ba 2019-08-23 stsp if (tag2)
3694 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3695 b8bad2ba 2019-08-23 stsp return err;
3696 b8bad2ba 2019-08-23 stsp }
3697 b8bad2ba 2019-08-23 stsp
3698 b8bad2ba 2019-08-23 stsp static const struct got_error *
3699 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3700 8e7bd50a 2019-08-22 stsp {
3701 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3702 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3703 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3704 8e7bd50a 2019-08-22 stsp
3705 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3706 8e7bd50a 2019-08-22 stsp
3707 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3708 8e7bd50a 2019-08-22 stsp if (err)
3709 8e7bd50a 2019-08-22 stsp return err;
3710 8e7bd50a 2019-08-22 stsp
3711 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3712 8e7bd50a 2019-08-22 stsp const char *refname;
3713 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3714 8e7bd50a 2019-08-22 stsp char datebuf[26];
3715 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3716 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3717 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3718 8e7bd50a 2019-08-22 stsp
3719 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3720 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3721 8e7bd50a 2019-08-22 stsp continue;
3722 8e7bd50a 2019-08-22 stsp refname += 10;
3723 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3724 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3725 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3726 8e7bd50a 2019-08-22 stsp break;
3727 8e7bd50a 2019-08-22 stsp }
3728 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3729 8e7bd50a 2019-08-22 stsp free(refstr);
3730 8e7bd50a 2019-08-22 stsp
3731 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3732 8e7bd50a 2019-08-22 stsp if (err)
3733 8e7bd50a 2019-08-22 stsp break;
3734 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3735 8e7bd50a 2019-08-22 stsp free(id);
3736 8e7bd50a 2019-08-22 stsp if (err)
3737 8e7bd50a 2019-08-22 stsp break;
3738 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3739 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3740 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3741 2417344c 2019-08-23 stsp if (datestr)
3742 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3743 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3744 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3745 8e7bd50a 2019-08-22 stsp if (err)
3746 8e7bd50a 2019-08-22 stsp break;
3747 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3748 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3749 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3750 8e7bd50a 2019-08-22 stsp break;
3751 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3752 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3753 8e7bd50a 2019-08-22 stsp break;
3754 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3755 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3756 8e7bd50a 2019-08-22 stsp break;
3757 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3758 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3759 8e7bd50a 2019-08-22 stsp break;
3760 8e7bd50a 2019-08-22 stsp default:
3761 8e7bd50a 2019-08-22 stsp break;
3762 8e7bd50a 2019-08-22 stsp }
3763 8e7bd50a 2019-08-22 stsp free(id_str);
3764 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3765 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3766 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3767 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3768 8e7bd50a 2019-08-22 stsp break;
3769 8e7bd50a 2019-08-22 stsp }
3770 8e7bd50a 2019-08-22 stsp
3771 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3772 8e7bd50a 2019-08-22 stsp do {
3773 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3774 8e7bd50a 2019-08-22 stsp if (line)
3775 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3776 8e7bd50a 2019-08-22 stsp } while (line);
3777 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3778 8e7bd50a 2019-08-22 stsp }
3779 8e7bd50a 2019-08-22 stsp
3780 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3781 8e7bd50a 2019-08-22 stsp return NULL;
3782 8e7bd50a 2019-08-22 stsp }
3783 8e7bd50a 2019-08-22 stsp
3784 8e7bd50a 2019-08-22 stsp static const struct got_error *
3785 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3786 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3787 8e7bd50a 2019-08-22 stsp {
3788 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3789 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3790 f372d5cd 2019-10-21 stsp char *editor = NULL;
3791 8e7bd50a 2019-08-22 stsp int fd = -1;
3792 8e7bd50a 2019-08-22 stsp
3793 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3794 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3795 8e7bd50a 2019-08-22 stsp goto done;
3796 8e7bd50a 2019-08-22 stsp }
3797 8e7bd50a 2019-08-22 stsp
3798 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3799 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3800 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3801 8e7bd50a 2019-08-22 stsp goto done;
3802 8e7bd50a 2019-08-22 stsp }
3803 8e7bd50a 2019-08-22 stsp
3804 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3805 8e7bd50a 2019-08-22 stsp if (err)
3806 8e7bd50a 2019-08-22 stsp goto done;
3807 8e7bd50a 2019-08-22 stsp
3808 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3809 8e7bd50a 2019-08-22 stsp close(fd);
3810 8e7bd50a 2019-08-22 stsp
3811 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3812 8e7bd50a 2019-08-22 stsp if (err)
3813 8e7bd50a 2019-08-22 stsp goto done;
3814 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3815 8e7bd50a 2019-08-22 stsp done:
3816 8e7bd50a 2019-08-22 stsp free(initial_content);
3817 8e7bd50a 2019-08-22 stsp free(template);
3818 8e7bd50a 2019-08-22 stsp free(editor);
3819 8e7bd50a 2019-08-22 stsp
3820 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3821 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3822 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3823 8e7bd50a 2019-08-22 stsp if (err) {
3824 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3825 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3826 8e7bd50a 2019-08-22 stsp }
3827 8e7bd50a 2019-08-22 stsp }
3828 8e7bd50a 2019-08-22 stsp return err;
3829 8e7bd50a 2019-08-22 stsp }
3830 8e7bd50a 2019-08-22 stsp
3831 8e7bd50a 2019-08-22 stsp static const struct got_error *
3832 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3833 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3834 8e7bd50a 2019-08-22 stsp {
3835 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3836 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3837 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3838 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3839 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3840 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3841 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3842 8e7bd50a 2019-08-22 stsp
3843 8e7bd50a 2019-08-22 stsp /*
3844 8e7bd50a 2019-08-22 stsp * Don't let the user create a tag named '-'.
3845 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3846 8e7bd50a 2019-08-22 stsp * an unintended typo.
3847 8e7bd50a 2019-08-22 stsp */
3848 8e7bd50a 2019-08-22 stsp if (tag_name[0] == '-' && tag_name[1] == '\0')
3849 24b5452a 2019-10-09 stsp return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3850 8e7bd50a 2019-08-22 stsp
3851 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3852 8e7bd50a 2019-08-22 stsp if (err)
3853 8e7bd50a 2019-08-22 stsp return err;
3854 8e7bd50a 2019-08-22 stsp
3855 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3856 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3857 8e7bd50a 2019-08-22 stsp if (err)
3858 8e7bd50a 2019-08-22 stsp goto done;
3859 8e7bd50a 2019-08-22 stsp
3860 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3861 8e7bd50a 2019-08-22 stsp if (err)
3862 8e7bd50a 2019-08-22 stsp goto done;
3863 8e7bd50a 2019-08-22 stsp
3864 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3865 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3866 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3867 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3868 8e7bd50a 2019-08-22 stsp goto done;
3869 8e7bd50a 2019-08-22 stsp }
3870 8e7bd50a 2019-08-22 stsp tag_name += 10;
3871 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3872 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3873 8e7bd50a 2019-08-22 stsp goto done;
3874 8e7bd50a 2019-08-22 stsp }
3875 8e7bd50a 2019-08-22 stsp
3876 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3877 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3878 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3879 8e7bd50a 2019-08-22 stsp goto done;
3880 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3881 8e7bd50a 2019-08-22 stsp goto done;
3882 8e7bd50a 2019-08-22 stsp
3883 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3884 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3885 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3886 f372d5cd 2019-10-21 stsp if (err) {
3887 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3888 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3889 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3890 8e7bd50a 2019-08-22 stsp goto done;
3891 f372d5cd 2019-10-21 stsp }
3892 8e7bd50a 2019-08-22 stsp }
3893 8e7bd50a 2019-08-22 stsp
3894 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3895 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3896 f372d5cd 2019-10-21 stsp if (err) {
3897 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3898 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3899 8e7bd50a 2019-08-22 stsp goto done;
3900 f372d5cd 2019-10-21 stsp }
3901 8e7bd50a 2019-08-22 stsp
3902 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3903 f372d5cd 2019-10-21 stsp if (err) {
3904 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3905 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3906 8e7bd50a 2019-08-22 stsp goto done;
3907 f372d5cd 2019-10-21 stsp }
3908 8e7bd50a 2019-08-22 stsp
3909 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3910 f372d5cd 2019-10-21 stsp if (err) {
3911 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3912 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3913 f372d5cd 2019-10-21 stsp goto done;
3914 f372d5cd 2019-10-21 stsp }
3915 8e7bd50a 2019-08-22 stsp
3916 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3917 f372d5cd 2019-10-21 stsp if (err) {
3918 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3919 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3920 f372d5cd 2019-10-21 stsp goto done;
3921 8e7bd50a 2019-08-22 stsp }
3922 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3923 8e7bd50a 2019-08-22 stsp done:
3924 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3925 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3926 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3927 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3928 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3929 f372d5cd 2019-10-21 stsp free(tag_id_str);
3930 8e7bd50a 2019-08-22 stsp if (ref)
3931 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3932 8e7bd50a 2019-08-22 stsp free(commit_id);
3933 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3934 8e7bd50a 2019-08-22 stsp free(refname);
3935 8e7bd50a 2019-08-22 stsp free(tagmsg);
3936 f372d5cd 2019-10-21 stsp free(tagmsg_path);
3937 aba9c984 2019-09-08 stsp free(tagger);
3938 8e7bd50a 2019-08-22 stsp return err;
3939 8e7bd50a 2019-08-22 stsp }
3940 8e7bd50a 2019-08-22 stsp
3941 8e7bd50a 2019-08-22 stsp static const struct got_error *
3942 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3943 8e7bd50a 2019-08-22 stsp {
3944 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3945 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3946 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3947 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3948 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
3949 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3950 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3951 8e7bd50a 2019-08-22 stsp
3952 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3953 8e7bd50a 2019-08-22 stsp switch (ch) {
3954 8e7bd50a 2019-08-22 stsp case 'm':
3955 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3956 8e7bd50a 2019-08-22 stsp break;
3957 8e7bd50a 2019-08-22 stsp case 'r':
3958 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3959 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3960 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3961 9ba1d308 2019-10-21 stsp optarg);
3962 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3963 8e7bd50a 2019-08-22 stsp break;
3964 8e7bd50a 2019-08-22 stsp case 'l':
3965 8e7bd50a 2019-08-22 stsp do_list = 1;
3966 8e7bd50a 2019-08-22 stsp break;
3967 8e7bd50a 2019-08-22 stsp default:
3968 8e7bd50a 2019-08-22 stsp usage_tag();
3969 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3970 8e7bd50a 2019-08-22 stsp }
3971 8e7bd50a 2019-08-22 stsp }
3972 8e7bd50a 2019-08-22 stsp
3973 8e7bd50a 2019-08-22 stsp argc -= optind;
3974 8e7bd50a 2019-08-22 stsp argv += optind;
3975 8e7bd50a 2019-08-22 stsp
3976 8e7bd50a 2019-08-22 stsp if (do_list) {
3977 8e7bd50a 2019-08-22 stsp if (tagmsg)
3978 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3979 8e7bd50a 2019-08-22 stsp if (argc > 0)
3980 8e7bd50a 2019-08-22 stsp usage_tag();
3981 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3982 8e7bd50a 2019-08-22 stsp usage_tag();
3983 a2887370 2019-08-23 stsp else if (argc > 1)
3984 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3985 a2887370 2019-08-23 stsp tag_name = argv[0];
3986 8e7bd50a 2019-08-22 stsp
3987 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3988 8e7bd50a 2019-08-22 stsp if (do_list) {
3989 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3990 8e7bd50a 2019-08-22 stsp NULL) == -1)
3991 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3992 8e7bd50a 2019-08-22 stsp } else {
3993 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3994 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3995 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3996 8e7bd50a 2019-08-22 stsp }
3997 8e7bd50a 2019-08-22 stsp #endif
3998 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
3999 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4000 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4001 8e7bd50a 2019-08-22 stsp goto done;
4002 8e7bd50a 2019-08-22 stsp }
4003 8e7bd50a 2019-08-22 stsp
4004 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4005 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4006 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4007 8e7bd50a 2019-08-22 stsp goto done;
4008 8e7bd50a 2019-08-22 stsp else
4009 8e7bd50a 2019-08-22 stsp error = NULL;
4010 8e7bd50a 2019-08-22 stsp if (worktree) {
4011 8e7bd50a 2019-08-22 stsp repo_path =
4012 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4013 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4014 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4015 8e7bd50a 2019-08-22 stsp if (error)
4016 8e7bd50a 2019-08-22 stsp goto done;
4017 8e7bd50a 2019-08-22 stsp } else {
4018 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4019 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4020 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4021 8e7bd50a 2019-08-22 stsp goto done;
4022 8e7bd50a 2019-08-22 stsp }
4023 8e7bd50a 2019-08-22 stsp }
4024 8e7bd50a 2019-08-22 stsp }
4025 8e7bd50a 2019-08-22 stsp
4026 8e7bd50a 2019-08-22 stsp if (do_list) {
4027 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4028 c9956ddf 2019-09-08 stsp if (error != NULL)
4029 c9956ddf 2019-09-08 stsp goto done;
4030 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4031 8e7bd50a 2019-08-22 stsp if (error)
4032 8e7bd50a 2019-08-22 stsp goto done;
4033 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4034 8e7bd50a 2019-08-22 stsp } else {
4035 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4036 c9956ddf 2019-09-08 stsp if (error)
4037 c9956ddf 2019-09-08 stsp goto done;
4038 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4039 c9956ddf 2019-09-08 stsp if (error != NULL)
4040 c9956ddf 2019-09-08 stsp goto done;
4041 c9956ddf 2019-09-08 stsp
4042 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4043 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4044 8e7bd50a 2019-08-22 stsp if (error)
4045 8e7bd50a 2019-08-22 stsp goto done;
4046 8e7bd50a 2019-08-22 stsp }
4047 8e7bd50a 2019-08-22 stsp
4048 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4049 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4050 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4051 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4052 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4053 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4054 8e7bd50a 2019-08-22 stsp if (error)
4055 8e7bd50a 2019-08-22 stsp goto done;
4056 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4057 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4058 8e7bd50a 2019-08-22 stsp if (error)
4059 8e7bd50a 2019-08-22 stsp goto done;
4060 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4061 8e7bd50a 2019-08-22 stsp free(commit_id);
4062 8e7bd50a 2019-08-22 stsp if (error)
4063 8e7bd50a 2019-08-22 stsp goto done;
4064 8e7bd50a 2019-08-22 stsp }
4065 8e7bd50a 2019-08-22 stsp
4066 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4067 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4068 8e7bd50a 2019-08-22 stsp }
4069 8e7bd50a 2019-08-22 stsp done:
4070 8e7bd50a 2019-08-22 stsp if (repo)
4071 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4072 8e7bd50a 2019-08-22 stsp if (worktree)
4073 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4074 8e7bd50a 2019-08-22 stsp free(cwd);
4075 8e7bd50a 2019-08-22 stsp free(repo_path);
4076 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4077 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4078 8e7bd50a 2019-08-22 stsp return error;
4079 8e7bd50a 2019-08-22 stsp }
4080 8e7bd50a 2019-08-22 stsp
4081 8e7bd50a 2019-08-22 stsp __dead static void
4082 d00136be 2019-03-26 stsp usage_add(void)
4083 d00136be 2019-03-26 stsp {
4084 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4085 d00136be 2019-03-26 stsp exit(1);
4086 d00136be 2019-03-26 stsp }
4087 d00136be 2019-03-26 stsp
4088 d00136be 2019-03-26 stsp static const struct got_error *
4089 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4090 d00136be 2019-03-26 stsp {
4091 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4092 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4093 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4094 1dd54920 2019-05-11 stsp char *cwd = NULL;
4095 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4096 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4097 6d022e97 2019-08-04 stsp int ch;
4098 1dd54920 2019-05-11 stsp
4099 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4100 d00136be 2019-03-26 stsp
4101 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4102 d00136be 2019-03-26 stsp switch (ch) {
4103 d00136be 2019-03-26 stsp default:
4104 d00136be 2019-03-26 stsp usage_add();
4105 d00136be 2019-03-26 stsp /* NOTREACHED */
4106 d00136be 2019-03-26 stsp }
4107 d00136be 2019-03-26 stsp }
4108 d00136be 2019-03-26 stsp
4109 d00136be 2019-03-26 stsp argc -= optind;
4110 d00136be 2019-03-26 stsp argv += optind;
4111 d00136be 2019-03-26 stsp
4112 43012d58 2019-07-14 stsp #ifndef PROFILE
4113 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4114 43012d58 2019-07-14 stsp NULL) == -1)
4115 43012d58 2019-07-14 stsp err(1, "pledge");
4116 43012d58 2019-07-14 stsp #endif
4117 723c305c 2019-05-11 jcs if (argc < 1)
4118 d00136be 2019-03-26 stsp usage_add();
4119 d00136be 2019-03-26 stsp
4120 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4121 d00136be 2019-03-26 stsp if (cwd == NULL) {
4122 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4123 d00136be 2019-03-26 stsp goto done;
4124 d00136be 2019-03-26 stsp }
4125 723c305c 2019-05-11 jcs
4126 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4127 d00136be 2019-03-26 stsp if (error)
4128 d00136be 2019-03-26 stsp goto done;
4129 d00136be 2019-03-26 stsp
4130 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4131 c9956ddf 2019-09-08 stsp NULL);
4132 031a5338 2019-03-26 stsp if (error != NULL)
4133 031a5338 2019-03-26 stsp goto done;
4134 031a5338 2019-03-26 stsp
4135 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4136 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4137 d00136be 2019-03-26 stsp if (error)
4138 d00136be 2019-03-26 stsp goto done;
4139 d00136be 2019-03-26 stsp
4140 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4141 6d022e97 2019-08-04 stsp if (error)
4142 6d022e97 2019-08-04 stsp goto done;
4143 723c305c 2019-05-11 jcs
4144 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
4145 1dd54920 2019-05-11 stsp NULL, repo);
4146 d00136be 2019-03-26 stsp done:
4147 031a5338 2019-03-26 stsp if (repo)
4148 031a5338 2019-03-26 stsp got_repo_close(repo);
4149 d00136be 2019-03-26 stsp if (worktree)
4150 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4151 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4152 1dd54920 2019-05-11 stsp free((char *)pe->path);
4153 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4154 2ec1f75b 2019-03-26 stsp free(cwd);
4155 2ec1f75b 2019-03-26 stsp return error;
4156 2ec1f75b 2019-03-26 stsp }
4157 2ec1f75b 2019-03-26 stsp
4158 2ec1f75b 2019-03-26 stsp __dead static void
4159 648e4ef7 2019-07-09 stsp usage_remove(void)
4160 2ec1f75b 2019-03-26 stsp {
4161 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4162 2ec1f75b 2019-03-26 stsp exit(1);
4163 2a06fe5f 2019-08-24 stsp }
4164 2a06fe5f 2019-08-24 stsp
4165 2a06fe5f 2019-08-24 stsp static const struct got_error *
4166 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4167 2a06fe5f 2019-08-24 stsp unsigned char staged_status, const char *path,
4168 2a06fe5f 2019-08-24 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4169 2a06fe5f 2019-08-24 stsp struct got_object_id *commit_id)
4170 2a06fe5f 2019-08-24 stsp {
4171 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4172 2a06fe5f 2019-08-24 stsp return NULL;
4173 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4174 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4175 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4176 2a06fe5f 2019-08-24 stsp return NULL;
4177 2ec1f75b 2019-03-26 stsp }
4178 2ec1f75b 2019-03-26 stsp
4179 2ec1f75b 2019-03-26 stsp static const struct got_error *
4180 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4181 2ec1f75b 2019-03-26 stsp {
4182 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4183 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4184 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4185 17ed4618 2019-06-02 stsp char *cwd = NULL;
4186 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4187 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4188 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
4189 2ec1f75b 2019-03-26 stsp
4190 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4191 17ed4618 2019-06-02 stsp
4192 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
4193 2ec1f75b 2019-03-26 stsp switch (ch) {
4194 2ec1f75b 2019-03-26 stsp case 'f':
4195 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4196 2ec1f75b 2019-03-26 stsp break;
4197 2ec1f75b 2019-03-26 stsp default:
4198 2ec1f75b 2019-03-26 stsp usage_add();
4199 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4200 2ec1f75b 2019-03-26 stsp }
4201 2ec1f75b 2019-03-26 stsp }
4202 2ec1f75b 2019-03-26 stsp
4203 2ec1f75b 2019-03-26 stsp argc -= optind;
4204 2ec1f75b 2019-03-26 stsp argv += optind;
4205 2ec1f75b 2019-03-26 stsp
4206 43012d58 2019-07-14 stsp #ifndef PROFILE
4207 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4208 43012d58 2019-07-14 stsp NULL) == -1)
4209 43012d58 2019-07-14 stsp err(1, "pledge");
4210 43012d58 2019-07-14 stsp #endif
4211 17ed4618 2019-06-02 stsp if (argc < 1)
4212 648e4ef7 2019-07-09 stsp usage_remove();
4213 2ec1f75b 2019-03-26 stsp
4214 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4215 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4216 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4217 2ec1f75b 2019-03-26 stsp goto done;
4218 2ec1f75b 2019-03-26 stsp }
4219 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4220 2ec1f75b 2019-03-26 stsp if (error)
4221 2ec1f75b 2019-03-26 stsp goto done;
4222 2ec1f75b 2019-03-26 stsp
4223 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4224 c9956ddf 2019-09-08 stsp NULL);
4225 2af4a041 2019-05-11 jcs if (error)
4226 2ec1f75b 2019-03-26 stsp goto done;
4227 2ec1f75b 2019-03-26 stsp
4228 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4229 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4230 2ec1f75b 2019-03-26 stsp if (error)
4231 2ec1f75b 2019-03-26 stsp goto done;
4232 2ec1f75b 2019-03-26 stsp
4233 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4234 6d022e97 2019-08-04 stsp if (error)
4235 6d022e97 2019-08-04 stsp goto done;
4236 17ed4618 2019-06-02 stsp
4237 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4238 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4239 a129376b 2019-03-28 stsp if (error)
4240 a129376b 2019-03-28 stsp goto done;
4241 a129376b 2019-03-28 stsp done:
4242 a129376b 2019-03-28 stsp if (repo)
4243 a129376b 2019-03-28 stsp got_repo_close(repo);
4244 a129376b 2019-03-28 stsp if (worktree)
4245 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4246 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4247 17ed4618 2019-06-02 stsp free((char *)pe->path);
4248 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4249 a129376b 2019-03-28 stsp free(cwd);
4250 a129376b 2019-03-28 stsp return error;
4251 a129376b 2019-03-28 stsp }
4252 a129376b 2019-03-28 stsp
4253 a129376b 2019-03-28 stsp __dead static void
4254 a129376b 2019-03-28 stsp usage_revert(void)
4255 a129376b 2019-03-28 stsp {
4256 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4257 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4258 a129376b 2019-03-28 stsp exit(1);
4259 a129376b 2019-03-28 stsp }
4260 a129376b 2019-03-28 stsp
4261 1ee397ad 2019-07-12 stsp static const struct got_error *
4262 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4263 a129376b 2019-03-28 stsp {
4264 a129376b 2019-03-28 stsp while (path[0] == '/')
4265 a129376b 2019-03-28 stsp path++;
4266 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4267 33aa809d 2019-08-08 stsp return NULL;
4268 33aa809d 2019-08-08 stsp }
4269 33aa809d 2019-08-08 stsp
4270 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4271 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4272 33aa809d 2019-08-08 stsp const char *action;
4273 33aa809d 2019-08-08 stsp };
4274 33aa809d 2019-08-08 stsp
4275 33aa809d 2019-08-08 stsp static const struct got_error *
4276 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4277 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4278 33aa809d 2019-08-08 stsp {
4279 33aa809d 2019-08-08 stsp char *line = NULL;
4280 33aa809d 2019-08-08 stsp size_t linesize = 0;
4281 33aa809d 2019-08-08 stsp ssize_t linelen;
4282 33aa809d 2019-08-08 stsp
4283 33aa809d 2019-08-08 stsp switch (status) {
4284 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4285 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4286 33aa809d 2019-08-08 stsp break;
4287 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4288 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4289 33aa809d 2019-08-08 stsp break;
4290 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4291 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4292 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4293 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4294 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4295 33aa809d 2019-08-08 stsp printf("%s", line);
4296 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4297 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4298 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4299 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4300 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4301 33aa809d 2019-08-08 stsp break;
4302 33aa809d 2019-08-08 stsp default:
4303 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4304 33aa809d 2019-08-08 stsp }
4305 33aa809d 2019-08-08 stsp
4306 33aa809d 2019-08-08 stsp return NULL;
4307 33aa809d 2019-08-08 stsp }
4308 33aa809d 2019-08-08 stsp
4309 33aa809d 2019-08-08 stsp static const struct got_error *
4310 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4311 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4312 33aa809d 2019-08-08 stsp {
4313 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4314 33aa809d 2019-08-08 stsp char *line = NULL;
4315 33aa809d 2019-08-08 stsp size_t linesize = 0;
4316 33aa809d 2019-08-08 stsp ssize_t linelen;
4317 33aa809d 2019-08-08 stsp int resp = ' ';
4318 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4319 33aa809d 2019-08-08 stsp
4320 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4321 33aa809d 2019-08-08 stsp
4322 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4323 33aa809d 2019-08-08 stsp char *nl;
4324 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4325 33aa809d 2019-08-08 stsp a->action);
4326 33aa809d 2019-08-08 stsp if (err)
4327 33aa809d 2019-08-08 stsp return err;
4328 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4329 33aa809d 2019-08-08 stsp if (linelen == -1) {
4330 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4331 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4332 33aa809d 2019-08-08 stsp return NULL;
4333 33aa809d 2019-08-08 stsp }
4334 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4335 33aa809d 2019-08-08 stsp if (nl)
4336 33aa809d 2019-08-08 stsp *nl = '\0';
4337 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4338 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4339 33aa809d 2019-08-08 stsp printf("y\n");
4340 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4341 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4342 33aa809d 2019-08-08 stsp printf("n\n");
4343 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4344 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4345 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4346 33aa809d 2019-08-08 stsp printf("q\n");
4347 33aa809d 2019-08-08 stsp } else
4348 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4349 33aa809d 2019-08-08 stsp free(line);
4350 33aa809d 2019-08-08 stsp return NULL;
4351 33aa809d 2019-08-08 stsp }
4352 33aa809d 2019-08-08 stsp
4353 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4354 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4355 33aa809d 2019-08-08 stsp a->action);
4356 33aa809d 2019-08-08 stsp if (err)
4357 33aa809d 2019-08-08 stsp return err;
4358 33aa809d 2019-08-08 stsp resp = getchar();
4359 33aa809d 2019-08-08 stsp if (resp == '\n')
4360 33aa809d 2019-08-08 stsp resp = getchar();
4361 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4362 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4363 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4364 33aa809d 2019-08-08 stsp resp = ' ';
4365 33aa809d 2019-08-08 stsp }
4366 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4367 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4368 33aa809d 2019-08-08 stsp resp = ' ';
4369 33aa809d 2019-08-08 stsp }
4370 33aa809d 2019-08-08 stsp }
4371 33aa809d 2019-08-08 stsp
4372 33aa809d 2019-08-08 stsp if (resp == 'y')
4373 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4374 33aa809d 2019-08-08 stsp else if (resp == 'n')
4375 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4376 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4377 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4378 33aa809d 2019-08-08 stsp
4379 1ee397ad 2019-07-12 stsp return NULL;
4380 a129376b 2019-03-28 stsp }
4381 a129376b 2019-03-28 stsp
4382 33aa809d 2019-08-08 stsp
4383 a129376b 2019-03-28 stsp static const struct got_error *
4384 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4385 a129376b 2019-03-28 stsp {
4386 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4387 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4388 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4389 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4390 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4391 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4392 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4393 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4394 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4395 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4396 a129376b 2019-03-28 stsp
4397 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4398 e20a8b6f 2019-06-04 stsp
4399 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4400 a129376b 2019-03-28 stsp switch (ch) {
4401 33aa809d 2019-08-08 stsp case 'p':
4402 33aa809d 2019-08-08 stsp pflag = 1;
4403 33aa809d 2019-08-08 stsp break;
4404 33aa809d 2019-08-08 stsp case 'F':
4405 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4406 33aa809d 2019-08-08 stsp break;
4407 0f6d7415 2019-08-08 stsp case 'R':
4408 0f6d7415 2019-08-08 stsp can_recurse = 1;
4409 0f6d7415 2019-08-08 stsp break;
4410 a129376b 2019-03-28 stsp default:
4411 a129376b 2019-03-28 stsp usage_revert();
4412 a129376b 2019-03-28 stsp /* NOTREACHED */
4413 a129376b 2019-03-28 stsp }
4414 a129376b 2019-03-28 stsp }
4415 a129376b 2019-03-28 stsp
4416 a129376b 2019-03-28 stsp argc -= optind;
4417 a129376b 2019-03-28 stsp argv += optind;
4418 a129376b 2019-03-28 stsp
4419 43012d58 2019-07-14 stsp #ifndef PROFILE
4420 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4421 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4422 43012d58 2019-07-14 stsp err(1, "pledge");
4423 43012d58 2019-07-14 stsp #endif
4424 e20a8b6f 2019-06-04 stsp if (argc < 1)
4425 a129376b 2019-03-28 stsp usage_revert();
4426 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4427 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4428 a129376b 2019-03-28 stsp
4429 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4430 a129376b 2019-03-28 stsp if (cwd == NULL) {
4431 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4432 a129376b 2019-03-28 stsp goto done;
4433 a129376b 2019-03-28 stsp }
4434 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4435 2ec1f75b 2019-03-26 stsp if (error)
4436 2ec1f75b 2019-03-26 stsp goto done;
4437 a129376b 2019-03-28 stsp
4438 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4439 c9956ddf 2019-09-08 stsp NULL);
4440 a129376b 2019-03-28 stsp if (error != NULL)
4441 a129376b 2019-03-28 stsp goto done;
4442 a129376b 2019-03-28 stsp
4443 33aa809d 2019-08-08 stsp if (patch_script_path) {
4444 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4445 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4446 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4447 33aa809d 2019-08-08 stsp patch_script_path);
4448 33aa809d 2019-08-08 stsp goto done;
4449 33aa809d 2019-08-08 stsp }
4450 33aa809d 2019-08-08 stsp }
4451 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4452 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4453 a129376b 2019-03-28 stsp if (error)
4454 a129376b 2019-03-28 stsp goto done;
4455 a129376b 2019-03-28 stsp
4456 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4457 6d022e97 2019-08-04 stsp if (error)
4458 6d022e97 2019-08-04 stsp goto done;
4459 00db391e 2019-08-03 stsp
4460 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4461 0f6d7415 2019-08-08 stsp char *ondisk_path;
4462 0f6d7415 2019-08-08 stsp struct stat sb;
4463 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4464 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4465 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4466 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4467 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4468 0f6d7415 2019-08-08 stsp goto done;
4469 0f6d7415 2019-08-08 stsp }
4470 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4471 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4472 0f6d7415 2019-08-08 stsp free(ondisk_path);
4473 0f6d7415 2019-08-08 stsp continue;
4474 0f6d7415 2019-08-08 stsp }
4475 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4476 0f6d7415 2019-08-08 stsp ondisk_path);
4477 0f6d7415 2019-08-08 stsp free(ondisk_path);
4478 0f6d7415 2019-08-08 stsp goto done;
4479 0f6d7415 2019-08-08 stsp }
4480 0f6d7415 2019-08-08 stsp free(ondisk_path);
4481 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4482 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4483 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4484 0f6d7415 2019-08-08 stsp goto done;
4485 0f6d7415 2019-08-08 stsp }
4486 0f6d7415 2019-08-08 stsp }
4487 0f6d7415 2019-08-08 stsp }
4488 0f6d7415 2019-08-08 stsp
4489 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4490 33aa809d 2019-08-08 stsp cpa.action = "revert";
4491 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4492 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4493 a129376b 2019-03-28 stsp if (error)
4494 a129376b 2019-03-28 stsp goto done;
4495 2ec1f75b 2019-03-26 stsp done:
4496 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4497 33aa809d 2019-08-08 stsp error == NULL)
4498 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4499 2ec1f75b 2019-03-26 stsp if (repo)
4500 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4501 2ec1f75b 2019-03-26 stsp if (worktree)
4502 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4503 2ec1f75b 2019-03-26 stsp free(path);
4504 d00136be 2019-03-26 stsp free(cwd);
4505 6bad629b 2019-02-04 stsp return error;
4506 c4296144 2019-05-09 stsp }
4507 c4296144 2019-05-09 stsp
4508 c4296144 2019-05-09 stsp __dead static void
4509 c4296144 2019-05-09 stsp usage_commit(void)
4510 c4296144 2019-05-09 stsp {
4511 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4512 5c1e53bc 2019-07-28 stsp getprogname());
4513 c4296144 2019-05-09 stsp exit(1);
4514 33ad4cbe 2019-05-12 jcs }
4515 33ad4cbe 2019-05-12 jcs
4516 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4517 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4518 e2ba3d07 2019-05-13 stsp const char *editor;
4519 e0870e44 2019-05-13 stsp const char *worktree_path;
4520 76d98825 2019-06-03 stsp const char *branch_name;
4521 314a6357 2019-05-13 stsp const char *repo_path;
4522 e0870e44 2019-05-13 stsp char *logmsg_path;
4523 e2ba3d07 2019-05-13 stsp
4524 e2ba3d07 2019-05-13 stsp };
4525 e0870e44 2019-05-13 stsp
4526 33ad4cbe 2019-05-12 jcs static const struct got_error *
4527 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4528 33ad4cbe 2019-05-12 jcs void *arg)
4529 33ad4cbe 2019-05-12 jcs {
4530 76d98825 2019-06-03 stsp char *initial_content = NULL;
4531 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4532 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4533 e0870e44 2019-05-13 stsp char *template = NULL;
4534 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4535 3ce1b845 2019-07-15 stsp int fd;
4536 33ad4cbe 2019-05-12 jcs size_t len;
4537 33ad4cbe 2019-05-12 jcs
4538 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4539 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4540 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4541 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4542 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4543 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4544 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4545 33ad4cbe 2019-05-12 jcs return NULL;
4546 33ad4cbe 2019-05-12 jcs }
4547 33ad4cbe 2019-05-12 jcs
4548 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4549 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4550 76d98825 2019-06-03 stsp
4551 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4552 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4553 76d98825 2019-06-03 stsp a->branch_name) == -1)
4554 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4555 e0870e44 2019-05-13 stsp
4556 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4557 793c30b5 2019-05-13 stsp if (err)
4558 e0870e44 2019-05-13 stsp goto done;
4559 33ad4cbe 2019-05-12 jcs
4560 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4561 33ad4cbe 2019-05-12 jcs
4562 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4563 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4564 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4565 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4566 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4567 33ad4cbe 2019-05-12 jcs }
4568 33ad4cbe 2019-05-12 jcs close(fd);
4569 33ad4cbe 2019-05-12 jcs
4570 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4571 33ad4cbe 2019-05-12 jcs done:
4572 76d98825 2019-06-03 stsp free(initial_content);
4573 e0870e44 2019-05-13 stsp free(template);
4574 314a6357 2019-05-13 stsp
4575 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4576 3ce1b845 2019-07-15 stsp if (err == NULL) {
4577 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4578 3ce1b845 2019-07-15 stsp if (err) {
4579 3ce1b845 2019-07-15 stsp free(*logmsg);
4580 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4581 3ce1b845 2019-07-15 stsp }
4582 3ce1b845 2019-07-15 stsp }
4583 33ad4cbe 2019-05-12 jcs return err;
4584 6bad629b 2019-02-04 stsp }
4585 c4296144 2019-05-09 stsp
4586 c4296144 2019-05-09 stsp static const struct got_error *
4587 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4588 c4296144 2019-05-09 stsp {
4589 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4590 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4591 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4592 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4593 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4594 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4595 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4596 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4597 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4598 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4599 5c1e53bc 2019-07-28 stsp
4600 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4601 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4602 c4296144 2019-05-09 stsp
4603 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4604 c4296144 2019-05-09 stsp switch (ch) {
4605 c4296144 2019-05-09 stsp case 'm':
4606 c4296144 2019-05-09 stsp logmsg = optarg;
4607 c4296144 2019-05-09 stsp break;
4608 c4296144 2019-05-09 stsp default:
4609 c4296144 2019-05-09 stsp usage_commit();
4610 c4296144 2019-05-09 stsp /* NOTREACHED */
4611 c4296144 2019-05-09 stsp }
4612 c4296144 2019-05-09 stsp }
4613 c4296144 2019-05-09 stsp
4614 c4296144 2019-05-09 stsp argc -= optind;
4615 c4296144 2019-05-09 stsp argv += optind;
4616 c4296144 2019-05-09 stsp
4617 43012d58 2019-07-14 stsp #ifndef PROFILE
4618 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4619 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4620 43012d58 2019-07-14 stsp err(1, "pledge");
4621 43012d58 2019-07-14 stsp #endif
4622 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4623 c4296144 2019-05-09 stsp if (cwd == NULL) {
4624 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4625 c4296144 2019-05-09 stsp goto done;
4626 c4296144 2019-05-09 stsp }
4627 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4628 7d5807f4 2019-07-11 stsp if (error)
4629 7d5807f4 2019-07-11 stsp goto done;
4630 7d5807f4 2019-07-11 stsp
4631 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4632 c4296144 2019-05-09 stsp if (error)
4633 a698f62e 2019-07-25 stsp goto done;
4634 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4635 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4636 c4296144 2019-05-09 stsp goto done;
4637 a698f62e 2019-07-25 stsp }
4638 5c1e53bc 2019-07-28 stsp
4639 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4640 916f288c 2019-07-30 stsp worktree);
4641 5c1e53bc 2019-07-28 stsp if (error)
4642 5c1e53bc 2019-07-28 stsp goto done;
4643 c4296144 2019-05-09 stsp
4644 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4645 c9956ddf 2019-09-08 stsp if (error)
4646 c9956ddf 2019-09-08 stsp goto done;
4647 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4648 c9956ddf 2019-09-08 stsp gitconfig_path);
4649 c4296144 2019-05-09 stsp if (error != NULL)
4650 c4296144 2019-05-09 stsp goto done;
4651 c4296144 2019-05-09 stsp
4652 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4653 aba9c984 2019-09-08 stsp if (error)
4654 aba9c984 2019-09-08 stsp return error;
4655 aba9c984 2019-09-08 stsp
4656 314a6357 2019-05-13 stsp /*
4657 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4658 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4659 314a6357 2019-05-13 stsp */
4660 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4661 314a6357 2019-05-13 stsp error = get_editor(&editor);
4662 314a6357 2019-05-13 stsp else
4663 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4664 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4665 c4296144 2019-05-09 stsp if (error)
4666 c4296144 2019-05-09 stsp goto done;
4667 c4296144 2019-05-09 stsp
4668 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4669 087fb88c 2019-08-04 stsp if (error)
4670 087fb88c 2019-08-04 stsp goto done;
4671 087fb88c 2019-08-04 stsp
4672 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4673 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4674 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4675 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4676 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4677 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4678 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4679 916f288c 2019-07-30 stsp goto done;
4680 916f288c 2019-07-30 stsp }
4681 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4682 916f288c 2019-07-30 stsp }
4683 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4684 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4685 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4686 e0870e44 2019-05-13 stsp if (error) {
4687 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4688 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4689 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4690 c4296144 2019-05-09 stsp goto done;
4691 e0870e44 2019-05-13 stsp }
4692 c4296144 2019-05-09 stsp
4693 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4694 c4296144 2019-05-09 stsp if (error)
4695 c4296144 2019-05-09 stsp goto done;
4696 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4697 c4296144 2019-05-09 stsp done:
4698 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4699 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4700 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4701 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4702 7266f21f 2019-10-21 stsp error == NULL)
4703 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4704 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4705 c4296144 2019-05-09 stsp if (repo)
4706 c4296144 2019-05-09 stsp got_repo_close(repo);
4707 c4296144 2019-05-09 stsp if (worktree)
4708 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4709 c4296144 2019-05-09 stsp free(cwd);
4710 c4296144 2019-05-09 stsp free(id_str);
4711 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4712 e2ba3d07 2019-05-13 stsp free(editor);
4713 aba9c984 2019-09-08 stsp free(author);
4714 234035bc 2019-06-01 stsp return error;
4715 234035bc 2019-06-01 stsp }
4716 234035bc 2019-06-01 stsp
4717 234035bc 2019-06-01 stsp __dead static void
4718 234035bc 2019-06-01 stsp usage_cherrypick(void)
4719 234035bc 2019-06-01 stsp {
4720 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4721 234035bc 2019-06-01 stsp exit(1);
4722 234035bc 2019-06-01 stsp }
4723 234035bc 2019-06-01 stsp
4724 234035bc 2019-06-01 stsp static const struct got_error *
4725 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4726 234035bc 2019-06-01 stsp {
4727 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4728 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4729 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4730 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4731 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4732 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4733 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4734 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4735 234035bc 2019-06-01 stsp int ch, did_something = 0;
4736 234035bc 2019-06-01 stsp
4737 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4738 234035bc 2019-06-01 stsp switch (ch) {
4739 234035bc 2019-06-01 stsp default:
4740 234035bc 2019-06-01 stsp usage_cherrypick();
4741 234035bc 2019-06-01 stsp /* NOTREACHED */
4742 234035bc 2019-06-01 stsp }
4743 234035bc 2019-06-01 stsp }
4744 234035bc 2019-06-01 stsp
4745 234035bc 2019-06-01 stsp argc -= optind;
4746 234035bc 2019-06-01 stsp argv += optind;
4747 234035bc 2019-06-01 stsp
4748 43012d58 2019-07-14 stsp #ifndef PROFILE
4749 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4750 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4751 43012d58 2019-07-14 stsp err(1, "pledge");
4752 43012d58 2019-07-14 stsp #endif
4753 234035bc 2019-06-01 stsp if (argc != 1)
4754 234035bc 2019-06-01 stsp usage_cherrypick();
4755 234035bc 2019-06-01 stsp
4756 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4757 234035bc 2019-06-01 stsp if (cwd == NULL) {
4758 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4759 234035bc 2019-06-01 stsp goto done;
4760 234035bc 2019-06-01 stsp }
4761 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4762 234035bc 2019-06-01 stsp if (error)
4763 234035bc 2019-06-01 stsp goto done;
4764 234035bc 2019-06-01 stsp
4765 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4766 c9956ddf 2019-09-08 stsp NULL);
4767 234035bc 2019-06-01 stsp if (error != NULL)
4768 234035bc 2019-06-01 stsp goto done;
4769 234035bc 2019-06-01 stsp
4770 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4771 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4772 234035bc 2019-06-01 stsp if (error)
4773 234035bc 2019-06-01 stsp goto done;
4774 234035bc 2019-06-01 stsp
4775 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4776 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4777 234035bc 2019-06-01 stsp if (error != NULL) {
4778 234035bc 2019-06-01 stsp struct got_reference *ref;
4779 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4780 234035bc 2019-06-01 stsp goto done;
4781 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4782 234035bc 2019-06-01 stsp if (error != NULL)
4783 234035bc 2019-06-01 stsp goto done;
4784 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4785 234035bc 2019-06-01 stsp got_ref_close(ref);
4786 234035bc 2019-06-01 stsp if (error != NULL)
4787 234035bc 2019-06-01 stsp goto done;
4788 234035bc 2019-06-01 stsp }
4789 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4790 234035bc 2019-06-01 stsp if (error)
4791 234035bc 2019-06-01 stsp goto done;
4792 234035bc 2019-06-01 stsp
4793 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4794 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4795 234035bc 2019-06-01 stsp if (error != NULL)
4796 234035bc 2019-06-01 stsp goto done;
4797 234035bc 2019-06-01 stsp
4798 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4799 234035bc 2019-06-01 stsp if (error) {
4800 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4801 234035bc 2019-06-01 stsp goto done;
4802 234035bc 2019-06-01 stsp error = NULL;
4803 234035bc 2019-06-01 stsp } else {
4804 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4805 234035bc 2019-06-01 stsp goto done;
4806 234035bc 2019-06-01 stsp }
4807 234035bc 2019-06-01 stsp
4808 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4809 234035bc 2019-06-01 stsp if (error)
4810 234035bc 2019-06-01 stsp goto done;
4811 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4812 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4813 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4814 03415a1a 2019-06-02 stsp NULL);
4815 234035bc 2019-06-01 stsp if (error != NULL)
4816 234035bc 2019-06-01 stsp goto done;
4817 234035bc 2019-06-01 stsp
4818 234035bc 2019-06-01 stsp if (did_something)
4819 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4820 234035bc 2019-06-01 stsp done:
4821 234035bc 2019-06-01 stsp if (commit)
4822 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4823 234035bc 2019-06-01 stsp free(commit_id_str);
4824 234035bc 2019-06-01 stsp if (head_ref)
4825 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4826 234035bc 2019-06-01 stsp if (worktree)
4827 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4828 234035bc 2019-06-01 stsp if (repo)
4829 234035bc 2019-06-01 stsp got_repo_close(repo);
4830 c4296144 2019-05-09 stsp return error;
4831 c4296144 2019-05-09 stsp }
4832 5ef14e63 2019-06-02 stsp
4833 5ef14e63 2019-06-02 stsp __dead static void
4834 5ef14e63 2019-06-02 stsp usage_backout(void)
4835 5ef14e63 2019-06-02 stsp {
4836 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4837 5ef14e63 2019-06-02 stsp exit(1);
4838 5ef14e63 2019-06-02 stsp }
4839 5ef14e63 2019-06-02 stsp
4840 5ef14e63 2019-06-02 stsp static const struct got_error *
4841 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4842 5ef14e63 2019-06-02 stsp {
4843 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4844 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4845 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4846 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4847 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4848 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4849 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4850 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4851 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4852 5ef14e63 2019-06-02 stsp
4853 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4854 5ef14e63 2019-06-02 stsp switch (ch) {
4855 5ef14e63 2019-06-02 stsp default:
4856 5ef14e63 2019-06-02 stsp usage_backout();
4857 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4858 5ef14e63 2019-06-02 stsp }
4859 5ef14e63 2019-06-02 stsp }
4860 5ef14e63 2019-06-02 stsp
4861 5ef14e63 2019-06-02 stsp argc -= optind;
4862 5ef14e63 2019-06-02 stsp argv += optind;
4863 5ef14e63 2019-06-02 stsp
4864 43012d58 2019-07-14 stsp #ifndef PROFILE
4865 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4866 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4867 43012d58 2019-07-14 stsp err(1, "pledge");
4868 43012d58 2019-07-14 stsp #endif
4869 5ef14e63 2019-06-02 stsp if (argc != 1)
4870 5ef14e63 2019-06-02 stsp usage_backout();
4871 5ef14e63 2019-06-02 stsp
4872 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4873 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4874 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4875 5ef14e63 2019-06-02 stsp goto done;
4876 5ef14e63 2019-06-02 stsp }
4877 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4878 5ef14e63 2019-06-02 stsp if (error)
4879 5ef14e63 2019-06-02 stsp goto done;
4880 5ef14e63 2019-06-02 stsp
4881 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4882 c9956ddf 2019-09-08 stsp NULL);
4883 5ef14e63 2019-06-02 stsp if (error != NULL)
4884 5ef14e63 2019-06-02 stsp goto done;
4885 5ef14e63 2019-06-02 stsp
4886 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4887 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4888 5ef14e63 2019-06-02 stsp if (error)
4889 5ef14e63 2019-06-02 stsp goto done;
4890 5ef14e63 2019-06-02 stsp
4891 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4892 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4893 5ef14e63 2019-06-02 stsp if (error != NULL) {
4894 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4895 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4896 5ef14e63 2019-06-02 stsp goto done;
4897 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4898 5ef14e63 2019-06-02 stsp if (error != NULL)
4899 5ef14e63 2019-06-02 stsp goto done;
4900 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4901 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4902 5ef14e63 2019-06-02 stsp if (error != NULL)
4903 5ef14e63 2019-06-02 stsp goto done;
4904 5ef14e63 2019-06-02 stsp }
4905 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4906 5ef14e63 2019-06-02 stsp if (error)
4907 5ef14e63 2019-06-02 stsp goto done;
4908 5ef14e63 2019-06-02 stsp
4909 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4910 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4911 5ef14e63 2019-06-02 stsp if (error != NULL)
4912 5ef14e63 2019-06-02 stsp goto done;
4913 5ef14e63 2019-06-02 stsp
4914 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4915 5ef14e63 2019-06-02 stsp if (error)
4916 5ef14e63 2019-06-02 stsp goto done;
4917 5ef14e63 2019-06-02 stsp
4918 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4919 5ef14e63 2019-06-02 stsp if (error)
4920 5ef14e63 2019-06-02 stsp goto done;
4921 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4922 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4923 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4924 5ef14e63 2019-06-02 stsp goto done;
4925 5ef14e63 2019-06-02 stsp }
4926 5ef14e63 2019-06-02 stsp
4927 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4928 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4929 5ef14e63 2019-06-02 stsp if (error != NULL)
4930 5ef14e63 2019-06-02 stsp goto done;
4931 5ef14e63 2019-06-02 stsp
4932 5ef14e63 2019-06-02 stsp if (did_something)
4933 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4934 5ef14e63 2019-06-02 stsp done:
4935 5ef14e63 2019-06-02 stsp if (commit)
4936 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4937 5ef14e63 2019-06-02 stsp free(commit_id_str);
4938 5ef14e63 2019-06-02 stsp if (head_ref)
4939 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4940 818c7501 2019-07-11 stsp if (worktree)
4941 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4942 818c7501 2019-07-11 stsp if (repo)
4943 818c7501 2019-07-11 stsp got_repo_close(repo);
4944 818c7501 2019-07-11 stsp return error;
4945 818c7501 2019-07-11 stsp }
4946 818c7501 2019-07-11 stsp
4947 818c7501 2019-07-11 stsp __dead static void
4948 818c7501 2019-07-11 stsp usage_rebase(void)
4949 818c7501 2019-07-11 stsp {
4950 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4951 af54c8f8 2019-07-11 stsp getprogname());
4952 818c7501 2019-07-11 stsp exit(1);
4953 0ebf8283 2019-07-24 stsp }
4954 0ebf8283 2019-07-24 stsp
4955 0ebf8283 2019-07-24 stsp void
4956 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4957 0ebf8283 2019-07-24 stsp {
4958 0ebf8283 2019-07-24 stsp char *nl;
4959 0ebf8283 2019-07-24 stsp size_t len;
4960 0ebf8283 2019-07-24 stsp
4961 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4962 0ebf8283 2019-07-24 stsp if (len > limit)
4963 0ebf8283 2019-07-24 stsp len = limit;
4964 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4965 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4966 0ebf8283 2019-07-24 stsp if (nl)
4967 0ebf8283 2019-07-24 stsp *nl = '\0';
4968 0ebf8283 2019-07-24 stsp }
4969 0ebf8283 2019-07-24 stsp
4970 0ebf8283 2019-07-24 stsp static const struct got_error *
4971 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4972 0ebf8283 2019-07-24 stsp {
4973 5943eee2 2019-08-13 stsp const struct got_error *err;
4974 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4975 5943eee2 2019-08-13 stsp const char *s;
4976 0ebf8283 2019-07-24 stsp
4977 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4978 5943eee2 2019-08-13 stsp if (err)
4979 5943eee2 2019-08-13 stsp return err;
4980 0ebf8283 2019-07-24 stsp
4981 5943eee2 2019-08-13 stsp s = logmsg0;
4982 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4983 5943eee2 2019-08-13 stsp s++;
4984 5943eee2 2019-08-13 stsp
4985 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4986 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4987 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4988 5943eee2 2019-08-13 stsp goto done;
4989 5943eee2 2019-08-13 stsp }
4990 0ebf8283 2019-07-24 stsp
4991 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4992 5943eee2 2019-08-13 stsp done:
4993 5943eee2 2019-08-13 stsp free(logmsg0);
4994 5943eee2 2019-08-13 stsp return err;
4995 818c7501 2019-07-11 stsp }
4996 818c7501 2019-07-11 stsp
4997 818c7501 2019-07-11 stsp static const struct got_error *
4998 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4999 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5000 818c7501 2019-07-11 stsp {
5001 818c7501 2019-07-11 stsp const struct got_error *err;
5002 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5003 818c7501 2019-07-11 stsp
5004 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5005 818c7501 2019-07-11 stsp if (err)
5006 818c7501 2019-07-11 stsp goto done;
5007 818c7501 2019-07-11 stsp
5008 ff0d2220 2019-07-11 stsp if (new_id) {
5009 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5010 ff0d2220 2019-07-11 stsp if (err)
5011 ff0d2220 2019-07-11 stsp goto done;
5012 ff0d2220 2019-07-11 stsp }
5013 818c7501 2019-07-11 stsp
5014 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5015 ff0d2220 2019-07-11 stsp if (new_id_str)
5016 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5017 0ebf8283 2019-07-24 stsp
5018 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5019 0ebf8283 2019-07-24 stsp if (err)
5020 0ebf8283 2019-07-24 stsp goto done;
5021 0ebf8283 2019-07-24 stsp
5022 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5023 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5024 818c7501 2019-07-11 stsp done:
5025 818c7501 2019-07-11 stsp free(old_id_str);
5026 818c7501 2019-07-11 stsp free(new_id_str);
5027 818c7501 2019-07-11 stsp return err;
5028 818c7501 2019-07-11 stsp }
5029 818c7501 2019-07-11 stsp
5030 1ee397ad 2019-07-12 stsp static const struct got_error *
5031 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5032 818c7501 2019-07-11 stsp {
5033 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5034 818c7501 2019-07-11 stsp
5035 818c7501 2019-07-11 stsp while (path[0] == '/')
5036 818c7501 2019-07-11 stsp path++;
5037 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5038 818c7501 2019-07-11 stsp
5039 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5040 1ee397ad 2019-07-12 stsp return NULL;
5041 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5042 818c7501 2019-07-11 stsp *rebase_status = status;
5043 1ee397ad 2019-07-12 stsp return NULL;
5044 818c7501 2019-07-11 stsp }
5045 818c7501 2019-07-11 stsp
5046 818c7501 2019-07-11 stsp static const struct got_error *
5047 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5048 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5049 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5050 818c7501 2019-07-11 stsp {
5051 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5052 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5053 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5054 818c7501 2019-07-11 stsp }
5055 818c7501 2019-07-11 stsp
5056 818c7501 2019-07-11 stsp static const struct got_error *
5057 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5058 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5059 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5060 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5061 ff0d2220 2019-07-11 stsp {
5062 ff0d2220 2019-07-11 stsp const struct got_error *error;
5063 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5064 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5065 ff0d2220 2019-07-11 stsp
5066 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5067 ff0d2220 2019-07-11 stsp if (error)
5068 ff0d2220 2019-07-11 stsp return error;
5069 ff0d2220 2019-07-11 stsp
5070 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5071 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5072 ff0d2220 2019-07-11 stsp if (error) {
5073 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5074 ff0d2220 2019-07-11 stsp goto done;
5075 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5076 ff0d2220 2019-07-11 stsp } else {
5077 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5078 ff0d2220 2019-07-11 stsp free(new_commit_id);
5079 ff0d2220 2019-07-11 stsp }
5080 ff0d2220 2019-07-11 stsp done:
5081 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5082 ff0d2220 2019-07-11 stsp return error;
5083 64c6d990 2019-07-11 stsp }
5084 64c6d990 2019-07-11 stsp
5085 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5086 64c6d990 2019-07-11 stsp const char *path_prefix;
5087 64c6d990 2019-07-11 stsp size_t len;
5088 8ca9bd68 2019-07-25 stsp int errcode;
5089 64c6d990 2019-07-11 stsp };
5090 64c6d990 2019-07-11 stsp
5091 64c6d990 2019-07-11 stsp static const struct got_error *
5092 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5093 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5094 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5095 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5096 64c6d990 2019-07-11 stsp {
5097 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5098 64c6d990 2019-07-11 stsp
5099 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5100 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5101 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5102 64c6d990 2019-07-11 stsp
5103 64c6d990 2019-07-11 stsp return NULL;
5104 64c6d990 2019-07-11 stsp }
5105 64c6d990 2019-07-11 stsp
5106 64c6d990 2019-07-11 stsp static const struct got_error *
5107 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5108 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5109 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5110 64c6d990 2019-07-11 stsp {
5111 64c6d990 2019-07-11 stsp const struct got_error *err;
5112 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5113 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5114 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5115 64c6d990 2019-07-11 stsp
5116 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5117 64c6d990 2019-07-11 stsp return NULL;
5118 64c6d990 2019-07-11 stsp
5119 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5120 64c6d990 2019-07-11 stsp if (err)
5121 64c6d990 2019-07-11 stsp goto done;
5122 64c6d990 2019-07-11 stsp
5123 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5124 64c6d990 2019-07-11 stsp if (err)
5125 64c6d990 2019-07-11 stsp goto done;
5126 64c6d990 2019-07-11 stsp
5127 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5128 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5129 64c6d990 2019-07-11 stsp if (err)
5130 64c6d990 2019-07-11 stsp goto done;
5131 64c6d990 2019-07-11 stsp
5132 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5133 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5134 64c6d990 2019-07-11 stsp if (err)
5135 64c6d990 2019-07-11 stsp goto done;
5136 64c6d990 2019-07-11 stsp
5137 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5138 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5139 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5140 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5141 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5142 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5143 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5144 64c6d990 2019-07-11 stsp done:
5145 64c6d990 2019-07-11 stsp if (tree1)
5146 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5147 64c6d990 2019-07-11 stsp if (tree2)
5148 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5149 64c6d990 2019-07-11 stsp if (commit)
5150 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5151 64c6d990 2019-07-11 stsp if (parent_commit)
5152 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5153 64c6d990 2019-07-11 stsp return err;
5154 ff0d2220 2019-07-11 stsp }
5155 ff0d2220 2019-07-11 stsp
5156 ff0d2220 2019-07-11 stsp static const struct got_error *
5157 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5158 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5159 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5160 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5161 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5162 af61c510 2019-07-19 stsp {
5163 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5164 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5165 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5166 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5167 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5168 af61c510 2019-07-19 stsp
5169 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5170 af61c510 2019-07-19 stsp if (err)
5171 af61c510 2019-07-19 stsp return err;
5172 af61c510 2019-07-19 stsp
5173 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5174 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5175 af61c510 2019-07-19 stsp if (err)
5176 af61c510 2019-07-19 stsp goto done;
5177 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5178 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5179 af61c510 2019-07-19 stsp if (err) {
5180 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5181 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5182 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5183 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5184 af61c510 2019-07-19 stsp "been reached?!?");
5185 af61c510 2019-07-19 stsp goto done;
5186 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5187 af61c510 2019-07-19 stsp goto done;
5188 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5189 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5190 af61c510 2019-07-19 stsp if (err)
5191 af61c510 2019-07-19 stsp goto done;
5192 af61c510 2019-07-19 stsp } else {
5193 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5194 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5195 af61c510 2019-07-19 stsp if (err)
5196 af61c510 2019-07-19 stsp goto done;
5197 af61c510 2019-07-19 stsp
5198 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5199 af61c510 2019-07-19 stsp if (err)
5200 af61c510 2019-07-19 stsp goto done;
5201 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5202 af61c510 2019-07-19 stsp commit_id = parent_id;
5203 af61c510 2019-07-19 stsp }
5204 af61c510 2019-07-19 stsp }
5205 af61c510 2019-07-19 stsp done:
5206 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5207 af61c510 2019-07-19 stsp return err;
5208 af61c510 2019-07-19 stsp }
5209 af61c510 2019-07-19 stsp
5210 af61c510 2019-07-19 stsp static const struct got_error *
5211 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5212 818c7501 2019-07-11 stsp {
5213 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5214 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5215 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5216 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5217 818c7501 2019-07-11 stsp char *cwd = NULL;
5218 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5219 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5220 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5221 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5222 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5223 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5224 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5225 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5226 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5227 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5228 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5229 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5230 818c7501 2019-07-11 stsp
5231 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5232 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5233 818c7501 2019-07-11 stsp
5234 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5235 818c7501 2019-07-11 stsp switch (ch) {
5236 818c7501 2019-07-11 stsp case 'a':
5237 818c7501 2019-07-11 stsp abort_rebase = 1;
5238 818c7501 2019-07-11 stsp break;
5239 818c7501 2019-07-11 stsp case 'c':
5240 818c7501 2019-07-11 stsp continue_rebase = 1;
5241 818c7501 2019-07-11 stsp break;
5242 818c7501 2019-07-11 stsp default:
5243 818c7501 2019-07-11 stsp usage_rebase();
5244 818c7501 2019-07-11 stsp /* NOTREACHED */
5245 818c7501 2019-07-11 stsp }
5246 818c7501 2019-07-11 stsp }
5247 818c7501 2019-07-11 stsp
5248 818c7501 2019-07-11 stsp argc -= optind;
5249 818c7501 2019-07-11 stsp argv += optind;
5250 818c7501 2019-07-11 stsp
5251 43012d58 2019-07-14 stsp #ifndef PROFILE
5252 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5253 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5254 43012d58 2019-07-14 stsp err(1, "pledge");
5255 43012d58 2019-07-14 stsp #endif
5256 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5257 818c7501 2019-07-11 stsp usage_rebase();
5258 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5259 818c7501 2019-07-11 stsp if (argc != 0)
5260 818c7501 2019-07-11 stsp usage_rebase();
5261 818c7501 2019-07-11 stsp } else if (argc != 1)
5262 818c7501 2019-07-11 stsp usage_rebase();
5263 818c7501 2019-07-11 stsp
5264 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5265 818c7501 2019-07-11 stsp if (cwd == NULL) {
5266 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5267 818c7501 2019-07-11 stsp goto done;
5268 818c7501 2019-07-11 stsp }
5269 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5270 818c7501 2019-07-11 stsp if (error)
5271 818c7501 2019-07-11 stsp goto done;
5272 818c7501 2019-07-11 stsp
5273 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5274 c9956ddf 2019-09-08 stsp NULL);
5275 818c7501 2019-07-11 stsp if (error != NULL)
5276 818c7501 2019-07-11 stsp goto done;
5277 818c7501 2019-07-11 stsp
5278 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5279 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5280 818c7501 2019-07-11 stsp if (error)
5281 818c7501 2019-07-11 stsp goto done;
5282 818c7501 2019-07-11 stsp
5283 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5284 818c7501 2019-07-11 stsp if (error)
5285 818c7501 2019-07-11 stsp goto done;
5286 818c7501 2019-07-11 stsp
5287 f6794adc 2019-07-23 stsp if (abort_rebase) {
5288 818c7501 2019-07-11 stsp int did_something;
5289 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5290 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5291 f6794adc 2019-07-23 stsp goto done;
5292 f6794adc 2019-07-23 stsp }
5293 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5294 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5295 3e3a69f1 2019-07-25 stsp worktree, repo);
5296 818c7501 2019-07-11 stsp if (error)
5297 818c7501 2019-07-11 stsp goto done;
5298 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5299 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5300 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5301 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5302 818c7501 2019-07-11 stsp if (error)
5303 818c7501 2019-07-11 stsp goto done;
5304 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5305 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5306 818c7501 2019-07-11 stsp }
5307 818c7501 2019-07-11 stsp
5308 818c7501 2019-07-11 stsp if (continue_rebase) {
5309 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5310 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5311 f6794adc 2019-07-23 stsp goto done;
5312 f6794adc 2019-07-23 stsp }
5313 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5314 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5315 3e3a69f1 2019-07-25 stsp worktree, repo);
5316 818c7501 2019-07-11 stsp if (error)
5317 818c7501 2019-07-11 stsp goto done;
5318 818c7501 2019-07-11 stsp
5319 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5320 01757395 2019-07-12 stsp resume_commit_id, repo);
5321 818c7501 2019-07-11 stsp if (error)
5322 818c7501 2019-07-11 stsp goto done;
5323 818c7501 2019-07-11 stsp
5324 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5325 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5326 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5327 818c7501 2019-07-11 stsp goto done;
5328 818c7501 2019-07-11 stsp }
5329 818c7501 2019-07-11 stsp } else {
5330 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5331 818c7501 2019-07-11 stsp if (error != NULL)
5332 818c7501 2019-07-11 stsp goto done;
5333 ff0d2220 2019-07-11 stsp }
5334 818c7501 2019-07-11 stsp
5335 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5336 ff0d2220 2019-07-11 stsp if (error)
5337 ff0d2220 2019-07-11 stsp goto done;
5338 ff0d2220 2019-07-11 stsp
5339 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5340 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5341 a51a74b3 2019-07-27 stsp
5342 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5343 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5344 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5345 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5346 818c7501 2019-07-11 stsp if (error)
5347 818c7501 2019-07-11 stsp goto done;
5348 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5349 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5350 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5351 818c7501 2019-07-11 stsp "with work tree's branch");
5352 818c7501 2019-07-11 stsp goto done;
5353 818c7501 2019-07-11 stsp }
5354 818c7501 2019-07-11 stsp
5355 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5356 a51a74b3 2019-07-27 stsp if (error) {
5357 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5358 a51a74b3 2019-07-27 stsp goto done;
5359 a51a74b3 2019-07-27 stsp error = NULL;
5360 a51a74b3 2019-07-27 stsp } else {
5361 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5362 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5363 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5364 a51a74b3 2019-07-27 stsp goto done;
5365 a51a74b3 2019-07-27 stsp }
5366 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5367 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5368 818c7501 2019-07-11 stsp if (error)
5369 818c7501 2019-07-11 stsp goto done;
5370 818c7501 2019-07-11 stsp }
5371 818c7501 2019-07-11 stsp
5372 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5373 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5374 818c7501 2019-07-11 stsp if (error)
5375 818c7501 2019-07-11 stsp goto done;
5376 818c7501 2019-07-11 stsp
5377 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5378 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5379 fc66b545 2019-08-12 stsp if (pid == NULL) {
5380 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5381 fc66b545 2019-08-12 stsp int did_something;
5382 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5383 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5384 fc66b545 2019-08-12 stsp &did_something);
5385 fc66b545 2019-08-12 stsp if (error)
5386 fc66b545 2019-08-12 stsp goto done;
5387 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5388 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5389 fc66b545 2019-08-12 stsp }
5390 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5391 fc66b545 2019-08-12 stsp goto done;
5392 fc66b545 2019-08-12 stsp }
5393 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5394 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5395 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5396 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5397 818c7501 2019-07-11 stsp commit = NULL;
5398 818c7501 2019-07-11 stsp if (error)
5399 818c7501 2019-07-11 stsp goto done;
5400 64c6d990 2019-07-11 stsp
5401 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5402 ff0d2220 2019-07-11 stsp if (continue_rebase)
5403 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5404 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5405 ff0d2220 2019-07-11 stsp else
5406 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5407 818c7501 2019-07-11 stsp goto done;
5408 818c7501 2019-07-11 stsp }
5409 818c7501 2019-07-11 stsp
5410 818c7501 2019-07-11 stsp pid = NULL;
5411 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5412 818c7501 2019-07-11 stsp commit_id = qid->id;
5413 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5414 818c7501 2019-07-11 stsp pid = qid;
5415 818c7501 2019-07-11 stsp
5416 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5417 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5418 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5419 818c7501 2019-07-11 stsp if (error)
5420 818c7501 2019-07-11 stsp goto done;
5421 818c7501 2019-07-11 stsp
5422 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5423 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5424 818c7501 2019-07-11 stsp break;
5425 01757395 2019-07-12 stsp }
5426 818c7501 2019-07-11 stsp
5427 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5428 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5429 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5430 818c7501 2019-07-11 stsp if (error)
5431 818c7501 2019-07-11 stsp goto done;
5432 818c7501 2019-07-11 stsp }
5433 818c7501 2019-07-11 stsp
5434 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5435 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5436 818c7501 2019-07-11 stsp if (error)
5437 818c7501 2019-07-11 stsp goto done;
5438 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5439 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5440 818c7501 2019-07-11 stsp } else
5441 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5442 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5443 818c7501 2019-07-11 stsp done:
5444 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5445 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5446 818c7501 2019-07-11 stsp free(resume_commit_id);
5447 818c7501 2019-07-11 stsp free(yca_id);
5448 818c7501 2019-07-11 stsp if (commit)
5449 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5450 818c7501 2019-07-11 stsp if (branch)
5451 818c7501 2019-07-11 stsp got_ref_close(branch);
5452 818c7501 2019-07-11 stsp if (new_base_branch)
5453 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5454 818c7501 2019-07-11 stsp if (tmp_branch)
5455 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5456 5ef14e63 2019-06-02 stsp if (worktree)
5457 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5458 5ef14e63 2019-06-02 stsp if (repo)
5459 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5460 5ef14e63 2019-06-02 stsp return error;
5461 0ebf8283 2019-07-24 stsp }
5462 0ebf8283 2019-07-24 stsp
5463 0ebf8283 2019-07-24 stsp __dead static void
5464 0ebf8283 2019-07-24 stsp usage_histedit(void)
5465 0ebf8283 2019-07-24 stsp {
5466 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5467 0ebf8283 2019-07-24 stsp getprogname());
5468 0ebf8283 2019-07-24 stsp exit(1);
5469 0ebf8283 2019-07-24 stsp }
5470 0ebf8283 2019-07-24 stsp
5471 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5472 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5473 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5474 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5475 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5476 0ebf8283 2019-07-24 stsp
5477 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5478 0ebf8283 2019-07-24 stsp unsigned char code;
5479 0ebf8283 2019-07-24 stsp const char *name;
5480 0ebf8283 2019-07-24 stsp const char *desc;
5481 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5482 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5483 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5484 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5485 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5486 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5487 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5488 0ebf8283 2019-07-24 stsp };
5489 0ebf8283 2019-07-24 stsp
5490 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5491 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5492 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5493 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5494 0ebf8283 2019-07-24 stsp char *logmsg;
5495 0ebf8283 2019-07-24 stsp };
5496 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5497 0ebf8283 2019-07-24 stsp
5498 0ebf8283 2019-07-24 stsp static const struct got_error *
5499 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5500 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5501 0ebf8283 2019-07-24 stsp {
5502 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5503 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5504 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5505 8138f3e1 2019-08-11 stsp int n;
5506 0ebf8283 2019-07-24 stsp
5507 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5508 0ebf8283 2019-07-24 stsp if (err)
5509 0ebf8283 2019-07-24 stsp goto done;
5510 0ebf8283 2019-07-24 stsp
5511 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5512 0ebf8283 2019-07-24 stsp if (err)
5513 0ebf8283 2019-07-24 stsp goto done;
5514 0ebf8283 2019-07-24 stsp
5515 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5516 0ebf8283 2019-07-24 stsp if (err)
5517 0ebf8283 2019-07-24 stsp goto done;
5518 0ebf8283 2019-07-24 stsp
5519 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5520 0ebf8283 2019-07-24 stsp if (n < 0)
5521 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5522 0ebf8283 2019-07-24 stsp done:
5523 0ebf8283 2019-07-24 stsp if (commit)
5524 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5525 0ebf8283 2019-07-24 stsp free(id_str);
5526 0ebf8283 2019-07-24 stsp free(logmsg);
5527 0ebf8283 2019-07-24 stsp return err;
5528 0ebf8283 2019-07-24 stsp }
5529 0ebf8283 2019-07-24 stsp
5530 0ebf8283 2019-07-24 stsp static const struct got_error *
5531 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5532 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5533 0ebf8283 2019-07-24 stsp {
5534 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5535 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5536 0ebf8283 2019-07-24 stsp
5537 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5538 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5539 0ebf8283 2019-07-24 stsp
5540 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5541 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5542 0ebf8283 2019-07-24 stsp f, repo);
5543 0ebf8283 2019-07-24 stsp if (err)
5544 0ebf8283 2019-07-24 stsp break;
5545 0ebf8283 2019-07-24 stsp }
5546 0ebf8283 2019-07-24 stsp
5547 0ebf8283 2019-07-24 stsp return err;
5548 0ebf8283 2019-07-24 stsp }
5549 0ebf8283 2019-07-24 stsp
5550 0ebf8283 2019-07-24 stsp static const struct got_error *
5551 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5552 0ebf8283 2019-07-24 stsp {
5553 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5554 0ebf8283 2019-07-24 stsp int n, i;
5555 0ebf8283 2019-07-24 stsp
5556 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5557 0ebf8283 2019-07-24 stsp if (n < 0)
5558 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5559 0ebf8283 2019-07-24 stsp
5560 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5561 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5562 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5563 0ebf8283 2019-07-24 stsp cmd->desc);
5564 0ebf8283 2019-07-24 stsp if (n < 0) {
5565 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5566 0ebf8283 2019-07-24 stsp break;
5567 0ebf8283 2019-07-24 stsp }
5568 0ebf8283 2019-07-24 stsp }
5569 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5570 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5571 0ebf8283 2019-07-24 stsp if (n < 0)
5572 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5573 0ebf8283 2019-07-24 stsp return err;
5574 0ebf8283 2019-07-24 stsp }
5575 0ebf8283 2019-07-24 stsp
5576 0ebf8283 2019-07-24 stsp static const struct got_error *
5577 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5578 0ebf8283 2019-07-24 stsp {
5579 0ebf8283 2019-07-24 stsp static char msg[42];
5580 0ebf8283 2019-07-24 stsp int ret;
5581 0ebf8283 2019-07-24 stsp
5582 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5583 0ebf8283 2019-07-24 stsp lineno);
5584 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5585 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5586 0ebf8283 2019-07-24 stsp
5587 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5588 0ebf8283 2019-07-24 stsp }
5589 0ebf8283 2019-07-24 stsp
5590 0ebf8283 2019-07-24 stsp static const struct got_error *
5591 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5592 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5593 0ebf8283 2019-07-24 stsp {
5594 0ebf8283 2019-07-24 stsp const struct got_error *err;
5595 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5596 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5597 0ebf8283 2019-07-24 stsp
5598 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5599 0ebf8283 2019-07-24 stsp if (err)
5600 0ebf8283 2019-07-24 stsp return err;
5601 0ebf8283 2019-07-24 stsp
5602 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5603 0ebf8283 2019-07-24 stsp if (err)
5604 0ebf8283 2019-07-24 stsp goto done;
5605 0ebf8283 2019-07-24 stsp
5606 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5607 5943eee2 2019-08-13 stsp if (err)
5608 5943eee2 2019-08-13 stsp goto done;
5609 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5610 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5611 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5612 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5613 0ebf8283 2019-07-24 stsp goto done;
5614 0ebf8283 2019-07-24 stsp }
5615 0ebf8283 2019-07-24 stsp done:
5616 0ebf8283 2019-07-24 stsp if (folded_commit)
5617 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5618 0ebf8283 2019-07-24 stsp free(id_str);
5619 5943eee2 2019-08-13 stsp free(folded_logmsg);
5620 0ebf8283 2019-07-24 stsp return err;
5621 0ebf8283 2019-07-24 stsp }
5622 0ebf8283 2019-07-24 stsp
5623 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5624 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5625 0ebf8283 2019-07-24 stsp {
5626 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5627 0ebf8283 2019-07-24 stsp
5628 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5629 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5630 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5631 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5632 3f9de99f 2019-07-24 stsp folded = prev;
5633 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5634 0ebf8283 2019-07-24 stsp }
5635 0ebf8283 2019-07-24 stsp
5636 0ebf8283 2019-07-24 stsp return folded;
5637 0ebf8283 2019-07-24 stsp }
5638 0ebf8283 2019-07-24 stsp
5639 0ebf8283 2019-07-24 stsp static const struct got_error *
5640 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5641 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5642 0ebf8283 2019-07-24 stsp {
5643 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5644 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5645 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5646 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5647 0ebf8283 2019-07-24 stsp int fd;
5648 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5649 0ebf8283 2019-07-24 stsp
5650 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5651 0ebf8283 2019-07-24 stsp if (err)
5652 0ebf8283 2019-07-24 stsp return err;
5653 0ebf8283 2019-07-24 stsp
5654 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5655 0ebf8283 2019-07-24 stsp if (folded) {
5656 0ebf8283 2019-07-24 stsp while (folded != hle) {
5657 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5658 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5659 3f9de99f 2019-07-24 stsp continue;
5660 3f9de99f 2019-07-24 stsp }
5661 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5662 0ebf8283 2019-07-24 stsp logmsg, repo);
5663 0ebf8283 2019-07-24 stsp if (err)
5664 0ebf8283 2019-07-24 stsp goto done;
5665 0ebf8283 2019-07-24 stsp free(logmsg);
5666 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5667 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5668 0ebf8283 2019-07-24 stsp }
5669 0ebf8283 2019-07-24 stsp }
5670 0ebf8283 2019-07-24 stsp
5671 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5672 0ebf8283 2019-07-24 stsp if (err)
5673 0ebf8283 2019-07-24 stsp goto done;
5674 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5675 5943eee2 2019-08-13 stsp if (err)
5676 5943eee2 2019-08-13 stsp goto done;
5677 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5678 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5679 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5680 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5681 0ebf8283 2019-07-24 stsp goto done;
5682 0ebf8283 2019-07-24 stsp }
5683 0ebf8283 2019-07-24 stsp free(logmsg);
5684 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5685 0ebf8283 2019-07-24 stsp
5686 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5687 0ebf8283 2019-07-24 stsp if (err)
5688 0ebf8283 2019-07-24 stsp goto done;
5689 0ebf8283 2019-07-24 stsp
5690 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5691 0ebf8283 2019-07-24 stsp if (err)
5692 0ebf8283 2019-07-24 stsp goto done;
5693 0ebf8283 2019-07-24 stsp
5694 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5695 0ebf8283 2019-07-24 stsp close(fd);
5696 0ebf8283 2019-07-24 stsp
5697 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5698 0ebf8283 2019-07-24 stsp if (err)
5699 0ebf8283 2019-07-24 stsp goto done;
5700 0ebf8283 2019-07-24 stsp
5701 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5702 0ebf8283 2019-07-24 stsp if (err) {
5703 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5704 0ebf8283 2019-07-24 stsp goto done;
5705 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5706 0ebf8283 2019-07-24 stsp }
5707 0ebf8283 2019-07-24 stsp done:
5708 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5709 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5710 0ebf8283 2019-07-24 stsp free(logmsg_path);
5711 0ebf8283 2019-07-24 stsp free(logmsg);
5712 5943eee2 2019-08-13 stsp free(orig_logmsg);
5713 0ebf8283 2019-07-24 stsp free(editor);
5714 0ebf8283 2019-07-24 stsp if (commit)
5715 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5716 0ebf8283 2019-07-24 stsp return err;
5717 5ef14e63 2019-06-02 stsp }
5718 0ebf8283 2019-07-24 stsp
5719 0ebf8283 2019-07-24 stsp static const struct got_error *
5720 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5721 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5722 0ebf8283 2019-07-24 stsp {
5723 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5724 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5725 0ebf8283 2019-07-24 stsp size_t size;
5726 0ebf8283 2019-07-24 stsp ssize_t len;
5727 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5728 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5729 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5730 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5731 0ebf8283 2019-07-24 stsp
5732 0ebf8283 2019-07-24 stsp for (;;) {
5733 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5734 0ebf8283 2019-07-24 stsp if (len == -1) {
5735 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5736 0ebf8283 2019-07-24 stsp if (feof(f))
5737 0ebf8283 2019-07-24 stsp break;
5738 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5739 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5740 0ebf8283 2019-07-24 stsp break;
5741 0ebf8283 2019-07-24 stsp }
5742 0ebf8283 2019-07-24 stsp lineno++;
5743 0ebf8283 2019-07-24 stsp p = line;
5744 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5745 0ebf8283 2019-07-24 stsp p++;
5746 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5747 0ebf8283 2019-07-24 stsp free(line);
5748 0ebf8283 2019-07-24 stsp line = NULL;
5749 0ebf8283 2019-07-24 stsp continue;
5750 0ebf8283 2019-07-24 stsp }
5751 0ebf8283 2019-07-24 stsp cmd = NULL;
5752 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5753 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5754 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5755 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5756 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5757 0ebf8283 2019-07-24 stsp break;
5758 0ebf8283 2019-07-24 stsp }
5759 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5760 0ebf8283 2019-07-24 stsp p++;
5761 0ebf8283 2019-07-24 stsp break;
5762 0ebf8283 2019-07-24 stsp }
5763 0ebf8283 2019-07-24 stsp }
5764 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5765 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5766 0ebf8283 2019-07-24 stsp break;
5767 0ebf8283 2019-07-24 stsp }
5768 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5769 0ebf8283 2019-07-24 stsp p++;
5770 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5771 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5772 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5773 0ebf8283 2019-07-24 stsp break;
5774 0ebf8283 2019-07-24 stsp }
5775 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5776 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5777 0ebf8283 2019-07-24 stsp if (err)
5778 0ebf8283 2019-07-24 stsp break;
5779 0ebf8283 2019-07-24 stsp } else {
5780 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5781 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5782 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5783 0ebf8283 2019-07-24 stsp break;
5784 0ebf8283 2019-07-24 stsp }
5785 0ebf8283 2019-07-24 stsp }
5786 0ebf8283 2019-07-24 stsp free(line);
5787 0ebf8283 2019-07-24 stsp line = NULL;
5788 0ebf8283 2019-07-24 stsp continue;
5789 0ebf8283 2019-07-24 stsp } else {
5790 0ebf8283 2019-07-24 stsp end = p;
5791 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5792 0ebf8283 2019-07-24 stsp end++;
5793 0ebf8283 2019-07-24 stsp *end = '\0';
5794 0ebf8283 2019-07-24 stsp
5795 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5796 0ebf8283 2019-07-24 stsp if (err) {
5797 0ebf8283 2019-07-24 stsp /* override error code */
5798 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5799 0ebf8283 2019-07-24 stsp break;
5800 0ebf8283 2019-07-24 stsp }
5801 0ebf8283 2019-07-24 stsp }
5802 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5803 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5804 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5805 0ebf8283 2019-07-24 stsp break;
5806 0ebf8283 2019-07-24 stsp }
5807 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5808 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5809 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5810 0ebf8283 2019-07-24 stsp commit_id = NULL;
5811 0ebf8283 2019-07-24 stsp free(line);
5812 0ebf8283 2019-07-24 stsp line = NULL;
5813 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5814 0ebf8283 2019-07-24 stsp }
5815 0ebf8283 2019-07-24 stsp
5816 0ebf8283 2019-07-24 stsp free(line);
5817 0ebf8283 2019-07-24 stsp free(commit_id);
5818 0ebf8283 2019-07-24 stsp return err;
5819 0ebf8283 2019-07-24 stsp }
5820 0ebf8283 2019-07-24 stsp
5821 0ebf8283 2019-07-24 stsp static const struct got_error *
5822 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5823 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5824 bfce7f83 2019-07-27 stsp {
5825 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5826 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5827 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5828 bfce7f83 2019-07-27 stsp static char msg[80];
5829 bfce7f83 2019-07-27 stsp char *id_str;
5830 bfce7f83 2019-07-27 stsp
5831 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5832 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5833 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5834 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5835 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5836 bfce7f83 2019-07-27 stsp
5837 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5838 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5839 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5840 bfce7f83 2019-07-27 stsp break;
5841 bfce7f83 2019-07-27 stsp }
5842 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5843 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5844 bfce7f83 2019-07-27 stsp if (err)
5845 bfce7f83 2019-07-27 stsp return err;
5846 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5847 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5848 bfce7f83 2019-07-27 stsp free(id_str);
5849 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5850 bfce7f83 2019-07-27 stsp }
5851 bfce7f83 2019-07-27 stsp }
5852 bfce7f83 2019-07-27 stsp
5853 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5854 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5855 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5856 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5857 bfce7f83 2019-07-27 stsp
5858 bfce7f83 2019-07-27 stsp return NULL;
5859 bfce7f83 2019-07-27 stsp }
5860 bfce7f83 2019-07-27 stsp
5861 bfce7f83 2019-07-27 stsp static const struct got_error *
5862 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5863 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5864 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5865 0ebf8283 2019-07-24 stsp {
5866 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5867 0ebf8283 2019-07-24 stsp char *editor;
5868 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5869 0ebf8283 2019-07-24 stsp
5870 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5871 0ebf8283 2019-07-24 stsp if (err)
5872 0ebf8283 2019-07-24 stsp return err;
5873 0ebf8283 2019-07-24 stsp
5874 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5875 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5876 0ebf8283 2019-07-24 stsp goto done;
5877 0ebf8283 2019-07-24 stsp }
5878 0ebf8283 2019-07-24 stsp
5879 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5880 0ebf8283 2019-07-24 stsp if (f == NULL) {
5881 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5882 0ebf8283 2019-07-24 stsp goto done;
5883 0ebf8283 2019-07-24 stsp }
5884 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5885 bfce7f83 2019-07-27 stsp if (err)
5886 bfce7f83 2019-07-27 stsp goto done;
5887 bfce7f83 2019-07-27 stsp
5888 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5889 0ebf8283 2019-07-24 stsp done:
5890 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5891 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5892 0ebf8283 2019-07-24 stsp free(editor);
5893 0ebf8283 2019-07-24 stsp return err;
5894 0ebf8283 2019-07-24 stsp }
5895 0ebf8283 2019-07-24 stsp
5896 0ebf8283 2019-07-24 stsp static const struct got_error *
5897 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5898 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5899 0ebf8283 2019-07-24 stsp
5900 0ebf8283 2019-07-24 stsp static const struct got_error *
5901 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5902 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5903 0ebf8283 2019-07-24 stsp {
5904 0ebf8283 2019-07-24 stsp const struct got_error *err;
5905 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5906 0ebf8283 2019-07-24 stsp char *path = NULL;
5907 0ebf8283 2019-07-24 stsp
5908 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5909 0ebf8283 2019-07-24 stsp if (err)
5910 0ebf8283 2019-07-24 stsp return err;
5911 0ebf8283 2019-07-24 stsp
5912 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5913 0ebf8283 2019-07-24 stsp if (err)
5914 0ebf8283 2019-07-24 stsp goto done;
5915 0ebf8283 2019-07-24 stsp
5916 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5917 0ebf8283 2019-07-24 stsp if (err)
5918 0ebf8283 2019-07-24 stsp goto done;
5919 0ebf8283 2019-07-24 stsp
5920 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5921 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5922 0ebf8283 2019-07-24 stsp goto done;
5923 0ebf8283 2019-07-24 stsp }
5924 0ebf8283 2019-07-24 stsp f = NULL;
5925 0ebf8283 2019-07-24 stsp
5926 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5927 0ebf8283 2019-07-24 stsp if (err) {
5928 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5929 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5930 0ebf8283 2019-07-24 stsp goto done;
5931 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5932 0ebf8283 2019-07-24 stsp commits, path, repo);
5933 0ebf8283 2019-07-24 stsp }
5934 0ebf8283 2019-07-24 stsp done:
5935 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5936 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5937 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5938 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5939 0ebf8283 2019-07-24 stsp free(path);
5940 0ebf8283 2019-07-24 stsp return err;
5941 0ebf8283 2019-07-24 stsp }
5942 0ebf8283 2019-07-24 stsp
5943 0ebf8283 2019-07-24 stsp static const struct got_error *
5944 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5945 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5946 0ebf8283 2019-07-24 stsp {
5947 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5948 0ebf8283 2019-07-24 stsp char *path = NULL;
5949 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5950 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5951 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5952 0ebf8283 2019-07-24 stsp
5953 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5954 0ebf8283 2019-07-24 stsp if (err)
5955 0ebf8283 2019-07-24 stsp return err;
5956 0ebf8283 2019-07-24 stsp
5957 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5958 0ebf8283 2019-07-24 stsp if (f == NULL) {
5959 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5960 0ebf8283 2019-07-24 stsp goto done;
5961 0ebf8283 2019-07-24 stsp }
5962 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5963 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5964 0ebf8283 2019-07-24 stsp repo);
5965 0ebf8283 2019-07-24 stsp if (err)
5966 0ebf8283 2019-07-24 stsp break;
5967 0ebf8283 2019-07-24 stsp
5968 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5969 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5970 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5971 0ebf8283 2019-07-24 stsp if (n < 0) {
5972 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5973 0ebf8283 2019-07-24 stsp break;
5974 0ebf8283 2019-07-24 stsp }
5975 0ebf8283 2019-07-24 stsp }
5976 0ebf8283 2019-07-24 stsp }
5977 0ebf8283 2019-07-24 stsp done:
5978 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5979 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5980 0ebf8283 2019-07-24 stsp free(path);
5981 0ebf8283 2019-07-24 stsp if (commit)
5982 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5983 0ebf8283 2019-07-24 stsp return err;
5984 0ebf8283 2019-07-24 stsp }
5985 0ebf8283 2019-07-24 stsp
5986 bfce7f83 2019-07-27 stsp void
5987 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5988 bfce7f83 2019-07-27 stsp {
5989 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5990 bfce7f83 2019-07-27 stsp
5991 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5992 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5993 bfce7f83 2019-07-27 stsp free(hle);
5994 bfce7f83 2019-07-27 stsp }
5995 bfce7f83 2019-07-27 stsp }
5996 bfce7f83 2019-07-27 stsp
5997 0ebf8283 2019-07-24 stsp static const struct got_error *
5998 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5999 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6000 0ebf8283 2019-07-24 stsp {
6001 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6002 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6003 0ebf8283 2019-07-24 stsp
6004 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6005 0ebf8283 2019-07-24 stsp if (f == NULL) {
6006 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6007 0ebf8283 2019-07-24 stsp goto done;
6008 0ebf8283 2019-07-24 stsp }
6009 0ebf8283 2019-07-24 stsp
6010 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6011 0ebf8283 2019-07-24 stsp done:
6012 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6013 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6014 0ebf8283 2019-07-24 stsp return err;
6015 0ebf8283 2019-07-24 stsp }
6016 0ebf8283 2019-07-24 stsp
6017 0ebf8283 2019-07-24 stsp static const struct got_error *
6018 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6019 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6020 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6021 0ebf8283 2019-07-24 stsp {
6022 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6023 0ebf8283 2019-07-24 stsp int resp = ' ';
6024 0ebf8283 2019-07-24 stsp
6025 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6026 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6027 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6028 0ebf8283 2019-07-24 stsp resp = getchar();
6029 a61a4414 2019-08-07 stsp if (resp == '\n')
6030 a61a4414 2019-08-07 stsp resp = getchar();
6031 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6032 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6033 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6034 bfce7f83 2019-07-27 stsp repo);
6035 426ebf2e 2019-07-25 stsp if (err) {
6036 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6037 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6038 426ebf2e 2019-07-25 stsp break;
6039 bfce7f83 2019-07-27 stsp prev_err = err;
6040 426ebf2e 2019-07-25 stsp resp = ' ';
6041 426ebf2e 2019-07-25 stsp continue;
6042 426ebf2e 2019-07-25 stsp }
6043 bfce7f83 2019-07-27 stsp break;
6044 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6045 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6046 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6047 0ebf8283 2019-07-24 stsp commits, repo);
6048 426ebf2e 2019-07-25 stsp if (err) {
6049 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6050 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6051 426ebf2e 2019-07-25 stsp break;
6052 bfce7f83 2019-07-27 stsp prev_err = err;
6053 426ebf2e 2019-07-25 stsp resp = ' ';
6054 426ebf2e 2019-07-25 stsp continue;
6055 426ebf2e 2019-07-25 stsp }
6056 bfce7f83 2019-07-27 stsp break;
6057 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6058 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6059 0ebf8283 2019-07-24 stsp break;
6060 426ebf2e 2019-07-25 stsp } else
6061 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6062 0ebf8283 2019-07-24 stsp }
6063 0ebf8283 2019-07-24 stsp
6064 0ebf8283 2019-07-24 stsp return err;
6065 0ebf8283 2019-07-24 stsp }
6066 0ebf8283 2019-07-24 stsp
6067 0ebf8283 2019-07-24 stsp static const struct got_error *
6068 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6069 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6070 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6071 0ebf8283 2019-07-24 stsp {
6072 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6073 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6074 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6075 3e3a69f1 2019-07-25 stsp branch, repo);
6076 0ebf8283 2019-07-24 stsp }
6077 0ebf8283 2019-07-24 stsp
6078 0ebf8283 2019-07-24 stsp static const struct got_error *
6079 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6080 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6081 0ebf8283 2019-07-24 stsp {
6082 0ebf8283 2019-07-24 stsp const struct got_error *err;
6083 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6084 0ebf8283 2019-07-24 stsp
6085 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6086 0ebf8283 2019-07-24 stsp if (err)
6087 0ebf8283 2019-07-24 stsp goto done;
6088 0ebf8283 2019-07-24 stsp
6089 0ebf8283 2019-07-24 stsp if (new_id) {
6090 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6091 0ebf8283 2019-07-24 stsp if (err)
6092 0ebf8283 2019-07-24 stsp goto done;
6093 0ebf8283 2019-07-24 stsp }
6094 0ebf8283 2019-07-24 stsp
6095 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6096 0ebf8283 2019-07-24 stsp if (new_id_str)
6097 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6098 0ebf8283 2019-07-24 stsp
6099 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6100 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6101 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6102 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6103 0ebf8283 2019-07-24 stsp goto done;
6104 0ebf8283 2019-07-24 stsp }
6105 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6106 0ebf8283 2019-07-24 stsp } else {
6107 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6108 0ebf8283 2019-07-24 stsp if (err)
6109 0ebf8283 2019-07-24 stsp goto done;
6110 0ebf8283 2019-07-24 stsp }
6111 0ebf8283 2019-07-24 stsp
6112 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6113 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6114 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6115 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6116 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6117 0ebf8283 2019-07-24 stsp break;
6118 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6119 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6120 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6121 0ebf8283 2019-07-24 stsp logmsg);
6122 0ebf8283 2019-07-24 stsp break;
6123 0ebf8283 2019-07-24 stsp default:
6124 0ebf8283 2019-07-24 stsp break;
6125 0ebf8283 2019-07-24 stsp }
6126 0ebf8283 2019-07-24 stsp
6127 0ebf8283 2019-07-24 stsp done:
6128 0ebf8283 2019-07-24 stsp free(old_id_str);
6129 0ebf8283 2019-07-24 stsp free(new_id_str);
6130 0ebf8283 2019-07-24 stsp return err;
6131 0ebf8283 2019-07-24 stsp }
6132 0ebf8283 2019-07-24 stsp
6133 0ebf8283 2019-07-24 stsp static const struct got_error *
6134 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6135 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6136 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6137 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6138 0ebf8283 2019-07-24 stsp {
6139 0ebf8283 2019-07-24 stsp const struct got_error *err;
6140 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6141 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6142 0ebf8283 2019-07-24 stsp
6143 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6144 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6145 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6146 0ebf8283 2019-07-24 stsp if (err)
6147 0ebf8283 2019-07-24 stsp return err;
6148 0ebf8283 2019-07-24 stsp }
6149 0ebf8283 2019-07-24 stsp
6150 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6151 0ebf8283 2019-07-24 stsp if (err)
6152 0ebf8283 2019-07-24 stsp return err;
6153 0ebf8283 2019-07-24 stsp
6154 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6155 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6156 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6157 0ebf8283 2019-07-24 stsp if (err) {
6158 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6159 0ebf8283 2019-07-24 stsp goto done;
6160 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6161 0ebf8283 2019-07-24 stsp } else {
6162 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6163 0ebf8283 2019-07-24 stsp free(new_commit_id);
6164 0ebf8283 2019-07-24 stsp }
6165 0ebf8283 2019-07-24 stsp done:
6166 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6167 0ebf8283 2019-07-24 stsp return err;
6168 0ebf8283 2019-07-24 stsp }
6169 0ebf8283 2019-07-24 stsp
6170 0ebf8283 2019-07-24 stsp static const struct got_error *
6171 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6172 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6173 0ebf8283 2019-07-24 stsp {
6174 0ebf8283 2019-07-24 stsp const struct got_error *error;
6175 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6176 0ebf8283 2019-07-24 stsp
6177 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6178 0ebf8283 2019-07-24 stsp repo);
6179 0ebf8283 2019-07-24 stsp if (error)
6180 0ebf8283 2019-07-24 stsp return error;
6181 0ebf8283 2019-07-24 stsp
6182 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6183 0ebf8283 2019-07-24 stsp if (error)
6184 0ebf8283 2019-07-24 stsp return error;
6185 0ebf8283 2019-07-24 stsp
6186 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6187 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6188 0ebf8283 2019-07-24 stsp return error;
6189 0ebf8283 2019-07-24 stsp }
6190 0ebf8283 2019-07-24 stsp
6191 0ebf8283 2019-07-24 stsp static const struct got_error *
6192 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6193 0ebf8283 2019-07-24 stsp {
6194 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6195 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6196 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6197 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6198 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6199 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6200 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6201 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6202 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6203 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6204 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6205 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6206 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6207 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6208 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6209 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6210 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6211 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6212 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6213 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6214 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6215 0ebf8283 2019-07-24 stsp
6216 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6217 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6218 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6219 0ebf8283 2019-07-24 stsp
6220 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6221 0ebf8283 2019-07-24 stsp switch (ch) {
6222 0ebf8283 2019-07-24 stsp case 'a':
6223 0ebf8283 2019-07-24 stsp abort_edit = 1;
6224 0ebf8283 2019-07-24 stsp break;
6225 0ebf8283 2019-07-24 stsp case 'c':
6226 0ebf8283 2019-07-24 stsp continue_edit = 1;
6227 0ebf8283 2019-07-24 stsp break;
6228 0ebf8283 2019-07-24 stsp case 'F':
6229 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6230 0ebf8283 2019-07-24 stsp break;
6231 0ebf8283 2019-07-24 stsp default:
6232 0ebf8283 2019-07-24 stsp usage_histedit();
6233 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6234 0ebf8283 2019-07-24 stsp }
6235 0ebf8283 2019-07-24 stsp }
6236 0ebf8283 2019-07-24 stsp
6237 0ebf8283 2019-07-24 stsp argc -= optind;
6238 0ebf8283 2019-07-24 stsp argv += optind;
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6241 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6242 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6243 0ebf8283 2019-07-24 stsp err(1, "pledge");
6244 0ebf8283 2019-07-24 stsp #endif
6245 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6246 0ebf8283 2019-07-24 stsp usage_histedit();
6247 0ebf8283 2019-07-24 stsp if (argc != 0)
6248 0ebf8283 2019-07-24 stsp usage_histedit();
6249 0ebf8283 2019-07-24 stsp
6250 0ebf8283 2019-07-24 stsp /*
6251 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6252 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6253 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6254 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6255 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6256 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6257 0ebf8283 2019-07-24 stsp */
6258 0ebf8283 2019-07-24 stsp
6259 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6260 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6261 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6262 0ebf8283 2019-07-24 stsp goto done;
6263 0ebf8283 2019-07-24 stsp }
6264 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6265 0ebf8283 2019-07-24 stsp if (error)
6266 0ebf8283 2019-07-24 stsp goto done;
6267 0ebf8283 2019-07-24 stsp
6268 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6269 c9956ddf 2019-09-08 stsp NULL);
6270 0ebf8283 2019-07-24 stsp if (error != NULL)
6271 0ebf8283 2019-07-24 stsp goto done;
6272 0ebf8283 2019-07-24 stsp
6273 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6274 0ebf8283 2019-07-24 stsp if (error)
6275 0ebf8283 2019-07-24 stsp goto done;
6276 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6277 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6278 0ebf8283 2019-07-24 stsp goto done;
6279 0ebf8283 2019-07-24 stsp }
6280 0ebf8283 2019-07-24 stsp
6281 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6282 0ebf8283 2019-07-24 stsp if (error)
6283 0ebf8283 2019-07-24 stsp goto done;
6284 0ebf8283 2019-07-24 stsp
6285 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6286 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6287 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6288 3e3a69f1 2019-07-25 stsp worktree, repo);
6289 0ebf8283 2019-07-24 stsp if (error)
6290 0ebf8283 2019-07-24 stsp goto done;
6291 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6292 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6293 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6294 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6295 0ebf8283 2019-07-24 stsp if (error)
6296 0ebf8283 2019-07-24 stsp goto done;
6297 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6298 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6299 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6300 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6301 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6302 0ebf8283 2019-07-24 stsp goto done;
6303 0ebf8283 2019-07-24 stsp }
6304 0ebf8283 2019-07-24 stsp
6305 0ebf8283 2019-07-24 stsp if (continue_edit) {
6306 0ebf8283 2019-07-24 stsp char *path;
6307 0ebf8283 2019-07-24 stsp
6308 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6309 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6310 0ebf8283 2019-07-24 stsp goto done;
6311 0ebf8283 2019-07-24 stsp }
6312 0ebf8283 2019-07-24 stsp
6313 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6314 0ebf8283 2019-07-24 stsp if (error)
6315 0ebf8283 2019-07-24 stsp goto done;
6316 0ebf8283 2019-07-24 stsp
6317 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6318 0ebf8283 2019-07-24 stsp free(path);
6319 0ebf8283 2019-07-24 stsp if (error)
6320 0ebf8283 2019-07-24 stsp goto done;
6321 0ebf8283 2019-07-24 stsp
6322 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6323 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6324 3e3a69f1 2019-07-25 stsp worktree, repo);
6325 0ebf8283 2019-07-24 stsp if (error)
6326 0ebf8283 2019-07-24 stsp goto done;
6327 0ebf8283 2019-07-24 stsp
6328 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6329 0ebf8283 2019-07-24 stsp if (error)
6330 0ebf8283 2019-07-24 stsp goto done;
6331 0ebf8283 2019-07-24 stsp
6332 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6333 0ebf8283 2019-07-24 stsp head_commit_id);
6334 0ebf8283 2019-07-24 stsp if (error)
6335 0ebf8283 2019-07-24 stsp goto done;
6336 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6337 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6338 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6339 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6340 3aac7cf7 2019-07-25 stsp goto done;
6341 3aac7cf7 2019-07-25 stsp }
6342 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6343 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6344 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6345 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6346 0ebf8283 2019-07-24 stsp commit = NULL;
6347 0ebf8283 2019-07-24 stsp if (error)
6348 0ebf8283 2019-07-24 stsp goto done;
6349 0ebf8283 2019-07-24 stsp } else {
6350 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6351 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6352 0ebf8283 2019-07-24 stsp goto done;
6353 0ebf8283 2019-07-24 stsp }
6354 0ebf8283 2019-07-24 stsp
6355 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6356 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6357 0ebf8283 2019-07-24 stsp if (error != NULL)
6358 0ebf8283 2019-07-24 stsp goto done;
6359 0ebf8283 2019-07-24 stsp
6360 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6361 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6362 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6363 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6364 c7d20a3f 2019-07-30 stsp goto done;
6365 c7d20a3f 2019-07-30 stsp }
6366 c7d20a3f 2019-07-30 stsp
6367 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6368 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6369 bfce7f83 2019-07-27 stsp branch = NULL;
6370 0ebf8283 2019-07-24 stsp if (error)
6371 0ebf8283 2019-07-24 stsp goto done;
6372 0ebf8283 2019-07-24 stsp
6373 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6374 0ebf8283 2019-07-24 stsp head_commit_id);
6375 0ebf8283 2019-07-24 stsp if (error)
6376 0ebf8283 2019-07-24 stsp goto done;
6377 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6378 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6379 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6380 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6381 3aac7cf7 2019-07-25 stsp goto done;
6382 3aac7cf7 2019-07-25 stsp }
6383 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6384 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6385 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6386 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6387 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6388 0ebf8283 2019-07-24 stsp commit = NULL;
6389 0ebf8283 2019-07-24 stsp if (error)
6390 0ebf8283 2019-07-24 stsp goto done;
6391 0ebf8283 2019-07-24 stsp
6392 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6393 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6394 bfce7f83 2019-07-27 stsp if (error)
6395 bfce7f83 2019-07-27 stsp goto done;
6396 bfce7f83 2019-07-27 stsp
6397 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6398 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6399 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6400 6ba2bea2 2019-07-27 stsp if (error) {
6401 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6402 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6403 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6404 0ebf8283 2019-07-24 stsp goto done;
6405 6ba2bea2 2019-07-27 stsp }
6406 0ebf8283 2019-07-24 stsp } else {
6407 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6408 0ebf8283 2019-07-24 stsp repo);
6409 6ba2bea2 2019-07-27 stsp if (error) {
6410 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6411 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6412 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6413 0ebf8283 2019-07-24 stsp goto done;
6414 6ba2bea2 2019-07-27 stsp }
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp }
6417 0ebf8283 2019-07-24 stsp
6418 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6419 0ebf8283 2019-07-24 stsp repo);
6420 6ba2bea2 2019-07-27 stsp if (error) {
6421 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6422 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6423 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6424 0ebf8283 2019-07-24 stsp goto done;
6425 6ba2bea2 2019-07-27 stsp }
6426 0ebf8283 2019-07-24 stsp
6427 0ebf8283 2019-07-24 stsp }
6428 0ebf8283 2019-07-24 stsp
6429 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6430 4ec14e60 2019-08-28 hiltjo if (error)
6431 0ebf8283 2019-07-24 stsp goto done;
6432 0ebf8283 2019-07-24 stsp
6433 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6434 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6435 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6436 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6437 0ebf8283 2019-07-24 stsp continue;
6438 0ebf8283 2019-07-24 stsp
6439 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6440 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6441 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6442 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6443 0ebf8283 2019-07-24 stsp repo);
6444 0ebf8283 2019-07-24 stsp } else {
6445 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6446 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6447 0ebf8283 2019-07-24 stsp }
6448 0ebf8283 2019-07-24 stsp if (error)
6449 0ebf8283 2019-07-24 stsp goto done;
6450 0ebf8283 2019-07-24 stsp continue;
6451 0ebf8283 2019-07-24 stsp }
6452 0ebf8283 2019-07-24 stsp
6453 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6454 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6455 0ebf8283 2019-07-24 stsp if (error)
6456 0ebf8283 2019-07-24 stsp goto done;
6457 0ebf8283 2019-07-24 stsp continue;
6458 0ebf8283 2019-07-24 stsp }
6459 0ebf8283 2019-07-24 stsp
6460 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6461 0ebf8283 2019-07-24 stsp hle->commit_id);
6462 0ebf8283 2019-07-24 stsp if (error)
6463 0ebf8283 2019-07-24 stsp goto done;
6464 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6465 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6466 0ebf8283 2019-07-24 stsp
6467 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6468 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6469 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6470 0ebf8283 2019-07-24 stsp if (error)
6471 0ebf8283 2019-07-24 stsp goto done;
6472 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6473 0ebf8283 2019-07-24 stsp commit = NULL;
6474 0ebf8283 2019-07-24 stsp
6475 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6476 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6477 0ebf8283 2019-07-24 stsp break;
6478 0ebf8283 2019-07-24 stsp }
6479 0ebf8283 2019-07-24 stsp
6480 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6481 0ebf8283 2019-07-24 stsp char *id_str;
6482 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6483 0ebf8283 2019-07-24 stsp if (error)
6484 0ebf8283 2019-07-24 stsp goto done;
6485 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6486 0ebf8283 2019-07-24 stsp id_str);
6487 0ebf8283 2019-07-24 stsp free(id_str);
6488 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6489 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6490 3e3a69f1 2019-07-25 stsp fileindex);
6491 0ebf8283 2019-07-24 stsp goto done;
6492 0ebf8283 2019-07-24 stsp }
6493 0ebf8283 2019-07-24 stsp
6494 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6495 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6496 0ebf8283 2019-07-24 stsp if (error)
6497 0ebf8283 2019-07-24 stsp goto done;
6498 0ebf8283 2019-07-24 stsp continue;
6499 0ebf8283 2019-07-24 stsp }
6500 0ebf8283 2019-07-24 stsp
6501 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6502 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6503 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6504 0ebf8283 2019-07-24 stsp if (error)
6505 0ebf8283 2019-07-24 stsp goto done;
6506 0ebf8283 2019-07-24 stsp }
6507 0ebf8283 2019-07-24 stsp
6508 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6509 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6510 0ebf8283 2019-07-24 stsp if (error)
6511 0ebf8283 2019-07-24 stsp goto done;
6512 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6513 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6514 0ebf8283 2019-07-24 stsp } else
6515 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6516 3e3a69f1 2019-07-25 stsp branch, repo);
6517 0ebf8283 2019-07-24 stsp done:
6518 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6519 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6520 0ebf8283 2019-07-24 stsp free(head_commit_id);
6521 0ebf8283 2019-07-24 stsp free(base_commit_id);
6522 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6523 0ebf8283 2019-07-24 stsp if (commit)
6524 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6525 0ebf8283 2019-07-24 stsp if (branch)
6526 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6527 0ebf8283 2019-07-24 stsp if (tmp_branch)
6528 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6529 0ebf8283 2019-07-24 stsp if (worktree)
6530 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6531 2822a352 2019-10-15 stsp if (repo)
6532 2822a352 2019-10-15 stsp got_repo_close(repo);
6533 2822a352 2019-10-15 stsp return error;
6534 2822a352 2019-10-15 stsp }
6535 2822a352 2019-10-15 stsp
6536 2822a352 2019-10-15 stsp __dead static void
6537 2822a352 2019-10-15 stsp usage_integrate(void)
6538 2822a352 2019-10-15 stsp {
6539 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6540 2822a352 2019-10-15 stsp exit(1);
6541 2822a352 2019-10-15 stsp }
6542 2822a352 2019-10-15 stsp
6543 2822a352 2019-10-15 stsp static const struct got_error *
6544 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6545 2822a352 2019-10-15 stsp {
6546 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6547 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6548 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6549 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6550 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6551 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6552 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6553 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6554 2822a352 2019-10-15 stsp int ch, did_something = 0;
6555 2822a352 2019-10-15 stsp
6556 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6557 2822a352 2019-10-15 stsp switch (ch) {
6558 2822a352 2019-10-15 stsp default:
6559 2822a352 2019-10-15 stsp usage_integrate();
6560 2822a352 2019-10-15 stsp /* NOTREACHED */
6561 2822a352 2019-10-15 stsp }
6562 2822a352 2019-10-15 stsp }
6563 2822a352 2019-10-15 stsp
6564 2822a352 2019-10-15 stsp argc -= optind;
6565 2822a352 2019-10-15 stsp argv += optind;
6566 2822a352 2019-10-15 stsp
6567 2822a352 2019-10-15 stsp if (argc != 1)
6568 2822a352 2019-10-15 stsp usage_integrate();
6569 2822a352 2019-10-15 stsp branch_arg = argv[0];
6570 2822a352 2019-10-15 stsp
6571 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6572 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6573 2822a352 2019-10-15 stsp err(1, "pledge");
6574 2822a352 2019-10-15 stsp
6575 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6576 2822a352 2019-10-15 stsp if (cwd == NULL) {
6577 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6578 2822a352 2019-10-15 stsp goto done;
6579 2822a352 2019-10-15 stsp }
6580 2822a352 2019-10-15 stsp
6581 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6582 2822a352 2019-10-15 stsp if (error)
6583 2822a352 2019-10-15 stsp goto done;
6584 2822a352 2019-10-15 stsp
6585 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6586 2822a352 2019-10-15 stsp if (error)
6587 2822a352 2019-10-15 stsp goto done;
6588 2822a352 2019-10-15 stsp
6589 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6590 2822a352 2019-10-15 stsp NULL);
6591 2822a352 2019-10-15 stsp if (error != NULL)
6592 2822a352 2019-10-15 stsp goto done;
6593 2822a352 2019-10-15 stsp
6594 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6595 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6596 2822a352 2019-10-15 stsp if (error)
6597 2822a352 2019-10-15 stsp goto done;
6598 2822a352 2019-10-15 stsp
6599 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6600 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6601 2822a352 2019-10-15 stsp goto done;
6602 2822a352 2019-10-15 stsp }
6603 2822a352 2019-10-15 stsp
6604 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6605 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6606 2822a352 2019-10-15 stsp if (error)
6607 2822a352 2019-10-15 stsp goto done;
6608 2822a352 2019-10-15 stsp
6609 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6610 2822a352 2019-10-15 stsp if (refname == NULL) {
6611 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6612 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6613 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6614 2822a352 2019-10-15 stsp goto done;
6615 2822a352 2019-10-15 stsp }
6616 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6617 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6618 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6619 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6620 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6621 2822a352 2019-10-15 stsp goto done;
6622 2822a352 2019-10-15 stsp }
6623 2822a352 2019-10-15 stsp
6624 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6625 2822a352 2019-10-15 stsp if (error)
6626 2822a352 2019-10-15 stsp goto done;
6627 2822a352 2019-10-15 stsp
6628 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6629 2822a352 2019-10-15 stsp if (error)
6630 2822a352 2019-10-15 stsp goto done;
6631 2822a352 2019-10-15 stsp
6632 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6633 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6634 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6635 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6636 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6637 2822a352 2019-10-15 stsp goto done;
6638 2822a352 2019-10-15 stsp }
6639 2822a352 2019-10-15 stsp
6640 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6641 2822a352 2019-10-15 stsp if (error) {
6642 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6643 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6644 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6645 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6646 2822a352 2019-10-15 stsp goto done;
6647 2822a352 2019-10-15 stsp }
6648 2822a352 2019-10-15 stsp
6649 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6650 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6651 2822a352 2019-10-15 stsp check_cancelled, NULL);
6652 2822a352 2019-10-15 stsp if (error)
6653 2822a352 2019-10-15 stsp goto done;
6654 2822a352 2019-10-15 stsp
6655 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6656 2822a352 2019-10-15 stsp done:
6657 715dc77e 2019-08-03 stsp if (repo)
6658 715dc77e 2019-08-03 stsp got_repo_close(repo);
6659 2822a352 2019-10-15 stsp if (worktree)
6660 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6661 2822a352 2019-10-15 stsp free(cwd);
6662 2822a352 2019-10-15 stsp free(base_commit_id);
6663 2822a352 2019-10-15 stsp free(commit_id);
6664 2822a352 2019-10-15 stsp free(refname);
6665 2822a352 2019-10-15 stsp free(base_refname);
6666 715dc77e 2019-08-03 stsp return error;
6667 715dc77e 2019-08-03 stsp }
6668 715dc77e 2019-08-03 stsp
6669 715dc77e 2019-08-03 stsp __dead static void
6670 715dc77e 2019-08-03 stsp usage_stage(void)
6671 715dc77e 2019-08-03 stsp {
6672 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6673 f3055044 2019-08-07 stsp "[file-path ...]\n",
6674 715dc77e 2019-08-03 stsp getprogname());
6675 715dc77e 2019-08-03 stsp exit(1);
6676 715dc77e 2019-08-03 stsp }
6677 715dc77e 2019-08-03 stsp
6678 715dc77e 2019-08-03 stsp static const struct got_error *
6679 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6680 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6681 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6682 408b4ebc 2019-08-03 stsp {
6683 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6684 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6685 408b4ebc 2019-08-03 stsp
6686 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6687 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6688 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6689 408b4ebc 2019-08-03 stsp return NULL;
6690 408b4ebc 2019-08-03 stsp
6691 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6692 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6693 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6694 408b4ebc 2019-08-03 stsp else
6695 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6696 408b4ebc 2019-08-03 stsp if (err)
6697 408b4ebc 2019-08-03 stsp return err;
6698 408b4ebc 2019-08-03 stsp
6699 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6700 408b4ebc 2019-08-03 stsp free(id_str);
6701 dc424a06 2019-08-07 stsp return NULL;
6702 dc424a06 2019-08-07 stsp }
6703 2e1f37b0 2019-08-08 stsp
6704 dc424a06 2019-08-07 stsp static const struct got_error *
6705 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6706 715dc77e 2019-08-03 stsp {
6707 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6708 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6709 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6710 715dc77e 2019-08-03 stsp char *cwd = NULL;
6711 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6712 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6713 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6714 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6715 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6716 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6717 715dc77e 2019-08-03 stsp
6718 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6719 715dc77e 2019-08-03 stsp
6720 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6721 715dc77e 2019-08-03 stsp switch (ch) {
6722 408b4ebc 2019-08-03 stsp case 'l':
6723 408b4ebc 2019-08-03 stsp list_stage = 1;
6724 408b4ebc 2019-08-03 stsp break;
6725 dc424a06 2019-08-07 stsp case 'p':
6726 dc424a06 2019-08-07 stsp pflag = 1;
6727 dc424a06 2019-08-07 stsp break;
6728 dc424a06 2019-08-07 stsp case 'F':
6729 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6730 dc424a06 2019-08-07 stsp break;
6731 715dc77e 2019-08-03 stsp default:
6732 715dc77e 2019-08-03 stsp usage_stage();
6733 715dc77e 2019-08-03 stsp /* NOTREACHED */
6734 715dc77e 2019-08-03 stsp }
6735 715dc77e 2019-08-03 stsp }
6736 715dc77e 2019-08-03 stsp
6737 715dc77e 2019-08-03 stsp argc -= optind;
6738 715dc77e 2019-08-03 stsp argv += optind;
6739 715dc77e 2019-08-03 stsp
6740 715dc77e 2019-08-03 stsp #ifndef PROFILE
6741 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6742 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6743 715dc77e 2019-08-03 stsp err(1, "pledge");
6744 715dc77e 2019-08-03 stsp #endif
6745 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6746 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6747 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6748 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6749 715dc77e 2019-08-03 stsp
6750 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6751 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6752 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6753 715dc77e 2019-08-03 stsp goto done;
6754 715dc77e 2019-08-03 stsp }
6755 715dc77e 2019-08-03 stsp
6756 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6757 715dc77e 2019-08-03 stsp if (error)
6758 715dc77e 2019-08-03 stsp goto done;
6759 715dc77e 2019-08-03 stsp
6760 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6761 c9956ddf 2019-09-08 stsp NULL);
6762 715dc77e 2019-08-03 stsp if (error != NULL)
6763 715dc77e 2019-08-03 stsp goto done;
6764 715dc77e 2019-08-03 stsp
6765 dc424a06 2019-08-07 stsp if (patch_script_path) {
6766 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6767 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6768 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6769 dc424a06 2019-08-07 stsp patch_script_path);
6770 dc424a06 2019-08-07 stsp goto done;
6771 dc424a06 2019-08-07 stsp }
6772 dc424a06 2019-08-07 stsp }
6773 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6774 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6775 715dc77e 2019-08-03 stsp if (error)
6776 715dc77e 2019-08-03 stsp goto done;
6777 715dc77e 2019-08-03 stsp
6778 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6779 6d022e97 2019-08-04 stsp if (error)
6780 6d022e97 2019-08-04 stsp goto done;
6781 715dc77e 2019-08-03 stsp
6782 6d022e97 2019-08-04 stsp if (list_stage)
6783 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6784 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6785 2e1f37b0 2019-08-08 stsp else {
6786 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6787 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6788 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6789 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6790 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6791 2e1f37b0 2019-08-08 stsp }
6792 ad493afc 2019-08-03 stsp done:
6793 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6794 2e1f37b0 2019-08-08 stsp error == NULL)
6795 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6796 ad493afc 2019-08-03 stsp if (repo)
6797 ad493afc 2019-08-03 stsp got_repo_close(repo);
6798 ad493afc 2019-08-03 stsp if (worktree)
6799 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6800 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6801 ad493afc 2019-08-03 stsp free((char *)pe->path);
6802 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6803 ad493afc 2019-08-03 stsp free(cwd);
6804 ad493afc 2019-08-03 stsp return error;
6805 ad493afc 2019-08-03 stsp }
6806 ad493afc 2019-08-03 stsp
6807 ad493afc 2019-08-03 stsp __dead static void
6808 ad493afc 2019-08-03 stsp usage_unstage(void)
6809 ad493afc 2019-08-03 stsp {
6810 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6811 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6812 ad493afc 2019-08-03 stsp getprogname());
6813 ad493afc 2019-08-03 stsp exit(1);
6814 ad493afc 2019-08-03 stsp }
6815 ad493afc 2019-08-03 stsp
6816 ad493afc 2019-08-03 stsp
6817 ad493afc 2019-08-03 stsp static const struct got_error *
6818 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6819 ad493afc 2019-08-03 stsp {
6820 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6821 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6822 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6823 ad493afc 2019-08-03 stsp char *cwd = NULL;
6824 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6825 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6826 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6827 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6828 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6829 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6830 ad493afc 2019-08-03 stsp
6831 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6832 ad493afc 2019-08-03 stsp
6833 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6834 ad493afc 2019-08-03 stsp switch (ch) {
6835 2e1f37b0 2019-08-08 stsp case 'p':
6836 2e1f37b0 2019-08-08 stsp pflag = 1;
6837 2e1f37b0 2019-08-08 stsp break;
6838 2e1f37b0 2019-08-08 stsp case 'F':
6839 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6840 2e1f37b0 2019-08-08 stsp break;
6841 ad493afc 2019-08-03 stsp default:
6842 ad493afc 2019-08-03 stsp usage_unstage();
6843 ad493afc 2019-08-03 stsp /* NOTREACHED */
6844 ad493afc 2019-08-03 stsp }
6845 ad493afc 2019-08-03 stsp }
6846 ad493afc 2019-08-03 stsp
6847 ad493afc 2019-08-03 stsp argc -= optind;
6848 ad493afc 2019-08-03 stsp argv += optind;
6849 ad493afc 2019-08-03 stsp
6850 ad493afc 2019-08-03 stsp #ifndef PROFILE
6851 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6852 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6853 ad493afc 2019-08-03 stsp err(1, "pledge");
6854 ad493afc 2019-08-03 stsp #endif
6855 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6856 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6857 2e1f37b0 2019-08-08 stsp
6858 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6859 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6860 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6861 ad493afc 2019-08-03 stsp goto done;
6862 ad493afc 2019-08-03 stsp }
6863 ad493afc 2019-08-03 stsp
6864 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6865 ad493afc 2019-08-03 stsp if (error)
6866 ad493afc 2019-08-03 stsp goto done;
6867 ad493afc 2019-08-03 stsp
6868 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6869 c9956ddf 2019-09-08 stsp NULL);
6870 ad493afc 2019-08-03 stsp if (error != NULL)
6871 ad493afc 2019-08-03 stsp goto done;
6872 ad493afc 2019-08-03 stsp
6873 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6874 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6875 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6876 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6877 2e1f37b0 2019-08-08 stsp patch_script_path);
6878 2e1f37b0 2019-08-08 stsp goto done;
6879 2e1f37b0 2019-08-08 stsp }
6880 2e1f37b0 2019-08-08 stsp }
6881 2e1f37b0 2019-08-08 stsp
6882 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6883 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6884 ad493afc 2019-08-03 stsp if (error)
6885 ad493afc 2019-08-03 stsp goto done;
6886 ad493afc 2019-08-03 stsp
6887 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6888 ad493afc 2019-08-03 stsp if (error)
6889 ad493afc 2019-08-03 stsp goto done;
6890 ad493afc 2019-08-03 stsp
6891 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6892 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6893 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6894 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6895 715dc77e 2019-08-03 stsp done:
6896 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6897 2e1f37b0 2019-08-08 stsp error == NULL)
6898 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6899 0ebf8283 2019-07-24 stsp if (repo)
6900 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6901 715dc77e 2019-08-03 stsp if (worktree)
6902 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6903 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6904 715dc77e 2019-08-03 stsp free((char *)pe->path);
6905 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6906 715dc77e 2019-08-03 stsp free(cwd);
6907 01073a5d 2019-08-22 stsp return error;
6908 01073a5d 2019-08-22 stsp }
6909 01073a5d 2019-08-22 stsp
6910 01073a5d 2019-08-22 stsp __dead static void
6911 01073a5d 2019-08-22 stsp usage_cat(void)
6912 01073a5d 2019-08-22 stsp {
6913 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6914 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
6915 01073a5d 2019-08-22 stsp exit(1);
6916 01073a5d 2019-08-22 stsp }
6917 01073a5d 2019-08-22 stsp
6918 01073a5d 2019-08-22 stsp static const struct got_error *
6919 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6920 01073a5d 2019-08-22 stsp {
6921 01073a5d 2019-08-22 stsp const struct got_error *err;
6922 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6923 01073a5d 2019-08-22 stsp
6924 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6925 01073a5d 2019-08-22 stsp if (err)
6926 01073a5d 2019-08-22 stsp return err;
6927 01073a5d 2019-08-22 stsp
6928 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6929 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6930 01073a5d 2019-08-22 stsp return err;
6931 01073a5d 2019-08-22 stsp }
6932 01073a5d 2019-08-22 stsp
6933 01073a5d 2019-08-22 stsp static const struct got_error *
6934 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6935 01073a5d 2019-08-22 stsp {
6936 01073a5d 2019-08-22 stsp const struct got_error *err;
6937 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6938 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
6939 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
6940 01073a5d 2019-08-22 stsp
6941 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6942 01073a5d 2019-08-22 stsp if (err)
6943 01073a5d 2019-08-22 stsp return err;
6944 01073a5d 2019-08-22 stsp
6945 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6946 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6947 01073a5d 2019-08-22 stsp while (te) {
6948 01073a5d 2019-08-22 stsp char *id_str;
6949 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6950 01073a5d 2019-08-22 stsp break;
6951 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6952 01073a5d 2019-08-22 stsp if (err)
6953 01073a5d 2019-08-22 stsp break;
6954 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6955 01073a5d 2019-08-22 stsp free(id_str);
6956 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6957 01073a5d 2019-08-22 stsp }
6958 01073a5d 2019-08-22 stsp
6959 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6960 01073a5d 2019-08-22 stsp return err;
6961 01073a5d 2019-08-22 stsp }
6962 01073a5d 2019-08-22 stsp
6963 01073a5d 2019-08-22 stsp static const struct got_error *
6964 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6965 01073a5d 2019-08-22 stsp {
6966 01073a5d 2019-08-22 stsp const struct got_error *err;
6967 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6968 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6969 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6970 01073a5d 2019-08-22 stsp char *id_str = NULL;
6971 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6972 01073a5d 2019-08-22 stsp
6973 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6974 01073a5d 2019-08-22 stsp if (err)
6975 01073a5d 2019-08-22 stsp return err;
6976 01073a5d 2019-08-22 stsp
6977 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6978 01073a5d 2019-08-22 stsp if (err)
6979 01073a5d 2019-08-22 stsp goto done;
6980 01073a5d 2019-08-22 stsp
6981 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6982 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6983 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
6984 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6985 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6986 01073a5d 2019-08-22 stsp char *pid_str;
6987 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6988 01073a5d 2019-08-22 stsp if (err)
6989 01073a5d 2019-08-22 stsp goto done;
6990 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6991 01073a5d 2019-08-22 stsp free(pid_str);
6992 01073a5d 2019-08-22 stsp }
6993 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6994 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6995 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6996 01073a5d 2019-08-22 stsp
6997 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6998 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6999 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7000 01073a5d 2019-08-22 stsp
7001 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7002 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7003 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7004 01073a5d 2019-08-22 stsp done:
7005 01073a5d 2019-08-22 stsp free(id_str);
7006 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7007 01073a5d 2019-08-22 stsp return err;
7008 01073a5d 2019-08-22 stsp }
7009 01073a5d 2019-08-22 stsp
7010 01073a5d 2019-08-22 stsp static const struct got_error *
7011 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7012 01073a5d 2019-08-22 stsp {
7013 01073a5d 2019-08-22 stsp const struct got_error *err;
7014 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7015 01073a5d 2019-08-22 stsp char *id_str = NULL;
7016 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7017 01073a5d 2019-08-22 stsp
7018 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7019 01073a5d 2019-08-22 stsp if (err)
7020 01073a5d 2019-08-22 stsp return err;
7021 01073a5d 2019-08-22 stsp
7022 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7023 01073a5d 2019-08-22 stsp if (err)
7024 01073a5d 2019-08-22 stsp goto done;
7025 01073a5d 2019-08-22 stsp
7026 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7027 e15d5241 2019-08-22 stsp
7028 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7029 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7030 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7031 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7032 01073a5d 2019-08-22 stsp break;
7033 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7034 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7035 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7036 01073a5d 2019-08-22 stsp break;
7037 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7038 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7039 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7040 01073a5d 2019-08-22 stsp break;
7041 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7042 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7043 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7044 01073a5d 2019-08-22 stsp break;
7045 01073a5d 2019-08-22 stsp default:
7046 01073a5d 2019-08-22 stsp break;
7047 01073a5d 2019-08-22 stsp }
7048 01073a5d 2019-08-22 stsp
7049 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7050 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7051 e15d5241 2019-08-22 stsp
7052 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7053 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7054 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7055 01073a5d 2019-08-22 stsp
7056 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7057 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7058 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7059 01073a5d 2019-08-22 stsp done:
7060 01073a5d 2019-08-22 stsp free(id_str);
7061 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7062 01073a5d 2019-08-22 stsp return err;
7063 01073a5d 2019-08-22 stsp }
7064 01073a5d 2019-08-22 stsp
7065 01073a5d 2019-08-22 stsp static const struct got_error *
7066 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7067 01073a5d 2019-08-22 stsp {
7068 01073a5d 2019-08-22 stsp const struct got_error *error;
7069 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7070 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7071 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7072 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7073 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7074 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7075 01073a5d 2019-08-22 stsp
7076 01073a5d 2019-08-22 stsp #ifndef PROFILE
7077 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7078 01073a5d 2019-08-22 stsp NULL) == -1)
7079 01073a5d 2019-08-22 stsp err(1, "pledge");
7080 01073a5d 2019-08-22 stsp #endif
7081 01073a5d 2019-08-22 stsp
7082 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7083 01073a5d 2019-08-22 stsp switch (ch) {
7084 896e9b6f 2019-08-26 stsp case 'c':
7085 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7086 896e9b6f 2019-08-26 stsp break;
7087 01073a5d 2019-08-22 stsp case 'r':
7088 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7089 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7090 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7091 9ba1d308 2019-10-21 stsp optarg);
7092 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7093 01073a5d 2019-08-22 stsp break;
7094 896e9b6f 2019-08-26 stsp case 'P':
7095 896e9b6f 2019-08-26 stsp force_path = 1;
7096 896e9b6f 2019-08-26 stsp break;
7097 01073a5d 2019-08-22 stsp default:
7098 01073a5d 2019-08-22 stsp usage_cat();
7099 01073a5d 2019-08-22 stsp /* NOTREACHED */
7100 01073a5d 2019-08-22 stsp }
7101 01073a5d 2019-08-22 stsp }
7102 01073a5d 2019-08-22 stsp
7103 01073a5d 2019-08-22 stsp argc -= optind;
7104 01073a5d 2019-08-22 stsp argv += optind;
7105 01073a5d 2019-08-22 stsp
7106 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7107 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7108 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7109 01073a5d 2019-08-22 stsp goto done;
7110 01073a5d 2019-08-22 stsp }
7111 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7112 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7113 01073a5d 2019-08-22 stsp goto done;
7114 01073a5d 2019-08-22 stsp if (worktree) {
7115 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7116 01073a5d 2019-08-22 stsp repo_path = strdup(
7117 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7118 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7119 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7120 01073a5d 2019-08-22 stsp goto done;
7121 01073a5d 2019-08-22 stsp }
7122 01073a5d 2019-08-22 stsp }
7123 01073a5d 2019-08-22 stsp }
7124 01073a5d 2019-08-22 stsp
7125 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7126 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7127 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7128 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7129 01073a5d 2019-08-22 stsp }
7130 01073a5d 2019-08-22 stsp
7131 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7132 01073a5d 2019-08-22 stsp free(repo_path);
7133 01073a5d 2019-08-22 stsp if (error != NULL)
7134 01073a5d 2019-08-22 stsp goto done;
7135 01073a5d 2019-08-22 stsp
7136 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7137 896e9b6f 2019-08-26 stsp if (error)
7138 896e9b6f 2019-08-26 stsp goto done;
7139 896e9b6f 2019-08-26 stsp
7140 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7141 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7142 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7143 01073a5d 2019-08-22 stsp if (error)
7144 01073a5d 2019-08-22 stsp goto done;
7145 01073a5d 2019-08-22 stsp
7146 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7147 896e9b6f 2019-08-26 stsp if (force_path) {
7148 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7149 896e9b6f 2019-08-26 stsp argv[i]);
7150 896e9b6f 2019-08-26 stsp if (error)
7151 896e9b6f 2019-08-26 stsp break;
7152 896e9b6f 2019-08-26 stsp } else {
7153 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7154 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7155 896e9b6f 2019-08-26 stsp if (error) {
7156 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7157 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7158 896e9b6f 2019-08-26 stsp break;
7159 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7160 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7161 896e9b6f 2019-08-26 stsp if (error)
7162 896e9b6f 2019-08-26 stsp break;
7163 896e9b6f 2019-08-26 stsp }
7164 896e9b6f 2019-08-26 stsp }
7165 01073a5d 2019-08-22 stsp
7166 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7167 01073a5d 2019-08-22 stsp if (error)
7168 01073a5d 2019-08-22 stsp break;
7169 01073a5d 2019-08-22 stsp
7170 01073a5d 2019-08-22 stsp switch (obj_type) {
7171 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7172 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7173 01073a5d 2019-08-22 stsp break;
7174 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7175 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7176 01073a5d 2019-08-22 stsp break;
7177 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7178 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7179 01073a5d 2019-08-22 stsp break;
7180 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7181 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7182 01073a5d 2019-08-22 stsp break;
7183 01073a5d 2019-08-22 stsp default:
7184 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7185 01073a5d 2019-08-22 stsp break;
7186 01073a5d 2019-08-22 stsp }
7187 01073a5d 2019-08-22 stsp if (error)
7188 01073a5d 2019-08-22 stsp break;
7189 01073a5d 2019-08-22 stsp free(label);
7190 01073a5d 2019-08-22 stsp label = NULL;
7191 01073a5d 2019-08-22 stsp free(id);
7192 01073a5d 2019-08-22 stsp id = NULL;
7193 01073a5d 2019-08-22 stsp }
7194 01073a5d 2019-08-22 stsp
7195 01073a5d 2019-08-22 stsp done:
7196 01073a5d 2019-08-22 stsp free(label);
7197 01073a5d 2019-08-22 stsp free(id);
7198 896e9b6f 2019-08-26 stsp free(commit_id);
7199 01073a5d 2019-08-22 stsp if (worktree)
7200 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7201 01073a5d 2019-08-22 stsp if (repo) {
7202 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7203 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7204 01073a5d 2019-08-22 stsp if (error == NULL)
7205 01073a5d 2019-08-22 stsp error = repo_error;
7206 01073a5d 2019-08-22 stsp }
7207 0ebf8283 2019-07-24 stsp return error;
7208 0ebf8283 2019-07-24 stsp }