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 80d5f134 2018-11-11 stsp if (error && !(sigint_received || sigpipe_received)) {
223 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
224 d7d4f210 2018-03-12 stsp return 1;
225 d7d4f210 2018-03-12 stsp }
226 d7d4f210 2018-03-12 stsp
227 d7d4f210 2018-03-12 stsp return 0;
228 5c860e29 2018-03-12 stsp }
229 5c860e29 2018-03-12 stsp
230 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
231 ce5b7c56 2019-07-09 stsp list_commands();
232 5c860e29 2018-03-12 stsp return 1;
233 5c860e29 2018-03-12 stsp }
234 5c860e29 2018-03-12 stsp
235 4ed7e80c 2018-05-20 stsp __dead static void
236 ce5b7c56 2019-07-09 stsp usage(int hflag)
237 5c860e29 2018-03-12 stsp {
238 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
239 53ccebc2 2019-07-30 stsp getprogname());
240 ce5b7c56 2019-07-09 stsp if (hflag)
241 ce5b7c56 2019-07-09 stsp list_commands();
242 5c860e29 2018-03-12 stsp exit(1);
243 5c860e29 2018-03-12 stsp }
244 5c860e29 2018-03-12 stsp
245 0266afb7 2019-01-04 stsp static const struct got_error *
246 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
247 e2ba3d07 2019-05-13 stsp {
248 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
249 e2ba3d07 2019-05-13 stsp const char *editor;
250 8920fa04 2019-08-18 stsp
251 8920fa04 2019-08-18 stsp *abspath = NULL;
252 e2ba3d07 2019-05-13 stsp
253 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
254 e2ba3d07 2019-05-13 stsp if (editor == NULL)
255 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
256 e2ba3d07 2019-05-13 stsp
257 0ee7065d 2019-05-13 stsp if (editor) {
258 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
259 0ee7065d 2019-05-13 stsp if (err)
260 0ee7065d 2019-05-13 stsp return err;
261 0ee7065d 2019-05-13 stsp }
262 e2ba3d07 2019-05-13 stsp
263 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
264 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
265 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
266 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
267 0ee7065d 2019-05-13 stsp }
268 0ee7065d 2019-05-13 stsp
269 e2ba3d07 2019-05-13 stsp return NULL;
270 e2ba3d07 2019-05-13 stsp }
271 e2ba3d07 2019-05-13 stsp
272 e2ba3d07 2019-05-13 stsp static const struct got_error *
273 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
274 c530dc23 2019-07-23 stsp const char *worktree_path)
275 0266afb7 2019-01-04 stsp {
276 163ce85a 2019-05-13 stsp const struct got_error *err;
277 0266afb7 2019-01-04 stsp
278 37c06ea4 2019-07-15 stsp #ifdef PROFILE
279 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
280 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
281 37c06ea4 2019-07-15 stsp #endif
282 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
283 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
284 0266afb7 2019-01-04 stsp
285 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
286 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
287 0266afb7 2019-01-04 stsp
288 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
289 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
290 0266afb7 2019-01-04 stsp
291 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
292 163ce85a 2019-05-13 stsp if (err != NULL)
293 163ce85a 2019-05-13 stsp return err;
294 0266afb7 2019-01-04 stsp
295 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
296 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
297 0266afb7 2019-01-04 stsp
298 0266afb7 2019-01-04 stsp return NULL;
299 2c7829a4 2019-06-17 stsp }
300 2c7829a4 2019-06-17 stsp
301 2c7829a4 2019-06-17 stsp __dead static void
302 2c7829a4 2019-06-17 stsp usage_init(void)
303 2c7829a4 2019-06-17 stsp {
304 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
305 2c7829a4 2019-06-17 stsp exit(1);
306 2c7829a4 2019-06-17 stsp }
307 2c7829a4 2019-06-17 stsp
308 2c7829a4 2019-06-17 stsp static const struct got_error *
309 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
310 2c7829a4 2019-06-17 stsp {
311 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
312 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
313 2c7829a4 2019-06-17 stsp int ch;
314 2c7829a4 2019-06-17 stsp
315 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
316 2c7829a4 2019-06-17 stsp switch (ch) {
317 2c7829a4 2019-06-17 stsp default:
318 2c7829a4 2019-06-17 stsp usage_init();
319 2c7829a4 2019-06-17 stsp /* NOTREACHED */
320 2c7829a4 2019-06-17 stsp }
321 2c7829a4 2019-06-17 stsp }
322 2c7829a4 2019-06-17 stsp
323 2c7829a4 2019-06-17 stsp argc -= optind;
324 2c7829a4 2019-06-17 stsp argv += optind;
325 2c7829a4 2019-06-17 stsp
326 2c7829a4 2019-06-17 stsp #ifndef PROFILE
327 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
328 2c7829a4 2019-06-17 stsp err(1, "pledge");
329 2c7829a4 2019-06-17 stsp #endif
330 2c7829a4 2019-06-17 stsp if (argc != 1)
331 bc20e173 2019-06-17 stsp usage_init();
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
334 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
335 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
336 2c7829a4 2019-06-17 stsp
337 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
338 2c7829a4 2019-06-17 stsp
339 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
340 2c7829a4 2019-06-17 stsp if (error &&
341 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
342 2c7829a4 2019-06-17 stsp goto done;
343 2c7829a4 2019-06-17 stsp
344 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
345 2c7829a4 2019-06-17 stsp if (error)
346 2c7829a4 2019-06-17 stsp goto done;
347 2c7829a4 2019-06-17 stsp
348 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
349 2c7829a4 2019-06-17 stsp if (error != NULL)
350 3ce1b845 2019-07-15 stsp goto done;
351 3ce1b845 2019-07-15 stsp
352 3ce1b845 2019-07-15 stsp done:
353 3ce1b845 2019-07-15 stsp free(repo_path);
354 3ce1b845 2019-07-15 stsp return error;
355 3ce1b845 2019-07-15 stsp }
356 3ce1b845 2019-07-15 stsp
357 3ce1b845 2019-07-15 stsp __dead static void
358 3ce1b845 2019-07-15 stsp usage_import(void)
359 3ce1b845 2019-07-15 stsp {
360 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
361 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
362 3ce1b845 2019-07-15 stsp exit(1);
363 3ce1b845 2019-07-15 stsp }
364 3ce1b845 2019-07-15 stsp
365 3ce1b845 2019-07-15 stsp int
366 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
367 3ce1b845 2019-07-15 stsp {
368 3ce1b845 2019-07-15 stsp pid_t pid;
369 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
370 3ce1b845 2019-07-15 stsp int st = -1;
371 3ce1b845 2019-07-15 stsp
372 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
373 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
374 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
375 3ce1b845 2019-07-15 stsp
376 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
377 3ce1b845 2019-07-15 stsp case -1:
378 3ce1b845 2019-07-15 stsp goto doneediting;
379 3ce1b845 2019-07-15 stsp case 0:
380 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
381 3ce1b845 2019-07-15 stsp _exit(127);
382 3ce1b845 2019-07-15 stsp }
383 3ce1b845 2019-07-15 stsp
384 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
385 3ce1b845 2019-07-15 stsp if (errno != EINTR)
386 3ce1b845 2019-07-15 stsp break;
387 3ce1b845 2019-07-15 stsp
388 3ce1b845 2019-07-15 stsp doneediting:
389 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
390 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
391 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
392 3ce1b845 2019-07-15 stsp
393 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
394 3ce1b845 2019-07-15 stsp errno = EINTR;
395 3ce1b845 2019-07-15 stsp return -1;
396 3ce1b845 2019-07-15 stsp }
397 3ce1b845 2019-07-15 stsp
398 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
399 3ce1b845 2019-07-15 stsp }
400 3ce1b845 2019-07-15 stsp
401 3ce1b845 2019-07-15 stsp static const struct got_error *
402 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
403 3ce1b845 2019-07-15 stsp const char *initial_content)
404 3ce1b845 2019-07-15 stsp {
405 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
406 3ce1b845 2019-07-15 stsp char buf[1024];
407 3ce1b845 2019-07-15 stsp struct stat st, st2;
408 3ce1b845 2019-07-15 stsp FILE *fp;
409 3ce1b845 2019-07-15 stsp int content_changed = 0;
410 3ce1b845 2019-07-15 stsp size_t len;
411 3ce1b845 2019-07-15 stsp
412 3ce1b845 2019-07-15 stsp *logmsg = NULL;
413 3ce1b845 2019-07-15 stsp
414 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
415 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
416 3ce1b845 2019-07-15 stsp
417 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
418 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
419 3ce1b845 2019-07-15 stsp
420 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
421 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
424 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
425 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
428 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
429 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
430 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
431 3ce1b845 2019-07-15 stsp len = 0;
432 3ce1b845 2019-07-15 stsp
433 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
434 3ce1b845 2019-07-15 stsp if (fp == NULL) {
435 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
436 3ce1b845 2019-07-15 stsp goto done;
437 3ce1b845 2019-07-15 stsp }
438 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
439 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
440 3ce1b845 2019-07-15 stsp content_changed = 1;
441 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
442 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
443 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
444 3ce1b845 2019-07-15 stsp }
445 3ce1b845 2019-07-15 stsp fclose(fp);
446 3ce1b845 2019-07-15 stsp
447 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
448 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
449 3ce1b845 2019-07-15 stsp len--;
450 3ce1b845 2019-07-15 stsp }
451 3ce1b845 2019-07-15 stsp
452 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
453 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
454 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
455 3ce1b845 2019-07-15 stsp done:
456 3ce1b845 2019-07-15 stsp if (err) {
457 3ce1b845 2019-07-15 stsp free(*logmsg);
458 3ce1b845 2019-07-15 stsp *logmsg = NULL;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp return err;
461 3ce1b845 2019-07-15 stsp }
462 3ce1b845 2019-07-15 stsp
463 3ce1b845 2019-07-15 stsp static const struct got_error *
464 3ce1b845 2019-07-15 stsp collect_import_msg(char **logmsg, const char *editor, const char *path_dir,
465 3ce1b845 2019-07-15 stsp const char *branch_name)
466 3ce1b845 2019-07-15 stsp {
467 3ce1b845 2019-07-15 stsp char *initial_content = NULL, *logmsg_path = NULL;
468 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
469 3ce1b845 2019-07-15 stsp int fd;
470 3ce1b845 2019-07-15 stsp
471 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
472 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
473 3ce1b845 2019-07-15 stsp branch_name) == -1)
474 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
475 3ce1b845 2019-07-15 stsp
476 3ce1b845 2019-07-15 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-importmsg");
477 3ce1b845 2019-07-15 stsp if (err)
478 3ce1b845 2019-07-15 stsp goto done;
479 3ce1b845 2019-07-15 stsp
480 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
481 3ce1b845 2019-07-15 stsp close(fd);
482 3ce1b845 2019-07-15 stsp
483 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, editor, logmsg_path, initial_content);
484 3ce1b845 2019-07-15 stsp done:
485 3ce1b845 2019-07-15 stsp free(initial_content);
486 3ce1b845 2019-07-15 stsp free(logmsg_path);
487 3ce1b845 2019-07-15 stsp return err;
488 3ce1b845 2019-07-15 stsp }
489 3ce1b845 2019-07-15 stsp
490 3ce1b845 2019-07-15 stsp static const struct got_error *
491 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
492 3ce1b845 2019-07-15 stsp {
493 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
494 3ce1b845 2019-07-15 stsp return NULL;
495 3ce1b845 2019-07-15 stsp }
496 3ce1b845 2019-07-15 stsp
497 3ce1b845 2019-07-15 stsp static const struct got_error *
498 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
499 84792843 2019-08-09 stsp {
500 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
501 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
502 84792843 2019-08-09 stsp
503 84792843 2019-08-09 stsp *author = NULL;
504 aba9c984 2019-09-08 stsp
505 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
506 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
507 c9956ddf 2019-09-08 stsp if (name && email) {
508 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
509 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
510 aba9c984 2019-09-08 stsp return NULL;
511 aba9c984 2019-09-08 stsp }
512 84792843 2019-08-09 stsp
513 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
514 84792843 2019-08-09 stsp if (got_author == NULL) {
515 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
516 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
517 c9956ddf 2019-09-08 stsp if (name && email) {
518 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
519 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
520 c9956ddf 2019-09-08 stsp return NULL;
521 c9956ddf 2019-09-08 stsp }
522 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
523 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
524 84792843 2019-08-09 stsp }
525 84792843 2019-08-09 stsp
526 aba9c984 2019-09-08 stsp *author = strdup(got_author);
527 aba9c984 2019-09-08 stsp if (*author == NULL)
528 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
529 84792843 2019-08-09 stsp
530 84792843 2019-08-09 stsp /*
531 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
532 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
533 84792843 2019-08-09 stsp */
534 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
535 84792843 2019-08-09 stsp got_author++;
536 aba9c984 2019-09-08 stsp if (*got_author != '<') {
537 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
538 aba9c984 2019-09-08 stsp goto done;
539 aba9c984 2019-09-08 stsp }
540 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
541 84792843 2019-08-09 stsp got_author++;
542 aba9c984 2019-09-08 stsp if (*got_author != '@') {
543 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
544 aba9c984 2019-09-08 stsp goto done;
545 aba9c984 2019-09-08 stsp }
546 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
547 84792843 2019-08-09 stsp got_author++;
548 84792843 2019-08-09 stsp if (*got_author != '>')
549 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 aba9c984 2019-09-08 stsp done:
551 aba9c984 2019-09-08 stsp if (err) {
552 aba9c984 2019-09-08 stsp free(*author);
553 aba9c984 2019-09-08 stsp *author = NULL;
554 aba9c984 2019-09-08 stsp }
555 aba9c984 2019-09-08 stsp return err;
556 c9956ddf 2019-09-08 stsp }
557 c9956ddf 2019-09-08 stsp
558 c9956ddf 2019-09-08 stsp static const struct got_error *
559 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
560 c9956ddf 2019-09-08 stsp {
561 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
562 c9956ddf 2019-09-08 stsp
563 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
564 c9956ddf 2019-09-08 stsp if (homedir) {
565 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
566 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
567 c9956ddf 2019-09-08 stsp
568 c9956ddf 2019-09-08 stsp }
569 c9956ddf 2019-09-08 stsp return NULL;
570 84792843 2019-08-09 stsp }
571 84792843 2019-08-09 stsp
572 84792843 2019-08-09 stsp static const struct got_error *
573 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
574 3ce1b845 2019-07-15 stsp {
575 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
576 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
577 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
578 3ce1b845 2019-07-15 stsp const char *branch_name = "master";
579 3ce1b845 2019-07-15 stsp char *refname = NULL, *id_str = NULL;
580 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
581 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
582 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
583 3ce1b845 2019-07-15 stsp int ch;
584 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
585 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
586 3ce1b845 2019-07-15 stsp
587 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
588 3ce1b845 2019-07-15 stsp
589 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
590 3ce1b845 2019-07-15 stsp switch (ch) {
591 3ce1b845 2019-07-15 stsp case 'b':
592 3ce1b845 2019-07-15 stsp branch_name = optarg;
593 3ce1b845 2019-07-15 stsp break;
594 3ce1b845 2019-07-15 stsp case 'm':
595 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
596 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
597 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
598 3ce1b845 2019-07-15 stsp goto done;
599 3ce1b845 2019-07-15 stsp }
600 3ce1b845 2019-07-15 stsp break;
601 3ce1b845 2019-07-15 stsp case 'r':
602 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
603 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
604 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
605 3ce1b845 2019-07-15 stsp goto done;
606 3ce1b845 2019-07-15 stsp }
607 3ce1b845 2019-07-15 stsp break;
608 3ce1b845 2019-07-15 stsp case 'I':
609 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
610 3ce1b845 2019-07-15 stsp break;
611 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
612 3ce1b845 2019-07-15 stsp NULL);
613 3ce1b845 2019-07-15 stsp if (error)
614 3ce1b845 2019-07-15 stsp goto done;
615 3ce1b845 2019-07-15 stsp break;
616 3ce1b845 2019-07-15 stsp default:
617 b2b65d18 2019-08-22 stsp usage_import();
618 3ce1b845 2019-07-15 stsp /* NOTREACHED */
619 3ce1b845 2019-07-15 stsp }
620 3ce1b845 2019-07-15 stsp }
621 3ce1b845 2019-07-15 stsp
622 3ce1b845 2019-07-15 stsp argc -= optind;
623 3ce1b845 2019-07-15 stsp argv += optind;
624 3ce1b845 2019-07-15 stsp
625 3ce1b845 2019-07-15 stsp #ifndef PROFILE
626 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
627 aba9c984 2019-09-08 stsp "unveil",
628 3ce1b845 2019-07-15 stsp NULL) == -1)
629 3ce1b845 2019-07-15 stsp err(1, "pledge");
630 3ce1b845 2019-07-15 stsp #endif
631 3ce1b845 2019-07-15 stsp if (argc != 1)
632 3ce1b845 2019-07-15 stsp usage_import();
633 2c7829a4 2019-06-17 stsp
634 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
635 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
636 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
637 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
638 3ce1b845 2019-07-15 stsp }
639 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
640 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
641 c9956ddf 2019-09-08 stsp if (error)
642 c9956ddf 2019-09-08 stsp goto done;
643 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
644 3ce1b845 2019-07-15 stsp if (error)
645 3ce1b845 2019-07-15 stsp goto done;
646 aba9c984 2019-09-08 stsp
647 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
648 aba9c984 2019-09-08 stsp if (error)
649 aba9c984 2019-09-08 stsp return error;
650 3ce1b845 2019-07-15 stsp
651 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
652 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
653 3ce1b845 2019-07-15 stsp goto done;
654 3ce1b845 2019-07-15 stsp }
655 3ce1b845 2019-07-15 stsp
656 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
657 3ce1b845 2019-07-15 stsp if (error) {
658 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
659 3ce1b845 2019-07-15 stsp goto done;
660 3ce1b845 2019-07-15 stsp } else {
661 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
662 3ce1b845 2019-07-15 stsp "import target branch already exists");
663 3ce1b845 2019-07-15 stsp goto done;
664 3ce1b845 2019-07-15 stsp }
665 3ce1b845 2019-07-15 stsp
666 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
667 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
668 3ce1b845 2019-07-15 stsp error = got_error_from_errno("realpath");
669 3ce1b845 2019-07-15 stsp goto done;
670 3ce1b845 2019-07-15 stsp }
671 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
672 3ce1b845 2019-07-15 stsp
673 3ce1b845 2019-07-15 stsp /*
674 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
675 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
676 3ce1b845 2019-07-15 stsp */
677 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
678 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
679 3ce1b845 2019-07-15 stsp if (error)
680 3ce1b845 2019-07-15 stsp goto done;
681 8e158b01 2019-09-22 stsp free(logmsg);
682 3ce1b845 2019-07-15 stsp error = collect_import_msg(&logmsg, editor, path_dir, refname);
683 3ce1b845 2019-07-15 stsp if (error)
684 3ce1b845 2019-07-15 stsp goto done;
685 3ce1b845 2019-07-15 stsp }
686 3ce1b845 2019-07-15 stsp
687 3ce1b845 2019-07-15 stsp if (unveil(path_dir, "r") != 0)
688 3ce1b845 2019-07-15 stsp return got_error_from_errno2("unveil", path_dir);
689 3ce1b845 2019-07-15 stsp
690 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
691 3ce1b845 2019-07-15 stsp if (error)
692 3ce1b845 2019-07-15 stsp goto done;
693 3ce1b845 2019-07-15 stsp
694 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
695 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
696 3ce1b845 2019-07-15 stsp if (error)
697 3ce1b845 2019-07-15 stsp goto done;
698 3ce1b845 2019-07-15 stsp
699 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
700 3ce1b845 2019-07-15 stsp if (error)
701 3ce1b845 2019-07-15 stsp goto done;
702 3ce1b845 2019-07-15 stsp
703 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
704 3ce1b845 2019-07-15 stsp if (error)
705 3ce1b845 2019-07-15 stsp goto done;
706 3ce1b845 2019-07-15 stsp
707 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
708 3ce1b845 2019-07-15 stsp if (error)
709 3ce1b845 2019-07-15 stsp goto done;
710 3ce1b845 2019-07-15 stsp
711 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
712 3ce1b845 2019-07-15 stsp if (error) {
713 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
714 3ce1b845 2019-07-15 stsp goto done;
715 3ce1b845 2019-07-15 stsp
716 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
717 3ce1b845 2019-07-15 stsp branch_ref);
718 3ce1b845 2019-07-15 stsp if (error)
719 3ce1b845 2019-07-15 stsp goto done;
720 3ce1b845 2019-07-15 stsp
721 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
722 3ce1b845 2019-07-15 stsp if (error)
723 3ce1b845 2019-07-15 stsp goto done;
724 3ce1b845 2019-07-15 stsp }
725 3ce1b845 2019-07-15 stsp
726 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
727 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
728 2c7829a4 2019-06-17 stsp done:
729 8e158b01 2019-09-22 stsp free(logmsg);
730 2c7829a4 2019-06-17 stsp free(repo_path);
731 3ce1b845 2019-07-15 stsp free(editor);
732 3ce1b845 2019-07-15 stsp free(refname);
733 3ce1b845 2019-07-15 stsp free(new_commit_id);
734 3ce1b845 2019-07-15 stsp free(id_str);
735 aba9c984 2019-09-08 stsp free(author);
736 c9956ddf 2019-09-08 stsp free(gitconfig_path);
737 3ce1b845 2019-07-15 stsp if (branch_ref)
738 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
739 3ce1b845 2019-07-15 stsp if (head_ref)
740 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
741 2c7829a4 2019-06-17 stsp return error;
742 0266afb7 2019-01-04 stsp }
743 0266afb7 2019-01-04 stsp
744 4ed7e80c 2018-05-20 stsp __dead static void
745 c09a553d 2018-03-12 stsp usage_checkout(void)
746 c09a553d 2018-03-12 stsp {
747 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
748 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
749 c09a553d 2018-03-12 stsp exit(1);
750 92a684f4 2018-03-12 stsp }
751 92a684f4 2018-03-12 stsp
752 1ee397ad 2019-07-12 stsp static const struct got_error *
753 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
754 92a684f4 2018-03-12 stsp {
755 92a684f4 2018-03-12 stsp char *worktree_path = arg;
756 a484d721 2019-06-10 stsp
757 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
758 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
759 1ee397ad 2019-07-12 stsp return NULL;
760 92a684f4 2018-03-12 stsp
761 92a684f4 2018-03-12 stsp while (path[0] == '/')
762 92a684f4 2018-03-12 stsp path++;
763 92a684f4 2018-03-12 stsp
764 d7b62c98 2018-12-27 stsp printf("%c %s/%s\n", status, worktree_path, path);
765 1ee397ad 2019-07-12 stsp return NULL;
766 99437157 2018-11-11 stsp }
767 99437157 2018-11-11 stsp
768 99437157 2018-11-11 stsp static const struct got_error *
769 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
770 99437157 2018-11-11 stsp {
771 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
772 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
773 99437157 2018-11-11 stsp return NULL;
774 8069f636 2019-01-12 stsp }
775 8069f636 2019-01-12 stsp
776 8069f636 2019-01-12 stsp static const struct got_error *
777 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
778 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
779 3aef623b 2019-10-15 stsp struct got_repository *repo)
780 8069f636 2019-01-12 stsp {
781 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
782 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
783 8069f636 2019-01-12 stsp
784 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
785 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
786 36a38700 2019-05-10 stsp if (err)
787 36a38700 2019-05-10 stsp return err;
788 8069f636 2019-01-12 stsp
789 d5bea539 2019-05-13 stsp if (yca_id == NULL)
790 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
791 8069f636 2019-01-12 stsp
792 d5bea539 2019-05-13 stsp /*
793 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
794 d5bea539 2019-05-13 stsp * and the work tree's base commit.
795 d5bea539 2019-05-13 stsp *
796 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
797 d5bea539 2019-05-13 stsp *
798 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
799 d5bea539 2019-05-13 stsp * \ /
800 d5bea539 2019-05-13 stsp * C E
801 d5bea539 2019-05-13 stsp * \ /
802 d5bea539 2019-05-13 stsp * B (yca)
803 d5bea539 2019-05-13 stsp * |
804 d5bea539 2019-05-13 stsp * A
805 d5bea539 2019-05-13 stsp *
806 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
807 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
808 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
809 d5bea539 2019-05-13 stsp */
810 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
811 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
812 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
813 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
814 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
815 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
816 8069f636 2019-01-12 stsp
817 d5bea539 2019-05-13 stsp free(yca_id);
818 d5bea539 2019-05-13 stsp return NULL;
819 c09a553d 2018-03-12 stsp }
820 a367ff0f 2019-05-14 stsp
821 a367ff0f 2019-05-14 stsp static const struct got_error *
822 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
823 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
824 a51a74b3 2019-07-27 stsp struct got_repository *repo)
825 a367ff0f 2019-05-14 stsp {
826 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
827 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
828 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
829 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
830 a367ff0f 2019-05-14 stsp
831 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
832 a367ff0f 2019-05-14 stsp if (err)
833 a51a74b3 2019-07-27 stsp goto done;
834 a51a74b3 2019-07-27 stsp
835 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
836 a51a74b3 2019-07-27 stsp is_same_branch = 1;
837 a51a74b3 2019-07-27 stsp goto done;
838 a51a74b3 2019-07-27 stsp }
839 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
840 a51a74b3 2019-07-27 stsp is_same_branch = 1;
841 a367ff0f 2019-05-14 stsp goto done;
842 a51a74b3 2019-07-27 stsp }
843 a367ff0f 2019-05-14 stsp
844 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
845 a367ff0f 2019-05-14 stsp if (err)
846 a367ff0f 2019-05-14 stsp goto done;
847 a367ff0f 2019-05-14 stsp
848 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
849 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
850 a367ff0f 2019-05-14 stsp if (err)
851 a367ff0f 2019-05-14 stsp goto done;
852 a367ff0f 2019-05-14 stsp
853 a367ff0f 2019-05-14 stsp for (;;) {
854 a367ff0f 2019-05-14 stsp struct got_object_id *id;
855 a367ff0f 2019-05-14 stsp err = got_commit_graph_iter_next(&id, graph);
856 a367ff0f 2019-05-14 stsp if (err) {
857 a367ff0f 2019-05-14 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
858 a367ff0f 2019-05-14 stsp err = NULL;
859 a367ff0f 2019-05-14 stsp break;
860 a14c42fa 2019-07-15 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
861 a367ff0f 2019-05-14 stsp break;
862 a367ff0f 2019-05-14 stsp err = got_commit_graph_fetch_commits(graph, 1,
863 6fb7cd11 2019-08-22 stsp repo, check_cancelled, NULL);
864 a367ff0f 2019-05-14 stsp if (err)
865 a367ff0f 2019-05-14 stsp break;
866 a367ff0f 2019-05-14 stsp }
867 c09a553d 2018-03-12 stsp
868 a367ff0f 2019-05-14 stsp if (id) {
869 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
870 a51a74b3 2019-07-27 stsp break;
871 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
872 a367ff0f 2019-05-14 stsp is_same_branch = 1;
873 a367ff0f 2019-05-14 stsp break;
874 a367ff0f 2019-05-14 stsp }
875 a367ff0f 2019-05-14 stsp }
876 a367ff0f 2019-05-14 stsp }
877 a367ff0f 2019-05-14 stsp done:
878 a367ff0f 2019-05-14 stsp if (graph)
879 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
880 a367ff0f 2019-05-14 stsp free(head_commit_id);
881 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
882 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
883 04f57cb3 2019-07-25 stsp return err;
884 04f57cb3 2019-07-25 stsp }
885 04f57cb3 2019-07-25 stsp
886 04f57cb3 2019-07-25 stsp static const struct got_error *
887 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
888 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
889 04f57cb3 2019-07-25 stsp {
890 04f57cb3 2019-07-25 stsp const struct got_error *err;
891 04f57cb3 2019-07-25 stsp struct got_reference *ref;
892 303e2782 2019-08-09 stsp struct got_tag_object *tag;
893 04f57cb3 2019-07-25 stsp
894 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
895 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
896 303e2782 2019-08-09 stsp if (err == NULL) {
897 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
898 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
899 303e2782 2019-08-09 stsp if (*commit_id == NULL)
900 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
901 303e2782 2019-08-09 stsp got_object_tag_close(tag);
902 303e2782 2019-08-09 stsp return err;
903 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
904 303e2782 2019-08-09 stsp return err;
905 303e2782 2019-08-09 stsp
906 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
907 04f57cb3 2019-07-25 stsp if (err == NULL) {
908 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
909 04f57cb3 2019-07-25 stsp got_ref_close(ref);
910 04f57cb3 2019-07-25 stsp } else {
911 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
912 04f57cb3 2019-07-25 stsp return err;
913 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
914 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
915 04f57cb3 2019-07-25 stsp }
916 a367ff0f 2019-05-14 stsp return err;
917 a367ff0f 2019-05-14 stsp }
918 8069f636 2019-01-12 stsp
919 4ed7e80c 2018-05-20 stsp static const struct got_error *
920 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
921 c09a553d 2018-03-12 stsp {
922 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
923 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
924 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
925 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
926 c09a553d 2018-03-12 stsp char *repo_path = NULL;
927 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
928 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
929 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
930 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
931 72151b04 2019-05-11 stsp int ch, same_path_prefix;
932 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
933 f2ea84fa 2019-07-27 stsp
934 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
935 c09a553d 2018-03-12 stsp
936 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
937 0bb8a95e 2018-03-12 stsp switch (ch) {
938 08573d5b 2019-05-14 stsp case 'b':
939 08573d5b 2019-05-14 stsp branch_name = optarg;
940 08573d5b 2019-05-14 stsp break;
941 8069f636 2019-01-12 stsp case 'c':
942 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
943 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
944 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
945 8069f636 2019-01-12 stsp break;
946 0bb8a95e 2018-03-12 stsp case 'p':
947 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
948 0bb8a95e 2018-03-12 stsp break;
949 0bb8a95e 2018-03-12 stsp default:
950 2deda0b9 2019-03-07 stsp usage_checkout();
951 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
952 0bb8a95e 2018-03-12 stsp }
953 0bb8a95e 2018-03-12 stsp }
954 0bb8a95e 2018-03-12 stsp
955 0bb8a95e 2018-03-12 stsp argc -= optind;
956 0bb8a95e 2018-03-12 stsp argv += optind;
957 0bb8a95e 2018-03-12 stsp
958 6715a751 2018-03-16 stsp #ifndef PROFILE
959 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
960 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
961 c09a553d 2018-03-12 stsp err(1, "pledge");
962 6715a751 2018-03-16 stsp #endif
963 0bb8a95e 2018-03-12 stsp if (argc == 1) {
964 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
965 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
966 76089277 2018-04-01 stsp if (repo_path == NULL)
967 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
968 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
969 76089277 2018-04-01 stsp if (cwd == NULL) {
970 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
971 76089277 2018-04-01 stsp goto done;
972 76089277 2018-04-01 stsp }
973 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
974 230a42bd 2019-05-11 jcs base = basename(path_prefix);
975 230a42bd 2019-05-11 jcs if (base == NULL) {
976 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
977 230a42bd 2019-05-11 jcs path_prefix);
978 230a42bd 2019-05-11 jcs goto done;
979 230a42bd 2019-05-11 jcs }
980 230a42bd 2019-05-11 jcs } else {
981 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
982 230a42bd 2019-05-11 jcs if (base == NULL) {
983 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
984 230a42bd 2019-05-11 jcs repo_path);
985 230a42bd 2019-05-11 jcs goto done;
986 230a42bd 2019-05-11 jcs }
987 76089277 2018-04-01 stsp }
988 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
989 c09a553d 2018-03-12 stsp if (dotgit)
990 c09a553d 2018-03-12 stsp *dotgit = '\0';
991 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
992 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
993 c09a553d 2018-03-12 stsp free(cwd);
994 76089277 2018-04-01 stsp goto done;
995 c09a553d 2018-03-12 stsp }
996 c09a553d 2018-03-12 stsp free(cwd);
997 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
998 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
999 76089277 2018-04-01 stsp if (repo_path == NULL) {
1000 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1001 76089277 2018-04-01 stsp goto done;
1002 76089277 2018-04-01 stsp }
1003 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1004 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1005 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1006 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1007 b4b3a7dd 2019-07-22 stsp argv[1]);
1008 b4b3a7dd 2019-07-22 stsp goto done;
1009 b4b3a7dd 2019-07-22 stsp }
1010 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1011 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1012 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1013 b4b3a7dd 2019-07-22 stsp goto done;
1014 b4b3a7dd 2019-07-22 stsp }
1015 76089277 2018-04-01 stsp }
1016 c09a553d 2018-03-12 stsp } else
1017 c09a553d 2018-03-12 stsp usage_checkout();
1018 c09a553d 2018-03-12 stsp
1019 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1020 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1021 13bfb272 2019-05-10 jcs
1022 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1023 c09a553d 2018-03-12 stsp if (error != NULL)
1024 c02c541e 2019-03-29 stsp goto done;
1025 c02c541e 2019-03-29 stsp
1026 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1027 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1028 c530dc23 2019-07-23 stsp if (error) {
1029 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1030 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1031 c530dc23 2019-07-23 stsp goto done;
1032 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1033 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1034 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1035 c530dc23 2019-07-23 stsp goto done;
1036 c530dc23 2019-07-23 stsp }
1037 c530dc23 2019-07-23 stsp }
1038 c530dc23 2019-07-23 stsp
1039 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1040 c02c541e 2019-03-29 stsp if (error)
1041 c09a553d 2018-03-12 stsp goto done;
1042 8069f636 2019-01-12 stsp
1043 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1044 c09a553d 2018-03-12 stsp if (error != NULL)
1045 c09a553d 2018-03-12 stsp goto done;
1046 c09a553d 2018-03-12 stsp
1047 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1048 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1049 c09a553d 2018-03-12 stsp goto done;
1050 c09a553d 2018-03-12 stsp
1051 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1052 c09a553d 2018-03-12 stsp if (error != NULL)
1053 c09a553d 2018-03-12 stsp goto done;
1054 c09a553d 2018-03-12 stsp
1055 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1056 e5dc7198 2018-12-29 stsp path_prefix);
1057 e5dc7198 2018-12-29 stsp if (error != NULL)
1058 e5dc7198 2018-12-29 stsp goto done;
1059 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1060 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1061 49520a32 2018-12-29 stsp goto done;
1062 49520a32 2018-12-29 stsp }
1063 49520a32 2018-12-29 stsp
1064 8069f636 2019-01-12 stsp if (commit_id_str) {
1065 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1066 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1067 30837e32 2019-07-25 stsp if (error)
1068 8069f636 2019-01-12 stsp goto done;
1069 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1070 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1071 8069f636 2019-01-12 stsp if (error != NULL) {
1072 8069f636 2019-01-12 stsp free(commit_id);
1073 8069f636 2019-01-12 stsp goto done;
1074 8069f636 2019-01-12 stsp }
1075 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1076 45d344f6 2019-05-14 stsp if (error)
1077 45d344f6 2019-05-14 stsp goto done;
1078 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1079 8069f636 2019-01-12 stsp commit_id);
1080 8069f636 2019-01-12 stsp free(commit_id);
1081 8069f636 2019-01-12 stsp if (error)
1082 8069f636 2019-01-12 stsp goto done;
1083 8069f636 2019-01-12 stsp }
1084 8069f636 2019-01-12 stsp
1085 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1086 f2ea84fa 2019-07-27 stsp if (error)
1087 f2ea84fa 2019-07-27 stsp goto done;
1088 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1089 6bad629b 2019-02-04 stsp checkout_progress, worktree_path, check_cancelled, NULL);
1090 c09a553d 2018-03-12 stsp if (error != NULL)
1091 c09a553d 2018-03-12 stsp goto done;
1092 c09a553d 2018-03-12 stsp
1093 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1094 c09a553d 2018-03-12 stsp
1095 c09a553d 2018-03-12 stsp done:
1096 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1097 8069f636 2019-01-12 stsp free(commit_id_str);
1098 76089277 2018-04-01 stsp free(repo_path);
1099 507dc3bb 2018-12-29 stsp free(worktree_path);
1100 507dc3bb 2018-12-29 stsp return error;
1101 507dc3bb 2018-12-29 stsp }
1102 507dc3bb 2018-12-29 stsp
1103 507dc3bb 2018-12-29 stsp __dead static void
1104 507dc3bb 2018-12-29 stsp usage_update(void)
1105 507dc3bb 2018-12-29 stsp {
1106 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1107 507dc3bb 2018-12-29 stsp getprogname());
1108 507dc3bb 2018-12-29 stsp exit(1);
1109 507dc3bb 2018-12-29 stsp }
1110 507dc3bb 2018-12-29 stsp
1111 1ee397ad 2019-07-12 stsp static const struct got_error *
1112 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1113 507dc3bb 2018-12-29 stsp {
1114 784955db 2019-01-12 stsp int *did_something = arg;
1115 784955db 2019-01-12 stsp
1116 507dc3bb 2018-12-29 stsp if (status == GOT_STATUS_EXISTS)
1117 1ee397ad 2019-07-12 stsp return NULL;
1118 507dc3bb 2018-12-29 stsp
1119 1545c615 2019-02-10 stsp *did_something = 1;
1120 a484d721 2019-06-10 stsp
1121 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1122 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1123 1ee397ad 2019-07-12 stsp return NULL;
1124 a484d721 2019-06-10 stsp
1125 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1126 507dc3bb 2018-12-29 stsp path++;
1127 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1128 1ee397ad 2019-07-12 stsp return NULL;
1129 be7061eb 2018-12-30 stsp }
1130 be7061eb 2018-12-30 stsp
1131 be7061eb 2018-12-30 stsp static const struct got_error *
1132 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1133 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1134 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1135 a1fb16d8 2019-05-24 stsp {
1136 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1137 a1fb16d8 2019-05-24 stsp char *base_id_str;
1138 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1139 a1fb16d8 2019-05-24 stsp
1140 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1141 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1142 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1143 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1144 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1145 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1146 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1147 a1fb16d8 2019-05-24 stsp }
1148 a1fb16d8 2019-05-24 stsp
1149 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1150 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1151 a1fb16d8 2019-05-24 stsp if (err) {
1152 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1153 a1fb16d8 2019-05-24 stsp return err;
1154 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1155 a1fb16d8 2019-05-24 stsp }
1156 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1157 a1fb16d8 2019-05-24 stsp return NULL;
1158 a1fb16d8 2019-05-24 stsp
1159 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1160 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1161 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1162 a1fb16d8 2019-05-24 stsp if (err)
1163 a1fb16d8 2019-05-24 stsp return err;
1164 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1165 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1166 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1167 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1168 0ebf8283 2019-07-24 stsp return NULL;
1169 0ebf8283 2019-07-24 stsp }
1170 0ebf8283 2019-07-24 stsp
1171 0ebf8283 2019-07-24 stsp static const struct got_error *
1172 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1173 0ebf8283 2019-07-24 stsp {
1174 0ebf8283 2019-07-24 stsp const struct got_error *err;
1175 0ebf8283 2019-07-24 stsp int in_progress;
1176 0ebf8283 2019-07-24 stsp
1177 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1178 0ebf8283 2019-07-24 stsp if (err)
1179 0ebf8283 2019-07-24 stsp return err;
1180 0ebf8283 2019-07-24 stsp if (in_progress)
1181 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1182 0ebf8283 2019-07-24 stsp
1183 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1184 0ebf8283 2019-07-24 stsp if (err)
1185 0ebf8283 2019-07-24 stsp return err;
1186 0ebf8283 2019-07-24 stsp if (in_progress)
1187 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1188 0ebf8283 2019-07-24 stsp
1189 a1fb16d8 2019-05-24 stsp return NULL;
1190 a5edda0a 2019-07-27 stsp }
1191 a5edda0a 2019-07-27 stsp
1192 a5edda0a 2019-07-27 stsp static const struct got_error *
1193 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1194 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1195 a5edda0a 2019-07-27 stsp {
1196 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1197 a5edda0a 2019-07-27 stsp char *path;
1198 a5edda0a 2019-07-27 stsp int i;
1199 a5edda0a 2019-07-27 stsp
1200 a5edda0a 2019-07-27 stsp if (argc == 0) {
1201 a5edda0a 2019-07-27 stsp path = strdup("");
1202 a5edda0a 2019-07-27 stsp if (path == NULL)
1203 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1204 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1205 a5edda0a 2019-07-27 stsp }
1206 a5edda0a 2019-07-27 stsp
1207 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1208 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1209 a5edda0a 2019-07-27 stsp if (err)
1210 a5edda0a 2019-07-27 stsp break;
1211 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1212 a5edda0a 2019-07-27 stsp if (err) {
1213 a5edda0a 2019-07-27 stsp free(path);
1214 a5edda0a 2019-07-27 stsp break;
1215 a5edda0a 2019-07-27 stsp }
1216 a5edda0a 2019-07-27 stsp }
1217 a5edda0a 2019-07-27 stsp
1218 a5edda0a 2019-07-27 stsp return err;
1219 a1fb16d8 2019-05-24 stsp }
1220 a1fb16d8 2019-05-24 stsp
1221 a1fb16d8 2019-05-24 stsp static const struct got_error *
1222 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1223 507dc3bb 2018-12-29 stsp {
1224 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1225 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1226 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1227 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1228 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1229 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1230 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1231 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1232 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1233 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1234 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1235 507dc3bb 2018-12-29 stsp
1236 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1237 f2ea84fa 2019-07-27 stsp
1238 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1239 507dc3bb 2018-12-29 stsp switch (ch) {
1240 024e9686 2019-05-14 stsp case 'b':
1241 024e9686 2019-05-14 stsp branch_name = optarg;
1242 024e9686 2019-05-14 stsp break;
1243 507dc3bb 2018-12-29 stsp case 'c':
1244 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1245 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1246 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1247 507dc3bb 2018-12-29 stsp break;
1248 507dc3bb 2018-12-29 stsp default:
1249 2deda0b9 2019-03-07 stsp usage_update();
1250 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1251 507dc3bb 2018-12-29 stsp }
1252 507dc3bb 2018-12-29 stsp }
1253 507dc3bb 2018-12-29 stsp
1254 507dc3bb 2018-12-29 stsp argc -= optind;
1255 507dc3bb 2018-12-29 stsp argv += optind;
1256 507dc3bb 2018-12-29 stsp
1257 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1258 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1259 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1260 507dc3bb 2018-12-29 stsp err(1, "pledge");
1261 507dc3bb 2018-12-29 stsp #endif
1262 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1263 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1264 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1265 c4cdcb68 2019-04-03 stsp goto done;
1266 c4cdcb68 2019-04-03 stsp }
1267 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1268 7d5807f4 2019-07-11 stsp if (error)
1269 7d5807f4 2019-07-11 stsp goto done;
1270 7d5807f4 2019-07-11 stsp
1271 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1272 f2ea84fa 2019-07-27 stsp if (error)
1273 f2ea84fa 2019-07-27 stsp goto done;
1274 507dc3bb 2018-12-29 stsp
1275 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1276 c9956ddf 2019-09-08 stsp NULL);
1277 507dc3bb 2018-12-29 stsp if (error != NULL)
1278 507dc3bb 2018-12-29 stsp goto done;
1279 507dc3bb 2018-12-29 stsp
1280 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1281 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1282 087fb88c 2019-08-04 stsp if (error)
1283 087fb88c 2019-08-04 stsp goto done;
1284 087fb88c 2019-08-04 stsp
1285 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1286 0266afb7 2019-01-04 stsp if (error)
1287 0266afb7 2019-01-04 stsp goto done;
1288 0266afb7 2019-01-04 stsp
1289 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1290 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1291 024e9686 2019-05-14 stsp if (error != NULL)
1292 024e9686 2019-05-14 stsp goto done;
1293 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1294 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1295 9c4b8182 2019-01-02 stsp if (error != NULL)
1296 9c4b8182 2019-01-02 stsp goto done;
1297 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1298 507dc3bb 2018-12-29 stsp if (error != NULL)
1299 507dc3bb 2018-12-29 stsp goto done;
1300 507dc3bb 2018-12-29 stsp } else {
1301 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1302 04f57cb3 2019-07-25 stsp free(commit_id_str);
1303 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1304 30837e32 2019-07-25 stsp if (error)
1305 a297e751 2019-07-11 stsp goto done;
1306 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1307 a297e751 2019-07-11 stsp if (error)
1308 507dc3bb 2018-12-29 stsp goto done;
1309 507dc3bb 2018-12-29 stsp }
1310 35c965b2 2018-12-29 stsp
1311 a1fb16d8 2019-05-24 stsp if (branch_name) {
1312 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1313 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1314 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1315 f2ea84fa 2019-07-27 stsp continue;
1316 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1317 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1318 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1319 024e9686 2019-05-14 stsp goto done;
1320 024e9686 2019-05-14 stsp }
1321 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1322 024e9686 2019-05-14 stsp if (error)
1323 024e9686 2019-05-14 stsp goto done;
1324 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1325 3aef623b 2019-10-15 stsp repo);
1326 a367ff0f 2019-05-14 stsp free(head_commit_id);
1327 024e9686 2019-05-14 stsp if (error != NULL)
1328 024e9686 2019-05-14 stsp goto done;
1329 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1330 a367ff0f 2019-05-14 stsp if (error)
1331 a367ff0f 2019-05-14 stsp goto done;
1332 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1333 024e9686 2019-05-14 stsp if (error)
1334 024e9686 2019-05-14 stsp goto done;
1335 024e9686 2019-05-14 stsp } else {
1336 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1337 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1338 a367ff0f 2019-05-14 stsp if (error != NULL) {
1339 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1340 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1341 024e9686 2019-05-14 stsp goto done;
1342 a367ff0f 2019-05-14 stsp }
1343 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1344 a367ff0f 2019-05-14 stsp if (error)
1345 a367ff0f 2019-05-14 stsp goto done;
1346 024e9686 2019-05-14 stsp }
1347 507dc3bb 2018-12-29 stsp
1348 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1349 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1350 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1351 507dc3bb 2018-12-29 stsp commit_id);
1352 507dc3bb 2018-12-29 stsp if (error)
1353 507dc3bb 2018-12-29 stsp goto done;
1354 507dc3bb 2018-12-29 stsp }
1355 507dc3bb 2018-12-29 stsp
1356 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1357 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1358 507dc3bb 2018-12-29 stsp if (error != NULL)
1359 507dc3bb 2018-12-29 stsp goto done;
1360 9c4b8182 2019-01-02 stsp
1361 784955db 2019-01-12 stsp if (did_something)
1362 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1363 784955db 2019-01-12 stsp else
1364 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1365 507dc3bb 2018-12-29 stsp done:
1366 c09a553d 2018-03-12 stsp free(worktree_path);
1367 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1368 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1369 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1370 507dc3bb 2018-12-29 stsp free(commit_id);
1371 9c4b8182 2019-01-02 stsp free(commit_id_str);
1372 c09a553d 2018-03-12 stsp return error;
1373 c09a553d 2018-03-12 stsp }
1374 c09a553d 2018-03-12 stsp
1375 f42b1b34 2018-03-12 stsp static const struct got_error *
1376 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1377 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1378 63035f9f 2019-10-06 stsp struct got_repository *repo)
1379 5c860e29 2018-03-12 stsp {
1380 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1381 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1382 79109fed 2018-03-27 stsp
1383 44392932 2019-08-25 stsp if (blob_id1) {
1384 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1385 44392932 2019-08-25 stsp if (err)
1386 44392932 2019-08-25 stsp goto done;
1387 44392932 2019-08-25 stsp }
1388 79109fed 2018-03-27 stsp
1389 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1390 44392932 2019-08-25 stsp if (err)
1391 44392932 2019-08-25 stsp goto done;
1392 79109fed 2018-03-27 stsp
1393 44392932 2019-08-25 stsp while (path[0] == '/')
1394 44392932 2019-08-25 stsp path++;
1395 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1396 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1397 44392932 2019-08-25 stsp done:
1398 44392932 2019-08-25 stsp if (blob1)
1399 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1400 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1401 44392932 2019-08-25 stsp return err;
1402 44392932 2019-08-25 stsp }
1403 44392932 2019-08-25 stsp
1404 44392932 2019-08-25 stsp static const struct got_error *
1405 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1406 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1407 63035f9f 2019-10-06 stsp struct got_repository *repo)
1408 44392932 2019-08-25 stsp {
1409 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1410 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1411 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1412 44392932 2019-08-25 stsp
1413 44392932 2019-08-25 stsp if (tree_id1) {
1414 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1415 79109fed 2018-03-27 stsp if (err)
1416 44392932 2019-08-25 stsp goto done;
1417 79109fed 2018-03-27 stsp }
1418 79109fed 2018-03-27 stsp
1419 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1420 0f2b3dca 2018-12-22 stsp if (err)
1421 0f2b3dca 2018-12-22 stsp goto done;
1422 0f2b3dca 2018-12-22 stsp
1423 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1424 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1425 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1426 44392932 2019-08-25 stsp while (path[0] == '/')
1427 44392932 2019-08-25 stsp path++;
1428 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1429 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1430 0f2b3dca 2018-12-22 stsp done:
1431 79109fed 2018-03-27 stsp if (tree1)
1432 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1433 366e0a5f 2019-10-10 stsp if (tree2)
1434 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1435 44392932 2019-08-25 stsp return err;
1436 44392932 2019-08-25 stsp }
1437 44392932 2019-08-25 stsp
1438 44392932 2019-08-25 stsp static const struct got_error *
1439 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1440 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1441 44392932 2019-08-25 stsp {
1442 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1443 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1444 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1445 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1446 44392932 2019-08-25 stsp struct got_object_qid *qid;
1447 44392932 2019-08-25 stsp
1448 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1449 44392932 2019-08-25 stsp if (qid != NULL) {
1450 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1451 44392932 2019-08-25 stsp qid->id);
1452 44392932 2019-08-25 stsp if (err)
1453 44392932 2019-08-25 stsp return err;
1454 44392932 2019-08-25 stsp }
1455 44392932 2019-08-25 stsp
1456 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1457 44392932 2019-08-25 stsp int obj_type;
1458 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1459 44392932 2019-08-25 stsp if (err)
1460 44392932 2019-08-25 stsp goto done;
1461 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1462 44392932 2019-08-25 stsp if (err) {
1463 44392932 2019-08-25 stsp free(obj_id2);
1464 44392932 2019-08-25 stsp goto done;
1465 44392932 2019-08-25 stsp }
1466 44392932 2019-08-25 stsp if (pcommit) {
1467 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1468 44392932 2019-08-25 stsp qid->id, path);
1469 44392932 2019-08-25 stsp if (err) {
1470 44392932 2019-08-25 stsp free(obj_id2);
1471 44392932 2019-08-25 stsp goto done;
1472 44392932 2019-08-25 stsp }
1473 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1474 44392932 2019-08-25 stsp if (err) {
1475 44392932 2019-08-25 stsp free(obj_id2);
1476 44392932 2019-08-25 stsp goto done;
1477 44392932 2019-08-25 stsp }
1478 44392932 2019-08-25 stsp }
1479 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1480 44392932 2019-08-25 stsp if (err) {
1481 44392932 2019-08-25 stsp free(obj_id2);
1482 44392932 2019-08-25 stsp goto done;
1483 44392932 2019-08-25 stsp }
1484 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1485 44392932 2019-08-25 stsp switch (obj_type) {
1486 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1487 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1488 63035f9f 2019-10-06 stsp 0, repo);
1489 44392932 2019-08-25 stsp break;
1490 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1491 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1492 63035f9f 2019-10-06 stsp 0, repo);
1493 44392932 2019-08-25 stsp break;
1494 44392932 2019-08-25 stsp default:
1495 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1496 44392932 2019-08-25 stsp break;
1497 44392932 2019-08-25 stsp }
1498 44392932 2019-08-25 stsp free(obj_id1);
1499 44392932 2019-08-25 stsp free(obj_id2);
1500 44392932 2019-08-25 stsp } else {
1501 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1502 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1503 44392932 2019-08-25 stsp if (err)
1504 44392932 2019-08-25 stsp goto done;
1505 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1506 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1507 44392932 2019-08-25 stsp if (err)
1508 44392932 2019-08-25 stsp goto done;
1509 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1510 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1511 44392932 2019-08-25 stsp }
1512 44392932 2019-08-25 stsp
1513 44392932 2019-08-25 stsp done:
1514 0f2b3dca 2018-12-22 stsp free(id_str1);
1515 0f2b3dca 2018-12-22 stsp free(id_str2);
1516 44392932 2019-08-25 stsp if (pcommit)
1517 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1518 79109fed 2018-03-27 stsp return err;
1519 79109fed 2018-03-27 stsp }
1520 79109fed 2018-03-27 stsp
1521 4bb494d5 2018-06-16 stsp static char *
1522 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1523 6c281f94 2018-06-11 stsp {
1524 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1525 09867e48 2019-08-13 stsp char *p, *s;
1526 09867e48 2019-08-13 stsp
1527 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1528 09867e48 2019-08-13 stsp if (tm == NULL)
1529 09867e48 2019-08-13 stsp return NULL;
1530 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1531 09867e48 2019-08-13 stsp if (s == NULL)
1532 09867e48 2019-08-13 stsp return NULL;
1533 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1534 6c281f94 2018-06-11 stsp if (p)
1535 6c281f94 2018-06-11 stsp *p = '\0';
1536 6c281f94 2018-06-11 stsp return s;
1537 6c281f94 2018-06-11 stsp }
1538 dc424a06 2019-08-07 stsp
1539 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1540 6c281f94 2018-06-11 stsp
1541 79109fed 2018-03-27 stsp static const struct got_error *
1542 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1543 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1544 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1545 79109fed 2018-03-27 stsp {
1546 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1547 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1548 6c281f94 2018-06-11 stsp char datebuf[26];
1549 45d799e2 2018-12-23 stsp time_t committer_time;
1550 45d799e2 2018-12-23 stsp const char *author, *committer;
1551 199a4027 2019-02-02 stsp char *refs_str = NULL;
1552 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1553 5c860e29 2018-03-12 stsp
1554 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1555 199a4027 2019-02-02 stsp char *s;
1556 199a4027 2019-02-02 stsp const char *name;
1557 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1558 a436ad14 2019-08-13 stsp int cmp;
1559 a436ad14 2019-08-13 stsp
1560 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1561 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1562 d9498b20 2019-02-05 stsp continue;
1563 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1564 199a4027 2019-02-02 stsp name += 5;
1565 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1566 7143d404 2019-03-12 stsp continue;
1567 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1568 e34f9ed6 2019-02-02 stsp name += 6;
1569 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1570 141c2bff 2019-02-04 stsp name += 8;
1571 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1572 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1573 5d844a1e 2019-08-13 stsp if (err) {
1574 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1575 5d844a1e 2019-08-13 stsp return err;
1576 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1577 5d844a1e 2019-08-13 stsp err = NULL;
1578 5d844a1e 2019-08-13 stsp tag = NULL;
1579 5d844a1e 2019-08-13 stsp }
1580 a436ad14 2019-08-13 stsp }
1581 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1582 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1583 a436ad14 2019-08-13 stsp if (tag)
1584 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1585 a436ad14 2019-08-13 stsp if (cmp != 0)
1586 a436ad14 2019-08-13 stsp continue;
1587 199a4027 2019-02-02 stsp s = refs_str;
1588 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1589 199a4027 2019-02-02 stsp name) == -1) {
1590 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1591 199a4027 2019-02-02 stsp free(s);
1592 34ca4898 2019-08-13 stsp return err;
1593 199a4027 2019-02-02 stsp }
1594 199a4027 2019-02-02 stsp free(s);
1595 199a4027 2019-02-02 stsp }
1596 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1597 8bf5b3c9 2018-03-17 stsp if (err)
1598 8bf5b3c9 2018-03-17 stsp return err;
1599 788c352e 2018-06-16 stsp
1600 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1601 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1602 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1603 832c249c 2018-06-10 stsp free(id_str);
1604 d3d493d7 2019-02-21 stsp id_str = NULL;
1605 d3d493d7 2019-02-21 stsp free(refs_str);
1606 d3d493d7 2019-02-21 stsp refs_str = NULL;
1607 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1608 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1609 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1610 09867e48 2019-08-13 stsp if (datestr)
1611 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1612 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1613 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1614 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1615 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1616 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1617 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1618 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1619 3fe1abad 2018-06-10 stsp int n = 1;
1620 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1621 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1622 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1623 a0603db2 2018-06-10 stsp if (err)
1624 a0603db2 2018-06-10 stsp return err;
1625 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1626 a0603db2 2018-06-10 stsp free(id_str);
1627 a0603db2 2018-06-10 stsp }
1628 a0603db2 2018-06-10 stsp }
1629 832c249c 2018-06-10 stsp
1630 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1631 5943eee2 2019-08-13 stsp if (err)
1632 5943eee2 2019-08-13 stsp return err;
1633 8bf5b3c9 2018-03-17 stsp
1634 621015ac 2018-07-23 stsp logmsg = logmsg0;
1635 832c249c 2018-06-10 stsp do {
1636 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1637 832c249c 2018-06-10 stsp if (line)
1638 832c249c 2018-06-10 stsp printf(" %s\n", line);
1639 832c249c 2018-06-10 stsp } while (line);
1640 621015ac 2018-07-23 stsp free(logmsg0);
1641 832c249c 2018-06-10 stsp
1642 971751ac 2018-03-27 stsp if (show_patch) {
1643 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1644 971751ac 2018-03-27 stsp if (err == 0)
1645 971751ac 2018-03-27 stsp printf("\n");
1646 971751ac 2018-03-27 stsp }
1647 07862c20 2018-09-15 stsp
1648 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1649 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1650 07862c20 2018-09-15 stsp return err;
1651 f42b1b34 2018-03-12 stsp }
1652 5c860e29 2018-03-12 stsp
1653 f42b1b34 2018-03-12 stsp static const struct got_error *
1654 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1655 15a94983 2018-12-23 stsp char *path, int show_patch, int diff_context, int limit,
1656 199a4027 2019-02-02 stsp int first_parent_traversal, struct got_reflist_head *refs)
1657 f42b1b34 2018-03-12 stsp {
1658 f42b1b34 2018-03-12 stsp const struct got_error *err;
1659 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1660 372ccdbb 2018-06-10 stsp
1661 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1662 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1663 f42b1b34 2018-03-12 stsp if (err)
1664 f42b1b34 2018-03-12 stsp return err;
1665 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1666 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1667 372ccdbb 2018-06-10 stsp if (err)
1668 fcc85cad 2018-07-22 stsp goto done;
1669 656b1f76 2019-05-11 jcs for (;;) {
1670 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1671 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1672 8bf5b3c9 2018-03-17 stsp
1673 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1674 84453469 2018-11-11 stsp break;
1675 84453469 2018-11-11 stsp
1676 b43fbaa0 2018-06-11 stsp err = got_commit_graph_iter_next(&id, graph);
1677 372ccdbb 2018-06-10 stsp if (err) {
1678 9ba79e04 2018-06-11 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
1679 9ba79e04 2018-06-11 stsp err = NULL;
1680 9ba79e04 2018-06-11 stsp break;
1681 9ba79e04 2018-06-11 stsp }
1682 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
1683 372ccdbb 2018-06-10 stsp break;
1684 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
1685 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1686 372ccdbb 2018-06-10 stsp if (err)
1687 372ccdbb 2018-06-10 stsp break;
1688 372ccdbb 2018-06-10 stsp else
1689 372ccdbb 2018-06-10 stsp continue;
1690 7e665116 2018-04-02 stsp }
1691 b43fbaa0 2018-06-11 stsp if (id == NULL)
1692 7e665116 2018-04-02 stsp break;
1693 b43fbaa0 2018-06-11 stsp
1694 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1695 b43fbaa0 2018-06-11 stsp if (err)
1696 fcc85cad 2018-07-22 stsp break;
1697 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1698 44392932 2019-08-25 stsp diff_context, refs);
1699 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1700 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1701 7e665116 2018-04-02 stsp break;
1702 31cedeaf 2018-09-15 stsp }
1703 fcc85cad 2018-07-22 stsp done:
1704 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1705 f42b1b34 2018-03-12 stsp return err;
1706 f42b1b34 2018-03-12 stsp }
1707 5c860e29 2018-03-12 stsp
1708 4ed7e80c 2018-05-20 stsp __dead static void
1709 6f3d1eb0 2018-03-12 stsp usage_log(void)
1710 6f3d1eb0 2018-03-12 stsp {
1711 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1712 04ca23f4 2018-07-16 stsp "[-r repository-path] [path]\n", getprogname());
1713 6f3d1eb0 2018-03-12 stsp exit(1);
1714 b1ebc001 2019-08-13 stsp }
1715 b1ebc001 2019-08-13 stsp
1716 b1ebc001 2019-08-13 stsp static int
1717 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1718 b1ebc001 2019-08-13 stsp {
1719 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1720 b1ebc001 2019-08-13 stsp long long n;
1721 b1ebc001 2019-08-13 stsp const char *errstr;
1722 b1ebc001 2019-08-13 stsp
1723 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1724 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1725 b1ebc001 2019-08-13 stsp return 0;
1726 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1727 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1728 b1ebc001 2019-08-13 stsp return 0;
1729 b1ebc001 2019-08-13 stsp return n;
1730 6f3d1eb0 2018-03-12 stsp }
1731 6f3d1eb0 2018-03-12 stsp
1732 4ed7e80c 2018-05-20 stsp static const struct got_error *
1733 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1734 f42b1b34 2018-03-12 stsp {
1735 f42b1b34 2018-03-12 stsp const struct got_error *error;
1736 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1737 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1738 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1739 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1740 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1741 d142fc45 2018-04-01 stsp char *start_commit = NULL;
1742 c0cc5c62 2018-10-18 stsp int diff_context = 3, ch;
1743 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1744 64a96a6d 2018-04-01 stsp const char *errstr;
1745 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1746 1b3893a2 2019-03-18 stsp
1747 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1748 5c860e29 2018-03-12 stsp
1749 6715a751 2018-03-16 stsp #ifndef PROFILE
1750 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1751 6098196c 2019-01-04 stsp NULL)
1752 ad242220 2018-09-08 stsp == -1)
1753 f42b1b34 2018-03-12 stsp err(1, "pledge");
1754 6715a751 2018-03-16 stsp #endif
1755 79109fed 2018-03-27 stsp
1756 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1757 b1ebc001 2019-08-13 stsp
1758 cc54c501 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
1759 79109fed 2018-03-27 stsp switch (ch) {
1760 79109fed 2018-03-27 stsp case 'p':
1761 79109fed 2018-03-27 stsp show_patch = 1;
1762 d142fc45 2018-04-01 stsp break;
1763 d142fc45 2018-04-01 stsp case 'c':
1764 d142fc45 2018-04-01 stsp start_commit = optarg;
1765 64a96a6d 2018-04-01 stsp break;
1766 c0cc5c62 2018-10-18 stsp case 'C':
1767 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1768 4a8520aa 2018-10-18 stsp &errstr);
1769 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1770 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1771 c0cc5c62 2018-10-18 stsp break;
1772 64a96a6d 2018-04-01 stsp case 'l':
1773 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1774 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1775 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1776 cc54c501 2019-07-15 stsp break;
1777 cc54c501 2019-07-15 stsp case 'f':
1778 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1779 a0603db2 2018-06-10 stsp break;
1780 04ca23f4 2018-07-16 stsp case 'r':
1781 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1782 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1783 04ca23f4 2018-07-16 stsp err(1, "-r option");
1784 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1785 04ca23f4 2018-07-16 stsp break;
1786 79109fed 2018-03-27 stsp default:
1787 2deda0b9 2019-03-07 stsp usage_log();
1788 79109fed 2018-03-27 stsp /* NOTREACHED */
1789 79109fed 2018-03-27 stsp }
1790 79109fed 2018-03-27 stsp }
1791 79109fed 2018-03-27 stsp
1792 79109fed 2018-03-27 stsp argc -= optind;
1793 79109fed 2018-03-27 stsp argv += optind;
1794 f42b1b34 2018-03-12 stsp
1795 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1796 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1797 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1798 04ca23f4 2018-07-16 stsp goto done;
1799 04ca23f4 2018-07-16 stsp }
1800 cffc0aa4 2019-02-05 stsp
1801 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1802 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1803 cffc0aa4 2019-02-05 stsp goto done;
1804 cffc0aa4 2019-02-05 stsp error = NULL;
1805 cffc0aa4 2019-02-05 stsp
1806 e7301579 2019-03-18 stsp if (argc == 0) {
1807 cbd1af7a 2019-03-18 stsp path = strdup("");
1808 e7301579 2019-03-18 stsp if (path == NULL) {
1809 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1810 cbd1af7a 2019-03-18 stsp goto done;
1811 e7301579 2019-03-18 stsp }
1812 e7301579 2019-03-18 stsp } else if (argc == 1) {
1813 e7301579 2019-03-18 stsp if (worktree) {
1814 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1815 e7301579 2019-03-18 stsp argv[0]);
1816 e7301579 2019-03-18 stsp if (error)
1817 e7301579 2019-03-18 stsp goto done;
1818 e7301579 2019-03-18 stsp } else {
1819 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1820 e7301579 2019-03-18 stsp if (path == NULL) {
1821 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1822 e7301579 2019-03-18 stsp goto done;
1823 e7301579 2019-03-18 stsp }
1824 e7301579 2019-03-18 stsp }
1825 cbd1af7a 2019-03-18 stsp } else
1826 cbd1af7a 2019-03-18 stsp usage_log();
1827 cbd1af7a 2019-03-18 stsp
1828 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1829 5486daa2 2019-05-11 stsp repo_path = worktree ?
1830 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1831 5486daa2 2019-05-11 stsp }
1832 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1833 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1834 cffc0aa4 2019-02-05 stsp goto done;
1835 04ca23f4 2018-07-16 stsp }
1836 6098196c 2019-01-04 stsp
1837 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1838 d7d4f210 2018-03-12 stsp if (error != NULL)
1839 04ca23f4 2018-07-16 stsp goto done;
1840 f42b1b34 2018-03-12 stsp
1841 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1842 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1843 c02c541e 2019-03-29 stsp if (error)
1844 c02c541e 2019-03-29 stsp goto done;
1845 c02c541e 2019-03-29 stsp
1846 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1847 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1848 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1849 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1850 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1851 3235492e 2018-04-01 stsp if (error != NULL)
1852 3235492e 2018-04-01 stsp return error;
1853 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1854 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1855 3235492e 2018-04-01 stsp if (error != NULL)
1856 3235492e 2018-04-01 stsp return error;
1857 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1858 3235492e 2018-04-01 stsp } else {
1859 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1860 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1861 3235492e 2018-04-01 stsp if (error == NULL) {
1862 1caad61b 2019-02-01 stsp int obj_type;
1863 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1864 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1865 d1f2edc9 2018-06-13 stsp if (error != NULL)
1866 1caad61b 2019-02-01 stsp goto done;
1867 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1868 1caad61b 2019-02-01 stsp if (error != NULL)
1869 1caad61b 2019-02-01 stsp goto done;
1870 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1871 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1872 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1873 1caad61b 2019-02-01 stsp if (error != NULL)
1874 1caad61b 2019-02-01 stsp goto done;
1875 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1876 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1877 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1878 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1879 1caad61b 2019-02-01 stsp goto done;
1880 1caad61b 2019-02-01 stsp }
1881 1caad61b 2019-02-01 stsp free(id);
1882 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1883 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1884 1caad61b 2019-02-01 stsp if (id == NULL)
1885 638f9024 2019-05-13 stsp error = got_error_from_errno(
1886 230a42bd 2019-05-11 jcs "got_object_id_dup");
1887 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1888 1caad61b 2019-02-01 stsp if (error)
1889 1caad61b 2019-02-01 stsp goto done;
1890 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1891 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1892 1caad61b 2019-02-01 stsp goto done;
1893 1caad61b 2019-02-01 stsp }
1894 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1895 d1f2edc9 2018-06-13 stsp if (error != NULL)
1896 1caad61b 2019-02-01 stsp goto done;
1897 d1f2edc9 2018-06-13 stsp }
1898 15a94983 2018-12-23 stsp if (commit == NULL) {
1899 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1900 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1901 d1f2edc9 2018-06-13 stsp if (error != NULL)
1902 d1f2edc9 2018-06-13 stsp return error;
1903 3235492e 2018-04-01 stsp }
1904 3235492e 2018-04-01 stsp }
1905 d7d4f210 2018-03-12 stsp if (error != NULL)
1906 04ca23f4 2018-07-16 stsp goto done;
1907 04ca23f4 2018-07-16 stsp
1908 deeabeae 2019-08-27 stsp if (worktree) {
1909 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
1910 deeabeae 2019-08-27 stsp char *p;
1911 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
1912 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
1913 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
1914 deeabeae 2019-08-27 stsp goto done;
1915 deeabeae 2019-08-27 stsp }
1916 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
1917 deeabeae 2019-08-27 stsp free(p);
1918 deeabeae 2019-08-27 stsp } else
1919 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
1920 04ca23f4 2018-07-16 stsp if (error != NULL)
1921 04ca23f4 2018-07-16 stsp goto done;
1922 04ca23f4 2018-07-16 stsp if (in_repo_path) {
1923 04ca23f4 2018-07-16 stsp free(path);
1924 04ca23f4 2018-07-16 stsp path = in_repo_path;
1925 04ca23f4 2018-07-16 stsp }
1926 199a4027 2019-02-02 stsp
1927 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
1928 199a4027 2019-02-02 stsp if (error)
1929 199a4027 2019-02-02 stsp goto done;
1930 04ca23f4 2018-07-16 stsp
1931 15a94983 2018-12-23 stsp error = print_commits(id, repo, path, show_patch,
1932 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
1933 04ca23f4 2018-07-16 stsp done:
1934 04ca23f4 2018-07-16 stsp free(path);
1935 04ca23f4 2018-07-16 stsp free(repo_path);
1936 04ca23f4 2018-07-16 stsp free(cwd);
1937 f42b1b34 2018-03-12 stsp free(id);
1938 cffc0aa4 2019-02-05 stsp if (worktree)
1939 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
1940 ad242220 2018-09-08 stsp if (repo) {
1941 ad242220 2018-09-08 stsp const struct got_error *repo_error;
1942 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
1943 ad242220 2018-09-08 stsp if (error == NULL)
1944 ad242220 2018-09-08 stsp error = repo_error;
1945 ad242220 2018-09-08 stsp }
1946 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
1947 8bf5b3c9 2018-03-17 stsp return error;
1948 5c860e29 2018-03-12 stsp }
1949 5c860e29 2018-03-12 stsp
1950 4ed7e80c 2018-05-20 stsp __dead static void
1951 3f8b7d6a 2018-04-01 stsp usage_diff(void)
1952 3f8b7d6a 2018-04-01 stsp {
1953 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
1954 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
1955 3f8b7d6a 2018-04-01 stsp exit(1);
1956 b00d56cd 2018-04-01 stsp }
1957 b00d56cd 2018-04-01 stsp
1958 b72f483a 2019-02-05 stsp struct print_diff_arg {
1959 b72f483a 2019-02-05 stsp struct got_repository *repo;
1960 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
1961 b72f483a 2019-02-05 stsp int diff_context;
1962 3fc0c068 2019-02-10 stsp const char *id_str;
1963 3fc0c068 2019-02-10 stsp int header_shown;
1964 98eaaa12 2019-08-03 stsp int diff_staged;
1965 63035f9f 2019-10-06 stsp int ignore_whitespace;
1966 b72f483a 2019-02-05 stsp };
1967 b72f483a 2019-02-05 stsp
1968 4ed7e80c 2018-05-20 stsp static const struct got_error *
1969 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
1970 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
1971 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
1972 b72f483a 2019-02-05 stsp {
1973 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
1974 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
1975 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
1976 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
1977 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
1978 b72f483a 2019-02-05 stsp struct stat sb;
1979 b72f483a 2019-02-05 stsp
1980 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
1981 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
1982 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
1983 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
1984 98eaaa12 2019-08-03 stsp return NULL;
1985 98eaaa12 2019-08-03 stsp } else {
1986 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
1987 98eaaa12 2019-08-03 stsp return NULL;
1988 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
1989 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
1990 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
1991 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
1992 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
1993 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
1994 98eaaa12 2019-08-03 stsp return NULL;
1995 98eaaa12 2019-08-03 stsp }
1996 3fc0c068 2019-02-10 stsp
1997 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
1998 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
1999 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2000 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2001 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2002 3fc0c068 2019-02-10 stsp }
2003 b72f483a 2019-02-05 stsp
2004 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2005 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2006 98eaaa12 2019-08-03 stsp switch (staged_status) {
2007 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2008 98eaaa12 2019-08-03 stsp label1 = path;
2009 98eaaa12 2019-08-03 stsp label2 = path;
2010 98eaaa12 2019-08-03 stsp break;
2011 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2012 98eaaa12 2019-08-03 stsp label2 = path;
2013 98eaaa12 2019-08-03 stsp break;
2014 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2015 98eaaa12 2019-08-03 stsp label1 = path;
2016 98eaaa12 2019-08-03 stsp break;
2017 98eaaa12 2019-08-03 stsp default:
2018 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2019 98eaaa12 2019-08-03 stsp }
2020 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2021 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2022 63035f9f 2019-10-06 stsp a->repo, stdout);
2023 98eaaa12 2019-08-03 stsp }
2024 98eaaa12 2019-08-03 stsp
2025 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2026 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2027 4ce46740 2019-08-08 stsp char *id_str;
2028 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2029 408b4ebc 2019-08-03 stsp 8192);
2030 4ce46740 2019-08-08 stsp if (err)
2031 4ce46740 2019-08-08 stsp goto done;
2032 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2033 4ce46740 2019-08-08 stsp if (err)
2034 4ce46740 2019-08-08 stsp goto done;
2035 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2036 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2037 4ce46740 2019-08-08 stsp free(id_str);
2038 4ce46740 2019-08-08 stsp goto done;
2039 4ce46740 2019-08-08 stsp }
2040 4ce46740 2019-08-08 stsp free(id_str);
2041 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2042 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2043 4ce46740 2019-08-08 stsp if (err)
2044 4ce46740 2019-08-08 stsp goto done;
2045 4ce46740 2019-08-08 stsp }
2046 d00136be 2019-03-26 stsp
2047 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2048 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2049 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2050 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2051 2ec1f75b 2019-03-26 stsp goto done;
2052 2ec1f75b 2019-03-26 stsp }
2053 b72f483a 2019-02-05 stsp
2054 2ec1f75b 2019-03-26 stsp f2 = fopen(abspath, "r");
2055 2ec1f75b 2019-03-26 stsp if (f2 == NULL) {
2056 638f9024 2019-05-13 stsp err = got_error_from_errno2("fopen", abspath);
2057 2ec1f75b 2019-03-26 stsp goto done;
2058 2ec1f75b 2019-03-26 stsp }
2059 2ec1f75b 2019-03-26 stsp if (lstat(abspath, &sb) == -1) {
2060 638f9024 2019-05-13 stsp err = got_error_from_errno2("lstat", abspath);
2061 2ec1f75b 2019-03-26 stsp goto done;
2062 2ec1f75b 2019-03-26 stsp }
2063 2ec1f75b 2019-03-26 stsp } else
2064 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2065 b72f483a 2019-02-05 stsp
2066 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2067 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2068 b72f483a 2019-02-05 stsp done:
2069 b72f483a 2019-02-05 stsp if (blob1)
2070 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2071 fb43ecf1 2019-02-11 stsp if (f2 && fclose(f2) != 0 && err == NULL)
2072 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2073 b72f483a 2019-02-05 stsp free(abspath);
2074 927df6b7 2019-02-10 stsp return err;
2075 927df6b7 2019-02-10 stsp }
2076 927df6b7 2019-02-10 stsp
2077 927df6b7 2019-02-10 stsp static const struct got_error *
2078 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2079 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2080 01073a5d 2019-08-22 stsp struct got_repository *repo)
2081 0adb7196 2019-08-11 stsp {
2082 0adb7196 2019-08-11 stsp const struct got_error *err;
2083 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2084 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2085 0adb7196 2019-08-11 stsp
2086 0adb7196 2019-08-11 stsp *id = NULL;
2087 0adb7196 2019-08-11 stsp *label = NULL;
2088 d24820bf 2019-08-11 stsp
2089 01073a5d 2019-08-22 stsp if (resolve_tags) {
2090 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2091 01073a5d 2019-08-22 stsp repo);
2092 01073a5d 2019-08-22 stsp if (err == NULL) {
2093 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2094 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2095 01073a5d 2019-08-22 stsp if (*id == NULL)
2096 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2097 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2098 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2099 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2100 32f0ab81 2019-08-28 hiltjo free(*id);
2101 01073a5d 2019-08-22 stsp *id = NULL;
2102 01073a5d 2019-08-22 stsp }
2103 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2104 01073a5d 2019-08-22 stsp return err;
2105 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2106 01073a5d 2019-08-22 stsp return err;
2107 01073a5d 2019-08-22 stsp }
2108 0adb7196 2019-08-11 stsp
2109 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2110 0adb7196 2019-08-11 stsp if (err) {
2111 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2112 0adb7196 2019-08-11 stsp return err;
2113 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2114 0adb7196 2019-08-11 stsp if (err != NULL)
2115 0adb7196 2019-08-11 stsp goto done;
2116 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2117 0adb7196 2019-08-11 stsp if (*label == NULL) {
2118 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2119 0adb7196 2019-08-11 stsp goto done;
2120 0adb7196 2019-08-11 stsp }
2121 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2122 0adb7196 2019-08-11 stsp } else {
2123 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2124 0adb7196 2019-08-11 stsp if (*label == NULL) {
2125 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2126 0adb7196 2019-08-11 stsp goto done;
2127 0adb7196 2019-08-11 stsp }
2128 0adb7196 2019-08-11 stsp }
2129 0adb7196 2019-08-11 stsp done:
2130 0adb7196 2019-08-11 stsp if (ref)
2131 0adb7196 2019-08-11 stsp got_ref_close(ref);
2132 0adb7196 2019-08-11 stsp return err;
2133 0adb7196 2019-08-11 stsp }
2134 0adb7196 2019-08-11 stsp
2135 0adb7196 2019-08-11 stsp
2136 0adb7196 2019-08-11 stsp static const struct got_error *
2137 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2138 b00d56cd 2018-04-01 stsp {
2139 b00d56cd 2018-04-01 stsp const struct got_error *error;
2140 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2141 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2142 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2143 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2144 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2145 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2146 15a94983 2018-12-23 stsp int type1, type2;
2147 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2148 c0cc5c62 2018-10-18 stsp const char *errstr;
2149 927df6b7 2019-02-10 stsp char *path = NULL;
2150 b00d56cd 2018-04-01 stsp
2151 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2152 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2153 25eccc22 2019-01-04 stsp NULL) == -1)
2154 b00d56cd 2018-04-01 stsp err(1, "pledge");
2155 b00d56cd 2018-04-01 stsp #endif
2156 b00d56cd 2018-04-01 stsp
2157 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2158 b00d56cd 2018-04-01 stsp switch (ch) {
2159 c0cc5c62 2018-10-18 stsp case 'C':
2160 c0cc5c62 2018-10-18 stsp diff_context = strtonum(optarg, 1, INT_MAX, &errstr);
2161 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2162 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2163 c0cc5c62 2018-10-18 stsp break;
2164 b72f483a 2019-02-05 stsp case 'r':
2165 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2166 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2167 b72f483a 2019-02-05 stsp err(1, "-r option");
2168 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2169 b72f483a 2019-02-05 stsp break;
2170 98eaaa12 2019-08-03 stsp case 's':
2171 98eaaa12 2019-08-03 stsp diff_staged = 1;
2172 63035f9f 2019-10-06 stsp break;
2173 63035f9f 2019-10-06 stsp case 'w':
2174 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2175 98eaaa12 2019-08-03 stsp break;
2176 b00d56cd 2018-04-01 stsp default:
2177 2deda0b9 2019-03-07 stsp usage_diff();
2178 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2179 b00d56cd 2018-04-01 stsp }
2180 b00d56cd 2018-04-01 stsp }
2181 b00d56cd 2018-04-01 stsp
2182 b00d56cd 2018-04-01 stsp argc -= optind;
2183 b00d56cd 2018-04-01 stsp argv += optind;
2184 b00d56cd 2018-04-01 stsp
2185 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2186 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2187 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2188 b72f483a 2019-02-05 stsp goto done;
2189 b72f483a 2019-02-05 stsp }
2190 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2191 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2192 927df6b7 2019-02-10 stsp goto done;
2193 927df6b7 2019-02-10 stsp if (argc <= 1) {
2194 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2195 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2196 927df6b7 2019-02-10 stsp goto done;
2197 927df6b7 2019-02-10 stsp }
2198 b72f483a 2019-02-05 stsp if (repo_path)
2199 b72f483a 2019-02-05 stsp errx(1,
2200 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2201 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2202 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2203 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2204 927df6b7 2019-02-10 stsp goto done;
2205 927df6b7 2019-02-10 stsp }
2206 927df6b7 2019-02-10 stsp if (argc == 1) {
2207 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2208 cbd1af7a 2019-03-18 stsp argv[0]);
2209 927df6b7 2019-02-10 stsp if (error)
2210 927df6b7 2019-02-10 stsp goto done;
2211 927df6b7 2019-02-10 stsp } else {
2212 927df6b7 2019-02-10 stsp path = strdup("");
2213 927df6b7 2019-02-10 stsp if (path == NULL) {
2214 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2215 927df6b7 2019-02-10 stsp goto done;
2216 927df6b7 2019-02-10 stsp }
2217 927df6b7 2019-02-10 stsp }
2218 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2219 98eaaa12 2019-08-03 stsp if (diff_staged)
2220 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2221 98eaaa12 2019-08-03 stsp "objects in repository");
2222 15a94983 2018-12-23 stsp id_str1 = argv[0];
2223 15a94983 2018-12-23 stsp id_str2 = argv[1];
2224 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2225 30db809c 2019-06-05 stsp repo_path =
2226 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2227 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2228 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2229 30db809c 2019-06-05 stsp goto done;
2230 30db809c 2019-06-05 stsp }
2231 30db809c 2019-06-05 stsp }
2232 b00d56cd 2018-04-01 stsp } else
2233 b00d56cd 2018-04-01 stsp usage_diff();
2234 25eccc22 2019-01-04 stsp
2235 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2236 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2237 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2238 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2239 b72f483a 2019-02-05 stsp }
2240 b00d56cd 2018-04-01 stsp
2241 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2242 76089277 2018-04-01 stsp free(repo_path);
2243 b00d56cd 2018-04-01 stsp if (error != NULL)
2244 b00d56cd 2018-04-01 stsp goto done;
2245 b00d56cd 2018-04-01 stsp
2246 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2247 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2248 c02c541e 2019-03-29 stsp if (error)
2249 c02c541e 2019-03-29 stsp goto done;
2250 c02c541e 2019-03-29 stsp
2251 30db809c 2019-06-05 stsp if (argc <= 1) {
2252 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2253 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2254 b72f483a 2019-02-05 stsp char *id_str;
2255 72ea6654 2019-07-27 stsp
2256 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2257 72ea6654 2019-07-27 stsp
2258 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2259 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2260 b72f483a 2019-02-05 stsp if (error)
2261 b72f483a 2019-02-05 stsp goto done;
2262 b72f483a 2019-02-05 stsp arg.repo = repo;
2263 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2264 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2265 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2266 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2267 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2268 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2269 b72f483a 2019-02-05 stsp
2270 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2271 72ea6654 2019-07-27 stsp if (error)
2272 72ea6654 2019-07-27 stsp goto done;
2273 72ea6654 2019-07-27 stsp
2274 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2275 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2276 b72f483a 2019-02-05 stsp free(id_str);
2277 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2278 b72f483a 2019-02-05 stsp goto done;
2279 b72f483a 2019-02-05 stsp }
2280 b72f483a 2019-02-05 stsp
2281 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2282 01073a5d 2019-08-22 stsp repo);
2283 0adb7196 2019-08-11 stsp if (error)
2284 0adb7196 2019-08-11 stsp goto done;
2285 b00d56cd 2018-04-01 stsp
2286 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2287 01073a5d 2019-08-22 stsp repo);
2288 0adb7196 2019-08-11 stsp if (error)
2289 0adb7196 2019-08-11 stsp goto done;
2290 b00d56cd 2018-04-01 stsp
2291 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2292 15a94983 2018-12-23 stsp if (error)
2293 15a94983 2018-12-23 stsp goto done;
2294 15a94983 2018-12-23 stsp
2295 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2296 15a94983 2018-12-23 stsp if (error)
2297 15a94983 2018-12-23 stsp goto done;
2298 15a94983 2018-12-23 stsp
2299 15a94983 2018-12-23 stsp if (type1 != type2) {
2300 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2301 b00d56cd 2018-04-01 stsp goto done;
2302 b00d56cd 2018-04-01 stsp }
2303 b00d56cd 2018-04-01 stsp
2304 15a94983 2018-12-23 stsp switch (type1) {
2305 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2306 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2307 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2308 b00d56cd 2018-04-01 stsp break;
2309 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2310 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2311 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2312 b00d56cd 2018-04-01 stsp break;
2313 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2314 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2315 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2316 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2317 b00d56cd 2018-04-01 stsp break;
2318 b00d56cd 2018-04-01 stsp default:
2319 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2320 b00d56cd 2018-04-01 stsp }
2321 b00d56cd 2018-04-01 stsp
2322 b00d56cd 2018-04-01 stsp done:
2323 5e70831e 2019-05-28 stsp free(label1);
2324 5e70831e 2019-05-28 stsp free(label2);
2325 15a94983 2018-12-23 stsp free(id1);
2326 15a94983 2018-12-23 stsp free(id2);
2327 927df6b7 2019-02-10 stsp free(path);
2328 b72f483a 2019-02-05 stsp if (worktree)
2329 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2330 ad242220 2018-09-08 stsp if (repo) {
2331 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2332 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2333 ad242220 2018-09-08 stsp if (error == NULL)
2334 ad242220 2018-09-08 stsp error = repo_error;
2335 ad242220 2018-09-08 stsp }
2336 b00d56cd 2018-04-01 stsp return error;
2337 404c43c4 2018-06-21 stsp }
2338 404c43c4 2018-06-21 stsp
2339 404c43c4 2018-06-21 stsp __dead static void
2340 404c43c4 2018-06-21 stsp usage_blame(void)
2341 404c43c4 2018-06-21 stsp {
2342 9270e621 2019-02-05 stsp fprintf(stderr,
2343 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2344 404c43c4 2018-06-21 stsp getprogname());
2345 404c43c4 2018-06-21 stsp exit(1);
2346 b00d56cd 2018-04-01 stsp }
2347 b00d56cd 2018-04-01 stsp
2348 28315671 2019-08-14 stsp struct blame_line {
2349 28315671 2019-08-14 stsp int annotated;
2350 28315671 2019-08-14 stsp char *id_str;
2351 82f6abb8 2019-08-14 stsp char *committer;
2352 bcb49d15 2019-08-14 stsp char datebuf[9]; /* YY-MM-DD + NUL */
2353 28315671 2019-08-14 stsp };
2354 28315671 2019-08-14 stsp
2355 28315671 2019-08-14 stsp struct blame_cb_args {
2356 28315671 2019-08-14 stsp struct blame_line *lines;
2357 28315671 2019-08-14 stsp int nlines;
2358 7ef28ff8 2019-08-14 stsp int nlines_prec;
2359 28315671 2019-08-14 stsp int lineno_cur;
2360 28315671 2019-08-14 stsp off_t *line_offsets;
2361 28315671 2019-08-14 stsp FILE *f;
2362 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2363 28315671 2019-08-14 stsp };
2364 28315671 2019-08-14 stsp
2365 404c43c4 2018-06-21 stsp static const struct got_error *
2366 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2367 28315671 2019-08-14 stsp {
2368 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2369 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2370 28315671 2019-08-14 stsp struct blame_line *bline;
2371 82f6abb8 2019-08-14 stsp char *line = NULL;
2372 28315671 2019-08-14 stsp size_t linesize = 0;
2373 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2374 28315671 2019-08-14 stsp off_t offset;
2375 bcb49d15 2019-08-14 stsp struct tm tm;
2376 bcb49d15 2019-08-14 stsp time_t committer_time;
2377 28315671 2019-08-14 stsp
2378 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2379 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2380 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2381 28315671 2019-08-14 stsp
2382 28315671 2019-08-14 stsp if (sigint_received)
2383 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2384 28315671 2019-08-14 stsp
2385 2839f8b9 2019-08-18 stsp if (lineno == -1)
2386 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2387 2839f8b9 2019-08-18 stsp
2388 28315671 2019-08-14 stsp /* Annotate this line. */
2389 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2390 28315671 2019-08-14 stsp if (bline->annotated)
2391 28315671 2019-08-14 stsp return NULL;
2392 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2393 28315671 2019-08-14 stsp if (err)
2394 28315671 2019-08-14 stsp return err;
2395 82f6abb8 2019-08-14 stsp
2396 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2397 82f6abb8 2019-08-14 stsp if (err)
2398 82f6abb8 2019-08-14 stsp goto done;
2399 82f6abb8 2019-08-14 stsp
2400 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2401 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2402 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2403 bcb49d15 2019-08-14 stsp goto done;
2404 bcb49d15 2019-08-14 stsp }
2405 bcb49d15 2019-08-14 stsp
2406 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2407 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2408 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2409 bcb49d15 2019-08-14 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%g/%m/%d",
2410 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2411 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2412 82f6abb8 2019-08-14 stsp goto done;
2413 82f6abb8 2019-08-14 stsp }
2414 28315671 2019-08-14 stsp bline->annotated = 1;
2415 28315671 2019-08-14 stsp
2416 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2417 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2418 28315671 2019-08-14 stsp if (!bline->annotated)
2419 82f6abb8 2019-08-14 stsp goto done;
2420 28315671 2019-08-14 stsp
2421 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2422 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2423 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2424 82f6abb8 2019-08-14 stsp goto done;
2425 82f6abb8 2019-08-14 stsp }
2426 28315671 2019-08-14 stsp
2427 28315671 2019-08-14 stsp while (bline->annotated) {
2428 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2429 82f6abb8 2019-08-14 stsp size_t len;
2430 82f6abb8 2019-08-14 stsp
2431 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2432 28315671 2019-08-14 stsp if (ferror(a->f))
2433 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2434 28315671 2019-08-14 stsp break;
2435 28315671 2019-08-14 stsp }
2436 82f6abb8 2019-08-14 stsp
2437 82f6abb8 2019-08-14 stsp committer = bline->committer;
2438 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2439 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2440 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2441 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2442 82f6abb8 2019-08-14 stsp if (at)
2443 82f6abb8 2019-08-14 stsp *at = '\0';
2444 82f6abb8 2019-08-14 stsp len = strlen(committer);
2445 82f6abb8 2019-08-14 stsp if (len >= 9)
2446 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2447 28315671 2019-08-14 stsp
2448 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2449 28315671 2019-08-14 stsp if (nl)
2450 28315671 2019-08-14 stsp *nl = '\0';
2451 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2452 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2453 28315671 2019-08-14 stsp
2454 28315671 2019-08-14 stsp a->lineno_cur++;
2455 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2456 28315671 2019-08-14 stsp }
2457 82f6abb8 2019-08-14 stsp done:
2458 82f6abb8 2019-08-14 stsp if (commit)
2459 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2460 28315671 2019-08-14 stsp free(line);
2461 28315671 2019-08-14 stsp return err;
2462 28315671 2019-08-14 stsp }
2463 28315671 2019-08-14 stsp
2464 28315671 2019-08-14 stsp static const struct got_error *
2465 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2466 404c43c4 2018-06-21 stsp {
2467 404c43c4 2018-06-21 stsp const struct got_error *error;
2468 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2469 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2470 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2471 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2472 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2473 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2474 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2475 28315671 2019-08-14 stsp struct blame_cb_args bca;
2476 28315671 2019-08-14 stsp int ch, obj_type, i;
2477 b02560ec 2019-08-19 stsp size_t filesize;
2478 90f3c347 2019-08-19 stsp
2479 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2480 404c43c4 2018-06-21 stsp
2481 404c43c4 2018-06-21 stsp #ifndef PROFILE
2482 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2483 36e2fb66 2019-01-04 stsp NULL) == -1)
2484 404c43c4 2018-06-21 stsp err(1, "pledge");
2485 404c43c4 2018-06-21 stsp #endif
2486 404c43c4 2018-06-21 stsp
2487 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2488 404c43c4 2018-06-21 stsp switch (ch) {
2489 404c43c4 2018-06-21 stsp case 'c':
2490 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2491 404c43c4 2018-06-21 stsp break;
2492 66bea077 2018-08-02 stsp case 'r':
2493 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2494 66bea077 2018-08-02 stsp if (repo_path == NULL)
2495 66bea077 2018-08-02 stsp err(1, "-r option");
2496 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2497 66bea077 2018-08-02 stsp break;
2498 404c43c4 2018-06-21 stsp default:
2499 2deda0b9 2019-03-07 stsp usage_blame();
2500 404c43c4 2018-06-21 stsp /* NOTREACHED */
2501 404c43c4 2018-06-21 stsp }
2502 404c43c4 2018-06-21 stsp }
2503 404c43c4 2018-06-21 stsp
2504 404c43c4 2018-06-21 stsp argc -= optind;
2505 404c43c4 2018-06-21 stsp argv += optind;
2506 404c43c4 2018-06-21 stsp
2507 a39318fd 2018-08-02 stsp if (argc == 1)
2508 404c43c4 2018-06-21 stsp path = argv[0];
2509 a39318fd 2018-08-02 stsp else
2510 404c43c4 2018-06-21 stsp usage_blame();
2511 404c43c4 2018-06-21 stsp
2512 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2513 66bea077 2018-08-02 stsp if (cwd == NULL) {
2514 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2515 66bea077 2018-08-02 stsp goto done;
2516 66bea077 2018-08-02 stsp }
2517 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2518 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2519 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2520 66bea077 2018-08-02 stsp goto done;
2521 0c06baac 2019-02-05 stsp else
2522 0c06baac 2019-02-05 stsp error = NULL;
2523 0c06baac 2019-02-05 stsp if (worktree) {
2524 0c06baac 2019-02-05 stsp repo_path =
2525 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2526 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2527 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2528 0c06baac 2019-02-05 stsp if (error)
2529 0c06baac 2019-02-05 stsp goto done;
2530 0c06baac 2019-02-05 stsp } else {
2531 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2532 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2533 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2534 0c06baac 2019-02-05 stsp goto done;
2535 0c06baac 2019-02-05 stsp }
2536 66bea077 2018-08-02 stsp }
2537 66bea077 2018-08-02 stsp }
2538 36e2fb66 2019-01-04 stsp
2539 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2540 c02c541e 2019-03-29 stsp if (error != NULL)
2541 36e2fb66 2019-01-04 stsp goto done;
2542 66bea077 2018-08-02 stsp
2543 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2544 c02c541e 2019-03-29 stsp if (error)
2545 404c43c4 2018-06-21 stsp goto done;
2546 404c43c4 2018-06-21 stsp
2547 0c06baac 2019-02-05 stsp if (worktree) {
2548 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2549 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2550 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2551 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2552 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2553 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2554 6efaaa2d 2019-02-05 stsp path) == -1) {
2555 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2556 0c06baac 2019-02-05 stsp goto done;
2557 0c06baac 2019-02-05 stsp }
2558 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2559 0c06baac 2019-02-05 stsp free(p);
2560 0c06baac 2019-02-05 stsp } else {
2561 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2562 0c06baac 2019-02-05 stsp }
2563 0c06baac 2019-02-05 stsp if (error)
2564 66bea077 2018-08-02 stsp goto done;
2565 66bea077 2018-08-02 stsp
2566 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2567 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2568 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2569 404c43c4 2018-06-21 stsp if (error != NULL)
2570 66bea077 2018-08-02 stsp goto done;
2571 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2572 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2573 404c43c4 2018-06-21 stsp if (error != NULL)
2574 66bea077 2018-08-02 stsp goto done;
2575 404c43c4 2018-06-21 stsp } else {
2576 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2577 30837e32 2019-07-25 stsp if (error)
2578 66bea077 2018-08-02 stsp goto done;
2579 404c43c4 2018-06-21 stsp }
2580 404c43c4 2018-06-21 stsp
2581 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2582 28315671 2019-08-14 stsp if (error)
2583 28315671 2019-08-14 stsp goto done;
2584 28315671 2019-08-14 stsp if (obj_id == NULL) {
2585 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2586 28315671 2019-08-14 stsp goto done;
2587 28315671 2019-08-14 stsp }
2588 28315671 2019-08-14 stsp
2589 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2590 28315671 2019-08-14 stsp if (error)
2591 28315671 2019-08-14 stsp goto done;
2592 28315671 2019-08-14 stsp
2593 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2594 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2595 28315671 2019-08-14 stsp goto done;
2596 28315671 2019-08-14 stsp }
2597 28315671 2019-08-14 stsp
2598 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2599 28315671 2019-08-14 stsp if (error)
2600 28315671 2019-08-14 stsp goto done;
2601 28315671 2019-08-14 stsp bca.f = got_opentemp();
2602 28315671 2019-08-14 stsp if (bca.f == NULL) {
2603 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2604 28315671 2019-08-14 stsp goto done;
2605 28315671 2019-08-14 stsp }
2606 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2607 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2608 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2609 28315671 2019-08-14 stsp goto done;
2610 28315671 2019-08-14 stsp
2611 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2612 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2613 b02560ec 2019-08-19 stsp bca.nlines--;
2614 b02560ec 2019-08-19 stsp
2615 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2616 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2617 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2618 28315671 2019-08-14 stsp goto done;
2619 28315671 2019-08-14 stsp }
2620 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2621 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2622 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2623 7ef28ff8 2019-08-14 stsp while (i > 0) {
2624 7ef28ff8 2019-08-14 stsp i /= 10;
2625 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2626 7ef28ff8 2019-08-14 stsp }
2627 82f6abb8 2019-08-14 stsp bca.repo = repo;
2628 7ef28ff8 2019-08-14 stsp
2629 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2630 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2631 28315671 2019-08-14 stsp if (error)
2632 28315671 2019-08-14 stsp goto done;
2633 404c43c4 2018-06-21 stsp done:
2634 66bea077 2018-08-02 stsp free(in_repo_path);
2635 66bea077 2018-08-02 stsp free(repo_path);
2636 66bea077 2018-08-02 stsp free(cwd);
2637 404c43c4 2018-06-21 stsp free(commit_id);
2638 28315671 2019-08-14 stsp free(obj_id);
2639 28315671 2019-08-14 stsp if (blob)
2640 28315671 2019-08-14 stsp got_object_blob_close(blob);
2641 0c06baac 2019-02-05 stsp if (worktree)
2642 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2643 ad242220 2018-09-08 stsp if (repo) {
2644 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2645 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2646 ad242220 2018-09-08 stsp if (error == NULL)
2647 ad242220 2018-09-08 stsp error = repo_error;
2648 ad242220 2018-09-08 stsp }
2649 3affba96 2019-09-22 stsp if (bca.lines) {
2650 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2651 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2652 3affba96 2019-09-22 stsp free(bline->id_str);
2653 3affba96 2019-09-22 stsp free(bline->committer);
2654 3affba96 2019-09-22 stsp }
2655 3affba96 2019-09-22 stsp free(bca.lines);
2656 28315671 2019-08-14 stsp }
2657 28315671 2019-08-14 stsp free(bca.line_offsets);
2658 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2659 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2660 404c43c4 2018-06-21 stsp return error;
2661 5de5890b 2018-10-18 stsp }
2662 5de5890b 2018-10-18 stsp
2663 5de5890b 2018-10-18 stsp __dead static void
2664 5de5890b 2018-10-18 stsp usage_tree(void)
2665 5de5890b 2018-10-18 stsp {
2666 c1669e2e 2019-01-09 stsp fprintf(stderr,
2667 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2668 5de5890b 2018-10-18 stsp getprogname());
2669 5de5890b 2018-10-18 stsp exit(1);
2670 5de5890b 2018-10-18 stsp }
2671 5de5890b 2018-10-18 stsp
2672 c1669e2e 2019-01-09 stsp static void
2673 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2674 c1669e2e 2019-01-09 stsp const char *root_path)
2675 c1669e2e 2019-01-09 stsp {
2676 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2677 848d6979 2019-08-12 stsp const char *modestr = "";
2678 5de5890b 2018-10-18 stsp
2679 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2680 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2681 c1669e2e 2019-01-09 stsp path++;
2682 c1669e2e 2019-01-09 stsp
2683 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2684 63c5ca5d 2019-08-24 stsp modestr = "$";
2685 63c5ca5d 2019-08-24 stsp else if (S_ISLNK(te->mode))
2686 848d6979 2019-08-12 stsp modestr = "@";
2687 848d6979 2019-08-12 stsp else if (S_ISDIR(te->mode))
2688 848d6979 2019-08-12 stsp modestr = "/";
2689 848d6979 2019-08-12 stsp else if (te->mode & S_IXUSR)
2690 848d6979 2019-08-12 stsp modestr = "*";
2691 848d6979 2019-08-12 stsp
2692 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2693 848d6979 2019-08-12 stsp is_root_path ? "" : "/", te->name, modestr);
2694 c1669e2e 2019-01-09 stsp }
2695 c1669e2e 2019-01-09 stsp
2696 5de5890b 2018-10-18 stsp static const struct got_error *
2697 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2698 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2699 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2700 5de5890b 2018-10-18 stsp {
2701 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2702 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2703 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2704 5de5890b 2018-10-18 stsp const struct got_tree_entries *entries;
2705 5de5890b 2018-10-18 stsp struct got_tree_entry *te;
2706 5de5890b 2018-10-18 stsp
2707 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2708 5de5890b 2018-10-18 stsp if (err)
2709 5de5890b 2018-10-18 stsp goto done;
2710 5de5890b 2018-10-18 stsp
2711 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2712 5de5890b 2018-10-18 stsp if (err)
2713 5de5890b 2018-10-18 stsp goto done;
2714 5de5890b 2018-10-18 stsp entries = got_object_tree_get_entries(tree);
2715 5de5890b 2018-10-18 stsp te = SIMPLEQ_FIRST(&entries->head);
2716 5de5890b 2018-10-18 stsp while (te) {
2717 5de5890b 2018-10-18 stsp char *id = NULL;
2718 84453469 2018-11-11 stsp
2719 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2720 84453469 2018-11-11 stsp break;
2721 84453469 2018-11-11 stsp
2722 5de5890b 2018-10-18 stsp if (show_ids) {
2723 5de5890b 2018-10-18 stsp char *id_str;
2724 5de5890b 2018-10-18 stsp err = got_object_id_str(&id_str, te->id);
2725 5de5890b 2018-10-18 stsp if (err)
2726 5de5890b 2018-10-18 stsp goto done;
2727 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2728 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2729 5de5890b 2018-10-18 stsp free(id_str);
2730 5de5890b 2018-10-18 stsp goto done;
2731 5de5890b 2018-10-18 stsp }
2732 5de5890b 2018-10-18 stsp free(id_str);
2733 5de5890b 2018-10-18 stsp }
2734 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2735 5de5890b 2018-10-18 stsp free(id);
2736 c1669e2e 2019-01-09 stsp
2737 c1669e2e 2019-01-09 stsp if (recurse && S_ISDIR(te->mode)) {
2738 c1669e2e 2019-01-09 stsp char *child_path;
2739 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2740 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2741 c1669e2e 2019-01-09 stsp te->name) == -1) {
2742 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2743 c1669e2e 2019-01-09 stsp goto done;
2744 c1669e2e 2019-01-09 stsp }
2745 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2746 c1669e2e 2019-01-09 stsp root_path, repo);
2747 c1669e2e 2019-01-09 stsp free(child_path);
2748 c1669e2e 2019-01-09 stsp if (err)
2749 c1669e2e 2019-01-09 stsp goto done;
2750 c1669e2e 2019-01-09 stsp }
2751 c1669e2e 2019-01-09 stsp
2752 c1669e2e 2019-01-09 stsp te = SIMPLEQ_NEXT(te, entry);
2753 5de5890b 2018-10-18 stsp }
2754 5de5890b 2018-10-18 stsp done:
2755 5de5890b 2018-10-18 stsp if (tree)
2756 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2757 5de5890b 2018-10-18 stsp free(tree_id);
2758 5de5890b 2018-10-18 stsp return err;
2759 404c43c4 2018-06-21 stsp }
2760 404c43c4 2018-06-21 stsp
2761 5de5890b 2018-10-18 stsp static const struct got_error *
2762 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2763 5de5890b 2018-10-18 stsp {
2764 5de5890b 2018-10-18 stsp const struct got_error *error;
2765 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2766 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2767 7a2c19d6 2019-02-05 stsp const char *path;
2768 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2769 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2770 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2771 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2772 5de5890b 2018-10-18 stsp int ch;
2773 5de5890b 2018-10-18 stsp
2774 5de5890b 2018-10-18 stsp #ifndef PROFILE
2775 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2776 0f8d269b 2019-01-04 stsp NULL) == -1)
2777 5de5890b 2018-10-18 stsp err(1, "pledge");
2778 5de5890b 2018-10-18 stsp #endif
2779 5de5890b 2018-10-18 stsp
2780 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2781 5de5890b 2018-10-18 stsp switch (ch) {
2782 5de5890b 2018-10-18 stsp case 'c':
2783 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2784 5de5890b 2018-10-18 stsp break;
2785 5de5890b 2018-10-18 stsp case 'r':
2786 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2787 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2788 5de5890b 2018-10-18 stsp err(1, "-r option");
2789 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2790 5de5890b 2018-10-18 stsp break;
2791 5de5890b 2018-10-18 stsp case 'i':
2792 5de5890b 2018-10-18 stsp show_ids = 1;
2793 5de5890b 2018-10-18 stsp break;
2794 c1669e2e 2019-01-09 stsp case 'R':
2795 c1669e2e 2019-01-09 stsp recurse = 1;
2796 c1669e2e 2019-01-09 stsp break;
2797 5de5890b 2018-10-18 stsp default:
2798 2deda0b9 2019-03-07 stsp usage_tree();
2799 5de5890b 2018-10-18 stsp /* NOTREACHED */
2800 5de5890b 2018-10-18 stsp }
2801 5de5890b 2018-10-18 stsp }
2802 5de5890b 2018-10-18 stsp
2803 5de5890b 2018-10-18 stsp argc -= optind;
2804 5de5890b 2018-10-18 stsp argv += optind;
2805 5de5890b 2018-10-18 stsp
2806 5de5890b 2018-10-18 stsp if (argc == 1)
2807 5de5890b 2018-10-18 stsp path = argv[0];
2808 5de5890b 2018-10-18 stsp else if (argc > 1)
2809 5de5890b 2018-10-18 stsp usage_tree();
2810 5de5890b 2018-10-18 stsp else
2811 7a2c19d6 2019-02-05 stsp path = NULL;
2812 7a2c19d6 2019-02-05 stsp
2813 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2814 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2815 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2816 9bf7a39b 2019-02-05 stsp goto done;
2817 9bf7a39b 2019-02-05 stsp }
2818 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2819 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2820 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2821 7a2c19d6 2019-02-05 stsp goto done;
2822 7a2c19d6 2019-02-05 stsp else
2823 7a2c19d6 2019-02-05 stsp error = NULL;
2824 7a2c19d6 2019-02-05 stsp if (worktree) {
2825 7a2c19d6 2019-02-05 stsp repo_path =
2826 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2827 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2828 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2829 7a2c19d6 2019-02-05 stsp if (error)
2830 7a2c19d6 2019-02-05 stsp goto done;
2831 7a2c19d6 2019-02-05 stsp } else {
2832 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2833 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2834 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2835 7a2c19d6 2019-02-05 stsp goto done;
2836 7a2c19d6 2019-02-05 stsp }
2837 5de5890b 2018-10-18 stsp }
2838 5de5890b 2018-10-18 stsp }
2839 5de5890b 2018-10-18 stsp
2840 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2841 5de5890b 2018-10-18 stsp if (error != NULL)
2842 c02c541e 2019-03-29 stsp goto done;
2843 c02c541e 2019-03-29 stsp
2844 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2845 c02c541e 2019-03-29 stsp if (error)
2846 5de5890b 2018-10-18 stsp goto done;
2847 5de5890b 2018-10-18 stsp
2848 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2849 9bf7a39b 2019-02-05 stsp if (worktree) {
2850 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2851 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2852 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2853 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2854 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2855 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2856 9bf7a39b 2019-02-05 stsp goto done;
2857 9bf7a39b 2019-02-05 stsp }
2858 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2859 9bf7a39b 2019-02-05 stsp free(p);
2860 9bf7a39b 2019-02-05 stsp if (error)
2861 9bf7a39b 2019-02-05 stsp goto done;
2862 9bf7a39b 2019-02-05 stsp } else
2863 9bf7a39b 2019-02-05 stsp path = "/";
2864 9bf7a39b 2019-02-05 stsp }
2865 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2866 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2867 9bf7a39b 2019-02-05 stsp if (error != NULL)
2868 9bf7a39b 2019-02-05 stsp goto done;
2869 9bf7a39b 2019-02-05 stsp }
2870 5de5890b 2018-10-18 stsp
2871 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2872 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2873 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2874 5de5890b 2018-10-18 stsp if (error != NULL)
2875 5de5890b 2018-10-18 stsp goto done;
2876 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2877 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2878 5de5890b 2018-10-18 stsp if (error != NULL)
2879 5de5890b 2018-10-18 stsp goto done;
2880 5de5890b 2018-10-18 stsp } else {
2881 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2882 30837e32 2019-07-25 stsp if (error)
2883 5de5890b 2018-10-18 stsp goto done;
2884 5de5890b 2018-10-18 stsp }
2885 5de5890b 2018-10-18 stsp
2886 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2887 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2888 5de5890b 2018-10-18 stsp done:
2889 5de5890b 2018-10-18 stsp free(in_repo_path);
2890 5de5890b 2018-10-18 stsp free(repo_path);
2891 5de5890b 2018-10-18 stsp free(cwd);
2892 5de5890b 2018-10-18 stsp free(commit_id);
2893 7a2c19d6 2019-02-05 stsp if (worktree)
2894 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2895 5de5890b 2018-10-18 stsp if (repo) {
2896 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2897 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2898 5de5890b 2018-10-18 stsp if (error == NULL)
2899 5de5890b 2018-10-18 stsp error = repo_error;
2900 5de5890b 2018-10-18 stsp }
2901 5de5890b 2018-10-18 stsp return error;
2902 5de5890b 2018-10-18 stsp }
2903 5de5890b 2018-10-18 stsp
2904 6bad629b 2019-02-04 stsp __dead static void
2905 6bad629b 2019-02-04 stsp usage_status(void)
2906 6bad629b 2019-02-04 stsp {
2907 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2908 6bad629b 2019-02-04 stsp exit(1);
2909 6bad629b 2019-02-04 stsp }
2910 5c860e29 2018-03-12 stsp
2911 b72f483a 2019-02-05 stsp static const struct got_error *
2912 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2913 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2914 537ac44b 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
2915 6bad629b 2019-02-04 stsp {
2916 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2917 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2918 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2919 b72f483a 2019-02-05 stsp return NULL;
2920 6bad629b 2019-02-04 stsp }
2921 5c860e29 2018-03-12 stsp
2922 6bad629b 2019-02-04 stsp static const struct got_error *
2923 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2924 6bad629b 2019-02-04 stsp {
2925 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2926 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2927 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2928 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2929 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2930 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2931 a5edda0a 2019-07-27 stsp int ch;
2932 5c860e29 2018-03-12 stsp
2933 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2934 72ea6654 2019-07-27 stsp
2935 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2936 6bad629b 2019-02-04 stsp switch (ch) {
2937 5c860e29 2018-03-12 stsp default:
2938 2deda0b9 2019-03-07 stsp usage_status();
2939 6bad629b 2019-02-04 stsp /* NOTREACHED */
2940 5c860e29 2018-03-12 stsp }
2941 5c860e29 2018-03-12 stsp }
2942 5c860e29 2018-03-12 stsp
2943 6bad629b 2019-02-04 stsp argc -= optind;
2944 6bad629b 2019-02-04 stsp argv += optind;
2945 5c860e29 2018-03-12 stsp
2946 6bad629b 2019-02-04 stsp #ifndef PROFILE
2947 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2948 6bad629b 2019-02-04 stsp NULL) == -1)
2949 6bad629b 2019-02-04 stsp err(1, "pledge");
2950 f42b1b34 2018-03-12 stsp #endif
2951 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
2952 927df6b7 2019-02-10 stsp if (cwd == NULL) {
2953 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2954 927df6b7 2019-02-10 stsp goto done;
2955 927df6b7 2019-02-10 stsp }
2956 927df6b7 2019-02-10 stsp
2957 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2958 927df6b7 2019-02-10 stsp if (error != NULL)
2959 a5edda0a 2019-07-27 stsp goto done;
2960 6bad629b 2019-02-04 stsp
2961 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
2962 c9956ddf 2019-09-08 stsp NULL);
2963 6bad629b 2019-02-04 stsp if (error != NULL)
2964 6bad629b 2019-02-04 stsp goto done;
2965 6bad629b 2019-02-04 stsp
2966 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2967 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
2968 087fb88c 2019-08-04 stsp if (error)
2969 087fb88c 2019-08-04 stsp goto done;
2970 087fb88c 2019-08-04 stsp
2971 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
2972 6bad629b 2019-02-04 stsp if (error)
2973 6bad629b 2019-02-04 stsp goto done;
2974 6bad629b 2019-02-04 stsp
2975 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
2976 6bad629b 2019-02-04 stsp check_cancelled, NULL);
2977 6bad629b 2019-02-04 stsp done:
2978 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
2979 a5edda0a 2019-07-27 stsp free((char *)pe->path);
2980 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2981 927df6b7 2019-02-10 stsp free(cwd);
2982 d0eebce4 2019-03-11 stsp return error;
2983 d0eebce4 2019-03-11 stsp }
2984 d0eebce4 2019-03-11 stsp
2985 d0eebce4 2019-03-11 stsp __dead static void
2986 d0eebce4 2019-03-11 stsp usage_ref(void)
2987 d0eebce4 2019-03-11 stsp {
2988 d0eebce4 2019-03-11 stsp fprintf(stderr,
2989 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
2990 d0eebce4 2019-03-11 stsp getprogname());
2991 d0eebce4 2019-03-11 stsp exit(1);
2992 d0eebce4 2019-03-11 stsp }
2993 d0eebce4 2019-03-11 stsp
2994 d0eebce4 2019-03-11 stsp static const struct got_error *
2995 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
2996 d0eebce4 2019-03-11 stsp {
2997 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
2998 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
2999 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3000 d0eebce4 2019-03-11 stsp
3001 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3002 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3003 d0eebce4 2019-03-11 stsp if (err)
3004 d0eebce4 2019-03-11 stsp return err;
3005 d0eebce4 2019-03-11 stsp
3006 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3007 d0eebce4 2019-03-11 stsp char *refstr;
3008 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3009 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3010 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3011 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3012 d0eebce4 2019-03-11 stsp free(refstr);
3013 d0eebce4 2019-03-11 stsp }
3014 d0eebce4 2019-03-11 stsp
3015 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3016 d0eebce4 2019-03-11 stsp return NULL;
3017 d0eebce4 2019-03-11 stsp }
3018 d0eebce4 2019-03-11 stsp
3019 d0eebce4 2019-03-11 stsp static const struct got_error *
3020 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3021 d0eebce4 2019-03-11 stsp {
3022 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3023 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3024 d0eebce4 2019-03-11 stsp
3025 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3026 d0eebce4 2019-03-11 stsp if (err)
3027 d0eebce4 2019-03-11 stsp return err;
3028 d0eebce4 2019-03-11 stsp
3029 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3030 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3031 d0eebce4 2019-03-11 stsp return err;
3032 d0eebce4 2019-03-11 stsp }
3033 d0eebce4 2019-03-11 stsp
3034 d0eebce4 2019-03-11 stsp static const struct got_error *
3035 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3036 d0eebce4 2019-03-11 stsp {
3037 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3038 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3039 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3040 d1644381 2019-07-14 stsp
3041 d1644381 2019-07-14 stsp /*
3042 d1644381 2019-07-14 stsp * Don't let the user create a reference named '-'.
3043 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3044 d1644381 2019-07-14 stsp * an unintended typo.
3045 d1644381 2019-07-14 stsp */
3046 d1644381 2019-07-14 stsp if (refname[0] == '-' && refname[1] == '\0')
3047 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3048 d0eebce4 2019-03-11 stsp
3049 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3050 dd88155e 2019-06-29 stsp repo);
3051 d83d9d5c 2019-05-13 stsp if (err) {
3052 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3053 d0eebce4 2019-03-11 stsp
3054 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3055 d83d9d5c 2019-05-13 stsp return err;
3056 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3057 d83d9d5c 2019-05-13 stsp if (err)
3058 d83d9d5c 2019-05-13 stsp return err;
3059 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3060 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3061 d83d9d5c 2019-05-13 stsp if (err)
3062 d83d9d5c 2019-05-13 stsp return err;
3063 d83d9d5c 2019-05-13 stsp }
3064 d83d9d5c 2019-05-13 stsp
3065 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3066 d0eebce4 2019-03-11 stsp if (err)
3067 d0eebce4 2019-03-11 stsp goto done;
3068 d0eebce4 2019-03-11 stsp
3069 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3070 d0eebce4 2019-03-11 stsp done:
3071 d0eebce4 2019-03-11 stsp if (ref)
3072 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3073 d0eebce4 2019-03-11 stsp free(id);
3074 d1c1ae5f 2019-08-12 stsp return err;
3075 d1c1ae5f 2019-08-12 stsp }
3076 d1c1ae5f 2019-08-12 stsp
3077 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3078 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3079 d1c1ae5f 2019-08-12 stsp {
3080 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3081 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3082 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3083 d1c1ae5f 2019-08-12 stsp
3084 d1c1ae5f 2019-08-12 stsp /*
3085 d1c1ae5f 2019-08-12 stsp * Don't let the user create a reference named '-'.
3086 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3087 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3088 d1c1ae5f 2019-08-12 stsp */
3089 d1c1ae5f 2019-08-12 stsp if (refname[0] == '-' && refname[1] == '\0')
3090 24b5452a 2019-10-09 stsp return got_error_path(refname, GOT_ERR_BAD_REF_NAME);
3091 d1c1ae5f 2019-08-12 stsp
3092 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3093 d1c1ae5f 2019-08-12 stsp if (err)
3094 d1c1ae5f 2019-08-12 stsp return err;
3095 d1c1ae5f 2019-08-12 stsp
3096 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3097 d1c1ae5f 2019-08-12 stsp if (err)
3098 d1c1ae5f 2019-08-12 stsp goto done;
3099 d1c1ae5f 2019-08-12 stsp
3100 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3101 d1c1ae5f 2019-08-12 stsp done:
3102 d1c1ae5f 2019-08-12 stsp if (target_ref)
3103 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3104 d1c1ae5f 2019-08-12 stsp if (ref)
3105 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3106 d0eebce4 2019-03-11 stsp return err;
3107 d0eebce4 2019-03-11 stsp }
3108 d0eebce4 2019-03-11 stsp
3109 d0eebce4 2019-03-11 stsp static const struct got_error *
3110 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3111 d0eebce4 2019-03-11 stsp {
3112 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3113 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3114 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3115 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3116 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3117 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3118 d0eebce4 2019-03-11 stsp
3119 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3120 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3121 d0eebce4 2019-03-11 stsp switch (ch) {
3122 d0eebce4 2019-03-11 stsp case 'd':
3123 d0eebce4 2019-03-11 stsp delref = optarg;
3124 d0eebce4 2019-03-11 stsp break;
3125 d0eebce4 2019-03-11 stsp case 'r':
3126 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3127 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3128 d0eebce4 2019-03-11 stsp err(1, "-r option");
3129 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3130 d0eebce4 2019-03-11 stsp break;
3131 d0eebce4 2019-03-11 stsp case 'l':
3132 d0eebce4 2019-03-11 stsp do_list = 1;
3133 d1c1ae5f 2019-08-12 stsp break;
3134 d1c1ae5f 2019-08-12 stsp case 's':
3135 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3136 d0eebce4 2019-03-11 stsp break;
3137 d0eebce4 2019-03-11 stsp default:
3138 d0eebce4 2019-03-11 stsp usage_ref();
3139 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3140 d0eebce4 2019-03-11 stsp }
3141 d0eebce4 2019-03-11 stsp }
3142 d0eebce4 2019-03-11 stsp
3143 d0eebce4 2019-03-11 stsp if (do_list && delref)
3144 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3145 d0eebce4 2019-03-11 stsp
3146 d0eebce4 2019-03-11 stsp argc -= optind;
3147 d0eebce4 2019-03-11 stsp argv += optind;
3148 d0eebce4 2019-03-11 stsp
3149 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3150 d1c1ae5f 2019-08-12 stsp if (create_symref)
3151 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3152 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3153 d0eebce4 2019-03-11 stsp if (argc > 0)
3154 d0eebce4 2019-03-11 stsp usage_ref();
3155 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3156 d0eebce4 2019-03-11 stsp usage_ref();
3157 d0eebce4 2019-03-11 stsp
3158 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3159 e0b57350 2019-03-12 stsp if (do_list) {
3160 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3161 e0b57350 2019-03-12 stsp NULL) == -1)
3162 e0b57350 2019-03-12 stsp err(1, "pledge");
3163 e0b57350 2019-03-12 stsp } else {
3164 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3165 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3166 e0b57350 2019-03-12 stsp err(1, "pledge");
3167 e0b57350 2019-03-12 stsp }
3168 d0eebce4 2019-03-11 stsp #endif
3169 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3170 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3171 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3172 d0eebce4 2019-03-11 stsp goto done;
3173 d0eebce4 2019-03-11 stsp }
3174 d0eebce4 2019-03-11 stsp
3175 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3176 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3177 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3178 d0eebce4 2019-03-11 stsp goto done;
3179 d0eebce4 2019-03-11 stsp else
3180 d0eebce4 2019-03-11 stsp error = NULL;
3181 d0eebce4 2019-03-11 stsp if (worktree) {
3182 d0eebce4 2019-03-11 stsp repo_path =
3183 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3184 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3185 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3186 d0eebce4 2019-03-11 stsp if (error)
3187 d0eebce4 2019-03-11 stsp goto done;
3188 d0eebce4 2019-03-11 stsp } else {
3189 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3190 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3191 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3192 d0eebce4 2019-03-11 stsp goto done;
3193 d0eebce4 2019-03-11 stsp }
3194 d0eebce4 2019-03-11 stsp }
3195 d0eebce4 2019-03-11 stsp }
3196 d0eebce4 2019-03-11 stsp
3197 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3198 d0eebce4 2019-03-11 stsp if (error != NULL)
3199 d0eebce4 2019-03-11 stsp goto done;
3200 d0eebce4 2019-03-11 stsp
3201 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3202 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3203 c02c541e 2019-03-29 stsp if (error)
3204 c02c541e 2019-03-29 stsp goto done;
3205 c02c541e 2019-03-29 stsp
3206 d0eebce4 2019-03-11 stsp if (do_list)
3207 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3208 d0eebce4 2019-03-11 stsp else if (delref)
3209 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3210 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3211 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3212 d0eebce4 2019-03-11 stsp else
3213 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3214 4e759de4 2019-06-26 stsp done:
3215 4e759de4 2019-06-26 stsp if (repo)
3216 4e759de4 2019-06-26 stsp got_repo_close(repo);
3217 4e759de4 2019-06-26 stsp if (worktree)
3218 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3219 4e759de4 2019-06-26 stsp free(cwd);
3220 4e759de4 2019-06-26 stsp free(repo_path);
3221 4e759de4 2019-06-26 stsp return error;
3222 4e759de4 2019-06-26 stsp }
3223 4e759de4 2019-06-26 stsp
3224 4e759de4 2019-06-26 stsp __dead static void
3225 4e759de4 2019-06-26 stsp usage_branch(void)
3226 4e759de4 2019-06-26 stsp {
3227 4e759de4 2019-06-26 stsp fprintf(stderr,
3228 ad89fa31 2019-10-04 stsp "usage: %s branch [-r repository] [-l] | -d name | "
3229 ad89fa31 2019-10-04 stsp "[name [commit]]\n", getprogname());
3230 4e759de4 2019-06-26 stsp exit(1);
3231 4e759de4 2019-06-26 stsp }
3232 4e759de4 2019-06-26 stsp
3233 4e759de4 2019-06-26 stsp static const struct got_error *
3234 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3235 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3236 4e99b47e 2019-10-04 stsp {
3237 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3238 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3239 4e99b47e 2019-10-04 stsp char *refstr;
3240 4e99b47e 2019-10-04 stsp
3241 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3242 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3243 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3244 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3245 4e99b47e 2019-10-04 stsp
3246 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3247 4e99b47e 2019-10-04 stsp if (err)
3248 4e99b47e 2019-10-04 stsp return err;
3249 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3250 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3251 4e99b47e 2019-10-04 stsp marker = "* ";
3252 4e99b47e 2019-10-04 stsp else
3253 4e99b47e 2019-10-04 stsp marker = "~ ";
3254 4e99b47e 2019-10-04 stsp free(id);
3255 4e99b47e 2019-10-04 stsp }
3256 4e99b47e 2019-10-04 stsp
3257 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3258 4e99b47e 2019-10-04 stsp refname += 11;
3259 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3260 4e99b47e 2019-10-04 stsp refname += 18;
3261 4e99b47e 2019-10-04 stsp
3262 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3263 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3264 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3265 4e99b47e 2019-10-04 stsp
3266 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3267 4e99b47e 2019-10-04 stsp free(refstr);
3268 4e99b47e 2019-10-04 stsp return NULL;
3269 4e99b47e 2019-10-04 stsp }
3270 4e99b47e 2019-10-04 stsp
3271 4e99b47e 2019-10-04 stsp static const struct got_error *
3272 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3273 ad89fa31 2019-10-04 stsp {
3274 ad89fa31 2019-10-04 stsp const char *refname;
3275 ad89fa31 2019-10-04 stsp
3276 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3277 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3278 ad89fa31 2019-10-04 stsp
3279 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3280 ad89fa31 2019-10-04 stsp
3281 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3282 ad89fa31 2019-10-04 stsp refname += 11;
3283 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3284 ad89fa31 2019-10-04 stsp refname += 18;
3285 ad89fa31 2019-10-04 stsp
3286 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3287 ad89fa31 2019-10-04 stsp
3288 ad89fa31 2019-10-04 stsp return NULL;
3289 ad89fa31 2019-10-04 stsp }
3290 ad89fa31 2019-10-04 stsp
3291 ad89fa31 2019-10-04 stsp static const struct got_error *
3292 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3293 4e759de4 2019-06-26 stsp {
3294 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3295 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3296 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3297 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3298 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3299 4e759de4 2019-06-26 stsp
3300 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3301 ba882ee3 2019-07-11 stsp
3302 4e99b47e 2019-10-04 stsp if (worktree) {
3303 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3304 4e99b47e 2019-10-04 stsp worktree);
3305 4e99b47e 2019-10-04 stsp if (err)
3306 4e99b47e 2019-10-04 stsp return err;
3307 4e99b47e 2019-10-04 stsp
3308 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3309 4e99b47e 2019-10-04 stsp worktree);
3310 4e99b47e 2019-10-04 stsp if (err)
3311 4e99b47e 2019-10-04 stsp return err;
3312 4e99b47e 2019-10-04 stsp
3313 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3314 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3315 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3316 4e99b47e 2019-10-04 stsp if (err)
3317 4e99b47e 2019-10-04 stsp return err;
3318 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3319 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3320 4e99b47e 2019-10-04 stsp }
3321 4e99b47e 2019-10-04 stsp }
3322 4e99b47e 2019-10-04 stsp
3323 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3324 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3325 4e759de4 2019-06-26 stsp if (err)
3326 4e759de4 2019-06-26 stsp return err;
3327 4e759de4 2019-06-26 stsp
3328 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3329 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3330 4e759de4 2019-06-26 stsp
3331 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3332 4e759de4 2019-06-26 stsp return NULL;
3333 4e759de4 2019-06-26 stsp }
3334 4e759de4 2019-06-26 stsp
3335 4e759de4 2019-06-26 stsp static const struct got_error *
3336 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3337 45cd4e47 2019-08-25 stsp const char *branch_name)
3338 4e759de4 2019-06-26 stsp {
3339 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3340 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3341 4e759de4 2019-06-26 stsp char *refname;
3342 4e759de4 2019-06-26 stsp
3343 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3344 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3345 4e759de4 2019-06-26 stsp
3346 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3347 4e759de4 2019-06-26 stsp if (err)
3348 4e759de4 2019-06-26 stsp goto done;
3349 4e759de4 2019-06-26 stsp
3350 45cd4e47 2019-08-25 stsp if (worktree &&
3351 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3352 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3353 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3354 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3355 45cd4e47 2019-08-25 stsp goto done;
3356 45cd4e47 2019-08-25 stsp }
3357 45cd4e47 2019-08-25 stsp
3358 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3359 4e759de4 2019-06-26 stsp done:
3360 45cd4e47 2019-08-25 stsp if (ref)
3361 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3362 4e759de4 2019-06-26 stsp free(refname);
3363 4e759de4 2019-06-26 stsp return err;
3364 4e759de4 2019-06-26 stsp }
3365 4e759de4 2019-06-26 stsp
3366 4e759de4 2019-06-26 stsp static const struct got_error *
3367 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3368 4e759de4 2019-06-26 stsp const char *base_branch)
3369 4e759de4 2019-06-26 stsp {
3370 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3371 4e759de4 2019-06-26 stsp struct got_object_id *id = NULL;
3372 a4f89d48 2019-08-25 stsp char *label;
3373 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3374 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3375 d3f84d51 2019-07-11 stsp
3376 d3f84d51 2019-07-11 stsp /*
3377 d3f84d51 2019-07-11 stsp * Don't let the user create a branch named '-'.
3378 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3379 d3f84d51 2019-07-11 stsp * an unintended typo.
3380 d3f84d51 2019-07-11 stsp */
3381 d3f84d51 2019-07-11 stsp if (branch_name[0] == '-' && branch_name[1] == '\0')
3382 24b5452a 2019-10-09 stsp return got_error_path(branch_name, GOT_ERR_BAD_REF_NAME);
3383 4e759de4 2019-06-26 stsp
3384 a4f89d48 2019-08-25 stsp err = match_object_id(&id, &label, base_branch,
3385 a4f89d48 2019-08-25 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3386 4e759de4 2019-06-26 stsp if (err)
3387 a4f89d48 2019-08-25 stsp return err;
3388 4e759de4 2019-06-26 stsp
3389 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3390 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3391 4e759de4 2019-06-26 stsp goto done;
3392 4e759de4 2019-06-26 stsp }
3393 4e759de4 2019-06-26 stsp
3394 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3395 4e759de4 2019-06-26 stsp if (err == NULL) {
3396 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3397 4e759de4 2019-06-26 stsp goto done;
3398 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3399 4e759de4 2019-06-26 stsp goto done;
3400 4e759de4 2019-06-26 stsp
3401 4e759de4 2019-06-26 stsp err = got_ref_alloc(&ref, refname, id);
3402 4e759de4 2019-06-26 stsp if (err)
3403 4e759de4 2019-06-26 stsp goto done;
3404 4e759de4 2019-06-26 stsp
3405 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3406 d0eebce4 2019-03-11 stsp done:
3407 4e759de4 2019-06-26 stsp if (ref)
3408 4e759de4 2019-06-26 stsp got_ref_close(ref);
3409 4e759de4 2019-06-26 stsp free(id);
3410 4e759de4 2019-06-26 stsp free(base_refname);
3411 4e759de4 2019-06-26 stsp free(refname);
3412 4e759de4 2019-06-26 stsp return err;
3413 4e759de4 2019-06-26 stsp }
3414 4e759de4 2019-06-26 stsp
3415 4e759de4 2019-06-26 stsp static const struct got_error *
3416 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3417 4e759de4 2019-06-26 stsp {
3418 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3419 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3420 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3421 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3422 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3423 4e759de4 2019-06-26 stsp const char *delref = NULL;
3424 4e759de4 2019-06-26 stsp
3425 4e759de4 2019-06-26 stsp while ((ch = getopt(argc, argv, "d:r:l")) != -1) {
3426 4e759de4 2019-06-26 stsp switch (ch) {
3427 4e759de4 2019-06-26 stsp case 'd':
3428 4e759de4 2019-06-26 stsp delref = optarg;
3429 4e759de4 2019-06-26 stsp break;
3430 4e759de4 2019-06-26 stsp case 'r':
3431 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3432 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3433 4e759de4 2019-06-26 stsp err(1, "-r option");
3434 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3435 4e759de4 2019-06-26 stsp break;
3436 4e759de4 2019-06-26 stsp case 'l':
3437 4e759de4 2019-06-26 stsp do_list = 1;
3438 4e759de4 2019-06-26 stsp break;
3439 4e759de4 2019-06-26 stsp default:
3440 4e759de4 2019-06-26 stsp usage_branch();
3441 4e759de4 2019-06-26 stsp /* NOTREACHED */
3442 4e759de4 2019-06-26 stsp }
3443 4e759de4 2019-06-26 stsp }
3444 4e759de4 2019-06-26 stsp
3445 4e759de4 2019-06-26 stsp if (do_list && delref)
3446 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3447 4e759de4 2019-06-26 stsp
3448 4e759de4 2019-06-26 stsp argc -= optind;
3449 4e759de4 2019-06-26 stsp argv += optind;
3450 ad89fa31 2019-10-04 stsp
3451 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3452 ad89fa31 2019-10-04 stsp do_show = 1;
3453 4e759de4 2019-06-26 stsp
3454 4e759de4 2019-06-26 stsp if (do_list || delref) {
3455 4e759de4 2019-06-26 stsp if (argc > 0)
3456 4e759de4 2019-06-26 stsp usage_branch();
3457 ad89fa31 2019-10-04 stsp } else if (!do_show && (argc < 1 || argc > 2))
3458 4e759de4 2019-06-26 stsp usage_branch();
3459 4e759de4 2019-06-26 stsp
3460 4e759de4 2019-06-26 stsp #ifndef PROFILE
3461 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3462 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3463 4e759de4 2019-06-26 stsp NULL) == -1)
3464 4e759de4 2019-06-26 stsp err(1, "pledge");
3465 4e759de4 2019-06-26 stsp } else {
3466 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3467 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3468 4e759de4 2019-06-26 stsp err(1, "pledge");
3469 4e759de4 2019-06-26 stsp }
3470 4e759de4 2019-06-26 stsp #endif
3471 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3472 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3473 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3474 4e759de4 2019-06-26 stsp goto done;
3475 4e759de4 2019-06-26 stsp }
3476 4e759de4 2019-06-26 stsp
3477 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3478 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3479 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3480 4e759de4 2019-06-26 stsp goto done;
3481 4e759de4 2019-06-26 stsp else
3482 4e759de4 2019-06-26 stsp error = NULL;
3483 4e759de4 2019-06-26 stsp if (worktree) {
3484 4e759de4 2019-06-26 stsp repo_path =
3485 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3486 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3487 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3488 4e759de4 2019-06-26 stsp if (error)
3489 4e759de4 2019-06-26 stsp goto done;
3490 4e759de4 2019-06-26 stsp } else {
3491 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3492 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3493 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3494 4e759de4 2019-06-26 stsp goto done;
3495 4e759de4 2019-06-26 stsp }
3496 4e759de4 2019-06-26 stsp }
3497 4e759de4 2019-06-26 stsp }
3498 4e759de4 2019-06-26 stsp
3499 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3500 4e759de4 2019-06-26 stsp if (error != NULL)
3501 4e759de4 2019-06-26 stsp goto done;
3502 4e759de4 2019-06-26 stsp
3503 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3504 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3505 4e759de4 2019-06-26 stsp if (error)
3506 4e759de4 2019-06-26 stsp goto done;
3507 4e759de4 2019-06-26 stsp
3508 ad89fa31 2019-10-04 stsp if (do_show)
3509 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3510 ad89fa31 2019-10-04 stsp else if (do_list)
3511 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3512 4e759de4 2019-06-26 stsp else if (delref)
3513 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3514 4e759de4 2019-06-26 stsp else {
3515 4e759de4 2019-06-26 stsp const char *base_branch;
3516 4e759de4 2019-06-26 stsp if (argc == 1) {
3517 4e759de4 2019-06-26 stsp base_branch = worktree ?
3518 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3519 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3520 dc5351b4 2019-07-30 stsp if (strncmp(base_branch, "refs/heads/", 11) == 0)
3521 dc5351b4 2019-07-30 stsp base_branch += 11;
3522 4e759de4 2019-06-26 stsp } else
3523 4e759de4 2019-06-26 stsp base_branch = argv[1];
3524 4e759de4 2019-06-26 stsp error = add_branch(repo, argv[0], base_branch);
3525 4e759de4 2019-06-26 stsp }
3526 4e759de4 2019-06-26 stsp done:
3527 d0eebce4 2019-03-11 stsp if (repo)
3528 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3529 d0eebce4 2019-03-11 stsp if (worktree)
3530 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3531 d0eebce4 2019-03-11 stsp free(cwd);
3532 d0eebce4 2019-03-11 stsp free(repo_path);
3533 d00136be 2019-03-26 stsp return error;
3534 d00136be 2019-03-26 stsp }
3535 d00136be 2019-03-26 stsp
3536 8e7bd50a 2019-08-22 stsp
3537 d00136be 2019-03-26 stsp __dead static void
3538 8e7bd50a 2019-08-22 stsp usage_tag(void)
3539 8e7bd50a 2019-08-22 stsp {
3540 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3541 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3542 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3543 8e7bd50a 2019-08-22 stsp exit(1);
3544 b8bad2ba 2019-08-23 stsp }
3545 b8bad2ba 2019-08-23 stsp
3546 b8bad2ba 2019-08-23 stsp #if 0
3547 b8bad2ba 2019-08-23 stsp static const struct got_error *
3548 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3549 b8bad2ba 2019-08-23 stsp {
3550 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3551 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3552 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3553 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3554 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3555 b8bad2ba 2019-08-23 stsp
3556 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3557 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3558 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3559 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3560 b8bad2ba 2019-08-23 stsp if (err)
3561 b8bad2ba 2019-08-23 stsp return err;
3562 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3563 b8bad2ba 2019-08-23 stsp continue;
3564 b8bad2ba 2019-08-23 stsp } else {
3565 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3566 b8bad2ba 2019-08-23 stsp if (err)
3567 b8bad2ba 2019-08-23 stsp break;
3568 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3569 b8bad2ba 2019-08-23 stsp free(re_id);
3570 b8bad2ba 2019-08-23 stsp if (err)
3571 b8bad2ba 2019-08-23 stsp break;
3572 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3573 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3574 b8bad2ba 2019-08-23 stsp }
3575 b8bad2ba 2019-08-23 stsp
3576 b8bad2ba 2019-08-23 stsp while (se) {
3577 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3578 b8bad2ba 2019-08-23 stsp if (err)
3579 b8bad2ba 2019-08-23 stsp break;
3580 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3581 b8bad2ba 2019-08-23 stsp free(se_id);
3582 b8bad2ba 2019-08-23 stsp if (err)
3583 b8bad2ba 2019-08-23 stsp break;
3584 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3585 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3586 b8bad2ba 2019-08-23 stsp
3587 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3588 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3589 b8bad2ba 2019-08-23 stsp if (err)
3590 b8bad2ba 2019-08-23 stsp return err;
3591 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3592 b8bad2ba 2019-08-23 stsp break;
3593 b8bad2ba 2019-08-23 stsp }
3594 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3595 b8bad2ba 2019-08-23 stsp continue;
3596 b8bad2ba 2019-08-23 stsp }
3597 b8bad2ba 2019-08-23 stsp }
3598 b8bad2ba 2019-08-23 stsp done:
3599 b8bad2ba 2019-08-23 stsp return err;
3600 8e7bd50a 2019-08-22 stsp }
3601 b8bad2ba 2019-08-23 stsp #endif
3602 8e7bd50a 2019-08-22 stsp
3603 8e7bd50a 2019-08-22 stsp static const struct got_error *
3604 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3605 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3606 b8bad2ba 2019-08-23 stsp {
3607 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3608 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3609 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3610 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3611 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3612 b8bad2ba 2019-08-23 stsp
3613 b8bad2ba 2019-08-23 stsp *cmp = 0;
3614 b8bad2ba 2019-08-23 stsp
3615 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3616 b8bad2ba 2019-08-23 stsp if (err)
3617 b8bad2ba 2019-08-23 stsp return err;
3618 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3619 b8bad2ba 2019-08-23 stsp if (err)
3620 b8bad2ba 2019-08-23 stsp goto done;
3621 b8bad2ba 2019-08-23 stsp
3622 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3623 b8bad2ba 2019-08-23 stsp if (err)
3624 b8bad2ba 2019-08-23 stsp goto done;
3625 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3626 b8bad2ba 2019-08-23 stsp if (err)
3627 b8bad2ba 2019-08-23 stsp goto done;
3628 b8bad2ba 2019-08-23 stsp
3629 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3630 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3631 b8bad2ba 2019-08-23 stsp
3632 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3633 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3634 b8bad2ba 2019-08-23 stsp *cmp = 1;
3635 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3636 b8bad2ba 2019-08-23 stsp *cmp = -1;
3637 b8bad2ba 2019-08-23 stsp else
3638 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3639 b8bad2ba 2019-08-23 stsp done:
3640 b8bad2ba 2019-08-23 stsp free(id1);
3641 b8bad2ba 2019-08-23 stsp free(id2);
3642 b8bad2ba 2019-08-23 stsp if (tag1)
3643 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3644 b8bad2ba 2019-08-23 stsp if (tag2)
3645 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3646 b8bad2ba 2019-08-23 stsp return err;
3647 b8bad2ba 2019-08-23 stsp }
3648 b8bad2ba 2019-08-23 stsp
3649 b8bad2ba 2019-08-23 stsp static const struct got_error *
3650 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3651 8e7bd50a 2019-08-22 stsp {
3652 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3653 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3654 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3655 8e7bd50a 2019-08-22 stsp
3656 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3657 8e7bd50a 2019-08-22 stsp
3658 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3659 8e7bd50a 2019-08-22 stsp if (err)
3660 8e7bd50a 2019-08-22 stsp return err;
3661 8e7bd50a 2019-08-22 stsp
3662 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3663 8e7bd50a 2019-08-22 stsp const char *refname;
3664 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3665 8e7bd50a 2019-08-22 stsp char datebuf[26];
3666 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3667 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3668 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3669 8e7bd50a 2019-08-22 stsp
3670 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3671 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3672 8e7bd50a 2019-08-22 stsp continue;
3673 8e7bd50a 2019-08-22 stsp refname += 10;
3674 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3675 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3676 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3677 8e7bd50a 2019-08-22 stsp break;
3678 8e7bd50a 2019-08-22 stsp }
3679 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3680 8e7bd50a 2019-08-22 stsp free(refstr);
3681 8e7bd50a 2019-08-22 stsp
3682 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3683 8e7bd50a 2019-08-22 stsp if (err)
3684 8e7bd50a 2019-08-22 stsp break;
3685 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3686 8e7bd50a 2019-08-22 stsp free(id);
3687 8e7bd50a 2019-08-22 stsp if (err)
3688 8e7bd50a 2019-08-22 stsp break;
3689 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3690 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3691 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3692 2417344c 2019-08-23 stsp if (datestr)
3693 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3694 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3695 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3696 8e7bd50a 2019-08-22 stsp if (err)
3697 8e7bd50a 2019-08-22 stsp break;
3698 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3699 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3700 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3701 8e7bd50a 2019-08-22 stsp break;
3702 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3703 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3704 8e7bd50a 2019-08-22 stsp break;
3705 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3706 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3707 8e7bd50a 2019-08-22 stsp break;
3708 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3709 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3710 8e7bd50a 2019-08-22 stsp break;
3711 8e7bd50a 2019-08-22 stsp default:
3712 8e7bd50a 2019-08-22 stsp break;
3713 8e7bd50a 2019-08-22 stsp }
3714 8e7bd50a 2019-08-22 stsp free(id_str);
3715 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3716 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3717 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3718 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3719 8e7bd50a 2019-08-22 stsp break;
3720 8e7bd50a 2019-08-22 stsp }
3721 8e7bd50a 2019-08-22 stsp
3722 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3723 8e7bd50a 2019-08-22 stsp do {
3724 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3725 8e7bd50a 2019-08-22 stsp if (line)
3726 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3727 8e7bd50a 2019-08-22 stsp } while (line);
3728 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3729 8e7bd50a 2019-08-22 stsp }
3730 8e7bd50a 2019-08-22 stsp
3731 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3732 8e7bd50a 2019-08-22 stsp return NULL;
3733 8e7bd50a 2019-08-22 stsp }
3734 8e7bd50a 2019-08-22 stsp
3735 8e7bd50a 2019-08-22 stsp static const struct got_error *
3736 62870f63 2019-08-22 stsp get_tag_message(char **tagmsg, const char *commit_id_str,
3737 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3738 8e7bd50a 2019-08-22 stsp {
3739 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3740 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3741 8e7bd50a 2019-08-22 stsp char *tagmsg_path = NULL, *editor = NULL;
3742 8e7bd50a 2019-08-22 stsp int fd = -1;
3743 8e7bd50a 2019-08-22 stsp
3744 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3745 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3746 8e7bd50a 2019-08-22 stsp goto done;
3747 8e7bd50a 2019-08-22 stsp }
3748 8e7bd50a 2019-08-22 stsp
3749 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3750 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3751 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3752 8e7bd50a 2019-08-22 stsp goto done;
3753 8e7bd50a 2019-08-22 stsp }
3754 8e7bd50a 2019-08-22 stsp
3755 8e7bd50a 2019-08-22 stsp err = got_opentemp_named_fd(&tagmsg_path, &fd, template);
3756 8e7bd50a 2019-08-22 stsp if (err)
3757 8e7bd50a 2019-08-22 stsp goto done;
3758 8e7bd50a 2019-08-22 stsp
3759 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3760 8e7bd50a 2019-08-22 stsp close(fd);
3761 8e7bd50a 2019-08-22 stsp
3762 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3763 8e7bd50a 2019-08-22 stsp if (err)
3764 8e7bd50a 2019-08-22 stsp goto done;
3765 8e7bd50a 2019-08-22 stsp err = edit_logmsg(tagmsg, editor, tagmsg_path, initial_content);
3766 8e7bd50a 2019-08-22 stsp done:
3767 8e7bd50a 2019-08-22 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
3768 8e7bd50a 2019-08-22 stsp unlink(tagmsg_path);
3769 8e7bd50a 2019-08-22 stsp free(tagmsg_path);
3770 8e7bd50a 2019-08-22 stsp tagmsg_path = NULL;
3771 8e7bd50a 2019-08-22 stsp }
3772 8e7bd50a 2019-08-22 stsp free(initial_content);
3773 8e7bd50a 2019-08-22 stsp free(template);
3774 8e7bd50a 2019-08-22 stsp free(editor);
3775 8e7bd50a 2019-08-22 stsp
3776 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3777 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3778 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3779 8e7bd50a 2019-08-22 stsp if (err) {
3780 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3781 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3782 8e7bd50a 2019-08-22 stsp }
3783 8e7bd50a 2019-08-22 stsp }
3784 8e7bd50a 2019-08-22 stsp return err;
3785 8e7bd50a 2019-08-22 stsp }
3786 8e7bd50a 2019-08-22 stsp
3787 8e7bd50a 2019-08-22 stsp static const struct got_error *
3788 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3789 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3790 8e7bd50a 2019-08-22 stsp {
3791 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3792 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3793 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3794 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3795 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3796 8e7bd50a 2019-08-22 stsp
3797 8e7bd50a 2019-08-22 stsp /*
3798 8e7bd50a 2019-08-22 stsp * Don't let the user create a tag named '-'.
3799 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3800 8e7bd50a 2019-08-22 stsp * an unintended typo.
3801 8e7bd50a 2019-08-22 stsp */
3802 8e7bd50a 2019-08-22 stsp if (tag_name[0] == '-' && tag_name[1] == '\0')
3803 24b5452a 2019-10-09 stsp return got_error_path(tag_name, GOT_ERR_BAD_REF_NAME);
3804 8e7bd50a 2019-08-22 stsp
3805 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3806 8e7bd50a 2019-08-22 stsp if (err)
3807 8e7bd50a 2019-08-22 stsp return err;
3808 8e7bd50a 2019-08-22 stsp
3809 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3810 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3811 8e7bd50a 2019-08-22 stsp if (err)
3812 8e7bd50a 2019-08-22 stsp goto done;
3813 8e7bd50a 2019-08-22 stsp
3814 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3815 8e7bd50a 2019-08-22 stsp if (err)
3816 8e7bd50a 2019-08-22 stsp goto done;
3817 8e7bd50a 2019-08-22 stsp
3818 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3819 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3820 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3821 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3822 8e7bd50a 2019-08-22 stsp goto done;
3823 8e7bd50a 2019-08-22 stsp }
3824 8e7bd50a 2019-08-22 stsp tag_name += 10;
3825 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3826 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3827 8e7bd50a 2019-08-22 stsp goto done;
3828 8e7bd50a 2019-08-22 stsp }
3829 8e7bd50a 2019-08-22 stsp
3830 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3831 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3832 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3833 8e7bd50a 2019-08-22 stsp goto done;
3834 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3835 8e7bd50a 2019-08-22 stsp goto done;
3836 8e7bd50a 2019-08-22 stsp
3837 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3838 8e7bd50a 2019-08-22 stsp err = get_tag_message(&tagmsg, commit_id_str,
3839 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3840 8e7bd50a 2019-08-22 stsp if (err)
3841 8e7bd50a 2019-08-22 stsp goto done;
3842 8e7bd50a 2019-08-22 stsp }
3843 8e7bd50a 2019-08-22 stsp
3844 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3845 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3846 8e7bd50a 2019-08-22 stsp if (err)
3847 8e7bd50a 2019-08-22 stsp goto done;
3848 8e7bd50a 2019-08-22 stsp
3849 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3850 8e7bd50a 2019-08-22 stsp if (err)
3851 8e7bd50a 2019-08-22 stsp goto done;
3852 8e7bd50a 2019-08-22 stsp
3853 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3854 8e7bd50a 2019-08-22 stsp
3855 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3856 8e7bd50a 2019-08-22 stsp char *tag_id_str;
3857 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&tag_id_str, tag_id);
3858 8e7bd50a 2019-08-22 stsp printf("Created tag %s\n", tag_id_str);
3859 8e7bd50a 2019-08-22 stsp free(tag_id_str);
3860 8e7bd50a 2019-08-22 stsp }
3861 8e7bd50a 2019-08-22 stsp done:
3862 8e7bd50a 2019-08-22 stsp if (ref)
3863 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3864 8e7bd50a 2019-08-22 stsp free(commit_id);
3865 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3866 8e7bd50a 2019-08-22 stsp free(refname);
3867 8e7bd50a 2019-08-22 stsp free(tagmsg);
3868 aba9c984 2019-09-08 stsp free(tagger);
3869 8e7bd50a 2019-08-22 stsp return err;
3870 8e7bd50a 2019-08-22 stsp }
3871 8e7bd50a 2019-08-22 stsp
3872 8e7bd50a 2019-08-22 stsp static const struct got_error *
3873 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3874 8e7bd50a 2019-08-22 stsp {
3875 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3876 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3877 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3878 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3879 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
3880 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3881 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3882 8e7bd50a 2019-08-22 stsp
3883 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3884 8e7bd50a 2019-08-22 stsp switch (ch) {
3885 8e7bd50a 2019-08-22 stsp case 'm':
3886 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3887 8e7bd50a 2019-08-22 stsp break;
3888 8e7bd50a 2019-08-22 stsp case 'r':
3889 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3890 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3891 8e7bd50a 2019-08-22 stsp err(1, "-r option");
3892 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3893 8e7bd50a 2019-08-22 stsp break;
3894 8e7bd50a 2019-08-22 stsp case 'l':
3895 8e7bd50a 2019-08-22 stsp do_list = 1;
3896 8e7bd50a 2019-08-22 stsp break;
3897 8e7bd50a 2019-08-22 stsp default:
3898 8e7bd50a 2019-08-22 stsp usage_tag();
3899 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3900 8e7bd50a 2019-08-22 stsp }
3901 8e7bd50a 2019-08-22 stsp }
3902 8e7bd50a 2019-08-22 stsp
3903 8e7bd50a 2019-08-22 stsp argc -= optind;
3904 8e7bd50a 2019-08-22 stsp argv += optind;
3905 8e7bd50a 2019-08-22 stsp
3906 8e7bd50a 2019-08-22 stsp if (do_list) {
3907 8e7bd50a 2019-08-22 stsp if (tagmsg)
3908 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3909 8e7bd50a 2019-08-22 stsp if (argc > 0)
3910 8e7bd50a 2019-08-22 stsp usage_tag();
3911 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3912 8e7bd50a 2019-08-22 stsp usage_tag();
3913 a2887370 2019-08-23 stsp else if (argc > 1)
3914 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3915 a2887370 2019-08-23 stsp tag_name = argv[0];
3916 8e7bd50a 2019-08-22 stsp
3917 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3918 8e7bd50a 2019-08-22 stsp if (do_list) {
3919 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3920 8e7bd50a 2019-08-22 stsp NULL) == -1)
3921 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3922 8e7bd50a 2019-08-22 stsp } else {
3923 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3924 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3925 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3926 8e7bd50a 2019-08-22 stsp }
3927 8e7bd50a 2019-08-22 stsp #endif
3928 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
3929 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
3930 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
3931 8e7bd50a 2019-08-22 stsp goto done;
3932 8e7bd50a 2019-08-22 stsp }
3933 8e7bd50a 2019-08-22 stsp
3934 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3935 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
3936 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3937 8e7bd50a 2019-08-22 stsp goto done;
3938 8e7bd50a 2019-08-22 stsp else
3939 8e7bd50a 2019-08-22 stsp error = NULL;
3940 8e7bd50a 2019-08-22 stsp if (worktree) {
3941 8e7bd50a 2019-08-22 stsp repo_path =
3942 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
3943 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3944 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3945 8e7bd50a 2019-08-22 stsp if (error)
3946 8e7bd50a 2019-08-22 stsp goto done;
3947 8e7bd50a 2019-08-22 stsp } else {
3948 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
3949 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
3950 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
3951 8e7bd50a 2019-08-22 stsp goto done;
3952 8e7bd50a 2019-08-22 stsp }
3953 8e7bd50a 2019-08-22 stsp }
3954 8e7bd50a 2019-08-22 stsp }
3955 8e7bd50a 2019-08-22 stsp
3956 8e7bd50a 2019-08-22 stsp if (do_list) {
3957 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3958 c9956ddf 2019-09-08 stsp if (error != NULL)
3959 c9956ddf 2019-09-08 stsp goto done;
3960 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3961 8e7bd50a 2019-08-22 stsp if (error)
3962 8e7bd50a 2019-08-22 stsp goto done;
3963 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
3964 8e7bd50a 2019-08-22 stsp } else {
3965 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
3966 c9956ddf 2019-09-08 stsp if (error)
3967 c9956ddf 2019-09-08 stsp goto done;
3968 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
3969 c9956ddf 2019-09-08 stsp if (error != NULL)
3970 c9956ddf 2019-09-08 stsp goto done;
3971 c9956ddf 2019-09-08 stsp
3972 8e7bd50a 2019-08-22 stsp if (tagmsg) {
3973 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
3974 8e7bd50a 2019-08-22 stsp if (error)
3975 8e7bd50a 2019-08-22 stsp goto done;
3976 8e7bd50a 2019-08-22 stsp }
3977 8e7bd50a 2019-08-22 stsp
3978 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
3979 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
3980 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
3981 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
3982 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
3983 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
3984 8e7bd50a 2019-08-22 stsp if (error)
3985 8e7bd50a 2019-08-22 stsp goto done;
3986 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3987 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
3988 8e7bd50a 2019-08-22 stsp if (error)
3989 8e7bd50a 2019-08-22 stsp goto done;
3990 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
3991 8e7bd50a 2019-08-22 stsp free(commit_id);
3992 8e7bd50a 2019-08-22 stsp if (error)
3993 8e7bd50a 2019-08-22 stsp goto done;
3994 8e7bd50a 2019-08-22 stsp }
3995 8e7bd50a 2019-08-22 stsp
3996 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
3997 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
3998 8e7bd50a 2019-08-22 stsp }
3999 8e7bd50a 2019-08-22 stsp done:
4000 8e7bd50a 2019-08-22 stsp if (repo)
4001 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4002 8e7bd50a 2019-08-22 stsp if (worktree)
4003 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4004 8e7bd50a 2019-08-22 stsp free(cwd);
4005 8e7bd50a 2019-08-22 stsp free(repo_path);
4006 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4007 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4008 8e7bd50a 2019-08-22 stsp return error;
4009 8e7bd50a 2019-08-22 stsp }
4010 8e7bd50a 2019-08-22 stsp
4011 8e7bd50a 2019-08-22 stsp __dead static void
4012 d00136be 2019-03-26 stsp usage_add(void)
4013 d00136be 2019-03-26 stsp {
4014 fbb7e5c7 2019-05-11 stsp fprintf(stderr, "usage: %s add file-path ...\n", getprogname());
4015 d00136be 2019-03-26 stsp exit(1);
4016 d00136be 2019-03-26 stsp }
4017 d00136be 2019-03-26 stsp
4018 d00136be 2019-03-26 stsp static const struct got_error *
4019 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4020 d00136be 2019-03-26 stsp {
4021 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4022 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4023 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4024 1dd54920 2019-05-11 stsp char *cwd = NULL;
4025 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4026 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4027 6d022e97 2019-08-04 stsp int ch;
4028 1dd54920 2019-05-11 stsp
4029 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4030 d00136be 2019-03-26 stsp
4031 d00136be 2019-03-26 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4032 d00136be 2019-03-26 stsp switch (ch) {
4033 d00136be 2019-03-26 stsp default:
4034 d00136be 2019-03-26 stsp usage_add();
4035 d00136be 2019-03-26 stsp /* NOTREACHED */
4036 d00136be 2019-03-26 stsp }
4037 d00136be 2019-03-26 stsp }
4038 d00136be 2019-03-26 stsp
4039 d00136be 2019-03-26 stsp argc -= optind;
4040 d00136be 2019-03-26 stsp argv += optind;
4041 d00136be 2019-03-26 stsp
4042 43012d58 2019-07-14 stsp #ifndef PROFILE
4043 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4044 43012d58 2019-07-14 stsp NULL) == -1)
4045 43012d58 2019-07-14 stsp err(1, "pledge");
4046 43012d58 2019-07-14 stsp #endif
4047 723c305c 2019-05-11 jcs if (argc < 1)
4048 d00136be 2019-03-26 stsp usage_add();
4049 d00136be 2019-03-26 stsp
4050 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4051 d00136be 2019-03-26 stsp if (cwd == NULL) {
4052 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4053 d00136be 2019-03-26 stsp goto done;
4054 d00136be 2019-03-26 stsp }
4055 723c305c 2019-05-11 jcs
4056 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4057 d00136be 2019-03-26 stsp if (error)
4058 d00136be 2019-03-26 stsp goto done;
4059 d00136be 2019-03-26 stsp
4060 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4061 c9956ddf 2019-09-08 stsp NULL);
4062 031a5338 2019-03-26 stsp if (error != NULL)
4063 031a5338 2019-03-26 stsp goto done;
4064 031a5338 2019-03-26 stsp
4065 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4066 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4067 d00136be 2019-03-26 stsp if (error)
4068 d00136be 2019-03-26 stsp goto done;
4069 d00136be 2019-03-26 stsp
4070 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4071 6d022e97 2019-08-04 stsp if (error)
4072 6d022e97 2019-08-04 stsp goto done;
4073 723c305c 2019-05-11 jcs
4074 1dd54920 2019-05-11 stsp error = got_worktree_schedule_add(worktree, &paths, print_status,
4075 1dd54920 2019-05-11 stsp NULL, repo);
4076 d00136be 2019-03-26 stsp done:
4077 031a5338 2019-03-26 stsp if (repo)
4078 031a5338 2019-03-26 stsp got_repo_close(repo);
4079 d00136be 2019-03-26 stsp if (worktree)
4080 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4081 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4082 1dd54920 2019-05-11 stsp free((char *)pe->path);
4083 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4084 2ec1f75b 2019-03-26 stsp free(cwd);
4085 2ec1f75b 2019-03-26 stsp return error;
4086 2ec1f75b 2019-03-26 stsp }
4087 2ec1f75b 2019-03-26 stsp
4088 2ec1f75b 2019-03-26 stsp __dead static void
4089 648e4ef7 2019-07-09 stsp usage_remove(void)
4090 2ec1f75b 2019-03-26 stsp {
4091 648e4ef7 2019-07-09 stsp fprintf(stderr, "usage: %s remove [-f] file-path ...\n", getprogname());
4092 2ec1f75b 2019-03-26 stsp exit(1);
4093 2a06fe5f 2019-08-24 stsp }
4094 2a06fe5f 2019-08-24 stsp
4095 2a06fe5f 2019-08-24 stsp static const struct got_error *
4096 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4097 2a06fe5f 2019-08-24 stsp unsigned char staged_status, const char *path,
4098 2a06fe5f 2019-08-24 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
4099 2a06fe5f 2019-08-24 stsp struct got_object_id *commit_id)
4100 2a06fe5f 2019-08-24 stsp {
4101 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4102 2a06fe5f 2019-08-24 stsp return NULL;
4103 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4104 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4105 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4106 2a06fe5f 2019-08-24 stsp return NULL;
4107 2ec1f75b 2019-03-26 stsp }
4108 2ec1f75b 2019-03-26 stsp
4109 2ec1f75b 2019-03-26 stsp static const struct got_error *
4110 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4111 2ec1f75b 2019-03-26 stsp {
4112 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4113 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4114 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4115 17ed4618 2019-06-02 stsp char *cwd = NULL;
4116 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4117 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4118 6d022e97 2019-08-04 stsp int ch, delete_local_mods = 0;
4119 2ec1f75b 2019-03-26 stsp
4120 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4121 17ed4618 2019-06-02 stsp
4122 2ec1f75b 2019-03-26 stsp while ((ch = getopt(argc, argv, "f")) != -1) {
4123 2ec1f75b 2019-03-26 stsp switch (ch) {
4124 2ec1f75b 2019-03-26 stsp case 'f':
4125 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4126 2ec1f75b 2019-03-26 stsp break;
4127 2ec1f75b 2019-03-26 stsp default:
4128 2ec1f75b 2019-03-26 stsp usage_add();
4129 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4130 2ec1f75b 2019-03-26 stsp }
4131 2ec1f75b 2019-03-26 stsp }
4132 2ec1f75b 2019-03-26 stsp
4133 2ec1f75b 2019-03-26 stsp argc -= optind;
4134 2ec1f75b 2019-03-26 stsp argv += optind;
4135 2ec1f75b 2019-03-26 stsp
4136 43012d58 2019-07-14 stsp #ifndef PROFILE
4137 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4138 43012d58 2019-07-14 stsp NULL) == -1)
4139 43012d58 2019-07-14 stsp err(1, "pledge");
4140 43012d58 2019-07-14 stsp #endif
4141 17ed4618 2019-06-02 stsp if (argc < 1)
4142 648e4ef7 2019-07-09 stsp usage_remove();
4143 2ec1f75b 2019-03-26 stsp
4144 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4145 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4146 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4147 2ec1f75b 2019-03-26 stsp goto done;
4148 2ec1f75b 2019-03-26 stsp }
4149 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4150 2ec1f75b 2019-03-26 stsp if (error)
4151 2ec1f75b 2019-03-26 stsp goto done;
4152 2ec1f75b 2019-03-26 stsp
4153 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4154 c9956ddf 2019-09-08 stsp NULL);
4155 2af4a041 2019-05-11 jcs if (error)
4156 2ec1f75b 2019-03-26 stsp goto done;
4157 2ec1f75b 2019-03-26 stsp
4158 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4159 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4160 2ec1f75b 2019-03-26 stsp if (error)
4161 2ec1f75b 2019-03-26 stsp goto done;
4162 2ec1f75b 2019-03-26 stsp
4163 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4164 6d022e97 2019-08-04 stsp if (error)
4165 6d022e97 2019-08-04 stsp goto done;
4166 17ed4618 2019-06-02 stsp
4167 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4168 2a06fe5f 2019-08-24 stsp delete_local_mods, print_remove_status, NULL, repo);
4169 a129376b 2019-03-28 stsp if (error)
4170 a129376b 2019-03-28 stsp goto done;
4171 a129376b 2019-03-28 stsp done:
4172 a129376b 2019-03-28 stsp if (repo)
4173 a129376b 2019-03-28 stsp got_repo_close(repo);
4174 a129376b 2019-03-28 stsp if (worktree)
4175 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4176 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4177 17ed4618 2019-06-02 stsp free((char *)pe->path);
4178 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4179 a129376b 2019-03-28 stsp free(cwd);
4180 a129376b 2019-03-28 stsp return error;
4181 a129376b 2019-03-28 stsp }
4182 a129376b 2019-03-28 stsp
4183 a129376b 2019-03-28 stsp __dead static void
4184 a129376b 2019-03-28 stsp usage_revert(void)
4185 a129376b 2019-03-28 stsp {
4186 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4187 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4188 a129376b 2019-03-28 stsp exit(1);
4189 a129376b 2019-03-28 stsp }
4190 a129376b 2019-03-28 stsp
4191 1ee397ad 2019-07-12 stsp static const struct got_error *
4192 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4193 a129376b 2019-03-28 stsp {
4194 a129376b 2019-03-28 stsp while (path[0] == '/')
4195 a129376b 2019-03-28 stsp path++;
4196 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4197 33aa809d 2019-08-08 stsp return NULL;
4198 33aa809d 2019-08-08 stsp }
4199 33aa809d 2019-08-08 stsp
4200 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4201 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4202 33aa809d 2019-08-08 stsp const char *action;
4203 33aa809d 2019-08-08 stsp };
4204 33aa809d 2019-08-08 stsp
4205 33aa809d 2019-08-08 stsp static const struct got_error *
4206 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4207 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4208 33aa809d 2019-08-08 stsp {
4209 33aa809d 2019-08-08 stsp char *line = NULL;
4210 33aa809d 2019-08-08 stsp size_t linesize = 0;
4211 33aa809d 2019-08-08 stsp ssize_t linelen;
4212 33aa809d 2019-08-08 stsp
4213 33aa809d 2019-08-08 stsp switch (status) {
4214 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4215 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4216 33aa809d 2019-08-08 stsp break;
4217 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4218 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4219 33aa809d 2019-08-08 stsp break;
4220 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4221 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4222 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4223 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4224 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4225 33aa809d 2019-08-08 stsp printf("%s", line);
4226 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4227 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4228 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4229 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4230 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4231 33aa809d 2019-08-08 stsp break;
4232 33aa809d 2019-08-08 stsp default:
4233 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4234 33aa809d 2019-08-08 stsp }
4235 33aa809d 2019-08-08 stsp
4236 33aa809d 2019-08-08 stsp return NULL;
4237 33aa809d 2019-08-08 stsp }
4238 33aa809d 2019-08-08 stsp
4239 33aa809d 2019-08-08 stsp static const struct got_error *
4240 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4241 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4242 33aa809d 2019-08-08 stsp {
4243 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4244 33aa809d 2019-08-08 stsp char *line = NULL;
4245 33aa809d 2019-08-08 stsp size_t linesize = 0;
4246 33aa809d 2019-08-08 stsp ssize_t linelen;
4247 33aa809d 2019-08-08 stsp int resp = ' ';
4248 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4249 33aa809d 2019-08-08 stsp
4250 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4251 33aa809d 2019-08-08 stsp
4252 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4253 33aa809d 2019-08-08 stsp char *nl;
4254 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4255 33aa809d 2019-08-08 stsp a->action);
4256 33aa809d 2019-08-08 stsp if (err)
4257 33aa809d 2019-08-08 stsp return err;
4258 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4259 33aa809d 2019-08-08 stsp if (linelen == -1) {
4260 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4261 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4262 33aa809d 2019-08-08 stsp return NULL;
4263 33aa809d 2019-08-08 stsp }
4264 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4265 33aa809d 2019-08-08 stsp if (nl)
4266 33aa809d 2019-08-08 stsp *nl = '\0';
4267 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4268 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4269 33aa809d 2019-08-08 stsp printf("y\n");
4270 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4271 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4272 33aa809d 2019-08-08 stsp printf("n\n");
4273 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4274 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4275 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4276 33aa809d 2019-08-08 stsp printf("q\n");
4277 33aa809d 2019-08-08 stsp } else
4278 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4279 33aa809d 2019-08-08 stsp free(line);
4280 33aa809d 2019-08-08 stsp return NULL;
4281 33aa809d 2019-08-08 stsp }
4282 33aa809d 2019-08-08 stsp
4283 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4284 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4285 33aa809d 2019-08-08 stsp a->action);
4286 33aa809d 2019-08-08 stsp if (err)
4287 33aa809d 2019-08-08 stsp return err;
4288 33aa809d 2019-08-08 stsp resp = getchar();
4289 33aa809d 2019-08-08 stsp if (resp == '\n')
4290 33aa809d 2019-08-08 stsp resp = getchar();
4291 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4292 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4293 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4294 33aa809d 2019-08-08 stsp resp = ' ';
4295 33aa809d 2019-08-08 stsp }
4296 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4297 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4298 33aa809d 2019-08-08 stsp resp = ' ';
4299 33aa809d 2019-08-08 stsp }
4300 33aa809d 2019-08-08 stsp }
4301 33aa809d 2019-08-08 stsp
4302 33aa809d 2019-08-08 stsp if (resp == 'y')
4303 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4304 33aa809d 2019-08-08 stsp else if (resp == 'n')
4305 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4306 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4307 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4308 33aa809d 2019-08-08 stsp
4309 1ee397ad 2019-07-12 stsp return NULL;
4310 a129376b 2019-03-28 stsp }
4311 a129376b 2019-03-28 stsp
4312 33aa809d 2019-08-08 stsp
4313 a129376b 2019-03-28 stsp static const struct got_error *
4314 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4315 a129376b 2019-03-28 stsp {
4316 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4317 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4318 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4319 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4320 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4321 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4322 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4323 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4324 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4325 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4326 a129376b 2019-03-28 stsp
4327 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4328 e20a8b6f 2019-06-04 stsp
4329 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4330 a129376b 2019-03-28 stsp switch (ch) {
4331 33aa809d 2019-08-08 stsp case 'p':
4332 33aa809d 2019-08-08 stsp pflag = 1;
4333 33aa809d 2019-08-08 stsp break;
4334 33aa809d 2019-08-08 stsp case 'F':
4335 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4336 33aa809d 2019-08-08 stsp break;
4337 0f6d7415 2019-08-08 stsp case 'R':
4338 0f6d7415 2019-08-08 stsp can_recurse = 1;
4339 0f6d7415 2019-08-08 stsp break;
4340 a129376b 2019-03-28 stsp default:
4341 a129376b 2019-03-28 stsp usage_revert();
4342 a129376b 2019-03-28 stsp /* NOTREACHED */
4343 a129376b 2019-03-28 stsp }
4344 a129376b 2019-03-28 stsp }
4345 a129376b 2019-03-28 stsp
4346 a129376b 2019-03-28 stsp argc -= optind;
4347 a129376b 2019-03-28 stsp argv += optind;
4348 a129376b 2019-03-28 stsp
4349 43012d58 2019-07-14 stsp #ifndef PROFILE
4350 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4351 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4352 43012d58 2019-07-14 stsp err(1, "pledge");
4353 43012d58 2019-07-14 stsp #endif
4354 e20a8b6f 2019-06-04 stsp if (argc < 1)
4355 a129376b 2019-03-28 stsp usage_revert();
4356 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4357 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4358 a129376b 2019-03-28 stsp
4359 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4360 a129376b 2019-03-28 stsp if (cwd == NULL) {
4361 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4362 a129376b 2019-03-28 stsp goto done;
4363 a129376b 2019-03-28 stsp }
4364 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4365 2ec1f75b 2019-03-26 stsp if (error)
4366 2ec1f75b 2019-03-26 stsp goto done;
4367 a129376b 2019-03-28 stsp
4368 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4369 c9956ddf 2019-09-08 stsp NULL);
4370 a129376b 2019-03-28 stsp if (error != NULL)
4371 a129376b 2019-03-28 stsp goto done;
4372 a129376b 2019-03-28 stsp
4373 33aa809d 2019-08-08 stsp if (patch_script_path) {
4374 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4375 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4376 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4377 33aa809d 2019-08-08 stsp patch_script_path);
4378 33aa809d 2019-08-08 stsp goto done;
4379 33aa809d 2019-08-08 stsp }
4380 33aa809d 2019-08-08 stsp }
4381 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4382 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4383 a129376b 2019-03-28 stsp if (error)
4384 a129376b 2019-03-28 stsp goto done;
4385 a129376b 2019-03-28 stsp
4386 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4387 6d022e97 2019-08-04 stsp if (error)
4388 6d022e97 2019-08-04 stsp goto done;
4389 00db391e 2019-08-03 stsp
4390 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4391 0f6d7415 2019-08-08 stsp char *ondisk_path;
4392 0f6d7415 2019-08-08 stsp struct stat sb;
4393 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4394 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4395 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4396 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4397 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4398 0f6d7415 2019-08-08 stsp goto done;
4399 0f6d7415 2019-08-08 stsp }
4400 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4401 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4402 0f6d7415 2019-08-08 stsp free(ondisk_path);
4403 0f6d7415 2019-08-08 stsp continue;
4404 0f6d7415 2019-08-08 stsp }
4405 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4406 0f6d7415 2019-08-08 stsp ondisk_path);
4407 0f6d7415 2019-08-08 stsp free(ondisk_path);
4408 0f6d7415 2019-08-08 stsp goto done;
4409 0f6d7415 2019-08-08 stsp }
4410 0f6d7415 2019-08-08 stsp free(ondisk_path);
4411 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4412 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4413 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4414 0f6d7415 2019-08-08 stsp goto done;
4415 0f6d7415 2019-08-08 stsp }
4416 0f6d7415 2019-08-08 stsp }
4417 0f6d7415 2019-08-08 stsp }
4418 0f6d7415 2019-08-08 stsp
4419 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4420 33aa809d 2019-08-08 stsp cpa.action = "revert";
4421 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4422 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4423 a129376b 2019-03-28 stsp if (error)
4424 a129376b 2019-03-28 stsp goto done;
4425 2ec1f75b 2019-03-26 stsp done:
4426 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4427 33aa809d 2019-08-08 stsp error == NULL)
4428 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4429 2ec1f75b 2019-03-26 stsp if (repo)
4430 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4431 2ec1f75b 2019-03-26 stsp if (worktree)
4432 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4433 2ec1f75b 2019-03-26 stsp free(path);
4434 d00136be 2019-03-26 stsp free(cwd);
4435 6bad629b 2019-02-04 stsp return error;
4436 c4296144 2019-05-09 stsp }
4437 c4296144 2019-05-09 stsp
4438 c4296144 2019-05-09 stsp __dead static void
4439 c4296144 2019-05-09 stsp usage_commit(void)
4440 c4296144 2019-05-09 stsp {
4441 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4442 5c1e53bc 2019-07-28 stsp getprogname());
4443 c4296144 2019-05-09 stsp exit(1);
4444 33ad4cbe 2019-05-12 jcs }
4445 33ad4cbe 2019-05-12 jcs
4446 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4447 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4448 e2ba3d07 2019-05-13 stsp const char *editor;
4449 e0870e44 2019-05-13 stsp const char *worktree_path;
4450 76d98825 2019-06-03 stsp const char *branch_name;
4451 314a6357 2019-05-13 stsp const char *repo_path;
4452 e0870e44 2019-05-13 stsp char *logmsg_path;
4453 e2ba3d07 2019-05-13 stsp
4454 e2ba3d07 2019-05-13 stsp };
4455 e0870e44 2019-05-13 stsp
4456 33ad4cbe 2019-05-12 jcs static const struct got_error *
4457 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4458 33ad4cbe 2019-05-12 jcs void *arg)
4459 33ad4cbe 2019-05-12 jcs {
4460 76d98825 2019-06-03 stsp char *initial_content = NULL;
4461 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4462 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4463 e0870e44 2019-05-13 stsp char *template = NULL;
4464 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4465 3ce1b845 2019-07-15 stsp int fd;
4466 33ad4cbe 2019-05-12 jcs size_t len;
4467 33ad4cbe 2019-05-12 jcs
4468 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4469 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4470 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4471 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4472 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4473 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4474 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4475 33ad4cbe 2019-05-12 jcs return NULL;
4476 33ad4cbe 2019-05-12 jcs }
4477 33ad4cbe 2019-05-12 jcs
4478 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4479 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4480 76d98825 2019-06-03 stsp
4481 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4482 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4483 76d98825 2019-06-03 stsp a->branch_name) == -1)
4484 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4485 e0870e44 2019-05-13 stsp
4486 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4487 793c30b5 2019-05-13 stsp if (err)
4488 e0870e44 2019-05-13 stsp goto done;
4489 33ad4cbe 2019-05-12 jcs
4490 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4491 33ad4cbe 2019-05-12 jcs
4492 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4493 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4494 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4495 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4496 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4497 33ad4cbe 2019-05-12 jcs }
4498 33ad4cbe 2019-05-12 jcs close(fd);
4499 33ad4cbe 2019-05-12 jcs
4500 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4501 33ad4cbe 2019-05-12 jcs done:
4502 e2af0fd1 2019-08-08 stsp if (err == NULL || err->code == GOT_ERR_COMMIT_MSG_EMPTY) {
4503 e2af0fd1 2019-08-08 stsp unlink(a->logmsg_path);
4504 e2af0fd1 2019-08-08 stsp free(a->logmsg_path);
4505 e2af0fd1 2019-08-08 stsp a->logmsg_path = NULL;
4506 e2af0fd1 2019-08-08 stsp }
4507 76d98825 2019-06-03 stsp free(initial_content);
4508 e0870e44 2019-05-13 stsp free(template);
4509 314a6357 2019-05-13 stsp
4510 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4511 3ce1b845 2019-07-15 stsp if (err == NULL) {
4512 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4513 3ce1b845 2019-07-15 stsp if (err) {
4514 3ce1b845 2019-07-15 stsp free(*logmsg);
4515 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4516 3ce1b845 2019-07-15 stsp }
4517 3ce1b845 2019-07-15 stsp }
4518 33ad4cbe 2019-05-12 jcs return err;
4519 6bad629b 2019-02-04 stsp }
4520 c4296144 2019-05-09 stsp
4521 c4296144 2019-05-09 stsp static const struct got_error *
4522 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4523 c4296144 2019-05-09 stsp {
4524 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4525 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4526 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4527 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4528 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4529 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4530 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4531 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4532 916f288c 2019-07-30 stsp int ch, rebase_in_progress, histedit_in_progress;
4533 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4534 5c1e53bc 2019-07-28 stsp
4535 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4536 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4537 c4296144 2019-05-09 stsp
4538 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4539 c4296144 2019-05-09 stsp switch (ch) {
4540 c4296144 2019-05-09 stsp case 'm':
4541 c4296144 2019-05-09 stsp logmsg = optarg;
4542 c4296144 2019-05-09 stsp break;
4543 c4296144 2019-05-09 stsp default:
4544 c4296144 2019-05-09 stsp usage_commit();
4545 c4296144 2019-05-09 stsp /* NOTREACHED */
4546 c4296144 2019-05-09 stsp }
4547 c4296144 2019-05-09 stsp }
4548 c4296144 2019-05-09 stsp
4549 c4296144 2019-05-09 stsp argc -= optind;
4550 c4296144 2019-05-09 stsp argv += optind;
4551 c4296144 2019-05-09 stsp
4552 43012d58 2019-07-14 stsp #ifndef PROFILE
4553 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4554 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4555 43012d58 2019-07-14 stsp err(1, "pledge");
4556 43012d58 2019-07-14 stsp #endif
4557 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4558 c4296144 2019-05-09 stsp if (cwd == NULL) {
4559 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4560 c4296144 2019-05-09 stsp goto done;
4561 c4296144 2019-05-09 stsp }
4562 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4563 7d5807f4 2019-07-11 stsp if (error)
4564 7d5807f4 2019-07-11 stsp goto done;
4565 7d5807f4 2019-07-11 stsp
4566 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4567 c4296144 2019-05-09 stsp if (error)
4568 a698f62e 2019-07-25 stsp goto done;
4569 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4570 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4571 c4296144 2019-05-09 stsp goto done;
4572 a698f62e 2019-07-25 stsp }
4573 5c1e53bc 2019-07-28 stsp
4574 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4575 916f288c 2019-07-30 stsp worktree);
4576 5c1e53bc 2019-07-28 stsp if (error)
4577 5c1e53bc 2019-07-28 stsp goto done;
4578 c4296144 2019-05-09 stsp
4579 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4580 c9956ddf 2019-09-08 stsp if (error)
4581 c9956ddf 2019-09-08 stsp goto done;
4582 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4583 c9956ddf 2019-09-08 stsp gitconfig_path);
4584 c4296144 2019-05-09 stsp if (error != NULL)
4585 c4296144 2019-05-09 stsp goto done;
4586 c4296144 2019-05-09 stsp
4587 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4588 aba9c984 2019-09-08 stsp if (error)
4589 aba9c984 2019-09-08 stsp return error;
4590 aba9c984 2019-09-08 stsp
4591 314a6357 2019-05-13 stsp /*
4592 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4593 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4594 314a6357 2019-05-13 stsp */
4595 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4596 314a6357 2019-05-13 stsp error = get_editor(&editor);
4597 314a6357 2019-05-13 stsp else
4598 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4599 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4600 c4296144 2019-05-09 stsp if (error)
4601 c4296144 2019-05-09 stsp goto done;
4602 c4296144 2019-05-09 stsp
4603 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4604 087fb88c 2019-08-04 stsp if (error)
4605 087fb88c 2019-08-04 stsp goto done;
4606 087fb88c 2019-08-04 stsp
4607 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4608 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4609 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4610 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4611 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4612 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4613 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4614 916f288c 2019-07-30 stsp goto done;
4615 916f288c 2019-07-30 stsp }
4616 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4617 916f288c 2019-07-30 stsp }
4618 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4619 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4620 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4621 e0870e44 2019-05-13 stsp if (error) {
4622 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4623 e0870e44 2019-05-13 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4624 e0870e44 2019-05-13 stsp getprogname(), cl_arg.logmsg_path);
4625 c4296144 2019-05-09 stsp goto done;
4626 e0870e44 2019-05-13 stsp }
4627 c4296144 2019-05-09 stsp
4628 e0870e44 2019-05-13 stsp if (cl_arg.logmsg_path)
4629 e0870e44 2019-05-13 stsp unlink(cl_arg.logmsg_path);
4630 e0870e44 2019-05-13 stsp
4631 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4632 c4296144 2019-05-09 stsp if (error)
4633 c4296144 2019-05-09 stsp goto done;
4634 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4635 c4296144 2019-05-09 stsp done:
4636 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4637 c4296144 2019-05-09 stsp if (repo)
4638 c4296144 2019-05-09 stsp got_repo_close(repo);
4639 c4296144 2019-05-09 stsp if (worktree)
4640 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4641 c4296144 2019-05-09 stsp free(cwd);
4642 c4296144 2019-05-09 stsp free(id_str);
4643 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4644 e2ba3d07 2019-05-13 stsp free(editor);
4645 aba9c984 2019-09-08 stsp free(author);
4646 234035bc 2019-06-01 stsp return error;
4647 234035bc 2019-06-01 stsp }
4648 234035bc 2019-06-01 stsp
4649 234035bc 2019-06-01 stsp __dead static void
4650 234035bc 2019-06-01 stsp usage_cherrypick(void)
4651 234035bc 2019-06-01 stsp {
4652 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4653 234035bc 2019-06-01 stsp exit(1);
4654 234035bc 2019-06-01 stsp }
4655 234035bc 2019-06-01 stsp
4656 234035bc 2019-06-01 stsp static const struct got_error *
4657 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4658 234035bc 2019-06-01 stsp {
4659 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4660 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4661 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4662 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4663 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4664 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4665 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4666 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4667 234035bc 2019-06-01 stsp int ch, did_something = 0;
4668 234035bc 2019-06-01 stsp
4669 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4670 234035bc 2019-06-01 stsp switch (ch) {
4671 234035bc 2019-06-01 stsp default:
4672 234035bc 2019-06-01 stsp usage_cherrypick();
4673 234035bc 2019-06-01 stsp /* NOTREACHED */
4674 234035bc 2019-06-01 stsp }
4675 234035bc 2019-06-01 stsp }
4676 234035bc 2019-06-01 stsp
4677 234035bc 2019-06-01 stsp argc -= optind;
4678 234035bc 2019-06-01 stsp argv += optind;
4679 234035bc 2019-06-01 stsp
4680 43012d58 2019-07-14 stsp #ifndef PROFILE
4681 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4682 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4683 43012d58 2019-07-14 stsp err(1, "pledge");
4684 43012d58 2019-07-14 stsp #endif
4685 234035bc 2019-06-01 stsp if (argc != 1)
4686 234035bc 2019-06-01 stsp usage_cherrypick();
4687 234035bc 2019-06-01 stsp
4688 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4689 234035bc 2019-06-01 stsp if (cwd == NULL) {
4690 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4691 234035bc 2019-06-01 stsp goto done;
4692 234035bc 2019-06-01 stsp }
4693 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4694 234035bc 2019-06-01 stsp if (error)
4695 234035bc 2019-06-01 stsp goto done;
4696 234035bc 2019-06-01 stsp
4697 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4698 c9956ddf 2019-09-08 stsp NULL);
4699 234035bc 2019-06-01 stsp if (error != NULL)
4700 234035bc 2019-06-01 stsp goto done;
4701 234035bc 2019-06-01 stsp
4702 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4703 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4704 234035bc 2019-06-01 stsp if (error)
4705 234035bc 2019-06-01 stsp goto done;
4706 234035bc 2019-06-01 stsp
4707 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4708 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4709 234035bc 2019-06-01 stsp if (error != NULL) {
4710 234035bc 2019-06-01 stsp struct got_reference *ref;
4711 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4712 234035bc 2019-06-01 stsp goto done;
4713 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4714 234035bc 2019-06-01 stsp if (error != NULL)
4715 234035bc 2019-06-01 stsp goto done;
4716 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4717 234035bc 2019-06-01 stsp got_ref_close(ref);
4718 234035bc 2019-06-01 stsp if (error != NULL)
4719 234035bc 2019-06-01 stsp goto done;
4720 234035bc 2019-06-01 stsp }
4721 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4722 234035bc 2019-06-01 stsp if (error)
4723 234035bc 2019-06-01 stsp goto done;
4724 234035bc 2019-06-01 stsp
4725 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4726 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4727 234035bc 2019-06-01 stsp if (error != NULL)
4728 234035bc 2019-06-01 stsp goto done;
4729 234035bc 2019-06-01 stsp
4730 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4731 234035bc 2019-06-01 stsp if (error) {
4732 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4733 234035bc 2019-06-01 stsp goto done;
4734 234035bc 2019-06-01 stsp error = NULL;
4735 234035bc 2019-06-01 stsp } else {
4736 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4737 234035bc 2019-06-01 stsp goto done;
4738 234035bc 2019-06-01 stsp }
4739 234035bc 2019-06-01 stsp
4740 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4741 234035bc 2019-06-01 stsp if (error)
4742 234035bc 2019-06-01 stsp goto done;
4743 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4744 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4745 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4746 03415a1a 2019-06-02 stsp NULL);
4747 234035bc 2019-06-01 stsp if (error != NULL)
4748 234035bc 2019-06-01 stsp goto done;
4749 234035bc 2019-06-01 stsp
4750 234035bc 2019-06-01 stsp if (did_something)
4751 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4752 234035bc 2019-06-01 stsp done:
4753 234035bc 2019-06-01 stsp if (commit)
4754 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4755 234035bc 2019-06-01 stsp free(commit_id_str);
4756 234035bc 2019-06-01 stsp if (head_ref)
4757 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4758 234035bc 2019-06-01 stsp if (worktree)
4759 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4760 234035bc 2019-06-01 stsp if (repo)
4761 234035bc 2019-06-01 stsp got_repo_close(repo);
4762 c4296144 2019-05-09 stsp return error;
4763 c4296144 2019-05-09 stsp }
4764 5ef14e63 2019-06-02 stsp
4765 5ef14e63 2019-06-02 stsp __dead static void
4766 5ef14e63 2019-06-02 stsp usage_backout(void)
4767 5ef14e63 2019-06-02 stsp {
4768 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4769 5ef14e63 2019-06-02 stsp exit(1);
4770 5ef14e63 2019-06-02 stsp }
4771 5ef14e63 2019-06-02 stsp
4772 5ef14e63 2019-06-02 stsp static const struct got_error *
4773 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4774 5ef14e63 2019-06-02 stsp {
4775 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4776 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4777 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4778 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4779 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4780 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4781 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4782 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4783 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4784 5ef14e63 2019-06-02 stsp
4785 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4786 5ef14e63 2019-06-02 stsp switch (ch) {
4787 5ef14e63 2019-06-02 stsp default:
4788 5ef14e63 2019-06-02 stsp usage_backout();
4789 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4790 5ef14e63 2019-06-02 stsp }
4791 5ef14e63 2019-06-02 stsp }
4792 5ef14e63 2019-06-02 stsp
4793 5ef14e63 2019-06-02 stsp argc -= optind;
4794 5ef14e63 2019-06-02 stsp argv += optind;
4795 5ef14e63 2019-06-02 stsp
4796 43012d58 2019-07-14 stsp #ifndef PROFILE
4797 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4798 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4799 43012d58 2019-07-14 stsp err(1, "pledge");
4800 43012d58 2019-07-14 stsp #endif
4801 5ef14e63 2019-06-02 stsp if (argc != 1)
4802 5ef14e63 2019-06-02 stsp usage_backout();
4803 5ef14e63 2019-06-02 stsp
4804 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4805 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4806 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4807 5ef14e63 2019-06-02 stsp goto done;
4808 5ef14e63 2019-06-02 stsp }
4809 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4810 5ef14e63 2019-06-02 stsp if (error)
4811 5ef14e63 2019-06-02 stsp goto done;
4812 5ef14e63 2019-06-02 stsp
4813 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4814 c9956ddf 2019-09-08 stsp NULL);
4815 5ef14e63 2019-06-02 stsp if (error != NULL)
4816 5ef14e63 2019-06-02 stsp goto done;
4817 5ef14e63 2019-06-02 stsp
4818 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4819 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4820 5ef14e63 2019-06-02 stsp if (error)
4821 5ef14e63 2019-06-02 stsp goto done;
4822 5ef14e63 2019-06-02 stsp
4823 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4824 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4825 5ef14e63 2019-06-02 stsp if (error != NULL) {
4826 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4827 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4828 5ef14e63 2019-06-02 stsp goto done;
4829 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4830 5ef14e63 2019-06-02 stsp if (error != NULL)
4831 5ef14e63 2019-06-02 stsp goto done;
4832 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4833 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4834 5ef14e63 2019-06-02 stsp if (error != NULL)
4835 5ef14e63 2019-06-02 stsp goto done;
4836 5ef14e63 2019-06-02 stsp }
4837 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4838 5ef14e63 2019-06-02 stsp if (error)
4839 5ef14e63 2019-06-02 stsp goto done;
4840 5ef14e63 2019-06-02 stsp
4841 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4842 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4843 5ef14e63 2019-06-02 stsp if (error != NULL)
4844 5ef14e63 2019-06-02 stsp goto done;
4845 5ef14e63 2019-06-02 stsp
4846 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4847 5ef14e63 2019-06-02 stsp if (error)
4848 5ef14e63 2019-06-02 stsp goto done;
4849 5ef14e63 2019-06-02 stsp
4850 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4851 5ef14e63 2019-06-02 stsp if (error)
4852 5ef14e63 2019-06-02 stsp goto done;
4853 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4854 5ef14e63 2019-06-02 stsp if (pid == NULL) {
4855 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
4856 5ef14e63 2019-06-02 stsp goto done;
4857 5ef14e63 2019-06-02 stsp }
4858 5ef14e63 2019-06-02 stsp
4859 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
4860 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
4861 5ef14e63 2019-06-02 stsp if (error != NULL)
4862 5ef14e63 2019-06-02 stsp goto done;
4863 5ef14e63 2019-06-02 stsp
4864 5ef14e63 2019-06-02 stsp if (did_something)
4865 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
4866 5ef14e63 2019-06-02 stsp done:
4867 5ef14e63 2019-06-02 stsp if (commit)
4868 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
4869 5ef14e63 2019-06-02 stsp free(commit_id_str);
4870 5ef14e63 2019-06-02 stsp if (head_ref)
4871 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
4872 818c7501 2019-07-11 stsp if (worktree)
4873 818c7501 2019-07-11 stsp got_worktree_close(worktree);
4874 818c7501 2019-07-11 stsp if (repo)
4875 818c7501 2019-07-11 stsp got_repo_close(repo);
4876 818c7501 2019-07-11 stsp return error;
4877 818c7501 2019-07-11 stsp }
4878 818c7501 2019-07-11 stsp
4879 818c7501 2019-07-11 stsp __dead static void
4880 818c7501 2019-07-11 stsp usage_rebase(void)
4881 818c7501 2019-07-11 stsp {
4882 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
4883 af54c8f8 2019-07-11 stsp getprogname());
4884 818c7501 2019-07-11 stsp exit(1);
4885 0ebf8283 2019-07-24 stsp }
4886 0ebf8283 2019-07-24 stsp
4887 0ebf8283 2019-07-24 stsp void
4888 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
4889 0ebf8283 2019-07-24 stsp {
4890 0ebf8283 2019-07-24 stsp char *nl;
4891 0ebf8283 2019-07-24 stsp size_t len;
4892 0ebf8283 2019-07-24 stsp
4893 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
4894 0ebf8283 2019-07-24 stsp if (len > limit)
4895 0ebf8283 2019-07-24 stsp len = limit;
4896 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
4897 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
4898 0ebf8283 2019-07-24 stsp if (nl)
4899 0ebf8283 2019-07-24 stsp *nl = '\0';
4900 0ebf8283 2019-07-24 stsp }
4901 0ebf8283 2019-07-24 stsp
4902 0ebf8283 2019-07-24 stsp static const struct got_error *
4903 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
4904 0ebf8283 2019-07-24 stsp {
4905 5943eee2 2019-08-13 stsp const struct got_error *err;
4906 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
4907 5943eee2 2019-08-13 stsp const char *s;
4908 0ebf8283 2019-07-24 stsp
4909 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
4910 5943eee2 2019-08-13 stsp if (err)
4911 5943eee2 2019-08-13 stsp return err;
4912 0ebf8283 2019-07-24 stsp
4913 5943eee2 2019-08-13 stsp s = logmsg0;
4914 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
4915 5943eee2 2019-08-13 stsp s++;
4916 5943eee2 2019-08-13 stsp
4917 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
4918 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
4919 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
4920 5943eee2 2019-08-13 stsp goto done;
4921 5943eee2 2019-08-13 stsp }
4922 0ebf8283 2019-07-24 stsp
4923 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
4924 5943eee2 2019-08-13 stsp done:
4925 5943eee2 2019-08-13 stsp free(logmsg0);
4926 5943eee2 2019-08-13 stsp return err;
4927 818c7501 2019-07-11 stsp }
4928 818c7501 2019-07-11 stsp
4929 818c7501 2019-07-11 stsp static const struct got_error *
4930 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
4931 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
4932 818c7501 2019-07-11 stsp {
4933 818c7501 2019-07-11 stsp const struct got_error *err;
4934 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
4935 818c7501 2019-07-11 stsp
4936 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
4937 818c7501 2019-07-11 stsp if (err)
4938 818c7501 2019-07-11 stsp goto done;
4939 818c7501 2019-07-11 stsp
4940 ff0d2220 2019-07-11 stsp if (new_id) {
4941 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
4942 ff0d2220 2019-07-11 stsp if (err)
4943 ff0d2220 2019-07-11 stsp goto done;
4944 ff0d2220 2019-07-11 stsp }
4945 818c7501 2019-07-11 stsp
4946 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
4947 ff0d2220 2019-07-11 stsp if (new_id_str)
4948 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
4949 0ebf8283 2019-07-24 stsp
4950 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
4951 0ebf8283 2019-07-24 stsp if (err)
4952 0ebf8283 2019-07-24 stsp goto done;
4953 0ebf8283 2019-07-24 stsp
4954 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
4955 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
4956 818c7501 2019-07-11 stsp done:
4957 818c7501 2019-07-11 stsp free(old_id_str);
4958 818c7501 2019-07-11 stsp free(new_id_str);
4959 818c7501 2019-07-11 stsp return err;
4960 818c7501 2019-07-11 stsp }
4961 818c7501 2019-07-11 stsp
4962 1ee397ad 2019-07-12 stsp static const struct got_error *
4963 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
4964 818c7501 2019-07-11 stsp {
4965 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
4966 818c7501 2019-07-11 stsp
4967 818c7501 2019-07-11 stsp while (path[0] == '/')
4968 818c7501 2019-07-11 stsp path++;
4969 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
4970 818c7501 2019-07-11 stsp
4971 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
4972 1ee397ad 2019-07-12 stsp return NULL;
4973 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
4974 818c7501 2019-07-11 stsp *rebase_status = status;
4975 1ee397ad 2019-07-12 stsp return NULL;
4976 818c7501 2019-07-11 stsp }
4977 818c7501 2019-07-11 stsp
4978 818c7501 2019-07-11 stsp static const struct got_error *
4979 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
4980 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
4981 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
4982 818c7501 2019-07-11 stsp {
4983 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
4984 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
4985 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
4986 818c7501 2019-07-11 stsp }
4987 818c7501 2019-07-11 stsp
4988 818c7501 2019-07-11 stsp static const struct got_error *
4989 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
4990 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
4991 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
4992 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
4993 ff0d2220 2019-07-11 stsp {
4994 ff0d2220 2019-07-11 stsp const struct got_error *error;
4995 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
4996 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
4997 ff0d2220 2019-07-11 stsp
4998 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4999 ff0d2220 2019-07-11 stsp if (error)
5000 ff0d2220 2019-07-11 stsp return error;
5001 ff0d2220 2019-07-11 stsp
5002 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5003 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5004 ff0d2220 2019-07-11 stsp if (error) {
5005 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5006 ff0d2220 2019-07-11 stsp goto done;
5007 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5008 ff0d2220 2019-07-11 stsp } else {
5009 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5010 ff0d2220 2019-07-11 stsp free(new_commit_id);
5011 ff0d2220 2019-07-11 stsp }
5012 ff0d2220 2019-07-11 stsp done:
5013 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5014 ff0d2220 2019-07-11 stsp return error;
5015 64c6d990 2019-07-11 stsp }
5016 64c6d990 2019-07-11 stsp
5017 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5018 64c6d990 2019-07-11 stsp const char *path_prefix;
5019 64c6d990 2019-07-11 stsp size_t len;
5020 8ca9bd68 2019-07-25 stsp int errcode;
5021 64c6d990 2019-07-11 stsp };
5022 64c6d990 2019-07-11 stsp
5023 64c6d990 2019-07-11 stsp static const struct got_error *
5024 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5025 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5026 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5027 64c6d990 2019-07-11 stsp struct got_repository *repo)
5028 64c6d990 2019-07-11 stsp {
5029 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5030 64c6d990 2019-07-11 stsp
5031 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5032 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5033 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5034 64c6d990 2019-07-11 stsp
5035 64c6d990 2019-07-11 stsp return NULL;
5036 64c6d990 2019-07-11 stsp }
5037 64c6d990 2019-07-11 stsp
5038 64c6d990 2019-07-11 stsp static const struct got_error *
5039 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5040 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5041 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5042 64c6d990 2019-07-11 stsp {
5043 64c6d990 2019-07-11 stsp const struct got_error *err;
5044 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5045 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5046 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5047 64c6d990 2019-07-11 stsp
5048 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5049 64c6d990 2019-07-11 stsp return NULL;
5050 64c6d990 2019-07-11 stsp
5051 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5052 64c6d990 2019-07-11 stsp if (err)
5053 64c6d990 2019-07-11 stsp goto done;
5054 64c6d990 2019-07-11 stsp
5055 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5056 64c6d990 2019-07-11 stsp if (err)
5057 64c6d990 2019-07-11 stsp goto done;
5058 64c6d990 2019-07-11 stsp
5059 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5060 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5061 64c6d990 2019-07-11 stsp if (err)
5062 64c6d990 2019-07-11 stsp goto done;
5063 64c6d990 2019-07-11 stsp
5064 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5065 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5066 64c6d990 2019-07-11 stsp if (err)
5067 64c6d990 2019-07-11 stsp goto done;
5068 64c6d990 2019-07-11 stsp
5069 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5070 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5071 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5072 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5073 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5074 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5075 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5076 64c6d990 2019-07-11 stsp done:
5077 64c6d990 2019-07-11 stsp if (tree1)
5078 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5079 64c6d990 2019-07-11 stsp if (tree2)
5080 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5081 64c6d990 2019-07-11 stsp if (commit)
5082 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5083 64c6d990 2019-07-11 stsp if (parent_commit)
5084 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5085 64c6d990 2019-07-11 stsp return err;
5086 ff0d2220 2019-07-11 stsp }
5087 ff0d2220 2019-07-11 stsp
5088 ff0d2220 2019-07-11 stsp static const struct got_error *
5089 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5090 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5091 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5092 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5093 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5094 af61c510 2019-07-19 stsp {
5095 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5096 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5097 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5098 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5099 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5100 af61c510 2019-07-19 stsp
5101 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5102 af61c510 2019-07-19 stsp if (err)
5103 af61c510 2019-07-19 stsp return err;
5104 af61c510 2019-07-19 stsp
5105 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5106 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5107 af61c510 2019-07-19 stsp if (err)
5108 af61c510 2019-07-19 stsp goto done;
5109 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5110 af61c510 2019-07-19 stsp err = got_commit_graph_iter_next(&parent_id, graph);
5111 af61c510 2019-07-19 stsp if (err) {
5112 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5113 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5114 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5115 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5116 af61c510 2019-07-19 stsp "been reached?!?");
5117 af61c510 2019-07-19 stsp goto done;
5118 af61c510 2019-07-19 stsp } else if (err->code != GOT_ERR_ITER_NEED_MORE)
5119 af61c510 2019-07-19 stsp goto done;
5120 6fb7cd11 2019-08-22 stsp err = got_commit_graph_fetch_commits(graph, 1, repo,
5121 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5122 af61c510 2019-07-19 stsp if (err)
5123 af61c510 2019-07-19 stsp goto done;
5124 af61c510 2019-07-19 stsp } else {
5125 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5126 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5127 af61c510 2019-07-19 stsp if (err)
5128 af61c510 2019-07-19 stsp goto done;
5129 af61c510 2019-07-19 stsp
5130 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5131 af61c510 2019-07-19 stsp if (err)
5132 af61c510 2019-07-19 stsp goto done;
5133 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5134 af61c510 2019-07-19 stsp commit_id = parent_id;
5135 af61c510 2019-07-19 stsp }
5136 af61c510 2019-07-19 stsp }
5137 af61c510 2019-07-19 stsp done:
5138 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5139 af61c510 2019-07-19 stsp return err;
5140 af61c510 2019-07-19 stsp }
5141 af61c510 2019-07-19 stsp
5142 af61c510 2019-07-19 stsp static const struct got_error *
5143 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5144 818c7501 2019-07-11 stsp {
5145 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5146 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5147 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5148 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5149 818c7501 2019-07-11 stsp char *cwd = NULL;
5150 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5151 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5152 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5153 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5154 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5155 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5156 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5157 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5158 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5159 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5160 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5161 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5162 818c7501 2019-07-11 stsp
5163 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5164 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5165 818c7501 2019-07-11 stsp
5166 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5167 818c7501 2019-07-11 stsp switch (ch) {
5168 818c7501 2019-07-11 stsp case 'a':
5169 818c7501 2019-07-11 stsp abort_rebase = 1;
5170 818c7501 2019-07-11 stsp break;
5171 818c7501 2019-07-11 stsp case 'c':
5172 818c7501 2019-07-11 stsp continue_rebase = 1;
5173 818c7501 2019-07-11 stsp break;
5174 818c7501 2019-07-11 stsp default:
5175 818c7501 2019-07-11 stsp usage_rebase();
5176 818c7501 2019-07-11 stsp /* NOTREACHED */
5177 818c7501 2019-07-11 stsp }
5178 818c7501 2019-07-11 stsp }
5179 818c7501 2019-07-11 stsp
5180 818c7501 2019-07-11 stsp argc -= optind;
5181 818c7501 2019-07-11 stsp argv += optind;
5182 818c7501 2019-07-11 stsp
5183 43012d58 2019-07-14 stsp #ifndef PROFILE
5184 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5185 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5186 43012d58 2019-07-14 stsp err(1, "pledge");
5187 43012d58 2019-07-14 stsp #endif
5188 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5189 818c7501 2019-07-11 stsp usage_rebase();
5190 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5191 818c7501 2019-07-11 stsp if (argc != 0)
5192 818c7501 2019-07-11 stsp usage_rebase();
5193 818c7501 2019-07-11 stsp } else if (argc != 1)
5194 818c7501 2019-07-11 stsp usage_rebase();
5195 818c7501 2019-07-11 stsp
5196 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5197 818c7501 2019-07-11 stsp if (cwd == NULL) {
5198 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5199 818c7501 2019-07-11 stsp goto done;
5200 818c7501 2019-07-11 stsp }
5201 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5202 818c7501 2019-07-11 stsp if (error)
5203 818c7501 2019-07-11 stsp goto done;
5204 818c7501 2019-07-11 stsp
5205 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5206 c9956ddf 2019-09-08 stsp NULL);
5207 818c7501 2019-07-11 stsp if (error != NULL)
5208 818c7501 2019-07-11 stsp goto done;
5209 818c7501 2019-07-11 stsp
5210 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5211 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5212 818c7501 2019-07-11 stsp if (error)
5213 818c7501 2019-07-11 stsp goto done;
5214 818c7501 2019-07-11 stsp
5215 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5216 818c7501 2019-07-11 stsp if (error)
5217 818c7501 2019-07-11 stsp goto done;
5218 818c7501 2019-07-11 stsp
5219 f6794adc 2019-07-23 stsp if (abort_rebase) {
5220 818c7501 2019-07-11 stsp int did_something;
5221 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5222 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5223 f6794adc 2019-07-23 stsp goto done;
5224 f6794adc 2019-07-23 stsp }
5225 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5226 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5227 3e3a69f1 2019-07-25 stsp worktree, repo);
5228 818c7501 2019-07-11 stsp if (error)
5229 818c7501 2019-07-11 stsp goto done;
5230 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5231 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5232 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5233 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5234 818c7501 2019-07-11 stsp if (error)
5235 818c7501 2019-07-11 stsp goto done;
5236 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5237 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5238 818c7501 2019-07-11 stsp }
5239 818c7501 2019-07-11 stsp
5240 818c7501 2019-07-11 stsp if (continue_rebase) {
5241 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5242 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5243 f6794adc 2019-07-23 stsp goto done;
5244 f6794adc 2019-07-23 stsp }
5245 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5246 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5247 3e3a69f1 2019-07-25 stsp worktree, repo);
5248 818c7501 2019-07-11 stsp if (error)
5249 818c7501 2019-07-11 stsp goto done;
5250 818c7501 2019-07-11 stsp
5251 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5252 01757395 2019-07-12 stsp resume_commit_id, repo);
5253 818c7501 2019-07-11 stsp if (error)
5254 818c7501 2019-07-11 stsp goto done;
5255 818c7501 2019-07-11 stsp
5256 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5257 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5258 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5259 818c7501 2019-07-11 stsp goto done;
5260 818c7501 2019-07-11 stsp }
5261 818c7501 2019-07-11 stsp } else {
5262 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5263 818c7501 2019-07-11 stsp if (error != NULL)
5264 818c7501 2019-07-11 stsp goto done;
5265 ff0d2220 2019-07-11 stsp }
5266 818c7501 2019-07-11 stsp
5267 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5268 ff0d2220 2019-07-11 stsp if (error)
5269 ff0d2220 2019-07-11 stsp goto done;
5270 ff0d2220 2019-07-11 stsp
5271 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5272 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5273 a51a74b3 2019-07-27 stsp
5274 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5275 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5276 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5277 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5278 818c7501 2019-07-11 stsp if (error)
5279 818c7501 2019-07-11 stsp goto done;
5280 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5281 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5282 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5283 818c7501 2019-07-11 stsp "with work tree's branch");
5284 818c7501 2019-07-11 stsp goto done;
5285 818c7501 2019-07-11 stsp }
5286 818c7501 2019-07-11 stsp
5287 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5288 a51a74b3 2019-07-27 stsp if (error) {
5289 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5290 a51a74b3 2019-07-27 stsp goto done;
5291 a51a74b3 2019-07-27 stsp error = NULL;
5292 a51a74b3 2019-07-27 stsp } else {
5293 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5294 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5295 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5296 a51a74b3 2019-07-27 stsp goto done;
5297 a51a74b3 2019-07-27 stsp }
5298 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5299 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5300 818c7501 2019-07-11 stsp if (error)
5301 818c7501 2019-07-11 stsp goto done;
5302 818c7501 2019-07-11 stsp }
5303 818c7501 2019-07-11 stsp
5304 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5305 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5306 818c7501 2019-07-11 stsp if (error)
5307 818c7501 2019-07-11 stsp goto done;
5308 818c7501 2019-07-11 stsp
5309 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5310 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5311 fc66b545 2019-08-12 stsp if (pid == NULL) {
5312 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5313 fc66b545 2019-08-12 stsp int did_something;
5314 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5315 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5316 fc66b545 2019-08-12 stsp &did_something);
5317 fc66b545 2019-08-12 stsp if (error)
5318 fc66b545 2019-08-12 stsp goto done;
5319 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5320 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5321 fc66b545 2019-08-12 stsp }
5322 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5323 fc66b545 2019-08-12 stsp goto done;
5324 fc66b545 2019-08-12 stsp }
5325 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5326 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5327 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5328 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5329 818c7501 2019-07-11 stsp commit = NULL;
5330 818c7501 2019-07-11 stsp if (error)
5331 818c7501 2019-07-11 stsp goto done;
5332 64c6d990 2019-07-11 stsp
5333 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5334 ff0d2220 2019-07-11 stsp if (continue_rebase)
5335 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5336 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5337 ff0d2220 2019-07-11 stsp else
5338 ff0d2220 2019-07-11 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5339 818c7501 2019-07-11 stsp goto done;
5340 818c7501 2019-07-11 stsp }
5341 818c7501 2019-07-11 stsp
5342 818c7501 2019-07-11 stsp pid = NULL;
5343 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5344 818c7501 2019-07-11 stsp commit_id = qid->id;
5345 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5346 818c7501 2019-07-11 stsp pid = qid;
5347 818c7501 2019-07-11 stsp
5348 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5349 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5350 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5351 818c7501 2019-07-11 stsp if (error)
5352 818c7501 2019-07-11 stsp goto done;
5353 818c7501 2019-07-11 stsp
5354 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5355 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5356 818c7501 2019-07-11 stsp break;
5357 01757395 2019-07-12 stsp }
5358 818c7501 2019-07-11 stsp
5359 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5360 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5361 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5362 818c7501 2019-07-11 stsp if (error)
5363 818c7501 2019-07-11 stsp goto done;
5364 818c7501 2019-07-11 stsp }
5365 818c7501 2019-07-11 stsp
5366 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5367 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5368 818c7501 2019-07-11 stsp if (error)
5369 818c7501 2019-07-11 stsp goto done;
5370 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5371 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5372 818c7501 2019-07-11 stsp } else
5373 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5374 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5375 818c7501 2019-07-11 stsp done:
5376 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5377 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5378 818c7501 2019-07-11 stsp free(resume_commit_id);
5379 818c7501 2019-07-11 stsp free(yca_id);
5380 818c7501 2019-07-11 stsp if (commit)
5381 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5382 818c7501 2019-07-11 stsp if (branch)
5383 818c7501 2019-07-11 stsp got_ref_close(branch);
5384 818c7501 2019-07-11 stsp if (new_base_branch)
5385 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5386 818c7501 2019-07-11 stsp if (tmp_branch)
5387 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5388 5ef14e63 2019-06-02 stsp if (worktree)
5389 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5390 5ef14e63 2019-06-02 stsp if (repo)
5391 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5392 5ef14e63 2019-06-02 stsp return error;
5393 0ebf8283 2019-07-24 stsp }
5394 0ebf8283 2019-07-24 stsp
5395 0ebf8283 2019-07-24 stsp __dead static void
5396 0ebf8283 2019-07-24 stsp usage_histedit(void)
5397 0ebf8283 2019-07-24 stsp {
5398 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5399 0ebf8283 2019-07-24 stsp getprogname());
5400 0ebf8283 2019-07-24 stsp exit(1);
5401 0ebf8283 2019-07-24 stsp }
5402 0ebf8283 2019-07-24 stsp
5403 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5404 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5405 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5406 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5407 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5408 0ebf8283 2019-07-24 stsp
5409 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5410 0ebf8283 2019-07-24 stsp unsigned char code;
5411 0ebf8283 2019-07-24 stsp const char *name;
5412 0ebf8283 2019-07-24 stsp const char *desc;
5413 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5414 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5415 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5416 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5417 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5418 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5419 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5420 0ebf8283 2019-07-24 stsp };
5421 0ebf8283 2019-07-24 stsp
5422 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5423 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5424 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5425 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5426 0ebf8283 2019-07-24 stsp char *logmsg;
5427 0ebf8283 2019-07-24 stsp };
5428 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5429 0ebf8283 2019-07-24 stsp
5430 0ebf8283 2019-07-24 stsp static const struct got_error *
5431 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5432 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5433 0ebf8283 2019-07-24 stsp {
5434 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5435 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5436 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5437 8138f3e1 2019-08-11 stsp int n;
5438 0ebf8283 2019-07-24 stsp
5439 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5440 0ebf8283 2019-07-24 stsp if (err)
5441 0ebf8283 2019-07-24 stsp goto done;
5442 0ebf8283 2019-07-24 stsp
5443 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5444 0ebf8283 2019-07-24 stsp if (err)
5445 0ebf8283 2019-07-24 stsp goto done;
5446 0ebf8283 2019-07-24 stsp
5447 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5448 0ebf8283 2019-07-24 stsp if (err)
5449 0ebf8283 2019-07-24 stsp goto done;
5450 0ebf8283 2019-07-24 stsp
5451 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5452 0ebf8283 2019-07-24 stsp if (n < 0)
5453 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5454 0ebf8283 2019-07-24 stsp done:
5455 0ebf8283 2019-07-24 stsp if (commit)
5456 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5457 0ebf8283 2019-07-24 stsp free(id_str);
5458 0ebf8283 2019-07-24 stsp free(logmsg);
5459 0ebf8283 2019-07-24 stsp return err;
5460 0ebf8283 2019-07-24 stsp }
5461 0ebf8283 2019-07-24 stsp
5462 0ebf8283 2019-07-24 stsp static const struct got_error *
5463 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5464 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5465 0ebf8283 2019-07-24 stsp {
5466 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5467 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5468 0ebf8283 2019-07-24 stsp
5469 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5470 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5471 0ebf8283 2019-07-24 stsp
5472 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5473 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5474 0ebf8283 2019-07-24 stsp f, repo);
5475 0ebf8283 2019-07-24 stsp if (err)
5476 0ebf8283 2019-07-24 stsp break;
5477 0ebf8283 2019-07-24 stsp }
5478 0ebf8283 2019-07-24 stsp
5479 0ebf8283 2019-07-24 stsp return err;
5480 0ebf8283 2019-07-24 stsp }
5481 0ebf8283 2019-07-24 stsp
5482 0ebf8283 2019-07-24 stsp static const struct got_error *
5483 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5484 0ebf8283 2019-07-24 stsp {
5485 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5486 0ebf8283 2019-07-24 stsp int n, i;
5487 0ebf8283 2019-07-24 stsp
5488 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5489 0ebf8283 2019-07-24 stsp if (n < 0)
5490 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5491 0ebf8283 2019-07-24 stsp
5492 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5493 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5494 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5495 0ebf8283 2019-07-24 stsp cmd->desc);
5496 0ebf8283 2019-07-24 stsp if (n < 0) {
5497 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5498 0ebf8283 2019-07-24 stsp break;
5499 0ebf8283 2019-07-24 stsp }
5500 0ebf8283 2019-07-24 stsp }
5501 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5502 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5503 0ebf8283 2019-07-24 stsp if (n < 0)
5504 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5505 0ebf8283 2019-07-24 stsp return err;
5506 0ebf8283 2019-07-24 stsp }
5507 0ebf8283 2019-07-24 stsp
5508 0ebf8283 2019-07-24 stsp static const struct got_error *
5509 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5510 0ebf8283 2019-07-24 stsp {
5511 0ebf8283 2019-07-24 stsp static char msg[42];
5512 0ebf8283 2019-07-24 stsp int ret;
5513 0ebf8283 2019-07-24 stsp
5514 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5515 0ebf8283 2019-07-24 stsp lineno);
5516 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5517 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5518 0ebf8283 2019-07-24 stsp
5519 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5520 0ebf8283 2019-07-24 stsp }
5521 0ebf8283 2019-07-24 stsp
5522 0ebf8283 2019-07-24 stsp static const struct got_error *
5523 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5524 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5525 0ebf8283 2019-07-24 stsp {
5526 0ebf8283 2019-07-24 stsp const struct got_error *err;
5527 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5528 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5529 0ebf8283 2019-07-24 stsp
5530 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5531 0ebf8283 2019-07-24 stsp if (err)
5532 0ebf8283 2019-07-24 stsp return err;
5533 0ebf8283 2019-07-24 stsp
5534 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5535 0ebf8283 2019-07-24 stsp if (err)
5536 0ebf8283 2019-07-24 stsp goto done;
5537 0ebf8283 2019-07-24 stsp
5538 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5539 5943eee2 2019-08-13 stsp if (err)
5540 5943eee2 2019-08-13 stsp goto done;
5541 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5542 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5543 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5544 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5545 0ebf8283 2019-07-24 stsp goto done;
5546 0ebf8283 2019-07-24 stsp }
5547 0ebf8283 2019-07-24 stsp done:
5548 0ebf8283 2019-07-24 stsp if (folded_commit)
5549 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5550 0ebf8283 2019-07-24 stsp free(id_str);
5551 5943eee2 2019-08-13 stsp free(folded_logmsg);
5552 0ebf8283 2019-07-24 stsp return err;
5553 0ebf8283 2019-07-24 stsp }
5554 0ebf8283 2019-07-24 stsp
5555 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5556 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5557 0ebf8283 2019-07-24 stsp {
5558 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5559 0ebf8283 2019-07-24 stsp
5560 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5561 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5562 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5563 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5564 3f9de99f 2019-07-24 stsp folded = prev;
5565 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5566 0ebf8283 2019-07-24 stsp }
5567 0ebf8283 2019-07-24 stsp
5568 0ebf8283 2019-07-24 stsp return folded;
5569 0ebf8283 2019-07-24 stsp }
5570 0ebf8283 2019-07-24 stsp
5571 0ebf8283 2019-07-24 stsp static const struct got_error *
5572 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5573 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5574 0ebf8283 2019-07-24 stsp {
5575 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5576 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5577 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5578 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5579 0ebf8283 2019-07-24 stsp int fd;
5580 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5581 0ebf8283 2019-07-24 stsp
5582 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5583 0ebf8283 2019-07-24 stsp if (err)
5584 0ebf8283 2019-07-24 stsp return err;
5585 0ebf8283 2019-07-24 stsp
5586 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5587 0ebf8283 2019-07-24 stsp if (folded) {
5588 0ebf8283 2019-07-24 stsp while (folded != hle) {
5589 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5590 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5591 3f9de99f 2019-07-24 stsp continue;
5592 3f9de99f 2019-07-24 stsp }
5593 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5594 0ebf8283 2019-07-24 stsp logmsg, repo);
5595 0ebf8283 2019-07-24 stsp if (err)
5596 0ebf8283 2019-07-24 stsp goto done;
5597 0ebf8283 2019-07-24 stsp free(logmsg);
5598 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5599 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5600 0ebf8283 2019-07-24 stsp }
5601 0ebf8283 2019-07-24 stsp }
5602 0ebf8283 2019-07-24 stsp
5603 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5604 0ebf8283 2019-07-24 stsp if (err)
5605 0ebf8283 2019-07-24 stsp goto done;
5606 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, 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,
5610 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5611 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_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 free(logmsg);
5616 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5617 0ebf8283 2019-07-24 stsp
5618 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5619 0ebf8283 2019-07-24 stsp if (err)
5620 0ebf8283 2019-07-24 stsp goto done;
5621 0ebf8283 2019-07-24 stsp
5622 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5623 0ebf8283 2019-07-24 stsp if (err)
5624 0ebf8283 2019-07-24 stsp goto done;
5625 0ebf8283 2019-07-24 stsp
5626 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5627 0ebf8283 2019-07-24 stsp close(fd);
5628 0ebf8283 2019-07-24 stsp
5629 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5630 0ebf8283 2019-07-24 stsp if (err)
5631 0ebf8283 2019-07-24 stsp goto done;
5632 0ebf8283 2019-07-24 stsp
5633 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5634 0ebf8283 2019-07-24 stsp if (err) {
5635 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5636 0ebf8283 2019-07-24 stsp goto done;
5637 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5638 0ebf8283 2019-07-24 stsp }
5639 0ebf8283 2019-07-24 stsp done:
5640 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5641 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5642 0ebf8283 2019-07-24 stsp free(logmsg_path);
5643 0ebf8283 2019-07-24 stsp free(logmsg);
5644 5943eee2 2019-08-13 stsp free(orig_logmsg);
5645 0ebf8283 2019-07-24 stsp free(editor);
5646 0ebf8283 2019-07-24 stsp if (commit)
5647 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5648 0ebf8283 2019-07-24 stsp return err;
5649 5ef14e63 2019-06-02 stsp }
5650 0ebf8283 2019-07-24 stsp
5651 0ebf8283 2019-07-24 stsp static const struct got_error *
5652 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5653 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5654 0ebf8283 2019-07-24 stsp {
5655 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5656 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5657 0ebf8283 2019-07-24 stsp size_t size;
5658 0ebf8283 2019-07-24 stsp ssize_t len;
5659 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5660 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5661 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5662 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5663 0ebf8283 2019-07-24 stsp
5664 0ebf8283 2019-07-24 stsp for (;;) {
5665 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5666 0ebf8283 2019-07-24 stsp if (len == -1) {
5667 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5668 0ebf8283 2019-07-24 stsp if (feof(f))
5669 0ebf8283 2019-07-24 stsp break;
5670 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5671 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5672 0ebf8283 2019-07-24 stsp break;
5673 0ebf8283 2019-07-24 stsp }
5674 0ebf8283 2019-07-24 stsp lineno++;
5675 0ebf8283 2019-07-24 stsp p = line;
5676 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5677 0ebf8283 2019-07-24 stsp p++;
5678 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5679 0ebf8283 2019-07-24 stsp free(line);
5680 0ebf8283 2019-07-24 stsp line = NULL;
5681 0ebf8283 2019-07-24 stsp continue;
5682 0ebf8283 2019-07-24 stsp }
5683 0ebf8283 2019-07-24 stsp cmd = NULL;
5684 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5685 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5686 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5687 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5688 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5689 0ebf8283 2019-07-24 stsp break;
5690 0ebf8283 2019-07-24 stsp }
5691 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5692 0ebf8283 2019-07-24 stsp p++;
5693 0ebf8283 2019-07-24 stsp break;
5694 0ebf8283 2019-07-24 stsp }
5695 0ebf8283 2019-07-24 stsp }
5696 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5697 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5698 0ebf8283 2019-07-24 stsp break;
5699 0ebf8283 2019-07-24 stsp }
5700 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5701 0ebf8283 2019-07-24 stsp p++;
5702 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5703 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5704 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5705 0ebf8283 2019-07-24 stsp break;
5706 0ebf8283 2019-07-24 stsp }
5707 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5708 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5709 0ebf8283 2019-07-24 stsp if (err)
5710 0ebf8283 2019-07-24 stsp break;
5711 0ebf8283 2019-07-24 stsp } else {
5712 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5713 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5714 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5715 0ebf8283 2019-07-24 stsp break;
5716 0ebf8283 2019-07-24 stsp }
5717 0ebf8283 2019-07-24 stsp }
5718 0ebf8283 2019-07-24 stsp free(line);
5719 0ebf8283 2019-07-24 stsp line = NULL;
5720 0ebf8283 2019-07-24 stsp continue;
5721 0ebf8283 2019-07-24 stsp } else {
5722 0ebf8283 2019-07-24 stsp end = p;
5723 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5724 0ebf8283 2019-07-24 stsp end++;
5725 0ebf8283 2019-07-24 stsp *end = '\0';
5726 0ebf8283 2019-07-24 stsp
5727 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5728 0ebf8283 2019-07-24 stsp if (err) {
5729 0ebf8283 2019-07-24 stsp /* override error code */
5730 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5731 0ebf8283 2019-07-24 stsp break;
5732 0ebf8283 2019-07-24 stsp }
5733 0ebf8283 2019-07-24 stsp }
5734 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5735 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5736 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5737 0ebf8283 2019-07-24 stsp break;
5738 0ebf8283 2019-07-24 stsp }
5739 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5740 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5741 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5742 0ebf8283 2019-07-24 stsp commit_id = NULL;
5743 0ebf8283 2019-07-24 stsp free(line);
5744 0ebf8283 2019-07-24 stsp line = NULL;
5745 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5746 0ebf8283 2019-07-24 stsp }
5747 0ebf8283 2019-07-24 stsp
5748 0ebf8283 2019-07-24 stsp free(line);
5749 0ebf8283 2019-07-24 stsp free(commit_id);
5750 0ebf8283 2019-07-24 stsp return err;
5751 0ebf8283 2019-07-24 stsp }
5752 0ebf8283 2019-07-24 stsp
5753 0ebf8283 2019-07-24 stsp static const struct got_error *
5754 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5755 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5756 bfce7f83 2019-07-27 stsp {
5757 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5758 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5759 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5760 bfce7f83 2019-07-27 stsp static char msg[80];
5761 bfce7f83 2019-07-27 stsp char *id_str;
5762 bfce7f83 2019-07-27 stsp
5763 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5764 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5765 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5766 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5767 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5768 bfce7f83 2019-07-27 stsp
5769 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5770 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5771 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5772 bfce7f83 2019-07-27 stsp break;
5773 bfce7f83 2019-07-27 stsp }
5774 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5775 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5776 bfce7f83 2019-07-27 stsp if (err)
5777 bfce7f83 2019-07-27 stsp return err;
5778 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5779 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5780 bfce7f83 2019-07-27 stsp free(id_str);
5781 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5782 bfce7f83 2019-07-27 stsp }
5783 bfce7f83 2019-07-27 stsp }
5784 bfce7f83 2019-07-27 stsp
5785 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5786 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5787 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5788 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5789 bfce7f83 2019-07-27 stsp
5790 bfce7f83 2019-07-27 stsp return NULL;
5791 bfce7f83 2019-07-27 stsp }
5792 bfce7f83 2019-07-27 stsp
5793 bfce7f83 2019-07-27 stsp static const struct got_error *
5794 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5795 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5796 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5797 0ebf8283 2019-07-24 stsp {
5798 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5799 0ebf8283 2019-07-24 stsp char *editor;
5800 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5801 0ebf8283 2019-07-24 stsp
5802 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5803 0ebf8283 2019-07-24 stsp if (err)
5804 0ebf8283 2019-07-24 stsp return err;
5805 0ebf8283 2019-07-24 stsp
5806 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5807 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5808 0ebf8283 2019-07-24 stsp goto done;
5809 0ebf8283 2019-07-24 stsp }
5810 0ebf8283 2019-07-24 stsp
5811 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5812 0ebf8283 2019-07-24 stsp if (f == NULL) {
5813 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5814 0ebf8283 2019-07-24 stsp goto done;
5815 0ebf8283 2019-07-24 stsp }
5816 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5817 bfce7f83 2019-07-27 stsp if (err)
5818 bfce7f83 2019-07-27 stsp goto done;
5819 bfce7f83 2019-07-27 stsp
5820 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5821 0ebf8283 2019-07-24 stsp done:
5822 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5823 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5824 0ebf8283 2019-07-24 stsp free(editor);
5825 0ebf8283 2019-07-24 stsp return err;
5826 0ebf8283 2019-07-24 stsp }
5827 0ebf8283 2019-07-24 stsp
5828 0ebf8283 2019-07-24 stsp static const struct got_error *
5829 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5830 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5831 0ebf8283 2019-07-24 stsp
5832 0ebf8283 2019-07-24 stsp static const struct got_error *
5833 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5834 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5835 0ebf8283 2019-07-24 stsp {
5836 0ebf8283 2019-07-24 stsp const struct got_error *err;
5837 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5838 0ebf8283 2019-07-24 stsp char *path = NULL;
5839 0ebf8283 2019-07-24 stsp
5840 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
5841 0ebf8283 2019-07-24 stsp if (err)
5842 0ebf8283 2019-07-24 stsp return err;
5843 0ebf8283 2019-07-24 stsp
5844 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
5845 0ebf8283 2019-07-24 stsp if (err)
5846 0ebf8283 2019-07-24 stsp goto done;
5847 0ebf8283 2019-07-24 stsp
5848 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
5849 0ebf8283 2019-07-24 stsp if (err)
5850 0ebf8283 2019-07-24 stsp goto done;
5851 0ebf8283 2019-07-24 stsp
5852 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
5853 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5854 0ebf8283 2019-07-24 stsp goto done;
5855 0ebf8283 2019-07-24 stsp }
5856 0ebf8283 2019-07-24 stsp f = NULL;
5857 0ebf8283 2019-07-24 stsp
5858 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
5859 0ebf8283 2019-07-24 stsp if (err) {
5860 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5861 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5862 0ebf8283 2019-07-24 stsp goto done;
5863 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
5864 0ebf8283 2019-07-24 stsp commits, path, repo);
5865 0ebf8283 2019-07-24 stsp }
5866 0ebf8283 2019-07-24 stsp done:
5867 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5868 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5869 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
5870 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
5871 0ebf8283 2019-07-24 stsp free(path);
5872 0ebf8283 2019-07-24 stsp return err;
5873 0ebf8283 2019-07-24 stsp }
5874 0ebf8283 2019-07-24 stsp
5875 0ebf8283 2019-07-24 stsp static const struct got_error *
5876 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
5877 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
5878 0ebf8283 2019-07-24 stsp {
5879 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5880 0ebf8283 2019-07-24 stsp char *path = NULL;
5881 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5882 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
5883 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5884 0ebf8283 2019-07-24 stsp
5885 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
5886 0ebf8283 2019-07-24 stsp if (err)
5887 0ebf8283 2019-07-24 stsp return err;
5888 0ebf8283 2019-07-24 stsp
5889 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
5890 0ebf8283 2019-07-24 stsp if (f == NULL) {
5891 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5892 0ebf8283 2019-07-24 stsp goto done;
5893 0ebf8283 2019-07-24 stsp }
5894 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5895 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
5896 0ebf8283 2019-07-24 stsp repo);
5897 0ebf8283 2019-07-24 stsp if (err)
5898 0ebf8283 2019-07-24 stsp break;
5899 0ebf8283 2019-07-24 stsp
5900 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
5901 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
5902 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
5903 0ebf8283 2019-07-24 stsp if (n < 0) {
5904 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5905 0ebf8283 2019-07-24 stsp break;
5906 0ebf8283 2019-07-24 stsp }
5907 0ebf8283 2019-07-24 stsp }
5908 0ebf8283 2019-07-24 stsp }
5909 0ebf8283 2019-07-24 stsp done:
5910 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5911 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5912 0ebf8283 2019-07-24 stsp free(path);
5913 0ebf8283 2019-07-24 stsp if (commit)
5914 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5915 0ebf8283 2019-07-24 stsp return err;
5916 0ebf8283 2019-07-24 stsp }
5917 0ebf8283 2019-07-24 stsp
5918 bfce7f83 2019-07-27 stsp void
5919 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
5920 bfce7f83 2019-07-27 stsp {
5921 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5922 bfce7f83 2019-07-27 stsp
5923 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
5924 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
5925 bfce7f83 2019-07-27 stsp free(hle);
5926 bfce7f83 2019-07-27 stsp }
5927 bfce7f83 2019-07-27 stsp }
5928 bfce7f83 2019-07-27 stsp
5929 0ebf8283 2019-07-24 stsp static const struct got_error *
5930 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
5931 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5932 0ebf8283 2019-07-24 stsp {
5933 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5934 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5935 0ebf8283 2019-07-24 stsp
5936 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5937 0ebf8283 2019-07-24 stsp if (f == NULL) {
5938 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
5939 0ebf8283 2019-07-24 stsp goto done;
5940 0ebf8283 2019-07-24 stsp }
5941 0ebf8283 2019-07-24 stsp
5942 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5943 0ebf8283 2019-07-24 stsp done:
5944 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5945 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5946 0ebf8283 2019-07-24 stsp return err;
5947 0ebf8283 2019-07-24 stsp }
5948 0ebf8283 2019-07-24 stsp
5949 0ebf8283 2019-07-24 stsp static const struct got_error *
5950 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
5951 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
5952 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
5953 0ebf8283 2019-07-24 stsp {
5954 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
5955 0ebf8283 2019-07-24 stsp int resp = ' ';
5956 0ebf8283 2019-07-24 stsp
5957 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
5958 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
5959 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
5960 0ebf8283 2019-07-24 stsp resp = getchar();
5961 a61a4414 2019-08-07 stsp if (resp == '\n')
5962 a61a4414 2019-08-07 stsp resp = getchar();
5963 426ebf2e 2019-07-25 stsp if (resp == 'c') {
5964 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5965 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
5966 bfce7f83 2019-07-27 stsp repo);
5967 426ebf2e 2019-07-25 stsp if (err) {
5968 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5969 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5970 426ebf2e 2019-07-25 stsp break;
5971 bfce7f83 2019-07-27 stsp prev_err = err;
5972 426ebf2e 2019-07-25 stsp resp = ' ';
5973 426ebf2e 2019-07-25 stsp continue;
5974 426ebf2e 2019-07-25 stsp }
5975 bfce7f83 2019-07-27 stsp break;
5976 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
5977 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
5978 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
5979 0ebf8283 2019-07-24 stsp commits, repo);
5980 426ebf2e 2019-07-25 stsp if (err) {
5981 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
5982 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
5983 426ebf2e 2019-07-25 stsp break;
5984 bfce7f83 2019-07-27 stsp prev_err = err;
5985 426ebf2e 2019-07-25 stsp resp = ' ';
5986 426ebf2e 2019-07-25 stsp continue;
5987 426ebf2e 2019-07-25 stsp }
5988 bfce7f83 2019-07-27 stsp break;
5989 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
5990 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
5991 0ebf8283 2019-07-24 stsp break;
5992 426ebf2e 2019-07-25 stsp } else
5993 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
5994 0ebf8283 2019-07-24 stsp }
5995 0ebf8283 2019-07-24 stsp
5996 0ebf8283 2019-07-24 stsp return err;
5997 0ebf8283 2019-07-24 stsp }
5998 0ebf8283 2019-07-24 stsp
5999 0ebf8283 2019-07-24 stsp static const struct got_error *
6000 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6001 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6002 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6003 0ebf8283 2019-07-24 stsp {
6004 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6005 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6006 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6007 3e3a69f1 2019-07-25 stsp branch, repo);
6008 0ebf8283 2019-07-24 stsp }
6009 0ebf8283 2019-07-24 stsp
6010 0ebf8283 2019-07-24 stsp static const struct got_error *
6011 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6012 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6013 0ebf8283 2019-07-24 stsp {
6014 0ebf8283 2019-07-24 stsp const struct got_error *err;
6015 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6016 0ebf8283 2019-07-24 stsp
6017 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6018 0ebf8283 2019-07-24 stsp if (err)
6019 0ebf8283 2019-07-24 stsp goto done;
6020 0ebf8283 2019-07-24 stsp
6021 0ebf8283 2019-07-24 stsp if (new_id) {
6022 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6023 0ebf8283 2019-07-24 stsp if (err)
6024 0ebf8283 2019-07-24 stsp goto done;
6025 0ebf8283 2019-07-24 stsp }
6026 0ebf8283 2019-07-24 stsp
6027 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6028 0ebf8283 2019-07-24 stsp if (new_id_str)
6029 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6030 0ebf8283 2019-07-24 stsp
6031 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6032 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6033 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6034 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6035 0ebf8283 2019-07-24 stsp goto done;
6036 0ebf8283 2019-07-24 stsp }
6037 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6038 0ebf8283 2019-07-24 stsp } else {
6039 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6040 0ebf8283 2019-07-24 stsp if (err)
6041 0ebf8283 2019-07-24 stsp goto done;
6042 0ebf8283 2019-07-24 stsp }
6043 0ebf8283 2019-07-24 stsp
6044 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6045 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6046 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6047 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6048 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6049 0ebf8283 2019-07-24 stsp break;
6050 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6051 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6052 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6053 0ebf8283 2019-07-24 stsp logmsg);
6054 0ebf8283 2019-07-24 stsp break;
6055 0ebf8283 2019-07-24 stsp default:
6056 0ebf8283 2019-07-24 stsp break;
6057 0ebf8283 2019-07-24 stsp }
6058 0ebf8283 2019-07-24 stsp
6059 0ebf8283 2019-07-24 stsp done:
6060 0ebf8283 2019-07-24 stsp free(old_id_str);
6061 0ebf8283 2019-07-24 stsp free(new_id_str);
6062 0ebf8283 2019-07-24 stsp return err;
6063 0ebf8283 2019-07-24 stsp }
6064 0ebf8283 2019-07-24 stsp
6065 0ebf8283 2019-07-24 stsp static const struct got_error *
6066 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6067 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6068 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6069 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6070 0ebf8283 2019-07-24 stsp {
6071 0ebf8283 2019-07-24 stsp const struct got_error *err;
6072 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6073 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6074 0ebf8283 2019-07-24 stsp
6075 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6076 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6077 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6078 0ebf8283 2019-07-24 stsp if (err)
6079 0ebf8283 2019-07-24 stsp return err;
6080 0ebf8283 2019-07-24 stsp }
6081 0ebf8283 2019-07-24 stsp
6082 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6083 0ebf8283 2019-07-24 stsp if (err)
6084 0ebf8283 2019-07-24 stsp return err;
6085 0ebf8283 2019-07-24 stsp
6086 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6087 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6088 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6089 0ebf8283 2019-07-24 stsp if (err) {
6090 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6091 0ebf8283 2019-07-24 stsp goto done;
6092 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6093 0ebf8283 2019-07-24 stsp } else {
6094 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6095 0ebf8283 2019-07-24 stsp free(new_commit_id);
6096 0ebf8283 2019-07-24 stsp }
6097 0ebf8283 2019-07-24 stsp done:
6098 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6099 0ebf8283 2019-07-24 stsp return err;
6100 0ebf8283 2019-07-24 stsp }
6101 0ebf8283 2019-07-24 stsp
6102 0ebf8283 2019-07-24 stsp static const struct got_error *
6103 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6104 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6105 0ebf8283 2019-07-24 stsp {
6106 0ebf8283 2019-07-24 stsp const struct got_error *error;
6107 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6108 0ebf8283 2019-07-24 stsp
6109 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6110 0ebf8283 2019-07-24 stsp repo);
6111 0ebf8283 2019-07-24 stsp if (error)
6112 0ebf8283 2019-07-24 stsp return error;
6113 0ebf8283 2019-07-24 stsp
6114 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6115 0ebf8283 2019-07-24 stsp if (error)
6116 0ebf8283 2019-07-24 stsp return error;
6117 0ebf8283 2019-07-24 stsp
6118 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6119 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6120 0ebf8283 2019-07-24 stsp return error;
6121 0ebf8283 2019-07-24 stsp }
6122 0ebf8283 2019-07-24 stsp
6123 0ebf8283 2019-07-24 stsp static const struct got_error *
6124 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6125 0ebf8283 2019-07-24 stsp {
6126 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6127 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6128 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6129 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6130 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6131 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6132 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6133 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6134 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6135 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6136 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6137 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6138 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6139 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6140 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6141 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6142 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6143 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6144 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6145 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6146 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6147 0ebf8283 2019-07-24 stsp
6148 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6149 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6150 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6151 0ebf8283 2019-07-24 stsp
6152 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6153 0ebf8283 2019-07-24 stsp switch (ch) {
6154 0ebf8283 2019-07-24 stsp case 'a':
6155 0ebf8283 2019-07-24 stsp abort_edit = 1;
6156 0ebf8283 2019-07-24 stsp break;
6157 0ebf8283 2019-07-24 stsp case 'c':
6158 0ebf8283 2019-07-24 stsp continue_edit = 1;
6159 0ebf8283 2019-07-24 stsp break;
6160 0ebf8283 2019-07-24 stsp case 'F':
6161 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6162 0ebf8283 2019-07-24 stsp break;
6163 0ebf8283 2019-07-24 stsp default:
6164 0ebf8283 2019-07-24 stsp usage_histedit();
6165 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6166 0ebf8283 2019-07-24 stsp }
6167 0ebf8283 2019-07-24 stsp }
6168 0ebf8283 2019-07-24 stsp
6169 0ebf8283 2019-07-24 stsp argc -= optind;
6170 0ebf8283 2019-07-24 stsp argv += optind;
6171 0ebf8283 2019-07-24 stsp
6172 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6173 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6174 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6175 0ebf8283 2019-07-24 stsp err(1, "pledge");
6176 0ebf8283 2019-07-24 stsp #endif
6177 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6178 0ebf8283 2019-07-24 stsp usage_histedit();
6179 0ebf8283 2019-07-24 stsp if (argc != 0)
6180 0ebf8283 2019-07-24 stsp usage_histedit();
6181 0ebf8283 2019-07-24 stsp
6182 0ebf8283 2019-07-24 stsp /*
6183 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6184 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6185 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6186 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6187 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6188 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6189 0ebf8283 2019-07-24 stsp */
6190 0ebf8283 2019-07-24 stsp
6191 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6192 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6193 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6194 0ebf8283 2019-07-24 stsp goto done;
6195 0ebf8283 2019-07-24 stsp }
6196 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6197 0ebf8283 2019-07-24 stsp if (error)
6198 0ebf8283 2019-07-24 stsp goto done;
6199 0ebf8283 2019-07-24 stsp
6200 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6201 c9956ddf 2019-09-08 stsp NULL);
6202 0ebf8283 2019-07-24 stsp if (error != NULL)
6203 0ebf8283 2019-07-24 stsp goto done;
6204 0ebf8283 2019-07-24 stsp
6205 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6206 0ebf8283 2019-07-24 stsp if (error)
6207 0ebf8283 2019-07-24 stsp goto done;
6208 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6209 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6210 0ebf8283 2019-07-24 stsp goto done;
6211 0ebf8283 2019-07-24 stsp }
6212 0ebf8283 2019-07-24 stsp
6213 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6214 0ebf8283 2019-07-24 stsp if (error)
6215 0ebf8283 2019-07-24 stsp goto done;
6216 0ebf8283 2019-07-24 stsp
6217 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6218 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6219 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6220 3e3a69f1 2019-07-25 stsp worktree, repo);
6221 0ebf8283 2019-07-24 stsp if (error)
6222 0ebf8283 2019-07-24 stsp goto done;
6223 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6224 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6225 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6226 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6227 0ebf8283 2019-07-24 stsp if (error)
6228 0ebf8283 2019-07-24 stsp goto done;
6229 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6230 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6231 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6232 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6233 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6234 0ebf8283 2019-07-24 stsp goto done;
6235 0ebf8283 2019-07-24 stsp }
6236 0ebf8283 2019-07-24 stsp
6237 0ebf8283 2019-07-24 stsp if (continue_edit) {
6238 0ebf8283 2019-07-24 stsp char *path;
6239 0ebf8283 2019-07-24 stsp
6240 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6241 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6242 0ebf8283 2019-07-24 stsp goto done;
6243 0ebf8283 2019-07-24 stsp }
6244 0ebf8283 2019-07-24 stsp
6245 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6246 0ebf8283 2019-07-24 stsp if (error)
6247 0ebf8283 2019-07-24 stsp goto done;
6248 0ebf8283 2019-07-24 stsp
6249 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6250 0ebf8283 2019-07-24 stsp free(path);
6251 0ebf8283 2019-07-24 stsp if (error)
6252 0ebf8283 2019-07-24 stsp goto done;
6253 0ebf8283 2019-07-24 stsp
6254 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6255 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6256 3e3a69f1 2019-07-25 stsp worktree, repo);
6257 0ebf8283 2019-07-24 stsp if (error)
6258 0ebf8283 2019-07-24 stsp goto done;
6259 0ebf8283 2019-07-24 stsp
6260 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6261 0ebf8283 2019-07-24 stsp if (error)
6262 0ebf8283 2019-07-24 stsp goto done;
6263 0ebf8283 2019-07-24 stsp
6264 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6265 0ebf8283 2019-07-24 stsp head_commit_id);
6266 0ebf8283 2019-07-24 stsp if (error)
6267 0ebf8283 2019-07-24 stsp goto done;
6268 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6269 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6270 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6271 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6272 3aac7cf7 2019-07-25 stsp goto done;
6273 3aac7cf7 2019-07-25 stsp }
6274 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6275 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6276 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6277 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6278 0ebf8283 2019-07-24 stsp commit = NULL;
6279 0ebf8283 2019-07-24 stsp if (error)
6280 0ebf8283 2019-07-24 stsp goto done;
6281 0ebf8283 2019-07-24 stsp } else {
6282 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6283 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6284 0ebf8283 2019-07-24 stsp goto done;
6285 0ebf8283 2019-07-24 stsp }
6286 0ebf8283 2019-07-24 stsp
6287 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6288 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6289 0ebf8283 2019-07-24 stsp if (error != NULL)
6290 0ebf8283 2019-07-24 stsp goto done;
6291 0ebf8283 2019-07-24 stsp
6292 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6293 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6294 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6295 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6296 c7d20a3f 2019-07-30 stsp goto done;
6297 c7d20a3f 2019-07-30 stsp }
6298 c7d20a3f 2019-07-30 stsp
6299 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6300 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6301 bfce7f83 2019-07-27 stsp branch = NULL;
6302 0ebf8283 2019-07-24 stsp if (error)
6303 0ebf8283 2019-07-24 stsp goto done;
6304 0ebf8283 2019-07-24 stsp
6305 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6306 0ebf8283 2019-07-24 stsp head_commit_id);
6307 0ebf8283 2019-07-24 stsp if (error)
6308 0ebf8283 2019-07-24 stsp goto done;
6309 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6310 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6311 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6312 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6313 3aac7cf7 2019-07-25 stsp goto done;
6314 3aac7cf7 2019-07-25 stsp }
6315 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6316 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6317 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6318 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6319 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6320 0ebf8283 2019-07-24 stsp commit = NULL;
6321 0ebf8283 2019-07-24 stsp if (error)
6322 0ebf8283 2019-07-24 stsp goto done;
6323 0ebf8283 2019-07-24 stsp
6324 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6325 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6326 bfce7f83 2019-07-27 stsp if (error)
6327 bfce7f83 2019-07-27 stsp goto done;
6328 bfce7f83 2019-07-27 stsp
6329 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6330 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6331 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6332 6ba2bea2 2019-07-27 stsp if (error) {
6333 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6334 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6335 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6336 0ebf8283 2019-07-24 stsp goto done;
6337 6ba2bea2 2019-07-27 stsp }
6338 0ebf8283 2019-07-24 stsp } else {
6339 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6340 0ebf8283 2019-07-24 stsp repo);
6341 6ba2bea2 2019-07-27 stsp if (error) {
6342 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6343 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6344 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6345 0ebf8283 2019-07-24 stsp goto done;
6346 6ba2bea2 2019-07-27 stsp }
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp }
6349 0ebf8283 2019-07-24 stsp
6350 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6351 0ebf8283 2019-07-24 stsp repo);
6352 6ba2bea2 2019-07-27 stsp if (error) {
6353 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6354 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6355 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6356 0ebf8283 2019-07-24 stsp goto done;
6357 6ba2bea2 2019-07-27 stsp }
6358 0ebf8283 2019-07-24 stsp
6359 0ebf8283 2019-07-24 stsp }
6360 0ebf8283 2019-07-24 stsp
6361 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6362 4ec14e60 2019-08-28 hiltjo if (error)
6363 0ebf8283 2019-07-24 stsp goto done;
6364 0ebf8283 2019-07-24 stsp
6365 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6366 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6367 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6368 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6369 0ebf8283 2019-07-24 stsp continue;
6370 0ebf8283 2019-07-24 stsp
6371 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6372 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6373 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6374 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6375 0ebf8283 2019-07-24 stsp repo);
6376 0ebf8283 2019-07-24 stsp } else {
6377 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6378 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6379 0ebf8283 2019-07-24 stsp }
6380 0ebf8283 2019-07-24 stsp if (error)
6381 0ebf8283 2019-07-24 stsp goto done;
6382 0ebf8283 2019-07-24 stsp continue;
6383 0ebf8283 2019-07-24 stsp }
6384 0ebf8283 2019-07-24 stsp
6385 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6386 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6387 0ebf8283 2019-07-24 stsp if (error)
6388 0ebf8283 2019-07-24 stsp goto done;
6389 0ebf8283 2019-07-24 stsp continue;
6390 0ebf8283 2019-07-24 stsp }
6391 0ebf8283 2019-07-24 stsp
6392 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6393 0ebf8283 2019-07-24 stsp hle->commit_id);
6394 0ebf8283 2019-07-24 stsp if (error)
6395 0ebf8283 2019-07-24 stsp goto done;
6396 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6397 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6398 0ebf8283 2019-07-24 stsp
6399 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6400 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6401 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6402 0ebf8283 2019-07-24 stsp if (error)
6403 0ebf8283 2019-07-24 stsp goto done;
6404 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6405 0ebf8283 2019-07-24 stsp commit = NULL;
6406 0ebf8283 2019-07-24 stsp
6407 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6408 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6409 0ebf8283 2019-07-24 stsp break;
6410 0ebf8283 2019-07-24 stsp }
6411 0ebf8283 2019-07-24 stsp
6412 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6413 0ebf8283 2019-07-24 stsp char *id_str;
6414 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6415 0ebf8283 2019-07-24 stsp if (error)
6416 0ebf8283 2019-07-24 stsp goto done;
6417 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6418 0ebf8283 2019-07-24 stsp id_str);
6419 0ebf8283 2019-07-24 stsp free(id_str);
6420 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6421 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6422 3e3a69f1 2019-07-25 stsp fileindex);
6423 0ebf8283 2019-07-24 stsp goto done;
6424 0ebf8283 2019-07-24 stsp }
6425 0ebf8283 2019-07-24 stsp
6426 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6427 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6428 0ebf8283 2019-07-24 stsp if (error)
6429 0ebf8283 2019-07-24 stsp goto done;
6430 0ebf8283 2019-07-24 stsp continue;
6431 0ebf8283 2019-07-24 stsp }
6432 0ebf8283 2019-07-24 stsp
6433 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6434 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6435 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6436 0ebf8283 2019-07-24 stsp if (error)
6437 0ebf8283 2019-07-24 stsp goto done;
6438 0ebf8283 2019-07-24 stsp }
6439 0ebf8283 2019-07-24 stsp
6440 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6441 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6442 0ebf8283 2019-07-24 stsp if (error)
6443 0ebf8283 2019-07-24 stsp goto done;
6444 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6445 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6446 0ebf8283 2019-07-24 stsp } else
6447 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6448 3e3a69f1 2019-07-25 stsp branch, repo);
6449 0ebf8283 2019-07-24 stsp done:
6450 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6451 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6452 0ebf8283 2019-07-24 stsp free(head_commit_id);
6453 0ebf8283 2019-07-24 stsp free(base_commit_id);
6454 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6455 0ebf8283 2019-07-24 stsp if (commit)
6456 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6457 0ebf8283 2019-07-24 stsp if (branch)
6458 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6459 0ebf8283 2019-07-24 stsp if (tmp_branch)
6460 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6461 0ebf8283 2019-07-24 stsp if (worktree)
6462 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6463 2822a352 2019-10-15 stsp if (repo)
6464 2822a352 2019-10-15 stsp got_repo_close(repo);
6465 2822a352 2019-10-15 stsp return error;
6466 2822a352 2019-10-15 stsp }
6467 2822a352 2019-10-15 stsp
6468 2822a352 2019-10-15 stsp __dead static void
6469 2822a352 2019-10-15 stsp usage_integrate(void)
6470 2822a352 2019-10-15 stsp {
6471 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6472 2822a352 2019-10-15 stsp exit(1);
6473 2822a352 2019-10-15 stsp }
6474 2822a352 2019-10-15 stsp
6475 2822a352 2019-10-15 stsp static const struct got_error *
6476 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6477 2822a352 2019-10-15 stsp {
6478 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6479 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6480 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6481 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6482 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6483 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6484 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6485 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6486 2822a352 2019-10-15 stsp int ch, did_something = 0;
6487 2822a352 2019-10-15 stsp
6488 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6489 2822a352 2019-10-15 stsp switch (ch) {
6490 2822a352 2019-10-15 stsp default:
6491 2822a352 2019-10-15 stsp usage_integrate();
6492 2822a352 2019-10-15 stsp /* NOTREACHED */
6493 2822a352 2019-10-15 stsp }
6494 2822a352 2019-10-15 stsp }
6495 2822a352 2019-10-15 stsp
6496 2822a352 2019-10-15 stsp argc -= optind;
6497 2822a352 2019-10-15 stsp argv += optind;
6498 2822a352 2019-10-15 stsp
6499 2822a352 2019-10-15 stsp if (argc != 1)
6500 2822a352 2019-10-15 stsp usage_integrate();
6501 2822a352 2019-10-15 stsp branch_arg = argv[0];
6502 2822a352 2019-10-15 stsp
6503 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6504 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6505 2822a352 2019-10-15 stsp err(1, "pledge");
6506 2822a352 2019-10-15 stsp
6507 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6508 2822a352 2019-10-15 stsp if (cwd == NULL) {
6509 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6510 2822a352 2019-10-15 stsp goto done;
6511 2822a352 2019-10-15 stsp }
6512 2822a352 2019-10-15 stsp
6513 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6514 2822a352 2019-10-15 stsp if (error)
6515 2822a352 2019-10-15 stsp goto done;
6516 2822a352 2019-10-15 stsp
6517 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6518 2822a352 2019-10-15 stsp if (error)
6519 2822a352 2019-10-15 stsp goto done;
6520 2822a352 2019-10-15 stsp
6521 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6522 2822a352 2019-10-15 stsp NULL);
6523 2822a352 2019-10-15 stsp if (error != NULL)
6524 2822a352 2019-10-15 stsp goto done;
6525 2822a352 2019-10-15 stsp
6526 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6527 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6528 2822a352 2019-10-15 stsp if (error)
6529 2822a352 2019-10-15 stsp goto done;
6530 2822a352 2019-10-15 stsp
6531 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6532 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6533 2822a352 2019-10-15 stsp goto done;
6534 2822a352 2019-10-15 stsp }
6535 2822a352 2019-10-15 stsp
6536 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6537 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6538 2822a352 2019-10-15 stsp if (error)
6539 2822a352 2019-10-15 stsp goto done;
6540 2822a352 2019-10-15 stsp
6541 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6542 2822a352 2019-10-15 stsp if (refname == NULL) {
6543 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6544 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6545 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6546 2822a352 2019-10-15 stsp goto done;
6547 2822a352 2019-10-15 stsp }
6548 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6549 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6550 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6551 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6552 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6553 2822a352 2019-10-15 stsp goto done;
6554 2822a352 2019-10-15 stsp }
6555 2822a352 2019-10-15 stsp
6556 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6557 2822a352 2019-10-15 stsp if (error)
6558 2822a352 2019-10-15 stsp goto done;
6559 2822a352 2019-10-15 stsp
6560 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6561 2822a352 2019-10-15 stsp if (error)
6562 2822a352 2019-10-15 stsp goto done;
6563 2822a352 2019-10-15 stsp
6564 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6565 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6566 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6567 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6568 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6569 2822a352 2019-10-15 stsp goto done;
6570 2822a352 2019-10-15 stsp }
6571 2822a352 2019-10-15 stsp
6572 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6573 2822a352 2019-10-15 stsp if (error) {
6574 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6575 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6576 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6577 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
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_integrate_continue(worktree, fileindex, repo,
6582 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6583 2822a352 2019-10-15 stsp check_cancelled, NULL);
6584 2822a352 2019-10-15 stsp if (error)
6585 2822a352 2019-10-15 stsp goto done;
6586 2822a352 2019-10-15 stsp
6587 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6588 2822a352 2019-10-15 stsp done:
6589 715dc77e 2019-08-03 stsp if (repo)
6590 715dc77e 2019-08-03 stsp got_repo_close(repo);
6591 2822a352 2019-10-15 stsp if (worktree)
6592 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6593 2822a352 2019-10-15 stsp free(cwd);
6594 2822a352 2019-10-15 stsp free(base_commit_id);
6595 2822a352 2019-10-15 stsp free(commit_id);
6596 2822a352 2019-10-15 stsp free(refname);
6597 2822a352 2019-10-15 stsp free(base_refname);
6598 715dc77e 2019-08-03 stsp return error;
6599 715dc77e 2019-08-03 stsp }
6600 715dc77e 2019-08-03 stsp
6601 715dc77e 2019-08-03 stsp __dead static void
6602 715dc77e 2019-08-03 stsp usage_stage(void)
6603 715dc77e 2019-08-03 stsp {
6604 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6605 f3055044 2019-08-07 stsp "[file-path ...]\n",
6606 715dc77e 2019-08-03 stsp getprogname());
6607 715dc77e 2019-08-03 stsp exit(1);
6608 715dc77e 2019-08-03 stsp }
6609 715dc77e 2019-08-03 stsp
6610 715dc77e 2019-08-03 stsp static const struct got_error *
6611 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6612 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6613 408b4ebc 2019-08-03 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id)
6614 408b4ebc 2019-08-03 stsp {
6615 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6616 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6617 408b4ebc 2019-08-03 stsp
6618 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6619 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6620 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6621 408b4ebc 2019-08-03 stsp return NULL;
6622 408b4ebc 2019-08-03 stsp
6623 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6624 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6625 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6626 408b4ebc 2019-08-03 stsp else
6627 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6628 408b4ebc 2019-08-03 stsp if (err)
6629 408b4ebc 2019-08-03 stsp return err;
6630 408b4ebc 2019-08-03 stsp
6631 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6632 408b4ebc 2019-08-03 stsp free(id_str);
6633 dc424a06 2019-08-07 stsp return NULL;
6634 dc424a06 2019-08-07 stsp }
6635 2e1f37b0 2019-08-08 stsp
6636 dc424a06 2019-08-07 stsp static const struct got_error *
6637 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6638 715dc77e 2019-08-03 stsp {
6639 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6640 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6641 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6642 715dc77e 2019-08-03 stsp char *cwd = NULL;
6643 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6644 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6645 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6646 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6647 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6648 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6649 715dc77e 2019-08-03 stsp
6650 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6651 715dc77e 2019-08-03 stsp
6652 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6653 715dc77e 2019-08-03 stsp switch (ch) {
6654 408b4ebc 2019-08-03 stsp case 'l':
6655 408b4ebc 2019-08-03 stsp list_stage = 1;
6656 408b4ebc 2019-08-03 stsp break;
6657 dc424a06 2019-08-07 stsp case 'p':
6658 dc424a06 2019-08-07 stsp pflag = 1;
6659 dc424a06 2019-08-07 stsp break;
6660 dc424a06 2019-08-07 stsp case 'F':
6661 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6662 dc424a06 2019-08-07 stsp break;
6663 715dc77e 2019-08-03 stsp default:
6664 715dc77e 2019-08-03 stsp usage_stage();
6665 715dc77e 2019-08-03 stsp /* NOTREACHED */
6666 715dc77e 2019-08-03 stsp }
6667 715dc77e 2019-08-03 stsp }
6668 715dc77e 2019-08-03 stsp
6669 715dc77e 2019-08-03 stsp argc -= optind;
6670 715dc77e 2019-08-03 stsp argv += optind;
6671 715dc77e 2019-08-03 stsp
6672 715dc77e 2019-08-03 stsp #ifndef PROFILE
6673 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6674 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6675 715dc77e 2019-08-03 stsp err(1, "pledge");
6676 715dc77e 2019-08-03 stsp #endif
6677 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6678 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6679 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6680 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6681 715dc77e 2019-08-03 stsp
6682 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6683 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6684 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6685 715dc77e 2019-08-03 stsp goto done;
6686 715dc77e 2019-08-03 stsp }
6687 715dc77e 2019-08-03 stsp
6688 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6689 715dc77e 2019-08-03 stsp if (error)
6690 715dc77e 2019-08-03 stsp goto done;
6691 715dc77e 2019-08-03 stsp
6692 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6693 c9956ddf 2019-09-08 stsp NULL);
6694 715dc77e 2019-08-03 stsp if (error != NULL)
6695 715dc77e 2019-08-03 stsp goto done;
6696 715dc77e 2019-08-03 stsp
6697 dc424a06 2019-08-07 stsp if (patch_script_path) {
6698 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6699 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6700 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6701 dc424a06 2019-08-07 stsp patch_script_path);
6702 dc424a06 2019-08-07 stsp goto done;
6703 dc424a06 2019-08-07 stsp }
6704 dc424a06 2019-08-07 stsp }
6705 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6706 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6707 715dc77e 2019-08-03 stsp if (error)
6708 715dc77e 2019-08-03 stsp goto done;
6709 715dc77e 2019-08-03 stsp
6710 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6711 6d022e97 2019-08-04 stsp if (error)
6712 6d022e97 2019-08-04 stsp goto done;
6713 715dc77e 2019-08-03 stsp
6714 6d022e97 2019-08-04 stsp if (list_stage)
6715 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6716 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6717 2e1f37b0 2019-08-08 stsp else {
6718 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6719 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6720 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6721 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6722 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6723 2e1f37b0 2019-08-08 stsp }
6724 ad493afc 2019-08-03 stsp done:
6725 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6726 2e1f37b0 2019-08-08 stsp error == NULL)
6727 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6728 ad493afc 2019-08-03 stsp if (repo)
6729 ad493afc 2019-08-03 stsp got_repo_close(repo);
6730 ad493afc 2019-08-03 stsp if (worktree)
6731 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6732 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6733 ad493afc 2019-08-03 stsp free((char *)pe->path);
6734 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6735 ad493afc 2019-08-03 stsp free(cwd);
6736 ad493afc 2019-08-03 stsp return error;
6737 ad493afc 2019-08-03 stsp }
6738 ad493afc 2019-08-03 stsp
6739 ad493afc 2019-08-03 stsp __dead static void
6740 ad493afc 2019-08-03 stsp usage_unstage(void)
6741 ad493afc 2019-08-03 stsp {
6742 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6743 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6744 ad493afc 2019-08-03 stsp getprogname());
6745 ad493afc 2019-08-03 stsp exit(1);
6746 ad493afc 2019-08-03 stsp }
6747 ad493afc 2019-08-03 stsp
6748 ad493afc 2019-08-03 stsp
6749 ad493afc 2019-08-03 stsp static const struct got_error *
6750 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6751 ad493afc 2019-08-03 stsp {
6752 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6753 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6754 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6755 ad493afc 2019-08-03 stsp char *cwd = NULL;
6756 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6757 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6758 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6759 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6760 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6761 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6762 ad493afc 2019-08-03 stsp
6763 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6764 ad493afc 2019-08-03 stsp
6765 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6766 ad493afc 2019-08-03 stsp switch (ch) {
6767 2e1f37b0 2019-08-08 stsp case 'p':
6768 2e1f37b0 2019-08-08 stsp pflag = 1;
6769 2e1f37b0 2019-08-08 stsp break;
6770 2e1f37b0 2019-08-08 stsp case 'F':
6771 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6772 2e1f37b0 2019-08-08 stsp break;
6773 ad493afc 2019-08-03 stsp default:
6774 ad493afc 2019-08-03 stsp usage_unstage();
6775 ad493afc 2019-08-03 stsp /* NOTREACHED */
6776 ad493afc 2019-08-03 stsp }
6777 ad493afc 2019-08-03 stsp }
6778 ad493afc 2019-08-03 stsp
6779 ad493afc 2019-08-03 stsp argc -= optind;
6780 ad493afc 2019-08-03 stsp argv += optind;
6781 ad493afc 2019-08-03 stsp
6782 ad493afc 2019-08-03 stsp #ifndef PROFILE
6783 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6784 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6785 ad493afc 2019-08-03 stsp err(1, "pledge");
6786 ad493afc 2019-08-03 stsp #endif
6787 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6788 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6789 2e1f37b0 2019-08-08 stsp
6790 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6791 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6792 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6793 ad493afc 2019-08-03 stsp goto done;
6794 ad493afc 2019-08-03 stsp }
6795 ad493afc 2019-08-03 stsp
6796 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6797 ad493afc 2019-08-03 stsp if (error)
6798 ad493afc 2019-08-03 stsp goto done;
6799 ad493afc 2019-08-03 stsp
6800 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6801 c9956ddf 2019-09-08 stsp NULL);
6802 ad493afc 2019-08-03 stsp if (error != NULL)
6803 ad493afc 2019-08-03 stsp goto done;
6804 ad493afc 2019-08-03 stsp
6805 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6806 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6807 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6808 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6809 2e1f37b0 2019-08-08 stsp patch_script_path);
6810 2e1f37b0 2019-08-08 stsp goto done;
6811 2e1f37b0 2019-08-08 stsp }
6812 2e1f37b0 2019-08-08 stsp }
6813 2e1f37b0 2019-08-08 stsp
6814 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6815 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6816 ad493afc 2019-08-03 stsp if (error)
6817 ad493afc 2019-08-03 stsp goto done;
6818 ad493afc 2019-08-03 stsp
6819 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6820 ad493afc 2019-08-03 stsp if (error)
6821 ad493afc 2019-08-03 stsp goto done;
6822 ad493afc 2019-08-03 stsp
6823 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6824 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6825 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6826 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6827 715dc77e 2019-08-03 stsp done:
6828 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6829 2e1f37b0 2019-08-08 stsp error == NULL)
6830 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6831 0ebf8283 2019-07-24 stsp if (repo)
6832 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6833 715dc77e 2019-08-03 stsp if (worktree)
6834 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6835 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6836 715dc77e 2019-08-03 stsp free((char *)pe->path);
6837 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
6838 715dc77e 2019-08-03 stsp free(cwd);
6839 01073a5d 2019-08-22 stsp return error;
6840 01073a5d 2019-08-22 stsp }
6841 01073a5d 2019-08-22 stsp
6842 01073a5d 2019-08-22 stsp __dead static void
6843 01073a5d 2019-08-22 stsp usage_cat(void)
6844 01073a5d 2019-08-22 stsp {
6845 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
6846 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
6847 01073a5d 2019-08-22 stsp exit(1);
6848 01073a5d 2019-08-22 stsp }
6849 01073a5d 2019-08-22 stsp
6850 01073a5d 2019-08-22 stsp static const struct got_error *
6851 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6852 01073a5d 2019-08-22 stsp {
6853 01073a5d 2019-08-22 stsp const struct got_error *err;
6854 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
6855 01073a5d 2019-08-22 stsp
6856 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
6857 01073a5d 2019-08-22 stsp if (err)
6858 01073a5d 2019-08-22 stsp return err;
6859 01073a5d 2019-08-22 stsp
6860 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
6861 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
6862 01073a5d 2019-08-22 stsp return err;
6863 01073a5d 2019-08-22 stsp }
6864 01073a5d 2019-08-22 stsp
6865 01073a5d 2019-08-22 stsp static const struct got_error *
6866 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6867 01073a5d 2019-08-22 stsp {
6868 01073a5d 2019-08-22 stsp const struct got_error *err;
6869 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
6870 01073a5d 2019-08-22 stsp const struct got_tree_entries *entries;
6871 01073a5d 2019-08-22 stsp struct got_tree_entry *te;
6872 01073a5d 2019-08-22 stsp
6873 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
6874 01073a5d 2019-08-22 stsp if (err)
6875 01073a5d 2019-08-22 stsp return err;
6876 01073a5d 2019-08-22 stsp
6877 01073a5d 2019-08-22 stsp entries = got_object_tree_get_entries(tree);
6878 01073a5d 2019-08-22 stsp te = SIMPLEQ_FIRST(&entries->head);
6879 01073a5d 2019-08-22 stsp while (te) {
6880 01073a5d 2019-08-22 stsp char *id_str;
6881 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
6882 01073a5d 2019-08-22 stsp break;
6883 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, te->id);
6884 01073a5d 2019-08-22 stsp if (err)
6885 01073a5d 2019-08-22 stsp break;
6886 01073a5d 2019-08-22 stsp fprintf(outfile, "%s %.7o %s\n", id_str, te->mode, te->name);
6887 01073a5d 2019-08-22 stsp free(id_str);
6888 01073a5d 2019-08-22 stsp te = SIMPLEQ_NEXT(te, entry);
6889 01073a5d 2019-08-22 stsp }
6890 01073a5d 2019-08-22 stsp
6891 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
6892 01073a5d 2019-08-22 stsp return err;
6893 01073a5d 2019-08-22 stsp }
6894 01073a5d 2019-08-22 stsp
6895 01073a5d 2019-08-22 stsp static const struct got_error *
6896 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6897 01073a5d 2019-08-22 stsp {
6898 01073a5d 2019-08-22 stsp const struct got_error *err;
6899 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
6900 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
6901 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
6902 01073a5d 2019-08-22 stsp char *id_str = NULL;
6903 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
6904 01073a5d 2019-08-22 stsp
6905 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
6906 01073a5d 2019-08-22 stsp if (err)
6907 01073a5d 2019-08-22 stsp return err;
6908 01073a5d 2019-08-22 stsp
6909 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
6910 01073a5d 2019-08-22 stsp if (err)
6911 01073a5d 2019-08-22 stsp goto done;
6912 01073a5d 2019-08-22 stsp
6913 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
6914 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6915 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
6916 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
6917 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
6918 01073a5d 2019-08-22 stsp char *pid_str;
6919 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
6920 01073a5d 2019-08-22 stsp if (err)
6921 01073a5d 2019-08-22 stsp goto done;
6922 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
6923 01073a5d 2019-08-22 stsp free(pid_str);
6924 01073a5d 2019-08-22 stsp }
6925 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
6926 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6927 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
6928 01073a5d 2019-08-22 stsp
6929 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
6930 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
6931 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
6932 01073a5d 2019-08-22 stsp
6933 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
6934 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
6935 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
6936 01073a5d 2019-08-22 stsp done:
6937 01073a5d 2019-08-22 stsp free(id_str);
6938 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
6939 01073a5d 2019-08-22 stsp return err;
6940 01073a5d 2019-08-22 stsp }
6941 01073a5d 2019-08-22 stsp
6942 01073a5d 2019-08-22 stsp static const struct got_error *
6943 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
6944 01073a5d 2019-08-22 stsp {
6945 01073a5d 2019-08-22 stsp const struct got_error *err;
6946 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
6947 01073a5d 2019-08-22 stsp char *id_str = NULL;
6948 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
6949 01073a5d 2019-08-22 stsp
6950 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
6951 01073a5d 2019-08-22 stsp if (err)
6952 01073a5d 2019-08-22 stsp return err;
6953 01073a5d 2019-08-22 stsp
6954 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
6955 01073a5d 2019-08-22 stsp if (err)
6956 01073a5d 2019-08-22 stsp goto done;
6957 01073a5d 2019-08-22 stsp
6958 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
6959 e15d5241 2019-08-22 stsp
6960 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
6961 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
6962 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6963 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
6964 01073a5d 2019-08-22 stsp break;
6965 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
6966 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6967 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
6968 01073a5d 2019-08-22 stsp break;
6969 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
6970 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6971 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
6972 01073a5d 2019-08-22 stsp break;
6973 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
6974 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
6975 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
6976 01073a5d 2019-08-22 stsp break;
6977 01073a5d 2019-08-22 stsp default:
6978 01073a5d 2019-08-22 stsp break;
6979 01073a5d 2019-08-22 stsp }
6980 01073a5d 2019-08-22 stsp
6981 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
6982 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
6983 e15d5241 2019-08-22 stsp
6984 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
6985 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
6986 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
6987 01073a5d 2019-08-22 stsp
6988 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
6989 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
6990 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
6991 01073a5d 2019-08-22 stsp done:
6992 01073a5d 2019-08-22 stsp free(id_str);
6993 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
6994 01073a5d 2019-08-22 stsp return err;
6995 01073a5d 2019-08-22 stsp }
6996 01073a5d 2019-08-22 stsp
6997 01073a5d 2019-08-22 stsp static const struct got_error *
6998 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
6999 01073a5d 2019-08-22 stsp {
7000 01073a5d 2019-08-22 stsp const struct got_error *error;
7001 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7002 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7003 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7004 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7005 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7006 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7007 01073a5d 2019-08-22 stsp
7008 01073a5d 2019-08-22 stsp #ifndef PROFILE
7009 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7010 01073a5d 2019-08-22 stsp NULL) == -1)
7011 01073a5d 2019-08-22 stsp err(1, "pledge");
7012 01073a5d 2019-08-22 stsp #endif
7013 01073a5d 2019-08-22 stsp
7014 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7015 01073a5d 2019-08-22 stsp switch (ch) {
7016 896e9b6f 2019-08-26 stsp case 'c':
7017 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7018 896e9b6f 2019-08-26 stsp break;
7019 01073a5d 2019-08-22 stsp case 'r':
7020 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7021 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7022 01073a5d 2019-08-22 stsp err(1, "-r option");
7023 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7024 01073a5d 2019-08-22 stsp break;
7025 896e9b6f 2019-08-26 stsp case 'P':
7026 896e9b6f 2019-08-26 stsp force_path = 1;
7027 896e9b6f 2019-08-26 stsp break;
7028 01073a5d 2019-08-22 stsp default:
7029 01073a5d 2019-08-22 stsp usage_cat();
7030 01073a5d 2019-08-22 stsp /* NOTREACHED */
7031 01073a5d 2019-08-22 stsp }
7032 01073a5d 2019-08-22 stsp }
7033 01073a5d 2019-08-22 stsp
7034 01073a5d 2019-08-22 stsp argc -= optind;
7035 01073a5d 2019-08-22 stsp argv += optind;
7036 01073a5d 2019-08-22 stsp
7037 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7038 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7039 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7040 01073a5d 2019-08-22 stsp goto done;
7041 01073a5d 2019-08-22 stsp }
7042 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7043 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7044 01073a5d 2019-08-22 stsp goto done;
7045 01073a5d 2019-08-22 stsp if (worktree) {
7046 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7047 01073a5d 2019-08-22 stsp repo_path = strdup(
7048 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7049 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7050 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7051 01073a5d 2019-08-22 stsp goto done;
7052 01073a5d 2019-08-22 stsp }
7053 01073a5d 2019-08-22 stsp }
7054 01073a5d 2019-08-22 stsp }
7055 01073a5d 2019-08-22 stsp
7056 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7057 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7058 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7059 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7060 01073a5d 2019-08-22 stsp }
7061 01073a5d 2019-08-22 stsp
7062 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7063 01073a5d 2019-08-22 stsp free(repo_path);
7064 01073a5d 2019-08-22 stsp if (error != NULL)
7065 01073a5d 2019-08-22 stsp goto done;
7066 01073a5d 2019-08-22 stsp
7067 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7068 896e9b6f 2019-08-26 stsp if (error)
7069 896e9b6f 2019-08-26 stsp goto done;
7070 896e9b6f 2019-08-26 stsp
7071 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7072 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7073 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7074 01073a5d 2019-08-22 stsp if (error)
7075 01073a5d 2019-08-22 stsp goto done;
7076 01073a5d 2019-08-22 stsp
7077 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7078 896e9b6f 2019-08-26 stsp if (force_path) {
7079 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7080 896e9b6f 2019-08-26 stsp argv[i]);
7081 896e9b6f 2019-08-26 stsp if (error)
7082 896e9b6f 2019-08-26 stsp break;
7083 896e9b6f 2019-08-26 stsp } else {
7084 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7085 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7086 896e9b6f 2019-08-26 stsp if (error) {
7087 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7088 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7089 896e9b6f 2019-08-26 stsp break;
7090 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7091 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7092 896e9b6f 2019-08-26 stsp if (error)
7093 896e9b6f 2019-08-26 stsp break;
7094 896e9b6f 2019-08-26 stsp }
7095 896e9b6f 2019-08-26 stsp }
7096 01073a5d 2019-08-22 stsp
7097 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7098 01073a5d 2019-08-22 stsp if (error)
7099 01073a5d 2019-08-22 stsp break;
7100 01073a5d 2019-08-22 stsp
7101 01073a5d 2019-08-22 stsp switch (obj_type) {
7102 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7103 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7104 01073a5d 2019-08-22 stsp break;
7105 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7106 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7107 01073a5d 2019-08-22 stsp break;
7108 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7109 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7110 01073a5d 2019-08-22 stsp break;
7111 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7112 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7113 01073a5d 2019-08-22 stsp break;
7114 01073a5d 2019-08-22 stsp default:
7115 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7116 01073a5d 2019-08-22 stsp break;
7117 01073a5d 2019-08-22 stsp }
7118 01073a5d 2019-08-22 stsp if (error)
7119 01073a5d 2019-08-22 stsp break;
7120 01073a5d 2019-08-22 stsp free(label);
7121 01073a5d 2019-08-22 stsp label = NULL;
7122 01073a5d 2019-08-22 stsp free(id);
7123 01073a5d 2019-08-22 stsp id = NULL;
7124 01073a5d 2019-08-22 stsp }
7125 01073a5d 2019-08-22 stsp
7126 01073a5d 2019-08-22 stsp done:
7127 01073a5d 2019-08-22 stsp free(label);
7128 01073a5d 2019-08-22 stsp free(id);
7129 896e9b6f 2019-08-26 stsp free(commit_id);
7130 01073a5d 2019-08-22 stsp if (worktree)
7131 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7132 01073a5d 2019-08-22 stsp if (repo) {
7133 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7134 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7135 01073a5d 2019-08-22 stsp if (error == NULL)
7136 01073a5d 2019-08-22 stsp error = repo_error;
7137 01073a5d 2019-08-22 stsp }
7138 0ebf8283 2019-07-24 stsp return error;
7139 0ebf8283 2019-07-24 stsp }