-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Git worktrees for
sync
(#1831)
## Changes This change allows the `sync` command to work from [git worktrees](https://git-scm.com/docs/git-worktree). ## Tests * Added unit tests for traversal of worktree related files. * Manually confirmed that synchronization of files from a main checkout, as well as a worktree, observed the same ignore rules (both locally defined as well as from `$GIT_DIR/info/exclude`). --------- Co-authored-by: Pieter Noordhuis <[email protected]>
- Loading branch information
Showing
4 changed files
with
271 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package git | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"io/fs" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/databricks/cli/libs/vfs" | ||
) | ||
|
||
func readLines(root vfs.Path, name string) ([]string, error) { | ||
file, err := root.Open(name) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer file.Close() | ||
|
||
var lines []string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
} | ||
|
||
return lines, scanner.Err() | ||
} | ||
|
||
// readGitDir reads the value of the `.git` file in a worktree. | ||
func readGitDir(root vfs.Path) (string, error) { | ||
lines, err := readLines(root, GitDirectoryName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
var gitDir string | ||
for _, line := range lines { | ||
parts := strings.SplitN(line, ": ", 2) | ||
if len(parts) != 2 { | ||
continue | ||
} | ||
|
||
if parts[0] == "gitdir" { | ||
gitDir = strings.TrimSpace(parts[1]) | ||
} | ||
} | ||
|
||
if gitDir == "" { | ||
return "", fmt.Errorf(`expected %q to contain a line with "gitdir: [...]"`, filepath.Join(root.Native(), GitDirectoryName)) | ||
} | ||
|
||
return gitDir, nil | ||
} | ||
|
||
// readGitCommonDir reads the value of the `commondir` file in the `.git` directory of a worktree. | ||
// This file typically contains "../.." to point to $GIT_COMMON_DIR. | ||
func readGitCommonDir(gitDir vfs.Path) (string, error) { | ||
lines, err := readLines(gitDir, "commondir") | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if len(lines) == 0 { | ||
return "", errors.New("file is empty") | ||
} | ||
|
||
return strings.TrimSpace(lines[0]), nil | ||
} | ||
|
||
// resolveGitDirs resolves the paths for $GIT_DIR and $GIT_COMMON_DIR. | ||
// The path argument is the root of the checkout where (supposedly) a `.git` file or directory exists. | ||
func resolveGitDirs(root vfs.Path) (vfs.Path, vfs.Path, error) { | ||
fileInfo, err := root.Stat(GitDirectoryName) | ||
if err != nil { | ||
// If the `.git` file or directory does not exist, then this is not a git repository. | ||
// Return paths that we know don't exist, so we do not need to perform nil checks in the caller. | ||
if errors.Is(err, fs.ErrNotExist) { | ||
gitDir := vfs.MustNew(filepath.Join(root.Native(), GitDirectoryName)) | ||
return gitDir, gitDir, nil | ||
} | ||
return nil, nil, err | ||
} | ||
|
||
// If the path is a directory, then it is the main working tree. | ||
// Both $GIT_DIR and $GIT_COMMON_DIR point to the same directory. | ||
if fileInfo.IsDir() { | ||
gitDir := vfs.MustNew(filepath.Join(root.Native(), GitDirectoryName)) | ||
return gitDir, gitDir, nil | ||
} | ||
|
||
// If the path is not a directory, then it is a worktree. | ||
// Read value for $GIT_DIR. | ||
gitDirValue, err := readGitDir(root) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
// Resolve $GIT_DIR. | ||
var gitDir vfs.Path | ||
if filepath.IsAbs(gitDirValue) { | ||
gitDir = vfs.MustNew(gitDirValue) | ||
} else { | ||
gitDir = vfs.MustNew(filepath.Join(root.Native(), gitDirValue)) | ||
} | ||
|
||
// Read value for $GIT_COMMON_DIR. | ||
gitCommonDirValue, err := readGitCommonDir(gitDir) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf(`expected "commondir" file in worktree git folder at %q: %w`, gitDir.Native(), err) | ||
} | ||
|
||
// Resolve $GIT_COMMON_DIR. | ||
var gitCommonDir vfs.Path | ||
if filepath.IsAbs(gitCommonDirValue) { | ||
gitCommonDir = vfs.MustNew(gitCommonDirValue) | ||
} else { | ||
gitCommonDir = vfs.MustNew(filepath.Join(gitDir.Native(), gitCommonDirValue)) | ||
} | ||
|
||
return gitDir, gitCommonDir, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package git | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/databricks/cli/libs/vfs" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func setupWorktree(t *testing.T) string { | ||
var err error | ||
|
||
tmpDir := t.TempDir() | ||
|
||
// Checkout path | ||
err = os.MkdirAll(filepath.Join(tmpDir, "my_worktree"), os.ModePerm) | ||
require.NoError(t, err) | ||
|
||
// Main $GIT_COMMON_DIR | ||
err = os.MkdirAll(filepath.Join(tmpDir, ".git"), os.ModePerm) | ||
require.NoError(t, err) | ||
|
||
// Worktree $GIT_DIR | ||
err = os.MkdirAll(filepath.Join(tmpDir, ".git/worktrees/my_worktree"), os.ModePerm) | ||
require.NoError(t, err) | ||
|
||
return tmpDir | ||
} | ||
|
||
func writeGitDir(t *testing.T, dir, content string) { | ||
err := os.WriteFile(filepath.Join(dir, "my_worktree/.git"), []byte(content), os.ModePerm) | ||
require.NoError(t, err) | ||
} | ||
|
||
func writeGitCommonDir(t *testing.T, dir, content string) { | ||
err := os.WriteFile(filepath.Join(dir, ".git/worktrees/my_worktree/commondir"), []byte(content), os.ModePerm) | ||
require.NoError(t, err) | ||
} | ||
|
||
func verifyCorrectDirs(t *testing.T, dir string) { | ||
gitDir, gitCommonDir, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree"))) | ||
require.NoError(t, err) | ||
assert.Equal(t, filepath.Join(dir, ".git/worktrees/my_worktree"), gitDir.Native()) | ||
assert.Equal(t, filepath.Join(dir, ".git"), gitCommonDir.Native()) | ||
} | ||
|
||
func TestWorktreeResolveGitDir(t *testing.T) { | ||
dir := setupWorktree(t) | ||
writeGitCommonDir(t, dir, "../..") | ||
|
||
t.Run("relative", func(t *testing.T) { | ||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", "../.git/worktrees/my_worktree")) | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("absolute", func(t *testing.T) { | ||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", filepath.Join(dir, ".git/worktrees/my_worktree"))) | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("additional spaces", func(t *testing.T) { | ||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s \n\n\n", "../.git/worktrees/my_worktree")) | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
writeGitDir(t, dir, "") | ||
|
||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree"))) | ||
assert.ErrorContains(t, err, ` to contain a line with "gitdir: [...]"`) | ||
}) | ||
} | ||
|
||
func TestWorktreeResolveCommonDir(t *testing.T) { | ||
dir := setupWorktree(t) | ||
writeGitDir(t, dir, fmt.Sprintf("gitdir: %s", "../.git/worktrees/my_worktree")) | ||
|
||
t.Run("relative", func(t *testing.T) { | ||
writeGitCommonDir(t, dir, "../..") | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("absolute", func(t *testing.T) { | ||
writeGitCommonDir(t, dir, filepath.Join(dir, ".git")) | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("additional spaces", func(t *testing.T) { | ||
writeGitCommonDir(t, dir, " ../.. \n\n\n") | ||
verifyCorrectDirs(t, dir) | ||
}) | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
writeGitCommonDir(t, dir, "") | ||
|
||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree"))) | ||
assert.ErrorContains(t, err, `expected "commondir" file in worktree git folder at `) | ||
}) | ||
|
||
t.Run("missing", func(t *testing.T) { | ||
_, _, err := resolveGitDirs(vfs.MustNew(filepath.Join(dir, "my_worktree"))) | ||
assert.ErrorContains(t, err, `expected "commondir" file in worktree git folder at `) | ||
}) | ||
} |