Skip to content

Commit b452a84

Browse files
committed
Eagerly dispose of Remotes
This is a continuation of #1249. Since Remotes are now disposable we should eagerly clean up their resources rather than wait for GC to call the destructor.
1 parent 45846e2 commit b452a84

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

LibGit2Sharp/Branch.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ public virtual string UpstreamBranchCanonicalName
146146
{
147147
if (IsRemote)
148148
{
149-
return Remote.FetchSpecTransformToSource(CanonicalName);
149+
using (var remote = Remote)
150+
{
151+
return remote.FetchSpecTransformToSource(CanonicalName);
152+
}
150153
}
151154

152155
return UpstreamBranchCanonicalNameFromLocalBranch();

LibGit2Sharp/BranchUpdater.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private void SetUpstreamRemote(string remoteName)
154154
if (!remoteName.Equals(".", StringComparison.Ordinal))
155155
{
156156
// Verify that remote exists.
157-
repo.Network.Remotes.RemoteForName(remoteName);
157+
using (repo.Network.Remotes.RemoteForName(remoteName)) { }
158158
}
159159

160160
repo.Config.Set(configKey, remoteName);
@@ -183,8 +183,10 @@ private void GetUpstreamInformation(string canonicalName, out string remoteName,
183183
{
184184
remoteName = Proxy.git_branch_remote_name(repo.Handle, canonicalName, true);
185185

186-
Remote remote = repo.Network.Remotes.RemoteForName(remoteName);
187-
mergeBranchName = remote.FetchSpecTransformToSource(canonicalName);
186+
using (var remote = repo.Network.Remotes.RemoteForName(remoteName))
187+
{
188+
mergeBranchName = remote.FetchSpecTransformToSource(canonicalName);
189+
}
188190
}
189191
else
190192
{

LibGit2Sharp/Network.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,12 @@ public virtual void Push(
415415

416416
foreach (var branch in enumeratedBranches)
417417
{
418-
Push(branch.Remote, string.Format(
419-
CultureInfo.InvariantCulture,
420-
"{0}:{1}", branch.CanonicalName, branch.UpstreamBranchCanonicalName), pushOptions);
418+
using (var remote = branch.Remote)
419+
{
420+
Push(remote, string.Format(
421+
CultureInfo.InvariantCulture,
422+
"{0}:{1}", branch.CanonicalName, branch.UpstreamBranchCanonicalName), pushOptions);
423+
}
421424
}
422425
}
423426

@@ -558,12 +561,16 @@ public virtual MergeResult Pull(Signature merger, PullOptions options)
558561
throw new LibGit2SharpException("There is no tracking information for the current branch.");
559562
}
560563

561-
if (currentBranch.Remote == null)
564+
using (var remote = currentBranch.Remote)
562565
{
563-
throw new LibGit2SharpException("No upstream remote for the current branch.");
566+
if (remote == null)
567+
{
568+
throw new LibGit2SharpException("No upstream remote for the current branch.");
569+
}
570+
571+
Fetch(remote, options.FetchOptions);
564572
}
565573

566-
Fetch(currentBranch.Remote, options.FetchOptions);
567574
return repository.MergeFetchedRefs(merger, options.MergeOptions);
568575
}
569576

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ public static void Fetch(this IRepository repository, string remoteName, FetchOp
221221
Ensure.ArgumentNotNull(repository, "repository");
222222
Ensure.ArgumentNotNullOrEmptyString(remoteName, "remoteName");
223223

224-
Remote remote = repository.Network.Remotes.RemoteForName(remoteName, true);
225-
repository.Network.Fetch(remote, options);
224+
using (var remote = repository.Network.Remotes.RemoteForName(remoteName, true))
225+
{
226+
repository.Network.Fetch(remote, options);
227+
}
226228
}
227229

228230
/// <summary>

0 commit comments

Comments
 (0)