nnn/patches/gitstatus/mainline.diff

245 lines
7.9 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.
#
# Dependencies: libgit2
#
# Authors: @crides, Luuk van Baal
diff --git a/src/nnn.c b/src/nnn.c
2021-06-17 23:32:44 +00:00
index 67523fe6..c965bd1c 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -105,6 +105,7 @@
#include <wchar.h>
#include <pwd.h>
#include <grp.h>
+#include <git2.h>
#if !defined(alloca) && defined(__GNUC__)
/*
2021-06-17 23:32:44 +00:00
@@ -265,6 +266,7 @@ typedef struct entry {
uid_t uid; /* 4 bytes */
2021-06-03 15:07:07 +00:00
gid_t gid; /* 4 bytes */
#endif
2021-06-17 23:32:44 +00:00
+ git_status_t git_status;
2021-06-03 15:07:07 +00:00
} *pEntry;
/* Key-value pairs from env */
2021-06-17 23:32:44 +00:00
@@ -315,6 +317,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;
/* Non-persistent program-internal states */
2021-06-17 23:32:44 +00:00
@@ -363,7 +366,19 @@ typedef struct {
} session_header_t;
#endif
+typedef struct {
+ char *path;
+ git_status_t status;
+} simple_git_status_t;
+
+typedef struct {
+ simple_git_status_t *statuses;
+ size_t len;
2021-06-17 23:32:44 +00:00
+ bool show;
+} simple_git_statuses_t;
+
/* GLOBALS */
+simple_git_statuses_t git_statuses;
/* Configuration, contexts */
static settings cfg = {
2021-06-17 23:32:44 +00:00
@@ -394,6 +409,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-06-17 23:32:44 +00:00
@@ -3498,6 +3514,66 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
return NULL;
}
2021-06-17 23:32:44 +00:00
+static void print_gitstatus(git_status_t status) {
+ if (status & GIT_STATUS_INDEX_NEW) addch('N' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_EXE)));
+ else if (status & GIT_STATUS_INDEX_MODIFIED) addch('M' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(4)));
+ else if (status & GIT_STATUS_INDEX_DELETED) addch('D' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_UND)));
+ else if (status & GIT_STATUS_INDEX_RENAMED) addch('R' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_CHR)));
+ else if (status & GIT_STATUS_INDEX_TYPECHANGE) addch('T' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_HRD)));
+ else addch('-' | 0);
+
+ if (status & GIT_STATUS_WT_NEW) addch('N' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_EXE)));
+ else if (status & GIT_STATUS_WT_MODIFIED) addch('M' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(4)));
+ else if (status & GIT_STATUS_WT_DELETED) addch('D' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_UND)));
+ else if (status & GIT_STATUS_WT_RENAMED) addch('R' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_CHR)));
+ else if (status & GIT_STATUS_WT_TYPECHANGE) addch('T' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_HRD)));
+ else if (status & GIT_STATUS_IGNORED) addch('I' | 0);
+ else if (status & GIT_STATUS_CONFLICTED) addch('U' | (g_state.oldcolor ? COLOR_PAIR(1) : COLOR_PAIR(C_UND)));
+ else addch('-' | 0);
+}
+
+static simple_git_statuses_t statuses_from_path(const char *path) {
+ git_repository *repo;
+ git_buf ret = { .ptr = NULL, .asize = 0, .size = 0 };
+ simple_git_statuses_t statuses = { .statuses = NULL, .len = 0 };
+ git_repository_discover(&ret, path, false, NULL);
+ git_repository_open(&repo, ret.ptr);
+ git_buf_dispose(&ret);
+
+ if (repo) {
+ char buf[PATH_MAX];
+ const char *workdir = git_repository_workdir(repo);
+ git_status_list *status_list;
+
+ 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));
+
+ for (size_t i = 0; i < statuses.len; ++i) {
+ 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;
+
+ xstrsncpy(buf, workdir, xstrlen(workdir));
+ statuses.statuses[i].path = abspath(entry_path, buf);
+ 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-03 15:07:07 +00:00
+ free(git_statuses.statuses);
+ git_statuses.len = 0;
2021-06-17 23:32:44 +00:00
+ git_statuses.show = FALSE;
+}
+
static void resetdircolor(int flags)
{
/* Directories are always shown on top, clear the color when moving to first file */
2021-06-17 23:32:44 +00:00
@@ -3835,6 +3911,12 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
2021-06-05 14:21:01 +00:00
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
2021-06-17 23:32:44 +00:00
+ if (git_statuses.show && (cfg.showdetail || cfg.normalgit)) {
2021-06-05 14:21:01 +00:00
+ if (cfg.normalgit && !cfg.showdetail)
+ addch(' ');
2021-06-17 23:32:44 +00:00
+ print_gitstatus(ent->git_status);
2021-06-05 14:21:01 +00:00
+ }
+
2021-06-05 14:21:01 +00:00
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
2021-06-05 14:21:01 +00:00
if (g_state.oldcolor)
2021-06-17 23:32:44 +00:00
@@ -5160,6 +5242,10 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1));
}
+ if (git_statuses.len)
2021-06-03 15:07:07 +00:00
+ git_statuses_free();
+ git_statuses = statuses_from_path(path);
+
#if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
2021-06-17 23:32:44 +00:00
@@ -5357,6 +5443,23 @@ static int dentfill(char *path, struct entry **ppdents)
#endif
}
+ if (git_statuses.len) {
+ char *dentpath = abspath(dentp->name, path);
2021-06-05 14:21:01 +00:00
+ namebuflen = xstrlen(dentpath);
2021-06-17 23:32:44 +00:00
+ dentp->git_status = GIT_STATUS_CURRENT;
+
2021-06-05 14:21:01 +00:00
+ for (size_t i = 0; i < git_statuses.len; ++i)
2021-06-17 23:32:44 +00:00
+ if (git_statuses.statuses[i].status)
2021-06-20 15:55:22 +00:00
+ if ((dentp->flags & DIR_OR_DIRLNK) ? (is_prefix(git_statuses.statuses[i].path, dentpath, namebuflen) &&
2021-06-17 23:32:44 +00:00
+ namebuflen <= xstrlen(git_statuses.statuses[i].path)) : !xstrcmp(git_statuses.statuses[i].path, dentpath)) {
+ if (git_statuses.statuses[i].status != GIT_STATUS_IGNORED)
+ git_statuses.show = TRUE;
+ dentp->git_status |= git_statuses.statuses[i].status;
+ }
+
+ free(dentpath);
+ }
+
++ndents;
} while ((dp = readdir(dirp)));
2021-06-17 23:32:44 +00:00
@@ -5873,16 +5976,16 @@ static int adjust_cols(int n)
#endif
if (cfg.showdetail) {
/* Fallback to light mode if less than 35 columns */
- if (n < 36)
+ if (n < 38)
cfg.showdetail ^= 1;
else {
2021-06-03 15:07:07 +00:00
/* 2 more accounted for below */
- n -= 32;
2021-06-17 23:32:44 +00:00
+ n -= (git_statuses.show ? 34 : 32);
}
}
2021-06-05 14:21:01 +00:00
/* 2 columns for preceding space and indicator */
- return (n - 2);
2021-06-17 23:32:44 +00:00
+ return (n - ((git_statuses.show && (cfg.normalgit && !cfg.showdetail)) ? 5 : 2));
2021-06-05 14:21:01 +00:00
}
static void draw_line(char *path, int ncols)
2021-06-17 23:32:44 +00:00
@@ -7608,6 +7711,7 @@ static void usage(void)
" -f use readline history file\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-06-17 23:32:44 +00:00
@@ -7746,6 +7850,8 @@ static void cleanup(void)
fflush(stdout);
}
#endif
2021-06-03 15:07:07 +00:00
+ git_statuses_free();
+ git_libgit2_shutdown();
free(selpath);
free(plgpath);
free(cfgpath);
2021-06-17 23:32:44 +00:00
@@ -7790,7 +7896,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-06-17 23:32:44 +00:00
@@ -7831,6 +7937,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;
2021-06-17 23:32:44 +00:00
@@ -7938,6 +8047,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
atexit(cleanup);
+ git_libgit2_init();
/* Check if we are in path list mode */
if (!isatty(STDIN_FILENO)) {