summaryrefslogtreecommitdiff
path: root/src/precheck.c
blob: cbabd2c1308c6229db9445eb683731b799e8d7ff (plain) (blame)
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
//
// Created by Anshul Gupta on 4/7/25.
//

#include "precheck.h"

#include <stdio.h>
#include <sys/errno.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>

/**
 * Check if git is installed and available in the PATH.
 * @return 1 if git is available, 0 if not.
 */
static int has_git(void)
{
	pid_t pid = fork();
	if (pid < 0) {
		perror("fork");
		return 0;
	}

	if (pid == 0) {
		// Child process
		char *args[] = {"git", "--version", NULL};
		execvp("git", args);
		_exit(127); // execvp only returns on error
	}

	// Parent process
	int status;
	pid_t result;
	do {
		result = waitpid(pid, &status, 0);
	} while (result == -1 && errno == EINTR);
	if (result == -1) {
		perror("waitpid");
		return 0;
	}

	// Check exit status
	if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
		return 1; // git is available
	}

	fprintf(stderr, "Error: git is not installed or not found in PATH\n");
	return 0; // git is not available
}

/**
 * Check if the git base directory exists.
 * @param path Path to the directory to check
 * @return 1 if the directory exists, 0 if not.
 */
static int git_base_exists(const char *path)
{
	struct stat st;
	if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
		return 1;
	}
	fprintf(stderr, "Error: git base directory does not exist: %s\n", path);
	return 0;
}

int precheck_self(const struct config *cfg)
{
	if (!has_git())
		return -1;
	if (!git_base_exists(cfg->git_base))
		return -1;
	return 0;
}