add.c 4.17 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 21 22 23 24 25 26 27 28
/**
 * 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.
 */

29 30
enum index_mode {
	INDEX_NONE,
31
	INDEX_ADD
Krzysztof Adamski committed
32 33
};

34 35 36
struct index_options {
	int dry_run;
	int verbose;
Krzysztof Adamski committed
37
	git_repository *repo;
38 39
	enum index_mode mode;
	int add_update;
Krzysztof Adamski committed
40 41
};

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

46
int lg2_add(git_repository *repo, int argc, char **argv)
Krzysztof Adamski committed
47
{
48 49 50
	git_index_matched_path_cb matched_cb = NULL;
	git_index *index;
	git_strarray array = {0};
51
	struct index_options options = {0};
52
	struct args_info args = ARGS_INFO_INIT;
Krzysztof Adamski committed
53

54 55
	options.mode = INDEX_ADD;

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

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

63
	/* Setup a callback if the requested options need it */
64
	if (options.verbose || options.dry_run) {
65
		matched_cb = &print_matched_cb;
Krzysztof Adamski committed
66 67
	}

68
	options.repo = repo;
69

70 71 72
	/* Perform the requested action with the index and files */
	if (options.add_update) {
		git_index_update_all(index, &array, matched_cb, &options);
73
	} else {
74
		git_index_add_all(index, &array, 0, matched_cb, &options);
75 76
	}

77
	/* Cleanup memory */
78 79 80 81
	git_index_write(index);
	git_index_free(index);

	return 0;
Krzysztof Adamski committed
82 83
}

84 85 86 87 88
/*
 * 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.
 */
Krzysztof Adamski committed
89 90
int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
{
91
	struct index_options *opts = (struct index_options *)(payload);
Krzysztof Adamski committed
92
	int ret;
93
	unsigned status;
94
	(void)matched_pathspec;
Krzysztof Adamski committed
95

96
	/* Get the file status */
97
	if (git_status_file(&status, opts->repo, path) < 0)
98
		return -1;
Krzysztof Adamski committed
99

100
	if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
101
		printf("add '%s'\n", path);
Krzysztof Adamski committed
102 103 104 105 106
		ret = 0;
	} else {
		ret = 1;
	}

107
	if (opts->dry_run)
Krzysztof Adamski committed
108 109 110 111 112
		ret = 1;

	return ret;
}

113
static void print_usage(void)
114 115 116 117 118
{
	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");
119
	exit(1);
120 121
}

122
static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
Krzysztof Adamski committed
123
{
124 125
	if (args->argc <= 1)
		print_usage();
Krzysztof Adamski committed
126

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

		if (curr[0] != '-') {
			if (!strcmp("add", curr)) {
132
				opts->mode = INDEX_ADD;
133
				continue;
134
			} else if (opts->mode == INDEX_NONE) {
135 136 137 138 139 140 141
				fprintf(stderr, "missing command: %s", curr);
				print_usage();
				break;
			} else {
				/* We might be looking at a filename */
				break;
			}
142 143
		} else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
				   match_bool_arg(&opts->dry_run, args, "--dry-run") ||
144
				   match_str_arg(repo_path, args, "--git-dir") ||
145
				   (opts->mode == INDEX_ADD && match_bool_arg(&opts->add_update, args, "--update"))) {
146 147
			continue;
		} else if (match_bool_arg(NULL, args, "--help")) {
148 149
			print_usage();
			break;
150
		} else if (match_arg_separator(args)) {
Krzysztof Adamski committed
151
			break;
152
		} else {
153
			fprintf(stderr, "Unsupported option %s.\n", curr);
154
			print_usage();
Krzysztof Adamski committed
155 156 157
		}
	}
}