add.c 3.52 KB
Newer Older
Ben Straub committed
1
/*
2
 * libgit2 "add" example - shows how to modify the index
Ben Straub committed
3
 *
4 5 6 7 8 9 10 11 12
 * Written by the libgit2 contributors
 *
 * To the extent possible under law, the author(s) have dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * You should have received a copy of the CC0 Public Domain Dedication along
 * with this software. If not, see
 * <http://creativecommons.org/publicdomain/zero/1.0/>.
Ben Straub committed
13 14 15
 */

#include "common.h"
Krzysztof Adamski committed
16 17 18 19 20
#include <assert.h>

enum print_options {
	SKIP = 1,
	VERBOSE = 2,
21
	UPDATE = 4,
Krzysztof Adamski committed
22 23 24 25 26 27 28
};

struct print_payload {
	enum print_options options;
	git_repository *repo;
};

29 30 31 32 33 34
/* Forward declarations for helpers */
static void parse_opts(int *options, int *count, int argc, char *argv[]);
void init_array(git_strarray *array, int argc, char **argv);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);

int main (int argc, char** argv)
Krzysztof Adamski committed
35
{
36 37 38 39 40 41
	git_index_matched_path_cb matched_cb = NULL;
	git_repository *repo = NULL;
	git_index *index;
	git_strarray array = {0};
	int options = 0, count = 0;
	struct print_payload payload = {0};
Krzysztof Adamski committed
42

43
	git_threads_init();
Krzysztof Adamski committed
44

45 46 47 48 49 50 51 52 53
	parse_opts(&options, &count, argc, argv);

	init_array(&array, argc-count, argv+count);

	check_lg2(git_repository_open(&repo, "."), "No git repository", NULL);
	check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);

	if (options&VERBOSE || options&SKIP) {
		matched_cb = &print_matched_cb;
Krzysztof Adamski committed
54 55
	}

56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
	payload.options = options;
	payload.repo = repo;

	if (options&UPDATE) {
		git_index_update_all(index, &array, matched_cb, &payload);
	} else {
		git_index_add_all(index, &array, 0, matched_cb, &payload);
	}

	git_index_write(index);
	git_index_free(index);
	git_repository_free(repo);

	git_threads_shutdown();

	return 0;
Krzysztof Adamski committed
72 73 74 75 76 77 78
}

int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
{
	struct print_payload p = *(struct print_payload*)(payload);
	int ret;
	git_status_t status;
79
	(void)matched_pathspec;
Krzysztof Adamski committed
80

81
	if (git_status_file((unsigned int*)(&status), p.repo, path)) {
Krzysztof Adamski committed
82 83 84 85 86
		return -1; //abort
	}

	if (status & GIT_STATUS_WT_MODIFIED ||
	         status & GIT_STATUS_WT_NEW) {
87
		printf("add '%s'\n", path);
Krzysztof Adamski committed
88 89 90 91 92 93 94 95 96 97 98 99
		ret = 0;
	} else {
		ret = 1;
	}

	if(p.options & SKIP) {
		ret = 1;
	}

	return ret;
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
void init_array(git_strarray *array, int argc, char **argv)
{
	unsigned int i;

	array->count = argc;
	array->strings = malloc(sizeof(char*) * array->count);
	assert(array->strings!=NULL);

	for(i=0; i<array->count; i++) {
		array->strings[i]=argv[i];
	}

	return;
}

115 116 117 118 119 120
void print_usage(void)
{
	fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
	fprintf(stderr, "\t-n, --dry-run    dry run\n");
	fprintf(stderr, "\t-v, --verbose    be verbose\n");
	fprintf(stderr, "\t-u, --update     update tracked files\n");
121
	exit(1);
122 123
}

124
static void parse_opts(int *options, int *count, int argc, char *argv[])
Krzysztof Adamski committed
125
{
126
	int i;
Krzysztof Adamski committed
127 128 129 130 131 132

	for (i = 1; i < argc; ++i) {
		if (argv[i][0] != '-') {
			break;
		}
		else if(!strcmp(argv[i], "--verbose") || !strcmp(argv[i], "-v")) {
133
			*options |= VERBOSE;
Krzysztof Adamski committed
134 135
		}
		else if(!strcmp(argv[i], "--dry-run") || !strcmp(argv[i], "-n")) {
136
			*options |= SKIP;
Krzysztof Adamski committed
137
		}
138
		else if(!strcmp(argv[i], "--update") || !strcmp(argv[i], "-u")) {
139
			*options |= UPDATE;
140
		}
141 142 143 144
		else if(!strcmp(argv[i], "-h")) {
			print_usage();
			break;
		}
Krzysztof Adamski committed
145
		else if(!strcmp(argv[i], "--")) {
146
			i++;
Krzysztof Adamski committed
147 148 149 150
			break;
		}
		else {
			fprintf(stderr, "Unsupported option %s.\n", argv[i]);
151
			print_usage();
Krzysztof Adamski committed
152 153 154
		}
	}

155
	if (argc<=i)
156
		print_usage();
Krzysztof Adamski committed
157

158
	*count = i;
Krzysztof Adamski committed
159
}