@matheusbsbs / matheus-bagliano / commits / e321180ed3
Fork 0

Remove calculation of the longest command name from where it is not used

Just calculate it where it is needed - it is cheap and trivial, as all the lengths are already there (stored when creating the command lists). Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alex Riesen committed Aug 28, 2008 at 19:15 UTC e321180ed3e22120e30bb350a5adecbe959e1241
4 files changed +24 -28
builtin-help.c
+2 -2
@@ -418,7 +418,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
418 {
419 int nongit;
420 const char *alias;
421 - unsigned int longest = load_command_list("git-", &main_cmds, &other_cmds);
421 + load_command_list("git-", &main_cmds, &other_cmds);
422
423 setup_git_directory_gently(&nongit);
424 git_config(git_help_config, NULL);
@@ -428,7 +428,7 @@ int cmd_help(int argc, const char **argv, const char *prefix)
428
429 if (show_all) {
430 printf("usage: %s\n\n", git_usage_string);
431 - list_commands("git commands", longest, &main_cmds, &other_cmds);
431 + list_commands("git commands", &main_cmds, &other_cmds);
432 printf("%s\n", git_more_info_string);
433 return 0;
434 }
builtin-merge.c
+4 -4
@@ -80,7 +80,7 @@ static struct strategy *get_strategy(const char *name)
80 int i;
81 struct strategy *ret;
82 static struct cmdnames main_cmds, other_cmds;
83 - static int longest;
83 + static int loaded;
84
85 if (!name)
86 return NULL;
@@ -89,14 +89,14 @@ static struct strategy *get_strategy(const char *name)
89 if (!strcmp(name, all_strategy[i].name))
90 return &all_strategy[i];
91
92 - if (!longest) {
92 + if (!loaded) {
93 struct cmdnames not_strategies;
94 + loaded = 1;
95
96 memset(&main_cmds, 0, sizeof(struct cmdnames));
97 memset(&other_cmds, 0, sizeof(struct cmdnames));
98 memset(&not_strategies, 0, sizeof(struct cmdnames));
98 - longest = load_command_list("git-merge-", &main_cmds,
99 - &other_cmds);
99 + load_command_list("git-merge-", &main_cmds, &other_cmds);
100 for (i = 0; i < main_cmds.cnt; i++) {
101 int j, found = 0;
102 struct cmdname *ent = main_cmds.names[i];
help.c
+15 -19
@@ -133,11 +133,10 @@ static int is_executable(const char *name)
133 return st.st_mode & S_IXUSR;
134 }
135
136 -static unsigned int list_commands_in_dir(struct cmdnames *cmds,
136 +static void list_commands_in_dir(struct cmdnames *cmds,
137 const char *path,
138 const char *prefix)
139 {
140 - unsigned int longest = 0;
140 int prefix_len;
141 DIR *dir = opendir(path);
142 struct dirent *de;
@@ -145,7 +144,7 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
144 int len;
145
146 if (!dir)
148 - return 0;
147 + return;
148 if (!prefix)
149 prefix = "git-";
150 prefix_len = strlen(prefix);
@@ -168,29 +167,22 @@ static unsigned int list_commands_in_dir(struct cmdnames *cmds,
167 if (has_extension(de->d_name, ".exe"))
168 entlen -= 4;
169
171 - if (longest < entlen)
172 - longest = entlen;
173 -
170 add_cmdname(cmds, de->d_name + prefix_len, entlen);
171 }
172 closedir(dir);
173 strbuf_release(&buf);
178 -
179 - return longest;
174 }
175
182 -unsigned int load_command_list(const char *prefix,
176 +void load_command_list(const char *prefix,
177 struct cmdnames *main_cmds,
178 struct cmdnames *other_cmds)
179 {
186 - unsigned int longest = 0;
187 - unsigned int len;
180 const char *env_path = getenv("PATH");
181 char *paths, *path, *colon;
182 const char *exec_path = git_exec_path();
183
184 if (exec_path)
193 - longest = list_commands_in_dir(main_cmds, exec_path, prefix);
185 + list_commands_in_dir(main_cmds, exec_path, prefix);
186
187 if (!env_path) {
188 fprintf(stderr, "PATH not set\n");
@@ -202,9 +194,7 @@ unsigned int load_command_list(const char *prefix,
194 if ((colon = strchr(path, PATH_SEP)))
195 *colon = 0;
196
205 - len = list_commands_in_dir(other_cmds, path, prefix);
206 - if (len > longest)
207 - longest = len;
197 + list_commands_in_dir(other_cmds, path, prefix);
198
199 if (!colon)
200 break;
@@ -220,14 +210,20 @@ unsigned int load_command_list(const char *prefix,
210 sizeof(*other_cmds->names), cmdname_compare);
211 uniq(other_cmds);
212 exclude_cmds(other_cmds, main_cmds);
223 -
224 - return longest;
213 }
214
227 -void list_commands(const char *title, unsigned int longest,
228 - struct cmdnames *main_cmds, struct cmdnames *other_cmds)
215 +void list_commands(const char *title, struct cmdnames *main_cmds,
216 + struct cmdnames *other_cmds)
217 {
218 const char *exec_path = git_exec_path();
219 + int i, longest = 0;
220 +
221 + for (i = 0; i < main_cmds->cnt; i++)
222 + if (longest < main_cmds->names[i]->len)
223 + longest = main_cmds->names[i]->len;
224 + for (i = 0; i < other_cmds->cnt; i++)
225 + if (longest < other_cmds->names[i]->len)
226 + longest = other_cmds->names[i]->len;
227
228 if (main_cmds->cnt) {
229 printf("available %s in '%s'\n", title, exec_path);
help.h
+3 -3
@@ -16,14 +16,14 @@ static inline void mput_char(char c, unsigned int num)
16 putchar(c);
17 }
18
19 -unsigned int load_command_list(const char *prefix,
19 +void load_command_list(const char *prefix,
20 struct cmdnames *main_cmds,
21 struct cmdnames *other_cmds);
22 void add_cmdname(struct cmdnames *cmds, const char *name, int len);
23 /* Here we require that excludes is a sorted list. */
24 void exclude_cmds(struct cmdnames *cmds, struct cmdnames *excludes);
25 int is_in_cmdlist(struct cmdnames *c, const char *s);
26 -void list_commands(const char *title, unsigned int longest,
27 - struct cmdnames *main_cmds, struct cmdnames *other_cmds);
26 +void list_commands(const char *title, struct cmdnames *main_cmds,
27 + struct cmdnames *other_cmds);
28
29 #endif /* HELP_H */