using System.Collections.Generic; using System.Text.RegularExpressions; namespace SourceGit.Git { /// /// Git remote /// public class Remote { private static readonly Regex FORMAT = new Regex(@"^([\w\.\-]+)\s*(\S+).*$"); /// /// Name of this remote /// public string Name { get; set; } /// /// URL /// public string URL { get; set; } /// /// Parsing remote /// /// Repository /// public static List Load(Repository repo) { var remotes = new List(); var added = new List(); repo.RunCommand("remote -v", data => { var match = FORMAT.Match(data); if (!match.Success) return; var remote = new Remote() { Name = match.Groups[1].Value, URL = match.Groups[2].Value, }; if (added.Contains(remote.Name)) return; added.Add(remote.Name); remotes.Add(remote); }); return remotes; } /// /// Add new remote /// /// /// /// public static void Add(Repository repo, string name, string url) { var errs = repo.RunCommand($"remote add {name} {url}", null); if (errs != null) { App.RaiseError(errs); } else { repo.Fetch(null, true, null); } } /// /// Delete remote. /// /// /// public static void Delete(Repository repo, string remote) { var errs = repo.RunCommand($"remote remove {remote}", null); if (errs != null) App.RaiseError(errs); } /// /// Edit remote. /// /// /// /// public void Edit(Repository repo, string name, string url) { string errs = null; if (name != Name) { errs = repo.RunCommand($"remote rename {Name} {name}", null); if (errs != null) { App.RaiseError(errs); return; } } if (url != URL) { errs = repo.RunCommand($"remote set-url {name} {url}", null); if (errs != null) App.RaiseError(errs); } } } }