diff --git a/.github/workflows/application.yaml b/.github/workflows/application.yaml index 59d5a59..c720561 100644 --- a/.github/workflows/application.yaml +++ b/.github/workflows/application.yaml @@ -16,9 +16,9 @@ jobs: - name: Checkout uses: actions/checkout@v2 - - uses: actions/setup-dotnet@v1 + - uses: actions/setup-dotnet@v4 with: - dotnet-version: '7.0.x' + dotnet-version: '9.0.x' - name: Build run: | diff --git a/.gitignore b/.gitignore index 376c1c0..edfe61d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -bin/ -obj/ +.vs/ +bin/ +obj/ *.sh \ No newline at end of file diff --git a/GitTools.csproj b/GitTools.csproj index b90061b..4ef583c 100644 --- a/GitTools.csproj +++ b/GitTools.csproj @@ -1,21 +1,21 @@ - - - - Exe - net7.0-windows - Iswenzz - - Iswenzz (c) 2021 - https://iswenzz.com/ - https://github.com/Iswenzz/GitTools.git - Iswenzz - - - - - - - - - - + + + + Exe + net9.0-windows7.0 + Iswenzz + + Iswenzz (c) 2021 + https://iswenzz.com/ + https://github.com/Iswenzz/GitTools.git + Iswenzz + + + + + + + + + + diff --git a/GitTools/CLI/CLIParser.cs b/GitTools/CLI/CLIParser.cs index 6631dc5..9fb8991 100644 --- a/GitTools/CLI/CLIParser.cs +++ b/GitTools/CLI/CLIParser.cs @@ -1,34 +1,34 @@ -using CommandLine; - -namespace Iswenzz.GitTools.CLI -{ - /// - /// Class for parsing the program arguments. - /// - public static class CLIParser - { - public static Options Options { get; set; } - public static CopyCommits CopyCommits { get; set; } - public static MockCommit Commit { get; set; } - - /// - /// Parse the program arguments. - /// - /// The program arguments. - public static void Parse(string[] args) - { - Parser.Default.ParseArguments(args) - .WithParsed(options => ParseAndExecute(Options = options)) - .WithParsed(options => ParseAndExecute(CopyCommits = options)) - .WithParsed(options => ParseAndExecute(Commit = options)); - } - - /// - /// Parse the arguments using the CommandLineParser library. - /// - /// Class that implements ICommand interface. - /// The parsed options. - private static void ParseAndExecute(T options) where T : ICMD => - options.Execute(); - } -} +using CommandLine; + +namespace Iswenzz.GitTools.CLI +{ + /// + /// Class for parsing the program arguments. + /// + public static class CLIParser + { + public static Options Options { get; set; } + public static CopyCommits CopyCommits { get; set; } + public static MockCommit Commit { get; set; } + + /// + /// Parse the program arguments. + /// + /// The program arguments. + public static void Parse(string[] args) + { + Parser.Default.ParseArguments(args) + .WithParsed(options => ParseAndExecute(Options = options)) + .WithParsed(options => ParseAndExecute(CopyCommits = options)) + .WithParsed(options => ParseAndExecute(Commit = options)); + } + + /// + /// Parse the arguments using the CommandLineParser library. + /// + /// Class that implements ICommand interface. + /// The parsed options. + private static void ParseAndExecute(T options) where T : ICMD => + options.Execute(); + } +} diff --git a/GitTools/CLI/CopyCommits.cs b/GitTools/CLI/CopyCommits.cs index 1061718..3a4ab39 100644 --- a/GitTools/CLI/CopyCommits.cs +++ b/GitTools/CLI/CopyCommits.cs @@ -1,14 +1,14 @@ using System; +using System.IO; using System.Collections.Generic; using System.Globalization; -using System.Linq; + using CommandLine; using CommandLine.Text; using LibGit2Sharp; using Iswenzz.GitTools.Sys; using Iswenzz.GitTools.Utils; -using System.IO; namespace Iswenzz.GitTools.CLI { @@ -45,9 +45,10 @@ public class CopyCommits : ICMD [Usage(ApplicationAlias = "gittools")] public static IEnumerable Examples { - get => new List { - new Example("Copy the commits on a specific date", - new CopyCommits { + get => [ + new("Copy the commits on a specific date", + new CopyCommits + { User = "Iswenzz", Email = "alexisnardiello@gmail.com", InputRepository = "C:\\Repository\\A", @@ -55,7 +56,7 @@ public static IEnumerable Examples SinceDate = "25/06/1999", UntilDate = "04/07/2021" }) - }; + ]; } /// diff --git a/GitTools/CLI/ICMD.cs b/GitTools/CLI/ICMD.cs index e272e7f..dbfb7f5 100644 --- a/GitTools/CLI/ICMD.cs +++ b/GitTools/CLI/ICMD.cs @@ -1,10 +1,10 @@ -namespace Iswenzz.GitTools.CLI -{ - /// - /// Executable command interface. - /// - public interface ICMD - { - public void Execute(); - } -} +namespace Iswenzz.GitTools.CLI +{ + /// + /// Executable command interface. + /// + public interface ICMD + { + public void Execute(); + } +} diff --git a/GitTools/CLI/MockCommit.cs b/GitTools/CLI/MockCommit.cs index 4fd9730..26004c7 100644 --- a/GitTools/CLI/MockCommit.cs +++ b/GitTools/CLI/MockCommit.cs @@ -1,56 +1,58 @@ -using System; -using System.Collections.Generic; -using CommandLine; -using CommandLine.Text; - -using Iswenzz.GitTools.Sys; - -namespace Iswenzz.GitTools.CLI -{ - /// - /// Command to create a commit at a specific date. - /// - [Verb("commit", HelpText = "Create a commit at a specific date.")] - public class MockCommit : ICMD - { - [Option('u', "user", Required = true, HelpText = "The user.")] - public string User { get; set; } - - [Option('e', "email", Required = true, HelpText = "The email.")] - public string Email { get; set; } - - [Option('m', "message", Required = true, HelpText = "The commit message.")] - public string Message { get; set; } - - [Option('o', "output-repository", Required = true, HelpText = "The output repository path.")] - public string OutputRepository { get; set; } - - [Option('d', "date", HelpText = "Commit at a specific date.")] - public string Date { get; set; } = DateTime.Now.ToString("O"); - - [Usage(ApplicationAlias = "gittools")] - public static IEnumerable Examples - { - get => new List { - new Example("Create a commit at a specific date", - new MockCommit { - User = "Iswenzz", - Email = "alexisnardiello@gmail.com", - OutputRepository = "C:\\Repository", - Message = "Happy Birthday !", - Date = "25/06/2021" - }) - }; - } - - /// - /// Execute the command. - /// - public void Execute() - { - // Commits to output repository - Git git = new(User, Email, OutputRepository); - git.MockCommit(Message, DateTime.Parse(Date)); - } - } -} +using System; +using System.Collections.Generic; + +using CommandLine; +using CommandLine.Text; + +using Iswenzz.GitTools.Sys; + +namespace Iswenzz.GitTools.CLI +{ + /// + /// Command to create a commit at a specific date. + /// + [Verb("commit", HelpText = "Create a commit at a specific date.")] + public class MockCommit : ICMD + { + [Option('u', "user", Required = true, HelpText = "The user.")] + public string User { get; set; } + + [Option('e', "email", Required = true, HelpText = "The email.")] + public string Email { get; set; } + + [Option('m', "message", Required = true, HelpText = "The commit message.")] + public string Message { get; set; } + + [Option('o', "output-repository", Required = true, HelpText = "The output repository path.")] + public string OutputRepository { get; set; } + + [Option('d', "date", HelpText = "Commit at a specific date.")] + public string Date { get; set; } = DateTime.Now.ToString("O"); + + [Usage(ApplicationAlias = "gittools")] + public static IEnumerable Examples + { + get => [ + new("Create a commit at a specific date", + new MockCommit + { + User = "Iswenzz", + Email = "alexisnardiello@gmail.com", + OutputRepository = "C:\\Repository", + Message = "Happy Birthday !", + Date = "25/06/2021" + }) + ]; + } + + /// + /// Execute the command. + /// + public void Execute() + { + // Commits to output repository + Git git = new(User, Email, OutputRepository); + git.MockCommit(Message, DateTime.Parse(Date)); + } + } +} diff --git a/GitTools/CLI/Options.cs b/GitTools/CLI/Options.cs index 93a8a8e..3a92e04 100644 --- a/GitTools/CLI/Options.cs +++ b/GitTools/CLI/Options.cs @@ -1,10 +1,10 @@ -namespace Iswenzz.GitTools.CLI -{ - /// - /// The program default CLI. - /// - public class Options : ICMD - { - public void Execute() { } - } -} +namespace Iswenzz.GitTools.CLI +{ + /// + /// The program default CLI. + /// + public class Options : ICMD + { + public void Execute() { } + } +} diff --git a/GitTools/GitTools.sln b/GitTools/GitTools.sln new file mode 100644 index 0000000..c6d7f8a --- /dev/null +++ b/GitTools/GitTools.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35309.182 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GitTools", "..\GitTools.csproj", "{9DEDF5CE-AF1A-4AF9-9894-8F825B0FB411}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9DEDF5CE-AF1A-4AF9-9894-8F825B0FB411}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9DEDF5CE-AF1A-4AF9-9894-8F825B0FB411}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9DEDF5CE-AF1A-4AF9-9894-8F825B0FB411}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9DEDF5CE-AF1A-4AF9-9894-8F825B0FB411}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A40B826F-D647-4A51-B92F-856ACDE66C0B} + EndGlobalSection +EndGlobal diff --git a/GitTools/Sys/Git.cs b/GitTools/Sys/Git.cs index 30c1cf5..62d58d7 100644 --- a/GitTools/Sys/Git.cs +++ b/GitTools/Sys/Git.cs @@ -1,145 +1,145 @@ -using System; -using System.Collections.Generic; -using LibGit2Sharp; - -namespace Iswenzz.GitTools.Sys -{ - /// - /// Class to manipulate git. - /// - public class Git - { - public Repository Repository { get; set; } - public string User { get; set; } - public string Email { get; set; } - - /// - /// Initialize a new Git instance with the specified git repository. - /// - /// The user. - /// The email. - /// Git repository path. - public Git(string user, string email, string repository) - { - Repository = new(repository); - User = user; - Email = email; - } - - /// - /// Add a remote with a specific name and url, then update the repository remotes. - /// - /// The remote name. - /// The remote URL. - public void AddRemote(string name, string url) - { - if (Repository.Network.Remotes[name] == null) - { - Repository.Network.Remotes.Add(name, url); - FetchRemote(name); - } - } - - /// - /// Fetch a remote. - /// - /// The remote name. - public void FetchRemote(string name) => - Repository.Network.Fetch(name, Array.Empty()); - - /// - /// Remove a specific remote. - /// - /// The remote name. - public void RemoveRemote(string name) => - Repository.Network.Remotes.Remove(name); - - /// - /// Predicate to check if an object exists in this repository. - /// - /// Class extending . - /// The . - /// - public bool ObjectExists(T obj) where T : GitObject => - Repository.Lookup(obj.Id) != null; - - /// - /// Get the commits in a specific time range and can be filtered by email. - /// Working with squashed commits as well. - /// - public List GetCommits(DateTime sinceDate = default, DateTime untilDate = default, - string email = null) - { - DateTime since = sinceDate; - DateTime until = untilDate != DateTime.UnixEpoch ? untilDate : DateTime.Now; - - List commits = new(); - foreach (GitObject o in Repository.ObjectDatabase) - { - try - { - Commit c = o.Peel(); - if (!string.IsNullOrEmpty(email) && c.Committer.Email != email) - continue; - if (c.Committer.When < since || c.Committer.When > until) - continue; - commits.Add(c); - } - catch (InvalidSpecificationException) { } - } - return commits; - } - - /// - /// Cherry-pick a specific commit. - /// - /// The commit to cherry-pick. - /// Commit as empty. - public void CherryPick(Commit commit, bool empty = false) - { - try - { - if (empty) throw new EmptyCommitException(); - - Repository.Reset(ResetMode.Hard); - CherryPickOptions options = new() { FileConflictStrategy = CheckoutFileConflictStrategy.Theirs }; - CherryPickResult result = Repository.CherryPick(commit, commit.Committer, options); - - if (result.Status == CherryPickStatus.Conflicts) - { - Commands.Stage(Repository, "*"); - Repository.Commit(commit.Message, commit.Author, commit.Committer); - } - } - catch (EmptyCommitException) - { - MockCommit(commit.Message, commit.Committer.When.DateTime); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - } - } - - /// - /// Create an empty commit to the git repository. - /// - /// The user to commit. - /// The commit message. - /// The commit date. - public void MockCommit(string message, DateTime date = default) - { - if (date == DateTime.MinValue) - date = DateTime.Now; - - Signature author = new(User, Email, date); - Signature committer = author; - - CommitOptions options = new() - { - AllowEmptyCommit = true - }; - Repository.Commit(message, author, committer, options); - } - } -} +using System; +using System.Collections.Generic; +using LibGit2Sharp; + +namespace Iswenzz.GitTools.Sys +{ + /// + /// Class to manipulate git. + /// + public class Git + { + public Repository Repository { get; set; } + public string User { get; set; } + public string Email { get; set; } + + /// + /// Initialize a new Git instance with the specified git repository. + /// + /// The user. + /// The email. + /// Git repository path. + public Git(string user, string email, string repository) + { + Repository = new(repository); + User = user; + Email = email; + } + + /// + /// Add a remote with a specific name and url, then update the repository remotes. + /// + /// The remote name. + /// The remote URL. + public void AddRemote(string name, string url) + { + if (Repository.Network.Remotes[name] == null) + { + Repository.Network.Remotes.Add(name, url); + FetchRemote(name); + } + } + + /// + /// Fetch a remote. + /// + /// The remote name. + public void FetchRemote(string name) => + Repository.Network.Fetch(name, Array.Empty()); + + /// + /// Remove a specific remote. + /// + /// The remote name. + public void RemoveRemote(string name) => + Repository.Network.Remotes.Remove(name); + + /// + /// Predicate to check if an object exists in this repository. + /// + /// Class extending . + /// The . + /// + public bool ObjectExists(T obj) where T : GitObject => + Repository.Lookup(obj.Id) != null; + + /// + /// Get the commits in a specific time range and can be filtered by email. + /// Working with squashed commits as well. + /// + public List GetCommits(DateTime sinceDate = default, DateTime untilDate = default, + string email = null) + { + DateTime since = sinceDate; + DateTime until = untilDate != DateTime.UnixEpoch ? untilDate : DateTime.Now; + + List commits = []; + foreach (GitObject o in Repository.ObjectDatabase) + { + try + { + Commit c = o.Peel(); + if (!string.IsNullOrEmpty(email) && c.Committer.Email != email) + continue; + if (c.Committer.When < since || c.Committer.When > until) + continue; + commits.Add(c); + } + catch (InvalidSpecificationException) { } + } + return commits; + } + + /// + /// Cherry-pick a specific commit. + /// + /// The commit to cherry-pick. + /// Commit as empty. + public void CherryPick(Commit commit, bool empty = false) + { + try + { + if (empty) throw new EmptyCommitException(); + + Repository.Reset(ResetMode.Hard); + CherryPickOptions options = new() { FileConflictStrategy = CheckoutFileConflictStrategy.Theirs }; + CherryPickResult result = Repository.CherryPick(commit, commit.Committer, options); + + if (result.Status == CherryPickStatus.Conflicts) + { + Commands.Stage(Repository, "*"); + Repository.Commit(commit.Message, commit.Author, commit.Committer); + } + } + catch (EmptyCommitException) + { + MockCommit(commit.Message, commit.Committer.When.DateTime); + } + catch (Exception e) + { + Console.WriteLine(e.Message); + } + } + + /// + /// Create an empty commit to the git repository. + /// + /// The user to commit. + /// The commit message. + /// The commit date. + public void MockCommit(string message, DateTime date = default) + { + if (date == DateTime.MinValue) + date = DateTime.Now; + + Signature author = new(User, Email, date); + Signature committer = author; + + CommitOptions options = new() + { + AllowEmptyCommit = true + }; + Repository.Commit(message, author, committer, options); + } + } +} diff --git a/GitTools/Utils/Editor.cs b/GitTools/Utils/Editor.cs index b7ef19f..d5b9462 100644 --- a/GitTools/Utils/Editor.cs +++ b/GitTools/Utils/Editor.cs @@ -1,80 +1,81 @@ -using Iswenzz.GitTools.Sys; -using LibGit2Sharp; - -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.IO; -using System.Linq; - -namespace Iswenzz.GitTools.Utils -{ - /// - /// Open a temporary file in the default editor to pick all lines to select. - /// - public class Editor : IDisposable - { - public string FilePath { get; set; } - public Process Process { get; set; } - - /// - /// Initialize a new EditorSlectableList instance. - /// - public Editor() - { - FilePath = $"{Path.GetTempFileName()}.txt"; - } - - /// - /// Select commits from git repository. - /// - /// The commits. - /// The git repository. - /// - public List SelectCommits(List commits, Git git) - { - IEnumerable commitLines = commits - .Where(git.ObjectExists) - .Select(c => $"{c.Id.Sha} {c.Committer.Email} {c.MessageShort}"); - - OpenWithContent(commitLines); - IEnumerable selectedCommitLines = GetFileContent(); - return commits.Where(c => selectedCommitLines.Any(l => l.Contains(c.Id.Sha))).ToList(); - } - - /// - /// Get the selected lines from the editor. - /// - /// - public IEnumerable GetFileContent() - { - Process.WaitForExit(); - return File.ReadAllLines(FilePath); - } - - /// - /// Open the editor with the specified content. - /// - /// The editor content. - public void OpenWithContent(IEnumerable lines) - { - File.WriteAllLines(FilePath, lines); - Process = new Process - { - StartInfo = new ProcessStartInfo(FilePath) - { - UseShellExecute = true, - } - }; - Process.Start(); - } - - /// - /// Release resources. - /// - public void Dispose() - { - Process?.Dispose(); - } - } -} +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; + +using LibGit2Sharp; + +using Iswenzz.GitTools.Sys; + +namespace Iswenzz.GitTools.Utils +{ + /// + /// Open a temporary file in the default editor to pick all lines to select. + /// + public class Editor : IDisposable + { + public string FilePath { get; set; } + public Process Process { get; set; } + + /// + /// Initialize a new EditorSlectableList instance. + /// + public Editor() + { + FilePath = $"{Path.GetTempFileName()}.txt"; + } + + /// + /// Select commits from git repository. + /// + /// The commits. + /// The git repository. + /// + public List SelectCommits(List commits, Git git) + { + IEnumerable commitLines = commits + .Where(git.ObjectExists) + .Select(c => $"{c.Id.Sha} {c.Committer.Email} {c.MessageShort}"); + + OpenWithContent(commitLines); + IEnumerable selectedCommitLines = GetFileContent(); + return commits.Where(c => selectedCommitLines.Any(l => l.Contains(c.Id.Sha))).ToList(); + } + + /// + /// Get the selected lines from the editor. + /// + /// + public IEnumerable GetFileContent() + { + Process.WaitForExit(); + return File.ReadAllLines(FilePath); + } + + /// + /// Open the editor with the specified content. + /// + /// The editor content. + public void OpenWithContent(IEnumerable lines) + { + File.WriteAllLines(FilePath, lines); + Process = new Process + { + StartInfo = new ProcessStartInfo(FilePath) + { + UseShellExecute = true, + } + }; + Process.Start(); + } + + /// + /// Release resources. + /// + public void Dispose() + { + Process?.Dispose(); + } + } +} diff --git a/GitTools/Utils/Reflect.cs b/GitTools/Utils/Reflect.cs deleted file mode 100644 index 764e8fe..0000000 --- a/GitTools/Utils/Reflect.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Reflection; - -namespace Iswenzz.GitTools.Utils -{ - /// - /// Utility class to reflect attributes that use the ReflectAttribute class. - /// - public static class Reflect - { - public static Assembly CurrentAssembly { get; set; } - - /// - /// Get the current Assembly. - /// - static Reflect() - { - CurrentAssembly = Assembly.GetExecutingAssembly(); - } - - /// - /// Retrieve the type from a custom attribute. - /// - /// The metadata to search. - /// - public static Type RetrieveTypeFromCustomAttributeMetadata(string expectedValue) - { - foreach (Type type in CurrentAssembly.GetTypes()) - { - ReflectiveAttribute attributes = type.GetCustomAttribute(); - if (attributes != null && attributes.ReflectMetadata == expectedValue) - return type; - } - return null; - } - } -} diff --git a/GitTools/Utils/ReflectiveAttribute.cs b/GitTools/Utils/ReflectiveAttribute.cs deleted file mode 100644 index b643a25..0000000 --- a/GitTools/Utils/ReflectiveAttribute.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; - -namespace Iswenzz.GitTools.Utils -{ - /// - /// Reflective attribute containing a ReflectMetadata property to indentify later. - /// - public class ReflectiveAttribute : Attribute - { - public string ReflectMetadata { get; set; } - } -}