@matheusbsbs / matheus-bagliano / commits / 46b5139cae
Fork 0

builtin-add: refactor the meat of interactive_add()

This moves the call setup for 'git add--interactive' to a separate function, as other users will call it without running validate_pathspec() first. Signed-off-by: Thomas Rast <trast@student.ethz.ch> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Thomas Rast committed Aug 13, 2009 at 14:29 UTC 46b5139cae7306194a39fdaf5c6abc12ab531c84
2 files changed +31 -14
builtin-add.c
+29 -14
@@ -131,27 +131,27 @@ static const char **validate_pathspec(int argc, const char **argv, const char *p
131 return pathspec;
132 }
133
134 -int interactive_add(int argc, const char **argv, const char *prefix)
134 +int run_add_interactive(const char *revision, const char *patch_mode,
135 + const char **pathspec)
136 {
136 - int status, ac;
137 + int status, ac, pc = 0;
138 const char **args;
138 - const char **pathspec = NULL;
139
140 - if (argc) {
141 - pathspec = validate_pathspec(argc, argv, prefix);
142 - if (!pathspec)
143 - return -1;
144 - }
140 + if (pathspec)
141 + while (pathspec[pc])
142 + pc++;
143
146 - args = xcalloc(sizeof(const char *), (argc + 4));
144 + args = xcalloc(sizeof(const char *), (pc + 5));
145 ac = 0;
146 args[ac++] = "add--interactive";
149 - if (patch_interactive)
150 - args[ac++] = "--patch";
147 + if (patch_mode)
148 + args[ac++] = patch_mode;
149 + if (revision)
150 + args[ac++] = revision;
151 args[ac++] = "--";
152 - if (argc) {
153 - memcpy(&(args[ac]), pathspec, sizeof(const char *) * argc);
154 - ac += argc;
152 + if (pc) {
153 + memcpy(&(args[ac]), pathspec, sizeof(const char *) * pc);
154 + ac += pc;
155 }
156 args[ac] = NULL;
157
@@ -160,6 +160,21 @@ int interactive_add(int argc, const char **argv, const char *prefix)
160 return status;
161 }
162
163 +int interactive_add(int argc, const char **argv, const char *prefix)
164 +{
165 + const char **pathspec = NULL;
166 +
167 + if (argc) {
168 + pathspec = validate_pathspec(argc, argv, prefix);
169 + if (!pathspec)
170 + return -1;
171 + }
172 +
173 + return run_add_interactive(NULL,
174 + patch_interactive ? "--patch" : NULL,
175 + pathspec);
176 +}
177 +
178 static int edit_patch(int argc, const char **argv, const char *prefix)
179 {
180 char *file = xstrdup(git_path("ADD_EDIT.patch"));
commit.h
+2
@@ -137,6 +137,8 @@ int is_descendant_of(struct commit *, struct commit_list *);
137 int in_merge_bases(struct commit *, struct commit **, int);
138
139 extern int interactive_add(int argc, const char **argv, const char *prefix);
140 +extern int run_add_interactive(const char *revision, const char *patch_mode,
141 + const char **pathspec);
142
143 static inline int single_parent(struct commit *commit)
144 {