From da1028388a9cde00172d392340ef77bf5584b5c6 Mon Sep 17 00:00:00 2001 From: Nuno 'Omega Leo' Diogo Date: Fri, 15 Nov 2024 15:50:29 +0000 Subject: [PATCH] Added commit functionality to the git library --- OmegaLeo.HelperLib.ConsoleApp/Program.cs | 44 ++++++++++++++++++++++-- OmegaLeo.HelperLib.Git/GitClient.cs | 20 +++++++++-- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/OmegaLeo.HelperLib.ConsoleApp/Program.cs b/OmegaLeo.HelperLib.ConsoleApp/Program.cs index 56a573f..2b350a0 100644 --- a/OmegaLeo.HelperLib.ConsoleApp/Program.cs +++ b/OmegaLeo.HelperLib.ConsoleApp/Program.cs @@ -3,6 +3,7 @@ // /media/omegaleo/Development/Library Dev/HelperLib using GameDevLibrary.Extensions; +using LibGit2Sharp; using OmegaLeo.HelperLib.Git; using OmegaLeo.HelperLib.Git.Models; @@ -18,9 +19,46 @@ var git = new GitClient(repo); var changes = git.GetChanges(); -if (changes != null) +if (changes.FolderName.IsNotNullOrEmpty()) { RecursiveOutput(new List() { changes }); + + Console.WriteLine(new string('=', 20)); + Console.WriteLine("What do you want to do in this repository?"); + Console.WriteLine("1. Commit all changes"); + + if (int.TryParse(Console.ReadLine(), out var option)) + { + while (option != 0) + { + switch (option) + { + case 1: + Console.WriteLine("Write the commit message"); + var msg = Console.ReadLine(); + + while (msg.IsNullOrEmpty()) + { + Console.WriteLine("Empty message detected, please write a commit message..."); + msg = Console.ReadLine(); + } + + var signature = new Signature("Nuno 'Omega Leo' Diogo", "nunodiogo@omegaleo.pt", DateTimeOffset.Now); + git.Commit(msg, signature); + + break; + default: + Console.WriteLine("Invalid Option!"); + break; + } + + int.TryParse(Console.ReadLine(), out option); + } + } +} +else +{ + Console.WriteLine("No changes found in specified repository"); } Console.ReadLine(); @@ -30,7 +68,7 @@ void RecursiveOutput(List folders, int depth = 0) { foreach (var folder in folders) { - Console.WriteLine($"{new string('\t', depth + 1)}>{folder.FolderName}"); + Console.WriteLine($"{new string('\t', depth)}>{folder.FolderName}"); if (folder.SubFolders.Any()) { RecursiveOutput(folder.SubFolders, depth + 1); @@ -38,7 +76,7 @@ void RecursiveOutput(List folders, int depth = 0) foreach (var change in folder.ChangesInFolder) { - Console.WriteLine($"{new string('\t', depth + 2)}>{Path.GetFileName(change.Path)} ({change.Status})"); + Console.WriteLine($"{new string('\t', depth + 1)}>{Path.GetFileName(change.Path)} ({change.Status})"); } } } \ No newline at end of file diff --git a/OmegaLeo.HelperLib.Git/GitClient.cs b/OmegaLeo.HelperLib.Git/GitClient.cs index 79b7f90..1e418f9 100644 --- a/OmegaLeo.HelperLib.Git/GitClient.cs +++ b/OmegaLeo.HelperLib.Git/GitClient.cs @@ -50,8 +50,6 @@ namespace OmegaLeo.HelperLib.Git allChanges.AddRange(changes.Deleted); var uniqueFolderPaths = allChanges.Select(x => Path.GetDirectoryName(x.Path)).Distinct(); - - foreach (var path in uniqueFolderPaths) { @@ -97,5 +95,23 @@ namespace OmegaLeo.HelperLib.Git return folder; } + + public void Commit(string message, Signature author) + { + if (HasChanges()) + { + var changes = _repo.Diff.Compare(); + var allChanges = new List(); + allChanges.AddRange(changes.Added); + allChanges.AddRange(changes.Modified); + allChanges.AddRange(changes.Deleted); + + var filePaths = allChanges.Select(x => x.Path).Where(x => x.IsNotNullOrEmpty()); + + Commands.Stage(_repo, filePaths); + } + + _repo.Commit(message, author, author); + } } } \ No newline at end of file