Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty folder issue in Repositoy.Worktrees.Add #2099

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 103 additions & 6 deletions LibGit2Sharp.Tests/WorktreeFixture.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using LibGit2Sharp.Tests.TestHelpers;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
Expand Down Expand Up @@ -238,7 +236,7 @@ public void CanForcePruneLockedWorktree()
}

[Fact]
public void CanAddWorktree()
public void CanAddWorktree_WithUncommitedChanges()
{
var repoPath = SandboxWorktreeTestRepo();
using (var repo = new Repository(repoPath))
Expand All @@ -252,11 +250,54 @@ public void CanAddWorktree()
Assert.False(worktree.IsLocked);

Assert.Equal(3, repo.Worktrees.Count());

// Check that branch contains same number of files and folders
Assert.True(repo.RetrieveStatus().IsDirty);
var filesInMain = GetFilesOfRepo(repoPath);
var filesInBranch = GetFilesOfRepo(path);
Assert.NotEqual(filesInMain, filesInBranch);

repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();

Assert.False(repo.RetrieveStatus().IsDirty);
filesInMain = GetFilesOfRepo(repoPath);
filesInBranch = GetFilesOfRepo(path);
Assert.Equal(filesInMain, filesInBranch);
}
}

[Fact]
public void CanAddLockedWorktree()
public void CanAddWorktree_WithCommitedChanges()
{
var repoPath = SandboxWorktreeTestRepo();
using (var repo = new Repository(repoPath))
{
// stage all changes
Commands.Stage(repo, "*");
repo.Commit("Apply all changes", Constants.Signature, Constants.Signature);

Assert.Equal(2, repo.Worktrees.Count());

var name = "blah";
var path = Path.Combine(repo.Info.WorkingDirectory, "..", "worktrees", name);
var worktree = repo.Worktrees.Add(name, path, false);
Assert.Equal(name, worktree.Name);
Assert.False(worktree.IsLocked);

Assert.Equal(3, repo.Worktrees.Count());

// Check that branch contains same number of files and folders
Assert.False(repo.RetrieveStatus().IsDirty);
var filesInMain = GetFilesOfRepo(repoPath);
var filesInBranch = GetFilesOfRepo(path);

Assert.Equal(filesInMain, filesInBranch);
}
}

[Fact]
public void CanAddLockedWorktree_WithUncommitedChanges()
{
var repoPath = SandboxWorktreeTestRepo();
using (var repo = new Repository(repoPath))
Expand All @@ -270,6 +311,48 @@ public void CanAddLockedWorktree()
Assert.True(worktree.IsLocked);

Assert.Equal(3, repo.Worktrees.Count());

// Check that branch contains same number of files and folders
Assert.True(repo.RetrieveStatus().IsDirty);
var filesInMain = GetFilesOfRepo(repoPath);
var filesInBranch = GetFilesOfRepo(path);
Assert.NotEqual(filesInMain, filesInBranch);

repo.Reset(ResetMode.Hard);
repo.RemoveUntrackedFiles();

Assert.False(repo.RetrieveStatus().IsDirty);
filesInMain = GetFilesOfRepo(repoPath);
filesInBranch = GetFilesOfRepo(path);
Assert.Equal(filesInMain, filesInBranch);
}
}

[Fact]
public void CanAddLockedWorktree_WithCommitedChanges()
{
var repoPath = SandboxWorktreeTestRepo();
using (var repo = new Repository(repoPath))
{
// stage all changes
Commands.Stage(repo, "*");
repo.Commit("Apply all changes", Constants.Signature, Constants.Signature);

Assert.Equal(2, repo.Worktrees.Count());

var name = "blah";
var path = Path.Combine(repo.Info.WorkingDirectory, "..", "worktrees", name);
var worktree = repo.Worktrees.Add(name, path, true);
Assert.Equal(name, worktree.Name);
Assert.True(worktree.IsLocked);

Assert.Equal(3, repo.Worktrees.Count());

// Check that branch contains same number of files and folders
Assert.False(repo.RetrieveStatus().IsDirty);
var filesInMain = GetFilesOfRepo(repoPath);
var filesInBranch = GetFilesOfRepo(path);
Assert.Equal(filesInMain, filesInBranch);
}
}

Expand All @@ -292,7 +375,21 @@ public void CanAddWorktreeForCommittish()
Assert.Equal(committish, repository.Head.FriendlyName);
}
Assert.Equal(3, repo.Worktrees.Count());

// Check that branch contains same number of files and folders
var filesInCommittish = new string[] { "numbers.txt", "super-file.txt" };
var filesInBranch = GetFilesOfRepo(path);
Assert.Equal(filesInCommittish, filesInBranch);
}
}

private static IEnumerable<string> GetFilesOfRepo(string repoPath)
{
return Directory.GetFiles(repoPath, "*", SearchOption.AllDirectories)
.Where(fileName => !fileName.StartsWith(Path.Combine(repoPath, ".git")))
.Select(fileName => fileName.Replace($"{repoPath}{Path.DirectorySeparatorChar}", ""))
.OrderBy(fileName => fileName)
.ToList();
}
}
}
6 changes: 5 additions & 1 deletion LibGit2Sharp/Core/GitWorktree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ internal class git_worktree_add_options

public IntPtr @ref = IntPtr.Zero;

public GitCheckoutOpts checkoutOpts = new GitCheckoutOpts { version = 1 };
public GitCheckoutOpts checkoutOpts = new GitCheckoutOpts
{
version = 1,
checkout_strategy = CheckoutStrategy.GIT_CHECKOUT_SAFE
};
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
21 changes: 9 additions & 12 deletions LibGit2Sharp/WorktreeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public virtual Worktree this[string name]
}

/// <summary>
///
/// Creates a worktree.
/// </summary>
/// <param name="committishOrBranchSpec"></param>
/// <param name="name"></param>
/// <param name="path"></param>
/// <param name="committishOrBranchSpec">The committish to checkout into the new worktree.</param>
/// <param name="name">Name of the worktree.</param>
/// <param name="path">Location of the worktree.</param>
/// <param name="isLocked"></param>
/// <returns></returns>
public virtual Worktree Add(string committishOrBranchSpec, string name, string path, bool isLocked)
{
if (string.Equals(committishOrBranchSpec, name))
Expand All @@ -61,7 +60,7 @@ public virtual Worktree Add(string committishOrBranchSpec, string name, string p
return null;
}

git_worktree_add_options options = new git_worktree_add_options
var options = new git_worktree_add_options
{
version = 1,
locked = Convert.ToInt32(isLocked)
Expand All @@ -81,20 +80,18 @@ public virtual Worktree Add(string committishOrBranchSpec, string name, string p
}
}



return this[name];
}

/// <summary>
///
/// Creates a worktree.
/// </summary>
/// <param name="name"></param>
/// <param name="path"></param>
/// <param name="name">Name of the worktree.</param>
/// <param name="path">Location of the worktree.</param>
/// <param name="isLocked"></param>
public virtual Worktree Add(string name, string path, bool isLocked)
{
git_worktree_add_options options = new git_worktree_add_options
var options = new git_worktree_add_options
{
version = 1,
locked = Convert.ToInt32(isLocked)
Expand Down