6static inline bool run_process_no_window(
int n_args,
wchar_t *arg0, ...) {
7 PROCESS_INFORMATION pi = {0};
8 STARTUPINFO si = {.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES,
9 .wShowWindow = SW_HIDE};
11 wmemset(cmd, L
'\0', 32767);
16 size_t req_size = (size_t)_snwprintf(
nullptr, 0, L
"\"%ls\" ", arg0);
17 _snwprintf(cmd, req_size + 1, L
"\"%ls\" ", arg0);
20 for (index = 0; index < (n_args - 1); index++) {
21 cur = va_arg(args,
wchar_t *);
25 req_size = (size_t)_snwprintf(
nullptr, 0, L
"\"%ls\" ", cur);
26 _snwprintf(cmd + i, req_size + 1, L
"\"%ls\" ", cur);
34 debug_print(L
"Executing: '%ls'\n", cmd);
35 bool ret = CreateProcess(
36 nullptr, cmd,
nullptr,
nullptr,
false, CREATE_NO_WINDOW,
nullptr,
nullptr, &si, &pi);
38 WaitForSingleObject(pi.hProcess, INFINITE);
40 bool result = GetExitCodeProcess(pi.hProcess, &exit_code);
41 CloseHandle(pi.hProcess);
42 CloseHandle(pi.hThread);
44 debug_print(L
"Failed to get exit code.\n");
47 debug_print(L
"Exit code: %lu\n", exit_code);
48 return exit_code == 0;
53static inline bool has_git() {
54 return run_process_no_window(2, L
"git.exe", L
"--version");
57static inline bool dir_exists(
wchar_t *path) {
58 DWORD attrib = GetFileAttributes(path);
59 return attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY);
62bool git_commit(
const wchar_t *output_dir,
const wchar_t *deploy_key) {
64 wchar_t *cwd, *date_buf, *git_dir, *git_dir_arg, *message_buf, *ssh_command, *time_buf,
66 cwd = date_buf = git_dir = git_dir_arg = message_buf = ssh_command = time_buf = work_tree_arg =
69 debug_print(L
"Wanted to commit but git.exe is not in PATH or failed to run.\n");
72 debug_print(L
"Committing changes.\n");
73 size_t work_tree_arg_len = wcslen(output_dir) + wcslen(L
"--work-tree=") + 1;
74 work_tree_arg = calloc(work_tree_arg_len, WL);
78 wmemset(work_tree_arg, L
'\0', work_tree_arg_len);
79 _snwprintf(work_tree_arg, work_tree_arg_len, L
"--work-tree=%ls", output_dir);
80 size_t git_dir_len = wcslen(output_dir) + wcslen(L
"\\.git") + 1;
81 git_dir = calloc(git_dir_len, WL);
85 _snwprintf(git_dir, git_dir_len, L
"%ls\\.git", output_dir);
86 git_dir[git_dir_len - 1] = L
'\0';
87 if (!dir_exists(git_dir)) {
88 cwd = calloc(MAX_PATH, WL);
92 wmemset(cwd, L
'\0', MAX_PATH);
93 if (!_wgetcwd(cwd, MAX_PATH) || _wchdir(output_dir) != 0 ||
94 !run_process_no_window(3, L
"git.exe", L
"init", L
"--quiet") || _wchdir(cwd) != 0) {
98 size_t git_dir_arg_len = git_dir_len + wcslen(L
"--git-dir=") + 1;
99 git_dir_arg = calloc(git_dir_arg_len, WL);
103 wmemset(git_dir_arg, L
'\0', git_dir_arg_len);
104 _snwprintf(git_dir_arg, git_dir_arg_len, L
"--git-dir=%ls", git_dir);
105 git_dir_arg[git_dir_arg_len - 1] = L
'\0';
106 if (!run_process_no_window(5, L
"git.exe", git_dir_arg, work_tree_arg, L
"add", L
".")) {
109 size_t time_needed_size =
110 (size_t)GetTimeFormat(LOCALE_USER_DEFAULT, 0,
nullptr,
nullptr,
nullptr, 0);
111 if (!time_needed_size) {
114 time_buf = calloc(time_needed_size, WL);
118 if (!GetTimeFormat(LOCALE_USER_DEFAULT, 0,
nullptr,
nullptr, time_buf, (
int)time_needed_size)) {
121 size_t date_needed_size =
122 (size_t)GetDateFormat(LOCALE_USER_DEFAULT, 0,
nullptr,
nullptr,
nullptr, 0);
123 if (!date_needed_size) {
126 date_buf = calloc(date_needed_size, WL);
130 if (!GetDateFormat(LOCALE_USER_DEFAULT, 0,
nullptr,
nullptr, date_buf, (
int)date_needed_size)) {
134 wcslen(AUTOMATIC_COMMIT_MESSAGE_PREFIX) + 3 + time_needed_size + date_needed_size;
135 message_buf = calloc(needed_size, WL);
139 wmemset(message_buf, L
'\0', needed_size);
140 _snwprintf(message_buf,
143 AUTOMATIC_COMMIT_MESSAGE_PREFIX,
146 if (!run_process_no_window(10,
154 L
"--author=winprefs <winprefs@tat.sh>",
160 wchar_t full_deploy_key_path[MAX_PATH];
161 if (!_wfullpath(full_deploy_key_path, deploy_key, MAX_PATH)) {
164 debug_print(L
"Deploy key: %ls\n", full_deploy_key_path);
165 size_t ssh_command_len = 68 + wcslen(full_deploy_key_path) + 3;
166 ssh_command = calloc(ssh_command_len, WL);
170 wmemset(ssh_command, L
'\0', ssh_command_len);
171 _snwprintf(ssh_command,
173 L
"ssh -i %ls -F nul -o UserKnownHostsFile=nul -o StrictHostKeyChecking=no",
174 full_deploy_key_path);
175 if (!run_process_no_window(6,
184 wchar_t *branch_arg =
185 get_git_branch(git_dir_arg, git_dir_arg_len, work_tree_arg, work_tree_arg_len);
186 if (!run_process_no_window(10,