Skip to content

Commit

Permalink
Merge branch 'nd/multiple-work-trees'
Browse files Browse the repository at this point in the history
A replacement for contrib/workdir/git-new-workdir that does not
rely on symbolic links and make sharing of objects and refs safer
by making the borrowee and borrowers aware of each other.

* nd/multiple-work-trees: (41 commits)
  prune --worktrees: fix expire vs worktree existence condition
  t1501: fix test with split index
  t2026: fix broken &&-chain
  t2026 needs procondition SANITY
  git-checkout.txt: a note about multiple checkout support for submodules
  checkout: add --ignore-other-wortrees
  checkout: pass whole struct to parse_branchname_arg instead of individual flags
  git-common-dir: make "modules/" per-working-directory directory
  checkout: do not fail if target is an empty directory
  t2025: add a test to make sure grafts is working from a linked checkout
  checkout: don't require a work tree when checking out into a new one
  git_path(): keep "info/sparse-checkout" per work-tree
  count-objects: report unused files in $GIT_DIR/worktrees/...
  gc: support prune --worktrees
  gc: factor out gc.pruneexpire parsing code
  gc: style change -- no SP before closing parenthesis
  checkout: clean up half-prepared directories in --to mode
  checkout: reject if the branch is already checked out elsewhere
  prune: strategies for linked checkouts
  checkout: support checking out into a new working directory
  ...
  • Loading branch information
gitster committed May 11, 2015
2 parents 17c7f4d + 562bc08 commit 68a2e6a
Show file tree
Hide file tree
Showing 52 changed files with 1,391 additions and 250 deletions.
9 changes: 9 additions & 0 deletions Documentation/config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ false), while all other repositories are assumed to be bare (bare

core.worktree::
Set the path to the root of the working tree.
If GIT_COMMON_DIR environment variable is set, core.worktree
is ignored and not used for determining the root of working tree.
This can be overridden by the GIT_WORK_TREE environment
variable and the '--work-tree' command-line option.
The value can be an absolute path or relative to the path to
Expand Down Expand Up @@ -1274,6 +1276,13 @@ gc.pruneExpire::
"now" may be used to disable this grace period and always prune
unreachable objects immediately.

gc.pruneWorktreesExpire::
When 'git gc' is run, it will call
'prune --worktrees --expire 3.months.ago'.
Override the grace period with this config variable. The value
"now" may be used to disable the grace period and prune
$GIT_DIR/worktrees immediately.

gc.reflogExpire::
gc.<pattern>.reflogExpire::
'git reflog expire' removes reflog entries older than
Expand Down
78 changes: 78 additions & 0 deletions Documentation/git-checkout.txt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,19 @@ This means that you can use `git checkout -p` to selectively discard
edits from your current working tree. See the ``Interactive Mode''
section of linkgit:git-add[1] to learn how to operate the `--patch` mode.

--to=<path>::
Check out a branch in a separate working directory at
`<path>`. A new working directory is linked to the current
repository, sharing everything except working directory
specific files such as HEAD, index... See "MULTIPLE WORKING
TREES" section for more information.

--ignore-other-worktrees::
`git checkout` refuses when the wanted ref is already checked
out by another worktree. This option makes it check the ref
out anyway. In other words, the ref can be held by more than one
worktree.

<branch>::
Branch to checkout; if it refers to a branch (i.e., a name that,
when prepended with "refs/heads/", is a valid ref), then that
Expand Down Expand Up @@ -388,6 +401,71 @@ $ git reflog -2 HEAD # or
$ git log -g -2 HEAD
------------

MULTIPLE WORKING TREES
----------------------

A git repository can support multiple working trees, allowing you to check
out more than one branch at a time. With `git checkout --to` a new working
tree is associated with the repository. This new working tree is called a
"linked working tree" as opposed to the "main working tree" prepared by "git
init" or "git clone". A repository has one main working tree (if it's not a
bare repository) and zero or more linked working trees.

Each linked working tree has a private sub-directory in the repository's
$GIT_DIR/worktrees directory. The private sub-directory's name is usually
the base name of the linked working tree's path, possibly appended with a
number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the
command `git checkout --to /path/other/test-next next` creates the linked
working tree in `/path/other/test-next` and also creates a
`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
if `test-next` is already taken).

Within a linked working tree, $GIT_DIR is set to point to this private
directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and
$GIT_COMMON_DIR is set to point back to the main working tree's $GIT_DIR
(e.g. `/path/main/.git`). These settings are made in a `.git` file located at
the top directory of the linked working tree.

Path resolution via `git rev-parse --git-path` uses either
$GIT_DIR or $GIT_COMMON_DIR depending on the path. For example, in the
linked working tree `git rev-parse --git-path HEAD` returns
`/path/main/.git/worktrees/test-next/HEAD` (not
`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
rev-parse --git-path refs/heads/master` uses
$GIT_COMMON_DIR and returns `/path/main/.git/refs/heads/master`,
since refs are shared across all working trees.

See linkgit:gitrepository-layout[5] for more information. The rule of
thumb is do not make any assumption about whether a path belongs to
$GIT_DIR or $GIT_COMMON_DIR when you need to directly access something
inside $GIT_DIR. Use `git rev-parse --git-path` to get the final path.

When you are done with a linked working tree you can simply delete it.
The working tree's entry in the repository's $GIT_DIR/worktrees
directory will eventually be removed automatically (see
`gc.pruneworktreesexpire` in linkgit::git-config[1]), or you can run
`git prune --worktrees` in the main or any linked working tree to
clean up any stale entries in $GIT_DIR/worktrees.

If you move a linked working directory to another file system, or
within a file system that does not support hard links, you need to run
at least one git command inside the linked working directory
(e.g. `git status`) in order to update its entry in $GIT_DIR/worktrees
so that it does not get automatically removed.

To prevent a $GIT_DIR/worktrees entry from from being pruned (which
can be useful in some situations, such as when the
entry's working tree is stored on a portable device), add a file named
'locked' to the entry's directory. The file contains the reason in
plain text. For example, if a linked working tree's `.git` file points
to `/path/main/.git/worktrees/test-next` then a file named
`/path/main/.git/worktrees/test-next/locked` will prevent the
`test-next` entry from being pruned. See
linkgit:gitrepository-layout[5] for details.

Multiple checkout support for submodules is incomplete. It is NOT
recommended to make multiple checkouts of a superproject.

EXAMPLES
--------

Expand Down
3 changes: 3 additions & 0 deletions Documentation/git-prune.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ OPTIONS
--expire <time>::
Only expire loose objects older than <time>.

--worktrees::
Prune dead working tree information in $GIT_DIR/worktrees.

<head>...::
In addition to objects
reachable from any of our references, keep objects
Expand Down
10 changes: 10 additions & 0 deletions Documentation/git-rev-parse.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ If `$GIT_DIR` is not defined and the current directory
is not detected to lie in a Git repository or work tree
print a message to stderr and exit with nonzero status.

--git-common-dir::
Show `$GIT_COMMON_DIR` if defined, else `$GIT_DIR`.

--is-inside-git-dir::
When the current working directory is below the repository
directory print "true", otherwise "false".
Expand All @@ -233,6 +236,13 @@ print a message to stderr and exit with nonzero status.
repository. If <path> is a gitfile then the resolved path
to the real repository is printed.

--git-path <path>::
Resolve "$GIT_DIR/<path>" and takes other path relocation
variables such as $GIT_OBJECT_DIRECTORY,
$GIT_INDEX_FILE... into account. For example, if
$GIT_OBJECT_DIRECTORY is set to /foo/bar then "git rev-parse
--git-path objects/abc" returns /foo/bar/abc.

--show-cdup::
When the command is invoked from a subdirectory, show the
path of the top-level directory relative to the current
Expand Down
9 changes: 9 additions & 0 deletions Documentation/git.txt
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,15 @@ Git so take care if using Cogito etc.
an explicit repository directory set via 'GIT_DIR' or on the
command line.

'GIT_COMMON_DIR'::
If this variable is set to a path, non-worktree files that are
normally in $GIT_DIR will be taken from this path
instead. Worktree-specific files such as HEAD or index are
taken from $GIT_DIR. See linkgit:gitrepository-layout[5] and
the section 'MULTIPLE CHECKOUT MODE' in linkgit:checkout[1]
details. This variable has lower precedence than other path
variables such as GIT_INDEX_FILE, GIT_OBJECT_DIRECTORY...

Git Commits
~~~~~~~~~~~
'GIT_AUTHOR_NAME'::
Expand Down
74 changes: 64 additions & 10 deletions Documentation/gitrepository-layout.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ of incomplete object store is not suitable to be published for
use with dumb transports but otherwise is OK as long as
`objects/info/alternates` points at the object stores it
borrows from.
+
This directory is ignored if $GIT_COMMON_DIR is set and
"$GIT_COMMON_DIR/objects" will be used instead.

objects/[0-9a-f][0-9a-f]::
A newly created object is stored in its own file.
Expand Down Expand Up @@ -92,7 +95,8 @@ refs::
References are stored in subdirectories of this
directory. The 'git prune' command knows to preserve
objects reachable from refs found in this directory and
its subdirectories.
its subdirectories. This directory is ignored if $GIT_COMMON_DIR
is set and "$GIT_COMMON_DIR/refs" will be used instead.

refs/heads/`name`::
records tip-of-the-tree commit objects of branch `name`
Expand All @@ -114,7 +118,8 @@ refs/replace/`<obj-sha1>`::
packed-refs::
records the same information as refs/heads/, refs/tags/,
and friends record in a more efficient way. See
linkgit:git-pack-refs[1].
linkgit:git-pack-refs[1]. This file is ignored if $GIT_COMMON_DIR
is set and "$GIT_COMMON_DIR/packed-refs" will be used instead.

HEAD::
A symref (see glossary) to the `refs/heads/` namespace
Expand All @@ -133,14 +138,22 @@ being a symref to point at the current branch. Such a state
is often called 'detached HEAD.' See linkgit:git-checkout[1]
for details.

config::
Repository specific configuration file. This file is ignored
if $GIT_COMMON_DIR is set and "$GIT_COMMON_DIR/config" will be
used instead.

branches::
A slightly deprecated way to store shorthands to be used
to specify a URL to 'git fetch', 'git pull' and 'git push'.
A file can be stored as `branches/<name>` and then
'name' can be given to these commands in place of
'repository' argument. See the REMOTES section in
linkgit:git-fetch[1] for details. This mechanism is legacy
and not likely to be found in modern repositories.
and not likely to be found in modern repositories. This
directory is ignored if $GIT_COMMON_DIR is set and
"$GIT_COMMON_DIR/branches" will be used instead.


hooks::
Hooks are customization scripts used by various Git
Expand All @@ -149,7 +162,9 @@ hooks::
default. To enable, the `.sample` suffix has to be
removed from the filename by renaming.
Read linkgit:githooks[5] for more details about
each hook.
each hook. This directory is ignored if $GIT_COMMON_DIR is set
and "$GIT_COMMON_DIR/hooks" will be used instead.


index::
The current index file for the repository. It is
Expand All @@ -161,7 +176,8 @@ sharedindex.<SHA-1>::

info::
Additional information about the repository is recorded
in this directory.
in this directory. This directory is ignored if $GIT_COMMON_DIR
is set and "$GIT_COMMON_DIR/index" will be used instead.

info/refs::
This file helps dumb transports discover what refs are
Expand Down Expand Up @@ -201,12 +217,15 @@ remotes::
when interacting with remote repositories via 'git fetch',
'git pull' and 'git push' commands. See the REMOTES section
in linkgit:git-fetch[1] for details. This mechanism is legacy
and not likely to be found in modern repositories.
and not likely to be found in modern repositories. This
directory is ignored if $GIT_COMMON_DIR is set and
"$GIT_COMMON_DIR/remotes" will be used instead.

logs::
Records of changes made to refs are stored in this
directory. See linkgit:git-update-ref[1]
for more information.
Records of changes made to refs are stored in this directory.
See linkgit:git-update-ref[1] for more information. This
directory is ignored if $GIT_COMMON_DIR is set and
"$GIT_COMMON_DIR/logs" will be used instead.

logs/refs/heads/`name`::
Records all changes made to the branch tip named `name`.
Expand All @@ -217,11 +236,46 @@ logs/refs/tags/`name`::
shallow::
This is similar to `info/grafts` but is internally used
and maintained by shallow clone mechanism. See `--depth`
option to linkgit:git-clone[1] and linkgit:git-fetch[1].
option to linkgit:git-clone[1] and linkgit:git-fetch[1]. This
file is ignored if $GIT_COMMON_DIR is set and
"$GIT_COMMON_DIR/shallow" will be used instead.

commondir::
If this file exists, $GIT_COMMON_DIR (see linkgit:git[1]) will
be set to the path specified in this file if it is not
explicitly set. If the specified path is relative, it is
relative to $GIT_DIR. The repository with commondir is
incomplete without the repository pointed by "commondir".

modules::
Contains the git-repositories of the submodules.

worktrees::
Contains worktree specific information of linked
checkouts. Each subdirectory contains the worktree-related
part of a linked checkout. This directory is ignored if
$GIT_COMMON_DIR is set and "$GIT_COMMON_DIR/worktrees" will be
used instead.

worktrees/<id>/gitdir::
A text file containing the absolute path back to the .git file
that points to here. This is used to check if the linked
repository has been manually removed and there is no need to
keep this directory any more. mtime of this file should be
updated every time the linked repository is accessed.

worktrees/<id>/locked::
If this file exists, the linked repository may be on a
portable device and not available. It does not mean that the
linked repository is gone and `worktrees/<id>` could be
removed. The file's content contains a reason string on why
the repository is locked.

worktrees/<id>/link::
If this file exists, it is a hard link to the linked .git
file. It is used to detect if the linked repository is
manually removed.

SEE ALSO
--------
linkgit:git-init[1],
Expand Down
4 changes: 1 addition & 3 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,6 @@ static const char edit_description[] = "BRANCH_DESCRIPTION";

static int edit_branch_description(const char *branch_name)
{
FILE *fp;
int status;
struct strbuf buf = STRBUF_INIT;
struct strbuf name = STRBUF_INIT;
Expand All @@ -784,8 +783,7 @@ static int edit_branch_description(const char *branch_name)
" %s\n"
"Lines starting with '%c' will be stripped.\n",
branch_name, comment_line_char);
fp = fopen(git_path(edit_description), "w");
if ((fwrite(buf.buf, 1, buf.len, fp) < buf.len) || fclose(fp)) {
if (write_file(git_path(edit_description), 0, "%s", buf.buf)) {
strbuf_release(&buf);
return error(_("could not write branch description template: %s"),
strerror(errno));
Expand Down
Loading

0 comments on commit 68a2e6a

Please sign in to comment.