Skip to content

Commit 46b5139

Browse files
trastgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b319ef7 commit 46b5139

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

builtin-add.c

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -131,27 +131,27 @@ static const char **validate_pathspec(int argc, const char **argv, const char *p
131131
return pathspec;
132132
}
133133

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)
135136
{
136-
int status, ac;
137+
int status, ac, pc = 0;
137138
const char **args;
138-
const char **pathspec = NULL;
139139

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++;
145143

146-
args = xcalloc(sizeof(const char *), (argc + 4));
144+
args = xcalloc(sizeof(const char *), (pc + 5));
147145
ac = 0;
148146
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;
151151
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;
155155
}
156156
args[ac] = NULL;
157157

@@ -160,6 +160,21 @@ int interactive_add(int argc, const char **argv, const char *prefix)
160160
return status;
161161
}
162162

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+
163178
static int edit_patch(int argc, const char **argv, const char *prefix)
164179
{
165180
char *file = xstrdup(git_path("ADD_EDIT.patch"));

commit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ int is_descendant_of(struct commit *, struct commit_list *);
137137
int in_merge_bases(struct commit *, struct commit **, int);
138138

139139
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);
140142

141143
static inline int single_parent(struct commit *commit)
142144
{

0 commit comments

Comments
 (0)