@matheusbsbs / matheus-bagliano / commits / 2b11e3170e
Fork 0

If you have a config containing something like this:

[alias] l = "log --stat -M ORIG_HEAD.." you can call git l and it will do the same as git log --stat -M ORIG_HEAD.. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 5, 2006 at 19:43 UTC 2b11e3170e919866c7f5554dd4089289a1bb6726
1 file changed +111
git.c
+111
@@ -10,6 +10,7 @@
10 #include <stdarg.h>
11 #include "git-compat-util.h"
12 #include "exec_cmd.h"
13 +#include "cache.h"
14
15 #include "builtin.h"
16
@@ -32,6 +33,113 @@ static void prepend_to_path(const char *dir, int len)
33 setenv("PATH", path, 1);
34 }
35
36 +static const char *alias_command;
37 +static char *alias_string = NULL;
38 +
39 +static int git_alias_config(const char *var, const char *value)
40 +{
41 + if (!strncmp(var, "alias.", 6) && !strcmp(var + 6, alias_command)) {
42 + alias_string = strdup(value);
43 + }
44 + return 0;
45 +}
46 +
47 +static int split_cmdline(char *cmdline, const char ***argv)
48 +{
49 + int src, dst, count = 0, size = 16;
50 + char quoted = 0;
51 +
52 + *argv = malloc(sizeof(char*) * size);
53 +
54 + /* split alias_string */
55 + (*argv)[count++] = cmdline;
56 + for (src = dst = 0; cmdline[src];) {
57 + char c = cmdline[src];
58 + if (!quoted && isspace(c)) {
59 + cmdline[dst++] = 0;
60 + while (cmdline[++src]
61 + && isspace(cmdline[src]))
62 + ; /* skip */
63 + if (count >= size) {
64 + size += 16;
65 + *argv = realloc(*argv, sizeof(char*) * size);
66 + }
67 + (*argv)[count++] = cmdline + dst;
68 + } else if(!quoted && (c == '\'' || c == '"')) {
69 + quoted = c;
70 + src++;
71 + } else if (c == quoted) {
72 + quoted = 0;
73 + src++;
74 + } else {
75 + if (c == '\\' && quoted != '\'') {
76 + src++;
77 + c = cmdline[src];
78 + if (!c) {
79 + free(*argv);
80 + *argv = NULL;
81 + return error("cmdline ends with \\");
82 + }
83 + }
84 + cmdline[dst++] = c;
85 + src++;
86 + }
87 + }
88 +
89 + cmdline[dst] = 0;
90 +
91 + if (quoted) {
92 + free(*argv);
93 + *argv = NULL;
94 + return error("unclosed quote");
95 + }
96 +
97 + return count;
98 +}
99 +
100 +static int handle_alias(int *argcp, const char ***argv)
101 +{
102 + int nongit = 0, ret = 0;
103 + const char *subdir;
104 +
105 + subdir = setup_git_directory_gently(&nongit);
106 + if (!nongit) {
107 + int count;
108 + const char** new_argv;
109 +
110 + alias_command = (*argv)[0];
111 + git_config(git_alias_config);
112 + if (alias_string) {
113 +
114 + count = split_cmdline(alias_string, &new_argv);
115 +
116 + if (count < 1)
117 + die("empty alias for %s", alias_command);
118 +
119 + if (!strcmp(alias_command, new_argv[0]))
120 + die("recursive alias: %s", alias_command);
121 +
122 + /* insert after command name */
123 + if (*argcp > 1) {
124 + new_argv = realloc(new_argv, sizeof(char*) *
125 + (count + *argcp - 1));
126 + memcpy(new_argv + count, *argv, sizeof(char*) *
127 + (*argcp - 1));
128 + }
129 +
130 + *argv = new_argv;
131 + *argcp += count - 1;
132 +
133 + ret = 1;
134 + }
135 + }
136 +
137 + if (subdir)
138 + chdir(subdir);
139 +
140 + return ret;
141 +}
142 +
143 const char git_version_string[] = GIT_VERSION;
144
145 static void handle_internal_command(int argc, const char **argv, char **envp)
@@ -121,6 +229,7 @@ int main(int argc, const char **argv, char **envp)
229 if (!strncmp(cmd, "git-", 4)) {
230 cmd += 4;
231 argv[0] = cmd;
232 + handle_alias(&argc, &argv);
233 handle_internal_command(argc, argv, envp);
234 die("cannot handle %s internally", cmd);
235 }
@@ -178,6 +287,8 @@ int main(int argc, const char **argv, char **envp)
287 exec_path = git_exec_path();
288 prepend_to_path(exec_path, strlen(exec_path));
289
290 + handle_alias(&argc, &argv);
291 +
292 /* See if it's an internal command */
293 handle_internal_command(argc, argv, envp);
294