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 # Compatibility patch for the namefirst patch.
7 # Authors: Luuk van Baal
9 diff --git a/src/nnn.c b/src/nnn.c
10 index 88538787..d4af7c43 100644
36 /* TYPE DEFINITIONS */
37 typedef unsigned int uint_t;
38 typedef unsigned char uchar_t;
39 @@ -294,6 +313,7 @@ typedef struct entry {
40 uid_t uid; /* 4 bytes */
41 gid_t gid; /* 4 bytes */
43 + char git_status[2][5];
46 /* Selection marker */
47 @@ -349,6 +369,7 @@ typedef struct {
48 uint_t cliopener : 1; /* All-CLI app opener */
49 uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */
50 uint_t rollover : 1; /* Roll over at edges */
51 + uint_t normalgit : 1; /* Show git status in normal mode */
54 /* Non-persistent program-internal states (alphabeical order) */
55 @@ -404,7 +425,17 @@ static struct {
56 ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
61 + char path[PATH_MAX];
68 + git_status_t *statuses;
71 /* Configuration, contexts */
72 static settings cfg = {
73 @@ -3804,6 +3835,47 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
77 +static size_t get_git_statuses(const char *path)
79 + static char gst[] = "git -c core.quotePath= status -s --no-renames --ignored=matching -unormal . 2>/dev/null";
80 + FILE *fp = popen(gst, "r");
81 + char status[PATH_MAX];
82 + size_t pathindex, i = -1;
83 + git_statuses.show = FALSE;
85 + while (fgets(status, PATH_MAX, fp)) {
86 + pathindex = (status[3] == '"') ? 4 : 3;
87 + if (!cfg.showhidden && status[pathindex] == '.')
89 + status[xstrlen(status) - pathindex + 2] = '\0';
90 + git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1));
91 + git_statuses.statuses[i].status[0] = status[0];
92 + git_statuses.statuses[i].status[1] = status[1];
93 + mkpath(path, status + pathindex, git_statuses.statuses[i].path);
100 +static void set_git_status(char status[][5], uint_t nr)
102 + for (int j = 0; j < 2; j++) {
103 + if (status[j][0] == '-')
104 + switch (git_statuses.statuses[nr].status[j]) {
105 + case ' ': xstrsncpy(status[j], GIT_NON, 4); break;
106 + case 'M': xstrsncpy(status[j], GIT_MOD, 4); break;
107 + case 'A': xstrsncpy(status[j], GIT_ADD, 4); break;
108 + case '?': xstrsncpy(status[j], GIT_NEW, 4); break;
109 + case '!': xstrsncpy(status[j], GIT_IGN, 4); break;
110 + case 'D': xstrsncpy(status[j], GIT_DEL, 4); break;
111 + case 'U': xstrsncpy(status[j], GIT_UPD, 4); break;
114 + if (git_statuses.statuses[nr].status[1] != '!')
115 + git_statuses.show = TRUE;
118 static void resetdircolor(int flags)
120 /* Directories are always shown on top, clear the color when moving to first file */
121 @@ -4104,6 +4176,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
122 int attrs = 0, namelen;
123 uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
125 + if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
126 + printw(" %s%s", ent->git_status[0], ent->git_status[1]);
128 addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
130 if (g_state.oldcolor)
131 @@ -5598,6 +5673,11 @@ static int dentfill(char *path, struct entry **ppdents)
132 attron(COLOR_PAIR(cfg.curctx + 1));
135 + char linkpath[PATH_MAX];
136 + if ((git_statuses.len = get_git_statuses(path)))
137 + if (!realpath(path, linkpath))
140 #if _POSIX_C_SOURCE >= 200112L
141 posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
143 @@ -5798,6 +5878,29 @@ static int dentfill(char *path, struct entry **ppdents)
147 + if (git_statuses.len) {
148 + char dentpath[PATH_MAX];
149 + size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
150 + dentp->git_status[0][0] = dentp->git_status[1][0] = '-';
151 + dentp->git_status[0][1] = dentp->git_status[1][1] = '\0';
153 + if (dentp->flags & DIR_OR_DIRLNK) {
154 + char prefix[PATH_MAX];
155 + memccpy(prefix, dentpath, '\0', PATH_MAX);
156 + prefix[pathlen - 1] = '/';
158 + for (size_t i = 0; i < git_statuses.len; ++i)
159 + if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen))
160 + set_git_status(dentp->git_status, i);
162 + for (size_t i = 0; i < git_statuses.len; ++i)
163 + if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) {
164 + set_git_status(dentp->git_status, i);
171 } while ((dp = readdir(dirp)));
173 @@ -6362,7 +6465,8 @@ static int adjust_cols(int n)
175 else /* 2 more accounted for below */
176 n -= (dtls.maxentln - 2 - dtls.maxnameln);
178 + } else if (cfg.normalgit && git_statuses.show)
181 /* 2 columns for preceding space and indicator */
183 @@ -6517,7 +6621,7 @@ static void redraw(char *path)
187 - dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 29) : 26);
188 + dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 3) : 0) + (git_statuses.show ? 29 : 26);
191 ncols = adjust_cols(ncols);
192 @@ -8153,6 +8257,7 @@ static void usage(void)
193 " -F val fifo mode [0:preview 1:explore]\n"
195 " -g regex filters\n"
196 + " -G always show git status\n"
197 " -H show hidden files\n"
198 " -i show current file info\n"
199 " -J no auto-advance on selection\n"
200 @@ -8292,6 +8397,7 @@ static void cleanup(void)
204 + free(git_statuses.statuses);
208 @@ -8336,7 +8442,7 @@ int main(int argc, char *argv[])
210 while ((opt = (env_opts_id > 0
211 ? env_opts[--env_opts_id]
212 - : getopt(argc, argv, "aAb:BcCdDeEfF:gHiJKl:nNop:P:QrRs:St:T:uUVxh"))) != -1) {
213 + : getopt(argc, argv, "aAb:BcCdDeEfF:gGHiJKl:nNop:P:QrRs:St:T:uUVxh"))) != -1) {
217 @@ -8390,6 +8496,9 @@ int main(int argc, char *argv[])
219 filterfn = &visible_re;