Blame examples/add.c

Packit Service 20376f
/*
Packit Service 20376f
 * libgit2 "add" example - shows how to modify 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
#include <assert.h>
Packit Service 20376f
Packit Service 20376f
enum print_options {
Packit Service 20376f
	SKIP = 1,
Packit Service 20376f
	VERBOSE = 2,
Packit Service 20376f
	UPDATE = 4,
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
struct print_payload {
Packit Service 20376f
	enum print_options options;
Packit Service 20376f
	git_repository *repo;
Packit Service 20376f
};
Packit Service 20376f
Packit Service 20376f
/* Forward declarations for helpers */
Packit Service 20376f
static void parse_opts(int *options, int *count, int argc, char *argv[]);
Packit Service 20376f
void init_array(git_strarray *array, int argc, char **argv);
Packit Service 20376f
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
Packit Service 20376f
Packit Service 20376f
int main (int argc, char** argv)
Packit Service 20376f
{
Packit Service 20376f
	git_index_matched_path_cb matched_cb = NULL;
Packit Service 20376f
	git_repository *repo = NULL;
Packit Service 20376f
	git_index *index;
Packit Service 20376f
	git_strarray array = {0};
Packit Service 20376f
	int options = 0, count = 0;
Packit Service 20376f
	struct print_payload payload = {0};
Packit Service 20376f
Packit Service 20376f
	git_libgit2_init();
Packit Service 20376f
Packit Service 20376f
	parse_opts(&options, &count, argc, argv);
Packit Service 20376f
Packit Service 20376f
	init_array(&array, argc-count, argv+count);
Packit Service 20376f
Packit Service 20376f
	check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
Packit Service 20376f
	check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
Packit Service 20376f
Packit Service 20376f
	if (options&VERBOSE || options&SKIP) {
Packit Service 20376f
		matched_cb = &print_matched_cb;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	payload.options = options;
Packit Service 20376f
	payload.repo = repo;
Packit Service 20376f
Packit Service 20376f
	if (options&UPDATE) {
Packit Service 20376f
		git_index_update_all(index, &array, matched_cb, &payload);
Packit Service 20376f
	} else {
Packit Service 20376f
		git_index_add_all(index, &array, 0, matched_cb, &payload);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	git_index_write(index);
Packit Service 20376f
	git_index_free(index);
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
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
Packit Service 20376f
{
Packit Service 20376f
	struct print_payload p = *(struct print_payload*)(payload);
Packit Service 20376f
	int ret;
Packit Service 20376f
	unsigned status;
Packit Service 20376f
	(void)matched_pathspec;
Packit Service 20376f
Packit Service 20376f
	if (git_status_file(&status, p.repo, path)) {
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (status & GIT_STATUS_WT_MODIFIED || status & GIT_STATUS_WT_NEW) {
Packit Service 20376f
		printf("add '%s'\n", path);
Packit Service 20376f
		ret = 0;
Packit Service 20376f
	} else {
Packit Service 20376f
		ret = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if(p.options & SKIP) {
Packit Service 20376f
		ret = 1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return ret;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void init_array(git_strarray *array, int argc, char **argv)
Packit Service 20376f
{
Packit Service 20376f
	unsigned int i;
Packit Service 20376f
Packit Service 20376f
	array->count = argc;
Packit Service 20376f
	array->strings = malloc(sizeof(char*) * array->count);
Packit Service 20376f
	assert(array->strings!=NULL);
Packit Service 20376f
Packit Service 20376f
	for(i=0; i<array->count; i++) {
Packit Service 20376f
		array->strings[i]=argv[i];
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void print_usage(void)
Packit Service 20376f
{
Packit Service 20376f
	fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
Packit Service 20376f
	fprintf(stderr, "\t-n, --dry-run    dry run\n");
Packit Service 20376f
	fprintf(stderr, "\t-v, --verbose    be verbose\n");
Packit Service 20376f
	fprintf(stderr, "\t-u, --update     update tracked files\n");
Packit Service 20376f
	exit(1);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void parse_opts(int *options, int *count, int argc, char *argv[])
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
Packit Service 20376f
	for (i = 1; i < argc; ++i) {
Packit Service 20376f
		if (argv[i][0] != '-') {
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
Packit Service 20376f
			*options |= VERBOSE;
Packit Service 20376f
		}
Packit Service 20376f
		else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
Packit Service 20376f
			*options |= SKIP;
Packit Service 20376f
		}
Packit Service 20376f
		else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
Packit Service 20376f
			*options |= UPDATE;
Packit Service 20376f
		}
Packit Service 20376f
		else if(!strcmp(argv[i], "-h")) {
Packit Service 20376f
			print_usage();
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		else if(!strcmp(argv[i], "--")) {
Packit Service 20376f
			i++;
Packit Service 20376f
			break;
Packit Service 20376f
		}
Packit Service 20376f
		else {
Packit Service 20376f
			fprintf(stderr, "Unsupported option %s.\n", argv[i]);
Packit Service 20376f
			print_usage();
Packit Service 20376f
		}
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	if (argc<=i)
Packit Service 20376f
		print_usage();
Packit Service 20376f
Packit Service 20376f
	*count = i;
Packit Service 20376f
}