Library code for user-relative paths, take three.
This patch provides the work-horse of the user-relative paths feature, using Linus' idea of a blind chdir() and getcwd() which makes it remarkably simple. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Junio C Hamano <junkio@cox.net>
Andreas Ericsson committed
Nov 17, 2005 at 20:37 UTC
54f4b87454824fedc629219620ce9b7cdcab3d27
2 files changed
+73
cache.h
+1
@@ -192,6 +192,7 @@ extern int diff_rename_limit_default;
192
193
/* Return a statically allocated filename matching the sha1 signature */
194
extern char *mkpath(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
195
+extern char *enter_repo(char *path, int strict);
196
extern char *git_path(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
197
extern char *sha1_file_name(const unsigned char *sha1);
198
extern char *sha1_pack_name(const unsigned char *sha1);
path.c
+72
@@ -11,6 +11,7 @@
11
* which is what it's designed for.
12
*/
13
#include "cache.h"
14
+#include <pwd.h>
15
16
static char pathname[PATH_MAX];
17
static char bad_path[] = "/bad-path/";
@@ -89,3 +90,74 @@ char *safe_strncpy(char *dest, const char *src, size_t n)
90
91
return dest;
92
}
93
+
94
+static char *current_dir()
95
+{
96
+ return getcwd(pathname, sizeof(pathname));
97
+}
98
+
99
+/* Take a raw path from is_git_repo() and canonicalize it using Linus'
100
+ * idea of a blind chdir() and getcwd(). */
101
+static const char *canonical_path(char *path, int strict)
102
+{
103
+ char *dir = path;
104
+
105
+ if(strict && *dir != '/')
106
+ return NULL;
107
+
108
+ if(*dir == '~') { /* user-relative path */
109
+ struct passwd *pw;
110
+ char *slash = strchr(dir, '/');
111
+
112
+ dir++;
113
+ /* '~/' and '~' (no slash) means users own home-dir */
114
+ if(!*dir || *dir == '/')
115
+ pw = getpwuid(getuid());
116
+ else {
117
+ if (slash) {
118
+ *slash = '\0';
119
+ pw = getpwnam(dir);
120
+ *slash = '/';
121
+ }
122
+ else
123
+ pw = getpwnam(dir);
124
+ }
125
+
126
+ /* make sure we got something back that we can chdir() to */
127
+ if(!pw || chdir(pw->pw_dir) < 0)
128
+ return NULL;
129
+
130
+ if(!slash || !slash[1]) /* no path following username */
131
+ return current_dir();
132
+
133
+ dir = slash + 1;
134
+ }
135
+
136
+ /* ~foo/path/to/repo is now path/to/repo and we're in foo's homedir */
137
+ if(chdir(dir) < 0)
138
+ return NULL;
139
+
140
+ return current_dir();
141
+}
142
+
143
+char *enter_repo(char *path, int strict)
144
+{
145
+ if(!path)
146
+ return NULL;
147
+
148
+ if(!canonical_path(path, strict)) {
149
+ if(strict || !canonical_path(mkpath("%s.git", path), strict))
150
+ return NULL;
151
+ }
152
+
153
+ /* This is perfectly safe, and people tend to think of the directory
154
+ * where they ran git-init-db as their repository, so humour them. */
155
+ (void)chdir(".git");
156
+
157
+ if(access("objects", X_OK) == 0 && access("refs", X_OK) == 0) {
158
+ putenv("GIT_DIR=.");
159
+ return current_dir();
160
+ }
161
+
162
+ return NULL;
163
+}