Port builtin-add.c to use the new option parser.
Signed-off-by: Kristian Høgsberg <krh@redhat.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
Kristian Høgsberg committed
Oct 3, 2007 at 17:45 UTC
5c46f75437e6764532f46cbaf1e0bedb52f5acb9
1 file changed
+27
-46
builtin-add.c
+27
-46
@@ -12,9 +12,13 @@
12
#include "diffcore.h"
13
#include "commit.h"
14
#include "revision.h"
15
+#include "run-command.h"
16
+#include "parse-options.h"
17
16
-static const char builtin_add_usage[] =
17
-"git-add [-n] [-v] [-f] [--interactive | -i] [-u] [--refresh] [--] <filepattern>...";
18
+static const char * const builtin_add_usage[] = {
19
+ "git-add [options] [--] <filepattern>...",
20
+ NULL
21
+};
22
23
static int take_worktree_changes;
24
static const char *excludes_file;
@@ -156,23 +160,32 @@ static struct lock_file lock_file;
160
static const char ignore_error[] =
161
"The following paths are ignored by one of your .gitignore files:\n";
162
163
+static int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
164
+static int add_interactive = 0;
165
+
166
+static struct option builtin_add_options[] = {
167
+ OPT__DRY_RUN(&show_only),
168
+ OPT__VERBOSE(&verbose),
169
+ OPT_GROUP(""),
170
+ OPT_BOOLEAN('i', "interactive", &add_interactive, "interactive picking"),
171
+ OPT_BOOLEAN('f', NULL, &ignored_too, "allow adding otherwise ignored files"),
172
+ OPT_BOOLEAN('u', NULL, &take_worktree_changes, "update tracked files"),
173
+ OPT_BOOLEAN( 0 , "refresh", &refresh_only, "don't add, only refresh the index"),
174
+ OPT_END(),
175
+};
176
+
177
int cmd_add(int argc, const char **argv, const char *prefix)
178
{
161
- int i, newfd;
162
- int verbose = 0, show_only = 0, ignored_too = 0, refresh_only = 0;
179
+ int i, newfd, orig_argc = argc;
180
const char **pathspec;
181
struct dir_struct dir;
165
- int add_interactive = 0;
182
167
- for (i = 1; i < argc; i++) {
168
- if (!strcmp("--interactive", argv[i]) ||
169
- !strcmp("-i", argv[i]))
170
- add_interactive++;
171
- }
183
+ argc = parse_options(argc, argv, builtin_add_options,
184
+ builtin_add_usage, 0);
185
if (add_interactive) {
186
const char *args[] = { "add--interactive", NULL };
187
175
- if (add_interactive != 1 || argc != 2)
188
+ if (add_interactive != 1 || orig_argc != 2)
189
die("add --interactive does not take any parameters");
190
execv_git_cmd(args);
191
exit(1);
@@ -182,49 +195,17 @@ int cmd_add(int argc, const char **argv, const char *prefix)
195
196
newfd = hold_locked_index(&lock_file, 1);
197
185
- for (i = 1; i < argc; i++) {
186
- const char *arg = argv[i];
187
-
188
- if (arg[0] != '-')
189
- break;
190
- if (!strcmp(arg, "--")) {
191
- i++;
192
- break;
193
- }
194
- if (!strcmp(arg, "-n")) {
195
- show_only = 1;
196
- continue;
197
- }
198
- if (!strcmp(arg, "-f")) {
199
- ignored_too = 1;
200
- continue;
201
- }
202
- if (!strcmp(arg, "-v")) {
203
- verbose = 1;
204
- continue;
205
- }
206
- if (!strcmp(arg, "-u")) {
207
- take_worktree_changes = 1;
208
- continue;
209
- }
210
- if (!strcmp(arg, "--refresh")) {
211
- refresh_only = 1;
212
- continue;
213
- }
214
- usage(builtin_add_usage);
215
- }
216
-
198
if (take_worktree_changes) {
218
- update(verbose, prefix, argv + i);
199
+ update(verbose, prefix, argv);
200
goto finish;
201
}
202
222
- if (argc <= i) {
203
+ if (argc == 0) {
204
fprintf(stderr, "Nothing specified, nothing added.\n");
205
fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
206
return 0;
207
}
227
- pathspec = get_pathspec(prefix, argv + i);
208
+ pathspec = get_pathspec(prefix, argv);
209
210
if (refresh_only) {
211
refresh(verbose, pathspec);