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
|
2021-11-05 16:01:56 +00:00
|
|
|
# column also in normal mode. Vim plugin users may consider
|
|
|
|
# adding the -G flag to their command override.
|
2021-06-02 04:27:06 +00:00
|
|
|
#
|
2021-11-05 16:01:56 +00:00
|
|
|
# Authors: Luuk van Baal
|
2021-06-03 09:01:27 +00:00
|
|
|
|
2021-06-02 04:27:06 +00:00
|
|
|
diff --git a/src/nnn.c b/src/nnn.c
|
2022-04-26 12:54:27 +00:00
|
|
|
index 508ee915..77d56d63 100644
|
2021-06-02 04:27:06 +00:00
|
|
|
--- a/src/nnn.c
|
|
|
|
+++ b/src/nnn.c
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -265,6 +265,25 @@
|
2021-11-05 16:01:56 +00:00
|
|
|
#define FREE 0
|
|
|
|
#define CAPACITY 1
|
|
|
|
|
|
|
|
+/* Git icons */
|
|
|
|
+#ifdef NERD
|
|
|
|
+#define GIT_ADD ""
|
|
|
|
+#define GIT_DEL ""
|
|
|
|
+#define GIT_IGN ""
|
|
|
|
+#define GIT_MOD ""
|
|
|
|
+#define GIT_NEW ""
|
|
|
|
+#define GIT_NON "-"
|
|
|
|
+#define GIT_UPD "ﮮ"
|
|
|
|
+#else
|
|
|
|
+#define GIT_ADD "A"
|
|
|
|
+#define GIT_DEL "D"
|
|
|
|
+#define GIT_IGN "!"
|
|
|
|
+#define GIT_MOD "M"
|
|
|
|
+#define GIT_NEW "?"
|
|
|
|
+#define GIT_NON "-"
|
|
|
|
+#define GIT_UPD "U"
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
/* TYPE DEFINITIONS */
|
|
|
|
typedef unsigned int uint_t;
|
|
|
|
typedef unsigned char uchar_t;
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -289,6 +308,7 @@ typedef struct entry {
|
2021-06-02 04:27:06 +00:00
|
|
|
uid_t uid; /* 4 bytes */
|
2021-06-03 15:07:07 +00:00
|
|
|
gid_t gid; /* 4 bytes */
|
|
|
|
#endif
|
2021-11-05 16:01:56 +00:00
|
|
|
+ char git_status[2][5];
|
2021-06-03 15:07:07 +00:00
|
|
|
} *pEntry;
|
|
|
|
|
2021-08-16 10:45:44 +00:00
|
|
|
/* Selection marker */
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -345,6 +365,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) */
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -394,7 +415,17 @@ typedef struct {
|
2021-06-02 04:27:06 +00:00
|
|
|
} session_header_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
+typedef struct {
|
2021-07-06 00:01:19 +00:00
|
|
|
+ char status[2];
|
|
|
|
+ char path[PATH_MAX];
|
|
|
|
+} git_status_t;
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
|
|
|
/* GLOBALS */
|
2021-07-06 00:01:19 +00:00
|
|
|
+struct {
|
|
|
|
+ bool show;
|
|
|
|
+ size_t len;
|
|
|
|
+ git_status_t *statuses;
|
|
|
|
+} git_statuses;
|
2021-06-02 04:27:06 +00:00
|
|
|
|
|
|
|
/* Configuration, contexts */
|
|
|
|
static settings cfg = {
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -425,6 +456,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));
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -3838,6 +3870,56 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
|
2021-09-29 09:33:19 +00:00
|
|
|
return -1;
|
2021-06-03 09:01:27 +00:00
|
|
|
}
|
2021-06-02 04:27:06 +00:00
|
|
|
|
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";
|
2021-09-29 09:33:19 +00:00
|
|
|
+ char workdir[PATH_MAX], *ret;
|
2021-07-06 00:01:19 +00:00
|
|
|
+ FILE *fp = popen(gitrev, "r");
|
2021-06-17 23:32:44 +00:00
|
|
|
+
|
2021-10-09 00:36:22 +00:00
|
|
|
+ git_statuses.show = FALSE;
|
2021-09-29 09:33:19 +00:00
|
|
|
+ ret = fgets(workdir, PATH_MAX, fp);
|
2021-07-06 00:01:19 +00:00
|
|
|
+ pclose(fp);
|
2021-09-29 09:33:19 +00:00
|
|
|
+ if (!ret)
|
2021-07-06 00:01:19 +00:00
|
|
|
+ return 0;
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
2021-11-05 16:01:56 +00:00
|
|
|
+ static char gitstat[] = "git -c core.quotePath= status --porcelain --no-renames --ignored=matching -u ";
|
2021-07-06 00:01:19 +00:00
|
|
|
+ char pathspec[PATH_MAX], status[PATH_MAX];
|
|
|
|
+ size_t i = -1;
|
|
|
|
+ workdir[xstrlen(workdir) - 1] = '\0';
|
2021-08-17 12:01:24 +00:00
|
|
|
+ snprintf(pathspec, PATH_MAX, "%s\"%s\"%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*");
|
2021-07-06 00:01:19 +00:00
|
|
|
+ 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));
|
2021-11-05 16:01:56 +00:00
|
|
|
+ git_statuses.statuses[i].status[0] = status[0];
|
|
|
|
+ git_statuses.statuses[i].status[1] = status[1];
|
2021-07-06 00:01:19 +00:00
|
|
|
+ mkpath(workdir, status + pathindex, git_statuses.statuses[i].path);
|
2021-06-02 04:27:06 +00:00
|
|
|
+ }
|
|
|
|
+
|
2021-07-06 00:01:19 +00:00
|
|
|
+ pclose(fp);
|
|
|
|
+ return (i + 1);
|
2021-06-02 04:27:06 +00:00
|
|
|
+}
|
2021-11-05 16:01:56 +00:00
|
|
|
+
|
|
|
|
+static void set_git_status(char status[][5], uint_t nr)
|
|
|
|
+{
|
|
|
|
+ for (int j = 0; j < 2; j++) {
|
|
|
|
+ if (status[j][0] == '-')
|
|
|
|
+ switch (git_statuses.statuses[nr].status[j]) {
|
|
|
|
+ case ' ': xstrsncpy(status[j], GIT_NON, 4); break;
|
|
|
|
+ case 'M': xstrsncpy(status[j], GIT_MOD, 4); break;
|
|
|
|
+ case 'A': xstrsncpy(status[j], GIT_ADD, 4); break;
|
|
|
|
+ case '?': xstrsncpy(status[j], GIT_NEW, 4); break;
|
|
|
|
+ case '!': xstrsncpy(status[j], GIT_IGN, 4); break;
|
|
|
|
+ case 'D': xstrsncpy(status[j], GIT_DEL, 4); break;
|
|
|
|
+ case 'U': xstrsncpy(status[j], GIT_UPD, 4); break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (git_statuses.statuses[nr].status[1] != '!')
|
|
|
|
+ git_statuses.show = TRUE;
|
|
|
|
+}
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
static void resetdircolor(int flags)
|
2021-06-02 04:27:06 +00:00
|
|
|
{
|
2021-06-03 09:01:27 +00:00
|
|
|
/* Directories are always shown on top, clear the color when moving to first file */
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -4182,6 +4264,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
|
2021-06-02 04:27:06 +00:00
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
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))
|
2021-11-05 16:01:56 +00:00
|
|
|
+ printw("%*s%s%s", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "",
|
2021-07-06 00:01:19 +00:00
|
|
|
+ ent->git_status[0], ent->git_status[1]);
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
2021-06-05 14:21:01 +00:00
|
|
|
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
|
2021-06-02 04:27:06 +00:00
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
if (g_state.oldcolor)
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -5630,6 +5716,11 @@ static int dentfill(char *path, struct entry **ppdents)
|
2021-06-03 09:01:27 +00:00
|
|
|
attron(COLOR_PAIR(cfg.curctx + 1));
|
|
|
|
}
|
2021-06-02 04:27:06 +00:00
|
|
|
|
2021-07-06 00:01:19 +00:00
|
|
|
+ char linkpath[PATH_MAX];
|
|
|
|
+ if ((git_statuses.len = get_git_statuses(path)))
|
2021-09-29 09:33:19 +00:00
|
|
|
+ if (!realpath(path, linkpath))
|
|
|
|
+ printwarn(NULL);
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
#if _POSIX_C_SOURCE >= 200112L
|
|
|
|
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
|
|
|
|
#endif
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -5830,6 +5921,29 @@ static int dentfill(char *path, struct entry **ppdents)
|
2021-06-02 04:27:06 +00:00
|
|
|
#endif
|
2021-06-03 09:01:27 +00:00
|
|
|
}
|
2021-06-02 04:27:06 +00:00
|
|
|
|
|
|
|
+ if (git_statuses.len) {
|
2021-10-09 00:36:22 +00:00
|
|
|
+ char dentpath[PATH_MAX];
|
2021-09-29 09:33:19 +00:00
|
|
|
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
|
2021-11-05 16:01:56 +00:00
|
|
|
+ dentp->git_status[0][0] = dentp->git_status[1][0] = '-';
|
|
|
|
+ dentp->git_status[0][1] = dentp->git_status[1][1] = '\0';
|
2021-07-06 00:01:19 +00:00
|
|
|
+
|
|
|
|
+ if (dentp->flags & DIR_OR_DIRLNK) {
|
2021-10-09 00:36:22 +00:00
|
|
|
+ char prefix[PATH_MAX];
|
|
|
|
+ memccpy(prefix, dentpath, '\0', PATH_MAX);
|
|
|
|
+ prefix[pathlen - 1] = '/';
|
|
|
|
+
|
2021-07-06 00:01:19 +00:00
|
|
|
+ for (size_t i = 0; i < git_statuses.len; ++i)
|
2021-11-05 16:01:56 +00:00
|
|
|
+ if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen))
|
|
|
|
+ set_git_status(dentp->git_status, i);
|
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)) {
|
2021-11-05 16:01:56 +00:00
|
|
|
+ set_git_status(dentp->git_status, i);
|
2021-07-06 00:01:19 +00:00
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
2021-06-02 04:27:06 +00:00
|
|
|
+ }
|
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
++ndents;
|
|
|
|
} while ((dp = readdir(dirp)));
|
|
|
|
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -6373,11 +6487,12 @@ static int adjust_cols(int n)
|
2021-06-03 09:01:27 +00:00
|
|
|
#endif
|
|
|
|
if (cfg.showdetail) {
|
|
|
|
/* Fallback to light mode if less than 35 columns */
|
|
|
|
- if (n < 36)
|
|
|
|
+ if (n < 38)
|
|
|
|
cfg.showdetail ^= 1;
|
2021-07-06 00:01:19 +00:00
|
|
|
else /* 2 more accounted for below */
|
2021-06-03 09:01:27 +00:00
|
|
|
- n -= 32;
|
2021-07-06 00:01:19 +00:00
|
|
|
- }
|
2021-06-17 23:32:44 +00:00
|
|
|
+ n -= (git_statuses.show ? 34 : 32);
|
2021-07-06 00:01:19 +00:00
|
|
|
+ } else if (cfg.normalgit && git_statuses.show)
|
|
|
|
+ n -= 3;
|
2021-06-03 09:01:27 +00:00
|
|
|
|
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);
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -8144,6 +8259,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"
|
2021-11-05 16:01:56 +00:00
|
|
|
" -i show current file info\n"
|
2022-04-26 12:54:27 +00:00
|
|
|
" -J no auto-jump on selection\n"
|
|
|
|
@@ -8282,6 +8398,7 @@ static void cleanup(void)
|
|
|
|
fflush(stdout);
|
2021-06-02 04:27:06 +00:00
|
|
|
}
|
|
|
|
#endif
|
2021-07-06 00:01:19 +00:00
|
|
|
+ free(git_statuses.statuses);
|
2021-06-02 04:27:06 +00:00
|
|
|
free(selpath);
|
|
|
|
free(plgpath);
|
|
|
|
free(cfgpath);
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -8326,7 +8443,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-10-22 01:12:42 +00:00
|
|
|
- : getopt(argc, argv, "aAb:cCdDeEfF:gHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) {
|
|
|
|
+ : getopt(argc, argv, "aAb:cCdDeEfF:gGHiJKl:nop:P:QrRs:St:T:uUVxh"))) != -1) {
|
2021-06-05 14:21:01 +00:00
|
|
|
switch (opt) {
|
|
|
|
#ifndef NOFIFO
|
|
|
|
case 'a':
|
2022-04-26 12:54:27 +00:00
|
|
|
@@ -8377,6 +8494,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;
|