-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
150 lines (131 loc) · 5.67 KB
/
Program.cs
File metadata and controls
150 lines (131 loc) · 5.67 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace PushPull
{
static class Program
{
[DllImport("kernel32.dll")] static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll")] static extern bool AllocConsole();
[DllImport("user32.dll")] static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_RESTORE = 9;
[STAThread]
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls13;
// CLI push mode: PushPull.exe "Project" push [comment]
if (args.Length >= 2 && string.Equals(args[1], "push", StringComparison.OrdinalIgnoreCase))
{
string commitMessage = "PushPull update";
if (args.Length >= 3 && string.Equals(args[2], "comment", StringComparison.OrdinalIgnoreCase))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var dlg = new CommentDialog())
{
if (dlg.ShowDialog() != DialogResult.OK) Environment.Exit(0);
commitMessage = string.IsNullOrWhiteSpace(dlg.Comment) ? "PushPull update" : dlg.Comment;
}
}
if (!AttachConsole(-1)) AllocConsole();
var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true };
Console.SetOut(writer);
Environment.Exit(RunCliPush(args[0], commitMessage));
return;
}
// GUI mode - enforce single instance
bool createdNew;
var mutex = new Mutex(true, "PushPullForGitHub_SingleInstance", out createdNew);
if (!createdNew)
{
IntPtr hWnd = FindWindow(null, "PushPull for GitHub");
if (hWnd != IntPtr.Zero)
{
ShowWindow(hWnd, SW_RESTORE);
SetForegroundWindow(hWnd);
}
return;
}
// Optional project name: PushPull.exe "Project"
string startupProject = args.Length >= 1 ? args[0] : null;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm(startupProject));
mutex.ReleaseMutex();
}
static int RunCliPush(string projectName, string commitMessage)
{
var config = ConfigManager.Load();
if (string.IsNullOrWhiteSpace(config.Token))
{
Console.WriteLine("Error: no GitHub token configured. Open the app and set one via Options > Settings.");
return 1;
}
GfdProject project = null;
foreach (var p in config.Projects)
{
if (string.Equals(p.Name, projectName, StringComparison.OrdinalIgnoreCase) ||
string.Equals(p.ToString(), projectName, StringComparison.OrdinalIgnoreCase))
{
project = p;
break;
}
}
if (project == null)
{
Console.WriteLine("Error: project '" + projectName + "' not found.");
if (config.Projects.Count > 0)
{
Console.WriteLine("Known projects:");
foreach (var p in config.Projects) Console.WriteLine(" " + p);
}
return 1;
}
Console.WriteLine("Project: " + project);
Console.WriteLine("Fetching remote file list...");
Dictionary<string, GitHub.RemoteFile> remote;
try
{
remote = GitHub.GetRepoTree(config.Token, project.Owner, project.Repo, project.Branch);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
return 1;
}
var entries = SyncEngine.Compare(project, remote);
var toPush = entries.FindAll(e => e.Status == SyncStatus.LocalNewer || e.Status == SyncStatus.LocalOnly);
if (toPush.Count == 0)
{
Console.WriteLine("Nothing to push.");
return 0;
}
Console.WriteLine("Pushing " + toPush.Count + " file(s)...");
int done = 0, failed = 0;
foreach (var e in toPush)
{
string localPath = Path.Combine(project.LocalFolder, e.RelativePath.Replace('/', '\\'));
try
{
GitHub.UploadFile(config.Token, project.Owner, project.Repo, project.Branch,
e.RelativePath, localPath, e.ExistsRemotely ? e.RemoteSha : null, commitMessage);
Console.WriteLine(" OK " + e.RelativePath);
done++;
}
catch (Exception ex)
{
Console.WriteLine(" FAIL " + e.RelativePath + " (" + ex.Message + ")");
failed++;
}
}
Console.WriteLine(string.Format("Done: {0} pushed, {1} failed.", done, failed));
return done == 0 ? 1 : 0;
}
}
}