8000 Get rid of Configuration{,Iterator}SafeHandle · vrkcse2011/libgit2sharp@4b8b026 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4b8b026

Browse files
committed
Get rid of Configuration{,Iterator}SafeHandle
1 parent 1d5b5f5 commit 4b8b026

9 files changed

+145
-103
lines changed

LibGit2Sharp/Configuration.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Configuration : IDisposable,
2020
private readonly FilePath xdgConfigPath;
2121
private readonly FilePath systemConfigPath;
2222

23-
private ConfigurationSafeHandle configHandle;
23+
private ConfigurationHandle configHandle;
2424< 6855 code class="diff-text syntax-highlighted-line">

2525
/// <summary>
2626
/// Needed for mocking purposes.
@@ -230,8 +230,8 @@ public Configuration(string globalConfigurationFileLocation, string xdgConfigura
230230
/// </summary>
231231
public virtual bool HasConfig(ConfigurationLevel level)
232232
{
233-
using (ConfigurationSafeHandle snapshot = Snapshot())
234-
using (ConfigurationSafeHandle handle = RetrieveConfigurationHandle(level, false, snapshot))
233+
using (ConfigurationHandle snapshot = Snapshot())
234+
using (ConfigurationHandle handle = RetrieveConfigurationHandle(level, false, snapshot))
235235
{
236236
return handle != null;
237237
}
@@ -269,7 +269,7 @@ public virtual void Unset(string key, ConfigurationLevel level)
269269
{
270270
Ensure.ArgumentNotNullOrEmptyString(key, "key");
271271

272-
using (ConfigurationSafeHandle h = RetrieveConfigurationHandle(level, true, configHandle))
272+
using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle))
273273
{
274274
Proxy.git_config_delete(h, key);
275275
}
@@ -279,7 +279,7 @@ internal void UnsetMultivar(string key, ConfigurationLevel level)
279279
{
280280
Ensure.ArgumentNotNullOrEmptyString(key, "key");
281281

282-
using (ConfigurationSafeHandle h = RetrieveConfigurationHandle(level, true, configHandle))
282+
using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle))
283283
{
284284
Proxy.git_config_delete_multivar(h, key);
285285
}
@@ -384,7 +384,7 @@ public virtual ConfigurationEntry<T> Get<T>(string key)
384384
{
385385
Ensure.ArgumentNotNullOrEmptyString(key, "key");
386386

387-
using (ConfigurationSafeHandle snapshot = Snapshot())
387+
using (ConfigurationHandle snapshot = Snapshot())
388388
{
389389
return Proxy.git_config_get_entry<T>(snapshot, key);
390390
}
@@ -415,8 +415,8 @@ public virtual ConfigurationEntry<T> Get<T>(string key, ConfigurationLevel level
415415
{
416416
Ensure.ArgumentNotNullOrEmptyString(key, "key");
417417

418-
using (ConfigurationSafeHandle snapshot = Snapshot())
419-
using (ConfigurationSafeHandle handle = RetrieveConfigurationHandle(level, false, snapshot))
418+
using (ConfigurationHandle snapshot = Snapshot())
419+
using (ConfigurationHandle handle = RetrieveConfigurationHandle(level, false, snapshot))
420420
{
421421
if (handle == null)
422422
{
@@ -645,7 +645,7 @@ public virtual void Set<T>(string key, T value, ConfigurationLevel level)
645645
Ensure.ArgumentNotNull(value, "value");
646646
Ensure.ArgumentNotNullOrEmptyString(key, "key");
647647

648-
using (ConfigurationSafeHandle h = RetrieveConfigurationHandle(level, true, configHandle))
648+
using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, configHandle))
649649
{
650650
if (!configurationTypedUpdater.ContainsKey(typeof(T)))
651651
{
@@ -676,16 +676,16 @@ public virtual IEnumerable<ConfigurationEntry<string>> Find(string regexp, Confi
676676
{
677677
Ensure.ArgumentNotNullOrEmptyString(regexp, "regexp");
678678

679-
using (ConfigurationSafeHandle snapshot = Snapshot())
680-
using (ConfigurationSafeHandle h = RetrieveConfigurationHandle(level, true, snapshot))
679+
using (ConfigurationHandle snapshot = Snapshot())
680+
using (ConfigurationHandle h = RetrieveConfigurationHandle(level, true, snapshot))
681681
{
682682
return Proxy.git_config_iterator_glob(h, regexp).ToList();
683683
}
684684
}
685685

686-
private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel level, bool throwIfStoreHasNotBeenFound, ConfigurationSafeHandle fromHandle)
686+
private ConfigurationHandle RetrieveConfigurationHandle(ConfigurationLevel level, bool throwIfStoreHasNotBeenFound, ConfigurationHandle fromHandle)
687687
{
688-
ConfigurationSafeHandle handle = null;
688+
ConfigurationHandle handle = null;
689689
if (fromHandle != null)
690690
{
691691
handle = Proxy.git_config_open_level(fromHandle, level);
@@ -700,12 +700,12 @@ private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel l
700700
return handle;
701701
}
702702

703-
private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Action<ConfigurationSafeHandle, string, T> setter)
703+
private static Action<string, object, ConfigurationHandle> GetUpdater<T>(Action<ConfigurationHandle, string, T> setter)
704704
{
705705
return (key, val, handle) => setter(handle, key, (T)val);
706706
}
707707

708-
private readonly static IDictionary<Type, Action<string, object, ConfigurationSafeHandle>> configurationTypedUpdater = new Dictionary<Type, Action<string, object, ConfigurationSafeHandle>>
708+
private readonly static IDictionary<Type, Action<string, object, ConfigurationHandle>> configurationTypedUpdater = new Dictionary<Type, Action<string, object, ConfigurationHandle>>
709709
{
710710
{ typeof(int), GetUpdater<int>(Proxy.git_config_set_int32) },
711711
{ typeof(long), GetUpdater<long>(Proxy.git_config_set_int64) },
@@ -778,7 +778,7 @@ internal Signature BuildSignatureOrThrow(DateTimeOffset now)
778778
return signature;
779779
}
780780

781-
private ConfigurationSafeHandle Snapshot()
781+
private ConfigurationHandle Snapshot()
782782
{
783783
return Proxy.git_config_snapshot(configHandle);
784784
}

LibGit2Sharp/Core/Handles/ConfigurationIteratorSafeHandle.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

LibGit2Sharp/Core/Handles/ConfigurationSafeHandle.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

LibGit2Sharp/Core/Handles/Objects.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,4 +556,73 @@ public void Dispose()
556556
}
557557
}
558558

559+
internal unsafe class ConfigurationHandle : IDisposable
560+
{
561+
git_config* ptr;
562+
internal git_config* Handle
563+
{
564+
get
565+
{
566+
return ptr;
567+
}
568+
}
569+
570+
bool owned;
571+
bool disposed;
572+
573+
public unsafe ConfigurationHandle(git_config* handle, bool owned)
574+
{
575+
this.ptr = handle;
576+
this.owned = owned;
577+
}
578+
579+
public unsafe ConfigurationHandle(IntPtr ptr, bool owned)
580+
{
581+
this.ptr = (git_config*) ptr.ToPointer();
582+
this.owned = owned;
583+
}
584+
585+
~ConfigurationHandle()
586+
{
587+
Dispose(false);
588+
}
589+
590+
internal bool IsNull
591+
{
592+
get
593+
{
594+
return ptr == null;
595+
}
596+
}
597+
598+
internal IntPtr AsIntPtr()
599+
{
600+
return new IntPtr(ptr);
601+
}
602+
603+
void Dispose(bool disposing)
604+
{
605+
if (!disposed)
606+
{
607+
if (owned)
608+
{
609+
NativeMethods.git_config_free(ptr);
610+
ptr = null;
611+
}
612+
}
613+
614+
disposed = true;
615+
}
616+
617+
public void Dispose()
618+
{
619+
Dispose(true);
620+
}
621+
622+
public static implicit operator git_config*(ConfigurationHandle handle)
623+
{
624+
return handle.Handle;
625+
}
626+
}
627+
559628
}

LibGit2Sharp/Core/Handles/Objects.tt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var cNames = new[] {
2020
"git_blame",
2121
"git_diff",
2222
"git_patch",
23+
"git_config",
2324
};
2425

2526
var csNames = new[] {
@@ -30,7 +31,8 @@ var csNames = new[] {
3031
"StatusListHandle",
3132
"BlameHandle",
3233
"DiffHandle",
33-
"PatchHandle"
34+
"PatchHandle",
35+
"ConfigurationHandle",
3436
};
3537

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

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,13 @@ internal static extern unsafe int git_commit_create_from_ids(
325325
internal static extern unsafe git_oid* git_commit_tree_id(GitObjectSafeHandle commit);
326326

327327
[DllImport(libgit2)]
328-
internal static extern int git_config_delete_entry(
329-
ConfigurationSafeHandle cfg,
328+
internal static extern unsafe int git_config_delete_entry(
329+
git_config* cfg,
330330
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
331331

332332
[DllImport(libgit2)]
333-
internal static extern int git_config_delete_multivar(
334-
ConfigurationSafeHandle cfg,
333+
internal static extern unsafe int git_config_delete_multivar(
334+
git_config* cfg,
335335
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
336336
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string regexp);
337337

@@ -345,31 +345,31 @@ internal static extern int git_config_delete_multivar(
345345
internal static extern int git_config_find_xdg(GitBuf xdg_config_path);
346346

347347
[DllImport(libgit2)]
348-
internal static extern void git_config_free(IntPtr cfg);
348+
internal static extern unsafe void git_config_free(git_config* cfg);
349349

350350
[DllImport(libgit2)]
351351
internal static extern unsafe void git_config_entry_free(GitConfigEntry* entry);
352352

353353
[DllImport(libgit2)]
354354
internal static extern unsafe int git_config_get_entry(
355355
out GitConfigEntry* entry,
356-
ConfigurationSafeHandle cfg,
356+
git_config* cfg,
357357
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
358358

359359
[DllImport(libgit2)]
360-
internal static extern< 10000 /span> int git_config_add_file_ondisk(
361-
ConfigurationSafeHandle cfg,
360+
internal static extern unsafe int git_config_add_file_ondisk(
361+
git_config* cfg,
362362
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path,
363363
uint level,
364364
[MarshalAs(UnmanagedType.Bool)] bool force);
365365

366366
[DllImport(libgit2)]
367-
internal static extern int git_config_new(out ConfigurationSafeHandle cfg);
367+
internal static extern unsafe int git_config_new(out git_config* cfg);
368368

369369
[DllImport(libgit2)]
370-
internal static extern int git_config_open_level(
371-
out ConfigurationSafeHandle cfg,
372-
ConfigurationSafeHandle parent,
370+
internal static extern unsafe int git_config_open_level(
371+
out git_config* cfg,
372+
git_config* parent,
373373
uint level);
374374

375375
[DllImport(libgit2)]
@@ -388,26 +388,26 @@ internal static extern int git_config_parse_int64(
388388
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string valueToParse);
389389

390390
[DllImport(libgit2)]
391-
internal static extern int git_config_set_bool(
392-
ConfigurationSafeHandle cfg,
391+
internal static extern unsafe int git_config_set_bool(
392+
git_config* cfg,
393393
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
394394
[MarshalAs(UnmanagedType.Bool)] bool value);
395395

396396
[DllImport(libgit2)]
397-
internal static extern int git_config_set_int32(
398-
ConfigurationSafeHandle cfg,
397+
internal static extern unsafe int git_config_set_int32(
398+
git_config* cfg,
399399
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
400400
int value);
401401

402402
[DllImport(libgit2)]
403-
internal static extern int git_config_set_int64(
404-
ConfigurationSafeHandle cfg,
403+
internal static extern unsafe int git_config_set_int64(
404+
git_config* cfg,
405405
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
406406
long value);
407407

408408
[DllImport(libgit2)]
409-
internal static extern int git_config_set_string(
410-
ConfigurationSafeHandle cfg,
409+
internal static extern unsafe int git_config_set_string(
410+
git_config* cfg,
411411
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name,
412412
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string value);
413413

@@ -416,27 +416,27 @@ internal delegate int config_foreach_callback(
416416
IntPtr payload);
417417

418418
[DllImport(libgit2)]
419-
internal static extern int git_config_foreach(
420-
ConfigurationSafeHandle cfg,
419+
internal static extern unsafe int git_config_foreach(
420+
git_config* cfg,
421421
config_foreach_callback callback,
422422
IntPtr payload);
423423

424424
[DllImport(libgit2)]
425-
internal static extern int git_config_iterator_glob_new(
426-
out ConfigurationIteratorSafeHandle iter,
427-
ConfigurationSafeHandle cfg,
425+
internal static extern unsafe int git_config_iterator_glob_new(
426+
out IntPtr iter,
427+
IntPtr cfg,
428428
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string regexp);
429429

430430
[DllImport(libgit2)]
431431
internal static extern int git_config_next(
432432
out IntPtr entry,
433-
ConfigurationIteratorSafeHandle iter);
433+
IntPtr iter);
434434

435435
[DllImport(libgit2)]
436436
internal static extern void git_config_iterator_free(IntPtr iter);
< BD94 code>437437

438438
[DllImport(libgit2)]
439-
internal static extern int git_config_snapshot(out ConfigurationSafeHandle @out, ConfigurationSafeHandle config);
439+
internal static extern unsafe int git_config_snapshot(out git_config* @out, git_config* config);
440440

441441
// Ordinarily we would decorate the `url` parameter with the StrictUtf8Marshaler like we do everywhere
442442
// else, but apparently doing a native->managed callback with the 64-bit version of CLR 2.0 can
@@ -1376,7 +1376,7 @@ internal static extern unsafe int git_repository_open_ext(
13761376
[DllImport(libgit2)]
13771377
internal static extern unsafe void git_repository_set_config(
13781378
git_repository* repository,
1379-
ConfigurationSafeHandle config);
1379+
git_config* config);
13801380

13811381
[DllImport(libgit2)]
13821382
internal static extern unsafe int git_repository_set_ident(

LibGit2Sharp/Core/Opaques.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ internal struct git_status_list {}
1010
internal struct git_blame {}
1111
internal struct git_diff {}
1212
internal struct git_patch {}
13+
internal struct git_config {}
1314
}
1415

0 commit comments

Comments
 (0)
0