]> git.proxmox.com Git - libgit2.git/blame - examples/add.c
New upstream version 1.4.3+dfsg.1
[libgit2.git] / examples / add.c
CommitLineData
dbfd2833 1/*
6cb831bd 2 * libgit2 "add" example - shows how to modify the index
dbfd2833 3 *
6cb831bd
BS
4 * Written by the libgit2 contributors
5 *
6 * To the extent possible under law, the author(s) have dedicated all copyright
7 * and related and neighboring rights to this software to the public domain
8 * worldwide. This software is distributed without any warranty.
9 *
10 * You should have received a copy of the CC0 Public Domain Dedication along
11 * with this software. If not, see
12 * <http://creativecommons.org/publicdomain/zero/1.0/>.
dbfd2833
BS
13 */
14
15#include "common.h"
62020aa8 16
22a2d3d5
UG
17/**
18 * The following example demonstrates how to add files with libgit2.
19 *
20 * It will use the repository in the current working directory, and act
21 * on files passed as its parameters.
22 *
23 * Recognized options are:
24 * -v/--verbose: show the file's status after acting on it.
25 * -n/--dry-run: do not actually change the index.
26 * -u/--update: update the index instead of adding to it.
27 */
28
29enum index_mode {
30 INDEX_NONE,
e579e0f7 31 INDEX_ADD
62020aa8
KA
32};
33
22a2d3d5
UG
34struct index_options {
35 int dry_run;
36 int verbose;
62020aa8 37 git_repository *repo;
22a2d3d5
UG
38 enum index_mode mode;
39 int add_update;
62020aa8
KA
40};
41
e568bedf 42/* Forward declarations for helpers */
22a2d3d5 43static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args);
e568bedf
BS
44int print_matched_cb(const char *path, const char *matched_pathspec, void *payload);
45
22a2d3d5 46int lg2_add(git_repository *repo, int argc, char **argv)
62020aa8 47{
e568bedf 48 git_index_matched_path_cb matched_cb = NULL;
e568bedf
BS
49 git_index *index;
50 git_strarray array = {0};
22a2d3d5
UG
51 struct index_options options = {0};
52 struct args_info args = ARGS_INFO_INIT;
62020aa8 53
22a2d3d5 54 options.mode = INDEX_ADD;
e568bedf 55
22a2d3d5
UG
56 /* Parse the options & arguments. */
57 parse_opts(NULL, &options, &args);
58 strarray_from_args(&array, &args);
e568bedf 59
22a2d3d5 60 /* Grab the repository's index. */
e568bedf
BS
61 check_lg2(git_repository_index(&index, repo), "Could not open repository index", NULL);
62
22a2d3d5
UG
63 /* Setup a callback if the requested options need it */
64 if (options.verbose || options.dry_run) {
e568bedf 65 matched_cb = &print_matched_cb;
62020aa8
KA
66 }
67
22a2d3d5 68 options.repo = repo;
e568bedf 69
22a2d3d5
UG
70 /* Perform the requested action with the index and files */
71 if (options.add_update) {
72 git_index_update_all(index, &array, matched_cb, &options);
e568bedf 73 } else {
22a2d3d5 74 git_index_add_all(index, &array, 0, matched_cb, &options);
e568bedf
BS
75 }
76
22a2d3d5 77 /* Cleanup memory */
e568bedf
BS
78 git_index_write(index);
79 git_index_free(index);
e568bedf
BS
80
81 return 0;
62020aa8
KA
82}
83
22a2d3d5
UG
84/*
85 * This callback is called for each file under consideration by
86 * git_index_(update|add)_all above.
87 * It makes uses of the callback's ability to abort the action.
88 */
62020aa8
KA
89int print_matched_cb(const char *path, const char *matched_pathspec, void *payload)
90{
22a2d3d5 91 struct index_options *opts = (struct index_options *)(payload);
62020aa8 92 int ret;
fc293919 93 unsigned status;
04fd2665 94 (void)matched_pathspec;
62020aa8 95
22a2d3d5
UG
96 /* Get the file status */
97 if (git_status_file(&status, opts->repo, path) < 0)
fc293919 98 return -1;
62020aa8 99
22a2d3d5 100 if ((status & GIT_STATUS_WT_MODIFIED) || (status & GIT_STATUS_WT_NEW)) {
b2395a82 101 printf("add '%s'\n", path);
62020aa8
KA
102 ret = 0;
103 } else {
104 ret = 1;
105 }
106
22a2d3d5 107 if (opts->dry_run)
62020aa8 108 ret = 1;
62020aa8
KA
109
110 return ret;
111}
112
e579e0f7 113static void print_usage(void)
813937ce
KA
114{
115 fprintf(stderr, "usage: add [options] [--] file-spec [file-spec] [...]\n\n");
116 fprintf(stderr, "\t-n, --dry-run dry run\n");
117 fprintf(stderr, "\t-v, --verbose be verbose\n");
118 fprintf(stderr, "\t-u, --update update tracked files\n");
e568bedf 119 exit(1);
813937ce
KA
120}
121
22a2d3d5 122static void parse_opts(const char **repo_path, struct index_options *opts, struct args_info *args)
62020aa8 123{
22a2d3d5
UG
124 if (args->argc <= 1)
125 print_usage();
62020aa8 126
22a2d3d5
UG
127 for (args->pos = 1; args->pos < args->argc; ++args->pos) {
128 const char *curr = args->argv[args->pos];
129
130 if (curr[0] != '-') {
131 if (!strcmp("add", curr)) {
132 opts->mode = INDEX_ADD;
133 continue;
134 } else if (opts->mode == INDEX_NONE) {
135 fprintf(stderr, "missing command: %s", curr);
136 print_usage();
137 break;
138 } else {
139 /* We might be looking at a filename */
140 break;
141 }
142 } else if (match_bool_arg(&opts->verbose, args, "--verbose") ||
143 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 continue;
147 } else if (match_bool_arg(NULL, args, "--help")) {
813937ce
KA
148 print_usage();
149 break;
22a2d3d5 150 } else if (match_arg_separator(args)) {
62020aa8 151 break;
22a2d3d5
UG
152 } else {
153 fprintf(stderr, "Unsupported option %s.\n", curr);
813937ce 154 print_usage();
62020aa8
KA
155 }
156 }
62020aa8 157}