GCC Code Coverage Report


Directory: ./
File: src/prompt/git_repository.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 0 33 0.0%
Functions: 0 3 0.0%
Branches: 0 10 0.0%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the git_repository functions for the prompt
6 */
7 /**
8 * @file git_repository.c
9 * @brief The file containing the git_repository functions for the prompt
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Display the current branch
16 * @return <b>void</b>
17 */
18 static void current_branch(void)
19 {
20 char *buff;
21 char **tab;
22 struct stat st;
23 int fd = open(".git/HEAD", O_RDONLY);
24
25 if (fd == -1)
26 return;
27 stat(".git/HEAD", &st);
28 buff = malloc(sizeof(char) * (st.st_size + 1));
29 read(fd, buff, st.st_size);
30 buff[st.st_size] = '\0';
31 tab = my_str_to_word_array_select(buff, "/: \n");
32 my_printf("\033[1m\033[32m%s\033[0m", tab[3]);
33 free(buff);
34 my_free_array((void *)tab);
35 close(fd);
36 }
37
38 /**
39 * @brief Change the path to the previous folder
40 * @param path The path to change
41 * @return <b>void</b>
42 */
43 static void previous_folder(char *path)
44 {
45 for (int index = my_strlen(path) - 1; index >= 0; index--) {
46 if (path[index] == '/') {
47 path[index] = '\0';
48 break;
49 }
50 }
51 }
52
53 /**
54 * @brief Check if we are in a git repository and display the branch
55 * @return <b>void</b>
56 */
57 void is_git_repository(void)
58 {
59 char *pwd = getcwd(NULL, 0);
60 char *path = my_strdup(pwd);
61
62 while (my_str_contains(path, "/")) {
63 if (access(".git", X_OK) == 0) {
64 my_printf("\033[32m 🐱\033[0m");
65 current_branch();
66 break;
67 }
68 previous_folder(path);
69 chdir(path);
70 }
71 chdir(pwd);
72 FREE(pwd);
73 FREE(path);
74 }
75