Blame examples/showindex.c

Packit Service 20376f
/*
Packit Service 20376f
 * libgit2 "showindex" example - shows how to extract data from the index
Packit Service 20376f
 *
Packit Service 20376f
 * Written by the libgit2 contributors
Packit Service 20376f
 *
Packit Service 20376f
 * To the extent possible under law, the author(s) have dedicated all copyright
Packit Service 20376f
 * and related and neighboring rights to this software to the public domain
Packit Service 20376f
 * worldwide. This software is distributed without any warranty.
Packit Service 20376f
 *
Packit Service 20376f
 * You should have received a copy of the CC0 Public Domain Dedication along
Packit Service 20376f
 * with this software. If not, see
Packit Service 20376f
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "common.h"
Packit Service 20376f
Packit Service 20376f
int main (int argc, char** argv)
Packit Service 20376f
{
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	unsigned int i, ecount;
Packit Service 20376f
	char *dir = ".";
Packit Service 20376f
	size_t dirlen;
Packit Service 20376f
	char out[GIT_OID_HEXSZ+1];
Packit Service 20376f
	out[GIT_OID_HEXSZ] = '\0';
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	if (argc > 2)
Packit Service 20376f
		fatal("usage: showindex [<repo-dir>]", NULL);
Packit Service 20376f
	if (argc > 1)
Packit Service 20376f
		dir = argv[1];
Packit Service 20376f
Packit Service 20376f
	dirlen = strlen(dir);
Packit Service 20376f
	if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
Packit Service 20376f
		check_lg2(git_index_open(&index, dir), "could not open index", dir);
Packit Service 20376f
	} else {
Packit Service 20376f
		git_repository *repo;
Packit Service 20376f
		check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
Packit Service 20376f
		check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);
Packit Service 20376f
		git_repository_free(repo);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_index_read(index, 0);
Packit Service 20376f
Packit Service 20376f
	ecount = git_index_entrycount(index);
Packit Service 20376f
	if (!ecount)
Packit Service 20376f
		printf("Empty index\n");
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < ecount; ++i) {
Packit Service 20376f
		const git_index_entry *e = git_index_get_byindex(index, i);
Packit Service 20376f
Packit Service 20376f
		git_oid_fmt(out, &e->id);
Packit Service 20376f
Packit Service 20376f
		printf("File Path: %s\n", e->path);
Packit Service 20376f
		printf("    Stage: %d\n", git_index_entry_stage(e));
Packit Service 20376f
		printf(" Blob SHA: %s\n", out);
Packit Service 20376f
		printf("File Mode: %07o\n", e->mode);
Packit Service 20376f
		printf("File Size: %d bytes\n", (int)e->file_size);
Packit Service 20376f
		printf("Dev/Inode: %d/%d\n", (int)e->dev, (int)e->ino);
Packit Service 20376f
		printf("  UID/GID: %d/%d\n", (int)e->uid, (int)e->gid);
Packit Service 20376f
		printf("    ctime: %d\n", (int)e->ctime.seconds);
Packit Service 20376f
		printf("    mtime: %d\n", (int)e->mtime.seconds);
Packit Service 20376f
		printf("\n");
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_index_free(index);
Packit Service 20376f
	git_libgit2_shutdown();
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}