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);
300 if (err == 0)
301 printf("\n");
304 free(buf);
305 return err;
308 struct commit_queue_entry {
309 TAILQ_ENTRY(commit_queue_entry) entry;
310 struct got_object_id *id;
311 struct got_commit_object *commit;
312 };
314 static const struct got_error *
315 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
316 struct got_repository *repo, int show_patch)
318 const struct got_error *err;
319 struct got_commit_object *root_commit;
320 TAILQ_HEAD(, commit_queue_entry) commits;
321 struct commit_queue_entry *entry;
323 TAILQ_INIT(&commits);
325 err = got_object_commit_open(&root_commit, repo, root_obj);
326 if (err)
327 return err;
329 entry = calloc(1, sizeof(*entry));
330 if (entry == NULL)
331 return got_error_from_errno();
332 entry->id = got_object_id_dup(root_id);
333 if (entry->id == NULL) {
334 err = got_error_from_errno();
335 free(entry);
336 return err;
338 entry->commit = root_commit;
339 TAILQ_INSERT_HEAD(&commits, entry, entry);
341 while (!TAILQ_EMPTY(&commits)) {
342 struct got_parent_id *pid;
344 entry = TAILQ_FIRST(&commits);
345 err = print_commit(entry->commit, entry->id, repo, show_patch);
346 if (err) {
347 while (!TAILQ_EMPTY(&commits)) {
348 entry = TAILQ_FIRST(&commits);
349 TAILQ_REMOVE(&commits, entry, entry);
350 got_object_commit_close(entry->commit);
351 free(entry->id);
352 free(entry);
354 break;
357 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
358 struct got_object *obj;
359 struct got_commit_object *pcommit;
360 struct commit_queue_entry *pentry;
362 err = got_object_open(&obj, repo, pid->id);
363 if (err)
364 break;
365 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
366 err = got_error(GOT_ERR_OBJ_TYPE);
367 break;
370 err = got_object_commit_open(&pcommit, repo, obj);
371 got_object_close(obj);
372 if (err)
373 break;
375 pentry = calloc(1, sizeof(*pentry));
376 if (pentry == NULL) {
377 err = got_error_from_errno();
378 got_object_commit_close(pcommit);
379 break;
381 pentry->id = got_object_id_dup(pid->id);
382 if (pentry->id == NULL) {
383 err = got_error_from_errno();
384 got_object_commit_close(pcommit);
385 break;
387 pentry->commit = pcommit;
388 TAILQ_INSERT_TAIL(&commits, pentry, entry);
391 TAILQ_REMOVE(&commits, entry, entry);
392 got_object_commit_close(entry->commit);
393 free(entry->id);
394 free(entry);
397 return err;
400 __dead void
401 usage_log(void)
403 fprintf(stderr, "usage: %s log [-p] [repository-path] [commit]\n",
404 getprogname());
405 exit(1);
408 const struct got_error *
409 cmd_log(int argc, char *argv[])
411 const struct got_error *error;
412 struct got_repository *repo;
413 struct got_object_id *id = NULL;
414 struct got_object *obj;
415 char *repo_path = NULL;
416 char *commit_id_str = NULL;
417 int ch;
418 int show_patch = 0;
420 #ifndef PROFILE
421 if (pledge("stdio rpath wpath cpath", NULL) == -1)
422 err(1, "pledge");
423 #endif
425 while ((ch = getopt(argc, argv, "p")) != -1) {
426 switch (ch) {
427 case 'p':
428 show_patch = 1;
429 break;
430 default:
431 usage();
432 /* NOTREACHED */
436 argc -= optind;
437 argv += optind;
439 if (argc == 0) {
440 repo_path = getcwd(NULL, 0);
441 if (repo_path == NULL)
442 err(1, "getcwd");
443 } else if (argc == 1) {
444 repo_path = argv[0];
445 } else if (argc == 2) {
446 repo_path = argv[0];
447 commit_id_str = argv[1];
448 } else
449 usage_log();
451 error = got_repo_open(&repo, repo_path);
452 if (error != NULL)
453 return error;
455 if (commit_id_str == NULL) {
456 struct got_reference *head_ref;
457 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
458 if (error != NULL)
459 return error;
460 error = got_ref_resolve(&id, repo, head_ref);
461 got_ref_close(head_ref);
462 if (error != NULL)
463 return error;
464 error = got_object_open(&obj, repo, id);
465 } else {
466 error = got_object_open_by_id_str(&obj, repo, commit_id_str);
467 if (error == NULL) {
468 id = got_object_get_id(obj);
469 if (id == NULL)
470 error = got_error_from_errno();
473 if (error != NULL)
474 return error;
475 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
476 error = print_commits(obj, id, repo, show_patch);
477 else
478 error = got_error(GOT_ERR_OBJ_TYPE);
479 got_object_close(obj);
480 free(id);
481 got_repo_close(repo);
482 return error;
485 #ifdef notyet
486 const struct got_error *
487 cmd_status(int argc __unused, char *argv[] __unused)
489 git_repository *repo = NULL;
490 git_status_list *status;
491 git_status_options statusopts;
492 size_t i;
494 git_libgit2_init();
496 if (git_repository_open_ext(&repo, ".", 0, NULL))
497 errx(1, "git_repository_open: %s", giterr_last()->message);
499 if (git_repository_is_bare(repo))
500 errx(1, "bar repository");
502 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
503 errx(1, "git_status_init_options: %s", giterr_last()->message);
505 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
506 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
507 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
508 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
510 if (git_status_list_new(&status, repo, &statusopts))
511 errx(1, "git_status_list_new: %s", giterr_last()->message);
513 for (i = 0; i < git_status_list_entrycount(status); i++) {
514 const git_status_entry *se;
516 se = git_status_byindex(status, i);
517 switch (se->status) {
518 case GIT_STATUS_WT_NEW:
519 printf("? %s\n", se->index_to_workdir->new_file.path);
520 break;
521 case GIT_STATUS_WT_MODIFIED:
522 printf("M %s\n", se->index_to_workdir->new_file.path);
523 break;
524 case GIT_STATUS_WT_DELETED:
525 printf("R %s\n", se->index_to_workdir->new_file.path);
526 break;
527 case GIT_STATUS_WT_RENAMED:
528 printf("m %s -> %s\n",
529 se->index_to_workdir->old_file.path,
530 se->index_to_workdir->new_file.path);
531 break;
532 case GIT_STATUS_WT_TYPECHANGE:
533 printf("t %s\n", se->index_to_workdir->new_file.path);
534 break;
535 case GIT_STATUS_INDEX_NEW:
536 printf("A %s\n", se->head_to_index->new_file.path);
537 break;
538 case GIT_STATUS_INDEX_MODIFIED:
539 printf("M %s\n", se->head_to_index->old_file.path);
540 break;
541 case GIT_STATUS_INDEX_DELETED:
542 printf("R %s\n", se->head_to_index->old_file.path);
543 break;
544 case GIT_STATUS_INDEX_RENAMED:
545 printf("m %s -> %s\n",
546 se->head_to_index->old_file.path,
547 se->head_to_index->new_file.path);
548 break;
549 case GIT_STATUS_INDEX_TYPECHANGE:
550 printf("t %s\n", se->head_to_index->old_file.path);
551 break;
552 case GIT_STATUS_CURRENT:
553 default:
554 break;
558 git_status_list_free(status);
559 git_repository_free(repo);
560 git_libgit2_shutdown();
562 return 0;
564 #endif