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 <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
This commit is contained in:
parent
4b441f47ce
commit
7eff28a9b4
@ -7,6 +7,7 @@ USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
|
|||||||
or, when resuming [--skip | --resolved]'
|
or, when resuming [--skip | --resolved]'
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
set_reflog_action am
|
set_reflog_action am
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
git var GIT_COMMITTER_IDENT >/dev/null || exit
|
git var GIT_COMMITTER_IDENT >/dev/null || exit
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
|
USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
|
||||||
SUBDIRECTORY_OK=Sometimes
|
SUBDIRECTORY_OK=Sometimes
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
old_name=HEAD
|
old_name=HEAD
|
||||||
old=$(git-rev-parse --verify $old_name 2>/dev/null)
|
old=$(git-rev-parse --verify $old_name 2>/dev/null)
|
||||||
|
@ -14,6 +14,7 @@ When optional <paths>... arguments are given, the paths
|
|||||||
affected are further limited to those that match them.'
|
affected are further limited to those that match them.'
|
||||||
SUBDIRECTORY_OK=Yes
|
SUBDIRECTORY_OK=Yes
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
ignored=
|
ignored=
|
||||||
ignoredonly=
|
ignoredonly=
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
|
USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
|
||||||
SUBDIRECTORY_OK=Yes
|
SUBDIRECTORY_OK=Yes
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
|
git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commi
|
|||||||
|
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
set_reflog_action "merge $*"
|
set_reflog_action "merge $*"
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
test -z "$(git ls-files -u)" ||
|
test -z "$(git ls-files -u)" ||
|
||||||
die "You are in a middle of conflicted merge."
|
die "You are in a middle of conflicted merge."
|
||||||
|
@ -8,6 +8,7 @@ USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <rep
|
|||||||
LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
|
LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
set_reflog_action "pull $*"
|
set_reflog_action "pull $*"
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
test -z "$(git ls-files -u)" ||
|
test -z "$(git ls-files -u)" ||
|
||||||
die "You are in a middle of conflicted merge."
|
die "You are in a middle of conflicted merge."
|
||||||
|
@ -29,6 +29,7 @@ Example: git-rebase master~1 topic
|
|||||||
'
|
'
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
set_reflog_action rebase
|
set_reflog_action rebase
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
RESOLVEMSG="
|
RESOLVEMSG="
|
||||||
When you have resolved this problem run \"git rebase --continue\".
|
When you have resolved this problem run \"git rebase --continue\".
|
||||||
|
@ -6,6 +6,7 @@ USAGE='[--mixed | --soft | --hard] [<commit-ish>] [ [--] <paths>...]'
|
|||||||
SUBDIRECTORY_OK=Yes
|
SUBDIRECTORY_OK=Yes
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
set_reflog_action "reset $*"
|
set_reflog_action "reset $*"
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
update= reset_type=--mixed
|
update= reset_type=--mixed
|
||||||
unset rev
|
unset rev
|
||||||
|
@ -19,6 +19,7 @@ case "$0" in
|
|||||||
die "What are you talking about?" ;;
|
die "What are you talking about?" ;;
|
||||||
esac
|
esac
|
||||||
. git-sh-setup
|
. git-sh-setup
|
||||||
|
require_work_tree
|
||||||
|
|
||||||
no_commit=
|
no_commit=
|
||||||
while case "$#" in 0) break ;; esac
|
while case "$#" in 0) break ;; esac
|
||||||
|
@ -36,6 +36,11 @@ is_bare_repository () {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
require_work_tree () {
|
||||||
|
test $(is_bare_repository) = false ||
|
||||||
|
die "fatal: $0 cannot be used without a working tree."
|
||||||
|
}
|
||||||
|
|
||||||
if [ -z "$LONG_USAGE" ]
|
if [ -z "$LONG_USAGE" ]
|
||||||
then
|
then
|
||||||
LONG_USAGE="Usage: $0 $USAGE"
|
LONG_USAGE="Usage: $0 $USAGE"
|
||||||
|
15
git.c
15
git.c
@ -199,6 +199,11 @@ const char git_version_string[] = GIT_VERSION;
|
|||||||
|
|
||||||
#define RUN_SETUP (1<<0)
|
#define RUN_SETUP (1<<0)
|
||||||
#define USE_PAGER (1<<1)
|
#define USE_PAGER (1<<1)
|
||||||
|
/*
|
||||||
|
* require working tree to be present -- anything uses this needs
|
||||||
|
* RUN_SETUP for reading from the configuration file.
|
||||||
|
*/
|
||||||
|
#define NOT_BARE (1<<2)
|
||||||
|
|
||||||
static void handle_internal_command(int argc, const char **argv, char **envp)
|
static void handle_internal_command(int argc, const char **argv, char **envp)
|
||||||
{
|
{
|
||||||
@ -208,7 +213,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
|
|||||||
int (*fn)(int, const char **, const char *);
|
int (*fn)(int, const char **, const char *);
|
||||||
int option;
|
int option;
|
||||||
} commands[] = {
|
} commands[] = {
|
||||||
{ "add", cmd_add, RUN_SETUP },
|
{ "add", cmd_add, RUN_SETUP | NOT_BARE },
|
||||||
{ "annotate", cmd_annotate, },
|
{ "annotate", cmd_annotate, },
|
||||||
{ "apply", cmd_apply },
|
{ "apply", cmd_apply },
|
||||||
{ "archive", cmd_archive },
|
{ "archive", cmd_archive },
|
||||||
@ -239,7 +244,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
|
|||||||
{ "mailinfo", cmd_mailinfo },
|
{ "mailinfo", cmd_mailinfo },
|
||||||
{ "mailsplit", cmd_mailsplit },
|
{ "mailsplit", cmd_mailsplit },
|
||||||
{ "merge-file", cmd_merge_file },
|
{ "merge-file", cmd_merge_file },
|
||||||
{ "mv", cmd_mv, RUN_SETUP },
|
{ "mv", cmd_mv, RUN_SETUP | NOT_BARE },
|
||||||
{ "name-rev", cmd_name_rev, RUN_SETUP },
|
{ "name-rev", cmd_name_rev, RUN_SETUP },
|
||||||
{ "pack-objects", cmd_pack_objects, RUN_SETUP },
|
{ "pack-objects", cmd_pack_objects, RUN_SETUP },
|
||||||
{ "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
|
{ "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
|
||||||
@ -252,8 +257,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
|
|||||||
{ "rerere", cmd_rerere, RUN_SETUP },
|
{ "rerere", cmd_rerere, RUN_SETUP },
|
||||||
{ "rev-list", cmd_rev_list, RUN_SETUP },
|
{ "rev-list", cmd_rev_list, RUN_SETUP },
|
||||||
{ "rev-parse", cmd_rev_parse, RUN_SETUP },
|
{ "rev-parse", cmd_rev_parse, RUN_SETUP },
|
||||||
{ "rm", cmd_rm, RUN_SETUP },
|
{ "rm", cmd_rm, RUN_SETUP | NOT_BARE },
|
||||||
{ "runstatus", cmd_runstatus, RUN_SETUP },
|
{ "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
|
||||||
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
|
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
|
||||||
{ "show-branch", cmd_show_branch, RUN_SETUP },
|
{ "show-branch", cmd_show_branch, RUN_SETUP },
|
||||||
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
|
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
|
||||||
@ -290,6 +295,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
|
|||||||
prefix = setup_git_directory();
|
prefix = setup_git_directory();
|
||||||
if (p->option & USE_PAGER)
|
if (p->option & USE_PAGER)
|
||||||
setup_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");
|
trace_argv_printf(argv, argc, "trace: built-in: git");
|
||||||
|
|
||||||
exit(p->fn(argc, argv, prefix));
|
exit(p->fn(argc, argv, prefix));
|
||||||
|
Loading…
Reference in New Issue
Block a user