using System.Collections.Generic; using System.Text; namespace SourceGit.Models { public class DiffOption { public Change WorkingCopyChange => _workingCopyChange; public bool IsUnstaged => _isUnstaged; public List Revisions => _revisions; public string Path => _path; public string OrgPath => _orgPath; /// /// Only used for working copy changes /// /// /// public DiffOption(Change change, bool isUnstaged) { _workingCopyChange = change; _isUnstaged = isUnstaged; if (isUnstaged) { switch (change.WorkTree) { case ChangeState.Added: case ChangeState.Untracked: _extra = "--no-index"; _path = change.Path; _orgPath = "/dev/null"; break; default: _path = change.Path; _orgPath = change.OriginalPath; break; } } else { if (change.DataForAmend != null) _extra = "--cached HEAD^"; else _extra = "--cached"; _path = change.Path; _orgPath = change.OriginalPath; } } /// /// Only used for commit changes. /// /// /// public DiffOption(Commit commit, Change change) { var baseRevision = commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : $"{commit.SHA}^"; _revisions.Add(baseRevision); _revisions.Add(commit.SHA); _path = change.Path; _orgPath = change.OriginalPath; } /// /// Diff with filepath. Used by FileHistories /// /// /// public DiffOption(Commit commit, string file) { var baseRevision = commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : $"{commit.SHA}^"; _revisions.Add(baseRevision); _revisions.Add(commit.SHA); _path = file; } /// /// Used to show differences between two revisions. /// /// /// /// public DiffOption(string baseRevision, string targetRevision, Change change) { _revisions.Add(string.IsNullOrEmpty(baseRevision) ? "-R" : baseRevision); _revisions.Add(targetRevision); _path = change.Path; _orgPath = change.OriginalPath; } /// /// Converts to diff command arguments. /// /// public override string ToString() { var builder = new StringBuilder(); if (!string.IsNullOrEmpty(_extra)) builder.Append($"{_extra} "); foreach (var r in _revisions) builder.Append($"{r} "); builder.Append("-- "); if (!string.IsNullOrEmpty(_orgPath)) builder.Append($"\"{_orgPath}\" "); builder.Append($"\"{_path}\""); return builder.ToString(); } private readonly Change _workingCopyChange = null; private readonly bool _isUnstaged = false; private readonly string _path; private readonly string _orgPath = string.Empty; private readonly string _extra = string.Empty; private readonly List _revisions = new List(); } }