From 510c5a8ee392fd2ab954b676bae486b53f444013 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 7 Jan 2007 01:35:34 -0800 Subject: [PATCH 1/4] Move initialization of log_all_ref_updates The patches to prevent Porcelainish that require working tree from doing any damage in a bare repository make a lot of sense, and I want to make the is_bare_git_dir() function more reliable. In order to allow the repository owner override the heuristic implemented in is_bare_git_dir() if/when it misidentifies a particular repository, it would make sense to introduce a new configuration variable "[core] bare = true/false", and make is_bare_git_dir() notice it. The scripts would do a 'repo-config --bool --get core.bare' and iff the command fails (i.e. there is no such variable in the configuration file), it would use the heuristic implemented at the script level [*1*]. However, setup_git_env() which is called a lot earlier than we even read from the repository configuration currently makes a call to is_bare_git_dir(), in order to change the default setting for log_all_ref_updates. It somehow feels that this is a hack. By the way, [*1*] is another thing I hate about the current config mechanism. "git-repo-config --get" does not know what the possible configuration variables are, let alone what the default values for them are. It allows us not to maintain a centralized configuration table, which makes it easy to introduce ad-hoc variables and gives a warm fuzzy feeling of being modular, but my feeling is that it is turning out to be a rather high price to pay for scripts. Signed-off-by: Junio C Hamano --- environment.c | 5 ++--- refs.c | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/environment.c b/environment.c index 09976c7bf6..64245e77a9 100644 --- a/environment.c +++ b/environment.c @@ -15,7 +15,7 @@ int use_legacy_headers = 1; int trust_executable_bit = 1; int assume_unchanged; int prefer_symlink_refs; -int log_all_ref_updates; +int log_all_ref_updates = -1; /* unspecified */ int warn_ambiguous_refs = 1; int repository_format_version; char *git_commit_encoding; @@ -51,10 +51,9 @@ static void setup_git_env(void) git_graft_file = getenv(GRAFT_ENVIRONMENT); if (!git_graft_file) git_graft_file = xstrdup(git_path("info/grafts")); - log_all_ref_updates = !is_bare_git_dir(git_dir); } -int is_bare_git_dir (const char *dir) +int is_bare_git_dir(const char *dir) { const char *s; if (!strcmp(dir, DEFAULT_GIT_DIR_ENVIRONMENT)) diff --git a/refs.c b/refs.c index 52057455a2..b5eee11bd5 100644 --- a/refs.c +++ b/refs.c @@ -923,6 +923,9 @@ static int log_ref_write(struct ref_lock *lock, char *logrec; const char *committer; + if (log_all_ref_updates < 0) + log_all_ref_updates = !is_bare_git_dir(get_git_dir()); + if (log_all_ref_updates && (!strncmp(lock->ref_name, "refs/heads/", 11) || !strncmp(lock->ref_name, "refs/remotes/", 13))) { From 7d1864ce67d83485cf5cbc8c90fc170ee884ef16 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 7 Jan 2007 02:00:28 -0800 Subject: [PATCH 2/4] Introduce is_bare_repository() and core.bare configuration variable This removes the old is_bare_git_dir(const char *) to ask if a directory, if it is a GIT_DIR, is a bare repository, and replaces it with is_bare_repository(void *). The function looks at core.bare configuration variable if exists but uses the old heuristics: if it is ".git" or ends with "/.git", then it does not look like a bare repository, otherwise it does. Signed-off-by: Junio C Hamano --- builtin-init-db.c | 8 ++++++-- cache.h | 3 ++- config.c | 5 +++++ environment.c | 9 +++++++-- refs.c | 2 +- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/builtin-init-db.c b/builtin-init-db.c index bbef820e47..22729f01ad 100644 --- a/builtin-init-db.c +++ b/builtin-init-db.c @@ -252,9 +252,13 @@ static int create_default_files(const char *git_dir, const char *template_path) } git_config_set("core.filemode", filemode ? "true" : "false"); - /* Enable logAllRefUpdates if a working tree is attached */ - if (!is_bare_git_dir(git_dir)) + if (is_bare_repository()) { + git_config_set("core.bare", "true"); + } + else { + git_config_set("core.bare", "false"); git_config_set("core.logallrefupdates", "true"); + } return reinit; } diff --git a/cache.h b/cache.h index 36be64e386..cff25690f1 100644 --- a/cache.h +++ b/cache.h @@ -127,7 +127,8 @@ extern int cache_errno; #define CONFIG_LOCAL_ENVIRONMENT "GIT_CONFIG_LOCAL" #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH" -extern int is_bare_git_dir(const char *dir); +extern int is_bare_repository_cfg; +extern int is_bare_repository(void); extern const char *get_git_dir(void); extern char *get_object_directory(void); extern char *get_refs_directory(void); diff --git a/config.c b/config.c index 5cbd130be2..20e6ecc361 100644 --- a/config.c +++ b/config.c @@ -269,6 +269,11 @@ int git_default_config(const char *var, const char *value) return 0; } + if (!strcmp(var, "core.bare")) { + is_bare_repository_cfg = git_config_bool(var, value); + return 0; + } + if (!strcmp(var, "core.ignorestat")) { assume_unchanged = git_config_bool(var, value); return 0; diff --git a/environment.c b/environment.c index 64245e77a9..54c22f8248 100644 --- a/environment.c +++ b/environment.c @@ -15,6 +15,7 @@ int use_legacy_headers = 1; int trust_executable_bit = 1; int assume_unchanged; int prefer_symlink_refs; +int is_bare_repository_cfg = -1; /* unspecified */ int log_all_ref_updates = -1; /* unspecified */ int warn_ambiguous_refs = 1; int repository_format_version; @@ -53,9 +54,13 @@ static void setup_git_env(void) git_graft_file = xstrdup(git_path("info/grafts")); } -int is_bare_git_dir(const char *dir) +int is_bare_repository(void) { - const char *s; + const char *dir, *s; + if (0 <= is_bare_repository_cfg) + return is_bare_repository_cfg; + + dir = get_git_dir(); if (!strcmp(dir, DEFAULT_GIT_DIR_ENVIRONMENT)) return 0; s = strrchr(dir, '/'); diff --git a/refs.c b/refs.c index b5eee11bd5..499086ba61 100644 --- a/refs.c +++ b/refs.c @@ -924,7 +924,7 @@ static int log_ref_write(struct ref_lock *lock, const char *committer; if (log_all_ref_updates < 0) - log_all_ref_updates = !is_bare_git_dir(get_git_dir()); + log_all_ref_updates = !is_bare_repository(); if (log_all_ref_updates && (!strncmp(lock->ref_name, "refs/heads/", 11) || From 4b441f47cefe7f4861167a151a395606e1a16745 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 7 Jan 2007 02:17:52 -0800 Subject: [PATCH 3/4] git-fetch: allow updating the current branch in a bare repository. Sometimes, people have only fetch access into a bare repository that is used as a back-up location (or a distribution point) but does not have a push access for networking reasons, e.g. one end being behind a firewall, and updating the "current branch" in such a case is perfectly fine. This allows such a fetch without --update-head-ok, which is a flag that should never be used by end users otherwise. Signed-off-by: Junio C Hamano --- git-fetch.sh | 9 +++++---- git-sh-setup.sh | 8 ++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/git-fetch.sh b/git-fetch.sh index 466fe59e35..c58704d794 100755 --- a/git-fetch.sh +++ b/git-fetch.sh @@ -231,11 +231,12 @@ update_local_ref () { esac } -case "$update_head_ok" in -'') +# updating the current HEAD with git-fetch in a bare +# repository is always fine. +if test -z "$update_head_ok" && test $(is_bare_repository) = false +then orig_head=$(git-rev-parse --verify HEAD 2>/dev/null) - ;; -esac +fi # If --tags (and later --heads or --all) is specified, then we are # not talking about defaults stored in Pull: line of remotes or diff --git a/git-sh-setup.sh b/git-sh-setup.sh index 87b939c0e4..7fdc912167 100755 --- a/git-sh-setup.sh +++ b/git-sh-setup.sh @@ -28,6 +28,14 @@ set_reflog_action() { fi } +is_bare_repository () { + git-repo-config --bool --get core.bare || + case "$GIT_DIR" in + .git | */.git) echo false ;; + *) echo true ;; + esac +} + if [ -z "$LONG_USAGE" ] then LONG_USAGE="Usage: $0 $USAGE" From 7eff28a9b42cb0d3aad932338b2e645fc6ed8fa9 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Sat, 30 Dec 2006 23:32:38 -0500 Subject: [PATCH 4/4] Disallow working directory commands in a bare repository. If the user tries to run a porcelainish command which requires a working directory in a bare repository they may get unexpected results which are difficult to predict and may differ from command to command. Instead we should detect that the current repository is a bare repository and refuse to run the command there, as there is no working directory associated with it. [jc: updated Shawn's original somewhat -- bugs are mine.] Signed-off-by: Shawn O. Pearce Signed-off-by: Junio C Hamano --- git-am.sh | 1 + git-checkout.sh | 1 + git-clean.sh | 1 + git-commit.sh | 1 + git-merge.sh | 1 + git-pull.sh | 1 + git-rebase.sh | 1 + git-reset.sh | 1 + git-revert.sh | 1 + git-sh-setup.sh | 5 +++++ git.c | 15 +++++++++++---- 11 files changed, 25 insertions(+), 4 deletions(-) diff --git a/git-am.sh b/git-am.sh index 7c0bb6084b..f50de61049 100755 --- a/git-am.sh +++ b/git-am.sh @@ -7,6 +7,7 @@ USAGE='[--signoff] [--dotest=] [--utf8] [--binary] [--3way] or, when resuming [--skip | --resolved]' . git-sh-setup set_reflog_action am +require_work_tree git var GIT_COMMITTER_IDENT >/dev/null || exit diff --git a/git-checkout.sh b/git-checkout.sh index 92ec069a3a..a5649a0504 100755 --- a/git-checkout.sh +++ b/git-checkout.sh @@ -3,6 +3,7 @@ USAGE='[-f] [-b ] [-m] [] [...]' SUBDIRECTORY_OK=Sometimes . git-sh-setup +require_work_tree old_name=HEAD old=$(git-rev-parse --verify $old_name 2>/dev/null) diff --git a/git-clean.sh b/git-clean.sh index 071b974f49..db177a7886 100755 --- a/git-clean.sh +++ b/git-clean.sh @@ -14,6 +14,7 @@ When optional ... arguments are given, the paths affected are further limited to those that match them.' SUBDIRECTORY_OK=Yes . git-sh-setup +require_work_tree ignored= ignoredonly= diff --git a/git-commit.sh b/git-commit.sh index 04aad5e5da..557b90355c 100755 --- a/git-commit.sh +++ b/git-commit.sh @@ -6,6 +6,7 @@ USAGE='[-a] [-s] [-v] [--no-verify] [-m | -F | (-C|-c) ] [-u] [--amend] [-e] [--author ] [[-i | -o] ...]' SUBDIRECTORY_OK=Yes . git-sh-setup +require_work_tree git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t diff --git a/git-merge.sh b/git-merge.sh index 477002910e..3eef048efc 100755 --- a/git-merge.sh +++ b/git-merge.sh @@ -7,6 +7,7 @@ USAGE='[-n] [--no-commit] [--squash] [-s ] [-m=] option & USE_PAGER) setup_pager(); + if ((p->option & NOT_BARE) && is_bare_repository()) + die("%s cannot be used in a bare git directory", cmd); trace_argv_printf(argv, argc, "trace: built-in: git"); exit(p->fn(argc, argv, prefix));