@matheusbsbs / matheus-bagliano / commits / 4ca0660816
Fork 0

working from subdirectory: preparation

- prefix_filename() is like prefix_path() but can be used to name any file on the filesystem, not the files that might go into the index file. - setup_git_directory_gently() tries to find the GIT_DIR, but does not die() if called outside a git repository. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Nov 25, 2005 at 23:14 UTC 4ca0660816671f65546626b6e2c2038b6a347a8b
2 files changed +27 -3
cache.h
+2
@@ -147,8 +147,10 @@ extern char *get_graft_file(void);
147 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
148
149 extern const char **get_pathspec(const char *prefix, const char **pathspec);
150 +extern const char *setup_git_directory_gently(int *);
151 extern const char *setup_git_directory(void);
152 extern const char *prefix_path(const char *prefix, int len, const char *path);
153 +extern const char *prefix_filename(const char *prefix, int len, const char *path);
154
155 #define alloc_nr(x) (((x)+16)*3/2)
156
setup.c
+25 -3
@@ -47,6 +47,21 @@ const char *prefix_path(const char *prefix, int len, const char *path)
47 return path;
48 }
49
50 +/*
51 + * Unlike prefix_path, this should be used if the named file does
52 + * not have to interact with index entry; i.e. name of a random file
53 + * on the filesystem.
54 + */
55 +const char *prefix_filename(const char *pfx, int pfx_len, const char *arg)
56 +{
57 + static char path[PATH_MAX];
58 + if (!pfx || !*pfx || arg[0] == '/')
59 + return arg;
60 + memcpy(path, pfx, pfx_len);
61 + strcpy(path + pfx_len, arg);
62 + return path;
63 +}
64 +
65 const char **get_pathspec(const char *prefix, const char **pathspec)
66 {
67 const char *entry = *pathspec;
@@ -92,7 +107,7 @@ static int is_toplevel_directory(void)
107 return 1;
108 }
109
95 -static const char *setup_git_directory_1(void)
110 +const char *setup_git_directory_gently(int *nongit_ok)
111 {
112 static char cwd[PATH_MAX+1];
113 int len, offset;
@@ -139,8 +154,15 @@ static const char *setup_git_directory_1(void)
154 break;
155 chdir("..");
156 do {
142 - if (!offset)
157 + if (!offset) {
158 + if (nongit_ok) {
159 + if (chdir(cwd))
160 + die("Cannot come back to cwd");
161 + *nongit_ok = 1;
162 + return NULL;
163 + }
164 die("Not a git repository");
165 + }
166 } while (cwd[--offset] != '/');
167 }
168
@@ -172,7 +194,7 @@ int check_repository_format(void)
194
195 const char *setup_git_directory(void)
196 {
175 - const char *retval = setup_git_directory_1();
197 + const char *retval = setup_git_directory_gently(NULL);
198 check_repository_format();
199 return retval;
200 }