2 * Copyright (c) 2017 Martin Pieuchot <mpi@openbsd.org>
3 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include <sys/queue.h>
29 #include "got_error.h"
30 #include "got_object.h"
32 #include "got_repository.h"
33 #include "got_worktree.h"
36 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
41 const struct got_error *(*cmd_main)(int, char *[]);
42 void (*cmd_usage)(void);
43 const char *cmd_descr;
46 __dead void usage(void);
47 __dead void usage_checkout(void);
48 __dead void usage_log(void);
50 const struct got_error* cmd_checkout(int, char *[]);
51 const struct got_error* cmd_log(int, char *[]);
52 const struct got_error* cmd_status(int, char *[]);
54 struct cmd got_commands[] = {
55 { "checkout", cmd_checkout, usage_checkout,
56 "check out a new work tree from a repository" },
57 { "log", cmd_log, usage_log,
58 "show repository history" },
60 { "status", cmd_status, usage_status,
61 "show modification status of files" },
66 main(int argc, char *argv[])
73 setlocale(LC_ALL, "");
75 while ((ch = getopt(argc, argv, "h")) != -1) {
92 for (i = 0; i < nitems(got_commands); i++) {
93 const struct got_error *error;
95 cmd = &got_commands[i];
97 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
101 got_commands[i].cmd_usage();
103 error = got_commands[i].cmd_main(argc, argv);
105 fprintf(stderr, "%s: %s\n", getprogname(), error->msg);
112 fprintf(stderr, "%s: unknown command '%s'\n", getprogname(), argv[0]);
121 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\n"
122 "Available commands:\n", getprogname());
123 for (i = 0; i < nitems(got_commands); i++) {
124 struct cmd *cmd = &got_commands[i];
125 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
133 fprintf(stderr, "usage: %s checkout [-p prefix] repository-path "
134 "[worktree-path]\n", getprogname());
139 checkout_progress(void *arg, const char *path)
141 char *worktree_path = arg;
143 while (path[0] == '/')
146 printf("A %s/%s\n", worktree_path, path);
149 const struct got_error *
150 cmd_checkout(int argc, char *argv[])
152 const struct got_error *error = NULL;
153 struct got_repository *repo = NULL;
154 struct got_reference *head_ref = NULL;
155 struct got_worktree *worktree = NULL;
156 char *repo_path = NULL;
157 char *worktree_path = NULL;
158 const char *path_prefix = "";
162 while ((ch = getopt(argc, argv, "p:")) != -1) {
165 path_prefix = optarg;
177 if (pledge("stdio rpath wpath cpath flock", NULL) == -1)
181 char *cwd, *base, *dotgit;
183 cwd = getcwd(NULL, 0);
186 base = basename(repo_path);
189 dotgit = strstr(base, ".git");
192 if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
193 error = got_error_from_errno();
198 } else if (argc == 2) {
200 worktree_path = strdup(argv[1]);
201 if (worktree_path == NULL)
202 return got_error_from_errno();
206 error = got_repo_open(&repo, repo_path);
209 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
213 error = got_worktree_init(worktree_path, head_ref, path_prefix, repo);
217 error = got_worktree_open(&worktree, worktree_path);
221 error = got_worktree_checkout_files(worktree, head_ref, repo,
222 checkout_progress, worktree_path);
226 printf("checked out %s\n", worktree_path);
233 static const struct got_error *
234 print_commit(struct got_commit_object *commit, struct got_object_id *id,
235 struct got_repository *repo)
237 const struct got_error *err = NULL;
240 err = got_object_id_str(&buf, id);
244 printf("-----------------------------------------------\n");
245 printf("commit: %s\n", buf);
246 printf("Author: %s\n", commit->author);
247 printf("\n%s\n", commit->logmsg);
253 struct commit_queue_entry {
254 TAILQ_ENTRY(commit_queue_entry) entry;
255 struct got_object_id *id;
256 struct got_commit_object *commit;
259 static const struct got_error *
260 print_commits(struct got_object *root_obj, struct got_object_id *root_id,
261 struct got_repository *repo)
263 const struct got_error *err;
264 struct got_commit_object *root_commit;
265 TAILQ_HEAD(, commit_queue_entry) commits;
266 struct commit_queue_entry *entry;
268 TAILQ_INIT(&commits);
270 err = got_object_commit_open(&root_commit, repo, root_obj);
274 entry = calloc(1, sizeof(*entry));
276 return got_error_from_errno();
277 entry->id = got_object_id_dup(root_id);
278 if (entry->id == NULL) {
279 err = got_error_from_errno();
283 entry->commit = root_commit;
284 TAILQ_INSERT_HEAD(&commits, entry, entry);
286 while (!TAILQ_EMPTY(&commits)) {
287 struct got_parent_id *pid;
289 entry = TAILQ_FIRST(&commits);
290 err = print_commit(entry->commit, entry->id, repo);
294 SIMPLEQ_FOREACH(pid, &entry->commit->parent_ids, entry) {
295 struct got_object *obj;
296 struct got_commit_object *pcommit;
297 struct commit_queue_entry *pentry;
299 err = got_object_open(&obj, repo, pid->id);
302 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
303 err = got_error(GOT_ERR_OBJ_TYPE);
307 err = got_object_commit_open(&pcommit, repo, obj);
308 got_object_close(obj);
312 pentry = calloc(1, sizeof(*pentry));
313 if (pentry == NULL) {
314 err = got_error_from_errno();
315 got_object_commit_close(pcommit);
318 pentry->id = got_object_id_dup(pid->id);
319 if (pentry->id == NULL) {
320 err = got_error_from_errno();
321 got_object_commit_close(pcommit);
324 pentry->commit = pcommit;
325 TAILQ_INSERT_TAIL(&commits, pentry, entry);
328 TAILQ_REMOVE(&commits, entry, entry);
329 got_object_commit_close(entry->commit);
340 fprintf(stderr, "usage: %s log [repository-path]\n", getprogname());
344 const struct got_error *
345 cmd_log(int argc, char *argv[])
347 const struct got_error *error;
348 struct got_repository *repo;
349 struct got_reference *head_ref;
350 struct got_object_id *id;
351 struct got_object *obj;
352 char *repo_path = NULL;
355 if (pledge("stdio rpath wpath cpath", NULL) == -1)
359 repo_path = getcwd(NULL, 0);
360 if (repo_path == NULL)
362 } else if (argc == 2)
367 error = got_repo_open(&repo, repo_path);
370 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
373 error = got_ref_resolve(&id, repo, head_ref);
377 error = got_object_open(&obj, repo, id);
380 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT)
381 error = print_commits(obj, id, repo);
383 error = got_error(GOT_ERR_OBJ_TYPE);
384 got_object_close(obj);
386 got_ref_close(head_ref);
387 got_repo_close(repo);
392 const struct got_error *
393 cmd_status(int argc __unused, char *argv[] __unused)
395 git_repository *repo = NULL;
396 git_status_list *status;
397 git_status_options statusopts;
402 if (git_repository_open_ext(&repo, ".", 0, NULL))
403 errx(1, "git_repository_open: %s", giterr_last()->message);
405 if (git_repository_is_bare(repo))
406 errx(1, "bar repository");
408 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
409 errx(1, "git_status_init_options: %s", giterr_last()->message);
411 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
412 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
413 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
414 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
416 if (git_status_list_new(&status, repo, &statusopts))
417 errx(1, "git_status_list_new: %s", giterr_last()->message);
419 for (i = 0; i < git_status_list_entrycount(status); i++) {
420 const git_status_entry *se;
422 se = git_status_byindex(status, i);
423 switch (se->status) {
424 case GIT_STATUS_WT_NEW:
425 printf("? %s\n", se->index_to_workdir->new_file.path);
427 case GIT_STATUS_WT_MODIFIED:
428 printf("M %s\n", se->index_to_workdir->new_file.path);
430 case GIT_STATUS_WT_DELETED:
431 printf("R %s\n", se->index_to_workdir->new_file.path);
433 case GIT_STATUS_WT_RENAMED:
434 printf("m %s -> %s\n",
435 se->index_to_workdir->old_file.path,
436 se->index_to_workdir->new_file.path);
438 case GIT_STATUS_WT_TYPECHANGE:
439 printf("t %s\n", se->index_to_workdir->new_file.path);
441 case GIT_STATUS_INDEX_NEW:
442 printf("A %s\n", se->head_to_index->new_file.path);
444 case GIT_STATUS_INDEX_MODIFIED:
445 printf("M %s\n", se->head_to_index->old_file.path);
447 case GIT_STATUS_INDEX_DELETED:
448 printf("R %s\n", se->head_to_index->old_file.path);
450 case GIT_STATUS_INDEX_RENAMED:
451 printf("m %s -> %s\n",
452 se->head_to_index->old_file.path,
453 se->head_to_index->new_file.path);
455 case GIT_STATUS_INDEX_TYPECHANGE:
456 printf("t %s\n", se->head_to_index->old_file.path);
458 case GIT_STATUS_CURRENT:
464 git_status_list_free(status);
465 git_repository_free(repo);
466 git_libgit2_shutdown();