public string[] GetRepositoryItemList(string basePath, string repositoryName) { var baseUri = new Uri(basePath); var repositoryPath = baseUri.LocalPath; var listCommand = new GitCommand(repositoryPath, "ls-tree") { new GitCommandItem('r'), new GitCommandItem("name-only"), repositoryName }; var lines = listCommand.ReadLines(true); var itemList = new List <string>(lines.Length); foreach (var item in lines) { if (item.EndsWith(KeepExtension) == true) { itemList.Add(PathUtility.Separator + item.Substring(0, item.Length - KeepExtension.Length)); } else { itemList.Add(PathUtility.Separator + item); } } return(itemList.ToArray()); }
public static GitBranchCollection Run(string repositoryPath) { var listCommand = new GitCommand(repositoryPath, "branch") { new GitCommandItem("list") }; var lines = listCommand.ReadLines(true); var itemList = new List <string>(lines.Length); var currentBranch = string.Empty; foreach (var line in lines) { var match = Regex.Match(line, "^(?<current>[*])*\\s*(?<branch>\\S+)"); if (match.Success == true) { var isCurrent = match.Groups["current"].Value == "*"; var branchName = match.Groups["branch"].Value; if (isCurrent == true) { currentBranch = branchName; } itemList.Add(branchName); } } return(new GitBranchCollection(itemList, currentBranch)); }
public GitRepository(GitRepositoryProvider repositoryProvider, RepositorySettings settings, RepositoryInfo repositoryInfo) { this.repositoryProvider = repositoryProvider; this.settings = settings; this.logService = settings.LogService; this.repositoryInfo = repositoryInfo; this.resetCommand = new GitCommand(this.BasePath, "reset") { new GitCommandItem("hard") }; this.cleanCommand = new GitCommand(this.BasePath, "clean") { new GitCommandItem('f'), new GitCommandItem('d') }; var statusCommand = new GitCommand(this.BasePath, "status") { new GitCommandItem('s'), }; var items = statusCommand.ReadLines(true); if (items.Length != 0) { var sb = new StringBuilder(); sb.AppendLine($"Repository is dirty. Please fix the problem before running the service."); sb.AppendLine(); foreach (var item in items) { sb.AppendLine(item); } throw new Exception($"{sb}"); } }
public void EndTransaction() { var transactionMessage = string.Join(Environment.NewLine, this.transactionMessageList); var statusCommand = new GitCommand(this.BasePath, "status") { new GitCommandItem('s') }; var items = statusCommand.ReadLines(true); if (items.Length != 0) { var commitCommand = new GitCommitCommand(this.BasePath, this.transactionAuthor, transactionMessage); var result = commitCommand.Run(this.logService); this.logService?.Debug(result); var log = GitLogInfo.GetLatestLog(this.BasePath); this.repositoryInfo.Revision = log.CommitID; this.repositoryInfo.ModificationInfo = new SignatureDate(this.transactionAuthor, log.CommitDate); this.SetNotes(this.transactionPropertyList.ToArray()); FileUtility.Delete(this.transactionPatchPath); this.transactionAuthor = null; this.transactionName = null; this.transactionMessageList = null; this.transactionPropertyList = null; this.transactionPatchPath = null; this.Pull(); this.Push(); this.PushNotes(); } else { this.logService?.Debug("repository has no changes."); } }
public void RevertRepository(string author, string basePath, string repositoryName, string revision, string comment) { var baseUri = new Uri(basePath); var repositoryPath = baseUri.LocalPath; this.CheckoutBranch(repositoryPath, repositoryName); try { var revisionsCommand = new GitCommand(repositoryPath, "log") { GitCommandItem.FromPretty("format:%H"), }; var revisions = revisionsCommand.ReadLines(); if (revisions.Contains(revision) == false) { throw new ArgumentException($"'{revision}' is invalid revision.", nameof(revision)); } foreach (var item in revisions) { if (item == revision) { break; } var revertCommand = new GitCommand(repositoryPath, "revert") { new GitCommandItem('n'), item, }; revertCommand.Run(); } var statusCommand = new GitCommand(repositoryPath, "status") { new GitCommandItem('s') }; var items = statusCommand.ReadLines(true); if (items.Length != 0) { var commitCommand = new GitCommitCommand(basePath, author, comment); commitCommand.Run(); } else { throw new InvalidOperationException("nothing to revert."); } } catch { var abortCommand = new GitCommand(repositoryPath, "revert") { new GitCommandItem("abort"), }; abortCommand.TryRun(); throw; } }
public void Commit(string author, string comment, params LogPropertyInfo[] properties) { if (this.transactionName != null) { var diffCommand = new GitCommand(this.BasePath, "diff") { "HEAD", new GitCommandItem("stat"), new GitCommandItem("binary") }; var output = diffCommand.ReadLine(); FileUtility.Prepare(this.transactionPatchPath); File.WriteAllText(this.transactionPatchPath, output); this.transactionMessageList.Add(comment); this.transactionPropertyList.AddRange(properties); return; } try { var statusCommand = new GitCommand(this.BasePath, "status") { new GitCommandItem('s') }; var items = statusCommand.ReadLines(true); if (items.Length != 0) { var authorValue = new GitAuthor(author); GitConfig.SetValue(this.BasePath, "user.email", authorValue.Email == string.Empty ? "<>" : authorValue.Email); GitConfig.SetValue(this.BasePath, "user.name", authorValue.Name); var commitCommand = new GitCommitCommand(this.BasePath, author, comment); var result = commitCommand.Run(this.logService); this.logService?.Debug(result); var log = GitLogInfo.GetLatestLog(this.BasePath); this.repositoryInfo.Revision = log.CommitID; this.repositoryInfo.ModificationInfo = new SignatureDate(author, log.CommitDate); this.SetNotes(properties); //this.isModified = true; //this.Pull(); //this.Push(); //this.PushNotes(); } else { this.logService?.Debug("repository no changes. \"{0}\"", this.BasePath); } } catch (Exception e) { this.logService?.Warn(e); throw; } }
public static GitBranchCollection GetRemoteBranches(string repositoryPath) { var listCommand = new GitCommand(repositoryPath, "branch") { new GitCommandItem('a') }; var lines = listCommand.ReadLines(true); var itemList = new List <string>(lines.Length); var currentBranch = string.Empty; foreach (var line in lines) { var match = Regex.Match(line, "remotes/origin/(?<branch>[^/]+)$"); if (match.Success == true) { var branchName = match.Groups["branch"].Value.Trim(); itemList.Add(branchName); } } return(new GitBranchCollection(itemList, currentBranch)); }
public string[] GetRepositories(string basePath) { var baseUri = new Uri(basePath); var repositoryPath = baseUri.LocalPath; var branchCommand = new GitCommand(repositoryPath, "branch") { new GitCommandItem("list") }; var lines = branchCommand.ReadLines(); var itemList = new List <string>(lines.Length); foreach (var line in lines) { var match = Regex.Match(line, "^[*]*\\s*(\\S+)"); if (match.Success) { var branchName = match.Groups[1].Value; itemList.Add(branchName); } } return(itemList.ToArray()); }