Skip to content

Commit df35159

Browse files
authored
Merge pull request #33 from CandyCoded/feature/git-status-init
[feat] Add button to status panel for creating new git repo with default gitignore
2 parents 7efbef6 + f9930f3 commit df35159

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/Git.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ public static async Task DiscardChanges(string path)
9999

100100
}
101101

102+
public static async Task Init()
103+
{
104+
105+
await GenerateProcess(GitPath, "init");
106+
107+
}
108+
109+
public static async Task<string> Status()
110+
{
111+
112+
var process = await GenerateProcess(GitPath, "status");
113+
114+
if (process?.StandardError.ReadLine() is string line && line.StartsWith("fatal: not a git repository"))
115+
{
116+
117+
throw new Exception("Path is not a git repository.");
118+
119+
}
120+
121+
return process?.StandardOutput.ReadToEnd();
122+
123+
}
124+
102125
public static async Task<string[]> UntrackedFiles()
103126
{
104127

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information.
2+
3+
#if UNITY_EDITOR
4+
using System.IO;
5+
using System.Net;
6+
using System.Threading.Tasks;
7+
8+
namespace CandyCoded.GitStatus
9+
{
10+
11+
public static class GitIgnore
12+
{
13+
14+
private const string URL = "https://raw.githubusercontent.com/github/gitignore/master/Unity.gitignore";
15+
16+
private const string FILENAME = ".gitignore";
17+
18+
public static async Task Create(string directory)
19+
{
20+
21+
var request = WebRequest.Create(URL);
22+
23+
var response = await request.GetResponseAsync();
24+
25+
var content = new MemoryStream();
26+
27+
using (var stream = response.GetResponseStream())
28+
{
29+
30+
await stream.CopyToAsync(content);
31+
32+
File.WriteAllBytes(Path.Combine(directory, FILENAME), content.ToArray());
33+
34+
}
35+
36+
}
37+
38+
}
39+
40+
}
41+
#endif

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitIgnore.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitStatus.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ namespace CandyCoded.GitStatus
1212
public static class GitStatus
1313
{
1414

15+
public static bool isGitRepo;
16+
17+
public static string status = "";
18+
1519
public static string branch = "HEAD";
1620

1721
public static string[] branches = { };
@@ -42,6 +46,23 @@ public static void Update()
4246
public static async void UpdateAsync()
4347
{
4448

49+
try
50+
{
51+
52+
status = await Git.Status();
53+
54+
isGitRepo = true;
55+
56+
}
57+
catch (Exception error)
58+
{
59+
60+
status = error.Message;
61+
62+
isGitRepo = false;
63+
64+
}
65+
4566
branch = await Git.Branch();
4667
branches = await Git.Branches();
4768
changedFiles = await Git.ChangedFiles();

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitStatusPanel.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#if UNITY_EDITOR
44
using System;
5+
using System.IO;
56
using System.Threading.Tasks;
67
using UnityEditor;
78
using UnityEngine;
@@ -23,6 +24,29 @@ public static void ShowWindow()
2324
private void OnGUI()
2425
{
2526

27+
if (!GitStatus.isGitRepo)
28+
{
29+
30+
if (GUILayout.Button("Initialize git repo"))
31+
{
32+
33+
Task.Run(async () =>
34+
{
35+
36+
await Git.Init();
37+
38+
await GitIgnore.Create(Directory.GetCurrentDirectory());
39+
40+
GitStatus.Update();
41+
42+
});
43+
44+
}
45+
46+
return;
47+
48+
}
49+
2650
GUILayout.Space(5);
2751

2852
var selectedBranch = Array.IndexOf(GitStatus.branches, GitStatus.branch);

0 commit comments

Comments
 (0)