var.con commit status: don't require the repository to be writable (ab68545)
   1/*
   2 * GIT - The information manager from hell
   3 *
   4 * Copyright (C) Eric Biederman, 2005
   5 */
   6#include "cache.h"
   7#include "exec_cmd.h"
   8
   9static const char var_usage[] = "git var [-l | <variable>]";
  10
  11static const char *editor(int flag)
  12{
  13        const char *pgm = git_editor();
  14
  15        if (!pgm && flag & IDENT_ERROR_ON_NO_NAME)
  16                die("Terminal is dumb, but EDITOR unset");
  17
  18        return pgm;
  19}
  20
  21static const char *pager(int flag)
  22{
  23        const char *pgm = git_pager();
  24
  25        if (!pgm)
  26                pgm = "cat";
  27        return pgm;
  28}
  29
  30struct git_var {
  31        const char *name;
  32        const char *(*read)(int);
  33};
  34static struct git_var git_vars[] = {
  35        { "GIT_COMMITTER_IDENT", git_committer_info },
  36        { "GIT_AUTHOR_IDENT",   git_author_info },
  37        { "GIT_EDITOR", editor },
  38        { "GIT_PAGER", pager },
  39        { "", NULL },
  40};
  41
  42static void list_vars(void)
  43{
  44        struct git_var *ptr;
  45        const char *val;
  46
  47        for (ptr = git_vars; ptr->read; ptr++)
  48                if ((val = ptr->read(0)))
  49                        printf("%s=%s\n", ptr->name, val);
  50}
  51
  52static const char *read_var(const char *var)
  53{
  54        struct git_var *ptr;
  55        const char *val;
  56        val = NULL;
  57        for (ptr = git_vars; ptr->read; ptr++) {
  58                if (strcmp(var, ptr->name) == 0) {
  59                        val = ptr->read(IDENT_ERROR_ON_NO_NAME);
  60                        break;
  61                }
  62        }
  63        return val;
  64}
  65
  66static int show_config(const char *var, const char *value, void *cb)
  67{
  68        if (value)
  69                printf("%s=%s\n", var, value);
  70        else
  71                printf("%s\n", var);
  72        return git_default_config(var, value, cb);
  73}
  74
  75int main(int argc, char **argv)
  76{
  77        const char *val;
  78        int nongit;
  79        if (argc != 2) {
  80                usage(var_usage);
  81        }
  82
  83        git_extract_argv0_path(argv[0]);
  84
  85        setup_git_directory_gently(&nongit);
  86        val = NULL;
  87
  88        if (strcmp(argv[1], "-l") == 0) {
  89                git_config(show_config, NULL);
  90                list_vars();
  91                return 0;
  92        }
  93        git_config(git_default_config, NULL);
  94        val = read_var(argv[1]);
  95        if (!val)
  96                usage(var_usage);
  97
  98        printf("%s\n", val);
  99
 100        return 0;
 101}