summaryrefslogtreecommitdiff
path: root/examples/add.c
blob: 542360ea9bfe6ec51182d6e70d123b5a8efbe44d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * libgit2 "add" example - shows how to modify the index
 *
 * 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/>.
 */

#include "common.h"

/**
 * The following example demonstrates how to add files with libgit2.
 *
 * It will use the repository in the current working directory, and act
 * on files passed as its parameters.
 *
 * Recognized options are:
 *   -v/--verbose: show the file's status after acting on it.
 *   -n/--dry-run: do not actually change the index.
 *   -u/--update: update the index instead of adding to it.
 */

enum index_mode {
	INDEX_NONE,
	INDEX_ADD,
};

struct index_options {
	int dry_run;
	int verbose;
	git_repository *repo;
	enum index_mode mode;
	int add_update;
};

/* Forward declarations for helpers */
static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args);
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);

int lg2_add(git_repository *repo, int argc, char **argv)
{
	git_index_matched_path_cb matched_cb = NULL;
	git_index *index;
	git_strarray array = {0};
	struct index_options options = {0};
	struct args_info args = ARGS_INFO_INIT;

	options.mode = INDEX_ADD;

	/* Parse the options & arguments. */
	parse_opts(NULL, &options, &args);
	strarray_from_args(&array, &args);

	/* Grab the repository's index. */
	check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);

	/* Setup a callback if the requested options need it */
	if (options.verbose || options.dry_run) {
		matched_cb = &print_matched_cb;
	}

	options.repo = repo;

	/* Perform the requested action with the index and files */
	if (options.add_update) {
		git_index_update_all(index, &array, matched_cb, &options);
	} else {
		git_index_add_all(index, &array, 0, matched_cb, &options);
	}

	/* Cleanup memory */
	git_index_write(index);
	git_index_free(index);

	return 0;
}

/*
 * This callback is called for each file under consideration by
 * git_index_(update|add)_all above.
 * It makes uses of the callback's ability to abort the action.
 */
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
{
	struct index_options *opts = (struct index_options *)(payload);
	int ret;
	unsigned status;
	(void)matched_pathspec;

	/* Get the file status */
	if (git_status_file(&status, opts->repo, path) < 0)
		return -1;

	if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
		printf("add '%s'\n", path);
		ret = 0;
	} else {
		ret = 1;
	}

	if (opts->dry_run)
		ret = 1;

	return ret;
}

void init_array(git_strarray *array, int argc, char **argv)
{
	unsigned int i;

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

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

	return;
}

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");
	exit(1);
}

static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
{
	if (args->argc <= 1)
		print_usage();

	for (args->pos = 1; args->pos < args->argc; ++args->pos) {
		const char *curr = args->argv[args->pos];

		if (curr[0] != '-') {
			if (!strcmp("add", curr)) {
				opts->mode = INDEX_ADD;
				continue;
			} else if (opts->mode == INDEX_NONE) {
				fprintf(stderr, "missing command: %s", curr);
				print_usage();
				break;
			} else {
				/* We might be looking at a filename */
				break;
			}
		} else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
				   match_bool_arg(&opts->dry_run, args, "--dry-run") ||
				   match_str_arg(repo_path, args, "--git-dir") ||
				   (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) {
			continue;
		} else if (match_bool_arg(NULL, args, "--help")) {
			print_usage();
			break;
		} else if (match_arg_separator(args)) {
			break;
		} else {
			fprintf(stderr, "Unsupported option %s.\n", curr);
			print_usage();
		}
	}
}