Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 83cd27f8 2020-01-13 stsp #include <getopt.h>
40 5c860e29 2018-03-12 stsp
41 53ccebc2 2019-07-30 stsp #include "got_version.h"
42 f42b1b34 2018-03-12 stsp #include "got_error.h"
43 f42b1b34 2018-03-12 stsp #include "got_object.h"
44 5261c201 2018-04-01 stsp #include "got_reference.h"
45 f42b1b34 2018-03-12 stsp #include "got_repository.h"
46 1dd54920 2019-05-11 stsp #include "got_path.h"
47 e6209546 2019-08-22 stsp #include "got_cancel.h"
48 c09a553d 2018-03-12 stsp #include "got_worktree.h"
49 79109fed 2018-03-27 stsp #include "got_diff.h"
50 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
51 404c43c4 2018-06-21 stsp #include "got_blame.h"
52 63219cd2 2019-01-04 stsp #include "got_privsep.h"
53 793c30b5 2019-05-13 stsp #include "got_opentemp.h"
54 5c860e29 2018-03-12 stsp
55 5c860e29 2018-03-12 stsp #ifndef nitems
56 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
57 5c860e29 2018-03-12 stsp #endif
58 99437157 2018-11-11 stsp
59 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
60 99437157 2018-11-11 stsp static volatile sig_atomic_t sigpipe_received;
61 99437157 2018-11-11 stsp
62 99437157 2018-11-11 stsp static void
63 99437157 2018-11-11 stsp catch_sigint(int signo)
64 99437157 2018-11-11 stsp {
65 99437157 2018-11-11 stsp sigint_received = 1;
66 99437157 2018-11-11 stsp }
67 99437157 2018-11-11 stsp
68 99437157 2018-11-11 stsp static void
69 99437157 2018-11-11 stsp catch_sigpipe(int signo)
70 99437157 2018-11-11 stsp {
71 99437157 2018-11-11 stsp sigpipe_received = 1;
72 99437157 2018-11-11 stsp }
73 5c860e29 2018-03-12 stsp
74 99437157 2018-11-11 stsp
75 8cfb4057 2019-07-09 stsp struct got_cmd {
76 5c860e29 2018-03-12 stsp const char *cmd_name;
77 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
78 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
79 97b3a7be 2019-07-09 stsp const char *cmd_alias;
80 5c860e29 2018-03-12 stsp };
81 5c860e29 2018-03-12 stsp
82 ce5b7c56 2019-07-09 stsp __dead static void usage(int);
83 2c7829a4 2019-06-17 stsp __dead static void usage_init(void);
84 3ce1b845 2019-07-15 stsp __dead static void usage_import(void);
85 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
86 507dc3bb 2018-12-29 stsp __dead static void usage_update(void);
87 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
88 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
89 404c43c4 2018-06-21 stsp __dead static void usage_blame(void);
90 5de5890b 2018-10-18 stsp __dead static void usage_tree(void);
91 6bad629b 2019-02-04 stsp __dead static void usage_status(void);
92 d0eebce4 2019-03-11 stsp __dead static void usage_ref(void);
93 4e759de4 2019-06-26 stsp __dead static void usage_branch(void);
94 8e7bd50a 2019-08-22 stsp __dead static void usage_tag(void);
95 d00136be 2019-03-26 stsp __dead static void usage_add(void);
96 648e4ef7 2019-07-09 stsp __dead static void usage_remove(void);
97 a129376b 2019-03-28 stsp __dead static void usage_revert(void);
98 c4296144 2019-05-09 stsp __dead static void usage_commit(void);
99 234035bc 2019-06-01 stsp __dead static void usage_cherrypick(void);
100 5ef14e63 2019-06-02 stsp __dead static void usage_backout(void);
101 818c7501 2019-07-11 stsp __dead static void usage_rebase(void);
102 0ebf8283 2019-07-24 stsp __dead static void usage_histedit(void);
103 2822a352 2019-10-15 stsp __dead static void usage_integrate(void);
104 715dc77e 2019-08-03 stsp __dead static void usage_stage(void);
105 ad493afc 2019-08-03 stsp __dead static void usage_unstage(void);
106 01073a5d 2019-08-22 stsp __dead static void usage_cat(void);
107 5c860e29 2018-03-12 stsp
108 2c7829a4 2019-06-17 stsp static const struct got_error* cmd_init(int, char *[]);
109 3ce1b845 2019-07-15 stsp static const struct got_error* cmd_import(int, char *[]);
110 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
111 507dc3bb 2018-12-29 stsp static const struct got_error* cmd_update(int, char *[]);
112 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
113 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
114 404c43c4 2018-06-21 stsp static const struct got_error* cmd_blame(int, char *[]);
115 5de5890b 2018-10-18 stsp static const struct got_error* cmd_tree(int, char *[]);
116 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
117 d0eebce4 2019-03-11 stsp static const struct got_error* cmd_ref(int, char *[]);
118 4e759de4 2019-06-26 stsp static const struct got_error* cmd_branch(int, char *[]);
119 8e7bd50a 2019-08-22 stsp static const struct got_error* cmd_tag(int, char *[]);
120 d00136be 2019-03-26 stsp static const struct got_error* cmd_add(int, char *[]);
121 648e4ef7 2019-07-09 stsp static const struct got_error* cmd_remove(int, char *[]);
122 a129376b 2019-03-28 stsp static const struct got_error* cmd_revert(int, char *[]);
123 c4296144 2019-05-09 stsp static const struct got_error* cmd_commit(int, char *[]);
124 234035bc 2019-06-01 stsp static const struct got_error* cmd_cherrypick(int, char *[]);
125 5ef14e63 2019-06-02 stsp static const struct got_error* cmd_backout(int, char *[]);
126 818c7501 2019-07-11 stsp static const struct got_error* cmd_rebase(int, char *[]);
127 0ebf8283 2019-07-24 stsp static const struct got_error* cmd_histedit(int, char *[]);
128 2822a352 2019-10-15 stsp static const struct got_error* cmd_integrate(int, char *[]);
129 715dc77e 2019-08-03 stsp static const struct got_error* cmd_stage(int, char *[]);
130 ad493afc 2019-08-03 stsp static const struct got_error* cmd_unstage(int, char *[]);
131 01073a5d 2019-08-22 stsp static const struct got_error* cmd_cat(int, char *[]);
132 5c860e29 2018-03-12 stsp
133 8cfb4057 2019-07-09 stsp static struct got_cmd got_commands[] = {
134 bc26cce8 2019-08-04 stsp { "init", cmd_init, usage_init, "in" },
135 bc26cce8 2019-08-04 stsp { "import", cmd_import, usage_import, "im" },
136 97b3a7be 2019-07-09 stsp { "checkout", cmd_checkout, usage_checkout, "co" },
137 97b3a7be 2019-07-09 stsp { "update", cmd_update, usage_update, "up" },
138 97b3a7be 2019-07-09 stsp { "log", cmd_log, usage_log, "" },
139 bc26cce8 2019-08-04 stsp { "diff", cmd_diff, usage_diff, "di" },
140 bc26cce8 2019-08-04 stsp { "blame", cmd_blame, usage_blame, "bl" },
141 bc26cce8 2019-08-04 stsp { "tree", cmd_tree, usage_tree, "tr" },
142 97b3a7be 2019-07-09 stsp { "status", cmd_status, usage_status, "st" },
143 97b3a7be 2019-07-09 stsp { "ref", cmd_ref, usage_ref, "" },
144 97b3a7be 2019-07-09 stsp { "branch", cmd_branch, usage_branch, "br" },
145 8e7bd50a 2019-08-22 stsp { "tag", cmd_tag, usage_tag, "" },
146 97b3a7be 2019-07-09 stsp { "add", cmd_add, usage_add, "" },
147 648e4ef7 2019-07-09 stsp { "remove", cmd_remove, usage_remove, "rm" },
148 97b3a7be 2019-07-09 stsp { "revert", cmd_revert, usage_revert, "rv" },
149 97b3a7be 2019-07-09 stsp { "commit", cmd_commit, usage_commit, "ci" },
150 016477fd 2019-07-09 stsp { "cherrypick", cmd_cherrypick, usage_cherrypick, "cy" },
151 97b3a7be 2019-07-09 stsp { "backout", cmd_backout, usage_backout, "bo" },
152 818c7501 2019-07-11 stsp { "rebase", cmd_rebase, usage_rebase, "rb" },
153 0ebf8283 2019-07-24 stsp { "histedit", cmd_histedit, usage_histedit, "he" },
154 2822a352 2019-10-15 stsp { "integrate", cmd_integrate, usage_integrate,"ig" },
155 715dc77e 2019-08-03 stsp { "stage", cmd_stage, usage_stage, "sg" },
156 ad493afc 2019-08-03 stsp { "unstage", cmd_unstage, usage_unstage, "ug" },
157 01073a5d 2019-08-22 stsp { "cat", cmd_cat, usage_cat, "" },
158 5c860e29 2018-03-12 stsp };
159 ce5b7c56 2019-07-09 stsp
160 ce5b7c56 2019-07-09 stsp static void
161 ce5b7c56 2019-07-09 stsp list_commands(void)
162 ce5b7c56 2019-07-09 stsp {
163 ce5b7c56 2019-07-09 stsp int i;
164 ce5b7c56 2019-07-09 stsp
165 ce5b7c56 2019-07-09 stsp fprintf(stderr, "commands:");
166 ce5b7c56 2019-07-09 stsp for (i = 0; i < nitems(got_commands); i++) {
167 ce5b7c56 2019-07-09 stsp struct got_cmd *cmd = &got_commands[i];
168 ce5b7c56 2019-07-09 stsp fprintf(stderr, " %s", cmd->cmd_name);
169 ce5b7c56 2019-07-09 stsp }
170 ce5b7c56 2019-07-09 stsp fputc('\n', stderr);
171 ce5b7c56 2019-07-09 stsp }
172 5c860e29 2018-03-12 stsp
173 5c860e29 2018-03-12 stsp int
174 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
175 5c860e29 2018-03-12 stsp {
176 8cfb4057 2019-07-09 stsp struct got_cmd *cmd;
177 5c860e29 2018-03-12 stsp unsigned int i;
178 5c860e29 2018-03-12 stsp int ch;
179 53ccebc2 2019-07-30 stsp int hflag = 0, Vflag = 0;
180 83cd27f8 2020-01-13 stsp static struct option longopts[] = {
181 83cd27f8 2020-01-13 stsp { "version", no_argument, NULL, 'V' },
182 83cd27f8 2020-01-13 stsp { NULL, 0, NULL, 0}
183 83cd27f8 2020-01-13 stsp };
184 5c860e29 2018-03-12 stsp
185 289e3cbf 2019-02-04 stsp setlocale(LC_CTYPE, "");
186 5c860e29 2018-03-12 stsp
187 6586ea88 2020-01-13 stsp while ((ch = getopt_long(argc, argv, "+hV", longopts, NULL)) != -1) {
188 5c860e29 2018-03-12 stsp switch (ch) {
189 1b6b95a8 2018-03-12 stsp case 'h':
190 1b6b95a8 2018-03-12 stsp hflag = 1;
191 53ccebc2 2019-07-30 stsp break;
192 53ccebc2 2019-07-30 stsp case 'V':
193 53ccebc2 2019-07-30 stsp Vflag = 1;
194 1b6b95a8 2018-03-12 stsp break;
195 5c860e29 2018-03-12 stsp default:
196 ce5b7c56 2019-07-09 stsp usage(hflag);
197 5c860e29 2018-03-12 stsp /* NOTREACHED */
198 5c860e29 2018-03-12 stsp }
199 5c860e29 2018-03-12 stsp }
200 5c860e29 2018-03-12 stsp
201 5c860e29 2018-03-12 stsp argc -= optind;
202 5c860e29 2018-03-12 stsp argv += optind;
203 1e70621d 2018-03-27 stsp optind = 0;
204 53ccebc2 2019-07-30 stsp
205 53ccebc2 2019-07-30 stsp if (Vflag) {
206 53ccebc2 2019-07-30 stsp got_version_print_str();
207 53ccebc2 2019-07-30 stsp return 1;
208 53ccebc2 2019-07-30 stsp }
209 5c860e29 2018-03-12 stsp
210 5c860e29 2018-03-12 stsp if (argc <= 0)
211 ce5b7c56 2019-07-09 stsp usage(hflag);
212 5c860e29 2018-03-12 stsp
213 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
214 99437157 2018-11-11 stsp signal(SIGPIPE, catch_sigpipe);
215 99437157 2018-11-11 stsp
216 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
217 d7d4f210 2018-03-12 stsp const struct got_error *error;
218 d7d4f210 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
220 5c860e29 2018-03-12 stsp
221 97b3a7be 2019-07-09 stsp if (strcmp(cmd->cmd_name, argv[0]) != 0 &&
222 97b3a7be 2019-07-09 stsp strcmp(cmd->cmd_alias, argv[0]) != 0)
223 5c860e29 2018-03-12 stsp continue;
224 5c860e29 2018-03-12 stsp
225 1b6b95a8 2018-03-12 stsp if (hflag)
226 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
227 1b6b95a8 2018-03-12 stsp
228 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
229 f8afbdc8 2019-11-08 stsp if (error && error->code != GOT_ERR_CANCELLED &&
230 f8afbdc8 2019-11-08 stsp error->code != GOT_ERR_PRIVSEP_EXIT &&
231 f8afbdc8 2019-11-08 stsp !(sigpipe_received &&
232 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EPIPE) &&
233 70015d7a 2019-11-08 stsp !(sigint_received &&
234 70015d7a 2019-11-08 stsp error->code == GOT_ERR_ERRNO && errno == EINTR)) {
235 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
236 d7d4f210 2018-03-12 stsp return 1;
237 d7d4f210 2018-03-12 stsp }
238 d7d4f210 2018-03-12 stsp
239 d7d4f210 2018-03-12 stsp return 0;
240 5c860e29 2018-03-12 stsp }
241 5c860e29 2018-03-12 stsp
242 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
243 ce5b7c56 2019-07-09 stsp list_commands();
244 5c860e29 2018-03-12 stsp return 1;
245 5c860e29 2018-03-12 stsp }
246 5c860e29 2018-03-12 stsp
247 4ed7e80c 2018-05-20 stsp __dead static void
248 ce5b7c56 2019-07-09 stsp usage(int hflag)
249 5c860e29 2018-03-12 stsp {
250 e801a566 2020-01-13 stsp fprintf(stderr, "usage: %s [-h] [-V | --version] command [arg ...]\n",
251 53ccebc2 2019-07-30 stsp getprogname());
252 ce5b7c56 2019-07-09 stsp if (hflag)
253 ce5b7c56 2019-07-09 stsp list_commands();
254 5c860e29 2018-03-12 stsp exit(1);
255 5c860e29 2018-03-12 stsp }
256 5c860e29 2018-03-12 stsp
257 0266afb7 2019-01-04 stsp static const struct got_error *
258 0ee7065d 2019-05-13 stsp get_editor(char **abspath)
259 e2ba3d07 2019-05-13 stsp {
260 0ee7065d 2019-05-13 stsp const struct got_error *err = NULL;
261 e2ba3d07 2019-05-13 stsp const char *editor;
262 8920fa04 2019-08-18 stsp
263 8920fa04 2019-08-18 stsp *abspath = NULL;
264 e2ba3d07 2019-05-13 stsp
265 e2ba3d07 2019-05-13 stsp editor = getenv("VISUAL");
266 e2ba3d07 2019-05-13 stsp if (editor == NULL)
267 e2ba3d07 2019-05-13 stsp editor = getenv("EDITOR");
268 e2ba3d07 2019-05-13 stsp
269 0ee7065d 2019-05-13 stsp if (editor) {
270 0ee7065d 2019-05-13 stsp err = got_path_find_prog(abspath, editor);
271 0ee7065d 2019-05-13 stsp if (err)
272 0ee7065d 2019-05-13 stsp return err;
273 0ee7065d 2019-05-13 stsp }
274 e2ba3d07 2019-05-13 stsp
275 0ee7065d 2019-05-13 stsp if (*abspath == NULL) {
276 0ee7065d 2019-05-13 stsp *abspath = strdup("/bin/ed");
277 0ee7065d 2019-05-13 stsp if (*abspath == NULL)
278 0ee7065d 2019-05-13 stsp return got_error_from_errno("strdup");
279 0ee7065d 2019-05-13 stsp }
280 0ee7065d 2019-05-13 stsp
281 e2ba3d07 2019-05-13 stsp return NULL;
282 e2ba3d07 2019-05-13 stsp }
283 e2ba3d07 2019-05-13 stsp
284 e2ba3d07 2019-05-13 stsp static const struct got_error *
285 d0eebce4 2019-03-11 stsp apply_unveil(const char *repo_path, int repo_read_only,
286 c530dc23 2019-07-23 stsp const char *worktree_path)
287 0266afb7 2019-01-04 stsp {
288 163ce85a 2019-05-13 stsp const struct got_error *err;
289 0266afb7 2019-01-04 stsp
290 37c06ea4 2019-07-15 stsp #ifdef PROFILE
291 37c06ea4 2019-07-15 stsp if (unveil("gmon.out", "rwc") != 0)
292 37c06ea4 2019-07-15 stsp return got_error_from_errno2("unveil", "gmon.out");
293 37c06ea4 2019-07-15 stsp #endif
294 d0eebce4 2019-03-11 stsp if (repo_path && unveil(repo_path, repo_read_only ? "r" : "rwc") != 0)
295 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", repo_path);
296 0266afb7 2019-01-04 stsp
297 0266afb7 2019-01-04 stsp if (worktree_path && unveil(worktree_path, "rwc") != 0)
298 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", worktree_path);
299 0266afb7 2019-01-04 stsp
300 f12d0dbe 2019-01-04 stsp if (unveil("/tmp", "rwc") != 0)
301 638f9024 2019-05-13 stsp return got_error_from_errno2("unveil", "/tmp");
302 0266afb7 2019-01-04 stsp
303 163ce85a 2019-05-13 stsp err = got_privsep_unveil_exec_helpers();
304 163ce85a 2019-05-13 stsp if (err != NULL)
305 163ce85a 2019-05-13 stsp return err;
306 0266afb7 2019-01-04 stsp
307 0266afb7 2019-01-04 stsp if (unveil(NULL, NULL) != 0)
308 638f9024 2019-05-13 stsp return got_error_from_errno("unveil");
309 0266afb7 2019-01-04 stsp
310 0266afb7 2019-01-04 stsp return NULL;
311 2c7829a4 2019-06-17 stsp }
312 2c7829a4 2019-06-17 stsp
313 2c7829a4 2019-06-17 stsp __dead static void
314 2c7829a4 2019-06-17 stsp usage_init(void)
315 2c7829a4 2019-06-17 stsp {
316 09ea71ba 2019-07-27 stsp fprintf(stderr, "usage: %s init repository-path\n", getprogname());
317 2c7829a4 2019-06-17 stsp exit(1);
318 2c7829a4 2019-06-17 stsp }
319 2c7829a4 2019-06-17 stsp
320 2c7829a4 2019-06-17 stsp static const struct got_error *
321 2c7829a4 2019-06-17 stsp cmd_init(int argc, char *argv[])
322 2c7829a4 2019-06-17 stsp {
323 2c7829a4 2019-06-17 stsp const struct got_error *error = NULL;
324 2c7829a4 2019-06-17 stsp char *repo_path = NULL;
325 2c7829a4 2019-06-17 stsp int ch;
326 2c7829a4 2019-06-17 stsp
327 2c7829a4 2019-06-17 stsp while ((ch = getopt(argc, argv, "")) != -1) {
328 2c7829a4 2019-06-17 stsp switch (ch) {
329 2c7829a4 2019-06-17 stsp default:
330 2c7829a4 2019-06-17 stsp usage_init();
331 2c7829a4 2019-06-17 stsp /* NOTREACHED */
332 2c7829a4 2019-06-17 stsp }
333 2c7829a4 2019-06-17 stsp }
334 2c7829a4 2019-06-17 stsp
335 2c7829a4 2019-06-17 stsp argc -= optind;
336 2c7829a4 2019-06-17 stsp argv += optind;
337 2c7829a4 2019-06-17 stsp
338 2c7829a4 2019-06-17 stsp #ifndef PROFILE
339 2c7829a4 2019-06-17 stsp if (pledge("stdio rpath wpath cpath unveil", NULL) == -1)
340 2c7829a4 2019-06-17 stsp err(1, "pledge");
341 2c7829a4 2019-06-17 stsp #endif
342 2c7829a4 2019-06-17 stsp if (argc != 1)
343 bc20e173 2019-06-17 stsp usage_init();
344 2c7829a4 2019-06-17 stsp
345 2c7829a4 2019-06-17 stsp repo_path = strdup(argv[0]);
346 2c7829a4 2019-06-17 stsp if (repo_path == NULL)
347 2c7829a4 2019-06-17 stsp return got_error_from_errno("strdup");
348 2c7829a4 2019-06-17 stsp
349 2c7829a4 2019-06-17 stsp got_path_strip_trailing_slashes(repo_path);
350 2c7829a4 2019-06-17 stsp
351 2c7829a4 2019-06-17 stsp error = got_path_mkdir(repo_path);
352 2c7829a4 2019-06-17 stsp if (error &&
353 2c7829a4 2019-06-17 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
354 2c7829a4 2019-06-17 stsp goto done;
355 2c7829a4 2019-06-17 stsp
356 c530dc23 2019-07-23 stsp error = apply_unveil(repo_path, 0, NULL);
357 2c7829a4 2019-06-17 stsp if (error)
358 2c7829a4 2019-06-17 stsp goto done;
359 2c7829a4 2019-06-17 stsp
360 2c7829a4 2019-06-17 stsp error = got_repo_init(repo_path);
361 3ce1b845 2019-07-15 stsp done:
362 3ce1b845 2019-07-15 stsp free(repo_path);
363 3ce1b845 2019-07-15 stsp return error;
364 3ce1b845 2019-07-15 stsp }
365 3ce1b845 2019-07-15 stsp
366 3ce1b845 2019-07-15 stsp __dead static void
367 3ce1b845 2019-07-15 stsp usage_import(void)
368 3ce1b845 2019-07-15 stsp {
369 3ce1b845 2019-07-15 stsp fprintf(stderr, "usage: %s import [-b branch] [-m message] "
370 3ce1b845 2019-07-15 stsp "[-r repository-path] [-I pattern] path\n", getprogname());
371 3ce1b845 2019-07-15 stsp exit(1);
372 3ce1b845 2019-07-15 stsp }
373 3ce1b845 2019-07-15 stsp
374 3ce1b845 2019-07-15 stsp int
375 3ce1b845 2019-07-15 stsp spawn_editor(const char *editor, const char *file)
376 3ce1b845 2019-07-15 stsp {
377 3ce1b845 2019-07-15 stsp pid_t pid;
378 3ce1b845 2019-07-15 stsp sig_t sighup, sigint, sigquit;
379 3ce1b845 2019-07-15 stsp int st = -1;
380 3ce1b845 2019-07-15 stsp
381 3ce1b845 2019-07-15 stsp sighup = signal(SIGHUP, SIG_IGN);
382 3ce1b845 2019-07-15 stsp sigint = signal(SIGINT, SIG_IGN);
383 3ce1b845 2019-07-15 stsp sigquit = signal(SIGQUIT, SIG_IGN);
384 3ce1b845 2019-07-15 stsp
385 3ce1b845 2019-07-15 stsp switch (pid = fork()) {
386 3ce1b845 2019-07-15 stsp case -1:
387 3ce1b845 2019-07-15 stsp goto doneediting;
388 3ce1b845 2019-07-15 stsp case 0:
389 3ce1b845 2019-07-15 stsp execl(editor, editor, file, (char *)NULL);
390 3ce1b845 2019-07-15 stsp _exit(127);
391 3ce1b845 2019-07-15 stsp }
392 3ce1b845 2019-07-15 stsp
393 3ce1b845 2019-07-15 stsp while (waitpid(pid, &st, 0) == -1)
394 3ce1b845 2019-07-15 stsp if (errno != EINTR)
395 3ce1b845 2019-07-15 stsp break;
396 3ce1b845 2019-07-15 stsp
397 3ce1b845 2019-07-15 stsp doneediting:
398 3ce1b845 2019-07-15 stsp (void)signal(SIGHUP, sighup);
399 3ce1b845 2019-07-15 stsp (void)signal(SIGINT, sigint);
400 3ce1b845 2019-07-15 stsp (void)signal(SIGQUIT, sigquit);
401 3ce1b845 2019-07-15 stsp
402 3ce1b845 2019-07-15 stsp if (!WIFEXITED(st)) {
403 3ce1b845 2019-07-15 stsp errno = EINTR;
404 3ce1b845 2019-07-15 stsp return -1;
405 3ce1b845 2019-07-15 stsp }
406 3ce1b845 2019-07-15 stsp
407 3ce1b845 2019-07-15 stsp return WEXITSTATUS(st);
408 3ce1b845 2019-07-15 stsp }
409 3ce1b845 2019-07-15 stsp
410 3ce1b845 2019-07-15 stsp static const struct got_error *
411 3ce1b845 2019-07-15 stsp edit_logmsg(char **logmsg, const char *editor, const char *logmsg_path,
412 3ce1b845 2019-07-15 stsp const char *initial_content)
413 3ce1b845 2019-07-15 stsp {
414 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
415 3ce1b845 2019-07-15 stsp char buf[1024];
416 3ce1b845 2019-07-15 stsp struct stat st, st2;
417 3ce1b845 2019-07-15 stsp FILE *fp;
418 3ce1b845 2019-07-15 stsp int content_changed = 0;
419 3ce1b845 2019-07-15 stsp size_t len;
420 3ce1b845 2019-07-15 stsp
421 3ce1b845 2019-07-15 stsp *logmsg = NULL;
422 3ce1b845 2019-07-15 stsp
423 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st) == -1)
424 3ce1b845 2019-07-15 stsp return got_error_from_errno2("stat", logmsg_path);
425 3ce1b845 2019-07-15 stsp
426 3ce1b845 2019-07-15 stsp if (spawn_editor(editor, logmsg_path) == -1)
427 3ce1b845 2019-07-15 stsp return got_error_from_errno("failed spawning editor");
428 3ce1b845 2019-07-15 stsp
429 3ce1b845 2019-07-15 stsp if (stat(logmsg_path, &st2) == -1)
430 3ce1b845 2019-07-15 stsp return got_error_from_errno("stat");
431 3ce1b845 2019-07-15 stsp
432 3ce1b845 2019-07-15 stsp if (st.st_mtime == st2.st_mtime && st.st_size == st2.st_size)
433 3ce1b845 2019-07-15 stsp return got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
434 3ce1b845 2019-07-15 stsp "no changes made to commit message, aborting");
435 3ce1b845 2019-07-15 stsp
436 3ce1b845 2019-07-15 stsp *logmsg = malloc(st2.st_size + 1);
437 3ce1b845 2019-07-15 stsp if (*logmsg == NULL)
438 3ce1b845 2019-07-15 stsp return got_error_from_errno("malloc");
439 3ce1b845 2019-07-15 stsp (*logmsg)[0] = '\0';
440 3ce1b845 2019-07-15 stsp len = 0;
441 3ce1b845 2019-07-15 stsp
442 3ce1b845 2019-07-15 stsp fp = fopen(logmsg_path, "r");
443 3ce1b845 2019-07-15 stsp if (fp == NULL) {
444 3ce1b845 2019-07-15 stsp err = got_error_from_errno("fopen");
445 3ce1b845 2019-07-15 stsp goto done;
446 3ce1b845 2019-07-15 stsp }
447 3ce1b845 2019-07-15 stsp while (fgets(buf, sizeof(buf), fp) != NULL) {
448 3ce1b845 2019-07-15 stsp if (!content_changed && strcmp(buf, initial_content) != 0)
449 3ce1b845 2019-07-15 stsp content_changed = 1;
450 3ce1b845 2019-07-15 stsp if (buf[0] == '#' || (len == 0 && buf[0] == '\n'))
451 3ce1b845 2019-07-15 stsp continue; /* remove comments and leading empty lines */
452 3ce1b845 2019-07-15 stsp len = strlcat(*logmsg, buf, st2.st_size);
453 3ce1b845 2019-07-15 stsp }
454 3ce1b845 2019-07-15 stsp fclose(fp);
455 3ce1b845 2019-07-15 stsp
456 3ce1b845 2019-07-15 stsp while (len > 0 && (*logmsg)[len - 1] == '\n') {
457 3ce1b845 2019-07-15 stsp (*logmsg)[len - 1] = '\0';
458 3ce1b845 2019-07-15 stsp len--;
459 3ce1b845 2019-07-15 stsp }
460 3ce1b845 2019-07-15 stsp
461 3ce1b845 2019-07-15 stsp if (len == 0 || !content_changed)
462 3ce1b845 2019-07-15 stsp err = got_error_msg(GOT_ERR_COMMIT_MSG_EMPTY,
463 3ce1b845 2019-07-15 stsp "commit message cannot be empty, aborting");
464 3ce1b845 2019-07-15 stsp done:
465 3ce1b845 2019-07-15 stsp if (err) {
466 3ce1b845 2019-07-15 stsp free(*logmsg);
467 3ce1b845 2019-07-15 stsp *logmsg = NULL;
468 3ce1b845 2019-07-15 stsp }
469 3ce1b845 2019-07-15 stsp return err;
470 3ce1b845 2019-07-15 stsp }
471 3ce1b845 2019-07-15 stsp
472 3ce1b845 2019-07-15 stsp static const struct got_error *
473 ef293bdd 2019-10-21 stsp collect_import_msg(char **logmsg, char **logmsg_path, const char *editor,
474 ef293bdd 2019-10-21 stsp const char *path_dir, const char *branch_name)
475 3ce1b845 2019-07-15 stsp {
476 ef293bdd 2019-10-21 stsp char *initial_content = NULL;
477 3ce1b845 2019-07-15 stsp const struct got_error *err = NULL;
478 3ce1b845 2019-07-15 stsp int fd;
479 3ce1b845 2019-07-15 stsp
480 3ce1b845 2019-07-15 stsp if (asprintf(&initial_content,
481 3ce1b845 2019-07-15 stsp "\n# %s to be imported to branch %s\n", path_dir,
482 3ce1b845 2019-07-15 stsp branch_name) == -1)
483 3ce1b845 2019-07-15 stsp return got_error_from_errno("asprintf");
484 3ce1b845 2019-07-15 stsp
485 ef293bdd 2019-10-21 stsp err = got_opentemp_named_fd(logmsg_path, &fd, "/tmp/got-importmsg");
486 3ce1b845 2019-07-15 stsp if (err)
487 3ce1b845 2019-07-15 stsp goto done;
488 3ce1b845 2019-07-15 stsp
489 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
490 3ce1b845 2019-07-15 stsp close(fd);
491 3ce1b845 2019-07-15 stsp
492 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
493 3ce1b845 2019-07-15 stsp done:
494 3ce1b845 2019-07-15 stsp free(initial_content);
495 3ce1b845 2019-07-15 stsp return err;
496 3ce1b845 2019-07-15 stsp }
497 3ce1b845 2019-07-15 stsp
498 3ce1b845 2019-07-15 stsp static const struct got_error *
499 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
500 3ce1b845 2019-07-15 stsp {
501 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
502 3ce1b845 2019-07-15 stsp return NULL;
503 3ce1b845 2019-07-15 stsp }
504 3ce1b845 2019-07-15 stsp
505 3ce1b845 2019-07-15 stsp static const struct got_error *
506 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
507 84792843 2019-08-09 stsp {
508 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
509 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
510 84792843 2019-08-09 stsp
511 84792843 2019-08-09 stsp *author = NULL;
512 aba9c984 2019-09-08 stsp
513 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
514 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
515 c9956ddf 2019-09-08 stsp if (name && email) {
516 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
517 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
518 aba9c984 2019-09-08 stsp return NULL;
519 aba9c984 2019-09-08 stsp }
520 84792843 2019-08-09 stsp
521 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
522 84792843 2019-08-09 stsp if (got_author == NULL) {
523 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
524 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
525 c9956ddf 2019-09-08 stsp if (name && email) {
526 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
527 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
528 c9956ddf 2019-09-08 stsp return NULL;
529 c9956ddf 2019-09-08 stsp }
530 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
531 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
532 84792843 2019-08-09 stsp }
533 84792843 2019-08-09 stsp
534 aba9c984 2019-09-08 stsp *author = strdup(got_author);
535 aba9c984 2019-09-08 stsp if (*author == NULL)
536 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
537 84792843 2019-08-09 stsp
538 84792843 2019-08-09 stsp /*
539 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
540 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
541 84792843 2019-08-09 stsp */
542 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
543 84792843 2019-08-09 stsp got_author++;
544 aba9c984 2019-09-08 stsp if (*got_author != '<') {
545 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
546 aba9c984 2019-09-08 stsp goto done;
547 aba9c984 2019-09-08 stsp }
548 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
549 84792843 2019-08-09 stsp got_author++;
550 aba9c984 2019-09-08 stsp if (*got_author != '@') {
551 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
552 aba9c984 2019-09-08 stsp goto done;
553 aba9c984 2019-09-08 stsp }
554 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
555 84792843 2019-08-09 stsp got_author++;
556 84792843 2019-08-09 stsp if (*got_author != '>')
557 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
558 aba9c984 2019-09-08 stsp done:
559 aba9c984 2019-09-08 stsp if (err) {
560 aba9c984 2019-09-08 stsp free(*author);
561 aba9c984 2019-09-08 stsp *author = NULL;
562 aba9c984 2019-09-08 stsp }
563 aba9c984 2019-09-08 stsp return err;
564 c9956ddf 2019-09-08 stsp }
565 c9956ddf 2019-09-08 stsp
566 c9956ddf 2019-09-08 stsp static const struct got_error *
567 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
568 c9956ddf 2019-09-08 stsp {
569 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
570 c9956ddf 2019-09-08 stsp
571 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
572 c9956ddf 2019-09-08 stsp if (homedir) {
573 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
574 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
575 c9956ddf 2019-09-08 stsp
576 c9956ddf 2019-09-08 stsp }
577 c9956ddf 2019-09-08 stsp return NULL;
578 84792843 2019-08-09 stsp }
579 84792843 2019-08-09 stsp
580 84792843 2019-08-09 stsp static const struct got_error *
581 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
582 3ce1b845 2019-07-15 stsp {
583 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
584 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
585 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
586 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
587 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
588 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
589 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
590 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
591 3ce1b845 2019-07-15 stsp int ch;
592 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
593 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
594 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
595 3ce1b845 2019-07-15 stsp
596 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
597 3ce1b845 2019-07-15 stsp
598 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
599 3ce1b845 2019-07-15 stsp switch (ch) {
600 3ce1b845 2019-07-15 stsp case 'b':
601 3ce1b845 2019-07-15 stsp branch_name = optarg;
602 3ce1b845 2019-07-15 stsp break;
603 3ce1b845 2019-07-15 stsp case 'm':
604 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
605 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
606 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
607 3ce1b845 2019-07-15 stsp goto done;
608 3ce1b845 2019-07-15 stsp }
609 3ce1b845 2019-07-15 stsp break;
610 3ce1b845 2019-07-15 stsp case 'r':
611 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
612 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
613 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
614 9ba1d308 2019-10-21 stsp optarg);
615 3ce1b845 2019-07-15 stsp goto done;
616 3ce1b845 2019-07-15 stsp }
617 3ce1b845 2019-07-15 stsp break;
618 3ce1b845 2019-07-15 stsp case 'I':
619 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
620 3ce1b845 2019-07-15 stsp break;
621 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
622 3ce1b845 2019-07-15 stsp NULL);
623 3ce1b845 2019-07-15 stsp if (error)
624 3ce1b845 2019-07-15 stsp goto done;
625 3ce1b845 2019-07-15 stsp break;
626 3ce1b845 2019-07-15 stsp default:
627 b2b65d18 2019-08-22 stsp usage_import();
628 3ce1b845 2019-07-15 stsp /* NOTREACHED */
629 3ce1b845 2019-07-15 stsp }
630 3ce1b845 2019-07-15 stsp }
631 3ce1b845 2019-07-15 stsp
632 3ce1b845 2019-07-15 stsp argc -= optind;
633 3ce1b845 2019-07-15 stsp argv += optind;
634 3ce1b845 2019-07-15 stsp
635 3ce1b845 2019-07-15 stsp #ifndef PROFILE
636 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
637 aba9c984 2019-09-08 stsp "unveil",
638 3ce1b845 2019-07-15 stsp NULL) == -1)
639 3ce1b845 2019-07-15 stsp err(1, "pledge");
640 3ce1b845 2019-07-15 stsp #endif
641 3ce1b845 2019-07-15 stsp if (argc != 1)
642 3ce1b845 2019-07-15 stsp usage_import();
643 2c7829a4 2019-06-17 stsp
644 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
645 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
646 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
647 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
648 3ce1b845 2019-07-15 stsp }
649 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
650 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
651 c9956ddf 2019-09-08 stsp if (error)
652 c9956ddf 2019-09-08 stsp goto done;
653 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
654 3ce1b845 2019-07-15 stsp if (error)
655 3ce1b845 2019-07-15 stsp goto done;
656 aba9c984 2019-09-08 stsp
657 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
658 aba9c984 2019-09-08 stsp if (error)
659 aba9c984 2019-09-08 stsp return error;
660 e560b7e0 2019-11-28 stsp
661 e560b7e0 2019-11-28 stsp /*
662 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
663 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
664 e560b7e0 2019-11-28 stsp * an unintended typo.
665 e560b7e0 2019-11-28 stsp */
666 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
667 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
668 3ce1b845 2019-07-15 stsp
669 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
670 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
671 3ce1b845 2019-07-15 stsp goto done;
672 3ce1b845 2019-07-15 stsp }
673 3ce1b845 2019-07-15 stsp
674 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
675 3ce1b845 2019-07-15 stsp if (error) {
676 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
677 3ce1b845 2019-07-15 stsp goto done;
678 3ce1b845 2019-07-15 stsp } else {
679 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
680 3ce1b845 2019-07-15 stsp "import target branch already exists");
681 3ce1b845 2019-07-15 stsp goto done;
682 3ce1b845 2019-07-15 stsp }
683 3ce1b845 2019-07-15 stsp
684 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
685 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
686 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
687 3ce1b845 2019-07-15 stsp goto done;
688 3ce1b845 2019-07-15 stsp }
689 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
690 3ce1b845 2019-07-15 stsp
691 3ce1b845 2019-07-15 stsp /*
692 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
693 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
694 3ce1b845 2019-07-15 stsp */
695 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
696 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
697 3ce1b845 2019-07-15 stsp if (error)
698 3ce1b845 2019-07-15 stsp goto done;
699 8e158b01 2019-09-22 stsp free(logmsg);
700 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
701 ef293bdd 2019-10-21 stsp path_dir, refname);
702 ef293bdd 2019-10-21 stsp if (error) {
703 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
704 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
705 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
706 3ce1b845 2019-07-15 stsp goto done;
707 ef293bdd 2019-10-21 stsp }
708 3ce1b845 2019-07-15 stsp }
709 3ce1b845 2019-07-15 stsp
710 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
711 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
712 ef293bdd 2019-10-21 stsp if (logmsg_path)
713 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
714 3ce1b845 2019-07-15 stsp goto done;
715 ef293bdd 2019-10-21 stsp }
716 3ce1b845 2019-07-15 stsp
717 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
718 ef293bdd 2019-10-21 stsp if (error) {
719 ef293bdd 2019-10-21 stsp if (logmsg_path)
720 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
721 ef293bdd 2019-10-21 stsp goto done;
722 ef293bdd 2019-10-21 stsp }
723 ef293bdd 2019-10-21 stsp
724 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
725 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
726 ef293bdd 2019-10-21 stsp if (error) {
727 ef293bdd 2019-10-21 stsp if (logmsg_path)
728 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
729 3ce1b845 2019-07-15 stsp goto done;
730 ef293bdd 2019-10-21 stsp }
731 3ce1b845 2019-07-15 stsp
732 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
733 ef293bdd 2019-10-21 stsp if (error) {
734 ef293bdd 2019-10-21 stsp if (logmsg_path)
735 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
736 3ce1b845 2019-07-15 stsp goto done;
737 ef293bdd 2019-10-21 stsp }
738 3ce1b845 2019-07-15 stsp
739 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
740 ef293bdd 2019-10-21 stsp if (error) {
741 ef293bdd 2019-10-21 stsp if (logmsg_path)
742 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
743 3ce1b845 2019-07-15 stsp goto done;
744 ef293bdd 2019-10-21 stsp }
745 3ce1b845 2019-07-15 stsp
746 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
747 ef293bdd 2019-10-21 stsp if (error) {
748 ef293bdd 2019-10-21 stsp if (logmsg_path)
749 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
750 3ce1b845 2019-07-15 stsp goto done;
751 ef293bdd 2019-10-21 stsp }
752 3ce1b845 2019-07-15 stsp
753 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
754 3ce1b845 2019-07-15 stsp if (error) {
755 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
756 ef293bdd 2019-10-21 stsp if (logmsg_path)
757 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
758 3ce1b845 2019-07-15 stsp goto done;
759 ef293bdd 2019-10-21 stsp }
760 3ce1b845 2019-07-15 stsp
761 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
762 3ce1b845 2019-07-15 stsp branch_ref);
763 ef293bdd 2019-10-21 stsp if (error) {
764 ef293bdd 2019-10-21 stsp if (logmsg_path)
765 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
766 3ce1b845 2019-07-15 stsp goto done;
767 ef293bdd 2019-10-21 stsp }
768 3ce1b845 2019-07-15 stsp
769 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
770 ef293bdd 2019-10-21 stsp if (error) {
771 ef293bdd 2019-10-21 stsp if (logmsg_path)
772 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
773 3ce1b845 2019-07-15 stsp goto done;
774 ef293bdd 2019-10-21 stsp }
775 3ce1b845 2019-07-15 stsp }
776 3ce1b845 2019-07-15 stsp
777 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
778 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
779 2c7829a4 2019-06-17 stsp done:
780 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
781 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
782 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
783 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
784 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
785 8e158b01 2019-09-22 stsp free(logmsg);
786 ef293bdd 2019-10-21 stsp free(logmsg_path);
787 2c7829a4 2019-06-17 stsp free(repo_path);
788 3ce1b845 2019-07-15 stsp free(editor);
789 3ce1b845 2019-07-15 stsp free(refname);
790 3ce1b845 2019-07-15 stsp free(new_commit_id);
791 3ce1b845 2019-07-15 stsp free(id_str);
792 aba9c984 2019-09-08 stsp free(author);
793 c9956ddf 2019-09-08 stsp free(gitconfig_path);
794 3ce1b845 2019-07-15 stsp if (branch_ref)
795 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
796 3ce1b845 2019-07-15 stsp if (head_ref)
797 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
798 2c7829a4 2019-06-17 stsp return error;
799 0266afb7 2019-01-04 stsp }
800 0266afb7 2019-01-04 stsp
801 4ed7e80c 2018-05-20 stsp __dead static void
802 c09a553d 2018-03-12 stsp usage_checkout(void)
803 c09a553d 2018-03-12 stsp {
804 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
805 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
806 c09a553d 2018-03-12 stsp exit(1);
807 92a684f4 2018-03-12 stsp }
808 92a684f4 2018-03-12 stsp
809 7f47418f 2019-12-20 stsp static void
810 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
811 7f47418f 2019-12-20 stsp {
812 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
813 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
814 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
815 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
816 7f47418f 2019-12-20 stsp getprogname());
817 7f47418f 2019-12-20 stsp }
818 7f47418f 2019-12-20 stsp
819 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
820 7f47418f 2019-12-20 stsp const char *worktree_path;
821 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
822 7f47418f 2019-12-20 stsp };
823 7f47418f 2019-12-20 stsp
824 1ee397ad 2019-07-12 stsp static const struct got_error *
825 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
826 92a684f4 2018-03-12 stsp {
827 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
828 a484d721 2019-06-10 stsp
829 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
830 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
831 7f47418f 2019-12-20 stsp return NULL;
832 7f47418f 2019-12-20 stsp
833 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
834 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
835 1ee397ad 2019-07-12 stsp return NULL;
836 7f47418f 2019-12-20 stsp }
837 92a684f4 2018-03-12 stsp
838 92a684f4 2018-03-12 stsp while (path[0] == '/')
839 92a684f4 2018-03-12 stsp path++;
840 92a684f4 2018-03-12 stsp
841 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
842 1ee397ad 2019-07-12 stsp return NULL;
843 99437157 2018-11-11 stsp }
844 99437157 2018-11-11 stsp
845 99437157 2018-11-11 stsp static const struct got_error *
846 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
847 99437157 2018-11-11 stsp {
848 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
849 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
850 99437157 2018-11-11 stsp return NULL;
851 8069f636 2019-01-12 stsp }
852 8069f636 2019-01-12 stsp
853 8069f636 2019-01-12 stsp static const struct got_error *
854 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
855 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
856 3aef623b 2019-10-15 stsp struct got_repository *repo)
857 8069f636 2019-01-12 stsp {
858 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
859 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
860 8069f636 2019-01-12 stsp
861 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
862 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
863 36a38700 2019-05-10 stsp if (err)
864 36a38700 2019-05-10 stsp return err;
865 8069f636 2019-01-12 stsp
866 d5bea539 2019-05-13 stsp if (yca_id == NULL)
867 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
868 8069f636 2019-01-12 stsp
869 d5bea539 2019-05-13 stsp /*
870 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
871 d5bea539 2019-05-13 stsp * and the work tree's base commit.
872 d5bea539 2019-05-13 stsp *
873 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
874 d5bea539 2019-05-13 stsp *
875 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
876 d5bea539 2019-05-13 stsp * \ /
877 d5bea539 2019-05-13 stsp * C E
878 d5bea539 2019-05-13 stsp * \ /
879 d5bea539 2019-05-13 stsp * B (yca)
880 d5bea539 2019-05-13 stsp * |
881 d5bea539 2019-05-13 stsp * A
882 d5bea539 2019-05-13 stsp *
883 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
884 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
885 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
886 d5bea539 2019-05-13 stsp */
887 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
888 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
889 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
890 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
891 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
892 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
893 8069f636 2019-01-12 stsp
894 d5bea539 2019-05-13 stsp free(yca_id);
895 d5bea539 2019-05-13 stsp return NULL;
896 c09a553d 2018-03-12 stsp }
897 a367ff0f 2019-05-14 stsp
898 a367ff0f 2019-05-14 stsp static const struct got_error *
899 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
900 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
901 a51a74b3 2019-07-27 stsp struct got_repository *repo)
902 a367ff0f 2019-05-14 stsp {
903 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
904 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
905 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
906 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
907 a367ff0f 2019-05-14 stsp
908 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
909 a367ff0f 2019-05-14 stsp if (err)
910 a51a74b3 2019-07-27 stsp goto done;
911 a51a74b3 2019-07-27 stsp
912 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
913 a51a74b3 2019-07-27 stsp is_same_branch = 1;
914 a51a74b3 2019-07-27 stsp goto done;
915 a51a74b3 2019-07-27 stsp }
916 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
917 a51a74b3 2019-07-27 stsp is_same_branch = 1;
918 a367ff0f 2019-05-14 stsp goto done;
919 a51a74b3 2019-07-27 stsp }
920 a367ff0f 2019-05-14 stsp
921 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
922 a367ff0f 2019-05-14 stsp if (err)
923 a367ff0f 2019-05-14 stsp goto done;
924 a367ff0f 2019-05-14 stsp
925 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
926 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
927 a367ff0f 2019-05-14 stsp if (err)
928 a367ff0f 2019-05-14 stsp goto done;
929 a367ff0f 2019-05-14 stsp
930 a367ff0f 2019-05-14 stsp for (;;) {
931 a367ff0f 2019-05-14 stsp struct got_object_id *id;
932 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
933 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
934 a367ff0f 2019-05-14 stsp if (err) {
935 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
936 a367ff0f 2019-05-14 stsp err = NULL;
937 ee780d5c 2020-01-04 stsp break;
938 a367ff0f 2019-05-14 stsp }
939 c09a553d 2018-03-12 stsp
940 a367ff0f 2019-05-14 stsp if (id) {
941 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
942 a51a74b3 2019-07-27 stsp break;
943 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
944 a367ff0f 2019-05-14 stsp is_same_branch = 1;
945 a367ff0f 2019-05-14 stsp break;
946 a367ff0f 2019-05-14 stsp }
947 a367ff0f 2019-05-14 stsp }
948 a367ff0f 2019-05-14 stsp }
949 a367ff0f 2019-05-14 stsp done:
950 a367ff0f 2019-05-14 stsp if (graph)
951 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
952 a367ff0f 2019-05-14 stsp free(head_commit_id);
953 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
954 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
955 a367ff0f 2019-05-14 stsp return err;
956 a367ff0f 2019-05-14 stsp }
957 8069f636 2019-01-12 stsp
958 4ed7e80c 2018-05-20 stsp static const struct got_error *
959 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
960 c09a553d 2018-03-12 stsp {
961 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
962 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
963 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
964 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
965 c09a553d 2018-03-12 stsp char *repo_path = NULL;
966 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
967 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
968 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
969 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
970 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
971 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
972 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
973 f2ea84fa 2019-07-27 stsp
974 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
975 c09a553d 2018-03-12 stsp
976 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
977 0bb8a95e 2018-03-12 stsp switch (ch) {
978 08573d5b 2019-05-14 stsp case 'b':
979 08573d5b 2019-05-14 stsp branch_name = optarg;
980 08573d5b 2019-05-14 stsp break;
981 8069f636 2019-01-12 stsp case 'c':
982 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
983 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
984 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
985 bb51a5b4 2020-01-13 stsp break;
986 bb51a5b4 2020-01-13 stsp case 'E':
987 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
988 8069f636 2019-01-12 stsp break;
989 0bb8a95e 2018-03-12 stsp case 'p':
990 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
991 0bb8a95e 2018-03-12 stsp break;
992 0bb8a95e 2018-03-12 stsp default:
993 2deda0b9 2019-03-07 stsp usage_checkout();
994 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
995 0bb8a95e 2018-03-12 stsp }
996 0bb8a95e 2018-03-12 stsp }
997 0bb8a95e 2018-03-12 stsp
998 0bb8a95e 2018-03-12 stsp argc -= optind;
999 0bb8a95e 2018-03-12 stsp argv += optind;
1000 0bb8a95e 2018-03-12 stsp
1001 6715a751 2018-03-16 stsp #ifndef PROFILE
1002 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1003 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1004 c09a553d 2018-03-12 stsp err(1, "pledge");
1005 6715a751 2018-03-16 stsp #endif
1006 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1007 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1008 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1009 76089277 2018-04-01 stsp if (repo_path == NULL)
1010 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1011 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1012 76089277 2018-04-01 stsp if (cwd == NULL) {
1013 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1014 76089277 2018-04-01 stsp goto done;
1015 76089277 2018-04-01 stsp }
1016 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1017 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1018 230a42bd 2019-05-11 jcs if (base == NULL) {
1019 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1020 230a42bd 2019-05-11 jcs path_prefix);
1021 230a42bd 2019-05-11 jcs goto done;
1022 230a42bd 2019-05-11 jcs }
1023 230a42bd 2019-05-11 jcs } else {
1024 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1025 230a42bd 2019-05-11 jcs if (base == NULL) {
1026 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1027 230a42bd 2019-05-11 jcs repo_path);
1028 230a42bd 2019-05-11 jcs goto done;
1029 230a42bd 2019-05-11 jcs }
1030 76089277 2018-04-01 stsp }
1031 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1032 c09a553d 2018-03-12 stsp if (dotgit)
1033 c09a553d 2018-03-12 stsp *dotgit = '\0';
1034 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1035 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1036 c09a553d 2018-03-12 stsp free(cwd);
1037 76089277 2018-04-01 stsp goto done;
1038 c09a553d 2018-03-12 stsp }
1039 c09a553d 2018-03-12 stsp free(cwd);
1040 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1041 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1042 76089277 2018-04-01 stsp if (repo_path == NULL) {
1043 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1044 76089277 2018-04-01 stsp goto done;
1045 76089277 2018-04-01 stsp }
1046 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1047 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1048 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1049 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1050 b4b3a7dd 2019-07-22 stsp argv[1]);
1051 b4b3a7dd 2019-07-22 stsp goto done;
1052 b4b3a7dd 2019-07-22 stsp }
1053 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1054 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1055 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1056 b4b3a7dd 2019-07-22 stsp goto done;
1057 b4b3a7dd 2019-07-22 stsp }
1058 76089277 2018-04-01 stsp }
1059 c09a553d 2018-03-12 stsp } else
1060 c09a553d 2018-03-12 stsp usage_checkout();
1061 c09a553d 2018-03-12 stsp
1062 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1063 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1064 13bfb272 2019-05-10 jcs
1065 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1066 c09a553d 2018-03-12 stsp if (error != NULL)
1067 c02c541e 2019-03-29 stsp goto done;
1068 c02c541e 2019-03-29 stsp
1069 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1070 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1071 c530dc23 2019-07-23 stsp if (error) {
1072 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1073 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1074 c530dc23 2019-07-23 stsp goto done;
1075 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1076 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1077 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1078 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1079 c530dc23 2019-07-23 stsp goto done;
1080 c530dc23 2019-07-23 stsp }
1081 c530dc23 2019-07-23 stsp }
1082 c530dc23 2019-07-23 stsp
1083 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1084 c02c541e 2019-03-29 stsp if (error)
1085 c09a553d 2018-03-12 stsp goto done;
1086 8069f636 2019-01-12 stsp
1087 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1088 c09a553d 2018-03-12 stsp if (error != NULL)
1089 c09a553d 2018-03-12 stsp goto done;
1090 c09a553d 2018-03-12 stsp
1091 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1092 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1093 c09a553d 2018-03-12 stsp goto done;
1094 c09a553d 2018-03-12 stsp
1095 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1096 c09a553d 2018-03-12 stsp if (error != NULL)
1097 c09a553d 2018-03-12 stsp goto done;
1098 c09a553d 2018-03-12 stsp
1099 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1100 e5dc7198 2018-12-29 stsp path_prefix);
1101 e5dc7198 2018-12-29 stsp if (error != NULL)
1102 e5dc7198 2018-12-29 stsp goto done;
1103 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1104 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1105 49520a32 2018-12-29 stsp goto done;
1106 49520a32 2018-12-29 stsp }
1107 49520a32 2018-12-29 stsp
1108 8069f636 2019-01-12 stsp if (commit_id_str) {
1109 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1110 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1111 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1112 30837e32 2019-07-25 stsp if (error)
1113 8069f636 2019-01-12 stsp goto done;
1114 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1115 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1116 8069f636 2019-01-12 stsp if (error != NULL) {
1117 8069f636 2019-01-12 stsp free(commit_id);
1118 8069f636 2019-01-12 stsp goto done;
1119 8069f636 2019-01-12 stsp }
1120 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1121 45d344f6 2019-05-14 stsp if (error)
1122 45d344f6 2019-05-14 stsp goto done;
1123 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1124 8069f636 2019-01-12 stsp commit_id);
1125 8069f636 2019-01-12 stsp free(commit_id);
1126 8069f636 2019-01-12 stsp if (error)
1127 8069f636 2019-01-12 stsp goto done;
1128 8069f636 2019-01-12 stsp }
1129 8069f636 2019-01-12 stsp
1130 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1131 f2ea84fa 2019-07-27 stsp if (error)
1132 f2ea84fa 2019-07-27 stsp goto done;
1133 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1134 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1135 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1136 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1137 c09a553d 2018-03-12 stsp if (error != NULL)
1138 c09a553d 2018-03-12 stsp goto done;
1139 c09a553d 2018-03-12 stsp
1140 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1141 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1142 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1143 c09a553d 2018-03-12 stsp done:
1144 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1145 8069f636 2019-01-12 stsp free(commit_id_str);
1146 76089277 2018-04-01 stsp free(repo_path);
1147 507dc3bb 2018-12-29 stsp free(worktree_path);
1148 507dc3bb 2018-12-29 stsp return error;
1149 507dc3bb 2018-12-29 stsp }
1150 507dc3bb 2018-12-29 stsp
1151 507dc3bb 2018-12-29 stsp __dead static void
1152 507dc3bb 2018-12-29 stsp usage_update(void)
1153 507dc3bb 2018-12-29 stsp {
1154 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1155 507dc3bb 2018-12-29 stsp getprogname());
1156 507dc3bb 2018-12-29 stsp exit(1);
1157 507dc3bb 2018-12-29 stsp }
1158 507dc3bb 2018-12-29 stsp
1159 1ee397ad 2019-07-12 stsp static const struct got_error *
1160 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1161 507dc3bb 2018-12-29 stsp {
1162 784955db 2019-01-12 stsp int *did_something = arg;
1163 784955db 2019-01-12 stsp
1164 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1165 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1166 1ee397ad 2019-07-12 stsp return NULL;
1167 507dc3bb 2018-12-29 stsp
1168 1545c615 2019-02-10 stsp *did_something = 1;
1169 a484d721 2019-06-10 stsp
1170 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1171 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1172 1ee397ad 2019-07-12 stsp return NULL;
1173 a484d721 2019-06-10 stsp
1174 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1175 507dc3bb 2018-12-29 stsp path++;
1176 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1177 1ee397ad 2019-07-12 stsp return NULL;
1178 be7061eb 2018-12-30 stsp }
1179 be7061eb 2018-12-30 stsp
1180 be7061eb 2018-12-30 stsp static const struct got_error *
1181 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1182 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1183 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1184 a1fb16d8 2019-05-24 stsp {
1185 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1186 a1fb16d8 2019-05-24 stsp char *base_id_str;
1187 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1188 a1fb16d8 2019-05-24 stsp
1189 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1190 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1191 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1192 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1193 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1194 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1195 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1196 a1fb16d8 2019-05-24 stsp }
1197 a1fb16d8 2019-05-24 stsp
1198 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1199 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1200 a1fb16d8 2019-05-24 stsp if (err) {
1201 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1202 a1fb16d8 2019-05-24 stsp return err;
1203 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1204 a1fb16d8 2019-05-24 stsp }
1205 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1206 a1fb16d8 2019-05-24 stsp return NULL;
1207 a1fb16d8 2019-05-24 stsp
1208 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1209 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1210 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1211 a1fb16d8 2019-05-24 stsp if (err)
1212 a1fb16d8 2019-05-24 stsp return err;
1213 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1214 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1215 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1216 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1217 0ebf8283 2019-07-24 stsp return NULL;
1218 0ebf8283 2019-07-24 stsp }
1219 0ebf8283 2019-07-24 stsp
1220 0ebf8283 2019-07-24 stsp static const struct got_error *
1221 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1222 0ebf8283 2019-07-24 stsp {
1223 0ebf8283 2019-07-24 stsp const struct got_error *err;
1224 0ebf8283 2019-07-24 stsp int in_progress;
1225 0ebf8283 2019-07-24 stsp
1226 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1227 0ebf8283 2019-07-24 stsp if (err)
1228 0ebf8283 2019-07-24 stsp return err;
1229 0ebf8283 2019-07-24 stsp if (in_progress)
1230 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1231 0ebf8283 2019-07-24 stsp
1232 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1233 0ebf8283 2019-07-24 stsp if (err)
1234 0ebf8283 2019-07-24 stsp return err;
1235 0ebf8283 2019-07-24 stsp if (in_progress)
1236 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1237 0ebf8283 2019-07-24 stsp
1238 a1fb16d8 2019-05-24 stsp return NULL;
1239 a5edda0a 2019-07-27 stsp }
1240 a5edda0a 2019-07-27 stsp
1241 a5edda0a 2019-07-27 stsp static const struct got_error *
1242 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1243 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1244 a5edda0a 2019-07-27 stsp {
1245 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1246 a5edda0a 2019-07-27 stsp char *path;
1247 a5edda0a 2019-07-27 stsp int i;
1248 a5edda0a 2019-07-27 stsp
1249 a5edda0a 2019-07-27 stsp if (argc == 0) {
1250 a5edda0a 2019-07-27 stsp path = strdup("");
1251 a5edda0a 2019-07-27 stsp if (path == NULL)
1252 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1253 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1254 a5edda0a 2019-07-27 stsp }
1255 a5edda0a 2019-07-27 stsp
1256 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1257 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1258 a5edda0a 2019-07-27 stsp if (err)
1259 a5edda0a 2019-07-27 stsp break;
1260 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1261 a5edda0a 2019-07-27 stsp if (err) {
1262 a5edda0a 2019-07-27 stsp free(path);
1263 a5edda0a 2019-07-27 stsp break;
1264 a5edda0a 2019-07-27 stsp }
1265 a5edda0a 2019-07-27 stsp }
1266 a5edda0a 2019-07-27 stsp
1267 a5edda0a 2019-07-27 stsp return err;
1268 a1fb16d8 2019-05-24 stsp }
1269 a1fb16d8 2019-05-24 stsp
1270 a1fb16d8 2019-05-24 stsp static const struct got_error *
1271 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1272 507dc3bb 2018-12-29 stsp {
1273 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1274 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1275 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1276 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1277 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1278 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1279 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1280 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1281 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1282 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1283 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1284 507dc3bb 2018-12-29 stsp
1285 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1286 f2ea84fa 2019-07-27 stsp
1287 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1288 507dc3bb 2018-12-29 stsp switch (ch) {
1289 024e9686 2019-05-14 stsp case 'b':
1290 024e9686 2019-05-14 stsp branch_name = optarg;
1291 024e9686 2019-05-14 stsp break;
1292 507dc3bb 2018-12-29 stsp case 'c':
1293 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1294 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1295 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1296 507dc3bb 2018-12-29 stsp break;
1297 507dc3bb 2018-12-29 stsp default:
1298 2deda0b9 2019-03-07 stsp usage_update();
1299 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1300 507dc3bb 2018-12-29 stsp }
1301 507dc3bb 2018-12-29 stsp }
1302 507dc3bb 2018-12-29 stsp
1303 507dc3bb 2018-12-29 stsp argc -= optind;
1304 507dc3bb 2018-12-29 stsp argv += optind;
1305 507dc3bb 2018-12-29 stsp
1306 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1307 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1308 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1309 507dc3bb 2018-12-29 stsp err(1, "pledge");
1310 507dc3bb 2018-12-29 stsp #endif
1311 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1312 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1313 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1314 c4cdcb68 2019-04-03 stsp goto done;
1315 c4cdcb68 2019-04-03 stsp }
1316 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1317 7d5807f4 2019-07-11 stsp if (error)
1318 7d5807f4 2019-07-11 stsp goto done;
1319 7d5807f4 2019-07-11 stsp
1320 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1321 f2ea84fa 2019-07-27 stsp if (error)
1322 f2ea84fa 2019-07-27 stsp goto done;
1323 507dc3bb 2018-12-29 stsp
1324 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1325 c9956ddf 2019-09-08 stsp NULL);
1326 507dc3bb 2018-12-29 stsp if (error != NULL)
1327 507dc3bb 2018-12-29 stsp goto done;
1328 507dc3bb 2018-12-29 stsp
1329 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1330 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1331 087fb88c 2019-08-04 stsp if (error)
1332 087fb88c 2019-08-04 stsp goto done;
1333 087fb88c 2019-08-04 stsp
1334 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1335 0266afb7 2019-01-04 stsp if (error)
1336 0266afb7 2019-01-04 stsp goto done;
1337 0266afb7 2019-01-04 stsp
1338 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1339 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1340 024e9686 2019-05-14 stsp if (error != NULL)
1341 024e9686 2019-05-14 stsp goto done;
1342 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1343 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1344 9c4b8182 2019-01-02 stsp if (error != NULL)
1345 9c4b8182 2019-01-02 stsp goto done;
1346 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1347 507dc3bb 2018-12-29 stsp if (error != NULL)
1348 507dc3bb 2018-12-29 stsp goto done;
1349 507dc3bb 2018-12-29 stsp } else {
1350 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1351 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1352 04f57cb3 2019-07-25 stsp free(commit_id_str);
1353 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1354 30837e32 2019-07-25 stsp if (error)
1355 a297e751 2019-07-11 stsp goto done;
1356 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1357 a297e751 2019-07-11 stsp if (error)
1358 507dc3bb 2018-12-29 stsp goto done;
1359 507dc3bb 2018-12-29 stsp }
1360 35c965b2 2018-12-29 stsp
1361 a1fb16d8 2019-05-24 stsp if (branch_name) {
1362 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1363 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1364 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1365 f2ea84fa 2019-07-27 stsp continue;
1366 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1367 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1368 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1369 024e9686 2019-05-14 stsp goto done;
1370 024e9686 2019-05-14 stsp }
1371 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1372 024e9686 2019-05-14 stsp if (error)
1373 024e9686 2019-05-14 stsp goto done;
1374 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1375 3aef623b 2019-10-15 stsp repo);
1376 a367ff0f 2019-05-14 stsp free(head_commit_id);
1377 024e9686 2019-05-14 stsp if (error != NULL)
1378 024e9686 2019-05-14 stsp goto done;
1379 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1380 a367ff0f 2019-05-14 stsp if (error)
1381 a367ff0f 2019-05-14 stsp goto done;
1382 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1383 024e9686 2019-05-14 stsp if (error)
1384 024e9686 2019-05-14 stsp goto done;
1385 024e9686 2019-05-14 stsp } else {
1386 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1387 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1388 a367ff0f 2019-05-14 stsp if (error != NULL) {
1389 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1390 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1391 024e9686 2019-05-14 stsp goto done;
1392 a367ff0f 2019-05-14 stsp }
1393 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1394 a367ff0f 2019-05-14 stsp if (error)
1395 a367ff0f 2019-05-14 stsp goto done;
1396 024e9686 2019-05-14 stsp }
1397 507dc3bb 2018-12-29 stsp
1398 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1399 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1400 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1401 507dc3bb 2018-12-29 stsp commit_id);
1402 507dc3bb 2018-12-29 stsp if (error)
1403 507dc3bb 2018-12-29 stsp goto done;
1404 507dc3bb 2018-12-29 stsp }
1405 507dc3bb 2018-12-29 stsp
1406 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1407 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1408 507dc3bb 2018-12-29 stsp if (error != NULL)
1409 507dc3bb 2018-12-29 stsp goto done;
1410 9c4b8182 2019-01-02 stsp
1411 784955db 2019-01-12 stsp if (did_something)
1412 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1413 784955db 2019-01-12 stsp else
1414 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1415 507dc3bb 2018-12-29 stsp done:
1416 c09a553d 2018-03-12 stsp free(worktree_path);
1417 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1418 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1419 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1420 507dc3bb 2018-12-29 stsp free(commit_id);
1421 9c4b8182 2019-01-02 stsp free(commit_id_str);
1422 c09a553d 2018-03-12 stsp return error;
1423 c09a553d 2018-03-12 stsp }
1424 c09a553d 2018-03-12 stsp
1425 f42b1b34 2018-03-12 stsp static const struct got_error *
1426 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1427 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1428 63035f9f 2019-10-06 stsp struct got_repository *repo)
1429 5c860e29 2018-03-12 stsp {
1430 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1431 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1432 79109fed 2018-03-27 stsp
1433 44392932 2019-08-25 stsp if (blob_id1) {
1434 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1435 44392932 2019-08-25 stsp if (err)
1436 44392932 2019-08-25 stsp goto done;
1437 44392932 2019-08-25 stsp }
1438 79109fed 2018-03-27 stsp
1439 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1440 44392932 2019-08-25 stsp if (err)
1441 44392932 2019-08-25 stsp goto done;
1442 79109fed 2018-03-27 stsp
1443 44392932 2019-08-25 stsp while (path[0] == '/')
1444 44392932 2019-08-25 stsp path++;
1445 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1446 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1447 44392932 2019-08-25 stsp done:
1448 44392932 2019-08-25 stsp if (blob1)
1449 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1450 44392932 2019-08-25 stsp got_object_blob_close(blob2);
1451 44392932 2019-08-25 stsp return err;
1452 44392932 2019-08-25 stsp }
1453 44392932 2019-08-25 stsp
1454 44392932 2019-08-25 stsp static const struct got_error *
1455 44392932 2019-08-25 stsp diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1456 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1457 63035f9f 2019-10-06 stsp struct got_repository *repo)
1458 44392932 2019-08-25 stsp {
1459 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1460 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1461 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1462 44392932 2019-08-25 stsp
1463 44392932 2019-08-25 stsp if (tree_id1) {
1464 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1465 79109fed 2018-03-27 stsp if (err)
1466 44392932 2019-08-25 stsp goto done;
1467 79109fed 2018-03-27 stsp }
1468 79109fed 2018-03-27 stsp
1469 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1470 0f2b3dca 2018-12-22 stsp if (err)
1471 0f2b3dca 2018-12-22 stsp goto done;
1472 0f2b3dca 2018-12-22 stsp
1473 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1474 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1475 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1476 44392932 2019-08-25 stsp while (path[0] == '/')
1477 44392932 2019-08-25 stsp path++;
1478 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1479 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1480 0f2b3dca 2018-12-22 stsp done:
1481 79109fed 2018-03-27 stsp if (tree1)
1482 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1483 366e0a5f 2019-10-10 stsp if (tree2)
1484 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1485 44392932 2019-08-25 stsp return err;
1486 44392932 2019-08-25 stsp }
1487 44392932 2019-08-25 stsp
1488 44392932 2019-08-25 stsp static const struct got_error *
1489 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1490 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1491 44392932 2019-08-25 stsp {
1492 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1493 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1494 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1495 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1496 44392932 2019-08-25 stsp struct got_object_qid *qid;
1497 44392932 2019-08-25 stsp
1498 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1499 44392932 2019-08-25 stsp if (qid != NULL) {
1500 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1501 44392932 2019-08-25 stsp qid->id);
1502 44392932 2019-08-25 stsp if (err)
1503 44392932 2019-08-25 stsp return err;
1504 44392932 2019-08-25 stsp }
1505 44392932 2019-08-25 stsp
1506 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1507 44392932 2019-08-25 stsp int obj_type;
1508 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1509 44392932 2019-08-25 stsp if (err)
1510 44392932 2019-08-25 stsp goto done;
1511 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1512 44392932 2019-08-25 stsp if (err) {
1513 44392932 2019-08-25 stsp free(obj_id2);
1514 44392932 2019-08-25 stsp goto done;
1515 44392932 2019-08-25 stsp }
1516 44392932 2019-08-25 stsp if (pcommit) {
1517 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1518 44392932 2019-08-25 stsp qid->id, path);
1519 44392932 2019-08-25 stsp if (err) {
1520 44392932 2019-08-25 stsp free(obj_id2);
1521 44392932 2019-08-25 stsp goto done;
1522 44392932 2019-08-25 stsp }
1523 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1524 44392932 2019-08-25 stsp if (err) {
1525 44392932 2019-08-25 stsp free(obj_id2);
1526 44392932 2019-08-25 stsp goto done;
1527 44392932 2019-08-25 stsp }
1528 44392932 2019-08-25 stsp }
1529 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1530 44392932 2019-08-25 stsp if (err) {
1531 44392932 2019-08-25 stsp free(obj_id2);
1532 44392932 2019-08-25 stsp goto done;
1533 44392932 2019-08-25 stsp }
1534 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1535 44392932 2019-08-25 stsp switch (obj_type) {
1536 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1537 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1538 63035f9f 2019-10-06 stsp 0, repo);
1539 44392932 2019-08-25 stsp break;
1540 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1541 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1542 63035f9f 2019-10-06 stsp 0, repo);
1543 44392932 2019-08-25 stsp break;
1544 44392932 2019-08-25 stsp default:
1545 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1546 44392932 2019-08-25 stsp break;
1547 44392932 2019-08-25 stsp }
1548 44392932 2019-08-25 stsp free(obj_id1);
1549 44392932 2019-08-25 stsp free(obj_id2);
1550 44392932 2019-08-25 stsp } else {
1551 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1552 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1553 44392932 2019-08-25 stsp if (err)
1554 44392932 2019-08-25 stsp goto done;
1555 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1556 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1557 44392932 2019-08-25 stsp if (err)
1558 44392932 2019-08-25 stsp goto done;
1559 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1560 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1561 44392932 2019-08-25 stsp }
1562 44392932 2019-08-25 stsp done:
1563 0f2b3dca 2018-12-22 stsp free(id_str1);
1564 0f2b3dca 2018-12-22 stsp free(id_str2);
1565 44392932 2019-08-25 stsp if (pcommit)
1566 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1567 79109fed 2018-03-27 stsp return err;
1568 79109fed 2018-03-27 stsp }
1569 79109fed 2018-03-27 stsp
1570 4bb494d5 2018-06-16 stsp static char *
1571 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1572 6c281f94 2018-06-11 stsp {
1573 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1574 09867e48 2019-08-13 stsp char *p, *s;
1575 09867e48 2019-08-13 stsp
1576 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1577 09867e48 2019-08-13 stsp if (tm == NULL)
1578 09867e48 2019-08-13 stsp return NULL;
1579 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1580 09867e48 2019-08-13 stsp if (s == NULL)
1581 09867e48 2019-08-13 stsp return NULL;
1582 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1583 6c281f94 2018-06-11 stsp if (p)
1584 6c281f94 2018-06-11 stsp *p = '\0';
1585 6c281f94 2018-06-11 stsp return s;
1586 6c281f94 2018-06-11 stsp }
1587 dc424a06 2019-08-07 stsp
1588 6841bf13 2019-11-29 kn static const struct got_error *
1589 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1590 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1591 6841bf13 2019-11-29 kn {
1592 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1593 6841bf13 2019-11-29 kn regmatch_t regmatch;
1594 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1595 6841bf13 2019-11-29 kn
1596 6841bf13 2019-11-29 kn *have_match = 0;
1597 6841bf13 2019-11-29 kn
1598 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1599 6841bf13 2019-11-29 kn if (err)
1600 6841bf13 2019-11-29 kn return err;
1601 6841bf13 2019-11-29 kn
1602 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1603 6841bf13 2019-11-29 kn if (err)
1604 6841bf13 2019-11-29 kn goto done;
1605 6841bf13 2019-11-29 kn
1606 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1607 6841bf13 2019-11-29 kn *have_match = 1;
1608 6841bf13 2019-11-29 kn done:
1609 6841bf13 2019-11-29 kn free(id_str);
1610 6841bf13 2019-11-29 kn free(logmsg);
1611 6841bf13 2019-11-29 kn return err;
1612 6841bf13 2019-11-29 kn }
1613 6841bf13 2019-11-29 kn
1614 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1615 6c281f94 2018-06-11 stsp
1616 79109fed 2018-03-27 stsp static const struct got_error *
1617 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1618 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1619 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1620 79109fed 2018-03-27 stsp {
1621 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1622 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1623 6c281f94 2018-06-11 stsp char datebuf[26];
1624 45d799e2 2018-12-23 stsp time_t committer_time;
1625 45d799e2 2018-12-23 stsp const char *author, *committer;
1626 199a4027 2019-02-02 stsp char *refs_str = NULL;
1627 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1628 5c860e29 2018-03-12 stsp
1629 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1630 199a4027 2019-02-02 stsp char *s;
1631 199a4027 2019-02-02 stsp const char *name;
1632 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1633 a436ad14 2019-08-13 stsp int cmp;
1634 a436ad14 2019-08-13 stsp
1635 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1636 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1637 d9498b20 2019-02-05 stsp continue;
1638 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1639 199a4027 2019-02-02 stsp name += 5;
1640 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1641 7143d404 2019-03-12 stsp continue;
1642 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1643 e34f9ed6 2019-02-02 stsp name += 6;
1644 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1645 141c2bff 2019-02-04 stsp name += 8;
1646 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1647 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1648 5d844a1e 2019-08-13 stsp if (err) {
1649 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1650 5d844a1e 2019-08-13 stsp return err;
1651 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1652 5d844a1e 2019-08-13 stsp err = NULL;
1653 5d844a1e 2019-08-13 stsp tag = NULL;
1654 5d844a1e 2019-08-13 stsp }
1655 a436ad14 2019-08-13 stsp }
1656 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1657 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1658 a436ad14 2019-08-13 stsp if (tag)
1659 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1660 a436ad14 2019-08-13 stsp if (cmp != 0)
1661 a436ad14 2019-08-13 stsp continue;
1662 199a4027 2019-02-02 stsp s = refs_str;
1663 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1664 199a4027 2019-02-02 stsp name) == -1) {
1665 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1666 199a4027 2019-02-02 stsp free(s);
1667 34ca4898 2019-08-13 stsp return err;
1668 199a4027 2019-02-02 stsp }
1669 199a4027 2019-02-02 stsp free(s);
1670 199a4027 2019-02-02 stsp }
1671 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1672 8bf5b3c9 2018-03-17 stsp if (err)
1673 8bf5b3c9 2018-03-17 stsp return err;
1674 788c352e 2018-06-16 stsp
1675 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1676 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1677 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1678 832c249c 2018-06-10 stsp free(id_str);
1679 d3d493d7 2019-02-21 stsp id_str = NULL;
1680 d3d493d7 2019-02-21 stsp free(refs_str);
1681 d3d493d7 2019-02-21 stsp refs_str = NULL;
1682 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1683 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1684 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1685 09867e48 2019-08-13 stsp if (datestr)
1686 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1687 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1688 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1689 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1690 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1691 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1692 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1693 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1694 3fe1abad 2018-06-10 stsp int n = 1;
1695 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1696 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1697 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1698 a0603db2 2018-06-10 stsp if (err)
1699 a0603db2 2018-06-10 stsp return err;
1700 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1701 a0603db2 2018-06-10 stsp free(id_str);
1702 a0603db2 2018-06-10 stsp }
1703 a0603db2 2018-06-10 stsp }
1704 832c249c 2018-06-10 stsp
1705 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1706 5943eee2 2019-08-13 stsp if (err)
1707 5943eee2 2019-08-13 stsp return err;
1708 8bf5b3c9 2018-03-17 stsp
1709 621015ac 2018-07-23 stsp logmsg = logmsg0;
1710 832c249c 2018-06-10 stsp do {
1711 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1712 832c249c 2018-06-10 stsp if (line)
1713 832c249c 2018-06-10 stsp printf(" %s\n", line);
1714 832c249c 2018-06-10 stsp } while (line);
1715 621015ac 2018-07-23 stsp free(logmsg0);
1716 832c249c 2018-06-10 stsp
1717 971751ac 2018-03-27 stsp if (show_patch) {
1718 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1719 971751ac 2018-03-27 stsp if (err == 0)
1720 971751ac 2018-03-27 stsp printf("\n");
1721 971751ac 2018-03-27 stsp }
1722 07862c20 2018-09-15 stsp
1723 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1724 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1725 07862c20 2018-09-15 stsp return err;
1726 f42b1b34 2018-03-12 stsp }
1727 5c860e29 2018-03-12 stsp
1728 f42b1b34 2018-03-12 stsp static const struct got_error *
1729 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1730 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1731 463a997a 2019-12-08 kn int diff_context, int limit, int first_parent_traversal,
1732 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1733 f42b1b34 2018-03-12 stsp {
1734 f42b1b34 2018-03-12 stsp const struct got_error *err;
1735 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1736 6841bf13 2019-11-29 kn regex_t regex;
1737 6841bf13 2019-11-29 kn int have_match;
1738 372ccdbb 2018-06-10 stsp
1739 6841bf13 2019-11-29 kn if (search_pattern &&
1740 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1741 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1742 6841bf13 2019-11-29 kn
1743 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, path, first_parent_traversal);
1744 f42b1b34 2018-03-12 stsp if (err)
1745 f42b1b34 2018-03-12 stsp return err;
1746 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1747 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1748 372ccdbb 2018-06-10 stsp if (err)
1749 fcc85cad 2018-07-22 stsp goto done;
1750 656b1f76 2019-05-11 jcs for (;;) {
1751 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1752 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1753 8bf5b3c9 2018-03-17 stsp
1754 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1755 84453469 2018-11-11 stsp break;
1756 84453469 2018-11-11 stsp
1757 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1758 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1759 372ccdbb 2018-06-10 stsp if (err) {
1760 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1761 9ba79e04 2018-06-11 stsp err = NULL;
1762 ee780d5c 2020-01-04 stsp break;
1763 7e665116 2018-04-02 stsp }
1764 b43fbaa0 2018-06-11 stsp if (id == NULL)
1765 7e665116 2018-04-02 stsp break;
1766 b43fbaa0 2018-06-11 stsp
1767 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1768 b43fbaa0 2018-06-11 stsp if (err)
1769 fcc85cad 2018-07-22 stsp break;
1770 6841bf13 2019-11-29 kn
1771 6841bf13 2019-11-29 kn if (search_pattern) {
1772 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1773 6841bf13 2019-11-29 kn if (err) {
1774 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1775 6841bf13 2019-11-29 kn break;
1776 6841bf13 2019-11-29 kn }
1777 6841bf13 2019-11-29 kn if (have_match == 0) {
1778 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1779 6841bf13 2019-11-29 kn continue;
1780 6841bf13 2019-11-29 kn }
1781 6841bf13 2019-11-29 kn }
1782 6841bf13 2019-11-29 kn
1783 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1784 44392932 2019-08-25 stsp diff_context, refs);
1785 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1786 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1787 7e665116 2018-04-02 stsp break;
1788 31cedeaf 2018-09-15 stsp }
1789 fcc85cad 2018-07-22 stsp done:
1790 6841bf13 2019-11-29 kn if (search_pattern)
1791 6841bf13 2019-11-29 kn regfree(&regex);
1792 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1793 f42b1b34 2018-03-12 stsp return err;
1794 f42b1b34 2018-03-12 stsp }
1795 5c860e29 2018-03-12 stsp
1796 4ed7e80c 2018-05-20 stsp __dead static void
1797 6f3d1eb0 2018-03-12 stsp usage_log(void)
1798 6f3d1eb0 2018-03-12 stsp {
1799 cc54c501 2019-07-15 stsp fprintf(stderr, "usage: %s log [-c commit] [-C number] [-f] [ -l N ] [-p] "
1800 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1801 6f3d1eb0 2018-03-12 stsp exit(1);
1802 b1ebc001 2019-08-13 stsp }
1803 b1ebc001 2019-08-13 stsp
1804 b1ebc001 2019-08-13 stsp static int
1805 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1806 b1ebc001 2019-08-13 stsp {
1807 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1808 b1ebc001 2019-08-13 stsp long long n;
1809 b1ebc001 2019-08-13 stsp const char *errstr;
1810 b1ebc001 2019-08-13 stsp
1811 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1812 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1813 b1ebc001 2019-08-13 stsp return 0;
1814 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1815 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1816 b1ebc001 2019-08-13 stsp return 0;
1817 b1ebc001 2019-08-13 stsp return n;
1818 6f3d1eb0 2018-03-12 stsp }
1819 6f3d1eb0 2018-03-12 stsp
1820 4ed7e80c 2018-05-20 stsp static const struct got_error *
1821 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1822 f42b1b34 2018-03-12 stsp {
1823 f42b1b34 2018-03-12 stsp const struct got_error *error;
1824 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1825 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1826 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1827 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1828 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1829 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1830 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1831 1fd6d7ea 2018-06-13 stsp int show_patch = 0, limit = 0, first_parent_traversal = 0;
1832 64a96a6d 2018-04-01 stsp const char *errstr;
1833 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1834 1b3893a2 2019-03-18 stsp
1835 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1836 5c860e29 2018-03-12 stsp
1837 6715a751 2018-03-16 stsp #ifndef PROFILE
1838 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1839 6098196c 2019-01-04 stsp NULL)
1840 ad242220 2018-09-08 stsp == -1)
1841 f42b1b34 2018-03-12 stsp err(1, "pledge");
1842 6715a751 2018-03-16 stsp #endif
1843 79109fed 2018-03-27 stsp
1844 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1845 b1ebc001 2019-08-13 stsp
1846 6841bf13 2019-11-29 kn while ((ch = getopt(argc, argv, "b:pc:C:l:fr:s:")) != -1) {
1847 79109fed 2018-03-27 stsp switch (ch) {
1848 79109fed 2018-03-27 stsp case 'p':
1849 79109fed 2018-03-27 stsp show_patch = 1;
1850 d142fc45 2018-04-01 stsp break;
1851 d142fc45 2018-04-01 stsp case 'c':
1852 d142fc45 2018-04-01 stsp start_commit = optarg;
1853 64a96a6d 2018-04-01 stsp break;
1854 c0cc5c62 2018-10-18 stsp case 'C':
1855 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1856 4a8520aa 2018-10-18 stsp &errstr);
1857 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1858 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1859 c0cc5c62 2018-10-18 stsp break;
1860 64a96a6d 2018-04-01 stsp case 'l':
1861 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1862 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1863 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1864 cc54c501 2019-07-15 stsp break;
1865 cc54c501 2019-07-15 stsp case 'f':
1866 cc54c501 2019-07-15 stsp first_parent_traversal = 1;
1867 a0603db2 2018-06-10 stsp break;
1868 04ca23f4 2018-07-16 stsp case 'r':
1869 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1870 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1871 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1872 9ba1d308 2019-10-21 stsp optarg);
1873 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1874 04ca23f4 2018-07-16 stsp break;
1875 6841bf13 2019-11-29 kn case 's':
1876 6841bf13 2019-11-29 kn search_pattern = optarg;
1877 6841bf13 2019-11-29 kn break;
1878 79109fed 2018-03-27 stsp default:
1879 2deda0b9 2019-03-07 stsp usage_log();
1880 79109fed 2018-03-27 stsp /* NOTREACHED */
1881 79109fed 2018-03-27 stsp }
1882 79109fed 2018-03-27 stsp }
1883 79109fed 2018-03-27 stsp
1884 79109fed 2018-03-27 stsp argc -= optind;
1885 79109fed 2018-03-27 stsp argv += optind;
1886 f42b1b34 2018-03-12 stsp
1887 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1888 dc1edbfa 2019-11-29 kn diff_context = 3;
1889 dc1edbfa 2019-11-29 kn else if (!show_patch)
1890 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1891 dc1edbfa 2019-11-29 kn
1892 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1893 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1894 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1895 04ca23f4 2018-07-16 stsp goto done;
1896 04ca23f4 2018-07-16 stsp }
1897 cffc0aa4 2019-02-05 stsp
1898 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1899 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1900 cffc0aa4 2019-02-05 stsp goto done;
1901 cffc0aa4 2019-02-05 stsp error = NULL;
1902 cffc0aa4 2019-02-05 stsp
1903 e7301579 2019-03-18 stsp if (argc == 0) {
1904 cbd1af7a 2019-03-18 stsp path = strdup("");
1905 e7301579 2019-03-18 stsp if (path == NULL) {
1906 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1907 cbd1af7a 2019-03-18 stsp goto done;
1908 e7301579 2019-03-18 stsp }
1909 e7301579 2019-03-18 stsp } else if (argc == 1) {
1910 e7301579 2019-03-18 stsp if (worktree) {
1911 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1912 e7301579 2019-03-18 stsp argv[0]);
1913 e7301579 2019-03-18 stsp if (error)
1914 e7301579 2019-03-18 stsp goto done;
1915 e7301579 2019-03-18 stsp } else {
1916 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1917 e7301579 2019-03-18 stsp if (path == NULL) {
1918 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1919 e7301579 2019-03-18 stsp goto done;
1920 e7301579 2019-03-18 stsp }
1921 e7301579 2019-03-18 stsp }
1922 cbd1af7a 2019-03-18 stsp } else
1923 cbd1af7a 2019-03-18 stsp usage_log();
1924 cbd1af7a 2019-03-18 stsp
1925 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1926 5486daa2 2019-05-11 stsp repo_path = worktree ?
1927 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1928 5486daa2 2019-05-11 stsp }
1929 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1930 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1931 cffc0aa4 2019-02-05 stsp goto done;
1932 04ca23f4 2018-07-16 stsp }
1933 6098196c 2019-01-04 stsp
1934 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1935 d7d4f210 2018-03-12 stsp if (error != NULL)
1936 04ca23f4 2018-07-16 stsp goto done;
1937 f42b1b34 2018-03-12 stsp
1938 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1939 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1940 c02c541e 2019-03-29 stsp if (error)
1941 c02c541e 2019-03-29 stsp goto done;
1942 c02c541e 2019-03-29 stsp
1943 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1944 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1945 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1946 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1947 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1948 3235492e 2018-04-01 stsp if (error != NULL)
1949 3235492e 2018-04-01 stsp return error;
1950 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1951 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1952 3235492e 2018-04-01 stsp if (error != NULL)
1953 3235492e 2018-04-01 stsp return error;
1954 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1955 3235492e 2018-04-01 stsp } else {
1956 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1957 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1958 3235492e 2018-04-01 stsp if (error == NULL) {
1959 1caad61b 2019-02-01 stsp int obj_type;
1960 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1961 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1962 d1f2edc9 2018-06-13 stsp if (error != NULL)
1963 1caad61b 2019-02-01 stsp goto done;
1964 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1965 1caad61b 2019-02-01 stsp if (error != NULL)
1966 1caad61b 2019-02-01 stsp goto done;
1967 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1968 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
1969 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
1970 1caad61b 2019-02-01 stsp if (error != NULL)
1971 1caad61b 2019-02-01 stsp goto done;
1972 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
1973 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
1974 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1975 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1976 1caad61b 2019-02-01 stsp goto done;
1977 1caad61b 2019-02-01 stsp }
1978 1caad61b 2019-02-01 stsp free(id);
1979 1caad61b 2019-02-01 stsp id = got_object_id_dup(
1980 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
1981 1caad61b 2019-02-01 stsp if (id == NULL)
1982 638f9024 2019-05-13 stsp error = got_error_from_errno(
1983 230a42bd 2019-05-11 jcs "got_object_id_dup");
1984 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
1985 1caad61b 2019-02-01 stsp if (error)
1986 1caad61b 2019-02-01 stsp goto done;
1987 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
1988 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
1989 1caad61b 2019-02-01 stsp goto done;
1990 1caad61b 2019-02-01 stsp }
1991 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1992 d1f2edc9 2018-06-13 stsp if (error != NULL)
1993 1caad61b 2019-02-01 stsp goto done;
1994 d1f2edc9 2018-06-13 stsp }
1995 15a94983 2018-12-23 stsp if (commit == NULL) {
1996 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
1997 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
1998 d1f2edc9 2018-06-13 stsp if (error != NULL)
1999 d1f2edc9 2018-06-13 stsp return error;
2000 3235492e 2018-04-01 stsp }
2001 3235492e 2018-04-01 stsp }
2002 d7d4f210 2018-03-12 stsp if (error != NULL)
2003 04ca23f4 2018-07-16 stsp goto done;
2004 04ca23f4 2018-07-16 stsp
2005 deeabeae 2019-08-27 stsp if (worktree) {
2006 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2007 deeabeae 2019-08-27 stsp char *p;
2008 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2009 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2010 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2011 deeabeae 2019-08-27 stsp goto done;
2012 deeabeae 2019-08-27 stsp }
2013 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2014 deeabeae 2019-08-27 stsp free(p);
2015 deeabeae 2019-08-27 stsp } else
2016 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2017 04ca23f4 2018-07-16 stsp if (error != NULL)
2018 04ca23f4 2018-07-16 stsp goto done;
2019 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2020 04ca23f4 2018-07-16 stsp free(path);
2021 04ca23f4 2018-07-16 stsp path = in_repo_path;
2022 04ca23f4 2018-07-16 stsp }
2023 199a4027 2019-02-02 stsp
2024 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2025 199a4027 2019-02-02 stsp if (error)
2026 199a4027 2019-02-02 stsp goto done;
2027 04ca23f4 2018-07-16 stsp
2028 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2029 199a4027 2019-02-02 stsp diff_context, limit, first_parent_traversal, &refs);
2030 04ca23f4 2018-07-16 stsp done:
2031 04ca23f4 2018-07-16 stsp free(path);
2032 04ca23f4 2018-07-16 stsp free(repo_path);
2033 04ca23f4 2018-07-16 stsp free(cwd);
2034 f42b1b34 2018-03-12 stsp free(id);
2035 cffc0aa4 2019-02-05 stsp if (worktree)
2036 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2037 ad242220 2018-09-08 stsp if (repo) {
2038 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2039 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2040 ad242220 2018-09-08 stsp if (error == NULL)
2041 ad242220 2018-09-08 stsp error = repo_error;
2042 ad242220 2018-09-08 stsp }
2043 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2044 8bf5b3c9 2018-03-17 stsp return error;
2045 5c860e29 2018-03-12 stsp }
2046 5c860e29 2018-03-12 stsp
2047 4ed7e80c 2018-05-20 stsp __dead static void
2048 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2049 3f8b7d6a 2018-04-01 stsp {
2050 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2051 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2052 3f8b7d6a 2018-04-01 stsp exit(1);
2053 b00d56cd 2018-04-01 stsp }
2054 b00d56cd 2018-04-01 stsp
2055 b72f483a 2019-02-05 stsp struct print_diff_arg {
2056 b72f483a 2019-02-05 stsp struct got_repository *repo;
2057 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2058 b72f483a 2019-02-05 stsp int diff_context;
2059 3fc0c068 2019-02-10 stsp const char *id_str;
2060 3fc0c068 2019-02-10 stsp int header_shown;
2061 98eaaa12 2019-08-03 stsp int diff_staged;
2062 63035f9f 2019-10-06 stsp int ignore_whitespace;
2063 b72f483a 2019-02-05 stsp };
2064 b72f483a 2019-02-05 stsp
2065 4ed7e80c 2018-05-20 stsp static const struct got_error *
2066 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2067 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2068 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2069 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2070 b72f483a 2019-02-05 stsp {
2071 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2072 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2073 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2074 12463d8b 2019-12-13 stsp int fd = -1;
2075 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2076 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2077 b72f483a 2019-02-05 stsp struct stat sb;
2078 b72f483a 2019-02-05 stsp
2079 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2080 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2081 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2082 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2083 98eaaa12 2019-08-03 stsp return NULL;
2084 98eaaa12 2019-08-03 stsp } else {
2085 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2086 98eaaa12 2019-08-03 stsp return NULL;
2087 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2088 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2089 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2090 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2091 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2092 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2093 98eaaa12 2019-08-03 stsp return NULL;
2094 98eaaa12 2019-08-03 stsp }
2095 3fc0c068 2019-02-10 stsp
2096 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2097 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2098 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2099 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2100 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2101 3fc0c068 2019-02-10 stsp }
2102 b72f483a 2019-02-05 stsp
2103 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2104 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2105 98eaaa12 2019-08-03 stsp switch (staged_status) {
2106 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2107 98eaaa12 2019-08-03 stsp label1 = path;
2108 98eaaa12 2019-08-03 stsp label2 = path;
2109 98eaaa12 2019-08-03 stsp break;
2110 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2111 98eaaa12 2019-08-03 stsp label2 = path;
2112 98eaaa12 2019-08-03 stsp break;
2113 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2114 98eaaa12 2019-08-03 stsp label1 = path;
2115 98eaaa12 2019-08-03 stsp break;
2116 98eaaa12 2019-08-03 stsp default:
2117 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2118 98eaaa12 2019-08-03 stsp }
2119 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2120 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2121 63035f9f 2019-10-06 stsp a->repo, stdout);
2122 98eaaa12 2019-08-03 stsp }
2123 98eaaa12 2019-08-03 stsp
2124 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2125 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2126 4ce46740 2019-08-08 stsp char *id_str;
2127 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2128 408b4ebc 2019-08-03 stsp 8192);
2129 4ce46740 2019-08-08 stsp if (err)
2130 4ce46740 2019-08-08 stsp goto done;
2131 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2132 4ce46740 2019-08-08 stsp if (err)
2133 4ce46740 2019-08-08 stsp goto done;
2134 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2135 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2136 4ce46740 2019-08-08 stsp free(id_str);
2137 4ce46740 2019-08-08 stsp goto done;
2138 4ce46740 2019-08-08 stsp }
2139 4ce46740 2019-08-08 stsp free(id_str);
2140 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2141 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2142 4ce46740 2019-08-08 stsp if (err)
2143 4ce46740 2019-08-08 stsp goto done;
2144 4ce46740 2019-08-08 stsp }
2145 d00136be 2019-03-26 stsp
2146 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2147 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2148 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2149 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2150 2ec1f75b 2019-03-26 stsp goto done;
2151 2ec1f75b 2019-03-26 stsp }
2152 b72f483a 2019-02-05 stsp
2153 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2154 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2155 12463d8b 2019-12-13 stsp if (fd == -1) {
2156 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2157 12463d8b 2019-12-13 stsp goto done;
2158 12463d8b 2019-12-13 stsp }
2159 12463d8b 2019-12-13 stsp } else {
2160 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2161 12463d8b 2019-12-13 stsp if (fd == -1) {
2162 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2163 12463d8b 2019-12-13 stsp goto done;
2164 12463d8b 2019-12-13 stsp }
2165 12463d8b 2019-12-13 stsp }
2166 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2167 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2168 2ec1f75b 2019-03-26 stsp goto done;
2169 2ec1f75b 2019-03-26 stsp }
2170 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2171 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2172 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2173 2ec1f75b 2019-03-26 stsp goto done;
2174 2ec1f75b 2019-03-26 stsp }
2175 12463d8b 2019-12-13 stsp fd = -1;
2176 2ec1f75b 2019-03-26 stsp } else
2177 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2178 b72f483a 2019-02-05 stsp
2179 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2180 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2181 b72f483a 2019-02-05 stsp done:
2182 b72f483a 2019-02-05 stsp if (blob1)
2183 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2184 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2185 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2186 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2187 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2188 b72f483a 2019-02-05 stsp free(abspath);
2189 927df6b7 2019-02-10 stsp return err;
2190 927df6b7 2019-02-10 stsp }
2191 927df6b7 2019-02-10 stsp
2192 927df6b7 2019-02-10 stsp static const struct got_error *
2193 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2194 b00d56cd 2018-04-01 stsp {
2195 b00d56cd 2018-04-01 stsp const struct got_error *error;
2196 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2197 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2198 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2199 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2200 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2201 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2202 15a94983 2018-12-23 stsp int type1, type2;
2203 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2204 c0cc5c62 2018-10-18 stsp const char *errstr;
2205 927df6b7 2019-02-10 stsp char *path = NULL;
2206 b00d56cd 2018-04-01 stsp
2207 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2208 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2209 25eccc22 2019-01-04 stsp NULL) == -1)
2210 b00d56cd 2018-04-01 stsp err(1, "pledge");
2211 b00d56cd 2018-04-01 stsp #endif
2212 b00d56cd 2018-04-01 stsp
2213 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2214 b00d56cd 2018-04-01 stsp switch (ch) {
2215 c0cc5c62 2018-10-18 stsp case 'C':
2216 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2217 dfcab68b 2019-11-29 kn &errstr);
2218 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2219 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2220 c0cc5c62 2018-10-18 stsp break;
2221 b72f483a 2019-02-05 stsp case 'r':
2222 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2223 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2224 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2225 9ba1d308 2019-10-21 stsp optarg);
2226 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2227 b72f483a 2019-02-05 stsp break;
2228 98eaaa12 2019-08-03 stsp case 's':
2229 98eaaa12 2019-08-03 stsp diff_staged = 1;
2230 63035f9f 2019-10-06 stsp break;
2231 63035f9f 2019-10-06 stsp case 'w':
2232 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2233 98eaaa12 2019-08-03 stsp break;
2234 b00d56cd 2018-04-01 stsp default:
2235 2deda0b9 2019-03-07 stsp usage_diff();
2236 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2237 b00d56cd 2018-04-01 stsp }
2238 b00d56cd 2018-04-01 stsp }
2239 b00d56cd 2018-04-01 stsp
2240 b00d56cd 2018-04-01 stsp argc -= optind;
2241 b00d56cd 2018-04-01 stsp argv += optind;
2242 b00d56cd 2018-04-01 stsp
2243 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2244 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2245 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2246 b72f483a 2019-02-05 stsp goto done;
2247 b72f483a 2019-02-05 stsp }
2248 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2249 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2250 927df6b7 2019-02-10 stsp goto done;
2251 927df6b7 2019-02-10 stsp if (argc <= 1) {
2252 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2253 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2254 927df6b7 2019-02-10 stsp goto done;
2255 927df6b7 2019-02-10 stsp }
2256 b72f483a 2019-02-05 stsp if (repo_path)
2257 b72f483a 2019-02-05 stsp errx(1,
2258 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2259 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2260 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2261 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2262 927df6b7 2019-02-10 stsp goto done;
2263 927df6b7 2019-02-10 stsp }
2264 927df6b7 2019-02-10 stsp if (argc == 1) {
2265 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2266 cbd1af7a 2019-03-18 stsp argv[0]);
2267 927df6b7 2019-02-10 stsp if (error)
2268 927df6b7 2019-02-10 stsp goto done;
2269 927df6b7 2019-02-10 stsp } else {
2270 927df6b7 2019-02-10 stsp path = strdup("");
2271 927df6b7 2019-02-10 stsp if (path == NULL) {
2272 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2273 927df6b7 2019-02-10 stsp goto done;
2274 927df6b7 2019-02-10 stsp }
2275 927df6b7 2019-02-10 stsp }
2276 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2277 98eaaa12 2019-08-03 stsp if (diff_staged)
2278 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2279 98eaaa12 2019-08-03 stsp "objects in repository");
2280 15a94983 2018-12-23 stsp id_str1 = argv[0];
2281 15a94983 2018-12-23 stsp id_str2 = argv[1];
2282 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2283 30db809c 2019-06-05 stsp repo_path =
2284 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2285 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2286 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2287 30db809c 2019-06-05 stsp goto done;
2288 30db809c 2019-06-05 stsp }
2289 30db809c 2019-06-05 stsp }
2290 b00d56cd 2018-04-01 stsp } else
2291 b00d56cd 2018-04-01 stsp usage_diff();
2292 25eccc22 2019-01-04 stsp
2293 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2294 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2295 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2296 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2297 b72f483a 2019-02-05 stsp }
2298 b00d56cd 2018-04-01 stsp
2299 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2300 76089277 2018-04-01 stsp free(repo_path);
2301 b00d56cd 2018-04-01 stsp if (error != NULL)
2302 b00d56cd 2018-04-01 stsp goto done;
2303 b00d56cd 2018-04-01 stsp
2304 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2305 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2306 c02c541e 2019-03-29 stsp if (error)
2307 c02c541e 2019-03-29 stsp goto done;
2308 c02c541e 2019-03-29 stsp
2309 30db809c 2019-06-05 stsp if (argc <= 1) {
2310 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2311 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2312 b72f483a 2019-02-05 stsp char *id_str;
2313 72ea6654 2019-07-27 stsp
2314 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2315 72ea6654 2019-07-27 stsp
2316 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2317 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2318 b72f483a 2019-02-05 stsp if (error)
2319 b72f483a 2019-02-05 stsp goto done;
2320 b72f483a 2019-02-05 stsp arg.repo = repo;
2321 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2322 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2323 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2324 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2325 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2326 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2327 b72f483a 2019-02-05 stsp
2328 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2329 72ea6654 2019-07-27 stsp if (error)
2330 72ea6654 2019-07-27 stsp goto done;
2331 72ea6654 2019-07-27 stsp
2332 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2333 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2334 b72f483a 2019-02-05 stsp free(id_str);
2335 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2336 b72f483a 2019-02-05 stsp goto done;
2337 b72f483a 2019-02-05 stsp }
2338 b72f483a 2019-02-05 stsp
2339 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2340 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2341 0adb7196 2019-08-11 stsp if (error)
2342 0adb7196 2019-08-11 stsp goto done;
2343 b00d56cd 2018-04-01 stsp
2344 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2345 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2346 0adb7196 2019-08-11 stsp if (error)
2347 0adb7196 2019-08-11 stsp goto done;
2348 b00d56cd 2018-04-01 stsp
2349 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2350 15a94983 2018-12-23 stsp if (error)
2351 15a94983 2018-12-23 stsp goto done;
2352 15a94983 2018-12-23 stsp
2353 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2354 15a94983 2018-12-23 stsp if (error)
2355 15a94983 2018-12-23 stsp goto done;
2356 15a94983 2018-12-23 stsp
2357 15a94983 2018-12-23 stsp if (type1 != type2) {
2358 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2359 b00d56cd 2018-04-01 stsp goto done;
2360 b00d56cd 2018-04-01 stsp }
2361 b00d56cd 2018-04-01 stsp
2362 15a94983 2018-12-23 stsp switch (type1) {
2363 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2364 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2365 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2366 b00d56cd 2018-04-01 stsp break;
2367 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2368 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2369 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2370 b00d56cd 2018-04-01 stsp break;
2371 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2372 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2373 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2374 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2375 b00d56cd 2018-04-01 stsp break;
2376 b00d56cd 2018-04-01 stsp default:
2377 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2378 b00d56cd 2018-04-01 stsp }
2379 b00d56cd 2018-04-01 stsp done:
2380 5e70831e 2019-05-28 stsp free(label1);
2381 5e70831e 2019-05-28 stsp free(label2);
2382 15a94983 2018-12-23 stsp free(id1);
2383 15a94983 2018-12-23 stsp free(id2);
2384 927df6b7 2019-02-10 stsp free(path);
2385 b72f483a 2019-02-05 stsp if (worktree)
2386 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2387 ad242220 2018-09-08 stsp if (repo) {
2388 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2389 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2390 ad242220 2018-09-08 stsp if (error == NULL)
2391 ad242220 2018-09-08 stsp error = repo_error;
2392 ad242220 2018-09-08 stsp }
2393 b00d56cd 2018-04-01 stsp return error;
2394 404c43c4 2018-06-21 stsp }
2395 404c43c4 2018-06-21 stsp
2396 404c43c4 2018-06-21 stsp __dead static void
2397 404c43c4 2018-06-21 stsp usage_blame(void)
2398 404c43c4 2018-06-21 stsp {
2399 9270e621 2019-02-05 stsp fprintf(stderr,
2400 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2401 404c43c4 2018-06-21 stsp getprogname());
2402 404c43c4 2018-06-21 stsp exit(1);
2403 b00d56cd 2018-04-01 stsp }
2404 b00d56cd 2018-04-01 stsp
2405 28315671 2019-08-14 stsp struct blame_line {
2406 28315671 2019-08-14 stsp int annotated;
2407 28315671 2019-08-14 stsp char *id_str;
2408 82f6abb8 2019-08-14 stsp char *committer;
2409 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2410 28315671 2019-08-14 stsp };
2411 28315671 2019-08-14 stsp
2412 28315671 2019-08-14 stsp struct blame_cb_args {
2413 28315671 2019-08-14 stsp struct blame_line *lines;
2414 28315671 2019-08-14 stsp int nlines;
2415 7ef28ff8 2019-08-14 stsp int nlines_prec;
2416 28315671 2019-08-14 stsp int lineno_cur;
2417 28315671 2019-08-14 stsp off_t *line_offsets;
2418 28315671 2019-08-14 stsp FILE *f;
2419 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2420 28315671 2019-08-14 stsp };
2421 28315671 2019-08-14 stsp
2422 404c43c4 2018-06-21 stsp static const struct got_error *
2423 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2424 28315671 2019-08-14 stsp {
2425 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2426 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2427 28315671 2019-08-14 stsp struct blame_line *bline;
2428 82f6abb8 2019-08-14 stsp char *line = NULL;
2429 28315671 2019-08-14 stsp size_t linesize = 0;
2430 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2431 28315671 2019-08-14 stsp off_t offset;
2432 bcb49d15 2019-08-14 stsp struct tm tm;
2433 bcb49d15 2019-08-14 stsp time_t committer_time;
2434 28315671 2019-08-14 stsp
2435 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2436 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2437 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2438 28315671 2019-08-14 stsp
2439 28315671 2019-08-14 stsp if (sigint_received)
2440 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2441 28315671 2019-08-14 stsp
2442 2839f8b9 2019-08-18 stsp if (lineno == -1)
2443 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2444 2839f8b9 2019-08-18 stsp
2445 28315671 2019-08-14 stsp /* Annotate this line. */
2446 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2447 28315671 2019-08-14 stsp if (bline->annotated)
2448 28315671 2019-08-14 stsp return NULL;
2449 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2450 28315671 2019-08-14 stsp if (err)
2451 28315671 2019-08-14 stsp return err;
2452 82f6abb8 2019-08-14 stsp
2453 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2454 82f6abb8 2019-08-14 stsp if (err)
2455 82f6abb8 2019-08-14 stsp goto done;
2456 82f6abb8 2019-08-14 stsp
2457 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2458 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2459 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2460 bcb49d15 2019-08-14 stsp goto done;
2461 bcb49d15 2019-08-14 stsp }
2462 bcb49d15 2019-08-14 stsp
2463 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2464 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2465 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2466 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2467 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2468 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2469 82f6abb8 2019-08-14 stsp goto done;
2470 82f6abb8 2019-08-14 stsp }
2471 28315671 2019-08-14 stsp bline->annotated = 1;
2472 28315671 2019-08-14 stsp
2473 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2474 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2475 28315671 2019-08-14 stsp if (!bline->annotated)
2476 82f6abb8 2019-08-14 stsp goto done;
2477 28315671 2019-08-14 stsp
2478 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2479 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2480 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2481 82f6abb8 2019-08-14 stsp goto done;
2482 82f6abb8 2019-08-14 stsp }
2483 28315671 2019-08-14 stsp
2484 28315671 2019-08-14 stsp while (bline->annotated) {
2485 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2486 82f6abb8 2019-08-14 stsp size_t len;
2487 82f6abb8 2019-08-14 stsp
2488 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2489 28315671 2019-08-14 stsp if (ferror(a->f))
2490 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2491 28315671 2019-08-14 stsp break;
2492 28315671 2019-08-14 stsp }
2493 82f6abb8 2019-08-14 stsp
2494 82f6abb8 2019-08-14 stsp committer = bline->committer;
2495 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2496 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2497 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2498 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2499 82f6abb8 2019-08-14 stsp if (at)
2500 82f6abb8 2019-08-14 stsp *at = '\0';
2501 82f6abb8 2019-08-14 stsp len = strlen(committer);
2502 82f6abb8 2019-08-14 stsp if (len >= 9)
2503 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2504 28315671 2019-08-14 stsp
2505 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2506 28315671 2019-08-14 stsp if (nl)
2507 28315671 2019-08-14 stsp *nl = '\0';
2508 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2509 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2510 28315671 2019-08-14 stsp
2511 28315671 2019-08-14 stsp a->lineno_cur++;
2512 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2513 28315671 2019-08-14 stsp }
2514 82f6abb8 2019-08-14 stsp done:
2515 82f6abb8 2019-08-14 stsp if (commit)
2516 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2517 28315671 2019-08-14 stsp free(line);
2518 28315671 2019-08-14 stsp return err;
2519 28315671 2019-08-14 stsp }
2520 28315671 2019-08-14 stsp
2521 28315671 2019-08-14 stsp static const struct got_error *
2522 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2523 404c43c4 2018-06-21 stsp {
2524 404c43c4 2018-06-21 stsp const struct got_error *error;
2525 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2526 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2527 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2528 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2529 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2530 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2531 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2532 28315671 2019-08-14 stsp struct blame_cb_args bca;
2533 28315671 2019-08-14 stsp int ch, obj_type, i;
2534 b02560ec 2019-08-19 stsp size_t filesize;
2535 90f3c347 2019-08-19 stsp
2536 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2537 404c43c4 2018-06-21 stsp
2538 404c43c4 2018-06-21 stsp #ifndef PROFILE
2539 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2540 36e2fb66 2019-01-04 stsp NULL) == -1)
2541 404c43c4 2018-06-21 stsp err(1, "pledge");
2542 404c43c4 2018-06-21 stsp #endif
2543 404c43c4 2018-06-21 stsp
2544 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2545 404c43c4 2018-06-21 stsp switch (ch) {
2546 404c43c4 2018-06-21 stsp case 'c':
2547 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2548 404c43c4 2018-06-21 stsp break;
2549 66bea077 2018-08-02 stsp case 'r':
2550 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2551 66bea077 2018-08-02 stsp if (repo_path == NULL)
2552 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2553 9ba1d308 2019-10-21 stsp optarg);
2554 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2555 66bea077 2018-08-02 stsp break;
2556 404c43c4 2018-06-21 stsp default:
2557 2deda0b9 2019-03-07 stsp usage_blame();
2558 404c43c4 2018-06-21 stsp /* NOTREACHED */
2559 404c43c4 2018-06-21 stsp }
2560 404c43c4 2018-06-21 stsp }
2561 404c43c4 2018-06-21 stsp
2562 404c43c4 2018-06-21 stsp argc -= optind;
2563 404c43c4 2018-06-21 stsp argv += optind;
2564 404c43c4 2018-06-21 stsp
2565 a39318fd 2018-08-02 stsp if (argc == 1)
2566 404c43c4 2018-06-21 stsp path = argv[0];
2567 a39318fd 2018-08-02 stsp else
2568 404c43c4 2018-06-21 stsp usage_blame();
2569 404c43c4 2018-06-21 stsp
2570 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2571 66bea077 2018-08-02 stsp if (cwd == NULL) {
2572 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2573 66bea077 2018-08-02 stsp goto done;
2574 66bea077 2018-08-02 stsp }
2575 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2576 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2577 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2578 66bea077 2018-08-02 stsp goto done;
2579 0c06baac 2019-02-05 stsp else
2580 0c06baac 2019-02-05 stsp error = NULL;
2581 0c06baac 2019-02-05 stsp if (worktree) {
2582 0c06baac 2019-02-05 stsp repo_path =
2583 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2584 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2585 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2586 0c06baac 2019-02-05 stsp if (error)
2587 0c06baac 2019-02-05 stsp goto done;
2588 0c06baac 2019-02-05 stsp } else {
2589 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2590 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2591 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2592 0c06baac 2019-02-05 stsp goto done;
2593 0c06baac 2019-02-05 stsp }
2594 66bea077 2018-08-02 stsp }
2595 66bea077 2018-08-02 stsp }
2596 36e2fb66 2019-01-04 stsp
2597 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2598 c02c541e 2019-03-29 stsp if (error != NULL)
2599 36e2fb66 2019-01-04 stsp goto done;
2600 66bea077 2018-08-02 stsp
2601 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2602 c02c541e 2019-03-29 stsp if (error)
2603 404c43c4 2018-06-21 stsp goto done;
2604 404c43c4 2018-06-21 stsp
2605 0c06baac 2019-02-05 stsp if (worktree) {
2606 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2607 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2608 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2609 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2610 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2611 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2612 6efaaa2d 2019-02-05 stsp path) == -1) {
2613 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2614 0c06baac 2019-02-05 stsp goto done;
2615 0c06baac 2019-02-05 stsp }
2616 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2617 0c06baac 2019-02-05 stsp free(p);
2618 0c06baac 2019-02-05 stsp } else {
2619 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2620 0c06baac 2019-02-05 stsp }
2621 0c06baac 2019-02-05 stsp if (error)
2622 66bea077 2018-08-02 stsp goto done;
2623 66bea077 2018-08-02 stsp
2624 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2625 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2626 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2627 404c43c4 2018-06-21 stsp if (error != NULL)
2628 66bea077 2018-08-02 stsp goto done;
2629 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2630 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2631 404c43c4 2018-06-21 stsp if (error != NULL)
2632 66bea077 2018-08-02 stsp goto done;
2633 404c43c4 2018-06-21 stsp } else {
2634 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2635 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2636 30837e32 2019-07-25 stsp if (error)
2637 66bea077 2018-08-02 stsp goto done;
2638 404c43c4 2018-06-21 stsp }
2639 404c43c4 2018-06-21 stsp
2640 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2641 28315671 2019-08-14 stsp if (error)
2642 28315671 2019-08-14 stsp goto done;
2643 28315671 2019-08-14 stsp if (obj_id == NULL) {
2644 28315671 2019-08-14 stsp error = got_error(GOT_ERR_NO_OBJ);
2645 28315671 2019-08-14 stsp goto done;
2646 28315671 2019-08-14 stsp }
2647 28315671 2019-08-14 stsp
2648 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2649 28315671 2019-08-14 stsp if (error)
2650 28315671 2019-08-14 stsp goto done;
2651 28315671 2019-08-14 stsp
2652 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2653 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2654 28315671 2019-08-14 stsp goto done;
2655 28315671 2019-08-14 stsp }
2656 28315671 2019-08-14 stsp
2657 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2658 28315671 2019-08-14 stsp if (error)
2659 28315671 2019-08-14 stsp goto done;
2660 28315671 2019-08-14 stsp bca.f = got_opentemp();
2661 28315671 2019-08-14 stsp if (bca.f == NULL) {
2662 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2663 28315671 2019-08-14 stsp goto done;
2664 28315671 2019-08-14 stsp }
2665 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2666 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2667 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2668 28315671 2019-08-14 stsp goto done;
2669 28315671 2019-08-14 stsp
2670 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2671 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2672 b02560ec 2019-08-19 stsp bca.nlines--;
2673 b02560ec 2019-08-19 stsp
2674 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2675 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2676 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2677 28315671 2019-08-14 stsp goto done;
2678 28315671 2019-08-14 stsp }
2679 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2680 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2681 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2682 7ef28ff8 2019-08-14 stsp while (i > 0) {
2683 7ef28ff8 2019-08-14 stsp i /= 10;
2684 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2685 7ef28ff8 2019-08-14 stsp }
2686 82f6abb8 2019-08-14 stsp bca.repo = repo;
2687 7ef28ff8 2019-08-14 stsp
2688 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2689 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2690 404c43c4 2018-06-21 stsp done:
2691 66bea077 2018-08-02 stsp free(in_repo_path);
2692 66bea077 2018-08-02 stsp free(repo_path);
2693 66bea077 2018-08-02 stsp free(cwd);
2694 404c43c4 2018-06-21 stsp free(commit_id);
2695 28315671 2019-08-14 stsp free(obj_id);
2696 28315671 2019-08-14 stsp if (blob)
2697 28315671 2019-08-14 stsp got_object_blob_close(blob);
2698 0c06baac 2019-02-05 stsp if (worktree)
2699 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2700 ad242220 2018-09-08 stsp if (repo) {
2701 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2702 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2703 ad242220 2018-09-08 stsp if (error == NULL)
2704 ad242220 2018-09-08 stsp error = repo_error;
2705 ad242220 2018-09-08 stsp }
2706 3affba96 2019-09-22 stsp if (bca.lines) {
2707 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2708 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2709 3affba96 2019-09-22 stsp free(bline->id_str);
2710 3affba96 2019-09-22 stsp free(bline->committer);
2711 3affba96 2019-09-22 stsp }
2712 3affba96 2019-09-22 stsp free(bca.lines);
2713 28315671 2019-08-14 stsp }
2714 28315671 2019-08-14 stsp free(bca.line_offsets);
2715 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2716 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2717 404c43c4 2018-06-21 stsp return error;
2718 5de5890b 2018-10-18 stsp }
2719 5de5890b 2018-10-18 stsp
2720 5de5890b 2018-10-18 stsp __dead static void
2721 5de5890b 2018-10-18 stsp usage_tree(void)
2722 5de5890b 2018-10-18 stsp {
2723 c1669e2e 2019-01-09 stsp fprintf(stderr,
2724 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2725 5de5890b 2018-10-18 stsp getprogname());
2726 5de5890b 2018-10-18 stsp exit(1);
2727 5de5890b 2018-10-18 stsp }
2728 5de5890b 2018-10-18 stsp
2729 c1669e2e 2019-01-09 stsp static void
2730 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2731 c1669e2e 2019-01-09 stsp const char *root_path)
2732 c1669e2e 2019-01-09 stsp {
2733 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2734 848d6979 2019-08-12 stsp const char *modestr = "";
2735 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2736 5de5890b 2018-10-18 stsp
2737 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2738 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2739 c1669e2e 2019-01-09 stsp path++;
2740 c1669e2e 2019-01-09 stsp
2741 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2742 63c5ca5d 2019-08-24 stsp modestr = "$";
2743 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2744 848d6979 2019-08-12 stsp modestr = "@";
2745 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2746 848d6979 2019-08-12 stsp modestr = "/";
2747 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2748 848d6979 2019-08-12 stsp modestr = "*";
2749 848d6979 2019-08-12 stsp
2750 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2751 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2752 c1669e2e 2019-01-09 stsp }
2753 c1669e2e 2019-01-09 stsp
2754 5de5890b 2018-10-18 stsp static const struct got_error *
2755 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2756 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2757 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2758 5de5890b 2018-10-18 stsp {
2759 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2760 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2761 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2762 56e0773d 2019-11-28 stsp int nentries, i;
2763 5de5890b 2018-10-18 stsp
2764 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2765 5de5890b 2018-10-18 stsp if (err)
2766 5de5890b 2018-10-18 stsp goto done;
2767 5de5890b 2018-10-18 stsp
2768 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2769 5de5890b 2018-10-18 stsp if (err)
2770 5de5890b 2018-10-18 stsp goto done;
2771 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2772 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2773 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2774 5de5890b 2018-10-18 stsp char *id = NULL;
2775 84453469 2018-11-11 stsp
2776 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2777 84453469 2018-11-11 stsp break;
2778 84453469 2018-11-11 stsp
2779 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2780 5de5890b 2018-10-18 stsp if (show_ids) {
2781 5de5890b 2018-10-18 stsp char *id_str;
2782 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2783 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2784 5de5890b 2018-10-18 stsp if (err)
2785 5de5890b 2018-10-18 stsp goto done;
2786 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2787 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2788 5de5890b 2018-10-18 stsp free(id_str);
2789 5de5890b 2018-10-18 stsp goto done;
2790 5de5890b 2018-10-18 stsp }
2791 5de5890b 2018-10-18 stsp free(id_str);
2792 5de5890b 2018-10-18 stsp }
2793 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2794 5de5890b 2018-10-18 stsp free(id);
2795 c1669e2e 2019-01-09 stsp
2796 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2797 c1669e2e 2019-01-09 stsp char *child_path;
2798 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2799 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2800 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2801 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2802 c1669e2e 2019-01-09 stsp goto done;
2803 c1669e2e 2019-01-09 stsp }
2804 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2805 c1669e2e 2019-01-09 stsp root_path, repo);
2806 c1669e2e 2019-01-09 stsp free(child_path);
2807 c1669e2e 2019-01-09 stsp if (err)
2808 c1669e2e 2019-01-09 stsp goto done;
2809 c1669e2e 2019-01-09 stsp }
2810 5de5890b 2018-10-18 stsp }
2811 5de5890b 2018-10-18 stsp done:
2812 5de5890b 2018-10-18 stsp if (tree)
2813 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2814 5de5890b 2018-10-18 stsp free(tree_id);
2815 5de5890b 2018-10-18 stsp return err;
2816 404c43c4 2018-06-21 stsp }
2817 404c43c4 2018-06-21 stsp
2818 5de5890b 2018-10-18 stsp static const struct got_error *
2819 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2820 5de5890b 2018-10-18 stsp {
2821 5de5890b 2018-10-18 stsp const struct got_error *error;
2822 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2823 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2824 7a2c19d6 2019-02-05 stsp const char *path;
2825 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2826 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2827 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2828 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2829 5de5890b 2018-10-18 stsp int ch;
2830 5de5890b 2018-10-18 stsp
2831 5de5890b 2018-10-18 stsp #ifndef PROFILE
2832 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2833 0f8d269b 2019-01-04 stsp NULL) == -1)
2834 5de5890b 2018-10-18 stsp err(1, "pledge");
2835 5de5890b 2018-10-18 stsp #endif
2836 5de5890b 2018-10-18 stsp
2837 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2838 5de5890b 2018-10-18 stsp switch (ch) {
2839 5de5890b 2018-10-18 stsp case 'c':
2840 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2841 5de5890b 2018-10-18 stsp break;
2842 5de5890b 2018-10-18 stsp case 'r':
2843 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2844 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2845 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2846 9ba1d308 2019-10-21 stsp optarg);
2847 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2848 5de5890b 2018-10-18 stsp break;
2849 5de5890b 2018-10-18 stsp case 'i':
2850 5de5890b 2018-10-18 stsp show_ids = 1;
2851 5de5890b 2018-10-18 stsp break;
2852 c1669e2e 2019-01-09 stsp case 'R':
2853 c1669e2e 2019-01-09 stsp recurse = 1;
2854 c1669e2e 2019-01-09 stsp break;
2855 5de5890b 2018-10-18 stsp default:
2856 2deda0b9 2019-03-07 stsp usage_tree();
2857 5de5890b 2018-10-18 stsp /* NOTREACHED */
2858 5de5890b 2018-10-18 stsp }
2859 5de5890b 2018-10-18 stsp }
2860 5de5890b 2018-10-18 stsp
2861 5de5890b 2018-10-18 stsp argc -= optind;
2862 5de5890b 2018-10-18 stsp argv += optind;
2863 5de5890b 2018-10-18 stsp
2864 5de5890b 2018-10-18 stsp if (argc == 1)
2865 5de5890b 2018-10-18 stsp path = argv[0];
2866 5de5890b 2018-10-18 stsp else if (argc > 1)
2867 5de5890b 2018-10-18 stsp usage_tree();
2868 5de5890b 2018-10-18 stsp else
2869 7a2c19d6 2019-02-05 stsp path = NULL;
2870 7a2c19d6 2019-02-05 stsp
2871 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2872 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2873 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2874 9bf7a39b 2019-02-05 stsp goto done;
2875 9bf7a39b 2019-02-05 stsp }
2876 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2877 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2878 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2879 7a2c19d6 2019-02-05 stsp goto done;
2880 7a2c19d6 2019-02-05 stsp else
2881 7a2c19d6 2019-02-05 stsp error = NULL;
2882 7a2c19d6 2019-02-05 stsp if (worktree) {
2883 7a2c19d6 2019-02-05 stsp repo_path =
2884 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2885 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2886 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2887 7a2c19d6 2019-02-05 stsp if (error)
2888 7a2c19d6 2019-02-05 stsp goto done;
2889 7a2c19d6 2019-02-05 stsp } else {
2890 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2891 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2892 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2893 7a2c19d6 2019-02-05 stsp goto done;
2894 7a2c19d6 2019-02-05 stsp }
2895 5de5890b 2018-10-18 stsp }
2896 5de5890b 2018-10-18 stsp }
2897 5de5890b 2018-10-18 stsp
2898 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2899 5de5890b 2018-10-18 stsp if (error != NULL)
2900 c02c541e 2019-03-29 stsp goto done;
2901 c02c541e 2019-03-29 stsp
2902 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2903 c02c541e 2019-03-29 stsp if (error)
2904 5de5890b 2018-10-18 stsp goto done;
2905 5de5890b 2018-10-18 stsp
2906 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2907 9bf7a39b 2019-02-05 stsp if (worktree) {
2908 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2909 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2910 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2911 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2912 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2913 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2914 9bf7a39b 2019-02-05 stsp goto done;
2915 9bf7a39b 2019-02-05 stsp }
2916 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 1);
2917 9bf7a39b 2019-02-05 stsp free(p);
2918 9bf7a39b 2019-02-05 stsp if (error)
2919 9bf7a39b 2019-02-05 stsp goto done;
2920 9bf7a39b 2019-02-05 stsp } else
2921 9bf7a39b 2019-02-05 stsp path = "/";
2922 9bf7a39b 2019-02-05 stsp }
2923 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2924 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2925 9bf7a39b 2019-02-05 stsp if (error != NULL)
2926 9bf7a39b 2019-02-05 stsp goto done;
2927 9bf7a39b 2019-02-05 stsp }
2928 5de5890b 2018-10-18 stsp
2929 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2930 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2931 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2932 5de5890b 2018-10-18 stsp if (error != NULL)
2933 5de5890b 2018-10-18 stsp goto done;
2934 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2935 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2936 5de5890b 2018-10-18 stsp if (error != NULL)
2937 5de5890b 2018-10-18 stsp goto done;
2938 5de5890b 2018-10-18 stsp } else {
2939 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2940 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2941 30837e32 2019-07-25 stsp if (error)
2942 5de5890b 2018-10-18 stsp goto done;
2943 5de5890b 2018-10-18 stsp }
2944 5de5890b 2018-10-18 stsp
2945 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2946 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2947 5de5890b 2018-10-18 stsp done:
2948 5de5890b 2018-10-18 stsp free(in_repo_path);
2949 5de5890b 2018-10-18 stsp free(repo_path);
2950 5de5890b 2018-10-18 stsp free(cwd);
2951 5de5890b 2018-10-18 stsp free(commit_id);
2952 7a2c19d6 2019-02-05 stsp if (worktree)
2953 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2954 5de5890b 2018-10-18 stsp if (repo) {
2955 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2956 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2957 5de5890b 2018-10-18 stsp if (error == NULL)
2958 5de5890b 2018-10-18 stsp error = repo_error;
2959 5de5890b 2018-10-18 stsp }
2960 5de5890b 2018-10-18 stsp return error;
2961 5de5890b 2018-10-18 stsp }
2962 5de5890b 2018-10-18 stsp
2963 6bad629b 2019-02-04 stsp __dead static void
2964 6bad629b 2019-02-04 stsp usage_status(void)
2965 6bad629b 2019-02-04 stsp {
2966 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2967 6bad629b 2019-02-04 stsp exit(1);
2968 6bad629b 2019-02-04 stsp }
2969 5c860e29 2018-03-12 stsp
2970 b72f483a 2019-02-05 stsp static const struct got_error *
2971 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
2972 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2973 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2974 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2975 6bad629b 2019-02-04 stsp {
2976 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
2977 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
2978 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
2979 b72f483a 2019-02-05 stsp return NULL;
2980 6bad629b 2019-02-04 stsp }
2981 5c860e29 2018-03-12 stsp
2982 6bad629b 2019-02-04 stsp static const struct got_error *
2983 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
2984 6bad629b 2019-02-04 stsp {
2985 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
2986 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
2987 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
2988 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
2989 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2990 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
2991 a5edda0a 2019-07-27 stsp int ch;
2992 5c860e29 2018-03-12 stsp
2993 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2994 72ea6654 2019-07-27 stsp
2995 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
2996 6bad629b 2019-02-04 stsp switch (ch) {
2997 5c860e29 2018-03-12 stsp default:
2998 2deda0b9 2019-03-07 stsp usage_status();
2999 6bad629b 2019-02-04 stsp /* NOTREACHED */
3000 5c860e29 2018-03-12 stsp }
3001 5c860e29 2018-03-12 stsp }
3002 5c860e29 2018-03-12 stsp
3003 6bad629b 2019-02-04 stsp argc -= optind;
3004 6bad629b 2019-02-04 stsp argv += optind;
3005 5c860e29 2018-03-12 stsp
3006 6bad629b 2019-02-04 stsp #ifndef PROFILE
3007 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3008 6bad629b 2019-02-04 stsp NULL) == -1)
3009 6bad629b 2019-02-04 stsp err(1, "pledge");
3010 f42b1b34 2018-03-12 stsp #endif
3011 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3012 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3013 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3014 927df6b7 2019-02-10 stsp goto done;
3015 927df6b7 2019-02-10 stsp }
3016 927df6b7 2019-02-10 stsp
3017 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3018 927df6b7 2019-02-10 stsp if (error != NULL)
3019 a5edda0a 2019-07-27 stsp goto done;
3020 6bad629b 2019-02-04 stsp
3021 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3022 c9956ddf 2019-09-08 stsp NULL);
3023 6bad629b 2019-02-04 stsp if (error != NULL)
3024 6bad629b 2019-02-04 stsp goto done;
3025 6bad629b 2019-02-04 stsp
3026 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3027 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3028 087fb88c 2019-08-04 stsp if (error)
3029 087fb88c 2019-08-04 stsp goto done;
3030 087fb88c 2019-08-04 stsp
3031 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3032 6bad629b 2019-02-04 stsp if (error)
3033 6bad629b 2019-02-04 stsp goto done;
3034 6bad629b 2019-02-04 stsp
3035 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3036 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3037 6bad629b 2019-02-04 stsp done:
3038 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3039 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3040 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3041 927df6b7 2019-02-10 stsp free(cwd);
3042 d0eebce4 2019-03-11 stsp return error;
3043 d0eebce4 2019-03-11 stsp }
3044 d0eebce4 2019-03-11 stsp
3045 d0eebce4 2019-03-11 stsp __dead static void
3046 d0eebce4 2019-03-11 stsp usage_ref(void)
3047 d0eebce4 2019-03-11 stsp {
3048 d0eebce4 2019-03-11 stsp fprintf(stderr,
3049 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3050 d0eebce4 2019-03-11 stsp getprogname());
3051 d0eebce4 2019-03-11 stsp exit(1);
3052 d0eebce4 2019-03-11 stsp }
3053 d0eebce4 2019-03-11 stsp
3054 d0eebce4 2019-03-11 stsp static const struct got_error *
3055 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3056 d0eebce4 2019-03-11 stsp {
3057 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3058 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3059 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3060 d0eebce4 2019-03-11 stsp
3061 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3062 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3063 d0eebce4 2019-03-11 stsp if (err)
3064 d0eebce4 2019-03-11 stsp return err;
3065 d0eebce4 2019-03-11 stsp
3066 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3067 d0eebce4 2019-03-11 stsp char *refstr;
3068 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3069 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3070 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3071 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3072 d0eebce4 2019-03-11 stsp free(refstr);
3073 d0eebce4 2019-03-11 stsp }
3074 d0eebce4 2019-03-11 stsp
3075 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3076 d0eebce4 2019-03-11 stsp return NULL;
3077 d0eebce4 2019-03-11 stsp }
3078 d0eebce4 2019-03-11 stsp
3079 d0eebce4 2019-03-11 stsp static const struct got_error *
3080 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3081 d0eebce4 2019-03-11 stsp {
3082 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3083 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3084 d0eebce4 2019-03-11 stsp
3085 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3086 d0eebce4 2019-03-11 stsp if (err)
3087 d0eebce4 2019-03-11 stsp return err;
3088 d0eebce4 2019-03-11 stsp
3089 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3090 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3091 d0eebce4 2019-03-11 stsp return err;
3092 d0eebce4 2019-03-11 stsp }
3093 d0eebce4 2019-03-11 stsp
3094 d0eebce4 2019-03-11 stsp static const struct got_error *
3095 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3096 d0eebce4 2019-03-11 stsp {
3097 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3098 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3099 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3100 d1644381 2019-07-14 stsp
3101 d1644381 2019-07-14 stsp /*
3102 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3103 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3104 d1644381 2019-07-14 stsp * an unintended typo.
3105 d1644381 2019-07-14 stsp */
3106 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3107 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3108 d0eebce4 2019-03-11 stsp
3109 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3110 dd88155e 2019-06-29 stsp repo);
3111 d83d9d5c 2019-05-13 stsp if (err) {
3112 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3113 d0eebce4 2019-03-11 stsp
3114 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3115 d83d9d5c 2019-05-13 stsp return err;
3116 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3117 d83d9d5c 2019-05-13 stsp if (err)
3118 d83d9d5c 2019-05-13 stsp return err;
3119 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3120 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3121 d83d9d5c 2019-05-13 stsp if (err)
3122 d83d9d5c 2019-05-13 stsp return err;
3123 d83d9d5c 2019-05-13 stsp }
3124 d83d9d5c 2019-05-13 stsp
3125 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3126 d0eebce4 2019-03-11 stsp if (err)
3127 d0eebce4 2019-03-11 stsp goto done;
3128 d0eebce4 2019-03-11 stsp
3129 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3130 d0eebce4 2019-03-11 stsp done:
3131 d0eebce4 2019-03-11 stsp if (ref)
3132 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3133 d0eebce4 2019-03-11 stsp free(id);
3134 d1c1ae5f 2019-08-12 stsp return err;
3135 d1c1ae5f 2019-08-12 stsp }
3136 d1c1ae5f 2019-08-12 stsp
3137 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3138 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3139 d1c1ae5f 2019-08-12 stsp {
3140 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3141 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3142 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3143 d1c1ae5f 2019-08-12 stsp
3144 d1c1ae5f 2019-08-12 stsp /*
3145 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3146 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3147 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3148 d1c1ae5f 2019-08-12 stsp */
3149 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3150 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3151 d1c1ae5f 2019-08-12 stsp
3152 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3153 d1c1ae5f 2019-08-12 stsp if (err)
3154 d1c1ae5f 2019-08-12 stsp return err;
3155 d1c1ae5f 2019-08-12 stsp
3156 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3157 d1c1ae5f 2019-08-12 stsp if (err)
3158 d1c1ae5f 2019-08-12 stsp goto done;
3159 d1c1ae5f 2019-08-12 stsp
3160 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3161 d1c1ae5f 2019-08-12 stsp done:
3162 d1c1ae5f 2019-08-12 stsp if (target_ref)
3163 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3164 d1c1ae5f 2019-08-12 stsp if (ref)
3165 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3166 d0eebce4 2019-03-11 stsp return err;
3167 d0eebce4 2019-03-11 stsp }
3168 d0eebce4 2019-03-11 stsp
3169 d0eebce4 2019-03-11 stsp static const struct got_error *
3170 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3171 d0eebce4 2019-03-11 stsp {
3172 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3173 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3174 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3175 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3176 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3177 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3178 d0eebce4 2019-03-11 stsp
3179 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3180 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3181 d0eebce4 2019-03-11 stsp switch (ch) {
3182 d0eebce4 2019-03-11 stsp case 'd':
3183 d0eebce4 2019-03-11 stsp delref = optarg;
3184 d0eebce4 2019-03-11 stsp break;
3185 d0eebce4 2019-03-11 stsp case 'r':
3186 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3187 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3188 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3189 9ba1d308 2019-10-21 stsp optarg);
3190 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3191 d0eebce4 2019-03-11 stsp break;
3192 d0eebce4 2019-03-11 stsp case 'l':
3193 d0eebce4 2019-03-11 stsp do_list = 1;
3194 d1c1ae5f 2019-08-12 stsp break;
3195 d1c1ae5f 2019-08-12 stsp case 's':
3196 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3197 d0eebce4 2019-03-11 stsp break;
3198 d0eebce4 2019-03-11 stsp default:
3199 d0eebce4 2019-03-11 stsp usage_ref();
3200 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3201 d0eebce4 2019-03-11 stsp }
3202 d0eebce4 2019-03-11 stsp }
3203 d0eebce4 2019-03-11 stsp
3204 d0eebce4 2019-03-11 stsp if (do_list && delref)
3205 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3206 d0eebce4 2019-03-11 stsp
3207 d0eebce4 2019-03-11 stsp argc -= optind;
3208 d0eebce4 2019-03-11 stsp argv += optind;
3209 d0eebce4 2019-03-11 stsp
3210 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3211 d1c1ae5f 2019-08-12 stsp if (create_symref)
3212 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3213 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3214 d0eebce4 2019-03-11 stsp if (argc > 0)
3215 d0eebce4 2019-03-11 stsp usage_ref();
3216 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3217 d0eebce4 2019-03-11 stsp usage_ref();
3218 d0eebce4 2019-03-11 stsp
3219 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3220 e0b57350 2019-03-12 stsp if (do_list) {
3221 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3222 e0b57350 2019-03-12 stsp NULL) == -1)
3223 e0b57350 2019-03-12 stsp err(1, "pledge");
3224 e0b57350 2019-03-12 stsp } else {
3225 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3226 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3227 e0b57350 2019-03-12 stsp err(1, "pledge");
3228 e0b57350 2019-03-12 stsp }
3229 d0eebce4 2019-03-11 stsp #endif
3230 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3231 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3232 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3233 d0eebce4 2019-03-11 stsp goto done;
3234 d0eebce4 2019-03-11 stsp }
3235 d0eebce4 2019-03-11 stsp
3236 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3237 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3238 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3239 d0eebce4 2019-03-11 stsp goto done;
3240 d0eebce4 2019-03-11 stsp else
3241 d0eebce4 2019-03-11 stsp error = NULL;
3242 d0eebce4 2019-03-11 stsp if (worktree) {
3243 d0eebce4 2019-03-11 stsp repo_path =
3244 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3245 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3246 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3247 d0eebce4 2019-03-11 stsp if (error)
3248 d0eebce4 2019-03-11 stsp goto done;
3249 d0eebce4 2019-03-11 stsp } else {
3250 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3251 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3252 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3253 d0eebce4 2019-03-11 stsp goto done;
3254 d0eebce4 2019-03-11 stsp }
3255 d0eebce4 2019-03-11 stsp }
3256 d0eebce4 2019-03-11 stsp }
3257 d0eebce4 2019-03-11 stsp
3258 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3259 d0eebce4 2019-03-11 stsp if (error != NULL)
3260 d0eebce4 2019-03-11 stsp goto done;
3261 d0eebce4 2019-03-11 stsp
3262 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3263 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3264 c02c541e 2019-03-29 stsp if (error)
3265 c02c541e 2019-03-29 stsp goto done;
3266 c02c541e 2019-03-29 stsp
3267 d0eebce4 2019-03-11 stsp if (do_list)
3268 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3269 d0eebce4 2019-03-11 stsp else if (delref)
3270 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3271 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3272 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3273 d0eebce4 2019-03-11 stsp else
3274 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3275 4e759de4 2019-06-26 stsp done:
3276 4e759de4 2019-06-26 stsp if (repo)
3277 4e759de4 2019-06-26 stsp got_repo_close(repo);
3278 4e759de4 2019-06-26 stsp if (worktree)
3279 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3280 4e759de4 2019-06-26 stsp free(cwd);
3281 4e759de4 2019-06-26 stsp free(repo_path);
3282 4e759de4 2019-06-26 stsp return error;
3283 4e759de4 2019-06-26 stsp }
3284 4e759de4 2019-06-26 stsp
3285 4e759de4 2019-06-26 stsp __dead static void
3286 4e759de4 2019-06-26 stsp usage_branch(void)
3287 4e759de4 2019-06-26 stsp {
3288 4e759de4 2019-06-26 stsp fprintf(stderr,
3289 a74f7e83 2019-11-10 stsp "usage: %s branch [-c commit] [-r repository] [-l] | -d name | "
3290 a74f7e83 2019-11-10 stsp "[name]\n", getprogname());
3291 4e759de4 2019-06-26 stsp exit(1);
3292 4e759de4 2019-06-26 stsp }
3293 4e759de4 2019-06-26 stsp
3294 4e759de4 2019-06-26 stsp static const struct got_error *
3295 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3296 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3297 4e99b47e 2019-10-04 stsp {
3298 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3299 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3300 4e99b47e 2019-10-04 stsp char *refstr;
3301 4e99b47e 2019-10-04 stsp
3302 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3303 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3304 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3305 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3306 4e99b47e 2019-10-04 stsp
3307 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3308 4e99b47e 2019-10-04 stsp if (err)
3309 4e99b47e 2019-10-04 stsp return err;
3310 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3311 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3312 4e99b47e 2019-10-04 stsp marker = "* ";
3313 4e99b47e 2019-10-04 stsp else
3314 4e99b47e 2019-10-04 stsp marker = "~ ";
3315 4e99b47e 2019-10-04 stsp free(id);
3316 4e99b47e 2019-10-04 stsp }
3317 4e99b47e 2019-10-04 stsp
3318 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3319 4e99b47e 2019-10-04 stsp refname += 11;
3320 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3321 4e99b47e 2019-10-04 stsp refname += 18;
3322 4e99b47e 2019-10-04 stsp
3323 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3324 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3325 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3326 4e99b47e 2019-10-04 stsp
3327 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3328 4e99b47e 2019-10-04 stsp free(refstr);
3329 4e99b47e 2019-10-04 stsp return NULL;
3330 4e99b47e 2019-10-04 stsp }
3331 4e99b47e 2019-10-04 stsp
3332 4e99b47e 2019-10-04 stsp static const struct got_error *
3333 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3334 ad89fa31 2019-10-04 stsp {
3335 ad89fa31 2019-10-04 stsp const char *refname;
3336 ad89fa31 2019-10-04 stsp
3337 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3338 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3339 ad89fa31 2019-10-04 stsp
3340 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3341 ad89fa31 2019-10-04 stsp
3342 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3343 ad89fa31 2019-10-04 stsp refname += 11;
3344 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3345 ad89fa31 2019-10-04 stsp refname += 18;
3346 ad89fa31 2019-10-04 stsp
3347 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3348 ad89fa31 2019-10-04 stsp
3349 ad89fa31 2019-10-04 stsp return NULL;
3350 ad89fa31 2019-10-04 stsp }
3351 ad89fa31 2019-10-04 stsp
3352 ad89fa31 2019-10-04 stsp static const struct got_error *
3353 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3354 4e759de4 2019-06-26 stsp {
3355 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3356 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3357 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3358 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3359 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3360 4e759de4 2019-06-26 stsp
3361 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3362 ba882ee3 2019-07-11 stsp
3363 4e99b47e 2019-10-04 stsp if (worktree) {
3364 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3365 4e99b47e 2019-10-04 stsp worktree);
3366 4e99b47e 2019-10-04 stsp if (err)
3367 4e99b47e 2019-10-04 stsp return err;
3368 4e99b47e 2019-10-04 stsp
3369 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3370 4e99b47e 2019-10-04 stsp worktree);
3371 4e99b47e 2019-10-04 stsp if (err)
3372 4e99b47e 2019-10-04 stsp return err;
3373 4e99b47e 2019-10-04 stsp
3374 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3375 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3376 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3377 4e99b47e 2019-10-04 stsp if (err)
3378 4e99b47e 2019-10-04 stsp return err;
3379 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3380 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3381 4e99b47e 2019-10-04 stsp }
3382 4e99b47e 2019-10-04 stsp }
3383 4e99b47e 2019-10-04 stsp
3384 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3385 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3386 4e759de4 2019-06-26 stsp if (err)
3387 4e759de4 2019-06-26 stsp return err;
3388 4e759de4 2019-06-26 stsp
3389 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3390 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3391 4e759de4 2019-06-26 stsp
3392 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3393 4e759de4 2019-06-26 stsp return NULL;
3394 4e759de4 2019-06-26 stsp }
3395 4e759de4 2019-06-26 stsp
3396 4e759de4 2019-06-26 stsp static const struct got_error *
3397 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3398 45cd4e47 2019-08-25 stsp const char *branch_name)
3399 4e759de4 2019-06-26 stsp {
3400 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3401 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3402 4e759de4 2019-06-26 stsp char *refname;
3403 4e759de4 2019-06-26 stsp
3404 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3405 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3406 4e759de4 2019-06-26 stsp
3407 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3408 4e759de4 2019-06-26 stsp if (err)
3409 4e759de4 2019-06-26 stsp goto done;
3410 4e759de4 2019-06-26 stsp
3411 45cd4e47 2019-08-25 stsp if (worktree &&
3412 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3413 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3414 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3415 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3416 45cd4e47 2019-08-25 stsp goto done;
3417 45cd4e47 2019-08-25 stsp }
3418 45cd4e47 2019-08-25 stsp
3419 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3420 4e759de4 2019-06-26 stsp done:
3421 45cd4e47 2019-08-25 stsp if (ref)
3422 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3423 4e759de4 2019-06-26 stsp free(refname);
3424 4e759de4 2019-06-26 stsp return err;
3425 4e759de4 2019-06-26 stsp }
3426 4e759de4 2019-06-26 stsp
3427 4e759de4 2019-06-26 stsp static const struct got_error *
3428 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3429 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3430 4e759de4 2019-06-26 stsp {
3431 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3432 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3433 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3434 d3f84d51 2019-07-11 stsp
3435 d3f84d51 2019-07-11 stsp /*
3436 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3437 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3438 d3f84d51 2019-07-11 stsp * an unintended typo.
3439 d3f84d51 2019-07-11 stsp */
3440 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3441 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3442 4e759de4 2019-06-26 stsp
3443 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3444 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3445 4e759de4 2019-06-26 stsp goto done;
3446 4e759de4 2019-06-26 stsp }
3447 4e759de4 2019-06-26 stsp
3448 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3449 4e759de4 2019-06-26 stsp if (err == NULL) {
3450 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3451 4e759de4 2019-06-26 stsp goto done;
3452 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3453 4e759de4 2019-06-26 stsp goto done;
3454 4e759de4 2019-06-26 stsp
3455 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3456 4e759de4 2019-06-26 stsp if (err)
3457 4e759de4 2019-06-26 stsp goto done;
3458 4e759de4 2019-06-26 stsp
3459 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3460 d0eebce4 2019-03-11 stsp done:
3461 4e759de4 2019-06-26 stsp if (ref)
3462 4e759de4 2019-06-26 stsp got_ref_close(ref);
3463 4e759de4 2019-06-26 stsp free(base_refname);
3464 4e759de4 2019-06-26 stsp free(refname);
3465 4e759de4 2019-06-26 stsp return err;
3466 4e759de4 2019-06-26 stsp }
3467 4e759de4 2019-06-26 stsp
3468 4e759de4 2019-06-26 stsp static const struct got_error *
3469 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3470 4e759de4 2019-06-26 stsp {
3471 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3472 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3473 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3474 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3475 ad89fa31 2019-10-04 stsp int ch, do_list = 0, do_show = 0;
3476 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3477 4e759de4 2019-06-26 stsp
3478 a74f7e83 2019-11-10 stsp while ((ch = getopt(argc, argv, "c:d:r:l")) != -1) {
3479 4e759de4 2019-06-26 stsp switch (ch) {
3480 a74f7e83 2019-11-10 stsp case 'c':
3481 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3482 a74f7e83 2019-11-10 stsp break;
3483 4e759de4 2019-06-26 stsp case 'd':
3484 4e759de4 2019-06-26 stsp delref = optarg;
3485 4e759de4 2019-06-26 stsp break;
3486 4e759de4 2019-06-26 stsp case 'r':
3487 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3488 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3489 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3490 9ba1d308 2019-10-21 stsp optarg);
3491 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3492 4e759de4 2019-06-26 stsp break;
3493 4e759de4 2019-06-26 stsp case 'l':
3494 4e759de4 2019-06-26 stsp do_list = 1;
3495 4e759de4 2019-06-26 stsp break;
3496 4e759de4 2019-06-26 stsp default:
3497 4e759de4 2019-06-26 stsp usage_branch();
3498 4e759de4 2019-06-26 stsp /* NOTREACHED */
3499 4e759de4 2019-06-26 stsp }
3500 4e759de4 2019-06-26 stsp }
3501 4e759de4 2019-06-26 stsp
3502 4e759de4 2019-06-26 stsp if (do_list && delref)
3503 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3504 4e759de4 2019-06-26 stsp
3505 4e759de4 2019-06-26 stsp argc -= optind;
3506 4e759de4 2019-06-26 stsp argv += optind;
3507 ad89fa31 2019-10-04 stsp
3508 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3509 ad89fa31 2019-10-04 stsp do_show = 1;
3510 4e759de4 2019-06-26 stsp
3511 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3512 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3513 a74f7e83 2019-11-10 stsp
3514 4e759de4 2019-06-26 stsp if (do_list || delref) {
3515 4e759de4 2019-06-26 stsp if (argc > 0)
3516 4e759de4 2019-06-26 stsp usage_branch();
3517 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3518 4e759de4 2019-06-26 stsp usage_branch();
3519 4e759de4 2019-06-26 stsp
3520 4e759de4 2019-06-26 stsp #ifndef PROFILE
3521 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3522 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3523 4e759de4 2019-06-26 stsp NULL) == -1)
3524 4e759de4 2019-06-26 stsp err(1, "pledge");
3525 4e759de4 2019-06-26 stsp } else {
3526 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3527 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3528 4e759de4 2019-06-26 stsp err(1, "pledge");
3529 4e759de4 2019-06-26 stsp }
3530 4e759de4 2019-06-26 stsp #endif
3531 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3532 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3533 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3534 4e759de4 2019-06-26 stsp goto done;
3535 4e759de4 2019-06-26 stsp }
3536 4e759de4 2019-06-26 stsp
3537 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3538 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3539 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3540 4e759de4 2019-06-26 stsp goto done;
3541 4e759de4 2019-06-26 stsp else
3542 4e759de4 2019-06-26 stsp error = NULL;
3543 4e759de4 2019-06-26 stsp if (worktree) {
3544 4e759de4 2019-06-26 stsp repo_path =
3545 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3546 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3547 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3548 4e759de4 2019-06-26 stsp if (error)
3549 4e759de4 2019-06-26 stsp goto done;
3550 4e759de4 2019-06-26 stsp } else {
3551 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3552 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3553 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3554 4e759de4 2019-06-26 stsp goto done;
3555 4e759de4 2019-06-26 stsp }
3556 4e759de4 2019-06-26 stsp }
3557 4e759de4 2019-06-26 stsp }
3558 4e759de4 2019-06-26 stsp
3559 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3560 4e759de4 2019-06-26 stsp if (error != NULL)
3561 4e759de4 2019-06-26 stsp goto done;
3562 4e759de4 2019-06-26 stsp
3563 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3564 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3565 4e759de4 2019-06-26 stsp if (error)
3566 4e759de4 2019-06-26 stsp goto done;
3567 4e759de4 2019-06-26 stsp
3568 ad89fa31 2019-10-04 stsp if (do_show)
3569 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3570 ad89fa31 2019-10-04 stsp else if (do_list)
3571 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3572 4e759de4 2019-06-26 stsp else if (delref)
3573 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3574 4e759de4 2019-06-26 stsp else {
3575 a74f7e83 2019-11-10 stsp struct got_object_id *commit_id;
3576 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3577 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3578 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3579 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3580 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3581 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3582 a74f7e83 2019-11-10 stsp if (error)
3583 a74f7e83 2019-11-10 stsp goto done;
3584 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3585 a74f7e83 2019-11-10 stsp free(commit_id);
3586 4e759de4 2019-06-26 stsp }
3587 4e759de4 2019-06-26 stsp done:
3588 d0eebce4 2019-03-11 stsp if (repo)
3589 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3590 d0eebce4 2019-03-11 stsp if (worktree)
3591 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3592 d0eebce4 2019-03-11 stsp free(cwd);
3593 d0eebce4 2019-03-11 stsp free(repo_path);
3594 d00136be 2019-03-26 stsp return error;
3595 d00136be 2019-03-26 stsp }
3596 d00136be 2019-03-26 stsp
3597 8e7bd50a 2019-08-22 stsp
3598 d00136be 2019-03-26 stsp __dead static void
3599 8e7bd50a 2019-08-22 stsp usage_tag(void)
3600 8e7bd50a 2019-08-22 stsp {
3601 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3602 c904c63e 2019-08-22 stsp "usage: %s tag [-r repository] | -l | "
3603 8e7bd50a 2019-08-22 stsp "[-m message] name [commit]\n", getprogname());
3604 8e7bd50a 2019-08-22 stsp exit(1);
3605 b8bad2ba 2019-08-23 stsp }
3606 b8bad2ba 2019-08-23 stsp
3607 b8bad2ba 2019-08-23 stsp #if 0
3608 b8bad2ba 2019-08-23 stsp static const struct got_error *
3609 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3610 b8bad2ba 2019-08-23 stsp {
3611 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3612 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3613 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3614 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3615 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3616 b8bad2ba 2019-08-23 stsp
3617 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3618 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3619 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3620 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3621 b8bad2ba 2019-08-23 stsp if (err)
3622 b8bad2ba 2019-08-23 stsp return err;
3623 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3624 b8bad2ba 2019-08-23 stsp continue;
3625 b8bad2ba 2019-08-23 stsp } else {
3626 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3627 b8bad2ba 2019-08-23 stsp if (err)
3628 b8bad2ba 2019-08-23 stsp break;
3629 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3630 b8bad2ba 2019-08-23 stsp free(re_id);
3631 b8bad2ba 2019-08-23 stsp if (err)
3632 b8bad2ba 2019-08-23 stsp break;
3633 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3634 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3635 b8bad2ba 2019-08-23 stsp }
3636 b8bad2ba 2019-08-23 stsp
3637 b8bad2ba 2019-08-23 stsp while (se) {
3638 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3639 b8bad2ba 2019-08-23 stsp if (err)
3640 b8bad2ba 2019-08-23 stsp break;
3641 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3642 b8bad2ba 2019-08-23 stsp free(se_id);
3643 b8bad2ba 2019-08-23 stsp if (err)
3644 b8bad2ba 2019-08-23 stsp break;
3645 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3646 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3647 b8bad2ba 2019-08-23 stsp
3648 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3649 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3650 b8bad2ba 2019-08-23 stsp if (err)
3651 b8bad2ba 2019-08-23 stsp return err;
3652 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3653 b8bad2ba 2019-08-23 stsp break;
3654 b8bad2ba 2019-08-23 stsp }
3655 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3656 b8bad2ba 2019-08-23 stsp continue;
3657 b8bad2ba 2019-08-23 stsp }
3658 b8bad2ba 2019-08-23 stsp }
3659 b8bad2ba 2019-08-23 stsp done:
3660 b8bad2ba 2019-08-23 stsp return err;
3661 8e7bd50a 2019-08-22 stsp }
3662 b8bad2ba 2019-08-23 stsp #endif
3663 b8bad2ba 2019-08-23 stsp
3664 b8bad2ba 2019-08-23 stsp static const struct got_error *
3665 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3666 8e7bd50a 2019-08-22 stsp {
3667 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3668 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3669 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3670 8e7bd50a 2019-08-22 stsp
3671 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3672 8e7bd50a 2019-08-22 stsp
3673 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3674 8e7bd50a 2019-08-22 stsp if (err)
3675 8e7bd50a 2019-08-22 stsp return err;
3676 8e7bd50a 2019-08-22 stsp
3677 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3678 8e7bd50a 2019-08-22 stsp const char *refname;
3679 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3680 8e7bd50a 2019-08-22 stsp char datebuf[26];
3681 d4efa91b 2020-01-14 stsp const char *tagger;
3682 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3683 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3684 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3685 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3686 8e7bd50a 2019-08-22 stsp
3687 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3688 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3689 8e7bd50a 2019-08-22 stsp continue;
3690 8e7bd50a 2019-08-22 stsp refname += 10;
3691 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3692 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3693 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3694 8e7bd50a 2019-08-22 stsp break;
3695 8e7bd50a 2019-08-22 stsp }
3696 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3697 8e7bd50a 2019-08-22 stsp free(refstr);
3698 8e7bd50a 2019-08-22 stsp
3699 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3700 8e7bd50a 2019-08-22 stsp if (err)
3701 8e7bd50a 2019-08-22 stsp break;
3702 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3703 d4efa91b 2020-01-14 stsp if (err) {
3704 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3705 d4efa91b 2020-01-14 stsp free(id);
3706 d4efa91b 2020-01-14 stsp break;
3707 d4efa91b 2020-01-14 stsp }
3708 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3709 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3710 d4efa91b 2020-01-14 stsp if (err) {
3711 d4efa91b 2020-01-14 stsp free(id);
3712 d4efa91b 2020-01-14 stsp break;
3713 d4efa91b 2020-01-14 stsp }
3714 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3715 d4efa91b 2020-01-14 stsp tagger_time =
3716 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3717 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3718 d4efa91b 2020-01-14 stsp free(id);
3719 d4efa91b 2020-01-14 stsp if (err)
3720 d4efa91b 2020-01-14 stsp break;
3721 d4efa91b 2020-01-14 stsp } else {
3722 d4efa91b 2020-01-14 stsp free(id);
3723 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3724 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3725 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3726 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3727 d4efa91b 2020-01-14 stsp if (err)
3728 d4efa91b 2020-01-14 stsp break;
3729 d4efa91b 2020-01-14 stsp }
3730 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3731 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3732 2417344c 2019-08-23 stsp if (datestr)
3733 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3734 d4efa91b 2020-01-14 stsp if (commit)
3735 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3736 d4efa91b 2020-01-14 stsp else {
3737 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3738 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3739 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3740 d4efa91b 2020-01-14 stsp id_str);
3741 d4efa91b 2020-01-14 stsp break;
3742 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3743 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3744 d4efa91b 2020-01-14 stsp id_str);
3745 d4efa91b 2020-01-14 stsp break;
3746 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3747 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3748 d4efa91b 2020-01-14 stsp id_str);
3749 d4efa91b 2020-01-14 stsp break;
3750 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
3751 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3752 d4efa91b 2020-01-14 stsp id_str);
3753 d4efa91b 2020-01-14 stsp break;
3754 d4efa91b 2020-01-14 stsp default:
3755 d4efa91b 2020-01-14 stsp break;
3756 d4efa91b 2020-01-14 stsp }
3757 8e7bd50a 2019-08-22 stsp }
3758 8e7bd50a 2019-08-22 stsp free(id_str);
3759 d4efa91b 2020-01-14 stsp if (commit) {
3760 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
3761 d4efa91b 2020-01-14 stsp if (err)
3762 d4efa91b 2020-01-14 stsp break;
3763 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
3764 d4efa91b 2020-01-14 stsp } else {
3765 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3766 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
3767 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
3768 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
3769 d4efa91b 2020-01-14 stsp break;
3770 d4efa91b 2020-01-14 stsp }
3771 8e7bd50a 2019-08-22 stsp }
3772 8e7bd50a 2019-08-22 stsp
3773 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3774 8e7bd50a 2019-08-22 stsp do {
3775 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3776 8e7bd50a 2019-08-22 stsp if (line)
3777 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3778 8e7bd50a 2019-08-22 stsp } while (line);
3779 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3780 8e7bd50a 2019-08-22 stsp }
3781 8e7bd50a 2019-08-22 stsp
3782 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3783 8e7bd50a 2019-08-22 stsp return NULL;
3784 8e7bd50a 2019-08-22 stsp }
3785 8e7bd50a 2019-08-22 stsp
3786 8e7bd50a 2019-08-22 stsp static const struct got_error *
3787 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3788 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3789 8e7bd50a 2019-08-22 stsp {
3790 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3791 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3792 f372d5cd 2019-10-21 stsp char *editor = NULL;
3793 8e7bd50a 2019-08-22 stsp int fd = -1;
3794 8e7bd50a 2019-08-22 stsp
3795 8e7bd50a 2019-08-22 stsp if (asprintf(&template, "/tmp/got-tagmsg") == -1) {
3796 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3797 8e7bd50a 2019-08-22 stsp goto done;
3798 8e7bd50a 2019-08-22 stsp }
3799 8e7bd50a 2019-08-22 stsp
3800 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3801 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3802 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3803 8e7bd50a 2019-08-22 stsp goto done;
3804 8e7bd50a 2019-08-22 stsp }
3805 8e7bd50a 2019-08-22 stsp
3806 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3807 8e7bd50a 2019-08-22 stsp if (err)
3808 8e7bd50a 2019-08-22 stsp goto done;
3809 8e7bd50a 2019-08-22 stsp
3810 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3811 8e7bd50a 2019-08-22 stsp close(fd);
3812 8e7bd50a 2019-08-22 stsp
3813 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3814 8e7bd50a 2019-08-22 stsp if (err)
3815 8e7bd50a 2019-08-22 stsp goto done;
3816 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3817 8e7bd50a 2019-08-22 stsp done:
3818 8e7bd50a 2019-08-22 stsp free(initial_content);
3819 8e7bd50a 2019-08-22 stsp free(template);
3820 8e7bd50a 2019-08-22 stsp free(editor);
3821 8e7bd50a 2019-08-22 stsp
3822 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3823 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3824 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3825 8e7bd50a 2019-08-22 stsp if (err) {
3826 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3827 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3828 8e7bd50a 2019-08-22 stsp }
3829 8e7bd50a 2019-08-22 stsp }
3830 8e7bd50a 2019-08-22 stsp return err;
3831 8e7bd50a 2019-08-22 stsp }
3832 8e7bd50a 2019-08-22 stsp
3833 8e7bd50a 2019-08-22 stsp static const struct got_error *
3834 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3835 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3836 8e7bd50a 2019-08-22 stsp {
3837 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3838 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3839 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3840 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3841 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3842 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3843 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3844 8e7bd50a 2019-08-22 stsp
3845 8e7bd50a 2019-08-22 stsp /*
3846 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3847 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3848 8e7bd50a 2019-08-22 stsp * an unintended typo.
3849 8e7bd50a 2019-08-22 stsp */
3850 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3851 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3852 8e7bd50a 2019-08-22 stsp
3853 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3854 8e7bd50a 2019-08-22 stsp if (err)
3855 8e7bd50a 2019-08-22 stsp return err;
3856 8e7bd50a 2019-08-22 stsp
3857 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3858 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3859 8e7bd50a 2019-08-22 stsp if (err)
3860 8e7bd50a 2019-08-22 stsp goto done;
3861 8e7bd50a 2019-08-22 stsp
3862 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3863 8e7bd50a 2019-08-22 stsp if (err)
3864 8e7bd50a 2019-08-22 stsp goto done;
3865 8e7bd50a 2019-08-22 stsp
3866 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3867 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3868 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3869 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3870 8e7bd50a 2019-08-22 stsp goto done;
3871 8e7bd50a 2019-08-22 stsp }
3872 8e7bd50a 2019-08-22 stsp tag_name += 10;
3873 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3874 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3875 8e7bd50a 2019-08-22 stsp goto done;
3876 8e7bd50a 2019-08-22 stsp }
3877 8e7bd50a 2019-08-22 stsp
3878 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3879 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3880 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3881 8e7bd50a 2019-08-22 stsp goto done;
3882 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3883 8e7bd50a 2019-08-22 stsp goto done;
3884 8e7bd50a 2019-08-22 stsp
3885 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3886 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3887 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3888 f372d5cd 2019-10-21 stsp if (err) {
3889 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3890 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3891 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3892 8e7bd50a 2019-08-22 stsp goto done;
3893 f372d5cd 2019-10-21 stsp }
3894 8e7bd50a 2019-08-22 stsp }
3895 8e7bd50a 2019-08-22 stsp
3896 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3897 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3898 f372d5cd 2019-10-21 stsp if (err) {
3899 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3900 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3901 8e7bd50a 2019-08-22 stsp goto done;
3902 f372d5cd 2019-10-21 stsp }
3903 8e7bd50a 2019-08-22 stsp
3904 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3905 f372d5cd 2019-10-21 stsp if (err) {
3906 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3907 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3908 8e7bd50a 2019-08-22 stsp goto done;
3909 f372d5cd 2019-10-21 stsp }
3910 8e7bd50a 2019-08-22 stsp
3911 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3912 f372d5cd 2019-10-21 stsp if (err) {
3913 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3914 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3915 f372d5cd 2019-10-21 stsp goto done;
3916 f372d5cd 2019-10-21 stsp }
3917 8e7bd50a 2019-08-22 stsp
3918 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
3919 f372d5cd 2019-10-21 stsp if (err) {
3920 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3921 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3922 f372d5cd 2019-10-21 stsp goto done;
3923 8e7bd50a 2019-08-22 stsp }
3924 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
3925 8e7bd50a 2019-08-22 stsp done:
3926 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
3927 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
3928 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
3929 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
3930 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
3931 f372d5cd 2019-10-21 stsp free(tag_id_str);
3932 8e7bd50a 2019-08-22 stsp if (ref)
3933 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
3934 8e7bd50a 2019-08-22 stsp free(commit_id);
3935 8e7bd50a 2019-08-22 stsp free(commit_id_str);
3936 8e7bd50a 2019-08-22 stsp free(refname);
3937 8e7bd50a 2019-08-22 stsp free(tagmsg);
3938 f372d5cd 2019-10-21 stsp free(tagmsg_path);
3939 aba9c984 2019-09-08 stsp free(tagger);
3940 8e7bd50a 2019-08-22 stsp return err;
3941 8e7bd50a 2019-08-22 stsp }
3942 8e7bd50a 2019-08-22 stsp
3943 8e7bd50a 2019-08-22 stsp static const struct got_error *
3944 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
3945 8e7bd50a 2019-08-22 stsp {
3946 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
3947 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
3948 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
3949 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
3950 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
3951 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
3952 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
3953 8e7bd50a 2019-08-22 stsp
3954 8e7bd50a 2019-08-22 stsp while ((ch = getopt(argc, argv, "m:r:l")) != -1) {
3955 8e7bd50a 2019-08-22 stsp switch (ch) {
3956 8e7bd50a 2019-08-22 stsp case 'm':
3957 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
3958 8e7bd50a 2019-08-22 stsp break;
3959 8e7bd50a 2019-08-22 stsp case 'r':
3960 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
3961 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
3962 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3963 9ba1d308 2019-10-21 stsp optarg);
3964 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
3965 8e7bd50a 2019-08-22 stsp break;
3966 8e7bd50a 2019-08-22 stsp case 'l':
3967 8e7bd50a 2019-08-22 stsp do_list = 1;
3968 8e7bd50a 2019-08-22 stsp break;
3969 8e7bd50a 2019-08-22 stsp default:
3970 8e7bd50a 2019-08-22 stsp usage_tag();
3971 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
3972 8e7bd50a 2019-08-22 stsp }
3973 8e7bd50a 2019-08-22 stsp }
3974 8e7bd50a 2019-08-22 stsp
3975 8e7bd50a 2019-08-22 stsp argc -= optind;
3976 8e7bd50a 2019-08-22 stsp argv += optind;
3977 8e7bd50a 2019-08-22 stsp
3978 8e7bd50a 2019-08-22 stsp if (do_list) {
3979 8e7bd50a 2019-08-22 stsp if (tagmsg)
3980 8e7bd50a 2019-08-22 stsp errx(1, "-l and -m options are mutually exclusive\n");
3981 8e7bd50a 2019-08-22 stsp if (argc > 0)
3982 8e7bd50a 2019-08-22 stsp usage_tag();
3983 8e7bd50a 2019-08-22 stsp } else if (argc < 1 || argc > 2)
3984 8e7bd50a 2019-08-22 stsp usage_tag();
3985 a2887370 2019-08-23 stsp else if (argc > 1)
3986 a2887370 2019-08-23 stsp commit_id_arg = argv[1];
3987 a2887370 2019-08-23 stsp tag_name = argv[0];
3988 8e7bd50a 2019-08-22 stsp
3989 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
3990 8e7bd50a 2019-08-22 stsp if (do_list) {
3991 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3992 8e7bd50a 2019-08-22 stsp NULL) == -1)
3993 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3994 8e7bd50a 2019-08-22 stsp } else {
3995 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3996 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
3997 8e7bd50a 2019-08-22 stsp err(1, "pledge");
3998 8e7bd50a 2019-08-22 stsp }
3999 8e7bd50a 2019-08-22 stsp #endif
4000 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4001 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4002 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4003 8e7bd50a 2019-08-22 stsp goto done;
4004 8e7bd50a 2019-08-22 stsp }
4005 8e7bd50a 2019-08-22 stsp
4006 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4007 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4008 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4009 8e7bd50a 2019-08-22 stsp goto done;
4010 8e7bd50a 2019-08-22 stsp else
4011 8e7bd50a 2019-08-22 stsp error = NULL;
4012 8e7bd50a 2019-08-22 stsp if (worktree) {
4013 8e7bd50a 2019-08-22 stsp repo_path =
4014 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4015 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4016 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4017 8e7bd50a 2019-08-22 stsp if (error)
4018 8e7bd50a 2019-08-22 stsp goto done;
4019 8e7bd50a 2019-08-22 stsp } else {
4020 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4021 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4022 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4023 8e7bd50a 2019-08-22 stsp goto done;
4024 8e7bd50a 2019-08-22 stsp }
4025 8e7bd50a 2019-08-22 stsp }
4026 8e7bd50a 2019-08-22 stsp }
4027 8e7bd50a 2019-08-22 stsp
4028 8e7bd50a 2019-08-22 stsp if (do_list) {
4029 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4030 c9956ddf 2019-09-08 stsp if (error != NULL)
4031 c9956ddf 2019-09-08 stsp goto done;
4032 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4033 8e7bd50a 2019-08-22 stsp if (error)
4034 8e7bd50a 2019-08-22 stsp goto done;
4035 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4036 8e7bd50a 2019-08-22 stsp } else {
4037 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4038 c9956ddf 2019-09-08 stsp if (error)
4039 c9956ddf 2019-09-08 stsp goto done;
4040 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4041 c9956ddf 2019-09-08 stsp if (error != NULL)
4042 c9956ddf 2019-09-08 stsp goto done;
4043 c9956ddf 2019-09-08 stsp
4044 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4045 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4046 8e7bd50a 2019-08-22 stsp if (error)
4047 8e7bd50a 2019-08-22 stsp goto done;
4048 8e7bd50a 2019-08-22 stsp }
4049 8e7bd50a 2019-08-22 stsp
4050 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4051 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4052 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4053 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4054 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4055 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4056 8e7bd50a 2019-08-22 stsp if (error)
4057 8e7bd50a 2019-08-22 stsp goto done;
4058 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4059 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4060 8e7bd50a 2019-08-22 stsp if (error)
4061 8e7bd50a 2019-08-22 stsp goto done;
4062 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4063 8e7bd50a 2019-08-22 stsp free(commit_id);
4064 8e7bd50a 2019-08-22 stsp if (error)
4065 8e7bd50a 2019-08-22 stsp goto done;
4066 8e7bd50a 2019-08-22 stsp }
4067 8e7bd50a 2019-08-22 stsp
4068 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4069 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4070 8e7bd50a 2019-08-22 stsp }
4071 8e7bd50a 2019-08-22 stsp done:
4072 8e7bd50a 2019-08-22 stsp if (repo)
4073 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4074 8e7bd50a 2019-08-22 stsp if (worktree)
4075 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4076 8e7bd50a 2019-08-22 stsp free(cwd);
4077 8e7bd50a 2019-08-22 stsp free(repo_path);
4078 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4079 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4080 8e7bd50a 2019-08-22 stsp return error;
4081 8e7bd50a 2019-08-22 stsp }
4082 8e7bd50a 2019-08-22 stsp
4083 8e7bd50a 2019-08-22 stsp __dead static void
4084 d00136be 2019-03-26 stsp usage_add(void)
4085 d00136be 2019-03-26 stsp {
4086 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4087 022fae89 2019-12-06 tracey getprogname());
4088 d00136be 2019-03-26 stsp exit(1);
4089 d00136be 2019-03-26 stsp }
4090 d00136be 2019-03-26 stsp
4091 d00136be 2019-03-26 stsp static const struct got_error *
4092 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4093 4e68cba3 2019-11-23 stsp {
4094 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4095 4e68cba3 2019-11-23 stsp path++;
4096 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4097 4e68cba3 2019-11-23 stsp return NULL;
4098 4e68cba3 2019-11-23 stsp }
4099 4e68cba3 2019-11-23 stsp
4100 4e68cba3 2019-11-23 stsp static const struct got_error *
4101 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4102 d00136be 2019-03-26 stsp {
4103 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4104 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4105 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4106 1dd54920 2019-05-11 stsp char *cwd = NULL;
4107 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4108 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4109 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4110 1dd54920 2019-05-11 stsp
4111 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4112 d00136be 2019-03-26 stsp
4113 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4114 d00136be 2019-03-26 stsp switch (ch) {
4115 022fae89 2019-12-06 tracey case 'I':
4116 022fae89 2019-12-06 tracey no_ignores = 1;
4117 022fae89 2019-12-06 tracey break;
4118 4e68cba3 2019-11-23 stsp case 'R':
4119 4e68cba3 2019-11-23 stsp can_recurse = 1;
4120 4e68cba3 2019-11-23 stsp break;
4121 d00136be 2019-03-26 stsp default:
4122 d00136be 2019-03-26 stsp usage_add();
4123 d00136be 2019-03-26 stsp /* NOTREACHED */
4124 d00136be 2019-03-26 stsp }
4125 d00136be 2019-03-26 stsp }
4126 d00136be 2019-03-26 stsp
4127 d00136be 2019-03-26 stsp argc -= optind;
4128 d00136be 2019-03-26 stsp argv += optind;
4129 d00136be 2019-03-26 stsp
4130 43012d58 2019-07-14 stsp #ifndef PROFILE
4131 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4132 43012d58 2019-07-14 stsp NULL) == -1)
4133 43012d58 2019-07-14 stsp err(1, "pledge");
4134 43012d58 2019-07-14 stsp #endif
4135 723c305c 2019-05-11 jcs if (argc < 1)
4136 d00136be 2019-03-26 stsp usage_add();
4137 d00136be 2019-03-26 stsp
4138 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4139 d00136be 2019-03-26 stsp if (cwd == NULL) {
4140 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4141 d00136be 2019-03-26 stsp goto done;
4142 d00136be 2019-03-26 stsp }
4143 723c305c 2019-05-11 jcs
4144 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4145 d00136be 2019-03-26 stsp if (error)
4146 d00136be 2019-03-26 stsp goto done;
4147 d00136be 2019-03-26 stsp
4148 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4149 c9956ddf 2019-09-08 stsp NULL);
4150 031a5338 2019-03-26 stsp if (error != NULL)
4151 031a5338 2019-03-26 stsp goto done;
4152 031a5338 2019-03-26 stsp
4153 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4154 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4155 d00136be 2019-03-26 stsp if (error)
4156 d00136be 2019-03-26 stsp goto done;
4157 d00136be 2019-03-26 stsp
4158 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4159 6d022e97 2019-08-04 stsp if (error)
4160 022fae89 2019-12-06 tracey goto done;
4161 022fae89 2019-12-06 tracey
4162 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4163 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4164 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4165 6d022e97 2019-08-04 stsp goto done;
4166 723c305c 2019-05-11 jcs
4167 022fae89 2019-12-06 tracey }
4168 022fae89 2019-12-06 tracey
4169 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4170 4e68cba3 2019-11-23 stsp char *ondisk_path;
4171 4e68cba3 2019-11-23 stsp struct stat sb;
4172 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4173 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4174 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4175 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4176 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4177 4e68cba3 2019-11-23 stsp goto done;
4178 4e68cba3 2019-11-23 stsp }
4179 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4180 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4181 4e68cba3 2019-11-23 stsp free(ondisk_path);
4182 4e68cba3 2019-11-23 stsp continue;
4183 4e68cba3 2019-11-23 stsp }
4184 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4185 4e68cba3 2019-11-23 stsp ondisk_path);
4186 4e68cba3 2019-11-23 stsp free(ondisk_path);
4187 4e68cba3 2019-11-23 stsp goto done;
4188 4e68cba3 2019-11-23 stsp }
4189 4e68cba3 2019-11-23 stsp free(ondisk_path);
4190 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4191 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4192 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4193 4e68cba3 2019-11-23 stsp goto done;
4194 4e68cba3 2019-11-23 stsp }
4195 4e68cba3 2019-11-23 stsp }
4196 4e68cba3 2019-11-23 stsp }
4197 022fae89 2019-12-06 tracey
4198 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4199 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4200 d00136be 2019-03-26 stsp done:
4201 031a5338 2019-03-26 stsp if (repo)
4202 031a5338 2019-03-26 stsp got_repo_close(repo);
4203 d00136be 2019-03-26 stsp if (worktree)
4204 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4205 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4206 1dd54920 2019-05-11 stsp free((char *)pe->path);
4207 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4208 2ec1f75b 2019-03-26 stsp free(cwd);
4209 2ec1f75b 2019-03-26 stsp return error;
4210 2ec1f75b 2019-03-26 stsp }
4211 2ec1f75b 2019-03-26 stsp
4212 2ec1f75b 2019-03-26 stsp __dead static void
4213 648e4ef7 2019-07-09 stsp usage_remove(void)
4214 2ec1f75b 2019-03-26 stsp {
4215 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4216 f2a9dc41 2019-12-13 tracey getprogname());
4217 2ec1f75b 2019-03-26 stsp exit(1);
4218 2a06fe5f 2019-08-24 stsp }
4219 2a06fe5f 2019-08-24 stsp
4220 2a06fe5f 2019-08-24 stsp static const struct got_error *
4221 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4222 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4223 2a06fe5f 2019-08-24 stsp {
4224 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4225 f2a9dc41 2019-12-13 tracey path++;
4226 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4227 2a06fe5f 2019-08-24 stsp return NULL;
4228 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4229 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4230 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4231 2a06fe5f 2019-08-24 stsp return NULL;
4232 2ec1f75b 2019-03-26 stsp }
4233 2ec1f75b 2019-03-26 stsp
4234 2ec1f75b 2019-03-26 stsp static const struct got_error *
4235 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4236 2ec1f75b 2019-03-26 stsp {
4237 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4238 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4239 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4240 17ed4618 2019-06-02 stsp char *cwd = NULL;
4241 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4242 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4243 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4244 2ec1f75b 2019-03-26 stsp
4245 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4246 17ed4618 2019-06-02 stsp
4247 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4248 2ec1f75b 2019-03-26 stsp switch (ch) {
4249 2ec1f75b 2019-03-26 stsp case 'f':
4250 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4251 2ec1f75b 2019-03-26 stsp break;
4252 70e3e7f5 2019-12-13 tracey case 'k':
4253 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4254 70e3e7f5 2019-12-13 tracey break;
4255 f2a9dc41 2019-12-13 tracey case 'R':
4256 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4257 f2a9dc41 2019-12-13 tracey break;
4258 2ec1f75b 2019-03-26 stsp default:
4259 f2a9dc41 2019-12-13 tracey usage_remove();
4260 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4261 2ec1f75b 2019-03-26 stsp }
4262 2ec1f75b 2019-03-26 stsp }
4263 2ec1f75b 2019-03-26 stsp
4264 2ec1f75b 2019-03-26 stsp argc -= optind;
4265 2ec1f75b 2019-03-26 stsp argv += optind;
4266 2ec1f75b 2019-03-26 stsp
4267 43012d58 2019-07-14 stsp #ifndef PROFILE
4268 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4269 43012d58 2019-07-14 stsp NULL) == -1)
4270 43012d58 2019-07-14 stsp err(1, "pledge");
4271 43012d58 2019-07-14 stsp #endif
4272 17ed4618 2019-06-02 stsp if (argc < 1)
4273 648e4ef7 2019-07-09 stsp usage_remove();
4274 2ec1f75b 2019-03-26 stsp
4275 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4276 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4277 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4278 2ec1f75b 2019-03-26 stsp goto done;
4279 2ec1f75b 2019-03-26 stsp }
4280 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4281 2ec1f75b 2019-03-26 stsp if (error)
4282 2ec1f75b 2019-03-26 stsp goto done;
4283 2ec1f75b 2019-03-26 stsp
4284 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4285 c9956ddf 2019-09-08 stsp NULL);
4286 2af4a041 2019-05-11 jcs if (error)
4287 2ec1f75b 2019-03-26 stsp goto done;
4288 2ec1f75b 2019-03-26 stsp
4289 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4290 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4291 2ec1f75b 2019-03-26 stsp if (error)
4292 2ec1f75b 2019-03-26 stsp goto done;
4293 2ec1f75b 2019-03-26 stsp
4294 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4295 6d022e97 2019-08-04 stsp if (error)
4296 6d022e97 2019-08-04 stsp goto done;
4297 17ed4618 2019-06-02 stsp
4298 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4299 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4300 f2a9dc41 2019-12-13 tracey struct stat sb;
4301 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4302 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4303 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4304 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4305 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4306 f2a9dc41 2019-12-13 tracey goto done;
4307 f2a9dc41 2019-12-13 tracey }
4308 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4309 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4310 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4311 f2a9dc41 2019-12-13 tracey continue;
4312 f2a9dc41 2019-12-13 tracey }
4313 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4314 f2a9dc41 2019-12-13 tracey ondisk_path);
4315 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4316 f2a9dc41 2019-12-13 tracey goto done;
4317 f2a9dc41 2019-12-13 tracey }
4318 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4319 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4320 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4321 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4322 f2a9dc41 2019-12-13 tracey goto done;
4323 f2a9dc41 2019-12-13 tracey }
4324 f2a9dc41 2019-12-13 tracey }
4325 f2a9dc41 2019-12-13 tracey }
4326 f2a9dc41 2019-12-13 tracey
4327 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4328 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4329 a129376b 2019-03-28 stsp done:
4330 a129376b 2019-03-28 stsp if (repo)
4331 a129376b 2019-03-28 stsp got_repo_close(repo);
4332 a129376b 2019-03-28 stsp if (worktree)
4333 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4334 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4335 17ed4618 2019-06-02 stsp free((char *)pe->path);
4336 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4337 a129376b 2019-03-28 stsp free(cwd);
4338 a129376b 2019-03-28 stsp return error;
4339 a129376b 2019-03-28 stsp }
4340 a129376b 2019-03-28 stsp
4341 a129376b 2019-03-28 stsp __dead static void
4342 a129376b 2019-03-28 stsp usage_revert(void)
4343 a129376b 2019-03-28 stsp {
4344 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4345 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4346 a129376b 2019-03-28 stsp exit(1);
4347 a129376b 2019-03-28 stsp }
4348 a129376b 2019-03-28 stsp
4349 1ee397ad 2019-07-12 stsp static const struct got_error *
4350 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4351 a129376b 2019-03-28 stsp {
4352 a129376b 2019-03-28 stsp while (path[0] == '/')
4353 a129376b 2019-03-28 stsp path++;
4354 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4355 33aa809d 2019-08-08 stsp return NULL;
4356 33aa809d 2019-08-08 stsp }
4357 33aa809d 2019-08-08 stsp
4358 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4359 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4360 33aa809d 2019-08-08 stsp const char *action;
4361 33aa809d 2019-08-08 stsp };
4362 33aa809d 2019-08-08 stsp
4363 33aa809d 2019-08-08 stsp static const struct got_error *
4364 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4365 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4366 33aa809d 2019-08-08 stsp {
4367 33aa809d 2019-08-08 stsp char *line = NULL;
4368 33aa809d 2019-08-08 stsp size_t linesize = 0;
4369 33aa809d 2019-08-08 stsp ssize_t linelen;
4370 33aa809d 2019-08-08 stsp
4371 33aa809d 2019-08-08 stsp switch (status) {
4372 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4373 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4374 33aa809d 2019-08-08 stsp break;
4375 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4376 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4377 33aa809d 2019-08-08 stsp break;
4378 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4379 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4380 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4381 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4382 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4383 33aa809d 2019-08-08 stsp printf("%s", line);
4384 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4385 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4386 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4387 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4388 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4389 33aa809d 2019-08-08 stsp break;
4390 33aa809d 2019-08-08 stsp default:
4391 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4392 33aa809d 2019-08-08 stsp }
4393 33aa809d 2019-08-08 stsp
4394 33aa809d 2019-08-08 stsp return NULL;
4395 33aa809d 2019-08-08 stsp }
4396 33aa809d 2019-08-08 stsp
4397 33aa809d 2019-08-08 stsp static const struct got_error *
4398 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4399 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4400 33aa809d 2019-08-08 stsp {
4401 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4402 33aa809d 2019-08-08 stsp char *line = NULL;
4403 33aa809d 2019-08-08 stsp size_t linesize = 0;
4404 33aa809d 2019-08-08 stsp ssize_t linelen;
4405 33aa809d 2019-08-08 stsp int resp = ' ';
4406 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4407 33aa809d 2019-08-08 stsp
4408 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4409 33aa809d 2019-08-08 stsp
4410 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4411 33aa809d 2019-08-08 stsp char *nl;
4412 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4413 33aa809d 2019-08-08 stsp a->action);
4414 33aa809d 2019-08-08 stsp if (err)
4415 33aa809d 2019-08-08 stsp return err;
4416 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4417 33aa809d 2019-08-08 stsp if (linelen == -1) {
4418 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4419 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4420 33aa809d 2019-08-08 stsp return NULL;
4421 33aa809d 2019-08-08 stsp }
4422 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4423 33aa809d 2019-08-08 stsp if (nl)
4424 33aa809d 2019-08-08 stsp *nl = '\0';
4425 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4426 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4427 33aa809d 2019-08-08 stsp printf("y\n");
4428 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4429 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4430 33aa809d 2019-08-08 stsp printf("n\n");
4431 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4432 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4433 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4434 33aa809d 2019-08-08 stsp printf("q\n");
4435 33aa809d 2019-08-08 stsp } else
4436 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4437 33aa809d 2019-08-08 stsp free(line);
4438 33aa809d 2019-08-08 stsp return NULL;
4439 33aa809d 2019-08-08 stsp }
4440 33aa809d 2019-08-08 stsp
4441 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4442 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4443 33aa809d 2019-08-08 stsp a->action);
4444 33aa809d 2019-08-08 stsp if (err)
4445 33aa809d 2019-08-08 stsp return err;
4446 33aa809d 2019-08-08 stsp resp = getchar();
4447 33aa809d 2019-08-08 stsp if (resp == '\n')
4448 33aa809d 2019-08-08 stsp resp = getchar();
4449 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4450 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4451 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4452 33aa809d 2019-08-08 stsp resp = ' ';
4453 33aa809d 2019-08-08 stsp }
4454 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4455 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4456 33aa809d 2019-08-08 stsp resp = ' ';
4457 33aa809d 2019-08-08 stsp }
4458 33aa809d 2019-08-08 stsp }
4459 33aa809d 2019-08-08 stsp
4460 33aa809d 2019-08-08 stsp if (resp == 'y')
4461 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4462 33aa809d 2019-08-08 stsp else if (resp == 'n')
4463 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4464 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4465 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4466 33aa809d 2019-08-08 stsp
4467 1ee397ad 2019-07-12 stsp return NULL;
4468 a129376b 2019-03-28 stsp }
4469 a129376b 2019-03-28 stsp
4470 33aa809d 2019-08-08 stsp
4471 a129376b 2019-03-28 stsp static const struct got_error *
4472 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4473 a129376b 2019-03-28 stsp {
4474 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4475 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4476 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4477 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4478 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4479 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4480 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4481 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4482 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4483 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4484 a129376b 2019-03-28 stsp
4485 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4486 e20a8b6f 2019-06-04 stsp
4487 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4488 a129376b 2019-03-28 stsp switch (ch) {
4489 33aa809d 2019-08-08 stsp case 'p':
4490 33aa809d 2019-08-08 stsp pflag = 1;
4491 33aa809d 2019-08-08 stsp break;
4492 33aa809d 2019-08-08 stsp case 'F':
4493 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4494 33aa809d 2019-08-08 stsp break;
4495 0f6d7415 2019-08-08 stsp case 'R':
4496 0f6d7415 2019-08-08 stsp can_recurse = 1;
4497 0f6d7415 2019-08-08 stsp break;
4498 a129376b 2019-03-28 stsp default:
4499 a129376b 2019-03-28 stsp usage_revert();
4500 a129376b 2019-03-28 stsp /* NOTREACHED */
4501 a129376b 2019-03-28 stsp }
4502 a129376b 2019-03-28 stsp }
4503 a129376b 2019-03-28 stsp
4504 a129376b 2019-03-28 stsp argc -= optind;
4505 a129376b 2019-03-28 stsp argv += optind;
4506 a129376b 2019-03-28 stsp
4507 43012d58 2019-07-14 stsp #ifndef PROFILE
4508 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4509 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4510 43012d58 2019-07-14 stsp err(1, "pledge");
4511 43012d58 2019-07-14 stsp #endif
4512 e20a8b6f 2019-06-04 stsp if (argc < 1)
4513 a129376b 2019-03-28 stsp usage_revert();
4514 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4515 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4516 a129376b 2019-03-28 stsp
4517 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4518 a129376b 2019-03-28 stsp if (cwd == NULL) {
4519 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4520 a129376b 2019-03-28 stsp goto done;
4521 a129376b 2019-03-28 stsp }
4522 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4523 2ec1f75b 2019-03-26 stsp if (error)
4524 2ec1f75b 2019-03-26 stsp goto done;
4525 a129376b 2019-03-28 stsp
4526 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4527 c9956ddf 2019-09-08 stsp NULL);
4528 a129376b 2019-03-28 stsp if (error != NULL)
4529 a129376b 2019-03-28 stsp goto done;
4530 a129376b 2019-03-28 stsp
4531 33aa809d 2019-08-08 stsp if (patch_script_path) {
4532 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4533 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4534 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4535 33aa809d 2019-08-08 stsp patch_script_path);
4536 33aa809d 2019-08-08 stsp goto done;
4537 33aa809d 2019-08-08 stsp }
4538 33aa809d 2019-08-08 stsp }
4539 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4540 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4541 a129376b 2019-03-28 stsp if (error)
4542 a129376b 2019-03-28 stsp goto done;
4543 a129376b 2019-03-28 stsp
4544 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4545 6d022e97 2019-08-04 stsp if (error)
4546 6d022e97 2019-08-04 stsp goto done;
4547 00db391e 2019-08-03 stsp
4548 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4549 0f6d7415 2019-08-08 stsp char *ondisk_path;
4550 0f6d7415 2019-08-08 stsp struct stat sb;
4551 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4552 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4553 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4554 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4555 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4556 0f6d7415 2019-08-08 stsp goto done;
4557 0f6d7415 2019-08-08 stsp }
4558 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4559 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4560 0f6d7415 2019-08-08 stsp free(ondisk_path);
4561 0f6d7415 2019-08-08 stsp continue;
4562 0f6d7415 2019-08-08 stsp }
4563 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4564 0f6d7415 2019-08-08 stsp ondisk_path);
4565 0f6d7415 2019-08-08 stsp free(ondisk_path);
4566 0f6d7415 2019-08-08 stsp goto done;
4567 0f6d7415 2019-08-08 stsp }
4568 0f6d7415 2019-08-08 stsp free(ondisk_path);
4569 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4570 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4571 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4572 0f6d7415 2019-08-08 stsp goto done;
4573 0f6d7415 2019-08-08 stsp }
4574 0f6d7415 2019-08-08 stsp }
4575 0f6d7415 2019-08-08 stsp }
4576 0f6d7415 2019-08-08 stsp
4577 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4578 33aa809d 2019-08-08 stsp cpa.action = "revert";
4579 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4580 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4581 2ec1f75b 2019-03-26 stsp done:
4582 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4583 33aa809d 2019-08-08 stsp error == NULL)
4584 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4585 2ec1f75b 2019-03-26 stsp if (repo)
4586 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4587 2ec1f75b 2019-03-26 stsp if (worktree)
4588 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4589 2ec1f75b 2019-03-26 stsp free(path);
4590 d00136be 2019-03-26 stsp free(cwd);
4591 6bad629b 2019-02-04 stsp return error;
4592 c4296144 2019-05-09 stsp }
4593 c4296144 2019-05-09 stsp
4594 c4296144 2019-05-09 stsp __dead static void
4595 c4296144 2019-05-09 stsp usage_commit(void)
4596 c4296144 2019-05-09 stsp {
4597 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4598 5c1e53bc 2019-07-28 stsp getprogname());
4599 c4296144 2019-05-09 stsp exit(1);
4600 33ad4cbe 2019-05-12 jcs }
4601 33ad4cbe 2019-05-12 jcs
4602 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4603 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4604 e2ba3d07 2019-05-13 stsp const char *editor;
4605 e0870e44 2019-05-13 stsp const char *worktree_path;
4606 76d98825 2019-06-03 stsp const char *branch_name;
4607 314a6357 2019-05-13 stsp const char *repo_path;
4608 e0870e44 2019-05-13 stsp char *logmsg_path;
4609 e2ba3d07 2019-05-13 stsp
4610 e2ba3d07 2019-05-13 stsp };
4611 e0870e44 2019-05-13 stsp
4612 33ad4cbe 2019-05-12 jcs static const struct got_error *
4613 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4614 33ad4cbe 2019-05-12 jcs void *arg)
4615 33ad4cbe 2019-05-12 jcs {
4616 76d98825 2019-06-03 stsp char *initial_content = NULL;
4617 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4618 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4619 e0870e44 2019-05-13 stsp char *template = NULL;
4620 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4621 3ce1b845 2019-07-15 stsp int fd;
4622 33ad4cbe 2019-05-12 jcs size_t len;
4623 33ad4cbe 2019-05-12 jcs
4624 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4625 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4626 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4627 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4628 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4629 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4630 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4631 33ad4cbe 2019-05-12 jcs return NULL;
4632 33ad4cbe 2019-05-12 jcs }
4633 33ad4cbe 2019-05-12 jcs
4634 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4635 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4636 76d98825 2019-06-03 stsp
4637 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4638 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4639 76d98825 2019-06-03 stsp a->branch_name) == -1)
4640 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4641 e0870e44 2019-05-13 stsp
4642 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4643 793c30b5 2019-05-13 stsp if (err)
4644 e0870e44 2019-05-13 stsp goto done;
4645 33ad4cbe 2019-05-12 jcs
4646 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4647 33ad4cbe 2019-05-12 jcs
4648 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4649 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4650 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4651 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4652 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4653 33ad4cbe 2019-05-12 jcs }
4654 33ad4cbe 2019-05-12 jcs close(fd);
4655 33ad4cbe 2019-05-12 jcs
4656 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4657 33ad4cbe 2019-05-12 jcs done:
4658 76d98825 2019-06-03 stsp free(initial_content);
4659 e0870e44 2019-05-13 stsp free(template);
4660 314a6357 2019-05-13 stsp
4661 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4662 3ce1b845 2019-07-15 stsp if (err == NULL) {
4663 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4664 3ce1b845 2019-07-15 stsp if (err) {
4665 3ce1b845 2019-07-15 stsp free(*logmsg);
4666 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4667 3ce1b845 2019-07-15 stsp }
4668 3ce1b845 2019-07-15 stsp }
4669 33ad4cbe 2019-05-12 jcs return err;
4670 6bad629b 2019-02-04 stsp }
4671 c4296144 2019-05-09 stsp
4672 c4296144 2019-05-09 stsp static const struct got_error *
4673 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4674 c4296144 2019-05-09 stsp {
4675 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4676 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4677 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4678 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4679 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4680 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4681 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4682 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4683 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4684 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4685 5c1e53bc 2019-07-28 stsp
4686 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4687 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4688 c4296144 2019-05-09 stsp
4689 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4690 c4296144 2019-05-09 stsp switch (ch) {
4691 c4296144 2019-05-09 stsp case 'm':
4692 c4296144 2019-05-09 stsp logmsg = optarg;
4693 c4296144 2019-05-09 stsp break;
4694 c4296144 2019-05-09 stsp default:
4695 c4296144 2019-05-09 stsp usage_commit();
4696 c4296144 2019-05-09 stsp /* NOTREACHED */
4697 c4296144 2019-05-09 stsp }
4698 c4296144 2019-05-09 stsp }
4699 c4296144 2019-05-09 stsp
4700 c4296144 2019-05-09 stsp argc -= optind;
4701 c4296144 2019-05-09 stsp argv += optind;
4702 c4296144 2019-05-09 stsp
4703 43012d58 2019-07-14 stsp #ifndef PROFILE
4704 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4705 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4706 43012d58 2019-07-14 stsp err(1, "pledge");
4707 43012d58 2019-07-14 stsp #endif
4708 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4709 c4296144 2019-05-09 stsp if (cwd == NULL) {
4710 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4711 c4296144 2019-05-09 stsp goto done;
4712 c4296144 2019-05-09 stsp }
4713 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4714 7d5807f4 2019-07-11 stsp if (error)
4715 7d5807f4 2019-07-11 stsp goto done;
4716 7d5807f4 2019-07-11 stsp
4717 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4718 c4296144 2019-05-09 stsp if (error)
4719 a698f62e 2019-07-25 stsp goto done;
4720 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4721 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4722 c4296144 2019-05-09 stsp goto done;
4723 a698f62e 2019-07-25 stsp }
4724 5c1e53bc 2019-07-28 stsp
4725 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4726 916f288c 2019-07-30 stsp worktree);
4727 5c1e53bc 2019-07-28 stsp if (error)
4728 5c1e53bc 2019-07-28 stsp goto done;
4729 c4296144 2019-05-09 stsp
4730 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4731 c9956ddf 2019-09-08 stsp if (error)
4732 c9956ddf 2019-09-08 stsp goto done;
4733 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4734 c9956ddf 2019-09-08 stsp gitconfig_path);
4735 c4296144 2019-05-09 stsp if (error != NULL)
4736 c4296144 2019-05-09 stsp goto done;
4737 c4296144 2019-05-09 stsp
4738 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4739 aba9c984 2019-09-08 stsp if (error)
4740 aba9c984 2019-09-08 stsp return error;
4741 aba9c984 2019-09-08 stsp
4742 314a6357 2019-05-13 stsp /*
4743 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4744 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4745 314a6357 2019-05-13 stsp */
4746 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4747 314a6357 2019-05-13 stsp error = get_editor(&editor);
4748 314a6357 2019-05-13 stsp else
4749 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4750 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4751 c4296144 2019-05-09 stsp if (error)
4752 c4296144 2019-05-09 stsp goto done;
4753 c4296144 2019-05-09 stsp
4754 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4755 087fb88c 2019-08-04 stsp if (error)
4756 087fb88c 2019-08-04 stsp goto done;
4757 087fb88c 2019-08-04 stsp
4758 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4759 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4760 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4761 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4762 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4763 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4764 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4765 916f288c 2019-07-30 stsp goto done;
4766 916f288c 2019-07-30 stsp }
4767 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4768 916f288c 2019-07-30 stsp }
4769 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4770 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4771 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4772 e0870e44 2019-05-13 stsp if (error) {
4773 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4774 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4775 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4776 c4296144 2019-05-09 stsp goto done;
4777 e0870e44 2019-05-13 stsp }
4778 c4296144 2019-05-09 stsp
4779 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4780 c4296144 2019-05-09 stsp if (error)
4781 c4296144 2019-05-09 stsp goto done;
4782 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4783 c4296144 2019-05-09 stsp done:
4784 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4785 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4786 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4787 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4788 7266f21f 2019-10-21 stsp error == NULL)
4789 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4790 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4791 c4296144 2019-05-09 stsp if (repo)
4792 c4296144 2019-05-09 stsp got_repo_close(repo);
4793 c4296144 2019-05-09 stsp if (worktree)
4794 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4795 c4296144 2019-05-09 stsp free(cwd);
4796 c4296144 2019-05-09 stsp free(id_str);
4797 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4798 e2ba3d07 2019-05-13 stsp free(editor);
4799 aba9c984 2019-09-08 stsp free(author);
4800 234035bc 2019-06-01 stsp return error;
4801 234035bc 2019-06-01 stsp }
4802 234035bc 2019-06-01 stsp
4803 234035bc 2019-06-01 stsp __dead static void
4804 234035bc 2019-06-01 stsp usage_cherrypick(void)
4805 234035bc 2019-06-01 stsp {
4806 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4807 234035bc 2019-06-01 stsp exit(1);
4808 234035bc 2019-06-01 stsp }
4809 234035bc 2019-06-01 stsp
4810 234035bc 2019-06-01 stsp static const struct got_error *
4811 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4812 234035bc 2019-06-01 stsp {
4813 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4814 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4815 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4816 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4817 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4818 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4819 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4820 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4821 234035bc 2019-06-01 stsp int ch, did_something = 0;
4822 234035bc 2019-06-01 stsp
4823 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4824 234035bc 2019-06-01 stsp switch (ch) {
4825 234035bc 2019-06-01 stsp default:
4826 234035bc 2019-06-01 stsp usage_cherrypick();
4827 234035bc 2019-06-01 stsp /* NOTREACHED */
4828 234035bc 2019-06-01 stsp }
4829 234035bc 2019-06-01 stsp }
4830 234035bc 2019-06-01 stsp
4831 234035bc 2019-06-01 stsp argc -= optind;
4832 234035bc 2019-06-01 stsp argv += optind;
4833 234035bc 2019-06-01 stsp
4834 43012d58 2019-07-14 stsp #ifndef PROFILE
4835 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4836 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4837 43012d58 2019-07-14 stsp err(1, "pledge");
4838 43012d58 2019-07-14 stsp #endif
4839 234035bc 2019-06-01 stsp if (argc != 1)
4840 234035bc 2019-06-01 stsp usage_cherrypick();
4841 234035bc 2019-06-01 stsp
4842 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4843 234035bc 2019-06-01 stsp if (cwd == NULL) {
4844 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4845 234035bc 2019-06-01 stsp goto done;
4846 234035bc 2019-06-01 stsp }
4847 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4848 234035bc 2019-06-01 stsp if (error)
4849 234035bc 2019-06-01 stsp goto done;
4850 234035bc 2019-06-01 stsp
4851 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4852 c9956ddf 2019-09-08 stsp NULL);
4853 234035bc 2019-06-01 stsp if (error != NULL)
4854 234035bc 2019-06-01 stsp goto done;
4855 234035bc 2019-06-01 stsp
4856 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4857 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4858 234035bc 2019-06-01 stsp if (error)
4859 234035bc 2019-06-01 stsp goto done;
4860 234035bc 2019-06-01 stsp
4861 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4862 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4863 234035bc 2019-06-01 stsp if (error != NULL) {
4864 234035bc 2019-06-01 stsp struct got_reference *ref;
4865 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4866 234035bc 2019-06-01 stsp goto done;
4867 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4868 234035bc 2019-06-01 stsp if (error != NULL)
4869 234035bc 2019-06-01 stsp goto done;
4870 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4871 234035bc 2019-06-01 stsp got_ref_close(ref);
4872 234035bc 2019-06-01 stsp if (error != NULL)
4873 234035bc 2019-06-01 stsp goto done;
4874 234035bc 2019-06-01 stsp }
4875 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4876 234035bc 2019-06-01 stsp if (error)
4877 234035bc 2019-06-01 stsp goto done;
4878 234035bc 2019-06-01 stsp
4879 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4880 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4881 234035bc 2019-06-01 stsp if (error != NULL)
4882 234035bc 2019-06-01 stsp goto done;
4883 234035bc 2019-06-01 stsp
4884 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4885 234035bc 2019-06-01 stsp if (error) {
4886 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4887 234035bc 2019-06-01 stsp goto done;
4888 234035bc 2019-06-01 stsp error = NULL;
4889 234035bc 2019-06-01 stsp } else {
4890 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4891 234035bc 2019-06-01 stsp goto done;
4892 234035bc 2019-06-01 stsp }
4893 234035bc 2019-06-01 stsp
4894 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4895 234035bc 2019-06-01 stsp if (error)
4896 234035bc 2019-06-01 stsp goto done;
4897 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4898 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4899 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4900 03415a1a 2019-06-02 stsp NULL);
4901 234035bc 2019-06-01 stsp if (error != NULL)
4902 234035bc 2019-06-01 stsp goto done;
4903 234035bc 2019-06-01 stsp
4904 234035bc 2019-06-01 stsp if (did_something)
4905 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4906 234035bc 2019-06-01 stsp done:
4907 234035bc 2019-06-01 stsp if (commit)
4908 234035bc 2019-06-01 stsp got_object_commit_close(commit);
4909 234035bc 2019-06-01 stsp free(commit_id_str);
4910 234035bc 2019-06-01 stsp if (head_ref)
4911 234035bc 2019-06-01 stsp got_ref_close(head_ref);
4912 234035bc 2019-06-01 stsp if (worktree)
4913 234035bc 2019-06-01 stsp got_worktree_close(worktree);
4914 234035bc 2019-06-01 stsp if (repo)
4915 234035bc 2019-06-01 stsp got_repo_close(repo);
4916 c4296144 2019-05-09 stsp return error;
4917 c4296144 2019-05-09 stsp }
4918 5ef14e63 2019-06-02 stsp
4919 5ef14e63 2019-06-02 stsp __dead static void
4920 5ef14e63 2019-06-02 stsp usage_backout(void)
4921 5ef14e63 2019-06-02 stsp {
4922 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
4923 5ef14e63 2019-06-02 stsp exit(1);
4924 5ef14e63 2019-06-02 stsp }
4925 5ef14e63 2019-06-02 stsp
4926 5ef14e63 2019-06-02 stsp static const struct got_error *
4927 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
4928 5ef14e63 2019-06-02 stsp {
4929 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
4930 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
4931 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
4932 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
4933 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
4934 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
4935 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
4936 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
4937 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
4938 5ef14e63 2019-06-02 stsp
4939 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4940 5ef14e63 2019-06-02 stsp switch (ch) {
4941 5ef14e63 2019-06-02 stsp default:
4942 5ef14e63 2019-06-02 stsp usage_backout();
4943 5ef14e63 2019-06-02 stsp /* NOTREACHED */
4944 5ef14e63 2019-06-02 stsp }
4945 5ef14e63 2019-06-02 stsp }
4946 5ef14e63 2019-06-02 stsp
4947 5ef14e63 2019-06-02 stsp argc -= optind;
4948 5ef14e63 2019-06-02 stsp argv += optind;
4949 5ef14e63 2019-06-02 stsp
4950 43012d58 2019-07-14 stsp #ifndef PROFILE
4951 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4952 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4953 43012d58 2019-07-14 stsp err(1, "pledge");
4954 43012d58 2019-07-14 stsp #endif
4955 5ef14e63 2019-06-02 stsp if (argc != 1)
4956 5ef14e63 2019-06-02 stsp usage_backout();
4957 5ef14e63 2019-06-02 stsp
4958 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
4959 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
4960 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
4961 5ef14e63 2019-06-02 stsp goto done;
4962 5ef14e63 2019-06-02 stsp }
4963 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
4964 5ef14e63 2019-06-02 stsp if (error)
4965 5ef14e63 2019-06-02 stsp goto done;
4966 5ef14e63 2019-06-02 stsp
4967 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4968 c9956ddf 2019-09-08 stsp NULL);
4969 5ef14e63 2019-06-02 stsp if (error != NULL)
4970 5ef14e63 2019-06-02 stsp goto done;
4971 5ef14e63 2019-06-02 stsp
4972 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4973 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4974 5ef14e63 2019-06-02 stsp if (error)
4975 5ef14e63 2019-06-02 stsp goto done;
4976 5ef14e63 2019-06-02 stsp
4977 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4978 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4979 5ef14e63 2019-06-02 stsp if (error != NULL) {
4980 5ef14e63 2019-06-02 stsp struct got_reference *ref;
4981 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4982 5ef14e63 2019-06-02 stsp goto done;
4983 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4984 5ef14e63 2019-06-02 stsp if (error != NULL)
4985 5ef14e63 2019-06-02 stsp goto done;
4986 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
4987 5ef14e63 2019-06-02 stsp got_ref_close(ref);
4988 5ef14e63 2019-06-02 stsp if (error != NULL)
4989 5ef14e63 2019-06-02 stsp goto done;
4990 5ef14e63 2019-06-02 stsp }
4991 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
4992 5ef14e63 2019-06-02 stsp if (error)
4993 5ef14e63 2019-06-02 stsp goto done;
4994 5ef14e63 2019-06-02 stsp
4995 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
4996 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
4997 5ef14e63 2019-06-02 stsp if (error != NULL)
4998 5ef14e63 2019-06-02 stsp goto done;
4999 5ef14e63 2019-06-02 stsp
5000 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5001 5ef14e63 2019-06-02 stsp if (error)
5002 5ef14e63 2019-06-02 stsp goto done;
5003 5ef14e63 2019-06-02 stsp
5004 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5005 5ef14e63 2019-06-02 stsp if (error)
5006 5ef14e63 2019-06-02 stsp goto done;
5007 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5008 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5009 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5010 5ef14e63 2019-06-02 stsp goto done;
5011 5ef14e63 2019-06-02 stsp }
5012 5ef14e63 2019-06-02 stsp
5013 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5014 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5015 5ef14e63 2019-06-02 stsp if (error != NULL)
5016 5ef14e63 2019-06-02 stsp goto done;
5017 5ef14e63 2019-06-02 stsp
5018 5ef14e63 2019-06-02 stsp if (did_something)
5019 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5020 5ef14e63 2019-06-02 stsp done:
5021 5ef14e63 2019-06-02 stsp if (commit)
5022 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5023 5ef14e63 2019-06-02 stsp free(commit_id_str);
5024 5ef14e63 2019-06-02 stsp if (head_ref)
5025 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5026 818c7501 2019-07-11 stsp if (worktree)
5027 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5028 818c7501 2019-07-11 stsp if (repo)
5029 818c7501 2019-07-11 stsp got_repo_close(repo);
5030 818c7501 2019-07-11 stsp return error;
5031 818c7501 2019-07-11 stsp }
5032 818c7501 2019-07-11 stsp
5033 818c7501 2019-07-11 stsp __dead static void
5034 818c7501 2019-07-11 stsp usage_rebase(void)
5035 818c7501 2019-07-11 stsp {
5036 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5037 af54c8f8 2019-07-11 stsp getprogname());
5038 818c7501 2019-07-11 stsp exit(1);
5039 0ebf8283 2019-07-24 stsp }
5040 0ebf8283 2019-07-24 stsp
5041 0ebf8283 2019-07-24 stsp void
5042 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5043 0ebf8283 2019-07-24 stsp {
5044 0ebf8283 2019-07-24 stsp char *nl;
5045 0ebf8283 2019-07-24 stsp size_t len;
5046 0ebf8283 2019-07-24 stsp
5047 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5048 0ebf8283 2019-07-24 stsp if (len > limit)
5049 0ebf8283 2019-07-24 stsp len = limit;
5050 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5051 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5052 0ebf8283 2019-07-24 stsp if (nl)
5053 0ebf8283 2019-07-24 stsp *nl = '\0';
5054 0ebf8283 2019-07-24 stsp }
5055 0ebf8283 2019-07-24 stsp
5056 0ebf8283 2019-07-24 stsp static const struct got_error *
5057 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5058 0ebf8283 2019-07-24 stsp {
5059 5943eee2 2019-08-13 stsp const struct got_error *err;
5060 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5061 5943eee2 2019-08-13 stsp const char *s;
5062 0ebf8283 2019-07-24 stsp
5063 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5064 5943eee2 2019-08-13 stsp if (err)
5065 5943eee2 2019-08-13 stsp return err;
5066 0ebf8283 2019-07-24 stsp
5067 5943eee2 2019-08-13 stsp s = logmsg0;
5068 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5069 5943eee2 2019-08-13 stsp s++;
5070 5943eee2 2019-08-13 stsp
5071 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5072 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5073 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5074 5943eee2 2019-08-13 stsp goto done;
5075 5943eee2 2019-08-13 stsp }
5076 0ebf8283 2019-07-24 stsp
5077 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5078 5943eee2 2019-08-13 stsp done:
5079 5943eee2 2019-08-13 stsp free(logmsg0);
5080 5943eee2 2019-08-13 stsp return err;
5081 818c7501 2019-07-11 stsp }
5082 818c7501 2019-07-11 stsp
5083 818c7501 2019-07-11 stsp static const struct got_error *
5084 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5085 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5086 818c7501 2019-07-11 stsp {
5087 818c7501 2019-07-11 stsp const struct got_error *err;
5088 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5089 818c7501 2019-07-11 stsp
5090 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5091 818c7501 2019-07-11 stsp if (err)
5092 818c7501 2019-07-11 stsp goto done;
5093 818c7501 2019-07-11 stsp
5094 ff0d2220 2019-07-11 stsp if (new_id) {
5095 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5096 ff0d2220 2019-07-11 stsp if (err)
5097 ff0d2220 2019-07-11 stsp goto done;
5098 ff0d2220 2019-07-11 stsp }
5099 818c7501 2019-07-11 stsp
5100 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5101 ff0d2220 2019-07-11 stsp if (new_id_str)
5102 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5103 0ebf8283 2019-07-24 stsp
5104 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5105 0ebf8283 2019-07-24 stsp if (err)
5106 0ebf8283 2019-07-24 stsp goto done;
5107 0ebf8283 2019-07-24 stsp
5108 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5109 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5110 818c7501 2019-07-11 stsp done:
5111 818c7501 2019-07-11 stsp free(old_id_str);
5112 818c7501 2019-07-11 stsp free(new_id_str);
5113 818c7501 2019-07-11 stsp return err;
5114 818c7501 2019-07-11 stsp }
5115 818c7501 2019-07-11 stsp
5116 1ee397ad 2019-07-12 stsp static const struct got_error *
5117 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5118 818c7501 2019-07-11 stsp {
5119 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5120 818c7501 2019-07-11 stsp
5121 818c7501 2019-07-11 stsp while (path[0] == '/')
5122 818c7501 2019-07-11 stsp path++;
5123 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5124 818c7501 2019-07-11 stsp
5125 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5126 1ee397ad 2019-07-12 stsp return NULL;
5127 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5128 818c7501 2019-07-11 stsp *rebase_status = status;
5129 1ee397ad 2019-07-12 stsp return NULL;
5130 818c7501 2019-07-11 stsp }
5131 818c7501 2019-07-11 stsp
5132 818c7501 2019-07-11 stsp static const struct got_error *
5133 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5134 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5135 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5136 818c7501 2019-07-11 stsp {
5137 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5138 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5139 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5140 818c7501 2019-07-11 stsp }
5141 818c7501 2019-07-11 stsp
5142 818c7501 2019-07-11 stsp static const struct got_error *
5143 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5144 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5145 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5146 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5147 ff0d2220 2019-07-11 stsp {
5148 ff0d2220 2019-07-11 stsp const struct got_error *error;
5149 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5150 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5151 ff0d2220 2019-07-11 stsp
5152 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5153 ff0d2220 2019-07-11 stsp if (error)
5154 ff0d2220 2019-07-11 stsp return error;
5155 ff0d2220 2019-07-11 stsp
5156 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5157 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5158 ff0d2220 2019-07-11 stsp if (error) {
5159 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5160 ff0d2220 2019-07-11 stsp goto done;
5161 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5162 ff0d2220 2019-07-11 stsp } else {
5163 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5164 ff0d2220 2019-07-11 stsp free(new_commit_id);
5165 ff0d2220 2019-07-11 stsp }
5166 ff0d2220 2019-07-11 stsp done:
5167 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5168 ff0d2220 2019-07-11 stsp return error;
5169 64c6d990 2019-07-11 stsp }
5170 64c6d990 2019-07-11 stsp
5171 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5172 64c6d990 2019-07-11 stsp const char *path_prefix;
5173 64c6d990 2019-07-11 stsp size_t len;
5174 8ca9bd68 2019-07-25 stsp int errcode;
5175 64c6d990 2019-07-11 stsp };
5176 64c6d990 2019-07-11 stsp
5177 64c6d990 2019-07-11 stsp static const struct got_error *
5178 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5179 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5180 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5181 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5182 64c6d990 2019-07-11 stsp {
5183 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5184 64c6d990 2019-07-11 stsp
5185 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5186 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5187 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5188 64c6d990 2019-07-11 stsp
5189 64c6d990 2019-07-11 stsp return NULL;
5190 64c6d990 2019-07-11 stsp }
5191 64c6d990 2019-07-11 stsp
5192 64c6d990 2019-07-11 stsp static const struct got_error *
5193 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5194 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5195 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5196 64c6d990 2019-07-11 stsp {
5197 64c6d990 2019-07-11 stsp const struct got_error *err;
5198 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5199 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5200 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5201 64c6d990 2019-07-11 stsp
5202 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5203 64c6d990 2019-07-11 stsp return NULL;
5204 64c6d990 2019-07-11 stsp
5205 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5206 64c6d990 2019-07-11 stsp if (err)
5207 64c6d990 2019-07-11 stsp goto done;
5208 64c6d990 2019-07-11 stsp
5209 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5210 64c6d990 2019-07-11 stsp if (err)
5211 64c6d990 2019-07-11 stsp goto done;
5212 64c6d990 2019-07-11 stsp
5213 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5214 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5215 64c6d990 2019-07-11 stsp if (err)
5216 64c6d990 2019-07-11 stsp goto done;
5217 64c6d990 2019-07-11 stsp
5218 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5219 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5220 64c6d990 2019-07-11 stsp if (err)
5221 64c6d990 2019-07-11 stsp goto done;
5222 64c6d990 2019-07-11 stsp
5223 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5224 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5225 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5226 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5227 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5228 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5229 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5230 64c6d990 2019-07-11 stsp done:
5231 64c6d990 2019-07-11 stsp if (tree1)
5232 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5233 64c6d990 2019-07-11 stsp if (tree2)
5234 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5235 64c6d990 2019-07-11 stsp if (commit)
5236 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5237 64c6d990 2019-07-11 stsp if (parent_commit)
5238 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5239 64c6d990 2019-07-11 stsp return err;
5240 ff0d2220 2019-07-11 stsp }
5241 ff0d2220 2019-07-11 stsp
5242 ff0d2220 2019-07-11 stsp static const struct got_error *
5243 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5244 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5245 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5246 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5247 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5248 af61c510 2019-07-19 stsp {
5249 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5250 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5251 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5252 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5253 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5254 af61c510 2019-07-19 stsp
5255 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5256 af61c510 2019-07-19 stsp if (err)
5257 af61c510 2019-07-19 stsp return err;
5258 af61c510 2019-07-19 stsp
5259 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5260 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5261 af61c510 2019-07-19 stsp if (err)
5262 af61c510 2019-07-19 stsp goto done;
5263 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5264 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5265 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5266 af61c510 2019-07-19 stsp if (err) {
5267 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5268 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5269 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5270 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5271 af61c510 2019-07-19 stsp "been reached?!?");
5272 ee780d5c 2020-01-04 stsp }
5273 ee780d5c 2020-01-04 stsp goto done;
5274 af61c510 2019-07-19 stsp } else {
5275 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5276 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5277 af61c510 2019-07-19 stsp if (err)
5278 af61c510 2019-07-19 stsp goto done;
5279 af61c510 2019-07-19 stsp
5280 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5281 af61c510 2019-07-19 stsp if (err)
5282 af61c510 2019-07-19 stsp goto done;
5283 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5284 af61c510 2019-07-19 stsp commit_id = parent_id;
5285 af61c510 2019-07-19 stsp }
5286 af61c510 2019-07-19 stsp }
5287 af61c510 2019-07-19 stsp done:
5288 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5289 af61c510 2019-07-19 stsp return err;
5290 af61c510 2019-07-19 stsp }
5291 af61c510 2019-07-19 stsp
5292 af61c510 2019-07-19 stsp static const struct got_error *
5293 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5294 818c7501 2019-07-11 stsp {
5295 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5296 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5297 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5298 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5299 818c7501 2019-07-11 stsp char *cwd = NULL;
5300 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5301 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5302 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5303 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5304 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5305 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5306 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5307 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5308 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5309 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5310 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5311 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5312 818c7501 2019-07-11 stsp
5313 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5314 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5315 818c7501 2019-07-11 stsp
5316 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5317 818c7501 2019-07-11 stsp switch (ch) {
5318 818c7501 2019-07-11 stsp case 'a':
5319 818c7501 2019-07-11 stsp abort_rebase = 1;
5320 818c7501 2019-07-11 stsp break;
5321 818c7501 2019-07-11 stsp case 'c':
5322 818c7501 2019-07-11 stsp continue_rebase = 1;
5323 818c7501 2019-07-11 stsp break;
5324 818c7501 2019-07-11 stsp default:
5325 818c7501 2019-07-11 stsp usage_rebase();
5326 818c7501 2019-07-11 stsp /* NOTREACHED */
5327 818c7501 2019-07-11 stsp }
5328 818c7501 2019-07-11 stsp }
5329 818c7501 2019-07-11 stsp
5330 818c7501 2019-07-11 stsp argc -= optind;
5331 818c7501 2019-07-11 stsp argv += optind;
5332 818c7501 2019-07-11 stsp
5333 43012d58 2019-07-14 stsp #ifndef PROFILE
5334 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5335 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5336 43012d58 2019-07-14 stsp err(1, "pledge");
5337 43012d58 2019-07-14 stsp #endif
5338 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5339 818c7501 2019-07-11 stsp usage_rebase();
5340 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5341 818c7501 2019-07-11 stsp if (argc != 0)
5342 818c7501 2019-07-11 stsp usage_rebase();
5343 818c7501 2019-07-11 stsp } else if (argc != 1)
5344 818c7501 2019-07-11 stsp usage_rebase();
5345 818c7501 2019-07-11 stsp
5346 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5347 818c7501 2019-07-11 stsp if (cwd == NULL) {
5348 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5349 818c7501 2019-07-11 stsp goto done;
5350 818c7501 2019-07-11 stsp }
5351 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5352 818c7501 2019-07-11 stsp if (error)
5353 818c7501 2019-07-11 stsp goto done;
5354 818c7501 2019-07-11 stsp
5355 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5356 c9956ddf 2019-09-08 stsp NULL);
5357 818c7501 2019-07-11 stsp if (error != NULL)
5358 818c7501 2019-07-11 stsp goto done;
5359 818c7501 2019-07-11 stsp
5360 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5361 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5362 818c7501 2019-07-11 stsp if (error)
5363 818c7501 2019-07-11 stsp goto done;
5364 818c7501 2019-07-11 stsp
5365 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5366 818c7501 2019-07-11 stsp if (error)
5367 818c7501 2019-07-11 stsp goto done;
5368 818c7501 2019-07-11 stsp
5369 f6794adc 2019-07-23 stsp if (abort_rebase) {
5370 818c7501 2019-07-11 stsp int did_something;
5371 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5372 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5373 f6794adc 2019-07-23 stsp goto done;
5374 f6794adc 2019-07-23 stsp }
5375 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5376 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5377 3e3a69f1 2019-07-25 stsp worktree, repo);
5378 818c7501 2019-07-11 stsp if (error)
5379 818c7501 2019-07-11 stsp goto done;
5380 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5381 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5382 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5383 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5384 818c7501 2019-07-11 stsp if (error)
5385 818c7501 2019-07-11 stsp goto done;
5386 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5387 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5388 818c7501 2019-07-11 stsp }
5389 818c7501 2019-07-11 stsp
5390 818c7501 2019-07-11 stsp if (continue_rebase) {
5391 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5392 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5393 f6794adc 2019-07-23 stsp goto done;
5394 f6794adc 2019-07-23 stsp }
5395 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5396 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5397 3e3a69f1 2019-07-25 stsp worktree, repo);
5398 818c7501 2019-07-11 stsp if (error)
5399 818c7501 2019-07-11 stsp goto done;
5400 818c7501 2019-07-11 stsp
5401 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5402 01757395 2019-07-12 stsp resume_commit_id, repo);
5403 818c7501 2019-07-11 stsp if (error)
5404 818c7501 2019-07-11 stsp goto done;
5405 818c7501 2019-07-11 stsp
5406 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5407 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5408 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5409 818c7501 2019-07-11 stsp goto done;
5410 818c7501 2019-07-11 stsp }
5411 818c7501 2019-07-11 stsp } else {
5412 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5413 818c7501 2019-07-11 stsp if (error != NULL)
5414 818c7501 2019-07-11 stsp goto done;
5415 ff0d2220 2019-07-11 stsp }
5416 818c7501 2019-07-11 stsp
5417 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5418 ff0d2220 2019-07-11 stsp if (error)
5419 ff0d2220 2019-07-11 stsp goto done;
5420 ff0d2220 2019-07-11 stsp
5421 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5422 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5423 a51a74b3 2019-07-27 stsp
5424 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5425 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5426 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5427 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5428 818c7501 2019-07-11 stsp if (error)
5429 818c7501 2019-07-11 stsp goto done;
5430 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5431 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5432 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5433 818c7501 2019-07-11 stsp "with work tree's branch");
5434 818c7501 2019-07-11 stsp goto done;
5435 818c7501 2019-07-11 stsp }
5436 818c7501 2019-07-11 stsp
5437 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5438 a51a74b3 2019-07-27 stsp if (error) {
5439 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5440 a51a74b3 2019-07-27 stsp goto done;
5441 a51a74b3 2019-07-27 stsp error = NULL;
5442 a51a74b3 2019-07-27 stsp } else {
5443 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5444 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5445 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5446 a51a74b3 2019-07-27 stsp goto done;
5447 a51a74b3 2019-07-27 stsp }
5448 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5449 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5450 818c7501 2019-07-11 stsp if (error)
5451 818c7501 2019-07-11 stsp goto done;
5452 818c7501 2019-07-11 stsp }
5453 818c7501 2019-07-11 stsp
5454 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5455 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5456 818c7501 2019-07-11 stsp if (error)
5457 818c7501 2019-07-11 stsp goto done;
5458 818c7501 2019-07-11 stsp
5459 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5460 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5461 fc66b545 2019-08-12 stsp if (pid == NULL) {
5462 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5463 fc66b545 2019-08-12 stsp int did_something;
5464 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5465 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5466 fc66b545 2019-08-12 stsp &did_something);
5467 fc66b545 2019-08-12 stsp if (error)
5468 fc66b545 2019-08-12 stsp goto done;
5469 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5470 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5471 fc66b545 2019-08-12 stsp }
5472 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5473 fc66b545 2019-08-12 stsp goto done;
5474 fc66b545 2019-08-12 stsp }
5475 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5476 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5477 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5478 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5479 818c7501 2019-07-11 stsp commit = NULL;
5480 818c7501 2019-07-11 stsp if (error)
5481 818c7501 2019-07-11 stsp goto done;
5482 64c6d990 2019-07-11 stsp
5483 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5484 38b0338b 2019-11-29 stsp if (continue_rebase) {
5485 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5486 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5487 38b0338b 2019-11-29 stsp goto done;
5488 38b0338b 2019-11-29 stsp } else {
5489 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5490 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5491 38b0338b 2019-11-29 stsp char *id_str;
5492 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5493 38b0338b 2019-11-29 stsp new_base_branch);
5494 38b0338b 2019-11-29 stsp if (error)
5495 38b0338b 2019-11-29 stsp goto done;
5496 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5497 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5498 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5499 38b0338b 2019-11-29 stsp free(id_str);
5500 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5501 38b0338b 2019-11-29 stsp new_head_commit_id);
5502 38b0338b 2019-11-29 stsp if (error)
5503 38b0338b 2019-11-29 stsp goto done;
5504 38b0338b 2019-11-29 stsp }
5505 818c7501 2019-07-11 stsp }
5506 818c7501 2019-07-11 stsp
5507 818c7501 2019-07-11 stsp pid = NULL;
5508 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5509 818c7501 2019-07-11 stsp commit_id = qid->id;
5510 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5511 818c7501 2019-07-11 stsp pid = qid;
5512 818c7501 2019-07-11 stsp
5513 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5514 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5515 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5516 818c7501 2019-07-11 stsp if (error)
5517 818c7501 2019-07-11 stsp goto done;
5518 818c7501 2019-07-11 stsp
5519 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5520 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5521 818c7501 2019-07-11 stsp break;
5522 01757395 2019-07-12 stsp }
5523 818c7501 2019-07-11 stsp
5524 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5525 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5526 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5527 818c7501 2019-07-11 stsp if (error)
5528 818c7501 2019-07-11 stsp goto done;
5529 818c7501 2019-07-11 stsp }
5530 818c7501 2019-07-11 stsp
5531 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5532 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5533 818c7501 2019-07-11 stsp if (error)
5534 818c7501 2019-07-11 stsp goto done;
5535 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5536 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5537 818c7501 2019-07-11 stsp } else
5538 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5539 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5540 818c7501 2019-07-11 stsp done:
5541 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5542 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5543 818c7501 2019-07-11 stsp free(resume_commit_id);
5544 818c7501 2019-07-11 stsp free(yca_id);
5545 818c7501 2019-07-11 stsp if (commit)
5546 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5547 818c7501 2019-07-11 stsp if (branch)
5548 818c7501 2019-07-11 stsp got_ref_close(branch);
5549 818c7501 2019-07-11 stsp if (new_base_branch)
5550 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5551 818c7501 2019-07-11 stsp if (tmp_branch)
5552 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5553 5ef14e63 2019-06-02 stsp if (worktree)
5554 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5555 5ef14e63 2019-06-02 stsp if (repo)
5556 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5557 5ef14e63 2019-06-02 stsp return error;
5558 0ebf8283 2019-07-24 stsp }
5559 0ebf8283 2019-07-24 stsp
5560 0ebf8283 2019-07-24 stsp __dead static void
5561 0ebf8283 2019-07-24 stsp usage_histedit(void)
5562 0ebf8283 2019-07-24 stsp {
5563 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script]\n",
5564 0ebf8283 2019-07-24 stsp getprogname());
5565 0ebf8283 2019-07-24 stsp exit(1);
5566 0ebf8283 2019-07-24 stsp }
5567 0ebf8283 2019-07-24 stsp
5568 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5569 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5570 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5571 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5572 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5573 0ebf8283 2019-07-24 stsp
5574 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5575 0ebf8283 2019-07-24 stsp unsigned char code;
5576 0ebf8283 2019-07-24 stsp const char *name;
5577 0ebf8283 2019-07-24 stsp const char *desc;
5578 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5579 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5580 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5581 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with commit below" },
5582 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5583 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5584 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5585 0ebf8283 2019-07-24 stsp };
5586 0ebf8283 2019-07-24 stsp
5587 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5588 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5589 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5590 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5591 0ebf8283 2019-07-24 stsp char *logmsg;
5592 0ebf8283 2019-07-24 stsp };
5593 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5594 0ebf8283 2019-07-24 stsp
5595 0ebf8283 2019-07-24 stsp static const struct got_error *
5596 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5597 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5598 0ebf8283 2019-07-24 stsp {
5599 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5600 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5601 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5602 8138f3e1 2019-08-11 stsp int n;
5603 0ebf8283 2019-07-24 stsp
5604 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5605 0ebf8283 2019-07-24 stsp if (err)
5606 0ebf8283 2019-07-24 stsp goto done;
5607 0ebf8283 2019-07-24 stsp
5608 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5609 0ebf8283 2019-07-24 stsp if (err)
5610 0ebf8283 2019-07-24 stsp goto done;
5611 0ebf8283 2019-07-24 stsp
5612 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5613 0ebf8283 2019-07-24 stsp if (err)
5614 0ebf8283 2019-07-24 stsp goto done;
5615 0ebf8283 2019-07-24 stsp
5616 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5617 0ebf8283 2019-07-24 stsp if (n < 0)
5618 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5619 0ebf8283 2019-07-24 stsp done:
5620 0ebf8283 2019-07-24 stsp if (commit)
5621 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5622 0ebf8283 2019-07-24 stsp free(id_str);
5623 0ebf8283 2019-07-24 stsp free(logmsg);
5624 0ebf8283 2019-07-24 stsp return err;
5625 0ebf8283 2019-07-24 stsp }
5626 0ebf8283 2019-07-24 stsp
5627 0ebf8283 2019-07-24 stsp static const struct got_error *
5628 0ebf8283 2019-07-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits, FILE *f,
5629 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5630 0ebf8283 2019-07-24 stsp {
5631 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5632 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5633 0ebf8283 2019-07-24 stsp
5634 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5635 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5636 0ebf8283 2019-07-24 stsp
5637 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5638 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5639 0ebf8283 2019-07-24 stsp f, repo);
5640 0ebf8283 2019-07-24 stsp if (err)
5641 0ebf8283 2019-07-24 stsp break;
5642 0ebf8283 2019-07-24 stsp }
5643 0ebf8283 2019-07-24 stsp
5644 0ebf8283 2019-07-24 stsp return err;
5645 0ebf8283 2019-07-24 stsp }
5646 0ebf8283 2019-07-24 stsp
5647 0ebf8283 2019-07-24 stsp static const struct got_error *
5648 0ebf8283 2019-07-24 stsp write_cmd_list(FILE *f)
5649 0ebf8283 2019-07-24 stsp {
5650 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5651 0ebf8283 2019-07-24 stsp int n, i;
5652 0ebf8283 2019-07-24 stsp
5653 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5654 0ebf8283 2019-07-24 stsp if (n < 0)
5655 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5656 0ebf8283 2019-07-24 stsp
5657 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5658 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5659 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5660 0ebf8283 2019-07-24 stsp cmd->desc);
5661 0ebf8283 2019-07-24 stsp if (n < 0) {
5662 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5663 0ebf8283 2019-07-24 stsp break;
5664 0ebf8283 2019-07-24 stsp }
5665 0ebf8283 2019-07-24 stsp }
5666 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Commits will be processed in order from top to "
5667 0ebf8283 2019-07-24 stsp "bottom of this file.\n");
5668 0ebf8283 2019-07-24 stsp if (n < 0)
5669 0ebf8283 2019-07-24 stsp return got_ferror(f, GOT_ERR_IO);
5670 0ebf8283 2019-07-24 stsp return err;
5671 0ebf8283 2019-07-24 stsp }
5672 0ebf8283 2019-07-24 stsp
5673 0ebf8283 2019-07-24 stsp static const struct got_error *
5674 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5675 0ebf8283 2019-07-24 stsp {
5676 0ebf8283 2019-07-24 stsp static char msg[42];
5677 0ebf8283 2019-07-24 stsp int ret;
5678 0ebf8283 2019-07-24 stsp
5679 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5680 0ebf8283 2019-07-24 stsp lineno);
5681 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5682 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5683 0ebf8283 2019-07-24 stsp
5684 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5685 0ebf8283 2019-07-24 stsp }
5686 0ebf8283 2019-07-24 stsp
5687 0ebf8283 2019-07-24 stsp static const struct got_error *
5688 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5689 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5690 0ebf8283 2019-07-24 stsp {
5691 0ebf8283 2019-07-24 stsp const struct got_error *err;
5692 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5693 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5694 0ebf8283 2019-07-24 stsp
5695 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5696 0ebf8283 2019-07-24 stsp if (err)
5697 0ebf8283 2019-07-24 stsp return err;
5698 0ebf8283 2019-07-24 stsp
5699 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5700 0ebf8283 2019-07-24 stsp if (err)
5701 0ebf8283 2019-07-24 stsp goto done;
5702 0ebf8283 2019-07-24 stsp
5703 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5704 5943eee2 2019-08-13 stsp if (err)
5705 5943eee2 2019-08-13 stsp goto done;
5706 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5707 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5708 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5709 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5710 0ebf8283 2019-07-24 stsp }
5711 0ebf8283 2019-07-24 stsp done:
5712 0ebf8283 2019-07-24 stsp if (folded_commit)
5713 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5714 0ebf8283 2019-07-24 stsp free(id_str);
5715 5943eee2 2019-08-13 stsp free(folded_logmsg);
5716 0ebf8283 2019-07-24 stsp return err;
5717 0ebf8283 2019-07-24 stsp }
5718 0ebf8283 2019-07-24 stsp
5719 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5720 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5721 0ebf8283 2019-07-24 stsp {
5722 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5723 0ebf8283 2019-07-24 stsp
5724 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5725 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5726 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5727 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5728 3f9de99f 2019-07-24 stsp folded = prev;
5729 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5730 0ebf8283 2019-07-24 stsp }
5731 0ebf8283 2019-07-24 stsp
5732 0ebf8283 2019-07-24 stsp return folded;
5733 0ebf8283 2019-07-24 stsp }
5734 0ebf8283 2019-07-24 stsp
5735 0ebf8283 2019-07-24 stsp static const struct got_error *
5736 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5737 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5738 0ebf8283 2019-07-24 stsp {
5739 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5740 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5741 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5742 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5743 0ebf8283 2019-07-24 stsp int fd;
5744 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5745 0ebf8283 2019-07-24 stsp
5746 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5747 0ebf8283 2019-07-24 stsp if (err)
5748 0ebf8283 2019-07-24 stsp return err;
5749 0ebf8283 2019-07-24 stsp
5750 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5751 0ebf8283 2019-07-24 stsp if (folded) {
5752 0ebf8283 2019-07-24 stsp while (folded != hle) {
5753 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5754 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5755 3f9de99f 2019-07-24 stsp continue;
5756 3f9de99f 2019-07-24 stsp }
5757 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5758 0ebf8283 2019-07-24 stsp logmsg, repo);
5759 0ebf8283 2019-07-24 stsp if (err)
5760 0ebf8283 2019-07-24 stsp goto done;
5761 0ebf8283 2019-07-24 stsp free(logmsg);
5762 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5763 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5764 0ebf8283 2019-07-24 stsp }
5765 0ebf8283 2019-07-24 stsp }
5766 0ebf8283 2019-07-24 stsp
5767 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5768 0ebf8283 2019-07-24 stsp if (err)
5769 0ebf8283 2019-07-24 stsp goto done;
5770 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5771 5943eee2 2019-08-13 stsp if (err)
5772 5943eee2 2019-08-13 stsp goto done;
5773 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5774 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5775 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5776 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5777 0ebf8283 2019-07-24 stsp goto done;
5778 0ebf8283 2019-07-24 stsp }
5779 0ebf8283 2019-07-24 stsp free(logmsg);
5780 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5781 0ebf8283 2019-07-24 stsp
5782 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5783 0ebf8283 2019-07-24 stsp if (err)
5784 0ebf8283 2019-07-24 stsp goto done;
5785 0ebf8283 2019-07-24 stsp
5786 0ebf8283 2019-07-24 stsp err = got_opentemp_named_fd(&logmsg_path, &fd, "/tmp/got-logmsg");
5787 0ebf8283 2019-07-24 stsp if (err)
5788 0ebf8283 2019-07-24 stsp goto done;
5789 0ebf8283 2019-07-24 stsp
5790 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5791 0ebf8283 2019-07-24 stsp close(fd);
5792 0ebf8283 2019-07-24 stsp
5793 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5794 0ebf8283 2019-07-24 stsp if (err)
5795 0ebf8283 2019-07-24 stsp goto done;
5796 0ebf8283 2019-07-24 stsp
5797 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5798 0ebf8283 2019-07-24 stsp if (err) {
5799 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5800 0ebf8283 2019-07-24 stsp goto done;
5801 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5802 0ebf8283 2019-07-24 stsp }
5803 0ebf8283 2019-07-24 stsp done:
5804 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5805 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5806 0ebf8283 2019-07-24 stsp free(logmsg_path);
5807 0ebf8283 2019-07-24 stsp free(logmsg);
5808 5943eee2 2019-08-13 stsp free(orig_logmsg);
5809 0ebf8283 2019-07-24 stsp free(editor);
5810 0ebf8283 2019-07-24 stsp if (commit)
5811 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5812 0ebf8283 2019-07-24 stsp return err;
5813 5ef14e63 2019-06-02 stsp }
5814 0ebf8283 2019-07-24 stsp
5815 0ebf8283 2019-07-24 stsp static const struct got_error *
5816 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5817 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5818 0ebf8283 2019-07-24 stsp {
5819 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5820 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5821 0ebf8283 2019-07-24 stsp size_t size;
5822 0ebf8283 2019-07-24 stsp ssize_t len;
5823 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5824 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5825 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5826 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5827 0ebf8283 2019-07-24 stsp
5828 0ebf8283 2019-07-24 stsp for (;;) {
5829 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5830 0ebf8283 2019-07-24 stsp if (len == -1) {
5831 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5832 0ebf8283 2019-07-24 stsp if (feof(f))
5833 0ebf8283 2019-07-24 stsp break;
5834 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5835 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5836 0ebf8283 2019-07-24 stsp break;
5837 0ebf8283 2019-07-24 stsp }
5838 0ebf8283 2019-07-24 stsp lineno++;
5839 0ebf8283 2019-07-24 stsp p = line;
5840 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5841 0ebf8283 2019-07-24 stsp p++;
5842 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
5843 0ebf8283 2019-07-24 stsp free(line);
5844 0ebf8283 2019-07-24 stsp line = NULL;
5845 0ebf8283 2019-07-24 stsp continue;
5846 0ebf8283 2019-07-24 stsp }
5847 0ebf8283 2019-07-24 stsp cmd = NULL;
5848 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5849 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
5850 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
5851 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
5852 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
5853 0ebf8283 2019-07-24 stsp break;
5854 0ebf8283 2019-07-24 stsp }
5855 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
5856 0ebf8283 2019-07-24 stsp p++;
5857 0ebf8283 2019-07-24 stsp break;
5858 0ebf8283 2019-07-24 stsp }
5859 0ebf8283 2019-07-24 stsp }
5860 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
5861 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5862 0ebf8283 2019-07-24 stsp break;
5863 0ebf8283 2019-07-24 stsp }
5864 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
5865 0ebf8283 2019-07-24 stsp p++;
5866 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
5867 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
5868 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
5869 0ebf8283 2019-07-24 stsp break;
5870 0ebf8283 2019-07-24 stsp }
5871 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
5872 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
5873 0ebf8283 2019-07-24 stsp if (err)
5874 0ebf8283 2019-07-24 stsp break;
5875 0ebf8283 2019-07-24 stsp } else {
5876 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
5877 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
5878 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
5879 0ebf8283 2019-07-24 stsp break;
5880 0ebf8283 2019-07-24 stsp }
5881 0ebf8283 2019-07-24 stsp }
5882 0ebf8283 2019-07-24 stsp free(line);
5883 0ebf8283 2019-07-24 stsp line = NULL;
5884 0ebf8283 2019-07-24 stsp continue;
5885 0ebf8283 2019-07-24 stsp } else {
5886 0ebf8283 2019-07-24 stsp end = p;
5887 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
5888 0ebf8283 2019-07-24 stsp end++;
5889 0ebf8283 2019-07-24 stsp *end = '\0';
5890 0ebf8283 2019-07-24 stsp
5891 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
5892 0ebf8283 2019-07-24 stsp if (err) {
5893 0ebf8283 2019-07-24 stsp /* override error code */
5894 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
5895 0ebf8283 2019-07-24 stsp break;
5896 0ebf8283 2019-07-24 stsp }
5897 0ebf8283 2019-07-24 stsp }
5898 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
5899 0ebf8283 2019-07-24 stsp if (hle == NULL) {
5900 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
5901 0ebf8283 2019-07-24 stsp break;
5902 0ebf8283 2019-07-24 stsp }
5903 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
5904 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
5905 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
5906 0ebf8283 2019-07-24 stsp commit_id = NULL;
5907 0ebf8283 2019-07-24 stsp free(line);
5908 0ebf8283 2019-07-24 stsp line = NULL;
5909 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
5910 0ebf8283 2019-07-24 stsp }
5911 0ebf8283 2019-07-24 stsp
5912 0ebf8283 2019-07-24 stsp free(line);
5913 0ebf8283 2019-07-24 stsp free(commit_id);
5914 0ebf8283 2019-07-24 stsp return err;
5915 0ebf8283 2019-07-24 stsp }
5916 0ebf8283 2019-07-24 stsp
5917 0ebf8283 2019-07-24 stsp static const struct got_error *
5918 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
5919 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5920 bfce7f83 2019-07-27 stsp {
5921 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
5922 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
5923 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
5924 bfce7f83 2019-07-27 stsp static char msg[80];
5925 bfce7f83 2019-07-27 stsp char *id_str;
5926 bfce7f83 2019-07-27 stsp
5927 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
5928 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
5929 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
5930 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
5931 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5932 bfce7f83 2019-07-27 stsp
5933 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5934 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
5935 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
5936 bfce7f83 2019-07-27 stsp break;
5937 bfce7f83 2019-07-27 stsp }
5938 bfce7f83 2019-07-27 stsp if (hle == NULL) {
5939 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
5940 bfce7f83 2019-07-27 stsp if (err)
5941 bfce7f83 2019-07-27 stsp return err;
5942 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
5943 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
5944 bfce7f83 2019-07-27 stsp free(id_str);
5945 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
5946 bfce7f83 2019-07-27 stsp }
5947 bfce7f83 2019-07-27 stsp }
5948 bfce7f83 2019-07-27 stsp
5949 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
5950 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
5951 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
5952 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
5953 bfce7f83 2019-07-27 stsp
5954 bfce7f83 2019-07-27 stsp return NULL;
5955 bfce7f83 2019-07-27 stsp }
5956 bfce7f83 2019-07-27 stsp
5957 bfce7f83 2019-07-27 stsp static const struct got_error *
5958 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
5959 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
5960 bfce7f83 2019-07-27 stsp struct got_repository *repo)
5961 0ebf8283 2019-07-24 stsp {
5962 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5963 0ebf8283 2019-07-24 stsp char *editor;
5964 0ebf8283 2019-07-24 stsp FILE *f = NULL;
5965 0ebf8283 2019-07-24 stsp
5966 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5967 0ebf8283 2019-07-24 stsp if (err)
5968 0ebf8283 2019-07-24 stsp return err;
5969 0ebf8283 2019-07-24 stsp
5970 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
5971 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
5972 0ebf8283 2019-07-24 stsp goto done;
5973 0ebf8283 2019-07-24 stsp }
5974 0ebf8283 2019-07-24 stsp
5975 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
5976 0ebf8283 2019-07-24 stsp if (f == NULL) {
5977 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
5978 0ebf8283 2019-07-24 stsp goto done;
5979 0ebf8283 2019-07-24 stsp }
5980 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
5981 bfce7f83 2019-07-27 stsp if (err)
5982 bfce7f83 2019-07-27 stsp goto done;
5983 bfce7f83 2019-07-27 stsp
5984 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
5985 0ebf8283 2019-07-24 stsp done:
5986 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
5987 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
5988 0ebf8283 2019-07-24 stsp free(editor);
5989 0ebf8283 2019-07-24 stsp return err;
5990 0ebf8283 2019-07-24 stsp }
5991 0ebf8283 2019-07-24 stsp
5992 0ebf8283 2019-07-24 stsp static const struct got_error *
5993 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
5994 0ebf8283 2019-07-24 stsp struct got_object_id_queue *, const char *, struct got_repository *);
5995 0ebf8283 2019-07-24 stsp
5996 0ebf8283 2019-07-24 stsp static const struct got_error *
5997 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
5998 0ebf8283 2019-07-24 stsp struct got_object_id_queue *commits, struct got_repository *repo)
5999 0ebf8283 2019-07-24 stsp {
6000 0ebf8283 2019-07-24 stsp const struct got_error *err;
6001 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6002 0ebf8283 2019-07-24 stsp char *path = NULL;
6003 0ebf8283 2019-07-24 stsp
6004 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6005 0ebf8283 2019-07-24 stsp if (err)
6006 0ebf8283 2019-07-24 stsp return err;
6007 0ebf8283 2019-07-24 stsp
6008 0ebf8283 2019-07-24 stsp err = write_cmd_list(f);
6009 0ebf8283 2019-07-24 stsp if (err)
6010 0ebf8283 2019-07-24 stsp goto done;
6011 0ebf8283 2019-07-24 stsp
6012 0ebf8283 2019-07-24 stsp err = histedit_write_commit_list(commits, f, repo);
6013 0ebf8283 2019-07-24 stsp if (err)
6014 0ebf8283 2019-07-24 stsp goto done;
6015 0ebf8283 2019-07-24 stsp
6016 0ebf8283 2019-07-24 stsp if (fclose(f) != 0) {
6017 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6018 0ebf8283 2019-07-24 stsp goto done;
6019 0ebf8283 2019-07-24 stsp }
6020 0ebf8283 2019-07-24 stsp f = NULL;
6021 0ebf8283 2019-07-24 stsp
6022 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6023 0ebf8283 2019-07-24 stsp if (err) {
6024 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6025 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6026 0ebf8283 2019-07-24 stsp goto done;
6027 bfce7f83 2019-07-27 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6028 0ebf8283 2019-07-24 stsp commits, path, repo);
6029 0ebf8283 2019-07-24 stsp }
6030 0ebf8283 2019-07-24 stsp done:
6031 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6032 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6033 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6034 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6035 0ebf8283 2019-07-24 stsp free(path);
6036 0ebf8283 2019-07-24 stsp return err;
6037 0ebf8283 2019-07-24 stsp }
6038 0ebf8283 2019-07-24 stsp
6039 0ebf8283 2019-07-24 stsp static const struct got_error *
6040 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6041 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6042 0ebf8283 2019-07-24 stsp {
6043 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6044 0ebf8283 2019-07-24 stsp char *path = NULL;
6045 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6046 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6047 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6048 0ebf8283 2019-07-24 stsp
6049 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6050 0ebf8283 2019-07-24 stsp if (err)
6051 0ebf8283 2019-07-24 stsp return err;
6052 0ebf8283 2019-07-24 stsp
6053 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6054 0ebf8283 2019-07-24 stsp if (f == NULL) {
6055 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6056 0ebf8283 2019-07-24 stsp goto done;
6057 0ebf8283 2019-07-24 stsp }
6058 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6059 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6060 0ebf8283 2019-07-24 stsp repo);
6061 0ebf8283 2019-07-24 stsp if (err)
6062 0ebf8283 2019-07-24 stsp break;
6063 0ebf8283 2019-07-24 stsp
6064 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6065 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6066 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6067 0ebf8283 2019-07-24 stsp if (n < 0) {
6068 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6069 0ebf8283 2019-07-24 stsp break;
6070 0ebf8283 2019-07-24 stsp }
6071 0ebf8283 2019-07-24 stsp }
6072 0ebf8283 2019-07-24 stsp }
6073 0ebf8283 2019-07-24 stsp done:
6074 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6075 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6076 0ebf8283 2019-07-24 stsp free(path);
6077 0ebf8283 2019-07-24 stsp if (commit)
6078 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6079 0ebf8283 2019-07-24 stsp return err;
6080 0ebf8283 2019-07-24 stsp }
6081 0ebf8283 2019-07-24 stsp
6082 bfce7f83 2019-07-27 stsp void
6083 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6084 bfce7f83 2019-07-27 stsp {
6085 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6086 bfce7f83 2019-07-27 stsp
6087 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6088 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6089 bfce7f83 2019-07-27 stsp free(hle);
6090 bfce7f83 2019-07-27 stsp }
6091 bfce7f83 2019-07-27 stsp }
6092 bfce7f83 2019-07-27 stsp
6093 0ebf8283 2019-07-24 stsp static const struct got_error *
6094 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6095 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6096 0ebf8283 2019-07-24 stsp {
6097 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6098 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6099 0ebf8283 2019-07-24 stsp
6100 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6101 0ebf8283 2019-07-24 stsp if (f == NULL) {
6102 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6103 0ebf8283 2019-07-24 stsp goto done;
6104 0ebf8283 2019-07-24 stsp }
6105 0ebf8283 2019-07-24 stsp
6106 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6107 0ebf8283 2019-07-24 stsp done:
6108 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6109 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6110 0ebf8283 2019-07-24 stsp return err;
6111 0ebf8283 2019-07-24 stsp }
6112 0ebf8283 2019-07-24 stsp
6113 0ebf8283 2019-07-24 stsp static const struct got_error *
6114 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6115 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6116 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6117 0ebf8283 2019-07-24 stsp {
6118 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6119 0ebf8283 2019-07-24 stsp int resp = ' ';
6120 0ebf8283 2019-07-24 stsp
6121 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6122 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6123 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6124 0ebf8283 2019-07-24 stsp resp = getchar();
6125 a61a4414 2019-08-07 stsp if (resp == '\n')
6126 a61a4414 2019-08-07 stsp resp = getchar();
6127 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6128 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6129 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6130 bfce7f83 2019-07-27 stsp repo);
6131 426ebf2e 2019-07-25 stsp if (err) {
6132 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6133 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6134 426ebf2e 2019-07-25 stsp break;
6135 bfce7f83 2019-07-27 stsp prev_err = err;
6136 426ebf2e 2019-07-25 stsp resp = ' ';
6137 426ebf2e 2019-07-25 stsp continue;
6138 426ebf2e 2019-07-25 stsp }
6139 bfce7f83 2019-07-27 stsp break;
6140 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6141 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6142 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6143 0ebf8283 2019-07-24 stsp commits, repo);
6144 426ebf2e 2019-07-25 stsp if (err) {
6145 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6146 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6147 426ebf2e 2019-07-25 stsp break;
6148 bfce7f83 2019-07-27 stsp prev_err = err;
6149 426ebf2e 2019-07-25 stsp resp = ' ';
6150 426ebf2e 2019-07-25 stsp continue;
6151 426ebf2e 2019-07-25 stsp }
6152 bfce7f83 2019-07-27 stsp break;
6153 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6154 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6155 0ebf8283 2019-07-24 stsp break;
6156 426ebf2e 2019-07-25 stsp } else
6157 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6158 0ebf8283 2019-07-24 stsp }
6159 0ebf8283 2019-07-24 stsp
6160 0ebf8283 2019-07-24 stsp return err;
6161 0ebf8283 2019-07-24 stsp }
6162 0ebf8283 2019-07-24 stsp
6163 0ebf8283 2019-07-24 stsp static const struct got_error *
6164 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6165 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6166 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6167 0ebf8283 2019-07-24 stsp {
6168 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6169 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6170 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6171 3e3a69f1 2019-07-25 stsp branch, repo);
6172 0ebf8283 2019-07-24 stsp }
6173 0ebf8283 2019-07-24 stsp
6174 0ebf8283 2019-07-24 stsp static const struct got_error *
6175 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6176 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6177 0ebf8283 2019-07-24 stsp {
6178 0ebf8283 2019-07-24 stsp const struct got_error *err;
6179 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6180 0ebf8283 2019-07-24 stsp
6181 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6182 0ebf8283 2019-07-24 stsp if (err)
6183 0ebf8283 2019-07-24 stsp goto done;
6184 0ebf8283 2019-07-24 stsp
6185 0ebf8283 2019-07-24 stsp if (new_id) {
6186 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6187 0ebf8283 2019-07-24 stsp if (err)
6188 0ebf8283 2019-07-24 stsp goto done;
6189 0ebf8283 2019-07-24 stsp }
6190 0ebf8283 2019-07-24 stsp
6191 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6192 0ebf8283 2019-07-24 stsp if (new_id_str)
6193 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6194 0ebf8283 2019-07-24 stsp
6195 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6196 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6197 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6198 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6199 0ebf8283 2019-07-24 stsp goto done;
6200 0ebf8283 2019-07-24 stsp }
6201 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6202 0ebf8283 2019-07-24 stsp } else {
6203 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6204 0ebf8283 2019-07-24 stsp if (err)
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 switch (hle->cmd->code) {
6209 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6210 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6211 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6212 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6213 0ebf8283 2019-07-24 stsp break;
6214 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6215 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6216 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6217 0ebf8283 2019-07-24 stsp logmsg);
6218 0ebf8283 2019-07-24 stsp break;
6219 0ebf8283 2019-07-24 stsp default:
6220 0ebf8283 2019-07-24 stsp break;
6221 0ebf8283 2019-07-24 stsp }
6222 0ebf8283 2019-07-24 stsp done:
6223 0ebf8283 2019-07-24 stsp free(old_id_str);
6224 0ebf8283 2019-07-24 stsp free(new_id_str);
6225 0ebf8283 2019-07-24 stsp return err;
6226 0ebf8283 2019-07-24 stsp }
6227 0ebf8283 2019-07-24 stsp
6228 0ebf8283 2019-07-24 stsp static const struct got_error *
6229 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6230 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6231 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6232 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6233 0ebf8283 2019-07-24 stsp {
6234 0ebf8283 2019-07-24 stsp const struct got_error *err;
6235 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6236 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6237 0ebf8283 2019-07-24 stsp
6238 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6239 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6240 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6241 0ebf8283 2019-07-24 stsp if (err)
6242 0ebf8283 2019-07-24 stsp return err;
6243 0ebf8283 2019-07-24 stsp }
6244 0ebf8283 2019-07-24 stsp
6245 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6246 0ebf8283 2019-07-24 stsp if (err)
6247 0ebf8283 2019-07-24 stsp return err;
6248 0ebf8283 2019-07-24 stsp
6249 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6250 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6251 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6252 0ebf8283 2019-07-24 stsp if (err) {
6253 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6254 0ebf8283 2019-07-24 stsp goto done;
6255 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6256 0ebf8283 2019-07-24 stsp } else {
6257 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6258 0ebf8283 2019-07-24 stsp free(new_commit_id);
6259 0ebf8283 2019-07-24 stsp }
6260 0ebf8283 2019-07-24 stsp done:
6261 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
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_skip_commit(struct got_histedit_list_entry *hle,
6267 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6268 0ebf8283 2019-07-24 stsp {
6269 0ebf8283 2019-07-24 stsp const struct got_error *error;
6270 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6271 0ebf8283 2019-07-24 stsp
6272 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6273 0ebf8283 2019-07-24 stsp repo);
6274 0ebf8283 2019-07-24 stsp if (error)
6275 0ebf8283 2019-07-24 stsp return error;
6276 0ebf8283 2019-07-24 stsp
6277 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6278 0ebf8283 2019-07-24 stsp if (error)
6279 0ebf8283 2019-07-24 stsp return error;
6280 0ebf8283 2019-07-24 stsp
6281 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6282 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6283 0ebf8283 2019-07-24 stsp return error;
6284 0ebf8283 2019-07-24 stsp }
6285 0ebf8283 2019-07-24 stsp
6286 0ebf8283 2019-07-24 stsp static const struct got_error *
6287 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6288 0ebf8283 2019-07-24 stsp {
6289 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6290 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6291 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6292 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6293 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6294 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6295 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6296 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6297 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6298 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6299 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6300 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6301 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6302 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6303 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6304 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6305 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6306 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6307 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6308 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6309 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6310 0ebf8283 2019-07-24 stsp
6311 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6312 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6313 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6314 0ebf8283 2019-07-24 stsp
6315 0ebf8283 2019-07-24 stsp while ((ch = getopt(argc, argv, "acF:")) != -1) {
6316 0ebf8283 2019-07-24 stsp switch (ch) {
6317 0ebf8283 2019-07-24 stsp case 'a':
6318 0ebf8283 2019-07-24 stsp abort_edit = 1;
6319 0ebf8283 2019-07-24 stsp break;
6320 0ebf8283 2019-07-24 stsp case 'c':
6321 0ebf8283 2019-07-24 stsp continue_edit = 1;
6322 0ebf8283 2019-07-24 stsp break;
6323 0ebf8283 2019-07-24 stsp case 'F':
6324 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6325 0ebf8283 2019-07-24 stsp break;
6326 0ebf8283 2019-07-24 stsp default:
6327 0ebf8283 2019-07-24 stsp usage_histedit();
6328 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6329 0ebf8283 2019-07-24 stsp }
6330 0ebf8283 2019-07-24 stsp }
6331 0ebf8283 2019-07-24 stsp
6332 0ebf8283 2019-07-24 stsp argc -= optind;
6333 0ebf8283 2019-07-24 stsp argv += optind;
6334 0ebf8283 2019-07-24 stsp
6335 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6336 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6337 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6338 0ebf8283 2019-07-24 stsp err(1, "pledge");
6339 0ebf8283 2019-07-24 stsp #endif
6340 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6341 0ebf8283 2019-07-24 stsp usage_histedit();
6342 0ebf8283 2019-07-24 stsp if (argc != 0)
6343 0ebf8283 2019-07-24 stsp usage_histedit();
6344 0ebf8283 2019-07-24 stsp
6345 0ebf8283 2019-07-24 stsp /*
6346 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6347 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6348 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6349 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6350 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6351 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6352 0ebf8283 2019-07-24 stsp */
6353 0ebf8283 2019-07-24 stsp
6354 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6355 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6356 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6357 0ebf8283 2019-07-24 stsp goto done;
6358 0ebf8283 2019-07-24 stsp }
6359 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6360 0ebf8283 2019-07-24 stsp if (error)
6361 0ebf8283 2019-07-24 stsp goto done;
6362 0ebf8283 2019-07-24 stsp
6363 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6364 c9956ddf 2019-09-08 stsp NULL);
6365 0ebf8283 2019-07-24 stsp if (error != NULL)
6366 0ebf8283 2019-07-24 stsp goto done;
6367 0ebf8283 2019-07-24 stsp
6368 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6369 0ebf8283 2019-07-24 stsp if (error)
6370 0ebf8283 2019-07-24 stsp goto done;
6371 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6372 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6373 0ebf8283 2019-07-24 stsp goto done;
6374 0ebf8283 2019-07-24 stsp }
6375 0ebf8283 2019-07-24 stsp
6376 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6377 0ebf8283 2019-07-24 stsp if (error)
6378 0ebf8283 2019-07-24 stsp goto done;
6379 0ebf8283 2019-07-24 stsp
6380 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6381 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6382 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6383 3e3a69f1 2019-07-25 stsp worktree, repo);
6384 0ebf8283 2019-07-24 stsp if (error)
6385 0ebf8283 2019-07-24 stsp goto done;
6386 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6387 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6388 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6389 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6390 0ebf8283 2019-07-24 stsp if (error)
6391 0ebf8283 2019-07-24 stsp goto done;
6392 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6393 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6394 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6395 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6396 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6397 0ebf8283 2019-07-24 stsp goto done;
6398 0ebf8283 2019-07-24 stsp }
6399 0ebf8283 2019-07-24 stsp
6400 0ebf8283 2019-07-24 stsp if (continue_edit) {
6401 0ebf8283 2019-07-24 stsp char *path;
6402 0ebf8283 2019-07-24 stsp
6403 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6404 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6405 0ebf8283 2019-07-24 stsp goto done;
6406 0ebf8283 2019-07-24 stsp }
6407 0ebf8283 2019-07-24 stsp
6408 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6409 0ebf8283 2019-07-24 stsp if (error)
6410 0ebf8283 2019-07-24 stsp goto done;
6411 0ebf8283 2019-07-24 stsp
6412 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6413 0ebf8283 2019-07-24 stsp free(path);
6414 0ebf8283 2019-07-24 stsp if (error)
6415 0ebf8283 2019-07-24 stsp goto done;
6416 0ebf8283 2019-07-24 stsp
6417 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6418 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6419 3e3a69f1 2019-07-25 stsp worktree, repo);
6420 0ebf8283 2019-07-24 stsp if (error)
6421 0ebf8283 2019-07-24 stsp goto done;
6422 0ebf8283 2019-07-24 stsp
6423 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6424 0ebf8283 2019-07-24 stsp if (error)
6425 0ebf8283 2019-07-24 stsp goto done;
6426 0ebf8283 2019-07-24 stsp
6427 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6428 0ebf8283 2019-07-24 stsp head_commit_id);
6429 0ebf8283 2019-07-24 stsp if (error)
6430 0ebf8283 2019-07-24 stsp goto done;
6431 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6432 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6433 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6434 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6435 3aac7cf7 2019-07-25 stsp goto done;
6436 3aac7cf7 2019-07-25 stsp }
6437 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6438 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6439 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6440 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6441 0ebf8283 2019-07-24 stsp commit = NULL;
6442 0ebf8283 2019-07-24 stsp if (error)
6443 0ebf8283 2019-07-24 stsp goto done;
6444 0ebf8283 2019-07-24 stsp } else {
6445 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6446 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6447 0ebf8283 2019-07-24 stsp goto done;
6448 0ebf8283 2019-07-24 stsp }
6449 0ebf8283 2019-07-24 stsp
6450 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6451 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6452 0ebf8283 2019-07-24 stsp if (error != NULL)
6453 0ebf8283 2019-07-24 stsp goto done;
6454 0ebf8283 2019-07-24 stsp
6455 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6456 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6457 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6458 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6459 c7d20a3f 2019-07-30 stsp goto done;
6460 c7d20a3f 2019-07-30 stsp }
6461 c7d20a3f 2019-07-30 stsp
6462 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6463 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6464 bfce7f83 2019-07-27 stsp branch = NULL;
6465 0ebf8283 2019-07-24 stsp if (error)
6466 0ebf8283 2019-07-24 stsp goto done;
6467 0ebf8283 2019-07-24 stsp
6468 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6469 0ebf8283 2019-07-24 stsp head_commit_id);
6470 0ebf8283 2019-07-24 stsp if (error)
6471 0ebf8283 2019-07-24 stsp goto done;
6472 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6473 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6474 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6475 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6476 3aac7cf7 2019-07-25 stsp goto done;
6477 3aac7cf7 2019-07-25 stsp }
6478 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6479 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6480 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6481 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6482 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6483 0ebf8283 2019-07-24 stsp commit = NULL;
6484 0ebf8283 2019-07-24 stsp if (error)
6485 0ebf8283 2019-07-24 stsp goto done;
6486 0ebf8283 2019-07-24 stsp
6487 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6488 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6489 bfce7f83 2019-07-27 stsp if (error)
6490 bfce7f83 2019-07-27 stsp goto done;
6491 bfce7f83 2019-07-27 stsp
6492 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6493 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6494 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6495 6ba2bea2 2019-07-27 stsp if (error) {
6496 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6497 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6498 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6499 0ebf8283 2019-07-24 stsp goto done;
6500 6ba2bea2 2019-07-27 stsp }
6501 0ebf8283 2019-07-24 stsp } else {
6502 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6503 0ebf8283 2019-07-24 stsp repo);
6504 6ba2bea2 2019-07-27 stsp if (error) {
6505 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6506 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6507 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6508 0ebf8283 2019-07-24 stsp goto done;
6509 6ba2bea2 2019-07-27 stsp }
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp }
6512 0ebf8283 2019-07-24 stsp
6513 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6514 0ebf8283 2019-07-24 stsp repo);
6515 6ba2bea2 2019-07-27 stsp if (error) {
6516 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6517 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6518 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6519 0ebf8283 2019-07-24 stsp goto done;
6520 6ba2bea2 2019-07-27 stsp }
6521 0ebf8283 2019-07-24 stsp
6522 0ebf8283 2019-07-24 stsp }
6523 0ebf8283 2019-07-24 stsp
6524 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6525 4ec14e60 2019-08-28 hiltjo if (error)
6526 0ebf8283 2019-07-24 stsp goto done;
6527 0ebf8283 2019-07-24 stsp
6528 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6529 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6530 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6531 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6532 0ebf8283 2019-07-24 stsp continue;
6533 0ebf8283 2019-07-24 stsp
6534 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6535 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6536 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6537 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6538 0ebf8283 2019-07-24 stsp repo);
6539 0ebf8283 2019-07-24 stsp } else {
6540 0ebf8283 2019-07-24 stsp error = histedit_commit(NULL, worktree,
6541 3e3a69f1 2019-07-25 stsp fileindex, tmp_branch, hle, repo);
6542 0ebf8283 2019-07-24 stsp }
6543 0ebf8283 2019-07-24 stsp if (error)
6544 0ebf8283 2019-07-24 stsp goto done;
6545 0ebf8283 2019-07-24 stsp continue;
6546 0ebf8283 2019-07-24 stsp }
6547 0ebf8283 2019-07-24 stsp
6548 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6549 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6550 0ebf8283 2019-07-24 stsp if (error)
6551 0ebf8283 2019-07-24 stsp goto done;
6552 0ebf8283 2019-07-24 stsp continue;
6553 0ebf8283 2019-07-24 stsp }
6554 0ebf8283 2019-07-24 stsp
6555 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6556 0ebf8283 2019-07-24 stsp hle->commit_id);
6557 0ebf8283 2019-07-24 stsp if (error)
6558 0ebf8283 2019-07-24 stsp goto done;
6559 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6560 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6561 0ebf8283 2019-07-24 stsp
6562 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6563 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6564 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6565 0ebf8283 2019-07-24 stsp if (error)
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6568 0ebf8283 2019-07-24 stsp commit = NULL;
6569 0ebf8283 2019-07-24 stsp
6570 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6571 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6572 0ebf8283 2019-07-24 stsp break;
6573 0ebf8283 2019-07-24 stsp }
6574 0ebf8283 2019-07-24 stsp
6575 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6576 0ebf8283 2019-07-24 stsp char *id_str;
6577 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6578 0ebf8283 2019-07-24 stsp if (error)
6579 0ebf8283 2019-07-24 stsp goto done;
6580 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6581 0ebf8283 2019-07-24 stsp id_str);
6582 0ebf8283 2019-07-24 stsp free(id_str);
6583 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6584 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6585 3e3a69f1 2019-07-25 stsp fileindex);
6586 0ebf8283 2019-07-24 stsp goto done;
6587 0ebf8283 2019-07-24 stsp }
6588 0ebf8283 2019-07-24 stsp
6589 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6590 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6591 0ebf8283 2019-07-24 stsp if (error)
6592 0ebf8283 2019-07-24 stsp goto done;
6593 0ebf8283 2019-07-24 stsp continue;
6594 0ebf8283 2019-07-24 stsp }
6595 0ebf8283 2019-07-24 stsp
6596 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6597 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6598 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6599 0ebf8283 2019-07-24 stsp if (error)
6600 0ebf8283 2019-07-24 stsp goto done;
6601 0ebf8283 2019-07-24 stsp }
6602 0ebf8283 2019-07-24 stsp
6603 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6604 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6605 0ebf8283 2019-07-24 stsp if (error)
6606 0ebf8283 2019-07-24 stsp goto done;
6607 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6608 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6609 0ebf8283 2019-07-24 stsp } else
6610 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6611 3e3a69f1 2019-07-25 stsp branch, repo);
6612 0ebf8283 2019-07-24 stsp done:
6613 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6614 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6615 0ebf8283 2019-07-24 stsp free(head_commit_id);
6616 0ebf8283 2019-07-24 stsp free(base_commit_id);
6617 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6618 0ebf8283 2019-07-24 stsp if (commit)
6619 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6620 0ebf8283 2019-07-24 stsp if (branch)
6621 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6622 0ebf8283 2019-07-24 stsp if (tmp_branch)
6623 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6624 0ebf8283 2019-07-24 stsp if (worktree)
6625 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6626 2822a352 2019-10-15 stsp if (repo)
6627 2822a352 2019-10-15 stsp got_repo_close(repo);
6628 2822a352 2019-10-15 stsp return error;
6629 2822a352 2019-10-15 stsp }
6630 2822a352 2019-10-15 stsp
6631 2822a352 2019-10-15 stsp __dead static void
6632 2822a352 2019-10-15 stsp usage_integrate(void)
6633 2822a352 2019-10-15 stsp {
6634 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6635 2822a352 2019-10-15 stsp exit(1);
6636 2822a352 2019-10-15 stsp }
6637 2822a352 2019-10-15 stsp
6638 2822a352 2019-10-15 stsp static const struct got_error *
6639 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6640 2822a352 2019-10-15 stsp {
6641 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6642 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6643 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6644 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6645 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6646 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6647 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6648 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6649 2822a352 2019-10-15 stsp int ch, did_something = 0;
6650 2822a352 2019-10-15 stsp
6651 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6652 2822a352 2019-10-15 stsp switch (ch) {
6653 2822a352 2019-10-15 stsp default:
6654 2822a352 2019-10-15 stsp usage_integrate();
6655 2822a352 2019-10-15 stsp /* NOTREACHED */
6656 2822a352 2019-10-15 stsp }
6657 2822a352 2019-10-15 stsp }
6658 2822a352 2019-10-15 stsp
6659 2822a352 2019-10-15 stsp argc -= optind;
6660 2822a352 2019-10-15 stsp argv += optind;
6661 2822a352 2019-10-15 stsp
6662 2822a352 2019-10-15 stsp if (argc != 1)
6663 2822a352 2019-10-15 stsp usage_integrate();
6664 2822a352 2019-10-15 stsp branch_arg = argv[0];
6665 2822a352 2019-10-15 stsp
6666 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6667 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6668 2822a352 2019-10-15 stsp err(1, "pledge");
6669 2822a352 2019-10-15 stsp
6670 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6671 2822a352 2019-10-15 stsp if (cwd == NULL) {
6672 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6673 2822a352 2019-10-15 stsp goto done;
6674 2822a352 2019-10-15 stsp }
6675 2822a352 2019-10-15 stsp
6676 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6677 2822a352 2019-10-15 stsp if (error)
6678 2822a352 2019-10-15 stsp goto done;
6679 2822a352 2019-10-15 stsp
6680 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6681 2822a352 2019-10-15 stsp if (error)
6682 2822a352 2019-10-15 stsp goto done;
6683 2822a352 2019-10-15 stsp
6684 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6685 2822a352 2019-10-15 stsp NULL);
6686 2822a352 2019-10-15 stsp if (error != NULL)
6687 2822a352 2019-10-15 stsp goto done;
6688 2822a352 2019-10-15 stsp
6689 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6690 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6691 2822a352 2019-10-15 stsp if (error)
6692 2822a352 2019-10-15 stsp goto done;
6693 2822a352 2019-10-15 stsp
6694 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6695 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6696 2822a352 2019-10-15 stsp goto done;
6697 2822a352 2019-10-15 stsp }
6698 2822a352 2019-10-15 stsp
6699 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6700 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6701 2822a352 2019-10-15 stsp if (error)
6702 2822a352 2019-10-15 stsp goto done;
6703 2822a352 2019-10-15 stsp
6704 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6705 2822a352 2019-10-15 stsp if (refname == NULL) {
6706 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6707 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6708 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6709 2822a352 2019-10-15 stsp goto done;
6710 2822a352 2019-10-15 stsp }
6711 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6712 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6713 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6714 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6715 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6716 2822a352 2019-10-15 stsp goto done;
6717 2822a352 2019-10-15 stsp }
6718 2822a352 2019-10-15 stsp
6719 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6720 2822a352 2019-10-15 stsp if (error)
6721 2822a352 2019-10-15 stsp goto done;
6722 2822a352 2019-10-15 stsp
6723 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6724 2822a352 2019-10-15 stsp if (error)
6725 2822a352 2019-10-15 stsp goto done;
6726 2822a352 2019-10-15 stsp
6727 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6728 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6729 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6730 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6731 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6732 2822a352 2019-10-15 stsp goto done;
6733 2822a352 2019-10-15 stsp }
6734 2822a352 2019-10-15 stsp
6735 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6736 2822a352 2019-10-15 stsp if (error) {
6737 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
6738 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
6739 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6740 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6741 2822a352 2019-10-15 stsp goto done;
6742 2822a352 2019-10-15 stsp }
6743 2822a352 2019-10-15 stsp
6744 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
6745 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
6746 2822a352 2019-10-15 stsp check_cancelled, NULL);
6747 2822a352 2019-10-15 stsp if (error)
6748 2822a352 2019-10-15 stsp goto done;
6749 2822a352 2019-10-15 stsp
6750 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
6751 2822a352 2019-10-15 stsp done:
6752 715dc77e 2019-08-03 stsp if (repo)
6753 715dc77e 2019-08-03 stsp got_repo_close(repo);
6754 2822a352 2019-10-15 stsp if (worktree)
6755 2822a352 2019-10-15 stsp got_worktree_close(worktree);
6756 2822a352 2019-10-15 stsp free(cwd);
6757 2822a352 2019-10-15 stsp free(base_commit_id);
6758 2822a352 2019-10-15 stsp free(commit_id);
6759 2822a352 2019-10-15 stsp free(refname);
6760 2822a352 2019-10-15 stsp free(base_refname);
6761 715dc77e 2019-08-03 stsp return error;
6762 715dc77e 2019-08-03 stsp }
6763 715dc77e 2019-08-03 stsp
6764 715dc77e 2019-08-03 stsp __dead static void
6765 715dc77e 2019-08-03 stsp usage_stage(void)
6766 715dc77e 2019-08-03 stsp {
6767 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
6768 f3055044 2019-08-07 stsp "[file-path ...]\n",
6769 715dc77e 2019-08-03 stsp getprogname());
6770 715dc77e 2019-08-03 stsp exit(1);
6771 715dc77e 2019-08-03 stsp }
6772 715dc77e 2019-08-03 stsp
6773 715dc77e 2019-08-03 stsp static const struct got_error *
6774 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
6775 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
6776 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
6777 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
6778 408b4ebc 2019-08-03 stsp {
6779 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
6780 408b4ebc 2019-08-03 stsp char *id_str = NULL;
6781 408b4ebc 2019-08-03 stsp
6782 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
6783 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
6784 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
6785 408b4ebc 2019-08-03 stsp return NULL;
6786 408b4ebc 2019-08-03 stsp
6787 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
6788 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
6789 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
6790 408b4ebc 2019-08-03 stsp else
6791 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
6792 408b4ebc 2019-08-03 stsp if (err)
6793 408b4ebc 2019-08-03 stsp return err;
6794 408b4ebc 2019-08-03 stsp
6795 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
6796 408b4ebc 2019-08-03 stsp free(id_str);
6797 dc424a06 2019-08-07 stsp return NULL;
6798 dc424a06 2019-08-07 stsp }
6799 2e1f37b0 2019-08-08 stsp
6800 dc424a06 2019-08-07 stsp static const struct got_error *
6801 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
6802 715dc77e 2019-08-03 stsp {
6803 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
6804 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
6805 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
6806 715dc77e 2019-08-03 stsp char *cwd = NULL;
6807 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
6808 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
6809 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
6810 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
6811 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6812 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6813 715dc77e 2019-08-03 stsp
6814 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
6815 715dc77e 2019-08-03 stsp
6816 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
6817 715dc77e 2019-08-03 stsp switch (ch) {
6818 408b4ebc 2019-08-03 stsp case 'l':
6819 408b4ebc 2019-08-03 stsp list_stage = 1;
6820 408b4ebc 2019-08-03 stsp break;
6821 dc424a06 2019-08-07 stsp case 'p':
6822 dc424a06 2019-08-07 stsp pflag = 1;
6823 dc424a06 2019-08-07 stsp break;
6824 dc424a06 2019-08-07 stsp case 'F':
6825 dc424a06 2019-08-07 stsp patch_script_path = optarg;
6826 dc424a06 2019-08-07 stsp break;
6827 715dc77e 2019-08-03 stsp default:
6828 715dc77e 2019-08-03 stsp usage_stage();
6829 715dc77e 2019-08-03 stsp /* NOTREACHED */
6830 715dc77e 2019-08-03 stsp }
6831 715dc77e 2019-08-03 stsp }
6832 715dc77e 2019-08-03 stsp
6833 715dc77e 2019-08-03 stsp argc -= optind;
6834 715dc77e 2019-08-03 stsp argv += optind;
6835 715dc77e 2019-08-03 stsp
6836 715dc77e 2019-08-03 stsp #ifndef PROFILE
6837 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6838 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
6839 715dc77e 2019-08-03 stsp err(1, "pledge");
6840 715dc77e 2019-08-03 stsp #endif
6841 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
6842 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
6843 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
6844 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
6845 715dc77e 2019-08-03 stsp
6846 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
6847 715dc77e 2019-08-03 stsp if (cwd == NULL) {
6848 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
6849 715dc77e 2019-08-03 stsp goto done;
6850 715dc77e 2019-08-03 stsp }
6851 715dc77e 2019-08-03 stsp
6852 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6853 715dc77e 2019-08-03 stsp if (error)
6854 715dc77e 2019-08-03 stsp goto done;
6855 715dc77e 2019-08-03 stsp
6856 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6857 c9956ddf 2019-09-08 stsp NULL);
6858 715dc77e 2019-08-03 stsp if (error != NULL)
6859 715dc77e 2019-08-03 stsp goto done;
6860 715dc77e 2019-08-03 stsp
6861 dc424a06 2019-08-07 stsp if (patch_script_path) {
6862 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
6863 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
6864 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
6865 dc424a06 2019-08-07 stsp patch_script_path);
6866 dc424a06 2019-08-07 stsp goto done;
6867 dc424a06 2019-08-07 stsp }
6868 dc424a06 2019-08-07 stsp }
6869 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6870 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
6871 715dc77e 2019-08-03 stsp if (error)
6872 715dc77e 2019-08-03 stsp goto done;
6873 715dc77e 2019-08-03 stsp
6874 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6875 6d022e97 2019-08-04 stsp if (error)
6876 6d022e97 2019-08-04 stsp goto done;
6877 715dc77e 2019-08-03 stsp
6878 6d022e97 2019-08-04 stsp if (list_stage)
6879 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
6880 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
6881 2e1f37b0 2019-08-08 stsp else {
6882 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6883 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
6884 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
6885 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
6886 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
6887 2e1f37b0 2019-08-08 stsp }
6888 ad493afc 2019-08-03 stsp done:
6889 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
6890 2e1f37b0 2019-08-08 stsp error == NULL)
6891 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
6892 ad493afc 2019-08-03 stsp if (repo)
6893 ad493afc 2019-08-03 stsp got_repo_close(repo);
6894 ad493afc 2019-08-03 stsp if (worktree)
6895 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
6896 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
6897 ad493afc 2019-08-03 stsp free((char *)pe->path);
6898 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
6899 ad493afc 2019-08-03 stsp free(cwd);
6900 ad493afc 2019-08-03 stsp return error;
6901 ad493afc 2019-08-03 stsp }
6902 ad493afc 2019-08-03 stsp
6903 ad493afc 2019-08-03 stsp __dead static void
6904 ad493afc 2019-08-03 stsp usage_unstage(void)
6905 ad493afc 2019-08-03 stsp {
6906 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
6907 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
6908 ad493afc 2019-08-03 stsp getprogname());
6909 ad493afc 2019-08-03 stsp exit(1);
6910 ad493afc 2019-08-03 stsp }
6911 ad493afc 2019-08-03 stsp
6912 ad493afc 2019-08-03 stsp
6913 ad493afc 2019-08-03 stsp static const struct got_error *
6914 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
6915 ad493afc 2019-08-03 stsp {
6916 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
6917 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
6918 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
6919 ad493afc 2019-08-03 stsp char *cwd = NULL;
6920 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
6921 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
6922 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
6923 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
6924 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
6925 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
6926 ad493afc 2019-08-03 stsp
6927 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
6928 ad493afc 2019-08-03 stsp
6929 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
6930 ad493afc 2019-08-03 stsp switch (ch) {
6931 2e1f37b0 2019-08-08 stsp case 'p':
6932 2e1f37b0 2019-08-08 stsp pflag = 1;
6933 2e1f37b0 2019-08-08 stsp break;
6934 2e1f37b0 2019-08-08 stsp case 'F':
6935 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
6936 2e1f37b0 2019-08-08 stsp break;
6937 ad493afc 2019-08-03 stsp default:
6938 ad493afc 2019-08-03 stsp usage_unstage();
6939 ad493afc 2019-08-03 stsp /* NOTREACHED */
6940 ad493afc 2019-08-03 stsp }
6941 ad493afc 2019-08-03 stsp }
6942 ad493afc 2019-08-03 stsp
6943 ad493afc 2019-08-03 stsp argc -= optind;
6944 ad493afc 2019-08-03 stsp argv += optind;
6945 ad493afc 2019-08-03 stsp
6946 ad493afc 2019-08-03 stsp #ifndef PROFILE
6947 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6948 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
6949 ad493afc 2019-08-03 stsp err(1, "pledge");
6950 ad493afc 2019-08-03 stsp #endif
6951 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
6952 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
6953 2e1f37b0 2019-08-08 stsp
6954 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
6955 ad493afc 2019-08-03 stsp if (cwd == NULL) {
6956 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
6957 ad493afc 2019-08-03 stsp goto done;
6958 ad493afc 2019-08-03 stsp }
6959 ad493afc 2019-08-03 stsp
6960 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
6961 ad493afc 2019-08-03 stsp if (error)
6962 ad493afc 2019-08-03 stsp goto done;
6963 ad493afc 2019-08-03 stsp
6964 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6965 c9956ddf 2019-09-08 stsp NULL);
6966 ad493afc 2019-08-03 stsp if (error != NULL)
6967 ad493afc 2019-08-03 stsp goto done;
6968 ad493afc 2019-08-03 stsp
6969 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
6970 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
6971 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
6972 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
6973 2e1f37b0 2019-08-08 stsp patch_script_path);
6974 2e1f37b0 2019-08-08 stsp goto done;
6975 2e1f37b0 2019-08-08 stsp }
6976 2e1f37b0 2019-08-08 stsp }
6977 2e1f37b0 2019-08-08 stsp
6978 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6979 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
6980 ad493afc 2019-08-03 stsp if (error)
6981 ad493afc 2019-08-03 stsp goto done;
6982 ad493afc 2019-08-03 stsp
6983 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
6984 ad493afc 2019-08-03 stsp if (error)
6985 ad493afc 2019-08-03 stsp goto done;
6986 ad493afc 2019-08-03 stsp
6987 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
6988 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
6989 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
6990 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
6991 715dc77e 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 0ebf8283 2019-07-24 stsp if (repo)
6996 0ebf8283 2019-07-24 stsp got_repo_close(repo);
6997 715dc77e 2019-08-03 stsp if (worktree)
6998 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
6999 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7000 715dc77e 2019-08-03 stsp free((char *)pe->path);
7001 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7002 715dc77e 2019-08-03 stsp free(cwd);
7003 01073a5d 2019-08-22 stsp return error;
7004 01073a5d 2019-08-22 stsp }
7005 01073a5d 2019-08-22 stsp
7006 01073a5d 2019-08-22 stsp __dead static void
7007 01073a5d 2019-08-22 stsp usage_cat(void)
7008 01073a5d 2019-08-22 stsp {
7009 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7010 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7011 01073a5d 2019-08-22 stsp exit(1);
7012 01073a5d 2019-08-22 stsp }
7013 01073a5d 2019-08-22 stsp
7014 01073a5d 2019-08-22 stsp static const struct got_error *
7015 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7016 01073a5d 2019-08-22 stsp {
7017 01073a5d 2019-08-22 stsp const struct got_error *err;
7018 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7019 01073a5d 2019-08-22 stsp
7020 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7021 01073a5d 2019-08-22 stsp if (err)
7022 01073a5d 2019-08-22 stsp return err;
7023 01073a5d 2019-08-22 stsp
7024 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7025 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7026 01073a5d 2019-08-22 stsp return err;
7027 01073a5d 2019-08-22 stsp }
7028 01073a5d 2019-08-22 stsp
7029 01073a5d 2019-08-22 stsp static const struct got_error *
7030 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7031 01073a5d 2019-08-22 stsp {
7032 01073a5d 2019-08-22 stsp const struct got_error *err;
7033 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7034 56e0773d 2019-11-28 stsp int nentries, i;
7035 01073a5d 2019-08-22 stsp
7036 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7037 01073a5d 2019-08-22 stsp if (err)
7038 01073a5d 2019-08-22 stsp return err;
7039 01073a5d 2019-08-22 stsp
7040 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7041 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7042 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7043 01073a5d 2019-08-22 stsp char *id_str;
7044 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7045 01073a5d 2019-08-22 stsp break;
7046 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7047 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7048 01073a5d 2019-08-22 stsp if (err)
7049 01073a5d 2019-08-22 stsp break;
7050 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7051 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7052 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7053 01073a5d 2019-08-22 stsp free(id_str);
7054 01073a5d 2019-08-22 stsp }
7055 01073a5d 2019-08-22 stsp
7056 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7057 01073a5d 2019-08-22 stsp return err;
7058 01073a5d 2019-08-22 stsp }
7059 01073a5d 2019-08-22 stsp
7060 01073a5d 2019-08-22 stsp static const struct got_error *
7061 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7062 01073a5d 2019-08-22 stsp {
7063 01073a5d 2019-08-22 stsp const struct got_error *err;
7064 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7065 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7066 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7067 01073a5d 2019-08-22 stsp char *id_str = NULL;
7068 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7069 01073a5d 2019-08-22 stsp
7070 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7071 01073a5d 2019-08-22 stsp if (err)
7072 01073a5d 2019-08-22 stsp return err;
7073 01073a5d 2019-08-22 stsp
7074 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7075 01073a5d 2019-08-22 stsp if (err)
7076 01073a5d 2019-08-22 stsp goto done;
7077 01073a5d 2019-08-22 stsp
7078 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7079 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7080 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7081 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7082 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7083 01073a5d 2019-08-22 stsp char *pid_str;
7084 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7085 01073a5d 2019-08-22 stsp if (err)
7086 01073a5d 2019-08-22 stsp goto done;
7087 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7088 01073a5d 2019-08-22 stsp free(pid_str);
7089 01073a5d 2019-08-22 stsp }
7090 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7091 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7092 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7093 01073a5d 2019-08-22 stsp
7094 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7095 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7096 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7097 01073a5d 2019-08-22 stsp
7098 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7099 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7100 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7101 01073a5d 2019-08-22 stsp done:
7102 01073a5d 2019-08-22 stsp free(id_str);
7103 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7104 01073a5d 2019-08-22 stsp return err;
7105 01073a5d 2019-08-22 stsp }
7106 01073a5d 2019-08-22 stsp
7107 01073a5d 2019-08-22 stsp static const struct got_error *
7108 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7109 01073a5d 2019-08-22 stsp {
7110 01073a5d 2019-08-22 stsp const struct got_error *err;
7111 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7112 01073a5d 2019-08-22 stsp char *id_str = NULL;
7113 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7114 01073a5d 2019-08-22 stsp
7115 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7116 01073a5d 2019-08-22 stsp if (err)
7117 01073a5d 2019-08-22 stsp return err;
7118 01073a5d 2019-08-22 stsp
7119 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7120 01073a5d 2019-08-22 stsp if (err)
7121 01073a5d 2019-08-22 stsp goto done;
7122 01073a5d 2019-08-22 stsp
7123 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7124 e15d5241 2019-08-22 stsp
7125 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7126 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7127 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7128 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7129 01073a5d 2019-08-22 stsp break;
7130 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7131 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7132 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7133 01073a5d 2019-08-22 stsp break;
7134 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7135 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7136 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7137 01073a5d 2019-08-22 stsp break;
7138 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7139 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7140 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7141 01073a5d 2019-08-22 stsp break;
7142 01073a5d 2019-08-22 stsp default:
7143 01073a5d 2019-08-22 stsp break;
7144 01073a5d 2019-08-22 stsp }
7145 01073a5d 2019-08-22 stsp
7146 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7147 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7148 e15d5241 2019-08-22 stsp
7149 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7150 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7151 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7152 01073a5d 2019-08-22 stsp
7153 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7154 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7155 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7156 01073a5d 2019-08-22 stsp done:
7157 01073a5d 2019-08-22 stsp free(id_str);
7158 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7159 01073a5d 2019-08-22 stsp return err;
7160 01073a5d 2019-08-22 stsp }
7161 01073a5d 2019-08-22 stsp
7162 01073a5d 2019-08-22 stsp static const struct got_error *
7163 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7164 01073a5d 2019-08-22 stsp {
7165 01073a5d 2019-08-22 stsp const struct got_error *error;
7166 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7167 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7168 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7169 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7170 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7171 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7172 01073a5d 2019-08-22 stsp
7173 01073a5d 2019-08-22 stsp #ifndef PROFILE
7174 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7175 01073a5d 2019-08-22 stsp NULL) == -1)
7176 01073a5d 2019-08-22 stsp err(1, "pledge");
7177 01073a5d 2019-08-22 stsp #endif
7178 01073a5d 2019-08-22 stsp
7179 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7180 01073a5d 2019-08-22 stsp switch (ch) {
7181 896e9b6f 2019-08-26 stsp case 'c':
7182 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7183 896e9b6f 2019-08-26 stsp break;
7184 01073a5d 2019-08-22 stsp case 'r':
7185 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7186 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7187 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7188 9ba1d308 2019-10-21 stsp optarg);
7189 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7190 01073a5d 2019-08-22 stsp break;
7191 896e9b6f 2019-08-26 stsp case 'P':
7192 896e9b6f 2019-08-26 stsp force_path = 1;
7193 896e9b6f 2019-08-26 stsp break;
7194 01073a5d 2019-08-22 stsp default:
7195 01073a5d 2019-08-22 stsp usage_cat();
7196 01073a5d 2019-08-22 stsp /* NOTREACHED */
7197 01073a5d 2019-08-22 stsp }
7198 01073a5d 2019-08-22 stsp }
7199 01073a5d 2019-08-22 stsp
7200 01073a5d 2019-08-22 stsp argc -= optind;
7201 01073a5d 2019-08-22 stsp argv += optind;
7202 01073a5d 2019-08-22 stsp
7203 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7204 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7205 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7206 01073a5d 2019-08-22 stsp goto done;
7207 01073a5d 2019-08-22 stsp }
7208 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7209 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7210 01073a5d 2019-08-22 stsp goto done;
7211 01073a5d 2019-08-22 stsp if (worktree) {
7212 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7213 01073a5d 2019-08-22 stsp repo_path = strdup(
7214 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7215 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7216 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7217 01073a5d 2019-08-22 stsp goto done;
7218 01073a5d 2019-08-22 stsp }
7219 01073a5d 2019-08-22 stsp }
7220 01073a5d 2019-08-22 stsp }
7221 01073a5d 2019-08-22 stsp
7222 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7223 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7224 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7225 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7226 01073a5d 2019-08-22 stsp }
7227 01073a5d 2019-08-22 stsp
7228 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7229 01073a5d 2019-08-22 stsp free(repo_path);
7230 01073a5d 2019-08-22 stsp if (error != NULL)
7231 01073a5d 2019-08-22 stsp goto done;
7232 01073a5d 2019-08-22 stsp
7233 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7234 896e9b6f 2019-08-26 stsp if (error)
7235 896e9b6f 2019-08-26 stsp goto done;
7236 896e9b6f 2019-08-26 stsp
7237 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7238 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7239 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7240 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7241 01073a5d 2019-08-22 stsp if (error)
7242 01073a5d 2019-08-22 stsp goto done;
7243 01073a5d 2019-08-22 stsp
7244 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7245 896e9b6f 2019-08-26 stsp if (force_path) {
7246 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7247 896e9b6f 2019-08-26 stsp argv[i]);
7248 896e9b6f 2019-08-26 stsp if (error)
7249 896e9b6f 2019-08-26 stsp break;
7250 896e9b6f 2019-08-26 stsp } else {
7251 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7252 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7253 896e9b6f 2019-08-26 stsp if (error) {
7254 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7255 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7256 896e9b6f 2019-08-26 stsp break;
7257 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7258 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7259 896e9b6f 2019-08-26 stsp if (error)
7260 896e9b6f 2019-08-26 stsp break;
7261 896e9b6f 2019-08-26 stsp }
7262 896e9b6f 2019-08-26 stsp }
7263 01073a5d 2019-08-22 stsp
7264 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7265 01073a5d 2019-08-22 stsp if (error)
7266 01073a5d 2019-08-22 stsp break;
7267 01073a5d 2019-08-22 stsp
7268 01073a5d 2019-08-22 stsp switch (obj_type) {
7269 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7270 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7271 01073a5d 2019-08-22 stsp break;
7272 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7273 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7274 01073a5d 2019-08-22 stsp break;
7275 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7276 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7277 01073a5d 2019-08-22 stsp break;
7278 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7279 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7280 01073a5d 2019-08-22 stsp break;
7281 01073a5d 2019-08-22 stsp default:
7282 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7283 01073a5d 2019-08-22 stsp break;
7284 01073a5d 2019-08-22 stsp }
7285 01073a5d 2019-08-22 stsp if (error)
7286 01073a5d 2019-08-22 stsp break;
7287 01073a5d 2019-08-22 stsp free(label);
7288 01073a5d 2019-08-22 stsp label = NULL;
7289 01073a5d 2019-08-22 stsp free(id);
7290 01073a5d 2019-08-22 stsp id = NULL;
7291 01073a5d 2019-08-22 stsp }
7292 01073a5d 2019-08-22 stsp done:
7293 01073a5d 2019-08-22 stsp free(label);
7294 01073a5d 2019-08-22 stsp free(id);
7295 896e9b6f 2019-08-26 stsp free(commit_id);
7296 01073a5d 2019-08-22 stsp if (worktree)
7297 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7298 01073a5d 2019-08-22 stsp if (repo) {
7299 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7300 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7301 01073a5d 2019-08-22 stsp if (error == NULL)
7302 01073a5d 2019-08-22 stsp error = repo_error;
7303 01073a5d 2019-08-22 stsp }
7304 0ebf8283 2019-07-24 stsp return error;
7305 0ebf8283 2019-07-24 stsp }