-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
120 lines (104 loc) · 5.28 KB
/
Program.cs
File metadata and controls
120 lines (104 loc) · 5.28 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
using System;
using System.IO;
using System.Runtime.InteropServices;
using LibGit2Sharp;
using LibGit2Sharp.Handlers;
namespace UseLibgit2sharp
{
class Program
{
// when using the http(s) repository transport, libgit2sharp uses the
// ManagedHttpSmartSubtransport class, which uses the .net http(s)
// stack (e.g. HttpClient). to accept a certificate issued by a private
// ca you must add it to the os/system certificate store.
// NB ServicePointManager.ServerCertificateValidationCallback no longer
// works in .NET 5. instead we could have used
// HttpClientHandler.ServerCertificateCustomValidationCallback, but
// thats not possible to do with libgit2sharp and at the PR 1618
// comments, it seems there is no desire to expose this kind of
// customization to the library user.
// see https://docs.microsoft.com/en-us/dotnet/framework/network-programming/managing-connections
// see https://github.com/libgit2/libgit2sharp/pull/1618
static void Main(string[] args)
{
var repositoryUrl = Environment.GetEnvironmentVariable("GIT_REPOSITORY_URL") ?? "http://localhost:3000/jane.doe/test.git";
var userName = Environment.GetEnvironmentVariable("GIT_USER_NAME") ?? "Jane Doe";
var userEmail = Environment.GetEnvironmentVariable("GIT_USER_EMAIL") ?? "jane.doe@example.com";
var userUsername = Environment.GetEnvironmentVariable("GIT_USER_USERNAME") ?? "jane.doe";
var userPassword = Environment.GetEnvironmentVariable("GIT_USER_PASSWORD") ?? "password";
Console.WriteLine($"Using libgit2sharp {GlobalSettings.Version}");
// create the temporary directory that will host our local test repository.
Console.WriteLine("Deleting the tmp directory...");
DeleteDirectory("tmp");
Directory.CreateDirectory("tmp");
// initialize a test repository.
Console.WriteLine("Initializing the tmp/test repository...");
var testRepositoryPath = Repository.Init(@"tmp/test");
// create sample content.
Console.WriteLine("Creating the example content...");
File.WriteAllText("tmp/test/message.txt", "Hello World");
// add sample content to the test repository and push it to origin.
using (var repo = new Repository(testRepositoryPath))
{
// stage all the working directory changes.
Console.WriteLine("Staging...");
Commands.Stage(repo, "*");
// commit the staged changes.
Console.WriteLine("Committing...");
var author = new Signature(userName, userEmail, DateTimeOffset.Now);
var committer = author;
var commit = repo.Commit("hello world", author, committer);
// add the origin remote that points to the test remote repository.
var remote = repo.Network.Remotes.Add("origin", repositoryUrl);
// push the master branch to the origin remote repository.
Console.WriteLine("Pushing...");
var pushOptions = new PushOptions
{
CredentialsProvider = new CredentialsHandler(
(url, usernameFromUrl, types) =>
new UsernamePasswordCredentials()
{
Username = userUsername,
Password = userPassword,
}
),
};
repo.Network.Push(remote, @"refs/heads/master", pushOptions);
}
}
// recursively force the deletion of the given directory.
// NB on windows, because the git repository files are read-only,
// Directory.Delete will fail with UnauthorizedAccessException,
// so, before deleting a file, we have to remove its read-only
// attribute.
private static void DeleteDirectory(string path)
{
if (!Directory.Exists(path))
{
return;
}
// on non-Windows use the regular Directory.Delete because they
// do not care about the file permissions. to delete a file,
// only the parent directory permissions matter.
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Directory.Delete(path, true);
return;
}
foreach (var directoryPath in Directory.GetDirectories(path))
{
DeleteDirectory(directoryPath);
}
foreach (var filePath in Directory.GetFiles(path))
{
var fileAttributes = File.GetAttributes(filePath);
if ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
File.SetAttributes(filePath, fileAttributes ^ FileAttributes.ReadOnly);
}
File.Delete(filePath);
}
Directory.Delete(path);
}
}
}