git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] make alias lookup a public, procedural function

From
Jeff King <peff@peff.net>
Date
Feb 24, 2008, 22:17 UTC
Message-ID
<20080224221714.GC31309@coredump.intra.peff.net>
In-Reply-To
<cover.1203890846.git.peff@peff.net>

This converts git_config_alias to the public alias_lookup function. Because of the nature of our config parser, we still have to rely on setting static data. However, that interface is wrapped so that you can just say

  value = alias_lookup(key);
Signed-off-by: Jeff King <peff@peff.net>
---
 Makefile |    3 ++-
 alias.c  |   22 ++++++++++++++++++++++
 cache.h  |    2 ++
 git.c    |   17 +++--------------
 4 files changed, 29 insertions(+), 15 deletions(-)
 create mode 100644 alias.c
diff --git a/Makefile b/Makefile
index d33a556..b460bb2 100644
--- a/Makefile
+++ b/Makefile
@@ -327,7 +327,8 @@ LIB_OBJS = \
 	alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
 	color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
 	convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
-	transport.o bundle.o walker.o parse-options.o ws.o archive.o
+	transport.o bundle.o walker.o parse-options.o ws.o archive.o \
+	alias.o
 
 BUILTIN_OBJS = \
 	builtin-add.o \
diff --git a/alias.c b/alias.c
new file mode 100644
index 0000000..116cac8
--- /dev/null
+++ b/alias.c
@@ -0,0 +1,22 @@
+#include "cache.h"
+
+static const char *alias_key;
+static char *alias_val;
+static int alias_lookup_cb(const char *k, const char *v)
+{
+	if (!prefixcmp(k, "alias.") && !strcmp(k+6, alias_key)) {
+		if (!v)
+			return config_error_nonbool(k);
+		alias_val = xstrdup(v);
+		return 0;
+	}
+	return 0;
+}
+
+char *alias_lookup(const char *alias)
+{
+	alias_key = alias;
+	alias_val = NULL;
+	git_config(alias_lookup_cb);
+	return alias_val;
+}
diff --git a/cache.h b/cache.h
index fa5a9e5..1e9c937 100644
--- a/cache.h
+++ b/cache.h
@@ -765,4 +765,6 @@ int pathspec_match(const char **spec, char *matched, const char *filename, int s
 int report_path_error(const char *ps_matched, const char **pathspec, int prefix_offset);
 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 
+char *alias_lookup(const char *alias);
+
 #endif /* CACHE_H */
diff --git a/git.c b/git.c
index 0cb8688..8f08b12 100644
--- a/git.c
+++ b/git.c
@@ -87,19 +87,6 @@ static int handle_options(const char*** argv, int* argc, int* envchanged)
 	return handled;
 }
 
-static const char *alias_command;
-static char *alias_string;
-
-static int git_alias_config(const char *var, const char *value)
-{
-	if (!prefixcmp(var, "alias.") && !strcmp(var + 6, alias_command)) {
-		if (!value)
-			return config_error_nonbool(var);
-		alias_string = xstrdup(value);
-	}
-	return 0;
-}
-
 static int split_cmdline(char *cmdline, const char ***argv)
 {
 	int src, dst, count = 0, size = 16;
@@ -159,11 +146,13 @@ static int handle_alias(int *argcp, const char ***argv)
 	const char *subdir;
 	int count, option_count;
 	const char** new_argv;
+	const char *alias_command;
+	char *alias_string;
 
 	subdir = setup_git_directory_gently(&nongit);
 
 	alias_command = (*argv)[0];
-	git_config(git_alias_config);
+	alias_string = alias_lookup(alias_command);
 	if (alias_string) {
 		if (alias_string[0] == '!') {
 			if (*argcp > 1) {
-- 
1.5.4.3.305.g073a4
Previous: Junio C HamanoNext: Jeff King
Message 5 of 12 in “help: use parseopt”
  1. 1/3 help: use parseoptJeff King, Feb 24, 2008
  2. Christian CouderFeb 25, 2008
  3. Jeff KingFeb 25, 2008
  4. Junio C HamanoFeb 25, 2008
  5. 2/3 make alias lookup a public, procedural functionJeff King, Feb 24, 2008
  6. 3/3 help: respect aliasesJeff King, Feb 24, 2008
  7. Jay SoffianFeb 25, 2008
  8. Johannes SchindelinFeb 26, 2008
  9. Jay SoffianFeb 26, 2008
  10. Johannes SchindelinFeb 26, 2008
  11. Junio C HamanoFeb 26, 2008
  12. Jay SoffianFeb 26, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.