Skip to content

Commit

Permalink
native/main: run git init if necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatsh committed Oct 22, 2023
1 parent e1674f3 commit b16716c
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion native/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#include "msvc.h"
#include "reg_command.h"

BOOL dir_exists(wchar_t *path) {
DWORD attrib = GetFileAttributes(path);
return (attrib != INVALID_FILE_ATTRIBUTES && (attrib & FILE_ATTRIBUTE_DIRECTORY));
}

// Based on https://stackoverflow.com/a/35658917/374110
wchar_t *get_git_branch(const wchar_t *git_dir_arg,
size_t git_dir_arg_len,
Expand Down Expand Up @@ -264,7 +269,29 @@ int save_preferences(bool commit,
}
wmemset(work_tree_arg, L'\0', work_tree_arg_len);
_snwprintf(work_tree_arg, work_tree_arg_len, L"--work-tree=%ls", output_dir);
size_t git_dir_arg_len = wcslen(output_dir) + wcslen(L"--git-dir=") + wcslen(L"\\.git") + 1;
size_t git_dir_len = wcslen(output_dir) + wcslen(L"\\.git") + 1;
wchar_t *git_dir = calloc(git_dir_len, WL);
if (!git_dir) {
abort();
}
_snwprintf(git_dir, git_dir_len, L"%ls\\.git", output_dir);
git_dir[git_dir_len - 1] = L'\0';
if (!dir_exists(git_dir)) {
wchar_t *cwd = calloc(MAX_PATH, WL);
if (!_wgetcwd(cwd, MAX_PATH)) {
abort();
}
if (_wchdir(output_dir) != 0) {
abort();
}
if (_wspawnlp(P_WAIT, L"git.exe", L"git", "init")) {
abort();
}
if (_wchdir(cwd) != 0) {
abort();
}
}
size_t git_dir_arg_len = git_dir_len + wcslen(L"--git-dir=") + 1;
wchar_t *git_dir_arg = calloc(git_dir_arg_len, WL);
if (!git_dir_arg) {
abort();
Expand Down

0 comments on commit b16716c

Please sign in to comment.