Skip to content

Commit

Permalink
Fix gitstatus and rebase patches
Browse files Browse the repository at this point in the history
  • Loading branch information
luukvbaal committed Sep 29, 2021
1 parent fc86152 commit 541b936
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 121 deletions.
62 changes: 32 additions & 30 deletions patches/gitstatus/mainline.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
# Authors: Luuk van Baal, @crides

diff --git a/src/nnn.c b/src/nnn.c
index fe5d650..059c7bf 100644
index 88263beb..597ff2b3 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -277,6 +277,7 @@ typedef struct entry {
@@ -285,6 +285,7 @@ typedef struct entry {
uid_t uid; /* 4 bytes */
gid_t gid; /* 4 bytes */
#endif
+ char git_status[2];
} *pEntry;

/* Selection marker */
@@ -333,6 +334,7 @@ typedef struct {
@@ -341,6 +342,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) */
@@ -382,7 +384,17 @@ typedef struct {
@@ -390,7 +392,17 @@ typedef struct {
} session_header_t;
#endif

Expand All @@ -43,28 +43,27 @@ index fe5d650..059c7bf 100644

/* Configuration, contexts */
static settings cfg = {
@@ -413,6 +425,7 @@ static settings cfg = {
@@ -421,6 +433,7 @@ static settings cfg = {
0, /* cliopener */
0, /* waitedit */
1, /* rollover */
+ 0, /* normalgit */
};

static context g_ctx[CTX_MAX] __attribute__ ((aligned));
@@ -3781,6 +3794,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
return NULL;
@@ -3814,6 +3827,38 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
return -1;
}

+static size_t get_git_statuses(const char *path)
+{
+ static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
+ char workdir[PATH_MAX];
+ char workdir[PATH_MAX], *ret;
+ FILE *fp = popen(gitrev, "r");
+
+ fgets(workdir, PATH_MAX, fp);
+ ret = fgets(workdir, PATH_MAX, fp);
+ pclose(fp);
+
+ if (!workdir[0])
+ if (!ret)
+ return 0;
+
+ static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
Expand All @@ -91,7 +90,7 @@ index fe5d650..059c7bf 100644
static void resetdircolor(int flags)
{
/* Directories are always shown on top, clear the color when moving to first file */
@@ -4118,6 +4164,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
@@ -4151,6 +4196,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)

uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);

Expand All @@ -102,36 +101,39 @@ index fe5d650..059c7bf 100644
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');

if (g_state.oldcolor)
@@ -5451,6 +5501,10 @@ static int dentfill(char *path, struct entry **ppdents)
@@ -5548,6 +5597,11 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1));
}

+ char linkpath[PATH_MAX];
+ if ((git_statuses.len = get_git_statuses(path)))
+ realpath(path, linkpath);
+ if (!realpath(path, linkpath))
+ printwarn(NULL);
+
#if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
@@ -5649,6 +5703,34 @@ static int dentfill(char *path, struct entry **ppdents)
@@ -5746,6 +5800,36 @@ static int dentfill(char *path, struct entry **ppdents)
#endif
}

+ if (git_statuses.len) {
+ char dentpath[PATH_MAX];
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
+ char dentpath[PATH_MAX], prefix[PATH_MAX + 1];
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
+ snprintf(prefix, PATH_MAX + 1, "%s/", dentpath);
+ 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] != '!') {
+ if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) {
+ if ((dentp->git_status[0] == '-') && (git_statuses.statuses[i].status[0] != '-'))
+ dentp->git_status[0] = git_statuses.statuses[i].status[0];
+ if ((dentp->git_status[1] == '-') && (git_statuses.statuses[i].status[1] != '-'))
+ dentp->git_status[1] = git_statuses.statuses[i].status[1];
+ if (git_statuses.statuses[i].status[1] != '!')
+ git_statuses.show = TRUE;
+ if (dentp->git_status[1] == '?')
+ break;
+ }
+ if ((dentp->git_status[0] != '-') && (dentp->git_status[1] != '-'))
+ break;
+ }
+ } else {
+ for (size_t i = 0; i < git_statuses.len; ++i)
Expand All @@ -148,7 +150,7 @@ index fe5d650..059c7bf 100644
++ndents;
} while ((dp = readdir(dirp)));

@@ -6160,11 +6242,12 @@ static int adjust_cols(int n)
@@ -6270,11 +6354,12 @@ static int adjust_cols(int n)
#endif
if (cfg.showdetail) {
/* Fallback to light mode if less than 35 columns */
Expand All @@ -164,23 +166,23 @@ index fe5d650..059c7bf 100644

/* 2 columns for preceding space and indicator */
return (n - 2);
@@ -7913,6 +7996,7 @@ static void usage(void)
@@ -8034,6 +8119,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"
@@ -8051,6 +8135,7 @@ static void cleanup(void)
fflush(stdout);
@@ -8174,6 +8260,7 @@ static void cleanup(void)
free(hostname);
}
#endif
+ free(git_statuses.statuses);
free(selpath);
free(plgpath);
free(cfgpath);
@@ -8095,7 +8180,7 @@ int main(int argc, char *argv[])
@@ -8218,7 +8305,7 @@ int main(int argc, char *argv[])

while ((opt = (env_opts_id > 0
? env_opts[--env_opts_id]
Expand All @@ -189,7 +191,7 @@ index fe5d650..059c7bf 100644
switch (opt) {
#ifndef NOFIFO
case 'a':
@@ -8146,6 +8231,9 @@ int main(int argc, char *argv[])
@@ -8269,6 +8356,9 @@ int main(int argc, char *argv[])
cfg.regex = 1;
filterfn = &visible_re;
break;
Expand Down
64 changes: 33 additions & 31 deletions patches/gitstatus/namefirst.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
# Authors: Luuk van Baal, @crides

diff --git a/src/nnn.c b/src/nnn.c
index 2d33716..b190177 100644
index 55f32e73..ed771826 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -277,6 +277,7 @@ typedef struct entry {
@@ -285,6 +285,7 @@ typedef struct entry {
uid_t uid; /* 4 bytes */
gid_t gid; /* 4 bytes */
#endif
+ char git_status[2];
} *pEntry;

/* Selection marker */
@@ -333,6 +334,7 @@ typedef struct {
@@ -341,6 +342,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) */
@@ -386,7 +388,17 @@ static struct {
@@ -394,7 +396,17 @@ static struct {
ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
} dtls;

Expand All @@ -44,28 +44,27 @@ index 2d33716..b190177 100644

/* Configuration, contexts */
static settings cfg = {
@@ -417,6 +429,7 @@ static settings cfg = {
@@ -425,6 +437,7 @@ static settings cfg = {
0, /* cliopener */
0, /* waitedit */
1, /* rollover */
+ 0, /* normalgit */
};

static context g_ctx[CTX_MAX] __attribute__ ((aligned));
@@ -3789,6 +3802,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
return NULL;
@@ -3822,6 +3835,38 @@ static int get_kv_key(kv *kvarr, char *val, uchar_t max, uchar_t id)
return -1;
}

+static size_t get_git_statuses(const char *path)
+{
+ static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
+ char workdir[PATH_MAX];
+ char workdir[PATH_MAX], *ret;
+ FILE *fp = popen(gitrev, "r");
+
+ fgets(workdir, PATH_MAX, fp);
+ ret = fgets(workdir, PATH_MAX, fp);
+ pclose(fp);
+
+ if (!workdir[0])
+ if (!ret)
+ return 0;
+
+ static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
Expand All @@ -92,7 +91,7 @@ index 2d33716..b190177 100644
static void resetdircolor(int flags)
{
/* Directories are always shown on top, clear the color when moving to first file */
@@ -4099,6 +4145,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
@@ -4132,6 +4177,9 @@ 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);

Expand All @@ -102,36 +101,39 @@ index 2d33716..b190177 100644
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');

if (g_state.oldcolor)
@@ -5457,6 +5506,10 @@ static int dentfill(char *path, struct entry **ppdents)
@@ -5554,6 +5602,11 @@ static int dentfill(char *path, struct entry **ppdents)
attron(COLOR_PAIR(cfg.curctx + 1));
}

+ char linkpath[PATH_MAX];
+ if ((git_statuses.len = get_git_statuses(path)))
+ realpath(path, linkpath);
+ if (!realpath(path, linkpath))
+ printwarn(NULL);
+
#if _POSIX_C_SOURCE >= 200112L
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
#endif
@@ -5655,6 +5708,34 @@ static int dentfill(char *path, struct entry **ppdents)
@@ -5752,6 +5805,36 @@ static int dentfill(char *path, struct entry **ppdents)
#endif
}

+ if (git_statuses.len) {
+ char dentpath[PATH_MAX];
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
+ char dentpath[PATH_MAX], prefix[PATH_MAX + 1];
+ size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
+ snprintf(prefix, PATH_MAX + 1, "%s/", dentpath);
+ 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] != '!') {
+ if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) {
+ if ((dentp->git_status[0] == '-') && (git_statuses.statuses[i].status[0] != '-'))
+ dentp->git_status[0] = git_statuses.statuses[i].status[0];
+ if ((dentp->git_status[1] == '-') && (git_statuses.statuses[i].status[1] != '-'))
+ dentp->git_status[1] = git_statuses.statuses[i].status[1];
+ if (git_statuses.statuses[i].status[1] != '!')
+ git_statuses.show = TRUE;
+ if (dentp->git_status[1] == '?')
+ break;
+ }
+ if ((dentp->git_status[0] != '-') && (dentp->git_status[1] != '-'))
+ break;
+ }
+ } else {
+ for (size_t i = 0; i < git_statuses.len; ++i)
Expand All @@ -148,7 +150,7 @@ index 2d33716..b190177 100644
++ndents;
} while ((dp = readdir(dirp)));

@@ -6157,7 +6238,8 @@ static int adjust_cols(int n)
@@ -6267,7 +6350,8 @@ static int adjust_cols(int n)
cfg.showdetail ^= 1;
else /* 2 more accounted for below */
n -= (dtls.maxentln - 2 - dtls.maxnameln);
Expand All @@ -158,7 +160,7 @@ index 2d33716..b190177 100644

/* 2 columns for preceding space and indicator */
return (n - 2);
@@ -6312,7 +6394,7 @@ static void redraw(char *path)
@@ -6422,7 +6506,7 @@ static void redraw(char *path)
}
#endif
}
Expand All @@ -167,23 +169,23 @@ index 2d33716..b190177 100644
}

ncols = adjust_cols(ncols);
@@ -7919,6 +8001,7 @@ static void usage(void)
@@ -8040,6 +8124,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"
@@ -8057,6 +8140,7 @@ static void cleanup(void)
fflush(stdout);
@@ -8180,6 +8265,7 @@ static void cleanup(void)
free(hostname);
}
#endif
+ free(git_statuses.statuses);
free(selpath);
free(plgpath);
free(cfgpath);
@@ -8101,7 +8185,7 @@ int main(int argc, char *argv[])
@@ -8224,7 +8310,7 @@ int main(int argc, char *argv[])

while ((opt = (env_opts_id > 0
? env_opts[--env_opts_id]
Expand All @@ -192,7 +194,7 @@ index 2d33716..b190177 100644
switch (opt) {
#ifndef NOFIFO
case 'a':
@@ -8152,6 +8236,9 @@ int main(int argc, char *argv[])
@@ -8275,6 +8361,9 @@ int main(int argc, char *argv[])
cfg.regex = 1;
filterfn = &visible_re;
break;
Expand Down
Loading

0 comments on commit 541b936

Please sign in to comment.