"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "patches/gitstatus/mainline.diff" between
nnn-v4.1.1.tar.gz and nnn-v4.2.tar.gz

About: nnn is a full-featured terminal file manager.

mainline.diff  (nnn-v4.1.1):mainline.diff  (nnn-v4.2)
# Description: Add git status column to detail mode. # 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: Luuk van Baal, @crides
#
# Authors: @crides, Luuk van Baal
diff --git a/src/nnn.c b/src/nnn.c diff --git a/src/nnn.c b/src/nnn.c
index d83d7f6..7940077 100644 index c4bced5..a4f2a39 100644
--- a/src/nnn.c --- a/src/nnn.c
+++ b/src/nnn.c +++ b/src/nnn.c
@@ -105,6 +105,7 @@ @@ -269,6 +269,7 @@ typedef struct entry {
#include <wchar.h>
#include <pwd.h>
#include <grp.h>
+#include <git2.h>
#if !defined(alloca) && defined(__GNUC__)
/*
@@ -244,6 +245,16 @@ typedef unsigned int uint_t;
typedef unsigned char uchar_t;
typedef unsigned short ushort_t;
typedef unsigned long long ulong_t;
+typedef enum {
+ GIT_COLUMN_STATUS_UNMOD = 0,
+ 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;
/* STRUCTURES */
@@ -263,6 +274,8 @@ typedef struct entry {
uid_t uid; /* 4 bytes */ uid_t uid; /* 4 bytes */
gid_t gid; /* 4 bytes */ gid_t gid; /* 4 bytes */
#endif #endif
+ git_column_status_t status_indexed; + char git_status[2];
+ git_column_status_t status_staged;
} *pEntry; } *pEntry;
<span class="insert">char git_status[2];</span>
/* Key-value pairs from env */ /* Key-value pairs from env */
@@ -362,7 +375,18 @@ typedef struct { @@ -319,6 +320,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 (alphabeical order) */
@@ -368,7 +370,17 @@ typedef struct {
} session_header_t; } session_header_t;
#endif #endif
+typedef struct { +typedef struct {
+ char *path; + char status[2];
+ git_status_t status; + char path[PATH_MAX];
+} simple_git_status_t; +} git_status_t;
+
+typedef struct {
+ simple_git_status_t *statuses;
+ size_t len;
+} simple_git_statuses_t;
+ +
} <span class="insert">git_status_t;</span>
/* GLOBALS */ /* GLOBALS */
+simple_git_statuses_t git_statuses; +struct {
+ bool show;
+ size_t len;
+ git_status_t *statuses;
+} git_statuses;
/* Configuration, contexts */ /* Configuration, contexts */
static settings cfg = { static settings cfg = {
@@ -3464,6 +3488,80 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uc @@ -399,6 +411,7 @@ static settings cfg = {
har_t max, uchar_t id) 0, /* cliopener */
0, /* waitedit */
1, /* rollover */
+ 0, /* normalgit */
};
static context g_ctx[CTX_MAX] __attribute__ ((aligned));
@@ -3506,6 +3519,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uc
har_t max, uchar_t id)
return NULL; return NULL;
} }
+static git_column_status_t git_get_indexed_status(const uint32_t status) { +static size_t get_git_statuses(const char *path)
+ if (status & GIT_STATUS_INDEX_NEW) return GIT_COLUMN_STATUS_NEW; +{
+ if (status & GIT_STATUS_INDEX_MODIFIED) return GIT_COLUMN_STATUS_MODIF + static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
IED; + char workdir[PATH_MAX];
+ if (status & GIT_STATUS_INDEX_DELETED) return GIT_COLUMN_STATUS_DELET + FILE *fp = popen(gitrev, "r");
ED; +
+ if (status & GIT_STATUS_INDEX_RENAMED) return GIT_COLUMN_STATUS_RENAM + fgets(workdir, PATH_MAX, fp);
ED; + pclose(fp);
+ if (status & GIT_STATUS_INDEX_TYPECHANGE) return GIT_COLUMN_STATUS_TYPE_ +
CHANGE; + if (!workdir[0])
+ return GIT_COLUMN_STATUS_UNMOD; + return 0;
+} +
+ + static char gitstat[] = "git -c core.quotePath= status --porcelain --igno
+static git_column_status_t git_get_staged_status(const uint32_t status) { red=matching -u ";
+ if (status & GIT_STATUS_WT_NEW) return GIT_COLUMN_STATUS_NEW; + char pathspec[PATH_MAX], status[PATH_MAX];
+ if (status & GIT_STATUS_WT_MODIFIED) return GIT_COLUMN_STATUS_MODIFIED + size_t i = -1;
; + git_statuses.show = FALSE;
+ if (status & GIT_STATUS_WT_DELETED) return GIT_COLUMN_STATUS_DELETED; + workdir[xstrlen(workdir) - 1] = '\0';
+ if (status & GIT_STATUS_WT_RENAMED) return GIT_COLUMN_STATUS_RENAMED; + snprintf(pathspec, PATH_MAX, "%s%s%s 2>/dev/null", gitstat, path, cfg.sho
+ if (status & GIT_STATUS_WT_TYPECHANGE) return GIT_COLUMN_STATUS_TYPE_CHA whidden ? "" : "/*");
NGE; + fp = popen(pathspec, "r");
+ if (status & GIT_STATUS_IGNORED) return GIT_COLUMN_STATUS_IGNORED; +
+ if (status & GIT_STATUS_CONFLICTED) return GIT_COLUMN_STATUS_CONFLICT + while (fgets(status, PATH_MAX, fp)) {
ED; + size_t pathindex = (status[3] == '"') ? 4 : 3;
+ return GIT_COLUMN_STATUS_UNMOD; + status[xstrlen(status) - pathindex + 2] = '\0';
+} + git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(gi
+ t_status_t) * (++i + 1));
+static void print_gitstatus(git_column_status_t status) { + git_statuses.statuses[i].status[0] = (status[0] == ' ') ? '-' : s
+ switch (status) { tatus[0];
+ case GIT_COLUMN_STATUS_UNMOD: addch('-' | 0); break; + git_statuses.statuses[i].status[1] = (status[1] == ' ') ? '-' : s
+ case GIT_COLUMN_STATUS_NEW: addch('N' | (g_state.oldcolo tatus[1];
r ? COLOR_PAIR(1) : COLOR_PAIR(C_EXE))); break; + mkpath(workdir, status + pathindex, git_statuses.statuses[i].path
+ case GIT_COLUMN_STATUS_MODIFIED: addch('M' | (g_state.oldcolo );
r ? COLOR_PAIR(1) : COLOR_PAIR(4))); break;
+ case GIT_COLUMN_STATUS_DELETED: addch('D' | (g_state.oldcolo
r ? COLOR_PAIR(1) : COLOR_PAIR(C_UND))); break;
+ case GIT_COLUMN_STATUS_RENAMED: addch('R' | (g_state.oldcolo
r ? COLOR_PAIR(1) : COLOR_PAIR(C_CHR))); break;
+ case GIT_COLUMN_STATUS_TYPE_CHANGE: addch('T' | (g_state.oldcolo
r ? 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.oldcolo
r ? COLOR_PAIR(1) : COLOR_PAIR(C_UND))); break;
+ }
+}
+
+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_statu
s_t));
+
+ for (size_t i = 0; i < statuses.len; ++i) {
+ const git_status_entry *status_ent = git_status_byindex(s
tatus_list, i);
+ const char *entry_path = status_ent->head_to_index ? stat
us_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;
+}
+
+static void git_statuses_free(void) {
+ for (size_t i = 0; i < git_statuses.len; ++i)
+ free(git_statuses.statuses[i].path);
+ +
+ free(git_statuses.statuses); + pclose(fp);
+ git_statuses.len = 0; + return (i + 1);
+} +}
<span class="insert">return (i + 1);</span>
+ +
static void resetdircolor(int flags) static void resetdircolor(int flags)
{ {
/* Directories are always shown on top, clear the color when moving to fi rst file */ /* Directories are always shown on top, clear the color when moving to fi rst file */
@@ -3794,6 +3892,11 @@ static void printent(const struct entry *ent, uint_t name @@ -3843,6 +3889,10 @@ static void printent(const struct entry *ent, uint_t name
cols, bool sel) cols, bool sel)
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
if (attrs) + if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
attroff(attrs); + printw("%*s%c%c", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "",
+ ent->git_status[0], ent->git_status[1]);
+ +
+ if (git_statuses.len) { addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
+ print_gitstatus(ent->status_indexed);
+ print_gitstatus(ent->status_staged);
+ }
}
attrs = 0; if (g_state.oldcolor)
@@ -5119,6 +5222,10 @@ static int dentfill(char *path, struct entry **ppdents) @@ -5177,6 +5227,10 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1)); attron(COLOR_PAIR(cfg.curctx + 1));
} }
+ if (git_statuses.len) + char linkpath[PATH_MAX];
+ git_statuses_free(); + if ((git_statuses.len = get_git_statuses(path)))
+ git_statuses = statuses_from_path(path); + realpath(path, linkpath);
+ +
#if _POSIX_C_SOURCE >= 200112L #if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL); posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif #endif
@@ -5316,6 +5423,25 @@ static int dentfill(char *path, struct entry **ppdents) @@ -5375,6 +5429,34 @@ static int dentfill(char *path, struct entry **ppdents)
#endif #endif
} }
+ if (git_statuses.len) { + if (git_statuses.len) {
+ uint merged_status = 0; + char dentpath[PATH_MAX];
+ char *dentpath = abspath(dentp->name, path); + size_t pathlen = mkpath(linkpath, dentp->name, dentpath)
+ - 1;
+ for (size_t i = 0; i < git_statuses.len; ++i) { + dentp->git_status[0] = dentp->git_status[1] = '-';
+ if (dentp->flags & DIR_OR_LINK_TO_DIR) { +
+ size_t dentlen = xstrlen(dentpath); + if (dentp->flags & DIR_OR_DIRLNK) {
+ + for (size_t i = 0; i < git_statuses.len; ++i)
+ if ((dentlen <= xstrlen(git_statuses.stat + if (is_prefix(git_statuses.statuses[i].pa
uses[i].path)) && is_prefix(git_statuses.statuses[i].path, dentpath, dentlen)) th, dentpath, pathlen)) {
+ merged_status |= git_statuses.sta + dentp->git_status[0] = git_status
tuses[i].status; es.statuses[i].status[0];
+ } else if (!xstrcmp(git_statuses.statuses[i].path + dentp->git_status[1] = git_status
, dentpath)) es.statuses[i].status[1];
+ merged_status |= git_statuses.sta + if (dentp->git_status[1] != '!')
tuses[i].status; {
+ git_statuses.show = TRUE;
+ if (dentp->git_status[1]
== '?')
+ break;
+ }
+ }
+ } else {
+ for (size_t i = 0; i < git_statuses.len; ++i)
+ if (!xstrcmp(git_statuses.statuses[i].pat
h, dentpath)) {
+ dentp->git_status[0] = git_status
es.statuses[i].status[0];
+ dentp->git_status[1] = git_status
es.statuses[i].status[1];
+ if (dentp->git_status[1] != '!')
+ git_statuses.show = TRUE;
+ break;
+ }
+ } + }
+
+ dentp->status_indexed = git_get_indexed_status(merged_sta
tus);
+ dentp->status_staged = git_get_staged_status(merged_statu
s);
+ free(dentpath);
+ } + }
+ +
++ndents; ++ndents;
} while ((dp = readdir(dirp))); } while ((dp = readdir(dirp)));
@@ -5834,11 +5960,11 @@ static int adjust_cols(int n) @@ -5891,11 +5973,12 @@ static int adjust_cols(int n)
#endif #endif
if (cfg.showdetail) { if (cfg.showdetail) {
/* Fallback to light mode if less than 35 columns */ /* Fallback to light mode if less than 35 columns */
- if (n < 36) - if (n < 36)
+ if (n < 38) + if (n < 38)
cfg.showdetail ^= 1; cfg.showdetail ^= 1;
else { else /* 2 more accounted for below */
/* 2 more accounted for below */
- n -= 32; - n -= 32;
+ n -= 34; - }
} + n -= (git_statuses.show ? 34 : 32);
} + } else if (cfg.normalgit && git_statuses.show)
+ n -= 3;
@@ -7712,6 +7838,8 @@ static void cleanup(void)
/* 2 columns for preceding space and indicator */
return (n - 2);
@@ -7642,6 +7725,7 @@ static void usage(void)
" -F val fifo mode [0:preview 1:explore]\n"
#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"
@@ -7780,6 +7864,7 @@ static void cleanup(void)
fflush(stdout); fflush(stdout);
} }
#endif #endif
+ git_statuses_free(); + free(git_statuses.statuses);
+ git_libgit2_shutdown();
free(selpath); free(selpath);
<span class="insert">free(git_statuses.statuses);</span>
free(plgpath); free(plgpath);
free(cfgpath); free(cfgpath);
@@ -7907,6 +8035,7 @@ int main(int argc, char *argv[]) @@ -7823,7 +7908,7 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
atexit(cleanup);
+ git_libgit2_init();
/* Check if we are in path list mode */ while ((opt = (env_opts_id > 0
if (!isatty(STDIN_FILENO)) { ? env_opts[--env_opts_id]
- : getopt(argc, argv, "aAb:cCdDeEfF:gHJKl:nop:P:QrRs:St:T:u
UVwxh"))) != -1) {
+ : getopt(argc, argv, "aAb:cCdDeEfF:gGHJKl:nop:P:QrRs:St:T:
uUVwxh"))) != -1) {
switch (opt) {
#ifndef NOFIFO
case 'a':
@@ -7874,6 +7959,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;
 End of changes. 30 change blocks. 
183 lines changed or deleted 141 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)