Blame


1 5c860e29 2018-03-12 stsp /*
2 f42b1b34 2018-03-12 stsp * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 f42b1b34 2018-03-12 stsp * Copyright (c) 2018 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 64a96a6d 2018-04-01 stsp #include <sys/limits.h>
20 f42b1b34 2018-03-12 stsp
21 5c860e29 2018-03-12 stsp #include <err.h>
22 5c860e29 2018-03-12 stsp #include <errno.h>
23 5c860e29 2018-03-12 stsp #include <locale.h>
24 5c860e29 2018-03-12 stsp #include <stdio.h>
25 5c860e29 2018-03-12 stsp #include <stdlib.h>
26 5c860e29 2018-03-12 stsp #include <string.h>
27 5c860e29 2018-03-12 stsp #include <unistd.h>
28 c09a553d 2018-03-12 stsp #include <libgen.h>
29 5c860e29 2018-03-12 stsp
30 f42b1b34 2018-03-12 stsp #include "got_error.h"
31 f42b1b34 2018-03-12 stsp #include "got_object.h"
32 5261c201 2018-04-01 stsp #include "got_reference.h"
33 f42b1b34 2018-03-12 stsp #include "got_repository.h"
34 c09a553d 2018-03-12 stsp #include "got_worktree.h"
35 79109fed 2018-03-27 stsp #include "got_diff.h"
36 372ccdbb 2018-06-10 stsp #include "got_commit_graph.h"
37 5c860e29 2018-03-12 stsp
38 5c860e29 2018-03-12 stsp #ifndef nitems
39 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
40 5c860e29 2018-03-12 stsp #endif
41 5c860e29 2018-03-12 stsp
42 5c860e29 2018-03-12 stsp struct cmd {
43 5c860e29 2018-03-12 stsp const char *cmd_name;
44 d7d4f210 2018-03-12 stsp const struct got_error *(*cmd_main)(int, char *[]);
45 1b6b95a8 2018-03-12 stsp void (*cmd_usage)(void);
46 46a0db7d 2018-03-12 stsp const char *cmd_descr;
47 5c860e29 2018-03-12 stsp };
48 5c860e29 2018-03-12 stsp
49 4ed7e80c 2018-05-20 stsp __dead static void usage(void);
50 4ed7e80c 2018-05-20 stsp __dead static void usage_checkout(void);
51 4ed7e80c 2018-05-20 stsp __dead static void usage_log(void);
52 4ed7e80c 2018-05-20 stsp __dead static void usage_diff(void);
53 5c860e29 2018-03-12 stsp
54 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_checkout(int, char *[]);
55 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_log(int, char *[]);
56 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_diff(int, char *[]);
57 4ed7e80c 2018-05-20 stsp #ifdef notyet
58 4ed7e80c 2018-05-20 stsp static const struct got_error* cmd_status(int, char *[]);
59 4ed7e80c 2018-05-20 stsp #endif
60 5c860e29 2018-03-12 stsp
61 4ed7e80c 2018-05-20 stsp static struct cmd got_commands[] = {
62 c09a553d 2018-03-12 stsp { "checkout", cmd_checkout, usage_checkout,
63 0bb8a95e 2018-03-12 stsp "check out a new work tree from a repository" },
64 1b6b95a8 2018-03-12 stsp { "log", cmd_log, usage_log,
65 1b6b95a8 2018-03-12 stsp "show repository history" },
66 b00d56cd 2018-04-01 stsp { "diff", cmd_diff, usage_diff,
67 b00d56cd 2018-04-01 stsp "compare files and directories" },
68 f42b1b34 2018-03-12 stsp #ifdef notyet
69 1b6b95a8 2018-03-12 stsp { "status", cmd_status, usage_status,
70 1b6b95a8 2018-03-12 stsp "show modification status of files" },
71 f42b1b34 2018-03-12 stsp #endif
72 5c860e29 2018-03-12 stsp };
73 5c860e29 2018-03-12 stsp
74 5c860e29 2018-03-12 stsp int
75 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
76 5c860e29 2018-03-12 stsp {
77 5c860e29 2018-03-12 stsp struct cmd *cmd;
78 5c860e29 2018-03-12 stsp unsigned int i;
79 5c860e29 2018-03-12 stsp int ch;
80 1b6b95a8 2018-03-12 stsp int hflag = 0;
81 5c860e29 2018-03-12 stsp
82 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
83 5c860e29 2018-03-12 stsp
84 1b6b95a8 2018-03-12 stsp while ((ch = getopt(argc, argv, "h")) != -1) {
85 5c860e29 2018-03-12 stsp switch (ch) {
86 1b6b95a8 2018-03-12 stsp case 'h':
87 1b6b95a8 2018-03-12 stsp hflag = 1;
88 1b6b95a8 2018-03-12 stsp break;
89 5c860e29 2018-03-12 stsp default:
90 5c860e29 2018-03-12 stsp usage();
91 5c860e29 2018-03-12 stsp /* NOTREACHED */
92 5c860e29 2018-03-12 stsp }
93 5c860e29 2018-03-12 stsp }
94 5c860e29 2018-03-12 stsp
95 5c860e29 2018-03-12 stsp argc -= optind;
96 5c860e29 2018-03-12 stsp argv += optind;
97 1e70621d 2018-03-27 stsp optind = 0;
98 5c860e29 2018-03-12 stsp
99 5c860e29 2018-03-12 stsp if (argc <= 0)
100 5c860e29 2018-03-12 stsp usage();
101 5c860e29 2018-03-12 stsp
102 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
103 d7d4f210 2018-03-12 stsp const struct got_error *error;
104 d7d4f210 2018-03-12 stsp
105 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
106 5c860e29 2018-03-12 stsp
107 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
108 5c860e29 2018-03-12 stsp continue;
109 5c860e29 2018-03-12 stsp
110 1b6b95a8 2018-03-12 stsp if (hflag)
111 1b6b95a8 2018-03-12 stsp got_commands[i].cmd_usage();
112 1b6b95a8 2018-03-12 stsp
113 d7d4f210 2018-03-12 stsp error = got_commands[i].cmd_main(argc, argv);
114 d7d4f210 2018-03-12 stsp if (error) {
115 d7d4f210 2018-03-12 stsp fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
116 d7d4f210 2018-03-12 stsp return 1;
117 d7d4f210 2018-03-12 stsp }
118 d7d4f210 2018-03-12 stsp
119 d7d4f210 2018-03-12 stsp return 0;
120 5c860e29 2018-03-12 stsp }
121 5c860e29 2018-03-12 stsp
122 20ecf764 2018-03-12 stsp fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
123 5c860e29 2018-03-12 stsp return 1;
124 5c860e29 2018-03-12 stsp }
125 5c860e29 2018-03-12 stsp
126 4ed7e80c 2018-05-20 stsp __dead static void
127 5c860e29 2018-03-12 stsp usage(void)
128 5c860e29 2018-03-12 stsp {
129 46a0db7d 2018-03-12 stsp int i;
130 46a0db7d 2018-03-12 stsp
131 987e94ba 2018-03-12 stsp fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
132 987e94ba 2018-03-12 stsp "Available commands:\n", getprogname());
133 46a0db7d 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
134 46a0db7d 2018-03-12 stsp struct cmd *cmd = &got_commands[i];
135 46a0db7d 2018-03-12 stsp fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
136 46a0db7d 2018-03-12 stsp }
137 5c860e29 2018-03-12 stsp exit(1);
138 5c860e29 2018-03-12 stsp }
139 5c860e29 2018-03-12 stsp
140 4ed7e80c 2018-05-20 stsp __dead static void
141 c09a553d 2018-03-12 stsp usage_checkout(void)
142 c09a553d 2018-03-12 stsp {
143 0bb8a95e 2018-03-12 stsp fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
144 0bb8a95e 2018-03-12 stsp "[worktree-path]\n", getprogname());
145 c09a553d 2018-03-12 stsp exit(1);
146 92a684f4 2018-03-12 stsp }
147 92a684f4 2018-03-12 stsp
148 92a684f4 2018-03-12 stsp static void
149 92a684f4 2018-03-12 stsp checkout_progress(void *arg, const char *path)
150 92a684f4 2018-03-12 stsp {
151 92a684f4 2018-03-12 stsp char *worktree_path = arg;
152 92a684f4 2018-03-12 stsp
153 92a684f4 2018-03-12 stsp while (path[0] == '/')
154 92a684f4 2018-03-12 stsp path++;
155 92a684f4 2018-03-12 stsp
156 92a684f4 2018-03-12 stsp printf("A %s/%s\n", worktree_path, path);
157 c09a553d 2018-03-12 stsp }
158 c09a553d 2018-03-12 stsp
159 4ed7e80c 2018-05-20 stsp static const struct got_error *
160 c09a553d 2018-03-12 stsp cmd_checkout(int argc, char *argv[])
161 c09a553d 2018-03-12 stsp {
162 c09a553d 2018-03-12 stsp const struct got_error *error = NULL;
163 c09a553d 2018-03-12 stsp struct got_repository *repo = NULL;
164 c09a553d 2018-03-12 stsp struct got_reference *head_ref = NULL;
165 c09a553d 2018-03-12 stsp struct got_worktree *worktree = NULL;
166 c09a553d 2018-03-12 stsp char *repo_path = NULL;
167 c09a553d 2018-03-12 stsp char *worktree_path = NULL;
168 0bb8a95e 2018-03-12 stsp const char *path_prefix = "";
169 0bb8a95e 2018-03-12 stsp int ch;
170 c09a553d 2018-03-12 stsp
171 0bb8a95e 2018-03-12 stsp while ((ch = getopt(argc, argv, "p:")) != -1) {
172 0bb8a95e 2018-03-12 stsp switch (ch) {
173 0bb8a95e 2018-03-12 stsp case 'p':
174 0bb8a95e 2018-03-12 stsp path_prefix = optarg;
175 0bb8a95e 2018-03-12 stsp break;
176 0bb8a95e 2018-03-12 stsp default:
177 0bb8a95e 2018-03-12 stsp usage();
178 0bb8a95e 2018-03-12 stsp /* NOTREACHED */
179 0bb8a95e 2018-03-12 stsp }
180 0bb8a95e 2018-03-12 stsp }
181 0bb8a95e 2018-03-12 stsp
182 0bb8a95e 2018-03-12 stsp argc -= optind;
183 0bb8a95e 2018-03-12 stsp argv += optind;
184 0bb8a95e 2018-03-12 stsp
185 6715a751 2018-03-16 stsp #ifndef PROFILE
186 2178c42e 2018-04-22 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
187 c09a553d 2018-03-12 stsp err(1, "pledge");
188 6715a751 2018-03-16 stsp #endif
189 0bb8a95e 2018-03-12 stsp if (argc == 1) {
190 c09a553d 2018-03-12 stsp char *cwd, *base, *dotgit;
191 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
192 76089277 2018-04-01 stsp if (repo_path == NULL)
193 76089277 2018-04-01 stsp return got_error_from_errno();
194 c09a553d 2018-03-12 stsp cwd = getcwd(NULL, 0);
195 76089277 2018-04-01 stsp if (cwd == NULL) {
196 76089277 2018-04-01 stsp error = got_error_from_errno();
197 76089277 2018-04-01 stsp goto done;
198 76089277 2018-04-01 stsp }
199 5d7c1dab 2018-04-01 stsp if (path_prefix[0])
200 5d7c1dab 2018-04-01 stsp base = basename(path_prefix);
201 5d7c1dab 2018-04-01 stsp else
202 5d7c1dab 2018-04-01 stsp base = basename(repo_path);
203 76089277 2018-04-01 stsp if (base == NULL) {
204 76089277 2018-04-01 stsp error = got_error_from_errno();
205 76089277 2018-04-01 stsp goto done;
206 76089277 2018-04-01 stsp }
207 c09a553d 2018-03-12 stsp dotgit = strstr(base, ".git");
208 c09a553d 2018-03-12 stsp if (dotgit)
209 c09a553d 2018-03-12 stsp *dotgit = '\0';
210 c09a553d 2018-03-12 stsp if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
211 0a585a0d 2018-03-17 stsp error = got_error_from_errno();
212 c09a553d 2018-03-12 stsp free(cwd);
213 76089277 2018-04-01 stsp goto done;
214 c09a553d 2018-03-12 stsp }
215 c09a553d 2018-03-12 stsp free(cwd);
216 0bb8a95e 2018-03-12 stsp } else if (argc == 2) {
217 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
218 76089277 2018-04-01 stsp if (repo_path == NULL) {
219 76089277 2018-04-01 stsp error = got_error_from_errno();
220 76089277 2018-04-01 stsp goto done;
221 76089277 2018-04-01 stsp }
222 f7b38925 2018-04-01 stsp worktree_path = realpath(argv[1], NULL);
223 76089277 2018-04-01 stsp if (worktree_path == NULL) {
224 76089277 2018-04-01 stsp error = got_error_from_errno();
225 76089277 2018-04-01 stsp goto done;
226 76089277 2018-04-01 stsp }
227 c09a553d 2018-03-12 stsp } else
228 c09a553d 2018-03-12 stsp usage_checkout();
229 c09a553d 2018-03-12 stsp
230 c09a553d 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
231 c09a553d 2018-03-12 stsp if (error != NULL)
232 c09a553d 2018-03-12 stsp goto done;
233 c09a553d 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
234 c09a553d 2018-03-12 stsp if (error != NULL)
235 c09a553d 2018-03-12 stsp goto done;
236 c09a553d 2018-03-12 stsp
237 0bb8a95e 2018-03-12 stsp error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
238 c09a553d 2018-03-12 stsp if (error != NULL)
239 c09a553d 2018-03-12 stsp goto done;
240 c09a553d 2018-03-12 stsp
241 c09a553d 2018-03-12 stsp error = got_worktree_open(&worktree, worktree_path);
242 c09a553d 2018-03-12 stsp if (error != NULL)
243 c09a553d 2018-03-12 stsp goto done;
244 c09a553d 2018-03-12 stsp
245 92a684f4 2018-03-12 stsp error = got_worktree_checkout_files(worktree, head_ref, repo,
246 92a684f4 2018-03-12 stsp checkout_progress, worktree_path);
247 c09a553d 2018-03-12 stsp if (error != NULL)
248 c09a553d 2018-03-12 stsp goto done;
249 c09a553d 2018-03-12 stsp
250 b65ae19a 2018-04-24 stsp printf("Checked out %s\n", worktree_path);
251 b65ae19a 2018-04-24 stsp printf("Now shut up and hack\n");
252 c09a553d 2018-03-12 stsp
253 c09a553d 2018-03-12 stsp done:
254 76089277 2018-04-01 stsp free(repo_path);
255 c09a553d 2018-03-12 stsp free(worktree_path);
256 c09a553d 2018-03-12 stsp return error;
257 c09a553d 2018-03-12 stsp }
258 c09a553d 2018-03-12 stsp
259 f42b1b34 2018-03-12 stsp static const struct got_error *
260 79109fed 2018-03-27 stsp print_patch(struct got_commit_object *commit, struct got_object_id *id,
261 f42b1b34 2018-03-12 stsp struct got_repository *repo)
262 5c860e29 2018-03-12 stsp {
263 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
264 79109fed 2018-03-27 stsp struct got_tree_object *tree1 = NULL, *tree2;
265 79109fed 2018-03-27 stsp struct got_object *obj;
266 79109fed 2018-03-27 stsp struct got_parent_id *pid;
267 79109fed 2018-03-27 stsp
268 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, commit->tree_id);
269 79109fed 2018-03-27 stsp if (err)
270 79109fed 2018-03-27 stsp return err;
271 79109fed 2018-03-27 stsp
272 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree2, repo, obj);
273 79109fed 2018-03-27 stsp got_object_close(obj);
274 79109fed 2018-03-27 stsp if (err)
275 79109fed 2018-03-27 stsp return err;
276 79109fed 2018-03-27 stsp
277 79109fed 2018-03-27 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
278 79109fed 2018-03-27 stsp if (pid != NULL) {
279 79109fed 2018-03-27 stsp struct got_commit_object *pcommit;
280 79109fed 2018-03-27 stsp
281 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pid->id);
282 79109fed 2018-03-27 stsp if (err)
283 79109fed 2018-03-27 stsp return err;
284 79109fed 2018-03-27 stsp
285 79109fed 2018-03-27 stsp err = got_object_commit_open(&pcommit, repo, obj);
286 79109fed 2018-03-27 stsp got_object_close(obj);
287 79109fed 2018-03-27 stsp if (err)
288 79109fed 2018-03-27 stsp return err;
289 79109fed 2018-03-27 stsp
290 79109fed 2018-03-27 stsp err = got_object_open(&obj, repo, pcommit->tree_id);
291 79109fed 2018-03-27 stsp got_object_commit_close(pcommit);
292 79109fed 2018-03-27 stsp if (err)
293 79109fed 2018-03-27 stsp return err;
294 79109fed 2018-03-27 stsp err = got_object_tree_open(&tree1, repo, obj);
295 79109fed 2018-03-27 stsp got_object_close(obj);
296 79109fed 2018-03-27 stsp if (err)
297 79109fed 2018-03-27 stsp return err;
298 79109fed 2018-03-27 stsp }
299 79109fed 2018-03-27 stsp
300 79109fed 2018-03-27 stsp err = got_diff_tree(tree1, tree2, repo, stdout);
301 79109fed 2018-03-27 stsp if (tree1)
302 79109fed 2018-03-27 stsp got_object_tree_close(tree1);
303 79109fed 2018-03-27 stsp got_object_tree_close(tree2);
304 79109fed 2018-03-27 stsp return err;
305 79109fed 2018-03-27 stsp }
306 79109fed 2018-03-27 stsp
307 79109fed 2018-03-27 stsp static const struct got_error *
308 79109fed 2018-03-27 stsp print_commit(struct got_commit_object *commit, struct got_object_id *id,
309 a0603db2 2018-06-10 stsp struct got_repository *repo, int show_patch, int verbose)
310 79109fed 2018-03-27 stsp {
311 79109fed 2018-03-27 stsp const struct got_error *err = NULL;
312 832c249c 2018-06-10 stsp char *id_str, *logmsg, *line;
313 5c860e29 2018-03-12 stsp
314 832c249c 2018-06-10 stsp err = got_object_id_str(&id_str, id);
315 8bf5b3c9 2018-03-17 stsp if (err)
316 8bf5b3c9 2018-03-17 stsp return err;
317 5c860e29 2018-03-12 stsp
318 8bf5b3c9 2018-03-17 stsp printf("-----------------------------------------------\n");
319 832c249c 2018-06-10 stsp printf("commit %s\n", id_str);
320 832c249c 2018-06-10 stsp free(id_str);
321 b65ae19a 2018-04-24 stsp printf("author: %s\n", commit->author);
322 f3d135e1 2018-04-01 stsp if (strcmp(commit->author, commit->committer) != 0)
323 b65ae19a 2018-04-24 stsp printf("committer: %s\n", commit->committer);
324 a0603db2 2018-06-10 stsp if (verbose) {
325 a0603db2 2018-06-10 stsp struct got_parent_id *pid;
326 a0603db2 2018-06-10 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
327 a0603db2 2018-06-10 stsp err = got_object_id_str(&id_str, pid->id);
328 a0603db2 2018-06-10 stsp if (err)
329 a0603db2 2018-06-10 stsp return err;
330 a0603db2 2018-06-10 stsp printf("parent commit: %s\n", id_str);
331 a0603db2 2018-06-10 stsp free(id_str);
332 a0603db2 2018-06-10 stsp }
333 a0603db2 2018-06-10 stsp }
334 832c249c 2018-06-10 stsp
335 832c249c 2018-06-10 stsp logmsg = strdup(commit->logmsg);
336 832c249c 2018-06-10 stsp if (logmsg == NULL)
337 832c249c 2018-06-10 stsp return got_error_from_errno();
338 8bf5b3c9 2018-03-17 stsp
339 832c249c 2018-06-10 stsp do {
340 832c249c 2018-06-10 stsp line = strsep(&logmsg, "\n");
341 832c249c 2018-06-10 stsp if (line)
342 832c249c 2018-06-10 stsp printf(" %s\n", line);
343 832c249c 2018-06-10 stsp } while (line);
344 832c249c 2018-06-10 stsp free(logmsg);
345 832c249c 2018-06-10 stsp
346 971751ac 2018-03-27 stsp if (show_patch) {
347 79109fed 2018-03-27 stsp err = print_patch(commit, id, repo);
348 971751ac 2018-03-27 stsp if (err == 0)
349 971751ac 2018-03-27 stsp printf("\n");
350 971751ac 2018-03-27 stsp }
351 79109fed 2018-03-27 stsp
352 79109fed 2018-03-27 stsp return err;
353 f42b1b34 2018-03-12 stsp }
354 5c860e29 2018-03-12 stsp
355 f42b1b34 2018-03-12 stsp static const struct got_error *
356 8bf5b3c9 2018-03-17 stsp print_commits(struct got_object *root_obj, struct got_object_id *root_id,
357 a0603db2 2018-06-10 stsp struct got_repository *repo, int show_patch, int limit, int verbose)
358 f42b1b34 2018-03-12 stsp {
359 f42b1b34 2018-03-12 stsp const struct got_error *err;
360 372ccdbb 2018-06-10 stsp struct got_commit_graph *graph;
361 372ccdbb 2018-06-10 stsp int ncommits;
362 372ccdbb 2018-06-10 stsp
363 372ccdbb 2018-06-10 stsp err = got_commit_graph_open(&graph, root_id, repo);
364 f42b1b34 2018-03-12 stsp if (err)
365 f42b1b34 2018-03-12 stsp return err;
366 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_start(graph, root_id);
367 372ccdbb 2018-06-10 stsp if (err)
368 0a585a0d 2018-03-17 stsp return err;
369 372ccdbb 2018-06-10 stsp do {
370 372ccdbb 2018-06-10 stsp struct got_commit_object *commit;
371 372ccdbb 2018-06-10 stsp struct got_object_id *id;
372 8bf5b3c9 2018-03-17 stsp
373 372ccdbb 2018-06-10 stsp err = got_commit_graph_iter_next(&commit, &id, graph);
374 372ccdbb 2018-06-10 stsp if (err) {
375 372ccdbb 2018-06-10 stsp if (err->code != GOT_ERR_ITER_NEED_MORE)
376 372ccdbb 2018-06-10 stsp break;
377 372ccdbb 2018-06-10 stsp err = got_commit_graph_fetch_commits(&ncommits,
378 372ccdbb 2018-06-10 stsp graph, 1, repo);
379 372ccdbb 2018-06-10 stsp if (err)
380 372ccdbb 2018-06-10 stsp break;
381 372ccdbb 2018-06-10 stsp else
382 372ccdbb 2018-06-10 stsp continue;
383 7e665116 2018-04-02 stsp }
384 372ccdbb 2018-06-10 stsp if (commit == NULL)
385 7e665116 2018-04-02 stsp break;
386 a0603db2 2018-06-10 stsp err = print_commit(commit, id, repo, show_patch, verbose);
387 372ccdbb 2018-06-10 stsp if (err || (limit && --limit == 0))
388 7e665116 2018-04-02 stsp break;
389 372ccdbb 2018-06-10 stsp } while (ncommits > 0);
390 8bf5b3c9 2018-03-17 stsp
391 372ccdbb 2018-06-10 stsp got_commit_graph_close(graph);
392 f42b1b34 2018-03-12 stsp return err;
393 f42b1b34 2018-03-12 stsp }
394 5c860e29 2018-03-12 stsp
395 4ed7e80c 2018-05-20 stsp __dead static void
396 6f3d1eb0 2018-03-12 stsp usage_log(void)
397 6f3d1eb0 2018-03-12 stsp {
398 64a96a6d 2018-04-01 stsp fprintf(stderr, "usage: %s log [-p] [-c commit] [ -l N ] "
399 64a96a6d 2018-04-01 stsp "[repository-path]\n", getprogname());
400 6f3d1eb0 2018-03-12 stsp exit(1);
401 6f3d1eb0 2018-03-12 stsp }
402 6f3d1eb0 2018-03-12 stsp
403 4ed7e80c 2018-05-20 stsp static const struct got_error *
404 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
405 f42b1b34 2018-03-12 stsp {
406 f42b1b34 2018-03-12 stsp const struct got_error *error;
407 f42b1b34 2018-03-12 stsp struct got_repository *repo;
408 3235492e 2018-04-01 stsp struct got_object_id *id = NULL;
409 f42b1b34 2018-03-12 stsp struct got_object *obj;
410 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
411 d142fc45 2018-04-01 stsp char *start_commit = NULL;
412 79109fed 2018-03-27 stsp int ch;
413 a0603db2 2018-06-10 stsp int show_patch = 0, limit = 0, verbose = 0;
414 64a96a6d 2018-04-01 stsp const char *errstr;
415 5c860e29 2018-03-12 stsp
416 6715a751 2018-03-16 stsp #ifndef PROFILE
417 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
418 f42b1b34 2018-03-12 stsp err(1, "pledge");
419 6715a751 2018-03-16 stsp #endif
420 79109fed 2018-03-27 stsp
421 a0603db2 2018-06-10 stsp while ((ch = getopt(argc, argv, "pc:l:v")) != -1) {
422 79109fed 2018-03-27 stsp switch (ch) {
423 79109fed 2018-03-27 stsp case 'p':
424 79109fed 2018-03-27 stsp show_patch = 1;
425 d142fc45 2018-04-01 stsp break;
426 d142fc45 2018-04-01 stsp case 'c':
427 d142fc45 2018-04-01 stsp start_commit = optarg;
428 64a96a6d 2018-04-01 stsp break;
429 64a96a6d 2018-04-01 stsp case 'l':
430 64a96a6d 2018-04-01 stsp limit = strtonum(optarg, 1, INT_MAX, &errstr);
431 64a96a6d 2018-04-01 stsp if (errstr != NULL)
432 64a96a6d 2018-04-01 stsp err(1, "-l option %s", errstr);
433 79109fed 2018-03-27 stsp break;
434 a0603db2 2018-06-10 stsp case 'v':
435 a0603db2 2018-06-10 stsp verbose = 1;
436 a0603db2 2018-06-10 stsp break;
437 79109fed 2018-03-27 stsp default:
438 79109fed 2018-03-27 stsp usage();
439 79109fed 2018-03-27 stsp /* NOTREACHED */
440 79109fed 2018-03-27 stsp }
441 79109fed 2018-03-27 stsp }
442 79109fed 2018-03-27 stsp
443 79109fed 2018-03-27 stsp argc -= optind;
444 79109fed 2018-03-27 stsp argv += optind;
445 79109fed 2018-03-27 stsp
446 79109fed 2018-03-27 stsp if (argc == 0) {
447 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
448 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
449 e1e3f570 2018-04-01 stsp return got_error_from_errno();
450 3235492e 2018-04-01 stsp } else if (argc == 1) {
451 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
452 76089277 2018-04-01 stsp if (repo_path == NULL)
453 76089277 2018-04-01 stsp return got_error_from_errno();
454 3235492e 2018-04-01 stsp } else
455 6f3d1eb0 2018-03-12 stsp usage_log();
456 f42b1b34 2018-03-12 stsp
457 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
458 76089277 2018-04-01 stsp free(repo_path);
459 d7d4f210 2018-03-12 stsp if (error != NULL)
460 d7d4f210 2018-03-12 stsp return error;
461 f42b1b34 2018-03-12 stsp
462 d142fc45 2018-04-01 stsp if (start_commit == NULL) {
463 3235492e 2018-04-01 stsp struct got_reference *head_ref;
464 3235492e 2018-04-01 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
465 3235492e 2018-04-01 stsp if (error != NULL)
466 3235492e 2018-04-01 stsp return error;
467 3235492e 2018-04-01 stsp error = got_ref_resolve(&id, repo, head_ref);
468 3235492e 2018-04-01 stsp got_ref_close(head_ref);
469 3235492e 2018-04-01 stsp if (error != NULL)
470 3235492e 2018-04-01 stsp return error;
471 3235492e 2018-04-01 stsp error = got_object_open(&obj, repo, id);
472 3235492e 2018-04-01 stsp } else {
473 d142fc45 2018-04-01 stsp error = got_object_open_by_id_str(&obj, repo, start_commit);
474 3235492e 2018-04-01 stsp if (error == NULL) {
475 3235492e 2018-04-01 stsp id = got_object_get_id(obj);
476 3235492e 2018-04-01 stsp if (id == NULL)
477 3235492e 2018-04-01 stsp error = got_error_from_errno();
478 3235492e 2018-04-01 stsp }
479 3235492e 2018-04-01 stsp }
480 d7d4f210 2018-03-12 stsp if (error != NULL)
481 d7d4f210 2018-03-12 stsp return error;
482 8bf5b3c9 2018-03-17 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
483 a0603db2 2018-06-10 stsp error = print_commits(obj, id, repo, show_patch, limit,
484 a0603db2 2018-06-10 stsp verbose);
485 8bf5b3c9 2018-03-17 stsp else
486 8bf5b3c9 2018-03-17 stsp error = got_error(GOT_ERR_OBJ_TYPE);
487 f42b1b34 2018-03-12 stsp got_object_close(obj);
488 f42b1b34 2018-03-12 stsp free(id);
489 f42b1b34 2018-03-12 stsp got_repo_close(repo);
490 8bf5b3c9 2018-03-17 stsp return error;
491 5c860e29 2018-03-12 stsp }
492 5c860e29 2018-03-12 stsp
493 4ed7e80c 2018-05-20 stsp __dead static void
494 3f8b7d6a 2018-04-01 stsp usage_diff(void)
495 3f8b7d6a 2018-04-01 stsp {
496 3f8b7d6a 2018-04-01 stsp fprintf(stderr, "usage: %s diff [repository-path] object1 object2\n",
497 3f8b7d6a 2018-04-01 stsp getprogname());
498 3f8b7d6a 2018-04-01 stsp exit(1);
499 b00d56cd 2018-04-01 stsp }
500 b00d56cd 2018-04-01 stsp
501 4ed7e80c 2018-05-20 stsp static const struct got_error *
502 b00d56cd 2018-04-01 stsp cmd_diff(int argc, char *argv[])
503 b00d56cd 2018-04-01 stsp {
504 b00d56cd 2018-04-01 stsp const struct got_error *error;
505 b00d56cd 2018-04-01 stsp struct got_repository *repo = NULL;
506 b00d56cd 2018-04-01 stsp struct got_object_id *id1 = NULL, *id2 = NULL;
507 b00d56cd 2018-04-01 stsp struct got_object *obj1 = NULL, *obj2 = NULL;
508 b00d56cd 2018-04-01 stsp char *repo_path = NULL;
509 b00d56cd 2018-04-01 stsp char *obj_id_str1 = NULL, *obj_id_str2 = NULL;
510 b00d56cd 2018-04-01 stsp int ch;
511 b00d56cd 2018-04-01 stsp
512 b00d56cd 2018-04-01 stsp #ifndef PROFILE
513 442a3ddc 2018-04-23 stsp if (pledge("stdio rpath wpath cpath flock proc", NULL) == -1)
514 b00d56cd 2018-04-01 stsp err(1, "pledge");
515 b00d56cd 2018-04-01 stsp #endif
516 b00d56cd 2018-04-01 stsp
517 b00d56cd 2018-04-01 stsp while ((ch = getopt(argc, argv, "")) != -1) {
518 b00d56cd 2018-04-01 stsp switch (ch) {
519 b00d56cd 2018-04-01 stsp default:
520 b00d56cd 2018-04-01 stsp usage();
521 b00d56cd 2018-04-01 stsp /* NOTREACHED */
522 b00d56cd 2018-04-01 stsp }
523 b00d56cd 2018-04-01 stsp }
524 b00d56cd 2018-04-01 stsp
525 b00d56cd 2018-04-01 stsp argc -= optind;
526 b00d56cd 2018-04-01 stsp argv += optind;
527 b00d56cd 2018-04-01 stsp
528 b00d56cd 2018-04-01 stsp if (argc == 0) {
529 b00d56cd 2018-04-01 stsp usage_diff(); /* TODO show local worktree changes */
530 3f8b7d6a 2018-04-01 stsp } else if (argc == 2) {
531 3f8b7d6a 2018-04-01 stsp repo_path = getcwd(NULL, 0);
532 3f8b7d6a 2018-04-01 stsp if (repo_path == NULL)
533 e1e3f570 2018-04-01 stsp return got_error_from_errno();
534 3f8b7d6a 2018-04-01 stsp obj_id_str1 = argv[0];
535 3f8b7d6a 2018-04-01 stsp obj_id_str2 = argv[1];
536 b00d56cd 2018-04-01 stsp } else if (argc == 3) {
537 76089277 2018-04-01 stsp repo_path = realpath(argv[0], NULL);
538 76089277 2018-04-01 stsp if (repo_path == NULL)
539 76089277 2018-04-01 stsp return got_error_from_errno();
540 b00d56cd 2018-04-01 stsp obj_id_str1 = argv[1];
541 b00d56cd 2018-04-01 stsp obj_id_str2 = argv[2];
542 b00d56cd 2018-04-01 stsp } else
543 b00d56cd 2018-04-01 stsp usage_diff();
544 b00d56cd 2018-04-01 stsp
545 b00d56cd 2018-04-01 stsp error = got_repo_open(&repo, repo_path);
546 76089277 2018-04-01 stsp free(repo_path);
547 b00d56cd 2018-04-01 stsp if (error != NULL)
548 b00d56cd 2018-04-01 stsp goto done;
549 b00d56cd 2018-04-01 stsp
550 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj1, repo, obj_id_str1);
551 b00d56cd 2018-04-01 stsp if (error == NULL) {
552 b00d56cd 2018-04-01 stsp id1 = got_object_get_id(obj1);
553 b00d56cd 2018-04-01 stsp if (id1 == NULL)
554 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
555 b00d56cd 2018-04-01 stsp }
556 b00d56cd 2018-04-01 stsp if (error != NULL)
557 b00d56cd 2018-04-01 stsp goto done;
558 b00d56cd 2018-04-01 stsp
559 b00d56cd 2018-04-01 stsp error = got_object_open_by_id_str(&obj2, repo, obj_id_str2);
560 b00d56cd 2018-04-01 stsp if (error == NULL) {
561 b00d56cd 2018-04-01 stsp id2 = got_object_get_id(obj2);
562 b00d56cd 2018-04-01 stsp if (id2 == NULL)
563 b00d56cd 2018-04-01 stsp error = got_error_from_errno();
564 b00d56cd 2018-04-01 stsp }
565 b00d56cd 2018-04-01 stsp if (error != NULL)
566 b00d56cd 2018-04-01 stsp goto done;
567 b00d56cd 2018-04-01 stsp
568 b00d56cd 2018-04-01 stsp if (got_object_get_type(obj1) != got_object_get_type(obj2)) {
569 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
570 b00d56cd 2018-04-01 stsp goto done;
571 b00d56cd 2018-04-01 stsp }
572 b00d56cd 2018-04-01 stsp
573 b00d56cd 2018-04-01 stsp switch (got_object_get_type(obj1)) {
574 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_BLOB:
575 11528a82 2018-05-19 stsp error = got_diff_objects_as_blobs(obj1, obj2, repo, stdout);
576 b00d56cd 2018-04-01 stsp break;
577 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_TREE:
578 11528a82 2018-05-19 stsp error = got_diff_objects_as_trees(obj1, obj2, repo, stdout);
579 b00d56cd 2018-04-01 stsp break;
580 b00d56cd 2018-04-01 stsp case GOT_OBJ_TYPE_COMMIT:
581 11528a82 2018-05-19 stsp error = got_diff_objects_as_commits(obj1, obj2, repo, stdout);
582 b00d56cd 2018-04-01 stsp break;
583 b00d56cd 2018-04-01 stsp default:
584 b00d56cd 2018-04-01 stsp error = got_error(GOT_ERR_OBJ_TYPE);
585 b00d56cd 2018-04-01 stsp }
586 b00d56cd 2018-04-01 stsp
587 b00d56cd 2018-04-01 stsp done:
588 b00d56cd 2018-04-01 stsp if (obj1)
589 b00d56cd 2018-04-01 stsp got_object_close(obj1);
590 b00d56cd 2018-04-01 stsp if (obj2)
591 b00d56cd 2018-04-01 stsp got_object_close(obj2);
592 b00d56cd 2018-04-01 stsp if (id1)
593 b00d56cd 2018-04-01 stsp free(id1);
594 b00d56cd 2018-04-01 stsp if (id2)
595 b00d56cd 2018-04-01 stsp free(id2);
596 b00d56cd 2018-04-01 stsp if (repo)
597 b00d56cd 2018-04-01 stsp got_repo_close(repo);
598 b00d56cd 2018-04-01 stsp return error;
599 b00d56cd 2018-04-01 stsp }
600 b00d56cd 2018-04-01 stsp
601 f42b1b34 2018-03-12 stsp #ifdef notyet
602 4ed7e80c 2018-05-20 stsp static const struct got_error *
603 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
604 5c860e29 2018-03-12 stsp {
605 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
606 5c860e29 2018-03-12 stsp git_status_list *status;
607 5c860e29 2018-03-12 stsp git_status_options statusopts;
608 5c860e29 2018-03-12 stsp size_t i;
609 5c860e29 2018-03-12 stsp
610 5c860e29 2018-03-12 stsp git_libgit2_init();
611 5c860e29 2018-03-12 stsp
612 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
613 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
614 5c860e29 2018-03-12 stsp
615 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
616 5c860e29 2018-03-12 stsp errx(1, "bar repository");
617 5c860e29 2018-03-12 stsp
618 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
619 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
620 5c860e29 2018-03-12 stsp
621 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
622 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
623 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
624 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
625 5c860e29 2018-03-12 stsp
626 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
627 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
628 5c860e29 2018-03-12 stsp
629 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
630 5c860e29 2018-03-12 stsp const git_status_entry *se;
631 5c860e29 2018-03-12 stsp
632 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
633 5c860e29 2018-03-12 stsp switch (se->status) {
634 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
635 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
636 5c860e29 2018-03-12 stsp break;
637 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
638 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
639 5c860e29 2018-03-12 stsp break;
640 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
641 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
642 5c860e29 2018-03-12 stsp break;
643 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
644 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
645 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
646 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
647 5c860e29 2018-03-12 stsp break;
648 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
649 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
650 5c860e29 2018-03-12 stsp break;
651 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
652 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
653 5c860e29 2018-03-12 stsp break;
654 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
655 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
656 5c860e29 2018-03-12 stsp break;
657 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
658 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
659 5c860e29 2018-03-12 stsp break;
660 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
661 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
662 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
663 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
664 5c860e29 2018-03-12 stsp break;
665 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
666 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
667 5c860e29 2018-03-12 stsp break;
668 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
669 5c860e29 2018-03-12 stsp default:
670 5c860e29 2018-03-12 stsp break;
671 5c860e29 2018-03-12 stsp }
672 5c860e29 2018-03-12 stsp }
673 5c860e29 2018-03-12 stsp
674 5c860e29 2018-03-12 stsp git_status_list_free(status);
675 5c860e29 2018-03-12 stsp git_repository_free(repo);
676 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
677 5c860e29 2018-03-12 stsp
678 5c860e29 2018-03-12 stsp return 0;
679 5c860e29 2018-03-12 stsp }
680 f42b1b34 2018-03-12 stsp #endif