#include "cache.h"
static int force = 0, quiet = 0, not_new = 0, refresh_cache = 0;
+static const char *base_dir = "";
+static int base_dir_len = 0;
static void create_directories(const char *path)
{
if (mkdir(buf, 0755)) {
if (errno == EEXIST) {
struct stat st;
- if (!lstat(buf, &st) && S_ISDIR(st.st_mode))
- continue; /* ok */
- if (force && !unlink(buf) && !mkdir(buf, 0755))
+ if (len > base_dir_len && force && !unlink(buf) && !mkdir(buf, 0755))
continue;
+ if (!stat(buf, &st) && S_ISDIR(st.st_mode))
+ continue; /* ok */
}
die("cannot create directory at %s", buf);
}
return 0;
}
-static int checkout_entry(struct cache_entry *ce, const char *base_dir)
+static int checkout_entry(struct cache_entry *ce)
{
struct stat st;
static char path[MAXPATHLEN+1];
- int len = strlen(base_dir);
+ int len = base_dir_len;
memcpy(path, base_dir, len);
strcpy(path + len, ce->name);
return write_entry(ce, path);
}
-static int checkout_file(const char *name, const char *base_dir)
+static int checkout_file(const char *name)
{
int pos = cache_name_pos(name, strlen(name));
if (pos < 0) {
}
return -1;
}
- return checkout_entry(active_cache[pos], base_dir);
+ return checkout_entry(active_cache[pos]);
}
-static int checkout_all(const char *base_dir)
+static int checkout_all(void)
{
int i;
struct cache_entry *ce = active_cache[i];
if (ce_stage(ce))
continue;
- if (checkout_entry(ce, base_dir) < 0)
+ if (checkout_entry(ce) < 0)
return -1;
}
return 0;
int main(int argc, char **argv)
{
int i, force_filename = 0;
- const char *base_dir = "";
struct cache_file cache_file;
int newfd = -1;
const char *arg = argv[i];
if (!force_filename) {
if (!strcmp(arg, "-a")) {
- checkout_all(base_dir);
+ checkout_all();
continue;
}
if (!strcmp(arg, "--")) {
}
if (!memcmp(arg, "--prefix=", 9)) {
base_dir = arg+9;
+ base_dir_len = strlen(base_dir);
continue;
}
}
- if (base_dir[0]) {
+ if (base_dir_len) {
/* when --prefix is specified we do not
* want to update cache.
*/
}
refresh_cache = 0;
}
- checkout_file(arg, base_dir);
+ checkout_file(arg);
}
if (0 <= newfd &&
--- /dev/null
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git-checkout-cache --prefix test.
+
+This test makes sure that --prefix option works as advertised, and
+also verifies that such leading path may contain symlinks, unlike
+the GIT controlled paths.
+'
+
+. ./test-lib.sh
+
+test_expect_success \
+ 'setup' \
+ 'mkdir path1 &&
+ echo frotz >path0 &&
+ echo rezrov >path1/file1 &&
+ git-update-cache --add path0 path1/file1'
+
+test_expect_success \
+ 'have symlink in place where dir is expected.' \
+ 'rm -fr path0 path1 &&
+ mkdir path2 &&
+ ln -s path2 path1 &&
+ git-checkout-cache -f -a &&
+ test ! -h path1 && test -d path1 &&
+ test -f path1/file1 && test ! -f path2/file1'
+
+test_expect_success \
+ 'use --prefix=path2/' \
+ 'rm -fr path0 path1 path2 &&
+ mkdir path2 &&
+ git-checkout-cache --prefix=path2/ -f -a &&
+ test -f path2/path0 &&
+ test -f path2/path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1'
+
+test_expect_success \
+ 'use --prefix=tmp-' \
+ 'rm -fr path0 path1 path2 tmp* &&
+ git-checkout-cache --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test -f tmp-path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1'
+
+test_expect_success \
+ 'use --prefix=tmp- but with a conflicting file and dir' \
+ 'rm -fr path0 path1 path2 tmp* &&
+ echo nitfol >tmp-path1 &&
+ mkdir tmp-path0 &&
+ git-checkout-cache --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test -f tmp-path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1'
+
+# Linus fix #1
+test_expect_success \
+ 'use --prefix=tmp/orary/ where tmp is a symlink' \
+ 'rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 tmp1/orary &&
+ ln -s tmp1 tmp &&
+ git-checkout-cache --prefix=tmp/orary/ -f -a &&
+ test -d tmp1/orary &&
+ test -f tmp1/orary/path0 &&
+ test -f tmp1/orary/path1/file1 &&
+ test -h tmp'
+
+# Linus fix #2
+test_expect_success \
+ 'use --prefix=tmp/orary- where tmp is a symlink' \
+ 'rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 &&
+ ln -s tmp1 tmp &&
+ git-checkout-cache --prefix=tmp/orary- -f -a &&
+ test -f tmp1/orary-path0 &&
+ test -f tmp1/orary-path1/file1 &&
+ test -h tmp'
+
+# Linus fix #3
+test_expect_success \
+ 'use --prefix=tmp- where tmp-path1 is a symlink' \
+ 'rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 &&
+ ln -s tmp1 tmp-path1 &&
+ git-checkout-cache --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test ! -h tmp-path1 &&
+ test -d tmp-path1 &&
+ test -f tmp-path1/file1'
+