/
log.c
48 lines (41 loc) · 1 KB
/
log.c
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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "commands.h"
#include "commit.h"
#include "object.h"
#include "path.h"
void print_log(char *hash) {
if (!hash) {
return;
}
print_object(hash);
printf("\n");
print_log(get_parent(hash));
}
int cmd_log(int argc, const char *argv[]) {
die_if_not_troll_repo();
char *path = get_repo_troll_dir();
path = (char *) realloc(path, strlen(path) + 5);
strcat(path, "HEAD");
int fd = open(path, O_RDONLY);
char *hash = (char *) malloc(41 * sizeof(char));
int test;
test = read(fd, hash, 40);
if(!test)
return -1;
printf("%s\n", hash);
if (strcmp(hash, "master") == 0) {
char *path2 = get_repo_troll_dir();
path2 = (char *) realloc(path2, strlen(path2) + 7);
strcat(path2, "master");
int fd = open(path2, O_RDONLY);
read(fd, hash, 40);
}
print_log(hash);
free(hash);
close(fd);
return 0;
}