From 999a97899d67924dd66c5a632a12f8fe4201a9a7 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 28 Feb 2026 22:07:39 +0800 Subject: Create .cvn dir and empty index. --- .gitignore | 4 +++- Makefile | 2 +- main.c | 42 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index c803222..b398ecb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ -svs +cvn +.cvn/ + **/*.o **/*.out **/*.swp diff --git a/Makefile b/Makefile index 780931c..9104cae 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC = cc -TARGET = svs +TARGET = cvn SRC = main.c OBJ = $(SRC:.c=.o) diff --git a/main.c b/main.c index ddf094e..d9c3c53 100644 --- a/main.c +++ b/main.c @@ -1,8 +1,14 @@ +#include +#include #include #include #include -#define DEFAULT_BRANCH_NAME "master" +#include +#include + +#define REPO ".cvn" +#define INDEX "index" static inline void init(int argc, char *argv[]); @@ -35,22 +41,46 @@ int main(int argc, char *argv[]) return 0; } -void init(int argc, char *argv[]) +static inline void init(int argc, char *argv[]) { - int opt; char *branch; + int opt, repo_fd, idx_fd; optind = 1; - branch = DEFAULT_BRANCH_NAME; + branch = "master"; while ((opt = getopt(argc, argv, "b:")) != -1) { switch (opt) { case 'b': branch = optarg; break; - default: return; + default: + break; } } - printf("Initialized repository, branch: %s\n", branch); + + if (mkdir(REPO, 0755) == -1) { + if (errno != EEXIST) { + perror("Failed to create repository"); + return; + } + } + + if ((repo_fd = open(REPO, O_RDONLY | O_DIRECTORY)) == -1) { + perror("Failed to open repository"); + return; + } + + if ((idx_fd = openat(repo_fd, INDEX, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) { + if (errno != EEXIST) { + close(repo_fd); + perror("Failed to create index"); + return; + } + } + + close(idx_fd); + close(repo_fd); + printf("Initialized repository in %s\n", REPO); } -- cgit v1.2.3