using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; namespace SourceGit.Git { /// /// Git tag. /// public class Tag { private static readonly Regex FORMAT = new Regex(@"\$(.*)\$(.*)\$(.*)"); /// /// SHA /// public string SHA { get; set; } /// /// Display name. /// public string Name { get; set; } /// /// Enable filter in log histories. /// public bool IsFiltered { get; set; } /// /// Load all tags /// /// /// public static List Load(Repository repo) { var args = "for-each-ref --sort=-creatordate --format=\"$%(refname:short)$%(objectname)$%(*objectname)\" refs/tags"; var tags = new List(); repo.RunCommand(args, line => { var match = FORMAT.Match(line); if (!match.Success) return; var name = match.Groups[1].Value; var commit = match.Groups[2].Value; var dereference = match.Groups[3].Value; if (string.IsNullOrEmpty(dereference)) { tags.Add(new Tag() { Name = name, SHA = commit, }); } else { tags.Add(new Tag() { Name = name, SHA = dereference, }); } }); return tags; } /// /// Add new tag. /// /// /// /// /// public static void Add(Repository repo, string name, string startPoint, string message) { var args = $"tag -a {name} {startPoint} "; if (!string.IsNullOrEmpty(message)) { string temp = Path.GetTempFileName(); File.WriteAllText(temp, message); args += $"-F \"{temp}\""; } else { args += $"-m {name}"; } var errs = repo.RunCommand(args, null); if (errs != null) App.RaiseError(errs); else repo.OnCommitsChanged?.Invoke(); } /// /// Delete tag. /// /// /// /// public static void Delete(Repository repo, string name, bool push) { var errs = repo.RunCommand($"tag --delete {name}", null); if (errs != null) { App.RaiseError(errs); return; } if (push) { var remotes = repo.Remotes(); foreach (var r in remotes) { repo.RunCommand($"-c credential.helper=manager push --delete {r.Name} refs/tags/{name}", null); } } repo.OnCommitsChanged?.Invoke(); } /// /// Push tag to remote. /// /// /// /// public static void Push(Repository repo, string name, string remote) { var errs = repo.RunCommand($"-c credential.helper=manager push {remote} refs/tags/{name}", null); if (errs != null) App.RaiseError(errs); } } }