<feed xmlns='http://www.w3.org/2005/Atom'>
<title>git/worktree.h, branch v2.51.2</title>
<subtitle>Mirror of https://git.kernel.org/pub/scm/git/git.git/
</subtitle>
<id>https://git.shady.money/git/atom?h=v2.51.2</id>
<link rel='self' href='https://git.shady.money/git/atom?h=v2.51.2'/>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/'/>
<updated>2025-03-26T07:26:10Z</updated>
<entry>
<title>Merge branch 'sj/ref-consistency-checks-more'</title>
<updated>2025-03-26T07:26:10Z</updated>
<author>
<name>Junio C Hamano</name>
<email>gitster@pobox.com</email>
</author>
<published>2025-03-26T07:26:09Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=de35b7b3ffb6e642b47f748eb38e31182377fb0a'/>
<id>urn:sha1:de35b7b3ffb6e642b47f748eb38e31182377fb0a</id>
<content type='text'>
"git fsck" becomes more careful when checking the refs.

* sj/ref-consistency-checks-more:
  builtin/fsck: add `git refs verify` child process
  packed-backend: check whether the "packed-refs" is sorted
  packed-backend: add "packed-refs" entry consistency check
  packed-backend: check whether the refname contains NUL characters
  packed-backend: add "packed-refs" header consistency check
  packed-backend: check if header starts with "# pack-refs with: "
  packed-backend: check whether the "packed-refs" is regular file
  builtin/refs: get worktrees without reading head information
  t0602: use subshell to ensure working directory unchanged
</content>
</entry>
<entry>
<title>builtin/refs: get worktrees without reading head information</title>
<updated>2025-02-27T22:03:07Z</updated>
<author>
<name>shejialuo</name>
<email>shejialuo@gmail.com</email>
</author>
<published>2025-02-27T16:06:06Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=fdf3820b7ef69dcf887bd86565e2442f89edc7c0'/>
<id>urn:sha1:fdf3820b7ef69dcf887bd86565e2442f89edc7c0</id>
<content type='text'>
In "packed-backend.c", there are some functions such as "create_snapshot"
and "next_record" which would check the correctness of the content of
the "packed-ref" file. When anything is bad, the program will die.

It may seem that we have nothing relevant to above feature, because we
are going to read and parse the raw "packed-ref" file without creating
the snapshot and using the ref iterator to check the consistency.

However, when using "get_worktrees" in "builtin/refs", we would parse
the "HEAD" information. If the referent of the "HEAD" is inside the
"packed-ref", we will call "create_snapshot" function to parse the
"packed-ref" to get the information. No matter whether the entry of
"HEAD" in "packed-ref" is correct, "create_snapshot" would call
"verify_buffer_safe" to check whether there is a newline in the last
line of the file. If not, the program will die.

Although this behavior has no harm for the program, it will
short-circuit the program. When the users execute "git refs verify" or
"git fsck", we should avoid reading the head information, which may
execute the read operation in packed backend with stricter checks to die
the program. Instead, we should continue to check other parts of the
"packed-refs" file completely.

Fortunately, in 465a22b338 (worktree: skip reading HEAD when repairing
worktrees, 2023-12-29), we have introduced a function
"get_worktrees_internal" which allows us to get worktrees without
reading head information.

Create a new exposed function "get_worktrees_without_reading_head", then
replace the "get_worktrees" in "builtin/refs" with the new created
function.

Mentored-by: Patrick Steinhardt &lt;ps@pks.im&gt;
Mentored-by: Karthik Nayak &lt;karthik.188@gmail.com&gt;
Signed-off-by: shejialuo &lt;shejialuo@gmail.com&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>worktree: return allocated string from `get_worktree_git_dir()`</title>
<updated>2025-02-07T17:59:23Z</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2025-02-07T11:03:34Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=8e4710f011dce286d24838fdafd5ce52cfac5285'/>
<id>urn:sha1:8e4710f011dce286d24838fdafd5ce52cfac5285</id>
<content type='text'>
The `get_worktree_git_dir()` function returns a string constant that
does not need to be free'd by the caller. This string is computed for
three different cases:

  - If we don't have a worktree we return a path into the Git directory.
    The returned string is owned by `the_repository`, so there is no
    need for the caller to free it.

  - If we have a worktree, but no worktree ID then the caller requests
    the main worktree. In this case we return a path into the common
    directory, which again is owned by `the_repository` and thus does
    not need to be free'd.

  - In the third case, where we have an actual worktree, we compute the
    path relative to "$GIT_COMMON_DIR/worktrees/". This string does not
    need to be released either, even though `git_common_path()` ends up
    allocating memory. But this doesn't result in a memory leak either
    because we write into a buffer returned by `get_pathname()`, which
    returns one out of four static buffers.

We're about to drop `git_common_path()` in favor of `repo_common_path()`,
which doesn't use the same mechanism but instead returns an allocated
string owned by the caller. While we could adapt `get_worktree_git_dir()`
to also use `get_pathname()` and print the derived common path into that
buffer, the whole schema feels a lot like premature optimization in this
context. There are some callsites where we call `get_worktree_git_dir()`
in a loop that iterates through all worktrees. But none of these loops
seem to be even remotely in the hot path, so saving a single allocation
there does not feel worth it.

Refactor the function to instead consistently return an allocated path
so that we can start using `repo_common_path()` in a subsequent commit.

Signed-off-by: Patrick Steinhardt &lt;ps@pks.im&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>worktree: add relative cli/config options to `repair` command</title>
<updated>2024-12-02T00:36:17Z</updated>
<author>
<name>Caleb White</name>
<email>cdwhite3@pm.me</email>
</author>
<published>2024-11-29T22:23:10Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=e6df1ee2c13405ef7077256fef49424f69d61125'/>
<id>urn:sha1:e6df1ee2c13405ef7077256fef49424f69d61125</id>
<content type='text'>
This teaches the `worktree repair` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree with an absolute path is repaired
with `--relative-paths`, the links will be replaced with relative paths,
even if the original path was correct. This allows a user to covert
existing worktrees between absolute/relative as desired.

To simplify things, both linking files are written when one of the files
needs to be repaired. In some cases, this fixes the other file before it
is checked, in other cases this results in a correct file being written
with the same contents.

Signed-off-by: Caleb White &lt;cdwhite3@pm.me&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>worktree: add relative cli/config options to `move` command</title>
<updated>2024-12-02T00:36:17Z</updated>
<author>
<name>Caleb White</name>
<email>cdwhite3@pm.me</email>
</author>
<published>2024-11-29T22:23:03Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=298d2917e26520791b47ba5d38c1866e21894cc7'/>
<id>urn:sha1:298d2917e26520791b47ba5d38c1866e21894cc7</id>
<content type='text'>
This teaches the `worktree move` command to respect the
`--[no-]relative-paths` CLI option and `worktree.useRelativePaths`
config setting. If an existing worktree is moved with `--relative-paths`
the new path will be relative (and visa-versa).

Signed-off-by: Caleb White &lt;cdwhite3@pm.me&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>worktree: add `write_worktree_linking_files()` function</title>
<updated>2024-12-02T00:36:17Z</updated>
<author>
<name>Caleb White</name>
<email>cdwhite3@pm.me</email>
</author>
<published>2024-11-29T22:22:47Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=4dac9e3c01cf056edd315e0ed26e6df1c4d94571'/>
<id>urn:sha1:4dac9e3c01cf056edd315e0ed26e6df1c4d94571</id>
<content type='text'>
A new helper function, `write_worktree_linking_files()`, centralizes
the logic for computing and writing either relative or absolute
paths, based on the provided configuration. This function accepts
`strbuf` pointers to both the worktree’s `.git` link and the
repository’s `gitdir`, and then writes the appropriate path to each.
The `relativeWorktrees` extension is automatically set when a worktree
is linked with relative paths.

Signed-off-by: Caleb White &lt;cdwhite3@pm.me&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>worktree: link worktrees with relative paths</title>
<updated>2024-10-08T18:49:22Z</updated>
<author>
<name>Caleb White</name>
<email>cdwhite3@pm.me</email>
</author>
<published>2024-10-08T03:12:31Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=717af916cd69d2565aa2a7b7d73d895aa92ff392'/>
<id>urn:sha1:717af916cd69d2565aa2a7b7d73d895aa92ff392</id>
<content type='text'>
Git currently stores absolute paths to both the main repository and
linked worktrees. However, this causes problems when moving repositories
or working in containerized environments where absolute paths differ
between systems. The worktree links break, and users are required to
manually execute `worktree repair` to repair them, leading to workflow
disruptions. Additionally, mapping repositories inside of containerized
environments renders the repository unusable inside the containers, and
this is not repairable as repairing the worktrees inside the containers
will result in them being broken outside the containers.

To address this, this patch makes Git always write relative paths when
linking worktrees. Relative paths increase the resilience of the
worktree links across various systems and environments, particularly
when the worktrees are self-contained inside the main repository (such
as when using a bare repository with worktrees). This improves
portability, workflow efficiency, and reduces overall breakages.

Although Git now writes relative paths, existing repositories with
absolute paths are still supported. There are no breaking changes
to workflows based on absolute paths, ensuring backward compatibility.

At a low level, the changes involve modifying functions in `worktree.c`
and `builtin/worktree.c` to use `relative_path()` when writing the
worktree’s `.git` file and the main repository’s `gitdir` reference.
Instead of hardcoding absolute paths, Git now computes the relative path
between the worktree and the repository, ensuring that these links are
portable. Locations where these respective file are read have also been
updated to properly handle both absolute and relative paths. Generally,
relative paths are always resolved into absolute paths before any
operations or comparisons are performed.

Additionally, `repair_worktrees_after_gitdir_move()` has been introduced
to address the case where both the `&lt;worktree&gt;/.git` and
`&lt;repo&gt;/worktrees/&lt;id&gt;/gitdir` links are broken after the gitdir is
moved (such as during a re-initialization). This function repairs both
sides of the worktree link using the old gitdir path to reestablish the
correct paths after a move.

The `worktree.path` struct member has also been updated to always store
the absolute path of a worktree. This ensures that worktree consumers
never have to worry about trying to resolve the absolute path themselves.

Signed-off-by: Caleb White &lt;cdwhite3@pm.me&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>Merge branch 'jc/worktree-git-path'</title>
<updated>2024-06-24T23:39:15Z</updated>
<author>
<name>Junio C Hamano</name>
<email>gitster@pobox.com</email>
</author>
<published>2024-06-24T23:39:15Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=107ed551030713e25933dc8e92ad8d67cff28ff6'/>
<id>urn:sha1:107ed551030713e25933dc8e92ad8d67cff28ff6</id>
<content type='text'>
Code cleanup.

* jc/worktree-git-path:
  worktree_git_path(): move the declaration to path.h
</content>
</entry>
<entry>
<title>worktree_git_path(): move the declaration to path.h</title>
<updated>2024-06-08T18:42:37Z</updated>
<author>
<name>Junio C Hamano</name>
<email>gitster@pobox.com</email>
</author>
<published>2024-06-08T18:39:01Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=bf6a86236e829d08c92356e5c861193ccb516ef7'/>
<id>urn:sha1:bf6a86236e829d08c92356e5c861193ccb516ef7</id>
<content type='text'>
The definition of this function is in path.c but its declaration is
in worktree.h, which is something unexpected.  The function is
explained as "Similar to git_path()"; declaring it next to where
git_path() is declared would make more sense.

Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
<entry>
<title>refs: retrieve worktree ref stores via associated repository</title>
<updated>2024-05-17T17:33:38Z</updated>
<author>
<name>Patrick Steinhardt</name>
<email>ps@pks.im</email>
</author>
<published>2024-05-17T08:18:44Z</published>
<link rel='alternate' type='text/html' href='https://git.shady.money/git/commit/?id=dc7fb4f72c2e39ffbb98aee55ad7ea4c3f8e12fc'/>
<id>urn:sha1:dc7fb4f72c2e39ffbb98aee55ad7ea4c3f8e12fc</id>
<content type='text'>
Similar as with the preceding commit, the worktree ref stores are always
looked up via `the_repository`. Also, again, those ref stores are stored
in a global map.

Refactor the code so that worktrees have a pointer to their repository.
Like this, we can move the global map into `struct repository` and stop
using `the_repository`. With this change, we can now in theory look up
worktree ref stores for repositories other than `the_repository`. In
practice, the worktree code will need further changes to look up
arbitrary worktrees.

Signed-off-by: Patrick Steinhardt &lt;ps@pks.im&gt;
Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</content>
</entry>
</feed>
