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>
28 #include "got_error.h"
29 #include "got_object.h"
31 #include "got_repository.h"
34 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
39 int (*cmd_main)(int, char *[]);
40 void (*cmd_usage)(void);
41 const char *cmd_descr;
44 __dead void usage(void);
45 __dead void usage_log(void);
47 int cmd_log(int, char *[]);
48 int cmd_status(int, char *[]);
50 struct cmd got_commands[] = {
51 { "log", cmd_log, usage_log,
52 "show repository history" },
54 { "status", cmd_status, usage_status,
55 "show modification status of files" },
60 main(int argc, char *argv[])
67 setlocale(LC_ALL, "");
69 if (pledge("stdio rpath wpath cpath", NULL) == -1)
72 while ((ch = getopt(argc, argv, "h")) != -1) {
89 for (i = 0; i < nitems(got_commands); i++) {
90 cmd = &got_commands[i];
92 if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
96 got_commands[i].cmd_usage();
98 return got_commands[i].cmd_main(argc, argv);
102 fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
111 fprintf(stderr, "usage: %s [-h] command [arg ...]\n\nAvailable commands:\n",
113 for (i = 0; i < nitems(got_commands); i++) {
114 struct cmd *cmd = &got_commands[i];
115 fprintf(stderr, " %s: %s\n", cmd->cmd_name, cmd->cmd_descr);
120 static const struct got_error *
121 print_commit_object(struct got_object *, struct got_object_id *,
122 struct got_repository *);
124 static const struct got_error *
125 print_parent_commits(struct got_commit_object *commit,
126 struct got_repository *repo)
128 struct got_parent_id *pid;
129 const struct got_error *err = NULL;
130 struct got_object *obj;
132 SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
133 err = got_object_open(&obj, repo, pid->id);
136 if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
137 return got_error(GOT_ERR_OBJ_TYPE);
138 err = print_commit_object(obj, pid->id, repo);
139 got_object_close(obj);
145 static const struct got_error *
146 print_commit_object(struct got_object *obj, struct got_object_id *id,
147 struct got_repository *repo)
149 struct got_commit_object *commit;
151 const struct got_error *err;
153 err = got_object_commit_open(&commit, repo, obj);
157 err = got_object_id_str(&buf, id);
161 printf("-----------------------------------------------\n");
162 printf("commit: %s\n", buf);
163 printf("Author: %s\n", commit->author);
164 printf("\n%s\n", commit->logmsg);
168 err = print_parent_commits(commit, repo);
169 got_object_commit_close(commit);
176 fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
181 cmd_log(int argc, char *argv[])
183 const struct got_error *error;
184 struct got_repository *repo;
185 struct got_reference *head_ref;
186 struct got_object_id *id;
187 struct got_object *obj;
188 char *repo_path = NULL;
190 if (pledge("stdio rpath wpath cpath", NULL) == -1)
194 repo_path = getcwd(NULL, 0);
195 if (repo_path == NULL)
197 } else if (argc == 2)
202 error = got_repo_open(&repo, repo_path);
203 if (error != NULL || repo == NULL)
205 error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
206 if (error != NULL || head_ref == NULL)
208 error = got_ref_resolve(&id, repo, head_ref);
209 if (error != NULL || head_ref == NULL)
212 error = got_object_open(&obj, repo, id);
213 if (error != NULL || obj == NULL)
215 if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
216 error = print_commit_object(obj, id, repo);
221 got_object_close(obj);
223 got_ref_close(head_ref);
224 got_repo_close(repo);
230 cmd_status(int argc __unused, char *argv[] __unused)
232 git_repository *repo = NULL;
233 git_status_list *status;
234 git_status_options statusopts;
239 if (git_repository_open_ext(&repo, ".", 0, NULL))
240 errx(1, "git_repository_open: %s", giterr_last()->message);
242 if (git_repository_is_bare(repo))
243 errx(1, "bar repository");
245 if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
246 errx(1, "git_status_init_options: %s", giterr_last()->message);
248 statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
249 statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
250 GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
251 GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
253 if (git_status_list_new(&status, repo, &statusopts))
254 errx(1, "git_status_list_new: %s", giterr_last()->message);
256 for (i = 0; i < git_status_list_entrycount(status); i++) {
257 const git_status_entry *se;
259 se = git_status_byindex(status, i);
260 switch (se->status) {
261 case GIT_STATUS_WT_NEW:
262 printf("? %s\n", se->index_to_workdir->new_file.path);
264 case GIT_STATUS_WT_MODIFIED:
265 printf("M %s\n", se->index_to_workdir->new_file.path);
267 case GIT_STATUS_WT_DELETED:
268 printf("R %s\n", se->index_to_workdir->new_file.path);
270 case GIT_STATUS_WT_RENAMED:
271 printf("m %s -> %s\n",
272 se->index_to_workdir->old_file.path,
273 se->index_to_workdir->new_file.path);
275 case GIT_STATUS_WT_TYPECHANGE:
276 printf("t %s\n", se->index_to_workdir->new_file.path);
278 case GIT_STATUS_INDEX_NEW:
279 printf("A %s\n", se->head_to_index->new_file.path);
281 case GIT_STATUS_INDEX_MODIFIED:
282 printf("M %s\n", se->head_to_index->old_file.path);
284 case GIT_STATUS_INDEX_DELETED:
285 printf("R %s\n", se->head_to_index->old_file.path);
287 case GIT_STATUS_INDEX_RENAMED:
288 printf("m %s -> %s\n",
289 se->head_to_index->old_file.path,
290 se->head_to_index->new_file.path);
292 case GIT_STATUS_INDEX_TYPECHANGE:
293 printf("t %s\n", se->head_to_index->old_file.path);
295 case GIT_STATUS_CURRENT:
301 git_status_list_free(status);
302 git_repository_free(repo);
303 git_libgit2_shutdown();