forked from yysun/Git-Source-Control-Provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitFileStatusTracker.cs
331 lines (277 loc) · 10.8 KB
/
GitFileStatusTracker.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Timers;
using GitSharp.Core;
using GitSharp.Core.Util;
using GitSharp.Core.Util.JavaHelper;
namespace GitScc
{
public class GitFileStatusTracker : IDisposable
{
private string initFolder;
private Repository repository;
private Tree commitTree;
private GitIndex index;
//private IgnoreHandler ignoreHandler;
private Dictionary<string, GitFileStatus> cache;
public GitFileStatusTracker(string workingFolder)
{
cache = new Dictionary<string, GitFileStatus>();
this.initFolder = workingFolder;
Refresh();
}
public void Refresh()
{
cache.Clear();
if (!string.IsNullOrEmpty(initFolder))
{
try
{
this.repository = Repository.Open(initFolder);
if (this.repository != null)
{
var id = repository.Resolve("HEAD");
var commit = repository.MapCommit(id);
this.commitTree = (commit != null ? commit.TreeEntry : new Tree(repository));
this.index = repository.Index;
this.index.RereadIfNecessary();
//this.ignoreHandler = new IgnoreHandler(repository);
//this.watcher = new FileSystemWatcher(this.repository.WorkingDirectory.FullName);
}
}
catch (Exception ex)
{
}
}
}
public void Dispose()
{
if (this.repository != null) this.repository.Close();
}
public string GitWorkingDirectory
{
get
{
return this.repository == null ? null :
this.repository.WorkingDirectory.FullName;
}
}
public bool HasGitRepository
{
get { return this.repository != null; }
}
public GitFileStatus GetFileStatus(string fileName)
{
if (!HasGitRepository || string.IsNullOrEmpty(fileName))
return GitFileStatus.NotControlled;
if (!cache.ContainsKey(fileName))
{
var status = GetFileStatusNoCache(fileName);
cache.Add(fileName, status);
//Debug.WriteLine(string.Format("GetFileStatus {0} - {1}", fileName, status));
return status;
}
else
{
return cache[fileName];
}
}
private GitFileStatus GetFileStatusNoCache(string fileName)
{
var fileNameRel = GetRelativeFileName(fileName);
TreeEntry treeEntry = this.commitTree.FindBlobMember(fileNameRel);
GitIndex.Entry indexEntry = this.index.GetEntry(fileNameRel);
//the order of 'if' below is important
if (indexEntry != null)
{
if (treeEntry == null)
{
return GitFileStatus.Added;
}
if (treeEntry != null && !treeEntry.Id.Equals(indexEntry.ObjectId))
{
return GitFileStatus.Staged;
}
if (!File.Exists(fileName))
{
return GitFileStatus.Missing;
}
if (File.Exists(fileName) && indexEntry.IsModified(repository.WorkingDirectory, true))
{
return GitFileStatus.Modified;
}
if (indexEntry.Stage != 0)
{
return GitFileStatus.MergeConflict;
}
if (treeEntry != null && treeEntry.Id.Equals(indexEntry.ObjectId))
{
return GitFileStatus.Tracked;
}
}
else // <-- index entry == null
{
if (treeEntry != null && !(treeEntry is Tree))
{
return GitFileStatus.Removed;
}
if (File.Exists(fileName))
{
//remove the ingore check for better performance
//if (this.ignoreHandler.IsIgnored(fileName))
//{
// return GitFileStatus.Ignored;
//}
return GitFileStatus.New;
}
}
return GitFileStatus.NotControlled;
}
private string GetRelativeFileName(string fileName)
{
Uri workingFolderUri = new Uri(repository.WorkingDirectory.FullName + "\\");
fileName = workingFolderUri.MakeRelativeUri(new Uri(fileName)).ToString();
fileName = fileName.Replace("%20", " ");
return fileName;
}
public byte[] GetFileContent(string fileName)
{
if (!HasGitRepository || string.IsNullOrEmpty(fileName))
return null;
fileName = GetRelativeFileName(fileName);
var entry = commitTree.FindBlobMember(fileName);
if (entry != null)
{
var blob = repository.OpenBlob(entry.Id);
if (blob != null) return blob.CachedBytes;
}
return null;
}
public string CurrentBranch
{
get
{
return this.HasGitRepository ? this.repository.getBranch() : "";
}
}
internal static string GetRepositoryDirectory(string folder)
{
var repository = (!string.IsNullOrEmpty(folder) && Directory.Exists(folder)) ?
Repository.Open(folder) : null;
return repository == null ? null : repository.WorkingDirectory.FullName;
}
public override string ToString()
{
return repository == null ? "[no repo]" : repository.WorkingDirectory.FullName;
}
public System.Collections.IEnumerable ChangedFiles { get; set; }
public void UnStageFile(string fileName)
{
if (!this.HasGitRepository) return;
this.index.RereadIfNecessary();
var content = GetFileContent(fileName);
fileName = GetRelativeFileName(fileName);
if (content == null)
{
this.index.remove(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName));
}
else
{
this.index.add(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName),
content);
}
this.index.write();
}
public void StageFile(string fileName)
{
if (!this.HasGitRepository) return;
this.index.RereadIfNecessary();
var content = File.ReadAllBytes(fileName);
this.index.add(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName),
content);
this.index.write();
}
public void RemoveFile(string fileName)
{
if (!this.HasGitRepository) return;
this.index.RereadIfNecessary();
this.index.remove(
new DirectoryInfo(this.repository.WorkingDirectory.FullName),
new FileInfo(fileName));
this.index.write();
}
public string DiffFile(string fileName)
{
if (!this.HasGitRepository) return null;
throw new NotImplementedException();
}
public void Commit(string message)
{
if (!this.HasGitRepository) return;
if (string.IsNullOrEmpty(message))
throw new ArgumentException("Commit message must not be null or empty!", "message");
this.index.RereadIfNecessary();
var tree_id = this.index.writeTree();
var parent = repository.MapCommit(repository.Resolve("HEAD"));
if ((parent == null && this.index.Members.Count == 0) || (parent != null && parent.TreeId == tree_id))
throw new InvalidOperationException("There are no changes to commit");
var corecommit = new Commit(this.repository);
corecommit.ParentIds = parent == null ? new ObjectId[0] : new ObjectId[] { parent.TreeId };
var time = DateTimeOffset.Now;
corecommit.Author =
corecommit.Committer = new PersonIdent(this["user.name"], this["user.email"], time.ToMillisecondsSinceEpoch(), (int)time.Offset.TotalMinutes);
corecommit.Message = message;
corecommit.TreeEntry = this.repository.MapTree(tree_id);
corecommit.Encoding = Charset.forName(this["i18n.commitencoding"] ?? "utf-8");
corecommit.Save();
var updateRef = this.repository.UpdateRef("HEAD");
updateRef.NewObjectId = corecommit.CommitId;
updateRef.IsForceUpdate = true;
updateRef.update();
Refresh();
}
public static void Init(string folderName)
{
var gitFolder = Path.Combine(folderName, Constants.DOT_GIT);
var repo = new Repository(new DirectoryInfo(gitFolder));
repo.Create(false);
}
public string this[string key]
{
get
{
if (!this.HasGitRepository) return null;
var config = this.repository.Config;
var token = key.Split('.');
if (token.Count() == 2)
{
return config.getString(token[0], null, token[1]);
}
if (token.Count() == 3)
{
return config.getString(token[0], token[1], token[2]);
}
return null;
}
set
{
if (!this.HasGitRepository) return;
var config = this.repository.Config;
var token = key.Split('.');
if (token.Count() == 2)
config.setString(token[0], null, token[1], value);
else if (token.Count() == 3)
config.setString(token[0], token[1], token[2], value);
}
}
}
}