Blame examples/cat-file.c

Packit Service 20376f
/*
Packit Service 20376f
 * libgit2 "cat-file" example - shows how to print data from the ODB
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
static void print_signature(const char *header, const git_signature *sig)
Packit Service 20376f
{
Packit Service 20376f
	char sign;
Packit Service 20376f
	int offset, hours, minutes;
Packit Service 20376f
Packit Service 20376f
	if (!sig)
Packit Service 20376f
		return;
Packit Service 20376f
Packit Service 20376f
	offset = sig->when.offset;
Packit Service 20376f
	if (offset < 0) {
Packit Service 20376f
		sign = '-';
Packit Service 20376f
		offset = -offset;
Packit Service 20376f
	} else {
Packit Service 20376f
		sign = '+';
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	hours   = offset / 60;
Packit Service 20376f
	minutes = offset % 60;
Packit Service 20376f
Packit Service 20376f
	printf("%s %s <%s> %ld %c%02d%02d\n",
Packit Service 20376f
		   header, sig->name, sig->email, (long)sig->when.time,
Packit Service 20376f
		   sign, hours, minutes);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Printing out a blob is simple, get the contents and print */
Packit Service 20376f
static void show_blob(const git_blob *blob)
Packit Service 20376f
{
Packit Service 20376f
	/* ? Does this need crlf filtering? */
Packit Service 20376f
	fwrite(git_blob_rawcontent(blob), (size_t)git_blob_rawsize(blob), 1, stdout);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Show each entry with its type, id and attributes */
Packit Service 20376f
static void show_tree(const git_tree *tree)
Packit Service 20376f
{
Packit Service 20376f
	size_t i, max_i = (int)git_tree_entrycount(tree);
Packit Service 20376f
	char oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
	const git_tree_entry *te;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < max_i; ++i) {
Packit Service 20376f
		te = git_tree_entry_byindex(tree, i);
Packit Service 20376f
Packit Service 20376f
		git_oid_tostr(oidstr, sizeof(oidstr), git_tree_entry_id(te));
Packit Service 20376f
Packit Service 20376f
		printf("%06o %s %s\t%s\n",
Packit Service 20376f
			git_tree_entry_filemode(te),
Packit Service 20376f
			git_object_type2string(git_tree_entry_type(te)),
Packit Service 20376f
			oidstr, git_tree_entry_name(te));
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/**
Packit Service 20376f
 * Commits and tags have a few interesting fields in their header.
Packit Service 20376f
 */
Packit Service 20376f
static void show_commit(const git_commit *commit)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i, max_i;
Packit Service 20376f
	char oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
Packit Service 20376f
	git_oid_tostr(oidstr, sizeof(oidstr), git_commit_tree_id(commit));
Packit Service 20376f
	printf("tree %s\n", oidstr);
Packit Service 20376f
Packit Service 20376f
	max_i = (unsigned int)git_commit_parentcount(commit);
Packit Service 20376f
	for (i = 0; i < max_i; ++i) {
Packit Service 20376f
		git_oid_tostr(oidstr, sizeof(oidstr), git_commit_parent_id(commit, i));
Packit Service 20376f
		printf("parent %s\n", oidstr);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	print_signature("author", git_commit_author(commit));
Packit Service 20376f
	print_signature("committer", git_commit_committer(commit));
Packit Service 20376f
Packit Service 20376f
	if (git_commit_message(commit))
Packit Service 20376f
		printf("\n%s\n", git_commit_message(commit));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void show_tag(const git_tag *tag)
Packit Service 20376f
{
Packit Service 20376f
	char oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
Packit Service 20376f
	git_oid_tostr(oidstr, sizeof(oidstr), git_tag_target_id(tag));;
Packit Service 20376f
	printf("object %s\n", oidstr);
Packit Service 20376f
	printf("type %s\n", git_object_type2string(git_tag_target_type(tag)));
Packit Service 20376f
	printf("tag %s\n", git_tag_name(tag));
Packit Service 20376f
	print_signature("tagger", git_tag_tagger(tag));
Packit Service 20376f
Packit Service 20376f
	if (git_tag_message(tag))
Packit Service 20376f
		printf("\n%s\n", git_tag_message(tag));
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
enum {
Packit Service 20376f
	SHOW_TYPE = 1,
Packit Service 20376f
	SHOW_SIZE = 2,
Packit Service 20376f
	SHOW_NONE = 3,
Packit Service 20376f
	SHOW_PRETTY = 4
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* Forward declarations for option-parsing helper */
Packit Service 20376f
struct opts {
Packit Service 20376f
	const char *dir;
Packit Service 20376f
	const char *rev;
Packit Service 20376f
	int action;
Packit Service 20376f
	int verbose;
Packit Service 20376f
};
Packit Service 20376f
static void parse_opts(struct opts *o, int argc, char *argv[]);
Packit Service 20376f
Packit Service 20376f
Packit Service 20376f
/** Entry point for this command */
Packit Service 20376f
int main(int argc, char *argv[])
Packit Service 20376f
{
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
	struct opts o = { ".", NULL, 0, 0 };
Packit Service 20376f
	git_object *obj = NULL;
Packit Service 20376f
	char oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	parse_opts(&o, argc, argv);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_repository_open_ext(&repo, o.dir, 0, NULL),
Packit Service 20376f
			"Could not open repository", NULL);
Packit Service 20376f
	check_lg2(git_revparse_single(&obj, repo, o.rev),
Packit Service 20376f
			"Could not resolve", o.rev);
Packit Service 20376f
Packit Service 20376f
	if (o.verbose) {
Packit Service 20376f
		char oidstr[GIT_OID_HEXSZ + 1];
Packit Service 20376f
		git_oid_tostr(oidstr, sizeof(oidstr), git_object_id(obj));
Packit Service 20376f
Packit Service 20376f
		printf("%s %s\n--\n",
Packit Service 20376f
			git_object_type2string(git_object_type(obj)), oidstr);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	switch (o.action) {
Packit Service 20376f
	case SHOW_TYPE:
Packit Service 20376f
		printf("%s\n", git_object_type2string(git_object_type(obj)));
Packit Service 20376f
		break;
Packit Service 20376f
	case SHOW_SIZE: {
Packit Service 20376f
		git_odb *odb;
Packit Service 20376f
		git_odb_object *odbobj;
Packit Service 20376f
Packit Service 20376f
		check_lg2(git_repository_odb(&odb, repo), "Could not open ODB", NULL);
Packit Service 20376f
		check_lg2(git_odb_read(&odbobj, odb, git_object_id(obj)),
Packit Service 20376f
			"Could not find obj", NULL);
Packit Service 20376f
Packit Service 20376f
		printf("%ld\n", (long)git_odb_object_size(odbobj));
Packit Service 20376f
Packit Service 20376f
		git_odb_object_free(odbobj);
Packit Service 20376f
		git_odb_free(odb);
Packit Service 20376f
		}
Packit Service 20376f
		break;
Packit Service 20376f
	case SHOW_NONE:
Packit Service 20376f
		/* just want return result */
Packit Service 20376f
		break;
Packit Service 20376f
	case SHOW_PRETTY:
Packit Service 20376f
Packit Service 20376f
		switch (git_object_type(obj)) {
Packit Service 20376f
		case GIT_OBJ_BLOB:
Packit Service 20376f
			show_blob((const git_blob *)obj);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_OBJ_COMMIT:
Packit Service 20376f
			show_commit((const git_commit *)obj);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_OBJ_TREE:
Packit Service 20376f
			show_tree((const git_tree *)obj);
Packit Service 20376f
			break;
Packit Service 20376f
		case GIT_OBJ_TAG:
Packit Service 20376f
			show_tag((const git_tag *)obj);
Packit Service 20376f
			break;
Packit Service 20376f
		default:
Packit Service 20376f
			printf("unknown %s\n", oidstr);
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		break;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_object_free(obj);
Packit Service 20376f
	git_repository_free(repo);
Packit Service 20376f
Packit Service 20376f
	git_libgit2_shutdown();
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Print out usage information */
Packit Service 20376f
static void usage(const char *message, const char *arg)
Packit Service 20376f
{
Packit Service 20376f
	if (message && arg)
Packit Service 20376f
		fprintf(stderr, "%s: %s\n", message, arg);
Packit Service 20376f
	else if (message)
Packit Service 20376f
		fprintf(stderr, "%s\n", message);
Packit Service 20376f
	fprintf(stderr,
Packit Service 20376f
			"usage: cat-file (-t | -s | -e | -p) [-v] [-q] "
Packit Service 20376f
			"[-h|--help] [--git-dir=<dir>] <object>\n");
Packit Service 20376f
	exit(1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
/** Parse the command-line options taken from git */
Packit Service 20376f
static void parse_opts(struct opts *o, int argc, char *argv[])
Packit Service 20376f
{
Packit Service 20376f
	struct args_info args = ARGS_INFO_INIT;
Packit Service 20376f
Packit Service 20376f
	for (args.pos = 1; args.pos < argc; ++args.pos) {
Packit Service 20376f
		char *a = argv[args.pos];
Packit Service 20376f
Packit Service 20376f
		if (a[0] != '-') {
Packit Service 20376f
			if (o->rev != NULL)
Packit Service 20376f
				usage("Only one rev should be provided", NULL);
Packit Service 20376f
			else
Packit Service 20376f
				o->rev = a;
Packit Service 20376f
		}
Packit Service 20376f
		else if (!strcmp(a, "-t"))
Packit Service 20376f
			o->action = SHOW_TYPE;
Packit Service 20376f
		else if (!strcmp(a, "-s"))
Packit Service 20376f
			o->action = SHOW_SIZE;
Packit Service 20376f
		else if (!strcmp(a, "-e"))
Packit Service 20376f
			o->action = SHOW_NONE;
Packit Service 20376f
		else if (!strcmp(a, "-p"))
Packit Service 20376f
			o->action = SHOW_PRETTY;
Packit Service 20376f
		else if (!strcmp(a, "-q"))
Packit Service 20376f
			o->verbose = 0;
Packit Service 20376f
		else if (!strcmp(a, "-v"))
Packit Service 20376f
			o->verbose = 1;
Packit Service 20376f
		else if (!strcmp(a, "--help") || !strcmp(a, "-h"))
Packit Service 20376f
			usage(NULL, NULL);
Packit Service 20376f
		else if (!match_str_arg(&o->dir, &args, "--git-dir"))
Packit Service 20376f
			usage("Unknown option", a);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (!o->action || !o->rev)
Packit Service 20376f
		usage(NULL, NULL);
Packit Service 20376f
Packit Service 20376f
}