nnn/patches/gitstatus/namefirst.diff

205 lines
6.3 KiB
Diff
Raw Normal View History

2021-06-05 14:21:01 +00:00
# Description: Add git status column to detail mode. Provides additional
# command line flag -G which will render the git status
# column also in normal mode. nnn.vim users may consider
# adding `let g:nnn#command = 'nnn -G' to their vim config.
# Compatibility patch for the namefirst patch.
#
2021-07-06 00:01:19 +00:00
# Authors: Luuk van Baal, @crides
diff --git a/src/nnn.c b/src/nnn.c
2021-08-16 10:45:44 +00:00
index 2d33716..b190177 100644
--- a/src/nnn.c
+++ b/src/nnn.c
2021-08-16 10:45:44 +00:00
@@ -277,6 +277,7 @@ typedef struct entry {
uid_t uid; /* 4 bytes */
2021-06-03 15:07:07 +00:00
gid_t gid; /* 4 bytes */
#endif
2021-07-06 00:01:19 +00:00
+ char git_status[2];
2021-06-03 15:07:07 +00:00
} *pEntry;
2021-08-16 10:45:44 +00:00
/* Selection marker */
@@ -333,6 +334,7 @@ typedef struct {
2021-06-05 14:21:01 +00:00
uint_t cliopener : 1; /* All-CLI app opener */
uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */
uint_t rollover : 1; /* Roll over at edges */
+ uint_t normalgit : 1; /* Show git status in normal mode */
} settings;
2021-07-06 00:01:19 +00:00
/* Non-persistent program-internal states (alphabeical order) */
2021-08-16 10:45:44 +00:00
@@ -386,7 +388,17 @@ static struct {
ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
} dtls;
+typedef struct {
2021-07-06 00:01:19 +00:00
+ char status[2];
+ char path[PATH_MAX];
+} git_status_t;
+
/* GLOBALS */
2021-07-06 00:01:19 +00:00
+struct {
+ bool show;
+ size_t len;
+ git_status_t *statuses;
+} git_statuses;
/* Configuration, contexts */
static settings cfg = {
2021-08-16 10:45:44 +00:00
@@ -417,6 +429,7 @@ static settings cfg = {
2021-06-05 14:21:01 +00:00
0, /* cliopener */
0, /* waitedit */
1, /* rollover */
+ 0, /* normalgit */
};
static context g_ctx[CTX_MAX] __attribute__ ((aligned));
2021-08-16 10:45:44 +00:00
@@ -3789,6 +3802,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
return NULL;
}
2021-07-06 00:01:19 +00:00
+static size_t get_git_statuses(const char *path)
+{
+ static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
+ char workdir[PATH_MAX];
+ FILE *fp = popen(gitrev, "r");
+
2021-07-06 00:01:19 +00:00
+ fgets(workdir, PATH_MAX, fp);
+ pclose(fp);
+
2021-07-06 00:01:19 +00:00
+ if (!workdir[0])
+ return 0;
+
2021-07-06 00:01:19 +00:00
+ static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
+ char pathspec[PATH_MAX], status[PATH_MAX];
+ size_t i = -1;
+ git_statuses.show = FALSE;
+ workdir[xstrlen(workdir) - 1] = '\0';
+ snprintf(pathspec, PATH_MAX, "%s%s%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*");
+ fp = popen(pathspec, "r");
+
+ while (fgets(status, PATH_MAX, fp)) {
+ size_t pathindex = (status[3] == '"') ? 4 : 3;
+ status[xstrlen(status) - pathindex + 2] = '\0';
+ git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1));
+ git_statuses.statuses[i].status[0] = (status[0] == ' ') ? '-' : status[0];
+ git_statuses.statuses[i].status[1] = (status[1] == ' ') ? '-' : status[1];
+ mkpath(workdir, status + pathindex, git_statuses.statuses[i].path);
+ }
+
2021-07-06 00:01:19 +00:00
+ pclose(fp);
+ return (i + 1);
+}
+
static void resetdircolor(int flags)
{
/* Directories are always shown on top, clear the color when moving to first file */
2021-08-16 10:45:44 +00:00
@@ -4099,6 +4145,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
2021-06-05 14:21:01 +00:00
int attrs = 0, namelen;
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
2021-07-06 00:01:19 +00:00
+ if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
+ printw(" %c%c", ent->git_status[0], ent->git_status[1]);
+
2021-06-05 14:21:01 +00:00
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
if (g_state.oldcolor)
2021-08-16 10:45:44 +00:00
@@ -5457,6 +5506,10 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1));
}
2021-07-06 00:01:19 +00:00
+ char linkpath[PATH_MAX];
+ if ((git_statuses.len = get_git_statuses(path)))
+ realpath(path, linkpath);
+
#if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
2021-08-16 10:45:44 +00:00
@@ -5655,6 +5708,34 @@ static int dentfill(char *path, struct entry **ppdents)
#endif
}
+ if (git_statuses.len) {
2021-07-06 00:01:19 +00:00
+ char dentpath[PATH_MAX];
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
+ dentp->git_status[0] = dentp->git_status[1] = '-';
+
+ if (dentp->flags & DIR_OR_DIRLNK) {
+ for (size_t i = 0; i < git_statuses.len; ++i)
+ if (is_prefix(git_statuses.statuses[i].path, dentpath, pathlen)) {
+ dentp->git_status[0] = git_statuses.statuses[i].status[0];
+ dentp->git_status[1] = git_statuses.statuses[i].status[1];
+ if (dentp->git_status[1] != '!') {
2021-06-17 23:32:44 +00:00
+ git_statuses.show = TRUE;
2021-07-06 00:01:19 +00:00
+ if (dentp->git_status[1] == '?')
+ break;
+ }
2021-06-17 23:32:44 +00:00
+ }
2021-07-06 00:01:19 +00:00
+ } else {
+ for (size_t i = 0; i < git_statuses.len; ++i)
+ if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) {
+ dentp->git_status[0] = git_statuses.statuses[i].status[0];
+ dentp->git_status[1] = git_statuses.statuses[i].status[1];
+ if (dentp->git_status[1] != '!')
+ git_statuses.show = TRUE;
+ break;
+ }
+ }
+ }
+
++ndents;
} while ((dp = readdir(dirp)));
2021-08-16 10:45:44 +00:00
@@ -6157,7 +6238,8 @@ static int adjust_cols(int n)
2021-07-06 00:01:19 +00:00
cfg.showdetail ^= 1;
else /* 2 more accounted for below */
n -= (dtls.maxentln - 2 - dtls.maxnameln);
- }
+ } else if (cfg.normalgit && git_statuses.show)
+ n -= 3;
2021-06-05 14:21:01 +00:00
/* 2 columns for preceding space and indicator */
2021-07-06 00:01:19 +00:00
return (n - 2);
2021-08-16 10:45:44 +00:00
@@ -6312,7 +6394,7 @@ static void redraw(char *path)
2021-07-06 00:01:19 +00:00
}
#endif
}
2021-07-06 00:01:19 +00:00
- dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 29) : 26);
+ dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln + 3) : 0) + (git_statuses.show ? 29 : 26);
}
ncols = adjust_cols(ncols);
2021-08-16 10:45:44 +00:00
@@ -7919,6 +8001,7 @@ static void usage(void)
2021-07-06 00:01:19 +00:00
" -F val fifo mode [0:preview 1:explore]\n"
2021-06-05 14:21:01 +00:00
#endif
" -g regex filters\n"
+ " -G always show git status\n"
" -H show hidden files\n"
" -J no auto-proceed on select\n"
" -K detect key collision\n"
2021-08-16 10:45:44 +00:00
@@ -8057,6 +8140,7 @@ static void cleanup(void)
fflush(stdout);
}
#endif
2021-07-06 00:01:19 +00:00
+ free(git_statuses.statuses);
free(selpath);
free(plgpath);
free(cfgpath);
2021-08-16 10:45:44 +00:00
@@ -8101,7 +8185,7 @@ int main(int argc, char *argv[])
2021-06-05 14:21:01 +00:00
while ((opt = (env_opts_id > 0
? env_opts[--env_opts_id]
2021-06-20 08:59:07 +00:00
- : getopt(argc, argv, "aAb:cCdDeEfF:gHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
+ : getopt(argc, argv, "aAb:cCdDeEfF:gGHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
2021-06-05 14:21:01 +00:00
switch (opt) {
#ifndef NOFIFO
case 'a':
2021-08-16 10:45:44 +00:00
@@ -8152,6 +8236,9 @@ int main(int argc, char *argv[])
2021-06-05 14:21:01 +00:00
cfg.regex = 1;
filterfn = &visible_re;
break;
+ case 'G':
+ cfg.normalgit = 1;
+ break;
case 'H':
cfg.showhidden = 1;
break;