commit dcd6abe6292f26d12b7947bae536ff581f74757c
parent 0a2e491aee5dffc32b4f06329a1e8e05c3fcfaf3
Author: Chris Bracken <chris@bracken.jp>
Date: Tue, 6 Jan 2026 16:34:59 +0900
Use a switch statement in format_filemode
Makes the code a little more readable in that it clarifies that we're
checking the file mode against a set of mutually-exclusive types.
Diffstat:
1 file changed, 23 insertions(+), 18 deletions(-)
diff --git a/src/git/repo.c b/src/git/repo.c
@@ -54,6 +54,7 @@ static size_t string_count_lines(const char* str, ssize_t size_bytes);
static bool string_ends_with(const char* str, const char* suffix);
static char* first_line_of_file(const char* path);
static const git_oid* oid_for_spec(git_repository* repo, const char* spec);
+static char get_filetype(git_filemode_t m);
static char* format_filemode(git_filemode_t m);
/* GitRepo utilities. */
@@ -124,29 +125,33 @@ const git_oid* oid_for_spec(git_repository* repo, const char* spec) {
return oid;
}
+char get_filetype(git_filemode_t m) {
+ switch (m & S_IFMT) {
+ case S_IFREG:
+ return '-';
+ case S_IFBLK:
+ return 'b';
+ case S_IFCHR:
+ return 'c';
+ case S_IFDIR:
+ return 'd';
+ case S_IFIFO:
+ return 'p';
+ case S_IFLNK:
+ return 'l';
+ case S_IFSOCK:
+ return 's';
+ default:
+ return '?';
+ }
+}
+
char* format_filemode(git_filemode_t m) {
char* mode = ecalloc(11, sizeof(char));
memset(mode, '-', 10);
mode[10] = '\0';
- if (S_ISREG(m)) {
- mode[0] = '-';
- } else if (S_ISBLK(m)) {
- mode[0] = 'b';
- } else if (S_ISCHR(m)) {
- mode[0] = 'c';
- } else if (S_ISDIR(m)) {
- mode[0] = 'd';
- } else if (S_ISFIFO(m)) {
- mode[0] = 'p';
- } else if (S_ISLNK(m)) {
- mode[0] = 'l';
- } else if (S_ISSOCK(m)) {
- mode[0] = 's';
- } else {
- mode[0] = '?';
- }
-
+ mode[0] = get_filetype(m);
if (m & S_IRUSR) {
mode[1] = 'r';
}