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>
Shawn O. Pearce committed
Dec 30, 2006 at 23:32 UTC
7eff28a9b42cb0d3aad932338b2e645fc6ed8fa9
11 files changed
+25
-4
git-am.sh
+1
@@ -7,6 +7,7 @@ USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
7
or, when resuming [--skip | --resolved]'
8
. git-sh-setup
9
set_reflog_action am
10
+require_work_tree
11
12
git var GIT_COMMITTER_IDENT >/dev/null || exit
13
git-checkout.sh
+1
@@ -3,6 +3,7 @@
3
USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4
SUBDIRECTORY_OK=Sometimes
5
. git-sh-setup
6
+require_work_tree
7
8
old_name=HEAD
9
old=$(git-rev-parse --verify $old_name 2>/dev/null)
git-clean.sh
+1
@@ -14,6 +14,7 @@ When optional <paths>... arguments are given, the paths
14
affected are further limited to those that match them.'
15
SUBDIRECTORY_OK=Yes
16
. git-sh-setup
17
+require_work_tree
18
19
ignored=
20
ignoredonly=
git-commit.sh
+1
@@ -6,6 +6,7 @@
6
USAGE='[-a] [-s] [-v] [--no-verify] [-m <message> | -F <logfile> | (-C|-c) <commit>] [-u] [--amend] [-e] [--author <author>] [[-i | -o] <path>...]'
7
SUBDIRECTORY_OK=Yes
8
. git-sh-setup
9
+require_work_tree
10
11
git-rev-parse --verify HEAD >/dev/null 2>&1 || initial_commit=t
12
git-merge.sh
+1
@@ -7,6 +7,7 @@ USAGE='[-n] [--no-commit] [--squash] [-s <strategy>] [-m=<merge-message>] <commi
7
8
. git-sh-setup
9
set_reflog_action "merge $*"
10
+require_work_tree
11
12
test -z "$(git ls-files -u)" ||
13
die "You are in a middle of conflicted merge."
git-pull.sh
+1
@@ -8,6 +8,7 @@ USAGE='[-n | --no-summary] [--no-commit] [-s strategy]... [<fetch-options>] <rep
8
LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
9
. git-sh-setup
10
set_reflog_action "pull $*"
11
+require_work_tree
12
13
test -z "$(git ls-files -u)" ||
14
die "You are in a middle of conflicted merge."
git-rebase.sh
+1
@@ -29,6 +29,7 @@ Example: git-rebase master~1 topic
29
'
30
. git-sh-setup
31
set_reflog_action rebase
32
+require_work_tree
33
34
RESOLVEMSG="
35
When you have resolved this problem run \"git rebase --continue\".
git-reset.sh
+1
@@ -6,6 +6,7 @@ USAGE='[--mixed | --soft | --hard] [<commit-ish>] [ [--] <paths>...]'
6
SUBDIRECTORY_OK=Yes
7
. git-sh-setup
8
set_reflog_action "reset $*"
9
+require_work_tree
10
11
update= reset_type=--mixed
12
unset rev
git-revert.sh
+1
@@ -19,6 +19,7 @@ case "$0" in
19
die "What are you talking about?" ;;
20
esac
21
. git-sh-setup
22
+require_work_tree
23
24
no_commit=
25
while case "$#" in 0) break ;; esac
git-sh-setup.sh
+5
@@ -36,6 +36,11 @@ is_bare_repository () {
36
esac
37
}
38
39
+require_work_tree () {
40
+ test $(is_bare_repository) = false ||
41
+ die "fatal: $0 cannot be used without a working tree."
42
+}
43
+
44
if [ -z "$LONG_USAGE" ]
45
then
46
LONG_USAGE="Usage: $0 $USAGE"
git.c
+11
-4
@@ -199,6 +199,11 @@ const char git_version_string[] = GIT_VERSION;
199
200
#define RUN_SETUP (1<<0)
201
#define USE_PAGER (1<<1)
202
+/*
203
+ * require working tree to be present -- anything uses this needs
204
+ * RUN_SETUP for reading from the configuration file.
205
+ */
206
+#define NOT_BARE (1<<2)
207
208
static void handle_internal_command(int argc, const char **argv, char **envp)
209
{
@@ -208,7 +213,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
213
int (*fn)(int, const char **, const char *);
214
int option;
215
} commands[] = {
211
- { "add", cmd_add, RUN_SETUP },
216
+ { "add", cmd_add, RUN_SETUP | NOT_BARE },
217
{ "annotate", cmd_annotate, },
218
{ "apply", cmd_apply },
219
{ "archive", cmd_archive },
@@ -239,7 +244,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
244
{ "mailinfo", cmd_mailinfo },
245
{ "mailsplit", cmd_mailsplit },
246
{ "merge-file", cmd_merge_file },
242
- { "mv", cmd_mv, RUN_SETUP },
247
+ { "mv", cmd_mv, RUN_SETUP | NOT_BARE },
248
{ "name-rev", cmd_name_rev, RUN_SETUP },
249
{ "pack-objects", cmd_pack_objects, RUN_SETUP },
250
{ "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
@@ -252,8 +257,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
257
{ "rerere", cmd_rerere, RUN_SETUP },
258
{ "rev-list", cmd_rev_list, RUN_SETUP },
259
{ "rev-parse", cmd_rev_parse, RUN_SETUP },
255
- { "rm", cmd_rm, RUN_SETUP },
256
- { "runstatus", cmd_runstatus, RUN_SETUP },
260
+ { "rm", cmd_rm, RUN_SETUP | NOT_BARE },
261
+ { "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
262
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
263
{ "show-branch", cmd_show_branch, RUN_SETUP },
264
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
@@ -290,6 +295,8 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
295
prefix = setup_git_directory();
296
if (p->option & USE_PAGER)
297
setup_pager();
298
+ if ((p->option & NOT_BARE) && is_bare_repository())
299
+ die("%s cannot be used in a bare git directory", cmd);
300
trace_argv_printf(argv, argc, "trace: built-in: git");
301
302
exit(p->fn(argc, argv, prefix));