Browse Source

Add IDispose to Git

Stuart Johnson 1 year ago
parent
commit
b71cd8091e
2 changed files with 37 additions and 26 deletions
  1. 9 1
      TFA-Bot/Git/clsGit.cs
  2. 28 25
      TFA-Bot/Git/clsGitBranchInfo.cs

+ 9 - 1
TFA-Bot/Git/clsGit.cs

@@ -32,6 +32,7 @@ namespace TFABot.Git
                 }
                 Console.WriteLine($"Opening Git @ {dir.FullName}");
                 Repo = new Repository(dir.FullName);
+                
                 Fetch();
                 Head = new clsGitHead(this);
             }
@@ -152,7 +153,14 @@ namespace TFABot.Git
 
         public void Dispose()
         {
-            Repo.Dispose();
+
+            foreach (var branch in BranchList)
+            {
+                if(branch!=null) branch.Dispose();
+            }
+        
+            if (Repo!=null) Repo.Dispose();
+            if (Head!=null) Head.Dispose();
         }
         
     }

+ 28 - 25
TFA-Bot/Git/clsGitBranchInfo.cs

@@ -4,48 +4,51 @@ using LibGit2Sharp;
 
 namespace TFABot.Git
 {
-    public class clsGitBranchInfo
+    public class clsGitBranchInfo : IDisposable
     {
         private string FriendlyName;
         clsGit Git;
-        
-        public Tag TagRemote {get; private set;}
-        public Tag TagHead {get; private set;}
-        public Branch BranchRemote {get; private set;}
-        public Branch BranchHead {get; private set;}
-        
+
+        public Tag TagRemote { get; private set; }
+        public Tag TagHead { get; private set; }
+        public Branch BranchRemote { get; private set; }
+        public Branch BranchHead { get; private set; }
+
         public clsGitBranchInfo(clsGit git, Branch branch, Tag tag)
         {
             Git = git;
             BranchRemote = branch;
             TagRemote = tag;
-            
+
             if (branch == Git.Repo.Head.TrackedBranch)
             {
-              BranchHead = Git.Repo.Head;
-              TagHead = Git.GetTag(BranchHead);
+                BranchHead = Git.Repo.Head;
+                TagHead = Git.GetTag(BranchHead);
             }
         }
-        
-        
+
+
         public void AppendDisplayColumns(ref clsColumnDisplay columnDisplay)
         {
-            columnDisplay.AppendCol( (BranchRemote.FriendlyName));
-            columnDisplay.AppendCol( (BranchRemote.Tip.Sha.Substring(0,7)));
-            columnDisplay.AppendCol( ($"{BranchRemote.Tip.Committer.When:yyyy-MM-dd HH:mm}"));
-            columnDisplay.AppendCol(TagRemote!=null ? TagRemote.FriendlyName:"");
-            if (BranchHead!=null)
+            columnDisplay.AppendCol((BranchRemote.FriendlyName));
+            columnDisplay.AppendCol((BranchRemote.Tip.Sha.Substring(0, 7)));
+            columnDisplay.AppendCol(($"{BranchRemote.Tip.Committer.When:yyyy-MM-dd HH:mm}"));
+            columnDisplay.AppendCol(TagRemote != null ? TagRemote.FriendlyName : "");
+            if (BranchHead != null)
             {
-                 columnDisplay.AppendCol( ($"<== HEAD "));
-                 if (Git.Repo.Head.Tip.Sha == BranchRemote.Tip.Sha) 
-                    columnDisplay.Append ("Up to date");
-                 else
-                    if (TagHead?.FriendlyName!=null) columnDisplay.Append(TagHead.FriendlyName);
-                    columnDisplay.Append ($" {(Git.Repo.Head.Tip.Tree.Count - BranchRemote.Tip.Tree.Count):+0;-0}");
+                columnDisplay.AppendCol(($"<== HEAD "));
+                if (Git.Repo.Head.Tip.Sha == BranchRemote.Tip.Sha)
+                    columnDisplay.Append("Up to date");
+                else
+                   if (TagHead?.FriendlyName != null) columnDisplay.Append(TagHead.FriendlyName);
+                columnDisplay.Append($" {(Git.Repo.Head.Tip.Tree.Count - BranchRemote.Tip.Tree.Count):+0;-0}");
             }
         }
-        
 
-        
+
+        public void Dispose()
+        {
+            if (Git!=null) Git.Dispose();
+        }
     }
 }