diff --git a/.github/workflows/check-format.yml b/.github/workflows/check-format.yml new file mode 100644 index 0000000..f91411a --- /dev/null +++ b/.github/workflows/check-format.yml @@ -0,0 +1,22 @@ +name: clang-format Check +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + clang-format: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Install clang-format + run: sudo apt-get install -y clang-format + + - name: Check code formatting + id: check_formatting + run: ./scripts/clang-check-format.sh + shell: bash diff --git a/.github/workflows/makefile.yml b/.github/workflows/makefile.yml new file mode 100644 index 0000000..5348f5c --- /dev/null +++ b/.github/workflows/makefile.yml @@ -0,0 +1,22 @@ +name: Makefile CI + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Run make + run: make + + - name: Clean + run: make clean + diff --git a/Makefile b/Makefile index a0ac884..0d7f8f6 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,7 @@ debug: $(TARGET) release: CFLAGS += $(CFLAGS_RELEASE) release: LDFLAGS += $(LDFLAGS_RELEASE) +release: clean release: $(TARGET) $(TARGET): $(OBJS) | $(BIN_DIR) @@ -46,10 +47,11 @@ clean: rm -rf $(OBJ_DIR) $(BIN_DIR) format: - clang-format -i $(PROJECT_DIR)/*.c $(PROJECT_DIR)/include/*.h + clang-format -i $(SRCS) $(PROJECT_DIR)/include/**.h install: release install -m 755 $(TARGET) /usr/local/bin + make clean uninstall: rm -f /usr/local/bin/$(notdir $(TARGET)) diff --git a/include/git/lgit.h b/include/git/lgit.h index 549caaa..66d980d 100644 --- a/include/git/lgit.h +++ b/include/git/lgit.h @@ -3,18 +3,18 @@ #include "../../include/utils.h" -#include #include +#include typedef struct { char *name; - unsigned tracked:1; + unsigned tracked : 1; - mode_t mode; + mode_t mode; } lgit_entry; -typedef struct +typedef struct { char **lgit_entry; } lgit_entries; diff --git a/include/git/lgit_utils.h b/include/git/lgit_utils.h index 09ab719..d3be41b 100644 --- a/include/git/lgit_utils.h +++ b/include/git/lgit_utils.h @@ -1,10 +1,10 @@ #ifndef LASER_GIT_UTILS_H #define LASER_GIT_UTILS_H +#include #include #include #include -#include uint32_t lgit_utils_hexToUint32(const char *hex); uint32_t lgit_utils_binToUint32(const char *bin); diff --git a/include/laser.h b/include/laser.h index 5172e01..b4a2a2c 100644 --- a/include/laser.h +++ b/include/laser.h @@ -2,17 +2,19 @@ #define LASER_LASER_H #include "colors.h" -#include "utils.h" #include "sort.h" +#include "utils.h" #include +#include #include #include #include -#include #define MAX_ENTRIES 1024 +#define PATH_MAX 1024 void laser_list_directory(laser_opts opts, int depth); -void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last); +void laser_print_entry(const char *name, const char *color, char *indent, + int depth, int is_last); #endif diff --git a/include/utils.h b/include/utils.h index 9b61d7b..64cbaf5 100644 --- a/include/utils.h +++ b/include/utils.h @@ -4,10 +4,10 @@ #include #include #include -#include -#include #include #include +#include +#include typedef struct laser_opts { @@ -24,7 +24,8 @@ typedef struct laser_opts laser_opts laser_utils_parsecmd(int argc, char **argv); //void laser_utils_format_date(time_t time, char *buffer, size_t buffer_size); -char **laser_utils_grow_strarray(char **array, size_t *alloc_size, size_t count); +char **laser_utils_grow_strarray(char **array, size_t *alloc_size, + size_t count); int laser_string_in_sorted_array(char *target, char **array, int size); int laser_cmp_string(const void *a, const void *b, const void *arg); void laser_swap(void *a, void *b, size_t size); diff --git a/scripts/clang-check-format.sh b/scripts/clang-check-format.sh new file mode 100755 index 0000000..36145e3 --- /dev/null +++ b/scripts/clang-check-format.sh @@ -0,0 +1,8 @@ + files=$(find . -name '*.c' -o -name '*.h') + for file in $files; do + clang-format -style=file $file | diff $file - >/dev/null + if [ $? -ne 0 ]; then \ + echo "::error file=$file::Code not formatted according to clang-format" + exit 1 + fi + done diff --git a/src/filetypes/archives.c b/src/filetypes/archives.c index a604599..03fd6f3 100644 --- a/src/filetypes/archives.c +++ b/src/filetypes/archives.c @@ -7,5 +7,4 @@ const struct laser_magicnumber laser_archiveformats[] = { {(unsigned char[]){0x42, 0x5A, 0x68}, 3}, // BZIP2 {(unsigned char[]){0xFD, 0x37, 0x7A, 0x58, 0x5A}, 5}, // XZ // ADD MORE ARCHIVES - {NULL, 0} -}; + {NULL, 0}}; diff --git a/src/filetypes/checktype.c b/src/filetypes/checktype.c index 69abbc2..7e43f2a 100644 --- a/src/filetypes/checktype.c +++ b/src/filetypes/checktype.c @@ -32,7 +32,8 @@ int laser_checktype(const char *filename, while (formats[i].magic_size != 0) { if (bytesRead >= formats[i].magic_size && - memcmp(buffer, formats[i].magic, formats[i].magic_size) == 0) + memcmp(buffer, formats[i].magic, formats[i].magic_size) == + 0) ///asdjaklsdjalksdj return 1; i++; } diff --git a/src/filetypes/medias.c b/src/filetypes/medias.c index 9f35551..9fbe195 100644 --- a/src/filetypes/medias.c +++ b/src/filetypes/medias.c @@ -8,5 +8,4 @@ const struct laser_magicnumber laser_mediaformats[] = { {(unsigned char[]){0x66, 0x74, 0x79, 0x70, 0x4D, 0x53, 0x4E, 0x56}, 3}, // MP4 // ADD MORE MEDIA TYPES - {NULL, 0} -}; + {NULL, 0}}; diff --git a/src/git/lgit.c b/src/git/lgit.c index 257aaec..752e43d 100644 --- a/src/git/lgit.c +++ b/src/git/lgit.c @@ -8,10 +8,7 @@ char **lgit_parseGitignore(const char *dir, int *count) { - size_t filename_len = - strlen(dir) + strlen("/.gitignore") + - 1; - + size_t filename_len = strlen(dir) + strlen("/.gitignore") + 1; char *filename = malloc(filename_len); snprintf(filename, filename_len, "%s/.gitignore", dir); @@ -69,51 +66,51 @@ char **lgit_parseGitignore(const char *dir, int *count) // lgit_entries *lgit_getGitEntries(laser_opts opts) // { // lgit_entries *entries = malloc(sizeof(lgit_entries)); -// +// // lgit_parseGit(opts.parentDir); -// +// // return entries; // } -// +// // void lgit_parseGit(char *dir) // { // // TODO: check if git dir exists!! // char *index_file = malloc(strlen(dir) + strlen("/.git/index") + 1); -// +// // snprintf(index_file, strlen(dir) + strlen("/.git/index") + 1, // "%s/.git/index", dir); -// +// // size_t idxfsize; // char *idx_bincontent = lgit_utils_readbin(index_file, &idxfsize); // char *idx_file_as_hex = lgit_utils_binToHex(idx_bincontent, idxfsize); -// +// // // https://git-scm.com/docs/index-format // // HEADER // // first 4 bytes DIRC // char HEADER_SIGNATURE[4] = {'D', 'I', 'R', 'C'}; -// +// // if (strcmp(lgit_utils_getChars(idx_bincontent, 0, 4 * 8), // HEADER_SIGNATURE) != 0) // { // fprintf(stderr, "lsr: %s: file is not formatted right!!\n", index_file); // return; // } -// +// // // next 4 bytes: version // // 4 bytes = 8 hex nums // uint32_t version = // lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 8, 8)); -// +// // // then 32 bit num for index entries // // 32 bit 4 byte // uint32_t entries_num = // lgit_utils_hexToUint32(lgit_utils_getChars(idx_file_as_hex, 16, 8)); -// +// // printf("version: %u, entries_count: %u\n", version, entries_num); // } -// +// // void lgit_entries_free(lgit_entries *lgit) // { // free(lgit); // } -// +// diff --git a/src/laser.c b/src/laser.c index f3aca6b..2844951 100644 --- a/src/laser.c +++ b/src/laser.c @@ -3,6 +3,8 @@ #include "git/lgit.h" #include "utils.h" +#define BRANCH_SIZE 8 + char *strip_parent_dir(const char *full_path, const char *parent_dir) { size_t parent_len = strlen(parent_dir); @@ -22,8 +24,8 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg) struct dirent *dirent_a = *(struct dirent **)a; struct dirent *dirent_b = *(struct dirent **)b; - char path_a[1024]; - char path_b[1024]; + char path_a[PATH_MAX]; + char path_b[PATH_MAX]; snprintf(path_a, sizeof(path_a), "%s/%s", dir_path, dirent_a->d_name); snprintf(path_b, sizeof(path_b), "%s/%s", dir_path, dirent_b->d_name); @@ -48,7 +50,7 @@ int laser_cmp_dirent(const void *a, const void *b, const void *arg) void laser_print_entry(const char *name, const char *color, char *indent, int depth, int is_last) { - char branch[8] = ""; + char branch[BRANCH_SIZE] = ""; if (depth > 0) strcpy(branch, is_last ? "└─ " : "├─ "); @@ -70,7 +72,7 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, struct dirent **entries = malloc(sizeof(struct dirent *)); int entry_count = 0; - char full_path[1024]; + char full_path[PATH_MAX]; while ((entry = readdir(dir)) != NULL) { snprintf(full_path, sizeof(full_path), "%s/%s", opts.dir, @@ -138,13 +140,13 @@ void laser_process_entries(laser_opts opts, int depth, char *indent, { if (S_ISLNK(file_stat.st_mode) && opts.show_symlinks) { - char symlink_target[1024]; + char symlink_target[PATH_MAX]; int len = readlink(full_path, symlink_target, sizeof(symlink_target) - 1); if (len != -1) { symlink_target[len] = '\0'; - char res_string[2048]; + char res_string[PATH_MAX * 2 + 4]; // 4 is " -> " snprintf(res_string, sizeof(res_string), "%s -> %s", entries[i]->d_name, symlink_target); laser_print_entry(res_string, SYMLINK_COLOR, indent, depth,