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 f42b1b34 2018-03-12 stsp
20 5c860e29 2018-03-12 stsp #include <err.h>
21 5c860e29 2018-03-12 stsp #include <errno.h>
22 5c860e29 2018-03-12 stsp #include <locale.h>
23 5c860e29 2018-03-12 stsp #include <stdio.h>
24 5c860e29 2018-03-12 stsp #include <stdlib.h>
25 5c860e29 2018-03-12 stsp #include <string.h>
26 5c860e29 2018-03-12 stsp #include <unistd.h>
27 5c860e29 2018-03-12 stsp
28 f42b1b34 2018-03-12 stsp #include "got_error.h"
29 f42b1b34 2018-03-12 stsp #include "got_object.h"
30 f42b1b34 2018-03-12 stsp #include "got_refs.h"
31 f42b1b34 2018-03-12 stsp #include "got_repository.h"
32 5c860e29 2018-03-12 stsp
33 5c860e29 2018-03-12 stsp #ifndef nitems
34 5c860e29 2018-03-12 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
35 5c860e29 2018-03-12 stsp #endif
36 5c860e29 2018-03-12 stsp
37 5c860e29 2018-03-12 stsp struct cmd {
38 5c860e29 2018-03-12 stsp const char *cmd_name;
39 5c860e29 2018-03-12 stsp int (*cmd_main)(int, char *[]);
40 5c860e29 2018-03-12 stsp };
41 5c860e29 2018-03-12 stsp
42 5c860e29 2018-03-12 stsp __dead void usage(void);
43 6f3d1eb0 2018-03-12 stsp __dead void usage_log(void);
44 5c860e29 2018-03-12 stsp
45 5c860e29 2018-03-12 stsp int cmd_log(int, char *[]);
46 5c860e29 2018-03-12 stsp int cmd_status(int, char *[]);
47 5c860e29 2018-03-12 stsp
48 5c860e29 2018-03-12 stsp struct cmd got_commands[] = {
49 5c860e29 2018-03-12 stsp { "log", cmd_log },
50 f42b1b34 2018-03-12 stsp #ifdef notyet
51 5c860e29 2018-03-12 stsp { "status", cmd_status },
52 f42b1b34 2018-03-12 stsp #endif
53 5c860e29 2018-03-12 stsp };
54 5c860e29 2018-03-12 stsp
55 5c860e29 2018-03-12 stsp int
56 5c860e29 2018-03-12 stsp main(int argc, char *argv[])
57 5c860e29 2018-03-12 stsp {
58 5c860e29 2018-03-12 stsp struct cmd *cmd;
59 5c860e29 2018-03-12 stsp unsigned int i;
60 5c860e29 2018-03-12 stsp int ch;
61 5c860e29 2018-03-12 stsp
62 5c860e29 2018-03-12 stsp setlocale(LC_ALL, "");
63 5c860e29 2018-03-12 stsp
64 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
65 5c860e29 2018-03-12 stsp err(1, "pledge");
66 5c860e29 2018-03-12 stsp
67 5c860e29 2018-03-12 stsp while ((ch = getopt(argc, argv, "")) != -1) {
68 5c860e29 2018-03-12 stsp switch (ch) {
69 5c860e29 2018-03-12 stsp default:
70 5c860e29 2018-03-12 stsp usage();
71 5c860e29 2018-03-12 stsp /* NOTREACHED */
72 5c860e29 2018-03-12 stsp }
73 5c860e29 2018-03-12 stsp }
74 5c860e29 2018-03-12 stsp
75 5c860e29 2018-03-12 stsp argc -= optind;
76 5c860e29 2018-03-12 stsp argv += optind;
77 5c860e29 2018-03-12 stsp
78 5c860e29 2018-03-12 stsp if (argc <= 0)
79 5c860e29 2018-03-12 stsp usage();
80 5c860e29 2018-03-12 stsp
81 5c860e29 2018-03-12 stsp for (i = 0; i < nitems(got_commands); i++) {
82 5c860e29 2018-03-12 stsp cmd = &got_commands[i];
83 5c860e29 2018-03-12 stsp
84 5c860e29 2018-03-12 stsp if (strncmp(cmd->cmd_name, argv[0], strlen(argv[0])))
85 5c860e29 2018-03-12 stsp continue;
86 5c860e29 2018-03-12 stsp
87 5c860e29 2018-03-12 stsp return got_commands[i].cmd_main(argc, argv);
88 5c860e29 2018-03-12 stsp /* NOTREACHED */
89 5c860e29 2018-03-12 stsp }
90 5c860e29 2018-03-12 stsp
91 5c860e29 2018-03-12 stsp fprintf(stderr, "%s: unknown command -- %s\n", getprogname(), argv[0]);
92 5c860e29 2018-03-12 stsp return 1;
93 5c860e29 2018-03-12 stsp }
94 5c860e29 2018-03-12 stsp
95 5c860e29 2018-03-12 stsp __dead void
96 5c860e29 2018-03-12 stsp usage(void)
97 5c860e29 2018-03-12 stsp {
98 5c860e29 2018-03-12 stsp fprintf(stderr, "usage: %s command [arg ...]\n", getprogname());
99 5c860e29 2018-03-12 stsp exit(1);
100 5c860e29 2018-03-12 stsp }
101 5c860e29 2018-03-12 stsp
102 f42b1b34 2018-03-12 stsp static const struct got_error *
103 f42b1b34 2018-03-12 stsp print_commit_object(struct got_object *, struct got_object_id *,
104 f42b1b34 2018-03-12 stsp struct got_repository *);
105 f42b1b34 2018-03-12 stsp
106 f42b1b34 2018-03-12 stsp static const struct got_error *
107 f42b1b34 2018-03-12 stsp print_parent_commits(struct got_commit_object *commit,
108 f42b1b34 2018-03-12 stsp struct got_repository *repo)
109 5c860e29 2018-03-12 stsp {
110 f42b1b34 2018-03-12 stsp struct got_parent_id *pid;
111 f42b1b34 2018-03-12 stsp const struct got_error *err = NULL;
112 f42b1b34 2018-03-12 stsp struct got_object *obj;
113 5c860e29 2018-03-12 stsp
114 f42b1b34 2018-03-12 stsp SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
115 f42b1b34 2018-03-12 stsp err = got_object_open(&obj, repo, pid->id);
116 f42b1b34 2018-03-12 stsp if (err != NULL)
117 f42b1b34 2018-03-12 stsp return err;
118 f42b1b34 2018-03-12 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
119 f42b1b34 2018-03-12 stsp return got_error(GOT_ERR_OBJ_TYPE);
120 f42b1b34 2018-03-12 stsp err = print_commit_object(obj, pid->id, repo);
121 f42b1b34 2018-03-12 stsp got_object_close(obj);
122 f42b1b34 2018-03-12 stsp }
123 5c860e29 2018-03-12 stsp
124 f42b1b34 2018-03-12 stsp return err;
125 f42b1b34 2018-03-12 stsp }
126 5c860e29 2018-03-12 stsp
127 f42b1b34 2018-03-12 stsp static const struct got_error *
128 f42b1b34 2018-03-12 stsp print_commit_object(struct got_object *obj, struct got_object_id *id,
129 f42b1b34 2018-03-12 stsp struct got_repository *repo)
130 f42b1b34 2018-03-12 stsp {
131 f42b1b34 2018-03-12 stsp struct got_commit_object *commit;
132 f42b1b34 2018-03-12 stsp char *buf;
133 f42b1b34 2018-03-12 stsp const struct got_error *err;
134 5c860e29 2018-03-12 stsp
135 f42b1b34 2018-03-12 stsp err = got_object_commit_open(&commit, repo, obj);
136 f42b1b34 2018-03-12 stsp if (err)
137 f42b1b34 2018-03-12 stsp return err;
138 5c860e29 2018-03-12 stsp
139 f42b1b34 2018-03-12 stsp err = got_object_id_str(&buf, id);
140 f42b1b34 2018-03-12 stsp if (err)
141 f42b1b34 2018-03-12 stsp return err;
142 5c860e29 2018-03-12 stsp
143 f42b1b34 2018-03-12 stsp printf("-----------------------------------------------\n");
144 f42b1b34 2018-03-12 stsp printf("commit: %s\n", buf);
145 f42b1b34 2018-03-12 stsp printf("Author: %s\n", commit->author);
146 f42b1b34 2018-03-12 stsp printf("\n%s\n", commit->logmsg);
147 5c860e29 2018-03-12 stsp
148 f42b1b34 2018-03-12 stsp free(buf);
149 5c860e29 2018-03-12 stsp
150 f42b1b34 2018-03-12 stsp err = print_parent_commits(commit, repo);
151 f42b1b34 2018-03-12 stsp got_object_commit_close(commit);
152 f42b1b34 2018-03-12 stsp return err;
153 f42b1b34 2018-03-12 stsp }
154 5c860e29 2018-03-12 stsp
155 6f3d1eb0 2018-03-12 stsp __dead void
156 6f3d1eb0 2018-03-12 stsp usage_log(void)
157 6f3d1eb0 2018-03-12 stsp {
158 6f3d1eb0 2018-03-12 stsp fprintf(stderr, "usage: %s log [REPO_PATH]\n", getprogname());
159 6f3d1eb0 2018-03-12 stsp exit(1);
160 6f3d1eb0 2018-03-12 stsp }
161 6f3d1eb0 2018-03-12 stsp
162 f42b1b34 2018-03-12 stsp int
163 f42b1b34 2018-03-12 stsp cmd_log(int argc, char *argv[])
164 f42b1b34 2018-03-12 stsp {
165 f42b1b34 2018-03-12 stsp const struct got_error *error;
166 f42b1b34 2018-03-12 stsp struct got_repository *repo;
167 f42b1b34 2018-03-12 stsp struct got_reference *head_ref;
168 f42b1b34 2018-03-12 stsp struct got_object_id *id;
169 f42b1b34 2018-03-12 stsp struct got_object *obj;
170 6f3d1eb0 2018-03-12 stsp char *repo_path = NULL;
171 5c860e29 2018-03-12 stsp
172 f42b1b34 2018-03-12 stsp if (pledge("stdio rpath wpath cpath", NULL) == -1)
173 f42b1b34 2018-03-12 stsp err(1, "pledge");
174 f42b1b34 2018-03-12 stsp
175 6f3d1eb0 2018-03-12 stsp if (argc == 1) {
176 6f3d1eb0 2018-03-12 stsp repo_path = getcwd(NULL, 0);
177 6f3d1eb0 2018-03-12 stsp if (repo_path == NULL)
178 6f3d1eb0 2018-03-12 stsp err(1, "getcwd");
179 6f3d1eb0 2018-03-12 stsp } else if (argc == 2)
180 6f3d1eb0 2018-03-12 stsp repo_path = argv[1];
181 6f3d1eb0 2018-03-12 stsp else
182 6f3d1eb0 2018-03-12 stsp usage_log();
183 f42b1b34 2018-03-12 stsp
184 f42b1b34 2018-03-12 stsp error = got_repo_open(&repo, repo_path);
185 f42b1b34 2018-03-12 stsp if (error != NULL || repo == NULL)
186 f42b1b34 2018-03-12 stsp return 1;
187 f42b1b34 2018-03-12 stsp error = got_ref_open(&head_ref, repo, GOT_REF_HEAD);
188 f42b1b34 2018-03-12 stsp if (error != NULL || head_ref == NULL)
189 f42b1b34 2018-03-12 stsp return 1;
190 f42b1b34 2018-03-12 stsp error = got_ref_resolve(&id, repo, head_ref);
191 f42b1b34 2018-03-12 stsp if (error != NULL || head_ref == NULL)
192 f42b1b34 2018-03-12 stsp return 1;
193 f42b1b34 2018-03-12 stsp
194 f42b1b34 2018-03-12 stsp error = got_object_open(&obj, repo, id);
195 f42b1b34 2018-03-12 stsp if (error != NULL || obj == NULL)
196 f42b1b34 2018-03-12 stsp return 1;
197 f42b1b34 2018-03-12 stsp if (got_object_get_type(obj) == GOT_OBJ_TYPE_COMMIT) {
198 f42b1b34 2018-03-12 stsp error = print_commit_object(obj, id, repo);
199 f42b1b34 2018-03-12 stsp if (error)
200 f42b1b34 2018-03-12 stsp return 1;
201 f42b1b34 2018-03-12 stsp } else
202 f42b1b34 2018-03-12 stsp return 1;
203 f42b1b34 2018-03-12 stsp got_object_close(obj);
204 f42b1b34 2018-03-12 stsp free(id);
205 f42b1b34 2018-03-12 stsp got_ref_close(head_ref);
206 f42b1b34 2018-03-12 stsp got_repo_close(repo);
207 5c860e29 2018-03-12 stsp return 0;
208 5c860e29 2018-03-12 stsp }
209 5c860e29 2018-03-12 stsp
210 f42b1b34 2018-03-12 stsp #ifdef notyet
211 5c860e29 2018-03-12 stsp int
212 5c860e29 2018-03-12 stsp cmd_status(int argc __unused, char *argv[] __unused)
213 5c860e29 2018-03-12 stsp {
214 5c860e29 2018-03-12 stsp git_repository *repo = NULL;
215 5c860e29 2018-03-12 stsp git_status_list *status;
216 5c860e29 2018-03-12 stsp git_status_options statusopts;
217 5c860e29 2018-03-12 stsp size_t i;
218 5c860e29 2018-03-12 stsp
219 5c860e29 2018-03-12 stsp git_libgit2_init();
220 5c860e29 2018-03-12 stsp
221 5c860e29 2018-03-12 stsp if (git_repository_open_ext(&repo, ".", 0, NULL))
222 5c860e29 2018-03-12 stsp errx(1, "git_repository_open: %s", giterr_last()->message);
223 5c860e29 2018-03-12 stsp
224 5c860e29 2018-03-12 stsp if (git_repository_is_bare(repo))
225 5c860e29 2018-03-12 stsp errx(1, "bar repository");
226 5c860e29 2018-03-12 stsp
227 5c860e29 2018-03-12 stsp if (git_status_init_options(&statusopts, GIT_STATUS_OPTIONS_VERSION))
228 5c860e29 2018-03-12 stsp errx(1, "git_status_init_options: %s", giterr_last()->message);
229 5c860e29 2018-03-12 stsp
230 5c860e29 2018-03-12 stsp statusopts.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
231 5c860e29 2018-03-12 stsp statusopts.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED |
232 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_RENAMES_HEAD_TO_INDEX |
233 5c860e29 2018-03-12 stsp GIT_STATUS_OPT_SORT_CASE_SENSITIVELY;
234 5c860e29 2018-03-12 stsp
235 5c860e29 2018-03-12 stsp if (git_status_list_new(&status, repo, &statusopts))
236 5c860e29 2018-03-12 stsp errx(1, "git_status_list_new: %s", giterr_last()->message);
237 5c860e29 2018-03-12 stsp
238 5c860e29 2018-03-12 stsp for (i = 0; i < git_status_list_entrycount(status); i++) {
239 5c860e29 2018-03-12 stsp const git_status_entry *se;
240 5c860e29 2018-03-12 stsp
241 5c860e29 2018-03-12 stsp se = git_status_byindex(status, i);
242 5c860e29 2018-03-12 stsp switch (se->status) {
243 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_NEW:
244 5c860e29 2018-03-12 stsp printf("? %s\n", se->index_to_workdir->new_file.path);
245 5c860e29 2018-03-12 stsp break;
246 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_MODIFIED:
247 5c860e29 2018-03-12 stsp printf("M %s\n", se->index_to_workdir->new_file.path);
248 5c860e29 2018-03-12 stsp break;
249 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_DELETED:
250 5c860e29 2018-03-12 stsp printf("R %s\n", se->index_to_workdir->new_file.path);
251 5c860e29 2018-03-12 stsp break;
252 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_RENAMED:
253 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
254 5c860e29 2018-03-12 stsp se->index_to_workdir->old_file.path,
255 5c860e29 2018-03-12 stsp se->index_to_workdir->new_file.path);
256 5c860e29 2018-03-12 stsp break;
257 5c860e29 2018-03-12 stsp case GIT_STATUS_WT_TYPECHANGE:
258 5c860e29 2018-03-12 stsp printf("t %s\n", se->index_to_workdir->new_file.path);
259 5c860e29 2018-03-12 stsp break;
260 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_NEW:
261 5c860e29 2018-03-12 stsp printf("A %s\n", se->head_to_index->new_file.path);
262 5c860e29 2018-03-12 stsp break;
263 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_MODIFIED:
264 5c860e29 2018-03-12 stsp printf("M %s\n", se->head_to_index->old_file.path);
265 5c860e29 2018-03-12 stsp break;
266 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_DELETED:
267 5c860e29 2018-03-12 stsp printf("R %s\n", se->head_to_index->old_file.path);
268 5c860e29 2018-03-12 stsp break;
269 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_RENAMED:
270 5c860e29 2018-03-12 stsp printf("m %s -> %s\n",
271 5c860e29 2018-03-12 stsp se->head_to_index->old_file.path,
272 5c860e29 2018-03-12 stsp se->head_to_index->new_file.path);
273 5c860e29 2018-03-12 stsp break;
274 5c860e29 2018-03-12 stsp case GIT_STATUS_INDEX_TYPECHANGE:
275 5c860e29 2018-03-12 stsp printf("t %s\n", se->head_to_index->old_file.path);
276 5c860e29 2018-03-12 stsp break;
277 5c860e29 2018-03-12 stsp case GIT_STATUS_CURRENT:
278 5c860e29 2018-03-12 stsp default:
279 5c860e29 2018-03-12 stsp break;
280 5c860e29 2018-03-12 stsp }
281 5c860e29 2018-03-12 stsp }
282 5c860e29 2018-03-12 stsp
283 5c860e29 2018-03-12 stsp git_status_list_free(status);
284 5c860e29 2018-03-12 stsp git_repository_free(repo);
285 5c860e29 2018-03-12 stsp git_libgit2_shutdown();
286 5c860e29 2018-03-12 stsp
287 5c860e29 2018-03-12 stsp return 0;
288 5c860e29 2018-03-12 stsp }
289 f42b1b34 2018-03-12 stsp #endif