Blob


1 /*
2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/queue.h>
20 #include <err.h>
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <libgen.h>
29 #include "got_error.h"
30 #include "got_object.h"
31 #include "got_refs.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
34 #include "got_diff.h"
36 #ifndef nitems
37 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
38 #endif
40 struct cmd {
41 const char *cmd_name;
42 const struct got_error *(*cmd_main)(int, char *[]);
43 void (*cmd_usage)(void);
44 const char *cmd_descr;
45 };
47 __dead void usage(void);
48 __dead void usage_checkout(void);
49 __dead void usage_log(void);
51 const struct got_error* cmd_checkout(int, char *[]);
52 const struct got_error* cmd_log(int, char *[]);
53 const struct got_error* cmd_status(int, char *[]);
55 struct cmd got_commands[] = {
56 { "checkout", cmd_checkout, usage_checkout,
57 "check out a new work tree from a repository" },
58 { "log", cmd_log, usage_log,
59 "show repository history" },
60 #ifdef notyet
61 { "status", cmd_status, usage_status,
62 "show modification status of files" },
63 #endif
64 };
66 int
67 main(int argc, char *argv[])
68 {
69 struct cmd *cmd;
70 unsigned int i;
71 int ch;
72 int hflag = 0;
74 setlocale(LC_ALL, "");
76 while ((ch = getopt(argc, argv, "h")) != -1) {
77 switch (ch) {
78 case 'h':
79 hflag = 1;
80 break;
81 default:
82 usage();
83 /* NOTREACHED */
84 }
85 }
87 argc -= optind;
88 argv += optind;
89 optind = 0;
91 if (argc <= 0)
92 usage();
94 for (i = 0; i < nitems(got_commands); i++) {
95 const struct got_error *error;
97 cmd = &got_commands[i];
99 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
100 continue;
102 if (hflag)
103 got_commands[i].cmd_usage();
105 error = got_commands[i].cmd_main(argc, argv);
106 if (error) {
107 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
108 return 1;
111 return 0;
114 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
115 return 1;
118 __dead void
119 usage(void)
121 int i;
123 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
124 "Available commands:\n", getprogname());
125 for (i = 0; i < nitems(got_commands); i++) {
126 struct cmd *cmd = &got_commands[i];
127 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
129 exit(1);
132 __dead void
133 usage_checkout(void)
135 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
136 "[worktree-path]\n", getprogname());
137 exit(1);
140 static void
141 checkout_progress(void *arg, const char *path)
143 char *worktree_path = arg;
145 while (path[0] == '/')
146 path++;
148 printf("A %s/%s\n", worktree_path, path);
151 const struct got_error *
152 cmd_checkout(int argc, char *argv[])
154 const struct got_error *error = NULL;
155 struct got_repository *repo = NULL;
156 struct got_reference *head_ref = NULL;
157 struct got_worktree *worktree = NULL;
158 char *repo_path = NULL;
159 char *worktree_path = NULL;
160 const char *path_prefix = "";
161 int ch;
163 while ((ch = getopt(argc, argv, "p:")) != -1) {
164 switch (ch) {
165 case 'p':
166 path_prefix = optarg;
167 break;
168 default:
169 usage();
170 /* NOTREACHED */
174 argc -= optind;
175 argv += optind;
177 #ifndef PROFILE
178 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
179 err(1, "pledge");
180 #endif
181 if (argc == 1) {
182 char *cwd, *base, *dotgit;
183 repo_path = argv[0];
184 cwd = getcwd(NULL, 0);
185 if (cwd == NULL)
186 err(1, "getcwd");
187 base = basename(repo_path);
188 if (base == NULL)
189 err(1, "basename");
190 dotgit = strstr(base, ".git");
191 if (dotgit)
192 *dotgit = '\0';
193 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
194 error = got_error_from_errno();
195 free(cwd);
196 return error;
198 free(cwd);
199 } else if (argc == 2) {
200 repo_path = argv[0];
201 worktree_path = strdup(argv[1]);
202 if (worktree_path == NULL)
203 return got_error_from_errno();
204 } else
205 usage_checkout();
207 error = got_repo_open(&repo, repo_path);
208 if (error != NULL)
209 goto done;
210 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
211 if (error != NULL)
212 goto done;
214 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
215 if (error != NULL)
216 goto done;
218 error = got_worktree_open(&worktree, worktree_path);
219 if (error != NULL)
220 goto done;
222 error = got_worktree_checkout_files(worktree, head_ref, repo,
223 checkout_progress, worktree_path);
224 if (error != NULL)
225 goto done;
227 printf("checked out %s\n", worktree_path);
229 done:
230 free(worktree_path);
231 return error;
234 static const struct got_error *
235 print_patch(struct got_commit_object *commit, struct got_object_id *id,
236 struct got_repository *repo)
238 const struct got_error *err = NULL;
239 struct got_tree_object *tree1 = NULL, *tree2;
240 struct got_object *obj;
241 struct got_parent_id *pid;
243 err = got_object_open(&obj, repo, commit->tree_id);
244 if (err)
245 return err;
247 err = got_object_tree_open(&tree2, repo, obj);
248 got_object_close(obj);
249 if (err)
250 return err;
252 pid = SIMPLEQ_FIRST(&commit->parent_ids);
253 if (pid != NULL) {
254 struct got_commit_object *pcommit;
256 err = got_object_open(&obj, repo, pid->id);
257 if (err)
258 return err;
260 err = got_object_commit_open(&pcommit, repo, obj);
261 got_object_close(obj);
262 if (err)
263 return err;
265 err = got_object_open(&obj, repo, pcommit->tree_id);
266 got_object_commit_close(pcommit);
267 if (err)
268 return err;
269 err = got_object_tree_open(&tree1, repo, obj);
270 got_object_close(obj);
271 if (err)
272 return err;
275 err = got_diff_tree(tree1, tree2, repo, stdout);
276 if (tree1)
277 got_object_tree_close(tree1);
278 got_object_tree_close(tree2);
279 return err;
282 static const struct got_error *
283 print_commit(struct got_commit_object *commit, struct got_object_id *id,
284 struct got_repository *repo, int show_patch)
286 const struct got_error *err = NULL;
287 char *buf;
289 err = got_object_id_str(&buf, id);
290 if (err)
291 return err;
293 printf("-----------------------------------------------\n");
294 printf("commit: %s\n", buf);
295 printf("Author: %s\n", commit->author);
296 printf("\n%s\n", commit->logmsg);
298 if (show_patch)
299 err = print_patch(commit, id, repo);
301 free(buf);
302 return err;
305 struct commit_queue_entry {
306 TAILQ_ENTRY(commit_queue_entry) entry;
307 struct got_object_id *id;
308 struct got_commit_object *commit;
309 };
311 static const struct got_error *
312 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
313 struct got_repository *repo, int show_patch)
315 const struct got_error *err;
316 struct got_commit_object *root_commit;
317 TAILQ_HEAD(, commit_queue_entry) commits;
318 struct commit_queue_entry *entry;
320 TAILQ_INIT(&commits);
322 err = got_object_commit_open(&root_commit, repo, root_obj);
323 if (err)
324 return err;
326 entry = calloc(1, sizeof(*entry));
327 if (entry == NULL)
328 return got_error_from_errno();
329 entry->id = got_object_id_dup(root_id);
330 if (entry->id == NULL) {
331 err = got_error_from_errno();
332 free(entry);
333 return err;
335 entry->commit = root_commit;
336 TAILQ_INSERT_HEAD(&commits, entry, entry);
338 while (!TAILQ_EMPTY(&commits)) {
339 struct got_parent_id *pid;
341 entry = TAILQ_FIRST(&commits);
342 err = print_commit(entry->commit, entry->id, repo, show_patch);
343 if (err)
344 break;
346 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
347 struct got_object *obj;
348 struct got_commit_object *pcommit;
349 struct commit_queue_entry *pentry;
351 err = got_object_open(&obj, repo, pid->id);
352 if (err)
353 break;
354 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
355 err = got_error(GOT_ERR_OBJ_TYPE);
356 break;
359 err = got_object_commit_open(&pcommit, repo, obj);
360 got_object_close(obj);
361 if (err)
362 break;
364 pentry = calloc(1, sizeof(*pentry));
365 if (pentry == NULL) {
366 err = got_error_from_errno();
367 got_object_commit_close(pcommit);
368 break;
370 pentry->id = got_object_id_dup(pid->id);
371 if (pentry->id == NULL) {
372 err = got_error_from_errno();
373 got_object_commit_close(pcommit);
374 break;
376 pentry->commit = pcommit;
377 TAILQ_INSERT_TAIL(&commits, pentry, entry);
380 TAILQ_REMOVE(&commits, entry, entry);
381 got_object_commit_close(entry->commit);
382 free(entry->id);
383 free(entry);
386 return err;
389 __dead void
390 usage_log(void)
392 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
393 exit(1);
396 const struct got_error *
397 cmd_log(int argc, char *argv[])
399 const struct got_error *error;
400 struct got_repository *repo;
401 struct got_reference *head_ref;
402 struct got_object_id *id;
403 struct got_object *obj;
404 char *repo_path = NULL;
405 int ch;
406 int show_patch = 0;
408 #ifndef PROFILE
409 if (pledge("stdio rpath wpath cpath", NULL) == -1)
410 err(1, "pledge");
411 #endif
413 while ((ch = getopt(argc, argv, "p")) != -1) {
414 switch (ch) {
415 case 'p':
416 show_patch = 1;
417 break;
418 default:
419 usage();
420 /* NOTREACHED */
424 argc -= optind;
425 argv += optind;
427 if (argc == 0) {
428 repo_path = getcwd(NULL, 0);
429 if (repo_path == NULL)
430 err(1, "getcwd");
431 } else if (argc == 1)
432 repo_path = argv[1];
433 else
434 usage_log();
436 error = got_repo_open(&repo, repo_path);
437 if (error != NULL)
438 return error;
439 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
440 if (error != NULL)
441 return error;
442 error = got_ref_resolve(&id, repo, head_ref);
443 if (error != NULL)
444 return error;
446 error = got_object_open(&obj, repo, id);
447 if (error != NULL)
448 return error;
449 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
450 error = print_commits(obj, id, repo, show_patch);
451 else
452 error = got_error(GOT_ERR_OBJ_TYPE);
453 got_object_close(obj);
454 free(id);
455 got_ref_close(head_ref);
456 got_repo_close(repo);
457 return error;
460 #ifdef notyet
461 const struct got_error *
462 cmd_status(int argc __unused, char *argv[] __unused)
464 git_repository *repo = NULL;
465 git_status_list *status;
466 git_status_options statusopts;
467 size_t i;
469 git_libgit2_init();
471 if (git_repository_open_ext(&repo, ".", 0, NULL))
472 errx(1, "git_repository_open: %s", giterr_last()->message);
474 if (git_repository_is_bare(repo))
475 errx(1, "bar repository");
477 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
478 errx(1, "git_status_init_options: %s", giterr_last()->message);
480 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
481 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
482 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
483 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
485 if (git_status_list_new(&status, repo, &statusopts))
486 errx(1, "git_status_list_new: %s", giterr_last()->message);
488 for (i = 0; i < git_status_list_entrycount(status); i++) {
489 const git_status_entry *se;
491 se = git_status_byindex(status, i);
492 switch (se->status) {
493 case GIT_STATUS_WT_NEW:
494 printf("? %s\n", se->index_to_workdir->new_file.path);
495 break;
496 case GIT_STATUS_WT_MODIFIED:
497 printf("M %s\n", se->index_to_workdir->new_file.path);
498 break;
499 case GIT_STATUS_WT_DELETED:
500 printf("R %s\n", se->index_to_workdir->new_file.path);
501 break;
502 case GIT_STATUS_WT_RENAMED:
503 printf("m %s -> %s\n",
504 se->index_to_workdir->old_file.path,
505 se->index_to_workdir->new_file.path);
506 break;
507 case GIT_STATUS_WT_TYPECHANGE:
508 printf("t %s\n", se->index_to_workdir->new_file.path);
509 break;
510 case GIT_STATUS_INDEX_NEW:
511 printf("A %s\n", se->head_to_index->new_file.path);
512 break;
513 case GIT_STATUS_INDEX_MODIFIED:
514 printf("M %s\n", se->head_to_index->old_file.path);
515 break;
516 case GIT_STATUS_INDEX_DELETED:
517 printf("R %s\n", se->head_to_index->old_file.path);
518 break;
519 case GIT_STATUS_INDEX_RENAMED:
520 printf("m %s -> %s\n",
521 se->head_to_index->old_file.path,
522 se->head_to_index->new_file.path);
523 break;
524 case GIT_STATUS_INDEX_TYPECHANGE:
525 printf("t %s\n", se->head_to_index->old_file.path);
526 break;
527 case GIT_STATUS_CURRENT:
528 default:
529 break;
533 git_status_list_free(status);
534 git_repository_free(repo);
535 git_libgit2_shutdown();
537 return 0;
539 #endif