mainline.diff (7023B)
1 # Description: Add git status column to detail mode. Provides additional 2 # command line flag -G which will render the git status 3 # column also in normal mode. Vim plugin users may consider 4 # adding the -G flag to their command override. 5 # 6 # Authors: Luuk van Baal 7 8 diff --git a/src/nnn.c b/src/nnn.c 9 index 1028906a..c80314de 100644 10 --- a/src/nnn.c 11 +++ b/src/nnn.c 12 @@ -262,6 +262,25 @@ 13 #define FREE 0 14 #define CAPACITY 1 15 16 +/* Git icons */ 17 +#ifdef NERD 18 +#define GIT_ADD "" 19 +#define GIT_DEL "" 20 +#define GIT_IGN "" 21 +#define GIT_MOD "" 22 +#define GIT_NEW "" 23 +#define GIT_NON "-" 24 +#define GIT_UPD "ﮮ" 25 +#else 26 +#define GIT_ADD "A" 27 +#define GIT_DEL "D" 28 +#define GIT_IGN "!" 29 +#define GIT_MOD "M" 30 +#define GIT_NEW "?" 31 +#define GIT_NON "-" 32 +#define GIT_UPD "U" 33 +#endif 34 + 35 /* TYPE DEFINITIONS */ 36 typedef unsigned int uint_t; 37 typedef unsigned char uchar_t; 38 @@ -286,6 +305,7 @@ typedef struct entry { 39 uid_t uid; /* 4 bytes */ 40 gid_t gid; /* 4 bytes */ 41 #endif 42 + char git_status[2][5]; 43 } *pEntry; 44 45 /* Selection marker */ 46 @@ -342,6 +362,7 @@ typedef struct { 47 uint_t cliopener : 1; /* All-CLI app opener */ 48 uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */ 49 uint_t rollover : 1; /* Roll over at edges */ 50 + uint_t normalgit : 1; /* Show git status in normal mode */ 51 } settings; 52 53 /* Non-persistent program-internal states (alphabeical order) */ 54 @@ -391,7 +412,17 @@ typedef struct { 55 } session_header_t; 56 #endif 57 58 +typedef struct { 59 + char status[2]; 60 + char path[PATH_MAX]; 61 +} git_status_t; 62 + 63 /* GLOBALS */ 64 +struct { 65 + bool show; 66 + size_t len; 67 + git_status_t *statuses; 68 +} git_statuses; 69 70 /* Configuration, contexts */ 71 static settings cfg = { 72 @@ -422,6 +453,7 @@ static settings cfg = { 73 0, /* cliopener */ 74 0, /* waitedit */ 75 1, /* rollover */ 76 + 0, /* normalgit */ 77 }; 78 79 static context g_ctx[CTX_MAX] __attribute__ ((aligned)); 80 @@ -3839,6 +3871,56 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id) 81 return -1; 82 } 83 84 +static size_t get_git_statuses(const char *path) 85 +{ 86 + static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null"; 87 + char workdir[PATH_MAX], *ret; 88 + FILE *fp = popen(gitrev, "r"); 89 + 90 + git_statuses.show = FALSE; 91 + ret = fgets(workdir, PATH_MAX, fp); 92 + pclose(fp); 93 + if (!ret) 94 + return 0; 95 + 96 + static char gitstat[] = "git -c core.quotePath= status --porcelain --no-renames --ignored=matching -u "; 97 + char pathspec[PATH_MAX], status[PATH_MAX]; 98 + size_t i = -1; 99 + workdir[xstrlen(workdir) - 1] = '\0'; 100 + snprintf(pathspec, PATH_MAX, "%s\"%s\"%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*"); 101 + fp = popen(pathspec, "r"); 102 + 103 + while (fgets(status, PATH_MAX, fp)) { 104 + size_t pathindex = (status[3] == '"') ? 4 : 3; 105 + status[xstrlen(status) - pathindex + 2] = '\0'; 106 + git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1)); 107 + git_statuses.statuses[i].status[0] = status[0]; 108 + git_statuses.statuses[i].status[1] = status[1]; 109 + mkpath(workdir, status + pathindex, git_statuses.statuses[i].path); 110 + } 111 + 112 + pclose(fp); 113 + return (i + 1); 114 +} 115 + 116 +static void set_git_status(char status[][5], uint_t nr) 117 +{ 118 + for (int j = 0; j < 2; j++) { 119 + if (status[j][0] == '-') 120 + switch (git_statuses.statuses[nr].status[j]) { 121 + case ' ': xstrsncpy(status[j], GIT_NON, 4); break; 122 + case 'M': xstrsncpy(status[j], GIT_MOD, 4); break; 123 + case 'A': xstrsncpy(status[j], GIT_ADD, 4); break; 124 + case '?': xstrsncpy(status[j], GIT_NEW, 4); break; 125 + case '!': xstrsncpy(status[j], GIT_IGN, 4); break; 126 + case 'D': xstrsncpy(status[j], GIT_DEL, 4); break; 127 + case 'U': xstrsncpy(status[j], GIT_UPD, 4); break; 128 + } 129 + } 130 + if (git_statuses.statuses[nr].status[1] != '!') 131 + git_statuses.show = TRUE; 132 +} 133 + 134 static void resetdircolor(int flags) 135 { 136 /* Directories are always shown on top, clear the color when moving to first file */ 137 @@ -4176,6 +4258,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel) 138 139 uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs); 140 141 + if (git_statuses.show && (cfg.showdetail || cfg.normalgit)) 142 + printw("%*s%s%s", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "", 143 + ent->git_status[0], ent->git_status[1]); 144 + 145 addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' '); 146 147 if (g_state.oldcolor) 148 @@ -5617,6 +5703,11 @@ static int dentfill(char *path, struct entry **ppdents) 149 attron(COLOR_PAIR(cfg.curctx + 1)); 150 } 151 152 + char linkpath[PATH_MAX]; 153 + if ((git_statuses.len = get_git_statuses(path))) 154 + if (!realpath(path, linkpath)) 155 + printwarn(NULL); 156 + 157 #if _POSIX_C_SOURCE >= 200112L 158 posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); 159 #endif 160 @@ -5815,6 +5906,29 @@ static int dentfill(char *path, struct entry **ppdents) 161 #endif 162 } 163 164 + if (git_statuses.len) { 165 + char dentpath[PATH_MAX]; 166 + size_t pathlen = mkpath(linkpath, dentp->name, dentpath); 167 + dentp->git_status[0][0] = dentp->git_status[1][0] = '-'; 168 + dentp->git_status[0][1] = dentp->git_status[1][1] = '\0'; 169 + 170 + if (dentp->flags & DIR_OR_DIRLNK) { 171 + char prefix[PATH_MAX]; 172 + memccpy(prefix, dentpath, '\0', PATH_MAX); 173 + prefix[pathlen - 1] = '/'; 174 + 175 + for (size_t i = 0; i < git_statuses.len; ++i) 176 + if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) 177 + set_git_status(dentp->git_status, i); 178 + } else { 179 + for (size_t i = 0; i < git_statuses.len; ++i) 180 + if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) { 181 + set_git_status(dentp->git_status, i); 182 + break; 183 + } 184 + } 185 + } 186 + 187 ++ndents; 188 } while ((dp = readdir(dirp))); 189 190 @@ -6360,11 +6474,12 @@ static int adjust_cols(int n) 191 #endif 192 if (cfg.showdetail) { 193 /* Fallback to light mode if less than 35 columns */ 194 - if (n < 36) 195 + if (n < 38) 196 cfg.showdetail ^= 1; 197 else /* 2 more accounted for below */ 198 - n -= 32; 199 - } 200 + n -= (git_statuses.show ? 34 : 32); 201 + } else if (cfg.normalgit && git_statuses.show) 202 + n -= 3; 203 204 /* 2 columns for preceding space and indicator */ 205 return (n - 2); 206 @@ -8126,6 +8241,7 @@ static void usage(void) 207 " -F val fifo mode [0:preview 1:explore]\n" 208 #endif 209 " -g regex filters\n" 210 + " -G always show git status\n" 211 " -H show hidden files\n" 212 " -i show current file info\n" 213 " -J no auto-proceed on select\n" 214 @@ -8266,6 +8382,7 @@ static void cleanup(void) 215 free(hostname); 216 } 217 #endif 218 + free(git_statuses.statuses); 219 free(selpath); 220 free(plgpath); 221 free(cfgpath); 222 @@ -8310,7 +8427,7 @@ int main(int argc, char *argv[]) 223 224 while ((opt = (env_opts_id > 0 225 ? env_opts[--env_opts_id] 226 - : getopt(argc, argv, "aAb:cCdDeEfF:gHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) { 227 + : getopt(argc, argv, "aAb:cCdDeEfF:gGHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) { 228 switch (opt) { 229 #ifndef NOFIFO 230 case 'a': 231 @@ -8361,6 +8478,9 @@ int main(int argc, char *argv[]) 232 cfg.regex = 1; 233 filterfn = &visible_re; 234 break; 235 + case 'G': 236 + cfg.normalgit = 1; 237 + break; 238 case 'H': 239 cfg.showhidden = 1; 240 break;