Code

Merge branch 'dw/gitweb-doc-grammo' into maint-1.7.8
[git.git] / setup.c
diff --git a/setup.c b/setup.c
index 346ef2eb2d678b742280c0816a09b8ca174ca799..61c22e6becc1e49f1e92c916a4b8badd30a9cb2f 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -4,13 +4,16 @@
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
 
-const char *prefix_path(const char *prefix, int len, const char *path)
+char *prefix_path(const char *prefix, int len, const char *path)
 {
        const char *orig = path;
-       char *sanitized = xmalloc(len + strlen(path) + 1);
-       if (is_absolute_path(orig))
-               strcpy(sanitized, path);
-       else {
+       char *sanitized;
+       if (is_absolute_path(orig)) {
+               const char *temp = real_path(path);
+               sanitized = xmalloc(len + strlen(temp) + 1);
+               strcpy(sanitized, temp);
+       } else {
+               sanitized = xmalloc(len + strlen(path) + 1);
                if (len)
                        memcpy(sanitized, prefix, len);
                strcpy(sanitized + len, path);
@@ -37,34 +40,6 @@ const char *prefix_path(const char *prefix, int len, const char *path)
        return sanitized;
 }
 
-/*
- * Unlike prefix_path, this should be used if the named file does
- * not have to interact with index entry; i.e. name of a random file
- * on the filesystem.
- */
-const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
-{
-       static char path[PATH_MAX];
-#ifndef WIN32
-       if (!pfx_len || is_absolute_path(arg))
-               return arg;
-       memcpy(path, pfx, pfx_len);
-       strcpy(path + pfx_len, arg);
-#else
-       char *p;
-       /* don't add prefix to absolute paths, but still replace '\' by '/' */
-       if (is_absolute_path(arg))
-               pfx_len = 0;
-       else if (pfx_len)
-               memcpy(path, pfx, pfx_len);
-       strcpy(path + pfx_len, arg);
-       for (p = path + pfx_len; *p; p++)
-               if (*p == '\\')
-                       *p = '/';
-#endif
-       return path;
-}
-
 int check_filename(const char *prefix, const char *arg)
 {
        const char *name;
@@ -82,8 +57,17 @@ static void NORETURN die_verify_filename(const char *prefix, const char *arg)
 {
        unsigned char sha1[20];
        unsigned mode;
-       /* try a detailed diagnostic ... */
-       get_sha1_with_mode_1(arg, sha1, &mode, 0, prefix);
+
+       /*
+        * Saying "'(icase)foo' does not exist in the index" when the
+        * user gave us ":(icase)foo" is just stupid.  A magic pathspec
+        * begins with a colon and is followed by a non-alnum; do not
+        * let get_sha1_with_mode_1(only_to_die=1) to even trigger.
+        */
+       if (!(arg[0] == ':' && !isalnum(arg[1])))
+               /* try a detailed diagnostic ... */
+               get_sha1_with_mode_1(arg, sha1, &mode, 1, prefix);
+
        /* ... or fall back the most general message. */
        die("ambiguous argument '%s': unknown revision or path not in the working tree.\n"
            "Use '--' to separate paths from revisions", arg);
@@ -123,6 +107,105 @@ void verify_non_filename(const char *prefix, const char *arg)
            "Use '--' to separate filenames from revisions", arg);
 }
 
+/*
+ * Magic pathspec
+ *
+ * NEEDSWORK: These need to be moved to dir.h or even to a new
+ * pathspec.h when we restructure get_pathspec() users to use the
+ * "struct pathspec" interface.
+ *
+ * Possible future magic semantics include stuff like:
+ *
+ *     { PATHSPEC_NOGLOB, '!', "noglob" },
+ *     { PATHSPEC_ICASE, '\0', "icase" },
+ *     { PATHSPEC_RECURSIVE, '*', "recursive" },
+ *     { PATHSPEC_REGEXP, '\0', "regexp" },
+ *
+ */
+#define PATHSPEC_FROMTOP    (1<<0)
+
+static struct pathspec_magic {
+       unsigned bit;
+       char mnemonic; /* this cannot be ':'! */
+       const char *name;
+} pathspec_magic[] = {
+       { PATHSPEC_FROMTOP, '/', "top" },
+};
+
+/*
+ * Take an element of a pathspec and check for magic signatures.
+ * Append the result to the prefix.
+ *
+ * For now, we only parse the syntax and throw out anything other than
+ * "top" magic.
+ *
+ * NEEDSWORK: This needs to be rewritten when we start migrating
+ * get_pathspec() users to use the "struct pathspec" interface.  For
+ * example, a pathspec element may be marked as case-insensitive, but
+ * the prefix part must always match literally, and a single stupid
+ * string cannot express such a case.
+ */
+static const char *prefix_pathspec(const char *prefix, int prefixlen, const char *elt)
+{
+       unsigned magic = 0;
+       const char *copyfrom = elt;
+       int i;
+
+       if (elt[0] != ':') {
+               ; /* nothing to do */
+       } else if (elt[1] == '(') {
+               /* longhand */
+               const char *nextat;
+               for (copyfrom = elt + 2;
+                    *copyfrom && *copyfrom != ')';
+                    copyfrom = nextat) {
+                       size_t len = strcspn(copyfrom, ",)");
+                       if (copyfrom[len] == ')')
+                               nextat = copyfrom + len;
+                       else
+                               nextat = copyfrom + len + 1;
+                       if (!len)
+                               continue;
+                       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
+                               if (strlen(pathspec_magic[i].name) == len &&
+                                   !strncmp(pathspec_magic[i].name, copyfrom, len)) {
+                                       magic |= pathspec_magic[i].bit;
+                                       break;
+                               }
+                       if (ARRAY_SIZE(pathspec_magic) <= i)
+                               die("Invalid pathspec magic '%.*s' in '%s'",
+                                   (int) len, copyfrom, elt);
+               }
+               if (*copyfrom == ')')
+                       copyfrom++;
+       } else {
+               /* shorthand */
+               for (copyfrom = elt + 1;
+                    *copyfrom && *copyfrom != ':';
+                    copyfrom++) {
+                       char ch = *copyfrom;
+
+                       if (!is_pathspec_magic(ch))
+                               break;
+                       for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
+                               if (pathspec_magic[i].mnemonic == ch) {
+                                       magic |= pathspec_magic[i].bit;
+                                       break;
+                               }
+                       if (ARRAY_SIZE(pathspec_magic) <= i)
+                               die("Unimplemented pathspec magic '%c' in '%s'",
+                                   ch, elt);
+               }
+               if (*copyfrom == ':')
+                       copyfrom++;
+       }
+
+       if (magic & PATHSPEC_FROMTOP)
+               return xstrdup(copyfrom);
+       else
+               return prefix_path(prefix, prefixlen, copyfrom);
+}
+
 const char **get_pathspec(const char *prefix, const char **pathspec)
 {
        const char *entry = *pathspec;
@@ -144,8 +227,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
        dst = pathspec;
        prefixlen = prefix ? strlen(prefix) : 0;
        while (*src) {
-               const char *p = prefix_path(prefix, prefixlen, *src);
-               *(dst++) = p;
+               *(dst++) = prefix_pathspec(prefix, prefixlen, *src);
                src++;
        }
        *dst = NULL;
@@ -208,24 +290,6 @@ int is_inside_work_tree(void)
        return inside_work_tree;
 }
 
-/*
- * set_work_tree() is only ever called if you set GIT_DIR explicitly.
- * The old behaviour (which we retain here) is to set the work tree root
- * to the cwd, unless overridden by the config, the command line, or
- * GIT_WORK_TREE.
- */
-static const char *set_work_tree(const char *dir)
-{
-       char buffer[PATH_MAX + 1];
-
-       if (!getcwd(buffer, sizeof(buffer)))
-               die ("Could not get the current working directory");
-       git_work_tree_cfg = xstrdup(buffer);
-       inside_work_tree = 1;
-
-       return NULL;
-}
-
 void setup_work_tree(void)
 {
        const char *work_tree, *git_dir;
@@ -236,16 +300,36 @@ void setup_work_tree(void)
        work_tree = get_git_work_tree();
        git_dir = get_git_dir();
        if (!is_absolute_path(git_dir))
-               git_dir = make_absolute_path(git_dir);
+               git_dir = real_path(get_git_dir());
        if (!work_tree || chdir(work_tree))
                die("This operation must be run in a work tree");
-       set_git_dir(make_relative_path(git_dir, work_tree));
+
+       /*
+        * Make sure subsequent git processes find correct worktree
+        * if $GIT_WORK_TREE is set relative
+        */
+       if (getenv(GIT_WORK_TREE_ENVIRONMENT))
+               setenv(GIT_WORK_TREE_ENVIRONMENT, ".", 1);
+
+       set_git_dir(relative_path(git_dir, work_tree));
        initialized = 1;
 }
 
-static int check_repository_format_gently(int *nongit_ok)
+static int check_repository_format_gently(const char *gitdir, int *nongit_ok)
 {
-       git_config(check_repository_format_version, NULL);
+       char repo_config[PATH_MAX+1];
+
+       /*
+        * git_config() can't be used here because it calls git_pathdup()
+        * to get $GIT_CONFIG/config. That call will make setup_git_env()
+        * set git_dir to ".git".
+        *
+        * We are in gitdir setup, no git dir has been found useable yet.
+        * Use a gentler version of git_config() to check if this repo
+        * is a good one.
+        */
+       snprintf(repo_config, PATH_MAX, "%s/config", gitdir);
+       git_config_early(check_repository_format_version, NULL, repo_config);
        if (GIT_REPO_VERSION < repository_format_version) {
                if (!nongit_ok)
                        die ("Expected git repo version <= %d, found %d",
@@ -263,14 +347,14 @@ static int check_repository_format_gently(int *nongit_ok)
  * Try to read the location of the git directory from the .git file,
  * return path to git directory if found.
  */
-const char *read_gitfile_gently(const char *path)
+const char *read_gitfile(const char *path)
 {
        char *buf;
        char *dir;
        const char *slash;
        struct stat st;
        int fd;
-       size_t len;
+       ssize_t len;
 
        if (stat(path, &st))
                return NULL;
@@ -307,71 +391,132 @@ const char *read_gitfile_gently(const char *path)
 
        if (!is_git_directory(dir))
                die("Not a git repository: %s", dir);
-       path = make_absolute_path(dir);
+       path = real_path(dir);
 
        free(buf);
        return path;
 }
 
 static const char *setup_explicit_git_dir(const char *gitdirenv,
-                               const char *work_tree_env, int *nongit_ok)
+                                         char *cwd, int len,
+                                         int *nongit_ok)
 {
-       static char buffer[1024 + 1];
-       const char *retval;
+       const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
+       const char *worktree;
+       char *gitfile;
+       int offset;
 
        if (PATH_MAX - 40 < strlen(gitdirenv))
                die("'$%s' too big", GIT_DIR_ENVIRONMENT);
+
+       gitfile = (char*)read_gitfile(gitdirenv);
+       if (gitfile) {
+               gitfile = xstrdup(gitfile);
+               gitdirenv = gitfile;
+       }
+
        if (!is_git_directory(gitdirenv)) {
                if (nongit_ok) {
                        *nongit_ok = 1;
+                       free(gitfile);
                        return NULL;
                }
                die("Not a git repository: '%s'", gitdirenv);
        }
-       if (!work_tree_env) {
-               retval = set_work_tree(gitdirenv);
-               /* config may override worktree */
-               if (check_repository_format_gently(nongit_ok))
-                       return NULL;
-               return retval;
+
+       if (check_repository_format_gently(gitdirenv, nongit_ok)) {
+               free(gitfile);
+               return NULL;
        }
-       if (check_repository_format_gently(nongit_ok))
+
+       /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
+       if (work_tree_env)
+               set_git_work_tree(work_tree_env);
+       else if (is_bare_repository_cfg > 0) {
+               if (git_work_tree_cfg) /* #22.2, #30 */
+                       die("core.bare and core.worktree do not make sense");
+
+               /* #18, #26 */
+               set_git_dir(gitdirenv);
+               free(gitfile);
                return NULL;
-       retval = get_relative_cwd(buffer, sizeof(buffer) - 1,
-                       get_git_work_tree());
-       if (!retval || !*retval)
+       }
+       else if (git_work_tree_cfg) { /* #6, #14 */
+               if (is_absolute_path(git_work_tree_cfg))
+                       set_git_work_tree(git_work_tree_cfg);
+               else {
+                       char core_worktree[PATH_MAX];
+                       if (chdir(gitdirenv))
+                               die_errno("Could not chdir to '%s'", gitdirenv);
+                       if (chdir(git_work_tree_cfg))
+                               die_errno("Could not chdir to '%s'", git_work_tree_cfg);
+                       if (!getcwd(core_worktree, PATH_MAX))
+                               die_errno("Could not get directory '%s'", git_work_tree_cfg);
+                       if (chdir(cwd))
+                               die_errno("Could not come back to cwd");
+                       set_git_work_tree(core_worktree);
+               }
+       }
+       else /* #2, #10 */
+               set_git_work_tree(".");
+
+       /* set_git_work_tree() must have been called by now */
+       worktree = get_git_work_tree();
+
+       /* both get_git_work_tree() and cwd are already normalized */
+       if (!strcmp(cwd, worktree)) { /* cwd == worktree */
+               set_git_dir(gitdirenv);
+               free(gitfile);
                return NULL;
-       set_git_dir(make_absolute_path(gitdirenv));
-       if (chdir(work_tree_env) < 0)
-               die_errno ("Could not chdir to '%s'", work_tree_env);
-       strcat(buffer, "/");
-       return retval;
-}
+       }
 
-static int cwd_contains_git_dir(const char **gitfile_dirp)
-{
-       const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
-       *gitfile_dirp = gitfile_dir;
-       if (gitfile_dir) {
-               if (set_git_dir(gitfile_dir))
-                       die("Repository setup failed");
-               return 1;
+       offset = dir_inside_of(cwd, worktree);
+       if (offset >= 0) {      /* cwd inside worktree? */
+               set_git_dir(real_path(gitdirenv));
+               if (chdir(worktree))
+                       die_errno("Could not chdir to '%s'", worktree);
+               cwd[len++] = '/';
+               cwd[len] = '\0';
+               free(gitfile);
+               return cwd + offset;
        }
-       return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
+
+       /* cwd outside worktree */
+       set_git_dir(gitdirenv);
+       free(gitfile);
+       return NULL;
 }
 
-static const char *setup_discovered_git_dir(const char *work_tree_env,
-               int offset, int len, char *cwd, int *nongit_ok)
+static const char *setup_discovered_git_dir(const char *gitdir,
+                                           char *cwd, int offset, int len,
+                                           int *nongit_ok)
 {
-       int root_len;
+       if (check_repository_format_gently(gitdir, nongit_ok))
+               return NULL;
 
-       inside_git_dir = 0;
-       if (!work_tree_env)
-               inside_work_tree = 1;
-       root_len = offset_1st_component(cwd);
-       git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
-       if (check_repository_format_gently(nongit_ok))
+       /* --work-tree is set without --git-dir; use discovered one */
+       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
+               if (offset != len && !is_absolute_path(gitdir))
+                       gitdir = xstrdup(real_path(gitdir));
+               if (chdir(cwd))
+                       die_errno("Could not come back to cwd");
+               return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
+       }
+
+       /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
+       if (is_bare_repository_cfg > 0) {
+               set_git_dir(offset == len ? gitdir : real_path(gitdir));
+               if (chdir(cwd))
+                       die_errno("Could not come back to cwd");
                return NULL;
+       }
+
+       /* #0, #1, #5, #8, #9, #12, #13 */
+       set_git_work_tree(".");
+       if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
+               set_git_dir(gitdir);
+       inside_git_dir = 0;
+       inside_work_tree = 1;
        if (offset == len)
                return NULL;
 
@@ -382,23 +527,35 @@ static const char *setup_discovered_git_dir(const char *work_tree_env,
        return cwd + offset;
 }
 
-static const char *setup_bare_git_dir(const char *work_tree_env,
-               int offset, int len, char *cwd, int *nongit_ok)
+/* #16.1, #17.1, #20.1, #21.1, #22.1 (see t1510) */
+static const char *setup_bare_git_dir(char *cwd, int offset, int len, int *nongit_ok)
 {
        int root_len;
 
+       if (check_repository_format_gently(".", nongit_ok))
+               return NULL;
+
+       /* --work-tree is set without --git-dir; use discovered one */
+       if (getenv(GIT_WORK_TREE_ENVIRONMENT) || git_work_tree_cfg) {
+               const char *gitdir;
+
+               gitdir = offset == len ? "." : xmemdupz(cwd, offset);
+               if (chdir(cwd))
+                       die_errno("Could not come back to cwd");
+               return setup_explicit_git_dir(gitdir, cwd, len, nongit_ok);
+       }
+
        inside_git_dir = 1;
-       if (!work_tree_env)
-               inside_work_tree = 0;
+       inside_work_tree = 0;
        if (offset != len) {
                if (chdir(cwd))
                        die_errno("Cannot come back to cwd");
                root_len = offset_1st_component(cwd);
                cwd[offset > root_len ? offset : root_len] = '\0';
                set_git_dir(cwd);
-       } else
+       }
+       else
                set_git_dir(".");
-       check_repository_format_gently(nongit_ok);
        return NULL;
 }
 
@@ -428,11 +585,10 @@ static dev_t get_device_or_die(const char *path, const char *prefix)
  */
 static const char *setup_git_directory_gently_1(int *nongit_ok)
 {
-       const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
        const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
        static char cwd[PATH_MAX+1];
-       const char *gitdirenv;
-       const char *gitfile_dir;
+       const char *gitdirenv, *ret;
+       char *gitfile;
        int len, offset, ceil_offset;
        dev_t current_device = 0;
        int one_filesystem = 1;
@@ -445,6 +601,10 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
        if (nongit_ok)
                *nongit_ok = 0;
 
+       if (!getcwd(cwd, sizeof(cwd)-1))
+               die_errno("Unable to read current working directory");
+       offset = len = strlen(cwd);
+
        /*
         * If GIT_DIR is set explicitly, we're not going
         * to do any discovery, but we still do repository
@@ -452,10 +612,7 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
         */
        gitdirenv = getenv(GIT_DIR_ENVIRONMENT);
        if (gitdirenv)
-               return setup_explicit_git_dir(gitdirenv, work_tree_env, nongit_ok);
-
-       if (!getcwd(cwd, sizeof(cwd)-1))
-               die_errno("Unable to read current working directory");
+               return setup_explicit_git_dir(gitdirenv, cwd, len, nongit_ok);
 
        ceil_offset = longest_ancestor_length(cwd, env_ceiling_dirs);
        if (ceil_offset < 0 && has_dos_drive_prefix(cwd))
@@ -472,17 +629,30 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
         * - ../../.git/
         *   etc.
         */
-       offset = len = strlen(cwd);
        one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
        if (one_filesystem)
                current_device = get_device_or_die(".", NULL);
        for (;;) {
-               if (cwd_contains_git_dir(&gitfile_dir))
-                       return setup_discovered_git_dir(work_tree_env, offset,
-                                                       len, cwd, nongit_ok);
+               gitfile = (char*)read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
+               if (gitfile)
+                       gitdirenv = gitfile = xstrdup(gitfile);
+               else {
+                       if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
+                               gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
+               }
+
+               if (gitdirenv) {
+                       ret = setup_discovered_git_dir(gitdirenv,
+                                                      cwd, offset, len,
+                                                      nongit_ok);
+                       free(gitfile);
+                       return ret;
+               }
+               free(gitfile);
+
                if (is_git_directory("."))
-                       return setup_bare_git_dir(work_tree_env, offset,
-                                                       len, cwd, nongit_ok);
+                       return setup_bare_git_dir(cwd, offset, len, nongit_ok);
+
                while (--offset > ceil_offset && cwd[offset] != '/');
                if (offset <= ceil_offset)
                        return setup_nongit(cwd, nongit_ok);
@@ -512,8 +682,15 @@ const char *setup_git_directory_gently(int *nongit_ok)
        const char *prefix;
 
        prefix = setup_git_directory_gently_1(nongit_ok);
-       if (startup_info)
+       if (prefix)
+               setenv("GIT_PREFIX", prefix, 1);
+       else
+               setenv("GIT_PREFIX", "", 1);
+
+       if (startup_info) {
                startup_info->have_repository = !nongit_ok || !*nongit_ok;
+               startup_info->prefix = prefix;
+       }
        return prefix;
 }
 
@@ -590,7 +767,7 @@ int check_repository_format_version(const char *var, const char *value, void *cb
 
 int check_repository_format(void)
 {
-       return check_repository_format_gently(NULL);
+       return check_repository_format_gently(get_git_dir(), NULL);
 }
 
 /*
@@ -601,19 +778,12 @@ int check_repository_format(void)
  */
 const char *setup_git_directory(void)
 {
-       const char *retval = setup_git_directory_gently(NULL);
-
-       /* If the work tree is not the default one, recompute prefix */
-       if (inside_work_tree < 0) {
-               static char buffer[PATH_MAX + 1];
-               char *rel;
-               if (retval && chdir(retval))
-                       die_errno ("Could not jump back into original cwd");
-               rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
-               if (rel && *rel && chdir(get_git_work_tree()))
-                       die_errno ("Could not jump to working directory");
-               return rel && *rel ? strcat(rel, "/") : NULL;
-       }
+       return setup_git_directory_gently(NULL);
+}
 
-       return retval;
+const char *resolve_gitdir(const char *suspect)
+{
+       if (is_git_directory(suspect))
+               return suspect;
+       return read_gitfile(suspect);
 }