forked from libgit2/libgit2sharp
/
GitDiff.cs
146 lines (122 loc) · 3.89 KB
/
GitDiff.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Runtime.InteropServices;
namespace LibGit2Sharp.Core
{
[Flags]
internal enum GitDiffOptionFlags
{
GIT_DIFF_NORMAL = 0,
GIT_DIFF_REVERSE = (1 << 0),
GIT_DIFF_FORCE_TEXT = (1 << 1),
GIT_DIFF_IGNORE_WHITESPACE = (1 << 2),
GIT_DIFF_IGNORE_WHITESPACE_CHANGE = (1 << 3),
GIT_DIFF_IGNORE_WHITESPACE_EOL = (1 << 4),
GIT_DIFF_IGNORE_SUBMODULES = (1 << 5),
GIT_DIFF_PATIENCE = (1 << 6),
GIT_DIFF_INCLUDE_IGNORED = (1 << 7),
GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8),
}
[StructLayout(LayoutKind.Sequential)]
internal class GitStrArrayIn : IDisposable
{
public IntPtr strings;
public uint size;
public static GitStrArrayIn BuildFrom(FilePath[] paths)
{
var nbOfPaths = paths.Length;
var pathPtrs = new IntPtr[nbOfPaths];
for (int i = 0; i < nbOfPaths; i++)
{
var s = paths[i];
pathPtrs[i] = FilePathMarshaler.FromManaged(s);
}
int dim = IntPtr.Size * nbOfPaths;
IntPtr arrayPtr = Marshal.AllocHGlobal(dim);
Marshal.Copy(pathPtrs, 0, arrayPtr, nbOfPaths);
return new GitStrArrayIn { strings = arrayPtr, size = (uint)nbOfPaths };
}
public void Dispose()
{
if (size == 0)
{
return;
}
var nbOfPaths = (int)size;
var pathPtrs = new IntPtr[nbOfPaths];
Marshal.Copy(strings, pathPtrs, 0, nbOfPaths);
for (int i = 0; i < nbOfPaths; i ++)
{
Marshal.FreeHGlobal(pathPtrs[i]);
}
Marshal.FreeHGlobal(strings);
}
}
[StructLayout(LayoutKind.Sequential)]
internal class GitDiffOptions : IDisposable
{
public GitDiffOptionFlags Flags;
public ushort ContextLines;
public ushort InterhunkLines;
// NB: These are char*s to UTF8 strings, finna marshal them by hand
public IntPtr OldPrefixString;
public IntPtr NewPrefixString;
public GitStrArrayIn PathSpec;
public void Dispose()
{
if (PathSpec == null)
{
return;
}
PathSpec.Dispose();
PathSpec.size = 0;
}
}
[Flags]
internal enum GitDiffFileFlags
{
GIT_DIFF_FILE_VALID_OID = (1 << 0),
GIT_DIFF_FILE_FREE_PATH = (1 << 1),
GIT_DIFF_FILE_BINARY = (1 << 2),
GIT_DIFF_FILE_NOT_BINARY = (1 << 3),
GIT_DIFF_FILE_FREE_DATA = (1 << 4),
GIT_DIFF_FILE_UNMAP_DATA = (1 << 5),
}
[StructLayout(LayoutKind.Sequential)]
internal class GitDiffFile
{
public GitOid Oid;
public IntPtr Path;
public long Size;
public GitDiffFileFlags Flags;
public ushort Mode;
}
[StructLayout(LayoutKind.Sequential)]
internal class GitDiffDelta
{
public GitDiffFile OldFile;
public GitDiffFile NewFile;
public ChangeKind Status;
public uint Similarity;
public int Binary;
}
[StructLayout(LayoutKind.Sequential)]
internal class GitDiffRange
{
public int OldStart;
public int OldLines;
public int NewStart;
public int NewLines;
}
enum GitDiffLineOrigin : byte
{
GIT_DIFF_LINE_CONTEXT = 0x20, //' ',
GIT_DIFF_LINE_ADDITION = 0x2B, //'+',
GIT_DIFF_LINE_DELETION = 0x2D, //'-',
GIT_DIFF_LINE_ADD_EOFNL = 0x0A, //'\n', /**< LF was added at end of file */
GIT_DIFF_LINE_DEL_EOFNL = 0x0, //'\0', /**< LF was removed at end of file */
/* these values will only be sent to a `git_diff_output_fn` */
GIT_DIFF_LINE_FILE_HDR = 0x46, //'F',
GIT_DIFF_LINE_HUNK_HDR = 0x48, //'H',
GIT_DIFF_LINE_BINARY = 0x42, //'B',
}
}