showindex.c 1.6 KB
Newer Older
Scott Chacon committed
1 2
#include <git2.h>
#include <stdio.h>
3
#include <string.h>
Scott Chacon committed
4 5 6

int main (int argc, char** argv)
{
7
	git_repository *repo = NULL;
8 9 10
	git_index *index;
	unsigned int i, ecount;
	char *dir = ".";
11
	size_t dirlen;
12 13 14
	char out[41];
	out[40] = '\0';

15 16
	git_threads_init();

17 18
	if (argc > 1)
		dir = argv[1];
19
	if (!dir || argc > 2) {
20 21 22 23
		fprintf(stderr, "usage: showindex [<repo-dir>]\n");
		return 1;
	}

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	dirlen = strlen(dir);
	if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
		if (git_index_open(&index, dir) < 0) {
			fprintf(stderr, "could not open index: %s\n", dir);
			return 1;
		}
	} else {
		if (git_repository_open_ext(&repo, dir, 0, NULL) < 0) {
			fprintf(stderr, "could not open repository: %s\n", dir);
			return 1;
		}
		if (git_repository_index(&index, repo) < 0) {
			fprintf(stderr, "could not open repository index\n");
			return 1;
		}
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
	}

	git_index_read(index);

	ecount = git_index_entrycount(index);
	if (!ecount)
		printf("Empty index\n");

	for (i = 0; i < ecount; ++i) {
		const git_index_entry *e = git_index_get_byindex(index, i);

		git_oid_fmt(out, &e->oid);

		printf("File Path: %s\n", e->path);
		printf("    Stage: %d\n", git_index_entry_stage(e));
		printf(" Blob SHA: %s\n", out);
55 56 57 58
		printf("File Mode: %07o\n", e->mode);
		printf("File Size: %d bytes\n", (int)e->file_size);
		printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
		printf("  UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
59 60 61 62 63 64 65 66
		printf("    ctime: %d\n", (int)e->ctime.seconds);
		printf("    mtime: %d\n", (int)e->mtime.seconds);
		printf("\n");
	}

	git_index_free(index);
	git_repository_free(repo);

67 68
	git_threads_shutdown();

69
	return 0;
Scott Chacon committed
70 71
}