Skip to content

Commit 672aa8d

Browse files
committed
Issue-1471 Added functionality to apply or remove locks.
1 parent cbf23cc commit 672aa8d

File tree

5 files changed

+95
-7
lines changed

5 files changed

+95
-7
lines changed

LibGit2Sharp.Tests/WorktreeFixture.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,52 @@ public void CanViewLockStatusForWorktrees()
7171
}
7272
}
7373

74+
[Fact]
75+
public void CanUnlockWorktree()
76+
{
77+
var testpath = SandboxWorktreeTestRepo();
78+
var repoPath = testpath;
79+
using (var repo = new Repository(repoPath))
80+
{
81+
// locked
82+
var worktreeLocked = repo.Worktrees["logo"];
83+
Assert.Equal("logo", worktreeLocked.Name);
84+
Assert.True(worktreeLocked.IsLocked);
85+
Assert.Equal("Test lock reason\n", worktreeLocked.LockReason);
86+
87+
worktreeLocked.Unlock();
88+
89+
// not locked
90+
var worktreeUnlocked = repo.Worktrees["logo"];
91+
Assert.Equal("logo", worktreeLocked.Name);
92+
Assert.False(worktreeUnlocked.IsLocked);
93+
Assert.Null(worktreeUnlocked.LockReason);
94+
}
95+
}
96+
97+
[Fact]
98+
public void CanLockWorktree()
99+
{
100+
var testpath = SandboxWorktreeTestRepo();
101+
var repoPath = testpath;
102+
using (var repo = new Repository(repoPath))
103+
{
104+
// locked
105+
var worktreeUnlocked = repo.Worktrees["i-do-numbers"];
106+
Assert.Equal("i-do-numbers", worktreeUnlocked.Name);
107+
Assert.False(worktreeUnlocked.IsLocked);
108+
Assert.Null(worktreeUnlocked.LockReason);
109+
110+
worktreeUnlocked.Lock("add a lock");
111+
112+
// not locked
113+
var worktreeLocked = repo.Worktrees["i-do-numbers"];
114+
Assert.Equal("i-do-numbers", worktreeLocked.Name);
115+
Assert.True(worktreeLocked.IsLocked);
116+
Assert.Equal("add a lock", worktreeLocked.LockReason);
117+
}
118+
}
119+
74120
[Fact]
75121
public void CanGetRepositoryForWorktree()
76122
{

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1913,6 +1913,15 @@ internal static extern unsafe int git_worktree_is_locked(
19131913
internal static extern unsafe int git_worktree_validate(
19141914
git_worktree* worktree);
19151915

1916+
[DllImport(libgit2)]
1917+
internal static extern unsafe int git_worktree_lock(
1918+
git_worktree* worktree,
1919+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string reason);
1920+
1921+
[DllImport(libgit2)]
1922+
internal static extern unsafe int git_worktree_unlock(
1923+
git_worktree* worktree);
1924+
19161925
}
19171926
}
19181927
// ReSharper restore InconsistentNaming

LibGit2Sharp/Core/Proxy.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,21 @@ public static unsafe WorktreeLock git_worktree_is_locked(WorktreeHandle worktree
35313531
public static unsafe bool git_worktree_validate(WorktreeHandle worktree)
35323532
{
35333533
int res = NativeMethods.git_worktree_validate(worktree);
3534+
3535+
return res == (int)GitErrorCode.Ok;
3536+
}
3537+
3538+
public static unsafe bool git_worktree_unlock(WorktreeHandle worktree)
3539+
{
3540+
int res = NativeMethods.git_worktree_unlock(worktree);
3541+
3542+
return res == (int)GitErrorCode.Ok;
3543+
}
3544+
3545+
public static unsafe bool git_worktree_lock(WorktreeHandle worktree, string reason)
3546+
{
3547+
int res = NativeMethods.git_worktree_lock(worktree, reason);
3548+
35343549
return res == (int)GitErrorCode.Ok;
35353550
}
35363551

LibGit2Sharp/Worktree.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Core.Handles;
23
using System;
34
using System.Collections.Generic;
45
using System.Diagnostics;
@@ -16,19 +17,21 @@ public class Worktree : IEquatable<Worktree>, IBelongToARepository
1617
private static readonly LambdaEqualityHelper<Worktree> equalityHelper =
1718
new LambdaEqualityHelper<Worktree>(x => x.Name);
1819

20+
private readonly WorktreeHandle handle;
1921
private readonly Repository parent;
2022
private readonly Repository worktree;
2123
private readonly string name;
22-
private readonly WorktreeLock worktreeLock;
24+
private WorktreeLock worktreeLock;
2325

2426
/// <summary>
2527
/// Needed for mocking purposes.
2628
/// </summary>
2729
protected Worktree()
2830
{ }
2931

30-
internal Worktree(Repository repo, string name, Repository worktree, WorktreeLock worktreeLock)
32+
internal Worktree(WorktreeHandle handle, Repository repo, string name, Repository worktree, WorktreeLock worktreeLock)
3133
{
34+
this.handle = Proxy.git_worktree_lookup(repo.Handle, name);
3235
this.parent = repo;
3336
this.name = name;
3437
this.worktree = worktree;
@@ -75,6 +78,24 @@ public bool Equals(Worktree other)
7578
return equalityHelper.Equals(this, other);
7679
}
7780

81+
/// <summary>
82+
/// Unlock the worktree
83+
/// </summary>
84+
public void Unlock()
85+
{
86+
Proxy.git_worktree_unlock(handle);
87+
this.worktreeLock = Proxy.git_worktree_is_locked(handle);
88+
}
89+
90+
/// <summary>
91+
/// Lock the worktree
92+
/// </summary>
93+
public void Lock(string reason)
94+
{
95+
Proxy.git_worktree_lock(handle, reason);
96+
this.worktreeLock = Proxy.git_worktree_is_locked(handle);
97+
}
98+
7899
/// <summary>
79100
/// Returns the hash code for this instance.
80101
/// </summary>

LibGit2Sharp/WorktreeCollection.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public virtual Worktree this[string name]
4040
{
4141
Ensure.ArgumentNotNullOrEmptyString(name, "name");
4242

43-
return Lookup(name, handle => new Worktree(repo,
43+
return Lookup(name, handle => new Worktree(handle, repo,
4444
name,
4545
new Repository(handle),
4646
Proxy.git_worktree_is_locked(handle)));
@@ -72,12 +72,9 @@ internal T Lookup<T>(string name, Func<WorktreeHandle, T> selector, bool throwIf
7272
public virtual IEnumerator<Worktree> GetEnumerator()
7373
{
7474
return Proxy.git_worktree_list(repo.Handle)
75-
.Select(n => Lookup(n, handle => new Worktree(repo, n,
75+
.Select(n => Lookup(n, handle => new Worktree(handle, repo, n,
7676
new Repository(handle), Proxy.git_worktree_is_locked(handle))))
7777
.GetEnumerator();
78-
//return Proxy.git_submodule_foreach(repo.Handle, (h, n) => LaxUtf8Marshaler.FromNative(n))
79-
// .Select(n => this[n])
80-
// .GetEnumerator();
8178
}
8279

8380
/// <summary>

0 commit comments

Comments
 (0)