aboutsummaryrefslogtreecommitdiff
path: root/builtin/init-db.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2011-03-19 22:16:56 +0700
committerJunio C Hamano <gitster@pobox.com>2011-03-19 21:48:19 -0700
commitb57fb80a7d7d19102b31ab94a28ed43ea1ee07bb (patch)
treec960ce2a4e566f9341f1623cf1f632cbda591166 /builtin/init-db.c
parent9d379f4fd0a845aba3efa44b254d8e3905c3c029 (diff)
downloadgit-b57fb80a7d7d19102b31ab94a28ed43ea1ee07bb.tar.gz
git-b57fb80a7d7d19102b31ab94a28ed43ea1ee07bb.tar.xz
init, clone: support --separate-git-dir for .git file
--separate-git-dir tells git to create git dir at the specified location, instead of where it is supposed to be. A .git file that points to that location will be put in place so that it appears normal to repo discovery process. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/init-db.c')
-rw-r--r--builtin/init-db.c68
1 files changed, 65 insertions, 3 deletions
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 8f5cfd712..887939923 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -21,6 +21,7 @@
static int init_is_bare_repository = 0;
static int init_shared_repository = -1;
static const char *init_db_template_dir;
+static const char *git_link;
static void safe_create_dir(const char *dir, int share)
{
@@ -311,11 +312,67 @@ static void create_object_directory(void)
free(path);
}
+int set_git_dir_init(const char *git_dir, const char *real_git_dir,
+ int exist_ok)
+{
+ if (real_git_dir) {
+ struct stat st;
+
+ if (!exist_ok && !stat(git_dir, &st))
+ die("%s already exists", git_dir);
+
+ if (!exist_ok && !stat(real_git_dir, &st))
+ die("%s already exists", real_git_dir);
+
+ /*
+ * make sure symlinks are resolved because we'll be
+ * moving the target repo later on in separate_git_dir()
+ */
+ git_link = xstrdup(real_path(git_dir));
+ }
+ else {
+ real_git_dir = real_path(git_dir);
+ git_link = NULL;
+ }
+ set_git_dir(real_path(real_git_dir));
+ return 0;
+}
+
+static void separate_git_dir(const char *git_dir)
+{
+ struct stat st;
+ FILE *fp;
+
+ if (!stat(git_link, &st)) {
+ const char *src;
+
+ if (S_ISREG(st.st_mode))
+ src = read_gitfile_gently(git_link);
+ else if (S_ISDIR(st.st_mode))
+ src = git_link;
+ else
+ die("unable to handle file type %d", st.st_mode);
+
+ if (rename(src, git_dir))
+ die_errno("unable to move %s to %s", src, git_dir);
+ }
+
+ fp = fopen(git_link, "w");
+ if (!fp)
+ die("Could not create git link %s", git_link);
+ fprintf(fp, "gitdir: %s\n", git_dir);
+ fclose(fp);
+}
+
int init_db(const char *template_dir, unsigned int flags)
{
int reinit;
+ const char *git_dir = get_git_dir();
- safe_create_dir(get_git_dir(), 0);
+ if (git_link)
+ separate_git_dir(git_dir);
+
+ safe_create_dir(git_dir, 0);
init_is_bare_repository = is_bare_repository();
@@ -352,7 +409,6 @@ int init_db(const char *template_dir, unsigned int flags)
}
if (!(flags & INIT_DB_QUIET)) {
- const char *git_dir = get_git_dir();
int len = strlen(git_dir);
printf("%s%s Git repository in %s%s\n",
reinit ? "Reinitialized existing" : "Initialized empty",
@@ -414,6 +470,7 @@ static const char *const init_db_usage[] = {
int cmd_init_db(int argc, const char **argv, const char *prefix)
{
const char *git_dir;
+ const char *real_git_dir = NULL;
const char *work_tree;
const char *template_dir = NULL;
unsigned int flags = 0;
@@ -427,11 +484,16 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
"specify that the git repository is to be shared amongst several users",
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, shared_callback, 0},
OPT_BIT('q', "quiet", &flags, "be quiet", INIT_DB_QUIET),
+ OPT_STRING('L', "separate-git-dir", &real_git_dir, "gitdir",
+ "separate git dir from working tree"),
OPT_END()
};
argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
+ if (real_git_dir && !is_absolute_path(real_git_dir))
+ real_git_dir = xstrdup(real_path(real_git_dir));
+
if (argc == 1) {
int mkdir_tried = 0;
retry:
@@ -522,7 +584,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
set_git_work_tree(real_path(work_tree));
}
- set_git_dir(real_path(git_dir));
+ set_git_dir_init(git_dir, real_git_dir, 1);
return init_db(template_dir, flags);
}