Skip to content

Commit 1b418ae

Browse files
committed
Obsolete repo.Clone() overload which returns a Repository
1 parent 09a26cf commit 1b418ae

File tree

7 files changed

+96
-73
lines changed

7 files changed

+96
-73
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -804,7 +804,10 @@ public void TrackedBranchExistsFromDefaultConfigInEmptyClone()
804804
}
805805

806806
SelfCleaningDirectory scd2 = BuildSelfCleaningDirectory();
807-
using (Repository repo = Repository.Clone(uri.AbsoluteUri, scd2.RootedDirectoryPath))
807+
808+
string clonedRepoPath = Repository.Clone(uri.AbsoluteUri, scd2.DirectoryPath);
809+
810+
using (var repo = new Repository(clonedRepoPath))
808811
{
809812
Assert.Empty(Directory.GetFiles(scd2.RootedDirectoryPath));
810813
Assert.Equal(repo.Head.Name, "master");

LibGit2Sharp.Tests/CloneFixture.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public class CloneFixture : BaseFixture
1717
public void CanClone(string url)
1818
{
1919
var scd = BuildSelfCleaningDirectory();
20-
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath))
20+
21+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
22+
23+
using (var repo = new Repository(clonedRepoPath))
2124
{
2225
string dir = repo.Info.Path;
2326
Assert.True(Path.IsPathRooted(dir));
@@ -36,7 +39,10 @@ public void CanClone(string url)
3639
private void AssertLocalClone(string path)
3740
{
3841
var scd = BuildSelfCleaningDirectory();
39-
using (Repository clonedRepo = Repository.Clone(path, scd.RootedDirectoryPath))
42+
43+
string clonedRepoPath = Repository.Clone(path, scd.DirectoryPath);
44+
45+
using (var clonedRepo = new Repository(clonedRepoPath))
4046
using (var originalRepo = new Repository(BareTestRepoPath))
4147
{
4248
Assert.NotEqual(originalRepo.Info.Path, clonedRepo.Info.Path);
@@ -71,7 +77,10 @@ public void CanCloneALocalRepositoryFromAStandardPath()
7177
public void CanCloneBarely(string url)
7278
{
7379
var scd = BuildSelfCleaningDirectory();
74-
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath, bare: true))
80+
81+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, bare: true);
82+
83+
using (var repo = new Repository(clonedRepoPath))
7584
{
7685
string dir = repo.Info.Path;
7786
Assert.True(Path.IsPathRooted(dir));
@@ -88,9 +97,12 @@ public void CanCloneBarely(string url)
8897
public void WontCheckoutIfAskedNotTo(string url)
8998
{
9099
var scd = BuildSelfCleaningDirectory();
91-
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath, checkout: false))
100+
101+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath, checkout: false);
102+
103+
using (var repo = new Repository(clonedRepoPath))
92104
{
93-
Assert.False(File.Exists(Path.Combine(scd.RootedDirectoryPath, "master.txt")));
105+
Assert.False(File.Exists(Path.Combine(repo.Info.WorkingDirectory, "master.txt")));
94106
}
95107
}
96108

@@ -102,13 +114,13 @@ public void CallsProgressCallbacks(string url)
102114
bool checkoutWasCalled = false;
103115

104116
var scd = BuildSelfCleaningDirectory();
105-
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath,
106-
onTransferProgress: (_) => { transferWasCalled = true; return 0; },
107-
onCheckoutProgress: (a, b, c) => checkoutWasCalled = true))
108-
{
109-
Assert.True(transferWasCalled);
110-
Assert.True(checkoutWasCalled);
111-
}
117+
118+
Repository.Clone(url, scd.DirectoryPath,
119+
onTransferProgress: _ => { transferWasCalled = true; return 0; },
120+
onCheckoutProgress: (a, b, c) => checkoutWasCalled = true);
121+
122+
Assert.True(transferWasCalled);
123+
Assert.True(checkoutWasCalled);
112124
}
113125

114126
[SkippableFact]
@@ -118,13 +130,16 @@ public void CanCloneWithCredentials()
118130
"Populate Constants.PrivateRepo* to run this test");
119131

120132
var scd = BuildSelfCleaningDirectory();
121-
using (Repository repo = Repository.Clone(
122-
Constants.PrivateRepoUrl, scd.RootedDirectoryPath,
133+
134+
string clonedRepoPath = Repository.Clone(Constants.PrivateRepoUrl, scd.DirectoryPath,
123135
credentials: new Credentials
124136
{
125137
Username = Constants.PrivateRepoUsername,
126138
Password = Constants.PrivateRepoPassword
127-
}))
139+
});
140+
141+
142+
using (var repo = new Repository(clonedRepoPath))
128143
{
129144
string dir = repo.Info.Path;
130145
Assert.True(Path.IsPathRooted(dir));

LibGit2Sharp.Tests/FetchHeadFixture.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ private class FetchHeadExpected
3434
public void CanIterateFetchHead(string url)
3535
{
3636
var scd = BuildSelfCleaningDirectory();
37-
using (var repo = Repository.Clone(url, scd.RootedDirectoryPath))
37+
38+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
39+
40+
using (var repo = new Repository(clonedRepoPath))
3841
{
3942
repo.Reset(ResetOptions.Hard, "HEAD~2");
4043

LibGit2Sharp.Tests/NetworkFixture.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ public void CanListRemoteReferences(string url)
3939
[Fact]
4040
public void CanListRemoteReferenceObjects()
4141
{
42-
string url = "http://github.com/libgit2/TestGitRepository";
43-
string remoteName = "origin";
42+
const string url = "http://github.com/libgit2/TestGitRepository";
43+
const string remoteName = "origin";
4444

4545
var scd = BuildSelfCleaningDirectory();
46-
using (Repository repo = Repository.Clone(url, scd.RootedDirectoryPath))
46+
47+
string clonedRepoPath = Repository.Clone(url, scd.DirectoryPath);
48+
49+
using (var repo = new Repository(clonedRepoPath))
4750
{
4851
Remote remote = repo.Network.Remotes[remoteName];
4952
IEnumerable<DirectReference> references = repo.Network.ListReferences(remote);
5053

51-
List<Tuple<string, string>> actualRefs = new List<Tuple<string,string>>();
54+
var actualRefs = new List<Tuple<string,string>>();
5255

5356
foreach(DirectReference reference in references)
5457
{

LibGit2Sharp.Tests/PushFixture.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ private void OnPushStatusError(PushStatusError pushStatusErrors)
1717
private void AssertPush(Action<Repository> push)
1818
{
1919
var scd = BuildSelfCleaningDirectory();
20-
using (var originalRepo = new Repository(CloneBareTestRepo()))
21-
using (Repository clonedRepo = Repository.Clone(originalRepo.Info.Path, scd.RootedDirectoryPath))
20+
21+
string originalRepoPath = CloneBareTestRepo();
22+
string clonedRepoPath = Repository.Clone(originalRepoPath, scd.DirectoryPath);
23+
24+
using (var originalRepo = new Repository(originalRepoPath))
25+
using (var clonedRepo = new Repository(clonedRepoPath))
2226
{
2327
Remote remote = clonedRepo.Network.Remotes["origin"];
2428

LibGit2Sharp.Tests/RepositoryOptionsFixture.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -174,40 +174,5 @@ public void CanProvideDifferentConfigurationFilesToARepository()
174174

175175
AssertValueInConfigFile(systemLocation, "xpaulbettsx");
176176
}
177-
178-
[Fact]
179-
public void CanProvideDifferentWorkingDirOnClone()
180-
{
181-
string url = "https://github.com/libgit2/TestGitRepository";
182-
var scd = BuildSelfCleaningDirectory();
183-
var options = new RepositoryOptions { WorkingDirectoryPath = newWorkdir };
184-
185-
using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
186-
{
187-
Assert.Equal(Path.GetFullPath(newWorkdir) + Path.DirectorySeparatorChar, repo.Info.WorkingDirectory);
188-
}
189-
}
190-
191-
[Fact]
192-
public void CanProvideDifferentConfigurationFilesOnClone()
193-
{
194-
string url = "https://github.com/libgit2/TestGitRepository";
195-
var scd = BuildSelfCleaningDirectory();
196-
var configScd = BuildSelfCleaningDirectory();
197-
var options = BuildFakeConfigs(configScd);
198-
199-
using (var repo = Repository.Clone(url, scd.DirectoryPath, false, true, null, null, options))
200-
{
201-
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global));
202-
Assert.Equal("global", repo.Config.Get<string>("woot.this-rocks").Value);
203-
Assert.Equal(42, repo.Config.Get<int>("wow.man-I-am-totally-global").Value);
204-
205-
Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg));
206-
Assert.Equal("xdg", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.Xdg).Value);
207-
208-
Assert.True(repo.Config.HasConfig(ConfigurationLevel.System));
209-
Assert.Equal("system", repo.Config.Get<string>("woot.this-rocks", ConfigurationLevel.System).Value);
210-
}
211-
}
212177
}
213178
}

LibGit2Sharp/Repository.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -569,31 +569,57 @@ public static string Discover(string startingPath)
569569
/// <param name="onCheckoutProgress">Handler for checkout progress information</param>
570570
/// <param name="options">Overrides to the way a repository is opened.</param>
571571
/// <param name="credentials">Credentials to use for user/pass authentication</param>
572-
/// <returns></returns>
572+
/// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref = "Dispose()" /> on this instance.</returns>
573+
[Obsolete("This method will be removed in the next release. Please use Clone(string, string, bool, bool, TransferProgressHandler, CheckoutProgressHandler, Credentials) instead.")]
573574
public static Repository Clone(string sourceUrl, string workdirPath,
575+
bool bare,
576+
bool checkout,
577+
TransferProgressHandler onTransferProgress,
578+
CheckoutProgressHandler onCheckoutProgress,
579+
RepositoryOptions options,
580+
Credentials credentials)
581+
{
582+
string gitDirPath = Clone(sourceUrl, workdirPath, bare,
583+
checkout, onTransferProgress, onCheckoutProgress, credentials);
584+
585+
return new Repository(gitDirPath, options);
586+
}
587+
588+
/// <summary>
589+
/// Clone with specified options.
590+
/// </summary>
591+
/// <param name="sourceUrl">URI for the remote repository</param>
592+
/// <param name="workdirPath">Local path to clone into</param>
593+
/// <param name="bare">True will result in a bare clone, false a full clone.</param>
594+
/// <param name="checkout">If true, the origin's HEAD will be checked out. This only applies
595+
/// to non-bare repositories.</param>
596+
/// <param name="onTransferProgress">Handler for network transfer and indexing progress information</param>
597+
/// <param name="onCheckoutProgress">Handler for checkout progress information</param>
598+
/// <param name="credentials">Credentials to use for user/pass authentication</param>
599+
/// <returns>The path to the created repository.</returns>
600+
public static string Clone(string sourceUrl, string workdirPath,
574601
bool bare = false,
575602
bool checkout = true,
576603
TransferProgressHandler onTransferProgress = null,
577604
CheckoutProgressHandler onCheckoutProgress = null,
578-
RepositoryOptions options = null,
579605
Credentials credentials = null)
580606
{
581607
CheckoutCallbacks checkoutCallbacks = CheckoutCallbacks.GenerateCheckoutCallbacks(onCheckoutProgress, null);
582608

583609
var cloneOpts = new GitCloneOptions
610+
{
611+
Bare = bare ? 1 : 0,
612+
TransferProgressCallback = TransferCallbacks.GenerateCallback(onTransferProgress),
613+
CheckoutOpts =
584614
{
585-
Bare = bare ? 1 : 0,
586-
TransferProgressCallback = TransferCallbacks.GenerateCallback(onTransferProgress),
587-
CheckoutOpts =
588-
{
589-
version = 1,
590-
progress_cb =
615+
version = 1,
616+
progress_cb =
591617
checkoutCallbacks.CheckoutProgressCallback,
592-
checkout_strategy = checkout
593-
? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
594-
: CheckoutStrategy.GIT_CHECKOUT_NONE
595-
},
596-
};
618+
checkout_strategy = checkout
619+
? CheckoutStrategy.GIT_CHECKOUT_SAFE_CREATE
620+
: CheckoutStrategy.GIT_CHECKOUT_NONE
621+
},
622+
};
597623

598624
if (credentials != null)
599625
{
@@ -602,13 +628,17 @@ public static Repository Clone(string sourceUrl, string workdirPath,
602628
NativeMethods.git_cred_userpass_plaintext_new(out cred, credentials.Username, credentials.Password);
603629
}
604630

605-
using(Proxy.git_clone(sourceUrl, workdirPath, cloneOpts)) {}
631+
FilePath repoPath;
632+
using (RepositorySafeHandle repo = Proxy.git_clone(sourceUrl, workdirPath, cloneOpts))
633+
{
634+
repoPath = Proxy.git_repository_path(repo);
635+
}
606636

607637
// To be safe, make sure the credential callback is kept until
608638
// alive until at least this point.
609639
GC.KeepAlive(cloneOpts.CredAcquireCallback);
610640

611-
return new Repository(workdirPath, options);
641+
return repoPath.Native;
612642
}
613643

614644
/// <summary>

0 commit comments

Comments
 (0)