Skip to content

Commit

Permalink
Issue-1471 Add worktree support, based on the current level of suppor…
Browse files Browse the repository at this point in the history
…t in libgit2
  • Loading branch information
mminns committed Oct 23, 2018
1 parent 0a9c84d commit c4c15a5
Show file tree
Hide file tree
Showing 132 changed files with 689 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -38,3 +38,5 @@ _ReSharper*/
*.DotSettings

_NCrunch_LibGit2Sharp/
packages/
worktree.playlist
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/Resources/worktree/testrepo_wd/1.txt
@@ -0,0 +1 @@
one
@@ -0,0 +1 @@
hi
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/Resources/worktree/testrepo_wd/README
@@ -0,0 +1 @@
hey there
@@ -0,0 +1 @@
hi
@@ -0,0 +1 @@
Add "1.txt" file beside "1" folder
@@ -0,0 +1 @@
ref: refs/heads/master
23 changes: 23 additions & 0 deletions LibGit2Sharp.Tests/Resources/worktree/testrepo_wd/dot_git/config
@@ -0,0 +1,23 @@
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
hideDotFiles = dotGitOnly
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = c:/GitHub/libgit2sharp/Resources/testrepo.git
[remote "no_url"]
url =
fetch = +refs/heads/*:refs/remotes/no_url/*
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "track-local"]
remote = .
merge = refs/heads/master
[unittests]
longsetting = 15234
intsetting = 2
Binary file not shown.
@@ -0,0 +1,6 @@
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 4c062a6361ae6959e06292c1fa5e2822d9c96345 Tim Clem <timothy.clem@gmail.com> 1303768198 -0700 clone: from c:/GitHub/libgit2sharp/Resources/testrepo.git
4c062a6361ae6959e06292c1fa5e2822d9c96345 592d3c869dbc4127fc57c189cb94f2794fa84e7e Tim Clem <timothy.clem@gmail.com> 1303835722 -0700 commit: add more test files
592d3c869dbc4127fc57c189cb94f2794fa84e7e 32eab9cb1f450b5fe7ab663462b77d7f4b703344 nulltoken <emeric.fermas@gmail.com> 1320047537 +0100 commit: Add "1.txt" file beside "1" folder
@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 4c062a6361ae6959e06292c1fa5e2822d9c96345 nulltoken <emeric.fermas@gmail.com> 1359021419 +0100 branch: Created from 4c062a6361ae6959e06292c1fa5e2822d9c96345
4c062a6361ae6959e06292c1fa5e2822d9c96345 a447ba2ca8fffd46dece72f7db6faf324afb8fcd nulltoken <emeric.fermas@gmail.com> 1359021433 +0100 commit: Add logo
@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 4c062a6361ae6959e06292c1fa5e2822d9c96345 Tim Clem <timothy.clem@gmail.com> 1303768198 -0700 clone: from c:/GitHub/libgit2sharp/Resources/testrepo.git
4c062a6361ae6959e06292c1fa5e2822d9c96345 592d3c869dbc4127fc57c189cb94f2794fa84e7e Tim Clem <timothy.clem@gmail.com> 1303835722 -0700 commit: add more test files
592d3c869dbc4127fc57c189cb94f2794fa84e7e 32eab9cb1f450b5fe7ab663462b77d7f4b703344 nulltoken <emeric.fermas@gmail.com> 1320047537 +0100 commit: Add "1.txt" file beside "1" folder
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x̱
1EQ����jm\�,��Dl,'�Ä���:"���큛�3���^4�7�u�و� �\0yVg��(W����ϝ�XmL?�2��ʍj�K=yPK™��I� �#Y
Expand Down
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
x��]j!���)�} �?�!d��m�+�qq]���3䥠>����Z�������&�0�M1h�}�V��v�Y+��.a�9�z�4�O� *�LGxx��d�V�ðc^����Zg�����(�e-<n-��Qo߀kBg��O�R�E������5%��6��r� ��%�b���x�7G�P
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��Q
�0D��)��6�ͦ "xO���-�Fb��Eo��0 �Ǥ�,ske�[�Pn8R,EpD?�g�}�^3� ��<��G�hYK���8ЖDA��)���;g�ݧ���j�p�4����-������r���;�s�GA4ۺ=���(�in7�I�K�FE
@@ -0,0 +1,2 @@
x��K
1D]�� C�����tg&`&������Q��VkQ�Fg��au����xved��0Sr.�y�J��!'C^!� @�`2�,�@ ����(�{��O�i��_�eO������\�V�Ra����g�)�:��w>R-�&�B�Ey
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��Aj1 E��)t�{d[6��Ev���G� �ep�}B{��><x�_zk� �0vU@������X��T"��B�dS%
�8�û�����"z4pq1&�Y�% %��~���p�u,p�e�'��߽}~7^����:��gB8�`�y��á�q͗�>�����B0��I�
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��� 1E�Ni@��k2 "X�$�YW0Yc����Ås���z����MD��0��8!�����s� X��g�d�::@X0�P�w�"F/����R��Uz��m�ZZ�����V}|�/�o5����I�!�1z��:v��Uim}�/�>
�F-
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��A
� E���/c�J�=�8�TA#�]��M�����ϭ�<��p]�_��*�6t�<�0�l��z�8�+[�U�k����:%X�S�D�J���Y)��[����Y�4"�5j�z<��U�=�L~�6u;=
Expand Down
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
x��[
�0E��*f��դ "�W0��-�F�t�݁��pS[�Y��x�^
D�b CLhut��}�8X*4Z��sY���U��A���X3�R�M��) s6輢M��រ�&Jm��;}�����<��\@����ޏpĀ�v�?��jۺL��?H�
Expand Down
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��;j1D��md�����ǎ|M��3`��V{ >���Q����vL0I?�!�4�Z=�!�צ8�F���!r�sQ�y�9�]$D�&�l6A�>jFW�ҵ IKNi��Z�%�S���
�� ������U~̽��>'��� �w��[��� ���DGڡ���Q-�M���>d���O����}�\�8g_Ш�oYr
Expand Down
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
x��M
�0F]�� e���D�z�q��@�@� oo� ����{��Rr��é���� �1��EȀKƀ��<�|���V�~�ƛ�|�L��|ܞ��>I-W�s���@�Ԡ��\"K�Z ���G�w����:�
@@ -0,0 +1,2 @@
x�A
�0�a�9��I� ���p'�1�Ѷ�v\x{c���V�p�vW�g��ǎ0x�[ ]"g�#{rD�Cot ���N �U $��?9-p+1�^��Qx���9O\�C�� �m�'D{m�V��(+���l��,�
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,3 @@
x��Kj1D��)z�UB��-0��uV9����<#������+�W<J���&8�/s��e���ȕKJ����S
�Rv��{��Q��r��Y�QN$H\E����=6�X5���K Fr)�(�dC��Ά�����j�s�}���9�c-�w8�o�\�r��I���:
l}F�W$Ds�ǣ��ٚOW�e�]V8-Ý��"U
@@ -0,0 +1,3 @@
x��Q
�0D��)�ʦ�I<�'�lR+�Fj��Eo��0<x�h���a ���]ș��XUl�PF)�z�4y�,\r 'S��-mI4
�Xh��&��F�}n+\���Y�-p|鷜oU�z;-��a��lt{��?�I�,:�o�R��cHK
Binary file not shown.
@@ -0,0 +1,2 @@
x��A
� E���/G�!�n{�q2��F����t���{�k)�:�M��^x��L����� ����ݚ�̛�W����6��~� �Z�����P��r4�-3��Qku���?�z�'����9�;I
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
x��KJ1Eg5s <*�t> ��e���^7t:�wo܂� �p��@.=�..nD�"�JH�q�D��V1�����t��U�e�ޕ�in���� afu9Fk�c��O�e�׿����*�q��k9����rL��^�"����!�a���y�%_����2f��w3�G_K�
Expand Down
@@ -0,0 +1 @@
x�A@0P��[�!���N�����#����ϋz�]�ld���uE/�D�nD�T$�h��Xϼ���z�p0(=��bhj73�|e�~#[�
Expand Down
@@ -0,0 +1,4 @@
x��A
�0E]���$�� ����f�L#1���
�>�ǃ?��Sc��U`=J�'�Dd�Q�)xF�DG��ު'W�D�¨���0x2L�-Z�#�q��b�m->��
���n�呶ے9=���+h�G7B�3jDu��a�ZuO�-�[�W�cT_�FHn
Binary file not shown.
Binary file not shown.
@@ -0,0 +1 @@
x���B!D=Sh؅�Bb�;�X�G��c�|�/��K�d��z-�FѲDXy) Y�1��������X4��z�.�����r�dv�4Mb�st+Ҍ���������S/�zkuk}�I�\�����q�VOl�m ����Q�ΣC��Pp���1�J�
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
# pack-refs with: peeled
b25fa35b38051e4ae45d4222e795f9df2e43f1d1 refs/tags/test
^e90810b8df3e80c413d903f631643c716887138d
e90810b8df3e80c413d903f631643c716887138d refs/tags/lw
7b4384978d2493e851f9cca7858815fac9b10980 refs/tags/e90810b
^e90810b8df3e80c413d903f631643c716887138d
e90810b8df3e80c413d903f631643c716887138d refs/remotes/origin/test
4a202b346bb0fb0db7eff3cffeb3c70babbd2045 refs/remotes/origin/packed-test
580c2111be43802dab11328176d94c391f1deae9 refs/remotes/origin/master
a4a7dce85cf63874e984719f4fdd239f5145052f refs/remotes/origin/br2
@@ -0,0 +1 @@
e7039e6d0e7dd4d4c1e2e8e5aa5306b2776436ca
@@ -0,0 +1 @@
7252fe2da2c4dd6d85231a150d0485ec46abaa7a
@@ -0,0 +1 @@
a447ba2ca8fffd46dece72f7db6faf324afb8fcd
@@ -0,0 +1 @@
32eab9cb1f450b5fe7ab663462b77d7f4b703344
@@ -0,0 +1 @@
580c2111be43802dab11328176d94c391f1deae9
@@ -0,0 +1 @@
f705abffe7015f2beacf2abe7a36583ebee3487e
@@ -0,0 +1 @@
ref: refs/remotes/origin/master
@@ -0,0 +1 @@
ref: refs/heads/i-do-numbers
@@ -0,0 +1 @@
7252fe2da2c4dd6d85231a150d0485ec46abaa7a
@@ -0,0 +1 @@
../..
@@ -0,0 +1 @@
../../../../worktrees/i-do-numbers/.git
Binary file not shown.
@@ -0,0 +1 @@
7252fe2da2c4dd6d85231a150d0485ec46abaa7a 7252fe2da2c4dd6d85231a150d0485ec46abaa7a Mike Minns <mminns@itofinity.co.uk> 1513714384 +0000 reset: moving to HEAD
@@ -0,0 +1 @@
ref: refs/heads/logo
@@ -0,0 +1 @@
a447ba2ca8fffd46dece72f7db6faf324afb8fcd
@@ -0,0 +1 @@
../..
@@ -0,0 +1 @@
../../../../worktrees/logo/.git
Binary file not shown.
@@ -0,0 +1 @@
Test lock reason
@@ -0,0 +1 @@
a447ba2ca8fffd46dece72f7db6faf324afb8fcd a447ba2ca8fffd46dece72f7db6faf324afb8fcd Mike Minns <mminns@itofinity.co.uk> 1513713776 +0000 reset: moving to HEAD
@@ -0,0 +1,2 @@
a change
more files!
@@ -0,0 +1,2 @@
some more text
more files! more files!
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/Resources/worktree/testrepo_wd/new.txt
@@ -0,0 +1 @@
my new file
@@ -0,0 +1 @@
a new file
@@ -0,0 +1 @@
content
@@ -0,0 +1,17 @@
1
2
3
4
5
6
7
7.2
8
9
10
11
12
13
14
15
16
@@ -0,0 +1,5 @@
That's a terrible name!
I don't like it.
People look down at me and laugh. :-(
Really!!!!
Yeah! Better!
@@ -0,0 +1 @@
hi
@@ -0,0 +1 @@
hey there
@@ -0,0 +1 @@
hi
@@ -0,0 +1 @@
my new file
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Expand Up @@ -43,6 +43,8 @@ static BaseFixture()
private static string SubmoduleTargetTestRepoWorkingDirPath { get; set; }
private static string AssumeUnchangedRepoWorkingDirPath { get; set; }
public static string SubmoduleSmallTestRepoWorkingDirPath { get; set; }
public static string WorktreeTestRepoWorkingDirPath { get; private set; }
public static string WorktreeTestRepoWorktreesDirPath { get; private set; }
public static string PackBuilderTestRepoPath { get; private set; }

public static DirectoryInfo ResourcesDirectory { get; private set; }
Expand Down Expand Up @@ -85,6 +87,8 @@ private static void SetUpTestEnvironment()
AssumeUnchangedRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "assume_unchanged_wd");
SubmoduleSmallTestRepoWorkingDirPath = Path.Combine(ResourcesDirectory.FullName, "submodule_small_wd");
PackBuilderTestRepoPath = Path.Combine(ResourcesDirectory.FullName, "packbuilder_testrepo_wd");
WorktreeTestRepoWorkingDirPath = Path.Combine(sourceRelativePath, "worktree", "testrepo_wd");
WorktreeTestRepoWorktreesDirPath = Path.Combine(sourceRelativePath, "worktree", "worktrees");

CleanupTestReposOlderThan(TimeSpan.FromMinutes(15));
}
Expand Down Expand Up @@ -220,6 +224,11 @@ public string SandboxSubmoduleSmallTestRepo()
return path;
}

public string SandboxWorktreeTestRepo()
{
return Sandbox(WorktreeTestRepoWorkingDirPath, WorktreeTestRepoWorktreesDirPath);
}

protected string SandboxPackBuilderTestRepo()
{
return Sandbox(PackBuilderTestRepoPath);
Expand Down
89 changes: 89 additions & 0 deletions LibGit2Sharp.Tests/WorktreeFixture.cs
@@ -0,0 +1,89 @@
using LibGit2Sharp.Tests.TestHelpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class WorktreeFixture : BaseFixture
{
[Fact]
public void RetrievingWorktreeForRandomNameReturnsNull()
{
var path = SandboxWorktreeTestRepo();
using (var repo = new Repository(path))
{
var worktree = repo.Worktrees["random"];
Assert.Null(worktree);
}
}

[Fact]
public void RetrievingWorktreeForWorktreeNameReturnsWorktree()
{
var path = SandboxWorktreeTestRepo();
using (var repo = new Repository(path))
{
var worktree = repo.Worktrees["logo"];
Assert.NotNull(worktree);
}
}

[Fact]
public void CanEnumerateRepositoryWorktrees()
{
var expectedWorktrees = new[]
{
"i-do-numbers",
"logo",
};

var path = SandboxWorktreeTestRepo();
using (var repo = new Repository(path))
{
var worktrees = repo.Worktrees.OrderBy(w => w.Name, StringComparer.Ordinal);

Assert.Equal(expectedWorktrees, worktrees.Select(w => w.Name).ToArray());
}
}

[Fact]
public void CanViewLockStatusForWorktrees()
{
var testpath = SandboxWorktreeTestRepo();
var repoPath = testpath;
using (var repo = new Repository(repoPath))
{
// locked
var worktreeLogo = repo.Worktrees["logo"];
Assert.Equal("logo", worktreeLogo.Name);
Assert.True(worktreeLogo.IsLocked);
Assert.Equal("Test lock reason\n", worktreeLogo.LockReason);

// not locked
var worktreeIDoNumbers = repo.Worktrees["i-do-numbers"];
Assert.Equal("i-do-numbers", worktreeIDoNumbers.Name);
Assert.False(worktreeIDoNumbers.IsLocked);
Assert.Null(worktreeIDoNumbers.LockReason);
}
}

[Fact]
public void CanGetRepositoryForWorktree()
{
var testpath = SandboxWorktreeTestRepo();
var repoPath = testpath;
using (var repo = new Repository(repoPath))
{
var worktree = repo.Worktrees["logo"];

Assert.Equal("logo", worktree.Name);
var worktreeRepo = worktree.WorktreeRepository;
Assert.NotNull(worktreeRepo);
}
}
}
}
2 changes: 1 addition & 1 deletion LibGit2Sharp.sln
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.10
VisualStudioVersion = 15.0.27009.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LibGit2Sharp", "LibGit2Sharp\LibGit2Sharp.csproj", "{EE6ED99F-CB12-4683-B055-D28FC7357A34}"
EndProject
Expand Down
23 changes: 23 additions & 0 deletions LibGit2Sharp/Core/Handles/Objects.cs
Expand Up @@ -556,4 +556,27 @@ public override void Free()
}
}

internal unsafe class WorktreeHandle : Libgit2Object
{
internal WorktreeHandle(git_worktree *ptr, bool owned)
: base((void *) ptr, owned)
{
}

internal WorktreeHandle(IntPtr ptr, bool owned)
: base(ptr, owned)
{
}

public override void Free()
{
NativeMethods.git_worktree_free((git_worktree*) ptr);
}

public static implicit operator git_worktree*(WorktreeHandle handle)
{
return (git_worktree*) handle.Handle;
}
}

}
2 changes: 2 additions & 0 deletions LibGit2Sharp/Core/Handles/Objects.tt
Expand Up @@ -36,6 +36,7 @@ var cNames = new[] {
"git_object",
"git_rebase",
"git_odb_stream",
"git_worktree",
};

var csNames = new[] {
Expand Down Expand Up @@ -63,6 +64,7 @@ var csNames = new[] {
"ObjectHandle",
"RebaseHandle",
"OdbStreamHandle",
"WorktreeHandle"
};

for (var i = 0; i < cNames.Length; i++)
Expand Down

0 comments on commit c4c15a5

Please sign in to comment.