void traverse_commit_list(struct rev_info *revs, void (*show_commit)(struct commit *), void (*show_object)(struct object_array_entry *)) { int i; struct commit *commit; struct object_array objects = { 0, 0, NULL }; while ((commit = get_revision(revs)) != NULL) { process_tree(revs, commit->tree, &objects, NULL, ""); show_commit(commit); } for (i = 0; i < revs->pending.nr; i++) { struct object_array_entry *pending = revs->pending.objects + i; struct object *obj = pending->item; const char *name = pending->name; if (obj->flags & (UNINTERESTING | SEEN)) continue; if (obj->type == OBJ_TAG) { obj->flags |= SEEN; add_object_array(obj, name, &objects); continue; } if (obj->type == OBJ_TREE) { process_tree(revs, (struct tree *)obj, &objects, NULL, name); continue; } if (obj->type == OBJ_BLOB) { process_blob(revs, (struct blob *)obj, &objects, NULL, name); continue; } die("unknown pending object %s (%s)", sha1_to_hex(obj->sha1), name); } for (i = 0; i < objects.nr; i++) show_object(&objects.objects[i]); free(objects.objects); if (revs->pending.nr) { free(revs->pending.objects); revs->pending.nr = 0; revs->pending.alloc = 0; revs->pending.objects = NULL; } }
/** Entry point for this command */ int main(int argc, char *argv[]) { git_repository *repo; struct opts o = { ".", NULL, 0, 0 }; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_libgit2_init(); parse_opts(&o, argc, argv); check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL), "Could not open repository", NULL); check_lg2(git_revparse_single(&obj, repo, o.rev), "Could not resolve", o.rev); if (o.verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (o.action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL); check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj", NULL); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(repo); git_libgit2_shutdown(); return 0; }
int main(int argc, char *argv[]) { const char *dir = ".", *rev = NULL; int i, action = 0, verbose = 0; git_object *obj = NULL; char oidstr[GIT_OID_HEXSZ + 1]; git_threads_init(); for (i = 1; i < argc; ++i) { char *a = argv[i]; if (a[0] != '-') { if (rev != NULL) usage("Only one rev should be provided", NULL); else rev = a; } else if (!strcmp(a, "-t")) action = SHOW_TYPE; else if (!strcmp(a, "-s")) action = SHOW_SIZE; else if (!strcmp(a, "-e")) action = SHOW_NONE; else if (!strcmp(a, "-p")) action = SHOW_PRETTY; else if (!strcmp(a, "-q")) verbose = 0; else if (!strcmp(a, "-v")) verbose = 1; else if (!strcmp(a, "--help") || !strcmp(a, "-h")) usage(NULL, NULL); else if (!check_str_param(a, "--git-dir=", &dir)) usage("Unknown option", a); } if (!action || !rev) usage(NULL, NULL); check(git_repository_open_ext(&g_repo, dir, 0, NULL), "Could not open repository"); if (git_revparse_single(&obj, g_repo, rev) < 0) { fprintf(stderr, "Could not resolve '%s'\n", rev); exit(1); } if (verbose) { char oidstr[GIT_OID_HEXSZ + 1]; git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj)); printf("%s %s\n--\n", git_object_type2string(git_object_type(obj)), oidstr); } switch (action) { case SHOW_TYPE: printf("%s\n", git_object_type2string(git_object_type(obj))); break; case SHOW_SIZE: { git_odb *odb; git_odb_object *odbobj; check(git_repository_odb(&odb, g_repo), "Could not open ODB"); check(git_odb_read(&odbobj, odb, git_object_id(obj)), "Could not find obj"); printf("%ld\n", (long)git_odb_object_size(odbobj)); git_odb_object_free(odbobj); git_odb_free(odb); } break; case SHOW_NONE: /* just want return result */ break; case SHOW_PRETTY: switch (git_object_type(obj)) { case GIT_OBJ_BLOB: show_blob((const git_blob *)obj); break; case GIT_OBJ_COMMIT: show_commit((const git_commit *)obj); break; case GIT_OBJ_TREE: show_tree((const git_tree *)obj); break; case GIT_OBJ_TAG: show_tag((const git_tag *)obj); break; default: printf("unknown %s\n", oidstr); break; } break; } git_object_free(obj); git_repository_free(g_repo); git_threads_shutdown(); return 0; }