packaging: Add contrib installation
[platform/upstream/git.git] / alias.c
diff --git a/alias.c b/alias.c
index 3b90397..c471538 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -1,21 +1,54 @@
 #include "cache.h"
+#include "alias.h"
+#include "config.h"
+#include "string-list.h"
+
+struct config_alias_data {
+       const char *alias;
+       char *v;
+       struct string_list *list;
+};
+
+static int config_alias_cb(const char *key, const char *value, void *d)
+{
+       struct config_alias_data *data = d;
+       const char *p;
+
+       if (!skip_prefix(key, "alias.", &p))
+               return 0;
+
+       if (data->alias) {
+               if (!strcasecmp(p, data->alias))
+                       return git_config_string((const char **)&data->v,
+                                                key, value);
+       } else if (data->list) {
+               string_list_append(data->list, p);
+       }
+
+       return 0;
+}
 
 char *alias_lookup(const char *alias)
 {
-       char *v = NULL;
-       struct strbuf key = STRBUF_INIT;
-       strbuf_addf(&key, "alias.%s", alias);
-       if (git_config_key_is_valid(key.buf))
-               git_config_get_string(key.buf, &v);
-       strbuf_release(&key);
-       return v;
+       struct config_alias_data data = { alias, NULL };
+
+       read_early_config(config_alias_cb, &data);
+
+       return data.v;
+}
+
+void list_aliases(struct string_list *list)
+{
+       struct config_alias_data data = { NULL, NULL, list };
+
+       read_early_config(config_alias_cb, &data);
 }
 
 #define SPLIT_CMDLINE_BAD_ENDING 1
 #define SPLIT_CMDLINE_UNCLOSED_QUOTE 2
 static const char *split_cmdline_errors[] = {
-       "cmdline ends with \\",
-       "unclosed quote"
+       N_("cmdline ends with \\"),
+       N_("unclosed quote")
 };
 
 int split_cmdline(char *cmdline, const char ***argv)
@@ -47,8 +80,7 @@ int split_cmdline(char *cmdline, const char ***argv)
                                src++;
                                c = cmdline[src];
                                if (!c) {
-                                       free(*argv);
-                                       *argv = NULL;
+                                       FREE_AND_NULL(*argv);
                                        return -SPLIT_CMDLINE_BAD_ENDING;
                                }
                        }
@@ -60,8 +92,7 @@ int split_cmdline(char *cmdline, const char ***argv)
        cmdline[dst] = 0;
 
        if (quoted) {
-               free(*argv);
-               *argv = NULL;
+               FREE_AND_NULL(*argv);
                return -SPLIT_CMDLINE_UNCLOSED_QUOTE;
        }