Skip to content

Commit 0347a8c

Browse files
matledJunio C Hamano
authored andcommitted
git.c: allow alias expansion without a git directory
With this, the configuration mechanism can be used to say: [alias] init = init-db --template=/path/to/template Signed-off-by: Matthias Lederhofer <matled@gmx.net> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 41e95f6 commit 0347a8c

File tree

1 file changed

+36
-40
lines changed

1 file changed

+36
-40
lines changed

git.c

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -156,52 +156,48 @@ static int handle_alias(int *argcp, const char ***argv)
156156
{
157157
int nongit = 0, ret = 0, saved_errno = errno;
158158
const char *subdir;
159+
int count, option_count;
160+
const char** new_argv;
159161

160162
subdir = setup_git_directory_gently(&nongit);
161-
if (!nongit) {
162-
int count, option_count;
163-
const char** new_argv;
164-
165-
alias_command = (*argv)[0];
166-
git_config(git_alias_config);
167-
if (alias_string) {
168-
169-
count = split_cmdline(alias_string, &new_argv);
170-
option_count = handle_options(&new_argv, &count);
171-
memmove(new_argv - option_count, new_argv,
172-
count * sizeof(char *));
173-
new_argv -= option_count;
174-
175-
if (count < 1)
176-
die("empty alias for %s", alias_command);
177-
178-
if (!strcmp(alias_command, new_argv[0]))
179-
die("recursive alias: %s", alias_command);
180-
181-
if (getenv("GIT_TRACE")) {
182-
int i;
183-
fprintf(stderr, "trace: alias expansion: %s =>",
184-
alias_command);
185-
for (i = 0; i < count; ++i) {
186-
fputc(' ', stderr);
187-
sq_quote_print(stderr, new_argv[i]);
188-
}
189-
fputc('\n', stderr);
190-
fflush(stderr);
191-
}
192163

193-
new_argv = realloc(new_argv, sizeof(char*) *
194-
(count + *argcp + 1));
195-
/* insert after command name */
196-
memcpy(new_argv + count, *argv + 1,
197-
sizeof(char*) * *argcp);
198-
new_argv[count+*argcp] = NULL;
164+
alias_command = (*argv)[0];
165+
git_config(git_alias_config);
166+
if (alias_string) {
167+
count = split_cmdline(alias_string, &new_argv);
168+
option_count = handle_options(&new_argv, &count);
169+
memmove(new_argv - option_count, new_argv,
170+
count * sizeof(char *));
171+
new_argv -= option_count;
172+
173+
if (count < 1)
174+
die("empty alias for %s", alias_command);
199175

200-
*argv = new_argv;
201-
*argcp += count - 1;
176+
if (!strcmp(alias_command, new_argv[0]))
177+
die("recursive alias: %s", alias_command);
202178

203-
ret = 1;
179+
if (getenv("GIT_TRACE")) {
180+
int i;
181+
fprintf(stderr, "trace: alias expansion: %s =>",
182+
alias_command);
183+
for (i = 0; i < count; ++i) {
184+
fputc(' ', stderr);
185+
sq_quote_print(stderr, new_argv[i]);
186+
}
187+
fputc('\n', stderr);
188+
fflush(stderr);
204189
}
190+
191+
new_argv = realloc(new_argv, sizeof(char*) *
192+
(count + *argcp + 1));
193+
/* insert after command name */
194+
memcpy(new_argv + count, *argv + 1, sizeof(char*) * *argcp);
195+
new_argv[count+*argcp] = NULL;
196+
197+
*argv = new_argv;
198+
*argcp += count - 1;
199+
200+
ret = 1;
205201
}
206202

207203
if (subdir)

0 commit comments

Comments
 (0)