forked from sourcegit-scm/sourcegit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffOption.cs
More file actions
129 lines (117 loc) · 4.21 KB
/
DiffOption.cs
File metadata and controls
129 lines (117 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System.Collections.Generic;
using System.Text;
namespace SourceGit.Models
{
public class DiffOption
{
/// <summary>
/// Enable `--ignore-cr-at-eol` by default?
/// </summary>
public static bool IgnoreCRAtEOL
{
get;
set;
} = true;
public Change WorkingCopyChange => _workingCopyChange;
public bool IsUnstaged => _isUnstaged;
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>
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 {change.DataForAmend.ParentSHA}";
else
_extra = "--cached";
_path = change.Path;
_orgPath = change.OriginalPath;
}
}
/// <summary>
/// Only used for commit changes.
/// </summary>
/// <param name="commit"></param>
/// <param name="change"></param>
public DiffOption(Commit commit, Change change)
{
var baseRevision = commit.Parents.Count == 0 ? Models.Commit.EmptyTreeSHA1 : $"{commit.SHA}^";
_revisions.Add(baseRevision);
_revisions.Add(commit.SHA);
_path = change.Path;
_orgPath = change.OriginalPath;
}
/// <summary>
/// Diff with filepath. Used by FileHistories
/// </summary>
/// <param name="commit"></param>
/// <param name="file"></param>
public DiffOption(Commit commit, string file)
{
var baseRevision = commit.Parents.Count == 0 ? Models.Commit.EmptyTreeSHA1 : $"{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>
public DiffOption(string baseRevision, string targetRevision, Change change)
{
_revisions.Add(string.IsNullOrEmpty(baseRevision) ? "-R" : baseRevision);
_revisions.Add(targetRevision);
_path = change.Path;
_orgPath = change.OriginalPath;
}
/// <summary>
/// Converts to diff command arguments.
/// </summary>
/// <returns></returns>
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<string> _revisions = new List<string>();
}
}