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 12463d8b 2019-12-13 stsp #include <fcntl.h>
27 12ce7a6c 2019-08-12 stsp #include <limits.h>
28 5c860e29 2018-03-12 stsp #include <locale.h>
29 818c7501 2019-07-11 stsp #include <ctype.h>
30 99437157 2018-11-11 stsp #include <signal.h>
31 5c860e29 2018-03-12 stsp #include <stdio.h>
32 5c860e29 2018-03-12 stsp #include <stdlib.h>
33 5c860e29 2018-03-12 stsp #include <string.h>
34 5c860e29 2018-03-12 stsp #include <unistd.h>
35 c09a553d 2018-03-12 stsp #include <libgen.h>
36 c0768b0f 2018-06-10 stsp #include <time.h>
37 33ad4cbe 2019-05-12 jcs #include <paths.h>
38 6841bf13 2019-11-29 kn #include <regex.h>
39 5c860e29 2018-03-12 stsp
40 53ccebc2 2019-07-30 stsp #include "got_version.h"
41 f42b1b34 2018-03-12 stsp #include "got_error.h"
42 f42b1b34 2018-03-12 stsp #include "got_object.h"
43 5261c201 2018-04-01 stsp #include "got_reference.h"
44 f42b1b34 2018-03-12 stsp #include "got_repository.h"
45 1dd54920 2019-05-11 stsp #include "got_path.h"
46 e6209546 2019-08-22 stsp #include "got_cancel.h"
47 c09a553d 2018-03-12 stsp #include "got_worktree.h"
48 79109fed 2018-03-27 stsp #include "got_diff.h"
49 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
50 404c43c4 2018-06-21 stsp #include "got_blame.h"
51 63219cd2 2019-01-04 stsp #include "got_privsep.h"
52 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
53 5c860e29 2018-03-12 stsp
54 5c860e29 2018-03-12 stsp #ifndef nitems
55 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
56 5c860e29 2018-03-12 stsp #endif
57 99437157 2018-11-11 stsp
58 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
59 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
60 99437157 2018-11-11 stsp
61 99437157 2018-11-11 stsp static void
62 99437157 2018-11-11 stsp catch_sigint(int signo)
63 99437157 2018-11-11 stsp {
64 99437157 2018-11-11 stsp sigint_received = 1;
65 99437157 2018-11-11 stsp }
66 99437157 2018-11-11 stsp
67 99437157 2018-11-11 stsp static void
68 99437157 2018-11-11 stsp catch_sigpipe(int signo)
69 99437157 2018-11-11 stsp {
70 99437157 2018-11-11 stsp sigpipe_received = 1;
71 99437157 2018-11-11 stsp }
72 5c860e29 2018-03-12 stsp
73 99437157 2018-11-11 stsp
74 8cfb4057 2019-07-09 stsp struct got_cmd {
75 5c860e29 2018-03-12 stsp const char *cmd_name;
76 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
77 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
78 97b3a7be 2019-07-09 stsp const char *cmd_alias;
79 5c860e29 2018-03-12 stsp };
80 5c860e29 2018-03-12 stsp
81 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
82 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
83 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
84 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
85 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
86 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
88 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
89 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
90 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
91 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
92 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
93 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
94 d00136be 2019-03-26 stsp __dead static void usage_add(void);
95 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
96 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
97 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
98 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
99 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
100 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
101 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
102 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
103 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
104 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
105 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
106 5c860e29 2018-03-12 stsp
107 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
108 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
109 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
110 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
111 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
113 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
114 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
115 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
116 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
117 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
118 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
119 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
120 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
121 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
122 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
123 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
124 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
125 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
126 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
127 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
128 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
129 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
130 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
131 5c860e29 2018-03-12 stsp
132 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
133 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
134 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
135 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
136 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
137 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
138 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
139 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
140 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
141 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
142 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
143 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
144 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
145 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
146 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
147 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
148 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
149 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
150 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
151 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
152 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
153 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
154 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
155 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
156 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
157 5c860e29 2018-03-12 stsp };
158 ce5b7c56 2019-07-09 stsp
159 ce5b7c56 2019-07-09 stsp static void
160 ce5b7c56 2019-07-09 stsp list_commands(void)
161 ce5b7c56 2019-07-09 stsp {
162 ce5b7c56 2019-07-09 stsp int i;
163 ce5b7c56 2019-07-09 stsp
164 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
165 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
166 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
167 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
168 ce5b7c56 2019-07-09 stsp }
169 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
170 ce5b7c56 2019-07-09 stsp }
171 5c860e29 2018-03-12 stsp
172 5c860e29 2018-03-12 stsp int
173 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
174 5c860e29 2018-03-12 stsp {
175 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
176 5c860e29 2018-03-12 stsp unsigned int i;
177 5c860e29 2018-03-12 stsp int ch;
178 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
179 5c860e29 2018-03-12 stsp
180 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
181 5c860e29 2018-03-12 stsp
182 53ccebc2 2019-07-30 stsp while ((ch = getopt(argc, argv, "hV")) != -1) {
183 5c860e29 2018-03-12 stsp switch (ch) {
184 1b6b95a8 2018-03-12 stsp case 'h':
185 1b6b95a8 2018-03-12 stsp hflag = 1;
186 53ccebc2 2019-07-30 stsp break;
187 53ccebc2 2019-07-30 stsp case 'V':
188 53ccebc2 2019-07-30 stsp Vflag = 1;
189 1b6b95a8 2018-03-12 stsp break;
190 5c860e29 2018-03-12 stsp default:
191 ce5b7c56 2019-07-09 stsp usage(hflag);
192 5c860e29 2018-03-12 stsp /* NOTREACHED */
193 5c860e29 2018-03-12 stsp }
194 5c860e29 2018-03-12 stsp }
195 5c860e29 2018-03-12 stsp
196 5c860e29 2018-03-12 stsp argc -= optind;
197 5c860e29 2018-03-12 stsp argv += optind;
198 1e70621d 2018-03-27 stsp optind = 0;
199 53ccebc2 2019-07-30 stsp
200 53ccebc2 2019-07-30 stsp if (Vflag) {
201 53ccebc2 2019-07-30 stsp got_version_print_str();
202 53ccebc2 2019-07-30 stsp return 1;
203 53ccebc2 2019-07-30 stsp }
204 5c860e29 2018-03-12 stsp
205 5c860e29 2018-03-12 stsp if (argc <= 0)
206 ce5b7c56 2019-07-09 stsp usage(hflag);
207 5c860e29 2018-03-12 stsp
208 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
209 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
210 99437157 2018-11-11 stsp
211 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
212 d7d4f210 2018-03-12 stsp const struct got_error *error;
213 d7d4f210 2018-03-12 stsp
214 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
215 5c860e29 2018-03-12 stsp
216 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
217 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
218 5c860e29 2018-03-12 stsp continue;
219 5c860e29 2018-03-12 stsp
220 1b6b95a8 2018-03-12 stsp if (hflag)
221 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
222 1b6b95a8 2018-03-12 stsp
223 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
224 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
225 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
226 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
227 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
228 70015d7a 2019-11-08 stsp !(sigint_received &&
229 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
230 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
231 d7d4f210 2018-03-12 stsp return 1;
232 d7d4f210 2018-03-12 stsp }
233 d7d4f210 2018-03-12 stsp
234 d7d4f210 2018-03-12 stsp return 0;
235 5c860e29 2018-03-12 stsp }
236 5c860e29 2018-03-12 stsp
237 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
238 ce5b7c56 2019-07-09 stsp list_commands();
239 5c860e29 2018-03-12 stsp return 1;
240 5c860e29 2018-03-12 stsp }
241 5c860e29 2018-03-12 stsp
242 4ed7e80c 2018-05-20 stsp __dead static void
243 ce5b7c56 2019-07-09 stsp usage(int hflag)
244 5c860e29 2018-03-12 stsp {
245 53ccebc2 2019-07-30 stsp fprintf(stderr, "usage: %s [-h] [-V] command [arg ...]\n",
246 53ccebc2 2019-07-30 stsp getprogname());
247 ce5b7c56 2019-07-09 stsp if (hflag)
248 ce5b7c56 2019-07-09 stsp list_commands();
249 5c860e29 2018-03-12 stsp exit(1);
250 5c860e29 2018-03-12 stsp }
251 5c860e29 2018-03-12 stsp
252 0266afb7 2019-01-04 stsp static const struct got_error *
253 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
254 e2ba3d07 2019-05-13 stsp {
255 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
256 e2ba3d07 2019-05-13 stsp const char *editor;
257 8920fa04 2019-08-18 stsp
258 8920fa04 2019-08-18 stsp *abspath = NULL;
259 e2ba3d07 2019-05-13 stsp
260 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
261 e2ba3d07 2019-05-13 stsp if (editor == NULL)
262 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
263 e2ba3d07 2019-05-13 stsp
264 0ee7065d 2019-05-13 stsp if (editor) {
265 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
266 0ee7065d 2019-05-13 stsp if (err)
267 0ee7065d 2019-05-13 stsp return err;
268 0ee7065d 2019-05-13 stsp }
269 e2ba3d07 2019-05-13 stsp
270 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
271 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
272 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
273 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
274 0ee7065d 2019-05-13 stsp }
275 0ee7065d 2019-05-13 stsp
276 e2ba3d07 2019-05-13 stsp return NULL;
277 e2ba3d07 2019-05-13 stsp }
278 e2ba3d07 2019-05-13 stsp
279 e2ba3d07 2019-05-13 stsp static const struct got_error *
280 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
281 c530dc23 2019-07-23 stsp const char *worktree_path)
282 0266afb7 2019-01-04 stsp {
283 163ce85a 2019-05-13 stsp const struct got_error *err;
284 0266afb7 2019-01-04 stsp
285 37c06ea4 2019-07-15 stsp #ifdef PROFILE
286 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
287 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
288 37c06ea4 2019-07-15 stsp #endif
289 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
290 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
291 0266afb7 2019-01-04 stsp
292 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
293 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
294 0266afb7 2019-01-04 stsp
295 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
296 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
297 0266afb7 2019-01-04 stsp
298 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
299 163ce85a 2019-05-13 stsp if (err != NULL)
300 163ce85a 2019-05-13 stsp return err;
301 0266afb7 2019-01-04 stsp
302 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
303 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
304 0266afb7 2019-01-04 stsp
305 0266afb7 2019-01-04 stsp return NULL;
306 2c7829a4 2019-06-17 stsp }
307 2c7829a4 2019-06-17 stsp
308 2c7829a4 2019-06-17 stsp __dead static void
309 2c7829a4 2019-06-17 stsp usage_init(void)
310 2c7829a4 2019-06-17 stsp {
311 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
312 2c7829a4 2019-06-17 stsp exit(1);
313 2c7829a4 2019-06-17 stsp }
314 2c7829a4 2019-06-17 stsp
315 2c7829a4 2019-06-17 stsp static const struct got_error *
316 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
317 2c7829a4 2019-06-17 stsp {
318 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
319 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
320 2c7829a4 2019-06-17 stsp int ch;
321 2c7829a4 2019-06-17 stsp
322 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
323 2c7829a4 2019-06-17 stsp switch (ch) {
324 2c7829a4 2019-06-17 stsp default:
325 2c7829a4 2019-06-17 stsp usage_init();
326 2c7829a4 2019-06-17 stsp /* NOTREACHED */
327 2c7829a4 2019-06-17 stsp }
328 2c7829a4 2019-06-17 stsp }
329 2c7829a4 2019-06-17 stsp
330 2c7829a4 2019-06-17 stsp argc -= optind;
331 2c7829a4 2019-06-17 stsp argv += optind;
332 2c7829a4 2019-06-17 stsp
333 2c7829a4 2019-06-17 stsp #ifndef PROFILE
334 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
335 2c7829a4 2019-06-17 stsp err(1, "pledge");
336 2c7829a4 2019-06-17 stsp #endif
337 2c7829a4 2019-06-17 stsp if (argc != 1)
338 bc20e173 2019-06-17 stsp usage_init();
339 2c7829a4 2019-06-17 stsp
340 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
341 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
342 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
343 2c7829a4 2019-06-17 stsp
344 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
345 2c7829a4 2019-06-17 stsp
346 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
347 2c7829a4 2019-06-17 stsp if (error &&
348 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
349 2c7829a4 2019-06-17 stsp goto done;
350 2c7829a4 2019-06-17 stsp
351 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
352 2c7829a4 2019-06-17 stsp if (error)
353 2c7829a4 2019-06-17 stsp goto done;
354 2c7829a4 2019-06-17 stsp
355 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
356 2c7829a4 2019-06-17 stsp if (error != NULL)
357 3ce1b845 2019-07-15 stsp goto done;
358 3ce1b845 2019-07-15 stsp
359 3ce1b845 2019-07-15 stsp done:
360 3ce1b845 2019-07-15 stsp free(repo_path);
361 3ce1b845 2019-07-15 stsp return error;
362 3ce1b845 2019-07-15 stsp }
363 3ce1b845 2019-07-15 stsp
364 3ce1b845 2019-07-15 stsp __dead static void
365 3ce1b845 2019-07-15 stsp usage_import(void)
366 3ce1b845 2019-07-15 stsp {
367 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
368 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
369 3ce1b845 2019-07-15 stsp exit(1);
370 3ce1b845 2019-07-15 stsp }
371 3ce1b845 2019-07-15 stsp
372 3ce1b845 2019-07-15 stsp int
373 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
374 3ce1b845 2019-07-15 stsp {
375 3ce1b845 2019-07-15 stsp pid_t pid;
376 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
377 3ce1b845 2019-07-15 stsp int st = -1;
378 3ce1b845 2019-07-15 stsp
379 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
380 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
381 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
382 3ce1b845 2019-07-15 stsp
383 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
384 3ce1b845 2019-07-15 stsp case -1:
385 3ce1b845 2019-07-15 stsp goto doneediting;
386 3ce1b845 2019-07-15 stsp case 0:
387 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
388 3ce1b845 2019-07-15 stsp _exit(127);
389 3ce1b845 2019-07-15 stsp }
390 3ce1b845 2019-07-15 stsp
391 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
392 3ce1b845 2019-07-15 stsp if (errno != EINTR)
393 3ce1b845 2019-07-15 stsp break;
394 3ce1b845 2019-07-15 stsp
395 3ce1b845 2019-07-15 stsp doneediting:
396 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
397 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
398 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
399 3ce1b845 2019-07-15 stsp
400 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
401 3ce1b845 2019-07-15 stsp errno = EINTR;
402 3ce1b845 2019-07-15 stsp return -1;
403 3ce1b845 2019-07-15 stsp }
404 3ce1b845 2019-07-15 stsp
405 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
406 3ce1b845 2019-07-15 stsp }
407 3ce1b845 2019-07-15 stsp
408 3ce1b845 2019-07-15 stsp static const struct got_error *
409 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
410 3ce1b845 2019-07-15 stsp const char *initial_content)
411 3ce1b845 2019-07-15 stsp {
412 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
413 3ce1b845 2019-07-15 stsp char buf[1024];
414 3ce1b845 2019-07-15 stsp struct stat st, st2;
415 3ce1b845 2019-07-15 stsp FILE *fp;
416 3ce1b845 2019-07-15 stsp int content_changed = 0;
417 3ce1b845 2019-07-15 stsp size_t len;
418 3ce1b845 2019-07-15 stsp
419 3ce1b845 2019-07-15 stsp *logmsg = NULL;
420 3ce1b845 2019-07-15 stsp
421 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
422 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
423 3ce1b845 2019-07-15 stsp
424 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
425 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
426 3ce1b845 2019-07-15 stsp
427 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
428 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
429 3ce1b845 2019-07-15 stsp
430 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
431 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
432 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
433 3ce1b845 2019-07-15 stsp
434 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
435 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
436 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
437 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
438 3ce1b845 2019-07-15 stsp len = 0;
439 3ce1b845 2019-07-15 stsp
440 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
441 3ce1b845 2019-07-15 stsp if (fp == NULL) {
442 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
443 3ce1b845 2019-07-15 stsp goto done;
444 3ce1b845 2019-07-15 stsp }
445 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
446 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
447 3ce1b845 2019-07-15 stsp content_changed = 1;
448 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
449 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
450 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
451 3ce1b845 2019-07-15 stsp }
452 3ce1b845 2019-07-15 stsp fclose(fp);
453 3ce1b845 2019-07-15 stsp
454 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
455 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
456 3ce1b845 2019-07-15 stsp len--;
457 3ce1b845 2019-07-15 stsp }
458 3ce1b845 2019-07-15 stsp
459 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
460 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
461 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
462 3ce1b845 2019-07-15 stsp done:
463 3ce1b845 2019-07-15 stsp if (err) {
464 3ce1b845 2019-07-15 stsp free(*logmsg);
465 3ce1b845 2019-07-15 stsp *logmsg = NULL;
466 3ce1b845 2019-07-15 stsp }
467 3ce1b845 2019-07-15 stsp return err;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp
470 3ce1b845 2019-07-15 stsp static const struct got_error *
471 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
472 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
473 3ce1b845 2019-07-15 stsp {
474 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
475 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
476 3ce1b845 2019-07-15 stsp int fd;
477 3ce1b845 2019-07-15 stsp
478 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
479 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
480 3ce1b845 2019-07-15 stsp branch_name) == -1)
481 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
482 3ce1b845 2019-07-15 stsp
483 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
484 3ce1b845 2019-07-15 stsp if (err)
485 3ce1b845 2019-07-15 stsp goto done;
486 3ce1b845 2019-07-15 stsp
487 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
488 3ce1b845 2019-07-15 stsp close(fd);
489 3ce1b845 2019-07-15 stsp
490 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
491 3ce1b845 2019-07-15 stsp done:
492 3ce1b845 2019-07-15 stsp free(initial_content);
493 3ce1b845 2019-07-15 stsp return err;
494 3ce1b845 2019-07-15 stsp }
495 3ce1b845 2019-07-15 stsp
496 3ce1b845 2019-07-15 stsp static const struct got_error *
497 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
498 3ce1b845 2019-07-15 stsp {
499 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
500 3ce1b845 2019-07-15 stsp return NULL;
501 3ce1b845 2019-07-15 stsp }
502 3ce1b845 2019-07-15 stsp
503 3ce1b845 2019-07-15 stsp static const struct got_error *
504 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
505 84792843 2019-08-09 stsp {
506 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
507 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
508 84792843 2019-08-09 stsp
509 84792843 2019-08-09 stsp *author = NULL;
510 aba9c984 2019-09-08 stsp
511 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
512 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
513 c9956ddf 2019-09-08 stsp if (name && email) {
514 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
515 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
516 aba9c984 2019-09-08 stsp return NULL;
517 aba9c984 2019-09-08 stsp }
518 84792843 2019-08-09 stsp
519 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
520 84792843 2019-08-09 stsp if (got_author == NULL) {
521 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
522 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
523 c9956ddf 2019-09-08 stsp if (name && email) {
524 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
525 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
526 c9956ddf 2019-09-08 stsp return NULL;
527 c9956ddf 2019-09-08 stsp }
528 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
529 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
530 84792843 2019-08-09 stsp }
531 84792843 2019-08-09 stsp
532 aba9c984 2019-09-08 stsp *author = strdup(got_author);
533 aba9c984 2019-09-08 stsp if (*author == NULL)
534 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
535 84792843 2019-08-09 stsp
536 84792843 2019-08-09 stsp /*
537 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
538 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
539 84792843 2019-08-09 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 aba9c984 2019-09-08 stsp if (*got_author != '@') {
549 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
550 aba9c984 2019-09-08 stsp goto done;
551 aba9c984 2019-09-08 stsp }
552 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
553 84792843 2019-08-09 stsp got_author++;
554 84792843 2019-08-09 stsp if (*got_author != '>')
555 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
556 aba9c984 2019-09-08 stsp done:
557 aba9c984 2019-09-08 stsp if (err) {
558 aba9c984 2019-09-08 stsp free(*author);
559 aba9c984 2019-09-08 stsp *author = NULL;
560 aba9c984 2019-09-08 stsp }
561 aba9c984 2019-09-08 stsp return err;
562 c9956ddf 2019-09-08 stsp }
563 c9956ddf 2019-09-08 stsp
564 c9956ddf 2019-09-08 stsp static const struct got_error *
565 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
566 c9956ddf 2019-09-08 stsp {
567 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
568 c9956ddf 2019-09-08 stsp
569 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
570 c9956ddf 2019-09-08 stsp if (homedir) {
571 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
572 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
573 c9956ddf 2019-09-08 stsp
574 c9956ddf 2019-09-08 stsp }
575 c9956ddf 2019-09-08 stsp return NULL;
576 84792843 2019-08-09 stsp }
577 84792843 2019-08-09 stsp
578 84792843 2019-08-09 stsp static const struct got_error *
579 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
580 3ce1b845 2019-07-15 stsp {
581 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
582 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
583 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
584 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
585 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
586 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
587 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
588 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
589 3ce1b845 2019-07-15 stsp int ch;
590 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
591 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
592 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
593 3ce1b845 2019-07-15 stsp
594 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
597 3ce1b845 2019-07-15 stsp switch (ch) {
598 3ce1b845 2019-07-15 stsp case 'b':
599 3ce1b845 2019-07-15 stsp branch_name = optarg;
600 3ce1b845 2019-07-15 stsp break;
601 3ce1b845 2019-07-15 stsp case 'm':
602 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
603 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
604 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
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 'r':
609 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
610 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
611 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
612 9ba1d308 2019-10-21 stsp optarg);
613 3ce1b845 2019-07-15 stsp goto done;
614 3ce1b845 2019-07-15 stsp }
615 3ce1b845 2019-07-15 stsp break;
616 3ce1b845 2019-07-15 stsp case 'I':
617 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
618 3ce1b845 2019-07-15 stsp break;
619 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
620 3ce1b845 2019-07-15 stsp NULL);
621 3ce1b845 2019-07-15 stsp if (error)
622 3ce1b845 2019-07-15 stsp goto done;
623 3ce1b845 2019-07-15 stsp break;
624 3ce1b845 2019-07-15 stsp default:
625 b2b65d18 2019-08-22 stsp usage_import();
626 3ce1b845 2019-07-15 stsp /* NOTREACHED */
627 3ce1b845 2019-07-15 stsp }
628 3ce1b845 2019-07-15 stsp }
629 3ce1b845 2019-07-15 stsp
630 3ce1b845 2019-07-15 stsp argc -= optind;
631 3ce1b845 2019-07-15 stsp argv += optind;
632 3ce1b845 2019-07-15 stsp
633 3ce1b845 2019-07-15 stsp #ifndef PROFILE
634 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
635 aba9c984 2019-09-08 stsp "unveil",
636 3ce1b845 2019-07-15 stsp NULL) == -1)
637 3ce1b845 2019-07-15 stsp err(1, "pledge");
638 3ce1b845 2019-07-15 stsp #endif
639 3ce1b845 2019-07-15 stsp if (argc != 1)
640 3ce1b845 2019-07-15 stsp usage_import();
641 2c7829a4 2019-06-17 stsp
642 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
643 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
644 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
645 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
646 3ce1b845 2019-07-15 stsp }
647 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
648 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
649 c9956ddf 2019-09-08 stsp if (error)
650 c9956ddf 2019-09-08 stsp goto done;
651 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
652 3ce1b845 2019-07-15 stsp if (error)
653 3ce1b845 2019-07-15 stsp goto done;
654 aba9c984 2019-09-08 stsp
655 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
656 aba9c984 2019-09-08 stsp if (error)
657 aba9c984 2019-09-08 stsp return error;
658 e560b7e0 2019-11-28 stsp
659 e560b7e0 2019-11-28 stsp /*
660 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
661 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
662 e560b7e0 2019-11-28 stsp * an unintended typo.
663 e560b7e0 2019-11-28 stsp */
664 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
665 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
666 3ce1b845 2019-07-15 stsp
667 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
668 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
669 3ce1b845 2019-07-15 stsp goto done;
670 3ce1b845 2019-07-15 stsp }
671 3ce1b845 2019-07-15 stsp
672 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
673 3ce1b845 2019-07-15 stsp if (error) {
674 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
675 3ce1b845 2019-07-15 stsp goto done;
676 3ce1b845 2019-07-15 stsp } else {
677 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
678 3ce1b845 2019-07-15 stsp "import target branch already exists");
679 3ce1b845 2019-07-15 stsp goto done;
680 3ce1b845 2019-07-15 stsp }
681 3ce1b845 2019-07-15 stsp
682 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
683 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
684 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
685 3ce1b845 2019-07-15 stsp goto done;
686 3ce1b845 2019-07-15 stsp }
687 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
688 3ce1b845 2019-07-15 stsp
689 3ce1b845 2019-07-15 stsp /*
690 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
691 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
692 3ce1b845 2019-07-15 stsp */
693 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
694 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
695 3ce1b845 2019-07-15 stsp if (error)
696 3ce1b845 2019-07-15 stsp goto done;
697 8e158b01 2019-09-22 stsp free(logmsg);
698 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
699 ef293bdd 2019-10-21 stsp path_dir, refname);
700 ef293bdd 2019-10-21 stsp if (error) {
701 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
702 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
703 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
704 3ce1b845 2019-07-15 stsp goto done;
705 ef293bdd 2019-10-21 stsp }
706 3ce1b845 2019-07-15 stsp }
707 3ce1b845 2019-07-15 stsp
708 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
709 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
710 ef293bdd 2019-10-21 stsp if (logmsg_path)
711 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
712 3ce1b845 2019-07-15 stsp goto done;
713 ef293bdd 2019-10-21 stsp }
714 3ce1b845 2019-07-15 stsp
715 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
716 ef293bdd 2019-10-21 stsp if (error) {
717 ef293bdd 2019-10-21 stsp if (logmsg_path)
718 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
719 ef293bdd 2019-10-21 stsp goto done;
720 ef293bdd 2019-10-21 stsp }
721 ef293bdd 2019-10-21 stsp
722 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
723 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
724 ef293bdd 2019-10-21 stsp if (error) {
725 ef293bdd 2019-10-21 stsp if (logmsg_path)
726 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
727 3ce1b845 2019-07-15 stsp goto done;
728 ef293bdd 2019-10-21 stsp }
729 3ce1b845 2019-07-15 stsp
730 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
731 ef293bdd 2019-10-21 stsp if (error) {
732 ef293bdd 2019-10-21 stsp if (logmsg_path)
733 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
734 3ce1b845 2019-07-15 stsp goto done;
735 ef293bdd 2019-10-21 stsp }
736 3ce1b845 2019-07-15 stsp
737 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
738 ef293bdd 2019-10-21 stsp if (error) {
739 ef293bdd 2019-10-21 stsp if (logmsg_path)
740 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
741 3ce1b845 2019-07-15 stsp goto done;
742 ef293bdd 2019-10-21 stsp }
743 3ce1b845 2019-07-15 stsp
744 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
745 ef293bdd 2019-10-21 stsp if (error) {
746 ef293bdd 2019-10-21 stsp if (logmsg_path)
747 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
748 3ce1b845 2019-07-15 stsp goto done;
749 ef293bdd 2019-10-21 stsp }
750 3ce1b845 2019-07-15 stsp
751 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
752 3ce1b845 2019-07-15 stsp if (error) {
753 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
754 ef293bdd 2019-10-21 stsp if (logmsg_path)
755 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
756 3ce1b845 2019-07-15 stsp goto done;
757 ef293bdd 2019-10-21 stsp }
758 3ce1b845 2019-07-15 stsp
759 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
760 3ce1b845 2019-07-15 stsp branch_ref);
761 ef293bdd 2019-10-21 stsp if (error) {
762 ef293bdd 2019-10-21 stsp if (logmsg_path)
763 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
764 3ce1b845 2019-07-15 stsp goto done;
765 ef293bdd 2019-10-21 stsp }
766 3ce1b845 2019-07-15 stsp
767 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
768 ef293bdd 2019-10-21 stsp if (error) {
769 ef293bdd 2019-10-21 stsp if (logmsg_path)
770 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
771 3ce1b845 2019-07-15 stsp goto done;
772 ef293bdd 2019-10-21 stsp }
773 3ce1b845 2019-07-15 stsp }
774 3ce1b845 2019-07-15 stsp
775 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
776 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
777 2c7829a4 2019-06-17 stsp done:
778 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
779 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
780 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
781 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
782 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
783 8e158b01 2019-09-22 stsp free(logmsg);
784 ef293bdd 2019-10-21 stsp free(logmsg_path);
785 2c7829a4 2019-06-17 stsp free(repo_path);
786 3ce1b845 2019-07-15 stsp free(editor);
787 3ce1b845 2019-07-15 stsp free(refname);
788 3ce1b845 2019-07-15 stsp free(new_commit_id);
789 3ce1b845 2019-07-15 stsp free(id_str);
790 aba9c984 2019-09-08 stsp free(author);
791 c9956ddf 2019-09-08 stsp free(gitconfig_path);
792 3ce1b845 2019-07-15 stsp if (branch_ref)
793 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
794 3ce1b845 2019-07-15 stsp if (head_ref)
795 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
796 2c7829a4 2019-06-17 stsp return error;
797 0266afb7 2019-01-04 stsp }
798 0266afb7 2019-01-04 stsp
799 4ed7e80c 2018-05-20 stsp __dead static void
800 c09a553d 2018-03-12 stsp usage_checkout(void)
801 c09a553d 2018-03-12 stsp {
802 08573d5b 2019-05-14 stsp fprintf(stderr, "usage: %s checkout [-b branch] [-c commit] "
803 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
804 c09a553d 2018-03-12 stsp exit(1);
805 92a684f4 2018-03-12 stsp }
806 92a684f4 2018-03-12 stsp
807 7f47418f 2019-12-20 stsp static void
808 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
809 7f47418f 2019-12-20 stsp {
810 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
811 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
812 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
813 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
814 7f47418f 2019-12-20 stsp getprogname());
815 7f47418f 2019-12-20 stsp }
816 7f47418f 2019-12-20 stsp
817 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
818 7f47418f 2019-12-20 stsp const char *worktree_path;
819 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
820 7f47418f 2019-12-20 stsp };
821 7f47418f 2019-12-20 stsp
822 1ee397ad 2019-07-12 stsp static const struct got_error *
823 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
824 92a684f4 2018-03-12 stsp {
825 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
826 a484d721 2019-06-10 stsp
827 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
828 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
829 7f47418f 2019-12-20 stsp return NULL;
830 7f47418f 2019-12-20 stsp
831 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
832 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
833 1ee397ad 2019-07-12 stsp return NULL;
834 7f47418f 2019-12-20 stsp }
835 92a684f4 2018-03-12 stsp
836 92a684f4 2018-03-12 stsp while (path[0] == '/')
837 92a684f4 2018-03-12 stsp path++;
838 92a684f4 2018-03-12 stsp
839 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
840 1ee397ad 2019-07-12 stsp return NULL;
841 99437157 2018-11-11 stsp }
842 99437157 2018-11-11 stsp
843 99437157 2018-11-11 stsp static const struct got_error *
844 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
845 99437157 2018-11-11 stsp {
846 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
847 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
848 99437157 2018-11-11 stsp return NULL;
849 8069f636 2019-01-12 stsp }
850 8069f636 2019-01-12 stsp
851 8069f636 2019-01-12 stsp static const struct got_error *
852 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
853 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
854 3aef623b 2019-10-15 stsp struct got_repository *repo)
855 8069f636 2019-01-12 stsp {
856 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
857 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
858 8069f636 2019-01-12 stsp
859 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
860 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
861 36a38700 2019-05-10 stsp if (err)
862 36a38700 2019-05-10 stsp return err;
863 8069f636 2019-01-12 stsp
864 d5bea539 2019-05-13 stsp if (yca_id == NULL)
865 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
866 8069f636 2019-01-12 stsp
867 d5bea539 2019-05-13 stsp /*
868 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
869 d5bea539 2019-05-13 stsp * and the work tree's base commit.
870 d5bea539 2019-05-13 stsp *
871 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
872 d5bea539 2019-05-13 stsp *
873 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
874 d5bea539 2019-05-13 stsp * \ /
875 d5bea539 2019-05-13 stsp * C E
876 d5bea539 2019-05-13 stsp * \ /
877 d5bea539 2019-05-13 stsp * B (yca)
878 d5bea539 2019-05-13 stsp * |
879 d5bea539 2019-05-13 stsp * A
880 d5bea539 2019-05-13 stsp *
881 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
882 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
883 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
884 d5bea539 2019-05-13 stsp */
885 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
886 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
887 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
888 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
889 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
890 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
891 8069f636 2019-01-12 stsp
892 d5bea539 2019-05-13 stsp free(yca_id);
893 d5bea539 2019-05-13 stsp return NULL;
894 c09a553d 2018-03-12 stsp }
895 a367ff0f 2019-05-14 stsp
896 a367ff0f 2019-05-14 stsp static const struct got_error *
897 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
898 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
899 a51a74b3 2019-07-27 stsp struct got_repository *repo)
900 a367ff0f 2019-05-14 stsp {
901 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
902 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
903 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
904 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
905 a367ff0f 2019-05-14 stsp
906 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
907 a367ff0f 2019-05-14 stsp if (err)
908 a51a74b3 2019-07-27 stsp goto done;
909 a51a74b3 2019-07-27 stsp
910 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
911 a51a74b3 2019-07-27 stsp is_same_branch = 1;
912 a51a74b3 2019-07-27 stsp goto done;
913 a51a74b3 2019-07-27 stsp }
914 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
915 a51a74b3 2019-07-27 stsp is_same_branch = 1;
916 a367ff0f 2019-05-14 stsp goto done;
917 a51a74b3 2019-07-27 stsp }
918 a367ff0f 2019-05-14 stsp
919 a367ff0f 2019-05-14 stsp err = got_commit_graph_open(&graph, head_commit_id, "/", 1, repo);
920 a367ff0f 2019-05-14 stsp if (err)
921 a367ff0f 2019-05-14 stsp goto done;
922 a367ff0f 2019-05-14 stsp
923 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
924 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
925 a367ff0f 2019-05-14 stsp if (err)
926 a367ff0f 2019-05-14 stsp goto done;
927 a367ff0f 2019-05-14 stsp
928 a367ff0f 2019-05-14 stsp for (;;) {
929 a367ff0f 2019-05-14 stsp struct got_object_id *id;
930 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
931 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
932 a367ff0f 2019-05-14 stsp if (err) {
933 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
934 a367ff0f 2019-05-14 stsp err = NULL;
935 ee780d5c 2020-01-04 stsp break;
936 a367ff0f 2019-05-14 stsp }
937 c09a553d 2018-03-12 stsp
938 a367ff0f 2019-05-14 stsp if (id) {
939 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
940 a51a74b3 2019-07-27 stsp break;
941 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
942 a367ff0f 2019-05-14 stsp is_same_branch = 1;
943 a367ff0f 2019-05-14 stsp break;
944 a367ff0f 2019-05-14 stsp }
945 a367ff0f 2019-05-14 stsp }
946 a367ff0f 2019-05-14 stsp }
947 a367ff0f 2019-05-14 stsp done:
948 a367ff0f 2019-05-14 stsp if (graph)
949 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
950 a367ff0f 2019-05-14 stsp free(head_commit_id);
951 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
952 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
953 04f57cb3 2019-07-25 stsp return err;
954 04f57cb3 2019-07-25 stsp }
955 04f57cb3 2019-07-25 stsp
956 04f57cb3 2019-07-25 stsp static const struct got_error *
957 04f57cb3 2019-07-25 stsp resolve_commit_arg(struct got_object_id **commit_id,
958 04f57cb3 2019-07-25 stsp const char *commit_id_arg, struct got_repository *repo)
959 04f57cb3 2019-07-25 stsp {
960 04f57cb3 2019-07-25 stsp const struct got_error *err;
961 04f57cb3 2019-07-25 stsp struct got_reference *ref;
962 303e2782 2019-08-09 stsp struct got_tag_object *tag;
963 04f57cb3 2019-07-25 stsp
964 303e2782 2019-08-09 stsp err = got_repo_object_match_tag(&tag, commit_id_arg,
965 303e2782 2019-08-09 stsp GOT_OBJ_TYPE_COMMIT, repo);
966 303e2782 2019-08-09 stsp if (err == NULL) {
967 303e2782 2019-08-09 stsp *commit_id = got_object_id_dup(
968 303e2782 2019-08-09 stsp got_object_tag_get_object_id(tag));
969 303e2782 2019-08-09 stsp if (*commit_id == NULL)
970 303e2782 2019-08-09 stsp err = got_error_from_errno("got_object_id_dup");
971 303e2782 2019-08-09 stsp got_object_tag_close(tag);
972 303e2782 2019-08-09 stsp return err;
973 303e2782 2019-08-09 stsp } else if (err->code != GOT_ERR_NO_OBJ)
974 303e2782 2019-08-09 stsp return err;
975 303e2782 2019-08-09 stsp
976 04f57cb3 2019-07-25 stsp err = got_ref_open(&ref, repo, commit_id_arg, 0);
977 04f57cb3 2019-07-25 stsp if (err == NULL) {
978 04f57cb3 2019-07-25 stsp err = got_ref_resolve(commit_id, repo, ref);
979 04f57cb3 2019-07-25 stsp got_ref_close(ref);
980 04f57cb3 2019-07-25 stsp } else {
981 04f57cb3 2019-07-25 stsp if (err->code != GOT_ERR_NOT_REF)
982 04f57cb3 2019-07-25 stsp return err;
983 04f57cb3 2019-07-25 stsp err = got_repo_match_object_id_prefix(commit_id,
984 04f57cb3 2019-07-25 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, repo);
985 04f57cb3 2019-07-25 stsp }
986 a367ff0f 2019-05-14 stsp return err;
987 a367ff0f 2019-05-14 stsp }
988 8069f636 2019-01-12 stsp
989 4ed7e80c 2018-05-20 stsp static const struct got_error *
990 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
991 c09a553d 2018-03-12 stsp {
992 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
993 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
994 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
995 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
996 c09a553d 2018-03-12 stsp char *repo_path = NULL;
997 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
998 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
999 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
1000 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
1001 72151b04 2019-05-11 stsp int ch, same_path_prefix;
1002 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1003 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
1004 f2ea84fa 2019-07-27 stsp
1005 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1006 c09a553d 2018-03-12 stsp
1007 08573d5b 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:p:")) != -1) {
1008 0bb8a95e 2018-03-12 stsp switch (ch) {
1009 08573d5b 2019-05-14 stsp case 'b':
1010 08573d5b 2019-05-14 stsp branch_name = optarg;
1011 08573d5b 2019-05-14 stsp break;
1012 8069f636 2019-01-12 stsp case 'c':
1013 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1014 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1015 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1016 8069f636 2019-01-12 stsp break;
1017 0bb8a95e 2018-03-12 stsp case 'p':
1018 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1019 0bb8a95e 2018-03-12 stsp break;
1020 0bb8a95e 2018-03-12 stsp default:
1021 2deda0b9 2019-03-07 stsp usage_checkout();
1022 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1023 0bb8a95e 2018-03-12 stsp }
1024 0bb8a95e 2018-03-12 stsp }
1025 0bb8a95e 2018-03-12 stsp
1026 0bb8a95e 2018-03-12 stsp argc -= optind;
1027 0bb8a95e 2018-03-12 stsp argv += optind;
1028 0bb8a95e 2018-03-12 stsp
1029 6715a751 2018-03-16 stsp #ifndef PROFILE
1030 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1031 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1032 c09a553d 2018-03-12 stsp err(1, "pledge");
1033 6715a751 2018-03-16 stsp #endif
1034 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1035 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1036 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1037 76089277 2018-04-01 stsp if (repo_path == NULL)
1038 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1039 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1040 76089277 2018-04-01 stsp if (cwd == NULL) {
1041 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1042 76089277 2018-04-01 stsp goto done;
1043 76089277 2018-04-01 stsp }
1044 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1045 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1046 230a42bd 2019-05-11 jcs if (base == NULL) {
1047 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1048 230a42bd 2019-05-11 jcs path_prefix);
1049 230a42bd 2019-05-11 jcs goto done;
1050 230a42bd 2019-05-11 jcs }
1051 230a42bd 2019-05-11 jcs } else {
1052 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1053 230a42bd 2019-05-11 jcs if (base == NULL) {
1054 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1055 230a42bd 2019-05-11 jcs repo_path);
1056 230a42bd 2019-05-11 jcs goto done;
1057 230a42bd 2019-05-11 jcs }
1058 76089277 2018-04-01 stsp }
1059 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1060 c09a553d 2018-03-12 stsp if (dotgit)
1061 c09a553d 2018-03-12 stsp *dotgit = '\0';
1062 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1063 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1064 c09a553d 2018-03-12 stsp free(cwd);
1065 76089277 2018-04-01 stsp goto done;
1066 c09a553d 2018-03-12 stsp }
1067 c09a553d 2018-03-12 stsp free(cwd);
1068 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1069 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1070 76089277 2018-04-01 stsp if (repo_path == NULL) {
1071 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1072 76089277 2018-04-01 stsp goto done;
1073 76089277 2018-04-01 stsp }
1074 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1075 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1076 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1077 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1078 b4b3a7dd 2019-07-22 stsp argv[1]);
1079 b4b3a7dd 2019-07-22 stsp goto done;
1080 b4b3a7dd 2019-07-22 stsp }
1081 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1082 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1083 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1084 b4b3a7dd 2019-07-22 stsp goto done;
1085 b4b3a7dd 2019-07-22 stsp }
1086 76089277 2018-04-01 stsp }
1087 c09a553d 2018-03-12 stsp } else
1088 c09a553d 2018-03-12 stsp usage_checkout();
1089 c09a553d 2018-03-12 stsp
1090 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1091 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1092 13bfb272 2019-05-10 jcs
1093 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1094 c09a553d 2018-03-12 stsp if (error != NULL)
1095 c02c541e 2019-03-29 stsp goto done;
1096 c02c541e 2019-03-29 stsp
1097 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1098 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1099 c530dc23 2019-07-23 stsp if (error) {
1100 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1101 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1102 c530dc23 2019-07-23 stsp goto done;
1103 c530dc23 2019-07-23 stsp if (!got_path_dir_is_empty(worktree_path)) {
1104 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1105 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1106 c530dc23 2019-07-23 stsp goto done;
1107 c530dc23 2019-07-23 stsp }
1108 c530dc23 2019-07-23 stsp }
1109 c530dc23 2019-07-23 stsp
1110 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1111 c02c541e 2019-03-29 stsp if (error)
1112 c09a553d 2018-03-12 stsp goto done;
1113 8069f636 2019-01-12 stsp
1114 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1115 c09a553d 2018-03-12 stsp if (error != NULL)
1116 c09a553d 2018-03-12 stsp goto done;
1117 c09a553d 2018-03-12 stsp
1118 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1119 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1120 c09a553d 2018-03-12 stsp goto done;
1121 c09a553d 2018-03-12 stsp
1122 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1123 c09a553d 2018-03-12 stsp if (error != NULL)
1124 c09a553d 2018-03-12 stsp goto done;
1125 c09a553d 2018-03-12 stsp
1126 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1127 e5dc7198 2018-12-29 stsp path_prefix);
1128 e5dc7198 2018-12-29 stsp if (error != NULL)
1129 e5dc7198 2018-12-29 stsp goto done;
1130 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1131 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1132 49520a32 2018-12-29 stsp goto done;
1133 49520a32 2018-12-29 stsp }
1134 49520a32 2018-12-29 stsp
1135 8069f636 2019-01-12 stsp if (commit_id_str) {
1136 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1137 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1138 30837e32 2019-07-25 stsp if (error)
1139 8069f636 2019-01-12 stsp goto done;
1140 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1141 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1142 8069f636 2019-01-12 stsp if (error != NULL) {
1143 8069f636 2019-01-12 stsp free(commit_id);
1144 8069f636 2019-01-12 stsp goto done;
1145 8069f636 2019-01-12 stsp }
1146 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1147 45d344f6 2019-05-14 stsp if (error)
1148 45d344f6 2019-05-14 stsp goto done;
1149 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1150 8069f636 2019-01-12 stsp commit_id);
1151 8069f636 2019-01-12 stsp free(commit_id);
1152 8069f636 2019-01-12 stsp if (error)
1153 8069f636 2019-01-12 stsp goto done;
1154 8069f636 2019-01-12 stsp }
1155 8069f636 2019-01-12 stsp
1156 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1157 f2ea84fa 2019-07-27 stsp if (error)
1158 f2ea84fa 2019-07-27 stsp goto done;
1159 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1160 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1161 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1162 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1163 c09a553d 2018-03-12 stsp if (error != NULL)
1164 c09a553d 2018-03-12 stsp goto done;
1165 c09a553d 2018-03-12 stsp
1166 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1167 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1168 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1169 c09a553d 2018-03-12 stsp
1170 c09a553d 2018-03-12 stsp done:
1171 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1172 8069f636 2019-01-12 stsp free(commit_id_str);
1173 76089277 2018-04-01 stsp free(repo_path);
1174 507dc3bb 2018-12-29 stsp free(worktree_path);
1175 507dc3bb 2018-12-29 stsp return error;
1176 507dc3bb 2018-12-29 stsp }
1177 507dc3bb 2018-12-29 stsp
1178 507dc3bb 2018-12-29 stsp __dead static void
1179 507dc3bb 2018-12-29 stsp usage_update(void)
1180 507dc3bb 2018-12-29 stsp {
1181 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1182 507dc3bb 2018-12-29 stsp getprogname());
1183 507dc3bb 2018-12-29 stsp exit(1);
1184 507dc3bb 2018-12-29 stsp }
1185 507dc3bb 2018-12-29 stsp
1186 1ee397ad 2019-07-12 stsp static const struct got_error *
1187 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1188 507dc3bb 2018-12-29 stsp {
1189 784955db 2019-01-12 stsp int *did_something = arg;
1190 784955db 2019-01-12 stsp
1191 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1192 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1193 1ee397ad 2019-07-12 stsp return NULL;
1194 507dc3bb 2018-12-29 stsp
1195 1545c615 2019-02-10 stsp *did_something = 1;
1196 a484d721 2019-06-10 stsp
1197 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1198 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1199 1ee397ad 2019-07-12 stsp return NULL;
1200 a484d721 2019-06-10 stsp
1201 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1202 507dc3bb 2018-12-29 stsp path++;
1203 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1204 1ee397ad 2019-07-12 stsp return NULL;
1205 be7061eb 2018-12-30 stsp }
1206 be7061eb 2018-12-30 stsp
1207 be7061eb 2018-12-30 stsp static const struct got_error *
1208 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1209 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1210 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1211 a1fb16d8 2019-05-24 stsp {
1212 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1213 a1fb16d8 2019-05-24 stsp char *base_id_str;
1214 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1215 a1fb16d8 2019-05-24 stsp
1216 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1217 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1218 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1219 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1220 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1221 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1222 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1223 a1fb16d8 2019-05-24 stsp }
1224 a1fb16d8 2019-05-24 stsp
1225 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1226 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1227 a1fb16d8 2019-05-24 stsp if (err) {
1228 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1229 a1fb16d8 2019-05-24 stsp return err;
1230 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1231 a1fb16d8 2019-05-24 stsp }
1232 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1233 a1fb16d8 2019-05-24 stsp return NULL;
1234 a1fb16d8 2019-05-24 stsp
1235 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1236 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1237 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1238 a1fb16d8 2019-05-24 stsp if (err)
1239 a1fb16d8 2019-05-24 stsp return err;
1240 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1241 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1242 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1243 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1244 0ebf8283 2019-07-24 stsp return NULL;
1245 0ebf8283 2019-07-24 stsp }
1246 0ebf8283 2019-07-24 stsp
1247 0ebf8283 2019-07-24 stsp static const struct got_error *
1248 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1249 0ebf8283 2019-07-24 stsp {
1250 0ebf8283 2019-07-24 stsp const struct got_error *err;
1251 0ebf8283 2019-07-24 stsp int in_progress;
1252 0ebf8283 2019-07-24 stsp
1253 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1254 0ebf8283 2019-07-24 stsp if (err)
1255 0ebf8283 2019-07-24 stsp return err;
1256 0ebf8283 2019-07-24 stsp if (in_progress)
1257 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1258 0ebf8283 2019-07-24 stsp
1259 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1260 0ebf8283 2019-07-24 stsp if (err)
1261 0ebf8283 2019-07-24 stsp return err;
1262 0ebf8283 2019-07-24 stsp if (in_progress)
1263 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1264 0ebf8283 2019-07-24 stsp
1265 a1fb16d8 2019-05-24 stsp return NULL;
1266 a5edda0a 2019-07-27 stsp }
1267 a5edda0a 2019-07-27 stsp
1268 a5edda0a 2019-07-27 stsp static const struct got_error *
1269 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1270 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1271 a5edda0a 2019-07-27 stsp {
1272 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1273 a5edda0a 2019-07-27 stsp char *path;
1274 a5edda0a 2019-07-27 stsp int i;
1275 a5edda0a 2019-07-27 stsp
1276 a5edda0a 2019-07-27 stsp if (argc == 0) {
1277 a5edda0a 2019-07-27 stsp path = strdup("");
1278 a5edda0a 2019-07-27 stsp if (path == NULL)
1279 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1280 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1281 a5edda0a 2019-07-27 stsp }
1282 a5edda0a 2019-07-27 stsp
1283 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1284 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1285 a5edda0a 2019-07-27 stsp if (err)
1286 a5edda0a 2019-07-27 stsp break;
1287 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1288 a5edda0a 2019-07-27 stsp if (err) {
1289 a5edda0a 2019-07-27 stsp free(path);
1290 a5edda0a 2019-07-27 stsp break;
1291 a5edda0a 2019-07-27 stsp }
1292 a5edda0a 2019-07-27 stsp }
1293 a5edda0a 2019-07-27 stsp
1294 a5edda0a 2019-07-27 stsp return err;
1295 a1fb16d8 2019-05-24 stsp }
1296 a1fb16d8 2019-05-24 stsp
1297 a1fb16d8 2019-05-24 stsp static const struct got_error *
1298 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1299 507dc3bb 2018-12-29 stsp {
1300 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1301 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1302 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1303 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1304 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1305 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1306 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1307 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1308 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1309 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1310 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1311 507dc3bb 2018-12-29 stsp
1312 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1313 f2ea84fa 2019-07-27 stsp
1314 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1315 507dc3bb 2018-12-29 stsp switch (ch) {
1316 024e9686 2019-05-14 stsp case 'b':
1317 024e9686 2019-05-14 stsp branch_name = optarg;
1318 024e9686 2019-05-14 stsp break;
1319 507dc3bb 2018-12-29 stsp case 'c':
1320 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1321 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1322 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1323 507dc3bb 2018-12-29 stsp break;
1324 507dc3bb 2018-12-29 stsp default:
1325 2deda0b9 2019-03-07 stsp usage_update();
1326 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1327 507dc3bb 2018-12-29 stsp }
1328 507dc3bb 2018-12-29 stsp }
1329 507dc3bb 2018-12-29 stsp
1330 507dc3bb 2018-12-29 stsp argc -= optind;
1331 507dc3bb 2018-12-29 stsp argv += optind;
1332 507dc3bb 2018-12-29 stsp
1333 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1334 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1335 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1336 507dc3bb 2018-12-29 stsp err(1, "pledge");
1337 507dc3bb 2018-12-29 stsp #endif
1338 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1339 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1340 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1341 c4cdcb68 2019-04-03 stsp goto done;
1342 c4cdcb68 2019-04-03 stsp }
1343 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1344 7d5807f4 2019-07-11 stsp if (error)
1345 7d5807f4 2019-07-11 stsp goto done;
1346 7d5807f4 2019-07-11 stsp
1347 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1348 f2ea84fa 2019-07-27 stsp if (error)
1349 f2ea84fa 2019-07-27 stsp goto done;
1350 507dc3bb 2018-12-29 stsp
1351 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1352 c9956ddf 2019-09-08 stsp NULL);
1353 507dc3bb 2018-12-29 stsp if (error != NULL)
1354 507dc3bb 2018-12-29 stsp goto done;
1355 507dc3bb 2018-12-29 stsp
1356 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1357 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1358 087fb88c 2019-08-04 stsp if (error)
1359 087fb88c 2019-08-04 stsp goto done;
1360 087fb88c 2019-08-04 stsp
1361 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1362 0266afb7 2019-01-04 stsp if (error)
1363 0266afb7 2019-01-04 stsp goto done;
1364 0266afb7 2019-01-04 stsp
1365 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1366 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1367 024e9686 2019-05-14 stsp if (error != NULL)
1368 024e9686 2019-05-14 stsp goto done;
1369 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1370 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1371 9c4b8182 2019-01-02 stsp if (error != NULL)
1372 9c4b8182 2019-01-02 stsp goto done;
1373 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1374 507dc3bb 2018-12-29 stsp if (error != NULL)
1375 507dc3bb 2018-12-29 stsp goto done;
1376 507dc3bb 2018-12-29 stsp } else {
1377 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
1378 04f57cb3 2019-07-25 stsp free(commit_id_str);
1379 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1380 30837e32 2019-07-25 stsp if (error)
1381 a297e751 2019-07-11 stsp goto done;
1382 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1383 a297e751 2019-07-11 stsp if (error)
1384 507dc3bb 2018-12-29 stsp goto done;
1385 507dc3bb 2018-12-29 stsp }
1386 35c965b2 2018-12-29 stsp
1387 a1fb16d8 2019-05-24 stsp if (branch_name) {
1388 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1389 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1390 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1391 f2ea84fa 2019-07-27 stsp continue;
1392 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1393 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1394 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1395 024e9686 2019-05-14 stsp goto done;
1396 024e9686 2019-05-14 stsp }
1397 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1398 024e9686 2019-05-14 stsp if (error)
1399 024e9686 2019-05-14 stsp goto done;
1400 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1401 3aef623b 2019-10-15 stsp repo);
1402 a367ff0f 2019-05-14 stsp free(head_commit_id);
1403 024e9686 2019-05-14 stsp if (error != NULL)
1404 024e9686 2019-05-14 stsp goto done;
1405 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1406 a367ff0f 2019-05-14 stsp if (error)
1407 a367ff0f 2019-05-14 stsp goto done;
1408 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1409 024e9686 2019-05-14 stsp if (error)
1410 024e9686 2019-05-14 stsp goto done;
1411 024e9686 2019-05-14 stsp } else {
1412 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1413 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1414 a367ff0f 2019-05-14 stsp if (error != NULL) {
1415 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1416 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1417 024e9686 2019-05-14 stsp goto done;
1418 a367ff0f 2019-05-14 stsp }
1419 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1420 a367ff0f 2019-05-14 stsp if (error)
1421 a367ff0f 2019-05-14 stsp goto done;
1422 024e9686 2019-05-14 stsp }
1423 507dc3bb 2018-12-29 stsp
1424 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1425 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1426 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1427 507dc3bb 2018-12-29 stsp commit_id);
1428 507dc3bb 2018-12-29 stsp if (error)
1429 507dc3bb 2018-12-29 stsp goto done;
1430 507dc3bb 2018-12-29 stsp }
1431 507dc3bb 2018-12-29 stsp
1432 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1433 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1434 507dc3bb 2018-12-29 stsp if (error != NULL)
1435 507dc3bb 2018-12-29 stsp goto done;
1436 9c4b8182 2019-01-02 stsp
1437 784955db 2019-01-12 stsp if (did_something)
1438 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1439 784955db 2019-01-12 stsp else
1440 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1441 507dc3bb 2018-12-29 stsp done:
1442 c09a553d 2018-03-12 stsp free(worktree_path);
1443 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1444 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1445 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1446 507dc3bb 2018-12-29 stsp free(commit_id);
1447 9c4b8182 2019-01-02 stsp free(commit_id_str);
1448 c09a553d 2018-03-12 stsp return error;
1449 c09a553d 2018-03-12 stsp }
1450 c09a553d 2018-03-12 stsp
1451 f42b1b34 2018-03-12 stsp static const struct got_error *
1452 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1453 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1454 63035f9f 2019-10-06 stsp struct got_repository *repo)
1455 5c860e29 2018-03-12 stsp {
1456 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1457 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1458 79109fed 2018-03-27 stsp
1459 44392932 2019-08-25 stsp if (blob_id1) {
1460 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1461 44392932 2019-08-25 stsp if (err)
1462 44392932 2019-08-25 stsp goto done;
1463 44392932 2019-08-25 stsp }
1464 79109fed 2018-03-27 stsp
1465 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1466 44392932 2019-08-25 stsp if (err)
1467 44392932 2019-08-25 stsp goto done;
1468 79109fed 2018-03-27 stsp
1469 44392932 2019-08-25 stsp while (path[0] == '/')
1470 44392932 2019-08-25 stsp path++;
1471 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1472 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1473 44392932 2019-08-25 stsp done:
1474 44392932 2019-08-25 stsp if (blob1)
1475 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1476 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1477 44392932 2019-08-25 stsp return err;
1478 44392932 2019-08-25 stsp }
1479 44392932 2019-08-25 stsp
1480 44392932 2019-08-25 stsp static const struct got_error *
1481 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1482 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1483 63035f9f 2019-10-06 stsp struct got_repository *repo)
1484 44392932 2019-08-25 stsp {
1485 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1486 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1487 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1488 44392932 2019-08-25 stsp
1489 44392932 2019-08-25 stsp if (tree_id1) {
1490 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1491 79109fed 2018-03-27 stsp if (err)
1492 44392932 2019-08-25 stsp goto done;
1493 79109fed 2018-03-27 stsp }
1494 79109fed 2018-03-27 stsp
1495 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1496 0f2b3dca 2018-12-22 stsp if (err)
1497 0f2b3dca 2018-12-22 stsp goto done;
1498 0f2b3dca 2018-12-22 stsp
1499 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1500 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1501 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1502 44392932 2019-08-25 stsp while (path[0] == '/')
1503 44392932 2019-08-25 stsp path++;
1504 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1505 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1506 0f2b3dca 2018-12-22 stsp done:
1507 79109fed 2018-03-27 stsp if (tree1)
1508 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1509 366e0a5f 2019-10-10 stsp if (tree2)
1510 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1511 44392932 2019-08-25 stsp return err;
1512 44392932 2019-08-25 stsp }
1513 44392932 2019-08-25 stsp
1514 44392932 2019-08-25 stsp static const struct got_error *
1515 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1516 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1517 44392932 2019-08-25 stsp {
1518 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1519 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1520 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1521 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1522 44392932 2019-08-25 stsp struct got_object_qid *qid;
1523 44392932 2019-08-25 stsp
1524 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1525 44392932 2019-08-25 stsp if (qid != NULL) {
1526 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1527 44392932 2019-08-25 stsp qid->id);
1528 44392932 2019-08-25 stsp if (err)
1529 44392932 2019-08-25 stsp return err;
1530 44392932 2019-08-25 stsp }
1531 44392932 2019-08-25 stsp
1532 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1533 44392932 2019-08-25 stsp int obj_type;
1534 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1535 44392932 2019-08-25 stsp if (err)
1536 44392932 2019-08-25 stsp goto done;
1537 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1538 44392932 2019-08-25 stsp if (err) {
1539 44392932 2019-08-25 stsp free(obj_id2);
1540 44392932 2019-08-25 stsp goto done;
1541 44392932 2019-08-25 stsp }
1542 44392932 2019-08-25 stsp if (pcommit) {
1543 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1544 44392932 2019-08-25 stsp qid->id, path);
1545 44392932 2019-08-25 stsp if (err) {
1546 44392932 2019-08-25 stsp free(obj_id2);
1547 44392932 2019-08-25 stsp goto done;
1548 44392932 2019-08-25 stsp }
1549 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1550 44392932 2019-08-25 stsp if (err) {
1551 44392932 2019-08-25 stsp free(obj_id2);
1552 44392932 2019-08-25 stsp goto done;
1553 44392932 2019-08-25 stsp }
1554 44392932 2019-08-25 stsp }
1555 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1556 44392932 2019-08-25 stsp if (err) {
1557 44392932 2019-08-25 stsp free(obj_id2);
1558 44392932 2019-08-25 stsp goto done;
1559 44392932 2019-08-25 stsp }
1560 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1561 44392932 2019-08-25 stsp switch (obj_type) {
1562 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1563 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1564 63035f9f 2019-10-06 stsp 0, repo);
1565 44392932 2019-08-25 stsp break;
1566 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1567 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1568 63035f9f 2019-10-06 stsp 0, repo);
1569 44392932 2019-08-25 stsp break;
1570 44392932 2019-08-25 stsp default:
1571 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1572 44392932 2019-08-25 stsp break;
1573 44392932 2019-08-25 stsp }
1574 44392932 2019-08-25 stsp free(obj_id1);
1575 44392932 2019-08-25 stsp free(obj_id2);
1576 44392932 2019-08-25 stsp } else {
1577 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1578 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1579 44392932 2019-08-25 stsp if (err)
1580 44392932 2019-08-25 stsp goto done;
1581 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1582 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1583 44392932 2019-08-25 stsp if (err)
1584 44392932 2019-08-25 stsp goto done;
1585 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1586 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1587 44392932 2019-08-25 stsp }
1588 44392932 2019-08-25 stsp
1589 44392932 2019-08-25 stsp done:
1590 0f2b3dca 2018-12-22 stsp free(id_str1);
1591 0f2b3dca 2018-12-22 stsp free(id_str2);
1592 44392932 2019-08-25 stsp if (pcommit)
1593 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1594 79109fed 2018-03-27 stsp return err;
1595 79109fed 2018-03-27 stsp }
1596 79109fed 2018-03-27 stsp
1597 4bb494d5 2018-06-16 stsp static char *
1598 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1599 6c281f94 2018-06-11 stsp {
1600 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1601 09867e48 2019-08-13 stsp char *p, *s;
1602 09867e48 2019-08-13 stsp
1603 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1604 09867e48 2019-08-13 stsp if (tm == NULL)
1605 09867e48 2019-08-13 stsp return NULL;
1606 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1607 09867e48 2019-08-13 stsp if (s == NULL)
1608 09867e48 2019-08-13 stsp return NULL;
1609 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1610 6c281f94 2018-06-11 stsp if (p)
1611 6c281f94 2018-06-11 stsp *p = '\0';
1612 6c281f94 2018-06-11 stsp return s;
1613 6c281f94 2018-06-11 stsp }
1614 dc424a06 2019-08-07 stsp
1615 6841bf13 2019-11-29 kn static const struct got_error *
1616 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1617 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1618 6841bf13 2019-11-29 kn {
1619 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1620 6841bf13 2019-11-29 kn regmatch_t regmatch;
1621 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1622 6841bf13 2019-11-29 kn
1623 6841bf13 2019-11-29 kn *have_match = 0;
1624 6841bf13 2019-11-29 kn
1625 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1626 6841bf13 2019-11-29 kn if (err)
1627 6841bf13 2019-11-29 kn return err;
1628 6841bf13 2019-11-29 kn
1629 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1630 6841bf13 2019-11-29 kn if (err)
1631 6841bf13 2019-11-29 kn goto done;
1632 6841bf13 2019-11-29 kn
1633 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1634 6841bf13 2019-11-29 kn *have_match = 1;
1635 6841bf13 2019-11-29 kn done:
1636 6841bf13 2019-11-29 kn free(id_str);
1637 6841bf13 2019-11-29 kn free(logmsg);
1638 6841bf13 2019-11-29 kn return err;
1639 6841bf13 2019-11-29 kn }
1640 6841bf13 2019-11-29 kn
1641 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1642 6c281f94 2018-06-11 stsp
1643 79109fed 2018-03-27 stsp static const struct got_error *
1644 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1645 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1646 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1647 79109fed 2018-03-27 stsp {
1648 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1649 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1650 6c281f94 2018-06-11 stsp char datebuf[26];
1651 45d799e2 2018-12-23 stsp time_t committer_time;
1652 45d799e2 2018-12-23 stsp const char *author, *committer;
1653 199a4027 2019-02-02 stsp char *refs_str = NULL;
1654 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1655 5c860e29 2018-03-12 stsp
1656 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1657 199a4027 2019-02-02 stsp char *s;
1658 199a4027 2019-02-02 stsp const char *name;
1659 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1660 a436ad14 2019-08-13 stsp int cmp;
1661 a436ad14 2019-08-13 stsp
1662 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1663 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1664 d9498b20 2019-02-05 stsp continue;
1665 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1666 199a4027 2019-02-02 stsp name += 5;
1667 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1668 7143d404 2019-03-12 stsp continue;
1669 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1670 e34f9ed6 2019-02-02 stsp name += 6;
1671 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1672 141c2bff 2019-02-04 stsp name += 8;
1673 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1674 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1675 5d844a1e 2019-08-13 stsp if (err) {
1676 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1677 5d844a1e 2019-08-13 stsp return err;
1678 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1679 5d844a1e 2019-08-13 stsp err = NULL;
1680 5d844a1e 2019-08-13 stsp tag = NULL;
1681 5d844a1e 2019-08-13 stsp }
1682 a436ad14 2019-08-13 stsp }
1683 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1684 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1685 a436ad14 2019-08-13 stsp if (tag)
1686 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1687 a436ad14 2019-08-13 stsp if (cmp != 0)
1688 a436ad14 2019-08-13 stsp continue;
1689 199a4027 2019-02-02 stsp s = refs_str;
1690 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1691 199a4027 2019-02-02 stsp name) == -1) {
1692 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1693 199a4027 2019-02-02 stsp free(s);
1694 34ca4898 2019-08-13 stsp return err;
1695 199a4027 2019-02-02 stsp }
1696 199a4027 2019-02-02 stsp free(s);
1697 199a4027 2019-02-02 stsp }
1698 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1699 8bf5b3c9 2018-03-17 stsp if (err)
1700 8bf5b3c9 2018-03-17 stsp return err;
1701 788c352e 2018-06-16 stsp
1702 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1703 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1704 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1705 832c249c 2018-06-10 stsp free(id_str);
1706 d3d493d7 2019-02-21 stsp id_str = NULL;
1707 d3d493d7 2019-02-21 stsp free(refs_str);
1708 d3d493d7 2019-02-21 stsp refs_str = NULL;
1709 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1710 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1711 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1712 09867e48 2019-08-13 stsp if (datestr)
1713 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1714 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1715 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1716 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1717 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1718 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1719 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1720 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1721 3fe1abad 2018-06-10 stsp int n = 1;
1722 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1723 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1724 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1725 a0603db2 2018-06-10 stsp if (err)
1726 a0603db2 2018-06-10 stsp return err;
1727 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1728 a0603db2 2018-06-10 stsp free(id_str);
1729 a0603db2 2018-06-10 stsp }
1730 a0603db2 2018-06-10 stsp }
1731 832c249c 2018-06-10 stsp
1732 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1733 5943eee2 2019-08-13 stsp if (err)
1734 5943eee2 2019-08-13 stsp return err;
1735 8bf5b3c9 2018-03-17 stsp
1736 621015ac 2018-07-23 stsp logmsg = logmsg0;
1737 832c249c 2018-06-10 stsp do {
1738 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1739 832c249c 2018-06-10 stsp if (line)
1740 832c249c 2018-06-10 stsp printf(" %s\n", line);
1741 832c249c 2018-06-10 stsp } while (line);
1742 621015ac 2018-07-23 stsp free(logmsg0);
1743 832c249c 2018-06-10 stsp
1744 971751ac 2018-03-27 stsp if (show_patch) {
1745 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1746 971751ac 2018-03-27 stsp if (err == 0)
1747 971751ac 2018-03-27 stsp printf("\n");
1748 971751ac 2018-03-27 stsp }
1749 07862c20 2018-09-15 stsp
1750 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1751 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1752 07862c20 2018-09-15 stsp return err;
1753 f42b1b34 2018-03-12 stsp }
1754 5c860e29 2018-03-12 stsp
1755 f42b1b34 2018-03-12 stsp static const struct got_error *
1756 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1757 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1758 463a997a 2019-12-08 kn int diff_context, int limit, int first_parent_traversal,
1759 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1760 f42b1b34 2018-03-12 stsp {
1761 f42b1b34 2018-03-12 stsp const struct got_error *err;
1762 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1763 6841bf13 2019-11-29 kn regex_t regex;
1764 6841bf13 2019-11-29 kn int have_match;
1765 372ccdbb 2018-06-10 stsp
1766 6841bf13 2019-11-29 kn if (search_pattern &&
1767 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1768 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1769 6841bf13 2019-11-29 kn
1770 31cedeaf 2018-09-15 stsp err = got_commit_graph_open(&graph, root_id, path,
1771 31cedeaf 2018-09-15 stsp first_parent_traversal, repo);
1772 f42b1b34 2018-03-12 stsp if (err)
1773 f42b1b34 2018-03-12 stsp return err;
1774 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1775 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1776 372ccdbb 2018-06-10 stsp if (err)
1777 fcc85cad 2018-07-22 stsp goto done;
1778 656b1f76 2019-05-11 jcs for (;;) {
1779 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1780 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1781 8bf5b3c9 2018-03-17 stsp
1782 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1783 84453469 2018-11-11 stsp break;
1784 84453469 2018-11-11 stsp
1785 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1786 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1787 372ccdbb 2018-06-10 stsp if (err) {
1788 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1789 9ba79e04 2018-06-11 stsp err = NULL;
1790 ee780d5c 2020-01-04 stsp break;
1791 7e665116 2018-04-02 stsp }
1792 b43fbaa0 2018-06-11 stsp if (id == NULL)
1793 7e665116 2018-04-02 stsp break;
1794 b43fbaa0 2018-06-11 stsp
1795 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1796 b43fbaa0 2018-06-11 stsp if (err)
1797 fcc85cad 2018-07-22 stsp break;
1798 6841bf13 2019-11-29 kn
1799 6841bf13 2019-11-29 kn if (search_pattern) {
1800 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1801 6841bf13 2019-11-29 kn if (err) {
1802 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1803 6841bf13 2019-11-29 kn break;
1804 6841bf13 2019-11-29 kn }
1805 6841bf13 2019-11-29 kn if (have_match == 0) {
1806 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1807 6841bf13 2019-11-29 kn continue;
1808 6841bf13 2019-11-29 kn }
1809 6841bf13 2019-11-29 kn }
1810 6841bf13 2019-11-29 kn
1811 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1812 44392932 2019-08-25 stsp diff_context, refs);
1813 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1814 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1815 7e665116 2018-04-02 stsp break;
1816 31cedeaf 2018-09-15 stsp }
1817 fcc85cad 2018-07-22 stsp done:
1818 6841bf13 2019-11-29 kn if (search_pattern)
1819 6841bf13 2019-11-29 kn regfree(&regex);
1820 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1821 f42b1b34 2018-03-12 stsp return err;
1822 f42b1b34 2018-03-12 stsp }
1823 5c860e29 2018-03-12 stsp
1824 4ed7e80c 2018-05-20 stsp __dead static void
1825 6f3d1eb0 2018-03-12 stsp usage_log(void)
1826 6f3d1eb0 2018-03-12 stsp {
1827 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1828 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1829 6f3d1eb0 2018-03-12 stsp exit(1);
1830 b1ebc001 2019-08-13 stsp }
1831 b1ebc001 2019-08-13 stsp
1832 b1ebc001 2019-08-13 stsp static int
1833 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1834 b1ebc001 2019-08-13 stsp {
1835 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1836 b1ebc001 2019-08-13 stsp long long n;
1837 b1ebc001 2019-08-13 stsp const char *errstr;
1838 b1ebc001 2019-08-13 stsp
1839 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1840 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1841 b1ebc001 2019-08-13 stsp return 0;
1842 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1843 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1844 b1ebc001 2019-08-13 stsp return 0;
1845 b1ebc001 2019-08-13 stsp return n;
1846 6f3d1eb0 2018-03-12 stsp }
1847 6f3d1eb0 2018-03-12 stsp
1848 4ed7e80c 2018-05-20 stsp static const struct got_error *
1849 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1850 f42b1b34 2018-03-12 stsp {
1851 f42b1b34 2018-03-12 stsp const struct got_error *error;
1852 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1853 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1854 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1855 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1856 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1857 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1858 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1859 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1860 64a96a6d 2018-04-01 stsp const char *errstr;
1861 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1862 1b3893a2 2019-03-18 stsp
1863 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1864 5c860e29 2018-03-12 stsp
1865 6715a751 2018-03-16 stsp #ifndef PROFILE
1866 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1867 6098196c 2019-01-04 stsp NULL)
1868 ad242220 2018-09-08 stsp == -1)
1869 f42b1b34 2018-03-12 stsp err(1, "pledge");
1870 6715a751 2018-03-16 stsp #endif
1871 79109fed 2018-03-27 stsp
1872 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1873 b1ebc001 2019-08-13 stsp
1874 6841bf13 2019-11-29 kn while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1875 79109fed 2018-03-27 stsp switch (ch) {
1876 79109fed 2018-03-27 stsp case 'p':
1877 79109fed 2018-03-27 stsp show_patch = 1;
1878 d142fc45 2018-04-01 stsp break;
1879 d142fc45 2018-04-01 stsp case 'c':
1880 d142fc45 2018-04-01 stsp start_commit = optarg;
1881 64a96a6d 2018-04-01 stsp break;
1882 c0cc5c62 2018-10-18 stsp case 'C':
1883 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1884 4a8520aa 2018-10-18 stsp &errstr);
1885 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1886 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1887 c0cc5c62 2018-10-18 stsp break;
1888 64a96a6d 2018-04-01 stsp case 'l':
1889 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1890 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1891 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1892 cc54c501 2019-07-15 stsp break;
1893 cc54c501 2019-07-15 stsp case 'f':
1894 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1895 a0603db2 2018-06-10 stsp break;
1896 04ca23f4 2018-07-16 stsp case 'r':
1897 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1898 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1899 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1900 9ba1d308 2019-10-21 stsp optarg);
1901 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1902 04ca23f4 2018-07-16 stsp break;
1903 6841bf13 2019-11-29 kn case 's':
1904 6841bf13 2019-11-29 kn search_pattern = optarg;
1905 6841bf13 2019-11-29 kn break;
1906 79109fed 2018-03-27 stsp default:
1907 2deda0b9 2019-03-07 stsp usage_log();
1908 79109fed 2018-03-27 stsp /* NOTREACHED */
1909 79109fed 2018-03-27 stsp }
1910 79109fed 2018-03-27 stsp }
1911 79109fed 2018-03-27 stsp
1912 79109fed 2018-03-27 stsp argc -= optind;
1913 79109fed 2018-03-27 stsp argv += optind;
1914 f42b1b34 2018-03-12 stsp
1915 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1916 dc1edbfa 2019-11-29 kn diff_context = 3;
1917 dc1edbfa 2019-11-29 kn else if (!show_patch)
1918 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1919 dc1edbfa 2019-11-29 kn
1920 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1921 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1922 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1923 04ca23f4 2018-07-16 stsp goto done;
1924 04ca23f4 2018-07-16 stsp }
1925 cffc0aa4 2019-02-05 stsp
1926 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1927 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1928 cffc0aa4 2019-02-05 stsp goto done;
1929 cffc0aa4 2019-02-05 stsp error = NULL;
1930 cffc0aa4 2019-02-05 stsp
1931 e7301579 2019-03-18 stsp if (argc == 0) {
1932 cbd1af7a 2019-03-18 stsp path = strdup("");
1933 e7301579 2019-03-18 stsp if (path == NULL) {
1934 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1935 cbd1af7a 2019-03-18 stsp goto done;
1936 e7301579 2019-03-18 stsp }
1937 e7301579 2019-03-18 stsp } else if (argc == 1) {
1938 e7301579 2019-03-18 stsp if (worktree) {
1939 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1940 e7301579 2019-03-18 stsp argv[0]);
1941 e7301579 2019-03-18 stsp if (error)
1942 e7301579 2019-03-18 stsp goto done;
1943 e7301579 2019-03-18 stsp } else {
1944 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1945 e7301579 2019-03-18 stsp if (path == NULL) {
1946 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1947 e7301579 2019-03-18 stsp goto done;
1948 e7301579 2019-03-18 stsp }
1949 e7301579 2019-03-18 stsp }
1950 cbd1af7a 2019-03-18 stsp } else
1951 cbd1af7a 2019-03-18 stsp usage_log();
1952 cbd1af7a 2019-03-18 stsp
1953 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1954 5486daa2 2019-05-11 stsp repo_path = worktree ?
1955 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1956 5486daa2 2019-05-11 stsp }
1957 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1958 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1959 cffc0aa4 2019-02-05 stsp goto done;
1960 04ca23f4 2018-07-16 stsp }
1961 6098196c 2019-01-04 stsp
1962 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1963 d7d4f210 2018-03-12 stsp if (error != NULL)
1964 04ca23f4 2018-07-16 stsp goto done;
1965 f42b1b34 2018-03-12 stsp
1966 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1967 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1968 c02c541e 2019-03-29 stsp if (error)
1969 c02c541e 2019-03-29 stsp goto done;
1970 c02c541e 2019-03-29 stsp
1971 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1972 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1973 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1974 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1975 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1976 3235492e 2018-04-01 stsp if (error != NULL)
1977 3235492e 2018-04-01 stsp return error;
1978 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1979 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1980 3235492e 2018-04-01 stsp if (error != NULL)
1981 3235492e 2018-04-01 stsp return error;
1982 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1983 3235492e 2018-04-01 stsp } else {
1984 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1985 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1986 3235492e 2018-04-01 stsp if (error == NULL) {
1987 1caad61b 2019-02-01 stsp int obj_type;
1988 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1989 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1990 d1f2edc9 2018-06-13 stsp if (error != NULL)
1991 1caad61b 2019-02-01 stsp goto done;
1992 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1993 1caad61b 2019-02-01 stsp if (error != NULL)
1994 1caad61b 2019-02-01 stsp goto done;
1995 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1996 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1997 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1998 1caad61b 2019-02-01 stsp if (error != NULL)
1999 1caad61b 2019-02-01 stsp goto done;
2000 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2001 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2002 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2003 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2004 1caad61b 2019-02-01 stsp goto done;
2005 1caad61b 2019-02-01 stsp }
2006 1caad61b 2019-02-01 stsp free(id);
2007 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2008 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2009 1caad61b 2019-02-01 stsp if (id == NULL)
2010 638f9024 2019-05-13 stsp error = got_error_from_errno(
2011 230a42bd 2019-05-11 jcs "got_object_id_dup");
2012 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2013 1caad61b 2019-02-01 stsp if (error)
2014 1caad61b 2019-02-01 stsp goto done;
2015 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2016 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2017 1caad61b 2019-02-01 stsp goto done;
2018 1caad61b 2019-02-01 stsp }
2019 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2020 d1f2edc9 2018-06-13 stsp if (error != NULL)
2021 1caad61b 2019-02-01 stsp goto done;
2022 d1f2edc9 2018-06-13 stsp }
2023 15a94983 2018-12-23 stsp if (commit == NULL) {
2024 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2025 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2026 d1f2edc9 2018-06-13 stsp if (error != NULL)
2027 d1f2edc9 2018-06-13 stsp return error;
2028 3235492e 2018-04-01 stsp }
2029 3235492e 2018-04-01 stsp }
2030 d7d4f210 2018-03-12 stsp if (error != NULL)
2031 04ca23f4 2018-07-16 stsp goto done;
2032 04ca23f4 2018-07-16 stsp
2033 deeabeae 2019-08-27 stsp if (worktree) {
2034 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2035 deeabeae 2019-08-27 stsp char *p;
2036 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2037 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2038 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2039 deeabeae 2019-08-27 stsp goto done;
2040 deeabeae 2019-08-27 stsp }
2041 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2042 deeabeae 2019-08-27 stsp free(p);
2043 deeabeae 2019-08-27 stsp } else
2044 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2045 04ca23f4 2018-07-16 stsp if (error != NULL)
2046 04ca23f4 2018-07-16 stsp goto done;
2047 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2048 04ca23f4 2018-07-16 stsp free(path);
2049 04ca23f4 2018-07-16 stsp path = in_repo_path;
2050 04ca23f4 2018-07-16 stsp }
2051 199a4027 2019-02-02 stsp
2052 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2053 199a4027 2019-02-02 stsp if (error)
2054 199a4027 2019-02-02 stsp goto done;
2055 04ca23f4 2018-07-16 stsp
2056 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2057 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
2058 04ca23f4 2018-07-16 stsp done:
2059 04ca23f4 2018-07-16 stsp free(path);
2060 04ca23f4 2018-07-16 stsp free(repo_path);
2061 04ca23f4 2018-07-16 stsp free(cwd);
2062 f42b1b34 2018-03-12 stsp free(id);
2063 cffc0aa4 2019-02-05 stsp if (worktree)
2064 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2065 ad242220 2018-09-08 stsp if (repo) {
2066 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2067 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2068 ad242220 2018-09-08 stsp if (error == NULL)
2069 ad242220 2018-09-08 stsp error = repo_error;
2070 ad242220 2018-09-08 stsp }
2071 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2072 8bf5b3c9 2018-03-17 stsp return error;
2073 5c860e29 2018-03-12 stsp }
2074 5c860e29 2018-03-12 stsp
2075 4ed7e80c 2018-05-20 stsp __dead static void
2076 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2077 3f8b7d6a 2018-04-01 stsp {
2078 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2079 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2080 3f8b7d6a 2018-04-01 stsp exit(1);
2081 b00d56cd 2018-04-01 stsp }
2082 b00d56cd 2018-04-01 stsp
2083 b72f483a 2019-02-05 stsp struct print_diff_arg {
2084 b72f483a 2019-02-05 stsp struct got_repository *repo;
2085 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2086 b72f483a 2019-02-05 stsp int diff_context;
2087 3fc0c068 2019-02-10 stsp const char *id_str;
2088 3fc0c068 2019-02-10 stsp int header_shown;
2089 98eaaa12 2019-08-03 stsp int diff_staged;
2090 63035f9f 2019-10-06 stsp int ignore_whitespace;
2091 b72f483a 2019-02-05 stsp };
2092 b72f483a 2019-02-05 stsp
2093 4ed7e80c 2018-05-20 stsp static const struct got_error *
2094 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2095 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2096 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2097 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2098 b72f483a 2019-02-05 stsp {
2099 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2100 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2101 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2102 12463d8b 2019-12-13 stsp int fd = -1;
2103 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2104 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2105 b72f483a 2019-02-05 stsp struct stat sb;
2106 b72f483a 2019-02-05 stsp
2107 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2108 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2109 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2110 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2111 98eaaa12 2019-08-03 stsp return NULL;
2112 98eaaa12 2019-08-03 stsp } else {
2113 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2114 98eaaa12 2019-08-03 stsp return NULL;
2115 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2116 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2117 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2118 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2119 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2120 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2121 98eaaa12 2019-08-03 stsp return NULL;
2122 98eaaa12 2019-08-03 stsp }
2123 3fc0c068 2019-02-10 stsp
2124 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2125 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2126 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2127 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2128 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2129 3fc0c068 2019-02-10 stsp }
2130 b72f483a 2019-02-05 stsp
2131 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2132 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2133 98eaaa12 2019-08-03 stsp switch (staged_status) {
2134 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2135 98eaaa12 2019-08-03 stsp label1 = path;
2136 98eaaa12 2019-08-03 stsp label2 = path;
2137 98eaaa12 2019-08-03 stsp break;
2138 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2139 98eaaa12 2019-08-03 stsp label2 = path;
2140 98eaaa12 2019-08-03 stsp break;
2141 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2142 98eaaa12 2019-08-03 stsp label1 = path;
2143 98eaaa12 2019-08-03 stsp break;
2144 98eaaa12 2019-08-03 stsp default:
2145 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2146 98eaaa12 2019-08-03 stsp }
2147 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2148 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2149 63035f9f 2019-10-06 stsp a->repo, stdout);
2150 98eaaa12 2019-08-03 stsp }
2151 98eaaa12 2019-08-03 stsp
2152 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2153 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2154 4ce46740 2019-08-08 stsp char *id_str;
2155 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2156 408b4ebc 2019-08-03 stsp 8192);
2157 4ce46740 2019-08-08 stsp if (err)
2158 4ce46740 2019-08-08 stsp goto done;
2159 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2160 4ce46740 2019-08-08 stsp if (err)
2161 4ce46740 2019-08-08 stsp goto done;
2162 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2163 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2164 4ce46740 2019-08-08 stsp free(id_str);
2165 4ce46740 2019-08-08 stsp goto done;
2166 4ce46740 2019-08-08 stsp }
2167 4ce46740 2019-08-08 stsp free(id_str);
2168 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2169 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2170 4ce46740 2019-08-08 stsp if (err)
2171 4ce46740 2019-08-08 stsp goto done;
2172 4ce46740 2019-08-08 stsp }
2173 d00136be 2019-03-26 stsp
2174 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2175 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2176 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2177 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2178 2ec1f75b 2019-03-26 stsp goto done;
2179 2ec1f75b 2019-03-26 stsp }
2180 b72f483a 2019-02-05 stsp
2181 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2182 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2183 12463d8b 2019-12-13 stsp if (fd == -1) {
2184 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2185 12463d8b 2019-12-13 stsp goto done;
2186 12463d8b 2019-12-13 stsp }
2187 12463d8b 2019-12-13 stsp } else {
2188 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2189 12463d8b 2019-12-13 stsp if (fd == -1) {
2190 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2191 12463d8b 2019-12-13 stsp goto done;
2192 12463d8b 2019-12-13 stsp }
2193 12463d8b 2019-12-13 stsp }
2194 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2195 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2196 2ec1f75b 2019-03-26 stsp goto done;
2197 2ec1f75b 2019-03-26 stsp }
2198 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2199 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2200 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2201 2ec1f75b 2019-03-26 stsp goto done;
2202 2ec1f75b 2019-03-26 stsp }
2203 12463d8b 2019-12-13 stsp fd = -1;
2204 2ec1f75b 2019-03-26 stsp } else
2205 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2206 b72f483a 2019-02-05 stsp
2207 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2208 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2209 b72f483a 2019-02-05 stsp done:
2210 b72f483a 2019-02-05 stsp if (blob1)
2211 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2212 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2213 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2214 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2215 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2216 b72f483a 2019-02-05 stsp free(abspath);
2217 927df6b7 2019-02-10 stsp return err;
2218 927df6b7 2019-02-10 stsp }
2219 927df6b7 2019-02-10 stsp
2220 927df6b7 2019-02-10 stsp static const struct got_error *
2221 0adb7196 2019-08-11 stsp match_object_id(struct got_object_id **id, char **label,
2222 01073a5d 2019-08-22 stsp const char *id_str, int obj_type, int resolve_tags,
2223 01073a5d 2019-08-22 stsp struct got_repository *repo)
2224 0adb7196 2019-08-11 stsp {
2225 0adb7196 2019-08-11 stsp const struct got_error *err;
2226 d24820bf 2019-08-11 stsp struct got_tag_object *tag;
2227 0adb7196 2019-08-11 stsp struct got_reference *ref = NULL;
2228 0adb7196 2019-08-11 stsp
2229 0adb7196 2019-08-11 stsp *id = NULL;
2230 0adb7196 2019-08-11 stsp *label = NULL;
2231 d24820bf 2019-08-11 stsp
2232 01073a5d 2019-08-22 stsp if (resolve_tags) {
2233 01073a5d 2019-08-22 stsp err = got_repo_object_match_tag(&tag, id_str, GOT_OBJ_TYPE_ANY,
2234 01073a5d 2019-08-22 stsp repo);
2235 01073a5d 2019-08-22 stsp if (err == NULL) {
2236 01073a5d 2019-08-22 stsp *id = got_object_id_dup(
2237 01073a5d 2019-08-22 stsp got_object_tag_get_object_id(tag));
2238 01073a5d 2019-08-22 stsp if (*id == NULL)
2239 01073a5d 2019-08-22 stsp err = got_error_from_errno("got_object_id_dup");
2240 01073a5d 2019-08-22 stsp else if (asprintf(label, "refs/tags/%s",
2241 01073a5d 2019-08-22 stsp got_object_tag_get_name(tag)) == -1) {
2242 01073a5d 2019-08-22 stsp err = got_error_from_errno("asprintf");
2243 32f0ab81 2019-08-28 hiltjo free(*id);
2244 01073a5d 2019-08-22 stsp *id = NULL;
2245 01073a5d 2019-08-22 stsp }
2246 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
2247 01073a5d 2019-08-22 stsp return err;
2248 01073a5d 2019-08-22 stsp } else if (err->code != GOT_ERR_NO_OBJ)
2249 01073a5d 2019-08-22 stsp return err;
2250 01073a5d 2019-08-22 stsp }
2251 0adb7196 2019-08-11 stsp
2252 0adb7196 2019-08-11 stsp err = got_repo_match_object_id_prefix(id, id_str, obj_type, repo);
2253 0adb7196 2019-08-11 stsp if (err) {
2254 0adb7196 2019-08-11 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
2255 0adb7196 2019-08-11 stsp return err;
2256 0adb7196 2019-08-11 stsp err = got_ref_open(&ref, repo, id_str, 0);
2257 0adb7196 2019-08-11 stsp if (err != NULL)
2258 0adb7196 2019-08-11 stsp goto done;
2259 0adb7196 2019-08-11 stsp *label = strdup(got_ref_get_name(ref));
2260 0adb7196 2019-08-11 stsp if (*label == NULL) {
2261 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2262 0adb7196 2019-08-11 stsp goto done;
2263 0adb7196 2019-08-11 stsp }
2264 0adb7196 2019-08-11 stsp err = got_ref_resolve(id, repo, ref);
2265 0adb7196 2019-08-11 stsp } else {
2266 0adb7196 2019-08-11 stsp err = got_object_id_str(label, *id);
2267 0adb7196 2019-08-11 stsp if (*label == NULL) {
2268 0adb7196 2019-08-11 stsp err = got_error_from_errno("strdup");
2269 0adb7196 2019-08-11 stsp goto done;
2270 0adb7196 2019-08-11 stsp }
2271 0adb7196 2019-08-11 stsp }
2272 0adb7196 2019-08-11 stsp done:
2273 0adb7196 2019-08-11 stsp if (ref)
2274 0adb7196 2019-08-11 stsp got_ref_close(ref);
2275 0adb7196 2019-08-11 stsp return err;
2276 0adb7196 2019-08-11 stsp }
2277 0adb7196 2019-08-11 stsp
2278 0adb7196 2019-08-11 stsp
2279 0adb7196 2019-08-11 stsp static const struct got_error *
2280 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2281 b00d56cd 2018-04-01 stsp {
2282 b00d56cd 2018-04-01 stsp const struct got_error *error;
2283 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2284 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2285 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2286 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2287 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2288 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2289 15a94983 2018-12-23 stsp int type1, type2;
2290 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2291 c0cc5c62 2018-10-18 stsp const char *errstr;
2292 927df6b7 2019-02-10 stsp char *path = NULL;
2293 b00d56cd 2018-04-01 stsp
2294 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2295 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2296 25eccc22 2019-01-04 stsp NULL) == -1)
2297 b00d56cd 2018-04-01 stsp err(1, "pledge");
2298 b00d56cd 2018-04-01 stsp #endif
2299 b00d56cd 2018-04-01 stsp
2300 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2301 b00d56cd 2018-04-01 stsp switch (ch) {
2302 c0cc5c62 2018-10-18 stsp case 'C':
2303 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2304 dfcab68b 2019-11-29 kn &errstr);
2305 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2306 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2307 c0cc5c62 2018-10-18 stsp break;
2308 b72f483a 2019-02-05 stsp case 'r':
2309 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2310 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2311 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2312 9ba1d308 2019-10-21 stsp optarg);
2313 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2314 b72f483a 2019-02-05 stsp break;
2315 98eaaa12 2019-08-03 stsp case 's':
2316 98eaaa12 2019-08-03 stsp diff_staged = 1;
2317 63035f9f 2019-10-06 stsp break;
2318 63035f9f 2019-10-06 stsp case 'w':
2319 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2320 98eaaa12 2019-08-03 stsp break;
2321 b00d56cd 2018-04-01 stsp default:
2322 2deda0b9 2019-03-07 stsp usage_diff();
2323 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2324 b00d56cd 2018-04-01 stsp }
2325 b00d56cd 2018-04-01 stsp }
2326 b00d56cd 2018-04-01 stsp
2327 b00d56cd 2018-04-01 stsp argc -= optind;
2328 b00d56cd 2018-04-01 stsp argv += optind;
2329 b00d56cd 2018-04-01 stsp
2330 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2331 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2332 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2333 b72f483a 2019-02-05 stsp goto done;
2334 b72f483a 2019-02-05 stsp }
2335 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2336 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2337 927df6b7 2019-02-10 stsp goto done;
2338 927df6b7 2019-02-10 stsp if (argc <= 1) {
2339 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2340 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2341 927df6b7 2019-02-10 stsp goto done;
2342 927df6b7 2019-02-10 stsp }
2343 b72f483a 2019-02-05 stsp if (repo_path)
2344 b72f483a 2019-02-05 stsp errx(1,
2345 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2346 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2347 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2348 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2349 927df6b7 2019-02-10 stsp goto done;
2350 927df6b7 2019-02-10 stsp }
2351 927df6b7 2019-02-10 stsp if (argc == 1) {
2352 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2353 cbd1af7a 2019-03-18 stsp argv[0]);
2354 927df6b7 2019-02-10 stsp if (error)
2355 927df6b7 2019-02-10 stsp goto done;
2356 927df6b7 2019-02-10 stsp } else {
2357 927df6b7 2019-02-10 stsp path = strdup("");
2358 927df6b7 2019-02-10 stsp if (path == NULL) {
2359 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2360 927df6b7 2019-02-10 stsp goto done;
2361 927df6b7 2019-02-10 stsp }
2362 927df6b7 2019-02-10 stsp }
2363 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2364 98eaaa12 2019-08-03 stsp if (diff_staged)
2365 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2366 98eaaa12 2019-08-03 stsp "objects in repository");
2367 15a94983 2018-12-23 stsp id_str1 = argv[0];
2368 15a94983 2018-12-23 stsp id_str2 = argv[1];
2369 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2370 30db809c 2019-06-05 stsp repo_path =
2371 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2372 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2373 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2374 30db809c 2019-06-05 stsp goto done;
2375 30db809c 2019-06-05 stsp }
2376 30db809c 2019-06-05 stsp }
2377 b00d56cd 2018-04-01 stsp } else
2378 b00d56cd 2018-04-01 stsp usage_diff();
2379 25eccc22 2019-01-04 stsp
2380 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2381 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2382 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2383 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2384 b72f483a 2019-02-05 stsp }
2385 b00d56cd 2018-04-01 stsp
2386 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2387 76089277 2018-04-01 stsp free(repo_path);
2388 b00d56cd 2018-04-01 stsp if (error != NULL)
2389 b00d56cd 2018-04-01 stsp goto done;
2390 b00d56cd 2018-04-01 stsp
2391 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2392 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2393 c02c541e 2019-03-29 stsp if (error)
2394 c02c541e 2019-03-29 stsp goto done;
2395 c02c541e 2019-03-29 stsp
2396 30db809c 2019-06-05 stsp if (argc <= 1) {
2397 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2398 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2399 b72f483a 2019-02-05 stsp char *id_str;
2400 72ea6654 2019-07-27 stsp
2401 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2402 72ea6654 2019-07-27 stsp
2403 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2404 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2405 b72f483a 2019-02-05 stsp if (error)
2406 b72f483a 2019-02-05 stsp goto done;
2407 b72f483a 2019-02-05 stsp arg.repo = repo;
2408 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2409 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2410 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2411 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2412 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2413 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2414 b72f483a 2019-02-05 stsp
2415 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2416 72ea6654 2019-07-27 stsp if (error)
2417 72ea6654 2019-07-27 stsp goto done;
2418 72ea6654 2019-07-27 stsp
2419 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2420 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2421 b72f483a 2019-02-05 stsp free(id_str);
2422 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2423 b72f483a 2019-02-05 stsp goto done;
2424 b72f483a 2019-02-05 stsp }
2425 b72f483a 2019-02-05 stsp
2426 01073a5d 2019-08-22 stsp error = match_object_id(&id1, &label1, id_str1, GOT_OBJ_TYPE_ANY, 1,
2427 01073a5d 2019-08-22 stsp repo);
2428 0adb7196 2019-08-11 stsp if (error)
2429 0adb7196 2019-08-11 stsp goto done;
2430 b00d56cd 2018-04-01 stsp
2431 01073a5d 2019-08-22 stsp error = match_object_id(&id2, &label2, id_str2, GOT_OBJ_TYPE_ANY, 1,
2432 01073a5d 2019-08-22 stsp repo);
2433 0adb7196 2019-08-11 stsp if (error)
2434 0adb7196 2019-08-11 stsp goto done;
2435 b00d56cd 2018-04-01 stsp
2436 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2437 15a94983 2018-12-23 stsp if (error)
2438 15a94983 2018-12-23 stsp goto done;
2439 15a94983 2018-12-23 stsp
2440 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2441 15a94983 2018-12-23 stsp if (error)
2442 15a94983 2018-12-23 stsp goto done;
2443 15a94983 2018-12-23 stsp
2444 15a94983 2018-12-23 stsp if (type1 != type2) {
2445 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2446 b00d56cd 2018-04-01 stsp goto done;
2447 b00d56cd 2018-04-01 stsp }
2448 b00d56cd 2018-04-01 stsp
2449 15a94983 2018-12-23 stsp switch (type1) {
2450 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2451 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2452 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2453 b00d56cd 2018-04-01 stsp break;
2454 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2455 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2456 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2457 b00d56cd 2018-04-01 stsp break;
2458 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2459 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2460 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2461 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2462 b00d56cd 2018-04-01 stsp break;
2463 b00d56cd 2018-04-01 stsp default:
2464 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2465 b00d56cd 2018-04-01 stsp }
2466 b00d56cd 2018-04-01 stsp
2467 b00d56cd 2018-04-01 stsp done:
2468 5e70831e 2019-05-28 stsp free(label1);
2469 5e70831e 2019-05-28 stsp free(label2);
2470 15a94983 2018-12-23 stsp free(id1);
2471 15a94983 2018-12-23 stsp free(id2);
2472 927df6b7 2019-02-10 stsp free(path);
2473 b72f483a 2019-02-05 stsp if (worktree)
2474 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2475 ad242220 2018-09-08 stsp if (repo) {
2476 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2477 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2478 ad242220 2018-09-08 stsp if (error == NULL)
2479 ad242220 2018-09-08 stsp error = repo_error;
2480 ad242220 2018-09-08 stsp }
2481 b00d56cd 2018-04-01 stsp return error;
2482 404c43c4 2018-06-21 stsp }
2483 404c43c4 2018-06-21 stsp
2484 404c43c4 2018-06-21 stsp __dead static void
2485 404c43c4 2018-06-21 stsp usage_blame(void)
2486 404c43c4 2018-06-21 stsp {
2487 9270e621 2019-02-05 stsp fprintf(stderr,
2488 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2489 404c43c4 2018-06-21 stsp getprogname());
2490 404c43c4 2018-06-21 stsp exit(1);
2491 b00d56cd 2018-04-01 stsp }
2492 b00d56cd 2018-04-01 stsp
2493 28315671 2019-08-14 stsp struct blame_line {
2494 28315671 2019-08-14 stsp int annotated;
2495 28315671 2019-08-14 stsp char *id_str;
2496 82f6abb8 2019-08-14 stsp char *committer;
2497 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2498 28315671 2019-08-14 stsp };
2499 28315671 2019-08-14 stsp
2500 28315671 2019-08-14 stsp struct blame_cb_args {
2501 28315671 2019-08-14 stsp struct blame_line *lines;
2502 28315671 2019-08-14 stsp int nlines;
2503 7ef28ff8 2019-08-14 stsp int nlines_prec;
2504 28315671 2019-08-14 stsp int lineno_cur;
2505 28315671 2019-08-14 stsp off_t *line_offsets;
2506 28315671 2019-08-14 stsp FILE *f;
2507 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2508 28315671 2019-08-14 stsp };
2509 28315671 2019-08-14 stsp
2510 404c43c4 2018-06-21 stsp static const struct got_error *
2511 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2512 28315671 2019-08-14 stsp {
2513 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2514 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2515 28315671 2019-08-14 stsp struct blame_line *bline;
2516 82f6abb8 2019-08-14 stsp char *line = NULL;
2517 28315671 2019-08-14 stsp size_t linesize = 0;
2518 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2519 28315671 2019-08-14 stsp off_t offset;
2520 bcb49d15 2019-08-14 stsp struct tm tm;
2521 bcb49d15 2019-08-14 stsp time_t committer_time;
2522 28315671 2019-08-14 stsp
2523 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2524 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2525 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2526 28315671 2019-08-14 stsp
2527 28315671 2019-08-14 stsp if (sigint_received)
2528 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2529 28315671 2019-08-14 stsp
2530 2839f8b9 2019-08-18 stsp if (lineno == -1)
2531 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2532 2839f8b9 2019-08-18 stsp
2533 28315671 2019-08-14 stsp /* Annotate this line. */
2534 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2535 28315671 2019-08-14 stsp if (bline->annotated)
2536 28315671 2019-08-14 stsp return NULL;
2537 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2538 28315671 2019-08-14 stsp if (err)
2539 28315671 2019-08-14 stsp return err;
2540 82f6abb8 2019-08-14 stsp
2541 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2542 82f6abb8 2019-08-14 stsp if (err)
2543 82f6abb8 2019-08-14 stsp goto done;
2544 82f6abb8 2019-08-14 stsp
2545 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2546 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2547 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2548 bcb49d15 2019-08-14 stsp goto done;
2549 bcb49d15 2019-08-14 stsp }
2550 bcb49d15 2019-08-14 stsp
2551 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2552 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2553 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2554 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2555 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2556 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2557 82f6abb8 2019-08-14 stsp goto done;
2558 82f6abb8 2019-08-14 stsp }
2559 28315671 2019-08-14 stsp bline->annotated = 1;
2560 28315671 2019-08-14 stsp
2561 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2562 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2563 28315671 2019-08-14 stsp if (!bline->annotated)
2564 82f6abb8 2019-08-14 stsp goto done;
2565 28315671 2019-08-14 stsp
2566 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2567 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2568 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2569 82f6abb8 2019-08-14 stsp goto done;
2570 82f6abb8 2019-08-14 stsp }
2571 28315671 2019-08-14 stsp
2572 28315671 2019-08-14 stsp while (bline->annotated) {
2573 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2574 82f6abb8 2019-08-14 stsp size_t len;
2575 82f6abb8 2019-08-14 stsp
2576 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2577 28315671 2019-08-14 stsp if (ferror(a->f))
2578 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2579 28315671 2019-08-14 stsp break;
2580 28315671 2019-08-14 stsp }
2581 82f6abb8 2019-08-14 stsp
2582 82f6abb8 2019-08-14 stsp committer = bline->committer;
2583 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2584 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2585 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2586 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2587 82f6abb8 2019-08-14 stsp if (at)
2588 82f6abb8 2019-08-14 stsp *at = '\0';
2589 82f6abb8 2019-08-14 stsp len = strlen(committer);
2590 82f6abb8 2019-08-14 stsp if (len >= 9)
2591 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2592 28315671 2019-08-14 stsp
2593 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2594 28315671 2019-08-14 stsp if (nl)
2595 28315671 2019-08-14 stsp *nl = '\0';
2596 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2597 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2598 28315671 2019-08-14 stsp
2599 28315671 2019-08-14 stsp a->lineno_cur++;
2600 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2601 28315671 2019-08-14 stsp }
2602 82f6abb8 2019-08-14 stsp done:
2603 82f6abb8 2019-08-14 stsp if (commit)
2604 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2605 28315671 2019-08-14 stsp free(line);
2606 28315671 2019-08-14 stsp return err;
2607 28315671 2019-08-14 stsp }
2608 28315671 2019-08-14 stsp
2609 28315671 2019-08-14 stsp static const struct got_error *
2610 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2611 404c43c4 2018-06-21 stsp {
2612 404c43c4 2018-06-21 stsp const struct got_error *error;
2613 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2614 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2615 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2616 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2617 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2618 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2619 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2620 28315671 2019-08-14 stsp struct blame_cb_args bca;
2621 28315671 2019-08-14 stsp int ch, obj_type, i;
2622 b02560ec 2019-08-19 stsp size_t filesize;
2623 90f3c347 2019-08-19 stsp
2624 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2625 404c43c4 2018-06-21 stsp
2626 404c43c4 2018-06-21 stsp #ifndef PROFILE
2627 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2628 36e2fb66 2019-01-04 stsp NULL) == -1)
2629 404c43c4 2018-06-21 stsp err(1, "pledge");
2630 404c43c4 2018-06-21 stsp #endif
2631 404c43c4 2018-06-21 stsp
2632 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2633 404c43c4 2018-06-21 stsp switch (ch) {
2634 404c43c4 2018-06-21 stsp case 'c':
2635 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2636 404c43c4 2018-06-21 stsp break;
2637 66bea077 2018-08-02 stsp case 'r':
2638 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2639 66bea077 2018-08-02 stsp if (repo_path == NULL)
2640 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2641 9ba1d308 2019-10-21 stsp optarg);
2642 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2643 66bea077 2018-08-02 stsp break;
2644 404c43c4 2018-06-21 stsp default:
2645 2deda0b9 2019-03-07 stsp usage_blame();
2646 404c43c4 2018-06-21 stsp /* NOTREACHED */
2647 404c43c4 2018-06-21 stsp }
2648 404c43c4 2018-06-21 stsp }
2649 404c43c4 2018-06-21 stsp
2650 404c43c4 2018-06-21 stsp argc -= optind;
2651 404c43c4 2018-06-21 stsp argv += optind;
2652 404c43c4 2018-06-21 stsp
2653 a39318fd 2018-08-02 stsp if (argc == 1)
2654 404c43c4 2018-06-21 stsp path = argv[0];
2655 a39318fd 2018-08-02 stsp else
2656 404c43c4 2018-06-21 stsp usage_blame();
2657 404c43c4 2018-06-21 stsp
2658 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2659 66bea077 2018-08-02 stsp if (cwd == NULL) {
2660 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2661 66bea077 2018-08-02 stsp goto done;
2662 66bea077 2018-08-02 stsp }
2663 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2664 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2665 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2666 66bea077 2018-08-02 stsp goto done;
2667 0c06baac 2019-02-05 stsp else
2668 0c06baac 2019-02-05 stsp error = NULL;
2669 0c06baac 2019-02-05 stsp if (worktree) {
2670 0c06baac 2019-02-05 stsp repo_path =
2671 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2672 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2673 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2674 0c06baac 2019-02-05 stsp if (error)
2675 0c06baac 2019-02-05 stsp goto done;
2676 0c06baac 2019-02-05 stsp } else {
2677 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2678 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2679 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2680 0c06baac 2019-02-05 stsp goto done;
2681 0c06baac 2019-02-05 stsp }
2682 66bea077 2018-08-02 stsp }
2683 66bea077 2018-08-02 stsp }
2684 36e2fb66 2019-01-04 stsp
2685 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2686 c02c541e 2019-03-29 stsp if (error != NULL)
2687 36e2fb66 2019-01-04 stsp goto done;
2688 66bea077 2018-08-02 stsp
2689 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2690 c02c541e 2019-03-29 stsp if (error)
2691 404c43c4 2018-06-21 stsp goto done;
2692 404c43c4 2018-06-21 stsp
2693 0c06baac 2019-02-05 stsp if (worktree) {
2694 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2695 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2696 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2697 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2698 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2699 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2700 6efaaa2d 2019-02-05 stsp path) == -1) {
2701 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2702 0c06baac 2019-02-05 stsp goto done;
2703 0c06baac 2019-02-05 stsp }
2704 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2705 0c06baac 2019-02-05 stsp free(p);
2706 0c06baac 2019-02-05 stsp } else {
2707 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2708 0c06baac 2019-02-05 stsp }
2709 0c06baac 2019-02-05 stsp if (error)
2710 66bea077 2018-08-02 stsp goto done;
2711 66bea077 2018-08-02 stsp
2712 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2713 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2714 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2715 404c43c4 2018-06-21 stsp if (error != NULL)
2716 66bea077 2018-08-02 stsp goto done;
2717 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2718 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2719 404c43c4 2018-06-21 stsp if (error != NULL)
2720 66bea077 2018-08-02 stsp goto done;
2721 404c43c4 2018-06-21 stsp } else {
2722 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
2723 30837e32 2019-07-25 stsp if (error)
2724 66bea077 2018-08-02 stsp goto done;
2725 404c43c4 2018-06-21 stsp }
2726 404c43c4 2018-06-21 stsp
2727 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2728 28315671 2019-08-14 stsp if (error)
2729 28315671 2019-08-14 stsp goto done;
2730 28315671 2019-08-14 stsp if (obj_id == NULL) {
2731 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2732 28315671 2019-08-14 stsp goto done;
2733 28315671 2019-08-14 stsp }
2734 28315671 2019-08-14 stsp
2735 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2736 28315671 2019-08-14 stsp if (error)
2737 28315671 2019-08-14 stsp goto done;
2738 28315671 2019-08-14 stsp
2739 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2740 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2741 28315671 2019-08-14 stsp goto done;
2742 28315671 2019-08-14 stsp }
2743 28315671 2019-08-14 stsp
2744 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2745 28315671 2019-08-14 stsp if (error)
2746 28315671 2019-08-14 stsp goto done;
2747 28315671 2019-08-14 stsp bca.f = got_opentemp();
2748 28315671 2019-08-14 stsp if (bca.f == NULL) {
2749 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2750 28315671 2019-08-14 stsp goto done;
2751 28315671 2019-08-14 stsp }
2752 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2753 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2754 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2755 28315671 2019-08-14 stsp goto done;
2756 28315671 2019-08-14 stsp
2757 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2758 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2759 b02560ec 2019-08-19 stsp bca.nlines--;
2760 b02560ec 2019-08-19 stsp
2761 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2762 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2763 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2764 28315671 2019-08-14 stsp goto done;
2765 28315671 2019-08-14 stsp }
2766 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2767 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2768 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2769 7ef28ff8 2019-08-14 stsp while (i > 0) {
2770 7ef28ff8 2019-08-14 stsp i /= 10;
2771 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2772 7ef28ff8 2019-08-14 stsp }
2773 82f6abb8 2019-08-14 stsp bca.repo = repo;
2774 7ef28ff8 2019-08-14 stsp
2775 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2776 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2777 28315671 2019-08-14 stsp if (error)
2778 28315671 2019-08-14 stsp goto done;
2779 404c43c4 2018-06-21 stsp done:
2780 66bea077 2018-08-02 stsp free(in_repo_path);
2781 66bea077 2018-08-02 stsp free(repo_path);
2782 66bea077 2018-08-02 stsp free(cwd);
2783 404c43c4 2018-06-21 stsp free(commit_id);
2784 28315671 2019-08-14 stsp free(obj_id);
2785 28315671 2019-08-14 stsp if (blob)
2786 28315671 2019-08-14 stsp got_object_blob_close(blob);
2787 0c06baac 2019-02-05 stsp if (worktree)
2788 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2789 ad242220 2018-09-08 stsp if (repo) {
2790 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2791 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2792 ad242220 2018-09-08 stsp if (error == NULL)
2793 ad242220 2018-09-08 stsp error = repo_error;
2794 ad242220 2018-09-08 stsp }
2795 3affba96 2019-09-22 stsp if (bca.lines) {
2796 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2797 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2798 3affba96 2019-09-22 stsp free(bline->id_str);
2799 3affba96 2019-09-22 stsp free(bline->committer);
2800 3affba96 2019-09-22 stsp }
2801 3affba96 2019-09-22 stsp free(bca.lines);
2802 28315671 2019-08-14 stsp }
2803 28315671 2019-08-14 stsp free(bca.line_offsets);
2804 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2805 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2806 404c43c4 2018-06-21 stsp return error;
2807 5de5890b 2018-10-18 stsp }
2808 5de5890b 2018-10-18 stsp
2809 5de5890b 2018-10-18 stsp __dead static void
2810 5de5890b 2018-10-18 stsp usage_tree(void)
2811 5de5890b 2018-10-18 stsp {
2812 c1669e2e 2019-01-09 stsp fprintf(stderr,
2813 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2814 5de5890b 2018-10-18 stsp getprogname());
2815 5de5890b 2018-10-18 stsp exit(1);
2816 5de5890b 2018-10-18 stsp }
2817 5de5890b 2018-10-18 stsp
2818 c1669e2e 2019-01-09 stsp static void
2819 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2820 c1669e2e 2019-01-09 stsp const char *root_path)
2821 c1669e2e 2019-01-09 stsp {
2822 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2823 848d6979 2019-08-12 stsp const char *modestr = "";
2824 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2825 5de5890b 2018-10-18 stsp
2826 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2827 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2828 c1669e2e 2019-01-09 stsp path++;
2829 c1669e2e 2019-01-09 stsp
2830 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2831 63c5ca5d 2019-08-24 stsp modestr = "$";
2832 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2833 848d6979 2019-08-12 stsp modestr = "@";
2834 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2835 848d6979 2019-08-12 stsp modestr = "/";
2836 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2837 848d6979 2019-08-12 stsp modestr = "*";
2838 848d6979 2019-08-12 stsp
2839 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2840 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2841 c1669e2e 2019-01-09 stsp }
2842 c1669e2e 2019-01-09 stsp
2843 5de5890b 2018-10-18 stsp static const struct got_error *
2844 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2845 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2846 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2847 5de5890b 2018-10-18 stsp {
2848 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2849 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2850 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2851 56e0773d 2019-11-28 stsp int nentries, i;
2852 5de5890b 2018-10-18 stsp
2853 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2854 5de5890b 2018-10-18 stsp if (err)
2855 5de5890b 2018-10-18 stsp goto done;
2856 5de5890b 2018-10-18 stsp
2857 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2858 5de5890b 2018-10-18 stsp if (err)
2859 5de5890b 2018-10-18 stsp goto done;
2860 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2861 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2862 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2863 5de5890b 2018-10-18 stsp char *id = NULL;
2864 84453469 2018-11-11 stsp
2865 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2866 84453469 2018-11-11 stsp break;
2867 84453469 2018-11-11 stsp
2868 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2869 5de5890b 2018-10-18 stsp if (show_ids) {
2870 5de5890b 2018-10-18 stsp char *id_str;
2871 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2872 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2873 5de5890b 2018-10-18 stsp if (err)
2874 5de5890b 2018-10-18 stsp goto done;
2875 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2876 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2877 5de5890b 2018-10-18 stsp free(id_str);
2878 5de5890b 2018-10-18 stsp goto done;
2879 5de5890b 2018-10-18 stsp }
2880 5de5890b 2018-10-18 stsp free(id_str);
2881 5de5890b 2018-10-18 stsp }
2882 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2883 5de5890b 2018-10-18 stsp free(id);
2884 c1669e2e 2019-01-09 stsp
2885 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2886 c1669e2e 2019-01-09 stsp char *child_path;
2887 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2888 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2889 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2890 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2891 c1669e2e 2019-01-09 stsp goto done;
2892 c1669e2e 2019-01-09 stsp }
2893 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2894 c1669e2e 2019-01-09 stsp root_path, repo);
2895 c1669e2e 2019-01-09 stsp free(child_path);
2896 c1669e2e 2019-01-09 stsp if (err)
2897 c1669e2e 2019-01-09 stsp goto done;
2898 c1669e2e 2019-01-09 stsp }
2899 5de5890b 2018-10-18 stsp }
2900 5de5890b 2018-10-18 stsp done:
2901 5de5890b 2018-10-18 stsp if (tree)
2902 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2903 5de5890b 2018-10-18 stsp free(tree_id);
2904 5de5890b 2018-10-18 stsp return err;
2905 404c43c4 2018-06-21 stsp }
2906 404c43c4 2018-06-21 stsp
2907 5de5890b 2018-10-18 stsp static const struct got_error *
2908 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2909 5de5890b 2018-10-18 stsp {
2910 5de5890b 2018-10-18 stsp const struct got_error *error;
2911 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2912 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2913 7a2c19d6 2019-02-05 stsp const char *path;
2914 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2915 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2916 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2917 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2918 5de5890b 2018-10-18 stsp int ch;
2919 5de5890b 2018-10-18 stsp
2920 5de5890b 2018-10-18 stsp #ifndef PROFILE
2921 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2922 0f8d269b 2019-01-04 stsp NULL) == -1)
2923 5de5890b 2018-10-18 stsp err(1, "pledge");
2924 5de5890b 2018-10-18 stsp #endif
2925 5de5890b 2018-10-18 stsp
2926 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2927 5de5890b 2018-10-18 stsp switch (ch) {
2928 5de5890b 2018-10-18 stsp case 'c':
2929 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2930 5de5890b 2018-10-18 stsp break;
2931 5de5890b 2018-10-18 stsp case 'r':
2932 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2933 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2934 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2935 9ba1d308 2019-10-21 stsp optarg);
2936 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2937 5de5890b 2018-10-18 stsp break;
2938 5de5890b 2018-10-18 stsp case 'i':
2939 5de5890b 2018-10-18 stsp show_ids = 1;
2940 5de5890b 2018-10-18 stsp break;
2941 c1669e2e 2019-01-09 stsp case 'R':
2942 c1669e2e 2019-01-09 stsp recurse = 1;
2943 c1669e2e 2019-01-09 stsp break;
2944 5de5890b 2018-10-18 stsp default:
2945 2deda0b9 2019-03-07 stsp usage_tree();
2946 5de5890b 2018-10-18 stsp /* NOTREACHED */
2947 5de5890b 2018-10-18 stsp }
2948 5de5890b 2018-10-18 stsp }
2949 5de5890b 2018-10-18 stsp
2950 5de5890b 2018-10-18 stsp argc -= optind;
2951 5de5890b 2018-10-18 stsp argv += optind;
2952 5de5890b 2018-10-18 stsp
2953 5de5890b 2018-10-18 stsp if (argc == 1)
2954 5de5890b 2018-10-18 stsp path = argv[0];
2955 5de5890b 2018-10-18 stsp else if (argc > 1)
2956 5de5890b 2018-10-18 stsp usage_tree();
2957 5de5890b 2018-10-18 stsp else
2958 7a2c19d6 2019-02-05 stsp path = NULL;
2959 7a2c19d6 2019-02-05 stsp
2960 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2961 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2962 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2963 9bf7a39b 2019-02-05 stsp goto done;
2964 9bf7a39b 2019-02-05 stsp }
2965 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2966 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2967 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2968 7a2c19d6 2019-02-05 stsp goto done;
2969 7a2c19d6 2019-02-05 stsp else
2970 7a2c19d6 2019-02-05 stsp error = NULL;
2971 7a2c19d6 2019-02-05 stsp if (worktree) {
2972 7a2c19d6 2019-02-05 stsp repo_path =
2973 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2974 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2975 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2976 7a2c19d6 2019-02-05 stsp if (error)
2977 7a2c19d6 2019-02-05 stsp goto done;
2978 7a2c19d6 2019-02-05 stsp } else {
2979 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2980 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2981 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2982 7a2c19d6 2019-02-05 stsp goto done;
2983 7a2c19d6 2019-02-05 stsp }
2984 5de5890b 2018-10-18 stsp }
2985 5de5890b 2018-10-18 stsp }
2986 5de5890b 2018-10-18 stsp
2987 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2988 5de5890b 2018-10-18 stsp if (error != NULL)
2989 c02c541e 2019-03-29 stsp goto done;
2990 c02c541e 2019-03-29 stsp
2991 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2992 c02c541e 2019-03-29 stsp if (error)
2993 5de5890b 2018-10-18 stsp goto done;
2994 5de5890b 2018-10-18 stsp
2995 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2996 9bf7a39b 2019-02-05 stsp if (worktree) {
2997 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2998 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2999 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
3000 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
3001 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
3002 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
3003 9bf7a39b 2019-02-05 stsp goto done;
3004 9bf7a39b 2019-02-05 stsp }
3005 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
3006 9bf7a39b 2019-02-05 stsp free(p);
3007 9bf7a39b 2019-02-05 stsp if (error)
3008 9bf7a39b 2019-02-05 stsp goto done;
3009 9bf7a39b 2019-02-05 stsp } else
3010 9bf7a39b 2019-02-05 stsp path = "/";
3011 9bf7a39b 2019-02-05 stsp }
3012 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
3013 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
3014 9bf7a39b 2019-02-05 stsp if (error != NULL)
3015 9bf7a39b 2019-02-05 stsp goto done;
3016 9bf7a39b 2019-02-05 stsp }
3017 5de5890b 2018-10-18 stsp
3018 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
3019 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
3020 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
3021 5de5890b 2018-10-18 stsp if (error != NULL)
3022 5de5890b 2018-10-18 stsp goto done;
3023 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
3024 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
3025 5de5890b 2018-10-18 stsp if (error != NULL)
3026 5de5890b 2018-10-18 stsp goto done;
3027 5de5890b 2018-10-18 stsp } else {
3028 04f57cb3 2019-07-25 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
3029 30837e32 2019-07-25 stsp if (error)
3030 5de5890b 2018-10-18 stsp goto done;
3031 5de5890b 2018-10-18 stsp }
3032 5de5890b 2018-10-18 stsp
3033 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
3034 c1669e2e 2019-01-09 stsp in_repo_path, repo);
3035 5de5890b 2018-10-18 stsp done:
3036 5de5890b 2018-10-18 stsp free(in_repo_path);
3037 5de5890b 2018-10-18 stsp free(repo_path);
3038 5de5890b 2018-10-18 stsp free(cwd);
3039 5de5890b 2018-10-18 stsp free(commit_id);
3040 7a2c19d6 2019-02-05 stsp if (worktree)
3041 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
3042 5de5890b 2018-10-18 stsp if (repo) {
3043 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
3044 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
3045 5de5890b 2018-10-18 stsp if (error == NULL)
3046 5de5890b 2018-10-18 stsp error = repo_error;
3047 5de5890b 2018-10-18 stsp }
3048 5de5890b 2018-10-18 stsp return error;
3049 5de5890b 2018-10-18 stsp }
3050 5de5890b 2018-10-18 stsp
3051 6bad629b 2019-02-04 stsp __dead static void
3052 6bad629b 2019-02-04 stsp usage_status(void)
3053 6bad629b 2019-02-04 stsp {
3054 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
3055 6bad629b 2019-02-04 stsp exit(1);
3056 6bad629b 2019-02-04 stsp }
3057 5c860e29 2018-03-12 stsp
3058 b72f483a 2019-02-05 stsp static const struct got_error *
3059 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3060 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3061 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3062 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3063 6bad629b 2019-02-04 stsp {
3064 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3065 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3066 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3067 b72f483a 2019-02-05 stsp return NULL;
3068 6bad629b 2019-02-04 stsp }
3069 5c860e29 2018-03-12 stsp
3070 6bad629b 2019-02-04 stsp static const struct got_error *
3071 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3072 6bad629b 2019-02-04 stsp {
3073 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3074 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3075 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3076 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3077 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3078 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3079 a5edda0a 2019-07-27 stsp int ch;
3080 5c860e29 2018-03-12 stsp
3081 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3082 72ea6654 2019-07-27 stsp
3083 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3084 6bad629b 2019-02-04 stsp switch (ch) {
3085 5c860e29 2018-03-12 stsp default:
3086 2deda0b9 2019-03-07 stsp usage_status();
3087 6bad629b 2019-02-04 stsp /* NOTREACHED */
3088 5c860e29 2018-03-12 stsp }
3089 5c860e29 2018-03-12 stsp }
3090 5c860e29 2018-03-12 stsp
3091 6bad629b 2019-02-04 stsp argc -= optind;
3092 6bad629b 2019-02-04 stsp argv += optind;
3093 5c860e29 2018-03-12 stsp
3094 6bad629b 2019-02-04 stsp #ifndef PROFILE
3095 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3096 6bad629b 2019-02-04 stsp NULL) == -1)
3097 6bad629b 2019-02-04 stsp err(1, "pledge");
3098 f42b1b34 2018-03-12 stsp #endif
3099 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3100 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3101 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3102 927df6b7 2019-02-10 stsp goto done;
3103 927df6b7 2019-02-10 stsp }
3104 927df6b7 2019-02-10 stsp
3105 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3106 927df6b7 2019-02-10 stsp if (error != NULL)
3107 a5edda0a 2019-07-27 stsp goto done;
3108 6bad629b 2019-02-04 stsp
3109 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3110 c9956ddf 2019-09-08 stsp NULL);
3111 6bad629b 2019-02-04 stsp if (error != NULL)
3112 6bad629b 2019-02-04 stsp goto done;
3113 6bad629b 2019-02-04 stsp
3114 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3115 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3116 087fb88c 2019-08-04 stsp if (error)
3117 087fb88c 2019-08-04 stsp goto done;
3118 087fb88c 2019-08-04 stsp
3119 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3120 6bad629b 2019-02-04 stsp if (error)
3121 6bad629b 2019-02-04 stsp goto done;
3122 6bad629b 2019-02-04 stsp
3123 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3124 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3125 6bad629b 2019-02-04 stsp done:
3126 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3127 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3128 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3129 927df6b7 2019-02-10 stsp free(cwd);
3130 d0eebce4 2019-03-11 stsp return error;
3131 d0eebce4 2019-03-11 stsp }
3132 d0eebce4 2019-03-11 stsp
3133 d0eebce4 2019-03-11 stsp __dead static void
3134 d0eebce4 2019-03-11 stsp usage_ref(void)
3135 d0eebce4 2019-03-11 stsp {
3136 d0eebce4 2019-03-11 stsp fprintf(stderr,
3137 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3138 d0eebce4 2019-03-11 stsp getprogname());
3139 d0eebce4 2019-03-11 stsp exit(1);
3140 d0eebce4 2019-03-11 stsp }
3141 d0eebce4 2019-03-11 stsp
3142 d0eebce4 2019-03-11 stsp static const struct got_error *
3143 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3144 d0eebce4 2019-03-11 stsp {
3145 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3146 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3147 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3148 d0eebce4 2019-03-11 stsp
3149 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3150 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3151 d0eebce4 2019-03-11 stsp if (err)
3152 d0eebce4 2019-03-11 stsp return err;
3153 d0eebce4 2019-03-11 stsp
3154 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3155 d0eebce4 2019-03-11 stsp char *refstr;
3156 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3157 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3158 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3159 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3160 d0eebce4 2019-03-11 stsp free(refstr);
3161 d0eebce4 2019-03-11 stsp }
3162 d0eebce4 2019-03-11 stsp
3163 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3164 d0eebce4 2019-03-11 stsp return NULL;
3165 d0eebce4 2019-03-11 stsp }
3166 d0eebce4 2019-03-11 stsp
3167 d0eebce4 2019-03-11 stsp static const struct got_error *
3168 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3169 d0eebce4 2019-03-11 stsp {
3170 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3171 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3172 d0eebce4 2019-03-11 stsp
3173 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3174 d0eebce4 2019-03-11 stsp if (err)
3175 d0eebce4 2019-03-11 stsp return err;
3176 d0eebce4 2019-03-11 stsp
3177 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3178 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3179 d0eebce4 2019-03-11 stsp return err;
3180 d0eebce4 2019-03-11 stsp }
3181 d0eebce4 2019-03-11 stsp
3182 d0eebce4 2019-03-11 stsp static const struct got_error *
3183 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3184 d0eebce4 2019-03-11 stsp {
3185 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3186 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3187 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3188 d1644381 2019-07-14 stsp
3189 d1644381 2019-07-14 stsp /*
3190 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3191 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3192 d1644381 2019-07-14 stsp * an unintended typo.
3193 d1644381 2019-07-14 stsp */
3194 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3195 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3196 d0eebce4 2019-03-11 stsp
3197 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3198 dd88155e 2019-06-29 stsp repo);
3199 d83d9d5c 2019-05-13 stsp if (err) {
3200 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3201 d0eebce4 2019-03-11 stsp
3202 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3203 d83d9d5c 2019-05-13 stsp return err;
3204 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3205 d83d9d5c 2019-05-13 stsp if (err)
3206 d83d9d5c 2019-05-13 stsp return err;
3207 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3208 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3209 d83d9d5c 2019-05-13 stsp if (err)
3210 d83d9d5c 2019-05-13 stsp return err;
3211 d83d9d5c 2019-05-13 stsp }
3212 d83d9d5c 2019-05-13 stsp
3213 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3214 d0eebce4 2019-03-11 stsp if (err)
3215 d0eebce4 2019-03-11 stsp goto done;
3216 d0eebce4 2019-03-11 stsp
3217 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3218 d0eebce4 2019-03-11 stsp done:
3219 d0eebce4 2019-03-11 stsp if (ref)
3220 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3221 d0eebce4 2019-03-11 stsp free(id);
3222 d1c1ae5f 2019-08-12 stsp return err;
3223 d1c1ae5f 2019-08-12 stsp }
3224 d1c1ae5f 2019-08-12 stsp
3225 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3226 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3227 d1c1ae5f 2019-08-12 stsp {
3228 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3229 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3230 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3231 d1c1ae5f 2019-08-12 stsp
3232 d1c1ae5f 2019-08-12 stsp /*
3233 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3234 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3235 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3236 d1c1ae5f 2019-08-12 stsp */
3237 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3238 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3239 d1c1ae5f 2019-08-12 stsp
3240 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3241 d1c1ae5f 2019-08-12 stsp if (err)
3242 d1c1ae5f 2019-08-12 stsp return err;
3243 d1c1ae5f 2019-08-12 stsp
3244 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3245 d1c1ae5f 2019-08-12 stsp if (err)
3246 d1c1ae5f 2019-08-12 stsp goto done;
3247 d1c1ae5f 2019-08-12 stsp
3248 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3249 d1c1ae5f 2019-08-12 stsp done:
3250 d1c1ae5f 2019-08-12 stsp if (target_ref)
3251 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3252 d1c1ae5f 2019-08-12 stsp if (ref)
3253 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3254 d0eebce4 2019-03-11 stsp return err;
3255 d0eebce4 2019-03-11 stsp }
3256 d0eebce4 2019-03-11 stsp
3257 d0eebce4 2019-03-11 stsp static const struct got_error *
3258 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3259 d0eebce4 2019-03-11 stsp {
3260 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3261 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3262 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3263 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3264 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3265 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3266 d0eebce4 2019-03-11 stsp
3267 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3268 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3269 d0eebce4 2019-03-11 stsp switch (ch) {
3270 d0eebce4 2019-03-11 stsp case 'd':
3271 d0eebce4 2019-03-11 stsp delref = optarg;
3272 d0eebce4 2019-03-11 stsp break;
3273 d0eebce4 2019-03-11 stsp case 'r':
3274 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3275 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3276 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3277 9ba1d308 2019-10-21 stsp optarg);
3278 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3279 d0eebce4 2019-03-11 stsp break;
3280 d0eebce4 2019-03-11 stsp case 'l':
3281 d0eebce4 2019-03-11 stsp do_list = 1;
3282 d1c1ae5f 2019-08-12 stsp break;
3283 d1c1ae5f 2019-08-12 stsp case 's':
3284 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3285 d0eebce4 2019-03-11 stsp break;
3286 d0eebce4 2019-03-11 stsp default:
3287 d0eebce4 2019-03-11 stsp usage_ref();
3288 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3289 d0eebce4 2019-03-11 stsp }
3290 d0eebce4 2019-03-11 stsp }
3291 d0eebce4 2019-03-11 stsp
3292 d0eebce4 2019-03-11 stsp if (do_list && delref)
3293 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3294 d0eebce4 2019-03-11 stsp
3295 d0eebce4 2019-03-11 stsp argc -= optind;
3296 d0eebce4 2019-03-11 stsp argv += optind;
3297 d0eebce4 2019-03-11 stsp
3298 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3299 d1c1ae5f 2019-08-12 stsp if (create_symref)
3300 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3301 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3302 d0eebce4 2019-03-11 stsp if (argc > 0)
3303 d0eebce4 2019-03-11 stsp usage_ref();
3304 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3305 d0eebce4 2019-03-11 stsp usage_ref();
3306 d0eebce4 2019-03-11 stsp
3307 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3308 e0b57350 2019-03-12 stsp if (do_list) {
3309 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3310 e0b57350 2019-03-12 stsp NULL) == -1)
3311 e0b57350 2019-03-12 stsp err(1, "pledge");
3312 e0b57350 2019-03-12 stsp } else {
3313 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3314 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3315 e0b57350 2019-03-12 stsp err(1, "pledge");
3316 e0b57350 2019-03-12 stsp }
3317 d0eebce4 2019-03-11 stsp #endif
3318 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3319 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3320 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3321 d0eebce4 2019-03-11 stsp goto done;
3322 d0eebce4 2019-03-11 stsp }
3323 d0eebce4 2019-03-11 stsp
3324 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3325 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3326 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3327 d0eebce4 2019-03-11 stsp goto done;
3328 d0eebce4 2019-03-11 stsp else
3329 d0eebce4 2019-03-11 stsp error = NULL;
3330 d0eebce4 2019-03-11 stsp if (worktree) {
3331 d0eebce4 2019-03-11 stsp repo_path =
3332 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3333 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3334 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3335 d0eebce4 2019-03-11 stsp if (error)
3336 d0eebce4 2019-03-11 stsp goto done;
3337 d0eebce4 2019-03-11 stsp } else {
3338 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3339 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3340 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3341 d0eebce4 2019-03-11 stsp goto done;
3342 d0eebce4 2019-03-11 stsp }
3343 d0eebce4 2019-03-11 stsp }
3344 d0eebce4 2019-03-11 stsp }
3345 d0eebce4 2019-03-11 stsp
3346 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3347 d0eebce4 2019-03-11 stsp if (error != NULL)
3348 d0eebce4 2019-03-11 stsp goto done;
3349 d0eebce4 2019-03-11 stsp
3350 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3351 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3352 c02c541e 2019-03-29 stsp if (error)
3353 c02c541e 2019-03-29 stsp goto done;
3354 c02c541e 2019-03-29 stsp
3355 d0eebce4 2019-03-11 stsp if (do_list)
3356 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3357 d0eebce4 2019-03-11 stsp else if (delref)
3358 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3359 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3360 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3361 d0eebce4 2019-03-11 stsp else
3362 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3363 4e759de4 2019-06-26 stsp done:
3364 4e759de4 2019-06-26 stsp if (repo)
3365 4e759de4 2019-06-26 stsp got_repo_close(repo);
3366 4e759de4 2019-06-26 stsp if (worktree)
3367 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3368 4e759de4 2019-06-26 stsp free(cwd);
3369 4e759de4 2019-06-26 stsp free(repo_path);
3370 4e759de4 2019-06-26 stsp return error;
3371 4e759de4 2019-06-26 stsp }
3372 4e759de4 2019-06-26 stsp
3373 4e759de4 2019-06-26 stsp __dead static void
3374 4e759de4 2019-06-26 stsp usage_branch(void)
3375 4e759de4 2019-06-26 stsp {
3376 4e759de4 2019-06-26 stsp fprintf(stderr,
3377 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3378 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3379 4e759de4 2019-06-26 stsp exit(1);
3380 4e759de4 2019-06-26 stsp }
3381 4e759de4 2019-06-26 stsp
3382 4e759de4 2019-06-26 stsp static const struct got_error *
3383 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3384 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3385 4e99b47e 2019-10-04 stsp {
3386 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3387 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3388 4e99b47e 2019-10-04 stsp char *refstr;
3389 4e99b47e 2019-10-04 stsp
3390 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3391 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3392 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3393 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3394 4e99b47e 2019-10-04 stsp
3395 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3396 4e99b47e 2019-10-04 stsp if (err)
3397 4e99b47e 2019-10-04 stsp return err;
3398 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3399 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3400 4e99b47e 2019-10-04 stsp marker = "* ";
3401 4e99b47e 2019-10-04 stsp else
3402 4e99b47e 2019-10-04 stsp marker = "~ ";
3403 4e99b47e 2019-10-04 stsp free(id);
3404 4e99b47e 2019-10-04 stsp }
3405 4e99b47e 2019-10-04 stsp
3406 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3407 4e99b47e 2019-10-04 stsp refname += 11;
3408 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3409 4e99b47e 2019-10-04 stsp refname += 18;
3410 4e99b47e 2019-10-04 stsp
3411 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3412 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3413 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3414 4e99b47e 2019-10-04 stsp
3415 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3416 4e99b47e 2019-10-04 stsp free(refstr);
3417 4e99b47e 2019-10-04 stsp return NULL;
3418 4e99b47e 2019-10-04 stsp }
3419 4e99b47e 2019-10-04 stsp
3420 4e99b47e 2019-10-04 stsp static const struct got_error *
3421 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3422 ad89fa31 2019-10-04 stsp {
3423 ad89fa31 2019-10-04 stsp const char *refname;
3424 ad89fa31 2019-10-04 stsp
3425 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3426 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3427 ad89fa31 2019-10-04 stsp
3428 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3429 ad89fa31 2019-10-04 stsp
3430 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3431 ad89fa31 2019-10-04 stsp refname += 11;
3432 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3433 ad89fa31 2019-10-04 stsp refname += 18;
3434 ad89fa31 2019-10-04 stsp
3435 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3436 ad89fa31 2019-10-04 stsp
3437 ad89fa31 2019-10-04 stsp return NULL;
3438 ad89fa31 2019-10-04 stsp }
3439 ad89fa31 2019-10-04 stsp
3440 ad89fa31 2019-10-04 stsp static const struct got_error *
3441 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3442 4e759de4 2019-06-26 stsp {
3443 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3444 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3445 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3446 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3447 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3448 4e759de4 2019-06-26 stsp
3449 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3450 ba882ee3 2019-07-11 stsp
3451 4e99b47e 2019-10-04 stsp if (worktree) {
3452 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3453 4e99b47e 2019-10-04 stsp worktree);
3454 4e99b47e 2019-10-04 stsp if (err)
3455 4e99b47e 2019-10-04 stsp return err;
3456 4e99b47e 2019-10-04 stsp
3457 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3458 4e99b47e 2019-10-04 stsp worktree);
3459 4e99b47e 2019-10-04 stsp if (err)
3460 4e99b47e 2019-10-04 stsp return err;
3461 4e99b47e 2019-10-04 stsp
3462 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3463 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3464 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3465 4e99b47e 2019-10-04 stsp if (err)
3466 4e99b47e 2019-10-04 stsp return err;
3467 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3468 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3469 4e99b47e 2019-10-04 stsp }
3470 4e99b47e 2019-10-04 stsp }
3471 4e99b47e 2019-10-04 stsp
3472 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3473 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3474 4e759de4 2019-06-26 stsp if (err)
3475 4e759de4 2019-06-26 stsp return err;
3476 4e759de4 2019-06-26 stsp
3477 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3478 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3479 4e759de4 2019-06-26 stsp
3480 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3481 4e759de4 2019-06-26 stsp return NULL;
3482 4e759de4 2019-06-26 stsp }
3483 4e759de4 2019-06-26 stsp
3484 4e759de4 2019-06-26 stsp static const struct got_error *
3485 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3486 45cd4e47 2019-08-25 stsp const char *branch_name)
3487 4e759de4 2019-06-26 stsp {
3488 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3489 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3490 4e759de4 2019-06-26 stsp char *refname;
3491 4e759de4 2019-06-26 stsp
3492 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3493 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3494 4e759de4 2019-06-26 stsp
3495 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3496 4e759de4 2019-06-26 stsp if (err)
3497 4e759de4 2019-06-26 stsp goto done;
3498 4e759de4 2019-06-26 stsp
3499 45cd4e47 2019-08-25 stsp if (worktree &&
3500 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3501 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3502 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3503 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3504 45cd4e47 2019-08-25 stsp goto done;
3505 45cd4e47 2019-08-25 stsp }
3506 45cd4e47 2019-08-25 stsp
3507 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3508 4e759de4 2019-06-26 stsp done:
3509 45cd4e47 2019-08-25 stsp if (ref)
3510 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3511 4e759de4 2019-06-26 stsp free(refname);
3512 4e759de4 2019-06-26 stsp return err;
3513 4e759de4 2019-06-26 stsp }
3514 4e759de4 2019-06-26 stsp
3515 4e759de4 2019-06-26 stsp static const struct got_error *
3516 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3517 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3518 4e759de4 2019-06-26 stsp {
3519 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3520 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3521 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3522 d3f84d51 2019-07-11 stsp
3523 d3f84d51 2019-07-11 stsp /*
3524 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3525 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3526 d3f84d51 2019-07-11 stsp * an unintended typo.
3527 d3f84d51 2019-07-11 stsp */
3528 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3529 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3530 4e759de4 2019-06-26 stsp
3531 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3532 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3533 4e759de4 2019-06-26 stsp goto done;
3534 4e759de4 2019-06-26 stsp }
3535 4e759de4 2019-06-26 stsp
3536 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3537 4e759de4 2019-06-26 stsp if (err == NULL) {
3538 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3539 4e759de4 2019-06-26 stsp goto done;
3540 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3541 4e759de4 2019-06-26 stsp goto done;
3542 4e759de4 2019-06-26 stsp
3543 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3544 4e759de4 2019-06-26 stsp if (err)
3545 4e759de4 2019-06-26 stsp goto done;
3546 4e759de4 2019-06-26 stsp
3547 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3548 d0eebce4 2019-03-11 stsp done:
3549 4e759de4 2019-06-26 stsp if (ref)
3550 4e759de4 2019-06-26 stsp got_ref_close(ref);
3551 4e759de4 2019-06-26 stsp free(base_refname);
3552 4e759de4 2019-06-26 stsp free(refname);
3553 4e759de4 2019-06-26 stsp return err;
3554 4e759de4 2019-06-26 stsp }
3555 4e759de4 2019-06-26 stsp
3556 4e759de4 2019-06-26 stsp static const struct got_error *
3557 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3558 4e759de4 2019-06-26 stsp {
3559 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3560 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3561 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3562 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3563 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3564 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3565 4e759de4 2019-06-26 stsp
3566 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3567 4e759de4 2019-06-26 stsp switch (ch) {
3568 a74f7e83 2019-11-10 stsp case 'c':
3569 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3570 a74f7e83 2019-11-10 stsp break;
3571 4e759de4 2019-06-26 stsp case 'd':
3572 4e759de4 2019-06-26 stsp delref = optarg;
3573 4e759de4 2019-06-26 stsp break;
3574 4e759de4 2019-06-26 stsp case 'r':
3575 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3576 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3577 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3578 9ba1d308 2019-10-21 stsp optarg);
3579 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3580 4e759de4 2019-06-26 stsp break;
3581 4e759de4 2019-06-26 stsp case 'l':
3582 4e759de4 2019-06-26 stsp do_list = 1;
3583 4e759de4 2019-06-26 stsp break;
3584 4e759de4 2019-06-26 stsp default:
3585 4e759de4 2019-06-26 stsp usage_branch();
3586 4e759de4 2019-06-26 stsp /* NOTREACHED */
3587 4e759de4 2019-06-26 stsp }
3588 4e759de4 2019-06-26 stsp }
3589 4e759de4 2019-06-26 stsp
3590 4e759de4 2019-06-26 stsp if (do_list && delref)
3591 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3592 4e759de4 2019-06-26 stsp
3593 4e759de4 2019-06-26 stsp argc -= optind;
3594 4e759de4 2019-06-26 stsp argv += optind;
3595 ad89fa31 2019-10-04 stsp
3596 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3597 ad89fa31 2019-10-04 stsp do_show = 1;
3598 4e759de4 2019-06-26 stsp
3599 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3600 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3601 a74f7e83 2019-11-10 stsp
3602 4e759de4 2019-06-26 stsp if (do_list || delref) {
3603 4e759de4 2019-06-26 stsp if (argc > 0)
3604 4e759de4 2019-06-26 stsp usage_branch();
3605 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3606 4e759de4 2019-06-26 stsp usage_branch();
3607 4e759de4 2019-06-26 stsp
3608 4e759de4 2019-06-26 stsp #ifndef PROFILE
3609 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3610 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3611 4e759de4 2019-06-26 stsp NULL) == -1)
3612 4e759de4 2019-06-26 stsp err(1, "pledge");
3613 4e759de4 2019-06-26 stsp } else {
3614 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3615 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3616 4e759de4 2019-06-26 stsp err(1, "pledge");
3617 4e759de4 2019-06-26 stsp }
3618 4e759de4 2019-06-26 stsp #endif
3619 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3620 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3621 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3622 4e759de4 2019-06-26 stsp goto done;
3623 4e759de4 2019-06-26 stsp }
3624 4e759de4 2019-06-26 stsp
3625 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3626 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3627 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3628 4e759de4 2019-06-26 stsp goto done;
3629 4e759de4 2019-06-26 stsp else
3630 4e759de4 2019-06-26 stsp error = NULL;
3631 4e759de4 2019-06-26 stsp if (worktree) {
3632 4e759de4 2019-06-26 stsp repo_path =
3633 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3634 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3635 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3636 4e759de4 2019-06-26 stsp if (error)
3637 4e759de4 2019-06-26 stsp goto done;
3638 4e759de4 2019-06-26 stsp } else {
3639 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3640 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3641 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3642 4e759de4 2019-06-26 stsp goto done;
3643 4e759de4 2019-06-26 stsp }
3644 4e759de4 2019-06-26 stsp }
3645 4e759de4 2019-06-26 stsp }
3646 4e759de4 2019-06-26 stsp
3647 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3648 4e759de4 2019-06-26 stsp if (error != NULL)
3649 4e759de4 2019-06-26 stsp goto done;
3650 4e759de4 2019-06-26 stsp
3651 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3652 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3653 4e759de4 2019-06-26 stsp if (error)
3654 4e759de4 2019-06-26 stsp goto done;
3655 4e759de4 2019-06-26 stsp
3656 ad89fa31 2019-10-04 stsp if (do_show)
3657 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3658 ad89fa31 2019-10-04 stsp else if (do_list)
3659 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3660 4e759de4 2019-06-26 stsp else if (delref)
3661 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3662 4e759de4 2019-06-26 stsp else {
3663 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3664 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3665 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3666 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3667 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3668 a74f7e83 2019-11-10 stsp error = resolve_commit_arg(&commit_id, commit_id_arg, repo);
3669 a74f7e83 2019-11-10 stsp if (error)
3670 a74f7e83 2019-11-10 stsp goto done;
3671 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3672 a74f7e83 2019-11-10 stsp free(commit_id);
3673 4e759de4 2019-06-26 stsp }
3674 4e759de4 2019-06-26 stsp done:
3675 d0eebce4 2019-03-11 stsp if (repo)
3676 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3677 d0eebce4 2019-03-11 stsp if (worktree)
3678 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3679 d0eebce4 2019-03-11 stsp free(cwd);
3680 d0eebce4 2019-03-11 stsp free(repo_path);
3681 d00136be 2019-03-26 stsp return error;
3682 d00136be 2019-03-26 stsp }
3683 d00136be 2019-03-26 stsp
3684 8e7bd50a 2019-08-22 stsp
3685 d00136be 2019-03-26 stsp __dead static void
3686 8e7bd50a 2019-08-22 stsp usage_tag(void)
3687 8e7bd50a 2019-08-22 stsp {
3688 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3689 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3690 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3691 8e7bd50a 2019-08-22 stsp exit(1);
3692 b8bad2ba 2019-08-23 stsp }
3693 b8bad2ba 2019-08-23 stsp
3694 b8bad2ba 2019-08-23 stsp #if 0
3695 b8bad2ba 2019-08-23 stsp static const struct got_error *
3696 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3697 b8bad2ba 2019-08-23 stsp {
3698 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3699 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3700 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3701 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3702 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3703 b8bad2ba 2019-08-23 stsp
3704 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3705 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3706 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3707 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3708 b8bad2ba 2019-08-23 stsp if (err)
3709 b8bad2ba 2019-08-23 stsp return err;
3710 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3711 b8bad2ba 2019-08-23 stsp continue;
3712 b8bad2ba 2019-08-23 stsp } else {
3713 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3714 b8bad2ba 2019-08-23 stsp if (err)
3715 b8bad2ba 2019-08-23 stsp break;
3716 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3717 b8bad2ba 2019-08-23 stsp free(re_id);
3718 b8bad2ba 2019-08-23 stsp if (err)
3719 b8bad2ba 2019-08-23 stsp break;
3720 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3721 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3722 b8bad2ba 2019-08-23 stsp }
3723 b8bad2ba 2019-08-23 stsp
3724 b8bad2ba 2019-08-23 stsp while (se) {
3725 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3726 b8bad2ba 2019-08-23 stsp if (err)
3727 b8bad2ba 2019-08-23 stsp break;
3728 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3729 b8bad2ba 2019-08-23 stsp free(se_id);
3730 b8bad2ba 2019-08-23 stsp if (err)
3731 b8bad2ba 2019-08-23 stsp break;
3732 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3733 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3734 b8bad2ba 2019-08-23 stsp
3735 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3736 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3737 b8bad2ba 2019-08-23 stsp if (err)
3738 b8bad2ba 2019-08-23 stsp return err;
3739 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3740 b8bad2ba 2019-08-23 stsp break;
3741 b8bad2ba 2019-08-23 stsp }
3742 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3743 b8bad2ba 2019-08-23 stsp continue;
3744 b8bad2ba 2019-08-23 stsp }
3745 b8bad2ba 2019-08-23 stsp }
3746 b8bad2ba 2019-08-23 stsp done:
3747 b8bad2ba 2019-08-23 stsp return err;
3748 8e7bd50a 2019-08-22 stsp }
3749 b8bad2ba 2019-08-23 stsp #endif
3750 8e7bd50a 2019-08-22 stsp
3751 8e7bd50a 2019-08-22 stsp static const struct got_error *
3752 b8bad2ba 2019-08-23 stsp cmp_tags(void *arg, int *cmp, struct got_reference *ref1,
3753 b8bad2ba 2019-08-23 stsp struct got_reference *ref2)
3754 b8bad2ba 2019-08-23 stsp {
3755 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3756 b8bad2ba 2019-08-23 stsp struct got_repository *repo = arg;
3757 b8bad2ba 2019-08-23 stsp struct got_object_id *id1, *id2 = NULL;
3758 b8bad2ba 2019-08-23 stsp struct got_tag_object *tag1 = NULL, *tag2 = NULL;
3759 b8bad2ba 2019-08-23 stsp time_t time1, time2;
3760 b8bad2ba 2019-08-23 stsp
3761 b8bad2ba 2019-08-23 stsp *cmp = 0;
3762 b8bad2ba 2019-08-23 stsp
3763 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id1, repo, ref1);
3764 b8bad2ba 2019-08-23 stsp if (err)
3765 b8bad2ba 2019-08-23 stsp return err;
3766 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag1, repo, id1);
3767 b8bad2ba 2019-08-23 stsp if (err)
3768 b8bad2ba 2019-08-23 stsp goto done;
3769 b8bad2ba 2019-08-23 stsp
3770 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&id2, repo, ref2);
3771 b8bad2ba 2019-08-23 stsp if (err)
3772 b8bad2ba 2019-08-23 stsp goto done;
3773 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&tag2, repo, id2);
3774 b8bad2ba 2019-08-23 stsp if (err)
3775 b8bad2ba 2019-08-23 stsp goto done;
3776 b8bad2ba 2019-08-23 stsp
3777 b8bad2ba 2019-08-23 stsp time1 = got_object_tag_get_tagger_time(tag1);
3778 b8bad2ba 2019-08-23 stsp time2 = got_object_tag_get_tagger_time(tag2);
3779 b8bad2ba 2019-08-23 stsp
3780 b8bad2ba 2019-08-23 stsp /* Put latest tags first. */
3781 b8bad2ba 2019-08-23 stsp if (time1 < time2)
3782 b8bad2ba 2019-08-23 stsp *cmp = 1;
3783 b8bad2ba 2019-08-23 stsp else if (time1 > time2)
3784 b8bad2ba 2019-08-23 stsp *cmp = -1;
3785 b8bad2ba 2019-08-23 stsp else
3786 b8bad2ba 2019-08-23 stsp err = got_ref_cmp_by_name(NULL, cmp, ref2, ref1);
3787 b8bad2ba 2019-08-23 stsp done:
3788 b8bad2ba 2019-08-23 stsp free(id1);
3789 b8bad2ba 2019-08-23 stsp free(id2);
3790 b8bad2ba 2019-08-23 stsp if (tag1)
3791 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag1);
3792 b8bad2ba 2019-08-23 stsp if (tag2)
3793 b8bad2ba 2019-08-23 stsp got_object_tag_close(tag2);
3794 b8bad2ba 2019-08-23 stsp return err;
3795 b8bad2ba 2019-08-23 stsp }
3796 b8bad2ba 2019-08-23 stsp
3797 b8bad2ba 2019-08-23 stsp static const struct got_error *
3798 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3799 8e7bd50a 2019-08-22 stsp {
3800 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3801 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3802 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3803 8e7bd50a 2019-08-22 stsp
3804 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3805 8e7bd50a 2019-08-22 stsp
3806 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/tags", cmp_tags, repo);
3807 8e7bd50a 2019-08-22 stsp if (err)
3808 8e7bd50a 2019-08-22 stsp return err;
3809 8e7bd50a 2019-08-22 stsp
3810 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3811 8e7bd50a 2019-08-22 stsp const char *refname;
3812 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3813 8e7bd50a 2019-08-22 stsp char datebuf[26];
3814 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3815 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3816 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3817 8e7bd50a 2019-08-22 stsp
3818 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3819 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3820 8e7bd50a 2019-08-22 stsp continue;
3821 8e7bd50a 2019-08-22 stsp refname += 10;
3822 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3823 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3824 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3825 8e7bd50a 2019-08-22 stsp break;
3826 8e7bd50a 2019-08-22 stsp }
3827 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3828 8e7bd50a 2019-08-22 stsp free(refstr);
3829 8e7bd50a 2019-08-22 stsp
3830 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3831 8e7bd50a 2019-08-22 stsp if (err)
3832 8e7bd50a 2019-08-22 stsp break;
3833 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3834 8e7bd50a 2019-08-22 stsp free(id);
3835 8e7bd50a 2019-08-22 stsp if (err)
3836 8e7bd50a 2019-08-22 stsp break;
3837 2417344c 2019-08-23 stsp printf("from: %s\n", got_object_tag_get_tagger(tag));
3838 2417344c 2019-08-23 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3839 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3840 2417344c 2019-08-23 stsp if (datestr)
3841 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3842 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&id_str,
3843 8e7bd50a 2019-08-22 stsp got_object_tag_get_object_id(tag));
3844 8e7bd50a 2019-08-22 stsp if (err)
3845 8e7bd50a 2019-08-22 stsp break;
3846 8e7bd50a 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
3847 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
3848 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB, id_str);
3849 8e7bd50a 2019-08-22 stsp break;
3850 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
3851 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE, id_str);
3852 8e7bd50a 2019-08-22 stsp break;
3853 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
3854 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3855 8e7bd50a 2019-08-22 stsp break;
3856 8e7bd50a 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
3857 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG, id_str);
3858 8e7bd50a 2019-08-22 stsp break;
3859 8e7bd50a 2019-08-22 stsp default:
3860 8e7bd50a 2019-08-22 stsp break;
3861 8e7bd50a 2019-08-22 stsp }
3862 8e7bd50a 2019-08-22 stsp free(id_str);
3863 8e7bd50a 2019-08-22 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3864 8e7bd50a 2019-08-22 stsp got_object_tag_close(tag);
3865 8e7bd50a 2019-08-22 stsp if (tagmsg0 == NULL) {
3866 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3867 8e7bd50a 2019-08-22 stsp break;
3868 8e7bd50a 2019-08-22 stsp }
3869 8e7bd50a 2019-08-22 stsp
3870 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3871 8e7bd50a 2019-08-22 stsp do {
3872 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3873 8e7bd50a 2019-08-22 stsp if (line)
3874 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3875 8e7bd50a 2019-08-22 stsp } while (line);
3876 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3877 8e7bd50a 2019-08-22 stsp }
3878 8e7bd50a 2019-08-22 stsp
3879 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3880 8e7bd50a 2019-08-22 stsp return NULL;
3881 8e7bd50a 2019-08-22 stsp }
3882 8e7bd50a 2019-08-22 stsp
3883 8e7bd50a 2019-08-22 stsp static const struct got_error *
3884 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3885 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3886 8e7bd50a 2019-08-22 stsp {
3887 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3888 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3889 f372d5cd 2019-10-21 stsp char *editor = NULL;
3890 8e7bd50a 2019-08-22 stsp int fd = -1;
3891 8e7bd50a 2019-08-22 stsp
3892 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3893 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3894 8e7bd50a 2019-08-22 stsp goto done;
3895 8e7bd50a 2019-08-22 stsp }
3896 8e7bd50a 2019-08-22 stsp
3897 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3898 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3899 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3900 8e7bd50a 2019-08-22 stsp goto done;
3901 8e7bd50a 2019-08-22 stsp }
3902 8e7bd50a 2019-08-22 stsp
3903 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3904 8e7bd50a 2019-08-22 stsp if (err)
3905 8e7bd50a 2019-08-22 stsp goto done;
3906 8e7bd50a 2019-08-22 stsp
3907 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3908 8e7bd50a 2019-08-22 stsp close(fd);
3909 8e7bd50a 2019-08-22 stsp
3910 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3911 8e7bd50a 2019-08-22 stsp if (err)
3912 8e7bd50a 2019-08-22 stsp goto done;
3913 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3914 8e7bd50a 2019-08-22 stsp done:
3915 8e7bd50a 2019-08-22 stsp free(initial_content);
3916 8e7bd50a 2019-08-22 stsp free(template);
3917 8e7bd50a 2019-08-22 stsp free(editor);
3918 8e7bd50a 2019-08-22 stsp
3919 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3920 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3921 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3922 8e7bd50a 2019-08-22 stsp if (err) {
3923 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3924 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3925 8e7bd50a 2019-08-22 stsp }
3926 8e7bd50a 2019-08-22 stsp }
3927 8e7bd50a 2019-08-22 stsp return err;
3928 8e7bd50a 2019-08-22 stsp }
3929 8e7bd50a 2019-08-22 stsp
3930 8e7bd50a 2019-08-22 stsp static const struct got_error *
3931 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3932 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3933 8e7bd50a 2019-08-22 stsp {
3934 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3935 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3936 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3937 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3938 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3939 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3940 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3941 8e7bd50a 2019-08-22 stsp
3942 8e7bd50a 2019-08-22 stsp /*
3943 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3944 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3945 8e7bd50a 2019-08-22 stsp * an unintended typo.
3946 8e7bd50a 2019-08-22 stsp */
3947 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3948 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3949 8e7bd50a 2019-08-22 stsp
3950 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3951 8e7bd50a 2019-08-22 stsp if (err)
3952 8e7bd50a 2019-08-22 stsp return err;
3953 8e7bd50a 2019-08-22 stsp
3954 8e7bd50a 2019-08-22 stsp err = match_object_id(&commit_id, &label, commit_arg,
3955 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3956 8e7bd50a 2019-08-22 stsp if (err)
3957 8e7bd50a 2019-08-22 stsp goto done;
3958 8e7bd50a 2019-08-22 stsp
3959 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3960 8e7bd50a 2019-08-22 stsp if (err)
3961 8e7bd50a 2019-08-22 stsp goto done;
3962 8e7bd50a 2019-08-22 stsp
3963 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3964 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3965 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3966 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3967 8e7bd50a 2019-08-22 stsp goto done;
3968 8e7bd50a 2019-08-22 stsp }
3969 8e7bd50a 2019-08-22 stsp tag_name += 10;
3970 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3971 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3972 8e7bd50a 2019-08-22 stsp goto done;
3973 8e7bd50a 2019-08-22 stsp }
3974 8e7bd50a 2019-08-22 stsp
3975 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3976 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3977 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3978 8e7bd50a 2019-08-22 stsp goto done;
3979 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3980 8e7bd50a 2019-08-22 stsp goto done;
3981 8e7bd50a 2019-08-22 stsp
3982 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3983 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3984 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3985 f372d5cd 2019-10-21 stsp if (err) {
3986 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3987 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3988 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3989 8e7bd50a 2019-08-22 stsp goto done;
3990 f372d5cd 2019-10-21 stsp }
3991 8e7bd50a 2019-08-22 stsp }
3992 8e7bd50a 2019-08-22 stsp
3993 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3994 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3995 f372d5cd 2019-10-21 stsp if (err) {
3996 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3997 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3998 8e7bd50a 2019-08-22 stsp goto done;
3999 f372d5cd 2019-10-21 stsp }
4000 8e7bd50a 2019-08-22 stsp
4001 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
4002 f372d5cd 2019-10-21 stsp if (err) {
4003 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4004 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4005 8e7bd50a 2019-08-22 stsp goto done;
4006 f372d5cd 2019-10-21 stsp }
4007 8e7bd50a 2019-08-22 stsp
4008 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
4009 f372d5cd 2019-10-21 stsp if (err) {
4010 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4011 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4012 f372d5cd 2019-10-21 stsp goto done;
4013 f372d5cd 2019-10-21 stsp }
4014 8e7bd50a 2019-08-22 stsp
4015 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4016 f372d5cd 2019-10-21 stsp if (err) {
4017 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4018 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4019 f372d5cd 2019-10-21 stsp goto done;
4020 8e7bd50a 2019-08-22 stsp }
4021 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4022 8e7bd50a 2019-08-22 stsp done:
4023 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4024 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4025 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4026 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4027 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4028 f372d5cd 2019-10-21 stsp free(tag_id_str);
4029 8e7bd50a 2019-08-22 stsp if (ref)
4030 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4031 8e7bd50a 2019-08-22 stsp free(commit_id);
4032 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4033 8e7bd50a 2019-08-22 stsp free(refname);
4034 8e7bd50a 2019-08-22 stsp free(tagmsg);
4035 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4036 aba9c984 2019-09-08 stsp free(tagger);
4037 8e7bd50a 2019-08-22 stsp return err;
4038 8e7bd50a 2019-08-22 stsp }
4039 8e7bd50a 2019-08-22 stsp
4040 8e7bd50a 2019-08-22 stsp static const struct got_error *
4041 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4042 8e7bd50a 2019-08-22 stsp {
4043 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4044 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4045 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4046 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4047 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4048 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4049 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4050 8e7bd50a 2019-08-22 stsp
4051 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
4052 8e7bd50a 2019-08-22 stsp switch (ch) {
4053 8e7bd50a 2019-08-22 stsp case 'm':
4054 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4055 8e7bd50a 2019-08-22 stsp break;
4056 8e7bd50a 2019-08-22 stsp case 'r':
4057 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4058 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4059 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4060 9ba1d308 2019-10-21 stsp optarg);
4061 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4062 8e7bd50a 2019-08-22 stsp break;
4063 8e7bd50a 2019-08-22 stsp case 'l':
4064 8e7bd50a 2019-08-22 stsp do_list = 1;
4065 8e7bd50a 2019-08-22 stsp break;
4066 8e7bd50a 2019-08-22 stsp default:
4067 8e7bd50a 2019-08-22 stsp usage_tag();
4068 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4069 8e7bd50a 2019-08-22 stsp }
4070 8e7bd50a 2019-08-22 stsp }
4071 8e7bd50a 2019-08-22 stsp
4072 8e7bd50a 2019-08-22 stsp argc -= optind;
4073 8e7bd50a 2019-08-22 stsp argv += optind;
4074 8e7bd50a 2019-08-22 stsp
4075 8e7bd50a 2019-08-22 stsp if (do_list) {
4076 8e7bd50a 2019-08-22 stsp if (tagmsg)
4077 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
4078 8e7bd50a 2019-08-22 stsp if (argc > 0)
4079 8e7bd50a 2019-08-22 stsp usage_tag();
4080 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
4081 8e7bd50a 2019-08-22 stsp usage_tag();
4082 a2887370 2019-08-23 stsp else if (argc > 1)
4083 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
4084 a2887370 2019-08-23 stsp tag_name = argv[0];
4085 8e7bd50a 2019-08-22 stsp
4086 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4087 8e7bd50a 2019-08-22 stsp if (do_list) {
4088 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4089 8e7bd50a 2019-08-22 stsp NULL) == -1)
4090 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4091 8e7bd50a 2019-08-22 stsp } else {
4092 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4093 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4094 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4095 8e7bd50a 2019-08-22 stsp }
4096 8e7bd50a 2019-08-22 stsp #endif
4097 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4098 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4099 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4100 8e7bd50a 2019-08-22 stsp goto done;
4101 8e7bd50a 2019-08-22 stsp }
4102 8e7bd50a 2019-08-22 stsp
4103 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4104 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4105 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4106 8e7bd50a 2019-08-22 stsp goto done;
4107 8e7bd50a 2019-08-22 stsp else
4108 8e7bd50a 2019-08-22 stsp error = NULL;
4109 8e7bd50a 2019-08-22 stsp if (worktree) {
4110 8e7bd50a 2019-08-22 stsp repo_path =
4111 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4112 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4113 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4114 8e7bd50a 2019-08-22 stsp if (error)
4115 8e7bd50a 2019-08-22 stsp goto done;
4116 8e7bd50a 2019-08-22 stsp } else {
4117 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4118 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4119 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4120 8e7bd50a 2019-08-22 stsp goto done;
4121 8e7bd50a 2019-08-22 stsp }
4122 8e7bd50a 2019-08-22 stsp }
4123 8e7bd50a 2019-08-22 stsp }
4124 8e7bd50a 2019-08-22 stsp
4125 8e7bd50a 2019-08-22 stsp if (do_list) {
4126 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4127 c9956ddf 2019-09-08 stsp if (error != NULL)
4128 c9956ddf 2019-09-08 stsp goto done;
4129 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4130 8e7bd50a 2019-08-22 stsp if (error)
4131 8e7bd50a 2019-08-22 stsp goto done;
4132 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4133 8e7bd50a 2019-08-22 stsp } else {
4134 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4135 c9956ddf 2019-09-08 stsp if (error)
4136 c9956ddf 2019-09-08 stsp goto done;
4137 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4138 c9956ddf 2019-09-08 stsp if (error != NULL)
4139 c9956ddf 2019-09-08 stsp goto done;
4140 c9956ddf 2019-09-08 stsp
4141 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4142 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4143 8e7bd50a 2019-08-22 stsp if (error)
4144 8e7bd50a 2019-08-22 stsp goto done;
4145 8e7bd50a 2019-08-22 stsp }
4146 8e7bd50a 2019-08-22 stsp
4147 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4148 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4149 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4150 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4151 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4152 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4153 8e7bd50a 2019-08-22 stsp if (error)
4154 8e7bd50a 2019-08-22 stsp goto done;
4155 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4156 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4157 8e7bd50a 2019-08-22 stsp if (error)
4158 8e7bd50a 2019-08-22 stsp goto done;
4159 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4160 8e7bd50a 2019-08-22 stsp free(commit_id);
4161 8e7bd50a 2019-08-22 stsp if (error)
4162 8e7bd50a 2019-08-22 stsp goto done;
4163 8e7bd50a 2019-08-22 stsp }
4164 8e7bd50a 2019-08-22 stsp
4165 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4166 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4167 8e7bd50a 2019-08-22 stsp }
4168 8e7bd50a 2019-08-22 stsp done:
4169 8e7bd50a 2019-08-22 stsp if (repo)
4170 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4171 8e7bd50a 2019-08-22 stsp if (worktree)
4172 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4173 8e7bd50a 2019-08-22 stsp free(cwd);
4174 8e7bd50a 2019-08-22 stsp free(repo_path);
4175 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4176 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4177 8e7bd50a 2019-08-22 stsp return error;
4178 8e7bd50a 2019-08-22 stsp }
4179 8e7bd50a 2019-08-22 stsp
4180 8e7bd50a 2019-08-22 stsp __dead static void
4181 d00136be 2019-03-26 stsp usage_add(void)
4182 d00136be 2019-03-26 stsp {
4183 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4184 022fae89 2019-12-06 tracey getprogname());
4185 d00136be 2019-03-26 stsp exit(1);
4186 d00136be 2019-03-26 stsp }
4187 d00136be 2019-03-26 stsp
4188 d00136be 2019-03-26 stsp static const struct got_error *
4189 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4190 4e68cba3 2019-11-23 stsp {
4191 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4192 4e68cba3 2019-11-23 stsp path++;
4193 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4194 4e68cba3 2019-11-23 stsp return NULL;
4195 4e68cba3 2019-11-23 stsp }
4196 4e68cba3 2019-11-23 stsp
4197 4e68cba3 2019-11-23 stsp static const struct got_error *
4198 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4199 d00136be 2019-03-26 stsp {
4200 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4201 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4202 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4203 1dd54920 2019-05-11 stsp char *cwd = NULL;
4204 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4205 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4206 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4207 1dd54920 2019-05-11 stsp
4208 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4209 d00136be 2019-03-26 stsp
4210 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4211 d00136be 2019-03-26 stsp switch (ch) {
4212 022fae89 2019-12-06 tracey case 'I':
4213 022fae89 2019-12-06 tracey no_ignores = 1;
4214 022fae89 2019-12-06 tracey break;
4215 4e68cba3 2019-11-23 stsp case 'R':
4216 4e68cba3 2019-11-23 stsp can_recurse = 1;
4217 4e68cba3 2019-11-23 stsp break;
4218 d00136be 2019-03-26 stsp default:
4219 d00136be 2019-03-26 stsp usage_add();
4220 d00136be 2019-03-26 stsp /* NOTREACHED */
4221 d00136be 2019-03-26 stsp }
4222 d00136be 2019-03-26 stsp }
4223 d00136be 2019-03-26 stsp
4224 d00136be 2019-03-26 stsp argc -= optind;
4225 d00136be 2019-03-26 stsp argv += optind;
4226 d00136be 2019-03-26 stsp
4227 43012d58 2019-07-14 stsp #ifndef PROFILE
4228 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4229 43012d58 2019-07-14 stsp NULL) == -1)
4230 43012d58 2019-07-14 stsp err(1, "pledge");
4231 43012d58 2019-07-14 stsp #endif
4232 723c305c 2019-05-11 jcs if (argc < 1)
4233 d00136be 2019-03-26 stsp usage_add();
4234 d00136be 2019-03-26 stsp
4235 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4236 d00136be 2019-03-26 stsp if (cwd == NULL) {
4237 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4238 d00136be 2019-03-26 stsp goto done;
4239 d00136be 2019-03-26 stsp }
4240 723c305c 2019-05-11 jcs
4241 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4242 d00136be 2019-03-26 stsp if (error)
4243 d00136be 2019-03-26 stsp goto done;
4244 d00136be 2019-03-26 stsp
4245 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4246 c9956ddf 2019-09-08 stsp NULL);
4247 031a5338 2019-03-26 stsp if (error != NULL)
4248 031a5338 2019-03-26 stsp goto done;
4249 031a5338 2019-03-26 stsp
4250 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4251 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4252 d00136be 2019-03-26 stsp if (error)
4253 d00136be 2019-03-26 stsp goto done;
4254 d00136be 2019-03-26 stsp
4255 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4256 6d022e97 2019-08-04 stsp if (error)
4257 022fae89 2019-12-06 tracey goto done;
4258 022fae89 2019-12-06 tracey
4259 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4260 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4261 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4262 6d022e97 2019-08-04 stsp goto done;
4263 723c305c 2019-05-11 jcs
4264 022fae89 2019-12-06 tracey }
4265 022fae89 2019-12-06 tracey
4266 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4267 4e68cba3 2019-11-23 stsp char *ondisk_path;
4268 4e68cba3 2019-11-23 stsp struct stat sb;
4269 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4270 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4271 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4272 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4273 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4274 4e68cba3 2019-11-23 stsp goto done;
4275 4e68cba3 2019-11-23 stsp }
4276 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4277 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4278 4e68cba3 2019-11-23 stsp free(ondisk_path);
4279 4e68cba3 2019-11-23 stsp continue;
4280 4e68cba3 2019-11-23 stsp }
4281 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4282 4e68cba3 2019-11-23 stsp ondisk_path);
4283 4e68cba3 2019-11-23 stsp free(ondisk_path);
4284 4e68cba3 2019-11-23 stsp goto done;
4285 4e68cba3 2019-11-23 stsp }
4286 4e68cba3 2019-11-23 stsp free(ondisk_path);
4287 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4288 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4289 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4290 4e68cba3 2019-11-23 stsp goto done;
4291 4e68cba3 2019-11-23 stsp }
4292 4e68cba3 2019-11-23 stsp }
4293 4e68cba3 2019-11-23 stsp }
4294 022fae89 2019-12-06 tracey
4295 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4296 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4297 d00136be 2019-03-26 stsp done:
4298 031a5338 2019-03-26 stsp if (repo)
4299 031a5338 2019-03-26 stsp got_repo_close(repo);
4300 d00136be 2019-03-26 stsp if (worktree)
4301 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4302 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4303 1dd54920 2019-05-11 stsp free((char *)pe->path);
4304 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4305 2ec1f75b 2019-03-26 stsp free(cwd);
4306 2ec1f75b 2019-03-26 stsp return error;
4307 2ec1f75b 2019-03-26 stsp }
4308 2ec1f75b 2019-03-26 stsp
4309 2ec1f75b 2019-03-26 stsp __dead static void
4310 648e4ef7 2019-07-09 stsp usage_remove(void)
4311 2ec1f75b 2019-03-26 stsp {
4312 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4313 f2a9dc41 2019-12-13 tracey getprogname());
4314 2ec1f75b 2019-03-26 stsp exit(1);
4315 2a06fe5f 2019-08-24 stsp }
4316 2a06fe5f 2019-08-24 stsp
4317 2a06fe5f 2019-08-24 stsp static const struct got_error *
4318 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4319 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4320 2a06fe5f 2019-08-24 stsp {
4321 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4322 f2a9dc41 2019-12-13 tracey path++;
4323 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4324 2a06fe5f 2019-08-24 stsp return NULL;
4325 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4326 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4327 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4328 2a06fe5f 2019-08-24 stsp return NULL;
4329 2ec1f75b 2019-03-26 stsp }
4330 2ec1f75b 2019-03-26 stsp
4331 2ec1f75b 2019-03-26 stsp static const struct got_error *
4332 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4333 2ec1f75b 2019-03-26 stsp {
4334 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4335 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4336 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4337 17ed4618 2019-06-02 stsp char *cwd = NULL;
4338 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4339 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4340 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4341 2ec1f75b 2019-03-26 stsp
4342 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4343 17ed4618 2019-06-02 stsp
4344 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4345 2ec1f75b 2019-03-26 stsp switch (ch) {
4346 2ec1f75b 2019-03-26 stsp case 'f':
4347 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4348 2ec1f75b 2019-03-26 stsp break;
4349 70e3e7f5 2019-12-13 tracey case 'k':
4350 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4351 70e3e7f5 2019-12-13 tracey break;
4352 f2a9dc41 2019-12-13 tracey case 'R':
4353 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4354 f2a9dc41 2019-12-13 tracey break;
4355 2ec1f75b 2019-03-26 stsp default:
4356 f2a9dc41 2019-12-13 tracey usage_remove();
4357 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4358 2ec1f75b 2019-03-26 stsp }
4359 2ec1f75b 2019-03-26 stsp }
4360 2ec1f75b 2019-03-26 stsp
4361 2ec1f75b 2019-03-26 stsp argc -= optind;
4362 2ec1f75b 2019-03-26 stsp argv += optind;
4363 2ec1f75b 2019-03-26 stsp
4364 43012d58 2019-07-14 stsp #ifndef PROFILE
4365 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4366 43012d58 2019-07-14 stsp NULL) == -1)
4367 43012d58 2019-07-14 stsp err(1, "pledge");
4368 43012d58 2019-07-14 stsp #endif
4369 17ed4618 2019-06-02 stsp if (argc < 1)
4370 648e4ef7 2019-07-09 stsp usage_remove();
4371 2ec1f75b 2019-03-26 stsp
4372 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4373 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4374 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4375 2ec1f75b 2019-03-26 stsp goto done;
4376 2ec1f75b 2019-03-26 stsp }
4377 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4378 2ec1f75b 2019-03-26 stsp if (error)
4379 2ec1f75b 2019-03-26 stsp goto done;
4380 2ec1f75b 2019-03-26 stsp
4381 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4382 c9956ddf 2019-09-08 stsp NULL);
4383 2af4a041 2019-05-11 jcs if (error)
4384 2ec1f75b 2019-03-26 stsp goto done;
4385 2ec1f75b 2019-03-26 stsp
4386 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4387 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4388 2ec1f75b 2019-03-26 stsp if (error)
4389 2ec1f75b 2019-03-26 stsp goto done;
4390 2ec1f75b 2019-03-26 stsp
4391 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4392 6d022e97 2019-08-04 stsp if (error)
4393 6d022e97 2019-08-04 stsp goto done;
4394 17ed4618 2019-06-02 stsp
4395 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4396 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4397 f2a9dc41 2019-12-13 tracey struct stat sb;
4398 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4399 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4400 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4401 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4402 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4403 f2a9dc41 2019-12-13 tracey goto done;
4404 f2a9dc41 2019-12-13 tracey }
4405 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4406 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4407 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4408 f2a9dc41 2019-12-13 tracey continue;
4409 f2a9dc41 2019-12-13 tracey }
4410 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4411 f2a9dc41 2019-12-13 tracey ondisk_path);
4412 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4413 f2a9dc41 2019-12-13 tracey goto done;
4414 f2a9dc41 2019-12-13 tracey }
4415 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4416 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4417 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4418 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4419 f2a9dc41 2019-12-13 tracey goto done;
4420 f2a9dc41 2019-12-13 tracey }
4421 f2a9dc41 2019-12-13 tracey }
4422 f2a9dc41 2019-12-13 tracey }
4423 f2a9dc41 2019-12-13 tracey
4424 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4425 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4426 a129376b 2019-03-28 stsp if (error)
4427 a129376b 2019-03-28 stsp goto done;
4428 a129376b 2019-03-28 stsp done:
4429 a129376b 2019-03-28 stsp if (repo)
4430 a129376b 2019-03-28 stsp got_repo_close(repo);
4431 a129376b 2019-03-28 stsp if (worktree)
4432 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4433 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4434 17ed4618 2019-06-02 stsp free((char *)pe->path);
4435 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4436 a129376b 2019-03-28 stsp free(cwd);
4437 a129376b 2019-03-28 stsp return error;
4438 a129376b 2019-03-28 stsp }
4439 a129376b 2019-03-28 stsp
4440 a129376b 2019-03-28 stsp __dead static void
4441 a129376b 2019-03-28 stsp usage_revert(void)
4442 a129376b 2019-03-28 stsp {
4443 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4444 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4445 a129376b 2019-03-28 stsp exit(1);
4446 a129376b 2019-03-28 stsp }
4447 a129376b 2019-03-28 stsp
4448 1ee397ad 2019-07-12 stsp static const struct got_error *
4449 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4450 a129376b 2019-03-28 stsp {
4451 a129376b 2019-03-28 stsp while (path[0] == '/')
4452 a129376b 2019-03-28 stsp path++;
4453 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4454 33aa809d 2019-08-08 stsp return NULL;
4455 33aa809d 2019-08-08 stsp }
4456 33aa809d 2019-08-08 stsp
4457 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4458 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4459 33aa809d 2019-08-08 stsp const char *action;
4460 33aa809d 2019-08-08 stsp };
4461 33aa809d 2019-08-08 stsp
4462 33aa809d 2019-08-08 stsp static const struct got_error *
4463 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4464 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4465 33aa809d 2019-08-08 stsp {
4466 33aa809d 2019-08-08 stsp char *line = NULL;
4467 33aa809d 2019-08-08 stsp size_t linesize = 0;
4468 33aa809d 2019-08-08 stsp ssize_t linelen;
4469 33aa809d 2019-08-08 stsp
4470 33aa809d 2019-08-08 stsp switch (status) {
4471 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4472 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4473 33aa809d 2019-08-08 stsp break;
4474 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4475 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4476 33aa809d 2019-08-08 stsp break;
4477 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4478 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4479 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4480 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4481 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4482 33aa809d 2019-08-08 stsp printf("%s", line);
4483 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4484 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4485 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4486 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4487 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4488 33aa809d 2019-08-08 stsp break;
4489 33aa809d 2019-08-08 stsp default:
4490 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4491 33aa809d 2019-08-08 stsp }
4492 33aa809d 2019-08-08 stsp
4493 33aa809d 2019-08-08 stsp return NULL;
4494 33aa809d 2019-08-08 stsp }
4495 33aa809d 2019-08-08 stsp
4496 33aa809d 2019-08-08 stsp static const struct got_error *
4497 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4498 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4499 33aa809d 2019-08-08 stsp {
4500 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4501 33aa809d 2019-08-08 stsp char *line = NULL;
4502 33aa809d 2019-08-08 stsp size_t linesize = 0;
4503 33aa809d 2019-08-08 stsp ssize_t linelen;
4504 33aa809d 2019-08-08 stsp int resp = ' ';
4505 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4506 33aa809d 2019-08-08 stsp
4507 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4508 33aa809d 2019-08-08 stsp
4509 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4510 33aa809d 2019-08-08 stsp char *nl;
4511 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4512 33aa809d 2019-08-08 stsp a->action);
4513 33aa809d 2019-08-08 stsp if (err)
4514 33aa809d 2019-08-08 stsp return err;
4515 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4516 33aa809d 2019-08-08 stsp if (linelen == -1) {
4517 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4518 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4519 33aa809d 2019-08-08 stsp return NULL;
4520 33aa809d 2019-08-08 stsp }
4521 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4522 33aa809d 2019-08-08 stsp if (nl)
4523 33aa809d 2019-08-08 stsp *nl = '\0';
4524 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4525 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4526 33aa809d 2019-08-08 stsp printf("y\n");
4527 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4528 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4529 33aa809d 2019-08-08 stsp printf("n\n");
4530 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4531 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4532 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4533 33aa809d 2019-08-08 stsp printf("q\n");
4534 33aa809d 2019-08-08 stsp } else
4535 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4536 33aa809d 2019-08-08 stsp free(line);
4537 33aa809d 2019-08-08 stsp return NULL;
4538 33aa809d 2019-08-08 stsp }
4539 33aa809d 2019-08-08 stsp
4540 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4541 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4542 33aa809d 2019-08-08 stsp a->action);
4543 33aa809d 2019-08-08 stsp if (err)
4544 33aa809d 2019-08-08 stsp return err;
4545 33aa809d 2019-08-08 stsp resp = getchar();
4546 33aa809d 2019-08-08 stsp if (resp == '\n')
4547 33aa809d 2019-08-08 stsp resp = getchar();
4548 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4549 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4550 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4551 33aa809d 2019-08-08 stsp resp = ' ';
4552 33aa809d 2019-08-08 stsp }
4553 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4554 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4555 33aa809d 2019-08-08 stsp resp = ' ';
4556 33aa809d 2019-08-08 stsp }
4557 33aa809d 2019-08-08 stsp }
4558 33aa809d 2019-08-08 stsp
4559 33aa809d 2019-08-08 stsp if (resp == 'y')
4560 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4561 33aa809d 2019-08-08 stsp else if (resp == 'n')
4562 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4563 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4564 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4565 33aa809d 2019-08-08 stsp
4566 1ee397ad 2019-07-12 stsp return NULL;
4567 a129376b 2019-03-28 stsp }
4568 a129376b 2019-03-28 stsp
4569 33aa809d 2019-08-08 stsp
4570 a129376b 2019-03-28 stsp static const struct got_error *
4571 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4572 a129376b 2019-03-28 stsp {
4573 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4574 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4575 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4576 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4577 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4578 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4579 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4580 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4581 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4582 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4583 a129376b 2019-03-28 stsp
4584 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4585 e20a8b6f 2019-06-04 stsp
4586 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4587 a129376b 2019-03-28 stsp switch (ch) {
4588 33aa809d 2019-08-08 stsp case 'p':
4589 33aa809d 2019-08-08 stsp pflag = 1;
4590 33aa809d 2019-08-08 stsp break;
4591 33aa809d 2019-08-08 stsp case 'F':
4592 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4593 33aa809d 2019-08-08 stsp break;
4594 0f6d7415 2019-08-08 stsp case 'R':
4595 0f6d7415 2019-08-08 stsp can_recurse = 1;
4596 0f6d7415 2019-08-08 stsp break;
4597 a129376b 2019-03-28 stsp default:
4598 a129376b 2019-03-28 stsp usage_revert();
4599 a129376b 2019-03-28 stsp /* NOTREACHED */
4600 a129376b 2019-03-28 stsp }
4601 a129376b 2019-03-28 stsp }
4602 a129376b 2019-03-28 stsp
4603 a129376b 2019-03-28 stsp argc -= optind;
4604 a129376b 2019-03-28 stsp argv += optind;
4605 a129376b 2019-03-28 stsp
4606 43012d58 2019-07-14 stsp #ifndef PROFILE
4607 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4608 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4609 43012d58 2019-07-14 stsp err(1, "pledge");
4610 43012d58 2019-07-14 stsp #endif
4611 e20a8b6f 2019-06-04 stsp if (argc < 1)
4612 a129376b 2019-03-28 stsp usage_revert();
4613 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4614 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4615 a129376b 2019-03-28 stsp
4616 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4617 a129376b 2019-03-28 stsp if (cwd == NULL) {
4618 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4619 a129376b 2019-03-28 stsp goto done;
4620 a129376b 2019-03-28 stsp }
4621 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4622 2ec1f75b 2019-03-26 stsp if (error)
4623 2ec1f75b 2019-03-26 stsp goto done;
4624 a129376b 2019-03-28 stsp
4625 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4626 c9956ddf 2019-09-08 stsp NULL);
4627 a129376b 2019-03-28 stsp if (error != NULL)
4628 a129376b 2019-03-28 stsp goto done;
4629 a129376b 2019-03-28 stsp
4630 33aa809d 2019-08-08 stsp if (patch_script_path) {
4631 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4632 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4633 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4634 33aa809d 2019-08-08 stsp patch_script_path);
4635 33aa809d 2019-08-08 stsp goto done;
4636 33aa809d 2019-08-08 stsp }
4637 33aa809d 2019-08-08 stsp }
4638 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4639 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4640 a129376b 2019-03-28 stsp if (error)
4641 a129376b 2019-03-28 stsp goto done;
4642 a129376b 2019-03-28 stsp
4643 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4644 6d022e97 2019-08-04 stsp if (error)
4645 6d022e97 2019-08-04 stsp goto done;
4646 00db391e 2019-08-03 stsp
4647 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4648 0f6d7415 2019-08-08 stsp char *ondisk_path;
4649 0f6d7415 2019-08-08 stsp struct stat sb;
4650 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4651 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4652 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4653 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4654 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4655 0f6d7415 2019-08-08 stsp goto done;
4656 0f6d7415 2019-08-08 stsp }
4657 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4658 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4659 0f6d7415 2019-08-08 stsp free(ondisk_path);
4660 0f6d7415 2019-08-08 stsp continue;
4661 0f6d7415 2019-08-08 stsp }
4662 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4663 0f6d7415 2019-08-08 stsp ondisk_path);
4664 0f6d7415 2019-08-08 stsp free(ondisk_path);
4665 0f6d7415 2019-08-08 stsp goto done;
4666 0f6d7415 2019-08-08 stsp }
4667 0f6d7415 2019-08-08 stsp free(ondisk_path);
4668 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4669 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4670 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4671 0f6d7415 2019-08-08 stsp goto done;
4672 0f6d7415 2019-08-08 stsp }
4673 0f6d7415 2019-08-08 stsp }
4674 0f6d7415 2019-08-08 stsp }
4675 0f6d7415 2019-08-08 stsp
4676 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4677 33aa809d 2019-08-08 stsp cpa.action = "revert";
4678 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4679 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4680 a129376b 2019-03-28 stsp if (error)
4681 a129376b 2019-03-28 stsp goto done;
4682 2ec1f75b 2019-03-26 stsp done:
4683 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4684 33aa809d 2019-08-08 stsp error == NULL)
4685 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4686 2ec1f75b 2019-03-26 stsp if (repo)
4687 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4688 2ec1f75b 2019-03-26 stsp if (worktree)
4689 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4690 2ec1f75b 2019-03-26 stsp free(path);
4691 d00136be 2019-03-26 stsp free(cwd);
4692 6bad629b 2019-02-04 stsp return error;
4693 c4296144 2019-05-09 stsp }
4694 c4296144 2019-05-09 stsp
4695 c4296144 2019-05-09 stsp __dead static void
4696 c4296144 2019-05-09 stsp usage_commit(void)
4697 c4296144 2019-05-09 stsp {
4698 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4699 5c1e53bc 2019-07-28 stsp getprogname());
4700 c4296144 2019-05-09 stsp exit(1);
4701 33ad4cbe 2019-05-12 jcs }
4702 33ad4cbe 2019-05-12 jcs
4703 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4704 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4705 e2ba3d07 2019-05-13 stsp const char *editor;
4706 e0870e44 2019-05-13 stsp const char *worktree_path;
4707 76d98825 2019-06-03 stsp const char *branch_name;
4708 314a6357 2019-05-13 stsp const char *repo_path;
4709 e0870e44 2019-05-13 stsp char *logmsg_path;
4710 e2ba3d07 2019-05-13 stsp
4711 e2ba3d07 2019-05-13 stsp };
4712 e0870e44 2019-05-13 stsp
4713 33ad4cbe 2019-05-12 jcs static const struct got_error *
4714 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4715 33ad4cbe 2019-05-12 jcs void *arg)
4716 33ad4cbe 2019-05-12 jcs {
4717 76d98825 2019-06-03 stsp char *initial_content = NULL;
4718 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4719 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4720 e0870e44 2019-05-13 stsp char *template = NULL;
4721 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4722 3ce1b845 2019-07-15 stsp int fd;
4723 33ad4cbe 2019-05-12 jcs size_t len;
4724 33ad4cbe 2019-05-12 jcs
4725 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4726 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4727 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4728 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4729 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4730 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4731 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4732 33ad4cbe 2019-05-12 jcs return NULL;
4733 33ad4cbe 2019-05-12 jcs }
4734 33ad4cbe 2019-05-12 jcs
4735 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4736 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4737 76d98825 2019-06-03 stsp
4738 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4739 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4740 76d98825 2019-06-03 stsp a->branch_name) == -1)
4741 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4742 e0870e44 2019-05-13 stsp
4743 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4744 793c30b5 2019-05-13 stsp if (err)
4745 e0870e44 2019-05-13 stsp goto done;
4746 33ad4cbe 2019-05-12 jcs
4747 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4748 33ad4cbe 2019-05-12 jcs
4749 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4750 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4751 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4752 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4753 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4754 33ad4cbe 2019-05-12 jcs }
4755 33ad4cbe 2019-05-12 jcs close(fd);
4756 33ad4cbe 2019-05-12 jcs
4757 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4758 33ad4cbe 2019-05-12 jcs done:
4759 76d98825 2019-06-03 stsp free(initial_content);
4760 e0870e44 2019-05-13 stsp free(template);
4761 314a6357 2019-05-13 stsp
4762 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4763 3ce1b845 2019-07-15 stsp if (err == NULL) {
4764 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4765 3ce1b845 2019-07-15 stsp if (err) {
4766 3ce1b845 2019-07-15 stsp free(*logmsg);
4767 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4768 3ce1b845 2019-07-15 stsp }
4769 3ce1b845 2019-07-15 stsp }
4770 33ad4cbe 2019-05-12 jcs return err;
4771 6bad629b 2019-02-04 stsp }
4772 c4296144 2019-05-09 stsp
4773 c4296144 2019-05-09 stsp static const struct got_error *
4774 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4775 c4296144 2019-05-09 stsp {
4776 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4777 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4778 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4779 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4780 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4781 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4782 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4783 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4784 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4785 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4786 5c1e53bc 2019-07-28 stsp
4787 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4788 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4789 c4296144 2019-05-09 stsp
4790 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4791 c4296144 2019-05-09 stsp switch (ch) {
4792 c4296144 2019-05-09 stsp case 'm':
4793 c4296144 2019-05-09 stsp logmsg = optarg;
4794 c4296144 2019-05-09 stsp break;
4795 c4296144 2019-05-09 stsp default:
4796 c4296144 2019-05-09 stsp usage_commit();
4797 c4296144 2019-05-09 stsp /* NOTREACHED */
4798 c4296144 2019-05-09 stsp }
4799 c4296144 2019-05-09 stsp }
4800 c4296144 2019-05-09 stsp
4801 c4296144 2019-05-09 stsp argc -= optind;
4802 c4296144 2019-05-09 stsp argv += optind;
4803 c4296144 2019-05-09 stsp
4804 43012d58 2019-07-14 stsp #ifndef PROFILE
4805 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4806 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4807 43012d58 2019-07-14 stsp err(1, "pledge");
4808 43012d58 2019-07-14 stsp #endif
4809 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4810 c4296144 2019-05-09 stsp if (cwd == NULL) {
4811 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4812 c4296144 2019-05-09 stsp goto done;
4813 c4296144 2019-05-09 stsp }
4814 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4815 7d5807f4 2019-07-11 stsp if (error)
4816 7d5807f4 2019-07-11 stsp goto done;
4817 7d5807f4 2019-07-11 stsp
4818 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4819 c4296144 2019-05-09 stsp if (error)
4820 a698f62e 2019-07-25 stsp goto done;
4821 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4822 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4823 c4296144 2019-05-09 stsp goto done;
4824 a698f62e 2019-07-25 stsp }
4825 5c1e53bc 2019-07-28 stsp
4826 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4827 916f288c 2019-07-30 stsp worktree);
4828 5c1e53bc 2019-07-28 stsp if (error)
4829 5c1e53bc 2019-07-28 stsp goto done;
4830 c4296144 2019-05-09 stsp
4831 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4832 c9956ddf 2019-09-08 stsp if (error)
4833 c9956ddf 2019-09-08 stsp goto done;
4834 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4835 c9956ddf 2019-09-08 stsp gitconfig_path);
4836 c4296144 2019-05-09 stsp if (error != NULL)
4837 c4296144 2019-05-09 stsp goto done;
4838 c4296144 2019-05-09 stsp
4839 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4840 aba9c984 2019-09-08 stsp if (error)
4841 aba9c984 2019-09-08 stsp return error;
4842 aba9c984 2019-09-08 stsp
4843 314a6357 2019-05-13 stsp /*
4844 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4845 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4846 314a6357 2019-05-13 stsp */
4847 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4848 314a6357 2019-05-13 stsp error = get_editor(&editor);
4849 314a6357 2019-05-13 stsp else
4850 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4851 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4852 c4296144 2019-05-09 stsp if (error)
4853 c4296144 2019-05-09 stsp goto done;
4854 c4296144 2019-05-09 stsp
4855 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4856 087fb88c 2019-08-04 stsp if (error)
4857 087fb88c 2019-08-04 stsp goto done;
4858 087fb88c 2019-08-04 stsp
4859 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4860 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4861 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4862 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4863 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4864 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4865 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4866 916f288c 2019-07-30 stsp goto done;
4867 916f288c 2019-07-30 stsp }
4868 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4869 916f288c 2019-07-30 stsp }
4870 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4871 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4872 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4873 e0870e44 2019-05-13 stsp if (error) {
4874 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4875 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4876 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4877 c4296144 2019-05-09 stsp goto done;
4878 e0870e44 2019-05-13 stsp }
4879 c4296144 2019-05-09 stsp
4880 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4881 c4296144 2019-05-09 stsp if (error)
4882 c4296144 2019-05-09 stsp goto done;
4883 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4884 c4296144 2019-05-09 stsp done:
4885 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4886 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4887 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4888 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4889 7266f21f 2019-10-21 stsp error == NULL)
4890 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4891 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4892 c4296144 2019-05-09 stsp if (repo)
4893 c4296144 2019-05-09 stsp got_repo_close(repo);
4894 c4296144 2019-05-09 stsp if (worktree)
4895 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4896 c4296144 2019-05-09 stsp free(cwd);
4897 c4296144 2019-05-09 stsp free(id_str);
4898 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4899 e2ba3d07 2019-05-13 stsp free(editor);
4900 aba9c984 2019-09-08 stsp free(author);
4901 234035bc 2019-06-01 stsp return error;
4902 234035bc 2019-06-01 stsp }
4903 234035bc 2019-06-01 stsp
4904 234035bc 2019-06-01 stsp __dead static void
4905 234035bc 2019-06-01 stsp usage_cherrypick(void)
4906 234035bc 2019-06-01 stsp {
4907 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4908 234035bc 2019-06-01 stsp exit(1);
4909 234035bc 2019-06-01 stsp }
4910 234035bc 2019-06-01 stsp
4911 234035bc 2019-06-01 stsp static const struct got_error *
4912 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4913 234035bc 2019-06-01 stsp {
4914 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4915 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4916 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4917 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4918 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4919 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4920 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4921 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4922 234035bc 2019-06-01 stsp int ch, did_something = 0;
4923 234035bc 2019-06-01 stsp
4924 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4925 234035bc 2019-06-01 stsp switch (ch) {
4926 234035bc 2019-06-01 stsp default:
4927 234035bc 2019-06-01 stsp usage_cherrypick();
4928 234035bc 2019-06-01 stsp /* NOTREACHED */
4929 234035bc 2019-06-01 stsp }
4930 234035bc 2019-06-01 stsp }
4931 234035bc 2019-06-01 stsp
4932 234035bc 2019-06-01 stsp argc -= optind;
4933 234035bc 2019-06-01 stsp argv += optind;
4934 234035bc 2019-06-01 stsp
4935 43012d58 2019-07-14 stsp #ifndef PROFILE
4936 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4937 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4938 43012d58 2019-07-14 stsp err(1, "pledge");
4939 43012d58 2019-07-14 stsp #endif
4940 234035bc 2019-06-01 stsp if (argc != 1)
4941 234035bc 2019-06-01 stsp usage_cherrypick();
4942 234035bc 2019-06-01 stsp
4943 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4944 234035bc 2019-06-01 stsp if (cwd == NULL) {
4945 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4946 234035bc 2019-06-01 stsp goto done;
4947 234035bc 2019-06-01 stsp }
4948 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4949 234035bc 2019-06-01 stsp if (error)
4950 234035bc 2019-06-01 stsp goto done;
4951 234035bc 2019-06-01 stsp
4952 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4953 c9956ddf 2019-09-08 stsp NULL);
4954 234035bc 2019-06-01 stsp if (error != NULL)
4955 234035bc 2019-06-01 stsp goto done;
4956 234035bc 2019-06-01 stsp
4957 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4958 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4959 234035bc 2019-06-01 stsp if (error)
4960 234035bc 2019-06-01 stsp goto done;
4961 234035bc 2019-06-01 stsp
4962 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4963 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4964 234035bc 2019-06-01 stsp if (error != NULL) {
4965 234035bc 2019-06-01 stsp struct got_reference *ref;
4966 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4967 234035bc 2019-06-01 stsp goto done;
4968 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4969 234035bc 2019-06-01 stsp if (error != NULL)
4970 234035bc 2019-06-01 stsp goto done;
4971 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4972 234035bc 2019-06-01 stsp got_ref_close(ref);
4973 234035bc 2019-06-01 stsp if (error != NULL)
4974 234035bc 2019-06-01 stsp goto done;
4975 234035bc 2019-06-01 stsp }
4976 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4977 234035bc 2019-06-01 stsp if (error)
4978 234035bc 2019-06-01 stsp goto done;
4979 234035bc 2019-06-01 stsp
4980 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4981 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4982 234035bc 2019-06-01 stsp if (error != NULL)
4983 234035bc 2019-06-01 stsp goto done;
4984 234035bc 2019-06-01 stsp
4985 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4986 234035bc 2019-06-01 stsp if (error) {
4987 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4988 234035bc 2019-06-01 stsp goto done;
4989 234035bc 2019-06-01 stsp error = NULL;
4990 234035bc 2019-06-01 stsp } else {
4991 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4992 234035bc 2019-06-01 stsp goto done;
4993 234035bc 2019-06-01 stsp }
4994 234035bc 2019-06-01 stsp
4995 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4996 234035bc 2019-06-01 stsp if (error)
4997 234035bc 2019-06-01 stsp goto done;
4998 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4999 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
5000 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
5001 03415a1a 2019-06-02 stsp NULL);
5002 234035bc 2019-06-01 stsp if (error != NULL)
5003 234035bc 2019-06-01 stsp goto done;
5004 234035bc 2019-06-01 stsp
5005 234035bc 2019-06-01 stsp if (did_something)
5006 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
5007 234035bc 2019-06-01 stsp done:
5008 234035bc 2019-06-01 stsp if (commit)
5009 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5010 234035bc 2019-06-01 stsp free(commit_id_str);
5011 234035bc 2019-06-01 stsp if (head_ref)
5012 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5013 234035bc 2019-06-01 stsp if (worktree)
5014 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5015 234035bc 2019-06-01 stsp if (repo)
5016 234035bc 2019-06-01 stsp got_repo_close(repo);
5017 c4296144 2019-05-09 stsp return error;
5018 c4296144 2019-05-09 stsp }
5019 5ef14e63 2019-06-02 stsp
5020 5ef14e63 2019-06-02 stsp __dead static void
5021 5ef14e63 2019-06-02 stsp usage_backout(void)
5022 5ef14e63 2019-06-02 stsp {
5023 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5024 5ef14e63 2019-06-02 stsp exit(1);
5025 5ef14e63 2019-06-02 stsp }
5026 5ef14e63 2019-06-02 stsp
5027 5ef14e63 2019-06-02 stsp static const struct got_error *
5028 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5029 5ef14e63 2019-06-02 stsp {
5030 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5031 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5032 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5033 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5034 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5035 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5036 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5037 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5038 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5039 5ef14e63 2019-06-02 stsp
5040 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5041 5ef14e63 2019-06-02 stsp switch (ch) {
5042 5ef14e63 2019-06-02 stsp default:
5043 5ef14e63 2019-06-02 stsp usage_backout();
5044 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5045 5ef14e63 2019-06-02 stsp }
5046 5ef14e63 2019-06-02 stsp }
5047 5ef14e63 2019-06-02 stsp
5048 5ef14e63 2019-06-02 stsp argc -= optind;
5049 5ef14e63 2019-06-02 stsp argv += optind;
5050 5ef14e63 2019-06-02 stsp
5051 43012d58 2019-07-14 stsp #ifndef PROFILE
5052 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5053 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5054 43012d58 2019-07-14 stsp err(1, "pledge");
5055 43012d58 2019-07-14 stsp #endif
5056 5ef14e63 2019-06-02 stsp if (argc != 1)
5057 5ef14e63 2019-06-02 stsp usage_backout();
5058 5ef14e63 2019-06-02 stsp
5059 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5060 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5061 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5062 5ef14e63 2019-06-02 stsp goto done;
5063 5ef14e63 2019-06-02 stsp }
5064 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5065 5ef14e63 2019-06-02 stsp if (error)
5066 5ef14e63 2019-06-02 stsp goto done;
5067 5ef14e63 2019-06-02 stsp
5068 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5069 c9956ddf 2019-09-08 stsp NULL);
5070 5ef14e63 2019-06-02 stsp if (error != NULL)
5071 5ef14e63 2019-06-02 stsp goto done;
5072 5ef14e63 2019-06-02 stsp
5073 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5074 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5075 5ef14e63 2019-06-02 stsp if (error)
5076 5ef14e63 2019-06-02 stsp goto done;
5077 5ef14e63 2019-06-02 stsp
5078 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5079 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5080 5ef14e63 2019-06-02 stsp if (error != NULL) {
5081 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5082 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5083 5ef14e63 2019-06-02 stsp goto done;
5084 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5085 5ef14e63 2019-06-02 stsp if (error != NULL)
5086 5ef14e63 2019-06-02 stsp goto done;
5087 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5088 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5089 5ef14e63 2019-06-02 stsp if (error != NULL)
5090 5ef14e63 2019-06-02 stsp goto done;
5091 5ef14e63 2019-06-02 stsp }
5092 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5093 5ef14e63 2019-06-02 stsp if (error)
5094 5ef14e63 2019-06-02 stsp goto done;
5095 5ef14e63 2019-06-02 stsp
5096 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5097 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5098 5ef14e63 2019-06-02 stsp if (error != NULL)
5099 5ef14e63 2019-06-02 stsp goto done;
5100 5ef14e63 2019-06-02 stsp
5101 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5102 5ef14e63 2019-06-02 stsp if (error)
5103 5ef14e63 2019-06-02 stsp goto done;
5104 5ef14e63 2019-06-02 stsp
5105 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5106 5ef14e63 2019-06-02 stsp if (error)
5107 5ef14e63 2019-06-02 stsp goto done;
5108 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5109 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5110 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5111 5ef14e63 2019-06-02 stsp goto done;
5112 5ef14e63 2019-06-02 stsp }
5113 5ef14e63 2019-06-02 stsp
5114 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5115 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5116 5ef14e63 2019-06-02 stsp if (error != NULL)
5117 5ef14e63 2019-06-02 stsp goto done;
5118 5ef14e63 2019-06-02 stsp
5119 5ef14e63 2019-06-02 stsp if (did_something)
5120 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5121 5ef14e63 2019-06-02 stsp done:
5122 5ef14e63 2019-06-02 stsp if (commit)
5123 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5124 5ef14e63 2019-06-02 stsp free(commit_id_str);
5125 5ef14e63 2019-06-02 stsp if (head_ref)
5126 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5127 818c7501 2019-07-11 stsp if (worktree)
5128 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5129 818c7501 2019-07-11 stsp if (repo)
5130 818c7501 2019-07-11 stsp got_repo_close(repo);
5131 818c7501 2019-07-11 stsp return error;
5132 818c7501 2019-07-11 stsp }
5133 818c7501 2019-07-11 stsp
5134 818c7501 2019-07-11 stsp __dead static void
5135 818c7501 2019-07-11 stsp usage_rebase(void)
5136 818c7501 2019-07-11 stsp {
5137 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5138 af54c8f8 2019-07-11 stsp getprogname());
5139 818c7501 2019-07-11 stsp exit(1);
5140 0ebf8283 2019-07-24 stsp }
5141 0ebf8283 2019-07-24 stsp
5142 0ebf8283 2019-07-24 stsp void
5143 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5144 0ebf8283 2019-07-24 stsp {
5145 0ebf8283 2019-07-24 stsp char *nl;
5146 0ebf8283 2019-07-24 stsp size_t len;
5147 0ebf8283 2019-07-24 stsp
5148 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5149 0ebf8283 2019-07-24 stsp if (len > limit)
5150 0ebf8283 2019-07-24 stsp len = limit;
5151 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5152 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5153 0ebf8283 2019-07-24 stsp if (nl)
5154 0ebf8283 2019-07-24 stsp *nl = '\0';
5155 0ebf8283 2019-07-24 stsp }
5156 0ebf8283 2019-07-24 stsp
5157 0ebf8283 2019-07-24 stsp static const struct got_error *
5158 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5159 0ebf8283 2019-07-24 stsp {
5160 5943eee2 2019-08-13 stsp const struct got_error *err;
5161 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5162 5943eee2 2019-08-13 stsp const char *s;
5163 0ebf8283 2019-07-24 stsp
5164 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5165 5943eee2 2019-08-13 stsp if (err)
5166 5943eee2 2019-08-13 stsp return err;
5167 0ebf8283 2019-07-24 stsp
5168 5943eee2 2019-08-13 stsp s = logmsg0;
5169 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5170 5943eee2 2019-08-13 stsp s++;
5171 5943eee2 2019-08-13 stsp
5172 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5173 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5174 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5175 5943eee2 2019-08-13 stsp goto done;
5176 5943eee2 2019-08-13 stsp }
5177 0ebf8283 2019-07-24 stsp
5178 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5179 5943eee2 2019-08-13 stsp done:
5180 5943eee2 2019-08-13 stsp free(logmsg0);
5181 5943eee2 2019-08-13 stsp return err;
5182 818c7501 2019-07-11 stsp }
5183 818c7501 2019-07-11 stsp
5184 818c7501 2019-07-11 stsp static const struct got_error *
5185 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5186 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5187 818c7501 2019-07-11 stsp {
5188 818c7501 2019-07-11 stsp const struct got_error *err;
5189 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5190 818c7501 2019-07-11 stsp
5191 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5192 818c7501 2019-07-11 stsp if (err)
5193 818c7501 2019-07-11 stsp goto done;
5194 818c7501 2019-07-11 stsp
5195 ff0d2220 2019-07-11 stsp if (new_id) {
5196 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5197 ff0d2220 2019-07-11 stsp if (err)
5198 ff0d2220 2019-07-11 stsp goto done;
5199 ff0d2220 2019-07-11 stsp }
5200 818c7501 2019-07-11 stsp
5201 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5202 ff0d2220 2019-07-11 stsp if (new_id_str)
5203 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5204 0ebf8283 2019-07-24 stsp
5205 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5206 0ebf8283 2019-07-24 stsp if (err)
5207 0ebf8283 2019-07-24 stsp goto done;
5208 0ebf8283 2019-07-24 stsp
5209 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5210 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5211 818c7501 2019-07-11 stsp done:
5212 818c7501 2019-07-11 stsp free(old_id_str);
5213 818c7501 2019-07-11 stsp free(new_id_str);
5214 818c7501 2019-07-11 stsp return err;
5215 818c7501 2019-07-11 stsp }
5216 818c7501 2019-07-11 stsp
5217 1ee397ad 2019-07-12 stsp static const struct got_error *
5218 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5219 818c7501 2019-07-11 stsp {
5220 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5221 818c7501 2019-07-11 stsp
5222 818c7501 2019-07-11 stsp while (path[0] == '/')
5223 818c7501 2019-07-11 stsp path++;
5224 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5225 818c7501 2019-07-11 stsp
5226 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5227 1ee397ad 2019-07-12 stsp return NULL;
5228 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5229 818c7501 2019-07-11 stsp *rebase_status = status;
5230 1ee397ad 2019-07-12 stsp return NULL;
5231 818c7501 2019-07-11 stsp }
5232 818c7501 2019-07-11 stsp
5233 818c7501 2019-07-11 stsp static const struct got_error *
5234 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5235 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5236 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5237 818c7501 2019-07-11 stsp {
5238 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5239 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5240 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5241 818c7501 2019-07-11 stsp }
5242 818c7501 2019-07-11 stsp
5243 818c7501 2019-07-11 stsp static const struct got_error *
5244 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5245 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5246 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5247 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5248 ff0d2220 2019-07-11 stsp {
5249 ff0d2220 2019-07-11 stsp const struct got_error *error;
5250 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5251 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5252 ff0d2220 2019-07-11 stsp
5253 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5254 ff0d2220 2019-07-11 stsp if (error)
5255 ff0d2220 2019-07-11 stsp return error;
5256 ff0d2220 2019-07-11 stsp
5257 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5258 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5259 ff0d2220 2019-07-11 stsp if (error) {
5260 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5261 ff0d2220 2019-07-11 stsp goto done;
5262 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5263 ff0d2220 2019-07-11 stsp } else {
5264 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5265 ff0d2220 2019-07-11 stsp free(new_commit_id);
5266 ff0d2220 2019-07-11 stsp }
5267 ff0d2220 2019-07-11 stsp done:
5268 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5269 ff0d2220 2019-07-11 stsp return error;
5270 64c6d990 2019-07-11 stsp }
5271 64c6d990 2019-07-11 stsp
5272 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5273 64c6d990 2019-07-11 stsp const char *path_prefix;
5274 64c6d990 2019-07-11 stsp size_t len;
5275 8ca9bd68 2019-07-25 stsp int errcode;
5276 64c6d990 2019-07-11 stsp };
5277 64c6d990 2019-07-11 stsp
5278 64c6d990 2019-07-11 stsp static const struct got_error *
5279 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5280 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5281 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5282 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5283 64c6d990 2019-07-11 stsp {
5284 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5285 64c6d990 2019-07-11 stsp
5286 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5287 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5288 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5289 64c6d990 2019-07-11 stsp
5290 64c6d990 2019-07-11 stsp return NULL;
5291 64c6d990 2019-07-11 stsp }
5292 64c6d990 2019-07-11 stsp
5293 64c6d990 2019-07-11 stsp static const struct got_error *
5294 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5295 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5296 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5297 64c6d990 2019-07-11 stsp {
5298 64c6d990 2019-07-11 stsp const struct got_error *err;
5299 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5300 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5301 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5302 64c6d990 2019-07-11 stsp
5303 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5304 64c6d990 2019-07-11 stsp return NULL;
5305 64c6d990 2019-07-11 stsp
5306 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5307 64c6d990 2019-07-11 stsp if (err)
5308 64c6d990 2019-07-11 stsp goto done;
5309 64c6d990 2019-07-11 stsp
5310 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5311 64c6d990 2019-07-11 stsp if (err)
5312 64c6d990 2019-07-11 stsp goto done;
5313 64c6d990 2019-07-11 stsp
5314 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5315 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5316 64c6d990 2019-07-11 stsp if (err)
5317 64c6d990 2019-07-11 stsp goto done;
5318 64c6d990 2019-07-11 stsp
5319 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5320 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5321 64c6d990 2019-07-11 stsp if (err)
5322 64c6d990 2019-07-11 stsp goto done;
5323 64c6d990 2019-07-11 stsp
5324 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5325 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5326 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5327 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5328 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5329 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5330 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5331 64c6d990 2019-07-11 stsp done:
5332 64c6d990 2019-07-11 stsp if (tree1)
5333 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5334 64c6d990 2019-07-11 stsp if (tree2)
5335 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5336 64c6d990 2019-07-11 stsp if (commit)
5337 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5338 64c6d990 2019-07-11 stsp if (parent_commit)
5339 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5340 64c6d990 2019-07-11 stsp return err;
5341 ff0d2220 2019-07-11 stsp }
5342 ff0d2220 2019-07-11 stsp
5343 ff0d2220 2019-07-11 stsp static const struct got_error *
5344 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5345 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5346 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5347 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5348 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5349 af61c510 2019-07-19 stsp {
5350 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5351 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5352 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5353 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5354 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5355 af61c510 2019-07-19 stsp
5356 af61c510 2019-07-19 stsp err = got_commit_graph_open(&graph, initial_commit_id, "/", 1, repo);
5357 af61c510 2019-07-19 stsp if (err)
5358 af61c510 2019-07-19 stsp return err;
5359 af61c510 2019-07-19 stsp
5360 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5361 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5362 af61c510 2019-07-19 stsp if (err)
5363 af61c510 2019-07-19 stsp goto done;
5364 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5365 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5366 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5367 af61c510 2019-07-19 stsp if (err) {
5368 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5369 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5370 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5371 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5372 af61c510 2019-07-19 stsp "been reached?!?");
5373 ee780d5c 2020-01-04 stsp }
5374 ee780d5c 2020-01-04 stsp goto done;
5375 af61c510 2019-07-19 stsp } else {
5376 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5377 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5378 af61c510 2019-07-19 stsp if (err)
5379 af61c510 2019-07-19 stsp goto done;
5380 af61c510 2019-07-19 stsp
5381 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5382 af61c510 2019-07-19 stsp if (err)
5383 af61c510 2019-07-19 stsp goto done;
5384 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5385 af61c510 2019-07-19 stsp commit_id = parent_id;
5386 af61c510 2019-07-19 stsp }
5387 af61c510 2019-07-19 stsp }
5388 af61c510 2019-07-19 stsp done:
5389 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5390 af61c510 2019-07-19 stsp return err;
5391 af61c510 2019-07-19 stsp }
5392 af61c510 2019-07-19 stsp
5393 af61c510 2019-07-19 stsp static const struct got_error *
5394 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5395 818c7501 2019-07-11 stsp {
5396 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5397 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5398 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5399 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5400 818c7501 2019-07-11 stsp char *cwd = NULL;
5401 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5402 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5403 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5404 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5405 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5406 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5407 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5408 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5409 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5410 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5411 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5412 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5413 818c7501 2019-07-11 stsp
5414 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5415 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5416 818c7501 2019-07-11 stsp
5417 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5418 818c7501 2019-07-11 stsp switch (ch) {
5419 818c7501 2019-07-11 stsp case 'a':
5420 818c7501 2019-07-11 stsp abort_rebase = 1;
5421 818c7501 2019-07-11 stsp break;
5422 818c7501 2019-07-11 stsp case 'c':
5423 818c7501 2019-07-11 stsp continue_rebase = 1;
5424 818c7501 2019-07-11 stsp break;
5425 818c7501 2019-07-11 stsp default:
5426 818c7501 2019-07-11 stsp usage_rebase();
5427 818c7501 2019-07-11 stsp /* NOTREACHED */
5428 818c7501 2019-07-11 stsp }
5429 818c7501 2019-07-11 stsp }
5430 818c7501 2019-07-11 stsp
5431 818c7501 2019-07-11 stsp argc -= optind;
5432 818c7501 2019-07-11 stsp argv += optind;
5433 818c7501 2019-07-11 stsp
5434 43012d58 2019-07-14 stsp #ifndef PROFILE
5435 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5436 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5437 43012d58 2019-07-14 stsp err(1, "pledge");
5438 43012d58 2019-07-14 stsp #endif
5439 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5440 818c7501 2019-07-11 stsp usage_rebase();
5441 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5442 818c7501 2019-07-11 stsp if (argc != 0)
5443 818c7501 2019-07-11 stsp usage_rebase();
5444 818c7501 2019-07-11 stsp } else if (argc != 1)
5445 818c7501 2019-07-11 stsp usage_rebase();
5446 818c7501 2019-07-11 stsp
5447 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5448 818c7501 2019-07-11 stsp if (cwd == NULL) {
5449 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5450 818c7501 2019-07-11 stsp goto done;
5451 818c7501 2019-07-11 stsp }
5452 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5453 818c7501 2019-07-11 stsp if (error)
5454 818c7501 2019-07-11 stsp goto done;
5455 818c7501 2019-07-11 stsp
5456 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5457 c9956ddf 2019-09-08 stsp NULL);
5458 818c7501 2019-07-11 stsp if (error != NULL)
5459 818c7501 2019-07-11 stsp goto done;
5460 818c7501 2019-07-11 stsp
5461 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5462 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5463 818c7501 2019-07-11 stsp if (error)
5464 818c7501 2019-07-11 stsp goto done;
5465 818c7501 2019-07-11 stsp
5466 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5467 818c7501 2019-07-11 stsp if (error)
5468 818c7501 2019-07-11 stsp goto done;
5469 818c7501 2019-07-11 stsp
5470 f6794adc 2019-07-23 stsp if (abort_rebase) {
5471 818c7501 2019-07-11 stsp int did_something;
5472 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5473 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5474 f6794adc 2019-07-23 stsp goto done;
5475 f6794adc 2019-07-23 stsp }
5476 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5477 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5478 3e3a69f1 2019-07-25 stsp worktree, repo);
5479 818c7501 2019-07-11 stsp if (error)
5480 818c7501 2019-07-11 stsp goto done;
5481 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5482 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5483 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5484 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5485 818c7501 2019-07-11 stsp if (error)
5486 818c7501 2019-07-11 stsp goto done;
5487 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5488 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5489 818c7501 2019-07-11 stsp }
5490 818c7501 2019-07-11 stsp
5491 818c7501 2019-07-11 stsp if (continue_rebase) {
5492 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5493 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5494 f6794adc 2019-07-23 stsp goto done;
5495 f6794adc 2019-07-23 stsp }
5496 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5497 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5498 3e3a69f1 2019-07-25 stsp worktree, repo);
5499 818c7501 2019-07-11 stsp if (error)
5500 818c7501 2019-07-11 stsp goto done;
5501 818c7501 2019-07-11 stsp
5502 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5503 01757395 2019-07-12 stsp resume_commit_id, repo);
5504 818c7501 2019-07-11 stsp if (error)
5505 818c7501 2019-07-11 stsp goto done;
5506 818c7501 2019-07-11 stsp
5507 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5508 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5509 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5510 818c7501 2019-07-11 stsp goto done;
5511 818c7501 2019-07-11 stsp }
5512 818c7501 2019-07-11 stsp } else {
5513 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5514 818c7501 2019-07-11 stsp if (error != NULL)
5515 818c7501 2019-07-11 stsp goto done;
5516 ff0d2220 2019-07-11 stsp }
5517 818c7501 2019-07-11 stsp
5518 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5519 ff0d2220 2019-07-11 stsp if (error)
5520 ff0d2220 2019-07-11 stsp goto done;
5521 ff0d2220 2019-07-11 stsp
5522 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5523 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5524 a51a74b3 2019-07-27 stsp
5525 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5526 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5527 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5528 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5529 818c7501 2019-07-11 stsp if (error)
5530 818c7501 2019-07-11 stsp goto done;
5531 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5532 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5533 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5534 818c7501 2019-07-11 stsp "with work tree's branch");
5535 818c7501 2019-07-11 stsp goto done;
5536 818c7501 2019-07-11 stsp }
5537 818c7501 2019-07-11 stsp
5538 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5539 a51a74b3 2019-07-27 stsp if (error) {
5540 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5541 a51a74b3 2019-07-27 stsp goto done;
5542 a51a74b3 2019-07-27 stsp error = NULL;
5543 a51a74b3 2019-07-27 stsp } else {
5544 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5545 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5546 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5547 a51a74b3 2019-07-27 stsp goto done;
5548 a51a74b3 2019-07-27 stsp }
5549 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5550 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5551 818c7501 2019-07-11 stsp if (error)
5552 818c7501 2019-07-11 stsp goto done;
5553 818c7501 2019-07-11 stsp }
5554 818c7501 2019-07-11 stsp
5555 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5556 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5557 818c7501 2019-07-11 stsp if (error)
5558 818c7501 2019-07-11 stsp goto done;
5559 818c7501 2019-07-11 stsp
5560 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5561 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5562 fc66b545 2019-08-12 stsp if (pid == NULL) {
5563 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5564 fc66b545 2019-08-12 stsp int did_something;
5565 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5566 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5567 fc66b545 2019-08-12 stsp &did_something);
5568 fc66b545 2019-08-12 stsp if (error)
5569 fc66b545 2019-08-12 stsp goto done;
5570 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5571 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5572 fc66b545 2019-08-12 stsp }
5573 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5574 fc66b545 2019-08-12 stsp goto done;
5575 fc66b545 2019-08-12 stsp }
5576 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5577 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5578 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5579 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5580 818c7501 2019-07-11 stsp commit = NULL;
5581 818c7501 2019-07-11 stsp if (error)
5582 818c7501 2019-07-11 stsp goto done;
5583 64c6d990 2019-07-11 stsp
5584 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5585 38b0338b 2019-11-29 stsp if (continue_rebase) {
5586 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5587 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5588 38b0338b 2019-11-29 stsp goto done;
5589 38b0338b 2019-11-29 stsp } else {
5590 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5591 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5592 38b0338b 2019-11-29 stsp char *id_str;
5593 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5594 38b0338b 2019-11-29 stsp new_base_branch);
5595 38b0338b 2019-11-29 stsp if (error)
5596 38b0338b 2019-11-29 stsp goto done;
5597 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5598 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5599 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5600 38b0338b 2019-11-29 stsp free(id_str);
5601 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5602 38b0338b 2019-11-29 stsp new_head_commit_id);
5603 38b0338b 2019-11-29 stsp if (error)
5604 38b0338b 2019-11-29 stsp goto done;
5605 38b0338b 2019-11-29 stsp }
5606 818c7501 2019-07-11 stsp }
5607 818c7501 2019-07-11 stsp
5608 818c7501 2019-07-11 stsp pid = NULL;
5609 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5610 818c7501 2019-07-11 stsp commit_id = qid->id;
5611 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5612 818c7501 2019-07-11 stsp pid = qid;
5613 818c7501 2019-07-11 stsp
5614 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5615 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5616 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5617 818c7501 2019-07-11 stsp if (error)
5618 818c7501 2019-07-11 stsp goto done;
5619 818c7501 2019-07-11 stsp
5620 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5621 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5622 818c7501 2019-07-11 stsp break;
5623 01757395 2019-07-12 stsp }
5624 818c7501 2019-07-11 stsp
5625 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5626 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5627 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5628 818c7501 2019-07-11 stsp if (error)
5629 818c7501 2019-07-11 stsp goto done;
5630 818c7501 2019-07-11 stsp }
5631 818c7501 2019-07-11 stsp
5632 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5633 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5634 818c7501 2019-07-11 stsp if (error)
5635 818c7501 2019-07-11 stsp goto done;
5636 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5637 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5638 818c7501 2019-07-11 stsp } else
5639 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5640 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5641 818c7501 2019-07-11 stsp done:
5642 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5643 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5644 818c7501 2019-07-11 stsp free(resume_commit_id);
5645 818c7501 2019-07-11 stsp free(yca_id);
5646 818c7501 2019-07-11 stsp if (commit)
5647 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5648 818c7501 2019-07-11 stsp if (branch)
5649 818c7501 2019-07-11 stsp got_ref_close(branch);
5650 818c7501 2019-07-11 stsp if (new_base_branch)
5651 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5652 818c7501 2019-07-11 stsp if (tmp_branch)
5653 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5654 5ef14e63 2019-06-02 stsp if (worktree)
5655 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5656 5ef14e63 2019-06-02 stsp if (repo)
5657 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5658 5ef14e63 2019-06-02 stsp return error;
5659 0ebf8283 2019-07-24 stsp }
5660 0ebf8283 2019-07-24 stsp
5661 0ebf8283 2019-07-24 stsp __dead static void
5662 0ebf8283 2019-07-24 stsp usage_histedit(void)
5663 0ebf8283 2019-07-24 stsp {
5664 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5665 0ebf8283 2019-07-24 stsp getprogname());
5666 0ebf8283 2019-07-24 stsp exit(1);
5667 0ebf8283 2019-07-24 stsp }
5668 0ebf8283 2019-07-24 stsp
5669 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5670 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5671 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5672 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5673 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5674 0ebf8283 2019-07-24 stsp
5675 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5676 0ebf8283 2019-07-24 stsp unsigned char code;
5677 0ebf8283 2019-07-24 stsp const char *name;
5678 0ebf8283 2019-07-24 stsp const char *desc;
5679 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5680 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5681 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5682 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5683 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5684 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5685 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5686 0ebf8283 2019-07-24 stsp };
5687 0ebf8283 2019-07-24 stsp
5688 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5689 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5690 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5691 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5692 0ebf8283 2019-07-24 stsp char *logmsg;
5693 0ebf8283 2019-07-24 stsp };
5694 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5695 0ebf8283 2019-07-24 stsp
5696 0ebf8283 2019-07-24 stsp static const struct got_error *
5697 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5698 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5699 0ebf8283 2019-07-24 stsp {
5700 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5701 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5702 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5703 8138f3e1 2019-08-11 stsp int n;
5704 0ebf8283 2019-07-24 stsp
5705 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5706 0ebf8283 2019-07-24 stsp if (err)
5707 0ebf8283 2019-07-24 stsp goto done;
5708 0ebf8283 2019-07-24 stsp
5709 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5710 0ebf8283 2019-07-24 stsp if (err)
5711 0ebf8283 2019-07-24 stsp goto done;
5712 0ebf8283 2019-07-24 stsp
5713 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5714 0ebf8283 2019-07-24 stsp if (err)
5715 0ebf8283 2019-07-24 stsp goto done;
5716 0ebf8283 2019-07-24 stsp
5717 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5718 0ebf8283 2019-07-24 stsp if (n < 0)
5719 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5720 0ebf8283 2019-07-24 stsp done:
5721 0ebf8283 2019-07-24 stsp if (commit)
5722 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5723 0ebf8283 2019-07-24 stsp free(id_str);
5724 0ebf8283 2019-07-24 stsp free(logmsg);
5725 0ebf8283 2019-07-24 stsp return err;
5726 0ebf8283 2019-07-24 stsp }
5727 0ebf8283 2019-07-24 stsp
5728 0ebf8283 2019-07-24 stsp static const struct got_error *
5729 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5730 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5731 0ebf8283 2019-07-24 stsp {
5732 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5733 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5734 0ebf8283 2019-07-24 stsp
5735 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5736 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5737 0ebf8283 2019-07-24 stsp
5738 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5739 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5740 0ebf8283 2019-07-24 stsp f, repo);
5741 0ebf8283 2019-07-24 stsp if (err)
5742 0ebf8283 2019-07-24 stsp break;
5743 0ebf8283 2019-07-24 stsp }
5744 0ebf8283 2019-07-24 stsp
5745 0ebf8283 2019-07-24 stsp return err;
5746 0ebf8283 2019-07-24 stsp }
5747 0ebf8283 2019-07-24 stsp
5748 0ebf8283 2019-07-24 stsp static const struct got_error *
5749 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5750 0ebf8283 2019-07-24 stsp {
5751 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5752 0ebf8283 2019-07-24 stsp int n, i;
5753 0ebf8283 2019-07-24 stsp
5754 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5755 0ebf8283 2019-07-24 stsp if (n < 0)
5756 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5757 0ebf8283 2019-07-24 stsp
5758 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5759 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5760 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5761 0ebf8283 2019-07-24 stsp cmd->desc);
5762 0ebf8283 2019-07-24 stsp if (n < 0) {
5763 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5764 0ebf8283 2019-07-24 stsp break;
5765 0ebf8283 2019-07-24 stsp }
5766 0ebf8283 2019-07-24 stsp }
5767 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5768 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5769 0ebf8283 2019-07-24 stsp if (n < 0)
5770 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5771 0ebf8283 2019-07-24 stsp return err;
5772 0ebf8283 2019-07-24 stsp }
5773 0ebf8283 2019-07-24 stsp
5774 0ebf8283 2019-07-24 stsp static const struct got_error *
5775 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5776 0ebf8283 2019-07-24 stsp {
5777 0ebf8283 2019-07-24 stsp static char msg[42];
5778 0ebf8283 2019-07-24 stsp int ret;
5779 0ebf8283 2019-07-24 stsp
5780 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5781 0ebf8283 2019-07-24 stsp lineno);
5782 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5783 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5784 0ebf8283 2019-07-24 stsp
5785 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5786 0ebf8283 2019-07-24 stsp }
5787 0ebf8283 2019-07-24 stsp
5788 0ebf8283 2019-07-24 stsp static const struct got_error *
5789 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5790 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5791 0ebf8283 2019-07-24 stsp {
5792 0ebf8283 2019-07-24 stsp const struct got_error *err;
5793 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5794 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5795 0ebf8283 2019-07-24 stsp
5796 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5797 0ebf8283 2019-07-24 stsp if (err)
5798 0ebf8283 2019-07-24 stsp return err;
5799 0ebf8283 2019-07-24 stsp
5800 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5801 0ebf8283 2019-07-24 stsp if (err)
5802 0ebf8283 2019-07-24 stsp goto done;
5803 0ebf8283 2019-07-24 stsp
5804 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5805 5943eee2 2019-08-13 stsp if (err)
5806 5943eee2 2019-08-13 stsp goto done;
5807 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5808 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5809 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5810 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5811 0ebf8283 2019-07-24 stsp goto done;
5812 0ebf8283 2019-07-24 stsp }
5813 0ebf8283 2019-07-24 stsp done:
5814 0ebf8283 2019-07-24 stsp if (folded_commit)
5815 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5816 0ebf8283 2019-07-24 stsp free(id_str);
5817 5943eee2 2019-08-13 stsp free(folded_logmsg);
5818 0ebf8283 2019-07-24 stsp return err;
5819 0ebf8283 2019-07-24 stsp }
5820 0ebf8283 2019-07-24 stsp
5821 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5822 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5823 0ebf8283 2019-07-24 stsp {
5824 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5825 0ebf8283 2019-07-24 stsp
5826 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5827 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5828 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5829 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5830 3f9de99f 2019-07-24 stsp folded = prev;
5831 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5832 0ebf8283 2019-07-24 stsp }
5833 0ebf8283 2019-07-24 stsp
5834 0ebf8283 2019-07-24 stsp return folded;
5835 0ebf8283 2019-07-24 stsp }
5836 0ebf8283 2019-07-24 stsp
5837 0ebf8283 2019-07-24 stsp static const struct got_error *
5838 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5839 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5840 0ebf8283 2019-07-24 stsp {
5841 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5842 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5843 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5844 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5845 0ebf8283 2019-07-24 stsp int fd;
5846 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5847 0ebf8283 2019-07-24 stsp
5848 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5849 0ebf8283 2019-07-24 stsp if (err)
5850 0ebf8283 2019-07-24 stsp return err;
5851 0ebf8283 2019-07-24 stsp
5852 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5853 0ebf8283 2019-07-24 stsp if (folded) {
5854 0ebf8283 2019-07-24 stsp while (folded != hle) {
5855 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5856 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5857 3f9de99f 2019-07-24 stsp continue;
5858 3f9de99f 2019-07-24 stsp }
5859 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5860 0ebf8283 2019-07-24 stsp logmsg, repo);
5861 0ebf8283 2019-07-24 stsp if (err)
5862 0ebf8283 2019-07-24 stsp goto done;
5863 0ebf8283 2019-07-24 stsp free(logmsg);
5864 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5865 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5866 0ebf8283 2019-07-24 stsp }
5867 0ebf8283 2019-07-24 stsp }
5868 0ebf8283 2019-07-24 stsp
5869 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5870 0ebf8283 2019-07-24 stsp if (err)
5871 0ebf8283 2019-07-24 stsp goto done;
5872 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5873 5943eee2 2019-08-13 stsp if (err)
5874 5943eee2 2019-08-13 stsp goto done;
5875 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5876 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5877 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5878 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5879 0ebf8283 2019-07-24 stsp goto done;
5880 0ebf8283 2019-07-24 stsp }
5881 0ebf8283 2019-07-24 stsp free(logmsg);
5882 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5883 0ebf8283 2019-07-24 stsp
5884 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5885 0ebf8283 2019-07-24 stsp if (err)
5886 0ebf8283 2019-07-24 stsp goto done;
5887 0ebf8283 2019-07-24 stsp
5888 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5889 0ebf8283 2019-07-24 stsp if (err)
5890 0ebf8283 2019-07-24 stsp goto done;
5891 0ebf8283 2019-07-24 stsp
5892 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5893 0ebf8283 2019-07-24 stsp close(fd);
5894 0ebf8283 2019-07-24 stsp
5895 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5896 0ebf8283 2019-07-24 stsp if (err)
5897 0ebf8283 2019-07-24 stsp goto done;
5898 0ebf8283 2019-07-24 stsp
5899 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5900 0ebf8283 2019-07-24 stsp if (err) {
5901 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5902 0ebf8283 2019-07-24 stsp goto done;
5903 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5904 0ebf8283 2019-07-24 stsp }
5905 0ebf8283 2019-07-24 stsp done:
5906 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5907 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5908 0ebf8283 2019-07-24 stsp free(logmsg_path);
5909 0ebf8283 2019-07-24 stsp free(logmsg);
5910 5943eee2 2019-08-13 stsp free(orig_logmsg);
5911 0ebf8283 2019-07-24 stsp free(editor);
5912 0ebf8283 2019-07-24 stsp if (commit)
5913 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5914 0ebf8283 2019-07-24 stsp return err;
5915 5ef14e63 2019-06-02 stsp }
5916 0ebf8283 2019-07-24 stsp
5917 0ebf8283 2019-07-24 stsp static const struct got_error *
5918 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5919 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5920 0ebf8283 2019-07-24 stsp {
5921 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5922 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5923 0ebf8283 2019-07-24 stsp size_t size;
5924 0ebf8283 2019-07-24 stsp ssize_t len;
5925 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5926 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5927 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5928 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5929 0ebf8283 2019-07-24 stsp
5930 0ebf8283 2019-07-24 stsp for (;;) {
5931 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5932 0ebf8283 2019-07-24 stsp if (len == -1) {
5933 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5934 0ebf8283 2019-07-24 stsp if (feof(f))
5935 0ebf8283 2019-07-24 stsp break;
5936 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5937 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5938 0ebf8283 2019-07-24 stsp break;
5939 0ebf8283 2019-07-24 stsp }
5940 0ebf8283 2019-07-24 stsp lineno++;
5941 0ebf8283 2019-07-24 stsp p = line;
5942 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5943 0ebf8283 2019-07-24 stsp p++;
5944 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5945 0ebf8283 2019-07-24 stsp free(line);
5946 0ebf8283 2019-07-24 stsp line = NULL;
5947 0ebf8283 2019-07-24 stsp continue;
5948 0ebf8283 2019-07-24 stsp }
5949 0ebf8283 2019-07-24 stsp cmd = NULL;
5950 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5951 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5952 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5953 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5954 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5955 0ebf8283 2019-07-24 stsp break;
5956 0ebf8283 2019-07-24 stsp }
5957 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5958 0ebf8283 2019-07-24 stsp p++;
5959 0ebf8283 2019-07-24 stsp break;
5960 0ebf8283 2019-07-24 stsp }
5961 0ebf8283 2019-07-24 stsp }
5962 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5963 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5964 0ebf8283 2019-07-24 stsp break;
5965 0ebf8283 2019-07-24 stsp }
5966 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5967 0ebf8283 2019-07-24 stsp p++;
5968 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5969 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5970 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5971 0ebf8283 2019-07-24 stsp break;
5972 0ebf8283 2019-07-24 stsp }
5973 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5974 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5975 0ebf8283 2019-07-24 stsp if (err)
5976 0ebf8283 2019-07-24 stsp break;
5977 0ebf8283 2019-07-24 stsp } else {
5978 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5979 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5980 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5981 0ebf8283 2019-07-24 stsp break;
5982 0ebf8283 2019-07-24 stsp }
5983 0ebf8283 2019-07-24 stsp }
5984 0ebf8283 2019-07-24 stsp free(line);
5985 0ebf8283 2019-07-24 stsp line = NULL;
5986 0ebf8283 2019-07-24 stsp continue;
5987 0ebf8283 2019-07-24 stsp } else {
5988 0ebf8283 2019-07-24 stsp end = p;
5989 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5990 0ebf8283 2019-07-24 stsp end++;
5991 0ebf8283 2019-07-24 stsp *end = '\0';
5992 0ebf8283 2019-07-24 stsp
5993 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5994 0ebf8283 2019-07-24 stsp if (err) {
5995 0ebf8283 2019-07-24 stsp /* override error code */
5996 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5997 0ebf8283 2019-07-24 stsp break;
5998 0ebf8283 2019-07-24 stsp }
5999 0ebf8283 2019-07-24 stsp }
6000 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6001 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6002 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6003 0ebf8283 2019-07-24 stsp break;
6004 0ebf8283 2019-07-24 stsp }
6005 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6006 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6007 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6008 0ebf8283 2019-07-24 stsp commit_id = NULL;
6009 0ebf8283 2019-07-24 stsp free(line);
6010 0ebf8283 2019-07-24 stsp line = NULL;
6011 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6012 0ebf8283 2019-07-24 stsp }
6013 0ebf8283 2019-07-24 stsp
6014 0ebf8283 2019-07-24 stsp free(line);
6015 0ebf8283 2019-07-24 stsp free(commit_id);
6016 0ebf8283 2019-07-24 stsp return err;
6017 0ebf8283 2019-07-24 stsp }
6018 0ebf8283 2019-07-24 stsp
6019 0ebf8283 2019-07-24 stsp static const struct got_error *
6020 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6021 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6022 bfce7f83 2019-07-27 stsp {
6023 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6024 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6025 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6026 bfce7f83 2019-07-27 stsp static char msg[80];
6027 bfce7f83 2019-07-27 stsp char *id_str;
6028 bfce7f83 2019-07-27 stsp
6029 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6030 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6031 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6032 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6033 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6034 bfce7f83 2019-07-27 stsp
6035 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6036 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6037 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6038 bfce7f83 2019-07-27 stsp break;
6039 bfce7f83 2019-07-27 stsp }
6040 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6041 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6042 bfce7f83 2019-07-27 stsp if (err)
6043 bfce7f83 2019-07-27 stsp return err;
6044 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6045 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6046 bfce7f83 2019-07-27 stsp free(id_str);
6047 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6048 bfce7f83 2019-07-27 stsp }
6049 bfce7f83 2019-07-27 stsp }
6050 bfce7f83 2019-07-27 stsp
6051 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6052 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6053 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6054 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6055 bfce7f83 2019-07-27 stsp
6056 bfce7f83 2019-07-27 stsp return NULL;
6057 bfce7f83 2019-07-27 stsp }
6058 bfce7f83 2019-07-27 stsp
6059 bfce7f83 2019-07-27 stsp static const struct got_error *
6060 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6061 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6062 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6063 0ebf8283 2019-07-24 stsp {
6064 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6065 0ebf8283 2019-07-24 stsp char *editor;
6066 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6067 0ebf8283 2019-07-24 stsp
6068 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6069 0ebf8283 2019-07-24 stsp if (err)
6070 0ebf8283 2019-07-24 stsp return err;
6071 0ebf8283 2019-07-24 stsp
6072 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6073 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6074 0ebf8283 2019-07-24 stsp goto done;
6075 0ebf8283 2019-07-24 stsp }
6076 0ebf8283 2019-07-24 stsp
6077 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6078 0ebf8283 2019-07-24 stsp if (f == NULL) {
6079 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6080 0ebf8283 2019-07-24 stsp goto done;
6081 0ebf8283 2019-07-24 stsp }
6082 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6083 bfce7f83 2019-07-27 stsp if (err)
6084 bfce7f83 2019-07-27 stsp goto done;
6085 bfce7f83 2019-07-27 stsp
6086 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6087 0ebf8283 2019-07-24 stsp done:
6088 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6089 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6090 0ebf8283 2019-07-24 stsp free(editor);
6091 0ebf8283 2019-07-24 stsp return err;
6092 0ebf8283 2019-07-24 stsp }
6093 0ebf8283 2019-07-24 stsp
6094 0ebf8283 2019-07-24 stsp static const struct got_error *
6095 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6096 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
6097 0ebf8283 2019-07-24 stsp
6098 0ebf8283 2019-07-24 stsp static const struct got_error *
6099 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6100 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6101 0ebf8283 2019-07-24 stsp {
6102 0ebf8283 2019-07-24 stsp const struct got_error *err;
6103 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6104 0ebf8283 2019-07-24 stsp char *path = NULL;
6105 0ebf8283 2019-07-24 stsp
6106 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6107 0ebf8283 2019-07-24 stsp if (err)
6108 0ebf8283 2019-07-24 stsp return err;
6109 0ebf8283 2019-07-24 stsp
6110 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
6111 0ebf8283 2019-07-24 stsp if (err)
6112 0ebf8283 2019-07-24 stsp goto done;
6113 0ebf8283 2019-07-24 stsp
6114 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
6115 0ebf8283 2019-07-24 stsp if (err)
6116 0ebf8283 2019-07-24 stsp goto done;
6117 0ebf8283 2019-07-24 stsp
6118 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
6119 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6120 0ebf8283 2019-07-24 stsp goto done;
6121 0ebf8283 2019-07-24 stsp }
6122 0ebf8283 2019-07-24 stsp f = NULL;
6123 0ebf8283 2019-07-24 stsp
6124 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6125 0ebf8283 2019-07-24 stsp if (err) {
6126 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6127 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6128 0ebf8283 2019-07-24 stsp goto done;
6129 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6130 0ebf8283 2019-07-24 stsp commits, path, repo);
6131 0ebf8283 2019-07-24 stsp }
6132 0ebf8283 2019-07-24 stsp done:
6133 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6134 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6135 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6136 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6137 0ebf8283 2019-07-24 stsp free(path);
6138 0ebf8283 2019-07-24 stsp return err;
6139 0ebf8283 2019-07-24 stsp }
6140 0ebf8283 2019-07-24 stsp
6141 0ebf8283 2019-07-24 stsp static const struct got_error *
6142 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6143 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6144 0ebf8283 2019-07-24 stsp {
6145 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6146 0ebf8283 2019-07-24 stsp char *path = NULL;
6147 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6148 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6149 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6150 0ebf8283 2019-07-24 stsp
6151 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6152 0ebf8283 2019-07-24 stsp if (err)
6153 0ebf8283 2019-07-24 stsp return err;
6154 0ebf8283 2019-07-24 stsp
6155 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6156 0ebf8283 2019-07-24 stsp if (f == NULL) {
6157 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6158 0ebf8283 2019-07-24 stsp goto done;
6159 0ebf8283 2019-07-24 stsp }
6160 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6161 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6162 0ebf8283 2019-07-24 stsp repo);
6163 0ebf8283 2019-07-24 stsp if (err)
6164 0ebf8283 2019-07-24 stsp break;
6165 0ebf8283 2019-07-24 stsp
6166 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6167 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6168 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6169 0ebf8283 2019-07-24 stsp if (n < 0) {
6170 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6171 0ebf8283 2019-07-24 stsp break;
6172 0ebf8283 2019-07-24 stsp }
6173 0ebf8283 2019-07-24 stsp }
6174 0ebf8283 2019-07-24 stsp }
6175 0ebf8283 2019-07-24 stsp done:
6176 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6177 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6178 0ebf8283 2019-07-24 stsp free(path);
6179 0ebf8283 2019-07-24 stsp if (commit)
6180 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6181 0ebf8283 2019-07-24 stsp return err;
6182 0ebf8283 2019-07-24 stsp }
6183 0ebf8283 2019-07-24 stsp
6184 bfce7f83 2019-07-27 stsp void
6185 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6186 bfce7f83 2019-07-27 stsp {
6187 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6188 bfce7f83 2019-07-27 stsp
6189 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6190 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6191 bfce7f83 2019-07-27 stsp free(hle);
6192 bfce7f83 2019-07-27 stsp }
6193 bfce7f83 2019-07-27 stsp }
6194 bfce7f83 2019-07-27 stsp
6195 0ebf8283 2019-07-24 stsp static const struct got_error *
6196 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6197 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6198 0ebf8283 2019-07-24 stsp {
6199 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6200 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6201 0ebf8283 2019-07-24 stsp
6202 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6203 0ebf8283 2019-07-24 stsp if (f == NULL) {
6204 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6205 0ebf8283 2019-07-24 stsp goto done;
6206 0ebf8283 2019-07-24 stsp }
6207 0ebf8283 2019-07-24 stsp
6208 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6209 0ebf8283 2019-07-24 stsp done:
6210 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6211 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6212 0ebf8283 2019-07-24 stsp return err;
6213 0ebf8283 2019-07-24 stsp }
6214 0ebf8283 2019-07-24 stsp
6215 0ebf8283 2019-07-24 stsp static const struct got_error *
6216 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6217 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6218 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6219 0ebf8283 2019-07-24 stsp {
6220 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6221 0ebf8283 2019-07-24 stsp int resp = ' ';
6222 0ebf8283 2019-07-24 stsp
6223 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6224 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6225 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6226 0ebf8283 2019-07-24 stsp resp = getchar();
6227 a61a4414 2019-08-07 stsp if (resp == '\n')
6228 a61a4414 2019-08-07 stsp resp = getchar();
6229 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6230 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6231 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6232 bfce7f83 2019-07-27 stsp repo);
6233 426ebf2e 2019-07-25 stsp if (err) {
6234 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6235 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6236 426ebf2e 2019-07-25 stsp break;
6237 bfce7f83 2019-07-27 stsp prev_err = err;
6238 426ebf2e 2019-07-25 stsp resp = ' ';
6239 426ebf2e 2019-07-25 stsp continue;
6240 426ebf2e 2019-07-25 stsp }
6241 bfce7f83 2019-07-27 stsp break;
6242 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6243 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6244 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6245 0ebf8283 2019-07-24 stsp commits, repo);
6246 426ebf2e 2019-07-25 stsp if (err) {
6247 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6248 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6249 426ebf2e 2019-07-25 stsp break;
6250 bfce7f83 2019-07-27 stsp prev_err = err;
6251 426ebf2e 2019-07-25 stsp resp = ' ';
6252 426ebf2e 2019-07-25 stsp continue;
6253 426ebf2e 2019-07-25 stsp }
6254 bfce7f83 2019-07-27 stsp break;
6255 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6256 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6257 0ebf8283 2019-07-24 stsp break;
6258 426ebf2e 2019-07-25 stsp } else
6259 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6260 0ebf8283 2019-07-24 stsp }
6261 0ebf8283 2019-07-24 stsp
6262 0ebf8283 2019-07-24 stsp return err;
6263 0ebf8283 2019-07-24 stsp }
6264 0ebf8283 2019-07-24 stsp
6265 0ebf8283 2019-07-24 stsp static const struct got_error *
6266 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6267 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6268 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6269 0ebf8283 2019-07-24 stsp {
6270 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6271 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6272 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6273 3e3a69f1 2019-07-25 stsp branch, repo);
6274 0ebf8283 2019-07-24 stsp }
6275 0ebf8283 2019-07-24 stsp
6276 0ebf8283 2019-07-24 stsp static const struct got_error *
6277 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6278 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6279 0ebf8283 2019-07-24 stsp {
6280 0ebf8283 2019-07-24 stsp const struct got_error *err;
6281 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6282 0ebf8283 2019-07-24 stsp
6283 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6284 0ebf8283 2019-07-24 stsp if (err)
6285 0ebf8283 2019-07-24 stsp goto done;
6286 0ebf8283 2019-07-24 stsp
6287 0ebf8283 2019-07-24 stsp if (new_id) {
6288 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6289 0ebf8283 2019-07-24 stsp if (err)
6290 0ebf8283 2019-07-24 stsp goto done;
6291 0ebf8283 2019-07-24 stsp }
6292 0ebf8283 2019-07-24 stsp
6293 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6294 0ebf8283 2019-07-24 stsp if (new_id_str)
6295 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6296 0ebf8283 2019-07-24 stsp
6297 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6298 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6299 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6300 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6301 0ebf8283 2019-07-24 stsp goto done;
6302 0ebf8283 2019-07-24 stsp }
6303 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6304 0ebf8283 2019-07-24 stsp } else {
6305 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6306 0ebf8283 2019-07-24 stsp if (err)
6307 0ebf8283 2019-07-24 stsp goto done;
6308 0ebf8283 2019-07-24 stsp }
6309 0ebf8283 2019-07-24 stsp
6310 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6311 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6312 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6313 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6314 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6315 0ebf8283 2019-07-24 stsp break;
6316 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6317 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6318 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6319 0ebf8283 2019-07-24 stsp logmsg);
6320 0ebf8283 2019-07-24 stsp break;
6321 0ebf8283 2019-07-24 stsp default:
6322 0ebf8283 2019-07-24 stsp break;
6323 0ebf8283 2019-07-24 stsp }
6324 0ebf8283 2019-07-24 stsp
6325 0ebf8283 2019-07-24 stsp done:
6326 0ebf8283 2019-07-24 stsp free(old_id_str);
6327 0ebf8283 2019-07-24 stsp free(new_id_str);
6328 0ebf8283 2019-07-24 stsp return err;
6329 0ebf8283 2019-07-24 stsp }
6330 0ebf8283 2019-07-24 stsp
6331 0ebf8283 2019-07-24 stsp static const struct got_error *
6332 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6333 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6334 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6335 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6336 0ebf8283 2019-07-24 stsp {
6337 0ebf8283 2019-07-24 stsp const struct got_error *err;
6338 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6339 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6340 0ebf8283 2019-07-24 stsp
6341 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6342 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6343 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6344 0ebf8283 2019-07-24 stsp if (err)
6345 0ebf8283 2019-07-24 stsp return err;
6346 0ebf8283 2019-07-24 stsp }
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6349 0ebf8283 2019-07-24 stsp if (err)
6350 0ebf8283 2019-07-24 stsp return err;
6351 0ebf8283 2019-07-24 stsp
6352 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6353 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6354 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6355 0ebf8283 2019-07-24 stsp if (err) {
6356 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6357 0ebf8283 2019-07-24 stsp goto done;
6358 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6359 0ebf8283 2019-07-24 stsp } else {
6360 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6361 0ebf8283 2019-07-24 stsp free(new_commit_id);
6362 0ebf8283 2019-07-24 stsp }
6363 0ebf8283 2019-07-24 stsp done:
6364 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6365 0ebf8283 2019-07-24 stsp return err;
6366 0ebf8283 2019-07-24 stsp }
6367 0ebf8283 2019-07-24 stsp
6368 0ebf8283 2019-07-24 stsp static const struct got_error *
6369 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6370 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6371 0ebf8283 2019-07-24 stsp {
6372 0ebf8283 2019-07-24 stsp const struct got_error *error;
6373 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6374 0ebf8283 2019-07-24 stsp
6375 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6376 0ebf8283 2019-07-24 stsp repo);
6377 0ebf8283 2019-07-24 stsp if (error)
6378 0ebf8283 2019-07-24 stsp return error;
6379 0ebf8283 2019-07-24 stsp
6380 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6381 0ebf8283 2019-07-24 stsp if (error)
6382 0ebf8283 2019-07-24 stsp return error;
6383 0ebf8283 2019-07-24 stsp
6384 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6385 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6386 0ebf8283 2019-07-24 stsp return error;
6387 0ebf8283 2019-07-24 stsp }
6388 0ebf8283 2019-07-24 stsp
6389 0ebf8283 2019-07-24 stsp static const struct got_error *
6390 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6391 0ebf8283 2019-07-24 stsp {
6392 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6393 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6394 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6395 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6396 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6397 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6398 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6399 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6400 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6401 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6402 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6403 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6404 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6405 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6406 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6407 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6408 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6409 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6410 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6411 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6412 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6413 0ebf8283 2019-07-24 stsp
6414 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6415 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6416 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6417 0ebf8283 2019-07-24 stsp
6418 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6419 0ebf8283 2019-07-24 stsp switch (ch) {
6420 0ebf8283 2019-07-24 stsp case 'a':
6421 0ebf8283 2019-07-24 stsp abort_edit = 1;
6422 0ebf8283 2019-07-24 stsp break;
6423 0ebf8283 2019-07-24 stsp case 'c':
6424 0ebf8283 2019-07-24 stsp continue_edit = 1;
6425 0ebf8283 2019-07-24 stsp break;
6426 0ebf8283 2019-07-24 stsp case 'F':
6427 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6428 0ebf8283 2019-07-24 stsp break;
6429 0ebf8283 2019-07-24 stsp default:
6430 0ebf8283 2019-07-24 stsp usage_histedit();
6431 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6432 0ebf8283 2019-07-24 stsp }
6433 0ebf8283 2019-07-24 stsp }
6434 0ebf8283 2019-07-24 stsp
6435 0ebf8283 2019-07-24 stsp argc -= optind;
6436 0ebf8283 2019-07-24 stsp argv += optind;
6437 0ebf8283 2019-07-24 stsp
6438 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6439 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6440 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6441 0ebf8283 2019-07-24 stsp err(1, "pledge");
6442 0ebf8283 2019-07-24 stsp #endif
6443 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6444 0ebf8283 2019-07-24 stsp usage_histedit();
6445 0ebf8283 2019-07-24 stsp if (argc != 0)
6446 0ebf8283 2019-07-24 stsp usage_histedit();
6447 0ebf8283 2019-07-24 stsp
6448 0ebf8283 2019-07-24 stsp /*
6449 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6450 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6451 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6452 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6453 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6454 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6455 0ebf8283 2019-07-24 stsp */
6456 0ebf8283 2019-07-24 stsp
6457 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6458 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6459 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6460 0ebf8283 2019-07-24 stsp goto done;
6461 0ebf8283 2019-07-24 stsp }
6462 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6463 0ebf8283 2019-07-24 stsp if (error)
6464 0ebf8283 2019-07-24 stsp goto done;
6465 0ebf8283 2019-07-24 stsp
6466 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6467 c9956ddf 2019-09-08 stsp NULL);
6468 0ebf8283 2019-07-24 stsp if (error != NULL)
6469 0ebf8283 2019-07-24 stsp goto done;
6470 0ebf8283 2019-07-24 stsp
6471 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6472 0ebf8283 2019-07-24 stsp if (error)
6473 0ebf8283 2019-07-24 stsp goto done;
6474 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6475 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6476 0ebf8283 2019-07-24 stsp goto done;
6477 0ebf8283 2019-07-24 stsp }
6478 0ebf8283 2019-07-24 stsp
6479 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6480 0ebf8283 2019-07-24 stsp if (error)
6481 0ebf8283 2019-07-24 stsp goto done;
6482 0ebf8283 2019-07-24 stsp
6483 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6484 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6485 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6486 3e3a69f1 2019-07-25 stsp worktree, repo);
6487 0ebf8283 2019-07-24 stsp if (error)
6488 0ebf8283 2019-07-24 stsp goto done;
6489 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6490 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6491 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6492 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6493 0ebf8283 2019-07-24 stsp if (error)
6494 0ebf8283 2019-07-24 stsp goto done;
6495 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6496 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6497 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6498 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6499 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6500 0ebf8283 2019-07-24 stsp goto done;
6501 0ebf8283 2019-07-24 stsp }
6502 0ebf8283 2019-07-24 stsp
6503 0ebf8283 2019-07-24 stsp if (continue_edit) {
6504 0ebf8283 2019-07-24 stsp char *path;
6505 0ebf8283 2019-07-24 stsp
6506 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6507 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6508 0ebf8283 2019-07-24 stsp goto done;
6509 0ebf8283 2019-07-24 stsp }
6510 0ebf8283 2019-07-24 stsp
6511 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6512 0ebf8283 2019-07-24 stsp if (error)
6513 0ebf8283 2019-07-24 stsp goto done;
6514 0ebf8283 2019-07-24 stsp
6515 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6516 0ebf8283 2019-07-24 stsp free(path);
6517 0ebf8283 2019-07-24 stsp if (error)
6518 0ebf8283 2019-07-24 stsp goto done;
6519 0ebf8283 2019-07-24 stsp
6520 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6521 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6522 3e3a69f1 2019-07-25 stsp worktree, repo);
6523 0ebf8283 2019-07-24 stsp if (error)
6524 0ebf8283 2019-07-24 stsp goto done;
6525 0ebf8283 2019-07-24 stsp
6526 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6527 0ebf8283 2019-07-24 stsp if (error)
6528 0ebf8283 2019-07-24 stsp goto done;
6529 0ebf8283 2019-07-24 stsp
6530 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6531 0ebf8283 2019-07-24 stsp head_commit_id);
6532 0ebf8283 2019-07-24 stsp if (error)
6533 0ebf8283 2019-07-24 stsp goto done;
6534 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6535 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6536 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6537 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6538 3aac7cf7 2019-07-25 stsp goto done;
6539 3aac7cf7 2019-07-25 stsp }
6540 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6541 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6542 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6543 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6544 0ebf8283 2019-07-24 stsp commit = NULL;
6545 0ebf8283 2019-07-24 stsp if (error)
6546 0ebf8283 2019-07-24 stsp goto done;
6547 0ebf8283 2019-07-24 stsp } else {
6548 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6549 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6550 0ebf8283 2019-07-24 stsp goto done;
6551 0ebf8283 2019-07-24 stsp }
6552 0ebf8283 2019-07-24 stsp
6553 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6554 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6555 0ebf8283 2019-07-24 stsp if (error != NULL)
6556 0ebf8283 2019-07-24 stsp goto done;
6557 0ebf8283 2019-07-24 stsp
6558 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6559 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6560 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6561 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6562 c7d20a3f 2019-07-30 stsp goto done;
6563 c7d20a3f 2019-07-30 stsp }
6564 c7d20a3f 2019-07-30 stsp
6565 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6566 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6567 bfce7f83 2019-07-27 stsp branch = NULL;
6568 0ebf8283 2019-07-24 stsp if (error)
6569 0ebf8283 2019-07-24 stsp goto done;
6570 0ebf8283 2019-07-24 stsp
6571 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6572 0ebf8283 2019-07-24 stsp head_commit_id);
6573 0ebf8283 2019-07-24 stsp if (error)
6574 0ebf8283 2019-07-24 stsp goto done;
6575 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6576 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6577 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6578 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6579 3aac7cf7 2019-07-25 stsp goto done;
6580 3aac7cf7 2019-07-25 stsp }
6581 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6582 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6583 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6584 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6585 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6586 0ebf8283 2019-07-24 stsp commit = NULL;
6587 0ebf8283 2019-07-24 stsp if (error)
6588 0ebf8283 2019-07-24 stsp goto done;
6589 0ebf8283 2019-07-24 stsp
6590 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6591 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6592 bfce7f83 2019-07-27 stsp if (error)
6593 bfce7f83 2019-07-27 stsp goto done;
6594 bfce7f83 2019-07-27 stsp
6595 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6596 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6597 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6598 6ba2bea2 2019-07-27 stsp if (error) {
6599 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6600 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6601 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6602 0ebf8283 2019-07-24 stsp goto done;
6603 6ba2bea2 2019-07-27 stsp }
6604 0ebf8283 2019-07-24 stsp } else {
6605 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6606 0ebf8283 2019-07-24 stsp repo);
6607 6ba2bea2 2019-07-27 stsp if (error) {
6608 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6609 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6610 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6611 0ebf8283 2019-07-24 stsp goto done;
6612 6ba2bea2 2019-07-27 stsp }
6613 0ebf8283 2019-07-24 stsp
6614 0ebf8283 2019-07-24 stsp }
6615 0ebf8283 2019-07-24 stsp
6616 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6617 0ebf8283 2019-07-24 stsp repo);
6618 6ba2bea2 2019-07-27 stsp if (error) {
6619 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6620 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6621 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6622 0ebf8283 2019-07-24 stsp goto done;
6623 6ba2bea2 2019-07-27 stsp }
6624 0ebf8283 2019-07-24 stsp
6625 0ebf8283 2019-07-24 stsp }
6626 0ebf8283 2019-07-24 stsp
6627 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6628 4ec14e60 2019-08-28 hiltjo if (error)
6629 0ebf8283 2019-07-24 stsp goto done;
6630 0ebf8283 2019-07-24 stsp
6631 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6632 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6633 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6634 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6635 0ebf8283 2019-07-24 stsp continue;
6636 0ebf8283 2019-07-24 stsp
6637 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6638 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6639 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6640 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6641 0ebf8283 2019-07-24 stsp repo);
6642 0ebf8283 2019-07-24 stsp } else {
6643 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6644 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6645 0ebf8283 2019-07-24 stsp }
6646 0ebf8283 2019-07-24 stsp if (error)
6647 0ebf8283 2019-07-24 stsp goto done;
6648 0ebf8283 2019-07-24 stsp continue;
6649 0ebf8283 2019-07-24 stsp }
6650 0ebf8283 2019-07-24 stsp
6651 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6652 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6653 0ebf8283 2019-07-24 stsp if (error)
6654 0ebf8283 2019-07-24 stsp goto done;
6655 0ebf8283 2019-07-24 stsp continue;
6656 0ebf8283 2019-07-24 stsp }
6657 0ebf8283 2019-07-24 stsp
6658 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6659 0ebf8283 2019-07-24 stsp hle->commit_id);
6660 0ebf8283 2019-07-24 stsp if (error)
6661 0ebf8283 2019-07-24 stsp goto done;
6662 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6663 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6664 0ebf8283 2019-07-24 stsp
6665 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6666 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6667 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6668 0ebf8283 2019-07-24 stsp if (error)
6669 0ebf8283 2019-07-24 stsp goto done;
6670 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6671 0ebf8283 2019-07-24 stsp commit = NULL;
6672 0ebf8283 2019-07-24 stsp
6673 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6674 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6675 0ebf8283 2019-07-24 stsp break;
6676 0ebf8283 2019-07-24 stsp }
6677 0ebf8283 2019-07-24 stsp
6678 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6679 0ebf8283 2019-07-24 stsp char *id_str;
6680 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6681 0ebf8283 2019-07-24 stsp if (error)
6682 0ebf8283 2019-07-24 stsp goto done;
6683 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6684 0ebf8283 2019-07-24 stsp id_str);
6685 0ebf8283 2019-07-24 stsp free(id_str);
6686 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6687 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6688 3e3a69f1 2019-07-25 stsp fileindex);
6689 0ebf8283 2019-07-24 stsp goto done;
6690 0ebf8283 2019-07-24 stsp }
6691 0ebf8283 2019-07-24 stsp
6692 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6693 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6694 0ebf8283 2019-07-24 stsp if (error)
6695 0ebf8283 2019-07-24 stsp goto done;
6696 0ebf8283 2019-07-24 stsp continue;
6697 0ebf8283 2019-07-24 stsp }
6698 0ebf8283 2019-07-24 stsp
6699 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6700 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6701 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6702 0ebf8283 2019-07-24 stsp if (error)
6703 0ebf8283 2019-07-24 stsp goto done;
6704 0ebf8283 2019-07-24 stsp }
6705 0ebf8283 2019-07-24 stsp
6706 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6707 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6708 0ebf8283 2019-07-24 stsp if (error)
6709 0ebf8283 2019-07-24 stsp goto done;
6710 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6711 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6712 0ebf8283 2019-07-24 stsp } else
6713 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6714 3e3a69f1 2019-07-25 stsp branch, repo);
6715 0ebf8283 2019-07-24 stsp done:
6716 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6717 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6718 0ebf8283 2019-07-24 stsp free(head_commit_id);
6719 0ebf8283 2019-07-24 stsp free(base_commit_id);
6720 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6721 0ebf8283 2019-07-24 stsp if (commit)
6722 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6723 0ebf8283 2019-07-24 stsp if (branch)
6724 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6725 0ebf8283 2019-07-24 stsp if (tmp_branch)
6726 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6727 0ebf8283 2019-07-24 stsp if (worktree)
6728 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6729 2822a352 2019-10-15 stsp if (repo)
6730 2822a352 2019-10-15 stsp got_repo_close(repo);
6731 2822a352 2019-10-15 stsp return error;
6732 2822a352 2019-10-15 stsp }
6733 2822a352 2019-10-15 stsp
6734 2822a352 2019-10-15 stsp __dead static void
6735 2822a352 2019-10-15 stsp usage_integrate(void)
6736 2822a352 2019-10-15 stsp {
6737 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6738 2822a352 2019-10-15 stsp exit(1);
6739 2822a352 2019-10-15 stsp }
6740 2822a352 2019-10-15 stsp
6741 2822a352 2019-10-15 stsp static const struct got_error *
6742 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6743 2822a352 2019-10-15 stsp {
6744 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6745 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6746 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6747 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6748 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6749 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6750 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6751 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6752 2822a352 2019-10-15 stsp int ch, did_something = 0;
6753 2822a352 2019-10-15 stsp
6754 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6755 2822a352 2019-10-15 stsp switch (ch) {
6756 2822a352 2019-10-15 stsp default:
6757 2822a352 2019-10-15 stsp usage_integrate();
6758 2822a352 2019-10-15 stsp /* NOTREACHED */
6759 2822a352 2019-10-15 stsp }
6760 2822a352 2019-10-15 stsp }
6761 2822a352 2019-10-15 stsp
6762 2822a352 2019-10-15 stsp argc -= optind;
6763 2822a352 2019-10-15 stsp argv += optind;
6764 2822a352 2019-10-15 stsp
6765 2822a352 2019-10-15 stsp if (argc != 1)
6766 2822a352 2019-10-15 stsp usage_integrate();
6767 2822a352 2019-10-15 stsp branch_arg = argv[0];
6768 2822a352 2019-10-15 stsp
6769 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6770 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6771 2822a352 2019-10-15 stsp err(1, "pledge");
6772 2822a352 2019-10-15 stsp
6773 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6774 2822a352 2019-10-15 stsp if (cwd == NULL) {
6775 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6776 2822a352 2019-10-15 stsp goto done;
6777 2822a352 2019-10-15 stsp }
6778 2822a352 2019-10-15 stsp
6779 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6780 2822a352 2019-10-15 stsp if (error)
6781 2822a352 2019-10-15 stsp goto done;
6782 2822a352 2019-10-15 stsp
6783 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6784 2822a352 2019-10-15 stsp if (error)
6785 2822a352 2019-10-15 stsp goto done;
6786 2822a352 2019-10-15 stsp
6787 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6788 2822a352 2019-10-15 stsp NULL);
6789 2822a352 2019-10-15 stsp if (error != NULL)
6790 2822a352 2019-10-15 stsp goto done;
6791 2822a352 2019-10-15 stsp
6792 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6793 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6794 2822a352 2019-10-15 stsp if (error)
6795 2822a352 2019-10-15 stsp goto done;
6796 2822a352 2019-10-15 stsp
6797 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6798 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6799 2822a352 2019-10-15 stsp goto done;
6800 2822a352 2019-10-15 stsp }
6801 2822a352 2019-10-15 stsp
6802 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6803 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6804 2822a352 2019-10-15 stsp if (error)
6805 2822a352 2019-10-15 stsp goto done;
6806 2822a352 2019-10-15 stsp
6807 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6808 2822a352 2019-10-15 stsp if (refname == NULL) {
6809 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6810 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6811 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6812 2822a352 2019-10-15 stsp goto done;
6813 2822a352 2019-10-15 stsp }
6814 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6815 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6816 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6817 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6818 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6819 2822a352 2019-10-15 stsp goto done;
6820 2822a352 2019-10-15 stsp }
6821 2822a352 2019-10-15 stsp
6822 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6823 2822a352 2019-10-15 stsp if (error)
6824 2822a352 2019-10-15 stsp goto done;
6825 2822a352 2019-10-15 stsp
6826 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6827 2822a352 2019-10-15 stsp if (error)
6828 2822a352 2019-10-15 stsp goto done;
6829 2822a352 2019-10-15 stsp
6830 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6831 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6832 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6833 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6834 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6835 2822a352 2019-10-15 stsp goto done;
6836 2822a352 2019-10-15 stsp }
6837 2822a352 2019-10-15 stsp
6838 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6839 2822a352 2019-10-15 stsp if (error) {
6840 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6841 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6842 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6843 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6844 2822a352 2019-10-15 stsp goto done;
6845 2822a352 2019-10-15 stsp }
6846 2822a352 2019-10-15 stsp
6847 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6848 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6849 2822a352 2019-10-15 stsp check_cancelled, NULL);
6850 2822a352 2019-10-15 stsp if (error)
6851 2822a352 2019-10-15 stsp goto done;
6852 2822a352 2019-10-15 stsp
6853 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6854 2822a352 2019-10-15 stsp done:
6855 715dc77e 2019-08-03 stsp if (repo)
6856 715dc77e 2019-08-03 stsp got_repo_close(repo);
6857 2822a352 2019-10-15 stsp if (worktree)
6858 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6859 2822a352 2019-10-15 stsp free(cwd);
6860 2822a352 2019-10-15 stsp free(base_commit_id);
6861 2822a352 2019-10-15 stsp free(commit_id);
6862 2822a352 2019-10-15 stsp free(refname);
6863 2822a352 2019-10-15 stsp free(base_refname);
6864 715dc77e 2019-08-03 stsp return error;
6865 715dc77e 2019-08-03 stsp }
6866 715dc77e 2019-08-03 stsp
6867 715dc77e 2019-08-03 stsp __dead static void
6868 715dc77e 2019-08-03 stsp usage_stage(void)
6869 715dc77e 2019-08-03 stsp {
6870 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6871 f3055044 2019-08-07 stsp "[file-path ...]\n",
6872 715dc77e 2019-08-03 stsp getprogname());
6873 715dc77e 2019-08-03 stsp exit(1);
6874 715dc77e 2019-08-03 stsp }
6875 715dc77e 2019-08-03 stsp
6876 715dc77e 2019-08-03 stsp static const struct got_error *
6877 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6878 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6879 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6880 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6881 408b4ebc 2019-08-03 stsp {
6882 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6883 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6884 408b4ebc 2019-08-03 stsp
6885 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6886 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6887 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6888 408b4ebc 2019-08-03 stsp return NULL;
6889 408b4ebc 2019-08-03 stsp
6890 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6891 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6892 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6893 408b4ebc 2019-08-03 stsp else
6894 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6895 408b4ebc 2019-08-03 stsp if (err)
6896 408b4ebc 2019-08-03 stsp return err;
6897 408b4ebc 2019-08-03 stsp
6898 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6899 408b4ebc 2019-08-03 stsp free(id_str);
6900 dc424a06 2019-08-07 stsp return NULL;
6901 dc424a06 2019-08-07 stsp }
6902 2e1f37b0 2019-08-08 stsp
6903 dc424a06 2019-08-07 stsp static const struct got_error *
6904 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6905 715dc77e 2019-08-03 stsp {
6906 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6907 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6908 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6909 715dc77e 2019-08-03 stsp char *cwd = NULL;
6910 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6911 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6912 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6913 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6914 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6915 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6916 715dc77e 2019-08-03 stsp
6917 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6918 715dc77e 2019-08-03 stsp
6919 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6920 715dc77e 2019-08-03 stsp switch (ch) {
6921 408b4ebc 2019-08-03 stsp case 'l':
6922 408b4ebc 2019-08-03 stsp list_stage = 1;
6923 408b4ebc 2019-08-03 stsp break;
6924 dc424a06 2019-08-07 stsp case 'p':
6925 dc424a06 2019-08-07 stsp pflag = 1;
6926 dc424a06 2019-08-07 stsp break;
6927 dc424a06 2019-08-07 stsp case 'F':
6928 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6929 dc424a06 2019-08-07 stsp break;
6930 715dc77e 2019-08-03 stsp default:
6931 715dc77e 2019-08-03 stsp usage_stage();
6932 715dc77e 2019-08-03 stsp /* NOTREACHED */
6933 715dc77e 2019-08-03 stsp }
6934 715dc77e 2019-08-03 stsp }
6935 715dc77e 2019-08-03 stsp
6936 715dc77e 2019-08-03 stsp argc -= optind;
6937 715dc77e 2019-08-03 stsp argv += optind;
6938 715dc77e 2019-08-03 stsp
6939 715dc77e 2019-08-03 stsp #ifndef PROFILE
6940 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6941 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6942 715dc77e 2019-08-03 stsp err(1, "pledge");
6943 715dc77e 2019-08-03 stsp #endif
6944 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6945 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6946 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6947 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6948 715dc77e 2019-08-03 stsp
6949 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6950 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6951 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6952 715dc77e 2019-08-03 stsp goto done;
6953 715dc77e 2019-08-03 stsp }
6954 715dc77e 2019-08-03 stsp
6955 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6956 715dc77e 2019-08-03 stsp if (error)
6957 715dc77e 2019-08-03 stsp goto done;
6958 715dc77e 2019-08-03 stsp
6959 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6960 c9956ddf 2019-09-08 stsp NULL);
6961 715dc77e 2019-08-03 stsp if (error != NULL)
6962 715dc77e 2019-08-03 stsp goto done;
6963 715dc77e 2019-08-03 stsp
6964 dc424a06 2019-08-07 stsp if (patch_script_path) {
6965 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6966 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6967 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6968 dc424a06 2019-08-07 stsp patch_script_path);
6969 dc424a06 2019-08-07 stsp goto done;
6970 dc424a06 2019-08-07 stsp }
6971 dc424a06 2019-08-07 stsp }
6972 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6973 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6974 715dc77e 2019-08-03 stsp if (error)
6975 715dc77e 2019-08-03 stsp goto done;
6976 715dc77e 2019-08-03 stsp
6977 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6978 6d022e97 2019-08-04 stsp if (error)
6979 6d022e97 2019-08-04 stsp goto done;
6980 715dc77e 2019-08-03 stsp
6981 6d022e97 2019-08-04 stsp if (list_stage)
6982 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6983 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6984 2e1f37b0 2019-08-08 stsp else {
6985 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6986 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6987 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6988 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6989 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6990 2e1f37b0 2019-08-08 stsp }
6991 ad493afc 2019-08-03 stsp done:
6992 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6993 2e1f37b0 2019-08-08 stsp error == NULL)
6994 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6995 ad493afc 2019-08-03 stsp if (repo)
6996 ad493afc 2019-08-03 stsp got_repo_close(repo);
6997 ad493afc 2019-08-03 stsp if (worktree)
6998 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6999 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7000 ad493afc 2019-08-03 stsp free((char *)pe->path);
7001 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7002 ad493afc 2019-08-03 stsp free(cwd);
7003 ad493afc 2019-08-03 stsp return error;
7004 ad493afc 2019-08-03 stsp }
7005 ad493afc 2019-08-03 stsp
7006 ad493afc 2019-08-03 stsp __dead static void
7007 ad493afc 2019-08-03 stsp usage_unstage(void)
7008 ad493afc 2019-08-03 stsp {
7009 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7010 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7011 ad493afc 2019-08-03 stsp getprogname());
7012 ad493afc 2019-08-03 stsp exit(1);
7013 ad493afc 2019-08-03 stsp }
7014 ad493afc 2019-08-03 stsp
7015 ad493afc 2019-08-03 stsp
7016 ad493afc 2019-08-03 stsp static const struct got_error *
7017 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7018 ad493afc 2019-08-03 stsp {
7019 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7020 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7021 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7022 ad493afc 2019-08-03 stsp char *cwd = NULL;
7023 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7024 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7025 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7026 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7027 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7028 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7029 ad493afc 2019-08-03 stsp
7030 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7031 ad493afc 2019-08-03 stsp
7032 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7033 ad493afc 2019-08-03 stsp switch (ch) {
7034 2e1f37b0 2019-08-08 stsp case 'p':
7035 2e1f37b0 2019-08-08 stsp pflag = 1;
7036 2e1f37b0 2019-08-08 stsp break;
7037 2e1f37b0 2019-08-08 stsp case 'F':
7038 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7039 2e1f37b0 2019-08-08 stsp break;
7040 ad493afc 2019-08-03 stsp default:
7041 ad493afc 2019-08-03 stsp usage_unstage();
7042 ad493afc 2019-08-03 stsp /* NOTREACHED */
7043 ad493afc 2019-08-03 stsp }
7044 ad493afc 2019-08-03 stsp }
7045 ad493afc 2019-08-03 stsp
7046 ad493afc 2019-08-03 stsp argc -= optind;
7047 ad493afc 2019-08-03 stsp argv += optind;
7048 ad493afc 2019-08-03 stsp
7049 ad493afc 2019-08-03 stsp #ifndef PROFILE
7050 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7051 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7052 ad493afc 2019-08-03 stsp err(1, "pledge");
7053 ad493afc 2019-08-03 stsp #endif
7054 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7055 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7056 2e1f37b0 2019-08-08 stsp
7057 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7058 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7059 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7060 ad493afc 2019-08-03 stsp goto done;
7061 ad493afc 2019-08-03 stsp }
7062 ad493afc 2019-08-03 stsp
7063 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7064 ad493afc 2019-08-03 stsp if (error)
7065 ad493afc 2019-08-03 stsp goto done;
7066 ad493afc 2019-08-03 stsp
7067 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7068 c9956ddf 2019-09-08 stsp NULL);
7069 ad493afc 2019-08-03 stsp if (error != NULL)
7070 ad493afc 2019-08-03 stsp goto done;
7071 ad493afc 2019-08-03 stsp
7072 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7073 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7074 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7075 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7076 2e1f37b0 2019-08-08 stsp patch_script_path);
7077 2e1f37b0 2019-08-08 stsp goto done;
7078 2e1f37b0 2019-08-08 stsp }
7079 2e1f37b0 2019-08-08 stsp }
7080 2e1f37b0 2019-08-08 stsp
7081 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7082 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7083 ad493afc 2019-08-03 stsp if (error)
7084 ad493afc 2019-08-03 stsp goto done;
7085 ad493afc 2019-08-03 stsp
7086 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7087 ad493afc 2019-08-03 stsp if (error)
7088 ad493afc 2019-08-03 stsp goto done;
7089 ad493afc 2019-08-03 stsp
7090 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7091 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7092 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7093 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7094 715dc77e 2019-08-03 stsp done:
7095 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7096 2e1f37b0 2019-08-08 stsp error == NULL)
7097 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7098 0ebf8283 2019-07-24 stsp if (repo)
7099 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7100 715dc77e 2019-08-03 stsp if (worktree)
7101 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7102 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7103 715dc77e 2019-08-03 stsp free((char *)pe->path);
7104 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7105 715dc77e 2019-08-03 stsp free(cwd);
7106 01073a5d 2019-08-22 stsp return error;
7107 01073a5d 2019-08-22 stsp }
7108 01073a5d 2019-08-22 stsp
7109 01073a5d 2019-08-22 stsp __dead static void
7110 01073a5d 2019-08-22 stsp usage_cat(void)
7111 01073a5d 2019-08-22 stsp {
7112 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7113 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7114 01073a5d 2019-08-22 stsp exit(1);
7115 01073a5d 2019-08-22 stsp }
7116 01073a5d 2019-08-22 stsp
7117 01073a5d 2019-08-22 stsp static const struct got_error *
7118 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7119 01073a5d 2019-08-22 stsp {
7120 01073a5d 2019-08-22 stsp const struct got_error *err;
7121 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7122 01073a5d 2019-08-22 stsp
7123 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7124 01073a5d 2019-08-22 stsp if (err)
7125 01073a5d 2019-08-22 stsp return err;
7126 01073a5d 2019-08-22 stsp
7127 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7128 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7129 01073a5d 2019-08-22 stsp return err;
7130 01073a5d 2019-08-22 stsp }
7131 01073a5d 2019-08-22 stsp
7132 01073a5d 2019-08-22 stsp static const struct got_error *
7133 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7134 01073a5d 2019-08-22 stsp {
7135 01073a5d 2019-08-22 stsp const struct got_error *err;
7136 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7137 56e0773d 2019-11-28 stsp int nentries, i;
7138 01073a5d 2019-08-22 stsp
7139 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7140 01073a5d 2019-08-22 stsp if (err)
7141 01073a5d 2019-08-22 stsp return err;
7142 01073a5d 2019-08-22 stsp
7143 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7144 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7145 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7146 01073a5d 2019-08-22 stsp char *id_str;
7147 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7148 01073a5d 2019-08-22 stsp break;
7149 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7150 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7151 01073a5d 2019-08-22 stsp if (err)
7152 01073a5d 2019-08-22 stsp break;
7153 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7154 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7155 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7156 01073a5d 2019-08-22 stsp free(id_str);
7157 01073a5d 2019-08-22 stsp }
7158 01073a5d 2019-08-22 stsp
7159 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7160 01073a5d 2019-08-22 stsp return err;
7161 01073a5d 2019-08-22 stsp }
7162 01073a5d 2019-08-22 stsp
7163 01073a5d 2019-08-22 stsp static const struct got_error *
7164 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7165 01073a5d 2019-08-22 stsp {
7166 01073a5d 2019-08-22 stsp const struct got_error *err;
7167 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7168 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7169 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7170 01073a5d 2019-08-22 stsp char *id_str = NULL;
7171 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7172 01073a5d 2019-08-22 stsp
7173 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7174 01073a5d 2019-08-22 stsp if (err)
7175 01073a5d 2019-08-22 stsp return err;
7176 01073a5d 2019-08-22 stsp
7177 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7178 01073a5d 2019-08-22 stsp if (err)
7179 01073a5d 2019-08-22 stsp goto done;
7180 01073a5d 2019-08-22 stsp
7181 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7182 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7183 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7184 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7185 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7186 01073a5d 2019-08-22 stsp char *pid_str;
7187 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7188 01073a5d 2019-08-22 stsp if (err)
7189 01073a5d 2019-08-22 stsp goto done;
7190 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7191 01073a5d 2019-08-22 stsp free(pid_str);
7192 01073a5d 2019-08-22 stsp }
7193 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7194 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7195 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7196 01073a5d 2019-08-22 stsp
7197 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7198 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7199 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7200 01073a5d 2019-08-22 stsp
7201 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7202 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7203 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7204 01073a5d 2019-08-22 stsp done:
7205 01073a5d 2019-08-22 stsp free(id_str);
7206 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7207 01073a5d 2019-08-22 stsp return err;
7208 01073a5d 2019-08-22 stsp }
7209 01073a5d 2019-08-22 stsp
7210 01073a5d 2019-08-22 stsp static const struct got_error *
7211 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7212 01073a5d 2019-08-22 stsp {
7213 01073a5d 2019-08-22 stsp const struct got_error *err;
7214 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7215 01073a5d 2019-08-22 stsp char *id_str = NULL;
7216 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7217 01073a5d 2019-08-22 stsp
7218 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7219 01073a5d 2019-08-22 stsp if (err)
7220 01073a5d 2019-08-22 stsp return err;
7221 01073a5d 2019-08-22 stsp
7222 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7223 01073a5d 2019-08-22 stsp if (err)
7224 01073a5d 2019-08-22 stsp goto done;
7225 01073a5d 2019-08-22 stsp
7226 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7227 e15d5241 2019-08-22 stsp
7228 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7229 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7230 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7231 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7232 01073a5d 2019-08-22 stsp break;
7233 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7234 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7235 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7236 01073a5d 2019-08-22 stsp break;
7237 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7238 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7239 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7240 01073a5d 2019-08-22 stsp break;
7241 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7242 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7243 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7244 01073a5d 2019-08-22 stsp break;
7245 01073a5d 2019-08-22 stsp default:
7246 01073a5d 2019-08-22 stsp break;
7247 01073a5d 2019-08-22 stsp }
7248 01073a5d 2019-08-22 stsp
7249 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7250 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7251 e15d5241 2019-08-22 stsp
7252 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7253 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7254 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7255 01073a5d 2019-08-22 stsp
7256 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7257 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7258 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7259 01073a5d 2019-08-22 stsp done:
7260 01073a5d 2019-08-22 stsp free(id_str);
7261 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7262 01073a5d 2019-08-22 stsp return err;
7263 01073a5d 2019-08-22 stsp }
7264 01073a5d 2019-08-22 stsp
7265 01073a5d 2019-08-22 stsp static const struct got_error *
7266 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7267 01073a5d 2019-08-22 stsp {
7268 01073a5d 2019-08-22 stsp const struct got_error *error;
7269 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7270 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7271 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7272 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7273 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7274 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7275 01073a5d 2019-08-22 stsp
7276 01073a5d 2019-08-22 stsp #ifndef PROFILE
7277 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7278 01073a5d 2019-08-22 stsp NULL) == -1)
7279 01073a5d 2019-08-22 stsp err(1, "pledge");
7280 01073a5d 2019-08-22 stsp #endif
7281 01073a5d 2019-08-22 stsp
7282 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7283 01073a5d 2019-08-22 stsp switch (ch) {
7284 896e9b6f 2019-08-26 stsp case 'c':
7285 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7286 896e9b6f 2019-08-26 stsp break;
7287 01073a5d 2019-08-22 stsp case 'r':
7288 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7289 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7290 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7291 9ba1d308 2019-10-21 stsp optarg);
7292 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7293 01073a5d 2019-08-22 stsp break;
7294 896e9b6f 2019-08-26 stsp case 'P':
7295 896e9b6f 2019-08-26 stsp force_path = 1;
7296 896e9b6f 2019-08-26 stsp break;
7297 01073a5d 2019-08-22 stsp default:
7298 01073a5d 2019-08-22 stsp usage_cat();
7299 01073a5d 2019-08-22 stsp /* NOTREACHED */
7300 01073a5d 2019-08-22 stsp }
7301 01073a5d 2019-08-22 stsp }
7302 01073a5d 2019-08-22 stsp
7303 01073a5d 2019-08-22 stsp argc -= optind;
7304 01073a5d 2019-08-22 stsp argv += optind;
7305 01073a5d 2019-08-22 stsp
7306 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7307 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7308 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7309 01073a5d 2019-08-22 stsp goto done;
7310 01073a5d 2019-08-22 stsp }
7311 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7312 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7313 01073a5d 2019-08-22 stsp goto done;
7314 01073a5d 2019-08-22 stsp if (worktree) {
7315 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7316 01073a5d 2019-08-22 stsp repo_path = strdup(
7317 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7318 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7319 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7320 01073a5d 2019-08-22 stsp goto done;
7321 01073a5d 2019-08-22 stsp }
7322 01073a5d 2019-08-22 stsp }
7323 01073a5d 2019-08-22 stsp }
7324 01073a5d 2019-08-22 stsp
7325 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7326 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7327 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7328 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7329 01073a5d 2019-08-22 stsp }
7330 01073a5d 2019-08-22 stsp
7331 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7332 01073a5d 2019-08-22 stsp free(repo_path);
7333 01073a5d 2019-08-22 stsp if (error != NULL)
7334 01073a5d 2019-08-22 stsp goto done;
7335 01073a5d 2019-08-22 stsp
7336 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7337 896e9b6f 2019-08-26 stsp if (error)
7338 896e9b6f 2019-08-26 stsp goto done;
7339 896e9b6f 2019-08-26 stsp
7340 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7341 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7342 896e9b6f 2019-08-26 stsp error = resolve_commit_arg(&commit_id, commit_id_str, repo);
7343 01073a5d 2019-08-22 stsp if (error)
7344 01073a5d 2019-08-22 stsp goto done;
7345 01073a5d 2019-08-22 stsp
7346 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7347 896e9b6f 2019-08-26 stsp if (force_path) {
7348 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7349 896e9b6f 2019-08-26 stsp argv[i]);
7350 896e9b6f 2019-08-26 stsp if (error)
7351 896e9b6f 2019-08-26 stsp break;
7352 896e9b6f 2019-08-26 stsp } else {
7353 896e9b6f 2019-08-26 stsp error = match_object_id(&id, &label, argv[i],
7354 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7355 896e9b6f 2019-08-26 stsp if (error) {
7356 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7357 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7358 896e9b6f 2019-08-26 stsp break;
7359 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7360 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7361 896e9b6f 2019-08-26 stsp if (error)
7362 896e9b6f 2019-08-26 stsp break;
7363 896e9b6f 2019-08-26 stsp }
7364 896e9b6f 2019-08-26 stsp }
7365 01073a5d 2019-08-22 stsp
7366 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7367 01073a5d 2019-08-22 stsp if (error)
7368 01073a5d 2019-08-22 stsp break;
7369 01073a5d 2019-08-22 stsp
7370 01073a5d 2019-08-22 stsp switch (obj_type) {
7371 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7372 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7373 01073a5d 2019-08-22 stsp break;
7374 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7375 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7376 01073a5d 2019-08-22 stsp break;
7377 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7378 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7379 01073a5d 2019-08-22 stsp break;
7380 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7381 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7382 01073a5d 2019-08-22 stsp break;
7383 01073a5d 2019-08-22 stsp default:
7384 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7385 01073a5d 2019-08-22 stsp break;
7386 01073a5d 2019-08-22 stsp }
7387 01073a5d 2019-08-22 stsp if (error)
7388 01073a5d 2019-08-22 stsp break;
7389 01073a5d 2019-08-22 stsp free(label);
7390 01073a5d 2019-08-22 stsp label = NULL;
7391 01073a5d 2019-08-22 stsp free(id);
7392 01073a5d 2019-08-22 stsp id = NULL;
7393 01073a5d 2019-08-22 stsp }
7394 01073a5d 2019-08-22 stsp
7395 01073a5d 2019-08-22 stsp done:
7396 01073a5d 2019-08-22 stsp free(label);
7397 01073a5d 2019-08-22 stsp free(id);
7398 896e9b6f 2019-08-26 stsp free(commit_id);
7399 01073a5d 2019-08-22 stsp if (worktree)
7400 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7401 01073a5d 2019-08-22 stsp if (repo) {
7402 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7403 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7404 01073a5d 2019-08-22 stsp if (error == NULL)
7405 01073a5d 2019-08-22 stsp error = repo_error;
7406 01073a5d 2019-08-22 stsp }
7407 0ebf8283 2019-07-24 stsp return error;
7408 0ebf8283 2019-07-24 stsp }