2024-02-05 23:08:37 -08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Models
|
|
|
|
|
{
|
|
|
|
|
public class DiffOption
|
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
|
public Change WorkingCopyChange => _workingCopyChange;
|
|
|
|
|
public bool IsUnstaged => _isUnstaged;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
public List<string> Revisions => _revisions;
|
|
|
|
|
public string Path => _path;
|
|
|
|
|
public string OrgPath => _orgPath;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Only used for working copy changes
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="change"></param>
|
|
|
|
|
/// <param name="isUnstaged"></param>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public DiffOption(Change change, bool isUnstaged)
|
|
|
|
|
{
|
2024-02-27 05:13:52 -08:00
|
|
|
|
_workingCopyChange = change;
|
|
|
|
|
_isUnstaged = isUnstaged;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
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;
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-30 21:04:29 -07:00
|
|
|
|
if (change.DataForAmend != null)
|
|
|
|
|
_extra = "--cached HEAD^";
|
|
|
|
|
else
|
|
|
|
|
_extra = "--cached";
|
|
|
|
|
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_path = change.Path;
|
|
|
|
|
_orgPath = change.OriginalPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Only used for commit changes.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="commit"></param>
|
|
|
|
|
/// <param name="change"></param>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public DiffOption(Commit commit, Change change)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var baseRevision = commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : $"{commit.SHA}^";
|
|
|
|
|
_revisions.Add(baseRevision);
|
|
|
|
|
_revisions.Add(commit.SHA);
|
|
|
|
|
_path = change.Path;
|
|
|
|
|
_orgPath = change.OriginalPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-02-27 05:13:52 -08:00
|
|
|
|
/// Diff with filepath. Used by FileHistories
|
2024-02-05 23:08:37 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="commit"></param>
|
|
|
|
|
/// <param name="file"></param>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public DiffOption(Commit commit, string file)
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var baseRevision = commit.Parents.Count == 0 ? "4b825dc642cb6eb9a060e54bf8d69288fbee4904" : $"{commit.SHA}^";
|
|
|
|
|
_revisions.Add(baseRevision);
|
|
|
|
|
_revisions.Add(commit.SHA);
|
|
|
|
|
_path = file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Used to show differences between two revisions.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="baseRevision"></param>
|
|
|
|
|
/// <param name="targetRevision"></param>
|
|
|
|
|
/// <param name="change"></param>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public DiffOption(string baseRevision, string targetRevision, Change change)
|
|
|
|
|
{
|
2024-07-27 06:35:57 -07:00
|
|
|
|
_revisions.Add(string.IsNullOrEmpty(baseRevision) ? "-R" : baseRevision);
|
2024-02-05 23:08:37 -08:00
|
|
|
|
_revisions.Add(targetRevision);
|
|
|
|
|
_path = change.Path;
|
|
|
|
|
_orgPath = change.OriginalPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Converts to diff command arguments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2024-02-05 23:08:37 -08:00
|
|
|
|
var builder = new StringBuilder();
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_extra))
|
|
|
|
|
builder.Append($"{_extra} ");
|
|
|
|
|
foreach (var r in _revisions)
|
|
|
|
|
builder.Append($"{r} ");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
|
|
|
|
|
builder.Append("-- ");
|
2024-03-31 01:54:29 -07:00
|
|
|
|
if (!string.IsNullOrEmpty(_orgPath))
|
|
|
|
|
builder.Append($"\"{_orgPath}\" ");
|
2024-02-05 23:08:37 -08:00
|
|
|
|
builder.Append($"\"{_path}\"");
|
|
|
|
|
|
|
|
|
|
return builder.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly Change _workingCopyChange = null;
|
|
|
|
|
private readonly bool _isUnstaged = false;
|
2024-07-14 09:30:31 -07:00
|
|
|
|
private readonly string _path;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
private readonly string _orgPath = string.Empty;
|
|
|
|
|
private readonly string _extra = string.Empty;
|
|
|
|
|
private readonly List<string> _revisions = new List<string>();
|
2024-02-05 23:08:37 -08:00
|
|
|
|
}
|
2024-03-31 01:54:29 -07:00
|
|
|
|
}
|