Skip to content

Commit

Permalink
Merge branch 'ei/worktree+filter'
Browse files Browse the repository at this point in the history
* ei/worktree+filter:
  filter-branch: always export GIT_DIR if it is set
  setup_git_directory: fix segfault if repository is found in cwd
  test GIT_WORK_TREE
  extend rev-parse test for --is-inside-work-tree
  Use new semantics of is_bare/inside_git_dir/inside_work_tree
  introduce GIT_WORK_TREE to specify the work tree
  test git rev-parse
  rev-parse: introduce --is-bare-repository
  rev-parse: document --is-inside-git-dir
  • Loading branch information
Junio C Hamano committed Jul 1, 2007
2 parents cd5ada9 + 9489d0f commit 0305b63
Show file tree
Hide file tree
Showing 15 changed files with 406 additions and 82 deletions.
7 changes: 7 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ repository that ends in "/.git" is assumed to be not bare (bare =
false), while all other repositories are assumed to be bare (bare
= true).

core.worktree::
Set the path to the working tree. The value will not be
used in combination with repositories found automatically in
a .git directory (i.e. $GIT_DIR is not set).
This can be overriden by the GIT_WORK_TREE environment
variable and the '--work-tree' command line option.

core.logAllRefUpdates::
Updates to a ref <ref> is logged to the file
"$GIT_DIR/logs/<ref>", by appending the new and old
Expand Down
11 changes: 9 additions & 2 deletions Documentation/git-rev-parse.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,15 @@ OPTIONS
Show `$GIT_DIR` if defined else show the path to the .git directory.

--is-inside-git-dir::
Return "true" if we are in the git directory, otherwise "false".
Some commands require to be run in a working directory.
When the current working directory is below the repository
directory print "true", otherwise "false".

--is-inside-work-tree::
When the current working directory is inside the work tree of the
repository print "true", otherwise "false".

--is-bare-repository::
When the repository is bare print "true", otherwise "false".

--short, --short=number::
Instead of outputting the full SHA1 values of object names try to
Expand Down
18 changes: 17 additions & 1 deletion Documentation/git.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ SYNOPSIS
--------
[verse]
'git' [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate]
[--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]
[--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]

DESCRIPTION
-----------
Expand Down Expand Up @@ -103,6 +104,14 @@ OPTIONS
Set the path to the repository. This can also be controlled by
setting the GIT_DIR environment variable.

--work-tree=<path>::
Set the path to the working tree. The value will not be
used in combination with repositories found automatically in
a .git directory (i.e. $GIT_DIR is not set).
This can also be controlled by setting the GIT_WORK_TREE
environment variable and the core.worktree configuration
variable.

--bare::
Same as --git-dir=`pwd`.

Expand Down Expand Up @@ -347,6 +356,13 @@ git so take care if using Cogito etc.
specifies a path to use instead of the default `.git`
for the base of the repository.

'GIT_WORK_TREE'::
Set the path to the working tree. The value will not be
used in combination with repositories found automatically in
a .git directory (i.e. $GIT_DIR is not set).
This can also be controlled by the '--work-tree' command line
option and the core.worktree configuration variable.

git Commits
~~~~~~~~~~~
'GIT_AUTHOR_NAME'::
Expand Down
2 changes: 1 addition & 1 deletion builtin-ls-files.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ int cmd_ls_files(int argc, const char **argv, const char *prefix)
}

if (require_work_tree &&
(is_bare_repository() || is_inside_git_dir()))
(!is_inside_work_tree() || is_inside_git_dir()))
die("This operation must be run in a work tree");

pathspec = get_pathspec(prefix, argv + i);
Expand Down
10 changes: 10 additions & 0 deletions builtin-rev-parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
: "false");
continue;
}
if (!strcmp(arg, "--is-inside-work-tree")) {
printf("%s\n", is_inside_work_tree() ? "true"
: "false");
continue;
}
if (!strcmp(arg, "--is-bare-repository")) {
printf("%s\n", is_bare_repository() ? "true"
: "false");
continue;
}
if (!prefixcmp(arg, "--since=")) {
show_datestring("--max-age=", arg+8);
continue;
Expand Down
2 changes: 2 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ enum object_type {
};

#define GIT_DIR_ENVIRONMENT "GIT_DIR"
#define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
Expand All @@ -207,6 +208,7 @@ enum object_type {
extern int is_bare_repository_cfg;
extern int is_bare_repository(void);
extern int is_inside_git_dir(void);
extern int is_inside_work_tree(void);
extern const char *get_git_dir(void);
extern char *get_object_directory(void);
extern char *get_refs_directory(void);
Expand Down
1 change: 1 addition & 0 deletions connect.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ pid_t git_connect(int fd[2], char *url, const char *prog, int flags)
unsetenv(ALTERNATE_DB_ENVIRONMENT);
unsetenv(DB_ENVIRONMENT);
unsetenv(GIT_DIR_ENVIRONMENT);
unsetenv(GIT_WORK_TREE_ENVIRONMENT);
unsetenv(GRAFT_ENVIRONMENT);
unsetenv(INDEX_ENVIRONMENT);
execlp("sh", "sh", "-c", command, NULL);
Expand Down
3 changes: 2 additions & 1 deletion git-filter-branch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ case "$GIT_DIR" in
/*)
;;
*)
export GIT_DIR="$(pwd)/../../$GIT_DIR"
GIT_DIR="$(pwd)/../../$GIT_DIR"
;;
esac
export GIT_DIR GIT_WORK_TREE=.

export GIT_INDEX_FILE="$(pwd)/../index"
git-read-tree # seed the index file
Expand Down
8 changes: 2 additions & 6 deletions git-sh-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ set_reflog_action() {
}

is_bare_repository () {
git-config --bool --get core.bare ||
case "$GIT_DIR" in
.git | */.git) echo false ;;
*) echo true ;;
esac
git-rev-parse --is-bare-repository
}

cd_to_toplevel () {
Expand All @@ -48,7 +44,7 @@ cd_to_toplevel () {
}

require_work_tree () {
test $(is_bare_repository) = false &&
test $(git-rev-parse --is-inside-work-tree) = true &&
test $(git-rev-parse --is-inside-git-dir) = false ||
die "fatal: $0 cannot be used without a working tree."
}
Expand Down
3 changes: 1 addition & 2 deletions git-svn.perl
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,7 @@ sub post_fetch_checkout {
my $index = $ENV{GIT_INDEX_FILE} || "$ENV{GIT_DIR}/index";
return if -f $index;

chomp(my $bare = `git config --bool --get core.bare`);
return if $bare eq 'true';
return if command_oneline(qw/rev-parse --is-inside-work-tree/) eq 'false';
return if command_oneline(qw/rev-parse --is-inside-git-dir/) eq 'true';
command_noisy(qw/read-tree -m -u -v HEAD HEAD/);
print STDERR "Checked out HEAD:\n ",
Expand Down
35 changes: 22 additions & 13 deletions git.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "quote.h"

const char git_usage_string[] =
"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--help] COMMAND [ARGS]";
"git [--version] [--exec-path[=GIT_EXEC_PATH]] [-p|--paginate] [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE] [--help] COMMAND [ARGS]";

static void prepend_to_path(const char *dir, int len)
{
Expand Down Expand Up @@ -69,6 +69,16 @@ static int handle_options(const char*** argv, int* argc)
handled++;
} else if (!prefixcmp(cmd, "--git-dir=")) {
setenv(GIT_DIR_ENVIRONMENT, cmd + 10, 1);
} else if (!strcmp(cmd, "--work-tree")) {
if (*argc < 2) {
fprintf(stderr, "No directory given for --work-tree.\n" );
usage(git_usage_string);
}
setenv(GIT_WORK_TREE_ENVIRONMENT, (*argv)[1], 1);
(*argv)++;
(*argc)--;
} else if (!prefixcmp(cmd, "--work-tree=")) {
setenv(GIT_WORK_TREE_ENVIRONMENT, cmd + 12, 1);
} else if (!strcmp(cmd, "--bare")) {
static char git_dir[PATH_MAX+1];
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir, sizeof(git_dir)), 1);
Expand Down Expand Up @@ -214,7 +224,7 @@ const char git_version_string[] = GIT_VERSION;
* require working tree to be present -- anything uses this needs
* RUN_SETUP for reading from the configuration file.
*/
#define NOT_BARE (1<<2)
#define NEED_WORK_TREE (1<<2)

struct cmd_struct {
const char *cmd;
Expand All @@ -233,10 +243,9 @@ static int run_command(struct cmd_struct *p, int argc, const char **argv)
prefix = setup_git_directory();
if (p->option & USE_PAGER)
setup_pager();
if (p->option & NOT_BARE) {
if (is_bare_repository() || is_inside_git_dir())
die("%s must be run in a work tree", p->cmd);
}
if ((p->option & NEED_WORK_TREE) &&
(!is_inside_work_tree() || is_inside_git_dir()))
die("%s must be run in a work tree", p->cmd);
trace_argv_printf(argv, argc, "trace: built-in: git");

status = p->fn(argc, argv, prefix);
Expand Down Expand Up @@ -264,7 +273,7 @@ static void handle_internal_command(int argc, const char **argv)
{
const char *cmd = argv[0];
static struct cmd_struct commands[] = {
{ "add", cmd_add, RUN_SETUP | NOT_BARE },
{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
{ "annotate", cmd_annotate, RUN_SETUP | USE_PAGER },
{ "apply", cmd_apply },
{ "archive", cmd_archive },
Expand All @@ -274,9 +283,9 @@ static void handle_internal_command(int argc, const char **argv)
{ "cat-file", cmd_cat_file, RUN_SETUP },
{ "checkout-index", cmd_checkout_index, RUN_SETUP },
{ "check-ref-format", cmd_check_ref_format },
{ "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE },
{ "check-attr", cmd_check_attr, RUN_SETUP | NEED_WORK_TREE },
{ "cherry", cmd_cherry, RUN_SETUP },
{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
{ "cherry-pick", cmd_cherry_pick, RUN_SETUP | NEED_WORK_TREE },
{ "commit-tree", cmd_commit_tree, RUN_SETUP },
{ "config", cmd_config },
{ "count-objects", cmd_count_objects, RUN_SETUP },
Expand Down Expand Up @@ -304,7 +313,7 @@ static void handle_internal_command(int argc, const char **argv)
{ "mailsplit", cmd_mailsplit },
{ "merge-base", cmd_merge_base, RUN_SETUP },
{ "merge-file", cmd_merge_file },
{ "mv", cmd_mv, RUN_SETUP | NOT_BARE },
{ "mv", cmd_mv, RUN_SETUP | NEED_WORK_TREE },
{ "name-rev", cmd_name_rev, RUN_SETUP },
{ "pack-objects", cmd_pack_objects, RUN_SETUP },
{ "pickaxe", cmd_blame, RUN_SETUP | USE_PAGER },
Expand All @@ -317,9 +326,9 @@ static void handle_internal_command(int argc, const char **argv)
{ "rerere", cmd_rerere, RUN_SETUP },
{ "rev-list", cmd_rev_list, RUN_SETUP },
{ "rev-parse", cmd_rev_parse, RUN_SETUP },
{ "revert", cmd_revert, RUN_SETUP | NOT_BARE },
{ "rm", cmd_rm, RUN_SETUP | NOT_BARE },
{ "runstatus", cmd_runstatus, RUN_SETUP | NOT_BARE },
{ "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE },
{ "rm", cmd_rm, RUN_SETUP | NEED_WORK_TREE },
{ "runstatus", cmd_runstatus, RUN_SETUP | NEED_WORK_TREE },
{ "shortlog", cmd_shortlog, RUN_SETUP | USE_PAGER },
{ "show-branch", cmd_show_branch, RUN_SETUP },
{ "show", cmd_show, RUN_SETUP | USE_PAGER },
Expand Down
Loading

0 comments on commit 0305b63

Please sign in to comment.