1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-25 01:56:27 +02:00

Merge branch 'ds/worktree-docs'

Tighten the language around "working tree" and "worktree" in the
docs.

* ds/worktree-docs:
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: use 'worktree' over 'working tree'
  worktree: extract checkout_worktree()
  worktree: extract copy_sparse_checkout()
  worktree: extract copy_filtered_worktree_config()
  worktree: combine two translatable messages
This commit is contained in:
Junio C Hamano 2022-03-06 21:25:31 -08:00
commit aae90a156d
2 changed files with 207 additions and 195 deletions

View File

@ -25,45 +25,49 @@ Manage multiple working trees attached to the same repository.
A git repository can support multiple working trees, allowing you to check A git repository can support multiple working trees, allowing you to check
out more than one branch at a time. With `git worktree add` a new working out more than one branch at a time. With `git worktree add` a new working
tree is associated with the repository. This new working tree is called a tree is associated with the repository, along with additional metadata
"linked working tree" as opposed to the "main working tree" prepared by that differentiates that working tree from others in the same repository.
linkgit:git-init[1] or linkgit:git-clone[1]. The working tree, along with this metadata, is called a "worktree".
A repository has one main working tree (if it's not a
bare repository) and zero or more linked working trees. When you are done This new worktree is called a "linked worktree" as opposed to the "main
with a linked working tree, remove it with `git worktree remove`. worktree" prepared by linkgit:git-init[1] or linkgit:git-clone[1].
A repository has one main worktree (if it's not a bare repository) and
zero or more linked worktrees. When you are done with a linked worktree,
remove it with `git worktree remove`.
In its simplest form, `git worktree add <path>` automatically creates a In its simplest form, `git worktree add <path>` automatically creates a
new branch whose name is the final component of `<path>`, which is new branch whose name is the final component of `<path>`, which is
convenient if you plan to work on a new topic. For instance, `git convenient if you plan to work on a new topic. For instance, `git
worktree add ../hotfix` creates new branch `hotfix` and checks it out at worktree add ../hotfix` creates new branch `hotfix` and checks it out at
path `../hotfix`. To instead work on an existing branch in a new working path `../hotfix`. To instead work on an existing branch in a new worktree,
tree, use `git worktree add <path> <branch>`. On the other hand, if you use `git worktree add <path> <branch>`. On the other hand, if you just
just plan to make some experimental changes or do testing without plan to make some experimental changes or do testing without disturbing
disturbing existing development, it is often convenient to create a existing development, it is often convenient to create a 'throwaway'
'throwaway' working tree not associated with any branch. For instance, worktree not associated with any branch. For instance,
`git worktree add -d <path>` creates a new working tree with a detached `git worktree add -d <path>` creates a new worktree with a detached `HEAD`
`HEAD` at the same commit as the current branch. at the same commit as the current branch.
If a working tree is deleted without using `git worktree remove`, then If a working tree is deleted without using `git worktree remove`, then
its associated administrative files, which reside in the repository its associated administrative files, which reside in the repository
(see "DETAILS" below), will eventually be removed automatically (see (see "DETAILS" below), will eventually be removed automatically (see
`gc.worktreePruneExpire` in linkgit:git-config[1]), or you can run `gc.worktreePruneExpire` in linkgit:git-config[1]), or you can run
`git worktree prune` in the main or any linked working tree to `git worktree prune` in the main or any linked worktree to clean up any
clean up any stale administrative files. stale administrative files.
If a linked working tree is stored on a portable device or network share If the working tree for a linked worktree is stored on a portable device
which is not always mounted, you can prevent its administrative files from or network share which is not always mounted, you can prevent its
being pruned by issuing the `git worktree lock` command, optionally administrative files from being pruned by issuing the `git worktree lock`
specifying `--reason` to explain why the working tree is locked. command, optionally specifying `--reason` to explain why the worktree is
locked.
COMMANDS COMMANDS
-------- --------
add <path> [<commit-ish>]:: add <path> [<commit-ish>]::
Create `<path>` and checkout `<commit-ish>` into it. The new working directory Create a worktree at `<path>` and checkout `<commit-ish>` into it. The new worktree
is linked to the current repository, sharing everything except working is linked to the current repository, sharing everything except per-worktree
directory specific files such as `HEAD`, `index`, etc. As a convenience, files such as `HEAD`, `index`, etc. As a convenience, `<commit-ish>` may
`<commit-ish>` may be a bare "`-`", which is synonymous with `@{-1}`. be a bare "`-`", which is synonymous with `@{-1}`.
+ +
If `<commit-ish>` is a branch name (call it `<branch>`) and is not found, If `<commit-ish>` is a branch name (call it `<branch>`) and is not found,
and neither `-b` nor `-B` nor `--detach` are used, but there does and neither `-b` nor `-B` nor `--detach` are used, but there does
@ -84,100 +88,97 @@ branches from there if `<branch>` is ambiguous but exists on the
linkgit:git-config[1]. linkgit:git-config[1].
+ +
If `<commit-ish>` is omitted and neither `-b` nor `-B` nor `--detach` used, If `<commit-ish>` is omitted and neither `-b` nor `-B` nor `--detach` used,
then, as a convenience, the new working tree is associated with a branch then, as a convenience, the new worktree is associated with a branch (call
(call it `<branch>`) named after `$(basename <path>)`. If `<branch>` it `<branch>`) named after `$(basename <path>)`. If `<branch>` doesn't
doesn't exist, a new branch based on `HEAD` is automatically created as exist, a new branch based on `HEAD` is automatically created as if
if `-b <branch>` was given. If `<branch>` does exist, it will be `-b <branch>` was given. If `<branch>` does exist, it will be checked out
checked out in the new working tree, if it's not checked out anywhere in the new worktree, if it's not checked out anywhere else, otherwise the
else, otherwise the command will refuse to create the working tree (unless command will refuse to create the worktree (unless `--force` is used).
`--force` is used).
list:: list::
List details of each working tree. The main working tree is listed first, List details of each worktree. The main worktree is listed first,
followed by each of the linked working trees. The output details include followed by each of the linked worktrees. The output details include
whether the working tree is bare, the revision currently checked out, the whether the worktree is bare, the revision currently checked out, the
branch currently checked out (or "detached HEAD" if none), "locked" if branch currently checked out (or "detached HEAD" if none), "locked" if
the worktree is locked, "prunable" if the worktree can be pruned by `prune` the worktree is locked, "prunable" if the worktree can be pruned by the
command. `prune` command.
lock:: lock::
If a working tree is on a portable device or network share which If a worktree is on a portable device or network share which is not always
is not always mounted, lock it to prevent its administrative mounted, lock it to prevent its administrative files from being pruned
files from being pruned automatically. This also prevents it from automatically. This also prevents it from being moved or deleted.
being moved or deleted. Optionally, specify a reason for the lock Optionally, specify a reason for the lock with `--reason`.
with `--reason`.
move:: move::
Move a working tree to a new location. Note that the main working tree Move a worktree to a new location. Note that the main worktree or linked
or linked working trees containing submodules cannot be moved with this worktrees containing submodules cannot be moved with this command. (The
command. (The `git worktree repair` command, however, can reestablish `git worktree repair` command, however, can reestablish the connection
the connection with linked working trees if you move the main working with linked worktrees if you move the main worktree manually.)
tree manually.)
prune:: prune::
Prune working tree information in `$GIT_DIR/worktrees`. Prune worktree information in `$GIT_DIR/worktrees`.
remove:: remove::
Remove a working tree. Only clean working trees (no untracked files Remove a worktree. Only clean worktrees (no untracked files and no
and no modification in tracked files) can be removed. Unclean working modification in tracked files) can be removed. Unclean worktrees or ones
trees or ones with submodules can be removed with `--force`. The main with submodules can be removed with `--force`. The main worktree cannot be
working tree cannot be removed. removed.
repair [<path>...]:: repair [<path>...]::
Repair working tree administrative files, if possible, if they have Repair worktree administrative files, if possible, if they have become
become corrupted or outdated due to external factors. corrupted or outdated due to external factors.
+ +
For instance, if the main working tree (or bare repository) is moved, For instance, if the main worktree (or bare repository) is moved, linked
linked working trees will be unable to locate it. Running `repair` in worktrees will be unable to locate it. Running `repair` in the main
the main working tree will reestablish the connection from linked worktree will reestablish the connection from linked worktrees back to the
working trees back to the main working tree. main worktree.
+ +
Similarly, if a linked working tree is moved without using `git worktree Similarly, if the working tree for a linked worktree is moved without
move`, the main working tree (or bare repository) will be unable to using `git worktree move`, the main worktree (or bare repository) will be
locate it. Running `repair` within the recently-moved working tree will unable to locate it. Running `repair` within the recently-moved worktree
reestablish the connection. If multiple linked working trees are moved, will reestablish the connection. If multiple linked worktrees are moved,
running `repair` from any working tree with each tree's new `<path>` as running `repair` from any worktree with each tree's new `<path>` as an
an argument, will reestablish the connection to all the specified paths. argument, will reestablish the connection to all the specified paths.
+ +
If both the main working tree and linked working trees have been moved If both the main worktree and linked worktrees have been moved manually,
manually, then running `repair` in the main working tree and specifying the then running `repair` in the main worktree and specifying the new `<path>`
new `<path>` of each linked working tree will reestablish all connections of each linked worktree will reestablish all connections in both
in both directions. directions.
unlock:: unlock::
Unlock a working tree, allowing it to be pruned, moved or deleted. Unlock a worktree, allowing it to be pruned, moved or deleted.
OPTIONS OPTIONS
------- -------
-f:: -f::
--force:: --force::
By default, `add` refuses to create a new working tree when By default, `add` refuses to create a new worktree when
`<commit-ish>` is a branch name and is already checked out by `<commit-ish>` is a branch name and is already checked out by
another working tree, or if `<path>` is already assigned to some another worktree, or if `<path>` is already assigned to some
working tree but is missing (for instance, if `<path>` was deleted worktree but is missing (for instance, if `<path>` was deleted
manually). This option overrides these safeguards. To add a missing but manually). This option overrides these safeguards. To add a missing but
locked working tree path, specify `--force` twice. locked worktree path, specify `--force` twice.
+ +
`move` refuses to move a locked working tree unless `--force` is specified `move` refuses to move a locked worktree unless `--force` is specified
twice. If the destination is already assigned to some other working tree but is twice. If the destination is already assigned to some other worktree but is
missing (for instance, if `<new-path>` was deleted manually), then `--force` missing (for instance, if `<new-path>` was deleted manually), then `--force`
allows the move to proceed; use `--force` twice if the destination is locked. allows the move to proceed; use `--force` twice if the destination is locked.
+ +
`remove` refuses to remove an unclean working tree unless `--force` is used. `remove` refuses to remove an unclean worktree unless `--force` is used.
To remove a locked working tree, specify `--force` twice. To remove a locked worktree, specify `--force` twice.
-b <new-branch>:: -b <new-branch>::
-B <new-branch>:: -B <new-branch>::
With `add`, create a new branch named `<new-branch>` starting at With `add`, create a new branch named `<new-branch>` starting at
`<commit-ish>`, and check out `<new-branch>` into the new working tree. `<commit-ish>`, and check out `<new-branch>` into the new worktree.
If `<commit-ish>` is omitted, it defaults to `HEAD`. If `<commit-ish>` is omitted, it defaults to `HEAD`.
By default, `-b` refuses to create a new branch if it already By default, `-b` refuses to create a new branch if it already
exists. `-B` overrides this safeguard, resetting `<new-branch>` to exists. `-B` overrides this safeguard, resetting `<new-branch>` to
@ -185,7 +186,7 @@ To remove a locked working tree, specify `--force` twice.
-d:: -d::
--detach:: --detach::
With `add`, detach `HEAD` in the new working tree. See "DETACHED HEAD" With `add`, detach `HEAD` in the new worktree. See "DETACHED HEAD"
in linkgit:git-checkout[1]. in linkgit:git-checkout[1].
--[no-]checkout:: --[no-]checkout::
@ -211,7 +212,7 @@ This can also be set up as the default behaviour by using the
`--track` in linkgit:git-branch[1] for details. `--track` in linkgit:git-branch[1] for details.
--lock:: --lock::
Keep the working tree locked after creation. This is the Keep the worktree locked after creation. This is the
equivalent of `git worktree lock` after `git worktree add`, equivalent of `git worktree lock` after `git worktree add`,
but without a race condition. but without a race condition.
@ -236,43 +237,42 @@ This can also be set up as the default behaviour by using the
With `list`, output additional information about worktrees (see below). With `list`, output additional information about worktrees (see below).
--expire <time>:: --expire <time>::
With `prune`, only expire unused working trees older than `<time>`. With `prune`, only expire unused worktrees older than `<time>`.
+ +
With `list`, annotate missing working trees as prunable if they are With `list`, annotate missing worktrees as prunable if they are older than
older than `<time>`. `<time>`.
--reason <string>:: --reason <string>::
With `lock` or with `add --lock`, an explanation why the working tree is locked. With `lock` or with `add --lock`, an explanation why the worktree
is locked.
<worktree>:: <worktree>::
Working trees can be identified by path, either relative or Worktrees can be identified by path, either relative or absolute.
absolute.
+ +
If the last path components in the working tree's path is unique among If the last path components in the worktree's path is unique among
working trees, it can be used to identify a working tree. For example if worktrees, it can be used to identify a worktree. For example if you only
you only have two working trees, at `/abc/def/ghi` and `/abc/def/ggg`, have two worktrees, at `/abc/def/ghi` and `/abc/def/ggg`, then `ghi` or
then `ghi` or `def/ghi` is enough to point to the former working tree. `def/ghi` is enough to point to the former worktree.
REFS REFS
---- ----
In multiple working trees, some refs may be shared between all working When using multiple worktrees, some refs are shared between all worktrees,
trees and some refs are local. One example is `HEAD` which is different for each but others are specific to an individual worktree. One example is `HEAD`,
working tree. This section is about the sharing rules and how to access which is different for each worktree. This section is about the sharing
refs of one working tree from another. rules and how to access refs of one worktree from another.
In general, all pseudo refs are per working tree and all refs starting In general, all pseudo refs are per-worktree and all refs starting with
with `refs/` are shared. Pseudo refs are ones like `HEAD` which are `refs/` are shared. Pseudo refs are ones like `HEAD` which are directly
directly under `$GIT_DIR` instead of inside `$GIT_DIR/refs`. There are under `$GIT_DIR` instead of inside `$GIT_DIR/refs`. There are exceptions,
exceptions, however: refs inside `refs/bisect` and `refs/worktree` are not however: refs inside `refs/bisect` and `refs/worktree` are not shared.
shared.
Refs that are per working tree can still be accessed from another Refs that are per-worktree can still be accessed from another worktree via
working tree via two special paths, `main-worktree` and `worktrees`. The two special paths, `main-worktree` and `worktrees`. The former gives
former gives access to per-working tree refs of the main working tree, access to per-worktree refs of the main worktree, while the latter to all
while the latter to all linked working trees. linked worktrees.
For example, `main-worktree/HEAD` or `main-worktree/refs/bisect/good` For example, `main-worktree/HEAD` or `main-worktree/refs/bisect/good`
resolve to the same value as the main working tree's `HEAD` and resolve to the same value as the main worktree's `HEAD` and
`refs/bisect/good` respectively. Similarly, `worktrees/foo/HEAD` or `refs/bisect/good` respectively. Similarly, `worktrees/foo/HEAD` or
`worktrees/bar/refs/bisect/bad` are the same as `worktrees/bar/refs/bisect/bad` are the same as
`$GIT_COMMON_DIR/worktrees/foo/HEAD` and `$GIT_COMMON_DIR/worktrees/foo/HEAD` and
@ -284,13 +284,13 @@ which will handle refs correctly.
CONFIGURATION FILE CONFIGURATION FILE
------------------ ------------------
By default, the repository `config` file is shared across all working By default, the repository `config` file is shared across all worktrees.
trees. If the config variables `core.bare` or `core.worktree` are If the config variables `core.bare` or `core.worktree` are present in the
present in the common config file and `extensions.worktreeConfig` is common config file and `extensions.worktreeConfig` is disabled, then they
disabled, then they will be applied to the main working tree only. will be applied to the main worktree only.
In order to have configuration specific to working trees, you can turn In order to have worktree-specific configuration, you can turn on the
on the `worktreeConfig` extension, e.g.: `worktreeConfig` extension, e.g.:
------------ ------------
$ git config extensions.worktreeConfig true $ git config extensions.worktreeConfig true
@ -303,45 +303,45 @@ versions will refuse to access repositories with this extension.
Note that in this file, the exception for `core.bare` and `core.worktree` Note that in this file, the exception for `core.bare` and `core.worktree`
is gone. If they exist in `$GIT_DIR/config`, you must move is gone. If they exist in `$GIT_DIR/config`, you must move
them to the `config.worktree` of the main working tree. You may also them to the `config.worktree` of the main worktree. You may also take this
take this opportunity to review and move other configuration that you opportunity to review and move other configuration that you do not want to
do not want to share to all working trees: share to all worktrees:
- `core.worktree` should never be shared. - `core.worktree` should never be shared.
- `core.bare` should not be shared if the value is `core.bare=true`. - `core.bare` should not be shared if the value is `core.bare=true`.
- `core.sparseCheckout` is recommended per working tree, unless you - `core.sparseCheckout` should not be shared, unless you are sure you
are sure you always use sparse checkout for all working trees. always use sparse checkout for all worktrees.
See the documentation of `extensions.worktreeConfig` in See the documentation of `extensions.worktreeConfig` in
linkgit:git-config[1] for more details. linkgit:git-config[1] for more details.
DETAILS DETAILS
------- -------
Each linked working tree has a private sub-directory in the repository's Each linked worktree has a private sub-directory in the repository's
`$GIT_DIR/worktrees` directory. The private sub-directory's name is usually `$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 the base name of the linked worktree's path, possibly appended with a
number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the number to make it unique. For example, when `$GIT_DIR=/path/main/.git` the
command `git worktree add /path/other/test-next next` creates the linked command `git worktree add /path/other/test-next next` creates the linked
working tree in `/path/other/test-next` and also creates a worktree in `/path/other/test-next` and also creates a
`$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1` `$GIT_DIR/worktrees/test-next` directory (or `$GIT_DIR/worktrees/test-next1`
if `test-next` is already taken). if `test-next` is already taken).
Within a linked working tree, `$GIT_DIR` is set to point to this private Within a linked worktree, `$GIT_DIR` is set to point to this private
directory (e.g. `/path/main/.git/worktrees/test-next` in the example) and 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` `$GIT_COMMON_DIR` is set to point back to the main worktree's `$GIT_DIR`
(e.g. `/path/main/.git`). These settings are made in a `.git` file located at (e.g. `/path/main/.git`). These settings are made in a `.git` file located at
the top directory of the linked working tree. the top directory of the linked worktree.
Path resolution via `git rev-parse --git-path` uses either Path resolution via `git rev-parse --git-path` uses either
`$GIT_DIR` or `$GIT_COMMON_DIR` depending on the path. For example, in the `$GIT_DIR` or `$GIT_COMMON_DIR` depending on the path. For example, in the
linked working tree `git rev-parse --git-path HEAD` returns linked worktree `git rev-parse --git-path HEAD` returns
`/path/main/.git/worktrees/test-next/HEAD` (not `/path/main/.git/worktrees/test-next/HEAD` (not
`/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git `/path/other/test-next/.git/HEAD` or `/path/main/.git/HEAD`) while `git
rev-parse --git-path refs/heads/master` uses rev-parse --git-path refs/heads/master` uses
`$GIT_COMMON_DIR` and returns `/path/main/.git/refs/heads/master`, `$GIT_COMMON_DIR` and returns `/path/main/.git/refs/heads/master`,
since refs are shared across all working trees, except `refs/bisect` and since refs are shared across all worktrees, except `refs/bisect` and
`refs/worktree`. `refs/worktree`.
See linkgit:gitrepository-layout[5] for more information. The rule of See linkgit:gitrepository-layout[5] for more information. The rule of
@ -349,8 +349,8 @@ 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 `$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. inside `$GIT_DIR`. Use `git rev-parse --git-path` to get the final path.
If you manually move a linked working tree, you need to update the `gitdir` file If you manually move a linked worktree, you need to update the `gitdir` file
in the entry's directory. For example, if a linked working tree is moved in the entry's directory. For example, if a linked worktree is moved
to `/newpath/test-next` and its `.git` file points to to `/newpath/test-next` and its `.git` file points to
`/path/main/.git/worktrees/test-next`, then update `/path/main/.git/worktrees/test-next`, then update
`/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next` `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
@ -359,10 +359,10 @@ automatically.
To prevent a `$GIT_DIR/worktrees` entry from being pruned (which To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
can be useful in some situations, such as when the can be useful in some situations, such as when the
entry's working tree is stored on a portable device), use the entry's worktree is stored on a portable device), use the
`git worktree lock` command, which adds a file named `git worktree lock` command, which adds a file named
`locked` to the entry's directory. The file contains the reason in `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 plain text. For example, if a linked worktree's `.git` file points
to `/path/main/.git/worktrees/test-next` then a file named to `/path/main/.git/worktrees/test-next` then a file named
`/path/main/.git/worktrees/test-next/locked` will prevent the `/path/main/.git/worktrees/test-next/locked` will prevent the
`test-next` entry from being pruned. See `test-next` entry from being pruned. See
@ -383,11 +383,11 @@ $ git worktree list
/path/to/other-linked-worktree 1234abc (detached HEAD) /path/to/other-linked-worktree 1234abc (detached HEAD)
------------ ------------
The command also shows annotations for each working tree, according to its state. The command also shows annotations for each worktree, according to its state.
These annotations are: These annotations are:
* `locked`, if the working tree is locked. * `locked`, if the worktree is locked.
* `prunable`, if the working tree can be pruned via `git worktree prune`. * `prunable`, if the worktree can be pruned via `git worktree prune`.
------------ ------------
$ git worktree list $ git worktree list
@ -405,14 +405,14 @@ $ git worktree list --verbose
/path/to/linked-worktree abcd1234 [master] /path/to/linked-worktree abcd1234 [master]
/path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked /path/to/locked-worktree-no-reason abcd5678 (detached HEAD) locked
/path/to/locked-worktree-with-reason 1234abcd (brancha) /path/to/locked-worktree-with-reason 1234abcd (brancha)
locked: working tree path is mounted on a portable device locked: worktree path is mounted on a portable device
/path/to/prunable-worktree 5678abc1 (detached HEAD) /path/to/prunable-worktree 5678abc1 (detached HEAD)
prunable: gitdir file points to non-existent location prunable: gitdir file points to non-existent location
------------ ------------
Note that the annotation is moved to the next line if the additional Note that the annotation is moved to the next line if the additional
information is available, otherwise it stays on the same line as the information is available, otherwise it stays on the same line as the
working tree itself. worktree itself.
Porcelain Format Porcelain Format
~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~
@ -421,7 +421,7 @@ label and value separated by a single space. Boolean attributes (like `bare`
and `detached`) are listed as a label only, and are present only and `detached`) are listed as a label only, and are present only
if the value is true. Some attributes (like `locked`) can be listed as a label if the value is true. Some attributes (like `locked`) can be listed as a label
only or with a value depending upon whether a reason is available. The first only or with a value depending upon whether a reason is available. The first
attribute of a working tree is always `worktree`, an empty line indicates the attribute of a worktree is always `worktree`, an empty line indicates the
end of the record. For example: end of the record. For example:
------------ ------------
@ -473,7 +473,7 @@ demands that you fix something immediately. You might typically use
linkgit:git-stash[1] to store your changes away temporarily, however, your linkgit:git-stash[1] to store your changes away temporarily, however, your
working tree is in such a state of disarray (with new, moved, and removed working tree is in such a state of disarray (with new, moved, and removed
files, and other bits and pieces strewn around) that you don't want to risk files, and other bits and pieces strewn around) that you don't want to risk
disturbing any of it. Instead, you create a temporary linked working tree to disturbing any of it. Instead, you create a temporary linked worktree to
make the emergency fix, remove it when done, and then resume your earlier make the emergency fix, remove it when done, and then resume your earlier
refactoring session. refactoring session.

View File

@ -236,6 +236,74 @@ static void check_candidate_path(const char *path,
die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd); die(_("'%s' is a missing but already registered worktree;\nuse '%s -f' to override, or 'prune' or 'remove' to clear"), path, cmd);
} }
static void copy_sparse_checkout(const char *worktree_git_dir)
{
char *from_file = git_pathdup("info/sparse-checkout");
char *to_file = xstrfmt("%s/info/sparse-checkout", worktree_git_dir);
if (file_exists(from_file)) {
if (safe_create_leading_directories(to_file) ||
copy_file(to_file, from_file, 0666))
error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
from_file, to_file);
}
free(from_file);
free(to_file);
}
static void copy_filtered_worktree_config(const char *worktree_git_dir)
{
char *from_file = git_pathdup("config.worktree");
char *to_file = xstrfmt("%s/config.worktree", worktree_git_dir);
if (file_exists(from_file)) {
struct config_set cs = { { 0 } };
const char *core_worktree;
int bare;
if (safe_create_leading_directories(to_file) ||
copy_file(to_file, from_file, 0666)) {
error(_("failed to copy worktree config from '%s' to '%s'"),
from_file, to_file);
goto worktree_copy_cleanup;
}
git_configset_init(&cs);
git_configset_add_file(&cs, from_file);
if (!git_configset_get_bool(&cs, "core.bare", &bare) &&
bare &&
git_config_set_multivar_in_file_gently(
to_file, "core.bare", NULL, "true", 0))
error(_("failed to unset '%s' in '%s'"),
"core.bare", to_file);
if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) &&
git_config_set_in_file_gently(to_file,
"core.worktree", NULL))
error(_("failed to unset '%s' in '%s'"),
"core.worktree", to_file);
git_configset_clear(&cs);
}
worktree_copy_cleanup:
free(from_file);
free(to_file);
}
static int checkout_worktree(const struct add_opts *opts,
struct strvec *child_env)
{
struct child_process cp = CHILD_PROCESS_INIT;
cp.git_cmd = 1;
strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
if (opts->quiet)
strvec_push(&cp.args, "--quiet");
strvec_pushv(&cp.env_array, child_env->v);
return run_command(&cp);
}
static int add_worktree(const char *path, const char *refname, static int add_worktree(const char *path, const char *refname,
const struct add_opts *opts) const struct add_opts *opts)
{ {
@ -339,64 +407,16 @@ static int add_worktree(const char *path, const char *refname,
* If the current worktree has sparse-checkout enabled, then copy * If the current worktree has sparse-checkout enabled, then copy
* the sparse-checkout patterns from the current worktree. * the sparse-checkout patterns from the current worktree.
*/ */
if (core_apply_sparse_checkout) { if (core_apply_sparse_checkout)
char *from_file = git_pathdup("info/sparse-checkout"); copy_sparse_checkout(sb_repo.buf);
char *to_file = xstrfmt("%s/info/sparse-checkout",
sb_repo.buf);
if (file_exists(from_file)) {
if (safe_create_leading_directories(to_file) ||
copy_file(to_file, from_file, 0666))
error(_("failed to copy '%s' to '%s'; sparse-checkout may not work correctly"),
from_file, to_file);
}
free(from_file);
free(to_file);
}
/* /*
* If we are using worktree config, then copy all current config * If we are using worktree config, then copy all current config
* values from the current worktree into the new one, that way the * values from the current worktree into the new one, that way the
* new worktree behaves the same as this one. * new worktree behaves the same as this one.
*/ */
if (repository_format_worktree_config) { if (repository_format_worktree_config)
char *from_file = git_pathdup("config.worktree"); copy_filtered_worktree_config(sb_repo.buf);
char *to_file = xstrfmt("%s/config.worktree",
sb_repo.buf);
if (file_exists(from_file)) {
struct config_set cs = { { 0 } };
const char *core_worktree;
int bare;
if (safe_create_leading_directories(to_file) ||
copy_file(to_file, from_file, 0666)) {
error(_("failed to copy worktree config from '%s' to '%s'"),
from_file, to_file);
goto worktree_copy_cleanup;
}
git_configset_init(&cs);
git_configset_add_file(&cs, from_file);
if (!git_configset_get_bool(&cs, "core.bare", &bare) &&
bare &&
git_config_set_multivar_in_file_gently(
to_file, "core.bare", NULL, "true", 0))
error(_("failed to unset 'core.bare' in '%s'"), to_file);
if (!git_configset_get_value(&cs, "core.worktree", &core_worktree) &&
git_config_set_in_file_gently(to_file,
"core.worktree", NULL))
error(_("failed to unset 'core.worktree' in '%s'"), to_file);
git_configset_clear(&cs);
}
worktree_copy_cleanup:
free(from_file);
free(to_file);
}
strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf); strvec_pushf(&child_env, "%s=%s", GIT_DIR_ENVIRONMENT, sb_git.buf);
strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path); strvec_pushf(&child_env, "%s=%s", GIT_WORK_TREE_ENVIRONMENT, path);
@ -417,17 +437,9 @@ static int add_worktree(const char *path, const char *refname,
if (ret) if (ret)
goto done; goto done;
if (opts->checkout) { if (opts->checkout &&
struct child_process cp = CHILD_PROCESS_INIT; (ret = checkout_worktree(opts, &child_env)))
cp.git_cmd = 1; goto done;
strvec_pushl(&cp.args, "reset", "--hard", "--no-recurse-submodules", NULL);
if (opts->quiet)
strvec_push(&cp.args, "--quiet");
strvec_pushv(&cp.env_array, child_env.v);
ret = run_command(&cp);
if (ret)
goto done;
}
is_junk = 0; is_junk = 0;
FREE_AND_NULL(junk_work_tree); FREE_AND_NULL(junk_work_tree);