"Fossies" - the Fresh Open Source Software Archive

Member "nnn-4.2/patches/gitstatus/mainline.diff" (21 Jul 2021, 6133 Bytes) of package /linux/misc/nnn-v4.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Diff source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "mainline.diff": v4.1.1_vs_v4.2.

    1 # Description: Add git status column to detail mode. Provides additional
    2 #              command line flag -G which will render the git status
    3 #              column also in normal mode. nnn.vim users may consider
    4 #              adding `let g:nnn#command = 'nnn -G' to their vim config.
    5 #
    6 # Authors: Luuk van Baal, @crides
    7 
    8 diff --git a/src/nnn.c b/src/nnn.c
    9 index c4bced5..a4f2a39 100644
   10 --- a/src/nnn.c
   11 +++ b/src/nnn.c
   12 @@ -269,6 +269,7 @@ typedef struct entry {
   13     uid_t uid; /* 4 bytes */
   14     gid_t gid; /* 4 bytes */
   15  #endif
   16 +   char git_status[2];
   17  } *pEntry;
   18 
   19  /* Key-value pairs from env */
   20 @@ -319,6 +320,7 @@ typedef struct {
   21     uint_t cliopener  : 1;  /* All-CLI app opener */
   22     uint_t waitedit   : 1;  /* For ops that can't be detached, used EDITOR */
   23     uint_t rollover   : 1;  /* Roll over at edges */
   24 +   uint_t normalgit  : 1;  /* Show git status in normal mode */
   25  } settings;
   26 
   27  /* Non-persistent program-internal states (alphabeical order) */
   28 @@ -368,7 +370,17 @@ typedef struct {
   29  } session_header_t;
   30  #endif
   31 
   32 +typedef struct {
   33 +   char status[2];
   34 +   char path[PATH_MAX];
   35 +} git_status_t;
   36 +
   37  /* GLOBALS */
   38 +struct {
   39 +   bool show;
   40 +   size_t len;
   41 +   git_status_t *statuses;
   42 +} git_statuses;
   43 
   44  /* Configuration, contexts */
   45  static settings cfg = {
   46 @@ -399,6 +411,7 @@ static settings cfg = {
   47     0, /* cliopener */
   48     0, /* waitedit */
   49     1, /* rollover */
   50 +   0, /* normalgit */
   51  };
   52 
   53  static context g_ctx[CTX_MAX] __attribute__ ((aligned));
   54 @@ -3506,6 +3519,39 @@ static char *get_kv_val(kv *kvarr, char *buf, int key, uchar_t max, uchar_t id)
   55     return NULL;
   56  }
   57 
   58 +static size_t get_git_statuses(const char *path)
   59 +{
   60 +   static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
   61 +   char workdir[PATH_MAX];
   62 +   FILE *fp = popen(gitrev, "r");
   63 +
   64 +   fgets(workdir, PATH_MAX, fp);
   65 +   pclose(fp);
   66 +
   67 +   if (!workdir[0])
   68 +       return 0;
   69 +
   70 +   static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
   71 +   char pathspec[PATH_MAX], status[PATH_MAX];
   72 +   size_t i = -1;
   73 +   git_statuses.show = FALSE;
   74 +   workdir[xstrlen(workdir) - 1] = '\0';
   75 +   snprintf(pathspec, PATH_MAX, "%s%s%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*");
   76 +   fp = popen(pathspec, "r");
   77 +
   78 +   while (fgets(status, PATH_MAX, fp)) {
   79 +       size_t pathindex = (status[3] == '"') ? 4 : 3;
   80 +       status[xstrlen(status) - pathindex + 2] = '\0';
   81 +       git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1));
   82 +       git_statuses.statuses[i].status[0] = (status[0] == ' ') ? '-' : status[0];
   83 +       git_statuses.statuses[i].status[1] = (status[1] == ' ') ? '-' : status[1];
   84 +       mkpath(workdir, status + pathindex, git_statuses.statuses[i].path);
   85 +   }
   86 +
   87 +   pclose(fp);
   88 +   return (i + 1);
   89 +}
   90 +
   91  static void resetdircolor(int flags)
   92  {
   93     /* Directories are always shown on top, clear the color when moving to first file */
   94 @@ -3843,6 +3889,10 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
   95 
   96     uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
   97 
   98 +   if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
   99 +       printw("%*s%c%c", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "",
  100 +               ent->git_status[0], ent->git_status[1]);
  101 +
  102     addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
  103 
  104     if (g_state.oldcolor)
  105 @@ -5177,6 +5227,10 @@ static int dentfill(char *path, struct entry **ppdents)
  106         attron(COLOR_PAIR(cfg.curctx + 1));
  107     }
  108 
  109 +   char linkpath[PATH_MAX];
  110 +   if ((git_statuses.len = get_git_statuses(path)))
  111 +       realpath(path, linkpath);
  112 +
  113  #if _POSIX_C_SOURCE >= 200112L
  114     posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
  115  #endif
  116 @@ -5375,6 +5429,34 @@ static int dentfill(char *path, struct entry **ppdents)
  117  #endif
  118         }
  119 
  120 +       if (git_statuses.len) {
  121 +           char dentpath[PATH_MAX];
  122 +           size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
  123 +           dentp->git_status[0] = dentp->git_status[1] = '-';
  124 +
  125 +           if (dentp->flags & DIR_OR_DIRLNK) {
  126 +               for (size_t i = 0; i < git_statuses.len; ++i)
  127 +                   if (is_prefix(git_statuses.statuses[i].path, dentpath, pathlen)) {
  128 +                       dentp->git_status[0] = git_statuses.statuses[i].status[0];
  129 +                       dentp->git_status[1] = git_statuses.statuses[i].status[1];
  130 +                       if (dentp->git_status[1] != '!') {
  131 +                           git_statuses.show = TRUE;
  132 +                           if (dentp->git_status[1] == '?')
  133 +                               break;
  134 +                       }
  135 +                   }
  136 +           } else {
  137 +               for (size_t i = 0; i < git_statuses.len; ++i)
  138 +                   if (!xstrcmp(git_statuses.statuses[i].path, dentpath)) {
  139 +                       dentp->git_status[0] = git_statuses.statuses[i].status[0];
  140 +                       dentp->git_status[1] = git_statuses.statuses[i].status[1];
  141 +                       if (dentp->git_status[1] != '!')
  142 +                           git_statuses.show = TRUE;
  143 +                       break;
  144 +                   }
  145 +           }
  146 +       }
  147 +
  148         ++ndents;
  149     } while ((dp = readdir(dirp)));
  150 
  151 @@ -5891,11 +5973,12 @@ static int adjust_cols(int n)
  152  #endif
  153     if (cfg.showdetail) {
  154         /* Fallback to light mode if less than 35 columns */
  155 -       if (n < 36)
  156 +       if (n < 38)
  157             cfg.showdetail ^= 1;
  158         else /* 2 more accounted for below */
  159 -           n -= 32;
  160 -   }
  161 +           n -= (git_statuses.show ? 34 : 32);
  162 +   } else if (cfg.normalgit && git_statuses.show)
  163 +       n -= 3;
  164 
  165     /* 2 columns for preceding space and indicator */
  166     return (n - 2);
  167 @@ -7642,6 +7725,7 @@ static void usage(void)
  168         " -F val  fifo mode [0:preview 1:explore]\n"
  169  #endif
  170         " -g      regex filters\n"
  171 +       " -G      always show git status\n"
  172         " -H      show hidden files\n"
  173         " -J      no auto-proceed on select\n"
  174         " -K      detect key collision\n"
  175 @@ -7780,6 +7864,7 @@ static void cleanup(void)
  176         fflush(stdout);
  177     }
  178  #endif
  179 +   free(git_statuses.statuses);
  180     free(selpath);
  181     free(plgpath);
  182     free(cfgpath);
  183 @@ -7823,7 +7908,7 @@ int main(int argc, char *argv[])
  184 
  185     while ((opt = (env_opts_id > 0
  186                ? env_opts[--env_opts_id]
  187 -              : getopt(argc, argv, "aAb:cCdDeEfF:gHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
  188 +              : getopt(argc, argv, "aAb:cCdDeEfF:gGHJKl:nop:P:QrRs:St:T:uUVwxh"))) != -1) {
  189         switch (opt) {
  190  #ifndef NOFIFO
  191         case 'a':
  192 @@ -7874,6 +7959,9 @@ int main(int argc, char *argv[])
  193             cfg.regex = 1;
  194             filterfn = &visible_re;
  195             break;
  196 +       case 'G':
  197 +           cfg.normalgit = 1;
  198 +           break;
  199         case 'H':
  200             cfg.showhidden = 1;
  201             break;