2005-04-07 22:16:10 +00:00
|
|
|
/*
|
|
|
|
* GIT - The information manager from hell
|
|
|
|
*
|
|
|
|
* Copyright (C) Linus Torvalds, 2005
|
|
|
|
*/
|
2005-04-07 22:13:13 +00:00
|
|
|
#include "cache.h"
|
2006-05-19 10:03:57 +00:00
|
|
|
#include "builtin.h"
|
2007-11-13 20:05:04 +00:00
|
|
|
#include "exec_cmd.h"
|
2005-04-07 22:13:13 +00:00
|
|
|
|
2005-08-06 19:50:14 +00:00
|
|
|
#ifndef DEFAULT_GIT_TEMPLATE_DIR
|
2007-06-11 09:10:47 +00:00
|
|
|
#define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
|
2005-08-06 19:50:14 +00:00
|
|
|
#endif
|
|
|
|
|
2006-12-31 04:53:55 +00:00
|
|
|
#ifdef NO_TRUSTABLE_FILEMODE
|
|
|
|
#define TEST_FILEMODE 0
|
|
|
|
#else
|
|
|
|
#define TEST_FILEMODE 1
|
|
|
|
#endif
|
|
|
|
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
static int init_is_bare_repository = 0;
|
|
|
|
static int init_shared_repository = -1;
|
|
|
|
|
2005-12-22 22:19:37 +00:00
|
|
|
static void safe_create_dir(const char *dir, int share)
|
2005-04-20 04:48:15 +00:00
|
|
|
{
|
2005-07-06 08:21:46 +00:00
|
|
|
if (mkdir(dir, 0777) < 0) {
|
2005-04-20 04:48:15 +00:00
|
|
|
if (errno != EEXIST) {
|
|
|
|
perror(dir);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2005-12-22 22:19:37 +00:00
|
|
|
else if (share && adjust_shared_perm(dir))
|
2009-01-04 18:38:41 +00:00
|
|
|
die("Could not make %s writable by group", dir);
|
2005-04-20 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2005-08-02 23:45:21 +00:00
|
|
|
static void copy_templates_1(char *path, int baselen,
|
|
|
|
char *template, int template_baselen,
|
|
|
|
DIR *dir)
|
|
|
|
{
|
|
|
|
struct dirent *de;
|
|
|
|
|
|
|
|
/* Note: if ".git/hooks" file exists in the repository being
|
|
|
|
* re-initialized, /etc/core-git/templates/hooks/update would
|
2008-09-13 17:18:36 +00:00
|
|
|
* cause "git init" to fail here. I think this is sane but
|
2005-08-02 23:45:21 +00:00
|
|
|
* it means that the set of templates we ship by default, along
|
|
|
|
* with the way the namespace under .git/ is organized, should
|
|
|
|
* be really carefully chosen.
|
|
|
|
*/
|
2005-12-22 22:19:37 +00:00
|
|
|
safe_create_dir(path, 1);
|
2005-08-02 23:45:21 +00:00
|
|
|
while ((de = readdir(dir)) != NULL) {
|
|
|
|
struct stat st_git, st_template;
|
|
|
|
int namelen;
|
|
|
|
int exists = 0;
|
|
|
|
|
|
|
|
if (de->d_name[0] == '.')
|
|
|
|
continue;
|
|
|
|
namelen = strlen(de->d_name);
|
|
|
|
if ((PATH_MAX <= baselen + namelen) ||
|
|
|
|
(PATH_MAX <= template_baselen + namelen))
|
|
|
|
die("insanely long template name %s", de->d_name);
|
|
|
|
memcpy(path + baselen, de->d_name, namelen+1);
|
|
|
|
memcpy(template + template_baselen, de->d_name, namelen+1);
|
|
|
|
if (lstat(path, &st_git)) {
|
|
|
|
if (errno != ENOENT)
|
|
|
|
die("cannot stat %s", path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
exists = 1;
|
|
|
|
|
|
|
|
if (lstat(template, &st_template))
|
|
|
|
die("cannot stat template %s", template);
|
|
|
|
|
|
|
|
if (S_ISDIR(st_template.st_mode)) {
|
|
|
|
DIR *subdir = opendir(template);
|
|
|
|
int baselen_sub = baselen + namelen;
|
|
|
|
int template_baselen_sub = template_baselen + namelen;
|
|
|
|
if (!subdir)
|
|
|
|
die("cannot opendir %s", template);
|
|
|
|
path[baselen_sub++] =
|
|
|
|
template[template_baselen_sub++] = '/';
|
|
|
|
path[baselen_sub] =
|
|
|
|
template[template_baselen_sub] = 0;
|
|
|
|
copy_templates_1(path, baselen_sub,
|
|
|
|
template, template_baselen_sub,
|
|
|
|
subdir);
|
|
|
|
closedir(subdir);
|
|
|
|
}
|
|
|
|
else if (exists)
|
|
|
|
continue;
|
|
|
|
else if (S_ISLNK(st_template.st_mode)) {
|
|
|
|
char lnk[256];
|
|
|
|
int len;
|
|
|
|
len = readlink(template, lnk, sizeof(lnk));
|
|
|
|
if (len < 0)
|
|
|
|
die("cannot readlink %s", template);
|
|
|
|
if (sizeof(lnk) <= len)
|
|
|
|
die("insanely long symlink %s", template);
|
|
|
|
lnk[len] = 0;
|
|
|
|
if (symlink(lnk, path))
|
|
|
|
die("cannot symlink %s %s", lnk, path);
|
|
|
|
}
|
|
|
|
else if (S_ISREG(st_template.st_mode)) {
|
|
|
|
if (copy_file(path, template, st_template.st_mode))
|
|
|
|
die("cannot copy %s to %s", template, path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
error("ignoring template %s", template);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
static void copy_templates(const char *template_dir)
|
2005-08-02 23:45:21 +00:00
|
|
|
{
|
|
|
|
char path[PATH_MAX];
|
|
|
|
char template_path[PATH_MAX];
|
2005-08-06 19:50:14 +00:00
|
|
|
int template_len;
|
2005-08-02 23:45:21 +00:00
|
|
|
DIR *dir;
|
2008-04-27 17:39:27 +00:00
|
|
|
const char *git_dir = get_git_dir();
|
|
|
|
int len = strlen(git_dir);
|
2005-08-02 23:45:21 +00:00
|
|
|
|
2007-11-13 20:05:04 +00:00
|
|
|
if (!template_dir)
|
2006-12-19 09:28:15 +00:00
|
|
|
template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
|
2008-07-13 20:31:18 +00:00
|
|
|
if (!template_dir)
|
|
|
|
template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
|
2008-07-28 06:02:04 +00:00
|
|
|
if (!template_dir[0])
|
|
|
|
return;
|
2005-08-02 23:45:21 +00:00
|
|
|
strcpy(template_path, template_dir);
|
|
|
|
template_len = strlen(template_path);
|
|
|
|
if (template_path[template_len-1] != '/') {
|
|
|
|
template_path[template_len++] = '/';
|
|
|
|
template_path[template_len] = 0;
|
|
|
|
}
|
|
|
|
dir = opendir(template_path);
|
2005-08-06 19:50:14 +00:00
|
|
|
if (!dir) {
|
2009-03-24 01:09:13 +00:00
|
|
|
warning("templates not found %s", template_dir);
|
2005-08-02 23:45:21 +00:00
|
|
|
return;
|
2005-08-06 19:50:14 +00:00
|
|
|
}
|
|
|
|
|
2005-11-26 00:03:56 +00:00
|
|
|
/* Make sure that template is from the correct vintage */
|
|
|
|
strcpy(template_path + template_len, "config");
|
|
|
|
repository_format_version = 0;
|
|
|
|
git_config_from_file(check_repository_format_version,
|
2008-05-14 17:46:53 +00:00
|
|
|
template_path, NULL);
|
2005-11-26 00:03:56 +00:00
|
|
|
template_path[template_len] = 0;
|
|
|
|
|
|
|
|
if (repository_format_version &&
|
|
|
|
repository_format_version != GIT_REPO_VERSION) {
|
2009-03-24 01:09:13 +00:00
|
|
|
warning("not copying templates of "
|
|
|
|
"a wrong format version %d from '%s'",
|
2005-11-26 00:03:56 +00:00
|
|
|
repository_format_version,
|
|
|
|
template_dir);
|
|
|
|
closedir(dir);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-08-06 19:50:14 +00:00
|
|
|
memcpy(path, git_dir, len);
|
2008-04-27 17:39:27 +00:00
|
|
|
if (len && path[len - 1] != '/')
|
|
|
|
path[len++] = '/';
|
2005-09-20 00:19:50 +00:00
|
|
|
path[len] = 0;
|
2005-08-02 23:45:21 +00:00
|
|
|
copy_templates_1(path, len,
|
|
|
|
template_path, template_len,
|
|
|
|
dir);
|
|
|
|
closedir(dir);
|
|
|
|
}
|
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
static int create_default_files(const char *template_path)
|
2005-05-30 17:20:44 +00:00
|
|
|
{
|
2008-04-27 17:39:27 +00:00
|
|
|
const char *git_dir = get_git_dir();
|
2005-05-30 17:20:44 +00:00
|
|
|
unsigned len = strlen(git_dir);
|
|
|
|
static char path[PATH_MAX];
|
2005-11-26 00:03:56 +00:00
|
|
|
struct stat st1;
|
|
|
|
char repo_version_string[10];
|
2008-03-24 15:14:52 +00:00
|
|
|
char junk[2];
|
2006-12-15 05:44:58 +00:00
|
|
|
int reinit;
|
2006-12-31 04:53:55 +00:00
|
|
|
int filemode;
|
2005-05-30 17:20:44 +00:00
|
|
|
|
|
|
|
if (len > sizeof(path)-50)
|
|
|
|
die("insane git directory %s", git_dir);
|
|
|
|
memcpy(path, git_dir, len);
|
|
|
|
|
|
|
|
if (len && path[len-1] != '/')
|
|
|
|
path[len++] = '/';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create .git/refs/{heads,tags}
|
|
|
|
*/
|
2008-04-27 17:39:27 +00:00
|
|
|
safe_create_dir(git_path("refs"), 1);
|
|
|
|
safe_create_dir(git_path("refs/heads"), 1);
|
|
|
|
safe_create_dir(git_path("refs/tags"), 1);
|
2005-05-30 17:20:44 +00:00
|
|
|
|
2005-11-26 00:03:56 +00:00
|
|
|
/* First copy the templates -- we might have the default
|
|
|
|
* config file there, in which case we would want to read
|
|
|
|
* from it after installing.
|
|
|
|
*/
|
2008-04-27 17:39:27 +00:00
|
|
|
copy_templates(template_path);
|
2005-11-26 00:03:56 +00:00
|
|
|
|
2008-05-14 17:46:53 +00:00
|
|
|
git_config(git_default_config, NULL);
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
is_bare_repository_cfg = init_is_bare_repository;
|
2009-03-25 23:19:36 +00:00
|
|
|
|
|
|
|
/* reading existing config may have overwrote it */
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
if (init_shared_repository != -1)
|
|
|
|
shared_repository = init_shared_repository;
|
2005-11-26 00:03:56 +00:00
|
|
|
|
2006-06-10 05:07:23 +00:00
|
|
|
/*
|
|
|
|
* We would have created the above under user's umask -- under
|
|
|
|
* shared-repository settings, we would need to fix them up.
|
|
|
|
*/
|
|
|
|
if (shared_repository) {
|
2008-04-27 17:39:27 +00:00
|
|
|
adjust_shared_perm(get_git_dir());
|
|
|
|
adjust_shared_perm(git_path("refs"));
|
|
|
|
adjust_shared_perm(git_path("refs/heads"));
|
|
|
|
adjust_shared_perm(git_path("refs/tags"));
|
2006-06-10 05:07:23 +00:00
|
|
|
}
|
|
|
|
|
2005-05-30 17:20:44 +00:00
|
|
|
/*
|
|
|
|
* Create the default symlink from ".git/HEAD" to the "master"
|
2005-09-30 21:26:57 +00:00
|
|
|
* branch, if it does not exist yet.
|
2005-05-30 17:20:44 +00:00
|
|
|
*/
|
|
|
|
strcpy(path + len, "HEAD");
|
2008-03-24 15:14:52 +00:00
|
|
|
reinit = (!access(path, R_OK)
|
|
|
|
|| readlink(path, junk, sizeof(junk)-1) != -1);
|
2006-12-15 05:44:58 +00:00
|
|
|
if (!reinit) {
|
2007-01-26 22:26:10 +00:00
|
|
|
if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
|
2005-05-30 17:20:44 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2005-10-25 23:43:03 +00:00
|
|
|
|
2005-11-26 00:03:56 +00:00
|
|
|
/* This forces creation of new config file */
|
|
|
|
sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
|
|
|
|
git_config_set("core.repositoryformatversion", repo_version_string);
|
|
|
|
|
|
|
|
path[len] = 0;
|
2005-10-25 23:43:03 +00:00
|
|
|
strcpy(path + len, "config");
|
2005-11-26 00:03:56 +00:00
|
|
|
|
|
|
|
/* Check filemode trustability */
|
2006-12-31 04:53:55 +00:00
|
|
|
filemode = TEST_FILEMODE;
|
|
|
|
if (TEST_FILEMODE && !lstat(path, &st1)) {
|
2005-11-26 00:03:56 +00:00
|
|
|
struct stat st2;
|
2006-12-31 04:53:55 +00:00
|
|
|
filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) &&
|
2005-11-26 00:03:56 +00:00
|
|
|
!lstat(path, &st2) &&
|
|
|
|
st1.st_mode != st2.st_mode);
|
2005-10-25 23:43:03 +00:00
|
|
|
}
|
2006-12-31 04:53:55 +00:00
|
|
|
git_config_set("core.filemode", filemode ? "true" : "false");
|
2006-12-15 05:44:58 +00:00
|
|
|
|
Clean up work-tree handling
The old version of work-tree support was an unholy mess, barely readable,
and not to the point.
For example, why do you have to provide a worktree, when it is not used?
As in "git status". Now it works.
Another riddle was: if you can have work trees inside the git dir, why
are some programs complaining that they need a work tree?
IOW it is allowed to call
$ git --git-dir=../ --work-tree=. bla
when you really want to. In this case, you are both in the git directory
and in the working tree. So, programs have to actually test for the right
thing, namely if they are inside a working tree, and not if they are
inside a git directory.
Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was
specified, unless there is a repository in the current working directory.
It does now.
The logic to determine if a repository is bare, or has a work tree
(tertium non datur), is this:
--work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true,
which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR
ends in /.git, which overrides the directory in which .git/ was found.
In related news, a long standing bug was fixed: when in .git/bla/x.git/,
which is a bare repository, git formerly assumed ../.. to be the
appropriate git dir. This problem was reported by Shawn Pearce to have
caused much pain, where a colleague mistakenly ran "git init" in "/" a
long time ago, and bare repositories just would not work.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-08-01 00:30:14 +00:00
|
|
|
if (is_bare_repository())
|
2007-01-07 10:00:28 +00:00
|
|
|
git_config_set("core.bare", "true");
|
|
|
|
else {
|
Clean up work-tree handling
The old version of work-tree support was an unholy mess, barely readable,
and not to the point.
For example, why do you have to provide a worktree, when it is not used?
As in "git status". Now it works.
Another riddle was: if you can have work trees inside the git dir, why
are some programs complaining that they need a work tree?
IOW it is allowed to call
$ git --git-dir=../ --work-tree=. bla
when you really want to. In this case, you are both in the git directory
and in the working tree. So, programs have to actually test for the right
thing, namely if they are inside a working tree, and not if they are
inside a git directory.
Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was
specified, unless there is a repository in the current working directory.
It does now.
The logic to determine if a repository is bare, or has a work tree
(tertium non datur), is this:
--work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true,
which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR
ends in /.git, which overrides the directory in which .git/ was found.
In related news, a long standing bug was fixed: when in .git/bla/x.git/,
which is a bare repository, git formerly assumed ../.. to be the
appropriate git dir. This problem was reported by Shawn Pearce to have
caused much pain, where a colleague mistakenly ran "git init" in "/" a
long time ago, and bare repositories just would not work.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-08-01 00:30:14 +00:00
|
|
|
const char *work_tree = get_git_work_tree();
|
2007-01-07 10:00:28 +00:00
|
|
|
git_config_set("core.bare", "false");
|
2007-01-23 15:51:18 +00:00
|
|
|
/* allow template config file to override the default */
|
|
|
|
if (log_all_ref_updates == -1)
|
|
|
|
git_config_set("core.logallrefupdates", "true");
|
2008-04-27 17:39:27 +00:00
|
|
|
if (prefixcmp(git_dir, work_tree) ||
|
|
|
|
strcmp(git_dir + strlen(work_tree), "/.git")) {
|
Clean up work-tree handling
The old version of work-tree support was an unholy mess, barely readable,
and not to the point.
For example, why do you have to provide a worktree, when it is not used?
As in "git status". Now it works.
Another riddle was: if you can have work trees inside the git dir, why
are some programs complaining that they need a work tree?
IOW it is allowed to call
$ git --git-dir=../ --work-tree=. bla
when you really want to. In this case, you are both in the git directory
and in the working tree. So, programs have to actually test for the right
thing, namely if they are inside a working tree, and not if they are
inside a git directory.
Also, GIT_DIR=../.git should behave the same as if no GIT_DIR was
specified, unless there is a repository in the current working directory.
It does now.
The logic to determine if a repository is bare, or has a work tree
(tertium non datur), is this:
--work-tree=bla overrides GIT_WORK_TREE, which overrides core.bare = true,
which overrides core.worktree, which overrides GIT_DIR/.. when GIT_DIR
ends in /.git, which overrides the directory in which .git/ was found.
In related news, a long standing bug was fixed: when in .git/bla/x.git/,
which is a bare repository, git formerly assumed ../.. to be the
appropriate git dir. This problem was reported by Shawn Pearce to have
caused much pain, where a colleague mistakenly ran "git init" in "/" a
long time ago, and bare repositories just would not work.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2007-08-01 00:30:14 +00:00
|
|
|
git_config_set("core.worktree", work_tree);
|
2008-04-27 17:39:27 +00:00
|
|
|
}
|
2007-01-07 10:00:28 +00:00
|
|
|
}
|
2007-08-31 07:25:04 +00:00
|
|
|
|
|
|
|
if (!reinit) {
|
2008-05-11 16:16:39 +00:00
|
|
|
/* Check if symlink is supported in the work tree */
|
2007-08-31 07:25:04 +00:00
|
|
|
path[len] = 0;
|
|
|
|
strcpy(path + len, "tXXXXXX");
|
|
|
|
if (!close(xmkstemp(path)) &&
|
|
|
|
!unlink(path) &&
|
|
|
|
!symlink("testing", path) &&
|
|
|
|
!lstat(path, &st1) &&
|
|
|
|
S_ISLNK(st1.st_mode))
|
|
|
|
unlink(path); /* good */
|
|
|
|
else
|
|
|
|
git_config_set("core.symlinks", "false");
|
2008-05-11 16:16:39 +00:00
|
|
|
|
|
|
|
/* Check if the filesystem is case-insensitive */
|
|
|
|
path[len] = 0;
|
|
|
|
strcpy(path + len, "CoNfIg");
|
|
|
|
if (!access(path, F_OK))
|
|
|
|
git_config_set("core.ignorecase", "true");
|
2007-08-31 07:25:04 +00:00
|
|
|
}
|
|
|
|
|
2006-12-15 05:44:58 +00:00
|
|
|
return reinit;
|
2005-05-30 17:20:44 +00:00
|
|
|
}
|
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
int init_db(const char *template_dir, unsigned int flags)
|
|
|
|
{
|
|
|
|
const char *sha1_dir;
|
|
|
|
char *path;
|
|
|
|
int len, reinit;
|
|
|
|
|
|
|
|
safe_create_dir(get_git_dir(), 0);
|
|
|
|
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
init_is_bare_repository = is_bare_repository();
|
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
/* Check to see if the repository version is right.
|
|
|
|
* Note that a newly created repository does not have
|
|
|
|
* config file, so this will not fail. What we are catching
|
|
|
|
* is an attempt to reinitialize new repository with an old tool.
|
|
|
|
*/
|
|
|
|
check_repository_format();
|
|
|
|
|
|
|
|
reinit = create_default_files(template_dir);
|
|
|
|
|
|
|
|
sha1_dir = get_object_directory();
|
|
|
|
len = strlen(sha1_dir);
|
|
|
|
path = xmalloc(len + 40);
|
|
|
|
memcpy(path, sha1_dir, len);
|
|
|
|
|
|
|
|
safe_create_dir(sha1_dir, 1);
|
|
|
|
strcpy(path+len, "/pack");
|
|
|
|
safe_create_dir(path, 1);
|
|
|
|
strcpy(path+len, "/info");
|
|
|
|
safe_create_dir(path, 1);
|
|
|
|
|
|
|
|
if (shared_repository) {
|
|
|
|
char buf[10];
|
|
|
|
/* We do not spell "group" and such, so that
|
|
|
|
* the configuration can be read by older version
|
|
|
|
* of git. Note, we use octal numbers for new share modes,
|
|
|
|
* and compatibility values for PERM_GROUP and
|
|
|
|
* PERM_EVERYBODY.
|
|
|
|
*/
|
2009-03-25 23:19:36 +00:00
|
|
|
if (shared_repository < 0)
|
|
|
|
/* force to the mode value */
|
|
|
|
sprintf(buf, "0%o", -shared_repository);
|
|
|
|
else if (shared_repository == PERM_GROUP)
|
2008-04-27 17:39:27 +00:00
|
|
|
sprintf(buf, "%d", OLD_PERM_GROUP);
|
|
|
|
else if (shared_repository == PERM_EVERYBODY)
|
|
|
|
sprintf(buf, "%d", OLD_PERM_EVERYBODY);
|
|
|
|
else
|
2009-03-25 23:19:36 +00:00
|
|
|
die("oops");
|
2008-04-27 17:39:27 +00:00
|
|
|
git_config_set("core.sharedrepository", buf);
|
|
|
|
git_config_set("receive.denyNonFastforwards", "true");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & INIT_DB_QUIET))
|
|
|
|
printf("%s%s Git repository in %s/\n",
|
|
|
|
reinit ? "Reinitialized existing" : "Initialized empty",
|
|
|
|
shared_repository ? " shared" : "",
|
|
|
|
get_git_dir());
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int guess_repository_type(const char *git_dir)
|
2007-08-27 07:58:06 +00:00
|
|
|
{
|
|
|
|
char cwd[PATH_MAX];
|
|
|
|
const char *slash;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "GIT_DIR=. git init" is always bare.
|
|
|
|
* "GIT_DIR=`pwd` git init" too.
|
|
|
|
*/
|
|
|
|
if (!strcmp(".", git_dir))
|
2008-04-27 17:39:27 +00:00
|
|
|
return 1;
|
2007-08-27 07:58:06 +00:00
|
|
|
if (!getcwd(cwd, sizeof(cwd)))
|
|
|
|
die("cannot tell cwd");
|
|
|
|
if (!strcmp(git_dir, cwd))
|
2008-04-27 17:39:27 +00:00
|
|
|
return 1;
|
2007-08-27 07:58:06 +00:00
|
|
|
/*
|
|
|
|
* "GIT_DIR=.git or GIT_DIR=something/.git is usually not.
|
|
|
|
*/
|
|
|
|
if (!strcmp(git_dir, ".git"))
|
2008-04-27 17:39:27 +00:00
|
|
|
return 0;
|
2007-08-27 07:58:06 +00:00
|
|
|
slash = strrchr(git_dir, '/');
|
|
|
|
if (slash && !strcmp(slash, "/.git"))
|
2008-04-27 17:39:27 +00:00
|
|
|
return 0;
|
2007-08-27 07:58:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Otherwise it is often bare. At this point
|
|
|
|
* we are just guessing.
|
|
|
|
*/
|
2008-04-27 17:39:27 +00:00
|
|
|
return 1;
|
2007-08-27 07:58:06 +00:00
|
|
|
}
|
|
|
|
|
2005-08-06 19:50:14 +00:00
|
|
|
static const char init_db_usage[] =
|
2008-07-13 13:36:15 +00:00
|
|
|
"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
|
2005-08-06 19:50:14 +00:00
|
|
|
|
2005-04-20 04:48:15 +00:00
|
|
|
/*
|
|
|
|
* If you want to, you can share the DB area with any number of branches.
|
|
|
|
* That has advantages: you can save space by sharing all the SHA1 objects.
|
|
|
|
* On the other hand, it might just make lookup slower and messier. You
|
|
|
|
* be the judge. The default case is to have one DB per managed directory.
|
|
|
|
*/
|
2006-07-29 05:44:25 +00:00
|
|
|
int cmd_init_db(int argc, const char **argv, const char *prefix)
|
2005-04-07 22:13:13 +00:00
|
|
|
{
|
2005-05-30 17:20:44 +00:00
|
|
|
const char *git_dir;
|
2006-05-19 10:03:57 +00:00
|
|
|
const char *template_dir = NULL;
|
2008-04-27 17:39:27 +00:00
|
|
|
unsigned int flags = 0;
|
|
|
|
int i;
|
2005-04-07 22:13:13 +00:00
|
|
|
|
2005-08-06 19:50:14 +00:00
|
|
|
for (i = 1; i < argc; i++, argv++) {
|
2006-05-19 10:03:57 +00:00
|
|
|
const char *arg = argv[1];
|
Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily. Leftover from this will be fixed in a separate step, including
idiotic conversions like
if (!strncmp("foo", arg, 3))
=>
if (!(-prefixcmp(arg, "foo")))
This was done by using this script in px.perl
#!/usr/bin/perl -i.bak -p
if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
}
if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
}
and running:
$ git grep -l strncmp -- '*.c' | xargs perl px.perl
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 09:53:29 +00:00
|
|
|
if (!prefixcmp(arg, "--template="))
|
2005-08-06 19:50:14 +00:00
|
|
|
template_dir = arg+11;
|
2008-05-28 18:53:57 +00:00
|
|
|
else if (!strcmp(arg, "--bare")) {
|
|
|
|
static char git_dir[PATH_MAX+1];
|
|
|
|
is_bare_repository_cfg = 1;
|
|
|
|
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
|
|
|
|
sizeof(git_dir)), 0);
|
|
|
|
} else if (!strcmp(arg, "--shared"))
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
init_shared_repository = PERM_GROUP;
|
Mechanical conversion to use prefixcmp()
This mechanically converts strncmp() to use prefixcmp(), but only when
the parameters match specific patterns, so that they can be verified
easily. Leftover from this will be fixed in a separate step, including
idiotic conversions like
if (!strncmp("foo", arg, 3))
=>
if (!(-prefixcmp(arg, "foo")))
This was done by using this script in px.perl
#!/usr/bin/perl -i.bak -p
if (/strncmp\(([^,]+), "([^\\"]*)", (\d+)\)/ && (length($2) == $3)) {
s|strncmp\(([^,]+), "([^\\"]*)", (\d+)\)|prefixcmp($1, "$2")|;
}
if (/strncmp\("([^\\"]*)", ([^,]+), (\d+)\)/ && (length($1) == $3)) {
s|strncmp\("([^\\"]*)", ([^,]+), (\d+)\)|(-prefixcmp($2, "$1"))|;
}
and running:
$ git grep -l strncmp -- '*.c' | xargs perl px.perl
Signed-off-by: Junio C Hamano <junkio@cox.net>
2007-02-20 09:53:29 +00:00
|
|
|
else if (!prefixcmp(arg, "--shared="))
|
git init: --bare/--shared overrides system/global config
If core.bare or core.sharedRepository are set in /etc/gitconfig or
~/.gitconfig, then 'git init' will read the values when constructing a
new config file; reading them, however, will override the values
specified on the command line. In the case of --bare, this ends up
causing a segfault, without the repository being properly initialised;
in the case of --shared, the permissions are set according to the
existing config settings, not what was specified on the command line.
This fix saves any specified values for --bare and --shared prior to
reading existing config settings, and restores them after reading but
before writing the new config file. core.bare is ignored in all
situations, while core.sharedRepository will only be used if --shared
is not specified to git init.
Also includes testcases which use a specified global config file
override, demonstrating the former failure scenario.
Signed-off-by: Deskin Miller <deskinm@umich.edu>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-10-07 05:37:48 +00:00
|
|
|
init_shared_repository = git_config_perm("arg", arg+9);
|
2007-06-07 12:50:29 +00:00
|
|
|
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
|
2008-04-27 17:39:27 +00:00
|
|
|
flags |= INIT_DB_QUIET;
|
2005-08-06 19:50:14 +00:00
|
|
|
else
|
2006-08-03 15:48:41 +00:00
|
|
|
usage(init_db_usage);
|
2005-08-06 19:50:14 +00:00
|
|
|
}
|
|
|
|
|
2009-03-25 23:19:36 +00:00
|
|
|
if (init_shared_repository != -1)
|
|
|
|
shared_repository = init_shared_repository;
|
|
|
|
|
2007-08-27 07:58:06 +00:00
|
|
|
/*
|
|
|
|
* GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
|
|
|
|
* without --bare. Catch the error early.
|
|
|
|
*/
|
|
|
|
git_dir = getenv(GIT_DIR_ENVIRONMENT);
|
|
|
|
if ((!git_dir || is_bare_repository_cfg == 1)
|
|
|
|
&& getenv(GIT_WORK_TREE_ENVIRONMENT))
|
|
|
|
die("%s (or --work-tree=<directory>) not allowed without "
|
|
|
|
"specifying %s (or --git-dir=<directory>)",
|
|
|
|
GIT_WORK_TREE_ENVIRONMENT,
|
|
|
|
GIT_DIR_ENVIRONMENT);
|
|
|
|
|
2005-05-30 17:20:44 +00:00
|
|
|
/*
|
|
|
|
* Set up the default .git directory contents
|
|
|
|
*/
|
2006-12-15 05:44:58 +00:00
|
|
|
if (!git_dir)
|
2005-05-30 17:20:44 +00:00
|
|
|
git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
|
2005-12-22 22:19:37 +00:00
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
if (is_bare_repository_cfg < 0)
|
|
|
|
is_bare_repository_cfg = guess_repository_type(git_dir);
|
|
|
|
|
|
|
|
if (!is_bare_repository_cfg) {
|
|
|
|
if (git_dir) {
|
|
|
|
const char *git_dir_parent = strrchr(git_dir, '/');
|
|
|
|
if (git_dir_parent) {
|
|
|
|
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
|
|
|
|
git_work_tree_cfg = xstrdup(make_absolute_path(rel));
|
|
|
|
free(rel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!git_work_tree_cfg) {
|
|
|
|
git_work_tree_cfg = xcalloc(PATH_MAX, 1);
|
|
|
|
if (!getcwd(git_work_tree_cfg, PATH_MAX))
|
|
|
|
die ("Cannot access current working directory.");
|
|
|
|
}
|
|
|
|
if (access(get_git_work_tree(), X_OK))
|
|
|
|
die ("Cannot access work tree '%s'",
|
|
|
|
get_git_work_tree());
|
2006-06-10 06:09:49 +00:00
|
|
|
}
|
2005-12-22 22:19:37 +00:00
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
set_git_dir(make_absolute_path(git_dir));
|
2006-12-15 05:44:58 +00:00
|
|
|
|
2008-04-27 17:39:27 +00:00
|
|
|
return init_db(template_dir, flags);
|
2005-04-07 22:13:13 +00:00
|
|
|
}
|