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
|
using LibGit2Sharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;
namespace Tango.Git
{
public class GitRepositoryManager : IDisposable
{
private Repository _repo;
private String _pat;
private String _userEmail;
private String _localFolder;
public event EventHandler<TangoProgressChangedEventArgs<double>> Progress;
public GitRepositoryManager(String localFolder, String userEmail, String personalAccessToken)
{
_pat = personalAccessToken;
_userEmail = userEmail;
_localFolder = localFolder;
_repo = new Repository(_localFolder);
}
public void CreatePushTag(String name, String description, String userName)
{
var tag = _repo.ApplyTag(name, new Signature(userName, _userEmail, DateTime.Now), description);
_repo.Network.Push(_repo.Network.Remotes.First(), tag.CanonicalName, new PushOptions()
{
CredentialsProvider = new LibGit2Sharp.Handlers.CredentialsHandler(CredentialsHandlerMethod),
OnPushTransferProgress = new LibGit2Sharp.Handlers.PushTransferProgressHandler(PushTagProgressHandlerMethod)
});
}
public void Commit(String message)
{
ExecuteGitProcess($"commit -a -m \"{message}\"");
}
public void Pull()
{
var branch = _repo.Head.TrackedBranch;
ExecuteGitProcess($"pull origin {branch.FriendlyName}");
}
public void Push()
{
var branch = _repo.Head.TrackedBranch;
ExecuteGitProcess($"push origin {branch.FriendlyName}");
}
public void Sync()
{
Pull();
Push();
}
private bool PushTagProgressHandlerMethod(int current, int total, long bytes)
{
//TODO: Implement via TangoProgress & event...
RaiseProgress("Pushing Tag...", false, 0, total);
return true;
}
public List<GitCommit> GetOutgoingCommits()
{
List<GitCommit> commits = new List<GitCommit>();
var branch = _repo.Head.TrackedBranch;
if (branch.TrackingDetails.AheadBy != null)
{
foreach (var commit in _repo.Commits.Take(branch.TrackingDetails.AheadBy.Value))
{
commits.Add(new GitCommit()
{
Message = commit.Message
});
}
}
return commits;
}
public List<GitCommit> GetIncomingCommits()
{
List<GitCommit> commits = new List<GitCommit>();
var trackingBranch = _repo.Head.TrackedBranch;
var log = _repo.Commits.QueryBy(new CommitFilter { IncludeReachableFrom = trackingBranch.Tip.Id, ExcludeReachableFrom = _repo.Head.Tip.Id });
var count = log.Count();
foreach (var commit in log)
{
commits.Add(new GitCommit()
{
Message = commit.Message
});
}
return commits;
}
public List<GitFile> GetChanges()
{
List<GitFile> files = new List<GitFile>();
var status = _repo.RetrieveStatus();
foreach (var file in status.Added)
{
files.Add(new GitFile()
{
File = file.FilePath,
State = GitFileState.Added
});
}
foreach (var file in status.Modified)
{
files.Add(new GitFile()
{
File = file.FilePath,
State = GitFileState.Modified
});
}
return files;
}
private Credentials CredentialsHandlerMethod(string url, string usernameFromUrl, SupportedCredentialTypes types)
{
return new UsernamePasswordCredentials { Username = _userEmail, Password = _pat };
}
protected virtual void RaiseProgress(String message, bool isIndeterminate = true, double value = 0, double maximum = 100)
{
Progress?.Invoke(this, new TangoProgressChangedEventArgs<double>(new TangoProgress<double>()
{
IsIndeterminate = isIndeterminate,
Message = message,
Value = value,
Maximum = maximum
}));
}
private void ExecuteGitProcess(String args)
{
Core.Components.CmdCommand command = new Core.Components.CmdCommand("git", args);
command.WorkingDir = _localFolder;
command.Timeout = TimeSpan.FromSeconds(60);
var result = command.Run().Result;
}
public void Dispose()
{
_repo.Dispose();
}
}
}
|