Skip to content

Commit 9dc946e

Browse files
committed
Fix --setup failing on fresh bare clones
- Set repositoryformatversion to 1 before enabling the extensions.relativeWorktrees v1 extension - Use existing local branch when bare clone already created it instead of failing with "branch already exists" - Pin install-local scripts to 0.0.0-local version to avoid pulling the published NuGet package
1 parent 1151879 commit 9dc946e

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

install-local.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ dotnet tool uninstall -g git-wt 2>$null
33

44
# Pack and install
55
dotnet pack src/git-wt -o ./artifacts
6-
dotnet tool install -g --add-source ./artifacts git-wt
6+
dotnet tool install -g --add-source ./artifacts --version "0.0.0-local" git-wt

install-local.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ dotnet tool uninstall -g git-wt 2>/dev/null || true
66

77
# Pack and install
88
dotnet pack src/git-wt -o ./artifacts
9-
dotnet tool install -g --add-source ./artifacts git-wt
9+
dotnet tool install -g --add-source ./artifacts --version "0.0.0-local" git-wt

src/git-wt/Commands.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,12 @@ public static int Setup(string remoteUrl)
121121
Console.WriteLine($"Creating worktree for '{defaultBranch}'...");
122122

123123
int wtExit;
124+
var localBranchExists = Git.Run(bareDir, "rev-parse", "--verify", $"refs/heads/{defaultBranch}").ExitCode == 0;
124125
using (new RelativeWorktreeScope(bareDir))
125126
{
126-
wtExit = Git.RunLive(bareDir, "worktree", "add", "--track", "-b", defaultBranch, worktreePath, $"origin/{defaultBranch}");
127+
wtExit = localBranchExists
128+
? Git.RunLive(bareDir, "worktree", "add", worktreePath, defaultBranch)
129+
: Git.RunLive(bareDir, "worktree", "add", "--track", "-b", defaultBranch, worktreePath, $"origin/{defaultBranch}");
127130
}
128131

129132
if (wtExit != 0)

src/git-wt/Git.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,20 @@ static Process StartProcess(ProcessStartInfo psi)
119119
sealed class RelativeWorktreeScope : IDisposable
120120
{
121121
readonly string _gitDir;
122+
readonly string? _previousRepoVersion;
122123
readonly bool _relExtWasEnabled;
123124
readonly bool _relPathWasEnabled;
124125

125126
public RelativeWorktreeScope(string gitDir)
126127
{
127128
_gitDir = gitDir;
128129

130+
// extensions.relativeWorktrees requires repositoryformatversion = 1.
131+
var (verExit, verOutput, _) = Git.Run(gitDir, "config", "--get", "core.repositoryformatversion");
132+
_previousRepoVersion = verExit == 0 ? verOutput.Trim() : null;
133+
if (_previousRepoVersion != "1")
134+
Git.Run(gitDir, "config", "core.repositoryformatversion", "1");
135+
129136
var (relExtExit, relExtOutput, _) = Git.Run(gitDir, "config", "--get", "extensions.relativeWorktrees");
130137
_relExtWasEnabled = relExtExit == 0 && relExtOutput.Trim().Equals("true", StringComparison.OrdinalIgnoreCase);
131138

@@ -144,5 +151,13 @@ public void Dispose()
144151
Git.Run(_gitDir, "config", "--unset", "extensions.relativeWorktrees");
145152
if (!_relPathWasEnabled)
146153
Git.Run(_gitDir, "config", "--unset", "worktree.useRelativePaths");
154+
155+
if (_previousRepoVersion != "1")
156+
{
157+
if (_previousRepoVersion is not null)
158+
Git.Run(_gitDir, "config", "core.repositoryformatversion", _previousRepoVersion);
159+
else
160+
Git.Run(_gitDir, "config", "--unset", "core.repositoryformatversion");
161+
}
147162
}
148163
}

0 commit comments

Comments
 (0)