8000 Fixup: Code Review - use more usings · libgit2/libgit2sharp@368a0a8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 368a0a8

Browse files
committed
Fixup: Code Review - use more usings
1 parent 1940bac commit 368a0a8

File tree

4 files changed

+65
-120
lines changed

4 files changed

+65
-120
lines changed

LibGit2Sharp/Core/Proxy.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Runtime.InteropServices;
88
using System.Threading;
9+
using LibGit2Sharp;
910
using LibGit2Sharp.Core.Handles;
1011
using LibGit2Sharp.Handlers;
1112

@@ -1740,17 +1741,11 @@ public static GitRebaseCommitResult git_rebase_commit(
17401741

17411742
GitRebaseCommitResult commitResult = new GitRebaseCommitResult();
17421743

1743-
using (ThreadAffinity())
1744+
using (SignatureSafeHandle committerHandle = committer.BuildHandle())
1745+
using (SignatureSafeHandle authorHandle = author.SafeBuildHandle())
17441746
{
1745-
SignatureSafeHandle authorHandle = null;
1746-
SignatureSafeHandle committerHandle = null;
1747-
1748-
try
1747+
using (ThreadAffinity())
17491748
{
1750-
committerHandle = committer.BuildHandle();
1751-
authorHandle = author == null ?
1752-
new SignatureSafeHandle() : author.BuildHandle();
1753-
17541749
int result = NativeMethods.git_rebase_commit(ref commitResult.CommitId, rebase, authorHandle, committerHandle, IntPtr.Zero, IntPtr.Zero);
17551750

17561751
if (result == (int)GitErrorCode.Applied)
@@ -1763,14 +1758,6 @@ public static GitRebaseCommitResult git_rebase_commit(
17631758
Ensure.ZeroResult(result);
17641759
}
17651760
}
1766-
finally
1767-
{
1768-
committerHandle.Dispose();
1769-
committerHandle = null;
1770-
1771-
authorHandle.SafeDispose();
1772-
authorHandle = null;
1773-
}
17741761
}
17751762

17761763
return commitResult;

LibGit2Sharp/Rebase.cs

Lines changed: 36 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -85,77 +85,44 @@ public virtual RebaseResult Start(Branch branch, Branch upstream, Branch onto, S
8585
"A {0} operation is already in progress.", this.repository.Info.CurrentOperation));
8686
}
8787

88-
ReferenceSafeHandle branchRefPtr = null;
89-
ReferenceSafeHandle upstreamRefPtr = null;
90-
ReferenceSafeHandle ontoRefPtr = null;
91-
92-
GitAnnotatedCommitHandle annotatedBranchCommitHandle = null;
93-
GitAnnotatedCommitHandle annotatedUpstreamRefPtrCommitHandle = null;
94-
GitAnnotatedCommitHandle annotatedOntoRefPtrCommitHandle = null;
95-
96-
RebaseSafeHandle rebaseOperationHandle = null;
97-
98-
try
88+
Func<Branch, ReferenceSafeHandle> RefHandleFromBranch = (Branch b) =>
9989
{
100-
branchRefPtr = (branch == null) ?
101-
this.repository.Refs.RetrieveReferencePtr(this.repository.Head.CanonicalName) :
102-
this.repository.Refs.RetrieveReferencePtr(branch.CanonicalName);
103-
104-
upstreamRefPtr = (upstream == null) ?
105-
null : this.repository.Refs.RetrieveReferencePtr(upstream.CanonicalName);
106-
107-
ontoRefPtr = (onto == null) ?
108-
null : this.repository.Refs.RetrieveReferencePtr(onto.CanonicalName);
109-
110-
annotatedBranchCommitHandle = (branchRefPtr == null) ?
111-
new GitAnnotatedCommitHandle() :
112-
Proxy.git_annotated_commit_from_ref(this.repository.Handle, branchRefPtr);
90+
return (b == null) ?
91+
null :
92+
this.repository.Refs.RetrieveReferencePtr(b.CanonicalName);
93+
};
11394

114-
annotatedUpstreamRefPtrCommitHandle = (upstreamRefPtr == null) ?
115-
new GitAnnotatedCommitHandle() :
116-
Proxy.git_annotated_commit_from_ref(this.repository.Handle, upstreamRefPtr);
117-
118-
annotatedOntoRefPtrCommitHandle = (ontoRefPtr == null) ?
95+
Func<ReferenceSafeHandle, GitAnnotatedCommitHandle> AnnotatedCommitHandleFromRefHandle =
96+
(ReferenceSafeHandle refHandle) =>
97+
{
98+
return (refHandle == null) ?
11999
new GitAnnotatedCommitHandle() :
120-
Proxy.git_annotated_commit_from_ref(this.repository.Handle, ontoRefPtr);
121-
122-
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
123-
{
124-
version = 1,
125-
};
126-
127-
rebaseOperationHandle = Proxy.git_rebase_init(this.repository.Handle,
128-
annotatedBranchCommitHandle,
129-
annotatedUpstreamRefPtrCommitHandle,
130-
annotatedOntoRefPtrCommitHandle,
131-
ref gitRebaseOptions);
100+
Proxy.git_annotated_commit_from_ref(this.repository.Handle, refHandle);
101+
};
132102

133-
RebaseResult rebaseResult =
134-
RebaseOperationImpl.Run(rebaseOperationHandle,
135-
this.repository,
136-
committer,
137-
options,
138-
true);
139-
return rebaseResult;
140-
}
141-
finally
103+
GitRebaseOptions gitRebaseOptions = new GitRebaseOptions()
142104
{
143-
branchRefPtr.SafeDispose();
144-
branchRefPtr = null;
145-
upstreamRefPtr.SafeDispose();
146-
upstreamRefPtr = null;
147-
ontoRefPtr.SafeDispose();
148-
ontoRefPtr = null;
149-
150-
annotatedBranchCommitHandle.SafeDispose();
151-
annotatedBranchCommitHandle = null;
152-
annotatedUpstreamRefPtrCommitHandle.SafeDispose();
153-
annotatedUpstreamRefPtrCommitHandle = null;
154-
annotatedOntoRefPtrCommitHandle.SafeDispose();
155-
annotatedOntoRefPtrCommitHandle = null;
156-
157-
rebaseOperationHandle.SafeDispose();
158-
rebaseOperationHandle = null;
105+
version = 1,
106+
};
107+
108+
using (ReferenceSafeHandle branchRefPtr = RefHandleFromBranch(branch))
109+
using (ReferenceSafeHandle upstreamRefPtr = RefHandleFromBranch(upstream))
110+
using (ReferenceSafeHandle ontoRefPtr = RefHandleFromBranch(onto))
111+
using (GitAnnotatedCommitHandle annotatedBranchCommitHandle = AnnotatedCommitHandleFromRefHandle(branchRefPtr))
112+
using (GitAnnotatedCommitHandle upstreamRefAnnotatedCommitHandle = AnnotatedCommitHandleFromRefHandle(upstreamRefPtr))
113+
using (GitAnnotatedCommitHandle ontoRefAnnotatedCommitHandle = AnnotatedCommitHandleFromRefHandle(ontoRefPtr))
114+
using (RebaseSafeHandle rebaseOperationHandle = Proxy.git_rebase_init(this.repository.Handle,
115+
annotatedBranchCommitHandle,
116+
upstreamRefAnnotatedCommitHandle,
117+
ontoRefAnnotatedCommitHandle,
118+
ref gitRebaseOptions))
119+
{
120+
RebaseResult rebaseResult = RebaseOperationImpl.Run(rebaseOperationHandle,
121+
this.repository,
122+
committer,
123+
options,
124+
true);
125+
return rebaseResult;
159126
}
160127
}
161128

@@ -170,10 +137,8 @@ public virtual RebaseResult Continue(Signature committer, RebaseOptions options)
170137

171138
options = options ?? new RebaseOptions();
172139

173-
RebaseSafeHandle rebase = null;
174-
try
140+
using (RebaseSafeHandle rebase = Proxy.git_rebase_open(repository.Handle))
175141
{
176-
rebase = Proxy.git_rebase_open(repository.Handle);
177142
var rebaseCommitResult = Proxy.git_rebase_commit(rebase, null, committer);
178143

179144
// Report that we just completed the step
@@ -203,29 +168,17 @@ public virtual RebaseResult Continue(Signature committer, RebaseOptions options)
203168
RebaseResult rebaseResult = RebaseOperationImpl.Run(rebase, repository, committer, options, false);
204169
return rebaseResult;
205170
}
206-
finally
207-
{
208-
rebase.SafeDispose();
209-
rebase = null;
210-
}
211171
}
212172

213173
/// <summary>
214174
/// Abort the rebase operation.
215175
/// </summary>
216176
public virtual void Abort()
217177
{
218-
RebaseSafeHandle rebase = null;
219-
try
178+
using (RebaseSafeHandle rebase = Proxy.git_rebase_open(repository.Handle))
220179
{
221-
rebase = Proxy.git_rebase_open(repository.Handle);
222180
Proxy.git_rebase_abort(rebase);
223181
}
224-
finally
225-
{
226-
rebase.SafeDispose();
227-
rebase = null;
228-
}
229182
}
230183

231184
/// <summary>
@@ -246,11 +199,8 @@ public virtual RebaseStepInfo GetCurrentStepInfo()
246199
return null;
247200
}
248201

249-
RebaseSafeHandle rebaseHandle = null;
250-
251-
try
202+
using (RebaseSafeHandle rebaseHandle = Proxy.git_rebase_open(repository.Handle))
252203
{
253-
rebaseHandle = Proxy.git_rebase_open(repository.Handle);
254204
long currentStepIndex = Proxy.git_rebase_operation_current(rebaseHandle);
255205
long totalStepCount = Proxy.git_rebase_operation_entrycount(rebaseHandle);
256206
GitRebaseOperation gitRebasestepInfo = Proxy.git_rebase_operation_byindex(rebaseHandle, currentStepIndex);
@@ -261,11 +211,6 @@ public virtual RebaseStepInfo GetCurrentStepInfo()
261211
totalStepCount);
262212
return stepInfo;
263213
}
264-
finally
265-
{
266-
rebaseHandle.SafeDispose();
267-
rebaseHandle = null;
268-
}
269214
}
270215
}
271216
}

LibGit2Sharp/RebaseOperationImpl.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ public static RebaseResult Run(RebaseSafeHandle rebaseOperationHandle,
3131
Ensure.ArgumentNotNull(committer, "committer");
3232
Ensure.ArgumentNotNull(options, "options");
3333

34-
GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options);
35-
GitCheckoutOpts gitCheckoutOpts = checkoutOptionsWrapper.Options;
36-
RebaseResult rebaseResult = null;
37-
38-
try
34+
using (GitCheckoutOptsWrapper checkoutOptionsWrapper = new GitCheckoutOptsWrapper(options))
3935
{
36+
GitCheckoutOpts gitCheckoutOpts = checkoutOptionsWrapper.Options;
37+
RebaseResult rebaseResult = null;
38+
4039
// stepBeingApplied indicates the step that will be applied by by git_rebase_next.
4140
// The current step does not get incremented until git_rebase_next (except on
4241
// the initial step), but we want to report the step that will be applied.
@@ -159,14 +158,9 @@ public static RebaseResult Run(RebaseSafeHandle rebaseOperationHandle,
159158
totalStepCount,
160159
null);
161160
}
162-
}
163-
finally
164-
{
165-
checkoutOptionsWrapper.SafeDispose();
166-
checkoutOptionsWrapper = null;
167-
}
168161

169-
return rebaseResult;
162+
return rebaseResult;
163+
}
170164
}
171165
}
172166
}

LibGit2Sharp/Signature.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,23 @@ public override string ToString()
147147
return string.Format(CultureInfo.InvariantCulture, "{0} <{1}>", Name, Email);
148148
}
149149
}
150+
151+
internal static class SignatureHelpers
152+
{
153+
/// <summary>
154+
/// Build the handle for the Signature, or return a handle
155+
/// to an empty signature.
156+
/// </summary>
157+
/// <param name="signature"></param>
158+
/// <returns></returns>
159+
public static SignatureSafeHandle SafeBuildHandle(this Signature signature)
160+
{
161+
if (signature == null)
162+
{
163+
return new SignatureSafeHandle();
164+
}
165+
166+
return signature.BuildHandle();
167+
}
168+
}
150169
}

0 commit comments

Comments
 (0)
0