Command line
This commit is contained in:
285
src/main.c
285
src/main.c
@@ -1,46 +1,267 @@
|
||||
// Requirements
|
||||
// commit
|
||||
// branching
|
||||
// revert
|
||||
// pull/push
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "includes.h"
|
||||
#include "objects.h"
|
||||
#include "types.h"
|
||||
#include "commit.h"
|
||||
#include "fs.h"
|
||||
#include "tree.h"
|
||||
#include "commit.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
#define ARGS_MAX_SIZE 256
|
||||
|
||||
int print_help()
|
||||
{
|
||||
// struct tree index = {0};
|
||||
// load_index(&index);
|
||||
printf("Usage: cgit init\n");
|
||||
printf(" cgit add [FILES]\n");
|
||||
printf(" cgit remove [FILES]\n");
|
||||
printf(" cgit commit -m [MESSAGE]\n");
|
||||
printf(" cgit diff <COMMIT1> [COMMIT2]\n");
|
||||
printf(" cgit branch [BRANCH]\n");
|
||||
printf(" cgit checkout [BRANCH]\n");
|
||||
printf(" cgit reset <COMMIT>\n");
|
||||
printf(" cgit log\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// add_to_index(&index, "src/commit.c");
|
||||
// add_to_index(&index, "src/commit.h");
|
||||
// add_to_index(&index, "src/fs.c");
|
||||
// add_to_index(&index, "src/fs.h");
|
||||
int pop_arg(int *argc, char ***argv, char *buf)
|
||||
{
|
||||
if (*argc == 0)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
// save_index(&index);
|
||||
|
||||
// commit();
|
||||
|
||||
struct object a = {0}, b = {0};
|
||||
read_object("083fec39f677b81de863cb0b8575ac76e87b7bff", &a);
|
||||
read_object("fb5d38457520619b6e2f3b162c2a21a22b531cb4", &b);
|
||||
|
||||
struct commit c_a, c_b;
|
||||
commit_from_object(&c_a, &a);
|
||||
commit_from_object(&c_b, &b);
|
||||
|
||||
diff_commit_with_working_tree(&c_b);
|
||||
|
||||
free_object(&a);
|
||||
free_object(&b);
|
||||
strcpy(buf, *argv[0]);
|
||||
*argc -= 1;
|
||||
*argv += 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int add(int argc, char **argv)
|
||||
{
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
int res;
|
||||
|
||||
res = pop_arg(&argc, &argv, buf);
|
||||
if (res != 0)
|
||||
{
|
||||
printf("No file specified\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct tree index = {0};
|
||||
if (load_index(&index) == REPO_NOT_INITIALIZED)
|
||||
{
|
||||
printf("Not a cgit repository\n");
|
||||
return 128;
|
||||
}
|
||||
|
||||
do {
|
||||
if (add_file_to_index(&index, buf) == FILE_NOT_FOUND)
|
||||
{
|
||||
printf("File %s does not exist\n", buf);
|
||||
return 1;
|
||||
}
|
||||
res = pop_arg(&argc, &argv, buf);
|
||||
} while (res == 0);
|
||||
|
||||
save_index(&index);
|
||||
free_tree(&index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int remove_cmd(int argc, char **argv)
|
||||
{
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
int res;
|
||||
|
||||
res = pop_arg(&argc, &argv, buf);
|
||||
if (res != 0)
|
||||
{
|
||||
printf("No file specified\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct tree index = {0};
|
||||
if (load_index(&index) == REPO_NOT_INITIALIZED)
|
||||
{
|
||||
printf("Not a cgit repository\n");
|
||||
return 128;
|
||||
}
|
||||
|
||||
do {
|
||||
remove_file_from_index(&index, buf);
|
||||
res = pop_arg(&argc, &argv, buf);
|
||||
} while (res == 0);
|
||||
|
||||
save_index(&index);
|
||||
free_tree(&index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int commit_cmd(int argc, char **argv)
|
||||
{
|
||||
if (commit() == REPO_NOT_INITIALIZED)
|
||||
{
|
||||
printf("Not a cgit repository\n");
|
||||
return 128;
|
||||
}
|
||||
}
|
||||
|
||||
int diff(int argc, char **argv)
|
||||
{
|
||||
char checksum_a[ARGS_MAX_SIZE];
|
||||
char checksum_b[ARGS_MAX_SIZE];
|
||||
|
||||
if(pop_arg(&argc, &argv, checksum_a) == 1)
|
||||
{
|
||||
printf("No commit specified\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(pop_arg(&argc, &argv, checksum_b) == 0)
|
||||
{
|
||||
if (diff_commit(checksum_a, checksum_b, 1) == OBJECT_DOES_NOT_EXIST)
|
||||
{
|
||||
if(errno == 1)
|
||||
{
|
||||
printf("Could not find commit %s\n", checksum_a);
|
||||
} else if (errno == 2)
|
||||
{
|
||||
printf("Could not find commit %s\n", checksum_b);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
} else
|
||||
{
|
||||
if (diff_commit_with_working_tree(checksum_a, 1) == OBJECT_DOES_NOT_EXIST)
|
||||
{
|
||||
printf("Could not find commit %s\n", checksum_a);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
FILE *p = popen("cat "LOCAL_REPO"/last.diff | less -R", "w");
|
||||
pclose(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int checkout(int argc, char **argv)
|
||||
{
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
|
||||
if(pop_arg(&argc, &argv, buf) == 1)
|
||||
{
|
||||
printf("No branch name specified\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (checkout_branch(buf) == BRANCH_DOES_NOT_EXIST)
|
||||
{
|
||||
printf("Branch %s does not exist, use cgit branch <name> to create one\n", buf);
|
||||
}
|
||||
}
|
||||
|
||||
int reset(int argc, char **argv)
|
||||
{
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
|
||||
if(pop_arg(&argc, &argv, buf) == 1)
|
||||
{
|
||||
printf("You must give a commit to reset to\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int res = reset_to(buf);
|
||||
if (res == OBJECT_DOES_NOT_EXIST)
|
||||
{
|
||||
printf("There is no commit named %s\n", buf);
|
||||
} else if (res == WRONG_OBJECT_TYPE)
|
||||
{
|
||||
printf("Object %s is not a commit and thus cannot be reset to\n", buf);
|
||||
}
|
||||
}
|
||||
|
||||
int branch(int argc, char **argv)
|
||||
{
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
|
||||
if (pop_arg(&argc, &argv, buf) == 1)
|
||||
{
|
||||
if (dump_branches() == REPO_NOT_INITIALIZED)
|
||||
{
|
||||
printf("Not a cgit repository\n");
|
||||
return 128;
|
||||
}
|
||||
FILE *p = popen("cat "TMP"/branches | less", "w");
|
||||
pclose(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (new_branch(buf) == BRANCH_ALREADY_EXIST)
|
||||
{
|
||||
printf("Branch %s already exist\n", buf);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int log_cmd(int argc, char **argv)
|
||||
{
|
||||
dump_log();
|
||||
FILE *p = popen("cat "LOG_FILE" | less", "w");
|
||||
pclose(p);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char cmd[ARGS_MAX_SIZE];
|
||||
char buf[ARGS_MAX_SIZE];
|
||||
|
||||
pop_arg(&argc, &argv, cmd);
|
||||
if(pop_arg(&argc, &argv, buf) == 1)
|
||||
{
|
||||
print_help();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (strcmp(buf, "init") == 0)
|
||||
{
|
||||
return init_repo();
|
||||
} else if (strcmp(buf, "add") == 0)
|
||||
{
|
||||
return add(argc, argv);
|
||||
} else if (strcmp(buf, "remove") == 0)
|
||||
{
|
||||
return remove_cmd(argc, argv);
|
||||
} else if (strcmp(buf, "commit") == 0)
|
||||
{
|
||||
return commit_cmd(argc, argv);
|
||||
} else if (strcmp(buf, "diff") == 0)
|
||||
{
|
||||
return diff(argc, argv);
|
||||
} else if (strcmp(buf, "branch") == 0)
|
||||
{
|
||||
return branch(argc, argv);
|
||||
} else if (strcmp(buf, "checkout") == 0)
|
||||
{
|
||||
return checkout(argc, argv);
|
||||
} else if (strcmp(buf, "reset") == 0)
|
||||
{
|
||||
return reset(argc, argv);
|
||||
} else if (strcmp(buf, "log") == 0)
|
||||
{
|
||||
return log_cmd(argc, argv);
|
||||
} else if (strcmp(buf, "-h") == 0) {
|
||||
return print_help();
|
||||
} else {
|
||||
printf("Unknown command %s, try using %s -h\n", buf, cmd);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user