Fix checkout bug

This commit is contained in:
womax
2024-06-05 19:30:57 +02:00
parent 0fb0039b7d
commit ff2d6f49aa
7 changed files with 44 additions and 28 deletions

View File

@@ -1,4 +1,6 @@
SRC := $(wildcard src/*.c)
OBJ := $(addsuffix .o, $(basename $(SRC)))
OBJ_DEST := $(addprefix build/, $(OBJ))
CFLAGS := -lcrypto -lm -lz
DEBUG ?= false
@@ -6,8 +8,15 @@ ifeq ($(DEBUG), true)
DEBUG_FLAG = -DDEBUG -ggdb
endif
all: $(SRC)
gcc -o build/cgit $(SRC) $(CFLAGS) $(DEBUG_FLAG)
all: $(OBJ_DEST)
gcc -o build/cgit $(OBJ_DEST) $(CFLAGS) $(DEBUG_FLAG)
build/%.o: %.c
@mkdir -p $(dir $@)
gcc -c $< -o $@ $(CFLAGS) $(DEBUG_FLAG)
clean:
@rm -rf build/*
run: all
build/cgit

View File

@@ -174,15 +174,15 @@ int diff_commit_with_working_tree(char *checksum, int for_print)
dump_tree(TMP"/a", &commit_tree);
#define DIFF_WT_BASE_CMD "diff -ru --exclude-from=.gitignore --color=%s "TMP"/a ./ > "LOCAL_REPO"/last.diff"
#define DIFF_WT_BASE_CMD "diff -ru%s --exclude-from=.gitignore --color=%s "TMP"/a ./ > "LOCAL_REPO"/last.diff"
char cmd[strlen(DIFF_WT_BASE_CMD) + 7];
char cmd[strlen(DIFF_WT_BASE_CMD) + 8];
if (for_print)
{
sprintf(cmd, DIFF_WT_BASE_CMD, "always");
sprintf(cmd, DIFF_WT_BASE_CMD, "N", "always");
} else
{
sprintf(cmd, DIFF_WT_BASE_CMD, "never");
sprintf(cmd, DIFF_WT_BASE_CMD, "", "never");
}
FILE *p = popen(cmd, "w");
@@ -209,7 +209,7 @@ int diff_commit(char* checksum_a, char* checksum_b, int for_print)
return OBJECT_DOES_NOT_EXIST;
}
struct commit commit_a, commit_b;
struct commit commit_a = {0}, commit_b = {0};
commit_from_object(&commit_a, &commit_a_obj);
commit_from_object(&commit_b, &commit_b_obj);

View File

@@ -379,12 +379,13 @@ int get_head_commit_checksum(char* checksum)
FILE *head_file = NULL;
head_file = fopen(HEAD_FILE, "r");
char head_path[head_size];
char head_path[head_size + 1];
memset(head_path, 0, head_size + 1);
fread(head_path, head_size, 1, head_file);
fclose(head_file);
struct stat buffer = {0};
if (stat(head_path, &buffer) != 0) return 0;
if (stat(head_path, &buffer) != 0) return NO_CURRENT_HEAD;
memset(checksum, 0, buffer.st_size + 1);
head_file = fopen(head_path, "r");
@@ -442,15 +443,19 @@ int branch_exist(char *branch)
if(!local_repo_exist() || !heads_dir_exist())
return REPO_NOT_INITIALIZED;
DIR *dir;
DIR *dir = opendir(HEADS_DIR);
struct dirent *ent;
if ((dir = opendir(HEADS_DIR)) != 0)
if (dir != NULL)
{
while((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, branch) == 0)
{
closedir(dir);
return 1;
}
}
closedir(dir);
} else
return FS_ERROR;
@@ -502,15 +507,16 @@ int checkout_branch(char *branch)
char branch_path[strlen(HEADS_DIR) + strlen(branch) + 2];
sprintf(branch_path, "%s/%s", HEADS_DIR, branch);
char commit_checksum[DIGEST_LENGTH * 2 + 1];
char commit_checksum[DIGEST_LENGTH * 2 + 1] = {0};
FILE *branch_head = fopen(branch_path, "r");
fread(commit_checksum, DIGEST_LENGTH * 2 + 1, 1, branch_head);
fread(commit_checksum, DIGEST_LENGTH * 2, 1, branch_head);
fclose(branch_head);
debug_print("Checking out on %s", commit_checksum);
reset_to(commit_checksum);
FILE *head_file = fopen(HEAD_FILE, "w");
fwrite(branch_path, DIGEST_LENGTH * 2 + 1, 1, head_file);
fprintf(head_file, "%s", branch_path);
fclose(head_file);
}
@@ -630,13 +636,17 @@ int dump_log()
{
struct object current_obj = {0};
get_last_commit(&current_obj);
if (current_obj.size == 0)
return 0;
struct commit current = {0};
commit_from_object(&current, &current_obj);
FILE *log_file = fopen(LOG_FILE, "w");
char checksum[DIGEST_LENGTH * 2 + 1];
hash_object(&current_obj, checksum);
fprintf(log_file, "commit %s\n", checksum);
fprintf(log_file, "commit %s HEAD\n", checksum);
fprintf(log_file, "Author: %s\n", current.author);
fprintf(log_file, "Tree: %s\n", current.tree);
fprintf(log_file, "\n");

View File

@@ -13,6 +13,11 @@
#define ARGS_MAX_SIZE 256
// TODO fix a bug during checkout that does not always appear in gdb
// Change branch comportement to avoid checking out at branch creation
// Change object format to be fully compatible with git (tree and commit)
// Add commit message
int print_help()
{
printf("Usage: cgit init\n");
@@ -107,7 +112,7 @@ int remove_cmd(int argc, char **argv)
int commit_cmd(int argc, char **argv)
{
if (commit() == REPO_NOT_INITIALIZED)
if (commit("") == REPO_NOT_INITIALIZED)
{
printf("Not a cgit repository\n");
return 128;
@@ -154,7 +159,7 @@ int diff(int argc, char **argv)
int checkout(int argc, char **argv)
{
char buf[ARGS_MAX_SIZE];
char buf[ARGS_MAX_SIZE] = {0};
if(pop_arg(&argc, &argv, buf) == 1)
{
@@ -162,6 +167,7 @@ int checkout(int argc, char **argv)
return 0;
}
debug_print("Checking out on %s", buf);
if (checkout_branch(buf) == BRANCH_DOES_NOT_EXIST)
{
printf("Branch %s does not exist, use cgit branch <name> to create one\n", buf);

View File

@@ -135,13 +135,4 @@ void free_object(struct object *obj)
{
if (obj->content != NULL)
free(obj->content);
}
// int main(void)
// {
// char result[SHA_DIGEST_LENGTH * 2] = {0};
// char* str = "Hello, world!\n";
// hash_object(str, strlen(str), "blob", result);
// printf("%s\n", result);
// }
}

View File

@@ -13,7 +13,6 @@
void free_tree(struct tree *index);
struct entry *find_entry(struct tree *index, char* filename);
void get_tree(char* content, struct tree *tree);
int add_to_tree(struct tree *tree, struct object *object, char *filename);
int add_to_index(struct tree *index, char *filename);
int remove_from_index(struct tree *index, char *filename, int delete);
int tree_to_object(struct tree *tree, struct object *object);

View File

@@ -36,6 +36,7 @@ struct commit
char *tree;
char *parent;
char *author;
char *message;
};
#endif // TYPES_H