feat(cli): added init command to create a merk repository in the local directory
This commit is contained in:
parent
c8b4d93c89
commit
8b862f8b87
1 changed files with 38 additions and 9 deletions
45
src/main.c
45
src/main.c
|
|
@ -1,15 +1,17 @@
|
||||||
#include "action_list.h"
|
|
||||||
#include "file.h"
|
|
||||||
#include "myers.h"
|
|
||||||
#include "utilities.h"
|
|
||||||
// main.c
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "action_list.h"
|
||||||
|
#include "file.h"
|
||||||
|
#include "myers.h"
|
||||||
|
#include "utilities.h"
|
||||||
|
|
||||||
static void usage(int exitcode) {
|
static void usage(int exitcode) {
|
||||||
printf("usage: merk <command> [<args>]\n\n diff Displays the difference between the given two files\n");
|
printf("usage: merk <command> [<args>]\n\n init Initializes a repository in the current directory\n diff Displays the difference between the given two files\n");
|
||||||
exit(exitcode);
|
exit(exitcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -20,12 +22,38 @@ int main(int argc, char **argv) {
|
||||||
|
|
||||||
const char* subcmd = argv[1];
|
const char* subcmd = argv[1];
|
||||||
|
|
||||||
if (strcmp(subcmd, "diff") != 0) {
|
if (strcmp(subcmd, "diff") != 0 && strcmp(subcmd, "init") != 0) {
|
||||||
fprintf(stderr, "ERROR: Unknown subcommand: '%s'\n", subcmd);
|
fprintf(stderr, "ERROR: Unknown subcommand: '%s'\n", subcmd);
|
||||||
usage(2);
|
usage(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(subcmd, "diff") == 0) {
|
// Initializes a merk repository in the current directory
|
||||||
|
if (strcmp(subcmd, "init") == 0) {
|
||||||
|
if (argc != 2) {
|
||||||
|
printf("ERROR: too many agruments given!\n");
|
||||||
|
printf("Usage: merk init");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mkdir(".merk", 0700) == 0) {
|
||||||
|
printf("Initialized merk repository\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (errno == EEXIST) {
|
||||||
|
struct stat st;
|
||||||
|
if (stat(".merk", &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||||
|
printf("This directory is already a merk repository\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mkdir(".merk/files", 0700);
|
||||||
|
mkdir(".merk/trees", 0700);
|
||||||
|
mkdir(".merk/branches", 0700);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prints out a visual representation of the diff of the files provided
|
||||||
|
else if (strcmp(subcmd, "diff") == 0) {
|
||||||
if (argc != 4) {
|
if (argc != 4) {
|
||||||
printf("ERROR: too many/too little arguments given!\n");
|
printf("ERROR: too many/too little arguments given!\n");
|
||||||
printf("Usage: merk diff <PATH> <PATH>");
|
printf("Usage: merk diff <PATH> <PATH>");
|
||||||
|
|
@ -40,6 +68,7 @@ int main(int argc, char **argv) {
|
||||||
default: file1 = NULL; printf("ERROR: unknown first path!");
|
default: file1 = NULL; printf("ERROR: unknown first path!");
|
||||||
}
|
}
|
||||||
if (!file1) exit(1);
|
if (!file1) exit(1);
|
||||||
|
|
||||||
File* file2;
|
File* file2;
|
||||||
switch (get_path_type(argv[3])) {
|
switch (get_path_type(argv[3])) {
|
||||||
case PT_FILE: file2 = new_file(argv[3]); break;
|
case PT_FILE: file2 = new_file(argv[3]); break;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue