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 bb63914a 2020-02-17 stsp if (unveil(GOT_TMPDIR_STR, "rwc") != 0)
301 bb63914a 2020-02-17 stsp return got_error_from_errno2("unveil", GOT_TMPDIR_STR);
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 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(logmsg_path, &fd,
486 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-importmsg");
487 3ce1b845 2019-07-15 stsp if (err)
488 3ce1b845 2019-07-15 stsp goto done;
489 3ce1b845 2019-07-15 stsp
490 3ce1b845 2019-07-15 stsp dprintf(fd, initial_content);
491 3ce1b845 2019-07-15 stsp close(fd);
492 3ce1b845 2019-07-15 stsp
493 ef293bdd 2019-10-21 stsp err = edit_logmsg(logmsg, editor, *logmsg_path, initial_content);
494 3ce1b845 2019-07-15 stsp done:
495 3ce1b845 2019-07-15 stsp free(initial_content);
496 3ce1b845 2019-07-15 stsp return err;
497 3ce1b845 2019-07-15 stsp }
498 3ce1b845 2019-07-15 stsp
499 3ce1b845 2019-07-15 stsp static const struct got_error *
500 3ce1b845 2019-07-15 stsp import_progress(void *arg, const char *path)
501 3ce1b845 2019-07-15 stsp {
502 3ce1b845 2019-07-15 stsp printf("A %s\n", path);
503 3ce1b845 2019-07-15 stsp return NULL;
504 3ce1b845 2019-07-15 stsp }
505 3ce1b845 2019-07-15 stsp
506 3ce1b845 2019-07-15 stsp static const struct got_error *
507 aba9c984 2019-09-08 stsp get_author(char **author, struct got_repository *repo)
508 84792843 2019-08-09 stsp {
509 aba9c984 2019-09-08 stsp const struct got_error *err = NULL;
510 c9956ddf 2019-09-08 stsp const char *got_author, *name, *email;
511 84792843 2019-08-09 stsp
512 84792843 2019-08-09 stsp *author = NULL;
513 aba9c984 2019-09-08 stsp
514 c9956ddf 2019-09-08 stsp name = got_repo_get_gitconfig_author_name(repo);
515 c9956ddf 2019-09-08 stsp email = got_repo_get_gitconfig_author_email(repo);
516 c9956ddf 2019-09-08 stsp if (name && email) {
517 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
518 aba9c984 2019-09-08 stsp return got_error_from_errno("asprintf");
519 aba9c984 2019-09-08 stsp return NULL;
520 aba9c984 2019-09-08 stsp }
521 84792843 2019-08-09 stsp
522 84792843 2019-08-09 stsp got_author = getenv("GOT_AUTHOR");
523 84792843 2019-08-09 stsp if (got_author == NULL) {
524 c9956ddf 2019-09-08 stsp name = got_repo_get_global_gitconfig_author_name(repo);
525 c9956ddf 2019-09-08 stsp email = got_repo_get_global_gitconfig_author_email(repo);
526 c9956ddf 2019-09-08 stsp if (name && email) {
527 c9956ddf 2019-09-08 stsp if (asprintf(author, "%s <%s>", name, email) == -1)
528 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
529 c9956ddf 2019-09-08 stsp return NULL;
530 c9956ddf 2019-09-08 stsp }
531 84792843 2019-08-09 stsp /* TODO: Look up user in password database? */
532 84792843 2019-08-09 stsp return got_error(GOT_ERR_COMMIT_NO_AUTHOR);
533 84792843 2019-08-09 stsp }
534 84792843 2019-08-09 stsp
535 aba9c984 2019-09-08 stsp *author = strdup(got_author);
536 aba9c984 2019-09-08 stsp if (*author == NULL)
537 aba9c984 2019-09-08 stsp return got_error_from_errno("strdup");
538 84792843 2019-08-09 stsp
539 84792843 2019-08-09 stsp /*
540 84792843 2019-08-09 stsp * Really dumb email address check; we're only doing this to
541 84792843 2019-08-09 stsp * avoid git's object parser breaking on commits we create.
542 84792843 2019-08-09 stsp */
543 84792843 2019-08-09 stsp while (*got_author && *got_author != '<')
544 84792843 2019-08-09 stsp got_author++;
545 aba9c984 2019-09-08 stsp if (*got_author != '<') {
546 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
547 aba9c984 2019-09-08 stsp goto done;
548 aba9c984 2019-09-08 stsp }
549 84792843 2019-08-09 stsp while (*got_author && *got_author != '@')
550 84792843 2019-08-09 stsp got_author++;
551 aba9c984 2019-09-08 stsp if (*got_author != '@') {
552 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
553 aba9c984 2019-09-08 stsp goto done;
554 aba9c984 2019-09-08 stsp }
555 84792843 2019-08-09 stsp while (*got_author && *got_author != '>')
556 84792843 2019-08-09 stsp got_author++;
557 84792843 2019-08-09 stsp if (*got_author != '>')
558 aba9c984 2019-09-08 stsp err = got_error(GOT_ERR_COMMIT_NO_EMAIL);
559 aba9c984 2019-09-08 stsp done:
560 aba9c984 2019-09-08 stsp if (err) {
561 aba9c984 2019-09-08 stsp free(*author);
562 aba9c984 2019-09-08 stsp *author = NULL;
563 aba9c984 2019-09-08 stsp }
564 aba9c984 2019-09-08 stsp return err;
565 c9956ddf 2019-09-08 stsp }
566 c9956ddf 2019-09-08 stsp
567 c9956ddf 2019-09-08 stsp static const struct got_error *
568 c9956ddf 2019-09-08 stsp get_gitconfig_path(char **gitconfig_path)
569 c9956ddf 2019-09-08 stsp {
570 c9956ddf 2019-09-08 stsp const char *homedir = getenv("HOME");
571 c9956ddf 2019-09-08 stsp
572 c9956ddf 2019-09-08 stsp *gitconfig_path = NULL;
573 c9956ddf 2019-09-08 stsp if (homedir) {
574 c9956ddf 2019-09-08 stsp if (asprintf(gitconfig_path, "%s/.gitconfig", homedir) == -1)
575 c9956ddf 2019-09-08 stsp return got_error_from_errno("asprintf");
576 c9956ddf 2019-09-08 stsp
577 c9956ddf 2019-09-08 stsp }
578 c9956ddf 2019-09-08 stsp return NULL;
579 84792843 2019-08-09 stsp }
580 84792843 2019-08-09 stsp
581 84792843 2019-08-09 stsp static const struct got_error *
582 3ce1b845 2019-07-15 stsp cmd_import(int argc, char *argv[])
583 3ce1b845 2019-07-15 stsp {
584 3ce1b845 2019-07-15 stsp const struct got_error *error = NULL;
585 3ce1b845 2019-07-15 stsp char *path_dir = NULL, *repo_path = NULL, *logmsg = NULL;
586 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
587 5d67f40d 2019-11-08 stsp const char *branch_name = "main";
588 ef293bdd 2019-10-21 stsp char *refname = NULL, *id_str = NULL, *logmsg_path = NULL;
589 3ce1b845 2019-07-15 stsp struct got_repository *repo = NULL;
590 3ce1b845 2019-07-15 stsp struct got_reference *branch_ref = NULL, *head_ref = NULL;
591 3ce1b845 2019-07-15 stsp struct got_object_id *new_commit_id = NULL;
592 3ce1b845 2019-07-15 stsp int ch;
593 3ce1b845 2019-07-15 stsp struct got_pathlist_head ignores;
594 3ce1b845 2019-07-15 stsp struct got_pathlist_entry *pe;
595 ef293bdd 2019-10-21 stsp int preserve_logmsg = 0;
596 3ce1b845 2019-07-15 stsp
597 3ce1b845 2019-07-15 stsp TAILQ_INIT(&ignores);
598 3ce1b845 2019-07-15 stsp
599 3ce1b845 2019-07-15 stsp while ((ch = getopt(argc, argv, "b:m:r:I:")) != -1) {
600 3ce1b845 2019-07-15 stsp switch (ch) {
601 3ce1b845 2019-07-15 stsp case 'b':
602 3ce1b845 2019-07-15 stsp branch_name = optarg;
603 3ce1b845 2019-07-15 stsp break;
604 3ce1b845 2019-07-15 stsp case 'm':
605 3ce1b845 2019-07-15 stsp logmsg = strdup(optarg);
606 3ce1b845 2019-07-15 stsp if (logmsg == NULL) {
607 3ce1b845 2019-07-15 stsp error = got_error_from_errno("strdup");
608 3ce1b845 2019-07-15 stsp goto done;
609 3ce1b845 2019-07-15 stsp }
610 3ce1b845 2019-07-15 stsp break;
611 3ce1b845 2019-07-15 stsp case 'r':
612 3ce1b845 2019-07-15 stsp repo_path = realpath(optarg, NULL);
613 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
614 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath",
615 9ba1d308 2019-10-21 stsp optarg);
616 3ce1b845 2019-07-15 stsp goto done;
617 3ce1b845 2019-07-15 stsp }
618 3ce1b845 2019-07-15 stsp break;
619 3ce1b845 2019-07-15 stsp case 'I':
620 3ce1b845 2019-07-15 stsp if (optarg[0] == '\0')
621 3ce1b845 2019-07-15 stsp break;
622 3ce1b845 2019-07-15 stsp error = got_pathlist_insert(&pe, &ignores, optarg,
623 3ce1b845 2019-07-15 stsp NULL);
624 3ce1b845 2019-07-15 stsp if (error)
625 3ce1b845 2019-07-15 stsp goto done;
626 3ce1b845 2019-07-15 stsp break;
627 3ce1b845 2019-07-15 stsp default:
628 b2b65d18 2019-08-22 stsp usage_import();
629 3ce1b845 2019-07-15 stsp /* NOTREACHED */
630 3ce1b845 2019-07-15 stsp }
631 3ce1b845 2019-07-15 stsp }
632 3ce1b845 2019-07-15 stsp
633 3ce1b845 2019-07-15 stsp argc -= optind;
634 3ce1b845 2019-07-15 stsp argv += optind;
635 3ce1b845 2019-07-15 stsp
636 3ce1b845 2019-07-15 stsp #ifndef PROFILE
637 aba9c984 2019-09-08 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
638 aba9c984 2019-09-08 stsp "unveil",
639 3ce1b845 2019-07-15 stsp NULL) == -1)
640 3ce1b845 2019-07-15 stsp err(1, "pledge");
641 3ce1b845 2019-07-15 stsp #endif
642 3ce1b845 2019-07-15 stsp if (argc != 1)
643 3ce1b845 2019-07-15 stsp usage_import();
644 2c7829a4 2019-06-17 stsp
645 3ce1b845 2019-07-15 stsp if (repo_path == NULL) {
646 3ce1b845 2019-07-15 stsp repo_path = getcwd(NULL, 0);
647 3ce1b845 2019-07-15 stsp if (repo_path == NULL)
648 3ce1b845 2019-07-15 stsp return got_error_from_errno("getcwd");
649 3ce1b845 2019-07-15 stsp }
650 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(repo_path);
651 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
652 c9956ddf 2019-09-08 stsp if (error)
653 c9956ddf 2019-09-08 stsp goto done;
654 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
655 3ce1b845 2019-07-15 stsp if (error)
656 3ce1b845 2019-07-15 stsp goto done;
657 aba9c984 2019-09-08 stsp
658 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
659 aba9c984 2019-09-08 stsp if (error)
660 aba9c984 2019-09-08 stsp return error;
661 e560b7e0 2019-11-28 stsp
662 e560b7e0 2019-11-28 stsp /*
663 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
664 e560b7e0 2019-11-28 stsp * While technically a valid reference name, this case is usually
665 e560b7e0 2019-11-28 stsp * an unintended typo.
666 e560b7e0 2019-11-28 stsp */
667 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
668 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
669 3ce1b845 2019-07-15 stsp
670 3ce1b845 2019-07-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
671 3ce1b845 2019-07-15 stsp error = got_error_from_errno("asprintf");
672 3ce1b845 2019-07-15 stsp goto done;
673 3ce1b845 2019-07-15 stsp }
674 3ce1b845 2019-07-15 stsp
675 3ce1b845 2019-07-15 stsp error = got_ref_open(&branch_ref, repo, refname, 0);
676 3ce1b845 2019-07-15 stsp if (error) {
677 3ce1b845 2019-07-15 stsp if (error->code != GOT_ERR_NOT_REF)
678 3ce1b845 2019-07-15 stsp goto done;
679 3ce1b845 2019-07-15 stsp } else {
680 3ce1b845 2019-07-15 stsp error = got_error_msg(GOT_ERR_BRANCH_EXISTS,
681 3ce1b845 2019-07-15 stsp "import target branch already exists");
682 3ce1b845 2019-07-15 stsp goto done;
683 3ce1b845 2019-07-15 stsp }
684 3ce1b845 2019-07-15 stsp
685 3ce1b845 2019-07-15 stsp path_dir = realpath(argv[0], NULL);
686 3ce1b845 2019-07-15 stsp if (path_dir == NULL) {
687 9ba1d308 2019-10-21 stsp error = got_error_from_errno2("realpath", argv[0]);
688 3ce1b845 2019-07-15 stsp goto done;
689 3ce1b845 2019-07-15 stsp }
690 3ce1b845 2019-07-15 stsp got_path_strip_trailing_slashes(path_dir);
691 3ce1b845 2019-07-15 stsp
692 3ce1b845 2019-07-15 stsp /*
693 3ce1b845 2019-07-15 stsp * unveil(2) traverses exec(2); if an editor is used we have
694 3ce1b845 2019-07-15 stsp * to apply unveil after the log message has been written.
695 3ce1b845 2019-07-15 stsp */
696 3ce1b845 2019-07-15 stsp if (logmsg == NULL || strlen(logmsg) == 0) {
697 3ce1b845 2019-07-15 stsp error = get_editor(&editor);
698 3ce1b845 2019-07-15 stsp if (error)
699 3ce1b845 2019-07-15 stsp goto done;
700 8e158b01 2019-09-22 stsp free(logmsg);
701 ef293bdd 2019-10-21 stsp error = collect_import_msg(&logmsg, &logmsg_path, editor,
702 ef293bdd 2019-10-21 stsp path_dir, refname);
703 ef293bdd 2019-10-21 stsp if (error) {
704 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
705 ef293bdd 2019-10-21 stsp logmsg_path != NULL)
706 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
707 3ce1b845 2019-07-15 stsp goto done;
708 ef293bdd 2019-10-21 stsp }
709 3ce1b845 2019-07-15 stsp }
710 3ce1b845 2019-07-15 stsp
711 ef293bdd 2019-10-21 stsp if (unveil(path_dir, "r") != 0) {
712 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unveil", path_dir);
713 ef293bdd 2019-10-21 stsp if (logmsg_path)
714 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
715 3ce1b845 2019-07-15 stsp goto done;
716 ef293bdd 2019-10-21 stsp }
717 3ce1b845 2019-07-15 stsp
718 ef293bdd 2019-10-21 stsp error = apply_unveil(got_repo_get_path(repo), 0, NULL);
719 ef293bdd 2019-10-21 stsp if (error) {
720 ef293bdd 2019-10-21 stsp if (logmsg_path)
721 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
722 ef293bdd 2019-10-21 stsp goto done;
723 ef293bdd 2019-10-21 stsp }
724 ef293bdd 2019-10-21 stsp
725 3ce1b845 2019-07-15 stsp error = got_repo_import(&new_commit_id, path_dir, logmsg,
726 84792843 2019-08-09 stsp author, &ignores, repo, import_progress, NULL);
727 ef293bdd 2019-10-21 stsp if (error) {
728 ef293bdd 2019-10-21 stsp if (logmsg_path)
729 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
730 3ce1b845 2019-07-15 stsp goto done;
731 ef293bdd 2019-10-21 stsp }
732 3ce1b845 2019-07-15 stsp
733 3ce1b845 2019-07-15 stsp error = got_ref_alloc(&branch_ref, refname, new_commit_id);
734 ef293bdd 2019-10-21 stsp if (error) {
735 ef293bdd 2019-10-21 stsp if (logmsg_path)
736 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
737 3ce1b845 2019-07-15 stsp goto done;
738 ef293bdd 2019-10-21 stsp }
739 3ce1b845 2019-07-15 stsp
740 3ce1b845 2019-07-15 stsp error = got_ref_write(branch_ref, repo);
741 ef293bdd 2019-10-21 stsp if (error) {
742 ef293bdd 2019-10-21 stsp if (logmsg_path)
743 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
744 3ce1b845 2019-07-15 stsp goto done;
745 ef293bdd 2019-10-21 stsp }
746 3ce1b845 2019-07-15 stsp
747 3ce1b845 2019-07-15 stsp error = got_object_id_str(&id_str, new_commit_id);
748 ef293bdd 2019-10-21 stsp if (error) {
749 ef293bdd 2019-10-21 stsp if (logmsg_path)
750 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
751 3ce1b845 2019-07-15 stsp goto done;
752 ef293bdd 2019-10-21 stsp }
753 3ce1b845 2019-07-15 stsp
754 3ce1b845 2019-07-15 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
755 3ce1b845 2019-07-15 stsp if (error) {
756 ef293bdd 2019-10-21 stsp if (error->code != GOT_ERR_NOT_REF) {
757 ef293bdd 2019-10-21 stsp if (logmsg_path)
758 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
759 3ce1b845 2019-07-15 stsp goto done;
760 ef293bdd 2019-10-21 stsp }
761 3ce1b845 2019-07-15 stsp
762 3ce1b845 2019-07-15 stsp error = got_ref_alloc_symref(&head_ref, GOT_REF_HEAD,
763 3ce1b845 2019-07-15 stsp branch_ref);
764 ef293bdd 2019-10-21 stsp if (error) {
765 ef293bdd 2019-10-21 stsp if (logmsg_path)
766 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
767 3ce1b845 2019-07-15 stsp goto done;
768 ef293bdd 2019-10-21 stsp }
769 3ce1b845 2019-07-15 stsp
770 3ce1b845 2019-07-15 stsp error = got_ref_write(head_ref, repo);
771 ef293bdd 2019-10-21 stsp if (error) {
772 ef293bdd 2019-10-21 stsp if (logmsg_path)
773 ef293bdd 2019-10-21 stsp preserve_logmsg = 1;
774 3ce1b845 2019-07-15 stsp goto done;
775 ef293bdd 2019-10-21 stsp }
776 3ce1b845 2019-07-15 stsp }
777 3ce1b845 2019-07-15 stsp
778 3ce1b845 2019-07-15 stsp printf("Created branch %s with commit %s\n",
779 3ce1b845 2019-07-15 stsp got_ref_get_name(branch_ref), id_str);
780 2c7829a4 2019-06-17 stsp done:
781 ef293bdd 2019-10-21 stsp if (preserve_logmsg) {
782 ef293bdd 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
783 ef293bdd 2019-10-21 stsp getprogname(), logmsg_path);
784 ef293bdd 2019-10-21 stsp } else if (logmsg_path && unlink(logmsg_path) == -1 && error == NULL)
785 ef293bdd 2019-10-21 stsp error = got_error_from_errno2("unlink", logmsg_path);
786 8e158b01 2019-09-22 stsp free(logmsg);
787 ef293bdd 2019-10-21 stsp free(logmsg_path);
788 2c7829a4 2019-06-17 stsp free(repo_path);
789 3ce1b845 2019-07-15 stsp free(editor);
790 3ce1b845 2019-07-15 stsp free(refname);
791 3ce1b845 2019-07-15 stsp free(new_commit_id);
792 3ce1b845 2019-07-15 stsp free(id_str);
793 aba9c984 2019-09-08 stsp free(author);
794 c9956ddf 2019-09-08 stsp free(gitconfig_path);
795 3ce1b845 2019-07-15 stsp if (branch_ref)
796 3ce1b845 2019-07-15 stsp got_ref_close(branch_ref);
797 3ce1b845 2019-07-15 stsp if (head_ref)
798 3ce1b845 2019-07-15 stsp got_ref_close(head_ref);
799 2c7829a4 2019-06-17 stsp return error;
800 0266afb7 2019-01-04 stsp }
801 0266afb7 2019-01-04 stsp
802 4ed7e80c 2018-05-20 stsp __dead static void
803 c09a553d 2018-03-12 stsp usage_checkout(void)
804 c09a553d 2018-03-12 stsp {
805 bb51a5b4 2020-01-13 stsp fprintf(stderr, "usage: %s checkout [-E] [-b branch] [-c commit] "
806 08573d5b 2019-05-14 stsp "[-p prefix] repository-path [worktree-path]\n", getprogname());
807 c09a553d 2018-03-12 stsp exit(1);
808 92a684f4 2018-03-12 stsp }
809 92a684f4 2018-03-12 stsp
810 7f47418f 2019-12-20 stsp static void
811 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning(void)
812 7f47418f 2019-12-20 stsp {
813 7f47418f 2019-12-20 stsp fprintf(stderr, "%s: warning: could not create a reference "
814 7f47418f 2019-12-20 stsp "to the work tree's base commit; the commit could be "
815 7f47418f 2019-12-20 stsp "garbage-collected by Git; making the repository "
816 7f47418f 2019-12-20 stsp "writable and running 'got update' will prevent this\n",
817 7f47418f 2019-12-20 stsp getprogname());
818 7f47418f 2019-12-20 stsp }
819 7f47418f 2019-12-20 stsp
820 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg {
821 7f47418f 2019-12-20 stsp const char *worktree_path;
822 7f47418f 2019-12-20 stsp int had_base_commit_ref_error;
823 7f47418f 2019-12-20 stsp };
824 7f47418f 2019-12-20 stsp
825 1ee397ad 2019-07-12 stsp static const struct got_error *
826 a0eb853d 2018-12-29 stsp checkout_progress(void *arg, unsigned char status, const char *path)
827 92a684f4 2018-03-12 stsp {
828 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg *a = arg;
829 a484d721 2019-06-10 stsp
830 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
831 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
832 7f47418f 2019-12-20 stsp return NULL;
833 7f47418f 2019-12-20 stsp
834 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_BASE_REF_ERR) {
835 7f47418f 2019-12-20 stsp a->had_base_commit_ref_error = 1;
836 1ee397ad 2019-07-12 stsp return NULL;
837 7f47418f 2019-12-20 stsp }
838 92a684f4 2018-03-12 stsp
839 92a684f4 2018-03-12 stsp while (path[0] == '/')
840 92a684f4 2018-03-12 stsp path++;
841 92a684f4 2018-03-12 stsp
842 7f47418f 2019-12-20 stsp printf("%c %s/%s\n", status, a->worktree_path, path);
843 1ee397ad 2019-07-12 stsp return NULL;
844 99437157 2018-11-11 stsp }
845 99437157 2018-11-11 stsp
846 99437157 2018-11-11 stsp static const struct got_error *
847 6bad629b 2019-02-04 stsp check_cancelled(void *arg)
848 99437157 2018-11-11 stsp {
849 99437157 2018-11-11 stsp if (sigint_received || sigpipe_received)
850 99437157 2018-11-11 stsp return got_error(GOT_ERR_CANCELLED);
851 99437157 2018-11-11 stsp return NULL;
852 8069f636 2019-01-12 stsp }
853 8069f636 2019-01-12 stsp
854 8069f636 2019-01-12 stsp static const struct got_error *
855 024e9686 2019-05-14 stsp check_linear_ancestry(struct got_object_id *commit_id,
856 3aef623b 2019-10-15 stsp struct got_object_id *base_commit_id, int allow_forwards_in_time_only,
857 3aef623b 2019-10-15 stsp struct got_repository *repo)
858 8069f636 2019-01-12 stsp {
859 d5bea539 2019-05-13 stsp const struct got_error *err = NULL;
860 024e9686 2019-05-14 stsp struct got_object_id *yca_id;
861 8069f636 2019-01-12 stsp
862 d5bea539 2019-05-13 stsp err = got_commit_graph_find_youngest_common_ancestor(&yca_id,
863 6fb7cd11 2019-08-22 stsp commit_id, base_commit_id, repo, check_cancelled, NULL);
864 36a38700 2019-05-10 stsp if (err)
865 36a38700 2019-05-10 stsp return err;
866 8069f636 2019-01-12 stsp
867 d5bea539 2019-05-13 stsp if (yca_id == NULL)
868 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
869 8069f636 2019-01-12 stsp
870 d5bea539 2019-05-13 stsp /*
871 d5bea539 2019-05-13 stsp * Require a straight line of history between the target commit
872 d5bea539 2019-05-13 stsp * and the work tree's base commit.
873 d5bea539 2019-05-13 stsp *
874 d5751d49 2019-05-14 stsp * Non-linear situations such as this require a rebase:
875 d5bea539 2019-05-13 stsp *
876 d5bea539 2019-05-13 stsp * (commit) D F (base_commit)
877 d5bea539 2019-05-13 stsp * \ /
878 d5bea539 2019-05-13 stsp * C E
879 d5bea539 2019-05-13 stsp * \ /
880 d5bea539 2019-05-13 stsp * B (yca)
881 d5bea539 2019-05-13 stsp * |
882 d5bea539 2019-05-13 stsp * A
883 d5bea539 2019-05-13 stsp *
884 d5bea539 2019-05-13 stsp * 'got update' only handles linear cases:
885 d5bea539 2019-05-13 stsp * Update forwards in time: A (base/yca) - B - C - D (commit)
886 efa2b6f7 2019-05-14 stsp * Update backwards in time: D (base) - C - B - A (commit/yca)
887 d5bea539 2019-05-13 stsp */
888 3aef623b 2019-10-15 stsp if (allow_forwards_in_time_only) {
889 3aef623b 2019-10-15 stsp if (got_object_id_cmp(base_commit_id, yca_id) != 0)
890 3aef623b 2019-10-15 stsp return got_error(GOT_ERR_ANCESTRY);
891 3aef623b 2019-10-15 stsp } else if (got_object_id_cmp(commit_id, yca_id) != 0 &&
892 d5bea539 2019-05-13 stsp got_object_id_cmp(base_commit_id, yca_id) != 0)
893 d5bea539 2019-05-13 stsp return got_error(GOT_ERR_ANCESTRY);
894 8069f636 2019-01-12 stsp
895 d5bea539 2019-05-13 stsp free(yca_id);
896 d5bea539 2019-05-13 stsp return NULL;
897 c09a553d 2018-03-12 stsp }
898 a367ff0f 2019-05-14 stsp
899 a367ff0f 2019-05-14 stsp static const struct got_error *
900 a367ff0f 2019-05-14 stsp check_same_branch(struct got_object_id *commit_id,
901 a51a74b3 2019-07-27 stsp struct got_reference *head_ref, struct got_object_id *yca_id,
902 a51a74b3 2019-07-27 stsp struct got_repository *repo)
903 a367ff0f 2019-05-14 stsp {
904 a367ff0f 2019-05-14 stsp const struct got_error *err = NULL;
905 a367ff0f 2019-05-14 stsp struct got_commit_graph *graph = NULL;
906 a367ff0f 2019-05-14 stsp struct got_object_id *head_commit_id = NULL;
907 a367ff0f 2019-05-14 stsp int is_same_branch = 0;
908 a367ff0f 2019-05-14 stsp
909 a367ff0f 2019-05-14 stsp err = got_ref_resolve(&head_commit_id, repo, head_ref);
910 a367ff0f 2019-05-14 stsp if (err)
911 a51a74b3 2019-07-27 stsp goto done;
912 a51a74b3 2019-07-27 stsp
913 a51a74b3 2019-07-27 stsp if (got_object_id_cmp(head_commit_id, commit_id) == 0) {
914 a51a74b3 2019-07-27 stsp is_same_branch = 1;
915 a51a74b3 2019-07-27 stsp goto done;
916 a51a74b3 2019-07-27 stsp }
917 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(commit_id, yca_id) == 0) {
918 a51a74b3 2019-07-27 stsp is_same_branch = 1;
919 a367ff0f 2019-05-14 stsp goto done;
920 a51a74b3 2019-07-27 stsp }
921 a367ff0f 2019-05-14 stsp
922 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
923 a367ff0f 2019-05-14 stsp if (err)
924 a367ff0f 2019-05-14 stsp goto done;
925 a367ff0f 2019-05-14 stsp
926 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, head_commit_id, repo,
927 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
928 a367ff0f 2019-05-14 stsp if (err)
929 a367ff0f 2019-05-14 stsp goto done;
930 a367ff0f 2019-05-14 stsp
931 a367ff0f 2019-05-14 stsp for (;;) {
932 a367ff0f 2019-05-14 stsp struct got_object_id *id;
933 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
934 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
935 a367ff0f 2019-05-14 stsp if (err) {
936 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
937 a367ff0f 2019-05-14 stsp err = NULL;
938 ee780d5c 2020-01-04 stsp break;
939 a367ff0f 2019-05-14 stsp }
940 c09a553d 2018-03-12 stsp
941 a367ff0f 2019-05-14 stsp if (id) {
942 a51a74b3 2019-07-27 stsp if (yca_id && got_object_id_cmp(id, yca_id) == 0)
943 a51a74b3 2019-07-27 stsp break;
944 a367ff0f 2019-05-14 stsp if (got_object_id_cmp(id, commit_id) == 0) {
945 a367ff0f 2019-05-14 stsp is_same_branch = 1;
946 a367ff0f 2019-05-14 stsp break;
947 a367ff0f 2019-05-14 stsp }
948 a367ff0f 2019-05-14 stsp }
949 a367ff0f 2019-05-14 stsp }
950 a367ff0f 2019-05-14 stsp done:
951 a367ff0f 2019-05-14 stsp if (graph)
952 a367ff0f 2019-05-14 stsp got_commit_graph_close(graph);
953 a367ff0f 2019-05-14 stsp free(head_commit_id);
954 a367ff0f 2019-05-14 stsp if (!err && !is_same_branch)
955 a367ff0f 2019-05-14 stsp err = got_error(GOT_ERR_ANCESTRY);
956 a367ff0f 2019-05-14 stsp return err;
957 a367ff0f 2019-05-14 stsp }
958 8069f636 2019-01-12 stsp
959 4ed7e80c 2018-05-20 stsp static const struct got_error *
960 4b6c9460 2020-03-05 stsp checkout_ancestry_error(struct got_reference *ref, const char *commit_id_str)
961 4b6c9460 2020-03-05 stsp {
962 4b6c9460 2020-03-05 stsp static char msg[512];
963 4b6c9460 2020-03-05 stsp const char *branch_name;
964 4b6c9460 2020-03-05 stsp
965 4b6c9460 2020-03-05 stsp if (got_ref_is_symbolic(ref))
966 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_symref_target(ref);
967 4b6c9460 2020-03-05 stsp else
968 4b6c9460 2020-03-05 stsp branch_name = got_ref_get_name(ref);
969 4b6c9460 2020-03-05 stsp
970 4b6c9460 2020-03-05 stsp if (strncmp("refs/heads/", branch_name, 11) == 0)
971 4b6c9460 2020-03-05 stsp branch_name += 11;
972 4b6c9460 2020-03-05 stsp
973 4b6c9460 2020-03-05 stsp snprintf(msg, sizeof(msg),
974 4b6c9460 2020-03-05 stsp "target commit is not contained in branch '%s'; "
975 4b6c9460 2020-03-05 stsp "the branch to use must be specified with -b; "
976 4b6c9460 2020-03-05 stsp "if necessary a new branch can be created for "
977 4b6c9460 2020-03-05 stsp "this commit with 'got branch -c %s BRANCH_NAME'",
978 4b6c9460 2020-03-05 stsp branch_name, commit_id_str);
979 4b6c9460 2020-03-05 stsp
980 4b6c9460 2020-03-05 stsp return got_error_msg(GOT_ERR_ANCESTRY, msg);
981 4b6c9460 2020-03-05 stsp }
982 4b6c9460 2020-03-05 stsp
983 4b6c9460 2020-03-05 stsp static const struct got_error *
984 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
985 c09a553d 2018-03-12 stsp {
986 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
987 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
988 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
989 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
990 c09a553d 2018-03-12 stsp char *repo_path = NULL;
991 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
992 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
993 08573d5b 2019-05-14 stsp const char *branch_name = GOT_REF_HEAD;
994 8069f636 2019-01-12 stsp char *commit_id_str = NULL;
995 bb51a5b4 2020-01-13 stsp int ch, same_path_prefix, allow_nonempty = 0;
996 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
997 7f47418f 2019-12-20 stsp struct got_checkout_progress_arg cpa;
998 f2ea84fa 2019-07-27 stsp
999 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1000 c09a553d 2018-03-12 stsp
1001 bb51a5b4 2020-01-13 stsp while ((ch = getopt(argc, argv, "b:c:Ep:")) != -1) {
1002 0bb8a95e 2018-03-12 stsp switch (ch) {
1003 08573d5b 2019-05-14 stsp case 'b':
1004 08573d5b 2019-05-14 stsp branch_name = optarg;
1005 08573d5b 2019-05-14 stsp break;
1006 8069f636 2019-01-12 stsp case 'c':
1007 8069f636 2019-01-12 stsp commit_id_str = strdup(optarg);
1008 8069f636 2019-01-12 stsp if (commit_id_str == NULL)
1009 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1010 bb51a5b4 2020-01-13 stsp break;
1011 bb51a5b4 2020-01-13 stsp case 'E':
1012 bb51a5b4 2020-01-13 stsp allow_nonempty = 1;
1013 8069f636 2019-01-12 stsp break;
1014 0bb8a95e 2018-03-12 stsp case 'p':
1015 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
1016 0bb8a95e 2018-03-12 stsp break;
1017 0bb8a95e 2018-03-12 stsp default:
1018 2deda0b9 2019-03-07 stsp usage_checkout();
1019 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
1020 0bb8a95e 2018-03-12 stsp }
1021 0bb8a95e 2018-03-12 stsp }
1022 0bb8a95e 2018-03-12 stsp
1023 0bb8a95e 2018-03-12 stsp argc -= optind;
1024 0bb8a95e 2018-03-12 stsp argv += optind;
1025 0bb8a95e 2018-03-12 stsp
1026 6715a751 2018-03-16 stsp #ifndef PROFILE
1027 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1028 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1029 c09a553d 2018-03-12 stsp err(1, "pledge");
1030 6715a751 2018-03-16 stsp #endif
1031 0bb8a95e 2018-03-12 stsp if (argc == 1) {
1032 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
1033 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1034 76089277 2018-04-01 stsp if (repo_path == NULL)
1035 638f9024 2019-05-13 stsp return got_error_from_errno2("realpath", argv[0]);
1036 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
1037 76089277 2018-04-01 stsp if (cwd == NULL) {
1038 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1039 76089277 2018-04-01 stsp goto done;
1040 76089277 2018-04-01 stsp }
1041 230a42bd 2019-05-11 jcs if (path_prefix[0]) {
1042 230a42bd 2019-05-11 jcs base = basename(path_prefix);
1043 230a42bd 2019-05-11 jcs if (base == NULL) {
1044 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1045 230a42bd 2019-05-11 jcs path_prefix);
1046 230a42bd 2019-05-11 jcs goto done;
1047 230a42bd 2019-05-11 jcs }
1048 230a42bd 2019-05-11 jcs } else {
1049 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
1050 230a42bd 2019-05-11 jcs if (base == NULL) {
1051 638f9024 2019-05-13 stsp error = got_error_from_errno2("basename",
1052 230a42bd 2019-05-11 jcs repo_path);
1053 230a42bd 2019-05-11 jcs goto done;
1054 230a42bd 2019-05-11 jcs }
1055 76089277 2018-04-01 stsp }
1056 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
1057 c09a553d 2018-03-12 stsp if (dotgit)
1058 c09a553d 2018-03-12 stsp *dotgit = '\0';
1059 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
1060 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
1061 c09a553d 2018-03-12 stsp free(cwd);
1062 76089277 2018-04-01 stsp goto done;
1063 c09a553d 2018-03-12 stsp }
1064 c09a553d 2018-03-12 stsp free(cwd);
1065 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
1066 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
1067 76089277 2018-04-01 stsp if (repo_path == NULL) {
1068 638f9024 2019-05-13 stsp error = got_error_from_errno2("realpath", argv[0]);
1069 76089277 2018-04-01 stsp goto done;
1070 76089277 2018-04-01 stsp }
1071 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
1072 76089277 2018-04-01 stsp if (worktree_path == NULL) {
1073 b4b3a7dd 2019-07-22 stsp if (errno != ENOENT) {
1074 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno2("realpath",
1075 b4b3a7dd 2019-07-22 stsp argv[1]);
1076 b4b3a7dd 2019-07-22 stsp goto done;
1077 b4b3a7dd 2019-07-22 stsp }
1078 b4b3a7dd 2019-07-22 stsp worktree_path = strdup(argv[1]);
1079 b4b3a7dd 2019-07-22 stsp if (worktree_path == NULL) {
1080 b4b3a7dd 2019-07-22 stsp error = got_error_from_errno("strdup");
1081 b4b3a7dd 2019-07-22 stsp goto done;
1082 b4b3a7dd 2019-07-22 stsp }
1083 76089277 2018-04-01 stsp }
1084 c09a553d 2018-03-12 stsp } else
1085 c09a553d 2018-03-12 stsp usage_checkout();
1086 c09a553d 2018-03-12 stsp
1087 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1088 72151b04 2019-05-11 stsp got_path_strip_trailing_slashes(worktree_path);
1089 13bfb272 2019-05-10 jcs
1090 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1091 c09a553d 2018-03-12 stsp if (error != NULL)
1092 c02c541e 2019-03-29 stsp goto done;
1093 c02c541e 2019-03-29 stsp
1094 c530dc23 2019-07-23 stsp /* Pre-create work tree path for unveil(2) */
1095 c530dc23 2019-07-23 stsp error = got_path_mkdir(worktree_path);
1096 c530dc23 2019-07-23 stsp if (error) {
1097 80c1b583 2019-08-07 stsp if (!(error->code == GOT_ERR_ERRNO && errno == EISDIR) &&
1098 80c1b583 2019-08-07 stsp !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1099 c530dc23 2019-07-23 stsp goto done;
1100 bb51a5b4 2020-01-13 stsp if (!allow_nonempty &&
1101 bb51a5b4 2020-01-13 stsp !got_path_dir_is_empty(worktree_path)) {
1102 c530dc23 2019-07-23 stsp error = got_error_path(worktree_path,
1103 c530dc23 2019-07-23 stsp GOT_ERR_DIR_NOT_EMPTY);
1104 c530dc23 2019-07-23 stsp goto done;
1105 c530dc23 2019-07-23 stsp }
1106 c530dc23 2019-07-23 stsp }
1107 c530dc23 2019-07-23 stsp
1108 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 0, worktree_path);
1109 c02c541e 2019-03-29 stsp if (error)
1110 c09a553d 2018-03-12 stsp goto done;
1111 8069f636 2019-01-12 stsp
1112 08573d5b 2019-05-14 stsp error = got_ref_open(&head_ref, repo, branch_name, 0);
1113 c09a553d 2018-03-12 stsp if (error != NULL)
1114 c09a553d 2018-03-12 stsp goto done;
1115 c09a553d 2018-03-12 stsp
1116 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
1117 d70b8e30 2018-12-27 stsp if (error != NULL && !(error->code == GOT_ERR_ERRNO && errno == EEXIST))
1118 c09a553d 2018-03-12 stsp goto done;
1119 c09a553d 2018-03-12 stsp
1120 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
1121 c09a553d 2018-03-12 stsp if (error != NULL)
1122 c09a553d 2018-03-12 stsp goto done;
1123 c09a553d 2018-03-12 stsp
1124 e5dc7198 2018-12-29 stsp error = got_worktree_match_path_prefix(&same_path_prefix, worktree,
1125 e5dc7198 2018-12-29 stsp path_prefix);
1126 e5dc7198 2018-12-29 stsp if (error != NULL)
1127 e5dc7198 2018-12-29 stsp goto done;
1128 e5dc7198 2018-12-29 stsp if (!same_path_prefix) {
1129 49520a32 2018-12-29 stsp error = got_error(GOT_ERR_PATH_PREFIX);
1130 49520a32 2018-12-29 stsp goto done;
1131 49520a32 2018-12-29 stsp }
1132 49520a32 2018-12-29 stsp
1133 8069f636 2019-01-12 stsp if (commit_id_str) {
1134 04f57cb3 2019-07-25 stsp struct got_object_id *commit_id;
1135 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1136 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1137 30837e32 2019-07-25 stsp if (error)
1138 8069f636 2019-01-12 stsp goto done;
1139 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1140 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1141 8069f636 2019-01-12 stsp if (error != NULL) {
1142 8069f636 2019-01-12 stsp free(commit_id);
1143 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1144 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1145 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1146 4b6c9460 2020-03-05 stsp }
1147 8069f636 2019-01-12 stsp goto done;
1148 8069f636 2019-01-12 stsp }
1149 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1150 4b6c9460 2020-03-05 stsp if (error) {
1151 4b6c9460 2020-03-05 stsp if (error->code == GOT_ERR_ANCESTRY) {
1152 4b6c9460 2020-03-05 stsp error = checkout_ancestry_error(
1153 4b6c9460 2020-03-05 stsp head_ref, commit_id_str);
1154 4b6c9460 2020-03-05 stsp }
1155 45d344f6 2019-05-14 stsp goto done;
1156 4b6c9460 2020-03-05 stsp }
1157 8069f636 2019-01-12 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1158 8069f636 2019-01-12 stsp commit_id);
1159 8069f636 2019-01-12 stsp free(commit_id);
1160 8069f636 2019-01-12 stsp if (error)
1161 8069f636 2019-01-12 stsp goto done;
1162 8069f636 2019-01-12 stsp }
1163 8069f636 2019-01-12 stsp
1164 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, "", NULL);
1165 f2ea84fa 2019-07-27 stsp if (error)
1166 f2ea84fa 2019-07-27 stsp goto done;
1167 7f47418f 2019-12-20 stsp cpa.worktree_path = worktree_path;
1168 7f47418f 2019-12-20 stsp cpa.had_base_commit_ref_error = 0;
1169 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1170 7f47418f 2019-12-20 stsp checkout_progress, &cpa, check_cancelled, NULL);
1171 c09a553d 2018-03-12 stsp if (error != NULL)
1172 c09a553d 2018-03-12 stsp goto done;
1173 c09a553d 2018-03-12 stsp
1174 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
1175 7f47418f 2019-12-20 stsp if (cpa.had_base_commit_ref_error)
1176 7f47418f 2019-12-20 stsp show_worktree_base_ref_warning();
1177 c09a553d 2018-03-12 stsp done:
1178 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1179 8069f636 2019-01-12 stsp free(commit_id_str);
1180 76089277 2018-04-01 stsp free(repo_path);
1181 507dc3bb 2018-12-29 stsp free(worktree_path);
1182 507dc3bb 2018-12-29 stsp return error;
1183 507dc3bb 2018-12-29 stsp }
1184 507dc3bb 2018-12-29 stsp
1185 507dc3bb 2018-12-29 stsp __dead static void
1186 507dc3bb 2018-12-29 stsp usage_update(void)
1187 507dc3bb 2018-12-29 stsp {
1188 f2ea84fa 2019-07-27 stsp fprintf(stderr, "usage: %s update [-b branch] [-c commit] [path ...]\n",
1189 507dc3bb 2018-12-29 stsp getprogname());
1190 507dc3bb 2018-12-29 stsp exit(1);
1191 507dc3bb 2018-12-29 stsp }
1192 507dc3bb 2018-12-29 stsp
1193 1ee397ad 2019-07-12 stsp static const struct got_error *
1194 507dc3bb 2018-12-29 stsp update_progress(void *arg, unsigned char status, const char *path)
1195 507dc3bb 2018-12-29 stsp {
1196 784955db 2019-01-12 stsp int *did_something = arg;
1197 784955db 2019-01-12 stsp
1198 7f47418f 2019-12-20 stsp if (status == GOT_STATUS_EXISTS ||
1199 7f47418f 2019-12-20 stsp status == GOT_STATUS_BASE_REF_ERR)
1200 1ee397ad 2019-07-12 stsp return NULL;
1201 507dc3bb 2018-12-29 stsp
1202 1545c615 2019-02-10 stsp *did_something = 1;
1203 a484d721 2019-06-10 stsp
1204 a484d721 2019-06-10 stsp /* Base commit bump happens silently. */
1205 a484d721 2019-06-10 stsp if (status == GOT_STATUS_BUMP_BASE)
1206 1ee397ad 2019-07-12 stsp return NULL;
1207 a484d721 2019-06-10 stsp
1208 507dc3bb 2018-12-29 stsp while (path[0] == '/')
1209 507dc3bb 2018-12-29 stsp path++;
1210 507dc3bb 2018-12-29 stsp printf("%c %s\n", status, path);
1211 1ee397ad 2019-07-12 stsp return NULL;
1212 be7061eb 2018-12-30 stsp }
1213 be7061eb 2018-12-30 stsp
1214 be7061eb 2018-12-30 stsp static const struct got_error *
1215 a1fb16d8 2019-05-24 stsp switch_head_ref(struct got_reference *head_ref,
1216 a1fb16d8 2019-05-24 stsp struct got_object_id *commit_id, struct got_worktree *worktree,
1217 a1fb16d8 2019-05-24 stsp struct got_repository *repo)
1218 a1fb16d8 2019-05-24 stsp {
1219 a1fb16d8 2019-05-24 stsp const struct got_error *err = NULL;
1220 a1fb16d8 2019-05-24 stsp char *base_id_str;
1221 a1fb16d8 2019-05-24 stsp int ref_has_moved = 0;
1222 a1fb16d8 2019-05-24 stsp
1223 a1fb16d8 2019-05-24 stsp /* Trivial case: switching between two different references. */
1224 a1fb16d8 2019-05-24 stsp if (strcmp(got_ref_get_name(head_ref),
1225 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree)) != 0) {
1226 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n",
1227 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree),
1228 a1fb16d8 2019-05-24 stsp got_ref_get_name(head_ref));
1229 a1fb16d8 2019-05-24 stsp return got_worktree_set_head_ref(worktree, head_ref);
1230 a1fb16d8 2019-05-24 stsp }
1231 a1fb16d8 2019-05-24 stsp
1232 a1fb16d8 2019-05-24 stsp err = check_linear_ancestry(commit_id,
1233 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1234 a1fb16d8 2019-05-24 stsp if (err) {
1235 a1fb16d8 2019-05-24 stsp if (err->code != GOT_ERR_ANCESTRY)
1236 a1fb16d8 2019-05-24 stsp return err;
1237 a1fb16d8 2019-05-24 stsp ref_has_moved = 1;
1238 a1fb16d8 2019-05-24 stsp }
1239 a1fb16d8 2019-05-24 stsp if (!ref_has_moved)
1240 a1fb16d8 2019-05-24 stsp return NULL;
1241 a1fb16d8 2019-05-24 stsp
1242 a1fb16d8 2019-05-24 stsp /* Switching to a rebased branch with the same reference name. */
1243 a1fb16d8 2019-05-24 stsp err = got_object_id_str(&base_id_str,
1244 a1fb16d8 2019-05-24 stsp got_worktree_get_base_commit_id(worktree));
1245 a1fb16d8 2019-05-24 stsp if (err)
1246 a1fb16d8 2019-05-24 stsp return err;
1247 a1fb16d8 2019-05-24 stsp printf("Reference %s now points at a different branch\n",
1248 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1249 a1fb16d8 2019-05-24 stsp printf("Switching work tree from %s to %s\n", base_id_str,
1250 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree));
1251 0ebf8283 2019-07-24 stsp return NULL;
1252 0ebf8283 2019-07-24 stsp }
1253 0ebf8283 2019-07-24 stsp
1254 0ebf8283 2019-07-24 stsp static const struct got_error *
1255 0ebf8283 2019-07-24 stsp check_rebase_or_histedit_in_progress(struct got_worktree *worktree)
1256 0ebf8283 2019-07-24 stsp {
1257 0ebf8283 2019-07-24 stsp const struct got_error *err;
1258 0ebf8283 2019-07-24 stsp int in_progress;
1259 0ebf8283 2019-07-24 stsp
1260 0ebf8283 2019-07-24 stsp err = got_worktree_rebase_in_progress(&in_progress, worktree);
1261 0ebf8283 2019-07-24 stsp if (err)
1262 0ebf8283 2019-07-24 stsp return err;
1263 0ebf8283 2019-07-24 stsp if (in_progress)
1264 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_REBASING);
1265 0ebf8283 2019-07-24 stsp
1266 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_in_progress(&in_progress, worktree);
1267 0ebf8283 2019-07-24 stsp if (err)
1268 0ebf8283 2019-07-24 stsp return err;
1269 0ebf8283 2019-07-24 stsp if (in_progress)
1270 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_BUSY);
1271 0ebf8283 2019-07-24 stsp
1272 a1fb16d8 2019-05-24 stsp return NULL;
1273 a5edda0a 2019-07-27 stsp }
1274 a5edda0a 2019-07-27 stsp
1275 a5edda0a 2019-07-27 stsp static const struct got_error *
1276 a5edda0a 2019-07-27 stsp get_worktree_paths_from_argv(struct got_pathlist_head *paths, int argc,
1277 a5edda0a 2019-07-27 stsp char *argv[], struct got_worktree *worktree)
1278 a5edda0a 2019-07-27 stsp {
1279 a0de39f3 2019-08-09 stsp const struct got_error *err = NULL;
1280 a5edda0a 2019-07-27 stsp char *path;
1281 a5edda0a 2019-07-27 stsp int i;
1282 a5edda0a 2019-07-27 stsp
1283 a5edda0a 2019-07-27 stsp if (argc == 0) {
1284 a5edda0a 2019-07-27 stsp path = strdup("");
1285 a5edda0a 2019-07-27 stsp if (path == NULL)
1286 a5edda0a 2019-07-27 stsp return got_error_from_errno("strdup");
1287 adc19d55 2019-07-28 stsp return got_pathlist_append(paths, path, NULL);
1288 a5edda0a 2019-07-27 stsp }
1289 a5edda0a 2019-07-27 stsp
1290 a5edda0a 2019-07-27 stsp for (i = 0; i < argc; i++) {
1291 a5edda0a 2019-07-27 stsp err = got_worktree_resolve_path(&path, worktree, argv[i]);
1292 a5edda0a 2019-07-27 stsp if (err)
1293 a5edda0a 2019-07-27 stsp break;
1294 adc19d55 2019-07-28 stsp err = got_pathlist_append(paths, path, NULL);
1295 a5edda0a 2019-07-27 stsp if (err) {
1296 a5edda0a 2019-07-27 stsp free(path);
1297 a5edda0a 2019-07-27 stsp break;
1298 a5edda0a 2019-07-27 stsp }
1299 a5edda0a 2019-07-27 stsp }
1300 a5edda0a 2019-07-27 stsp
1301 a5edda0a 2019-07-27 stsp return err;
1302 a1fb16d8 2019-05-24 stsp }
1303 a1fb16d8 2019-05-24 stsp
1304 a1fb16d8 2019-05-24 stsp static const struct got_error *
1305 507dc3bb 2018-12-29 stsp cmd_update(int argc, char *argv[])
1306 507dc3bb 2018-12-29 stsp {
1307 507dc3bb 2018-12-29 stsp const struct got_error *error = NULL;
1308 507dc3bb 2018-12-29 stsp struct got_repository *repo = NULL;
1309 507dc3bb 2018-12-29 stsp struct got_worktree *worktree = NULL;
1310 f2ea84fa 2019-07-27 stsp char *worktree_path = NULL;
1311 507dc3bb 2018-12-29 stsp struct got_object_id *commit_id = NULL;
1312 9c4b8182 2019-01-02 stsp char *commit_id_str = NULL;
1313 024e9686 2019-05-14 stsp const char *branch_name = NULL;
1314 024e9686 2019-05-14 stsp struct got_reference *head_ref = NULL;
1315 f2ea84fa 2019-07-27 stsp struct got_pathlist_head paths;
1316 f2ea84fa 2019-07-27 stsp struct got_pathlist_entry *pe;
1317 0ebf8283 2019-07-24 stsp int ch, did_something = 0;
1318 507dc3bb 2018-12-29 stsp
1319 f2ea84fa 2019-07-27 stsp TAILQ_INIT(&paths);
1320 f2ea84fa 2019-07-27 stsp
1321 024e9686 2019-05-14 stsp while ((ch = getopt(argc, argv, "b:c:")) != -1) {
1322 507dc3bb 2018-12-29 stsp switch (ch) {
1323 024e9686 2019-05-14 stsp case 'b':
1324 024e9686 2019-05-14 stsp branch_name = optarg;
1325 024e9686 2019-05-14 stsp break;
1326 507dc3bb 2018-12-29 stsp case 'c':
1327 9c4b8182 2019-01-02 stsp commit_id_str = strdup(optarg);
1328 9c4b8182 2019-01-02 stsp if (commit_id_str == NULL)
1329 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
1330 507dc3bb 2018-12-29 stsp break;
1331 507dc3bb 2018-12-29 stsp default:
1332 2deda0b9 2019-03-07 stsp usage_update();
1333 507dc3bb 2018-12-29 stsp /* NOTREACHED */
1334 507dc3bb 2018-12-29 stsp }
1335 507dc3bb 2018-12-29 stsp }
1336 507dc3bb 2018-12-29 stsp
1337 507dc3bb 2018-12-29 stsp argc -= optind;
1338 507dc3bb 2018-12-29 stsp argv += optind;
1339 507dc3bb 2018-12-29 stsp
1340 507dc3bb 2018-12-29 stsp #ifndef PROFILE
1341 68ed9ba5 2019-02-10 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
1342 68ed9ba5 2019-02-10 stsp "unveil", NULL) == -1)
1343 507dc3bb 2018-12-29 stsp err(1, "pledge");
1344 507dc3bb 2018-12-29 stsp #endif
1345 c4cdcb68 2019-04-03 stsp worktree_path = getcwd(NULL, 0);
1346 c4cdcb68 2019-04-03 stsp if (worktree_path == NULL) {
1347 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1348 c4cdcb68 2019-04-03 stsp goto done;
1349 c4cdcb68 2019-04-03 stsp }
1350 c4cdcb68 2019-04-03 stsp error = got_worktree_open(&worktree, worktree_path);
1351 7d5807f4 2019-07-11 stsp if (error)
1352 7d5807f4 2019-07-11 stsp goto done;
1353 7d5807f4 2019-07-11 stsp
1354 0ebf8283 2019-07-24 stsp error = check_rebase_or_histedit_in_progress(worktree);
1355 f2ea84fa 2019-07-27 stsp if (error)
1356 f2ea84fa 2019-07-27 stsp goto done;
1357 507dc3bb 2018-12-29 stsp
1358 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
1359 c9956ddf 2019-09-08 stsp NULL);
1360 507dc3bb 2018-12-29 stsp if (error != NULL)
1361 507dc3bb 2018-12-29 stsp goto done;
1362 507dc3bb 2018-12-29 stsp
1363 97430839 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
1364 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
1365 087fb88c 2019-08-04 stsp if (error)
1366 087fb88c 2019-08-04 stsp goto done;
1367 087fb88c 2019-08-04 stsp
1368 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
1369 0266afb7 2019-01-04 stsp if (error)
1370 0266afb7 2019-01-04 stsp goto done;
1371 0266afb7 2019-01-04 stsp
1372 a1fb16d8 2019-05-24 stsp error = got_ref_open(&head_ref, repo, branch_name ? branch_name :
1373 a1fb16d8 2019-05-24 stsp got_worktree_get_head_ref_name(worktree), 0);
1374 024e9686 2019-05-14 stsp if (error != NULL)
1375 024e9686 2019-05-14 stsp goto done;
1376 507dc3bb 2018-12-29 stsp if (commit_id_str == NULL) {
1377 507dc3bb 2018-12-29 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
1378 9c4b8182 2019-01-02 stsp if (error != NULL)
1379 9c4b8182 2019-01-02 stsp goto done;
1380 9c4b8182 2019-01-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
1381 507dc3bb 2018-12-29 stsp if (error != NULL)
1382 507dc3bb 2018-12-29 stsp goto done;
1383 507dc3bb 2018-12-29 stsp } else {
1384 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
1385 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
1386 04f57cb3 2019-07-25 stsp free(commit_id_str);
1387 bb787f09 2019-07-25 stsp commit_id_str = NULL;
1388 30837e32 2019-07-25 stsp if (error)
1389 a297e751 2019-07-11 stsp goto done;
1390 a297e751 2019-07-11 stsp error = got_object_id_str(&commit_id_str, commit_id);
1391 a297e751 2019-07-11 stsp if (error)
1392 507dc3bb 2018-12-29 stsp goto done;
1393 507dc3bb 2018-12-29 stsp }
1394 35c965b2 2018-12-29 stsp
1395 a1fb16d8 2019-05-24 stsp if (branch_name) {
1396 024e9686 2019-05-14 stsp struct got_object_id *head_commit_id;
1397 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry) {
1398 f2b16ada 2019-08-02 stsp if (pe->path_len == 0)
1399 f2ea84fa 2019-07-27 stsp continue;
1400 f2ea84fa 2019-07-27 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
1401 f2ea84fa 2019-07-27 stsp "switching between branches requires that "
1402 f2ea84fa 2019-07-27 stsp "the entire work tree gets updated");
1403 024e9686 2019-05-14 stsp goto done;
1404 024e9686 2019-05-14 stsp }
1405 024e9686 2019-05-14 stsp error = got_ref_resolve(&head_commit_id, repo, head_ref);
1406 024e9686 2019-05-14 stsp if (error)
1407 024e9686 2019-05-14 stsp goto done;
1408 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, head_commit_id, 0,
1409 3aef623b 2019-10-15 stsp repo);
1410 a367ff0f 2019-05-14 stsp free(head_commit_id);
1411 024e9686 2019-05-14 stsp if (error != NULL)
1412 024e9686 2019-05-14 stsp goto done;
1413 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1414 a367ff0f 2019-05-14 stsp if (error)
1415 a367ff0f 2019-05-14 stsp goto done;
1416 a1fb16d8 2019-05-24 stsp error = switch_head_ref(head_ref, commit_id, worktree, repo);
1417 024e9686 2019-05-14 stsp if (error)
1418 024e9686 2019-05-14 stsp goto done;
1419 024e9686 2019-05-14 stsp } else {
1420 024e9686 2019-05-14 stsp error = check_linear_ancestry(commit_id,
1421 3aef623b 2019-10-15 stsp got_worktree_get_base_commit_id(worktree), 0, repo);
1422 a367ff0f 2019-05-14 stsp if (error != NULL) {
1423 a367ff0f 2019-05-14 stsp if (error->code == GOT_ERR_ANCESTRY)
1424 a367ff0f 2019-05-14 stsp error = got_error(GOT_ERR_BRANCH_MOVED);
1425 024e9686 2019-05-14 stsp goto done;
1426 a367ff0f 2019-05-14 stsp }
1427 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
1428 a367ff0f 2019-05-14 stsp if (error)
1429 a367ff0f 2019-05-14 stsp goto done;
1430 024e9686 2019-05-14 stsp }
1431 507dc3bb 2018-12-29 stsp
1432 507dc3bb 2018-12-29 stsp if (got_object_id_cmp(got_worktree_get_base_commit_id(worktree),
1433 507dc3bb 2018-12-29 stsp commit_id) != 0) {
1434 507dc3bb 2018-12-29 stsp error = got_worktree_set_base_commit_id(worktree, repo,
1435 507dc3bb 2018-12-29 stsp commit_id);
1436 507dc3bb 2018-12-29 stsp if (error)
1437 507dc3bb 2018-12-29 stsp goto done;
1438 507dc3bb 2018-12-29 stsp }
1439 507dc3bb 2018-12-29 stsp
1440 f2ea84fa 2019-07-27 stsp error = got_worktree_checkout_files(worktree, &paths, repo,
1441 6bad629b 2019-02-04 stsp update_progress, &did_something, check_cancelled, NULL);
1442 507dc3bb 2018-12-29 stsp if (error != NULL)
1443 507dc3bb 2018-12-29 stsp goto done;
1444 9c4b8182 2019-01-02 stsp
1445 784955db 2019-01-12 stsp if (did_something)
1446 784955db 2019-01-12 stsp printf("Updated to commit %s\n", commit_id_str);
1447 784955db 2019-01-12 stsp else
1448 784955db 2019-01-12 stsp printf("Already up-to-date\n");
1449 507dc3bb 2018-12-29 stsp done:
1450 c09a553d 2018-03-12 stsp free(worktree_path);
1451 f2ea84fa 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
1452 f2ea84fa 2019-07-27 stsp free((char *)pe->path);
1453 f2ea84fa 2019-07-27 stsp got_pathlist_free(&paths);
1454 507dc3bb 2018-12-29 stsp free(commit_id);
1455 9c4b8182 2019-01-02 stsp free(commit_id_str);
1456 c09a553d 2018-03-12 stsp return error;
1457 c09a553d 2018-03-12 stsp }
1458 c09a553d 2018-03-12 stsp
1459 f42b1b34 2018-03-12 stsp static const struct got_error *
1460 44392932 2019-08-25 stsp diff_blobs(struct got_object_id *blob_id1, struct got_object_id *blob_id2,
1461 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1462 63035f9f 2019-10-06 stsp struct got_repository *repo)
1463 5c860e29 2018-03-12 stsp {
1464 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
1465 44392932 2019-08-25 stsp struct got_blob_object *blob1 = NULL, *blob2 = NULL;
1466 79109fed 2018-03-27 stsp
1467 44392932 2019-08-25 stsp if (blob_id1) {
1468 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob1, repo, blob_id1, 8192);
1469 44392932 2019-08-25 stsp if (err)
1470 44392932 2019-08-25 stsp goto done;
1471 44392932 2019-08-25 stsp }
1472 79109fed 2018-03-27 stsp
1473 44392932 2019-08-25 stsp err = got_object_open_as_blob(&blob2, repo, blob_id2, 8192);
1474 44392932 2019-08-25 stsp if (err)
1475 44392932 2019-08-25 stsp goto done;
1476 79109fed 2018-03-27 stsp
1477 44392932 2019-08-25 stsp while (path[0] == '/')
1478 44392932 2019-08-25 stsp path++;
1479 44392932 2019-08-25 stsp err = got_diff_blob(blob1, blob2, path, path, diff_context,
1480 63035f9f 2019-10-06 stsp ignore_whitespace, stdout);
1481 44392932 2019-08-25 stsp done:
1482 44392932 2019-08-25 stsp if (blob1)
1483 44392932 2019-08-25 stsp got_object_blob_close(blob1);
1484 44392932 2019-08-25 stsp got_object_blob_close(blob2);
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 diff_trees(struct got_object_id *tree_id1, struct got_object_id *tree_id2,
1490 63035f9f 2019-10-06 stsp const char *path, int diff_context, int ignore_whitespace,
1491 63035f9f 2019-10-06 stsp struct got_repository *repo)
1492 44392932 2019-08-25 stsp {
1493 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1494 44392932 2019-08-25 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1495 44392932 2019-08-25 stsp struct got_diff_blob_output_unidiff_arg arg;
1496 44392932 2019-08-25 stsp
1497 44392932 2019-08-25 stsp if (tree_id1) {
1498 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree1, repo, tree_id1);
1499 79109fed 2018-03-27 stsp if (err)
1500 44392932 2019-08-25 stsp goto done;
1501 79109fed 2018-03-27 stsp }
1502 79109fed 2018-03-27 stsp
1503 44392932 2019-08-25 stsp err = got_object_open_as_tree(&tree2, repo, tree_id2);
1504 0f2b3dca 2018-12-22 stsp if (err)
1505 0f2b3dca 2018-12-22 stsp goto done;
1506 0f2b3dca 2018-12-22 stsp
1507 aaa13589 2019-06-01 stsp arg.diff_context = diff_context;
1508 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
1509 aaa13589 2019-06-01 stsp arg.outfile = stdout;
1510 44392932 2019-08-25 stsp while (path[0] == '/')
1511 44392932 2019-08-25 stsp path++;
1512 44392932 2019-08-25 stsp err = got_diff_tree(tree1, tree2, path, path, repo,
1513 31b4484f 2019-07-27 stsp got_diff_blob_output_unidiff, &arg, 1);
1514 0f2b3dca 2018-12-22 stsp done:
1515 79109fed 2018-03-27 stsp if (tree1)
1516 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
1517 366e0a5f 2019-10-10 stsp if (tree2)
1518 366e0a5f 2019-10-10 stsp got_object_tree_close(tree2);
1519 44392932 2019-08-25 stsp return err;
1520 44392932 2019-08-25 stsp }
1521 44392932 2019-08-25 stsp
1522 44392932 2019-08-25 stsp static const struct got_error *
1523 44392932 2019-08-25 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
1524 44392932 2019-08-25 stsp const char *path, int diff_context, struct got_repository *repo)
1525 44392932 2019-08-25 stsp {
1526 44392932 2019-08-25 stsp const struct got_error *err = NULL;
1527 44392932 2019-08-25 stsp struct got_commit_object *pcommit = NULL;
1528 44392932 2019-08-25 stsp char *id_str1 = NULL, *id_str2 = NULL;
1529 44392932 2019-08-25 stsp struct got_object_id *obj_id1 = NULL, *obj_id2 = NULL;
1530 44392932 2019-08-25 stsp struct got_object_qid *qid;
1531 44392932 2019-08-25 stsp
1532 44392932 2019-08-25 stsp qid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
1533 44392932 2019-08-25 stsp if (qid != NULL) {
1534 44392932 2019-08-25 stsp err = got_object_open_as_commit(&pcommit, repo,
1535 44392932 2019-08-25 stsp qid->id);
1536 44392932 2019-08-25 stsp if (err)
1537 44392932 2019-08-25 stsp return err;
1538 44392932 2019-08-25 stsp }
1539 44392932 2019-08-25 stsp
1540 44392932 2019-08-25 stsp if (path && path[0] != '\0') {
1541 44392932 2019-08-25 stsp int obj_type;
1542 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id2, repo, id, path);
1543 44392932 2019-08-25 stsp if (err)
1544 44392932 2019-08-25 stsp goto done;
1545 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1546 44392932 2019-08-25 stsp if (err) {
1547 44392932 2019-08-25 stsp free(obj_id2);
1548 44392932 2019-08-25 stsp goto done;
1549 44392932 2019-08-25 stsp }
1550 44392932 2019-08-25 stsp if (pcommit) {
1551 44392932 2019-08-25 stsp err = got_object_id_by_path(&obj_id1, repo,
1552 44392932 2019-08-25 stsp qid->id, path);
1553 44392932 2019-08-25 stsp if (err) {
1554 44392932 2019-08-25 stsp free(obj_id2);
1555 44392932 2019-08-25 stsp goto done;
1556 44392932 2019-08-25 stsp }
1557 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1558 44392932 2019-08-25 stsp if (err) {
1559 44392932 2019-08-25 stsp free(obj_id2);
1560 44392932 2019-08-25 stsp goto done;
1561 44392932 2019-08-25 stsp }
1562 44392932 2019-08-25 stsp }
1563 44392932 2019-08-25 stsp err = got_object_get_type(&obj_type, repo, obj_id2);
1564 44392932 2019-08-25 stsp if (err) {
1565 44392932 2019-08-25 stsp free(obj_id2);
1566 44392932 2019-08-25 stsp goto done;
1567 44392932 2019-08-25 stsp }
1568 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1569 44392932 2019-08-25 stsp switch (obj_type) {
1570 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_BLOB:
1571 44392932 2019-08-25 stsp err = diff_blobs(obj_id1, obj_id2, path, diff_context,
1572 63035f9f 2019-10-06 stsp 0, repo);
1573 44392932 2019-08-25 stsp break;
1574 44392932 2019-08-25 stsp case GOT_OBJ_TYPE_TREE:
1575 44392932 2019-08-25 stsp err = diff_trees(obj_id1, obj_id2, path, diff_context,
1576 63035f9f 2019-10-06 stsp 0, repo);
1577 44392932 2019-08-25 stsp break;
1578 44392932 2019-08-25 stsp default:
1579 44392932 2019-08-25 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1580 44392932 2019-08-25 stsp break;
1581 44392932 2019-08-25 stsp }
1582 44392932 2019-08-25 stsp free(obj_id1);
1583 44392932 2019-08-25 stsp free(obj_id2);
1584 44392932 2019-08-25 stsp } else {
1585 44392932 2019-08-25 stsp obj_id2 = got_object_commit_get_tree_id(commit);
1586 44392932 2019-08-25 stsp err = got_object_id_str(&id_str2, obj_id2);
1587 44392932 2019-08-25 stsp if (err)
1588 44392932 2019-08-25 stsp goto done;
1589 44392932 2019-08-25 stsp obj_id1 = got_object_commit_get_tree_id(pcommit);
1590 44392932 2019-08-25 stsp err = got_object_id_str(&id_str1, obj_id1);
1591 44392932 2019-08-25 stsp if (err)
1592 44392932 2019-08-25 stsp goto done;
1593 44392932 2019-08-25 stsp printf("diff %s %s\n", id_str1 ? id_str1 : "/dev/null", id_str2);
1594 63035f9f 2019-10-06 stsp err = diff_trees(obj_id1, obj_id2, "", diff_context, 0, repo);
1595 44392932 2019-08-25 stsp }
1596 44392932 2019-08-25 stsp done:
1597 0f2b3dca 2018-12-22 stsp free(id_str1);
1598 0f2b3dca 2018-12-22 stsp free(id_str2);
1599 44392932 2019-08-25 stsp if (pcommit)
1600 44392932 2019-08-25 stsp got_object_commit_close(pcommit);
1601 79109fed 2018-03-27 stsp return err;
1602 79109fed 2018-03-27 stsp }
1603 79109fed 2018-03-27 stsp
1604 4bb494d5 2018-06-16 stsp static char *
1605 6c281f94 2018-06-11 stsp get_datestr(time_t *time, char *datebuf)
1606 6c281f94 2018-06-11 stsp {
1607 09867e48 2019-08-13 stsp struct tm mytm, *tm;
1608 09867e48 2019-08-13 stsp char *p, *s;
1609 09867e48 2019-08-13 stsp
1610 09867e48 2019-08-13 stsp tm = gmtime_r(time, &mytm);
1611 09867e48 2019-08-13 stsp if (tm == NULL)
1612 09867e48 2019-08-13 stsp return NULL;
1613 09867e48 2019-08-13 stsp s = asctime_r(tm, datebuf);
1614 09867e48 2019-08-13 stsp if (s == NULL)
1615 09867e48 2019-08-13 stsp return NULL;
1616 6c281f94 2018-06-11 stsp p = strchr(s, '\n');
1617 6c281f94 2018-06-11 stsp if (p)
1618 6c281f94 2018-06-11 stsp *p = '\0';
1619 6c281f94 2018-06-11 stsp return s;
1620 6c281f94 2018-06-11 stsp }
1621 dc424a06 2019-08-07 stsp
1622 6841bf13 2019-11-29 kn static const struct got_error *
1623 6841bf13 2019-11-29 kn match_logmsg(int *have_match, struct got_object_id *id,
1624 6841bf13 2019-11-29 kn struct got_commit_object *commit, regex_t *regex)
1625 6841bf13 2019-11-29 kn {
1626 6841bf13 2019-11-29 kn const struct got_error *err = NULL;
1627 6841bf13 2019-11-29 kn regmatch_t regmatch;
1628 6841bf13 2019-11-29 kn char *id_str = NULL, *logmsg = NULL;
1629 6841bf13 2019-11-29 kn
1630 6841bf13 2019-11-29 kn *have_match = 0;
1631 6841bf13 2019-11-29 kn
1632 6841bf13 2019-11-29 kn err = got_object_id_str(&id_str, id);
1633 6841bf13 2019-11-29 kn if (err)
1634 6841bf13 2019-11-29 kn return err;
1635 6841bf13 2019-11-29 kn
1636 6841bf13 2019-11-29 kn err = got_object_commit_get_logmsg(&logmsg, commit);
1637 6841bf13 2019-11-29 kn if (err)
1638 6841bf13 2019-11-29 kn goto done;
1639 6841bf13 2019-11-29 kn
1640 6841bf13 2019-11-29 kn if (regexec(regex, logmsg, 1, &regmatch, 0) == 0)
1641 6841bf13 2019-11-29 kn *have_match = 1;
1642 6841bf13 2019-11-29 kn done:
1643 6841bf13 2019-11-29 kn free(id_str);
1644 6841bf13 2019-11-29 kn free(logmsg);
1645 6841bf13 2019-11-29 kn return err;
1646 6841bf13 2019-11-29 kn }
1647 6841bf13 2019-11-29 kn
1648 dc424a06 2019-08-07 stsp #define GOT_COMMIT_SEP_STR "-----------------------------------------------\n"
1649 6c281f94 2018-06-11 stsp
1650 79109fed 2018-03-27 stsp static const struct got_error *
1651 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
1652 44392932 2019-08-25 stsp struct got_repository *repo, const char *path, int show_patch,
1653 44392932 2019-08-25 stsp int diff_context, struct got_reflist_head *refs)
1654 79109fed 2018-03-27 stsp {
1655 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
1656 621015ac 2018-07-23 stsp char *id_str, *datestr, *logmsg0, *logmsg, *line;
1657 6c281f94 2018-06-11 stsp char datebuf[26];
1658 45d799e2 2018-12-23 stsp time_t committer_time;
1659 45d799e2 2018-12-23 stsp const char *author, *committer;
1660 199a4027 2019-02-02 stsp char *refs_str = NULL;
1661 199a4027 2019-02-02 stsp struct got_reflist_entry *re;
1662 5c860e29 2018-03-12 stsp
1663 199a4027 2019-02-02 stsp SIMPLEQ_FOREACH(re, refs, entry) {
1664 199a4027 2019-02-02 stsp char *s;
1665 199a4027 2019-02-02 stsp const char *name;
1666 a436ad14 2019-08-13 stsp struct got_tag_object *tag = NULL;
1667 a436ad14 2019-08-13 stsp int cmp;
1668 a436ad14 2019-08-13 stsp
1669 199a4027 2019-02-02 stsp name = got_ref_get_name(re->ref);
1670 d9498b20 2019-02-05 stsp if (strcmp(name, GOT_REF_HEAD) == 0)
1671 d9498b20 2019-02-05 stsp continue;
1672 199a4027 2019-02-02 stsp if (strncmp(name, "refs/", 5) == 0)
1673 199a4027 2019-02-02 stsp name += 5;
1674 7143d404 2019-03-12 stsp if (strncmp(name, "got/", 4) == 0)
1675 7143d404 2019-03-12 stsp continue;
1676 e34f9ed6 2019-02-02 stsp if (strncmp(name, "heads/", 6) == 0)
1677 e34f9ed6 2019-02-02 stsp name += 6;
1678 141c2bff 2019-02-04 stsp if (strncmp(name, "remotes/", 8) == 0)
1679 141c2bff 2019-02-04 stsp name += 8;
1680 a436ad14 2019-08-13 stsp if (strncmp(name, "tags/", 5) == 0) {
1681 a436ad14 2019-08-13 stsp err = got_object_open_as_tag(&tag, repo, re->id);
1682 5d844a1e 2019-08-13 stsp if (err) {
1683 5d844a1e 2019-08-13 stsp if (err->code != GOT_ERR_OBJ_TYPE)
1684 5d844a1e 2019-08-13 stsp return err;
1685 5d844a1e 2019-08-13 stsp /* Ref points at something other than a tag. */
1686 5d844a1e 2019-08-13 stsp err = NULL;
1687 5d844a1e 2019-08-13 stsp tag = NULL;
1688 5d844a1e 2019-08-13 stsp }
1689 a436ad14 2019-08-13 stsp }
1690 a436ad14 2019-08-13 stsp cmp = got_object_id_cmp(tag ?
1691 a436ad14 2019-08-13 stsp got_object_tag_get_object_id(tag) : re->id, id);
1692 a436ad14 2019-08-13 stsp if (tag)
1693 a436ad14 2019-08-13 stsp got_object_tag_close(tag);
1694 a436ad14 2019-08-13 stsp if (cmp != 0)
1695 a436ad14 2019-08-13 stsp continue;
1696 199a4027 2019-02-02 stsp s = refs_str;
1697 199a4027 2019-02-02 stsp if (asprintf(&refs_str, "%s%s%s", s ? s : "", s ? ", " : "",
1698 199a4027 2019-02-02 stsp name) == -1) {
1699 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
1700 199a4027 2019-02-02 stsp free(s);
1701 34ca4898 2019-08-13 stsp return err;
1702 199a4027 2019-02-02 stsp }
1703 199a4027 2019-02-02 stsp free(s);
1704 199a4027 2019-02-02 stsp }
1705 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
1706 8bf5b3c9 2018-03-17 stsp if (err)
1707 8bf5b3c9 2018-03-17 stsp return err;
1708 788c352e 2018-06-16 stsp
1709 dc424a06 2019-08-07 stsp printf(GOT_COMMIT_SEP_STR);
1710 199a4027 2019-02-02 stsp printf("commit %s%s%s%s\n", id_str, refs_str ? " (" : "",
1711 199a4027 2019-02-02 stsp refs_str ? refs_str : "", refs_str ? ")" : "");
1712 832c249c 2018-06-10 stsp free(id_str);
1713 d3d493d7 2019-02-21 stsp id_str = NULL;
1714 d3d493d7 2019-02-21 stsp free(refs_str);
1715 d3d493d7 2019-02-21 stsp refs_str = NULL;
1716 45d799e2 2018-12-23 stsp printf("from: %s\n", got_object_commit_get_author(commit));
1717 45d799e2 2018-12-23 stsp committer_time = got_object_commit_get_committer_time(commit);
1718 45d799e2 2018-12-23 stsp datestr = get_datestr(&committer_time, datebuf);
1719 09867e48 2019-08-13 stsp if (datestr)
1720 09867e48 2019-08-13 stsp printf("date: %s UTC\n", datestr);
1721 45d799e2 2018-12-23 stsp author = got_object_commit_get_author(commit);
1722 45d799e2 2018-12-23 stsp committer = got_object_commit_get_committer(commit);
1723 45d799e2 2018-12-23 stsp if (strcmp(author, committer) != 0)
1724 45d799e2 2018-12-23 stsp printf("via: %s\n", committer);
1725 45d799e2 2018-12-23 stsp if (got_object_commit_get_nparents(commit) > 1) {
1726 45d799e2 2018-12-23 stsp const struct got_object_id_queue *parent_ids;
1727 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1728 3fe1abad 2018-06-10 stsp int n = 1;
1729 45d799e2 2018-12-23 stsp parent_ids = got_object_commit_get_parent_ids(commit);
1730 45d799e2 2018-12-23 stsp SIMPLEQ_FOREACH(qid, parent_ids, entry) {
1731 79f35eb3 2018-06-11 stsp err = got_object_id_str(&id_str, qid->id);
1732 a0603db2 2018-06-10 stsp if (err)
1733 a0603db2 2018-06-10 stsp return err;
1734 3fe1abad 2018-06-10 stsp printf("parent %d: %s\n", n++, id_str);
1735 a0603db2 2018-06-10 stsp free(id_str);
1736 a0603db2 2018-06-10 stsp }
1737 a0603db2 2018-06-10 stsp }
1738 832c249c 2018-06-10 stsp
1739 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
1740 5943eee2 2019-08-13 stsp if (err)
1741 5943eee2 2019-08-13 stsp return err;
1742 8bf5b3c9 2018-03-17 stsp
1743 621015ac 2018-07-23 stsp logmsg = logmsg0;
1744 832c249c 2018-06-10 stsp do {
1745 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
1746 832c249c 2018-06-10 stsp if (line)
1747 832c249c 2018-06-10 stsp printf(" %s\n", line);
1748 832c249c 2018-06-10 stsp } while (line);
1749 621015ac 2018-07-23 stsp free(logmsg0);
1750 832c249c 2018-06-10 stsp
1751 971751ac 2018-03-27 stsp if (show_patch) {
1752 44392932 2019-08-25 stsp err = print_patch(commit, id, path, diff_context, repo);
1753 971751ac 2018-03-27 stsp if (err == 0)
1754 971751ac 2018-03-27 stsp printf("\n");
1755 971751ac 2018-03-27 stsp }
1756 07862c20 2018-09-15 stsp
1757 cbe7f848 2019-02-11 stsp if (fflush(stdout) != 0 && err == NULL)
1758 638f9024 2019-05-13 stsp err = got_error_from_errno("fflush");
1759 07862c20 2018-09-15 stsp return err;
1760 f42b1b34 2018-03-12 stsp }
1761 5c860e29 2018-03-12 stsp
1762 f42b1b34 2018-03-12 stsp static const struct got_error *
1763 15a94983 2018-12-23 stsp print_commits(struct got_object_id *root_id, struct got_repository *repo,
1764 463a997a 2019-12-08 kn const char *path, int show_patch, const char *search_pattern,
1765 48c8c60d 2020-01-27 stsp int diff_context, int limit, int log_branches,
1766 463a997a 2019-12-08 kn struct got_reflist_head *refs)
1767 f42b1b34 2018-03-12 stsp {
1768 f42b1b34 2018-03-12 stsp const struct got_error *err;
1769 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
1770 6841bf13 2019-11-29 kn regex_t regex;
1771 6841bf13 2019-11-29 kn int have_match;
1772 372ccdbb 2018-06-10 stsp
1773 6841bf13 2019-11-29 kn if (search_pattern &&
1774 6841bf13 2019-11-29 kn regcomp(&regex, search_pattern, REG_EXTENDED | REG_NOSUB | REG_NEWLINE))
1775 6841bf13 2019-11-29 kn return got_error_msg(GOT_ERR_REGEX, search_pattern);
1776 6841bf13 2019-11-29 kn
1777 48c8c60d 2020-01-27 stsp err = got_commit_graph_open(&graph, path, !log_branches);
1778 f42b1b34 2018-03-12 stsp if (err)
1779 f42b1b34 2018-03-12 stsp return err;
1780 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, root_id, repo,
1781 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
1782 372ccdbb 2018-06-10 stsp if (err)
1783 fcc85cad 2018-07-22 stsp goto done;
1784 656b1f76 2019-05-11 jcs for (;;) {
1785 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
1786 372ccdbb 2018-06-10 stsp struct got_object_id *id;
1787 8bf5b3c9 2018-03-17 stsp
1788 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
1789 84453469 2018-11-11 stsp break;
1790 84453469 2018-11-11 stsp
1791 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&id, graph, repo,
1792 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
1793 372ccdbb 2018-06-10 stsp if (err) {
1794 ee780d5c 2020-01-04 stsp if (err->code == GOT_ERR_ITER_COMPLETED)
1795 9ba79e04 2018-06-11 stsp err = NULL;
1796 ee780d5c 2020-01-04 stsp break;
1797 7e665116 2018-04-02 stsp }
1798 b43fbaa0 2018-06-11 stsp if (id == NULL)
1799 7e665116 2018-04-02 stsp break;
1800 b43fbaa0 2018-06-11 stsp
1801 f8e900f3 2018-06-11 stsp err = got_object_open_as_commit(&commit, repo, id);
1802 b43fbaa0 2018-06-11 stsp if (err)
1803 fcc85cad 2018-07-22 stsp break;
1804 6841bf13 2019-11-29 kn
1805 6841bf13 2019-11-29 kn if (search_pattern) {
1806 6841bf13 2019-11-29 kn err = match_logmsg(&have_match, id, commit, &regex);
1807 6841bf13 2019-11-29 kn if (err) {
1808 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1809 6841bf13 2019-11-29 kn break;
1810 6841bf13 2019-11-29 kn }
1811 6841bf13 2019-11-29 kn if (have_match == 0) {
1812 6841bf13 2019-11-29 kn got_object_commit_close(commit);
1813 6841bf13 2019-11-29 kn continue;
1814 6841bf13 2019-11-29 kn }
1815 6841bf13 2019-11-29 kn }
1816 6841bf13 2019-11-29 kn
1817 44392932 2019-08-25 stsp err = print_commit(commit, id, repo, path, show_patch,
1818 44392932 2019-08-25 stsp diff_context, refs);
1819 b43fbaa0 2018-06-11 stsp got_object_commit_close(commit);
1820 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
1821 7e665116 2018-04-02 stsp break;
1822 31cedeaf 2018-09-15 stsp }
1823 fcc85cad 2018-07-22 stsp done:
1824 6841bf13 2019-11-29 kn if (search_pattern)
1825 6841bf13 2019-11-29 kn regfree(&regex);
1826 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
1827 f42b1b34 2018-03-12 stsp return err;
1828 f42b1b34 2018-03-12 stsp }
1829 5c860e29 2018-03-12 stsp
1830 4ed7e80c 2018-05-20 stsp __dead static void
1831 6f3d1eb0 2018-03-12 stsp usage_log(void)
1832 6f3d1eb0 2018-03-12 stsp {
1833 48c8c60d 2020-01-27 stsp fprintf(stderr, "usage: %s log [-b] [-c commit] [-C number] [ -l N ] [-p] "
1834 6841bf13 2019-11-29 kn "[-s search-pattern] [-r repository-path] [path]\n", getprogname());
1835 6f3d1eb0 2018-03-12 stsp exit(1);
1836 b1ebc001 2019-08-13 stsp }
1837 b1ebc001 2019-08-13 stsp
1838 b1ebc001 2019-08-13 stsp static int
1839 b1ebc001 2019-08-13 stsp get_default_log_limit(void)
1840 b1ebc001 2019-08-13 stsp {
1841 b1ebc001 2019-08-13 stsp const char *got_default_log_limit;
1842 b1ebc001 2019-08-13 stsp long long n;
1843 b1ebc001 2019-08-13 stsp const char *errstr;
1844 b1ebc001 2019-08-13 stsp
1845 b1ebc001 2019-08-13 stsp got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
1846 b1ebc001 2019-08-13 stsp if (got_default_log_limit == NULL)
1847 b1ebc001 2019-08-13 stsp return 0;
1848 b1ebc001 2019-08-13 stsp n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
1849 b1ebc001 2019-08-13 stsp if (errstr != NULL)
1850 b1ebc001 2019-08-13 stsp return 0;
1851 b1ebc001 2019-08-13 stsp return n;
1852 6f3d1eb0 2018-03-12 stsp }
1853 6f3d1eb0 2018-03-12 stsp
1854 4ed7e80c 2018-05-20 stsp static const struct got_error *
1855 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
1856 f42b1b34 2018-03-12 stsp {
1857 f42b1b34 2018-03-12 stsp const struct got_error *error;
1858 04ca23f4 2018-07-16 stsp struct got_repository *repo = NULL;
1859 cffc0aa4 2019-02-05 stsp struct got_worktree *worktree = NULL;
1860 15a94983 2018-12-23 stsp struct got_commit_object *commit = NULL;
1861 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
1862 04ca23f4 2018-07-16 stsp char *repo_path = NULL, *path = NULL, *cwd = NULL, *in_repo_path = NULL;
1863 463a997a 2019-12-08 kn const char *start_commit = NULL, *search_pattern = NULL;
1864 dc1edbfa 2019-11-29 kn int diff_context = -1, ch;
1865 48c8c60d 2020-01-27 stsp int show_patch = 0, limit = 0, log_branches = 0;
1866 64a96a6d 2018-04-01 stsp const char *errstr;
1867 199a4027 2019-02-02 stsp struct got_reflist_head refs;
1868 1b3893a2 2019-03-18 stsp
1869 1b3893a2 2019-03-18 stsp SIMPLEQ_INIT(&refs);
1870 5c860e29 2018-03-12 stsp
1871 6715a751 2018-03-16 stsp #ifndef PROFILE
1872 6098196c 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
1873 6098196c 2019-01-04 stsp NULL)
1874 ad242220 2018-09-08 stsp == -1)
1875 f42b1b34 2018-03-12 stsp err(1, "pledge");
1876 6715a751 2018-03-16 stsp #endif
1877 79109fed 2018-03-27 stsp
1878 b1ebc001 2019-08-13 stsp limit = get_default_log_limit();
1879 b1ebc001 2019-08-13 stsp
1880 48c8c60d 2020-01-27 stsp while ((ch = getopt(argc, argv, "bpc:C:l:r:s:")) != -1) {
1881 79109fed 2018-03-27 stsp switch (ch) {
1882 79109fed 2018-03-27 stsp case 'p':
1883 79109fed 2018-03-27 stsp show_patch = 1;
1884 d142fc45 2018-04-01 stsp break;
1885 d142fc45 2018-04-01 stsp case 'c':
1886 d142fc45 2018-04-01 stsp start_commit = optarg;
1887 64a96a6d 2018-04-01 stsp break;
1888 c0cc5c62 2018-10-18 stsp case 'C':
1889 4a8520aa 2018-10-18 stsp diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
1890 4a8520aa 2018-10-18 stsp &errstr);
1891 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
1892 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
1893 c0cc5c62 2018-10-18 stsp break;
1894 64a96a6d 2018-04-01 stsp case 'l':
1895 b1ebc001 2019-08-13 stsp limit = strtonum(optarg, 0, INT_MAX, &errstr);
1896 64a96a6d 2018-04-01 stsp if (errstr != NULL)
1897 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
1898 cc54c501 2019-07-15 stsp break;
1899 48c8c60d 2020-01-27 stsp case 'b':
1900 48c8c60d 2020-01-27 stsp log_branches = 1;
1901 a0603db2 2018-06-10 stsp break;
1902 04ca23f4 2018-07-16 stsp case 'r':
1903 04ca23f4 2018-07-16 stsp repo_path = realpath(optarg, NULL);
1904 04ca23f4 2018-07-16 stsp if (repo_path == NULL)
1905 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
1906 9ba1d308 2019-10-21 stsp optarg);
1907 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
1908 04ca23f4 2018-07-16 stsp break;
1909 6841bf13 2019-11-29 kn case 's':
1910 6841bf13 2019-11-29 kn search_pattern = optarg;
1911 6841bf13 2019-11-29 kn break;
1912 79109fed 2018-03-27 stsp default:
1913 2deda0b9 2019-03-07 stsp usage_log();
1914 79109fed 2018-03-27 stsp /* NOTREACHED */
1915 79109fed 2018-03-27 stsp }
1916 79109fed 2018-03-27 stsp }
1917 79109fed 2018-03-27 stsp
1918 79109fed 2018-03-27 stsp argc -= optind;
1919 79109fed 2018-03-27 stsp argv += optind;
1920 f42b1b34 2018-03-12 stsp
1921 dc1edbfa 2019-11-29 kn if (diff_context == -1)
1922 dc1edbfa 2019-11-29 kn diff_context = 3;
1923 dc1edbfa 2019-11-29 kn else if (!show_patch)
1924 dc1edbfa 2019-11-29 kn errx(1, "-C reguires -p");
1925 dc1edbfa 2019-11-29 kn
1926 04ca23f4 2018-07-16 stsp cwd = getcwd(NULL, 0);
1927 04ca23f4 2018-07-16 stsp if (cwd == NULL) {
1928 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
1929 04ca23f4 2018-07-16 stsp goto done;
1930 04ca23f4 2018-07-16 stsp }
1931 cffc0aa4 2019-02-05 stsp
1932 cffc0aa4 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
1933 cffc0aa4 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
1934 cffc0aa4 2019-02-05 stsp goto done;
1935 cffc0aa4 2019-02-05 stsp error = NULL;
1936 cffc0aa4 2019-02-05 stsp
1937 e7301579 2019-03-18 stsp if (argc == 0) {
1938 cbd1af7a 2019-03-18 stsp path = strdup("");
1939 e7301579 2019-03-18 stsp if (path == NULL) {
1940 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1941 cbd1af7a 2019-03-18 stsp goto done;
1942 e7301579 2019-03-18 stsp }
1943 e7301579 2019-03-18 stsp } else if (argc == 1) {
1944 e7301579 2019-03-18 stsp if (worktree) {
1945 e7301579 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
1946 e7301579 2019-03-18 stsp argv[0]);
1947 e7301579 2019-03-18 stsp if (error)
1948 e7301579 2019-03-18 stsp goto done;
1949 e7301579 2019-03-18 stsp } else {
1950 e7301579 2019-03-18 stsp path = strdup(argv[0]);
1951 e7301579 2019-03-18 stsp if (path == NULL) {
1952 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1953 e7301579 2019-03-18 stsp goto done;
1954 e7301579 2019-03-18 stsp }
1955 e7301579 2019-03-18 stsp }
1956 cbd1af7a 2019-03-18 stsp } else
1957 cbd1af7a 2019-03-18 stsp usage_log();
1958 cbd1af7a 2019-03-18 stsp
1959 5486daa2 2019-05-11 stsp if (repo_path == NULL) {
1960 5486daa2 2019-05-11 stsp repo_path = worktree ?
1961 5486daa2 2019-05-11 stsp strdup(got_worktree_get_repo_path(worktree)) : strdup(cwd);
1962 5486daa2 2019-05-11 stsp }
1963 04ca23f4 2018-07-16 stsp if (repo_path == NULL) {
1964 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
1965 cffc0aa4 2019-02-05 stsp goto done;
1966 04ca23f4 2018-07-16 stsp }
1967 6098196c 2019-01-04 stsp
1968 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
1969 d7d4f210 2018-03-12 stsp if (error != NULL)
1970 04ca23f4 2018-07-16 stsp goto done;
1971 f42b1b34 2018-03-12 stsp
1972 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
1973 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
1974 c02c541e 2019-03-29 stsp if (error)
1975 c02c541e 2019-03-29 stsp goto done;
1976 c02c541e 2019-03-29 stsp
1977 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
1978 3235492e 2018-04-01 stsp struct got_reference *head_ref;
1979 1cc14b9f 2019-05-14 stsp error = got_ref_open(&head_ref, repo,
1980 1cc14b9f 2019-05-14 stsp worktree ? got_worktree_get_head_ref_name(worktree)
1981 1cc14b9f 2019-05-14 stsp : GOT_REF_HEAD, 0);
1982 3235492e 2018-04-01 stsp if (error != NULL)
1983 3235492e 2018-04-01 stsp return error;
1984 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
1985 3235492e 2018-04-01 stsp got_ref_close(head_ref);
1986 3235492e 2018-04-01 stsp if (error != NULL)
1987 3235492e 2018-04-01 stsp return error;
1988 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
1989 3235492e 2018-04-01 stsp } else {
1990 d1f2edc9 2018-06-13 stsp struct got_reference *ref;
1991 2f17228e 2019-05-12 stsp error = got_ref_open(&ref, repo, start_commit, 0);
1992 3235492e 2018-04-01 stsp if (error == NULL) {
1993 1caad61b 2019-02-01 stsp int obj_type;
1994 d1f2edc9 2018-06-13 stsp error = got_ref_resolve(&id, repo, ref);
1995 d1f2edc9 2018-06-13 stsp got_ref_close(ref);
1996 d1f2edc9 2018-06-13 stsp if (error != NULL)
1997 1caad61b 2019-02-01 stsp goto done;
1998 1caad61b 2019-02-01 stsp error = got_object_get_type(&obj_type, repo, id);
1999 1caad61b 2019-02-01 stsp if (error != NULL)
2000 1caad61b 2019-02-01 stsp goto done;
2001 1caad61b 2019-02-01 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
2002 1caad61b 2019-02-01 stsp struct got_tag_object *tag;
2003 1caad61b 2019-02-01 stsp error = got_object_open_as_tag(&tag, repo, id);
2004 1caad61b 2019-02-01 stsp if (error != NULL)
2005 1caad61b 2019-02-01 stsp goto done;
2006 1caad61b 2019-02-01 stsp if (got_object_tag_get_object_type(tag) !=
2007 1caad61b 2019-02-01 stsp GOT_OBJ_TYPE_COMMIT) {
2008 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2009 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2010 1caad61b 2019-02-01 stsp goto done;
2011 1caad61b 2019-02-01 stsp }
2012 1caad61b 2019-02-01 stsp free(id);
2013 1caad61b 2019-02-01 stsp id = got_object_id_dup(
2014 1caad61b 2019-02-01 stsp got_object_tag_get_object_id(tag));
2015 1caad61b 2019-02-01 stsp if (id == NULL)
2016 638f9024 2019-05-13 stsp error = got_error_from_errno(
2017 230a42bd 2019-05-11 jcs "got_object_id_dup");
2018 1caad61b 2019-02-01 stsp got_object_tag_close(tag);
2019 1caad61b 2019-02-01 stsp if (error)
2020 1caad61b 2019-02-01 stsp goto done;
2021 1caad61b 2019-02-01 stsp } else if (obj_type != GOT_OBJ_TYPE_COMMIT) {
2022 1caad61b 2019-02-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2023 1caad61b 2019-02-01 stsp goto done;
2024 1caad61b 2019-02-01 stsp }
2025 15a94983 2018-12-23 stsp error = got_object_open_as_commit(&commit, repo, id);
2026 d1f2edc9 2018-06-13 stsp if (error != NULL)
2027 1caad61b 2019-02-01 stsp goto done;
2028 d1f2edc9 2018-06-13 stsp }
2029 15a94983 2018-12-23 stsp if (commit == NULL) {
2030 e09a504c 2019-06-28 stsp error = got_repo_match_object_id_prefix(&id,
2031 dd88155e 2019-06-29 stsp start_commit, GOT_OBJ_TYPE_COMMIT, repo);
2032 d1f2edc9 2018-06-13 stsp if (error != NULL)
2033 d1f2edc9 2018-06-13 stsp return error;
2034 3235492e 2018-04-01 stsp }
2035 3235492e 2018-04-01 stsp }
2036 d7d4f210 2018-03-12 stsp if (error != NULL)
2037 04ca23f4 2018-07-16 stsp goto done;
2038 04ca23f4 2018-07-16 stsp
2039 deeabeae 2019-08-27 stsp if (worktree) {
2040 deeabeae 2019-08-27 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2041 deeabeae 2019-08-27 stsp char *p;
2042 deeabeae 2019-08-27 stsp if (asprintf(&p, "%s%s%s", prefix,
2043 deeabeae 2019-08-27 stsp (strcmp(prefix, "/") != 0) ? "/" : "", path) == -1) {
2044 deeabeae 2019-08-27 stsp error = got_error_from_errno("asprintf");
2045 deeabeae 2019-08-27 stsp goto done;
2046 deeabeae 2019-08-27 stsp }
2047 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2048 deeabeae 2019-08-27 stsp free(p);
2049 deeabeae 2019-08-27 stsp } else
2050 deeabeae 2019-08-27 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2051 04ca23f4 2018-07-16 stsp if (error != NULL)
2052 04ca23f4 2018-07-16 stsp goto done;
2053 04ca23f4 2018-07-16 stsp if (in_repo_path) {
2054 04ca23f4 2018-07-16 stsp free(path);
2055 04ca23f4 2018-07-16 stsp path = in_repo_path;
2056 04ca23f4 2018-07-16 stsp }
2057 199a4027 2019-02-02 stsp
2058 b8bad2ba 2019-08-23 stsp error = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
2059 199a4027 2019-02-02 stsp if (error)
2060 199a4027 2019-02-02 stsp goto done;
2061 04ca23f4 2018-07-16 stsp
2062 6841bf13 2019-11-29 kn error = print_commits(id, repo, path, show_patch, search_pattern,
2063 48c8c60d 2020-01-27 stsp diff_context, limit, log_branches, &refs);
2064 04ca23f4 2018-07-16 stsp done:
2065 04ca23f4 2018-07-16 stsp free(path);
2066 04ca23f4 2018-07-16 stsp free(repo_path);
2067 04ca23f4 2018-07-16 stsp free(cwd);
2068 f42b1b34 2018-03-12 stsp free(id);
2069 cffc0aa4 2019-02-05 stsp if (worktree)
2070 cffc0aa4 2019-02-05 stsp got_worktree_close(worktree);
2071 ad242220 2018-09-08 stsp if (repo) {
2072 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2073 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2074 ad242220 2018-09-08 stsp if (error == NULL)
2075 ad242220 2018-09-08 stsp error = repo_error;
2076 ad242220 2018-09-08 stsp }
2077 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
2078 8bf5b3c9 2018-03-17 stsp return error;
2079 5c860e29 2018-03-12 stsp }
2080 5c860e29 2018-03-12 stsp
2081 4ed7e80c 2018-05-20 stsp __dead static void
2082 3f8b7d6a 2018-04-01 stsp usage_diff(void)
2083 3f8b7d6a 2018-04-01 stsp {
2084 98eaaa12 2019-08-03 stsp fprintf(stderr, "usage: %s diff [-C number] [-r repository-path] [-s] "
2085 63035f9f 2019-10-06 stsp "[-w] [object1 object2 | path]\n", getprogname());
2086 3f8b7d6a 2018-04-01 stsp exit(1);
2087 b00d56cd 2018-04-01 stsp }
2088 b00d56cd 2018-04-01 stsp
2089 b72f483a 2019-02-05 stsp struct print_diff_arg {
2090 b72f483a 2019-02-05 stsp struct got_repository *repo;
2091 b72f483a 2019-02-05 stsp struct got_worktree *worktree;
2092 b72f483a 2019-02-05 stsp int diff_context;
2093 3fc0c068 2019-02-10 stsp const char *id_str;
2094 3fc0c068 2019-02-10 stsp int header_shown;
2095 98eaaa12 2019-08-03 stsp int diff_staged;
2096 63035f9f 2019-10-06 stsp int ignore_whitespace;
2097 b72f483a 2019-02-05 stsp };
2098 b72f483a 2019-02-05 stsp
2099 4ed7e80c 2018-05-20 stsp static const struct got_error *
2100 88d0e355 2019-08-03 stsp print_diff(void *arg, unsigned char status, unsigned char staged_status,
2101 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
2102 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
2103 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
2104 b72f483a 2019-02-05 stsp {
2105 b72f483a 2019-02-05 stsp struct print_diff_arg *a = arg;
2106 b72f483a 2019-02-05 stsp const struct got_error *err = NULL;
2107 b72f483a 2019-02-05 stsp struct got_blob_object *blob1 = NULL;
2108 12463d8b 2019-12-13 stsp int fd = -1;
2109 b72f483a 2019-02-05 stsp FILE *f2 = NULL;
2110 4ce46740 2019-08-08 stsp char *abspath = NULL, *label1 = NULL;
2111 b72f483a 2019-02-05 stsp struct stat sb;
2112 b72f483a 2019-02-05 stsp
2113 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2114 98eaaa12 2019-08-03 stsp if (staged_status != GOT_STATUS_MODIFY &&
2115 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_ADD &&
2116 98eaaa12 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
2117 98eaaa12 2019-08-03 stsp return NULL;
2118 98eaaa12 2019-08-03 stsp } else {
2119 98eaaa12 2019-08-03 stsp if (staged_status == GOT_STATUS_DELETE)
2120 98eaaa12 2019-08-03 stsp return NULL;
2121 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
2122 2a06fe5f 2019-08-24 stsp return got_error_set_errno(ENOENT, path);
2123 98eaaa12 2019-08-03 stsp if (status != GOT_STATUS_MODIFY &&
2124 98eaaa12 2019-08-03 stsp status != GOT_STATUS_ADD &&
2125 98eaaa12 2019-08-03 stsp status != GOT_STATUS_DELETE &&
2126 98eaaa12 2019-08-03 stsp status != GOT_STATUS_CONFLICT)
2127 98eaaa12 2019-08-03 stsp return NULL;
2128 98eaaa12 2019-08-03 stsp }
2129 3fc0c068 2019-02-10 stsp
2130 3fc0c068 2019-02-10 stsp if (!a->header_shown) {
2131 98eaaa12 2019-08-03 stsp printf("diff %s %s%s\n", a->id_str,
2132 98eaaa12 2019-08-03 stsp got_worktree_get_root_path(a->worktree),
2133 98eaaa12 2019-08-03 stsp a->diff_staged ? " (staged changes)" : "");
2134 3fc0c068 2019-02-10 stsp a->header_shown = 1;
2135 3fc0c068 2019-02-10 stsp }
2136 b72f483a 2019-02-05 stsp
2137 98eaaa12 2019-08-03 stsp if (a->diff_staged) {
2138 98eaaa12 2019-08-03 stsp const char *label1 = NULL, *label2 = NULL;
2139 98eaaa12 2019-08-03 stsp switch (staged_status) {
2140 98eaaa12 2019-08-03 stsp case GOT_STATUS_MODIFY:
2141 98eaaa12 2019-08-03 stsp label1 = path;
2142 98eaaa12 2019-08-03 stsp label2 = path;
2143 98eaaa12 2019-08-03 stsp break;
2144 98eaaa12 2019-08-03 stsp case GOT_STATUS_ADD:
2145 98eaaa12 2019-08-03 stsp label2 = path;
2146 98eaaa12 2019-08-03 stsp break;
2147 98eaaa12 2019-08-03 stsp case GOT_STATUS_DELETE:
2148 98eaaa12 2019-08-03 stsp label1 = path;
2149 98eaaa12 2019-08-03 stsp break;
2150 98eaaa12 2019-08-03 stsp default:
2151 98eaaa12 2019-08-03 stsp return got_error(GOT_ERR_FILE_STATUS);
2152 98eaaa12 2019-08-03 stsp }
2153 98eaaa12 2019-08-03 stsp return got_diff_objects_as_blobs(blob_id, staged_blob_id,
2154 63035f9f 2019-10-06 stsp label1, label2, a->diff_context, a->ignore_whitespace,
2155 63035f9f 2019-10-06 stsp a->repo, stdout);
2156 98eaaa12 2019-08-03 stsp }
2157 98eaaa12 2019-08-03 stsp
2158 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
2159 4ce46740 2019-08-08 stsp staged_status == GOT_STATUS_MODIFY) {
2160 4ce46740 2019-08-08 stsp char *id_str;
2161 408b4ebc 2019-08-03 stsp err = got_object_open_as_blob(&blob1, a->repo, staged_blob_id,
2162 408b4ebc 2019-08-03 stsp 8192);
2163 4ce46740 2019-08-08 stsp if (err)
2164 4ce46740 2019-08-08 stsp goto done;
2165 4ce46740 2019-08-08 stsp err = got_object_id_str(&id_str, staged_blob_id);
2166 4ce46740 2019-08-08 stsp if (err)
2167 4ce46740 2019-08-08 stsp goto done;
2168 4ce46740 2019-08-08 stsp if (asprintf(&label1, "%s (staged)", id_str) == -1) {
2169 4ce46740 2019-08-08 stsp err = got_error_from_errno("asprintf");
2170 4ce46740 2019-08-08 stsp free(id_str);
2171 4ce46740 2019-08-08 stsp goto done;
2172 4ce46740 2019-08-08 stsp }
2173 4ce46740 2019-08-08 stsp free(id_str);
2174 4ce46740 2019-08-08 stsp } else if (status != GOT_STATUS_ADD) {
2175 016a88dd 2019-05-13 stsp err = got_object_open_as_blob(&blob1, a->repo, blob_id, 8192);
2176 4ce46740 2019-08-08 stsp if (err)
2177 4ce46740 2019-08-08 stsp goto done;
2178 4ce46740 2019-08-08 stsp }
2179 d00136be 2019-03-26 stsp
2180 7154f6ce 2019-03-27 stsp if (status != GOT_STATUS_DELETE) {
2181 2ec1f75b 2019-03-26 stsp if (asprintf(&abspath, "%s/%s",
2182 2ec1f75b 2019-03-26 stsp got_worktree_get_root_path(a->worktree), path) == -1) {
2183 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2184 2ec1f75b 2019-03-26 stsp goto done;
2185 2ec1f75b 2019-03-26 stsp }
2186 b72f483a 2019-02-05 stsp
2187 12463d8b 2019-12-13 stsp if (dirfd != -1) {
2188 12463d8b 2019-12-13 stsp fd = openat(dirfd, de_name, O_RDONLY | O_NOFOLLOW);
2189 12463d8b 2019-12-13 stsp if (fd == -1) {
2190 12463d8b 2019-12-13 stsp err = got_error_from_errno2("openat", abspath);
2191 12463d8b 2019-12-13 stsp goto done;
2192 12463d8b 2019-12-13 stsp }
2193 12463d8b 2019-12-13 stsp } else {
2194 12463d8b 2019-12-13 stsp fd = open(abspath, O_RDONLY | O_NOFOLLOW);
2195 12463d8b 2019-12-13 stsp if (fd == -1) {
2196 12463d8b 2019-12-13 stsp err = got_error_from_errno2("open", abspath);
2197 12463d8b 2019-12-13 stsp goto done;
2198 12463d8b 2019-12-13 stsp }
2199 12463d8b 2019-12-13 stsp }
2200 12463d8b 2019-12-13 stsp if (fstat(fd, &sb) == -1) {
2201 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fstat", abspath);
2202 2ec1f75b 2019-03-26 stsp goto done;
2203 2ec1f75b 2019-03-26 stsp }
2204 12463d8b 2019-12-13 stsp f2 = fdopen(fd, "r");
2205 12463d8b 2019-12-13 stsp if (f2 == NULL) {
2206 12463d8b 2019-12-13 stsp err = got_error_from_errno2("fdopen", abspath);
2207 2ec1f75b 2019-03-26 stsp goto done;
2208 2ec1f75b 2019-03-26 stsp }
2209 12463d8b 2019-12-13 stsp fd = -1;
2210 2ec1f75b 2019-03-26 stsp } else
2211 2ec1f75b 2019-03-26 stsp sb.st_size = 0;
2212 b72f483a 2019-02-05 stsp
2213 4ce46740 2019-08-08 stsp err = got_diff_blob_file(blob1, label1, f2, sb.st_size, path,
2214 63035f9f 2019-10-06 stsp a->diff_context, a->ignore_whitespace, stdout);
2215 b72f483a 2019-02-05 stsp done:
2216 b72f483a 2019-02-05 stsp if (blob1)
2217 b72f483a 2019-02-05 stsp got_object_blob_close(blob1);
2218 12463d8b 2019-12-13 stsp if (f2 && fclose(f2) == EOF && err == NULL)
2219 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
2220 12463d8b 2019-12-13 stsp if (fd != -1 && close(fd) == -1 && err == NULL)
2221 12463d8b 2019-12-13 stsp err = got_error_from_errno("close");
2222 b72f483a 2019-02-05 stsp free(abspath);
2223 927df6b7 2019-02-10 stsp return err;
2224 927df6b7 2019-02-10 stsp }
2225 927df6b7 2019-02-10 stsp
2226 927df6b7 2019-02-10 stsp static const struct got_error *
2227 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
2228 b00d56cd 2018-04-01 stsp {
2229 b00d56cd 2018-04-01 stsp const struct got_error *error;
2230 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
2231 b72f483a 2019-02-05 stsp struct got_worktree *worktree = NULL;
2232 b72f483a 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL;
2233 15a94983 2018-12-23 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
2234 cd628e99 2019-05-28 stsp const char *id_str1 = NULL, *id_str2 = NULL;
2235 5e70831e 2019-05-28 stsp char *label1 = NULL, *label2 = NULL;
2236 15a94983 2018-12-23 stsp int type1, type2;
2237 63035f9f 2019-10-06 stsp int diff_context = 3, diff_staged = 0, ignore_whitespace = 0, ch;
2238 c0cc5c62 2018-10-18 stsp const char *errstr;
2239 927df6b7 2019-02-10 stsp char *path = NULL;
2240 b00d56cd 2018-04-01 stsp
2241 b00d56cd 2018-04-01 stsp #ifndef PROFILE
2242 25eccc22 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2243 25eccc22 2019-01-04 stsp NULL) == -1)
2244 b00d56cd 2018-04-01 stsp err(1, "pledge");
2245 b00d56cd 2018-04-01 stsp #endif
2246 b00d56cd 2018-04-01 stsp
2247 63035f9f 2019-10-06 stsp while ((ch = getopt(argc, argv, "C:r:sw")) != -1) {
2248 b00d56cd 2018-04-01 stsp switch (ch) {
2249 c0cc5c62 2018-10-18 stsp case 'C':
2250 dfcab68b 2019-11-29 kn diff_context = strtonum(optarg, 0, GOT_DIFF_MAX_CONTEXT,
2251 dfcab68b 2019-11-29 kn &errstr);
2252 c0cc5c62 2018-10-18 stsp if (errstr != NULL)
2253 c0cc5c62 2018-10-18 stsp err(1, "-C option %s", errstr);
2254 c0cc5c62 2018-10-18 stsp break;
2255 b72f483a 2019-02-05 stsp case 'r':
2256 b72f483a 2019-02-05 stsp repo_path = realpath(optarg, NULL);
2257 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2258 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2259 9ba1d308 2019-10-21 stsp optarg);
2260 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2261 b72f483a 2019-02-05 stsp break;
2262 98eaaa12 2019-08-03 stsp case 's':
2263 98eaaa12 2019-08-03 stsp diff_staged = 1;
2264 63035f9f 2019-10-06 stsp break;
2265 63035f9f 2019-10-06 stsp case 'w':
2266 63035f9f 2019-10-06 stsp ignore_whitespace = 1;
2267 98eaaa12 2019-08-03 stsp break;
2268 b00d56cd 2018-04-01 stsp default:
2269 2deda0b9 2019-03-07 stsp usage_diff();
2270 b00d56cd 2018-04-01 stsp /* NOTREACHED */
2271 b00d56cd 2018-04-01 stsp }
2272 b00d56cd 2018-04-01 stsp }
2273 b00d56cd 2018-04-01 stsp
2274 b00d56cd 2018-04-01 stsp argc -= optind;
2275 b00d56cd 2018-04-01 stsp argv += optind;
2276 b00d56cd 2018-04-01 stsp
2277 b72f483a 2019-02-05 stsp cwd = getcwd(NULL, 0);
2278 b72f483a 2019-02-05 stsp if (cwd == NULL) {
2279 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2280 b72f483a 2019-02-05 stsp goto done;
2281 b72f483a 2019-02-05 stsp }
2282 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
2283 927df6b7 2019-02-10 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2284 927df6b7 2019-02-10 stsp goto done;
2285 927df6b7 2019-02-10 stsp if (argc <= 1) {
2286 927df6b7 2019-02-10 stsp if (worktree == NULL) {
2287 927df6b7 2019-02-10 stsp error = got_error(GOT_ERR_NOT_WORKTREE);
2288 927df6b7 2019-02-10 stsp goto done;
2289 927df6b7 2019-02-10 stsp }
2290 b72f483a 2019-02-05 stsp if (repo_path)
2291 b72f483a 2019-02-05 stsp errx(1,
2292 b72f483a 2019-02-05 stsp "-r option can't be used when diffing a work tree");
2293 b72f483a 2019-02-05 stsp repo_path = strdup(got_worktree_get_repo_path(worktree));
2294 927df6b7 2019-02-10 stsp if (repo_path == NULL) {
2295 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2296 927df6b7 2019-02-10 stsp goto done;
2297 927df6b7 2019-02-10 stsp }
2298 927df6b7 2019-02-10 stsp if (argc == 1) {
2299 6c7ab921 2019-03-18 stsp error = got_worktree_resolve_path(&path, worktree,
2300 cbd1af7a 2019-03-18 stsp argv[0]);
2301 927df6b7 2019-02-10 stsp if (error)
2302 927df6b7 2019-02-10 stsp goto done;
2303 927df6b7 2019-02-10 stsp } else {
2304 927df6b7 2019-02-10 stsp path = strdup("");
2305 927df6b7 2019-02-10 stsp if (path == NULL) {
2306 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2307 927df6b7 2019-02-10 stsp goto done;
2308 927df6b7 2019-02-10 stsp }
2309 927df6b7 2019-02-10 stsp }
2310 b72f483a 2019-02-05 stsp } else if (argc == 2) {
2311 98eaaa12 2019-08-03 stsp if (diff_staged)
2312 98eaaa12 2019-08-03 stsp errx(1, "-s option can't be used when diffing "
2313 98eaaa12 2019-08-03 stsp "objects in repository");
2314 15a94983 2018-12-23 stsp id_str1 = argv[0];
2315 15a94983 2018-12-23 stsp id_str2 = argv[1];
2316 30db809c 2019-06-05 stsp if (worktree && repo_path == NULL) {
2317 30db809c 2019-06-05 stsp repo_path =
2318 30db809c 2019-06-05 stsp strdup(got_worktree_get_repo_path(worktree));
2319 30db809c 2019-06-05 stsp if (repo_path == NULL) {
2320 30db809c 2019-06-05 stsp error = got_error_from_errno("strdup");
2321 30db809c 2019-06-05 stsp goto done;
2322 30db809c 2019-06-05 stsp }
2323 30db809c 2019-06-05 stsp }
2324 b00d56cd 2018-04-01 stsp } else
2325 b00d56cd 2018-04-01 stsp usage_diff();
2326 25eccc22 2019-01-04 stsp
2327 b72f483a 2019-02-05 stsp if (repo_path == NULL) {
2328 b72f483a 2019-02-05 stsp repo_path = getcwd(NULL, 0);
2329 b72f483a 2019-02-05 stsp if (repo_path == NULL)
2330 638f9024 2019-05-13 stsp return got_error_from_errno("getcwd");
2331 b72f483a 2019-02-05 stsp }
2332 b00d56cd 2018-04-01 stsp
2333 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2334 76089277 2018-04-01 stsp free(repo_path);
2335 b00d56cd 2018-04-01 stsp if (error != NULL)
2336 b00d56cd 2018-04-01 stsp goto done;
2337 b00d56cd 2018-04-01 stsp
2338 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), 1,
2339 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
2340 c02c541e 2019-03-29 stsp if (error)
2341 c02c541e 2019-03-29 stsp goto done;
2342 c02c541e 2019-03-29 stsp
2343 30db809c 2019-06-05 stsp if (argc <= 1) {
2344 b72f483a 2019-02-05 stsp struct print_diff_arg arg;
2345 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
2346 b72f483a 2019-02-05 stsp char *id_str;
2347 72ea6654 2019-07-27 stsp
2348 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
2349 72ea6654 2019-07-27 stsp
2350 b72f483a 2019-02-05 stsp error = got_object_id_str(&id_str,
2351 b72f483a 2019-02-05 stsp got_worktree_get_base_commit_id(worktree));
2352 b72f483a 2019-02-05 stsp if (error)
2353 b72f483a 2019-02-05 stsp goto done;
2354 b72f483a 2019-02-05 stsp arg.repo = repo;
2355 b72f483a 2019-02-05 stsp arg.worktree = worktree;
2356 b72f483a 2019-02-05 stsp arg.diff_context = diff_context;
2357 3fc0c068 2019-02-10 stsp arg.id_str = id_str;
2358 3fc0c068 2019-02-10 stsp arg.header_shown = 0;
2359 98eaaa12 2019-08-03 stsp arg.diff_staged = diff_staged;
2360 63035f9f 2019-10-06 stsp arg.ignore_whitespace = ignore_whitespace;
2361 b72f483a 2019-02-05 stsp
2362 adc19d55 2019-07-28 stsp error = got_pathlist_append(&paths, path, NULL);
2363 72ea6654 2019-07-27 stsp if (error)
2364 72ea6654 2019-07-27 stsp goto done;
2365 72ea6654 2019-07-27 stsp
2366 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_diff,
2367 b72f483a 2019-02-05 stsp &arg, check_cancelled, NULL);
2368 b72f483a 2019-02-05 stsp free(id_str);
2369 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
2370 b72f483a 2019-02-05 stsp goto done;
2371 b72f483a 2019-02-05 stsp }
2372 b72f483a 2019-02-05 stsp
2373 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id1, &label1, id_str1,
2374 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2375 0adb7196 2019-08-11 stsp if (error)
2376 0adb7196 2019-08-11 stsp goto done;
2377 b00d56cd 2018-04-01 stsp
2378 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id2, &label2, id_str2,
2379 71a27632 2020-01-15 stsp GOT_OBJ_TYPE_ANY, 1, repo);
2380 0adb7196 2019-08-11 stsp if (error)
2381 0adb7196 2019-08-11 stsp goto done;
2382 b00d56cd 2018-04-01 stsp
2383 15a94983 2018-12-23 stsp error = got_object_get_type(&type1, repo, id1);
2384 15a94983 2018-12-23 stsp if (error)
2385 15a94983 2018-12-23 stsp goto done;
2386 15a94983 2018-12-23 stsp
2387 15a94983 2018-12-23 stsp error = got_object_get_type(&type2, repo, id2);
2388 15a94983 2018-12-23 stsp if (error)
2389 15a94983 2018-12-23 stsp goto done;
2390 15a94983 2018-12-23 stsp
2391 15a94983 2018-12-23 stsp if (type1 != type2) {
2392 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2393 b00d56cd 2018-04-01 stsp goto done;
2394 b00d56cd 2018-04-01 stsp }
2395 b00d56cd 2018-04-01 stsp
2396 15a94983 2018-12-23 stsp switch (type1) {
2397 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
2398 15a94983 2018-12-23 stsp error = got_diff_objects_as_blobs(id1, id2, NULL, NULL,
2399 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2400 b00d56cd 2018-04-01 stsp break;
2401 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
2402 15a94983 2018-12-23 stsp error = got_diff_objects_as_trees(id1, id2, "", "",
2403 63035f9f 2019-10-06 stsp diff_context, ignore_whitespace, repo, stdout);
2404 b00d56cd 2018-04-01 stsp break;
2405 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
2406 5e70831e 2019-05-28 stsp printf("diff %s %s\n", label1, label2);
2407 15a94983 2018-12-23 stsp error = got_diff_objects_as_commits(id1, id2, diff_context,
2408 63035f9f 2019-10-06 stsp ignore_whitespace, repo, stdout);
2409 b00d56cd 2018-04-01 stsp break;
2410 b00d56cd 2018-04-01 stsp default:
2411 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2412 b00d56cd 2018-04-01 stsp }
2413 b00d56cd 2018-04-01 stsp done:
2414 5e70831e 2019-05-28 stsp free(label1);
2415 5e70831e 2019-05-28 stsp free(label2);
2416 15a94983 2018-12-23 stsp free(id1);
2417 15a94983 2018-12-23 stsp free(id2);
2418 927df6b7 2019-02-10 stsp free(path);
2419 b72f483a 2019-02-05 stsp if (worktree)
2420 b72f483a 2019-02-05 stsp got_worktree_close(worktree);
2421 ad242220 2018-09-08 stsp if (repo) {
2422 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2423 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2424 ad242220 2018-09-08 stsp if (error == NULL)
2425 ad242220 2018-09-08 stsp error = repo_error;
2426 ad242220 2018-09-08 stsp }
2427 b00d56cd 2018-04-01 stsp return error;
2428 404c43c4 2018-06-21 stsp }
2429 404c43c4 2018-06-21 stsp
2430 404c43c4 2018-06-21 stsp __dead static void
2431 404c43c4 2018-06-21 stsp usage_blame(void)
2432 404c43c4 2018-06-21 stsp {
2433 9270e621 2019-02-05 stsp fprintf(stderr,
2434 9270e621 2019-02-05 stsp "usage: %s blame [-c commit] [-r repository-path] path\n",
2435 404c43c4 2018-06-21 stsp getprogname());
2436 404c43c4 2018-06-21 stsp exit(1);
2437 b00d56cd 2018-04-01 stsp }
2438 b00d56cd 2018-04-01 stsp
2439 28315671 2019-08-14 stsp struct blame_line {
2440 28315671 2019-08-14 stsp int annotated;
2441 28315671 2019-08-14 stsp char *id_str;
2442 82f6abb8 2019-08-14 stsp char *committer;
2443 11db6024 2019-10-21 stsp char datebuf[11]; /* YYYY-MM-DD + NUL */
2444 28315671 2019-08-14 stsp };
2445 28315671 2019-08-14 stsp
2446 28315671 2019-08-14 stsp struct blame_cb_args {
2447 28315671 2019-08-14 stsp struct blame_line *lines;
2448 28315671 2019-08-14 stsp int nlines;
2449 7ef28ff8 2019-08-14 stsp int nlines_prec;
2450 28315671 2019-08-14 stsp int lineno_cur;
2451 28315671 2019-08-14 stsp off_t *line_offsets;
2452 28315671 2019-08-14 stsp FILE *f;
2453 82f6abb8 2019-08-14 stsp struct got_repository *repo;
2454 28315671 2019-08-14 stsp };
2455 28315671 2019-08-14 stsp
2456 404c43c4 2018-06-21 stsp static const struct got_error *
2457 28315671 2019-08-14 stsp blame_cb(void *arg, int nlines, int lineno, struct got_object_id *id)
2458 28315671 2019-08-14 stsp {
2459 28315671 2019-08-14 stsp const struct got_error *err = NULL;
2460 28315671 2019-08-14 stsp struct blame_cb_args *a = arg;
2461 28315671 2019-08-14 stsp struct blame_line *bline;
2462 82f6abb8 2019-08-14 stsp char *line = NULL;
2463 28315671 2019-08-14 stsp size_t linesize = 0;
2464 82f6abb8 2019-08-14 stsp struct got_commit_object *commit = NULL;
2465 28315671 2019-08-14 stsp off_t offset;
2466 bcb49d15 2019-08-14 stsp struct tm tm;
2467 bcb49d15 2019-08-14 stsp time_t committer_time;
2468 28315671 2019-08-14 stsp
2469 28315671 2019-08-14 stsp if (nlines != a->nlines ||
2470 28315671 2019-08-14 stsp (lineno != -1 && lineno < 1) || lineno > a->nlines)
2471 28315671 2019-08-14 stsp return got_error(GOT_ERR_RANGE);
2472 28315671 2019-08-14 stsp
2473 28315671 2019-08-14 stsp if (sigint_received)
2474 28315671 2019-08-14 stsp return got_error(GOT_ERR_ITER_COMPLETED);
2475 28315671 2019-08-14 stsp
2476 2839f8b9 2019-08-18 stsp if (lineno == -1)
2477 2839f8b9 2019-08-18 stsp return NULL; /* no change in this commit */
2478 2839f8b9 2019-08-18 stsp
2479 28315671 2019-08-14 stsp /* Annotate this line. */
2480 28315671 2019-08-14 stsp bline = &a->lines[lineno - 1];
2481 28315671 2019-08-14 stsp if (bline->annotated)
2482 28315671 2019-08-14 stsp return NULL;
2483 28315671 2019-08-14 stsp err = got_object_id_str(&bline->id_str, id);
2484 28315671 2019-08-14 stsp if (err)
2485 28315671 2019-08-14 stsp return err;
2486 82f6abb8 2019-08-14 stsp
2487 82f6abb8 2019-08-14 stsp err = got_object_open_as_commit(&commit, a->repo, id);
2488 82f6abb8 2019-08-14 stsp if (err)
2489 82f6abb8 2019-08-14 stsp goto done;
2490 82f6abb8 2019-08-14 stsp
2491 82f6abb8 2019-08-14 stsp bline->committer = strdup(got_object_commit_get_committer(commit));
2492 82f6abb8 2019-08-14 stsp if (bline->committer == NULL) {
2493 82f6abb8 2019-08-14 stsp err = got_error_from_errno("strdup");
2494 bcb49d15 2019-08-14 stsp goto done;
2495 bcb49d15 2019-08-14 stsp }
2496 bcb49d15 2019-08-14 stsp
2497 bcb49d15 2019-08-14 stsp committer_time = got_object_commit_get_committer_time(commit);
2498 bcb49d15 2019-08-14 stsp if (localtime_r(&committer_time, &tm) == NULL)
2499 bcb49d15 2019-08-14 stsp return got_error_from_errno("localtime_r");
2500 6db9f7f6 2019-12-10 stsp if (strftime(bline->datebuf, sizeof(bline->datebuf), "%G-%m-%d",
2501 bcb49d15 2019-08-14 stsp &tm) >= sizeof(bline->datebuf)) {
2502 bcb49d15 2019-08-14 stsp err = got_error(GOT_ERR_NO_SPACE);
2503 82f6abb8 2019-08-14 stsp goto done;
2504 82f6abb8 2019-08-14 stsp }
2505 28315671 2019-08-14 stsp bline->annotated = 1;
2506 28315671 2019-08-14 stsp
2507 28315671 2019-08-14 stsp /* Print lines annotated so far. */
2508 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2509 28315671 2019-08-14 stsp if (!bline->annotated)
2510 82f6abb8 2019-08-14 stsp goto done;
2511 28315671 2019-08-14 stsp
2512 28315671 2019-08-14 stsp offset = a->line_offsets[a->lineno_cur - 1];
2513 82f6abb8 2019-08-14 stsp if (fseeko(a->f, offset, SEEK_SET) == -1) {
2514 82f6abb8 2019-08-14 stsp err = got_error_from_errno("fseeko");
2515 82f6abb8 2019-08-14 stsp goto done;
2516 82f6abb8 2019-08-14 stsp }
2517 28315671 2019-08-14 stsp
2518 28315671 2019-08-14 stsp while (bline->annotated) {
2519 82f6abb8 2019-08-14 stsp char *smallerthan, *at, *nl, *committer;
2520 82f6abb8 2019-08-14 stsp size_t len;
2521 82f6abb8 2019-08-14 stsp
2522 500467ff 2019-09-25 hiltjo if (getline(&line, &linesize, a->f) == -1) {
2523 28315671 2019-08-14 stsp if (ferror(a->f))
2524 28315671 2019-08-14 stsp err = got_error_from_errno("getline");
2525 28315671 2019-08-14 stsp break;
2526 28315671 2019-08-14 stsp }
2527 82f6abb8 2019-08-14 stsp
2528 82f6abb8 2019-08-14 stsp committer = bline->committer;
2529 82f6abb8 2019-08-14 stsp smallerthan = strchr(committer, '<');
2530 82f6abb8 2019-08-14 stsp if (smallerthan && smallerthan[1] != '\0')
2531 82f6abb8 2019-08-14 stsp committer = smallerthan + 1;
2532 82f6abb8 2019-08-14 stsp at = strchr(committer, '@');
2533 82f6abb8 2019-08-14 stsp if (at)
2534 82f6abb8 2019-08-14 stsp *at = '\0';
2535 82f6abb8 2019-08-14 stsp len = strlen(committer);
2536 82f6abb8 2019-08-14 stsp if (len >= 9)
2537 82f6abb8 2019-08-14 stsp committer[8] = '\0';
2538 28315671 2019-08-14 stsp
2539 28315671 2019-08-14 stsp nl = strchr(line, '\n');
2540 28315671 2019-08-14 stsp if (nl)
2541 28315671 2019-08-14 stsp *nl = '\0';
2542 bcb49d15 2019-08-14 stsp printf("%.*d) %.8s %s %-8s %s\n", a->nlines_prec, a->lineno_cur,
2543 bcb49d15 2019-08-14 stsp bline->id_str, bline->datebuf, committer, line);
2544 28315671 2019-08-14 stsp
2545 28315671 2019-08-14 stsp a->lineno_cur++;
2546 28315671 2019-08-14 stsp bline = &a->lines[a->lineno_cur - 1];
2547 28315671 2019-08-14 stsp }
2548 82f6abb8 2019-08-14 stsp done:
2549 82f6abb8 2019-08-14 stsp if (commit)
2550 82f6abb8 2019-08-14 stsp got_object_commit_close(commit);
2551 28315671 2019-08-14 stsp free(line);
2552 28315671 2019-08-14 stsp return err;
2553 28315671 2019-08-14 stsp }
2554 28315671 2019-08-14 stsp
2555 28315671 2019-08-14 stsp static const struct got_error *
2556 404c43c4 2018-06-21 stsp cmd_blame(int argc, char *argv[])
2557 404c43c4 2018-06-21 stsp {
2558 404c43c4 2018-06-21 stsp const struct got_error *error;
2559 404c43c4 2018-06-21 stsp struct got_repository *repo = NULL;
2560 0c06baac 2019-02-05 stsp struct got_worktree *worktree = NULL;
2561 66bea077 2018-08-02 stsp char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2562 28315671 2019-08-14 stsp struct got_object_id *obj_id = NULL;
2563 404c43c4 2018-06-21 stsp struct got_object_id *commit_id = NULL;
2564 28315671 2019-08-14 stsp struct got_blob_object *blob = NULL;
2565 404c43c4 2018-06-21 stsp char *commit_id_str = NULL;
2566 28315671 2019-08-14 stsp struct blame_cb_args bca;
2567 28315671 2019-08-14 stsp int ch, obj_type, i;
2568 b02560ec 2019-08-19 stsp size_t filesize;
2569 90f3c347 2019-08-19 stsp
2570 90f3c347 2019-08-19 stsp memset(&bca, 0, sizeof(bca));
2571 404c43c4 2018-06-21 stsp
2572 404c43c4 2018-06-21 stsp #ifndef PROFILE
2573 36e2fb66 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2574 36e2fb66 2019-01-04 stsp NULL) == -1)
2575 404c43c4 2018-06-21 stsp err(1, "pledge");
2576 404c43c4 2018-06-21 stsp #endif
2577 404c43c4 2018-06-21 stsp
2578 66bea077 2018-08-02 stsp while ((ch = getopt(argc, argv, "c:r:")) != -1) {
2579 404c43c4 2018-06-21 stsp switch (ch) {
2580 404c43c4 2018-06-21 stsp case 'c':
2581 404c43c4 2018-06-21 stsp commit_id_str = optarg;
2582 404c43c4 2018-06-21 stsp break;
2583 66bea077 2018-08-02 stsp case 'r':
2584 66bea077 2018-08-02 stsp repo_path = realpath(optarg, NULL);
2585 66bea077 2018-08-02 stsp if (repo_path == NULL)
2586 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2587 9ba1d308 2019-10-21 stsp optarg);
2588 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2589 66bea077 2018-08-02 stsp break;
2590 404c43c4 2018-06-21 stsp default:
2591 2deda0b9 2019-03-07 stsp usage_blame();
2592 404c43c4 2018-06-21 stsp /* NOTREACHED */
2593 404c43c4 2018-06-21 stsp }
2594 404c43c4 2018-06-21 stsp }
2595 404c43c4 2018-06-21 stsp
2596 404c43c4 2018-06-21 stsp argc -= optind;
2597 404c43c4 2018-06-21 stsp argv += optind;
2598 404c43c4 2018-06-21 stsp
2599 a39318fd 2018-08-02 stsp if (argc == 1)
2600 404c43c4 2018-06-21 stsp path = argv[0];
2601 a39318fd 2018-08-02 stsp else
2602 404c43c4 2018-06-21 stsp usage_blame();
2603 404c43c4 2018-06-21 stsp
2604 66bea077 2018-08-02 stsp cwd = getcwd(NULL, 0);
2605 66bea077 2018-08-02 stsp if (cwd == NULL) {
2606 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2607 66bea077 2018-08-02 stsp goto done;
2608 66bea077 2018-08-02 stsp }
2609 66bea077 2018-08-02 stsp if (repo_path == NULL) {
2610 0c06baac 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2611 0c06baac 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2612 66bea077 2018-08-02 stsp goto done;
2613 0c06baac 2019-02-05 stsp else
2614 0c06baac 2019-02-05 stsp error = NULL;
2615 0c06baac 2019-02-05 stsp if (worktree) {
2616 0c06baac 2019-02-05 stsp repo_path =
2617 0c06baac 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2618 0c06baac 2019-02-05 stsp if (repo_path == NULL)
2619 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2620 0c06baac 2019-02-05 stsp if (error)
2621 0c06baac 2019-02-05 stsp goto done;
2622 0c06baac 2019-02-05 stsp } else {
2623 0c06baac 2019-02-05 stsp repo_path = strdup(cwd);
2624 0c06baac 2019-02-05 stsp if (repo_path == NULL) {
2625 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2626 0c06baac 2019-02-05 stsp goto done;
2627 0c06baac 2019-02-05 stsp }
2628 66bea077 2018-08-02 stsp }
2629 66bea077 2018-08-02 stsp }
2630 36e2fb66 2019-01-04 stsp
2631 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2632 c02c541e 2019-03-29 stsp if (error != NULL)
2633 36e2fb66 2019-01-04 stsp goto done;
2634 66bea077 2018-08-02 stsp
2635 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2636 c02c541e 2019-03-29 stsp if (error)
2637 404c43c4 2018-06-21 stsp goto done;
2638 404c43c4 2018-06-21 stsp
2639 0c06baac 2019-02-05 stsp if (worktree) {
2640 6efaaa2d 2019-02-05 stsp const char *prefix = got_worktree_get_path_prefix(worktree);
2641 0c06baac 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2642 0c06baac 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2643 6efaaa2d 2019-02-05 stsp if (asprintf(&p, "%s%s%s%s%s",
2644 6efaaa2d 2019-02-05 stsp prefix, (strcmp(prefix, "/") != 0) ? "/" : "",
2645 6efaaa2d 2019-02-05 stsp worktree_subdir, worktree_subdir[0] ? "/" : "",
2646 6efaaa2d 2019-02-05 stsp path) == -1) {
2647 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2648 0c06baac 2019-02-05 stsp goto done;
2649 0c06baac 2019-02-05 stsp }
2650 6efaaa2d 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2651 0c06baac 2019-02-05 stsp free(p);
2652 0c06baac 2019-02-05 stsp } else {
2653 0c06baac 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2654 0c06baac 2019-02-05 stsp }
2655 0c06baac 2019-02-05 stsp if (error)
2656 66bea077 2018-08-02 stsp goto done;
2657 66bea077 2018-08-02 stsp
2658 404c43c4 2018-06-21 stsp if (commit_id_str == NULL) {
2659 404c43c4 2018-06-21 stsp struct got_reference *head_ref;
2660 a0975128 2020-02-07 stsp error = got_ref_open(&head_ref, repo, worktree ?
2661 a0975128 2020-02-07 stsp got_worktree_get_head_ref_name(worktree) : GOT_REF_HEAD, 0);
2662 404c43c4 2018-06-21 stsp if (error != NULL)
2663 66bea077 2018-08-02 stsp goto done;
2664 404c43c4 2018-06-21 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2665 404c43c4 2018-06-21 stsp got_ref_close(head_ref);
2666 404c43c4 2018-06-21 stsp if (error != NULL)
2667 66bea077 2018-08-02 stsp goto done;
2668 404c43c4 2018-06-21 stsp } else {
2669 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2670 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2671 30837e32 2019-07-25 stsp if (error)
2672 66bea077 2018-08-02 stsp goto done;
2673 404c43c4 2018-06-21 stsp }
2674 404c43c4 2018-06-21 stsp
2675 28315671 2019-08-14 stsp error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
2676 28315671 2019-08-14 stsp if (error)
2677 28315671 2019-08-14 stsp goto done;
2678 28315671 2019-08-14 stsp
2679 28315671 2019-08-14 stsp error = got_object_get_type(&obj_type, repo, obj_id);
2680 28315671 2019-08-14 stsp if (error)
2681 28315671 2019-08-14 stsp goto done;
2682 28315671 2019-08-14 stsp
2683 28315671 2019-08-14 stsp if (obj_type != GOT_OBJ_TYPE_BLOB) {
2684 28315671 2019-08-14 stsp error = got_error(GOT_ERR_OBJ_TYPE);
2685 28315671 2019-08-14 stsp goto done;
2686 28315671 2019-08-14 stsp }
2687 28315671 2019-08-14 stsp
2688 28315671 2019-08-14 stsp error = got_object_open_as_blob(&blob, repo, obj_id, 8192);
2689 28315671 2019-08-14 stsp if (error)
2690 28315671 2019-08-14 stsp goto done;
2691 28315671 2019-08-14 stsp bca.f = got_opentemp();
2692 28315671 2019-08-14 stsp if (bca.f == NULL) {
2693 28315671 2019-08-14 stsp error = got_error_from_errno("got_opentemp");
2694 28315671 2019-08-14 stsp goto done;
2695 28315671 2019-08-14 stsp }
2696 b02560ec 2019-08-19 stsp error = got_object_blob_dump_to_file(&filesize, &bca.nlines,
2697 28315671 2019-08-14 stsp &bca.line_offsets, bca.f, blob);
2698 7ef28ff8 2019-08-14 stsp if (error || bca.nlines == 0)
2699 28315671 2019-08-14 stsp goto done;
2700 28315671 2019-08-14 stsp
2701 b02560ec 2019-08-19 stsp /* Don't include \n at EOF in the blame line count. */
2702 b02560ec 2019-08-19 stsp if (bca.line_offsets[bca.nlines - 1] == filesize)
2703 b02560ec 2019-08-19 stsp bca.nlines--;
2704 b02560ec 2019-08-19 stsp
2705 28315671 2019-08-14 stsp bca.lines = calloc(bca.nlines, sizeof(*bca.lines));
2706 28315671 2019-08-14 stsp if (bca.lines == NULL) {
2707 28315671 2019-08-14 stsp error = got_error_from_errno("calloc");
2708 28315671 2019-08-14 stsp goto done;
2709 28315671 2019-08-14 stsp }
2710 28315671 2019-08-14 stsp bca.lineno_cur = 1;
2711 7ef28ff8 2019-08-14 stsp bca.nlines_prec = 0;
2712 7ef28ff8 2019-08-14 stsp i = bca.nlines;
2713 7ef28ff8 2019-08-14 stsp while (i > 0) {
2714 7ef28ff8 2019-08-14 stsp i /= 10;
2715 7ef28ff8 2019-08-14 stsp bca.nlines_prec++;
2716 7ef28ff8 2019-08-14 stsp }
2717 82f6abb8 2019-08-14 stsp bca.repo = repo;
2718 7ef28ff8 2019-08-14 stsp
2719 6fb7cd11 2019-08-22 stsp error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
2720 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
2721 404c43c4 2018-06-21 stsp done:
2722 66bea077 2018-08-02 stsp free(in_repo_path);
2723 66bea077 2018-08-02 stsp free(repo_path);
2724 66bea077 2018-08-02 stsp free(cwd);
2725 404c43c4 2018-06-21 stsp free(commit_id);
2726 28315671 2019-08-14 stsp free(obj_id);
2727 28315671 2019-08-14 stsp if (blob)
2728 28315671 2019-08-14 stsp got_object_blob_close(blob);
2729 0c06baac 2019-02-05 stsp if (worktree)
2730 0c06baac 2019-02-05 stsp got_worktree_close(worktree);
2731 ad242220 2018-09-08 stsp if (repo) {
2732 ad242220 2018-09-08 stsp const struct got_error *repo_error;
2733 ad242220 2018-09-08 stsp repo_error = got_repo_close(repo);
2734 ad242220 2018-09-08 stsp if (error == NULL)
2735 ad242220 2018-09-08 stsp error = repo_error;
2736 ad242220 2018-09-08 stsp }
2737 3affba96 2019-09-22 stsp if (bca.lines) {
2738 3affba96 2019-09-22 stsp for (i = 0; i < bca.nlines; i++) {
2739 3affba96 2019-09-22 stsp struct blame_line *bline = &bca.lines[i];
2740 3affba96 2019-09-22 stsp free(bline->id_str);
2741 3affba96 2019-09-22 stsp free(bline->committer);
2742 3affba96 2019-09-22 stsp }
2743 3affba96 2019-09-22 stsp free(bca.lines);
2744 28315671 2019-08-14 stsp }
2745 28315671 2019-08-14 stsp free(bca.line_offsets);
2746 28315671 2019-08-14 stsp if (bca.f && fclose(bca.f) == EOF && error == NULL)
2747 28315671 2019-08-14 stsp error = got_error_from_errno("fclose");
2748 404c43c4 2018-06-21 stsp return error;
2749 5de5890b 2018-10-18 stsp }
2750 5de5890b 2018-10-18 stsp
2751 5de5890b 2018-10-18 stsp __dead static void
2752 5de5890b 2018-10-18 stsp usage_tree(void)
2753 5de5890b 2018-10-18 stsp {
2754 c1669e2e 2019-01-09 stsp fprintf(stderr,
2755 c1669e2e 2019-01-09 stsp "usage: %s tree [-c commit] [-r repository-path] [-iR] path\n",
2756 5de5890b 2018-10-18 stsp getprogname());
2757 5de5890b 2018-10-18 stsp exit(1);
2758 5de5890b 2018-10-18 stsp }
2759 5de5890b 2018-10-18 stsp
2760 c1669e2e 2019-01-09 stsp static void
2761 c1669e2e 2019-01-09 stsp print_entry(struct got_tree_entry *te, const char *id, const char *path,
2762 c1669e2e 2019-01-09 stsp const char *root_path)
2763 c1669e2e 2019-01-09 stsp {
2764 c1669e2e 2019-01-09 stsp int is_root_path = (strcmp(path, root_path) == 0);
2765 848d6979 2019-08-12 stsp const char *modestr = "";
2766 56e0773d 2019-11-28 stsp mode_t mode = got_tree_entry_get_mode(te);
2767 5de5890b 2018-10-18 stsp
2768 c1669e2e 2019-01-09 stsp path += strlen(root_path);
2769 c1669e2e 2019-01-09 stsp while (path[0] == '/')
2770 c1669e2e 2019-01-09 stsp path++;
2771 c1669e2e 2019-01-09 stsp
2772 63c5ca5d 2019-08-24 stsp if (got_object_tree_entry_is_submodule(te))
2773 63c5ca5d 2019-08-24 stsp modestr = "$";
2774 56e0773d 2019-11-28 stsp else if (S_ISLNK(mode))
2775 848d6979 2019-08-12 stsp modestr = "@";
2776 56e0773d 2019-11-28 stsp else if (S_ISDIR(mode))
2777 848d6979 2019-08-12 stsp modestr = "/";
2778 56e0773d 2019-11-28 stsp else if (mode & S_IXUSR)
2779 848d6979 2019-08-12 stsp modestr = "*";
2780 848d6979 2019-08-12 stsp
2781 c1669e2e 2019-01-09 stsp printf("%s%s%s%s%s\n", id ? id : "", path,
2782 56e0773d 2019-11-28 stsp is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
2783 c1669e2e 2019-01-09 stsp }
2784 c1669e2e 2019-01-09 stsp
2785 5de5890b 2018-10-18 stsp static const struct got_error *
2786 5de5890b 2018-10-18 stsp print_tree(const char *path, struct got_object_id *commit_id,
2787 c1669e2e 2019-01-09 stsp int show_ids, int recurse, const char *root_path,
2788 c1669e2e 2019-01-09 stsp struct got_repository *repo)
2789 5de5890b 2018-10-18 stsp {
2790 5de5890b 2018-10-18 stsp const struct got_error *err = NULL;
2791 5de5890b 2018-10-18 stsp struct got_object_id *tree_id = NULL;
2792 5de5890b 2018-10-18 stsp struct got_tree_object *tree = NULL;
2793 56e0773d 2019-11-28 stsp int nentries, i;
2794 5de5890b 2018-10-18 stsp
2795 5de5890b 2018-10-18 stsp err = got_object_id_by_path(&tree_id, repo, commit_id, path);
2796 5de5890b 2018-10-18 stsp if (err)
2797 5de5890b 2018-10-18 stsp goto done;
2798 5de5890b 2018-10-18 stsp
2799 5de5890b 2018-10-18 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
2800 5de5890b 2018-10-18 stsp if (err)
2801 5de5890b 2018-10-18 stsp goto done;
2802 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
2803 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
2804 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
2805 5de5890b 2018-10-18 stsp char *id = NULL;
2806 84453469 2018-11-11 stsp
2807 84453469 2018-11-11 stsp if (sigint_received || sigpipe_received)
2808 84453469 2018-11-11 stsp break;
2809 84453469 2018-11-11 stsp
2810 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
2811 5de5890b 2018-10-18 stsp if (show_ids) {
2812 5de5890b 2018-10-18 stsp char *id_str;
2813 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str,
2814 56e0773d 2019-11-28 stsp got_tree_entry_get_id(te));
2815 5de5890b 2018-10-18 stsp if (err)
2816 5de5890b 2018-10-18 stsp goto done;
2817 5de5890b 2018-10-18 stsp if (asprintf(&id, "%s ", id_str) == -1) {
2818 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2819 5de5890b 2018-10-18 stsp free(id_str);
2820 5de5890b 2018-10-18 stsp goto done;
2821 5de5890b 2018-10-18 stsp }
2822 5de5890b 2018-10-18 stsp free(id_str);
2823 5de5890b 2018-10-18 stsp }
2824 c1669e2e 2019-01-09 stsp print_entry(te, id, path, root_path);
2825 5de5890b 2018-10-18 stsp free(id);
2826 c1669e2e 2019-01-09 stsp
2827 56e0773d 2019-11-28 stsp if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
2828 c1669e2e 2019-01-09 stsp char *child_path;
2829 c1669e2e 2019-01-09 stsp if (asprintf(&child_path, "%s%s%s", path,
2830 c1669e2e 2019-01-09 stsp path[0] == '/' && path[1] == '\0' ? "" : "/",
2831 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te)) == -1) {
2832 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
2833 c1669e2e 2019-01-09 stsp goto done;
2834 c1669e2e 2019-01-09 stsp }
2835 c1669e2e 2019-01-09 stsp err = print_tree(child_path, commit_id, show_ids, 1,
2836 c1669e2e 2019-01-09 stsp root_path, repo);
2837 c1669e2e 2019-01-09 stsp free(child_path);
2838 c1669e2e 2019-01-09 stsp if (err)
2839 c1669e2e 2019-01-09 stsp goto done;
2840 c1669e2e 2019-01-09 stsp }
2841 5de5890b 2018-10-18 stsp }
2842 5de5890b 2018-10-18 stsp done:
2843 5de5890b 2018-10-18 stsp if (tree)
2844 5de5890b 2018-10-18 stsp got_object_tree_close(tree);
2845 5de5890b 2018-10-18 stsp free(tree_id);
2846 5de5890b 2018-10-18 stsp return err;
2847 404c43c4 2018-06-21 stsp }
2848 404c43c4 2018-06-21 stsp
2849 5de5890b 2018-10-18 stsp static const struct got_error *
2850 5de5890b 2018-10-18 stsp cmd_tree(int argc, char *argv[])
2851 5de5890b 2018-10-18 stsp {
2852 5de5890b 2018-10-18 stsp const struct got_error *error;
2853 5de5890b 2018-10-18 stsp struct got_repository *repo = NULL;
2854 7a2c19d6 2019-02-05 stsp struct got_worktree *worktree = NULL;
2855 7a2c19d6 2019-02-05 stsp const char *path;
2856 7a2c19d6 2019-02-05 stsp char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
2857 5de5890b 2018-10-18 stsp struct got_object_id *commit_id = NULL;
2858 5de5890b 2018-10-18 stsp char *commit_id_str = NULL;
2859 c1669e2e 2019-01-09 stsp int show_ids = 0, recurse = 0;
2860 5de5890b 2018-10-18 stsp int ch;
2861 5de5890b 2018-10-18 stsp
2862 5de5890b 2018-10-18 stsp #ifndef PROFILE
2863 0f8d269b 2019-01-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
2864 0f8d269b 2019-01-04 stsp NULL) == -1)
2865 5de5890b 2018-10-18 stsp err(1, "pledge");
2866 5de5890b 2018-10-18 stsp #endif
2867 5de5890b 2018-10-18 stsp
2868 c1669e2e 2019-01-09 stsp while ((ch = getopt(argc, argv, "c:r:iR")) != -1) {
2869 5de5890b 2018-10-18 stsp switch (ch) {
2870 5de5890b 2018-10-18 stsp case 'c':
2871 5de5890b 2018-10-18 stsp commit_id_str = optarg;
2872 5de5890b 2018-10-18 stsp break;
2873 5de5890b 2018-10-18 stsp case 'r':
2874 5de5890b 2018-10-18 stsp repo_path = realpath(optarg, NULL);
2875 5de5890b 2018-10-18 stsp if (repo_path == NULL)
2876 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
2877 9ba1d308 2019-10-21 stsp optarg);
2878 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
2879 5de5890b 2018-10-18 stsp break;
2880 5de5890b 2018-10-18 stsp case 'i':
2881 5de5890b 2018-10-18 stsp show_ids = 1;
2882 5de5890b 2018-10-18 stsp break;
2883 c1669e2e 2019-01-09 stsp case 'R':
2884 c1669e2e 2019-01-09 stsp recurse = 1;
2885 c1669e2e 2019-01-09 stsp break;
2886 5de5890b 2018-10-18 stsp default:
2887 2deda0b9 2019-03-07 stsp usage_tree();
2888 5de5890b 2018-10-18 stsp /* NOTREACHED */
2889 5de5890b 2018-10-18 stsp }
2890 5de5890b 2018-10-18 stsp }
2891 5de5890b 2018-10-18 stsp
2892 5de5890b 2018-10-18 stsp argc -= optind;
2893 5de5890b 2018-10-18 stsp argv += optind;
2894 5de5890b 2018-10-18 stsp
2895 5de5890b 2018-10-18 stsp if (argc == 1)
2896 5de5890b 2018-10-18 stsp path = argv[0];
2897 5de5890b 2018-10-18 stsp else if (argc > 1)
2898 5de5890b 2018-10-18 stsp usage_tree();
2899 5de5890b 2018-10-18 stsp else
2900 7a2c19d6 2019-02-05 stsp path = NULL;
2901 7a2c19d6 2019-02-05 stsp
2902 9bf7a39b 2019-02-05 stsp cwd = getcwd(NULL, 0);
2903 9bf7a39b 2019-02-05 stsp if (cwd == NULL) {
2904 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
2905 9bf7a39b 2019-02-05 stsp goto done;
2906 9bf7a39b 2019-02-05 stsp }
2907 5de5890b 2018-10-18 stsp if (repo_path == NULL) {
2908 7a2c19d6 2019-02-05 stsp error = got_worktree_open(&worktree, cwd);
2909 8994de28 2019-02-05 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
2910 7a2c19d6 2019-02-05 stsp goto done;
2911 7a2c19d6 2019-02-05 stsp else
2912 7a2c19d6 2019-02-05 stsp error = NULL;
2913 7a2c19d6 2019-02-05 stsp if (worktree) {
2914 7a2c19d6 2019-02-05 stsp repo_path =
2915 7a2c19d6 2019-02-05 stsp strdup(got_worktree_get_repo_path(worktree));
2916 7a2c19d6 2019-02-05 stsp if (repo_path == NULL)
2917 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2918 7a2c19d6 2019-02-05 stsp if (error)
2919 7a2c19d6 2019-02-05 stsp goto done;
2920 7a2c19d6 2019-02-05 stsp } else {
2921 7a2c19d6 2019-02-05 stsp repo_path = strdup(cwd);
2922 7a2c19d6 2019-02-05 stsp if (repo_path == NULL) {
2923 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
2924 7a2c19d6 2019-02-05 stsp goto done;
2925 7a2c19d6 2019-02-05 stsp }
2926 5de5890b 2018-10-18 stsp }
2927 5de5890b 2018-10-18 stsp }
2928 5de5890b 2018-10-18 stsp
2929 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
2930 5de5890b 2018-10-18 stsp if (error != NULL)
2931 c02c541e 2019-03-29 stsp goto done;
2932 c02c541e 2019-03-29 stsp
2933 c530dc23 2019-07-23 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
2934 c02c541e 2019-03-29 stsp if (error)
2935 5de5890b 2018-10-18 stsp goto done;
2936 5de5890b 2018-10-18 stsp
2937 9bf7a39b 2019-02-05 stsp if (path == NULL) {
2938 9bf7a39b 2019-02-05 stsp if (worktree) {
2939 9bf7a39b 2019-02-05 stsp char *p, *worktree_subdir = cwd +
2940 9bf7a39b 2019-02-05 stsp strlen(got_worktree_get_root_path(worktree));
2941 9bf7a39b 2019-02-05 stsp if (asprintf(&p, "%s/%s",
2942 9bf7a39b 2019-02-05 stsp got_worktree_get_path_prefix(worktree),
2943 9bf7a39b 2019-02-05 stsp worktree_subdir) == -1) {
2944 638f9024 2019-05-13 stsp error = got_error_from_errno("asprintf");
2945 9bf7a39b 2019-02-05 stsp goto done;
2946 9bf7a39b 2019-02-05 stsp }
2947 f43793a4 2020-01-27 stsp error = got_repo_map_path(&in_repo_path, repo, p, 0);
2948 9bf7a39b 2019-02-05 stsp free(p);
2949 9bf7a39b 2019-02-05 stsp if (error)
2950 9bf7a39b 2019-02-05 stsp goto done;
2951 9bf7a39b 2019-02-05 stsp } else
2952 9bf7a39b 2019-02-05 stsp path = "/";
2953 9bf7a39b 2019-02-05 stsp }
2954 9bf7a39b 2019-02-05 stsp if (in_repo_path == NULL) {
2955 9bf7a39b 2019-02-05 stsp error = got_repo_map_path(&in_repo_path, repo, path, 1);
2956 9bf7a39b 2019-02-05 stsp if (error != NULL)
2957 9bf7a39b 2019-02-05 stsp goto done;
2958 9bf7a39b 2019-02-05 stsp }
2959 5de5890b 2018-10-18 stsp
2960 5de5890b 2018-10-18 stsp if (commit_id_str == NULL) {
2961 5de5890b 2018-10-18 stsp struct got_reference *head_ref;
2962 2f17228e 2019-05-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD, 0);
2963 5de5890b 2018-10-18 stsp if (error != NULL)
2964 5de5890b 2018-10-18 stsp goto done;
2965 5de5890b 2018-10-18 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
2966 5de5890b 2018-10-18 stsp got_ref_close(head_ref);
2967 5de5890b 2018-10-18 stsp if (error != NULL)
2968 5de5890b 2018-10-18 stsp goto done;
2969 5de5890b 2018-10-18 stsp } else {
2970 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
2971 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
2972 30837e32 2019-07-25 stsp if (error)
2973 5de5890b 2018-10-18 stsp goto done;
2974 5de5890b 2018-10-18 stsp }
2975 5de5890b 2018-10-18 stsp
2976 c1669e2e 2019-01-09 stsp error = print_tree(in_repo_path, commit_id, show_ids, recurse,
2977 c1669e2e 2019-01-09 stsp in_repo_path, repo);
2978 5de5890b 2018-10-18 stsp done:
2979 5de5890b 2018-10-18 stsp free(in_repo_path);
2980 5de5890b 2018-10-18 stsp free(repo_path);
2981 5de5890b 2018-10-18 stsp free(cwd);
2982 5de5890b 2018-10-18 stsp free(commit_id);
2983 7a2c19d6 2019-02-05 stsp if (worktree)
2984 7a2c19d6 2019-02-05 stsp got_worktree_close(worktree);
2985 5de5890b 2018-10-18 stsp if (repo) {
2986 5de5890b 2018-10-18 stsp const struct got_error *repo_error;
2987 5de5890b 2018-10-18 stsp repo_error = got_repo_close(repo);
2988 5de5890b 2018-10-18 stsp if (error == NULL)
2989 5de5890b 2018-10-18 stsp error = repo_error;
2990 5de5890b 2018-10-18 stsp }
2991 5de5890b 2018-10-18 stsp return error;
2992 5de5890b 2018-10-18 stsp }
2993 5de5890b 2018-10-18 stsp
2994 6bad629b 2019-02-04 stsp __dead static void
2995 6bad629b 2019-02-04 stsp usage_status(void)
2996 6bad629b 2019-02-04 stsp {
2997 72ea6654 2019-07-27 stsp fprintf(stderr, "usage: %s status [path ...]\n", getprogname());
2998 6bad629b 2019-02-04 stsp exit(1);
2999 6bad629b 2019-02-04 stsp }
3000 5c860e29 2018-03-12 stsp
3001 b72f483a 2019-02-05 stsp static const struct got_error *
3002 88d0e355 2019-08-03 stsp print_status(void *arg, unsigned char status, unsigned char staged_status,
3003 88d0e355 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
3004 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
3005 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
3006 6bad629b 2019-02-04 stsp {
3007 244725f2 2019-08-03 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
3008 c363b2c1 2019-08-03 stsp status = GOT_STATUS_NO_CHANGE;
3009 88d0e355 2019-08-03 stsp printf("%c%c %s\n", status, staged_status, path);
3010 b72f483a 2019-02-05 stsp return NULL;
3011 6bad629b 2019-02-04 stsp }
3012 5c860e29 2018-03-12 stsp
3013 6bad629b 2019-02-04 stsp static const struct got_error *
3014 6bad629b 2019-02-04 stsp cmd_status(int argc, char *argv[])
3015 6bad629b 2019-02-04 stsp {
3016 6bad629b 2019-02-04 stsp const struct got_error *error = NULL;
3017 6bad629b 2019-02-04 stsp struct got_repository *repo = NULL;
3018 6bad629b 2019-02-04 stsp struct got_worktree *worktree = NULL;
3019 f86a1bf5 2019-07-27 stsp char *cwd = NULL;
3020 72ea6654 2019-07-27 stsp struct got_pathlist_head paths;
3021 a5edda0a 2019-07-27 stsp struct got_pathlist_entry *pe;
3022 a5edda0a 2019-07-27 stsp int ch;
3023 5c860e29 2018-03-12 stsp
3024 72ea6654 2019-07-27 stsp TAILQ_INIT(&paths);
3025 72ea6654 2019-07-27 stsp
3026 6bad629b 2019-02-04 stsp while ((ch = getopt(argc, argv, "")) != -1) {
3027 6bad629b 2019-02-04 stsp switch (ch) {
3028 5c860e29 2018-03-12 stsp default:
3029 2deda0b9 2019-03-07 stsp usage_status();
3030 6bad629b 2019-02-04 stsp /* NOTREACHED */
3031 5c860e29 2018-03-12 stsp }
3032 5c860e29 2018-03-12 stsp }
3033 5c860e29 2018-03-12 stsp
3034 6bad629b 2019-02-04 stsp argc -= optind;
3035 6bad629b 2019-02-04 stsp argv += optind;
3036 5c860e29 2018-03-12 stsp
3037 6bad629b 2019-02-04 stsp #ifndef PROFILE
3038 6bad629b 2019-02-04 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
3039 6bad629b 2019-02-04 stsp NULL) == -1)
3040 6bad629b 2019-02-04 stsp err(1, "pledge");
3041 f42b1b34 2018-03-12 stsp #endif
3042 927df6b7 2019-02-10 stsp cwd = getcwd(NULL, 0);
3043 927df6b7 2019-02-10 stsp if (cwd == NULL) {
3044 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3045 927df6b7 2019-02-10 stsp goto done;
3046 927df6b7 2019-02-10 stsp }
3047 927df6b7 2019-02-10 stsp
3048 927df6b7 2019-02-10 stsp error = got_worktree_open(&worktree, cwd);
3049 927df6b7 2019-02-10 stsp if (error != NULL)
3050 a5edda0a 2019-07-27 stsp goto done;
3051 6bad629b 2019-02-04 stsp
3052 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
3053 c9956ddf 2019-09-08 stsp NULL);
3054 6bad629b 2019-02-04 stsp if (error != NULL)
3055 6bad629b 2019-02-04 stsp goto done;
3056 6bad629b 2019-02-04 stsp
3057 d0eebce4 2019-03-11 stsp error = apply_unveil(got_repo_get_path(repo), 1,
3058 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
3059 087fb88c 2019-08-04 stsp if (error)
3060 087fb88c 2019-08-04 stsp goto done;
3061 087fb88c 2019-08-04 stsp
3062 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
3063 6bad629b 2019-02-04 stsp if (error)
3064 6bad629b 2019-02-04 stsp goto done;
3065 6bad629b 2019-02-04 stsp
3066 72ea6654 2019-07-27 stsp error = got_worktree_status(worktree, &paths, repo, print_status, NULL,
3067 6bad629b 2019-02-04 stsp check_cancelled, NULL);
3068 6bad629b 2019-02-04 stsp done:
3069 a5edda0a 2019-07-27 stsp TAILQ_FOREACH(pe, &paths, entry)
3070 a5edda0a 2019-07-27 stsp free((char *)pe->path);
3071 72ea6654 2019-07-27 stsp got_pathlist_free(&paths);
3072 927df6b7 2019-02-10 stsp free(cwd);
3073 d0eebce4 2019-03-11 stsp return error;
3074 d0eebce4 2019-03-11 stsp }
3075 d0eebce4 2019-03-11 stsp
3076 d0eebce4 2019-03-11 stsp __dead static void
3077 d0eebce4 2019-03-11 stsp usage_ref(void)
3078 d0eebce4 2019-03-11 stsp {
3079 d0eebce4 2019-03-11 stsp fprintf(stderr,
3080 d1c1ae5f 2019-08-12 stsp "usage: %s ref [-r repository] -l | -d name | [-s] name target\n",
3081 d0eebce4 2019-03-11 stsp getprogname());
3082 d0eebce4 2019-03-11 stsp exit(1);
3083 d0eebce4 2019-03-11 stsp }
3084 d0eebce4 2019-03-11 stsp
3085 d0eebce4 2019-03-11 stsp static const struct got_error *
3086 d0eebce4 2019-03-11 stsp list_refs(struct got_repository *repo)
3087 d0eebce4 2019-03-11 stsp {
3088 d0eebce4 2019-03-11 stsp static const struct got_error *err = NULL;
3089 d0eebce4 2019-03-11 stsp struct got_reflist_head refs;
3090 d0eebce4 2019-03-11 stsp struct got_reflist_entry *re;
3091 d0eebce4 2019-03-11 stsp
3092 d0eebce4 2019-03-11 stsp SIMPLEQ_INIT(&refs);
3093 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, NULL, got_ref_cmp_by_name, NULL);
3094 d0eebce4 2019-03-11 stsp if (err)
3095 d0eebce4 2019-03-11 stsp return err;
3096 d0eebce4 2019-03-11 stsp
3097 d0eebce4 2019-03-11 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3098 d0eebce4 2019-03-11 stsp char *refstr;
3099 d0eebce4 2019-03-11 stsp refstr = got_ref_to_str(re->ref);
3100 d0eebce4 2019-03-11 stsp if (refstr == NULL)
3101 638f9024 2019-05-13 stsp return got_error_from_errno("got_ref_to_str");
3102 d0eebce4 2019-03-11 stsp printf("%s: %s\n", got_ref_get_name(re->ref), refstr);
3103 d0eebce4 2019-03-11 stsp free(refstr);
3104 d0eebce4 2019-03-11 stsp }
3105 d0eebce4 2019-03-11 stsp
3106 e2e879a0 2019-03-11 stsp got_ref_list_free(&refs);
3107 d0eebce4 2019-03-11 stsp return NULL;
3108 d0eebce4 2019-03-11 stsp }
3109 d0eebce4 2019-03-11 stsp
3110 d0eebce4 2019-03-11 stsp static const struct got_error *
3111 d0eebce4 2019-03-11 stsp delete_ref(struct got_repository *repo, const char *refname)
3112 d0eebce4 2019-03-11 stsp {
3113 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3114 d0eebce4 2019-03-11 stsp struct got_reference *ref;
3115 d0eebce4 2019-03-11 stsp
3116 2f17228e 2019-05-12 stsp err = got_ref_open(&ref, repo, refname, 0);
3117 d0eebce4 2019-03-11 stsp if (err)
3118 d0eebce4 2019-03-11 stsp return err;
3119 d0eebce4 2019-03-11 stsp
3120 d0eebce4 2019-03-11 stsp err = got_ref_delete(ref, repo);
3121 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3122 d0eebce4 2019-03-11 stsp return err;
3123 d0eebce4 2019-03-11 stsp }
3124 d0eebce4 2019-03-11 stsp
3125 d0eebce4 2019-03-11 stsp static const struct got_error *
3126 d83d9d5c 2019-05-13 stsp add_ref(struct got_repository *repo, const char *refname, const char *target)
3127 d0eebce4 2019-03-11 stsp {
3128 d0eebce4 2019-03-11 stsp const struct got_error *err = NULL;
3129 d0eebce4 2019-03-11 stsp struct got_object_id *id;
3130 d0eebce4 2019-03-11 stsp struct got_reference *ref = NULL;
3131 d1644381 2019-07-14 stsp
3132 d1644381 2019-07-14 stsp /*
3133 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3134 d1644381 2019-07-14 stsp * While technically a valid reference name, this case is usually
3135 d1644381 2019-07-14 stsp * an unintended typo.
3136 d1644381 2019-07-14 stsp */
3137 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3138 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3139 d0eebce4 2019-03-11 stsp
3140 dd88155e 2019-06-29 stsp err = got_repo_match_object_id_prefix(&id, target, GOT_OBJ_TYPE_ANY,
3141 dd88155e 2019-06-29 stsp repo);
3142 d83d9d5c 2019-05-13 stsp if (err) {
3143 d83d9d5c 2019-05-13 stsp struct got_reference *target_ref;
3144 d0eebce4 2019-03-11 stsp
3145 d83d9d5c 2019-05-13 stsp if (err->code != GOT_ERR_BAD_OBJ_ID_STR)
3146 d83d9d5c 2019-05-13 stsp return err;
3147 d83d9d5c 2019-05-13 stsp err = got_ref_open(&target_ref, repo, target, 0);
3148 d83d9d5c 2019-05-13 stsp if (err)
3149 d83d9d5c 2019-05-13 stsp return err;
3150 d83d9d5c 2019-05-13 stsp err = got_ref_resolve(&id, repo, target_ref);
3151 d83d9d5c 2019-05-13 stsp got_ref_close(target_ref);
3152 d83d9d5c 2019-05-13 stsp if (err)
3153 d83d9d5c 2019-05-13 stsp return err;
3154 d83d9d5c 2019-05-13 stsp }
3155 d83d9d5c 2019-05-13 stsp
3156 d0eebce4 2019-03-11 stsp err = got_ref_alloc(&ref, refname, id);
3157 d0eebce4 2019-03-11 stsp if (err)
3158 d0eebce4 2019-03-11 stsp goto done;
3159 d0eebce4 2019-03-11 stsp
3160 d0eebce4 2019-03-11 stsp err = got_ref_write(ref, repo);
3161 d0eebce4 2019-03-11 stsp done:
3162 d0eebce4 2019-03-11 stsp if (ref)
3163 d0eebce4 2019-03-11 stsp got_ref_close(ref);
3164 d0eebce4 2019-03-11 stsp free(id);
3165 d1c1ae5f 2019-08-12 stsp return err;
3166 d1c1ae5f 2019-08-12 stsp }
3167 d1c1ae5f 2019-08-12 stsp
3168 d1c1ae5f 2019-08-12 stsp static const struct got_error *
3169 d1c1ae5f 2019-08-12 stsp add_symref(struct got_repository *repo, const char *refname, const char *target)
3170 d1c1ae5f 2019-08-12 stsp {
3171 d1c1ae5f 2019-08-12 stsp const struct got_error *err = NULL;
3172 d1c1ae5f 2019-08-12 stsp struct got_reference *ref = NULL;
3173 d1c1ae5f 2019-08-12 stsp struct got_reference *target_ref = NULL;
3174 d1c1ae5f 2019-08-12 stsp
3175 d1c1ae5f 2019-08-12 stsp /*
3176 bd5895f3 2019-11-28 stsp * Don't let the user create a reference name with a leading '-'.
3177 d1c1ae5f 2019-08-12 stsp * While technically a valid reference name, this case is usually
3178 d1c1ae5f 2019-08-12 stsp * an unintended typo.
3179 d1c1ae5f 2019-08-12 stsp */
3180 bd5895f3 2019-11-28 stsp if (refname[0] == '-')
3181 bd5895f3 2019-11-28 stsp return got_error_path(refname, GOT_ERR_REF_NAME_MINUS);
3182 d1c1ae5f 2019-08-12 stsp
3183 d1c1ae5f 2019-08-12 stsp err = got_ref_open(&target_ref, repo, target, 0);
3184 d1c1ae5f 2019-08-12 stsp if (err)
3185 d1c1ae5f 2019-08-12 stsp return err;
3186 d1c1ae5f 2019-08-12 stsp
3187 d1c1ae5f 2019-08-12 stsp err = got_ref_alloc_symref(&ref, refname, target_ref);
3188 d1c1ae5f 2019-08-12 stsp if (err)
3189 d1c1ae5f 2019-08-12 stsp goto done;
3190 d1c1ae5f 2019-08-12 stsp
3191 d1c1ae5f 2019-08-12 stsp err = got_ref_write(ref, repo);
3192 d1c1ae5f 2019-08-12 stsp done:
3193 d1c1ae5f 2019-08-12 stsp if (target_ref)
3194 d1c1ae5f 2019-08-12 stsp got_ref_close(target_ref);
3195 d1c1ae5f 2019-08-12 stsp if (ref)
3196 d1c1ae5f 2019-08-12 stsp got_ref_close(ref);
3197 d0eebce4 2019-03-11 stsp return err;
3198 d0eebce4 2019-03-11 stsp }
3199 d0eebce4 2019-03-11 stsp
3200 d0eebce4 2019-03-11 stsp static const struct got_error *
3201 d0eebce4 2019-03-11 stsp cmd_ref(int argc, char *argv[])
3202 d0eebce4 2019-03-11 stsp {
3203 d0eebce4 2019-03-11 stsp const struct got_error *error = NULL;
3204 d0eebce4 2019-03-11 stsp struct got_repository *repo = NULL;
3205 d0eebce4 2019-03-11 stsp struct got_worktree *worktree = NULL;
3206 d0eebce4 2019-03-11 stsp char *cwd = NULL, *repo_path = NULL;
3207 d1c1ae5f 2019-08-12 stsp int ch, do_list = 0, create_symref = 0;
3208 d0eebce4 2019-03-11 stsp const char *delref = NULL;
3209 d0eebce4 2019-03-11 stsp
3210 d0eebce4 2019-03-11 stsp /* TODO: Add -s option for adding symbolic references. */
3211 d1c1ae5f 2019-08-12 stsp while ((ch = getopt(argc, argv, "d:r:ls")) != -1) {
3212 d0eebce4 2019-03-11 stsp switch (ch) {
3213 d0eebce4 2019-03-11 stsp case 'd':
3214 d0eebce4 2019-03-11 stsp delref = optarg;
3215 d0eebce4 2019-03-11 stsp break;
3216 d0eebce4 2019-03-11 stsp case 'r':
3217 d0eebce4 2019-03-11 stsp repo_path = realpath(optarg, NULL);
3218 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3219 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3220 9ba1d308 2019-10-21 stsp optarg);
3221 7fbaa4f3 2019-05-11 stsp got_path_strip_trailing_slashes(repo_path);
3222 d0eebce4 2019-03-11 stsp break;
3223 d0eebce4 2019-03-11 stsp case 'l':
3224 d0eebce4 2019-03-11 stsp do_list = 1;
3225 d1c1ae5f 2019-08-12 stsp break;
3226 d1c1ae5f 2019-08-12 stsp case 's':
3227 d1c1ae5f 2019-08-12 stsp create_symref = 1;
3228 d0eebce4 2019-03-11 stsp break;
3229 d0eebce4 2019-03-11 stsp default:
3230 d0eebce4 2019-03-11 stsp usage_ref();
3231 d0eebce4 2019-03-11 stsp /* NOTREACHED */
3232 d0eebce4 2019-03-11 stsp }
3233 d0eebce4 2019-03-11 stsp }
3234 d0eebce4 2019-03-11 stsp
3235 d0eebce4 2019-03-11 stsp if (do_list && delref)
3236 d0eebce4 2019-03-11 stsp errx(1, "-l and -d options are mutually exclusive\n");
3237 d0eebce4 2019-03-11 stsp
3238 d0eebce4 2019-03-11 stsp argc -= optind;
3239 d0eebce4 2019-03-11 stsp argv += optind;
3240 d0eebce4 2019-03-11 stsp
3241 d0eebce4 2019-03-11 stsp if (do_list || delref) {
3242 d1c1ae5f 2019-08-12 stsp if (create_symref)
3243 d1c1ae5f 2019-08-12 stsp errx(1, "-s option cannot be used together with the "
3244 d1c1ae5f 2019-08-12 stsp "-l or -d options");
3245 d0eebce4 2019-03-11 stsp if (argc > 0)
3246 d0eebce4 2019-03-11 stsp usage_ref();
3247 d0eebce4 2019-03-11 stsp } else if (argc != 2)
3248 d0eebce4 2019-03-11 stsp usage_ref();
3249 d0eebce4 2019-03-11 stsp
3250 d0eebce4 2019-03-11 stsp #ifndef PROFILE
3251 e0b57350 2019-03-12 stsp if (do_list) {
3252 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3253 e0b57350 2019-03-12 stsp NULL) == -1)
3254 e0b57350 2019-03-12 stsp err(1, "pledge");
3255 e0b57350 2019-03-12 stsp } else {
3256 e0b57350 2019-03-12 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3257 e0b57350 2019-03-12 stsp "sendfd unveil", NULL) == -1)
3258 e0b57350 2019-03-12 stsp err(1, "pledge");
3259 e0b57350 2019-03-12 stsp }
3260 d0eebce4 2019-03-11 stsp #endif
3261 d0eebce4 2019-03-11 stsp cwd = getcwd(NULL, 0);
3262 d0eebce4 2019-03-11 stsp if (cwd == NULL) {
3263 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
3264 d0eebce4 2019-03-11 stsp goto done;
3265 d0eebce4 2019-03-11 stsp }
3266 d0eebce4 2019-03-11 stsp
3267 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3268 d0eebce4 2019-03-11 stsp error = got_worktree_open(&worktree, cwd);
3269 d0eebce4 2019-03-11 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3270 d0eebce4 2019-03-11 stsp goto done;
3271 d0eebce4 2019-03-11 stsp else
3272 d0eebce4 2019-03-11 stsp error = NULL;
3273 d0eebce4 2019-03-11 stsp if (worktree) {
3274 d0eebce4 2019-03-11 stsp repo_path =
3275 d0eebce4 2019-03-11 stsp strdup(got_worktree_get_repo_path(worktree));
3276 d0eebce4 2019-03-11 stsp if (repo_path == NULL)
3277 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3278 d0eebce4 2019-03-11 stsp if (error)
3279 d0eebce4 2019-03-11 stsp goto done;
3280 d0eebce4 2019-03-11 stsp } else {
3281 d0eebce4 2019-03-11 stsp repo_path = strdup(cwd);
3282 d0eebce4 2019-03-11 stsp if (repo_path == NULL) {
3283 638f9024 2019-05-13 stsp error = got_error_from_errno("strdup");
3284 d0eebce4 2019-03-11 stsp goto done;
3285 d0eebce4 2019-03-11 stsp }
3286 d0eebce4 2019-03-11 stsp }
3287 d0eebce4 2019-03-11 stsp }
3288 d0eebce4 2019-03-11 stsp
3289 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3290 d0eebce4 2019-03-11 stsp if (error != NULL)
3291 d0eebce4 2019-03-11 stsp goto done;
3292 d0eebce4 2019-03-11 stsp
3293 c02c541e 2019-03-29 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3294 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3295 c02c541e 2019-03-29 stsp if (error)
3296 c02c541e 2019-03-29 stsp goto done;
3297 c02c541e 2019-03-29 stsp
3298 d0eebce4 2019-03-11 stsp if (do_list)
3299 d0eebce4 2019-03-11 stsp error = list_refs(repo);
3300 d0eebce4 2019-03-11 stsp else if (delref)
3301 d0eebce4 2019-03-11 stsp error = delete_ref(repo, delref);
3302 d1c1ae5f 2019-08-12 stsp else if (create_symref)
3303 d1c1ae5f 2019-08-12 stsp error = add_symref(repo, argv[0], argv[1]);
3304 d0eebce4 2019-03-11 stsp else
3305 d0eebce4 2019-03-11 stsp error = add_ref(repo, argv[0], argv[1]);
3306 4e759de4 2019-06-26 stsp done:
3307 4e759de4 2019-06-26 stsp if (repo)
3308 4e759de4 2019-06-26 stsp got_repo_close(repo);
3309 4e759de4 2019-06-26 stsp if (worktree)
3310 4e759de4 2019-06-26 stsp got_worktree_close(worktree);
3311 4e759de4 2019-06-26 stsp free(cwd);
3312 4e759de4 2019-06-26 stsp free(repo_path);
3313 4e759de4 2019-06-26 stsp return error;
3314 4e759de4 2019-06-26 stsp }
3315 4e759de4 2019-06-26 stsp
3316 4e759de4 2019-06-26 stsp __dead static void
3317 4e759de4 2019-06-26 stsp usage_branch(void)
3318 4e759de4 2019-06-26 stsp {
3319 4e759de4 2019-06-26 stsp fprintf(stderr,
3320 da76fce2 2020-02-24 stsp "usage: %s branch [-c commit] [-d] [-r repository] [-l] [-n] "
3321 da76fce2 2020-02-24 stsp "[name]\n", getprogname());
3322 4e759de4 2019-06-26 stsp exit(1);
3323 4e759de4 2019-06-26 stsp }
3324 4e759de4 2019-06-26 stsp
3325 4e759de4 2019-06-26 stsp static const struct got_error *
3326 4e99b47e 2019-10-04 stsp list_branch(struct got_repository *repo, struct got_worktree *worktree,
3327 4e99b47e 2019-10-04 stsp struct got_reference *ref)
3328 4e99b47e 2019-10-04 stsp {
3329 4e99b47e 2019-10-04 stsp const struct got_error *err = NULL;
3330 4e99b47e 2019-10-04 stsp const char *refname, *marker = " ";
3331 4e99b47e 2019-10-04 stsp char *refstr;
3332 4e99b47e 2019-10-04 stsp
3333 4e99b47e 2019-10-04 stsp refname = got_ref_get_name(ref);
3334 4e99b47e 2019-10-04 stsp if (worktree && strcmp(refname,
3335 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree)) == 0) {
3336 4e99b47e 2019-10-04 stsp struct got_object_id *id = NULL;
3337 4e99b47e 2019-10-04 stsp
3338 4e99b47e 2019-10-04 stsp err = got_ref_resolve(&id, repo, ref);
3339 4e99b47e 2019-10-04 stsp if (err)
3340 4e99b47e 2019-10-04 stsp return err;
3341 4e99b47e 2019-10-04 stsp if (got_object_id_cmp(id,
3342 4e99b47e 2019-10-04 stsp got_worktree_get_base_commit_id(worktree)) == 0)
3343 4e99b47e 2019-10-04 stsp marker = "* ";
3344 4e99b47e 2019-10-04 stsp else
3345 4e99b47e 2019-10-04 stsp marker = "~ ";
3346 4e99b47e 2019-10-04 stsp free(id);
3347 4e99b47e 2019-10-04 stsp }
3348 4e99b47e 2019-10-04 stsp
3349 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3350 4e99b47e 2019-10-04 stsp refname += 11;
3351 4e99b47e 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3352 4e99b47e 2019-10-04 stsp refname += 18;
3353 4e99b47e 2019-10-04 stsp
3354 4e99b47e 2019-10-04 stsp refstr = got_ref_to_str(ref);
3355 4e99b47e 2019-10-04 stsp if (refstr == NULL)
3356 4e99b47e 2019-10-04 stsp return got_error_from_errno("got_ref_to_str");
3357 4e99b47e 2019-10-04 stsp
3358 4e99b47e 2019-10-04 stsp printf("%s%s: %s\n", marker, refname, refstr);
3359 4e99b47e 2019-10-04 stsp free(refstr);
3360 4e99b47e 2019-10-04 stsp return NULL;
3361 4e99b47e 2019-10-04 stsp }
3362 4e99b47e 2019-10-04 stsp
3363 4e99b47e 2019-10-04 stsp static const struct got_error *
3364 ad89fa31 2019-10-04 stsp show_current_branch(struct got_repository *repo, struct got_worktree *worktree)
3365 ad89fa31 2019-10-04 stsp {
3366 ad89fa31 2019-10-04 stsp const char *refname;
3367 ad89fa31 2019-10-04 stsp
3368 ad89fa31 2019-10-04 stsp if (worktree == NULL)
3369 ad89fa31 2019-10-04 stsp return got_error(GOT_ERR_NOT_WORKTREE);
3370 ad89fa31 2019-10-04 stsp
3371 ad89fa31 2019-10-04 stsp refname = got_worktree_get_head_ref_name(worktree);
3372 ad89fa31 2019-10-04 stsp
3373 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/heads/", 11) == 0)
3374 ad89fa31 2019-10-04 stsp refname += 11;
3375 ad89fa31 2019-10-04 stsp if (strncmp(refname, "refs/got/worktree/", 18) == 0)
3376 ad89fa31 2019-10-04 stsp refname += 18;
3377 ad89fa31 2019-10-04 stsp
3378 ad89fa31 2019-10-04 stsp printf("%s\n", refname);
3379 ad89fa31 2019-10-04 stsp
3380 ad89fa31 2019-10-04 stsp return NULL;
3381 ad89fa31 2019-10-04 stsp }
3382 ad89fa31 2019-10-04 stsp
3383 ad89fa31 2019-10-04 stsp static const struct got_error *
3384 ba882ee3 2019-07-11 stsp list_branches(struct got_repository *repo, struct got_worktree *worktree)
3385 4e759de4 2019-06-26 stsp {
3386 4e759de4 2019-06-26 stsp static const struct got_error *err = NULL;
3387 4e759de4 2019-06-26 stsp struct got_reflist_head refs;
3388 4e759de4 2019-06-26 stsp struct got_reflist_entry *re;
3389 4e99b47e 2019-10-04 stsp struct got_reference *temp_ref = NULL;
3390 4e99b47e 2019-10-04 stsp int rebase_in_progress, histedit_in_progress;
3391 4e759de4 2019-06-26 stsp
3392 4e759de4 2019-06-26 stsp SIMPLEQ_INIT(&refs);
3393 ba882ee3 2019-07-11 stsp
3394 4e99b47e 2019-10-04 stsp if (worktree) {
3395 4e99b47e 2019-10-04 stsp err = got_worktree_rebase_in_progress(&rebase_in_progress,
3396 4e99b47e 2019-10-04 stsp worktree);
3397 4e99b47e 2019-10-04 stsp if (err)
3398 4e99b47e 2019-10-04 stsp return err;
3399 4e99b47e 2019-10-04 stsp
3400 4e99b47e 2019-10-04 stsp err = got_worktree_histedit_in_progress(&histedit_in_progress,
3401 4e99b47e 2019-10-04 stsp worktree);
3402 4e99b47e 2019-10-04 stsp if (err)
3403 4e99b47e 2019-10-04 stsp return err;
3404 4e99b47e 2019-10-04 stsp
3405 4e99b47e 2019-10-04 stsp if (rebase_in_progress || histedit_in_progress) {
3406 4e99b47e 2019-10-04 stsp err = got_ref_open(&temp_ref, repo,
3407 4e99b47e 2019-10-04 stsp got_worktree_get_head_ref_name(worktree), 0);
3408 4e99b47e 2019-10-04 stsp if (err)
3409 4e99b47e 2019-10-04 stsp return err;
3410 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, temp_ref);
3411 4e99b47e 2019-10-04 stsp got_ref_close(temp_ref);
3412 4e99b47e 2019-10-04 stsp }
3413 4e99b47e 2019-10-04 stsp }
3414 4e99b47e 2019-10-04 stsp
3415 b8bad2ba 2019-08-23 stsp err = got_ref_list(&refs, repo, "refs/heads",
3416 b8bad2ba 2019-08-23 stsp got_ref_cmp_by_name, NULL);
3417 4e759de4 2019-06-26 stsp if (err)
3418 4e759de4 2019-06-26 stsp return err;
3419 4e759de4 2019-06-26 stsp
3420 4e99b47e 2019-10-04 stsp SIMPLEQ_FOREACH(re, &refs, entry)
3421 4e99b47e 2019-10-04 stsp list_branch(repo, worktree, re->ref);
3422 4e759de4 2019-06-26 stsp
3423 4e759de4 2019-06-26 stsp got_ref_list_free(&refs);
3424 4e759de4 2019-06-26 stsp return NULL;
3425 4e759de4 2019-06-26 stsp }
3426 4e759de4 2019-06-26 stsp
3427 4e759de4 2019-06-26 stsp static const struct got_error *
3428 45cd4e47 2019-08-25 stsp delete_branch(struct got_repository *repo, struct got_worktree *worktree,
3429 45cd4e47 2019-08-25 stsp const char *branch_name)
3430 4e759de4 2019-06-26 stsp {
3431 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3432 45cd4e47 2019-08-25 stsp struct got_reference *ref = NULL;
3433 4e759de4 2019-06-26 stsp char *refname;
3434 4e759de4 2019-06-26 stsp
3435 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1)
3436 4e759de4 2019-06-26 stsp return got_error_from_errno("asprintf");
3437 4e759de4 2019-06-26 stsp
3438 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3439 4e759de4 2019-06-26 stsp if (err)
3440 4e759de4 2019-06-26 stsp goto done;
3441 4e759de4 2019-06-26 stsp
3442 45cd4e47 2019-08-25 stsp if (worktree &&
3443 45cd4e47 2019-08-25 stsp strcmp(got_worktree_get_head_ref_name(worktree),
3444 45cd4e47 2019-08-25 stsp got_ref_get_name(ref)) == 0) {
3445 45cd4e47 2019-08-25 stsp err = got_error_msg(GOT_ERR_SAME_BRANCH,
3446 45cd4e47 2019-08-25 stsp "will not delete this work tree's current branch");
3447 45cd4e47 2019-08-25 stsp goto done;
3448 45cd4e47 2019-08-25 stsp }
3449 45cd4e47 2019-08-25 stsp
3450 45cd4e47 2019-08-25 stsp err = got_ref_delete(ref, repo);
3451 4e759de4 2019-06-26 stsp done:
3452 45cd4e47 2019-08-25 stsp if (ref)
3453 45cd4e47 2019-08-25 stsp got_ref_close(ref);
3454 4e759de4 2019-06-26 stsp free(refname);
3455 4e759de4 2019-06-26 stsp return err;
3456 4e759de4 2019-06-26 stsp }
3457 4e759de4 2019-06-26 stsp
3458 4e759de4 2019-06-26 stsp static const struct got_error *
3459 4e759de4 2019-06-26 stsp add_branch(struct got_repository *repo, const char *branch_name,
3460 a74f7e83 2019-11-10 stsp struct got_object_id *base_commit_id)
3461 4e759de4 2019-06-26 stsp {
3462 4e759de4 2019-06-26 stsp const struct got_error *err = NULL;
3463 4e759de4 2019-06-26 stsp struct got_reference *ref = NULL;
3464 4e759de4 2019-06-26 stsp char *base_refname = NULL, *refname = NULL;
3465 d3f84d51 2019-07-11 stsp
3466 d3f84d51 2019-07-11 stsp /*
3467 bd5895f3 2019-11-28 stsp * Don't let the user create a branch name with a leading '-'.
3468 d3f84d51 2019-07-11 stsp * While technically a valid reference name, this case is usually
3469 d3f84d51 2019-07-11 stsp * an unintended typo.
3470 d3f84d51 2019-07-11 stsp */
3471 bd5895f3 2019-11-28 stsp if (branch_name[0] == '-')
3472 bd5895f3 2019-11-28 stsp return got_error_path(branch_name, GOT_ERR_REF_NAME_MINUS);
3473 4e759de4 2019-06-26 stsp
3474 4e759de4 2019-06-26 stsp if (asprintf(&refname, "refs/heads/%s", branch_name) == -1) {
3475 4e759de4 2019-06-26 stsp err = got_error_from_errno("asprintf");
3476 4e759de4 2019-06-26 stsp goto done;
3477 4e759de4 2019-06-26 stsp }
3478 4e759de4 2019-06-26 stsp
3479 4e759de4 2019-06-26 stsp err = got_ref_open(&ref, repo, refname, 0);
3480 4e759de4 2019-06-26 stsp if (err == NULL) {
3481 4e759de4 2019-06-26 stsp err = got_error(GOT_ERR_BRANCH_EXISTS);
3482 4e759de4 2019-06-26 stsp goto done;
3483 4e759de4 2019-06-26 stsp } else if (err->code != GOT_ERR_NOT_REF)
3484 4e759de4 2019-06-26 stsp goto done;
3485 4e759de4 2019-06-26 stsp
3486 a74f7e83 2019-11-10 stsp err = got_ref_alloc(&ref, refname, base_commit_id);
3487 4e759de4 2019-06-26 stsp if (err)
3488 4e759de4 2019-06-26 stsp goto done;
3489 4e759de4 2019-06-26 stsp
3490 4e759de4 2019-06-26 stsp err = got_ref_write(ref, repo);
3491 d0eebce4 2019-03-11 stsp done:
3492 4e759de4 2019-06-26 stsp if (ref)
3493 4e759de4 2019-06-26 stsp got_ref_close(ref);
3494 4e759de4 2019-06-26 stsp free(base_refname);
3495 4e759de4 2019-06-26 stsp free(refname);
3496 4e759de4 2019-06-26 stsp return err;
3497 4e759de4 2019-06-26 stsp }
3498 4e759de4 2019-06-26 stsp
3499 4e759de4 2019-06-26 stsp static const struct got_error *
3500 4e759de4 2019-06-26 stsp cmd_branch(int argc, char *argv[])
3501 4e759de4 2019-06-26 stsp {
3502 4e759de4 2019-06-26 stsp const struct got_error *error = NULL;
3503 4e759de4 2019-06-26 stsp struct got_repository *repo = NULL;
3504 4e759de4 2019-06-26 stsp struct got_worktree *worktree = NULL;
3505 4e759de4 2019-06-26 stsp char *cwd = NULL, *repo_path = NULL;
3506 da76fce2 2020-02-24 stsp int ch, do_list = 0, do_show = 0, do_update = 1;
3507 a74f7e83 2019-11-10 stsp const char *delref = NULL, *commit_id_arg = NULL;
3508 da76fce2 2020-02-24 stsp struct got_reference *ref = NULL;
3509 da76fce2 2020-02-24 stsp struct got_pathlist_head paths;
3510 da76fce2 2020-02-24 stsp struct got_pathlist_entry *pe;
3511 da76fce2 2020-02-24 stsp struct got_object_id *commit_id = NULL;
3512 da76fce2 2020-02-24 stsp char *commit_id_str = NULL;
3513 4e759de4 2019-06-26 stsp
3514 da76fce2 2020-02-24 stsp TAILQ_INIT(&paths);
3515 da76fce2 2020-02-24 stsp
3516 da76fce2 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:d:r:ln")) != -1) {
3517 4e759de4 2019-06-26 stsp switch (ch) {
3518 a74f7e83 2019-11-10 stsp case 'c':
3519 a74f7e83 2019-11-10 stsp commit_id_arg = optarg;
3520 a74f7e83 2019-11-10 stsp break;
3521 4e759de4 2019-06-26 stsp case 'd':
3522 4e759de4 2019-06-26 stsp delref = optarg;
3523 4e759de4 2019-06-26 stsp break;
3524 4e759de4 2019-06-26 stsp case 'r':
3525 4e759de4 2019-06-26 stsp repo_path = realpath(optarg, NULL);
3526 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3527 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
3528 9ba1d308 2019-10-21 stsp optarg);
3529 4e759de4 2019-06-26 stsp got_path_strip_trailing_slashes(repo_path);
3530 4e759de4 2019-06-26 stsp break;
3531 4e759de4 2019-06-26 stsp case 'l':
3532 4e759de4 2019-06-26 stsp do_list = 1;
3533 da76fce2 2020-02-24 stsp break;
3534 da76fce2 2020-02-24 stsp case 'n':
3535 da76fce2 2020-02-24 stsp do_update = 0;
3536 4e759de4 2019-06-26 stsp break;
3537 4e759de4 2019-06-26 stsp default:
3538 4e759de4 2019-06-26 stsp usage_branch();
3539 4e759de4 2019-06-26 stsp /* NOTREACHED */
3540 4e759de4 2019-06-26 stsp }
3541 4e759de4 2019-06-26 stsp }
3542 4e759de4 2019-06-26 stsp
3543 4e759de4 2019-06-26 stsp if (do_list && delref)
3544 4e759de4 2019-06-26 stsp errx(1, "-l and -d options are mutually exclusive\n");
3545 4e759de4 2019-06-26 stsp
3546 4e759de4 2019-06-26 stsp argc -= optind;
3547 4e759de4 2019-06-26 stsp argv += optind;
3548 ad89fa31 2019-10-04 stsp
3549 ad89fa31 2019-10-04 stsp if (!do_list && !delref && argc == 0)
3550 ad89fa31 2019-10-04 stsp do_show = 1;
3551 4e759de4 2019-06-26 stsp
3552 a74f7e83 2019-11-10 stsp if ((do_list || delref || do_show) && commit_id_arg != NULL)
3553 a74f7e83 2019-11-10 stsp errx(1, "-c option can only be used when creating a branch");
3554 a74f7e83 2019-11-10 stsp
3555 4e759de4 2019-06-26 stsp if (do_list || delref) {
3556 4e759de4 2019-06-26 stsp if (argc > 0)
3557 4e759de4 2019-06-26 stsp usage_branch();
3558 a74f7e83 2019-11-10 stsp } else if (!do_show && argc != 1)
3559 4e759de4 2019-06-26 stsp usage_branch();
3560 4e759de4 2019-06-26 stsp
3561 4e759de4 2019-06-26 stsp #ifndef PROFILE
3562 ad89fa31 2019-10-04 stsp if (do_list || do_show) {
3563 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
3564 4e759de4 2019-06-26 stsp NULL) == -1)
3565 4e759de4 2019-06-26 stsp err(1, "pledge");
3566 4e759de4 2019-06-26 stsp } else {
3567 4e759de4 2019-06-26 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
3568 4e759de4 2019-06-26 stsp "sendfd unveil", NULL) == -1)
3569 4e759de4 2019-06-26 stsp err(1, "pledge");
3570 4e759de4 2019-06-26 stsp }
3571 4e759de4 2019-06-26 stsp #endif
3572 4e759de4 2019-06-26 stsp cwd = getcwd(NULL, 0);
3573 4e759de4 2019-06-26 stsp if (cwd == NULL) {
3574 4e759de4 2019-06-26 stsp error = got_error_from_errno("getcwd");
3575 4e759de4 2019-06-26 stsp goto done;
3576 4e759de4 2019-06-26 stsp }
3577 4e759de4 2019-06-26 stsp
3578 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3579 4e759de4 2019-06-26 stsp error = got_worktree_open(&worktree, cwd);
3580 4e759de4 2019-06-26 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
3581 4e759de4 2019-06-26 stsp goto done;
3582 4e759de4 2019-06-26 stsp else
3583 4e759de4 2019-06-26 stsp error = NULL;
3584 4e759de4 2019-06-26 stsp if (worktree) {
3585 4e759de4 2019-06-26 stsp repo_path =
3586 4e759de4 2019-06-26 stsp strdup(got_worktree_get_repo_path(worktree));
3587 4e759de4 2019-06-26 stsp if (repo_path == NULL)
3588 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3589 4e759de4 2019-06-26 stsp if (error)
3590 4e759de4 2019-06-26 stsp goto done;
3591 4e759de4 2019-06-26 stsp } else {
3592 4e759de4 2019-06-26 stsp repo_path = strdup(cwd);
3593 4e759de4 2019-06-26 stsp if (repo_path == NULL) {
3594 4e759de4 2019-06-26 stsp error = got_error_from_errno("strdup");
3595 4e759de4 2019-06-26 stsp goto done;
3596 4e759de4 2019-06-26 stsp }
3597 4e759de4 2019-06-26 stsp }
3598 4e759de4 2019-06-26 stsp }
3599 4e759de4 2019-06-26 stsp
3600 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
3601 4e759de4 2019-06-26 stsp if (error != NULL)
3602 4e759de4 2019-06-26 stsp goto done;
3603 4e759de4 2019-06-26 stsp
3604 4e759de4 2019-06-26 stsp error = apply_unveil(got_repo_get_path(repo), do_list,
3605 c530dc23 2019-07-23 stsp worktree ? got_worktree_get_root_path(worktree) : NULL);
3606 4e759de4 2019-06-26 stsp if (error)
3607 4e759de4 2019-06-26 stsp goto done;
3608 4e759de4 2019-06-26 stsp
3609 ad89fa31 2019-10-04 stsp if (do_show)
3610 ad89fa31 2019-10-04 stsp error = show_current_branch(repo, worktree);
3611 ad89fa31 2019-10-04 stsp else if (do_list)
3612 ba882ee3 2019-07-11 stsp error = list_branches(repo, worktree);
3613 4e759de4 2019-06-26 stsp else if (delref)
3614 45cd4e47 2019-08-25 stsp error = delete_branch(repo, worktree, delref);
3615 4e759de4 2019-06-26 stsp else {
3616 a74f7e83 2019-11-10 stsp if (commit_id_arg == NULL)
3617 a74f7e83 2019-11-10 stsp commit_id_arg = worktree ?
3618 4e759de4 2019-06-26 stsp got_worktree_get_head_ref_name(worktree) :
3619 4e759de4 2019-06-26 stsp GOT_REF_HEAD;
3620 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
3621 71a27632 2020-01-15 stsp commit_id_arg, GOT_OBJ_TYPE_COMMIT, 1, repo);
3622 a74f7e83 2019-11-10 stsp if (error)
3623 a74f7e83 2019-11-10 stsp goto done;
3624 a74f7e83 2019-11-10 stsp error = add_branch(repo, argv[0], commit_id);
3625 da76fce2 2020-02-24 stsp if (error)
3626 da76fce2 2020-02-24 stsp goto done;
3627 da76fce2 2020-02-24 stsp if (worktree && do_update) {
3628 da76fce2 2020-02-24 stsp int did_something = 0;
3629 da76fce2 2020-02-24 stsp char *branch_refname = NULL;
3630 da76fce2 2020-02-24 stsp
3631 da76fce2 2020-02-24 stsp error = got_object_id_str(&commit_id_str, commit_id);
3632 da76fce2 2020-02-24 stsp if (error)
3633 da76fce2 2020-02-24 stsp goto done;
3634 da76fce2 2020-02-24 stsp error = get_worktree_paths_from_argv(&paths, 0, NULL,
3635 da76fce2 2020-02-24 stsp worktree);
3636 da76fce2 2020-02-24 stsp if (error)
3637 da76fce2 2020-02-24 stsp goto done;
3638 da76fce2 2020-02-24 stsp if (asprintf(&branch_refname, "refs/heads/%s", argv[0])
3639 da76fce2 2020-02-24 stsp == -1) {
3640 da76fce2 2020-02-24 stsp error = got_error_from_errno("asprintf");
3641 da76fce2 2020-02-24 stsp goto done;
3642 da76fce2 2020-02-24 stsp }
3643 da76fce2 2020-02-24 stsp error = got_ref_open(&ref, repo, branch_refname, 0);
3644 da76fce2 2020-02-24 stsp free(branch_refname);
3645 da76fce2 2020-02-24 stsp if (error)
3646 da76fce2 2020-02-24 stsp goto done;
3647 da76fce2 2020-02-24 stsp error = switch_head_ref(ref, commit_id, worktree,
3648 da76fce2 2020-02-24 stsp repo);
3649 da76fce2 2020-02-24 stsp if (error)
3650 da76fce2 2020-02-24 stsp goto done;
3651 da76fce2 2020-02-24 stsp error = got_worktree_set_base_commit_id(worktree, repo,
3652 da76fce2 2020-02-24 stsp commit_id);
3653 da76fce2 2020-02-24 stsp if (error)
3654 da76fce2 2020-02-24 stsp goto done;
3655 da76fce2 2020-02-24 stsp error = got_worktree_checkout_files(worktree, &paths,
3656 da76fce2 2020-02-24 stsp repo, update_progress, &did_something,
3657 da76fce2 2020-02-24 stsp check_cancelled, NULL);
3658 da76fce2 2020-02-24 stsp if (error)
3659 da76fce2 2020-02-24 stsp goto done;
3660 da76fce2 2020-02-24 stsp if (did_something)
3661 da76fce2 2020-02-24 stsp printf("Updated to commit %s\n", commit_id_str);
3662 da76fce2 2020-02-24 stsp }
3663 4e759de4 2019-06-26 stsp }
3664 4e759de4 2019-06-26 stsp done:
3665 da76fce2 2020-02-24 stsp if (ref)
3666 da76fce2 2020-02-24 stsp got_ref_close(ref);
3667 d0eebce4 2019-03-11 stsp if (repo)
3668 d0eebce4 2019-03-11 stsp got_repo_close(repo);
3669 d0eebce4 2019-03-11 stsp if (worktree)
3670 d0eebce4 2019-03-11 stsp got_worktree_close(worktree);
3671 d0eebce4 2019-03-11 stsp free(cwd);
3672 d0eebce4 2019-03-11 stsp free(repo_path);
3673 da76fce2 2020-02-24 stsp free(commit_id);
3674 da76fce2 2020-02-24 stsp free(commit_id_str);
3675 da76fce2 2020-02-24 stsp TAILQ_FOREACH(pe, &paths, entry)
3676 da76fce2 2020-02-24 stsp free((char *)pe->path);
3677 da76fce2 2020-02-24 stsp got_pathlist_free(&paths);
3678 d00136be 2019-03-26 stsp return error;
3679 d00136be 2019-03-26 stsp }
3680 d00136be 2019-03-26 stsp
3681 8e7bd50a 2019-08-22 stsp
3682 d00136be 2019-03-26 stsp __dead static void
3683 8e7bd50a 2019-08-22 stsp usage_tag(void)
3684 8e7bd50a 2019-08-22 stsp {
3685 8e7bd50a 2019-08-22 stsp fprintf(stderr,
3686 80106605 2020-02-24 stsp "usage: %s tag [-c commit] [-r repository] [-l] "
3687 80106605 2020-02-24 stsp "[-m message] name\n", getprogname());
3688 8e7bd50a 2019-08-22 stsp exit(1);
3689 b8bad2ba 2019-08-23 stsp }
3690 b8bad2ba 2019-08-23 stsp
3691 b8bad2ba 2019-08-23 stsp #if 0
3692 b8bad2ba 2019-08-23 stsp static const struct got_error *
3693 b8bad2ba 2019-08-23 stsp sort_tags(struct got_reflist_head *sorted, struct got_reflist_head *tags)
3694 b8bad2ba 2019-08-23 stsp {
3695 b8bad2ba 2019-08-23 stsp const struct got_error *err = NULL;
3696 b8bad2ba 2019-08-23 stsp struct got_reflist_entry *re, *se, *new;
3697 b8bad2ba 2019-08-23 stsp struct got_object_id *re_id, *se_id;
3698 b8bad2ba 2019-08-23 stsp struct got_tag_object *re_tag, *se_tag;
3699 b8bad2ba 2019-08-23 stsp time_t re_time, se_time;
3700 b8bad2ba 2019-08-23 stsp
3701 b8bad2ba 2019-08-23 stsp SIMPLEQ_FOREACH(re, tags, entry) {
3702 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_FIRST(sorted);
3703 b8bad2ba 2019-08-23 stsp if (se == NULL) {
3704 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3705 b8bad2ba 2019-08-23 stsp if (err)
3706 b8bad2ba 2019-08-23 stsp return err;
3707 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_HEAD(sorted, new, entry);
3708 b8bad2ba 2019-08-23 stsp continue;
3709 b8bad2ba 2019-08-23 stsp } else {
3710 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&re_id, repo, re->ref);
3711 b8bad2ba 2019-08-23 stsp if (err)
3712 b8bad2ba 2019-08-23 stsp break;
3713 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&re_tag, repo, re_id);
3714 b8bad2ba 2019-08-23 stsp free(re_id);
3715 b8bad2ba 2019-08-23 stsp if (err)
3716 b8bad2ba 2019-08-23 stsp break;
3717 b8bad2ba 2019-08-23 stsp re_time = got_object_tag_get_tagger_time(re_tag);
3718 b8bad2ba 2019-08-23 stsp got_object_tag_close(re_tag);
3719 b8bad2ba 2019-08-23 stsp }
3720 b8bad2ba 2019-08-23 stsp
3721 b8bad2ba 2019-08-23 stsp while (se) {
3722 b8bad2ba 2019-08-23 stsp err = got_ref_resolve(&se_id, repo, re->ref);
3723 b8bad2ba 2019-08-23 stsp if (err)
3724 b8bad2ba 2019-08-23 stsp break;
3725 b8bad2ba 2019-08-23 stsp err = got_object_open_as_tag(&se_tag, repo, se_id);
3726 b8bad2ba 2019-08-23 stsp free(se_id);
3727 b8bad2ba 2019-08-23 stsp if (err)
3728 b8bad2ba 2019-08-23 stsp break;
3729 b8bad2ba 2019-08-23 stsp se_time = got_object_tag_get_tagger_time(se_tag);
3730 b8bad2ba 2019-08-23 stsp got_object_tag_close(se_tag);
3731 b8bad2ba 2019-08-23 stsp
3732 b8bad2ba 2019-08-23 stsp if (se_time > re_time) {
3733 b8bad2ba 2019-08-23 stsp err = got_reflist_entry_dup(&new, re);
3734 b8bad2ba 2019-08-23 stsp if (err)
3735 b8bad2ba 2019-08-23 stsp return err;
3736 b8bad2ba 2019-08-23 stsp SIMPLEQ_INSERT_AFTER(sorted, se, new, entry);
3737 b8bad2ba 2019-08-23 stsp break;
3738 b8bad2ba 2019-08-23 stsp }
3739 b8bad2ba 2019-08-23 stsp se = SIMPLEQ_NEXT(se, entry);
3740 b8bad2ba 2019-08-23 stsp continue;
3741 b8bad2ba 2019-08-23 stsp }
3742 b8bad2ba 2019-08-23 stsp }
3743 b8bad2ba 2019-08-23 stsp done:
3744 b8bad2ba 2019-08-23 stsp return err;
3745 8e7bd50a 2019-08-22 stsp }
3746 b8bad2ba 2019-08-23 stsp #endif
3747 b8bad2ba 2019-08-23 stsp
3748 b8bad2ba 2019-08-23 stsp static const struct got_error *
3749 8e7bd50a 2019-08-22 stsp list_tags(struct got_repository *repo, struct got_worktree *worktree)
3750 8e7bd50a 2019-08-22 stsp {
3751 8e7bd50a 2019-08-22 stsp static const struct got_error *err = NULL;
3752 8e7bd50a 2019-08-22 stsp struct got_reflist_head refs;
3753 8e7bd50a 2019-08-22 stsp struct got_reflist_entry *re;
3754 8e7bd50a 2019-08-22 stsp
3755 8e7bd50a 2019-08-22 stsp SIMPLEQ_INIT(&refs);
3756 8e7bd50a 2019-08-22 stsp
3757 d1f16636 2020-01-15 stsp err = got_ref_list(&refs, repo, "refs/tags", got_ref_cmp_tags, repo);
3758 8e7bd50a 2019-08-22 stsp if (err)
3759 8e7bd50a 2019-08-22 stsp return err;
3760 8e7bd50a 2019-08-22 stsp
3761 8e7bd50a 2019-08-22 stsp SIMPLEQ_FOREACH(re, &refs, entry) {
3762 8e7bd50a 2019-08-22 stsp const char *refname;
3763 8e7bd50a 2019-08-22 stsp char *refstr, *tagmsg0, *tagmsg, *line, *id_str, *datestr;
3764 8e7bd50a 2019-08-22 stsp char datebuf[26];
3765 d4efa91b 2020-01-14 stsp const char *tagger;
3766 8e7bd50a 2019-08-22 stsp time_t tagger_time;
3767 8e7bd50a 2019-08-22 stsp struct got_object_id *id;
3768 8e7bd50a 2019-08-22 stsp struct got_tag_object *tag;
3769 d4efa91b 2020-01-14 stsp struct got_commit_object *commit = NULL;
3770 8e7bd50a 2019-08-22 stsp
3771 8e7bd50a 2019-08-22 stsp refname = got_ref_get_name(re->ref);
3772 8e7bd50a 2019-08-22 stsp if (strncmp(refname, "refs/tags/", 10) != 0)
3773 8e7bd50a 2019-08-22 stsp continue;
3774 8e7bd50a 2019-08-22 stsp refname += 10;
3775 8e7bd50a 2019-08-22 stsp refstr = got_ref_to_str(re->ref);
3776 8e7bd50a 2019-08-22 stsp if (refstr == NULL) {
3777 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("got_ref_to_str");
3778 8e7bd50a 2019-08-22 stsp break;
3779 8e7bd50a 2019-08-22 stsp }
3780 8e7bd50a 2019-08-22 stsp printf("%stag %s %s\n", GOT_COMMIT_SEP_STR, refname, refstr);
3781 8e7bd50a 2019-08-22 stsp free(refstr);
3782 8e7bd50a 2019-08-22 stsp
3783 8e7bd50a 2019-08-22 stsp err = got_ref_resolve(&id, repo, re->ref);
3784 8e7bd50a 2019-08-22 stsp if (err)
3785 8e7bd50a 2019-08-22 stsp break;
3786 8e7bd50a 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
3787 d4efa91b 2020-01-14 stsp if (err) {
3788 d4efa91b 2020-01-14 stsp if (err->code != GOT_ERR_OBJ_TYPE) {
3789 d4efa91b 2020-01-14 stsp free(id);
3790 d4efa91b 2020-01-14 stsp break;
3791 d4efa91b 2020-01-14 stsp }
3792 d4efa91b 2020-01-14 stsp /* "lightweight" tag */
3793 d4efa91b 2020-01-14 stsp err = got_object_open_as_commit(&commit, repo, id);
3794 d4efa91b 2020-01-14 stsp if (err) {
3795 d4efa91b 2020-01-14 stsp free(id);
3796 d4efa91b 2020-01-14 stsp break;
3797 d4efa91b 2020-01-14 stsp }
3798 d4efa91b 2020-01-14 stsp tagger = got_object_commit_get_committer(commit);
3799 d4efa91b 2020-01-14 stsp tagger_time =
3800 d4efa91b 2020-01-14 stsp got_object_commit_get_committer_time(commit);
3801 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str, id);
3802 d4efa91b 2020-01-14 stsp free(id);
3803 d4efa91b 2020-01-14 stsp if (err)
3804 d4efa91b 2020-01-14 stsp break;
3805 d4efa91b 2020-01-14 stsp } else {
3806 d4efa91b 2020-01-14 stsp free(id);
3807 d4efa91b 2020-01-14 stsp tagger = got_object_tag_get_tagger(tag);
3808 d4efa91b 2020-01-14 stsp tagger_time = got_object_tag_get_tagger_time(tag);
3809 d4efa91b 2020-01-14 stsp err = got_object_id_str(&id_str,
3810 d4efa91b 2020-01-14 stsp got_object_tag_get_object_id(tag));
3811 d4efa91b 2020-01-14 stsp if (err)
3812 d4efa91b 2020-01-14 stsp break;
3813 d4efa91b 2020-01-14 stsp }
3814 d4efa91b 2020-01-14 stsp printf("from: %s\n", tagger);
3815 2417344c 2019-08-23 stsp datestr = get_datestr(&tagger_time, datebuf);
3816 2417344c 2019-08-23 stsp if (datestr)
3817 2417344c 2019-08-23 stsp printf("date: %s UTC\n", datestr);
3818 d4efa91b 2020-01-14 stsp if (commit)
3819 2417344c 2019-08-23 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT, id_str);
3820 d4efa91b 2020-01-14 stsp else {
3821 d4efa91b 2020-01-14 stsp switch (got_object_tag_get_object_type(tag)) {
3822 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_BLOB:
3823 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_BLOB,
3824 d4efa91b 2020-01-14 stsp id_str);
3825 d4efa91b 2020-01-14 stsp break;
3826 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TREE:
3827 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TREE,
3828 d4efa91b 2020-01-14 stsp id_str);
3829 d4efa91b 2020-01-14 stsp break;
3830 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_COMMIT:
3831 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_COMMIT,
3832 d4efa91b 2020-01-14 stsp id_str);
3833 d4efa91b 2020-01-14 stsp break;
3834 d4efa91b 2020-01-14 stsp case GOT_OBJ_TYPE_TAG:
3835 d4efa91b 2020-01-14 stsp printf("object: %s %s\n", GOT_OBJ_LABEL_TAG,
3836 d4efa91b 2020-01-14 stsp id_str);
3837 d4efa91b 2020-01-14 stsp break;
3838 d4efa91b 2020-01-14 stsp default:
3839 d4efa91b 2020-01-14 stsp break;
3840 d4efa91b 2020-01-14 stsp }
3841 8e7bd50a 2019-08-22 stsp }
3842 8e7bd50a 2019-08-22 stsp free(id_str);
3843 d4efa91b 2020-01-14 stsp if (commit) {
3844 d4efa91b 2020-01-14 stsp err = got_object_commit_get_logmsg(&tagmsg0, commit);
3845 d4efa91b 2020-01-14 stsp if (err)
3846 d4efa91b 2020-01-14 stsp break;
3847 d4efa91b 2020-01-14 stsp got_object_commit_close(commit);
3848 d4efa91b 2020-01-14 stsp } else {
3849 d4efa91b 2020-01-14 stsp tagmsg0 = strdup(got_object_tag_get_message(tag));
3850 d4efa91b 2020-01-14 stsp got_object_tag_close(tag);
3851 d4efa91b 2020-01-14 stsp if (tagmsg0 == NULL) {
3852 d4efa91b 2020-01-14 stsp err = got_error_from_errno("strdup");
3853 d4efa91b 2020-01-14 stsp break;
3854 d4efa91b 2020-01-14 stsp }
3855 8e7bd50a 2019-08-22 stsp }
3856 8e7bd50a 2019-08-22 stsp
3857 8e7bd50a 2019-08-22 stsp tagmsg = tagmsg0;
3858 8e7bd50a 2019-08-22 stsp do {
3859 8e7bd50a 2019-08-22 stsp line = strsep(&tagmsg, "\n");
3860 8e7bd50a 2019-08-22 stsp if (line)
3861 8e7bd50a 2019-08-22 stsp printf(" %s\n", line);
3862 8e7bd50a 2019-08-22 stsp } while (line);
3863 8e7bd50a 2019-08-22 stsp free(tagmsg0);
3864 8e7bd50a 2019-08-22 stsp }
3865 8e7bd50a 2019-08-22 stsp
3866 8e7bd50a 2019-08-22 stsp got_ref_list_free(&refs);
3867 8e7bd50a 2019-08-22 stsp return NULL;
3868 8e7bd50a 2019-08-22 stsp }
3869 8e7bd50a 2019-08-22 stsp
3870 8e7bd50a 2019-08-22 stsp static const struct got_error *
3871 f372d5cd 2019-10-21 stsp get_tag_message(char **tagmsg, char **tagmsg_path, const char *commit_id_str,
3872 62870f63 2019-08-22 stsp const char *tag_name, const char *repo_path)
3873 8e7bd50a 2019-08-22 stsp {
3874 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3875 8e7bd50a 2019-08-22 stsp char *template = NULL, *initial_content = NULL;
3876 f372d5cd 2019-10-21 stsp char *editor = NULL;
3877 8e7bd50a 2019-08-22 stsp int fd = -1;
3878 8e7bd50a 2019-08-22 stsp
3879 bb63914a 2020-02-17 stsp if (asprintf(&template, GOT_TMPDIR_STR "/got-tagmsg") == -1) {
3880 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3881 8e7bd50a 2019-08-22 stsp goto done;
3882 8e7bd50a 2019-08-22 stsp }
3883 8e7bd50a 2019-08-22 stsp
3884 62870f63 2019-08-22 stsp if (asprintf(&initial_content, "\n# tagging commit %s as %s\n",
3885 62870f63 2019-08-22 stsp commit_id_str, tag_name) == -1) {
3886 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3887 8e7bd50a 2019-08-22 stsp goto done;
3888 8e7bd50a 2019-08-22 stsp }
3889 8e7bd50a 2019-08-22 stsp
3890 f372d5cd 2019-10-21 stsp err = got_opentemp_named_fd(tagmsg_path, &fd, template);
3891 8e7bd50a 2019-08-22 stsp if (err)
3892 8e7bd50a 2019-08-22 stsp goto done;
3893 8e7bd50a 2019-08-22 stsp
3894 8e7bd50a 2019-08-22 stsp dprintf(fd, initial_content);
3895 8e7bd50a 2019-08-22 stsp close(fd);
3896 8e7bd50a 2019-08-22 stsp
3897 8e7bd50a 2019-08-22 stsp err = get_editor(&editor);
3898 8e7bd50a 2019-08-22 stsp if (err)
3899 8e7bd50a 2019-08-22 stsp goto done;
3900 f372d5cd 2019-10-21 stsp err = edit_logmsg(tagmsg, editor, *tagmsg_path, initial_content);
3901 8e7bd50a 2019-08-22 stsp done:
3902 8e7bd50a 2019-08-22 stsp free(initial_content);
3903 8e7bd50a 2019-08-22 stsp free(template);
3904 8e7bd50a 2019-08-22 stsp free(editor);
3905 8e7bd50a 2019-08-22 stsp
3906 8e7bd50a 2019-08-22 stsp /* Editor is done; we can now apply unveil(2) */
3907 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3908 8e7bd50a 2019-08-22 stsp err = apply_unveil(repo_path, 0, NULL);
3909 8e7bd50a 2019-08-22 stsp if (err) {
3910 8e7bd50a 2019-08-22 stsp free(*tagmsg);
3911 8e7bd50a 2019-08-22 stsp *tagmsg = NULL;
3912 8e7bd50a 2019-08-22 stsp }
3913 8e7bd50a 2019-08-22 stsp }
3914 8e7bd50a 2019-08-22 stsp return err;
3915 8e7bd50a 2019-08-22 stsp }
3916 8e7bd50a 2019-08-22 stsp
3917 8e7bd50a 2019-08-22 stsp static const struct got_error *
3918 8e7bd50a 2019-08-22 stsp add_tag(struct got_repository *repo, const char *tag_name,
3919 8e7bd50a 2019-08-22 stsp const char *commit_arg, const char *tagmsg_arg)
3920 8e7bd50a 2019-08-22 stsp {
3921 8e7bd50a 2019-08-22 stsp const struct got_error *err = NULL;
3922 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id = NULL, *tag_id = NULL;
3923 8e7bd50a 2019-08-22 stsp char *label = NULL, *commit_id_str = NULL;
3924 8e7bd50a 2019-08-22 stsp struct got_reference *ref = NULL;
3925 aba9c984 2019-09-08 stsp char *refname = NULL, *tagmsg = NULL, *tagger = NULL;
3926 f372d5cd 2019-10-21 stsp char *tagmsg_path = NULL, *tag_id_str = NULL;
3927 f372d5cd 2019-10-21 stsp int preserve_tagmsg = 0;
3928 8e7bd50a 2019-08-22 stsp
3929 8e7bd50a 2019-08-22 stsp /*
3930 bd5895f3 2019-11-28 stsp * Don't let the user create a tag name with a leading '-'.
3931 8e7bd50a 2019-08-22 stsp * While technically a valid reference name, this case is usually
3932 8e7bd50a 2019-08-22 stsp * an unintended typo.
3933 8e7bd50a 2019-08-22 stsp */
3934 bd5895f3 2019-11-28 stsp if (tag_name[0] == '-')
3935 bd5895f3 2019-11-28 stsp return got_error_path(tag_name, GOT_ERR_REF_NAME_MINUS);
3936 8e7bd50a 2019-08-22 stsp
3937 aba9c984 2019-09-08 stsp err = get_author(&tagger, repo);
3938 8e7bd50a 2019-08-22 stsp if (err)
3939 8e7bd50a 2019-08-22 stsp return err;
3940 8e7bd50a 2019-08-22 stsp
3941 71a27632 2020-01-15 stsp err = got_repo_match_object_id(&commit_id, &label, commit_arg,
3942 8e7bd50a 2019-08-22 stsp GOT_OBJ_TYPE_COMMIT, 1, repo);
3943 8e7bd50a 2019-08-22 stsp if (err)
3944 8e7bd50a 2019-08-22 stsp goto done;
3945 8e7bd50a 2019-08-22 stsp
3946 8e7bd50a 2019-08-22 stsp err = got_object_id_str(&commit_id_str, commit_id);
3947 8e7bd50a 2019-08-22 stsp if (err)
3948 8e7bd50a 2019-08-22 stsp goto done;
3949 8e7bd50a 2019-08-22 stsp
3950 8e7bd50a 2019-08-22 stsp if (strncmp("refs/tags/", tag_name, 10) == 0) {
3951 8e7bd50a 2019-08-22 stsp refname = strdup(tag_name);
3952 8e7bd50a 2019-08-22 stsp if (refname == NULL) {
3953 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("strdup");
3954 8e7bd50a 2019-08-22 stsp goto done;
3955 8e7bd50a 2019-08-22 stsp }
3956 8e7bd50a 2019-08-22 stsp tag_name += 10;
3957 8e7bd50a 2019-08-22 stsp } else if (asprintf(&refname, "refs/tags/%s", tag_name) == -1) {
3958 8e7bd50a 2019-08-22 stsp err = got_error_from_errno("asprintf");
3959 8e7bd50a 2019-08-22 stsp goto done;
3960 8e7bd50a 2019-08-22 stsp }
3961 8e7bd50a 2019-08-22 stsp
3962 8e7bd50a 2019-08-22 stsp err = got_ref_open(&ref, repo, refname, 0);
3963 8e7bd50a 2019-08-22 stsp if (err == NULL) {
3964 8e7bd50a 2019-08-22 stsp err = got_error(GOT_ERR_TAG_EXISTS);
3965 8e7bd50a 2019-08-22 stsp goto done;
3966 8e7bd50a 2019-08-22 stsp } else if (err->code != GOT_ERR_NOT_REF)
3967 8e7bd50a 2019-08-22 stsp goto done;
3968 8e7bd50a 2019-08-22 stsp
3969 8e7bd50a 2019-08-22 stsp if (tagmsg_arg == NULL) {
3970 f372d5cd 2019-10-21 stsp err = get_tag_message(&tagmsg, &tagmsg_path, commit_id_str,
3971 62870f63 2019-08-22 stsp tag_name, got_repo_get_path(repo));
3972 f372d5cd 2019-10-21 stsp if (err) {
3973 f372d5cd 2019-10-21 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY &&
3974 f372d5cd 2019-10-21 stsp tagmsg_path != NULL)
3975 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3976 8e7bd50a 2019-08-22 stsp goto done;
3977 f372d5cd 2019-10-21 stsp }
3978 8e7bd50a 2019-08-22 stsp }
3979 8e7bd50a 2019-08-22 stsp
3980 8e7bd50a 2019-08-22 stsp err = got_object_tag_create(&tag_id, tag_name, commit_id,
3981 8e7bd50a 2019-08-22 stsp tagger, time(NULL), tagmsg ? tagmsg : tagmsg_arg, repo);
3982 f372d5cd 2019-10-21 stsp if (err) {
3983 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3984 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3985 8e7bd50a 2019-08-22 stsp goto done;
3986 f372d5cd 2019-10-21 stsp }
3987 8e7bd50a 2019-08-22 stsp
3988 8e7bd50a 2019-08-22 stsp err = got_ref_alloc(&ref, refname, tag_id);
3989 f372d5cd 2019-10-21 stsp if (err) {
3990 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3991 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3992 8e7bd50a 2019-08-22 stsp goto done;
3993 f372d5cd 2019-10-21 stsp }
3994 8e7bd50a 2019-08-22 stsp
3995 8e7bd50a 2019-08-22 stsp err = got_ref_write(ref, repo);
3996 f372d5cd 2019-10-21 stsp if (err) {
3997 f372d5cd 2019-10-21 stsp if (tagmsg_path)
3998 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
3999 f372d5cd 2019-10-21 stsp goto done;
4000 f372d5cd 2019-10-21 stsp }
4001 8e7bd50a 2019-08-22 stsp
4002 f372d5cd 2019-10-21 stsp err = got_object_id_str(&tag_id_str, tag_id);
4003 f372d5cd 2019-10-21 stsp if (err) {
4004 f372d5cd 2019-10-21 stsp if (tagmsg_path)
4005 f372d5cd 2019-10-21 stsp preserve_tagmsg = 1;
4006 f372d5cd 2019-10-21 stsp goto done;
4007 8e7bd50a 2019-08-22 stsp }
4008 f372d5cd 2019-10-21 stsp printf("Created tag %s\n", tag_id_str);
4009 8e7bd50a 2019-08-22 stsp done:
4010 f372d5cd 2019-10-21 stsp if (preserve_tagmsg) {
4011 f372d5cd 2019-10-21 stsp fprintf(stderr, "%s: tag message preserved in %s\n",
4012 f372d5cd 2019-10-21 stsp getprogname(), tagmsg_path);
4013 f372d5cd 2019-10-21 stsp } else if (tagmsg_path && unlink(tagmsg_path) == -1 && err == NULL)
4014 f372d5cd 2019-10-21 stsp err = got_error_from_errno2("unlink", tagmsg_path);
4015 f372d5cd 2019-10-21 stsp free(tag_id_str);
4016 8e7bd50a 2019-08-22 stsp if (ref)
4017 8e7bd50a 2019-08-22 stsp got_ref_close(ref);
4018 8e7bd50a 2019-08-22 stsp free(commit_id);
4019 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4020 8e7bd50a 2019-08-22 stsp free(refname);
4021 8e7bd50a 2019-08-22 stsp free(tagmsg);
4022 f372d5cd 2019-10-21 stsp free(tagmsg_path);
4023 aba9c984 2019-09-08 stsp free(tagger);
4024 8e7bd50a 2019-08-22 stsp return err;
4025 8e7bd50a 2019-08-22 stsp }
4026 8e7bd50a 2019-08-22 stsp
4027 8e7bd50a 2019-08-22 stsp static const struct got_error *
4028 8e7bd50a 2019-08-22 stsp cmd_tag(int argc, char *argv[])
4029 8e7bd50a 2019-08-22 stsp {
4030 8e7bd50a 2019-08-22 stsp const struct got_error *error = NULL;
4031 8e7bd50a 2019-08-22 stsp struct got_repository *repo = NULL;
4032 8e7bd50a 2019-08-22 stsp struct got_worktree *worktree = NULL;
4033 8e7bd50a 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *commit_id_str = NULL;
4034 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL;
4035 8e7bd50a 2019-08-22 stsp const char *tag_name, *commit_id_arg = NULL, *tagmsg = NULL;
4036 8e7bd50a 2019-08-22 stsp int ch, do_list = 0;
4037 8e7bd50a 2019-08-22 stsp
4038 80106605 2020-02-24 stsp while ((ch = getopt(argc, argv, "c:m:r:l")) != -1) {
4039 8e7bd50a 2019-08-22 stsp switch (ch) {
4040 80106605 2020-02-24 stsp case 'c':
4041 80106605 2020-02-24 stsp commit_id_arg = optarg;
4042 80106605 2020-02-24 stsp break;
4043 8e7bd50a 2019-08-22 stsp case 'm':
4044 8e7bd50a 2019-08-22 stsp tagmsg = optarg;
4045 8e7bd50a 2019-08-22 stsp break;
4046 8e7bd50a 2019-08-22 stsp case 'r':
4047 8e7bd50a 2019-08-22 stsp repo_path = realpath(optarg, NULL);
4048 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4049 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
4050 9ba1d308 2019-10-21 stsp optarg);
4051 8e7bd50a 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
4052 8e7bd50a 2019-08-22 stsp break;
4053 8e7bd50a 2019-08-22 stsp case 'l':
4054 8e7bd50a 2019-08-22 stsp do_list = 1;
4055 8e7bd50a 2019-08-22 stsp break;
4056 8e7bd50a 2019-08-22 stsp default:
4057 8e7bd50a 2019-08-22 stsp usage_tag();
4058 8e7bd50a 2019-08-22 stsp /* NOTREACHED */
4059 8e7bd50a 2019-08-22 stsp }
4060 8e7bd50a 2019-08-22 stsp }
4061 8e7bd50a 2019-08-22 stsp
4062 8e7bd50a 2019-08-22 stsp argc -= optind;
4063 8e7bd50a 2019-08-22 stsp argv += optind;
4064 8e7bd50a 2019-08-22 stsp
4065 8e7bd50a 2019-08-22 stsp if (do_list) {
4066 80106605 2020-02-24 stsp if (commit_id_arg != NULL)
4067 80106605 2020-02-24 stsp errx(1, "-c option can only be used when creating a tag");
4068 8e7bd50a 2019-08-22 stsp if (tagmsg)
4069 80106605 2020-02-24 stsp errx(1, "-l and -m options are mutually exclusive");
4070 8e7bd50a 2019-08-22 stsp if (argc > 0)
4071 8e7bd50a 2019-08-22 stsp usage_tag();
4072 80106605 2020-02-24 stsp } else if (argc != 1)
4073 8e7bd50a 2019-08-22 stsp usage_tag();
4074 80106605 2020-02-24 stsp
4075 a2887370 2019-08-23 stsp tag_name = argv[0];
4076 8e7bd50a 2019-08-22 stsp
4077 8e7bd50a 2019-08-22 stsp #ifndef PROFILE
4078 8e7bd50a 2019-08-22 stsp if (do_list) {
4079 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath flock proc exec sendfd unveil",
4080 8e7bd50a 2019-08-22 stsp NULL) == -1)
4081 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4082 8e7bd50a 2019-08-22 stsp } else {
4083 8e7bd50a 2019-08-22 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec "
4084 8e7bd50a 2019-08-22 stsp "sendfd unveil", NULL) == -1)
4085 8e7bd50a 2019-08-22 stsp err(1, "pledge");
4086 8e7bd50a 2019-08-22 stsp }
4087 8e7bd50a 2019-08-22 stsp #endif
4088 8e7bd50a 2019-08-22 stsp cwd = getcwd(NULL, 0);
4089 8e7bd50a 2019-08-22 stsp if (cwd == NULL) {
4090 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("getcwd");
4091 8e7bd50a 2019-08-22 stsp goto done;
4092 8e7bd50a 2019-08-22 stsp }
4093 8e7bd50a 2019-08-22 stsp
4094 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4095 8e7bd50a 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
4096 8e7bd50a 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
4097 8e7bd50a 2019-08-22 stsp goto done;
4098 8e7bd50a 2019-08-22 stsp else
4099 8e7bd50a 2019-08-22 stsp error = NULL;
4100 8e7bd50a 2019-08-22 stsp if (worktree) {
4101 8e7bd50a 2019-08-22 stsp repo_path =
4102 8e7bd50a 2019-08-22 stsp strdup(got_worktree_get_repo_path(worktree));
4103 8e7bd50a 2019-08-22 stsp if (repo_path == NULL)
4104 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4105 8e7bd50a 2019-08-22 stsp if (error)
4106 8e7bd50a 2019-08-22 stsp goto done;
4107 8e7bd50a 2019-08-22 stsp } else {
4108 8e7bd50a 2019-08-22 stsp repo_path = strdup(cwd);
4109 8e7bd50a 2019-08-22 stsp if (repo_path == NULL) {
4110 8e7bd50a 2019-08-22 stsp error = got_error_from_errno("strdup");
4111 8e7bd50a 2019-08-22 stsp goto done;
4112 8e7bd50a 2019-08-22 stsp }
4113 8e7bd50a 2019-08-22 stsp }
4114 8e7bd50a 2019-08-22 stsp }
4115 8e7bd50a 2019-08-22 stsp
4116 8e7bd50a 2019-08-22 stsp if (do_list) {
4117 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
4118 c9956ddf 2019-09-08 stsp if (error != NULL)
4119 c9956ddf 2019-09-08 stsp goto done;
4120 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4121 8e7bd50a 2019-08-22 stsp if (error)
4122 8e7bd50a 2019-08-22 stsp goto done;
4123 8e7bd50a 2019-08-22 stsp error = list_tags(repo, worktree);
4124 8e7bd50a 2019-08-22 stsp } else {
4125 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4126 c9956ddf 2019-09-08 stsp if (error)
4127 c9956ddf 2019-09-08 stsp goto done;
4128 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, gitconfig_path);
4129 c9956ddf 2019-09-08 stsp if (error != NULL)
4130 c9956ddf 2019-09-08 stsp goto done;
4131 c9956ddf 2019-09-08 stsp
4132 8e7bd50a 2019-08-22 stsp if (tagmsg) {
4133 8e7bd50a 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
4134 8e7bd50a 2019-08-22 stsp if (error)
4135 8e7bd50a 2019-08-22 stsp goto done;
4136 8e7bd50a 2019-08-22 stsp }
4137 8e7bd50a 2019-08-22 stsp
4138 8e7bd50a 2019-08-22 stsp if (commit_id_arg == NULL) {
4139 8e7bd50a 2019-08-22 stsp struct got_reference *head_ref;
4140 8e7bd50a 2019-08-22 stsp struct got_object_id *commit_id;
4141 8e7bd50a 2019-08-22 stsp error = got_ref_open(&head_ref, repo,
4142 8e7bd50a 2019-08-22 stsp worktree ? got_worktree_get_head_ref_name(worktree)
4143 8e7bd50a 2019-08-22 stsp : GOT_REF_HEAD, 0);
4144 8e7bd50a 2019-08-22 stsp if (error)
4145 8e7bd50a 2019-08-22 stsp goto done;
4146 8e7bd50a 2019-08-22 stsp error = got_ref_resolve(&commit_id, repo, head_ref);
4147 8e7bd50a 2019-08-22 stsp got_ref_close(head_ref);
4148 8e7bd50a 2019-08-22 stsp if (error)
4149 8e7bd50a 2019-08-22 stsp goto done;
4150 8e7bd50a 2019-08-22 stsp error = got_object_id_str(&commit_id_str, commit_id);
4151 8e7bd50a 2019-08-22 stsp free(commit_id);
4152 8e7bd50a 2019-08-22 stsp if (error)
4153 8e7bd50a 2019-08-22 stsp goto done;
4154 8e7bd50a 2019-08-22 stsp }
4155 8e7bd50a 2019-08-22 stsp
4156 8e7bd50a 2019-08-22 stsp error = add_tag(repo, tag_name,
4157 8e7bd50a 2019-08-22 stsp commit_id_str ? commit_id_str : commit_id_arg, tagmsg);
4158 8e7bd50a 2019-08-22 stsp }
4159 8e7bd50a 2019-08-22 stsp done:
4160 8e7bd50a 2019-08-22 stsp if (repo)
4161 8e7bd50a 2019-08-22 stsp got_repo_close(repo);
4162 8e7bd50a 2019-08-22 stsp if (worktree)
4163 8e7bd50a 2019-08-22 stsp got_worktree_close(worktree);
4164 8e7bd50a 2019-08-22 stsp free(cwd);
4165 8e7bd50a 2019-08-22 stsp free(repo_path);
4166 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4167 8e7bd50a 2019-08-22 stsp free(commit_id_str);
4168 8e7bd50a 2019-08-22 stsp return error;
4169 8e7bd50a 2019-08-22 stsp }
4170 8e7bd50a 2019-08-22 stsp
4171 8e7bd50a 2019-08-22 stsp __dead static void
4172 d00136be 2019-03-26 stsp usage_add(void)
4173 d00136be 2019-03-26 stsp {
4174 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s add [-R] [-I] path ...\n",
4175 022fae89 2019-12-06 tracey getprogname());
4176 d00136be 2019-03-26 stsp exit(1);
4177 d00136be 2019-03-26 stsp }
4178 d00136be 2019-03-26 stsp
4179 d00136be 2019-03-26 stsp static const struct got_error *
4180 4e68cba3 2019-11-23 stsp add_progress(void *arg, unsigned char status, const char *path)
4181 4e68cba3 2019-11-23 stsp {
4182 4e68cba3 2019-11-23 stsp while (path[0] == '/')
4183 4e68cba3 2019-11-23 stsp path++;
4184 4e68cba3 2019-11-23 stsp printf("%c %s\n", status, path);
4185 4e68cba3 2019-11-23 stsp return NULL;
4186 4e68cba3 2019-11-23 stsp }
4187 4e68cba3 2019-11-23 stsp
4188 4e68cba3 2019-11-23 stsp static const struct got_error *
4189 d00136be 2019-03-26 stsp cmd_add(int argc, char *argv[])
4190 d00136be 2019-03-26 stsp {
4191 d00136be 2019-03-26 stsp const struct got_error *error = NULL;
4192 031a5338 2019-03-26 stsp struct got_repository *repo = NULL;
4193 d00136be 2019-03-26 stsp struct got_worktree *worktree = NULL;
4194 1dd54920 2019-05-11 stsp char *cwd = NULL;
4195 1dd54920 2019-05-11 stsp struct got_pathlist_head paths;
4196 1dd54920 2019-05-11 stsp struct got_pathlist_entry *pe;
4197 022fae89 2019-12-06 tracey int ch, can_recurse = 0, no_ignores = 0;
4198 1dd54920 2019-05-11 stsp
4199 1dd54920 2019-05-11 stsp TAILQ_INIT(&paths);
4200 d00136be 2019-03-26 stsp
4201 022fae89 2019-12-06 tracey while ((ch = getopt(argc, argv, "IR")) != -1) {
4202 d00136be 2019-03-26 stsp switch (ch) {
4203 022fae89 2019-12-06 tracey case 'I':
4204 022fae89 2019-12-06 tracey no_ignores = 1;
4205 022fae89 2019-12-06 tracey break;
4206 4e68cba3 2019-11-23 stsp case 'R':
4207 4e68cba3 2019-11-23 stsp can_recurse = 1;
4208 4e68cba3 2019-11-23 stsp break;
4209 d00136be 2019-03-26 stsp default:
4210 d00136be 2019-03-26 stsp usage_add();
4211 d00136be 2019-03-26 stsp /* NOTREACHED */
4212 d00136be 2019-03-26 stsp }
4213 d00136be 2019-03-26 stsp }
4214 d00136be 2019-03-26 stsp
4215 d00136be 2019-03-26 stsp argc -= optind;
4216 d00136be 2019-03-26 stsp argv += optind;
4217 d00136be 2019-03-26 stsp
4218 43012d58 2019-07-14 stsp #ifndef PROFILE
4219 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4220 43012d58 2019-07-14 stsp NULL) == -1)
4221 43012d58 2019-07-14 stsp err(1, "pledge");
4222 43012d58 2019-07-14 stsp #endif
4223 723c305c 2019-05-11 jcs if (argc < 1)
4224 d00136be 2019-03-26 stsp usage_add();
4225 d00136be 2019-03-26 stsp
4226 d00136be 2019-03-26 stsp cwd = getcwd(NULL, 0);
4227 d00136be 2019-03-26 stsp if (cwd == NULL) {
4228 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4229 d00136be 2019-03-26 stsp goto done;
4230 d00136be 2019-03-26 stsp }
4231 723c305c 2019-05-11 jcs
4232 d00136be 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4233 d00136be 2019-03-26 stsp if (error)
4234 d00136be 2019-03-26 stsp goto done;
4235 d00136be 2019-03-26 stsp
4236 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4237 c9956ddf 2019-09-08 stsp NULL);
4238 031a5338 2019-03-26 stsp if (error != NULL)
4239 031a5338 2019-03-26 stsp goto done;
4240 031a5338 2019-03-26 stsp
4241 031a5338 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4242 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4243 d00136be 2019-03-26 stsp if (error)
4244 d00136be 2019-03-26 stsp goto done;
4245 d00136be 2019-03-26 stsp
4246 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4247 6d022e97 2019-08-04 stsp if (error)
4248 022fae89 2019-12-06 tracey goto done;
4249 022fae89 2019-12-06 tracey
4250 022fae89 2019-12-06 tracey if (!can_recurse && no_ignores) {
4251 022fae89 2019-12-06 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4252 022fae89 2019-12-06 tracey "disregarding ignores requires -R option");
4253 6d022e97 2019-08-04 stsp goto done;
4254 723c305c 2019-05-11 jcs
4255 022fae89 2019-12-06 tracey }
4256 022fae89 2019-12-06 tracey
4257 4e68cba3 2019-11-23 stsp if (!can_recurse) {
4258 4e68cba3 2019-11-23 stsp char *ondisk_path;
4259 4e68cba3 2019-11-23 stsp struct stat sb;
4260 4e68cba3 2019-11-23 stsp TAILQ_FOREACH(pe, &paths, entry) {
4261 4e68cba3 2019-11-23 stsp if (asprintf(&ondisk_path, "%s/%s",
4262 4e68cba3 2019-11-23 stsp got_worktree_get_root_path(worktree),
4263 4e68cba3 2019-11-23 stsp pe->path) == -1) {
4264 4e68cba3 2019-11-23 stsp error = got_error_from_errno("asprintf");
4265 4e68cba3 2019-11-23 stsp goto done;
4266 4e68cba3 2019-11-23 stsp }
4267 4e68cba3 2019-11-23 stsp if (lstat(ondisk_path, &sb) == -1) {
4268 4e68cba3 2019-11-23 stsp if (errno == ENOENT) {
4269 4e68cba3 2019-11-23 stsp free(ondisk_path);
4270 4e68cba3 2019-11-23 stsp continue;
4271 4e68cba3 2019-11-23 stsp }
4272 4e68cba3 2019-11-23 stsp error = got_error_from_errno2("lstat",
4273 4e68cba3 2019-11-23 stsp ondisk_path);
4274 4e68cba3 2019-11-23 stsp free(ondisk_path);
4275 4e68cba3 2019-11-23 stsp goto done;
4276 4e68cba3 2019-11-23 stsp }
4277 4e68cba3 2019-11-23 stsp free(ondisk_path);
4278 4e68cba3 2019-11-23 stsp if (S_ISDIR(sb.st_mode)) {
4279 4e68cba3 2019-11-23 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4280 4e68cba3 2019-11-23 stsp "adding directories requires -R option");
4281 4e68cba3 2019-11-23 stsp goto done;
4282 4e68cba3 2019-11-23 stsp }
4283 4e68cba3 2019-11-23 stsp }
4284 4e68cba3 2019-11-23 stsp }
4285 022fae89 2019-12-06 tracey
4286 4e68cba3 2019-11-23 stsp error = got_worktree_schedule_add(worktree, &paths, add_progress,
4287 022fae89 2019-12-06 tracey NULL, repo, no_ignores);
4288 d00136be 2019-03-26 stsp done:
4289 031a5338 2019-03-26 stsp if (repo)
4290 031a5338 2019-03-26 stsp got_repo_close(repo);
4291 d00136be 2019-03-26 stsp if (worktree)
4292 d00136be 2019-03-26 stsp got_worktree_close(worktree);
4293 1dd54920 2019-05-11 stsp TAILQ_FOREACH(pe, &paths, entry)
4294 1dd54920 2019-05-11 stsp free((char *)pe->path);
4295 1dd54920 2019-05-11 stsp got_pathlist_free(&paths);
4296 2ec1f75b 2019-03-26 stsp free(cwd);
4297 2ec1f75b 2019-03-26 stsp return error;
4298 2ec1f75b 2019-03-26 stsp }
4299 2ec1f75b 2019-03-26 stsp
4300 2ec1f75b 2019-03-26 stsp __dead static void
4301 648e4ef7 2019-07-09 stsp usage_remove(void)
4302 2ec1f75b 2019-03-26 stsp {
4303 c29c428a 2019-12-16 stsp fprintf(stderr, "usage: %s remove [-f] [-k] [-R] path ...\n",
4304 f2a9dc41 2019-12-13 tracey getprogname());
4305 2ec1f75b 2019-03-26 stsp exit(1);
4306 2a06fe5f 2019-08-24 stsp }
4307 2a06fe5f 2019-08-24 stsp
4308 2a06fe5f 2019-08-24 stsp static const struct got_error *
4309 2a06fe5f 2019-08-24 stsp print_remove_status(void *arg, unsigned char status,
4310 f2a9dc41 2019-12-13 tracey unsigned char staged_status, const char *path)
4311 2a06fe5f 2019-08-24 stsp {
4312 f2a9dc41 2019-12-13 tracey while (path[0] == '/')
4313 f2a9dc41 2019-12-13 tracey path++;
4314 2a06fe5f 2019-08-24 stsp if (status == GOT_STATUS_NONEXISTENT)
4315 2a06fe5f 2019-08-24 stsp return NULL;
4316 2a06fe5f 2019-08-24 stsp if (status == staged_status && (status == GOT_STATUS_DELETE))
4317 2a06fe5f 2019-08-24 stsp status = GOT_STATUS_NO_CHANGE;
4318 2a06fe5f 2019-08-24 stsp printf("%c%c %s\n", status, staged_status, path);
4319 2a06fe5f 2019-08-24 stsp return NULL;
4320 2ec1f75b 2019-03-26 stsp }
4321 2ec1f75b 2019-03-26 stsp
4322 2ec1f75b 2019-03-26 stsp static const struct got_error *
4323 648e4ef7 2019-07-09 stsp cmd_remove(int argc, char *argv[])
4324 2ec1f75b 2019-03-26 stsp {
4325 2ec1f75b 2019-03-26 stsp const struct got_error *error = NULL;
4326 2ec1f75b 2019-03-26 stsp struct got_worktree *worktree = NULL;
4327 2ec1f75b 2019-03-26 stsp struct got_repository *repo = NULL;
4328 17ed4618 2019-06-02 stsp char *cwd = NULL;
4329 17ed4618 2019-06-02 stsp struct got_pathlist_head paths;
4330 17ed4618 2019-06-02 stsp struct got_pathlist_entry *pe;
4331 70e3e7f5 2019-12-13 tracey int ch, delete_local_mods = 0, can_recurse = 0, keep_on_disk = 0;
4332 2ec1f75b 2019-03-26 stsp
4333 17ed4618 2019-06-02 stsp TAILQ_INIT(&paths);
4334 17ed4618 2019-06-02 stsp
4335 70e3e7f5 2019-12-13 tracey while ((ch = getopt(argc, argv, "fkR")) != -1) {
4336 2ec1f75b 2019-03-26 stsp switch (ch) {
4337 2ec1f75b 2019-03-26 stsp case 'f':
4338 2ec1f75b 2019-03-26 stsp delete_local_mods = 1;
4339 2ec1f75b 2019-03-26 stsp break;
4340 70e3e7f5 2019-12-13 tracey case 'k':
4341 70e3e7f5 2019-12-13 tracey keep_on_disk = 1;
4342 70e3e7f5 2019-12-13 tracey break;
4343 f2a9dc41 2019-12-13 tracey case 'R':
4344 f2a9dc41 2019-12-13 tracey can_recurse = 1;
4345 f2a9dc41 2019-12-13 tracey break;
4346 2ec1f75b 2019-03-26 stsp default:
4347 f2a9dc41 2019-12-13 tracey usage_remove();
4348 2ec1f75b 2019-03-26 stsp /* NOTREACHED */
4349 2ec1f75b 2019-03-26 stsp }
4350 2ec1f75b 2019-03-26 stsp }
4351 2ec1f75b 2019-03-26 stsp
4352 2ec1f75b 2019-03-26 stsp argc -= optind;
4353 2ec1f75b 2019-03-26 stsp argv += optind;
4354 2ec1f75b 2019-03-26 stsp
4355 43012d58 2019-07-14 stsp #ifndef PROFILE
4356 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
4357 43012d58 2019-07-14 stsp NULL) == -1)
4358 43012d58 2019-07-14 stsp err(1, "pledge");
4359 43012d58 2019-07-14 stsp #endif
4360 17ed4618 2019-06-02 stsp if (argc < 1)
4361 648e4ef7 2019-07-09 stsp usage_remove();
4362 2ec1f75b 2019-03-26 stsp
4363 2ec1f75b 2019-03-26 stsp cwd = getcwd(NULL, 0);
4364 2ec1f75b 2019-03-26 stsp if (cwd == NULL) {
4365 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4366 2ec1f75b 2019-03-26 stsp goto done;
4367 2ec1f75b 2019-03-26 stsp }
4368 2ec1f75b 2019-03-26 stsp error = got_worktree_open(&worktree, cwd);
4369 2ec1f75b 2019-03-26 stsp if (error)
4370 2ec1f75b 2019-03-26 stsp goto done;
4371 2ec1f75b 2019-03-26 stsp
4372 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4373 c9956ddf 2019-09-08 stsp NULL);
4374 2af4a041 2019-05-11 jcs if (error)
4375 2ec1f75b 2019-03-26 stsp goto done;
4376 2ec1f75b 2019-03-26 stsp
4377 c2253644 2019-03-26 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4378 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4379 2ec1f75b 2019-03-26 stsp if (error)
4380 2ec1f75b 2019-03-26 stsp goto done;
4381 2ec1f75b 2019-03-26 stsp
4382 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4383 6d022e97 2019-08-04 stsp if (error)
4384 6d022e97 2019-08-04 stsp goto done;
4385 17ed4618 2019-06-02 stsp
4386 f2a9dc41 2019-12-13 tracey if (!can_recurse) {
4387 f2a9dc41 2019-12-13 tracey char *ondisk_path;
4388 f2a9dc41 2019-12-13 tracey struct stat sb;
4389 f2a9dc41 2019-12-13 tracey TAILQ_FOREACH(pe, &paths, entry) {
4390 f2a9dc41 2019-12-13 tracey if (asprintf(&ondisk_path, "%s/%s",
4391 f2a9dc41 2019-12-13 tracey got_worktree_get_root_path(worktree),
4392 f2a9dc41 2019-12-13 tracey pe->path) == -1) {
4393 f2a9dc41 2019-12-13 tracey error = got_error_from_errno("asprintf");
4394 f2a9dc41 2019-12-13 tracey goto done;
4395 f2a9dc41 2019-12-13 tracey }
4396 f2a9dc41 2019-12-13 tracey if (lstat(ondisk_path, &sb) == -1) {
4397 f2a9dc41 2019-12-13 tracey if (errno == ENOENT) {
4398 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4399 f2a9dc41 2019-12-13 tracey continue;
4400 f2a9dc41 2019-12-13 tracey }
4401 f2a9dc41 2019-12-13 tracey error = got_error_from_errno2("lstat",
4402 f2a9dc41 2019-12-13 tracey ondisk_path);
4403 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4404 f2a9dc41 2019-12-13 tracey goto done;
4405 f2a9dc41 2019-12-13 tracey }
4406 f2a9dc41 2019-12-13 tracey free(ondisk_path);
4407 f2a9dc41 2019-12-13 tracey if (S_ISDIR(sb.st_mode)) {
4408 f2a9dc41 2019-12-13 tracey error = got_error_msg(GOT_ERR_BAD_PATH,
4409 f2a9dc41 2019-12-13 tracey "removing directories requires -R option");
4410 f2a9dc41 2019-12-13 tracey goto done;
4411 f2a9dc41 2019-12-13 tracey }
4412 f2a9dc41 2019-12-13 tracey }
4413 f2a9dc41 2019-12-13 tracey }
4414 f2a9dc41 2019-12-13 tracey
4415 17ed4618 2019-06-02 stsp error = got_worktree_schedule_delete(worktree, &paths,
4416 70e3e7f5 2019-12-13 tracey delete_local_mods, print_remove_status, NULL, repo, keep_on_disk);
4417 a129376b 2019-03-28 stsp done:
4418 a129376b 2019-03-28 stsp if (repo)
4419 a129376b 2019-03-28 stsp got_repo_close(repo);
4420 a129376b 2019-03-28 stsp if (worktree)
4421 a129376b 2019-03-28 stsp got_worktree_close(worktree);
4422 17ed4618 2019-06-02 stsp TAILQ_FOREACH(pe, &paths, entry)
4423 17ed4618 2019-06-02 stsp free((char *)pe->path);
4424 17ed4618 2019-06-02 stsp got_pathlist_free(&paths);
4425 a129376b 2019-03-28 stsp free(cwd);
4426 a129376b 2019-03-28 stsp return error;
4427 a129376b 2019-03-28 stsp }
4428 a129376b 2019-03-28 stsp
4429 a129376b 2019-03-28 stsp __dead static void
4430 a129376b 2019-03-28 stsp usage_revert(void)
4431 a129376b 2019-03-28 stsp {
4432 33aa809d 2019-08-08 stsp fprintf(stderr, "usage: %s revert [-p] [-F response-script] [-R] "
4433 33aa809d 2019-08-08 stsp "path ...\n", getprogname());
4434 a129376b 2019-03-28 stsp exit(1);
4435 a129376b 2019-03-28 stsp }
4436 a129376b 2019-03-28 stsp
4437 1ee397ad 2019-07-12 stsp static const struct got_error *
4438 a129376b 2019-03-28 stsp revert_progress(void *arg, unsigned char status, const char *path)
4439 a129376b 2019-03-28 stsp {
4440 fb9704af 2020-01-27 stsp if (status == GOT_STATUS_UNVERSIONED)
4441 fb9704af 2020-01-27 stsp return NULL;
4442 fb9704af 2020-01-27 stsp
4443 a129376b 2019-03-28 stsp while (path[0] == '/')
4444 a129376b 2019-03-28 stsp path++;
4445 a129376b 2019-03-28 stsp printf("%c %s\n", status, path);
4446 33aa809d 2019-08-08 stsp return NULL;
4447 33aa809d 2019-08-08 stsp }
4448 33aa809d 2019-08-08 stsp
4449 33aa809d 2019-08-08 stsp struct choose_patch_arg {
4450 33aa809d 2019-08-08 stsp FILE *patch_script_file;
4451 33aa809d 2019-08-08 stsp const char *action;
4452 33aa809d 2019-08-08 stsp };
4453 33aa809d 2019-08-08 stsp
4454 33aa809d 2019-08-08 stsp static const struct got_error *
4455 33aa809d 2019-08-08 stsp show_change(unsigned char status, const char *path, FILE *patch_file, int n,
4456 33aa809d 2019-08-08 stsp int nchanges, const char *action)
4457 33aa809d 2019-08-08 stsp {
4458 33aa809d 2019-08-08 stsp char *line = NULL;
4459 33aa809d 2019-08-08 stsp size_t linesize = 0;
4460 33aa809d 2019-08-08 stsp ssize_t linelen;
4461 33aa809d 2019-08-08 stsp
4462 33aa809d 2019-08-08 stsp switch (status) {
4463 33aa809d 2019-08-08 stsp case GOT_STATUS_ADD:
4464 33aa809d 2019-08-08 stsp printf("A %s\n%s this addition? [y/n] ", path, action);
4465 33aa809d 2019-08-08 stsp break;
4466 33aa809d 2019-08-08 stsp case GOT_STATUS_DELETE:
4467 33aa809d 2019-08-08 stsp printf("D %s\n%s this deletion? [y/n] ", path, action);
4468 33aa809d 2019-08-08 stsp break;
4469 33aa809d 2019-08-08 stsp case GOT_STATUS_MODIFY:
4470 33aa809d 2019-08-08 stsp if (fseek(patch_file, 0L, SEEK_SET) == -1)
4471 33aa809d 2019-08-08 stsp return got_error_from_errno("fseek");
4472 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4473 9516e7cb 2019-08-11 stsp while ((linelen = getline(&line, &linesize, patch_file)) != -1)
4474 33aa809d 2019-08-08 stsp printf("%s", line);
4475 33aa809d 2019-08-08 stsp if (ferror(patch_file))
4476 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4477 33aa809d 2019-08-08 stsp printf(GOT_COMMIT_SEP_STR);
4478 33aa809d 2019-08-08 stsp printf("M %s (change %d of %d)\n%s this change? [y/n/q] ",
4479 33aa809d 2019-08-08 stsp path, n, nchanges, action);
4480 33aa809d 2019-08-08 stsp break;
4481 33aa809d 2019-08-08 stsp default:
4482 33aa809d 2019-08-08 stsp return got_error_path(path, GOT_ERR_FILE_STATUS);
4483 33aa809d 2019-08-08 stsp }
4484 33aa809d 2019-08-08 stsp
4485 33aa809d 2019-08-08 stsp return NULL;
4486 33aa809d 2019-08-08 stsp }
4487 33aa809d 2019-08-08 stsp
4488 33aa809d 2019-08-08 stsp static const struct got_error *
4489 33aa809d 2019-08-08 stsp choose_patch(int *choice, void *arg, unsigned char status, const char *path,
4490 33aa809d 2019-08-08 stsp FILE *patch_file, int n, int nchanges)
4491 33aa809d 2019-08-08 stsp {
4492 33aa809d 2019-08-08 stsp const struct got_error *err = NULL;
4493 33aa809d 2019-08-08 stsp char *line = NULL;
4494 33aa809d 2019-08-08 stsp size_t linesize = 0;
4495 33aa809d 2019-08-08 stsp ssize_t linelen;
4496 33aa809d 2019-08-08 stsp int resp = ' ';
4497 33aa809d 2019-08-08 stsp struct choose_patch_arg *a = arg;
4498 33aa809d 2019-08-08 stsp
4499 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NONE;
4500 33aa809d 2019-08-08 stsp
4501 33aa809d 2019-08-08 stsp if (a->patch_script_file) {
4502 33aa809d 2019-08-08 stsp char *nl;
4503 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4504 33aa809d 2019-08-08 stsp a->action);
4505 33aa809d 2019-08-08 stsp if (err)
4506 33aa809d 2019-08-08 stsp return err;
4507 33aa809d 2019-08-08 stsp linelen = getline(&line, &linesize, a->patch_script_file);
4508 33aa809d 2019-08-08 stsp if (linelen == -1) {
4509 33aa809d 2019-08-08 stsp if (ferror(a->patch_script_file))
4510 33aa809d 2019-08-08 stsp return got_error_from_errno("getline");
4511 33aa809d 2019-08-08 stsp return NULL;
4512 33aa809d 2019-08-08 stsp }
4513 33aa809d 2019-08-08 stsp nl = strchr(line, '\n');
4514 33aa809d 2019-08-08 stsp if (nl)
4515 33aa809d 2019-08-08 stsp *nl = '\0';
4516 33aa809d 2019-08-08 stsp if (strcmp(line, "y") == 0) {
4517 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4518 33aa809d 2019-08-08 stsp printf("y\n");
4519 33aa809d 2019-08-08 stsp } else if (strcmp(line, "n") == 0) {
4520 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4521 33aa809d 2019-08-08 stsp printf("n\n");
4522 33aa809d 2019-08-08 stsp } else if (strcmp(line, "q") == 0 &&
4523 33aa809d 2019-08-08 stsp status == GOT_STATUS_MODIFY) {
4524 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4525 33aa809d 2019-08-08 stsp printf("q\n");
4526 33aa809d 2019-08-08 stsp } else
4527 33aa809d 2019-08-08 stsp printf("invalid response '%s'\n", line);
4528 33aa809d 2019-08-08 stsp free(line);
4529 33aa809d 2019-08-08 stsp return NULL;
4530 33aa809d 2019-08-08 stsp }
4531 33aa809d 2019-08-08 stsp
4532 33aa809d 2019-08-08 stsp while (resp != 'y' && resp != 'n' && resp != 'q') {
4533 33aa809d 2019-08-08 stsp err = show_change(status, path, patch_file, n, nchanges,
4534 33aa809d 2019-08-08 stsp a->action);
4535 33aa809d 2019-08-08 stsp if (err)
4536 33aa809d 2019-08-08 stsp return err;
4537 33aa809d 2019-08-08 stsp resp = getchar();
4538 33aa809d 2019-08-08 stsp if (resp == '\n')
4539 33aa809d 2019-08-08 stsp resp = getchar();
4540 33aa809d 2019-08-08 stsp if (status == GOT_STATUS_MODIFY) {
4541 33aa809d 2019-08-08 stsp if (resp != 'y' && resp != 'n' && resp != 'q') {
4542 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4543 33aa809d 2019-08-08 stsp resp = ' ';
4544 33aa809d 2019-08-08 stsp }
4545 33aa809d 2019-08-08 stsp } else if (resp != 'y' && resp != 'n') {
4546 33aa809d 2019-08-08 stsp printf("invalid response '%c'\n", resp);
4547 33aa809d 2019-08-08 stsp resp = ' ';
4548 33aa809d 2019-08-08 stsp }
4549 33aa809d 2019-08-08 stsp }
4550 33aa809d 2019-08-08 stsp
4551 33aa809d 2019-08-08 stsp if (resp == 'y')
4552 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_YES;
4553 33aa809d 2019-08-08 stsp else if (resp == 'n')
4554 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_NO;
4555 33aa809d 2019-08-08 stsp else if (resp == 'q' && status == GOT_STATUS_MODIFY)
4556 33aa809d 2019-08-08 stsp *choice = GOT_PATCH_CHOICE_QUIT;
4557 33aa809d 2019-08-08 stsp
4558 1ee397ad 2019-07-12 stsp return NULL;
4559 a129376b 2019-03-28 stsp }
4560 a129376b 2019-03-28 stsp
4561 33aa809d 2019-08-08 stsp
4562 a129376b 2019-03-28 stsp static const struct got_error *
4563 a129376b 2019-03-28 stsp cmd_revert(int argc, char *argv[])
4564 a129376b 2019-03-28 stsp {
4565 a129376b 2019-03-28 stsp const struct got_error *error = NULL;
4566 a129376b 2019-03-28 stsp struct got_worktree *worktree = NULL;
4567 a129376b 2019-03-28 stsp struct got_repository *repo = NULL;
4568 a129376b 2019-03-28 stsp char *cwd = NULL, *path = NULL;
4569 e20a8b6f 2019-06-04 stsp struct got_pathlist_head paths;
4570 0f6d7415 2019-08-08 stsp struct got_pathlist_entry *pe;
4571 33aa809d 2019-08-08 stsp int ch, can_recurse = 0, pflag = 0;
4572 33aa809d 2019-08-08 stsp FILE *patch_script_file = NULL;
4573 33aa809d 2019-08-08 stsp const char *patch_script_path = NULL;
4574 33aa809d 2019-08-08 stsp struct choose_patch_arg cpa;
4575 a129376b 2019-03-28 stsp
4576 e20a8b6f 2019-06-04 stsp TAILQ_INIT(&paths);
4577 e20a8b6f 2019-06-04 stsp
4578 33aa809d 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:R")) != -1) {
4579 a129376b 2019-03-28 stsp switch (ch) {
4580 33aa809d 2019-08-08 stsp case 'p':
4581 33aa809d 2019-08-08 stsp pflag = 1;
4582 33aa809d 2019-08-08 stsp break;
4583 33aa809d 2019-08-08 stsp case 'F':
4584 33aa809d 2019-08-08 stsp patch_script_path = optarg;
4585 33aa809d 2019-08-08 stsp break;
4586 0f6d7415 2019-08-08 stsp case 'R':
4587 0f6d7415 2019-08-08 stsp can_recurse = 1;
4588 0f6d7415 2019-08-08 stsp break;
4589 a129376b 2019-03-28 stsp default:
4590 a129376b 2019-03-28 stsp usage_revert();
4591 a129376b 2019-03-28 stsp /* NOTREACHED */
4592 a129376b 2019-03-28 stsp }
4593 a129376b 2019-03-28 stsp }
4594 a129376b 2019-03-28 stsp
4595 a129376b 2019-03-28 stsp argc -= optind;
4596 a129376b 2019-03-28 stsp argv += optind;
4597 a129376b 2019-03-28 stsp
4598 43012d58 2019-07-14 stsp #ifndef PROFILE
4599 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4600 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4601 43012d58 2019-07-14 stsp err(1, "pledge");
4602 43012d58 2019-07-14 stsp #endif
4603 e20a8b6f 2019-06-04 stsp if (argc < 1)
4604 a129376b 2019-03-28 stsp usage_revert();
4605 33aa809d 2019-08-08 stsp if (patch_script_path && !pflag)
4606 33aa809d 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
4607 a129376b 2019-03-28 stsp
4608 a129376b 2019-03-28 stsp cwd = getcwd(NULL, 0);
4609 a129376b 2019-03-28 stsp if (cwd == NULL) {
4610 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4611 a129376b 2019-03-28 stsp goto done;
4612 a129376b 2019-03-28 stsp }
4613 a129376b 2019-03-28 stsp error = got_worktree_open(&worktree, cwd);
4614 2ec1f75b 2019-03-26 stsp if (error)
4615 2ec1f75b 2019-03-26 stsp goto done;
4616 a129376b 2019-03-28 stsp
4617 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4618 c9956ddf 2019-09-08 stsp NULL);
4619 a129376b 2019-03-28 stsp if (error != NULL)
4620 a129376b 2019-03-28 stsp goto done;
4621 a129376b 2019-03-28 stsp
4622 33aa809d 2019-08-08 stsp if (patch_script_path) {
4623 33aa809d 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
4624 33aa809d 2019-08-08 stsp if (patch_script_file == NULL) {
4625 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fopen",
4626 33aa809d 2019-08-08 stsp patch_script_path);
4627 33aa809d 2019-08-08 stsp goto done;
4628 33aa809d 2019-08-08 stsp }
4629 33aa809d 2019-08-08 stsp }
4630 a129376b 2019-03-28 stsp error = apply_unveil(got_repo_get_path(repo), 1,
4631 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4632 a129376b 2019-03-28 stsp if (error)
4633 a129376b 2019-03-28 stsp goto done;
4634 a129376b 2019-03-28 stsp
4635 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4636 6d022e97 2019-08-04 stsp if (error)
4637 6d022e97 2019-08-04 stsp goto done;
4638 00db391e 2019-08-03 stsp
4639 0f6d7415 2019-08-08 stsp if (!can_recurse) {
4640 0f6d7415 2019-08-08 stsp char *ondisk_path;
4641 0f6d7415 2019-08-08 stsp struct stat sb;
4642 0f6d7415 2019-08-08 stsp TAILQ_FOREACH(pe, &paths, entry) {
4643 0f6d7415 2019-08-08 stsp if (asprintf(&ondisk_path, "%s/%s",
4644 0f6d7415 2019-08-08 stsp got_worktree_get_root_path(worktree),
4645 0f6d7415 2019-08-08 stsp pe->path) == -1) {
4646 0f6d7415 2019-08-08 stsp error = got_error_from_errno("asprintf");
4647 0f6d7415 2019-08-08 stsp goto done;
4648 0f6d7415 2019-08-08 stsp }
4649 0f6d7415 2019-08-08 stsp if (lstat(ondisk_path, &sb) == -1) {
4650 0f6d7415 2019-08-08 stsp if (errno == ENOENT) {
4651 0f6d7415 2019-08-08 stsp free(ondisk_path);
4652 0f6d7415 2019-08-08 stsp continue;
4653 0f6d7415 2019-08-08 stsp }
4654 0f6d7415 2019-08-08 stsp error = got_error_from_errno2("lstat",
4655 0f6d7415 2019-08-08 stsp ondisk_path);
4656 0f6d7415 2019-08-08 stsp free(ondisk_path);
4657 0f6d7415 2019-08-08 stsp goto done;
4658 0f6d7415 2019-08-08 stsp }
4659 0f6d7415 2019-08-08 stsp free(ondisk_path);
4660 0f6d7415 2019-08-08 stsp if (S_ISDIR(sb.st_mode)) {
4661 0f6d7415 2019-08-08 stsp error = got_error_msg(GOT_ERR_BAD_PATH,
4662 0f6d7415 2019-08-08 stsp "reverting directories requires -R option");
4663 0f6d7415 2019-08-08 stsp goto done;
4664 0f6d7415 2019-08-08 stsp }
4665 0f6d7415 2019-08-08 stsp }
4666 0f6d7415 2019-08-08 stsp }
4667 0f6d7415 2019-08-08 stsp
4668 33aa809d 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
4669 33aa809d 2019-08-08 stsp cpa.action = "revert";
4670 33aa809d 2019-08-08 stsp error = got_worktree_revert(worktree, &paths, revert_progress, NULL,
4671 33aa809d 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
4672 2ec1f75b 2019-03-26 stsp done:
4673 33aa809d 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
4674 33aa809d 2019-08-08 stsp error == NULL)
4675 33aa809d 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
4676 2ec1f75b 2019-03-26 stsp if (repo)
4677 2ec1f75b 2019-03-26 stsp got_repo_close(repo);
4678 2ec1f75b 2019-03-26 stsp if (worktree)
4679 2ec1f75b 2019-03-26 stsp got_worktree_close(worktree);
4680 2ec1f75b 2019-03-26 stsp free(path);
4681 d00136be 2019-03-26 stsp free(cwd);
4682 6bad629b 2019-02-04 stsp return error;
4683 c4296144 2019-05-09 stsp }
4684 c4296144 2019-05-09 stsp
4685 c4296144 2019-05-09 stsp __dead static void
4686 c4296144 2019-05-09 stsp usage_commit(void)
4687 c4296144 2019-05-09 stsp {
4688 5c1e53bc 2019-07-28 stsp fprintf(stderr, "usage: %s commit [-m msg] [path ...]\n",
4689 5c1e53bc 2019-07-28 stsp getprogname());
4690 c4296144 2019-05-09 stsp exit(1);
4691 33ad4cbe 2019-05-12 jcs }
4692 33ad4cbe 2019-05-12 jcs
4693 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg {
4694 e2ba3d07 2019-05-13 stsp const char *cmdline_log;
4695 e2ba3d07 2019-05-13 stsp const char *editor;
4696 e0870e44 2019-05-13 stsp const char *worktree_path;
4697 76d98825 2019-06-03 stsp const char *branch_name;
4698 314a6357 2019-05-13 stsp const char *repo_path;
4699 e0870e44 2019-05-13 stsp char *logmsg_path;
4700 e2ba3d07 2019-05-13 stsp
4701 e2ba3d07 2019-05-13 stsp };
4702 e0870e44 2019-05-13 stsp
4703 33ad4cbe 2019-05-12 jcs static const struct got_error *
4704 33ad4cbe 2019-05-12 jcs collect_commit_logmsg(struct got_pathlist_head *commitable_paths, char **logmsg,
4705 33ad4cbe 2019-05-12 jcs void *arg)
4706 33ad4cbe 2019-05-12 jcs {
4707 76d98825 2019-06-03 stsp char *initial_content = NULL;
4708 33ad4cbe 2019-05-12 jcs struct got_pathlist_entry *pe;
4709 33ad4cbe 2019-05-12 jcs const struct got_error *err = NULL;
4710 e0870e44 2019-05-13 stsp char *template = NULL;
4711 e2ba3d07 2019-05-13 stsp struct collect_commit_logmsg_arg *a = arg;
4712 3ce1b845 2019-07-15 stsp int fd;
4713 33ad4cbe 2019-05-12 jcs size_t len;
4714 33ad4cbe 2019-05-12 jcs
4715 33ad4cbe 2019-05-12 jcs /* if a message was specified on the command line, just use it */
4716 e2ba3d07 2019-05-13 stsp if (a->cmdline_log != NULL && strlen(a->cmdline_log) != 0) {
4717 e2ba3d07 2019-05-13 stsp len = strlen(a->cmdline_log) + 1;
4718 33ad4cbe 2019-05-12 jcs *logmsg = malloc(len + 1);
4719 9f42ff69 2019-05-13 stsp if (*logmsg == NULL)
4720 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
4721 e2ba3d07 2019-05-13 stsp strlcpy(*logmsg, a->cmdline_log, len);
4722 33ad4cbe 2019-05-12 jcs return NULL;
4723 33ad4cbe 2019-05-12 jcs }
4724 33ad4cbe 2019-05-12 jcs
4725 e0870e44 2019-05-13 stsp if (asprintf(&template, "%s/logmsg", a->worktree_path) == -1)
4726 76d98825 2019-06-03 stsp return got_error_from_errno("asprintf");
4727 76d98825 2019-06-03 stsp
4728 76d98825 2019-06-03 stsp if (asprintf(&initial_content,
4729 76d98825 2019-06-03 stsp "\n# changes to be committed on branch %s:\n",
4730 76d98825 2019-06-03 stsp a->branch_name) == -1)
4731 638f9024 2019-05-13 stsp return got_error_from_errno("asprintf");
4732 e0870e44 2019-05-13 stsp
4733 e0870e44 2019-05-13 stsp err = got_opentemp_named_fd(&a->logmsg_path, &fd, template);
4734 793c30b5 2019-05-13 stsp if (err)
4735 e0870e44 2019-05-13 stsp goto done;
4736 33ad4cbe 2019-05-12 jcs
4737 e0870e44 2019-05-13 stsp dprintf(fd, initial_content);
4738 33ad4cbe 2019-05-12 jcs
4739 33ad4cbe 2019-05-12 jcs TAILQ_FOREACH(pe, commitable_paths, entry) {
4740 33ad4cbe 2019-05-12 jcs struct got_commitable *ct = pe->data;
4741 8656d6c4 2019-05-20 stsp dprintf(fd, "# %c %s\n",
4742 8656d6c4 2019-05-20 stsp got_commitable_get_status(ct),
4743 8656d6c4 2019-05-20 stsp got_commitable_get_path(ct));
4744 33ad4cbe 2019-05-12 jcs }
4745 33ad4cbe 2019-05-12 jcs close(fd);
4746 33ad4cbe 2019-05-12 jcs
4747 3ce1b845 2019-07-15 stsp err = edit_logmsg(logmsg, a->editor, a->logmsg_path, initial_content);
4748 33ad4cbe 2019-05-12 jcs done:
4749 76d98825 2019-06-03 stsp free(initial_content);
4750 e0870e44 2019-05-13 stsp free(template);
4751 314a6357 2019-05-13 stsp
4752 314a6357 2019-05-13 stsp /* Editor is done; we can now apply unveil(2) */
4753 3ce1b845 2019-07-15 stsp if (err == NULL) {
4754 c530dc23 2019-07-23 stsp err = apply_unveil(a->repo_path, 0, a->worktree_path);
4755 3ce1b845 2019-07-15 stsp if (err) {
4756 3ce1b845 2019-07-15 stsp free(*logmsg);
4757 3ce1b845 2019-07-15 stsp *logmsg = NULL;
4758 3ce1b845 2019-07-15 stsp }
4759 3ce1b845 2019-07-15 stsp }
4760 33ad4cbe 2019-05-12 jcs return err;
4761 6bad629b 2019-02-04 stsp }
4762 c4296144 2019-05-09 stsp
4763 c4296144 2019-05-09 stsp static const struct got_error *
4764 c4296144 2019-05-09 stsp cmd_commit(int argc, char *argv[])
4765 c4296144 2019-05-09 stsp {
4766 c4296144 2019-05-09 stsp const struct got_error *error = NULL;
4767 c4296144 2019-05-09 stsp struct got_worktree *worktree = NULL;
4768 c4296144 2019-05-09 stsp struct got_repository *repo = NULL;
4769 5c1e53bc 2019-07-28 stsp char *cwd = NULL, *id_str = NULL;
4770 c4296144 2019-05-09 stsp struct got_object_id *id = NULL;
4771 33ad4cbe 2019-05-12 jcs const char *logmsg = NULL;
4772 aba9c984 2019-09-08 stsp struct collect_commit_logmsg_arg cl_arg;
4773 c9956ddf 2019-09-08 stsp char *gitconfig_path = NULL, *editor = NULL, *author = NULL;
4774 7266f21f 2019-10-21 stsp int ch, rebase_in_progress, histedit_in_progress, preserve_logmsg = 0;
4775 5c1e53bc 2019-07-28 stsp struct got_pathlist_head paths;
4776 5c1e53bc 2019-07-28 stsp
4777 5c1e53bc 2019-07-28 stsp TAILQ_INIT(&paths);
4778 6ac5a73c 2019-08-08 stsp cl_arg.logmsg_path = NULL;
4779 c4296144 2019-05-09 stsp
4780 c4296144 2019-05-09 stsp while ((ch = getopt(argc, argv, "m:")) != -1) {
4781 c4296144 2019-05-09 stsp switch (ch) {
4782 c4296144 2019-05-09 stsp case 'm':
4783 c4296144 2019-05-09 stsp logmsg = optarg;
4784 c4296144 2019-05-09 stsp break;
4785 c4296144 2019-05-09 stsp default:
4786 c4296144 2019-05-09 stsp usage_commit();
4787 c4296144 2019-05-09 stsp /* NOTREACHED */
4788 c4296144 2019-05-09 stsp }
4789 c4296144 2019-05-09 stsp }
4790 c4296144 2019-05-09 stsp
4791 c4296144 2019-05-09 stsp argc -= optind;
4792 c4296144 2019-05-09 stsp argv += optind;
4793 c4296144 2019-05-09 stsp
4794 43012d58 2019-07-14 stsp #ifndef PROFILE
4795 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4796 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4797 43012d58 2019-07-14 stsp err(1, "pledge");
4798 43012d58 2019-07-14 stsp #endif
4799 c4296144 2019-05-09 stsp cwd = getcwd(NULL, 0);
4800 c4296144 2019-05-09 stsp if (cwd == NULL) {
4801 638f9024 2019-05-13 stsp error = got_error_from_errno("getcwd");
4802 c4296144 2019-05-09 stsp goto done;
4803 c4296144 2019-05-09 stsp }
4804 c4296144 2019-05-09 stsp error = got_worktree_open(&worktree, cwd);
4805 7d5807f4 2019-07-11 stsp if (error)
4806 7d5807f4 2019-07-11 stsp goto done;
4807 7d5807f4 2019-07-11 stsp
4808 a698f62e 2019-07-25 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
4809 c4296144 2019-05-09 stsp if (error)
4810 a698f62e 2019-07-25 stsp goto done;
4811 a698f62e 2019-07-25 stsp if (rebase_in_progress) {
4812 a698f62e 2019-07-25 stsp error = got_error(GOT_ERR_REBASING);
4813 c4296144 2019-05-09 stsp goto done;
4814 a698f62e 2019-07-25 stsp }
4815 5c1e53bc 2019-07-28 stsp
4816 916f288c 2019-07-30 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
4817 916f288c 2019-07-30 stsp worktree);
4818 5c1e53bc 2019-07-28 stsp if (error)
4819 5c1e53bc 2019-07-28 stsp goto done;
4820 c4296144 2019-05-09 stsp
4821 c9956ddf 2019-09-08 stsp error = get_gitconfig_path(&gitconfig_path);
4822 c9956ddf 2019-09-08 stsp if (error)
4823 c9956ddf 2019-09-08 stsp goto done;
4824 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4825 c9956ddf 2019-09-08 stsp gitconfig_path);
4826 c4296144 2019-05-09 stsp if (error != NULL)
4827 c4296144 2019-05-09 stsp goto done;
4828 c4296144 2019-05-09 stsp
4829 aba9c984 2019-09-08 stsp error = get_author(&author, repo);
4830 aba9c984 2019-09-08 stsp if (error)
4831 aba9c984 2019-09-08 stsp return error;
4832 aba9c984 2019-09-08 stsp
4833 314a6357 2019-05-13 stsp /*
4834 314a6357 2019-05-13 stsp * unveil(2) traverses exec(2); if an editor is used we have
4835 314a6357 2019-05-13 stsp * to apply unveil after the log message has been written.
4836 314a6357 2019-05-13 stsp */
4837 314a6357 2019-05-13 stsp if (logmsg == NULL || strlen(logmsg) == 0)
4838 314a6357 2019-05-13 stsp error = get_editor(&editor);
4839 314a6357 2019-05-13 stsp else
4840 314a6357 2019-05-13 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4841 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4842 c4296144 2019-05-09 stsp if (error)
4843 c4296144 2019-05-09 stsp goto done;
4844 c4296144 2019-05-09 stsp
4845 087fb88c 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
4846 087fb88c 2019-08-04 stsp if (error)
4847 087fb88c 2019-08-04 stsp goto done;
4848 087fb88c 2019-08-04 stsp
4849 e2ba3d07 2019-05-13 stsp cl_arg.editor = editor;
4850 e2ba3d07 2019-05-13 stsp cl_arg.cmdline_log = logmsg;
4851 e0870e44 2019-05-13 stsp cl_arg.worktree_path = got_worktree_get_root_path(worktree);
4852 76d98825 2019-06-03 stsp cl_arg.branch_name = got_worktree_get_head_ref_name(worktree);
4853 916f288c 2019-07-30 stsp if (!histedit_in_progress) {
4854 916f288c 2019-07-30 stsp if (strncmp(cl_arg.branch_name, "refs/heads/", 11) != 0) {
4855 916f288c 2019-07-30 stsp error = got_error(GOT_ERR_COMMIT_BRANCH);
4856 916f288c 2019-07-30 stsp goto done;
4857 916f288c 2019-07-30 stsp }
4858 916f288c 2019-07-30 stsp cl_arg.branch_name += 11;
4859 916f288c 2019-07-30 stsp }
4860 314a6357 2019-05-13 stsp cl_arg.repo_path = got_repo_get_path(repo);
4861 84792843 2019-08-09 stsp error = got_worktree_commit(&id, worktree, &paths, author, NULL,
4862 e2ba3d07 2019-05-13 stsp collect_commit_logmsg, &cl_arg, print_status, NULL, repo);
4863 e0870e44 2019-05-13 stsp if (error) {
4864 7266f21f 2019-10-21 stsp if (error->code != GOT_ERR_COMMIT_MSG_EMPTY &&
4865 7266f21f 2019-10-21 stsp cl_arg.logmsg_path != NULL)
4866 7266f21f 2019-10-21 stsp preserve_logmsg = 1;
4867 c4296144 2019-05-09 stsp goto done;
4868 e0870e44 2019-05-13 stsp }
4869 c4296144 2019-05-09 stsp
4870 c4296144 2019-05-09 stsp error = got_object_id_str(&id_str, id);
4871 c4296144 2019-05-09 stsp if (error)
4872 c4296144 2019-05-09 stsp goto done;
4873 a7648d7a 2019-06-02 stsp printf("Created commit %s\n", id_str);
4874 c4296144 2019-05-09 stsp done:
4875 7266f21f 2019-10-21 stsp if (preserve_logmsg) {
4876 7266f21f 2019-10-21 stsp fprintf(stderr, "%s: log message preserved in %s\n",
4877 7266f21f 2019-10-21 stsp getprogname(), cl_arg.logmsg_path);
4878 7266f21f 2019-10-21 stsp } else if (cl_arg.logmsg_path && unlink(cl_arg.logmsg_path) == -1 &&
4879 7266f21f 2019-10-21 stsp error == NULL)
4880 7266f21f 2019-10-21 stsp error = got_error_from_errno2("unlink", cl_arg.logmsg_path);
4881 6ac5a73c 2019-08-08 stsp free(cl_arg.logmsg_path);
4882 c4296144 2019-05-09 stsp if (repo)
4883 c4296144 2019-05-09 stsp got_repo_close(repo);
4884 c4296144 2019-05-09 stsp if (worktree)
4885 c4296144 2019-05-09 stsp got_worktree_close(worktree);
4886 c4296144 2019-05-09 stsp free(cwd);
4887 c4296144 2019-05-09 stsp free(id_str);
4888 c9956ddf 2019-09-08 stsp free(gitconfig_path);
4889 e2ba3d07 2019-05-13 stsp free(editor);
4890 aba9c984 2019-09-08 stsp free(author);
4891 234035bc 2019-06-01 stsp return error;
4892 234035bc 2019-06-01 stsp }
4893 234035bc 2019-06-01 stsp
4894 234035bc 2019-06-01 stsp __dead static void
4895 234035bc 2019-06-01 stsp usage_cherrypick(void)
4896 234035bc 2019-06-01 stsp {
4897 234035bc 2019-06-01 stsp fprintf(stderr, "usage: %s cherrypick commit-id\n", getprogname());
4898 234035bc 2019-06-01 stsp exit(1);
4899 234035bc 2019-06-01 stsp }
4900 234035bc 2019-06-01 stsp
4901 234035bc 2019-06-01 stsp static const struct got_error *
4902 234035bc 2019-06-01 stsp cmd_cherrypick(int argc, char *argv[])
4903 234035bc 2019-06-01 stsp {
4904 234035bc 2019-06-01 stsp const struct got_error *error = NULL;
4905 234035bc 2019-06-01 stsp struct got_worktree *worktree = NULL;
4906 234035bc 2019-06-01 stsp struct got_repository *repo = NULL;
4907 234035bc 2019-06-01 stsp char *cwd = NULL, *commit_id_str = NULL;
4908 234035bc 2019-06-01 stsp struct got_object_id *commit_id = NULL;
4909 234035bc 2019-06-01 stsp struct got_commit_object *commit = NULL;
4910 234035bc 2019-06-01 stsp struct got_object_qid *pid;
4911 234035bc 2019-06-01 stsp struct got_reference *head_ref = NULL;
4912 234035bc 2019-06-01 stsp int ch, did_something = 0;
4913 234035bc 2019-06-01 stsp
4914 234035bc 2019-06-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
4915 234035bc 2019-06-01 stsp switch (ch) {
4916 234035bc 2019-06-01 stsp default:
4917 234035bc 2019-06-01 stsp usage_cherrypick();
4918 234035bc 2019-06-01 stsp /* NOTREACHED */
4919 234035bc 2019-06-01 stsp }
4920 234035bc 2019-06-01 stsp }
4921 234035bc 2019-06-01 stsp
4922 234035bc 2019-06-01 stsp argc -= optind;
4923 234035bc 2019-06-01 stsp argv += optind;
4924 234035bc 2019-06-01 stsp
4925 43012d58 2019-07-14 stsp #ifndef PROFILE
4926 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
4927 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
4928 43012d58 2019-07-14 stsp err(1, "pledge");
4929 43012d58 2019-07-14 stsp #endif
4930 234035bc 2019-06-01 stsp if (argc != 1)
4931 234035bc 2019-06-01 stsp usage_cherrypick();
4932 234035bc 2019-06-01 stsp
4933 234035bc 2019-06-01 stsp cwd = getcwd(NULL, 0);
4934 234035bc 2019-06-01 stsp if (cwd == NULL) {
4935 234035bc 2019-06-01 stsp error = got_error_from_errno("getcwd");
4936 234035bc 2019-06-01 stsp goto done;
4937 234035bc 2019-06-01 stsp }
4938 234035bc 2019-06-01 stsp error = got_worktree_open(&worktree, cwd);
4939 234035bc 2019-06-01 stsp if (error)
4940 234035bc 2019-06-01 stsp goto done;
4941 234035bc 2019-06-01 stsp
4942 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
4943 c9956ddf 2019-09-08 stsp NULL);
4944 234035bc 2019-06-01 stsp if (error != NULL)
4945 234035bc 2019-06-01 stsp goto done;
4946 234035bc 2019-06-01 stsp
4947 234035bc 2019-06-01 stsp error = apply_unveil(got_repo_get_path(repo), 0,
4948 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
4949 234035bc 2019-06-01 stsp if (error)
4950 234035bc 2019-06-01 stsp goto done;
4951 234035bc 2019-06-01 stsp
4952 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
4953 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
4954 234035bc 2019-06-01 stsp if (error != NULL) {
4955 234035bc 2019-06-01 stsp struct got_reference *ref;
4956 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
4957 234035bc 2019-06-01 stsp goto done;
4958 234035bc 2019-06-01 stsp error = got_ref_open(&ref, repo, argv[0], 0);
4959 234035bc 2019-06-01 stsp if (error != NULL)
4960 234035bc 2019-06-01 stsp goto done;
4961 234035bc 2019-06-01 stsp error = got_ref_resolve(&commit_id, repo, ref);
4962 234035bc 2019-06-01 stsp got_ref_close(ref);
4963 234035bc 2019-06-01 stsp if (error != NULL)
4964 234035bc 2019-06-01 stsp goto done;
4965 234035bc 2019-06-01 stsp }
4966 234035bc 2019-06-01 stsp error = got_object_id_str(&commit_id_str, commit_id);
4967 234035bc 2019-06-01 stsp if (error)
4968 234035bc 2019-06-01 stsp goto done;
4969 234035bc 2019-06-01 stsp
4970 234035bc 2019-06-01 stsp error = got_ref_open(&head_ref, repo,
4971 234035bc 2019-06-01 stsp got_worktree_get_head_ref_name(worktree), 0);
4972 234035bc 2019-06-01 stsp if (error != NULL)
4973 234035bc 2019-06-01 stsp goto done;
4974 234035bc 2019-06-01 stsp
4975 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
4976 234035bc 2019-06-01 stsp if (error) {
4977 234035bc 2019-06-01 stsp if (error->code != GOT_ERR_ANCESTRY)
4978 234035bc 2019-06-01 stsp goto done;
4979 234035bc 2019-06-01 stsp error = NULL;
4980 234035bc 2019-06-01 stsp } else {
4981 234035bc 2019-06-01 stsp error = got_error(GOT_ERR_SAME_BRANCH);
4982 234035bc 2019-06-01 stsp goto done;
4983 234035bc 2019-06-01 stsp }
4984 234035bc 2019-06-01 stsp
4985 234035bc 2019-06-01 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
4986 234035bc 2019-06-01 stsp if (error)
4987 234035bc 2019-06-01 stsp goto done;
4988 234035bc 2019-06-01 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
4989 03415a1a 2019-06-02 stsp error = got_worktree_merge_files(worktree, pid ? pid->id : NULL,
4990 03415a1a 2019-06-02 stsp commit_id, repo, update_progress, &did_something, check_cancelled,
4991 03415a1a 2019-06-02 stsp NULL);
4992 234035bc 2019-06-01 stsp if (error != NULL)
4993 234035bc 2019-06-01 stsp goto done;
4994 234035bc 2019-06-01 stsp
4995 234035bc 2019-06-01 stsp if (did_something)
4996 a7648d7a 2019-06-02 stsp printf("Merged commit %s\n", commit_id_str);
4997 234035bc 2019-06-01 stsp done:
4998 234035bc 2019-06-01 stsp if (commit)
4999 234035bc 2019-06-01 stsp got_object_commit_close(commit);
5000 234035bc 2019-06-01 stsp free(commit_id_str);
5001 234035bc 2019-06-01 stsp if (head_ref)
5002 234035bc 2019-06-01 stsp got_ref_close(head_ref);
5003 234035bc 2019-06-01 stsp if (worktree)
5004 234035bc 2019-06-01 stsp got_worktree_close(worktree);
5005 234035bc 2019-06-01 stsp if (repo)
5006 234035bc 2019-06-01 stsp got_repo_close(repo);
5007 c4296144 2019-05-09 stsp return error;
5008 c4296144 2019-05-09 stsp }
5009 5ef14e63 2019-06-02 stsp
5010 5ef14e63 2019-06-02 stsp __dead static void
5011 5ef14e63 2019-06-02 stsp usage_backout(void)
5012 5ef14e63 2019-06-02 stsp {
5013 5ef14e63 2019-06-02 stsp fprintf(stderr, "usage: %s backout commit-id\n", getprogname());
5014 5ef14e63 2019-06-02 stsp exit(1);
5015 5ef14e63 2019-06-02 stsp }
5016 5ef14e63 2019-06-02 stsp
5017 5ef14e63 2019-06-02 stsp static const struct got_error *
5018 5ef14e63 2019-06-02 stsp cmd_backout(int argc, char *argv[])
5019 5ef14e63 2019-06-02 stsp {
5020 5ef14e63 2019-06-02 stsp const struct got_error *error = NULL;
5021 5ef14e63 2019-06-02 stsp struct got_worktree *worktree = NULL;
5022 5ef14e63 2019-06-02 stsp struct got_repository *repo = NULL;
5023 5ef14e63 2019-06-02 stsp char *cwd = NULL, *commit_id_str = NULL;
5024 5ef14e63 2019-06-02 stsp struct got_object_id *commit_id = NULL;
5025 5ef14e63 2019-06-02 stsp struct got_commit_object *commit = NULL;
5026 5ef14e63 2019-06-02 stsp struct got_object_qid *pid;
5027 5ef14e63 2019-06-02 stsp struct got_reference *head_ref = NULL;
5028 5ef14e63 2019-06-02 stsp int ch, did_something = 0;
5029 5ef14e63 2019-06-02 stsp
5030 5ef14e63 2019-06-02 stsp while ((ch = getopt(argc, argv, "")) != -1) {
5031 5ef14e63 2019-06-02 stsp switch (ch) {
5032 5ef14e63 2019-06-02 stsp default:
5033 5ef14e63 2019-06-02 stsp usage_backout();
5034 5ef14e63 2019-06-02 stsp /* NOTREACHED */
5035 5ef14e63 2019-06-02 stsp }
5036 5ef14e63 2019-06-02 stsp }
5037 5ef14e63 2019-06-02 stsp
5038 5ef14e63 2019-06-02 stsp argc -= optind;
5039 5ef14e63 2019-06-02 stsp argv += optind;
5040 5ef14e63 2019-06-02 stsp
5041 43012d58 2019-07-14 stsp #ifndef PROFILE
5042 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5043 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5044 43012d58 2019-07-14 stsp err(1, "pledge");
5045 43012d58 2019-07-14 stsp #endif
5046 5ef14e63 2019-06-02 stsp if (argc != 1)
5047 5ef14e63 2019-06-02 stsp usage_backout();
5048 5ef14e63 2019-06-02 stsp
5049 5ef14e63 2019-06-02 stsp cwd = getcwd(NULL, 0);
5050 5ef14e63 2019-06-02 stsp if (cwd == NULL) {
5051 5ef14e63 2019-06-02 stsp error = got_error_from_errno("getcwd");
5052 5ef14e63 2019-06-02 stsp goto done;
5053 5ef14e63 2019-06-02 stsp }
5054 5ef14e63 2019-06-02 stsp error = got_worktree_open(&worktree, cwd);
5055 5ef14e63 2019-06-02 stsp if (error)
5056 5ef14e63 2019-06-02 stsp goto done;
5057 5ef14e63 2019-06-02 stsp
5058 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5059 c9956ddf 2019-09-08 stsp NULL);
5060 5ef14e63 2019-06-02 stsp if (error != NULL)
5061 5ef14e63 2019-06-02 stsp goto done;
5062 5ef14e63 2019-06-02 stsp
5063 5ef14e63 2019-06-02 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5064 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5065 5ef14e63 2019-06-02 stsp if (error)
5066 5ef14e63 2019-06-02 stsp goto done;
5067 5ef14e63 2019-06-02 stsp
5068 dd88155e 2019-06-29 stsp error = got_repo_match_object_id_prefix(&commit_id, argv[0],
5069 dd88155e 2019-06-29 stsp GOT_OBJ_TYPE_COMMIT, repo);
5070 5ef14e63 2019-06-02 stsp if (error != NULL) {
5071 5ef14e63 2019-06-02 stsp struct got_reference *ref;
5072 5ef14e63 2019-06-02 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR)
5073 5ef14e63 2019-06-02 stsp goto done;
5074 5ef14e63 2019-06-02 stsp error = got_ref_open(&ref, repo, argv[0], 0);
5075 5ef14e63 2019-06-02 stsp if (error != NULL)
5076 5ef14e63 2019-06-02 stsp goto done;
5077 5ef14e63 2019-06-02 stsp error = got_ref_resolve(&commit_id, repo, ref);
5078 5ef14e63 2019-06-02 stsp got_ref_close(ref);
5079 5ef14e63 2019-06-02 stsp if (error != NULL)
5080 5ef14e63 2019-06-02 stsp goto done;
5081 5ef14e63 2019-06-02 stsp }
5082 5ef14e63 2019-06-02 stsp error = got_object_id_str(&commit_id_str, commit_id);
5083 5ef14e63 2019-06-02 stsp if (error)
5084 5ef14e63 2019-06-02 stsp goto done;
5085 5ef14e63 2019-06-02 stsp
5086 5ef14e63 2019-06-02 stsp error = got_ref_open(&head_ref, repo,
5087 5ef14e63 2019-06-02 stsp got_worktree_get_head_ref_name(worktree), 0);
5088 5ef14e63 2019-06-02 stsp if (error != NULL)
5089 5ef14e63 2019-06-02 stsp goto done;
5090 5ef14e63 2019-06-02 stsp
5091 a51a74b3 2019-07-27 stsp error = check_same_branch(commit_id, head_ref, NULL, repo);
5092 5ef14e63 2019-06-02 stsp if (error)
5093 5ef14e63 2019-06-02 stsp goto done;
5094 5ef14e63 2019-06-02 stsp
5095 5ef14e63 2019-06-02 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5096 5ef14e63 2019-06-02 stsp if (error)
5097 5ef14e63 2019-06-02 stsp goto done;
5098 5ef14e63 2019-06-02 stsp pid = SIMPLEQ_FIRST(got_object_commit_get_parent_ids(commit));
5099 5ef14e63 2019-06-02 stsp if (pid == NULL) {
5100 5ef14e63 2019-06-02 stsp error = got_error(GOT_ERR_ROOT_COMMIT);
5101 5ef14e63 2019-06-02 stsp goto done;
5102 5ef14e63 2019-06-02 stsp }
5103 5ef14e63 2019-06-02 stsp
5104 5ef14e63 2019-06-02 stsp error = got_worktree_merge_files(worktree, commit_id, pid->id, repo,
5105 5ef14e63 2019-06-02 stsp update_progress, &did_something, check_cancelled, NULL);
5106 5ef14e63 2019-06-02 stsp if (error != NULL)
5107 5ef14e63 2019-06-02 stsp goto done;
5108 5ef14e63 2019-06-02 stsp
5109 5ef14e63 2019-06-02 stsp if (did_something)
5110 a7648d7a 2019-06-02 stsp printf("Backed out commit %s\n", commit_id_str);
5111 5ef14e63 2019-06-02 stsp done:
5112 5ef14e63 2019-06-02 stsp if (commit)
5113 5ef14e63 2019-06-02 stsp got_object_commit_close(commit);
5114 5ef14e63 2019-06-02 stsp free(commit_id_str);
5115 5ef14e63 2019-06-02 stsp if (head_ref)
5116 5ef14e63 2019-06-02 stsp got_ref_close(head_ref);
5117 818c7501 2019-07-11 stsp if (worktree)
5118 818c7501 2019-07-11 stsp got_worktree_close(worktree);
5119 818c7501 2019-07-11 stsp if (repo)
5120 818c7501 2019-07-11 stsp got_repo_close(repo);
5121 818c7501 2019-07-11 stsp return error;
5122 818c7501 2019-07-11 stsp }
5123 818c7501 2019-07-11 stsp
5124 818c7501 2019-07-11 stsp __dead static void
5125 818c7501 2019-07-11 stsp usage_rebase(void)
5126 818c7501 2019-07-11 stsp {
5127 af54c8f8 2019-07-11 stsp fprintf(stderr, "usage: %s rebase [-a] | [-c] | branch\n",
5128 af54c8f8 2019-07-11 stsp getprogname());
5129 818c7501 2019-07-11 stsp exit(1);
5130 0ebf8283 2019-07-24 stsp }
5131 0ebf8283 2019-07-24 stsp
5132 0ebf8283 2019-07-24 stsp void
5133 0ebf8283 2019-07-24 stsp trim_logmsg(char *logmsg, int limit)
5134 0ebf8283 2019-07-24 stsp {
5135 0ebf8283 2019-07-24 stsp char *nl;
5136 0ebf8283 2019-07-24 stsp size_t len;
5137 0ebf8283 2019-07-24 stsp
5138 0ebf8283 2019-07-24 stsp len = strlen(logmsg);
5139 0ebf8283 2019-07-24 stsp if (len > limit)
5140 0ebf8283 2019-07-24 stsp len = limit;
5141 0ebf8283 2019-07-24 stsp logmsg[len] = '\0';
5142 0ebf8283 2019-07-24 stsp nl = strchr(logmsg, '\n');
5143 0ebf8283 2019-07-24 stsp if (nl)
5144 0ebf8283 2019-07-24 stsp *nl = '\0';
5145 0ebf8283 2019-07-24 stsp }
5146 0ebf8283 2019-07-24 stsp
5147 0ebf8283 2019-07-24 stsp static const struct got_error *
5148 0ebf8283 2019-07-24 stsp get_short_logmsg(char **logmsg, int limit, struct got_commit_object *commit)
5149 0ebf8283 2019-07-24 stsp {
5150 5943eee2 2019-08-13 stsp const struct got_error *err;
5151 5943eee2 2019-08-13 stsp char *logmsg0 = NULL;
5152 5943eee2 2019-08-13 stsp const char *s;
5153 0ebf8283 2019-07-24 stsp
5154 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&logmsg0, commit);
5155 5943eee2 2019-08-13 stsp if (err)
5156 5943eee2 2019-08-13 stsp return err;
5157 0ebf8283 2019-07-24 stsp
5158 5943eee2 2019-08-13 stsp s = logmsg0;
5159 5943eee2 2019-08-13 stsp while (isspace((unsigned char)s[0]))
5160 5943eee2 2019-08-13 stsp s++;
5161 5943eee2 2019-08-13 stsp
5162 5943eee2 2019-08-13 stsp *logmsg = strdup(s);
5163 5943eee2 2019-08-13 stsp if (*logmsg == NULL) {
5164 5943eee2 2019-08-13 stsp err = got_error_from_errno("strdup");
5165 5943eee2 2019-08-13 stsp goto done;
5166 5943eee2 2019-08-13 stsp }
5167 0ebf8283 2019-07-24 stsp
5168 0ebf8283 2019-07-24 stsp trim_logmsg(*logmsg, limit);
5169 5943eee2 2019-08-13 stsp done:
5170 5943eee2 2019-08-13 stsp free(logmsg0);
5171 a0ea4fc0 2020-02-28 stsp return err;
5172 a0ea4fc0 2020-02-28 stsp }
5173 a0ea4fc0 2020-02-28 stsp
5174 a0ea4fc0 2020-02-28 stsp static const struct got_error *
5175 a0ea4fc0 2020-02-28 stsp show_rebase_merge_conflict(struct got_object_id *id, struct got_repository *repo)
5176 a0ea4fc0 2020-02-28 stsp {
5177 a0ea4fc0 2020-02-28 stsp const struct got_error *err;
5178 a0ea4fc0 2020-02-28 stsp struct got_commit_object *commit = NULL;
5179 a0ea4fc0 2020-02-28 stsp char *id_str = NULL, *logmsg = NULL;
5180 a0ea4fc0 2020-02-28 stsp
5181 a0ea4fc0 2020-02-28 stsp err = got_object_open_as_commit(&commit, repo, id);
5182 a0ea4fc0 2020-02-28 stsp if (err)
5183 a0ea4fc0 2020-02-28 stsp return err;
5184 a0ea4fc0 2020-02-28 stsp
5185 a0ea4fc0 2020-02-28 stsp err = got_object_id_str(&id_str, id);
5186 a0ea4fc0 2020-02-28 stsp if (err)
5187 a0ea4fc0 2020-02-28 stsp goto done;
5188 a0ea4fc0 2020-02-28 stsp
5189 a0ea4fc0 2020-02-28 stsp id_str[12] = '\0';
5190 a0ea4fc0 2020-02-28 stsp
5191 a0ea4fc0 2020-02-28 stsp err = get_short_logmsg(&logmsg, 42, commit);
5192 a0ea4fc0 2020-02-28 stsp if (err)
5193 a0ea4fc0 2020-02-28 stsp goto done;
5194 a0ea4fc0 2020-02-28 stsp
5195 a0ea4fc0 2020-02-28 stsp printf("%s -> merge conflict: %s\n", id_str, logmsg);
5196 a0ea4fc0 2020-02-28 stsp done:
5197 a0ea4fc0 2020-02-28 stsp free(id_str);
5198 a0ea4fc0 2020-02-28 stsp got_object_commit_close(commit);
5199 a0ea4fc0 2020-02-28 stsp free(logmsg);
5200 5943eee2 2019-08-13 stsp return err;
5201 818c7501 2019-07-11 stsp }
5202 818c7501 2019-07-11 stsp
5203 818c7501 2019-07-11 stsp static const struct got_error *
5204 818c7501 2019-07-11 stsp show_rebase_progress(struct got_commit_object *commit,
5205 818c7501 2019-07-11 stsp struct got_object_id *old_id, struct got_object_id *new_id)
5206 818c7501 2019-07-11 stsp {
5207 818c7501 2019-07-11 stsp const struct got_error *err;
5208 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
5209 818c7501 2019-07-11 stsp
5210 818c7501 2019-07-11 stsp err = got_object_id_str(&old_id_str, old_id);
5211 818c7501 2019-07-11 stsp if (err)
5212 818c7501 2019-07-11 stsp goto done;
5213 818c7501 2019-07-11 stsp
5214 ff0d2220 2019-07-11 stsp if (new_id) {
5215 ff0d2220 2019-07-11 stsp err = got_object_id_str(&new_id_str, new_id);
5216 ff0d2220 2019-07-11 stsp if (err)
5217 ff0d2220 2019-07-11 stsp goto done;
5218 ff0d2220 2019-07-11 stsp }
5219 818c7501 2019-07-11 stsp
5220 818c7501 2019-07-11 stsp old_id_str[12] = '\0';
5221 ff0d2220 2019-07-11 stsp if (new_id_str)
5222 ff0d2220 2019-07-11 stsp new_id_str[12] = '\0';
5223 0ebf8283 2019-07-24 stsp
5224 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
5225 0ebf8283 2019-07-24 stsp if (err)
5226 0ebf8283 2019-07-24 stsp goto done;
5227 0ebf8283 2019-07-24 stsp
5228 ff0d2220 2019-07-11 stsp printf("%s -> %s: %s\n", old_id_str,
5229 ff0d2220 2019-07-11 stsp new_id_str ? new_id_str : "no-op change", logmsg);
5230 818c7501 2019-07-11 stsp done:
5231 818c7501 2019-07-11 stsp free(old_id_str);
5232 818c7501 2019-07-11 stsp free(new_id_str);
5233 272a1371 2020-02-28 stsp free(logmsg);
5234 818c7501 2019-07-11 stsp return err;
5235 818c7501 2019-07-11 stsp }
5236 818c7501 2019-07-11 stsp
5237 1ee397ad 2019-07-12 stsp static const struct got_error *
5238 818c7501 2019-07-11 stsp rebase_progress(void *arg, unsigned char status, const char *path)
5239 818c7501 2019-07-11 stsp {
5240 818c7501 2019-07-11 stsp unsigned char *rebase_status = arg;
5241 818c7501 2019-07-11 stsp
5242 818c7501 2019-07-11 stsp while (path[0] == '/')
5243 818c7501 2019-07-11 stsp path++;
5244 818c7501 2019-07-11 stsp printf("%c %s\n", status, path);
5245 818c7501 2019-07-11 stsp
5246 818c7501 2019-07-11 stsp if (*rebase_status == GOT_STATUS_CONFLICT)
5247 1ee397ad 2019-07-12 stsp return NULL;
5248 818c7501 2019-07-11 stsp if (status == GOT_STATUS_CONFLICT || status == GOT_STATUS_MERGE)
5249 818c7501 2019-07-11 stsp *rebase_status = status;
5250 1ee397ad 2019-07-12 stsp return NULL;
5251 818c7501 2019-07-11 stsp }
5252 818c7501 2019-07-11 stsp
5253 818c7501 2019-07-11 stsp static const struct got_error *
5254 3e3a69f1 2019-07-25 stsp rebase_complete(struct got_worktree *worktree, struct got_fileindex *fileindex,
5255 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_reference *new_base_branch,
5256 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_repository *repo)
5257 818c7501 2019-07-11 stsp {
5258 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n", got_ref_get_name(branch));
5259 3e3a69f1 2019-07-25 stsp return got_worktree_rebase_complete(worktree, fileindex,
5260 818c7501 2019-07-11 stsp new_base_branch, tmp_branch, branch, repo);
5261 818c7501 2019-07-11 stsp }
5262 818c7501 2019-07-11 stsp
5263 818c7501 2019-07-11 stsp static const struct got_error *
5264 01757395 2019-07-12 stsp rebase_commit(struct got_pathlist_head *merged_paths,
5265 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
5266 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch,
5267 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id, struct got_repository *repo)
5268 ff0d2220 2019-07-11 stsp {
5269 ff0d2220 2019-07-11 stsp const struct got_error *error;
5270 ff0d2220 2019-07-11 stsp struct got_commit_object *commit;
5271 ff0d2220 2019-07-11 stsp struct got_object_id *new_commit_id;
5272 ff0d2220 2019-07-11 stsp
5273 ff0d2220 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5274 ff0d2220 2019-07-11 stsp if (error)
5275 ff0d2220 2019-07-11 stsp return error;
5276 ff0d2220 2019-07-11 stsp
5277 01757395 2019-07-12 stsp error = got_worktree_rebase_commit(&new_commit_id, merged_paths,
5278 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, commit_id, repo);
5279 ff0d2220 2019-07-11 stsp if (error) {
5280 ff0d2220 2019-07-11 stsp if (error->code != GOT_ERR_COMMIT_NO_CHANGES)
5281 ff0d2220 2019-07-11 stsp goto done;
5282 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, NULL);
5283 ff0d2220 2019-07-11 stsp } else {
5284 ff0d2220 2019-07-11 stsp error = show_rebase_progress(commit, commit_id, new_commit_id);
5285 ff0d2220 2019-07-11 stsp free(new_commit_id);
5286 ff0d2220 2019-07-11 stsp }
5287 ff0d2220 2019-07-11 stsp done:
5288 ff0d2220 2019-07-11 stsp got_object_commit_close(commit);
5289 ff0d2220 2019-07-11 stsp return error;
5290 64c6d990 2019-07-11 stsp }
5291 64c6d990 2019-07-11 stsp
5292 64c6d990 2019-07-11 stsp struct check_path_prefix_arg {
5293 64c6d990 2019-07-11 stsp const char *path_prefix;
5294 64c6d990 2019-07-11 stsp size_t len;
5295 8ca9bd68 2019-07-25 stsp int errcode;
5296 64c6d990 2019-07-11 stsp };
5297 64c6d990 2019-07-11 stsp
5298 64c6d990 2019-07-11 stsp static const struct got_error *
5299 8ca9bd68 2019-07-25 stsp check_path_prefix_in_diff(void *arg, struct got_blob_object *blob1,
5300 64c6d990 2019-07-11 stsp struct got_blob_object *blob2, struct got_object_id *id1,
5301 64c6d990 2019-07-11 stsp struct got_object_id *id2, const char *path1, const char *path2,
5302 46f68b20 2019-10-19 stsp mode_t mode1, mode_t mode2, struct got_repository *repo)
5303 64c6d990 2019-07-11 stsp {
5304 64c6d990 2019-07-11 stsp struct check_path_prefix_arg *a = arg;
5305 64c6d990 2019-07-11 stsp
5306 64c6d990 2019-07-11 stsp if ((path1 && !got_path_is_child(path1, a->path_prefix, a->len)) ||
5307 64c6d990 2019-07-11 stsp (path2 && !got_path_is_child(path2, a->path_prefix, a->len)))
5308 8ca9bd68 2019-07-25 stsp return got_error(a->errcode);
5309 64c6d990 2019-07-11 stsp
5310 64c6d990 2019-07-11 stsp return NULL;
5311 64c6d990 2019-07-11 stsp }
5312 64c6d990 2019-07-11 stsp
5313 64c6d990 2019-07-11 stsp static const struct got_error *
5314 8ca9bd68 2019-07-25 stsp check_path_prefix(struct got_object_id *parent_id,
5315 64c6d990 2019-07-11 stsp struct got_object_id *commit_id, const char *path_prefix,
5316 8ca9bd68 2019-07-25 stsp int errcode, struct got_repository *repo)
5317 64c6d990 2019-07-11 stsp {
5318 64c6d990 2019-07-11 stsp const struct got_error *err;
5319 64c6d990 2019-07-11 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
5320 64c6d990 2019-07-11 stsp struct got_commit_object *commit = NULL, *parent_commit = NULL;
5321 64c6d990 2019-07-11 stsp struct check_path_prefix_arg cpp_arg;
5322 64c6d990 2019-07-11 stsp
5323 64c6d990 2019-07-11 stsp if (got_path_is_root_dir(path_prefix))
5324 64c6d990 2019-07-11 stsp return NULL;
5325 64c6d990 2019-07-11 stsp
5326 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5327 64c6d990 2019-07-11 stsp if (err)
5328 64c6d990 2019-07-11 stsp goto done;
5329 64c6d990 2019-07-11 stsp
5330 64c6d990 2019-07-11 stsp err = got_object_open_as_commit(&parent_commit, repo, parent_id);
5331 64c6d990 2019-07-11 stsp if (err)
5332 64c6d990 2019-07-11 stsp goto done;
5333 64c6d990 2019-07-11 stsp
5334 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree1, repo,
5335 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(parent_commit));
5336 64c6d990 2019-07-11 stsp if (err)
5337 64c6d990 2019-07-11 stsp goto done;
5338 64c6d990 2019-07-11 stsp
5339 64c6d990 2019-07-11 stsp err = got_object_open_as_tree(&tree2, repo,
5340 64c6d990 2019-07-11 stsp got_object_commit_get_tree_id(commit));
5341 64c6d990 2019-07-11 stsp if (err)
5342 64c6d990 2019-07-11 stsp goto done;
5343 64c6d990 2019-07-11 stsp
5344 64c6d990 2019-07-11 stsp cpp_arg.path_prefix = path_prefix;
5345 d23ace97 2019-07-25 stsp while (cpp_arg.path_prefix[0] == '/')
5346 d23ace97 2019-07-25 stsp cpp_arg.path_prefix++;
5347 d23ace97 2019-07-25 stsp cpp_arg.len = strlen(cpp_arg.path_prefix);
5348 8ca9bd68 2019-07-25 stsp cpp_arg.errcode = errcode;
5349 8ca9bd68 2019-07-25 stsp err = got_diff_tree(tree1, tree2, "", "", repo,
5350 31b4484f 2019-07-27 stsp check_path_prefix_in_diff, &cpp_arg, 0);
5351 64c6d990 2019-07-11 stsp done:
5352 64c6d990 2019-07-11 stsp if (tree1)
5353 64c6d990 2019-07-11 stsp got_object_tree_close(tree1);
5354 64c6d990 2019-07-11 stsp if (tree2)
5355 64c6d990 2019-07-11 stsp got_object_tree_close(tree2);
5356 64c6d990 2019-07-11 stsp if (commit)
5357 64c6d990 2019-07-11 stsp got_object_commit_close(commit);
5358 64c6d990 2019-07-11 stsp if (parent_commit)
5359 64c6d990 2019-07-11 stsp got_object_commit_close(parent_commit);
5360 64c6d990 2019-07-11 stsp return err;
5361 ff0d2220 2019-07-11 stsp }
5362 ff0d2220 2019-07-11 stsp
5363 ff0d2220 2019-07-11 stsp static const struct got_error *
5364 8ca9bd68 2019-07-25 stsp collect_commits(struct got_object_id_queue *commits,
5365 af61c510 2019-07-19 stsp struct got_object_id *initial_commit_id,
5366 af61c510 2019-07-19 stsp struct got_object_id *iter_start_id, struct got_object_id *iter_stop_id,
5367 8ca9bd68 2019-07-25 stsp const char *path_prefix, int path_prefix_errcode,
5368 8ca9bd68 2019-07-25 stsp struct got_repository *repo)
5369 af61c510 2019-07-19 stsp {
5370 af61c510 2019-07-19 stsp const struct got_error *err = NULL;
5371 af61c510 2019-07-19 stsp struct got_commit_graph *graph = NULL;
5372 af61c510 2019-07-19 stsp struct got_object_id *parent_id = NULL;
5373 af61c510 2019-07-19 stsp struct got_object_qid *qid;
5374 af61c510 2019-07-19 stsp struct got_object_id *commit_id = initial_commit_id;
5375 af61c510 2019-07-19 stsp
5376 3d509237 2020-01-04 stsp err = got_commit_graph_open(&graph, "/", 1);
5377 af61c510 2019-07-19 stsp if (err)
5378 af61c510 2019-07-19 stsp return err;
5379 af61c510 2019-07-19 stsp
5380 6fb7cd11 2019-08-22 stsp err = got_commit_graph_iter_start(graph, iter_start_id, repo,
5381 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5382 af61c510 2019-07-19 stsp if (err)
5383 af61c510 2019-07-19 stsp goto done;
5384 af61c510 2019-07-19 stsp while (got_object_id_cmp(commit_id, iter_stop_id) != 0) {
5385 ee780d5c 2020-01-04 stsp err = got_commit_graph_iter_next(&parent_id, graph, repo,
5386 ee780d5c 2020-01-04 stsp check_cancelled, NULL);
5387 af61c510 2019-07-19 stsp if (err) {
5388 af61c510 2019-07-19 stsp if (err->code == GOT_ERR_ITER_COMPLETED) {
5389 af61c510 2019-07-19 stsp err = got_error_msg(GOT_ERR_ANCESTRY,
5390 af61c510 2019-07-19 stsp "ran out of commits to rebase before "
5391 af61c510 2019-07-19 stsp "youngest common ancestor commit has "
5392 af61c510 2019-07-19 stsp "been reached?!?");
5393 ee780d5c 2020-01-04 stsp }
5394 ee780d5c 2020-01-04 stsp goto done;
5395 af61c510 2019-07-19 stsp } else {
5396 8ca9bd68 2019-07-25 stsp err = check_path_prefix(parent_id, commit_id,
5397 8ca9bd68 2019-07-25 stsp path_prefix, path_prefix_errcode, repo);
5398 af61c510 2019-07-19 stsp if (err)
5399 af61c510 2019-07-19 stsp goto done;
5400 af61c510 2019-07-19 stsp
5401 af61c510 2019-07-19 stsp err = got_object_qid_alloc(&qid, commit_id);
5402 af61c510 2019-07-19 stsp if (err)
5403 af61c510 2019-07-19 stsp goto done;
5404 af61c510 2019-07-19 stsp SIMPLEQ_INSERT_HEAD(commits, qid, entry);
5405 af61c510 2019-07-19 stsp commit_id = parent_id;
5406 af61c510 2019-07-19 stsp }
5407 af61c510 2019-07-19 stsp }
5408 af61c510 2019-07-19 stsp done:
5409 af61c510 2019-07-19 stsp got_commit_graph_close(graph);
5410 af61c510 2019-07-19 stsp return err;
5411 af61c510 2019-07-19 stsp }
5412 af61c510 2019-07-19 stsp
5413 af61c510 2019-07-19 stsp static const struct got_error *
5414 818c7501 2019-07-11 stsp cmd_rebase(int argc, char *argv[])
5415 818c7501 2019-07-11 stsp {
5416 818c7501 2019-07-11 stsp const struct got_error *error = NULL;
5417 818c7501 2019-07-11 stsp struct got_worktree *worktree = NULL;
5418 818c7501 2019-07-11 stsp struct got_repository *repo = NULL;
5419 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
5420 818c7501 2019-07-11 stsp char *cwd = NULL;
5421 818c7501 2019-07-11 stsp struct got_reference *branch = NULL;
5422 818c7501 2019-07-11 stsp struct got_reference *new_base_branch = NULL, *tmp_branch = NULL;
5423 818c7501 2019-07-11 stsp struct got_object_id *commit_id = NULL, *parent_id = NULL;
5424 818c7501 2019-07-11 stsp struct got_object_id *resume_commit_id = NULL;
5425 818c7501 2019-07-11 stsp struct got_object_id *branch_head_commit_id = NULL, *yca_id = NULL;
5426 818c7501 2019-07-11 stsp struct got_commit_object *commit = NULL;
5427 818c7501 2019-07-11 stsp int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
5428 7ef62c4e 2020-02-24 stsp int histedit_in_progress = 0;
5429 818c7501 2019-07-11 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
5430 818c7501 2019-07-11 stsp struct got_object_id_queue commits;
5431 01757395 2019-07-12 stsp struct got_pathlist_head merged_paths;
5432 818c7501 2019-07-11 stsp const struct got_object_id_queue *parent_ids;
5433 818c7501 2019-07-11 stsp struct got_object_qid *qid, *pid;
5434 818c7501 2019-07-11 stsp
5435 818c7501 2019-07-11 stsp SIMPLEQ_INIT(&commits);
5436 01757395 2019-07-12 stsp TAILQ_INIT(&merged_paths);
5437 818c7501 2019-07-11 stsp
5438 818c7501 2019-07-11 stsp while ((ch = getopt(argc, argv, "ac")) != -1) {
5439 818c7501 2019-07-11 stsp switch (ch) {
5440 818c7501 2019-07-11 stsp case 'a':
5441 818c7501 2019-07-11 stsp abort_rebase = 1;
5442 818c7501 2019-07-11 stsp break;
5443 818c7501 2019-07-11 stsp case 'c':
5444 818c7501 2019-07-11 stsp continue_rebase = 1;
5445 818c7501 2019-07-11 stsp break;
5446 818c7501 2019-07-11 stsp default:
5447 818c7501 2019-07-11 stsp usage_rebase();
5448 818c7501 2019-07-11 stsp /* NOTREACHED */
5449 818c7501 2019-07-11 stsp }
5450 818c7501 2019-07-11 stsp }
5451 818c7501 2019-07-11 stsp
5452 818c7501 2019-07-11 stsp argc -= optind;
5453 818c7501 2019-07-11 stsp argv += optind;
5454 818c7501 2019-07-11 stsp
5455 43012d58 2019-07-14 stsp #ifndef PROFILE
5456 43012d58 2019-07-14 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
5457 43012d58 2019-07-14 stsp "unveil", NULL) == -1)
5458 43012d58 2019-07-14 stsp err(1, "pledge");
5459 43012d58 2019-07-14 stsp #endif
5460 818c7501 2019-07-11 stsp if (abort_rebase && continue_rebase)
5461 818c7501 2019-07-11 stsp usage_rebase();
5462 818c7501 2019-07-11 stsp else if (abort_rebase || continue_rebase) {
5463 818c7501 2019-07-11 stsp if (argc != 0)
5464 818c7501 2019-07-11 stsp usage_rebase();
5465 818c7501 2019-07-11 stsp } else if (argc != 1)
5466 818c7501 2019-07-11 stsp usage_rebase();
5467 818c7501 2019-07-11 stsp
5468 818c7501 2019-07-11 stsp cwd = getcwd(NULL, 0);
5469 818c7501 2019-07-11 stsp if (cwd == NULL) {
5470 818c7501 2019-07-11 stsp error = got_error_from_errno("getcwd");
5471 818c7501 2019-07-11 stsp goto done;
5472 818c7501 2019-07-11 stsp }
5473 818c7501 2019-07-11 stsp error = got_worktree_open(&worktree, cwd);
5474 818c7501 2019-07-11 stsp if (error)
5475 818c7501 2019-07-11 stsp goto done;
5476 818c7501 2019-07-11 stsp
5477 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
5478 c9956ddf 2019-09-08 stsp NULL);
5479 818c7501 2019-07-11 stsp if (error != NULL)
5480 818c7501 2019-07-11 stsp goto done;
5481 818c7501 2019-07-11 stsp
5482 818c7501 2019-07-11 stsp error = apply_unveil(got_repo_get_path(repo), 0,
5483 c530dc23 2019-07-23 stsp got_worktree_get_root_path(worktree));
5484 7ef62c4e 2020-02-24 stsp if (error)
5485 7ef62c4e 2020-02-24 stsp goto done;
5486 7ef62c4e 2020-02-24 stsp
5487 7ef62c4e 2020-02-24 stsp error = got_worktree_histedit_in_progress(&histedit_in_progress,
5488 7ef62c4e 2020-02-24 stsp worktree);
5489 818c7501 2019-07-11 stsp if (error)
5490 7ef62c4e 2020-02-24 stsp goto done;
5491 7ef62c4e 2020-02-24 stsp if (histedit_in_progress) {
5492 7ef62c4e 2020-02-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
5493 818c7501 2019-07-11 stsp goto done;
5494 7ef62c4e 2020-02-24 stsp }
5495 818c7501 2019-07-11 stsp
5496 818c7501 2019-07-11 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
5497 818c7501 2019-07-11 stsp if (error)
5498 818c7501 2019-07-11 stsp goto done;
5499 818c7501 2019-07-11 stsp
5500 f6794adc 2019-07-23 stsp if (abort_rebase) {
5501 818c7501 2019-07-11 stsp int did_something;
5502 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5503 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5504 f6794adc 2019-07-23 stsp goto done;
5505 f6794adc 2019-07-23 stsp }
5506 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5507 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5508 3e3a69f1 2019-07-25 stsp worktree, repo);
5509 818c7501 2019-07-11 stsp if (error)
5510 818c7501 2019-07-11 stsp goto done;
5511 818c7501 2019-07-11 stsp printf("Switching work tree to %s\n",
5512 818c7501 2019-07-11 stsp got_ref_get_symref_target(new_base_branch));
5513 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_abort(worktree, fileindex, repo,
5514 818c7501 2019-07-11 stsp new_base_branch, update_progress, &did_something);
5515 818c7501 2019-07-11 stsp if (error)
5516 818c7501 2019-07-11 stsp goto done;
5517 818c7501 2019-07-11 stsp printf("Rebase of %s aborted\n", got_ref_get_name(branch));
5518 818c7501 2019-07-11 stsp goto done; /* nothing else to do */
5519 818c7501 2019-07-11 stsp }
5520 818c7501 2019-07-11 stsp
5521 818c7501 2019-07-11 stsp if (continue_rebase) {
5522 f6794adc 2019-07-23 stsp if (!rebase_in_progress) {
5523 f6794adc 2019-07-23 stsp error = got_error(GOT_ERR_NOT_REBASING);
5524 f6794adc 2019-07-23 stsp goto done;
5525 f6794adc 2019-07-23 stsp }
5526 818c7501 2019-07-11 stsp error = got_worktree_rebase_continue(&resume_commit_id,
5527 3e3a69f1 2019-07-25 stsp &new_base_branch, &tmp_branch, &branch, &fileindex,
5528 3e3a69f1 2019-07-25 stsp worktree, repo);
5529 818c7501 2019-07-11 stsp if (error)
5530 818c7501 2019-07-11 stsp goto done;
5531 818c7501 2019-07-11 stsp
5532 3e3a69f1 2019-07-25 stsp error = rebase_commit(NULL, worktree, fileindex, tmp_branch,
5533 01757395 2019-07-12 stsp resume_commit_id, repo);
5534 818c7501 2019-07-11 stsp if (error)
5535 818c7501 2019-07-11 stsp goto done;
5536 818c7501 2019-07-11 stsp
5537 ff0d2220 2019-07-11 stsp yca_id = got_object_id_dup(resume_commit_id);
5538 ff0d2220 2019-07-11 stsp if (yca_id == NULL) {
5539 818c7501 2019-07-11 stsp error = got_error_from_errno("got_object_id_dup");
5540 818c7501 2019-07-11 stsp goto done;
5541 818c7501 2019-07-11 stsp }
5542 818c7501 2019-07-11 stsp } else {
5543 818c7501 2019-07-11 stsp error = got_ref_open(&branch, repo, argv[0], 0);
5544 818c7501 2019-07-11 stsp if (error != NULL)
5545 818c7501 2019-07-11 stsp goto done;
5546 ff0d2220 2019-07-11 stsp }
5547 818c7501 2019-07-11 stsp
5548 ff0d2220 2019-07-11 stsp error = got_ref_resolve(&branch_head_commit_id, repo, branch);
5549 ff0d2220 2019-07-11 stsp if (error)
5550 ff0d2220 2019-07-11 stsp goto done;
5551 ff0d2220 2019-07-11 stsp
5552 ff0d2220 2019-07-11 stsp if (!continue_rebase) {
5553 a51a74b3 2019-07-27 stsp struct got_object_id *base_commit_id;
5554 a51a74b3 2019-07-27 stsp
5555 a51a74b3 2019-07-27 stsp base_commit_id = got_worktree_get_base_commit_id(worktree);
5556 818c7501 2019-07-11 stsp error = got_commit_graph_find_youngest_common_ancestor(&yca_id,
5557 6fb7cd11 2019-08-22 stsp base_commit_id, branch_head_commit_id, repo,
5558 6fb7cd11 2019-08-22 stsp check_cancelled, NULL);
5559 818c7501 2019-07-11 stsp if (error)
5560 818c7501 2019-07-11 stsp goto done;
5561 818c7501 2019-07-11 stsp if (yca_id == NULL) {
5562 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_ANCESTRY,
5563 818c7501 2019-07-11 stsp "specified branch shares no common ancestry "
5564 818c7501 2019-07-11 stsp "with work tree's branch");
5565 818c7501 2019-07-11 stsp goto done;
5566 818c7501 2019-07-11 stsp }
5567 818c7501 2019-07-11 stsp
5568 a51a74b3 2019-07-27 stsp error = check_same_branch(base_commit_id, branch, yca_id, repo);
5569 a51a74b3 2019-07-27 stsp if (error) {
5570 a51a74b3 2019-07-27 stsp if (error->code != GOT_ERR_ANCESTRY)
5571 a51a74b3 2019-07-27 stsp goto done;
5572 a51a74b3 2019-07-27 stsp error = NULL;
5573 a51a74b3 2019-07-27 stsp } else {
5574 a51a74b3 2019-07-27 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
5575 a51a74b3 2019-07-27 stsp "specified branch resolves to a commit which "
5576 a51a74b3 2019-07-27 stsp "is already contained in work tree's branch");
5577 a51a74b3 2019-07-27 stsp goto done;
5578 a51a74b3 2019-07-27 stsp }
5579 818c7501 2019-07-11 stsp error = got_worktree_rebase_prepare(&new_base_branch,
5580 3e3a69f1 2019-07-25 stsp &tmp_branch, &fileindex, worktree, branch, repo);
5581 818c7501 2019-07-11 stsp if (error)
5582 818c7501 2019-07-11 stsp goto done;
5583 818c7501 2019-07-11 stsp }
5584 818c7501 2019-07-11 stsp
5585 ff0d2220 2019-07-11 stsp commit_id = branch_head_commit_id;
5586 818c7501 2019-07-11 stsp error = got_object_open_as_commit(&commit, repo, commit_id);
5587 818c7501 2019-07-11 stsp if (error)
5588 818c7501 2019-07-11 stsp goto done;
5589 818c7501 2019-07-11 stsp
5590 818c7501 2019-07-11 stsp parent_ids = got_object_commit_get_parent_ids(commit);
5591 818c7501 2019-07-11 stsp pid = SIMPLEQ_FIRST(parent_ids);
5592 fc66b545 2019-08-12 stsp if (pid == NULL) {
5593 fc66b545 2019-08-12 stsp if (!continue_rebase) {
5594 fc66b545 2019-08-12 stsp int did_something;
5595 fc66b545 2019-08-12 stsp error = got_worktree_rebase_abort(worktree, fileindex,
5596 fc66b545 2019-08-12 stsp repo, new_base_branch, update_progress,
5597 fc66b545 2019-08-12 stsp &did_something);
5598 fc66b545 2019-08-12 stsp if (error)
5599 fc66b545 2019-08-12 stsp goto done;
5600 fc66b545 2019-08-12 stsp printf("Rebase of %s aborted\n",
5601 fc66b545 2019-08-12 stsp got_ref_get_name(branch));
5602 fc66b545 2019-08-12 stsp }
5603 fc66b545 2019-08-12 stsp error = got_error(GOT_ERR_EMPTY_REBASE);
5604 fc66b545 2019-08-12 stsp goto done;
5605 fc66b545 2019-08-12 stsp }
5606 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, commit_id, pid->id,
5607 8ca9bd68 2019-07-25 stsp yca_id, got_worktree_get_path_prefix(worktree),
5608 8ca9bd68 2019-07-25 stsp GOT_ERR_REBASE_PATH, repo);
5609 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5610 818c7501 2019-07-11 stsp commit = NULL;
5611 818c7501 2019-07-11 stsp if (error)
5612 818c7501 2019-07-11 stsp goto done;
5613 64c6d990 2019-07-11 stsp
5614 818c7501 2019-07-11 stsp if (SIMPLEQ_EMPTY(&commits)) {
5615 38b0338b 2019-11-29 stsp if (continue_rebase) {
5616 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex,
5617 3e3a69f1 2019-07-25 stsp branch, new_base_branch, tmp_branch, repo);
5618 38b0338b 2019-11-29 stsp goto done;
5619 38b0338b 2019-11-29 stsp } else {
5620 38b0338b 2019-11-29 stsp /* Fast-forward the reference of the branch. */
5621 38b0338b 2019-11-29 stsp struct got_object_id *new_head_commit_id;
5622 38b0338b 2019-11-29 stsp char *id_str;
5623 38b0338b 2019-11-29 stsp error = got_ref_resolve(&new_head_commit_id, repo,
5624 38b0338b 2019-11-29 stsp new_base_branch);
5625 38b0338b 2019-11-29 stsp if (error)
5626 38b0338b 2019-11-29 stsp goto done;
5627 38b0338b 2019-11-29 stsp error = got_object_id_str(&id_str, new_head_commit_id);
5628 38b0338b 2019-11-29 stsp printf("Forwarding %s to commit %s\n",
5629 38b0338b 2019-11-29 stsp got_ref_get_name(branch), id_str);
5630 38b0338b 2019-11-29 stsp free(id_str);
5631 38b0338b 2019-11-29 stsp error = got_ref_change_ref(branch,
5632 38b0338b 2019-11-29 stsp new_head_commit_id);
5633 38b0338b 2019-11-29 stsp if (error)
5634 38b0338b 2019-11-29 stsp goto done;
5635 38b0338b 2019-11-29 stsp }
5636 818c7501 2019-07-11 stsp }
5637 818c7501 2019-07-11 stsp
5638 818c7501 2019-07-11 stsp pid = NULL;
5639 818c7501 2019-07-11 stsp SIMPLEQ_FOREACH(qid, &commits, entry) {
5640 818c7501 2019-07-11 stsp commit_id = qid->id;
5641 818c7501 2019-07-11 stsp parent_id = pid ? pid->id : yca_id;
5642 818c7501 2019-07-11 stsp pid = qid;
5643 818c7501 2019-07-11 stsp
5644 01757395 2019-07-12 stsp error = got_worktree_rebase_merge_files(&merged_paths,
5645 3e3a69f1 2019-07-25 stsp worktree, fileindex, parent_id, commit_id, repo,
5646 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
5647 818c7501 2019-07-11 stsp if (error)
5648 818c7501 2019-07-11 stsp goto done;
5649 818c7501 2019-07-11 stsp
5650 01757395 2019-07-12 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5651 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(qid->id, repo);
5652 a0ea4fc0 2020-02-28 stsp if (error)
5653 a0ea4fc0 2020-02-28 stsp goto done;
5654 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5655 818c7501 2019-07-11 stsp break;
5656 01757395 2019-07-12 stsp }
5657 818c7501 2019-07-11 stsp
5658 3e3a69f1 2019-07-25 stsp error = rebase_commit(&merged_paths, worktree, fileindex,
5659 3e3a69f1 2019-07-25 stsp tmp_branch, commit_id, repo);
5660 01757395 2019-07-12 stsp got_worktree_rebase_pathlist_free(&merged_paths);
5661 818c7501 2019-07-11 stsp if (error)
5662 818c7501 2019-07-11 stsp goto done;
5663 818c7501 2019-07-11 stsp }
5664 818c7501 2019-07-11 stsp
5665 818c7501 2019-07-11 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
5666 3e3a69f1 2019-07-25 stsp error = got_worktree_rebase_postpone(worktree, fileindex);
5667 818c7501 2019-07-11 stsp if (error)
5668 818c7501 2019-07-11 stsp goto done;
5669 818c7501 2019-07-11 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
5670 11495e04 2019-07-12 stsp "conflicts must be resolved before rebasing can continue");
5671 818c7501 2019-07-11 stsp } else
5672 3e3a69f1 2019-07-25 stsp error = rebase_complete(worktree, fileindex, branch,
5673 3e3a69f1 2019-07-25 stsp new_base_branch, tmp_branch, repo);
5674 818c7501 2019-07-11 stsp done:
5675 818c7501 2019-07-11 stsp got_object_id_queue_free(&commits);
5676 818c7501 2019-07-11 stsp free(branch_head_commit_id);
5677 818c7501 2019-07-11 stsp free(resume_commit_id);
5678 818c7501 2019-07-11 stsp free(yca_id);
5679 818c7501 2019-07-11 stsp if (commit)
5680 818c7501 2019-07-11 stsp got_object_commit_close(commit);
5681 818c7501 2019-07-11 stsp if (branch)
5682 818c7501 2019-07-11 stsp got_ref_close(branch);
5683 818c7501 2019-07-11 stsp if (new_base_branch)
5684 818c7501 2019-07-11 stsp got_ref_close(new_base_branch);
5685 818c7501 2019-07-11 stsp if (tmp_branch)
5686 818c7501 2019-07-11 stsp got_ref_close(tmp_branch);
5687 5ef14e63 2019-06-02 stsp if (worktree)
5688 5ef14e63 2019-06-02 stsp got_worktree_close(worktree);
5689 5ef14e63 2019-06-02 stsp if (repo)
5690 5ef14e63 2019-06-02 stsp got_repo_close(repo);
5691 5ef14e63 2019-06-02 stsp return error;
5692 0ebf8283 2019-07-24 stsp }
5693 0ebf8283 2019-07-24 stsp
5694 0ebf8283 2019-07-24 stsp __dead static void
5695 0ebf8283 2019-07-24 stsp usage_histedit(void)
5696 0ebf8283 2019-07-24 stsp {
5697 083957f4 2020-02-24 stsp fprintf(stderr, "usage: %s histedit [-a] [-c] [-F histedit-script] [-m]\n",
5698 0ebf8283 2019-07-24 stsp getprogname());
5699 0ebf8283 2019-07-24 stsp exit(1);
5700 0ebf8283 2019-07-24 stsp }
5701 0ebf8283 2019-07-24 stsp
5702 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_PICK 'p'
5703 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_EDIT 'e'
5704 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_FOLD 'f'
5705 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_DROP 'd'
5706 0ebf8283 2019-07-24 stsp #define GOT_HISTEDIT_MESG 'm'
5707 0ebf8283 2019-07-24 stsp
5708 0ebf8283 2019-07-24 stsp static struct got_histedit_cmd {
5709 0ebf8283 2019-07-24 stsp unsigned char code;
5710 0ebf8283 2019-07-24 stsp const char *name;
5711 0ebf8283 2019-07-24 stsp const char *desc;
5712 0ebf8283 2019-07-24 stsp } got_histedit_cmds[] = {
5713 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_PICK, "pick", "use commit" },
5714 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_EDIT, "edit", "use commit but stop for amending" },
5715 82997472 2020-01-29 stsp { GOT_HISTEDIT_FOLD, "fold", "combine with next commit that will "
5716 82997472 2020-01-29 stsp "be used" },
5717 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_DROP, "drop", "remove commit from history" },
5718 0ebf8283 2019-07-24 stsp { GOT_HISTEDIT_MESG, "mesg",
5719 0ebf8283 2019-07-24 stsp "single-line log message for commit above (open editor if empty)" },
5720 0ebf8283 2019-07-24 stsp };
5721 0ebf8283 2019-07-24 stsp
5722 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry {
5723 0ebf8283 2019-07-24 stsp TAILQ_ENTRY(got_histedit_list_entry) entry;
5724 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id;
5725 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5726 0ebf8283 2019-07-24 stsp char *logmsg;
5727 0ebf8283 2019-07-24 stsp };
5728 0ebf8283 2019-07-24 stsp TAILQ_HEAD(got_histedit_list, got_histedit_list_entry);
5729 0ebf8283 2019-07-24 stsp
5730 0ebf8283 2019-07-24 stsp static const struct got_error *
5731 0ebf8283 2019-07-24 stsp histedit_write_commit(struct got_object_id *commit_id, const char *cmdname,
5732 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5733 0ebf8283 2019-07-24 stsp {
5734 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5735 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *id_str = NULL;
5736 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5737 8138f3e1 2019-08-11 stsp int n;
5738 0ebf8283 2019-07-24 stsp
5739 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
5740 0ebf8283 2019-07-24 stsp if (err)
5741 0ebf8283 2019-07-24 stsp goto done;
5742 0ebf8283 2019-07-24 stsp
5743 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 34, commit);
5744 0ebf8283 2019-07-24 stsp if (err)
5745 0ebf8283 2019-07-24 stsp goto done;
5746 0ebf8283 2019-07-24 stsp
5747 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, commit_id);
5748 0ebf8283 2019-07-24 stsp if (err)
5749 0ebf8283 2019-07-24 stsp goto done;
5750 0ebf8283 2019-07-24 stsp
5751 0ebf8283 2019-07-24 stsp n = fprintf(f, "%s %s %s\n", cmdname, id_str, logmsg);
5752 0ebf8283 2019-07-24 stsp if (n < 0)
5753 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5754 0ebf8283 2019-07-24 stsp done:
5755 0ebf8283 2019-07-24 stsp if (commit)
5756 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5757 0ebf8283 2019-07-24 stsp free(id_str);
5758 0ebf8283 2019-07-24 stsp free(logmsg);
5759 0ebf8283 2019-07-24 stsp return err;
5760 0ebf8283 2019-07-24 stsp }
5761 0ebf8283 2019-07-24 stsp
5762 0ebf8283 2019-07-24 stsp static const struct got_error *
5763 083957f4 2020-02-24 stsp histedit_write_commit_list(struct got_object_id_queue *commits,
5764 083957f4 2020-02-24 stsp FILE *f, int edit_logmsg_only, struct got_repository *repo)
5765 0ebf8283 2019-07-24 stsp {
5766 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5767 0ebf8283 2019-07-24 stsp struct got_object_qid *qid;
5768 0ebf8283 2019-07-24 stsp
5769 0ebf8283 2019-07-24 stsp if (SIMPLEQ_EMPTY(commits))
5770 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
5771 0ebf8283 2019-07-24 stsp
5772 0ebf8283 2019-07-24 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
5773 0ebf8283 2019-07-24 stsp err = histedit_write_commit(qid->id, got_histedit_cmds[0].name,
5774 0ebf8283 2019-07-24 stsp f, repo);
5775 0ebf8283 2019-07-24 stsp if (err)
5776 0ebf8283 2019-07-24 stsp break;
5777 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
5778 083957f4 2020-02-24 stsp int n = fprintf(f, "%c\n", GOT_HISTEDIT_MESG);
5779 083957f4 2020-02-24 stsp if (n < 0) {
5780 083957f4 2020-02-24 stsp err = got_ferror(f, GOT_ERR_IO);
5781 083957f4 2020-02-24 stsp break;
5782 083957f4 2020-02-24 stsp }
5783 083957f4 2020-02-24 stsp }
5784 0ebf8283 2019-07-24 stsp }
5785 0ebf8283 2019-07-24 stsp
5786 0ebf8283 2019-07-24 stsp return err;
5787 0ebf8283 2019-07-24 stsp }
5788 0ebf8283 2019-07-24 stsp
5789 0ebf8283 2019-07-24 stsp static const struct got_error *
5790 514f2ffe 2020-01-29 stsp write_cmd_list(FILE *f, const char *branch_name,
5791 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits)
5792 0ebf8283 2019-07-24 stsp {
5793 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5794 0ebf8283 2019-07-24 stsp int n, i;
5795 514f2ffe 2020-01-29 stsp char *id_str;
5796 514f2ffe 2020-01-29 stsp struct got_object_qid *qid;
5797 514f2ffe 2020-01-29 stsp
5798 514f2ffe 2020-01-29 stsp qid = SIMPLEQ_FIRST(commits);
5799 514f2ffe 2020-01-29 stsp err = got_object_id_str(&id_str, qid->id);
5800 514f2ffe 2020-01-29 stsp if (err)
5801 514f2ffe 2020-01-29 stsp return err;
5802 514f2ffe 2020-01-29 stsp
5803 514f2ffe 2020-01-29 stsp n = fprintf(f,
5804 514f2ffe 2020-01-29 stsp "# Editing the history of branch '%s' starting at\n"
5805 514f2ffe 2020-01-29 stsp "# commit %s\n"
5806 514f2ffe 2020-01-29 stsp "# Commits will be processed in order from top to "
5807 514f2ffe 2020-01-29 stsp "bottom of this file.\n", branch_name, id_str);
5808 514f2ffe 2020-01-29 stsp if (n < 0) {
5809 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5810 514f2ffe 2020-01-29 stsp goto done;
5811 514f2ffe 2020-01-29 stsp }
5812 0ebf8283 2019-07-24 stsp
5813 0ebf8283 2019-07-24 stsp n = fprintf(f, "# Available histedit commands:\n");
5814 514f2ffe 2020-01-29 stsp if (n < 0) {
5815 514f2ffe 2020-01-29 stsp err = got_ferror(f, GOT_ERR_IO);
5816 514f2ffe 2020-01-29 stsp goto done;
5817 514f2ffe 2020-01-29 stsp }
5818 0ebf8283 2019-07-24 stsp
5819 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
5820 0ebf8283 2019-07-24 stsp struct got_histedit_cmd *cmd = &got_histedit_cmds[i];
5821 0ebf8283 2019-07-24 stsp n = fprintf(f, "# %s (%c): %s\n", cmd->name, cmd->code,
5822 0ebf8283 2019-07-24 stsp cmd->desc);
5823 0ebf8283 2019-07-24 stsp if (n < 0) {
5824 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
5825 0ebf8283 2019-07-24 stsp break;
5826 0ebf8283 2019-07-24 stsp }
5827 0ebf8283 2019-07-24 stsp }
5828 514f2ffe 2020-01-29 stsp done:
5829 514f2ffe 2020-01-29 stsp free(id_str);
5830 0ebf8283 2019-07-24 stsp return err;
5831 0ebf8283 2019-07-24 stsp }
5832 0ebf8283 2019-07-24 stsp
5833 0ebf8283 2019-07-24 stsp static const struct got_error *
5834 0ebf8283 2019-07-24 stsp histedit_syntax_error(int lineno)
5835 0ebf8283 2019-07-24 stsp {
5836 0ebf8283 2019-07-24 stsp static char msg[42];
5837 0ebf8283 2019-07-24 stsp int ret;
5838 0ebf8283 2019-07-24 stsp
5839 0ebf8283 2019-07-24 stsp ret = snprintf(msg, sizeof(msg), "histedit syntax error on line %d",
5840 0ebf8283 2019-07-24 stsp lineno);
5841 0ebf8283 2019-07-24 stsp if (ret == -1 || ret >= sizeof(msg))
5842 0ebf8283 2019-07-24 stsp return got_error(GOT_ERR_HISTEDIT_SYNTAX);
5843 0ebf8283 2019-07-24 stsp
5844 0ebf8283 2019-07-24 stsp return got_error_msg(GOT_ERR_HISTEDIT_SYNTAX, msg);
5845 0ebf8283 2019-07-24 stsp }
5846 0ebf8283 2019-07-24 stsp
5847 0ebf8283 2019-07-24 stsp static const struct got_error *
5848 0ebf8283 2019-07-24 stsp append_folded_commit_msg(char **new_msg, struct got_histedit_list_entry *hle,
5849 0ebf8283 2019-07-24 stsp char *logmsg, struct got_repository *repo)
5850 0ebf8283 2019-07-24 stsp {
5851 0ebf8283 2019-07-24 stsp const struct got_error *err;
5852 0ebf8283 2019-07-24 stsp struct got_commit_object *folded_commit = NULL;
5853 5943eee2 2019-08-13 stsp char *id_str, *folded_logmsg = NULL;
5854 0ebf8283 2019-07-24 stsp
5855 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5856 0ebf8283 2019-07-24 stsp if (err)
5857 0ebf8283 2019-07-24 stsp return err;
5858 0ebf8283 2019-07-24 stsp
5859 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&folded_commit, repo, hle->commit_id);
5860 0ebf8283 2019-07-24 stsp if (err)
5861 0ebf8283 2019-07-24 stsp goto done;
5862 0ebf8283 2019-07-24 stsp
5863 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&folded_logmsg, folded_commit);
5864 5943eee2 2019-08-13 stsp if (err)
5865 5943eee2 2019-08-13 stsp goto done;
5866 0ebf8283 2019-07-24 stsp if (asprintf(new_msg, "%s%s# log message of folded commit %s: %s",
5867 0ebf8283 2019-07-24 stsp logmsg ? logmsg : "", logmsg ? "\n" : "", id_str,
5868 5943eee2 2019-08-13 stsp folded_logmsg) == -1) {
5869 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5870 0ebf8283 2019-07-24 stsp }
5871 0ebf8283 2019-07-24 stsp done:
5872 0ebf8283 2019-07-24 stsp if (folded_commit)
5873 0ebf8283 2019-07-24 stsp got_object_commit_close(folded_commit);
5874 0ebf8283 2019-07-24 stsp free(id_str);
5875 5943eee2 2019-08-13 stsp free(folded_logmsg);
5876 0ebf8283 2019-07-24 stsp return err;
5877 0ebf8283 2019-07-24 stsp }
5878 0ebf8283 2019-07-24 stsp
5879 0ebf8283 2019-07-24 stsp static struct got_histedit_list_entry *
5880 0ebf8283 2019-07-24 stsp get_folded_commits(struct got_histedit_list_entry *hle)
5881 0ebf8283 2019-07-24 stsp {
5882 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *prev, *folded = NULL;
5883 0ebf8283 2019-07-24 stsp
5884 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(hle, got_histedit_list, entry);
5885 3f9de99f 2019-07-24 stsp while (prev && (prev->cmd->code == GOT_HISTEDIT_FOLD ||
5886 3f9de99f 2019-07-24 stsp prev->cmd->code == GOT_HISTEDIT_DROP)) {
5887 3f9de99f 2019-07-24 stsp if (prev->cmd->code == GOT_HISTEDIT_FOLD)
5888 3f9de99f 2019-07-24 stsp folded = prev;
5889 0ebf8283 2019-07-24 stsp prev = TAILQ_PREV(prev, got_histedit_list, entry);
5890 0ebf8283 2019-07-24 stsp }
5891 0ebf8283 2019-07-24 stsp
5892 0ebf8283 2019-07-24 stsp return folded;
5893 0ebf8283 2019-07-24 stsp }
5894 0ebf8283 2019-07-24 stsp
5895 0ebf8283 2019-07-24 stsp static const struct got_error *
5896 0ebf8283 2019-07-24 stsp histedit_edit_logmsg(struct got_histedit_list_entry *hle,
5897 0ebf8283 2019-07-24 stsp struct got_repository *repo)
5898 0ebf8283 2019-07-24 stsp {
5899 5943eee2 2019-08-13 stsp char *logmsg_path = NULL, *id_str = NULL, *orig_logmsg = NULL;
5900 0ebf8283 2019-07-24 stsp char *logmsg = NULL, *new_msg = NULL, *editor = NULL;
5901 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5902 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
5903 0ebf8283 2019-07-24 stsp int fd;
5904 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *folded = NULL;
5905 0ebf8283 2019-07-24 stsp
5906 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
5907 0ebf8283 2019-07-24 stsp if (err)
5908 0ebf8283 2019-07-24 stsp return err;
5909 0ebf8283 2019-07-24 stsp
5910 0ebf8283 2019-07-24 stsp folded = get_folded_commits(hle);
5911 0ebf8283 2019-07-24 stsp if (folded) {
5912 0ebf8283 2019-07-24 stsp while (folded != hle) {
5913 3f9de99f 2019-07-24 stsp if (folded->cmd->code == GOT_HISTEDIT_DROP) {
5914 3f9de99f 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5915 3f9de99f 2019-07-24 stsp continue;
5916 3f9de99f 2019-07-24 stsp }
5917 0ebf8283 2019-07-24 stsp err = append_folded_commit_msg(&new_msg, folded,
5918 0ebf8283 2019-07-24 stsp logmsg, repo);
5919 0ebf8283 2019-07-24 stsp if (err)
5920 0ebf8283 2019-07-24 stsp goto done;
5921 0ebf8283 2019-07-24 stsp free(logmsg);
5922 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5923 0ebf8283 2019-07-24 stsp folded = TAILQ_NEXT(folded, entry);
5924 0ebf8283 2019-07-24 stsp }
5925 0ebf8283 2019-07-24 stsp }
5926 0ebf8283 2019-07-24 stsp
5927 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5928 0ebf8283 2019-07-24 stsp if (err)
5929 0ebf8283 2019-07-24 stsp goto done;
5930 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&orig_logmsg, commit);
5931 5943eee2 2019-08-13 stsp if (err)
5932 5943eee2 2019-08-13 stsp goto done;
5933 0ebf8283 2019-07-24 stsp if (asprintf(&new_msg,
5934 0ebf8283 2019-07-24 stsp "%s\n# original log message of commit %s: %s",
5935 5943eee2 2019-08-13 stsp logmsg ? logmsg : "", id_str, orig_logmsg) == -1) {
5936 0ebf8283 2019-07-24 stsp err = got_error_from_errno("asprintf");
5937 0ebf8283 2019-07-24 stsp goto done;
5938 0ebf8283 2019-07-24 stsp }
5939 0ebf8283 2019-07-24 stsp free(logmsg);
5940 0ebf8283 2019-07-24 stsp logmsg = new_msg;
5941 0ebf8283 2019-07-24 stsp
5942 0ebf8283 2019-07-24 stsp err = got_object_id_str(&id_str, hle->commit_id);
5943 0ebf8283 2019-07-24 stsp if (err)
5944 0ebf8283 2019-07-24 stsp goto done;
5945 0ebf8283 2019-07-24 stsp
5946 bb63914a 2020-02-17 stsp err = got_opentemp_named_fd(&logmsg_path, &fd,
5947 bb63914a 2020-02-17 stsp GOT_TMPDIR_STR "/got-logmsg");
5948 0ebf8283 2019-07-24 stsp if (err)
5949 0ebf8283 2019-07-24 stsp goto done;
5950 0ebf8283 2019-07-24 stsp
5951 0ebf8283 2019-07-24 stsp dprintf(fd, logmsg);
5952 0ebf8283 2019-07-24 stsp close(fd);
5953 0ebf8283 2019-07-24 stsp
5954 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
5955 0ebf8283 2019-07-24 stsp if (err)
5956 0ebf8283 2019-07-24 stsp goto done;
5957 0ebf8283 2019-07-24 stsp
5958 0ebf8283 2019-07-24 stsp err = edit_logmsg(&hle->logmsg, editor, logmsg_path, logmsg);
5959 0ebf8283 2019-07-24 stsp if (err) {
5960 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_MSG_EMPTY)
5961 0ebf8283 2019-07-24 stsp goto done;
5962 5943eee2 2019-08-13 stsp err = got_object_commit_get_logmsg(&hle->logmsg, commit);
5963 0ebf8283 2019-07-24 stsp }
5964 0ebf8283 2019-07-24 stsp done:
5965 0ebf8283 2019-07-24 stsp if (logmsg_path && unlink(logmsg_path) != 0 && err == NULL)
5966 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", logmsg_path);
5967 0ebf8283 2019-07-24 stsp free(logmsg_path);
5968 0ebf8283 2019-07-24 stsp free(logmsg);
5969 5943eee2 2019-08-13 stsp free(orig_logmsg);
5970 0ebf8283 2019-07-24 stsp free(editor);
5971 0ebf8283 2019-07-24 stsp if (commit)
5972 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
5973 0ebf8283 2019-07-24 stsp return err;
5974 5ef14e63 2019-06-02 stsp }
5975 0ebf8283 2019-07-24 stsp
5976 0ebf8283 2019-07-24 stsp static const struct got_error *
5977 0ebf8283 2019-07-24 stsp histedit_parse_list(struct got_histedit_list *histedit_cmds,
5978 0ebf8283 2019-07-24 stsp FILE *f, struct got_repository *repo)
5979 0ebf8283 2019-07-24 stsp {
5980 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
5981 0ebf8283 2019-07-24 stsp char *line = NULL, *p, *end;
5982 0ebf8283 2019-07-24 stsp size_t size;
5983 0ebf8283 2019-07-24 stsp ssize_t len;
5984 0ebf8283 2019-07-24 stsp int lineno = 0, i;
5985 0ebf8283 2019-07-24 stsp const struct got_histedit_cmd *cmd;
5986 0ebf8283 2019-07-24 stsp struct got_object_id *commit_id = NULL;
5987 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle = NULL;
5988 0ebf8283 2019-07-24 stsp
5989 0ebf8283 2019-07-24 stsp for (;;) {
5990 0ebf8283 2019-07-24 stsp len = getline(&line, &size, f);
5991 0ebf8283 2019-07-24 stsp if (len == -1) {
5992 0ebf8283 2019-07-24 stsp const struct got_error *getline_err;
5993 0ebf8283 2019-07-24 stsp if (feof(f))
5994 0ebf8283 2019-07-24 stsp break;
5995 0ebf8283 2019-07-24 stsp getline_err = got_error_from_errno("getline");
5996 0ebf8283 2019-07-24 stsp err = got_ferror(f, getline_err->code);
5997 0ebf8283 2019-07-24 stsp break;
5998 0ebf8283 2019-07-24 stsp }
5999 0ebf8283 2019-07-24 stsp lineno++;
6000 0ebf8283 2019-07-24 stsp p = line;
6001 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6002 0ebf8283 2019-07-24 stsp p++;
6003 0ebf8283 2019-07-24 stsp if (p[0] == '#' || p[0] == '\0') {
6004 0ebf8283 2019-07-24 stsp free(line);
6005 0ebf8283 2019-07-24 stsp line = NULL;
6006 0ebf8283 2019-07-24 stsp continue;
6007 0ebf8283 2019-07-24 stsp }
6008 0ebf8283 2019-07-24 stsp cmd = NULL;
6009 0ebf8283 2019-07-24 stsp for (i = 0; i < nitems(got_histedit_cmds); i++) {
6010 0ebf8283 2019-07-24 stsp cmd = &got_histedit_cmds[i];
6011 0ebf8283 2019-07-24 stsp if (strncmp(cmd->name, p, strlen(cmd->name)) == 0 &&
6012 0ebf8283 2019-07-24 stsp isspace((unsigned char)p[strlen(cmd->name)])) {
6013 0ebf8283 2019-07-24 stsp p += strlen(cmd->name);
6014 0ebf8283 2019-07-24 stsp break;
6015 0ebf8283 2019-07-24 stsp }
6016 0ebf8283 2019-07-24 stsp if (p[0] == cmd->code && isspace((unsigned char)p[1])) {
6017 0ebf8283 2019-07-24 stsp p++;
6018 0ebf8283 2019-07-24 stsp break;
6019 0ebf8283 2019-07-24 stsp }
6020 0ebf8283 2019-07-24 stsp }
6021 6c1844f6 2019-07-25 stsp if (i == nitems(got_histedit_cmds)) {
6022 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6023 0ebf8283 2019-07-24 stsp break;
6024 0ebf8283 2019-07-24 stsp }
6025 0ebf8283 2019-07-24 stsp while (isspace((unsigned char)p[0]))
6026 0ebf8283 2019-07-24 stsp p++;
6027 0ebf8283 2019-07-24 stsp if (cmd->code == GOT_HISTEDIT_MESG) {
6028 0ebf8283 2019-07-24 stsp if (hle == NULL || hle->logmsg != NULL) {
6029 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CMD);
6030 0ebf8283 2019-07-24 stsp break;
6031 0ebf8283 2019-07-24 stsp }
6032 0ebf8283 2019-07-24 stsp if (p[0] == '\0') {
6033 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6034 0ebf8283 2019-07-24 stsp if (err)
6035 0ebf8283 2019-07-24 stsp break;
6036 0ebf8283 2019-07-24 stsp } else {
6037 0ebf8283 2019-07-24 stsp hle->logmsg = strdup(p);
6038 0ebf8283 2019-07-24 stsp if (hle->logmsg == NULL) {
6039 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6040 0ebf8283 2019-07-24 stsp break;
6041 0ebf8283 2019-07-24 stsp }
6042 0ebf8283 2019-07-24 stsp }
6043 0ebf8283 2019-07-24 stsp free(line);
6044 0ebf8283 2019-07-24 stsp line = NULL;
6045 0ebf8283 2019-07-24 stsp continue;
6046 0ebf8283 2019-07-24 stsp } else {
6047 0ebf8283 2019-07-24 stsp end = p;
6048 0ebf8283 2019-07-24 stsp while (end[0] && !isspace((unsigned char)end[0]))
6049 0ebf8283 2019-07-24 stsp end++;
6050 0ebf8283 2019-07-24 stsp *end = '\0';
6051 0ebf8283 2019-07-24 stsp
6052 0ebf8283 2019-07-24 stsp err = got_object_resolve_id_str(&commit_id, repo, p);
6053 0ebf8283 2019-07-24 stsp if (err) {
6054 0ebf8283 2019-07-24 stsp /* override error code */
6055 0ebf8283 2019-07-24 stsp err = histedit_syntax_error(lineno);
6056 0ebf8283 2019-07-24 stsp break;
6057 0ebf8283 2019-07-24 stsp }
6058 0ebf8283 2019-07-24 stsp }
6059 0ebf8283 2019-07-24 stsp hle = malloc(sizeof(*hle));
6060 0ebf8283 2019-07-24 stsp if (hle == NULL) {
6061 0ebf8283 2019-07-24 stsp err = got_error_from_errno("malloc");
6062 0ebf8283 2019-07-24 stsp break;
6063 0ebf8283 2019-07-24 stsp }
6064 0ebf8283 2019-07-24 stsp hle->cmd = cmd;
6065 0ebf8283 2019-07-24 stsp hle->commit_id = commit_id;
6066 0ebf8283 2019-07-24 stsp hle->logmsg = NULL;
6067 0ebf8283 2019-07-24 stsp commit_id = NULL;
6068 0ebf8283 2019-07-24 stsp free(line);
6069 0ebf8283 2019-07-24 stsp line = NULL;
6070 0ebf8283 2019-07-24 stsp TAILQ_INSERT_TAIL(histedit_cmds, hle, entry);
6071 0ebf8283 2019-07-24 stsp }
6072 0ebf8283 2019-07-24 stsp
6073 0ebf8283 2019-07-24 stsp free(line);
6074 0ebf8283 2019-07-24 stsp free(commit_id);
6075 0ebf8283 2019-07-24 stsp return err;
6076 0ebf8283 2019-07-24 stsp }
6077 0ebf8283 2019-07-24 stsp
6078 0ebf8283 2019-07-24 stsp static const struct got_error *
6079 bfce7f83 2019-07-27 stsp histedit_check_script(struct got_histedit_list *histedit_cmds,
6080 bfce7f83 2019-07-27 stsp struct got_object_id_queue *commits, struct got_repository *repo)
6081 bfce7f83 2019-07-27 stsp {
6082 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL;
6083 bfce7f83 2019-07-27 stsp struct got_object_qid *qid;
6084 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6085 bfce7f83 2019-07-27 stsp static char msg[80];
6086 bfce7f83 2019-07-27 stsp char *id_str;
6087 bfce7f83 2019-07-27 stsp
6088 bfce7f83 2019-07-27 stsp if (TAILQ_EMPTY(histedit_cmds))
6089 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_EMPTY_HISTEDIT,
6090 bfce7f83 2019-07-27 stsp "histedit script contains no commands");
6091 a0de39f3 2019-08-09 stsp if (SIMPLEQ_EMPTY(commits))
6092 a0de39f3 2019-08-09 stsp return got_error(GOT_ERR_EMPTY_HISTEDIT);
6093 bfce7f83 2019-07-27 stsp
6094 bfce7f83 2019-07-27 stsp SIMPLEQ_FOREACH(qid, commits, entry) {
6095 bfce7f83 2019-07-27 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6096 bfce7f83 2019-07-27 stsp if (got_object_id_cmp(qid->id, hle->commit_id) == 0)
6097 bfce7f83 2019-07-27 stsp break;
6098 bfce7f83 2019-07-27 stsp }
6099 bfce7f83 2019-07-27 stsp if (hle == NULL) {
6100 bfce7f83 2019-07-27 stsp err = got_object_id_str(&id_str, qid->id);
6101 bfce7f83 2019-07-27 stsp if (err)
6102 bfce7f83 2019-07-27 stsp return err;
6103 bfce7f83 2019-07-27 stsp snprintf(msg, sizeof(msg),
6104 bfce7f83 2019-07-27 stsp "commit %s missing from histedit script", id_str);
6105 bfce7f83 2019-07-27 stsp free(id_str);
6106 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD, msg);
6107 bfce7f83 2019-07-27 stsp }
6108 bfce7f83 2019-07-27 stsp }
6109 bfce7f83 2019-07-27 stsp
6110 0def28b1 2019-08-17 stsp hle = TAILQ_LAST(histedit_cmds, got_histedit_list);
6111 0def28b1 2019-08-17 stsp if (hle && hle->cmd->code == GOT_HISTEDIT_FOLD)
6112 bfce7f83 2019-07-27 stsp return got_error_msg(GOT_ERR_HISTEDIT_CMD,
6113 bfce7f83 2019-07-27 stsp "last commit in histedit script cannot be folded");
6114 bfce7f83 2019-07-27 stsp
6115 bfce7f83 2019-07-27 stsp return NULL;
6116 bfce7f83 2019-07-27 stsp }
6117 bfce7f83 2019-07-27 stsp
6118 bfce7f83 2019-07-27 stsp static const struct got_error *
6119 0ebf8283 2019-07-24 stsp histedit_run_editor(struct got_histedit_list *histedit_cmds,
6120 bfce7f83 2019-07-27 stsp const char *path, struct got_object_id_queue *commits,
6121 bfce7f83 2019-07-27 stsp struct got_repository *repo)
6122 0ebf8283 2019-07-24 stsp {
6123 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6124 0ebf8283 2019-07-24 stsp char *editor;
6125 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6126 0ebf8283 2019-07-24 stsp
6127 0ebf8283 2019-07-24 stsp err = get_editor(&editor);
6128 0ebf8283 2019-07-24 stsp if (err)
6129 0ebf8283 2019-07-24 stsp return err;
6130 0ebf8283 2019-07-24 stsp
6131 0ebf8283 2019-07-24 stsp if (spawn_editor(editor, path) == -1) {
6132 0ebf8283 2019-07-24 stsp err = got_error_from_errno("failed spawning editor");
6133 0ebf8283 2019-07-24 stsp goto done;
6134 0ebf8283 2019-07-24 stsp }
6135 0ebf8283 2019-07-24 stsp
6136 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6137 0ebf8283 2019-07-24 stsp if (f == NULL) {
6138 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fopen");
6139 0ebf8283 2019-07-24 stsp goto done;
6140 0ebf8283 2019-07-24 stsp }
6141 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6142 bfce7f83 2019-07-27 stsp if (err)
6143 bfce7f83 2019-07-27 stsp goto done;
6144 bfce7f83 2019-07-27 stsp
6145 bfce7f83 2019-07-27 stsp err = histedit_check_script(histedit_cmds, commits, repo);
6146 0ebf8283 2019-07-24 stsp done:
6147 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6148 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6149 0ebf8283 2019-07-24 stsp free(editor);
6150 0ebf8283 2019-07-24 stsp return err;
6151 0ebf8283 2019-07-24 stsp }
6152 0ebf8283 2019-07-24 stsp
6153 0ebf8283 2019-07-24 stsp static const struct got_error *
6154 bfce7f83 2019-07-27 stsp histedit_edit_list_retry(struct got_histedit_list *, const struct got_error *,
6155 514f2ffe 2020-01-29 stsp struct got_object_id_queue *, const char *, const char *,
6156 514f2ffe 2020-01-29 stsp struct got_repository *);
6157 0ebf8283 2019-07-24 stsp
6158 0ebf8283 2019-07-24 stsp static const struct got_error *
6159 0ebf8283 2019-07-24 stsp histedit_edit_script(struct got_histedit_list *histedit_cmds,
6160 514f2ffe 2020-01-29 stsp struct got_object_id_queue *commits, const char *branch_name,
6161 083957f4 2020-02-24 stsp int edit_logmsg_only, struct got_repository *repo)
6162 0ebf8283 2019-07-24 stsp {
6163 0ebf8283 2019-07-24 stsp const struct got_error *err;
6164 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6165 0ebf8283 2019-07-24 stsp char *path = NULL;
6166 0ebf8283 2019-07-24 stsp
6167 0ebf8283 2019-07-24 stsp err = got_opentemp_named(&path, &f, "got-histedit");
6168 0ebf8283 2019-07-24 stsp if (err)
6169 0ebf8283 2019-07-24 stsp return err;
6170 0ebf8283 2019-07-24 stsp
6171 514f2ffe 2020-01-29 stsp err = write_cmd_list(f, branch_name, commits);
6172 0ebf8283 2019-07-24 stsp if (err)
6173 0ebf8283 2019-07-24 stsp goto done;
6174 0ebf8283 2019-07-24 stsp
6175 083957f4 2020-02-24 stsp err = histedit_write_commit_list(commits, f, edit_logmsg_only, repo);
6176 0ebf8283 2019-07-24 stsp if (err)
6177 0ebf8283 2019-07-24 stsp goto done;
6178 0ebf8283 2019-07-24 stsp
6179 083957f4 2020-02-24 stsp if (edit_logmsg_only) {
6180 083957f4 2020-02-24 stsp rewind(f);
6181 083957f4 2020-02-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6182 083957f4 2020-02-24 stsp } else {
6183 083957f4 2020-02-24 stsp if (fclose(f) != 0) {
6184 083957f4 2020-02-24 stsp err = got_error_from_errno("fclose");
6185 0ebf8283 2019-07-24 stsp goto done;
6186 083957f4 2020-02-24 stsp }
6187 083957f4 2020-02-24 stsp f = NULL;
6188 083957f4 2020-02-24 stsp err = histedit_run_editor(histedit_cmds, path, commits, repo);
6189 083957f4 2020-02-24 stsp if (err) {
6190 083957f4 2020-02-24 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6191 083957f4 2020-02-24 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6192 083957f4 2020-02-24 stsp goto done;
6193 083957f4 2020-02-24 stsp err = histedit_edit_list_retry(histedit_cmds, err,
6194 083957f4 2020-02-24 stsp commits, path, branch_name, repo);
6195 083957f4 2020-02-24 stsp }
6196 0ebf8283 2019-07-24 stsp }
6197 0ebf8283 2019-07-24 stsp done:
6198 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6199 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6200 0ebf8283 2019-07-24 stsp if (path && unlink(path) != 0 && err == NULL)
6201 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("unlink", path);
6202 0ebf8283 2019-07-24 stsp free(path);
6203 0ebf8283 2019-07-24 stsp return err;
6204 0ebf8283 2019-07-24 stsp }
6205 0ebf8283 2019-07-24 stsp
6206 0ebf8283 2019-07-24 stsp static const struct got_error *
6207 0ebf8283 2019-07-24 stsp histedit_save_list(struct got_histedit_list *histedit_cmds,
6208 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6209 0ebf8283 2019-07-24 stsp {
6210 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6211 0ebf8283 2019-07-24 stsp char *path = NULL;
6212 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6213 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6214 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6215 0ebf8283 2019-07-24 stsp
6216 c3022ba5 2019-07-27 stsp err = got_worktree_get_histedit_script_path(&path, worktree);
6217 0ebf8283 2019-07-24 stsp if (err)
6218 0ebf8283 2019-07-24 stsp return err;
6219 0ebf8283 2019-07-24 stsp
6220 0ebf8283 2019-07-24 stsp f = fopen(path, "w");
6221 0ebf8283 2019-07-24 stsp if (f == NULL) {
6222 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6223 0ebf8283 2019-07-24 stsp goto done;
6224 0ebf8283 2019-07-24 stsp }
6225 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, histedit_cmds, entry) {
6226 0ebf8283 2019-07-24 stsp err = histedit_write_commit(hle->commit_id, hle->cmd->name, f,
6227 0ebf8283 2019-07-24 stsp repo);
6228 0ebf8283 2019-07-24 stsp if (err)
6229 0ebf8283 2019-07-24 stsp break;
6230 0ebf8283 2019-07-24 stsp
6231 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6232 0ebf8283 2019-07-24 stsp int n = fprintf(f, "%c %s\n",
6233 0ebf8283 2019-07-24 stsp GOT_HISTEDIT_MESG, hle->logmsg);
6234 0ebf8283 2019-07-24 stsp if (n < 0) {
6235 0ebf8283 2019-07-24 stsp err = got_ferror(f, GOT_ERR_IO);
6236 0ebf8283 2019-07-24 stsp break;
6237 0ebf8283 2019-07-24 stsp }
6238 0ebf8283 2019-07-24 stsp }
6239 0ebf8283 2019-07-24 stsp }
6240 0ebf8283 2019-07-24 stsp done:
6241 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6242 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6243 0ebf8283 2019-07-24 stsp free(path);
6244 0ebf8283 2019-07-24 stsp if (commit)
6245 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6246 0ebf8283 2019-07-24 stsp return err;
6247 0ebf8283 2019-07-24 stsp }
6248 0ebf8283 2019-07-24 stsp
6249 bfce7f83 2019-07-27 stsp void
6250 bfce7f83 2019-07-27 stsp histedit_free_list(struct got_histedit_list *histedit_cmds)
6251 bfce7f83 2019-07-27 stsp {
6252 bfce7f83 2019-07-27 stsp struct got_histedit_list_entry *hle;
6253 bfce7f83 2019-07-27 stsp
6254 bfce7f83 2019-07-27 stsp while ((hle = TAILQ_FIRST(histedit_cmds))) {
6255 bfce7f83 2019-07-27 stsp TAILQ_REMOVE(histedit_cmds, hle, entry);
6256 bfce7f83 2019-07-27 stsp free(hle);
6257 bfce7f83 2019-07-27 stsp }
6258 bfce7f83 2019-07-27 stsp }
6259 bfce7f83 2019-07-27 stsp
6260 0ebf8283 2019-07-24 stsp static const struct got_error *
6261 0ebf8283 2019-07-24 stsp histedit_load_list(struct got_histedit_list *histedit_cmds,
6262 0ebf8283 2019-07-24 stsp const char *path, struct got_repository *repo)
6263 0ebf8283 2019-07-24 stsp {
6264 0ebf8283 2019-07-24 stsp const struct got_error *err = NULL;
6265 0ebf8283 2019-07-24 stsp FILE *f = NULL;
6266 0ebf8283 2019-07-24 stsp
6267 0ebf8283 2019-07-24 stsp f = fopen(path, "r");
6268 0ebf8283 2019-07-24 stsp if (f == NULL) {
6269 0ebf8283 2019-07-24 stsp err = got_error_from_errno2("fopen", path);
6270 0ebf8283 2019-07-24 stsp goto done;
6271 0ebf8283 2019-07-24 stsp }
6272 0ebf8283 2019-07-24 stsp
6273 0ebf8283 2019-07-24 stsp err = histedit_parse_list(histedit_cmds, f, repo);
6274 0ebf8283 2019-07-24 stsp done:
6275 0ebf8283 2019-07-24 stsp if (f && fclose(f) != 0 && err == NULL)
6276 0ebf8283 2019-07-24 stsp err = got_error_from_errno("fclose");
6277 0ebf8283 2019-07-24 stsp return err;
6278 0ebf8283 2019-07-24 stsp }
6279 0ebf8283 2019-07-24 stsp
6280 0ebf8283 2019-07-24 stsp static const struct got_error *
6281 0ebf8283 2019-07-24 stsp histedit_edit_list_retry(struct got_histedit_list *histedit_cmds,
6282 bfce7f83 2019-07-27 stsp const struct got_error *edit_err, struct got_object_id_queue *commits,
6283 514f2ffe 2020-01-29 stsp const char *path, const char *branch_name, struct got_repository *repo)
6284 0ebf8283 2019-07-24 stsp {
6285 bfce7f83 2019-07-27 stsp const struct got_error *err = NULL, *prev_err = edit_err;
6286 0ebf8283 2019-07-24 stsp int resp = ' ';
6287 0ebf8283 2019-07-24 stsp
6288 b006047e 2019-07-25 stsp while (resp != 'c' && resp != 'r' && resp != 'a') {
6289 0ebf8283 2019-07-24 stsp printf("%s: %s\n(c)ontinue editing, (r)estart editing, "
6290 bfce7f83 2019-07-27 stsp "or (a)bort: ", getprogname(), prev_err->msg);
6291 0ebf8283 2019-07-24 stsp resp = getchar();
6292 a61a4414 2019-08-07 stsp if (resp == '\n')
6293 a61a4414 2019-08-07 stsp resp = getchar();
6294 426ebf2e 2019-07-25 stsp if (resp == 'c') {
6295 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6296 bfce7f83 2019-07-27 stsp err = histedit_run_editor(histedit_cmds, path, commits,
6297 bfce7f83 2019-07-27 stsp repo);
6298 426ebf2e 2019-07-25 stsp if (err) {
6299 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6300 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6301 426ebf2e 2019-07-25 stsp break;
6302 bfce7f83 2019-07-27 stsp prev_err = err;
6303 426ebf2e 2019-07-25 stsp resp = ' ';
6304 426ebf2e 2019-07-25 stsp continue;
6305 426ebf2e 2019-07-25 stsp }
6306 bfce7f83 2019-07-27 stsp break;
6307 426ebf2e 2019-07-25 stsp } else if (resp == 'r') {
6308 bfce7f83 2019-07-27 stsp histedit_free_list(histedit_cmds);
6309 0ebf8283 2019-07-24 stsp err = histedit_edit_script(histedit_cmds,
6310 083957f4 2020-02-24 stsp commits, branch_name, 0, repo);
6311 426ebf2e 2019-07-25 stsp if (err) {
6312 bfce7f83 2019-07-27 stsp if (err->code != GOT_ERR_HISTEDIT_SYNTAX &&
6313 bfce7f83 2019-07-27 stsp err->code != GOT_ERR_HISTEDIT_CMD)
6314 426ebf2e 2019-07-25 stsp break;
6315 bfce7f83 2019-07-27 stsp prev_err = err;
6316 426ebf2e 2019-07-25 stsp resp = ' ';
6317 426ebf2e 2019-07-25 stsp continue;
6318 426ebf2e 2019-07-25 stsp }
6319 bfce7f83 2019-07-27 stsp break;
6320 426ebf2e 2019-07-25 stsp } else if (resp == 'a') {
6321 0ebf8283 2019-07-24 stsp err = got_error(GOT_ERR_HISTEDIT_CANCEL);
6322 0ebf8283 2019-07-24 stsp break;
6323 426ebf2e 2019-07-25 stsp } else
6324 0ebf8283 2019-07-24 stsp printf("invalid response '%c'\n", resp);
6325 0ebf8283 2019-07-24 stsp }
6326 0ebf8283 2019-07-24 stsp
6327 0ebf8283 2019-07-24 stsp return err;
6328 0ebf8283 2019-07-24 stsp }
6329 0ebf8283 2019-07-24 stsp
6330 0ebf8283 2019-07-24 stsp static const struct got_error *
6331 0ebf8283 2019-07-24 stsp histedit_complete(struct got_worktree *worktree,
6332 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex, struct got_reference *tmp_branch,
6333 3e3a69f1 2019-07-25 stsp struct got_reference *branch, struct got_repository *repo)
6334 0ebf8283 2019-07-24 stsp {
6335 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6336 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6337 3e3a69f1 2019-07-25 stsp return got_worktree_histedit_complete(worktree, fileindex, tmp_branch,
6338 3e3a69f1 2019-07-25 stsp branch, repo);
6339 0ebf8283 2019-07-24 stsp }
6340 0ebf8283 2019-07-24 stsp
6341 0ebf8283 2019-07-24 stsp static const struct got_error *
6342 0ebf8283 2019-07-24 stsp show_histedit_progress(struct got_commit_object *commit,
6343 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle, struct got_object_id *new_id)
6344 0ebf8283 2019-07-24 stsp {
6345 0ebf8283 2019-07-24 stsp const struct got_error *err;
6346 0ebf8283 2019-07-24 stsp char *old_id_str = NULL, *new_id_str = NULL, *logmsg = NULL;
6347 0ebf8283 2019-07-24 stsp
6348 0ebf8283 2019-07-24 stsp err = got_object_id_str(&old_id_str, hle->commit_id);
6349 0ebf8283 2019-07-24 stsp if (err)
6350 0ebf8283 2019-07-24 stsp goto done;
6351 0ebf8283 2019-07-24 stsp
6352 0ebf8283 2019-07-24 stsp if (new_id) {
6353 0ebf8283 2019-07-24 stsp err = got_object_id_str(&new_id_str, new_id);
6354 0ebf8283 2019-07-24 stsp if (err)
6355 0ebf8283 2019-07-24 stsp goto done;
6356 0ebf8283 2019-07-24 stsp }
6357 0ebf8283 2019-07-24 stsp
6358 0ebf8283 2019-07-24 stsp old_id_str[12] = '\0';
6359 0ebf8283 2019-07-24 stsp if (new_id_str)
6360 0ebf8283 2019-07-24 stsp new_id_str[12] = '\0';
6361 0ebf8283 2019-07-24 stsp
6362 0ebf8283 2019-07-24 stsp if (hle->logmsg) {
6363 0ebf8283 2019-07-24 stsp logmsg = strdup(hle->logmsg);
6364 0ebf8283 2019-07-24 stsp if (logmsg == NULL) {
6365 0ebf8283 2019-07-24 stsp err = got_error_from_errno("strdup");
6366 0ebf8283 2019-07-24 stsp goto done;
6367 0ebf8283 2019-07-24 stsp }
6368 0ebf8283 2019-07-24 stsp trim_logmsg(logmsg, 42);
6369 0ebf8283 2019-07-24 stsp } else {
6370 0ebf8283 2019-07-24 stsp err = get_short_logmsg(&logmsg, 42, commit);
6371 0ebf8283 2019-07-24 stsp if (err)
6372 0ebf8283 2019-07-24 stsp goto done;
6373 0ebf8283 2019-07-24 stsp }
6374 0ebf8283 2019-07-24 stsp
6375 0ebf8283 2019-07-24 stsp switch (hle->cmd->code) {
6376 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_PICK:
6377 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_EDIT:
6378 0ebf8283 2019-07-24 stsp printf("%s -> %s: %s\n", old_id_str,
6379 0ebf8283 2019-07-24 stsp new_id_str ? new_id_str : "no-op change", logmsg);
6380 0ebf8283 2019-07-24 stsp break;
6381 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_DROP:
6382 0ebf8283 2019-07-24 stsp case GOT_HISTEDIT_FOLD:
6383 0ebf8283 2019-07-24 stsp printf("%s -> %s commit: %s\n", old_id_str, hle->cmd->name,
6384 0ebf8283 2019-07-24 stsp logmsg);
6385 0ebf8283 2019-07-24 stsp break;
6386 0ebf8283 2019-07-24 stsp default:
6387 0ebf8283 2019-07-24 stsp break;
6388 0ebf8283 2019-07-24 stsp }
6389 0ebf8283 2019-07-24 stsp done:
6390 0ebf8283 2019-07-24 stsp free(old_id_str);
6391 0ebf8283 2019-07-24 stsp free(new_id_str);
6392 0ebf8283 2019-07-24 stsp return err;
6393 0ebf8283 2019-07-24 stsp }
6394 0ebf8283 2019-07-24 stsp
6395 0ebf8283 2019-07-24 stsp static const struct got_error *
6396 0ebf8283 2019-07-24 stsp histedit_commit(struct got_pathlist_head *merged_paths,
6397 3e3a69f1 2019-07-25 stsp struct got_worktree *worktree, struct got_fileindex *fileindex,
6398 3e3a69f1 2019-07-25 stsp struct got_reference *tmp_branch, struct got_histedit_list_entry *hle,
6399 3e3a69f1 2019-07-25 stsp struct got_repository *repo)
6400 0ebf8283 2019-07-24 stsp {
6401 0ebf8283 2019-07-24 stsp const struct got_error *err;
6402 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6403 0ebf8283 2019-07-24 stsp struct got_object_id *new_commit_id;
6404 0ebf8283 2019-07-24 stsp
6405 0ebf8283 2019-07-24 stsp if ((hle->cmd->code == GOT_HISTEDIT_EDIT || get_folded_commits(hle))
6406 0ebf8283 2019-07-24 stsp && hle->logmsg == NULL) {
6407 0ebf8283 2019-07-24 stsp err = histedit_edit_logmsg(hle, repo);
6408 0ebf8283 2019-07-24 stsp if (err)
6409 0ebf8283 2019-07-24 stsp return err;
6410 0ebf8283 2019-07-24 stsp }
6411 0ebf8283 2019-07-24 stsp
6412 0ebf8283 2019-07-24 stsp err = got_object_open_as_commit(&commit, repo, hle->commit_id);
6413 0ebf8283 2019-07-24 stsp if (err)
6414 0ebf8283 2019-07-24 stsp return err;
6415 0ebf8283 2019-07-24 stsp
6416 0ebf8283 2019-07-24 stsp err = got_worktree_histedit_commit(&new_commit_id, merged_paths,
6417 3e3a69f1 2019-07-25 stsp worktree, fileindex, tmp_branch, commit, hle->commit_id,
6418 3e3a69f1 2019-07-25 stsp hle->logmsg, repo);
6419 0ebf8283 2019-07-24 stsp if (err) {
6420 0ebf8283 2019-07-24 stsp if (err->code != GOT_ERR_COMMIT_NO_CHANGES)
6421 0ebf8283 2019-07-24 stsp goto done;
6422 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, NULL);
6423 0ebf8283 2019-07-24 stsp } else {
6424 0ebf8283 2019-07-24 stsp err = show_histedit_progress(commit, hle, new_commit_id);
6425 0ebf8283 2019-07-24 stsp free(new_commit_id);
6426 0ebf8283 2019-07-24 stsp }
6427 0ebf8283 2019-07-24 stsp done:
6428 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6429 0ebf8283 2019-07-24 stsp return err;
6430 0ebf8283 2019-07-24 stsp }
6431 0ebf8283 2019-07-24 stsp
6432 0ebf8283 2019-07-24 stsp static const struct got_error *
6433 0ebf8283 2019-07-24 stsp histedit_skip_commit(struct got_histedit_list_entry *hle,
6434 0ebf8283 2019-07-24 stsp struct got_worktree *worktree, struct got_repository *repo)
6435 0ebf8283 2019-07-24 stsp {
6436 0ebf8283 2019-07-24 stsp const struct got_error *error;
6437 0ebf8283 2019-07-24 stsp struct got_commit_object *commit;
6438 0ebf8283 2019-07-24 stsp
6439 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_skip_commit(worktree, hle->commit_id,
6440 0ebf8283 2019-07-24 stsp repo);
6441 0ebf8283 2019-07-24 stsp if (error)
6442 0ebf8283 2019-07-24 stsp return error;
6443 0ebf8283 2019-07-24 stsp
6444 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo, hle->commit_id);
6445 0ebf8283 2019-07-24 stsp if (error)
6446 0ebf8283 2019-07-24 stsp return error;
6447 0ebf8283 2019-07-24 stsp
6448 0ebf8283 2019-07-24 stsp error = show_histedit_progress(commit, hle, NULL);
6449 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6450 0ebf8283 2019-07-24 stsp return error;
6451 ab20a43a 2020-01-29 stsp }
6452 ab20a43a 2020-01-29 stsp
6453 ab20a43a 2020-01-29 stsp static const struct got_error *
6454 ab20a43a 2020-01-29 stsp check_local_changes(void *arg, unsigned char status,
6455 ab20a43a 2020-01-29 stsp unsigned char staged_status, const char *path,
6456 ab20a43a 2020-01-29 stsp struct got_object_id *blob_id, struct got_object_id *staged_blob_id,
6457 ab20a43a 2020-01-29 stsp struct got_object_id *commit_id, int dirfd, const char *de_name)
6458 ab20a43a 2020-01-29 stsp {
6459 ab20a43a 2020-01-29 stsp int *have_local_changes = arg;
6460 ab20a43a 2020-01-29 stsp
6461 ab20a43a 2020-01-29 stsp switch (status) {
6462 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6463 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6464 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6465 ab20a43a 2020-01-29 stsp case GOT_STATUS_CONFLICT:
6466 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6467 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6468 ab20a43a 2020-01-29 stsp default:
6469 ab20a43a 2020-01-29 stsp break;
6470 ab20a43a 2020-01-29 stsp }
6471 ab20a43a 2020-01-29 stsp
6472 ab20a43a 2020-01-29 stsp switch (staged_status) {
6473 ab20a43a 2020-01-29 stsp case GOT_STATUS_ADD:
6474 ab20a43a 2020-01-29 stsp case GOT_STATUS_DELETE:
6475 ab20a43a 2020-01-29 stsp case GOT_STATUS_MODIFY:
6476 ab20a43a 2020-01-29 stsp *have_local_changes = 1;
6477 ab20a43a 2020-01-29 stsp return got_error(GOT_ERR_CANCELLED);
6478 ab20a43a 2020-01-29 stsp default:
6479 ab20a43a 2020-01-29 stsp break;
6480 ab20a43a 2020-01-29 stsp }
6481 ab20a43a 2020-01-29 stsp
6482 ab20a43a 2020-01-29 stsp return NULL;
6483 0ebf8283 2019-07-24 stsp }
6484 0ebf8283 2019-07-24 stsp
6485 0ebf8283 2019-07-24 stsp static const struct got_error *
6486 0ebf8283 2019-07-24 stsp cmd_histedit(int argc, char *argv[])
6487 0ebf8283 2019-07-24 stsp {
6488 0ebf8283 2019-07-24 stsp const struct got_error *error = NULL;
6489 0ebf8283 2019-07-24 stsp struct got_worktree *worktree = NULL;
6490 3e3a69f1 2019-07-25 stsp struct got_fileindex *fileindex = NULL;
6491 0ebf8283 2019-07-24 stsp struct got_repository *repo = NULL;
6492 0ebf8283 2019-07-24 stsp char *cwd = NULL;
6493 0ebf8283 2019-07-24 stsp struct got_reference *branch = NULL;
6494 0ebf8283 2019-07-24 stsp struct got_reference *tmp_branch = NULL;
6495 0ebf8283 2019-07-24 stsp struct got_object_id *resume_commit_id = NULL;
6496 0ebf8283 2019-07-24 stsp struct got_object_id *base_commit_id = NULL;
6497 0ebf8283 2019-07-24 stsp struct got_object_id *head_commit_id = NULL;
6498 0ebf8283 2019-07-24 stsp struct got_commit_object *commit = NULL;
6499 6ba2bea2 2019-07-27 stsp int ch, rebase_in_progress = 0, did_something;
6500 0ebf8283 2019-07-24 stsp int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
6501 083957f4 2020-02-24 stsp int edit_logmsg_only = 0;
6502 0ebf8283 2019-07-24 stsp const char *edit_script_path = NULL;
6503 0ebf8283 2019-07-24 stsp unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
6504 0ebf8283 2019-07-24 stsp struct got_object_id_queue commits;
6505 0ebf8283 2019-07-24 stsp struct got_pathlist_head merged_paths;
6506 0ebf8283 2019-07-24 stsp const struct got_object_id_queue *parent_ids;
6507 0ebf8283 2019-07-24 stsp struct got_object_qid *pid;
6508 0ebf8283 2019-07-24 stsp struct got_histedit_list histedit_cmds;
6509 0ebf8283 2019-07-24 stsp struct got_histedit_list_entry *hle;
6510 0ebf8283 2019-07-24 stsp
6511 0ebf8283 2019-07-24 stsp SIMPLEQ_INIT(&commits);
6512 0ebf8283 2019-07-24 stsp TAILQ_INIT(&histedit_cmds);
6513 0ebf8283 2019-07-24 stsp TAILQ_INIT(&merged_paths);
6514 0ebf8283 2019-07-24 stsp
6515 083957f4 2020-02-24 stsp while ((ch = getopt(argc, argv, "acF:m")) != -1) {
6516 0ebf8283 2019-07-24 stsp switch (ch) {
6517 0ebf8283 2019-07-24 stsp case 'a':
6518 0ebf8283 2019-07-24 stsp abort_edit = 1;
6519 0ebf8283 2019-07-24 stsp break;
6520 0ebf8283 2019-07-24 stsp case 'c':
6521 0ebf8283 2019-07-24 stsp continue_edit = 1;
6522 0ebf8283 2019-07-24 stsp break;
6523 0ebf8283 2019-07-24 stsp case 'F':
6524 0ebf8283 2019-07-24 stsp edit_script_path = optarg;
6525 0ebf8283 2019-07-24 stsp break;
6526 083957f4 2020-02-24 stsp case 'm':
6527 083957f4 2020-02-24 stsp edit_logmsg_only = 1;
6528 083957f4 2020-02-24 stsp break;
6529 0ebf8283 2019-07-24 stsp default:
6530 0ebf8283 2019-07-24 stsp usage_histedit();
6531 0ebf8283 2019-07-24 stsp /* NOTREACHED */
6532 0ebf8283 2019-07-24 stsp }
6533 0ebf8283 2019-07-24 stsp }
6534 0ebf8283 2019-07-24 stsp
6535 0ebf8283 2019-07-24 stsp argc -= optind;
6536 0ebf8283 2019-07-24 stsp argv += optind;
6537 0ebf8283 2019-07-24 stsp
6538 0ebf8283 2019-07-24 stsp #ifndef PROFILE
6539 0ebf8283 2019-07-24 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6540 0ebf8283 2019-07-24 stsp "unveil", NULL) == -1)
6541 0ebf8283 2019-07-24 stsp err(1, "pledge");
6542 0ebf8283 2019-07-24 stsp #endif
6543 0ebf8283 2019-07-24 stsp if (abort_edit && continue_edit)
6544 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -c options are mutually exclusive");
6545 083957f4 2020-02-24 stsp if (edit_script_path && edit_logmsg_only)
6546 083957f4 2020-02-24 stsp errx(1, "histedit's -F and -m options are mutually exclusive");
6547 083957f4 2020-02-24 stsp if (abort_edit && edit_logmsg_only)
6548 083957f4 2020-02-24 stsp errx(1, "histedit's -a and -m options are mutually exclusive");
6549 083957f4 2020-02-24 stsp if (continue_edit && edit_logmsg_only)
6550 083957f4 2020-02-24 stsp errx(1, "histedit's -c and -m options are mutually exclusive");
6551 0ebf8283 2019-07-24 stsp if (argc != 0)
6552 0ebf8283 2019-07-24 stsp usage_histedit();
6553 0ebf8283 2019-07-24 stsp
6554 0ebf8283 2019-07-24 stsp /*
6555 0ebf8283 2019-07-24 stsp * This command cannot apply unveil(2) in all cases because the
6556 0ebf8283 2019-07-24 stsp * user may choose to run an editor to edit the histedit script
6557 0ebf8283 2019-07-24 stsp * and to edit individual commit log messages.
6558 0ebf8283 2019-07-24 stsp * unveil(2) traverses exec(2); if an editor is used we have to
6559 0ebf8283 2019-07-24 stsp * apply unveil after edit script and log messages have been written.
6560 0ebf8283 2019-07-24 stsp * XXX TODO: Make use of unveil(2) where possible.
6561 0ebf8283 2019-07-24 stsp */
6562 0ebf8283 2019-07-24 stsp
6563 0ebf8283 2019-07-24 stsp cwd = getcwd(NULL, 0);
6564 0ebf8283 2019-07-24 stsp if (cwd == NULL) {
6565 0ebf8283 2019-07-24 stsp error = got_error_from_errno("getcwd");
6566 0ebf8283 2019-07-24 stsp goto done;
6567 0ebf8283 2019-07-24 stsp }
6568 0ebf8283 2019-07-24 stsp error = got_worktree_open(&worktree, cwd);
6569 0ebf8283 2019-07-24 stsp if (error)
6570 0ebf8283 2019-07-24 stsp goto done;
6571 0ebf8283 2019-07-24 stsp
6572 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6573 c9956ddf 2019-09-08 stsp NULL);
6574 0ebf8283 2019-07-24 stsp if (error != NULL)
6575 0ebf8283 2019-07-24 stsp goto done;
6576 0ebf8283 2019-07-24 stsp
6577 0ebf8283 2019-07-24 stsp error = got_worktree_rebase_in_progress(&rebase_in_progress, worktree);
6578 0ebf8283 2019-07-24 stsp if (error)
6579 0ebf8283 2019-07-24 stsp goto done;
6580 0ebf8283 2019-07-24 stsp if (rebase_in_progress) {
6581 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_REBASING);
6582 0ebf8283 2019-07-24 stsp goto done;
6583 0ebf8283 2019-07-24 stsp }
6584 0ebf8283 2019-07-24 stsp
6585 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_in_progress(&edit_in_progress, worktree);
6586 0ebf8283 2019-07-24 stsp if (error)
6587 0ebf8283 2019-07-24 stsp goto done;
6588 0ebf8283 2019-07-24 stsp
6589 083957f4 2020-02-24 stsp if (edit_in_progress && edit_logmsg_only) {
6590 083957f4 2020-02-24 stsp error = got_error_msg(GOT_ERR_HISTEDIT_BUSY,
6591 083957f4 2020-02-24 stsp "histedit operation is in progress in this "
6592 083957f4 2020-02-24 stsp "work tree and must be continued or aborted "
6593 083957f4 2020-02-24 stsp "before the -m option can be used");
6594 083957f4 2020-02-24 stsp goto done;
6595 083957f4 2020-02-24 stsp }
6596 083957f4 2020-02-24 stsp
6597 0ebf8283 2019-07-24 stsp if (edit_in_progress && abort_edit) {
6598 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6599 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6600 3e3a69f1 2019-07-25 stsp worktree, repo);
6601 0ebf8283 2019-07-24 stsp if (error)
6602 0ebf8283 2019-07-24 stsp goto done;
6603 0ebf8283 2019-07-24 stsp printf("Switching work tree to %s\n",
6604 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6605 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_abort(worktree, fileindex, repo,
6606 0ebf8283 2019-07-24 stsp branch, base_commit_id, update_progress, &did_something);
6607 0ebf8283 2019-07-24 stsp if (error)
6608 0ebf8283 2019-07-24 stsp goto done;
6609 0ebf8283 2019-07-24 stsp printf("Histedit of %s aborted\n",
6610 0ebf8283 2019-07-24 stsp got_ref_get_symref_target(branch));
6611 0ebf8283 2019-07-24 stsp goto done; /* nothing else to do */
6612 0ebf8283 2019-07-24 stsp } else if (abort_edit) {
6613 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6614 0ebf8283 2019-07-24 stsp goto done;
6615 0ebf8283 2019-07-24 stsp }
6616 0ebf8283 2019-07-24 stsp
6617 0ebf8283 2019-07-24 stsp if (continue_edit) {
6618 0ebf8283 2019-07-24 stsp char *path;
6619 0ebf8283 2019-07-24 stsp
6620 0ebf8283 2019-07-24 stsp if (!edit_in_progress) {
6621 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_NOT_HISTEDIT);
6622 0ebf8283 2019-07-24 stsp goto done;
6623 0ebf8283 2019-07-24 stsp }
6624 0ebf8283 2019-07-24 stsp
6625 c3022ba5 2019-07-27 stsp error = got_worktree_get_histedit_script_path(&path, worktree);
6626 0ebf8283 2019-07-24 stsp if (error)
6627 0ebf8283 2019-07-24 stsp goto done;
6628 0ebf8283 2019-07-24 stsp
6629 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds, path, repo);
6630 0ebf8283 2019-07-24 stsp free(path);
6631 0ebf8283 2019-07-24 stsp if (error)
6632 0ebf8283 2019-07-24 stsp goto done;
6633 0ebf8283 2019-07-24 stsp
6634 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_continue(&resume_commit_id,
6635 3e3a69f1 2019-07-25 stsp &tmp_branch, &branch, &base_commit_id, &fileindex,
6636 3e3a69f1 2019-07-25 stsp worktree, repo);
6637 0ebf8283 2019-07-24 stsp if (error)
6638 0ebf8283 2019-07-24 stsp goto done;
6639 0ebf8283 2019-07-24 stsp
6640 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6641 0ebf8283 2019-07-24 stsp if (error)
6642 0ebf8283 2019-07-24 stsp goto done;
6643 0ebf8283 2019-07-24 stsp
6644 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6645 0ebf8283 2019-07-24 stsp head_commit_id);
6646 0ebf8283 2019-07-24 stsp if (error)
6647 0ebf8283 2019-07-24 stsp goto done;
6648 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6649 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6650 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6651 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6652 3aac7cf7 2019-07-25 stsp goto done;
6653 3aac7cf7 2019-07-25 stsp }
6654 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6655 8ca9bd68 2019-07-25 stsp base_commit_id, got_worktree_get_path_prefix(worktree),
6656 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6657 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6658 0ebf8283 2019-07-24 stsp commit = NULL;
6659 0ebf8283 2019-07-24 stsp if (error)
6660 0ebf8283 2019-07-24 stsp goto done;
6661 0ebf8283 2019-07-24 stsp } else {
6662 0ebf8283 2019-07-24 stsp if (edit_in_progress) {
6663 0ebf8283 2019-07-24 stsp error = got_error(GOT_ERR_HISTEDIT_BUSY);
6664 0ebf8283 2019-07-24 stsp goto done;
6665 0ebf8283 2019-07-24 stsp }
6666 0ebf8283 2019-07-24 stsp
6667 0ebf8283 2019-07-24 stsp error = got_ref_open(&branch, repo,
6668 0ebf8283 2019-07-24 stsp got_worktree_get_head_ref_name(worktree), 0);
6669 0ebf8283 2019-07-24 stsp if (error != NULL)
6670 0ebf8283 2019-07-24 stsp goto done;
6671 0ebf8283 2019-07-24 stsp
6672 c7d20a3f 2019-07-30 stsp if (strncmp(got_ref_get_name(branch), "refs/heads/", 11) != 0) {
6673 c7d20a3f 2019-07-30 stsp error = got_error_msg(GOT_ERR_COMMIT_BRANCH,
6674 c7d20a3f 2019-07-30 stsp "will not edit commit history of a branch outside "
6675 c7d20a3f 2019-07-30 stsp "the \"refs/heads/\" reference namespace");
6676 c7d20a3f 2019-07-30 stsp goto done;
6677 c7d20a3f 2019-07-30 stsp }
6678 c7d20a3f 2019-07-30 stsp
6679 0ebf8283 2019-07-24 stsp error = got_ref_resolve(&head_commit_id, repo, branch);
6680 bfce7f83 2019-07-27 stsp got_ref_close(branch);
6681 bfce7f83 2019-07-27 stsp branch = NULL;
6682 0ebf8283 2019-07-24 stsp if (error)
6683 0ebf8283 2019-07-24 stsp goto done;
6684 0ebf8283 2019-07-24 stsp
6685 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6686 0ebf8283 2019-07-24 stsp head_commit_id);
6687 0ebf8283 2019-07-24 stsp if (error)
6688 0ebf8283 2019-07-24 stsp goto done;
6689 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6690 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6691 3aac7cf7 2019-07-25 stsp if (pid == NULL) {
6692 3aac7cf7 2019-07-25 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6693 3aac7cf7 2019-07-25 stsp goto done;
6694 3aac7cf7 2019-07-25 stsp }
6695 8ca9bd68 2019-07-25 stsp error = collect_commits(&commits, head_commit_id, pid->id,
6696 8ca9bd68 2019-07-25 stsp got_worktree_get_base_commit_id(worktree),
6697 8ca9bd68 2019-07-25 stsp got_worktree_get_path_prefix(worktree),
6698 8ca9bd68 2019-07-25 stsp GOT_ERR_HISTEDIT_PATH, repo);
6699 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6700 0ebf8283 2019-07-24 stsp commit = NULL;
6701 0ebf8283 2019-07-24 stsp if (error)
6702 0ebf8283 2019-07-24 stsp goto done;
6703 0ebf8283 2019-07-24 stsp
6704 c5996fff 2020-01-29 stsp if (SIMPLEQ_EMPTY(&commits)) {
6705 c5996fff 2020-01-29 stsp error = got_error(GOT_ERR_EMPTY_HISTEDIT);
6706 c5996fff 2020-01-29 stsp goto done;
6707 c5996fff 2020-01-29 stsp }
6708 c5996fff 2020-01-29 stsp
6709 bfce7f83 2019-07-27 stsp error = got_worktree_histedit_prepare(&tmp_branch, &branch,
6710 bfce7f83 2019-07-27 stsp &base_commit_id, &fileindex, worktree, repo);
6711 bfce7f83 2019-07-27 stsp if (error)
6712 bfce7f83 2019-07-27 stsp goto done;
6713 bfce7f83 2019-07-27 stsp
6714 0ebf8283 2019-07-24 stsp if (edit_script_path) {
6715 0ebf8283 2019-07-24 stsp error = histedit_load_list(&histedit_cmds,
6716 0ebf8283 2019-07-24 stsp edit_script_path, repo);
6717 6ba2bea2 2019-07-27 stsp if (error) {
6718 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6719 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6720 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6721 0ebf8283 2019-07-24 stsp goto done;
6722 6ba2bea2 2019-07-27 stsp }
6723 0ebf8283 2019-07-24 stsp } else {
6724 514f2ffe 2020-01-29 stsp const char *branch_name;
6725 514f2ffe 2020-01-29 stsp branch_name = got_ref_get_symref_target(branch);
6726 514f2ffe 2020-01-29 stsp if (strncmp(branch_name, "refs/heads/", 11) == 0)
6727 514f2ffe 2020-01-29 stsp branch_name += 11;
6728 0ebf8283 2019-07-24 stsp error = histedit_edit_script(&histedit_cmds, &commits,
6729 083957f4 2020-02-24 stsp branch_name, edit_logmsg_only, repo);
6730 6ba2bea2 2019-07-27 stsp if (error) {
6731 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6732 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6733 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6734 0ebf8283 2019-07-24 stsp goto done;
6735 6ba2bea2 2019-07-27 stsp }
6736 0ebf8283 2019-07-24 stsp
6737 0ebf8283 2019-07-24 stsp }
6738 0ebf8283 2019-07-24 stsp
6739 0ebf8283 2019-07-24 stsp error = histedit_save_list(&histedit_cmds, worktree,
6740 0ebf8283 2019-07-24 stsp repo);
6741 6ba2bea2 2019-07-27 stsp if (error) {
6742 6ba2bea2 2019-07-27 stsp got_worktree_histedit_abort(worktree, fileindex,
6743 6ba2bea2 2019-07-27 stsp repo, branch, base_commit_id,
6744 6ba2bea2 2019-07-27 stsp update_progress, &did_something);
6745 0ebf8283 2019-07-24 stsp goto done;
6746 6ba2bea2 2019-07-27 stsp }
6747 0ebf8283 2019-07-24 stsp
6748 0ebf8283 2019-07-24 stsp }
6749 0ebf8283 2019-07-24 stsp
6750 4ec14e60 2019-08-28 hiltjo error = histedit_check_script(&histedit_cmds, &commits, repo);
6751 4ec14e60 2019-08-28 hiltjo if (error)
6752 0ebf8283 2019-07-24 stsp goto done;
6753 0ebf8283 2019-07-24 stsp
6754 0ebf8283 2019-07-24 stsp TAILQ_FOREACH(hle, &histedit_cmds, entry) {
6755 0ebf8283 2019-07-24 stsp if (resume_commit_id) {
6756 0ebf8283 2019-07-24 stsp if (got_object_id_cmp(hle->commit_id,
6757 0ebf8283 2019-07-24 stsp resume_commit_id) != 0)
6758 0ebf8283 2019-07-24 stsp continue;
6759 0ebf8283 2019-07-24 stsp
6760 0ebf8283 2019-07-24 stsp resume_commit_id = NULL;
6761 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP ||
6762 0ebf8283 2019-07-24 stsp hle->cmd->code == GOT_HISTEDIT_FOLD) {
6763 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree,
6764 0ebf8283 2019-07-24 stsp repo);
6765 ab20a43a 2020-01-29 stsp if (error)
6766 ab20a43a 2020-01-29 stsp goto done;
6767 0ebf8283 2019-07-24 stsp } else {
6768 ab20a43a 2020-01-29 stsp struct got_pathlist_head paths;
6769 ab20a43a 2020-01-29 stsp int have_changes = 0;
6770 ab20a43a 2020-01-29 stsp
6771 ab20a43a 2020-01-29 stsp TAILQ_INIT(&paths);
6772 ab20a43a 2020-01-29 stsp error = got_pathlist_append(&paths, "", NULL);
6773 ab20a43a 2020-01-29 stsp if (error)
6774 ab20a43a 2020-01-29 stsp goto done;
6775 ab20a43a 2020-01-29 stsp error = got_worktree_status(worktree, &paths,
6776 ab20a43a 2020-01-29 stsp repo, check_local_changes, &have_changes,
6777 ab20a43a 2020-01-29 stsp check_cancelled, NULL);
6778 ab20a43a 2020-01-29 stsp got_pathlist_free(&paths);
6779 ab20a43a 2020-01-29 stsp if (error) {
6780 ab20a43a 2020-01-29 stsp if (error->code != GOT_ERR_CANCELLED)
6781 ab20a43a 2020-01-29 stsp goto done;
6782 ab20a43a 2020-01-29 stsp if (sigint_received || sigpipe_received)
6783 ab20a43a 2020-01-29 stsp goto done;
6784 ab20a43a 2020-01-29 stsp }
6785 ab20a43a 2020-01-29 stsp if (have_changes) {
6786 ab20a43a 2020-01-29 stsp error = histedit_commit(NULL, worktree,
6787 ab20a43a 2020-01-29 stsp fileindex, tmp_branch, hle, repo);
6788 ab20a43a 2020-01-29 stsp if (error)
6789 ab20a43a 2020-01-29 stsp goto done;
6790 ab20a43a 2020-01-29 stsp } else {
6791 ab20a43a 2020-01-29 stsp error = got_object_open_as_commit(
6792 ab20a43a 2020-01-29 stsp &commit, repo, hle->commit_id);
6793 ab20a43a 2020-01-29 stsp if (error)
6794 ab20a43a 2020-01-29 stsp goto done;
6795 ab20a43a 2020-01-29 stsp error = show_histedit_progress(commit,
6796 ab20a43a 2020-01-29 stsp hle, NULL);
6797 ab20a43a 2020-01-29 stsp got_object_commit_close(commit);
6798 ab20a43a 2020-01-29 stsp commit = NULL;
6799 ab20a43a 2020-01-29 stsp if (error)
6800 ab20a43a 2020-01-29 stsp goto done;
6801 ab20a43a 2020-01-29 stsp }
6802 0ebf8283 2019-07-24 stsp }
6803 0ebf8283 2019-07-24 stsp continue;
6804 0ebf8283 2019-07-24 stsp }
6805 0ebf8283 2019-07-24 stsp
6806 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_DROP) {
6807 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6808 0ebf8283 2019-07-24 stsp if (error)
6809 0ebf8283 2019-07-24 stsp goto done;
6810 0ebf8283 2019-07-24 stsp continue;
6811 0ebf8283 2019-07-24 stsp }
6812 0ebf8283 2019-07-24 stsp
6813 0ebf8283 2019-07-24 stsp error = got_object_open_as_commit(&commit, repo,
6814 0ebf8283 2019-07-24 stsp hle->commit_id);
6815 0ebf8283 2019-07-24 stsp if (error)
6816 0ebf8283 2019-07-24 stsp goto done;
6817 0ebf8283 2019-07-24 stsp parent_ids = got_object_commit_get_parent_ids(commit);
6818 0ebf8283 2019-07-24 stsp pid = SIMPLEQ_FIRST(parent_ids);
6819 0ebf8283 2019-07-24 stsp
6820 0ebf8283 2019-07-24 stsp error = got_worktree_histedit_merge_files(&merged_paths,
6821 3e3a69f1 2019-07-25 stsp worktree, fileindex, pid->id, hle->commit_id, repo,
6822 3e3a69f1 2019-07-25 stsp rebase_progress, &rebase_status, check_cancelled, NULL);
6823 0ebf8283 2019-07-24 stsp if (error)
6824 0ebf8283 2019-07-24 stsp goto done;
6825 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6826 0ebf8283 2019-07-24 stsp commit = NULL;
6827 0ebf8283 2019-07-24 stsp
6828 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6829 a0ea4fc0 2020-02-28 stsp error = show_rebase_merge_conflict(hle->commit_id,
6830 a0ea4fc0 2020-02-28 stsp repo);
6831 a0ea4fc0 2020-02-28 stsp if (error)
6832 a0ea4fc0 2020-02-28 stsp goto done;
6833 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6834 0ebf8283 2019-07-24 stsp break;
6835 0ebf8283 2019-07-24 stsp }
6836 0ebf8283 2019-07-24 stsp
6837 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_EDIT) {
6838 0ebf8283 2019-07-24 stsp char *id_str;
6839 0ebf8283 2019-07-24 stsp error = got_object_id_str(&id_str, hle->commit_id);
6840 0ebf8283 2019-07-24 stsp if (error)
6841 0ebf8283 2019-07-24 stsp goto done;
6842 0ebf8283 2019-07-24 stsp printf("Stopping histedit for amending commit %s\n",
6843 0ebf8283 2019-07-24 stsp id_str);
6844 0ebf8283 2019-07-24 stsp free(id_str);
6845 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6846 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree,
6847 3e3a69f1 2019-07-25 stsp fileindex);
6848 0ebf8283 2019-07-24 stsp goto done;
6849 0ebf8283 2019-07-24 stsp }
6850 0ebf8283 2019-07-24 stsp
6851 0ebf8283 2019-07-24 stsp if (hle->cmd->code == GOT_HISTEDIT_FOLD) {
6852 0ebf8283 2019-07-24 stsp error = histedit_skip_commit(hle, worktree, repo);
6853 0ebf8283 2019-07-24 stsp if (error)
6854 0ebf8283 2019-07-24 stsp goto done;
6855 0ebf8283 2019-07-24 stsp continue;
6856 0ebf8283 2019-07-24 stsp }
6857 0ebf8283 2019-07-24 stsp
6858 3e3a69f1 2019-07-25 stsp error = histedit_commit(&merged_paths, worktree, fileindex,
6859 3e3a69f1 2019-07-25 stsp tmp_branch, hle, repo);
6860 0ebf8283 2019-07-24 stsp got_worktree_rebase_pathlist_free(&merged_paths);
6861 0ebf8283 2019-07-24 stsp if (error)
6862 0ebf8283 2019-07-24 stsp goto done;
6863 0ebf8283 2019-07-24 stsp }
6864 0ebf8283 2019-07-24 stsp
6865 0ebf8283 2019-07-24 stsp if (rebase_status == GOT_STATUS_CONFLICT) {
6866 3e3a69f1 2019-07-25 stsp error = got_worktree_histedit_postpone(worktree, fileindex);
6867 0ebf8283 2019-07-24 stsp if (error)
6868 0ebf8283 2019-07-24 stsp goto done;
6869 0ebf8283 2019-07-24 stsp error = got_error_msg(GOT_ERR_CONFLICTS,
6870 0ebf8283 2019-07-24 stsp "conflicts must be resolved before rebasing can continue");
6871 0ebf8283 2019-07-24 stsp } else
6872 3e3a69f1 2019-07-25 stsp error = histedit_complete(worktree, fileindex, tmp_branch,
6873 3e3a69f1 2019-07-25 stsp branch, repo);
6874 0ebf8283 2019-07-24 stsp done:
6875 0ebf8283 2019-07-24 stsp got_object_id_queue_free(&commits);
6876 bfce7f83 2019-07-27 stsp histedit_free_list(&histedit_cmds);
6877 0ebf8283 2019-07-24 stsp free(head_commit_id);
6878 0ebf8283 2019-07-24 stsp free(base_commit_id);
6879 0ebf8283 2019-07-24 stsp free(resume_commit_id);
6880 0ebf8283 2019-07-24 stsp if (commit)
6881 0ebf8283 2019-07-24 stsp got_object_commit_close(commit);
6882 0ebf8283 2019-07-24 stsp if (branch)
6883 0ebf8283 2019-07-24 stsp got_ref_close(branch);
6884 0ebf8283 2019-07-24 stsp if (tmp_branch)
6885 0ebf8283 2019-07-24 stsp got_ref_close(tmp_branch);
6886 0ebf8283 2019-07-24 stsp if (worktree)
6887 0ebf8283 2019-07-24 stsp got_worktree_close(worktree);
6888 2822a352 2019-10-15 stsp if (repo)
6889 2822a352 2019-10-15 stsp got_repo_close(repo);
6890 2822a352 2019-10-15 stsp return error;
6891 2822a352 2019-10-15 stsp }
6892 2822a352 2019-10-15 stsp
6893 2822a352 2019-10-15 stsp __dead static void
6894 2822a352 2019-10-15 stsp usage_integrate(void)
6895 2822a352 2019-10-15 stsp {
6896 2822a352 2019-10-15 stsp fprintf(stderr, "usage: %s integrate branch\n", getprogname());
6897 2822a352 2019-10-15 stsp exit(1);
6898 2822a352 2019-10-15 stsp }
6899 2822a352 2019-10-15 stsp
6900 2822a352 2019-10-15 stsp static const struct got_error *
6901 2822a352 2019-10-15 stsp cmd_integrate(int argc, char *argv[])
6902 2822a352 2019-10-15 stsp {
6903 2822a352 2019-10-15 stsp const struct got_error *error = NULL;
6904 2822a352 2019-10-15 stsp struct got_repository *repo = NULL;
6905 2822a352 2019-10-15 stsp struct got_worktree *worktree = NULL;
6906 2822a352 2019-10-15 stsp char *cwd = NULL, *refname = NULL, *base_refname = NULL;
6907 2822a352 2019-10-15 stsp const char *branch_arg = NULL;
6908 2822a352 2019-10-15 stsp struct got_reference *branch_ref = NULL, *base_branch_ref = NULL;
6909 2822a352 2019-10-15 stsp struct got_fileindex *fileindex = NULL;
6910 2822a352 2019-10-15 stsp struct got_object_id *commit_id = NULL, *base_commit_id = NULL;
6911 2822a352 2019-10-15 stsp int ch, did_something = 0;
6912 2822a352 2019-10-15 stsp
6913 2822a352 2019-10-15 stsp while ((ch = getopt(argc, argv, "")) != -1) {
6914 2822a352 2019-10-15 stsp switch (ch) {
6915 2822a352 2019-10-15 stsp default:
6916 2822a352 2019-10-15 stsp usage_integrate();
6917 2822a352 2019-10-15 stsp /* NOTREACHED */
6918 2822a352 2019-10-15 stsp }
6919 2822a352 2019-10-15 stsp }
6920 2822a352 2019-10-15 stsp
6921 2822a352 2019-10-15 stsp argc -= optind;
6922 2822a352 2019-10-15 stsp argv += optind;
6923 2822a352 2019-10-15 stsp
6924 2822a352 2019-10-15 stsp if (argc != 1)
6925 2822a352 2019-10-15 stsp usage_integrate();
6926 2822a352 2019-10-15 stsp branch_arg = argv[0];
6927 2822a352 2019-10-15 stsp
6928 2822a352 2019-10-15 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
6929 2822a352 2019-10-15 stsp "unveil", NULL) == -1)
6930 2822a352 2019-10-15 stsp err(1, "pledge");
6931 2822a352 2019-10-15 stsp
6932 2822a352 2019-10-15 stsp cwd = getcwd(NULL, 0);
6933 2822a352 2019-10-15 stsp if (cwd == NULL) {
6934 2822a352 2019-10-15 stsp error = got_error_from_errno("getcwd");
6935 2822a352 2019-10-15 stsp goto done;
6936 2822a352 2019-10-15 stsp }
6937 2822a352 2019-10-15 stsp
6938 2822a352 2019-10-15 stsp error = got_worktree_open(&worktree, cwd);
6939 2822a352 2019-10-15 stsp if (error)
6940 2822a352 2019-10-15 stsp goto done;
6941 2822a352 2019-10-15 stsp
6942 2822a352 2019-10-15 stsp error = check_rebase_or_histedit_in_progress(worktree);
6943 2822a352 2019-10-15 stsp if (error)
6944 2822a352 2019-10-15 stsp goto done;
6945 2822a352 2019-10-15 stsp
6946 2822a352 2019-10-15 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
6947 2822a352 2019-10-15 stsp NULL);
6948 2822a352 2019-10-15 stsp if (error != NULL)
6949 2822a352 2019-10-15 stsp goto done;
6950 2822a352 2019-10-15 stsp
6951 2822a352 2019-10-15 stsp error = apply_unveil(got_repo_get_path(repo), 0,
6952 2822a352 2019-10-15 stsp got_worktree_get_root_path(worktree));
6953 2822a352 2019-10-15 stsp if (error)
6954 2822a352 2019-10-15 stsp goto done;
6955 2822a352 2019-10-15 stsp
6956 2822a352 2019-10-15 stsp if (asprintf(&refname, "refs/heads/%s", branch_arg) == -1) {
6957 2822a352 2019-10-15 stsp error = got_error_from_errno("asprintf");
6958 2822a352 2019-10-15 stsp goto done;
6959 2822a352 2019-10-15 stsp }
6960 2822a352 2019-10-15 stsp
6961 2822a352 2019-10-15 stsp error = got_worktree_integrate_prepare(&fileindex, &branch_ref,
6962 2822a352 2019-10-15 stsp &base_branch_ref, worktree, refname, repo);
6963 2822a352 2019-10-15 stsp if (error)
6964 2822a352 2019-10-15 stsp goto done;
6965 2822a352 2019-10-15 stsp
6966 2822a352 2019-10-15 stsp refname = strdup(got_ref_get_name(branch_ref));
6967 2822a352 2019-10-15 stsp if (refname == NULL) {
6968 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6969 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6970 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6971 2822a352 2019-10-15 stsp goto done;
6972 2822a352 2019-10-15 stsp }
6973 2822a352 2019-10-15 stsp base_refname = strdup(got_ref_get_name(base_branch_ref));
6974 2822a352 2019-10-15 stsp if (base_refname == NULL) {
6975 2822a352 2019-10-15 stsp error = got_error_from_errno("strdup");
6976 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6977 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6978 2822a352 2019-10-15 stsp goto done;
6979 2822a352 2019-10-15 stsp }
6980 2822a352 2019-10-15 stsp
6981 2822a352 2019-10-15 stsp error = got_ref_resolve(&commit_id, repo, branch_ref);
6982 2822a352 2019-10-15 stsp if (error)
6983 2822a352 2019-10-15 stsp goto done;
6984 2822a352 2019-10-15 stsp
6985 2822a352 2019-10-15 stsp error = got_ref_resolve(&base_commit_id, repo, base_branch_ref);
6986 2822a352 2019-10-15 stsp if (error)
6987 2822a352 2019-10-15 stsp goto done;
6988 2822a352 2019-10-15 stsp
6989 2822a352 2019-10-15 stsp if (got_object_id_cmp(commit_id, base_commit_id) == 0) {
6990 2822a352 2019-10-15 stsp error = got_error_msg(GOT_ERR_SAME_BRANCH,
6991 2822a352 2019-10-15 stsp "specified branch has already been integrated");
6992 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
6993 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
6994 2822a352 2019-10-15 stsp goto done;
6995 2822a352 2019-10-15 stsp }
6996 2822a352 2019-10-15 stsp
6997 3aef623b 2019-10-15 stsp error = check_linear_ancestry(commit_id, base_commit_id, 1, repo);
6998 2822a352 2019-10-15 stsp if (error) {
6999 2822a352 2019-10-15 stsp if (error->code == GOT_ERR_ANCESTRY)
7000 2822a352 2019-10-15 stsp error = got_error(GOT_ERR_REBASE_REQUIRED);
7001 2822a352 2019-10-15 stsp got_worktree_integrate_abort(worktree, fileindex, repo,
7002 2822a352 2019-10-15 stsp branch_ref, base_branch_ref);
7003 2822a352 2019-10-15 stsp goto done;
7004 2822a352 2019-10-15 stsp }
7005 2822a352 2019-10-15 stsp
7006 2822a352 2019-10-15 stsp error = got_worktree_integrate_continue(worktree, fileindex, repo,
7007 2822a352 2019-10-15 stsp branch_ref, base_branch_ref, update_progress, &did_something,
7008 2822a352 2019-10-15 stsp check_cancelled, NULL);
7009 2822a352 2019-10-15 stsp if (error)
7010 2822a352 2019-10-15 stsp goto done;
7011 2822a352 2019-10-15 stsp
7012 2822a352 2019-10-15 stsp printf("Integrated %s into %s\n", refname, base_refname);
7013 2822a352 2019-10-15 stsp done:
7014 715dc77e 2019-08-03 stsp if (repo)
7015 715dc77e 2019-08-03 stsp got_repo_close(repo);
7016 2822a352 2019-10-15 stsp if (worktree)
7017 2822a352 2019-10-15 stsp got_worktree_close(worktree);
7018 2822a352 2019-10-15 stsp free(cwd);
7019 2822a352 2019-10-15 stsp free(base_commit_id);
7020 2822a352 2019-10-15 stsp free(commit_id);
7021 2822a352 2019-10-15 stsp free(refname);
7022 2822a352 2019-10-15 stsp free(base_refname);
7023 715dc77e 2019-08-03 stsp return error;
7024 715dc77e 2019-08-03 stsp }
7025 715dc77e 2019-08-03 stsp
7026 715dc77e 2019-08-03 stsp __dead static void
7027 715dc77e 2019-08-03 stsp usage_stage(void)
7028 715dc77e 2019-08-03 stsp {
7029 f3055044 2019-08-07 stsp fprintf(stderr, "usage: %s stage [-l] | [-p] [-F response-script] "
7030 f3055044 2019-08-07 stsp "[file-path ...]\n",
7031 715dc77e 2019-08-03 stsp getprogname());
7032 715dc77e 2019-08-03 stsp exit(1);
7033 715dc77e 2019-08-03 stsp }
7034 715dc77e 2019-08-03 stsp
7035 715dc77e 2019-08-03 stsp static const struct got_error *
7036 408b4ebc 2019-08-03 stsp print_stage(void *arg, unsigned char status, unsigned char staged_status,
7037 408b4ebc 2019-08-03 stsp const char *path, struct got_object_id *blob_id,
7038 12463d8b 2019-12-13 stsp struct got_object_id *staged_blob_id, struct got_object_id *commit_id,
7039 12463d8b 2019-12-13 stsp int dirfd, const char *de_name)
7040 408b4ebc 2019-08-03 stsp {
7041 408b4ebc 2019-08-03 stsp const struct got_error *err = NULL;
7042 408b4ebc 2019-08-03 stsp char *id_str = NULL;
7043 408b4ebc 2019-08-03 stsp
7044 408b4ebc 2019-08-03 stsp if (staged_status != GOT_STATUS_ADD &&
7045 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_MODIFY &&
7046 408b4ebc 2019-08-03 stsp staged_status != GOT_STATUS_DELETE)
7047 408b4ebc 2019-08-03 stsp return NULL;
7048 408b4ebc 2019-08-03 stsp
7049 408b4ebc 2019-08-03 stsp if (staged_status == GOT_STATUS_ADD ||
7050 408b4ebc 2019-08-03 stsp staged_status == GOT_STATUS_MODIFY)
7051 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, staged_blob_id);
7052 408b4ebc 2019-08-03 stsp else
7053 408b4ebc 2019-08-03 stsp err = got_object_id_str(&id_str, blob_id);
7054 408b4ebc 2019-08-03 stsp if (err)
7055 408b4ebc 2019-08-03 stsp return err;
7056 408b4ebc 2019-08-03 stsp
7057 408b4ebc 2019-08-03 stsp printf("%s %c %s\n", id_str, staged_status, path);
7058 408b4ebc 2019-08-03 stsp free(id_str);
7059 dc424a06 2019-08-07 stsp return NULL;
7060 dc424a06 2019-08-07 stsp }
7061 2e1f37b0 2019-08-08 stsp
7062 dc424a06 2019-08-07 stsp static const struct got_error *
7063 715dc77e 2019-08-03 stsp cmd_stage(int argc, char *argv[])
7064 715dc77e 2019-08-03 stsp {
7065 715dc77e 2019-08-03 stsp const struct got_error *error = NULL;
7066 715dc77e 2019-08-03 stsp struct got_repository *repo = NULL;
7067 715dc77e 2019-08-03 stsp struct got_worktree *worktree = NULL;
7068 715dc77e 2019-08-03 stsp char *cwd = NULL;
7069 715dc77e 2019-08-03 stsp struct got_pathlist_head paths;
7070 715dc77e 2019-08-03 stsp struct got_pathlist_entry *pe;
7071 dc424a06 2019-08-07 stsp int ch, list_stage = 0, pflag = 0;
7072 dc424a06 2019-08-07 stsp FILE *patch_script_file = NULL;
7073 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7074 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7075 715dc77e 2019-08-03 stsp
7076 715dc77e 2019-08-03 stsp TAILQ_INIT(&paths);
7077 715dc77e 2019-08-03 stsp
7078 dc424a06 2019-08-07 stsp while ((ch = getopt(argc, argv, "lpF:")) != -1) {
7079 715dc77e 2019-08-03 stsp switch (ch) {
7080 408b4ebc 2019-08-03 stsp case 'l':
7081 408b4ebc 2019-08-03 stsp list_stage = 1;
7082 408b4ebc 2019-08-03 stsp break;
7083 dc424a06 2019-08-07 stsp case 'p':
7084 dc424a06 2019-08-07 stsp pflag = 1;
7085 dc424a06 2019-08-07 stsp break;
7086 dc424a06 2019-08-07 stsp case 'F':
7087 dc424a06 2019-08-07 stsp patch_script_path = optarg;
7088 dc424a06 2019-08-07 stsp break;
7089 715dc77e 2019-08-03 stsp default:
7090 715dc77e 2019-08-03 stsp usage_stage();
7091 715dc77e 2019-08-03 stsp /* NOTREACHED */
7092 715dc77e 2019-08-03 stsp }
7093 715dc77e 2019-08-03 stsp }
7094 715dc77e 2019-08-03 stsp
7095 715dc77e 2019-08-03 stsp argc -= optind;
7096 715dc77e 2019-08-03 stsp argv += optind;
7097 715dc77e 2019-08-03 stsp
7098 715dc77e 2019-08-03 stsp #ifndef PROFILE
7099 715dc77e 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7100 715dc77e 2019-08-03 stsp "unveil", NULL) == -1)
7101 715dc77e 2019-08-03 stsp err(1, "pledge");
7102 715dc77e 2019-08-03 stsp #endif
7103 2db2652d 2019-08-07 stsp if (list_stage && (pflag || patch_script_path))
7104 2db2652d 2019-08-07 stsp errx(1, "-l option cannot be used with other options");
7105 dc424a06 2019-08-07 stsp if (patch_script_path && !pflag)
7106 dc424a06 2019-08-07 stsp errx(1, "-F option can only be used together with -p option");
7107 715dc77e 2019-08-03 stsp
7108 715dc77e 2019-08-03 stsp cwd = getcwd(NULL, 0);
7109 715dc77e 2019-08-03 stsp if (cwd == NULL) {
7110 715dc77e 2019-08-03 stsp error = got_error_from_errno("getcwd");
7111 715dc77e 2019-08-03 stsp goto done;
7112 715dc77e 2019-08-03 stsp }
7113 715dc77e 2019-08-03 stsp
7114 715dc77e 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7115 715dc77e 2019-08-03 stsp if (error)
7116 715dc77e 2019-08-03 stsp goto done;
7117 715dc77e 2019-08-03 stsp
7118 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7119 c9956ddf 2019-09-08 stsp NULL);
7120 715dc77e 2019-08-03 stsp if (error != NULL)
7121 715dc77e 2019-08-03 stsp goto done;
7122 715dc77e 2019-08-03 stsp
7123 dc424a06 2019-08-07 stsp if (patch_script_path) {
7124 dc424a06 2019-08-07 stsp patch_script_file = fopen(patch_script_path, "r");
7125 dc424a06 2019-08-07 stsp if (patch_script_file == NULL) {
7126 dc424a06 2019-08-07 stsp error = got_error_from_errno2("fopen",
7127 dc424a06 2019-08-07 stsp patch_script_path);
7128 dc424a06 2019-08-07 stsp goto done;
7129 dc424a06 2019-08-07 stsp }
7130 dc424a06 2019-08-07 stsp }
7131 9fd7cd22 2019-08-30 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7132 715dc77e 2019-08-03 stsp got_worktree_get_root_path(worktree));
7133 715dc77e 2019-08-03 stsp if (error)
7134 715dc77e 2019-08-03 stsp goto done;
7135 715dc77e 2019-08-03 stsp
7136 6d022e97 2019-08-04 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7137 6d022e97 2019-08-04 stsp if (error)
7138 6d022e97 2019-08-04 stsp goto done;
7139 715dc77e 2019-08-03 stsp
7140 6d022e97 2019-08-04 stsp if (list_stage)
7141 408b4ebc 2019-08-03 stsp error = got_worktree_status(worktree, &paths, repo,
7142 408b4ebc 2019-08-03 stsp print_stage, NULL, check_cancelled, NULL);
7143 2e1f37b0 2019-08-08 stsp else {
7144 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7145 2e1f37b0 2019-08-08 stsp cpa.action = "stage";
7146 dc424a06 2019-08-07 stsp error = got_worktree_stage(worktree, &paths,
7147 dc424a06 2019-08-07 stsp pflag ? NULL : print_status, NULL,
7148 2e1f37b0 2019-08-08 stsp pflag ? choose_patch : NULL, &cpa, repo);
7149 2e1f37b0 2019-08-08 stsp }
7150 ad493afc 2019-08-03 stsp done:
7151 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7152 2e1f37b0 2019-08-08 stsp error == NULL)
7153 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7154 ad493afc 2019-08-03 stsp if (repo)
7155 ad493afc 2019-08-03 stsp got_repo_close(repo);
7156 ad493afc 2019-08-03 stsp if (worktree)
7157 ad493afc 2019-08-03 stsp got_worktree_close(worktree);
7158 ad493afc 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7159 ad493afc 2019-08-03 stsp free((char *)pe->path);
7160 ad493afc 2019-08-03 stsp got_pathlist_free(&paths);
7161 ad493afc 2019-08-03 stsp free(cwd);
7162 ad493afc 2019-08-03 stsp return error;
7163 ad493afc 2019-08-03 stsp }
7164 ad493afc 2019-08-03 stsp
7165 ad493afc 2019-08-03 stsp __dead static void
7166 ad493afc 2019-08-03 stsp usage_unstage(void)
7167 ad493afc 2019-08-03 stsp {
7168 2e1f37b0 2019-08-08 stsp fprintf(stderr, "usage: %s unstage [-p] [-F response-script] "
7169 2e1f37b0 2019-08-08 stsp "[file-path ...]\n",
7170 ad493afc 2019-08-03 stsp getprogname());
7171 ad493afc 2019-08-03 stsp exit(1);
7172 ad493afc 2019-08-03 stsp }
7173 ad493afc 2019-08-03 stsp
7174 ad493afc 2019-08-03 stsp
7175 ad493afc 2019-08-03 stsp static const struct got_error *
7176 ad493afc 2019-08-03 stsp cmd_unstage(int argc, char *argv[])
7177 ad493afc 2019-08-03 stsp {
7178 ad493afc 2019-08-03 stsp const struct got_error *error = NULL;
7179 ad493afc 2019-08-03 stsp struct got_repository *repo = NULL;
7180 ad493afc 2019-08-03 stsp struct got_worktree *worktree = NULL;
7181 ad493afc 2019-08-03 stsp char *cwd = NULL;
7182 ad493afc 2019-08-03 stsp struct got_pathlist_head paths;
7183 ad493afc 2019-08-03 stsp struct got_pathlist_entry *pe;
7184 2e1f37b0 2019-08-08 stsp int ch, did_something = 0, pflag = 0;
7185 2e1f37b0 2019-08-08 stsp FILE *patch_script_file = NULL;
7186 2e1f37b0 2019-08-08 stsp const char *patch_script_path = NULL;
7187 2e1f37b0 2019-08-08 stsp struct choose_patch_arg cpa;
7188 ad493afc 2019-08-03 stsp
7189 ad493afc 2019-08-03 stsp TAILQ_INIT(&paths);
7190 ad493afc 2019-08-03 stsp
7191 2e1f37b0 2019-08-08 stsp while ((ch = getopt(argc, argv, "pF:")) != -1) {
7192 ad493afc 2019-08-03 stsp switch (ch) {
7193 2e1f37b0 2019-08-08 stsp case 'p':
7194 2e1f37b0 2019-08-08 stsp pflag = 1;
7195 2e1f37b0 2019-08-08 stsp break;
7196 2e1f37b0 2019-08-08 stsp case 'F':
7197 2e1f37b0 2019-08-08 stsp patch_script_path = optarg;
7198 2e1f37b0 2019-08-08 stsp break;
7199 ad493afc 2019-08-03 stsp default:
7200 ad493afc 2019-08-03 stsp usage_unstage();
7201 ad493afc 2019-08-03 stsp /* NOTREACHED */
7202 ad493afc 2019-08-03 stsp }
7203 ad493afc 2019-08-03 stsp }
7204 ad493afc 2019-08-03 stsp
7205 ad493afc 2019-08-03 stsp argc -= optind;
7206 ad493afc 2019-08-03 stsp argv += optind;
7207 ad493afc 2019-08-03 stsp
7208 ad493afc 2019-08-03 stsp #ifndef PROFILE
7209 ad493afc 2019-08-03 stsp if (pledge("stdio rpath wpath cpath fattr flock proc exec sendfd "
7210 ad493afc 2019-08-03 stsp "unveil", NULL) == -1)
7211 ad493afc 2019-08-03 stsp err(1, "pledge");
7212 ad493afc 2019-08-03 stsp #endif
7213 2e1f37b0 2019-08-08 stsp if (patch_script_path && !pflag)
7214 2e1f37b0 2019-08-08 stsp errx(1, "-F option can only be used together with -p option");
7215 2e1f37b0 2019-08-08 stsp
7216 ad493afc 2019-08-03 stsp cwd = getcwd(NULL, 0);
7217 ad493afc 2019-08-03 stsp if (cwd == NULL) {
7218 ad493afc 2019-08-03 stsp error = got_error_from_errno("getcwd");
7219 ad493afc 2019-08-03 stsp goto done;
7220 ad493afc 2019-08-03 stsp }
7221 ad493afc 2019-08-03 stsp
7222 ad493afc 2019-08-03 stsp error = got_worktree_open(&worktree, cwd);
7223 ad493afc 2019-08-03 stsp if (error)
7224 ad493afc 2019-08-03 stsp goto done;
7225 ad493afc 2019-08-03 stsp
7226 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, got_worktree_get_repo_path(worktree),
7227 c9956ddf 2019-09-08 stsp NULL);
7228 ad493afc 2019-08-03 stsp if (error != NULL)
7229 ad493afc 2019-08-03 stsp goto done;
7230 ad493afc 2019-08-03 stsp
7231 2e1f37b0 2019-08-08 stsp if (patch_script_path) {
7232 2e1f37b0 2019-08-08 stsp patch_script_file = fopen(patch_script_path, "r");
7233 2e1f37b0 2019-08-08 stsp if (patch_script_file == NULL) {
7234 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fopen",
7235 2e1f37b0 2019-08-08 stsp patch_script_path);
7236 2e1f37b0 2019-08-08 stsp goto done;
7237 2e1f37b0 2019-08-08 stsp }
7238 2e1f37b0 2019-08-08 stsp }
7239 2e1f37b0 2019-08-08 stsp
7240 00f36e47 2019-09-06 stsp error = apply_unveil(got_repo_get_path(repo), 0,
7241 ad493afc 2019-08-03 stsp got_worktree_get_root_path(worktree));
7242 ad493afc 2019-08-03 stsp if (error)
7243 ad493afc 2019-08-03 stsp goto done;
7244 ad493afc 2019-08-03 stsp
7245 ad493afc 2019-08-03 stsp error = get_worktree_paths_from_argv(&paths, argc, argv, worktree);
7246 ad493afc 2019-08-03 stsp if (error)
7247 ad493afc 2019-08-03 stsp goto done;
7248 ad493afc 2019-08-03 stsp
7249 2e1f37b0 2019-08-08 stsp cpa.patch_script_file = patch_script_file;
7250 2e1f37b0 2019-08-08 stsp cpa.action = "unstage";
7251 ad493afc 2019-08-03 stsp error = got_worktree_unstage(worktree, &paths, update_progress,
7252 2e1f37b0 2019-08-08 stsp &did_something, pflag ? choose_patch : NULL, &cpa, repo);
7253 715dc77e 2019-08-03 stsp done:
7254 2e1f37b0 2019-08-08 stsp if (patch_script_file && fclose(patch_script_file) == EOF &&
7255 2e1f37b0 2019-08-08 stsp error == NULL)
7256 2e1f37b0 2019-08-08 stsp error = got_error_from_errno2("fclose", patch_script_path);
7257 0ebf8283 2019-07-24 stsp if (repo)
7258 0ebf8283 2019-07-24 stsp got_repo_close(repo);
7259 715dc77e 2019-08-03 stsp if (worktree)
7260 715dc77e 2019-08-03 stsp got_worktree_close(worktree);
7261 715dc77e 2019-08-03 stsp TAILQ_FOREACH(pe, &paths, entry)
7262 715dc77e 2019-08-03 stsp free((char *)pe->path);
7263 715dc77e 2019-08-03 stsp got_pathlist_free(&paths);
7264 715dc77e 2019-08-03 stsp free(cwd);
7265 01073a5d 2019-08-22 stsp return error;
7266 01073a5d 2019-08-22 stsp }
7267 01073a5d 2019-08-22 stsp
7268 01073a5d 2019-08-22 stsp __dead static void
7269 01073a5d 2019-08-22 stsp usage_cat(void)
7270 01073a5d 2019-08-22 stsp {
7271 896e9b6f 2019-08-26 stsp fprintf(stderr, "usage: %s cat [-r repository ] [ -c commit ] [ -P ] "
7272 896e9b6f 2019-08-26 stsp "arg1 [arg2 ...]\n", getprogname());
7273 01073a5d 2019-08-22 stsp exit(1);
7274 01073a5d 2019-08-22 stsp }
7275 01073a5d 2019-08-22 stsp
7276 01073a5d 2019-08-22 stsp static const struct got_error *
7277 01073a5d 2019-08-22 stsp cat_blob(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7278 01073a5d 2019-08-22 stsp {
7279 01073a5d 2019-08-22 stsp const struct got_error *err;
7280 01073a5d 2019-08-22 stsp struct got_blob_object *blob;
7281 01073a5d 2019-08-22 stsp
7282 01073a5d 2019-08-22 stsp err = got_object_open_as_blob(&blob, repo, id, 8192);
7283 01073a5d 2019-08-22 stsp if (err)
7284 01073a5d 2019-08-22 stsp return err;
7285 01073a5d 2019-08-22 stsp
7286 01073a5d 2019-08-22 stsp err = got_object_blob_dump_to_file(NULL, NULL, NULL, outfile, blob);
7287 01073a5d 2019-08-22 stsp got_object_blob_close(blob);
7288 01073a5d 2019-08-22 stsp return err;
7289 01073a5d 2019-08-22 stsp }
7290 01073a5d 2019-08-22 stsp
7291 01073a5d 2019-08-22 stsp static const struct got_error *
7292 01073a5d 2019-08-22 stsp cat_tree(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7293 01073a5d 2019-08-22 stsp {
7294 01073a5d 2019-08-22 stsp const struct got_error *err;
7295 01073a5d 2019-08-22 stsp struct got_tree_object *tree;
7296 56e0773d 2019-11-28 stsp int nentries, i;
7297 01073a5d 2019-08-22 stsp
7298 01073a5d 2019-08-22 stsp err = got_object_open_as_tree(&tree, repo, id);
7299 01073a5d 2019-08-22 stsp if (err)
7300 01073a5d 2019-08-22 stsp return err;
7301 01073a5d 2019-08-22 stsp
7302 56e0773d 2019-11-28 stsp nentries = got_object_tree_get_nentries(tree);
7303 56e0773d 2019-11-28 stsp for (i = 0; i < nentries; i++) {
7304 56e0773d 2019-11-28 stsp struct got_tree_entry *te;
7305 01073a5d 2019-08-22 stsp char *id_str;
7306 01073a5d 2019-08-22 stsp if (sigint_received || sigpipe_received)
7307 01073a5d 2019-08-22 stsp break;
7308 56e0773d 2019-11-28 stsp te = got_object_tree_get_entry(tree, i);
7309 56e0773d 2019-11-28 stsp err = got_object_id_str(&id_str, got_tree_entry_get_id(te));
7310 01073a5d 2019-08-22 stsp if (err)
7311 01073a5d 2019-08-22 stsp break;
7312 56e0773d 2019-11-28 stsp fprintf(outfile, "%s %.7o %s\n", id_str,
7313 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(te),
7314 56e0773d 2019-11-28 stsp got_tree_entry_get_name(te));
7315 01073a5d 2019-08-22 stsp free(id_str);
7316 01073a5d 2019-08-22 stsp }
7317 01073a5d 2019-08-22 stsp
7318 01073a5d 2019-08-22 stsp got_object_tree_close(tree);
7319 01073a5d 2019-08-22 stsp return err;
7320 01073a5d 2019-08-22 stsp }
7321 01073a5d 2019-08-22 stsp
7322 01073a5d 2019-08-22 stsp static const struct got_error *
7323 01073a5d 2019-08-22 stsp cat_commit(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7324 01073a5d 2019-08-22 stsp {
7325 01073a5d 2019-08-22 stsp const struct got_error *err;
7326 01073a5d 2019-08-22 stsp struct got_commit_object *commit;
7327 01073a5d 2019-08-22 stsp const struct got_object_id_queue *parent_ids;
7328 01073a5d 2019-08-22 stsp struct got_object_qid *pid;
7329 01073a5d 2019-08-22 stsp char *id_str = NULL;
7330 24ea5512 2019-08-22 stsp const char *logmsg = NULL;
7331 01073a5d 2019-08-22 stsp
7332 01073a5d 2019-08-22 stsp err = got_object_open_as_commit(&commit, repo, id);
7333 01073a5d 2019-08-22 stsp if (err)
7334 01073a5d 2019-08-22 stsp return err;
7335 01073a5d 2019-08-22 stsp
7336 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_commit_get_tree_id(commit));
7337 01073a5d 2019-08-22 stsp if (err)
7338 01073a5d 2019-08-22 stsp goto done;
7339 01073a5d 2019-08-22 stsp
7340 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_TREE, id_str);
7341 01073a5d 2019-08-22 stsp parent_ids = got_object_commit_get_parent_ids(commit);
7342 8aa93786 2019-08-22 stsp fprintf(outfile, "numparents %d\n",
7343 01073a5d 2019-08-22 stsp got_object_commit_get_nparents(commit));
7344 01073a5d 2019-08-22 stsp SIMPLEQ_FOREACH(pid, parent_ids, entry) {
7345 01073a5d 2019-08-22 stsp char *pid_str;
7346 01073a5d 2019-08-22 stsp err = got_object_id_str(&pid_str, pid->id);
7347 01073a5d 2019-08-22 stsp if (err)
7348 01073a5d 2019-08-22 stsp goto done;
7349 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_COMMIT_LABEL_PARENT, pid_str);
7350 01073a5d 2019-08-22 stsp free(pid_str);
7351 01073a5d 2019-08-22 stsp }
7352 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_AUTHOR,
7353 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7354 01073a5d 2019-08-22 stsp got_object_commit_get_author_time(commit));
7355 01073a5d 2019-08-22 stsp
7356 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_COMMIT_LABEL_COMMITTER,
7357 8aa93786 2019-08-22 stsp got_object_commit_get_author(commit),
7358 01073a5d 2019-08-22 stsp got_object_commit_get_committer_time(commit));
7359 01073a5d 2019-08-22 stsp
7360 24ea5512 2019-08-22 stsp logmsg = got_object_commit_get_logmsg_raw(commit);
7361 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(logmsg));
7362 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", logmsg);
7363 01073a5d 2019-08-22 stsp done:
7364 01073a5d 2019-08-22 stsp free(id_str);
7365 01073a5d 2019-08-22 stsp got_object_commit_close(commit);
7366 01073a5d 2019-08-22 stsp return err;
7367 01073a5d 2019-08-22 stsp }
7368 01073a5d 2019-08-22 stsp
7369 01073a5d 2019-08-22 stsp static const struct got_error *
7370 01073a5d 2019-08-22 stsp cat_tag(struct got_object_id *id, struct got_repository *repo, FILE *outfile)
7371 01073a5d 2019-08-22 stsp {
7372 01073a5d 2019-08-22 stsp const struct got_error *err;
7373 01073a5d 2019-08-22 stsp struct got_tag_object *tag;
7374 01073a5d 2019-08-22 stsp char *id_str = NULL;
7375 01073a5d 2019-08-22 stsp const char *tagmsg = NULL;
7376 01073a5d 2019-08-22 stsp
7377 01073a5d 2019-08-22 stsp err = got_object_open_as_tag(&tag, repo, id);
7378 01073a5d 2019-08-22 stsp if (err)
7379 01073a5d 2019-08-22 stsp return err;
7380 01073a5d 2019-08-22 stsp
7381 01073a5d 2019-08-22 stsp err = got_object_id_str(&id_str, got_object_tag_get_object_id(tag));
7382 01073a5d 2019-08-22 stsp if (err)
7383 01073a5d 2019-08-22 stsp goto done;
7384 01073a5d 2019-08-22 stsp
7385 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_OBJECT, id_str);
7386 e15d5241 2019-08-22 stsp
7387 01073a5d 2019-08-22 stsp switch (got_object_tag_get_object_type(tag)) {
7388 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7389 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7390 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_BLOB);
7391 01073a5d 2019-08-22 stsp break;
7392 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7393 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7394 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TREE);
7395 01073a5d 2019-08-22 stsp break;
7396 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7397 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7398 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_COMMIT);
7399 01073a5d 2019-08-22 stsp break;
7400 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7401 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TYPE,
7402 8aa93786 2019-08-22 stsp GOT_OBJ_LABEL_TAG);
7403 01073a5d 2019-08-22 stsp break;
7404 01073a5d 2019-08-22 stsp default:
7405 01073a5d 2019-08-22 stsp break;
7406 01073a5d 2019-08-22 stsp }
7407 01073a5d 2019-08-22 stsp
7408 e15d5241 2019-08-22 stsp fprintf(outfile, "%s%s\n", GOT_TAG_LABEL_TAG,
7409 e15d5241 2019-08-22 stsp got_object_tag_get_name(tag));
7410 e15d5241 2019-08-22 stsp
7411 8aa93786 2019-08-22 stsp fprintf(outfile, "%s%s %lld +0000\n", GOT_TAG_LABEL_TAGGER,
7412 8aa93786 2019-08-22 stsp got_object_tag_get_tagger(tag),
7413 8aa93786 2019-08-22 stsp got_object_tag_get_tagger_time(tag));
7414 01073a5d 2019-08-22 stsp
7415 01073a5d 2019-08-22 stsp tagmsg = got_object_tag_get_message(tag);
7416 8aa93786 2019-08-22 stsp fprintf(outfile, "messagelen %zd\n", strlen(tagmsg));
7417 01073a5d 2019-08-22 stsp fprintf(outfile, "%s", tagmsg);
7418 01073a5d 2019-08-22 stsp done:
7419 01073a5d 2019-08-22 stsp free(id_str);
7420 01073a5d 2019-08-22 stsp got_object_tag_close(tag);
7421 01073a5d 2019-08-22 stsp return err;
7422 01073a5d 2019-08-22 stsp }
7423 01073a5d 2019-08-22 stsp
7424 01073a5d 2019-08-22 stsp static const struct got_error *
7425 01073a5d 2019-08-22 stsp cmd_cat(int argc, char *argv[])
7426 01073a5d 2019-08-22 stsp {
7427 01073a5d 2019-08-22 stsp const struct got_error *error;
7428 01073a5d 2019-08-22 stsp struct got_repository *repo = NULL;
7429 01073a5d 2019-08-22 stsp struct got_worktree *worktree = NULL;
7430 01073a5d 2019-08-22 stsp char *cwd = NULL, *repo_path = NULL, *label = NULL;
7431 896e9b6f 2019-08-26 stsp const char *commit_id_str = NULL;
7432 896e9b6f 2019-08-26 stsp struct got_object_id *id = NULL, *commit_id = NULL;
7433 896e9b6f 2019-08-26 stsp int ch, obj_type, i, force_path = 0;
7434 01073a5d 2019-08-22 stsp
7435 01073a5d 2019-08-22 stsp #ifndef PROFILE
7436 01073a5d 2019-08-22 stsp if (pledge("stdio rpath wpath cpath flock proc exec sendfd unveil",
7437 01073a5d 2019-08-22 stsp NULL) == -1)
7438 01073a5d 2019-08-22 stsp err(1, "pledge");
7439 01073a5d 2019-08-22 stsp #endif
7440 01073a5d 2019-08-22 stsp
7441 896e9b6f 2019-08-26 stsp while ((ch = getopt(argc, argv, "c:r:P")) != -1) {
7442 01073a5d 2019-08-22 stsp switch (ch) {
7443 896e9b6f 2019-08-26 stsp case 'c':
7444 896e9b6f 2019-08-26 stsp commit_id_str = optarg;
7445 896e9b6f 2019-08-26 stsp break;
7446 01073a5d 2019-08-22 stsp case 'r':
7447 01073a5d 2019-08-22 stsp repo_path = realpath(optarg, NULL);
7448 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7449 9ba1d308 2019-10-21 stsp return got_error_from_errno2("realpath",
7450 9ba1d308 2019-10-21 stsp optarg);
7451 01073a5d 2019-08-22 stsp got_path_strip_trailing_slashes(repo_path);
7452 01073a5d 2019-08-22 stsp break;
7453 896e9b6f 2019-08-26 stsp case 'P':
7454 896e9b6f 2019-08-26 stsp force_path = 1;
7455 896e9b6f 2019-08-26 stsp break;
7456 01073a5d 2019-08-22 stsp default:
7457 01073a5d 2019-08-22 stsp usage_cat();
7458 01073a5d 2019-08-22 stsp /* NOTREACHED */
7459 01073a5d 2019-08-22 stsp }
7460 01073a5d 2019-08-22 stsp }
7461 01073a5d 2019-08-22 stsp
7462 01073a5d 2019-08-22 stsp argc -= optind;
7463 01073a5d 2019-08-22 stsp argv += optind;
7464 01073a5d 2019-08-22 stsp
7465 01073a5d 2019-08-22 stsp cwd = getcwd(NULL, 0);
7466 01073a5d 2019-08-22 stsp if (cwd == NULL) {
7467 01073a5d 2019-08-22 stsp error = got_error_from_errno("getcwd");
7468 01073a5d 2019-08-22 stsp goto done;
7469 01073a5d 2019-08-22 stsp }
7470 01073a5d 2019-08-22 stsp error = got_worktree_open(&worktree, cwd);
7471 01073a5d 2019-08-22 stsp if (error && error->code != GOT_ERR_NOT_WORKTREE)
7472 01073a5d 2019-08-22 stsp goto done;
7473 01073a5d 2019-08-22 stsp if (worktree) {
7474 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7475 01073a5d 2019-08-22 stsp repo_path = strdup(
7476 01073a5d 2019-08-22 stsp got_worktree_get_repo_path(worktree));
7477 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7478 01073a5d 2019-08-22 stsp error = got_error_from_errno("strdup");
7479 01073a5d 2019-08-22 stsp goto done;
7480 01073a5d 2019-08-22 stsp }
7481 01073a5d 2019-08-22 stsp }
7482 01073a5d 2019-08-22 stsp }
7483 01073a5d 2019-08-22 stsp
7484 01073a5d 2019-08-22 stsp if (repo_path == NULL) {
7485 01073a5d 2019-08-22 stsp repo_path = getcwd(NULL, 0);
7486 01073a5d 2019-08-22 stsp if (repo_path == NULL)
7487 01073a5d 2019-08-22 stsp return got_error_from_errno("getcwd");
7488 01073a5d 2019-08-22 stsp }
7489 01073a5d 2019-08-22 stsp
7490 c9956ddf 2019-09-08 stsp error = got_repo_open(&repo, repo_path, NULL);
7491 01073a5d 2019-08-22 stsp free(repo_path);
7492 01073a5d 2019-08-22 stsp if (error != NULL)
7493 01073a5d 2019-08-22 stsp goto done;
7494 01073a5d 2019-08-22 stsp
7495 01073a5d 2019-08-22 stsp error = apply_unveil(got_repo_get_path(repo), 1, NULL);
7496 896e9b6f 2019-08-26 stsp if (error)
7497 896e9b6f 2019-08-26 stsp goto done;
7498 896e9b6f 2019-08-26 stsp
7499 896e9b6f 2019-08-26 stsp if (commit_id_str == NULL)
7500 896e9b6f 2019-08-26 stsp commit_id_str = GOT_REF_HEAD;
7501 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&commit_id, NULL,
7502 71a27632 2020-01-15 stsp commit_id_str, GOT_OBJ_TYPE_COMMIT, 1, repo);
7503 01073a5d 2019-08-22 stsp if (error)
7504 01073a5d 2019-08-22 stsp goto done;
7505 01073a5d 2019-08-22 stsp
7506 01073a5d 2019-08-22 stsp for (i = 0; i < argc; i++) {
7507 896e9b6f 2019-08-26 stsp if (force_path) {
7508 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo, commit_id,
7509 896e9b6f 2019-08-26 stsp argv[i]);
7510 896e9b6f 2019-08-26 stsp if (error)
7511 896e9b6f 2019-08-26 stsp break;
7512 896e9b6f 2019-08-26 stsp } else {
7513 71a27632 2020-01-15 stsp error = got_repo_match_object_id(&id, &label, argv[i],
7514 896e9b6f 2019-08-26 stsp GOT_OBJ_TYPE_ANY, 0, repo);
7515 896e9b6f 2019-08-26 stsp if (error) {
7516 896e9b6f 2019-08-26 stsp if (error->code != GOT_ERR_BAD_OBJ_ID_STR &&
7517 896e9b6f 2019-08-26 stsp error->code != GOT_ERR_NOT_REF)
7518 896e9b6f 2019-08-26 stsp break;
7519 896e9b6f 2019-08-26 stsp error = got_object_id_by_path(&id, repo,
7520 896e9b6f 2019-08-26 stsp commit_id, argv[i]);
7521 896e9b6f 2019-08-26 stsp if (error)
7522 896e9b6f 2019-08-26 stsp break;
7523 896e9b6f 2019-08-26 stsp }
7524 896e9b6f 2019-08-26 stsp }
7525 01073a5d 2019-08-22 stsp
7526 01073a5d 2019-08-22 stsp error = got_object_get_type(&obj_type, repo, id);
7527 01073a5d 2019-08-22 stsp if (error)
7528 01073a5d 2019-08-22 stsp break;
7529 01073a5d 2019-08-22 stsp
7530 01073a5d 2019-08-22 stsp switch (obj_type) {
7531 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_BLOB:
7532 01073a5d 2019-08-22 stsp error = cat_blob(id, repo, stdout);
7533 01073a5d 2019-08-22 stsp break;
7534 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TREE:
7535 01073a5d 2019-08-22 stsp error = cat_tree(id, repo, stdout);
7536 01073a5d 2019-08-22 stsp break;
7537 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_COMMIT:
7538 01073a5d 2019-08-22 stsp error = cat_commit(id, repo, stdout);
7539 01073a5d 2019-08-22 stsp break;
7540 01073a5d 2019-08-22 stsp case GOT_OBJ_TYPE_TAG:
7541 01073a5d 2019-08-22 stsp error = cat_tag(id, repo, stdout);
7542 01073a5d 2019-08-22 stsp break;
7543 01073a5d 2019-08-22 stsp default:
7544 01073a5d 2019-08-22 stsp error = got_error(GOT_ERR_OBJ_TYPE);
7545 01073a5d 2019-08-22 stsp break;
7546 01073a5d 2019-08-22 stsp }
7547 01073a5d 2019-08-22 stsp if (error)
7548 01073a5d 2019-08-22 stsp break;
7549 01073a5d 2019-08-22 stsp free(label);
7550 01073a5d 2019-08-22 stsp label = NULL;
7551 01073a5d 2019-08-22 stsp free(id);
7552 01073a5d 2019-08-22 stsp id = NULL;
7553 01073a5d 2019-08-22 stsp }
7554 01073a5d 2019-08-22 stsp done:
7555 01073a5d 2019-08-22 stsp free(label);
7556 01073a5d 2019-08-22 stsp free(id);
7557 896e9b6f 2019-08-26 stsp free(commit_id);
7558 01073a5d 2019-08-22 stsp if (worktree)
7559 01073a5d 2019-08-22 stsp got_worktree_close(worktree);
7560 01073a5d 2019-08-22 stsp if (repo) {
7561 01073a5d 2019-08-22 stsp const struct got_error *repo_error;
7562 01073a5d 2019-08-22 stsp repo_error = got_repo_close(repo);
7563 01073a5d 2019-08-22 stsp if (error == NULL)
7564 01073a5d 2019-08-22 stsp error = repo_error;
7565 01073a5d 2019-08-22 stsp }
7566 0ebf8283 2019-07-24 stsp return error;
7567 0ebf8283 2019-07-24 stsp }