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.
|
2021-06-02 04:27:06 +00:00
|
|
|
# Compatibility patch for the namefirst patch.
|
|
|
|
#
|
|
|
|
# Dependencies: libgit2
|
|
|
|
#
|
2021-06-05 14:21:01 +00:00
|
|
|
#
|
2021-06-03 09:01:27 +00:00
|
|
|
# Authors: @crides, Luuk van Baal
|
|
|
|
|
|
|
|
diff --git a/src/nnn.c b/src/nnn.c
|
2021-06-05 14:21:01 +00:00
|
|
|
index 9141d78..de4d9f0 100644
|
2021-06-02 04:27:06 +00:00
|
|
|
--- a/src/nnn.c
|
|
|
|
+++ b/src/nnn.c
|
2021-06-03 09:01:27 +00:00
|
|
|
@@ -105,6 +105,7 @@
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2021-06-02 04:27:06 +00:00
|
|
|
+#include <git2.h>
|
2021-06-03 09:01:27 +00:00
|
|
|
|
|
|
|
#if !defined(alloca) && defined(__GNUC__)
|
|
|
|
/*
|
|
|
|
@@ -244,6 +245,16 @@ typedef unsigned int uint_t;
|
|
|
|
typedef unsigned char uchar_t;
|
2021-06-02 04:27:06 +00:00
|
|
|
typedef unsigned short ushort_t;
|
|
|
|
typedef unsigned long long ulong_t;
|
|
|
|
+typedef enum {
|
2021-06-03 09:01:27 +00:00
|
|
|
+ GIT_COLUMN_STATUS_UNMOD = 0,
|
2021-06-02 04:27:06 +00:00
|
|
|
+ GIT_COLUMN_STATUS_NEW,
|
|
|
|
+ GIT_COLUMN_STATUS_MODIFIED,
|
|
|
|
+ GIT_COLUMN_STATUS_DELETED,
|
|
|
|
+ GIT_COLUMN_STATUS_RENAMED,
|
|
|
|
+ GIT_COLUMN_STATUS_TYPE_CHANGE,
|
|
|
|
+ GIT_COLUMN_STATUS_IGNORED,
|
|
|
|
+ GIT_COLUMN_STATUS_CONFLICTED,
|
|
|
|
+} git_column_status_t;
|
2021-06-03 09:01:27 +00:00
|
|
|
|
2021-06-02 04:27:06 +00:00
|
|
|
/* STRUCTURES */
|
|
|
|
|
2021-06-03 15:07:07 +00:00
|
|
|
@@ -262,6 +273,8 @@ typedef struct entry {
|
2021-06-02 04:27:06 +00:00
|
|
|
#ifndef NOUG
|
|
|
|
uid_t uid; /* 4 bytes */
|
2021-06-03 15:07:07 +00:00
|
|
|
gid_t gid; /* 4 bytes */
|
|
|
|
+ git_column_status_t status_indexed;
|
|
|
|
+ git_column_status_t status_staged;
|
|
|
|
#endif
|
|
|
|
} *pEntry;
|
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -313,6 +326,7 @@ typedef struct {
|
|
|
|
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;
|
|
|
|
|
|
|
|
/* Non-persistent program-internal states */
|
|
|
|
@@ -366,7 +380,18 @@ static struct {
|
2021-06-02 04:27:06 +00:00
|
|
|
ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
|
|
|
|
} dtls;
|
|
|
|
|
|
|
|
+typedef struct {
|
|
|
|
+ char *path;
|
|
|
|
+ git_status_t status;
|
|
|
|
+} simple_git_status_t;
|
|
|
|
+
|
|
|
|
+typedef struct {
|
|
|
|
+ simple_git_status_t *statuses;
|
|
|
|
+ size_t len;
|
|
|
|
+} simple_git_statuses_t;
|
|
|
|
+
|
|
|
|
/* GLOBALS */
|
|
|
|
+simple_git_statuses_t git_statuses;
|
|
|
|
|
|
|
|
/* Configuration, contexts */
|
|
|
|
static settings cfg = {
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -397,6 +422,7 @@ static settings cfg = {
|
|
|
|
0, /* cliopener */
|
|
|
|
0, /* waitedit */
|
|
|
|
1, /* rollover */
|
|
|
|
+ 0, /* normalgit */
|
|
|
|
};
|
|
|
|
|
|
|
|
static context g_ctx[CTX_MAX] __attribute__ ((aligned));
|
|
|
|
@@ -3472,6 +3498,79 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
|
2021-06-03 09:01:27 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-02 04:27:06 +00:00
|
|
|
|
|
|
|
+static git_column_status_t git_get_indexed_status(const uint32_t status) {
|
|
|
|
+ if (status & GIT_STATUS_INDEX_NEW) return GIT_COLUMN_STATUS_NEW;
|
|
|
|
+ if (status & GIT_STATUS_INDEX_MODIFIED) return GIT_COLUMN_STATUS_MODIFIED;
|
|
|
|
+ if (status & GIT_STATUS_INDEX_DELETED) return GIT_COLUMN_STATUS_DELETED;
|
|
|
|
+ if (status & GIT_STATUS_INDEX_RENAMED) return GIT_COLUMN_STATUS_RENAMED;
|
|
|
|
+ if (status & GIT_STATUS_INDEX_TYPECHANGE) return GIT_COLUMN_STATUS_TYPE_CHANGE;
|
|
|
|
+ return GIT_COLUMN_STATUS_UNMOD;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static git_column_status_t git_get_staged_status(const uint32_t status) {
|
|
|
|
+ if (status & GIT_STATUS_WT_NEW) return GIT_COLUMN_STATUS_NEW;
|
|
|
|
+ if (status & GIT_STATUS_WT_MODIFIED) return GIT_COLUMN_STATUS_MODIFIED;
|
|
|
|
+ if (status & GIT_STATUS_WT_DELETED) return GIT_COLUMN_STATUS_DELETED;
|
|
|
|
+ if (status & GIT_STATUS_WT_RENAMED) return GIT_COLUMN_STATUS_RENAMED;
|
|
|
|
+ if (status & GIT_STATUS_WT_TYPECHANGE) return GIT_COLUMN_STATUS_TYPE_CHANGE;
|
|
|
|
+ if (status & GIT_STATUS_IGNORED) return GIT_COLUMN_STATUS_IGNORED;
|
|
|
|
+ if (status & GIT_STATUS_CONFLICTED) return GIT_COLUMN_STATUS_CONFLICTED;
|
|
|
|
+ return GIT_COLUMN_STATUS_UNMOD;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void print_gitstatus(git_column_status_t status) {
|
|
|
|
+ switch (status) {
|
|
|
|
+ case GIT_COLUMN_STATUS_UNMOD: addch('-' | 0); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_NEW: addch('N' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_EXE))); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_MODIFIED: addch('M' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(4))); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_DELETED: addch('D' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_UND))); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_RENAMED: addch('R' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_CHR))); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_TYPE_CHANGE: addch('T' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_HRD))); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_IGNORED: addch('I' | 0); break;
|
|
|
|
+ case GIT_COLUMN_STATUS_CONFLICTED: addch('U' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_UND))); break;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static simple_git_statuses_t statuses_from_path(const char *path) {
|
|
|
|
+ git_repository *repo;
|
2021-06-03 09:01:27 +00:00
|
|
|
+ git_buf ret = { .ptr = NULL, .asize = 0, .size = 0 };
|
|
|
|
+ simple_git_statuses_t statuses = { .statuses = NULL, .len = 0 };
|
2021-06-02 04:27:06 +00:00
|
|
|
+ git_repository_discover(&ret, path, false, NULL);
|
|
|
|
+ git_repository_open(&repo, ret.ptr);
|
|
|
|
+ git_buf_dispose(&ret);
|
|
|
|
+
|
|
|
|
+ if (repo) {
|
2021-06-03 09:01:27 +00:00
|
|
|
+ char buf[PATH_MAX];
|
|
|
|
+ const char *workdir = git_repository_workdir(repo);
|
|
|
|
+ git_status_list *status_list;
|
|
|
|
+
|
2021-06-02 04:27:06 +00:00
|
|
|
+ git_status_list_new(&status_list, repo, NULL);
|
|
|
|
+ statuses.len = git_status_list_entrycount(status_list);
|
|
|
|
+ statuses.statuses = malloc(statuses.len * sizeof(simple_git_status_t));
|
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
+ for (size_t i = 0; i < statuses.len; ++i) {
|
2021-06-02 04:27:06 +00:00
|
|
|
+ const git_status_entry *status_ent = git_status_byindex(status_list, i);
|
|
|
|
+ const char *entry_path = status_ent->head_to_index ? status_ent->head_to_index->old_file.path
|
|
|
|
+ : status_ent->index_to_workdir->old_file.path;
|
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
+ xstrsncpy(buf, workdir, xstrlen(workdir));
|
|
|
|
+ statuses.statuses[i].path = abspath(entry_path, buf);
|
2021-06-02 04:27:06 +00:00
|
|
|
+ statuses.statuses[i].status = status_ent->status;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ git_status_list_free(status_list);
|
|
|
|
+ git_repository_free(repo);
|
|
|
|
+ }
|
|
|
|
+ return statuses;
|
|
|
|
+}
|
|
|
|
+
|
2021-06-03 15:07:07 +00:00
|
|
|
+static void git_statuses_free(void) {
|
|
|
|
+ for (size_t i = 0; i < git_statuses.len; ++i)
|
|
|
|
+ free(git_statuses.statuses[i].path);
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
2021-06-03 15:07:07 +00:00
|
|
|
+ free(git_statuses.statuses);
|
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 */
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -3781,6 +3880,12 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
|
|
|
|
int attrs = 0, namelen;
|
|
|
|
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
|
2021-06-02 04:27:06 +00:00
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
+ if (git_statuses.len && (cfg.showdetail || cfg.normalgit)) {
|
|
|
|
+ addch(' ');
|
2021-06-02 04:27:06 +00:00
|
|
|
+ print_gitstatus(ent->status_indexed);
|
|
|
|
+ print_gitstatus(ent->status_staged);
|
|
|
|
+ }
|
|
|
|
+
|
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
|
|
|
if (g_state.oldcolor)
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -5125,6 +5230,10 @@ 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-06-03 09:01:27 +00:00
|
|
|
+ if (git_statuses.len)
|
2021-06-03 15:07:07 +00:00
|
|
|
+ git_statuses_free();
|
|
|
|
+ git_statuses = statuses_from_path(path);
|
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
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -5322,6 +5431,22 @@ 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-06-03 15:07:07 +00:00
|
|
|
+ char *dentpath = abspath(dentp->name, path);
|
2021-06-05 14:21:01 +00:00
|
|
|
+ namebuflen = xstrlen(dentpath);
|
|
|
|
+ uint merged_status = 0;
|
2021-06-03 15:07:07 +00:00
|
|
|
+
|
2021-06-05 14:21:01 +00:00
|
|
|
+ for (size_t i = 0; i < git_statuses.len; ++i)
|
|
|
|
+ if (!(dentp->flags & DIR_OR_LINK_TO_DIR) ? !xstrcmp(git_statuses.statuses[i].path, dentpath) :
|
|
|
|
+ (is_prefix(git_statuses.statuses[i].path, dentpath, namebuflen) &&
|
|
|
|
+ (namebuflen <= xstrlen(git_statuses.statuses[i].path))))
|
|
|
|
+ merged_status |= git_statuses.statuses[i].status;
|
2021-06-02 04:27:06 +00:00
|
|
|
+
|
|
|
|
+ dentp->status_indexed = git_get_indexed_status(merged_status);
|
|
|
|
+ dentp->status_staged = git_get_staged_status(merged_status);
|
2021-06-03 15:07:07 +00:00
|
|
|
+ free(dentpath);
|
2021-06-02 04:27:06 +00:00
|
|
|
+ }
|
|
|
|
+
|
2021-06-03 09:01:27 +00:00
|
|
|
++ndents;
|
|
|
|
} while ((dp = readdir(dirp)));
|
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -5834,7 +5959,7 @@ static int adjust_cols(int n)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 2 columns for preceding space and indicator */
|
|
|
|
- return (n - 2);
|
|
|
|
+ return (n - ((cfg.normalgit && !cfg.showdetail) ? 5 : 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_line(char *path, int ncols)
|
|
|
|
@@ -5972,7 +6097,7 @@ static void redraw(char *path)
|
2021-06-03 09:01:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
- dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln) : 0) + (g_state.uidgid ? 26 : 23);
|
|
|
|
+ dtls.maxentln = dtls.maxnameln + dtls.maxsizeln + (dtls.printguid ? (dtls.maxuidln + dtls.maxgidln) : 0) + (g_state.uidgid ? 29 : 26);
|
|
|
|
|
|
|
|
ncols = adjust_cols(ncols);
|
|
|
|
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -7576,6 +7701,7 @@ static void usage(void)
|
|
|
|
#endif
|
|
|
|
" -F show fortune\n"
|
|
|
|
" -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-06-03 09:01:27 +00:00
|
|
|
@@ -7714,6 +7840,8 @@ static void cleanup(void)
|
2021-06-02 04:27:06 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-03 15:07:07 +00:00
|
|
|
+ git_statuses_free();
|
|
|
|
+ git_libgit2_shutdown();
|
2021-06-02 04:27:06 +00:00
|
|
|
free(selpath);
|
|
|
|
free(plgpath);
|
|
|
|
free(cfgpath);
|
2021-06-05 14:21:01 +00:00
|
|
|
@@ -7758,7 +7886,7 @@ int main(int argc, char *argv[])
|
|
|
|
|
|
|
|
while ((opt = (env_opts_id > 0
|
|
|
|
? env_opts[--env_opts_id]
|
2021-06-13 03:34:49 +00:00
|
|
|
- : getopt(argc, argv, "aAb:cCdDeEfgHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
|
|
|
|
+ : getopt(argc, argv, "aAb:cCdDeEfgGHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
|
2021-06-05 14:21:01 +00:00
|
|
|
switch (opt) {
|
|
|
|
#ifndef NOFIFO
|
|
|
|
case 'a':
|
|
|
|
@@ -7802,6 +7930,9 @@ int main(int argc, char *argv[])
|
|
|
|
cfg.regex = 1;
|
|
|
|
filterfn = &visible_re;
|
|
|
|
break;
|
|
|
|
+ case 'G':
|
|
|
|
+ cfg.normalgit = 1;
|
|
|
|
+ break;
|
|
|
|
case 'H':
|
|
|
|
cfg.showhidden = 1;
|
|
|
|
break;
|
|
|
|
@@ -7909,6 +8040,7 @@ int main(int argc, char *argv[])
|
2021-06-02 04:27:06 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
atexit(cleanup);
|
|
|
|
+ git_libgit2_init();
|
|
|
|
|
|
|
|
/* Check if we are in path list mode */
|
|
|
|
if (!isatty(STDIN_FILENO)) {
|