7
7
# Authors: Luuk van Baal, @crides
8
8
9
9
diff --git a/src/nnn.c b/src/nnn.c
10
- index 2d33716..b190177 100644
10
+ index 55f32e73..ed771826 100644
11
11
--- a/src/nnn.c
12
12
+++ b/src/nnn.c
13
- @@ -277 ,6 +277 ,7 @@ typedef struct entry {
13
+ @@ -285 ,6 +285 ,7 @@ typedef struct entry {
14
14
uid_t uid; /* 4 bytes */
15
15
gid_t gid; /* 4 bytes */
16
16
#endif
17
17
+ char git_status[2];
18
18
} *pEntry;
19
19
20
20
/* Selection marker */
21
- @@ -333 ,6 +334 ,7 @@ typedef struct {
21
+ @@ -341 ,6 +342 ,7 @@ typedef struct {
22
22
uint_t cliopener : 1; /* All-CLI app opener */
23
23
uint_t waitedit : 1; /* For ops that can't be detached, used EDITOR */
24
24
uint_t rollover : 1; /* Roll over at edges */
25
25
+ uint_t normalgit : 1; /* Show git status in normal mode */
26
26
} settings;
27
27
28
28
/* Non-persistent program-internal states (alphabeical order) */
29
- @@ -386 ,7 +388 ,17 @@ static struct {
29
+ @@ -394 ,7 +396 ,17 @@ static struct {
30
30
ushort_t maxnameln, maxsizeln, maxuidln, maxgidln, maxentln, uidln, gidln, printguid;
31
31
} dtls;
32
32
@@ -44,28 +44,27 @@ index 2d33716..b190177 100644
44
44
45
45
/* Configuration, contexts */
46
46
static settings cfg = {
47
- @@ -417 ,6 +429 ,7 @@ static settings cfg = {
47
+ @@ -425 ,6 +437 ,7 @@ static settings cfg = {
48
48
0, /* cliopener */
49
49
0, /* waitedit */
50
50
1, /* rollover */
51
51
+ 0, /* normalgit */
52
52
};
53
53
54
54
static context g_ctx[CTX_MAX] __attribute__ ((aligned));
55
- @@ -3789 ,6 +3802,39 @@ static char *get_kv_val (kv *kvarr, char *buf, int key , uchar_t max, uchar_t id)
56
- return NULL ;
55
+ @@ -3822 ,6 +3835,38 @@ static int get_kv_key (kv *kvarr, char *val , uchar_t max, uchar_t id)
56
+ return -1 ;
57
57
}
58
58
59
59
+ static size_t get_git_statuses(const char *path)
60
60
+ {
61
61
+ static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
62
- + char workdir[PATH_MAX];
62
+ + char workdir[PATH_MAX], *ret ;
63
63
+ FILE *fp = popen(gitrev, "r");
64
64
+
65
- + fgets(workdir, PATH_MAX, fp);
65
+ + ret = fgets(workdir, PATH_MAX, fp);
66
66
+ pclose(fp);
67
- +
68
- + if (!workdir[0])
67
+ + if (!ret)
69
68
+ return 0;
70
69
+
71
70
+ static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
@@ -92,7 +91,7 @@ index 2d33716..b190177 100644
92
91
static void resetdircolor(int flags)
93
92
{
94
93
/* Directories are always shown on top, clear the color when moving to first file */
95
- @@ -4099 ,6 +4145 ,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
94
+ @@ -4132 ,6 +4177 ,9 @@ static void printent(const struct entry *ent, uint_t namecols, bool sel)
96
95
int attrs = 0, namelen;
97
96
uchar_t color_pair = get_color_pair_name_ind(ent, &ind, &attrs);
98
97
@@ -102,36 +101,39 @@ index 2d33716..b190177 100644
102
101
addch((ent->flags & FILE_SELECTED) ? '+' | A_REVERSE | A_BOLD : ' ');
103
102
104
103
if (g_state.oldcolor)
105
- @@ -5457 ,6 +5506,10 @@ static int dentfill(char *path, struct entry **ppdents)
104
+ @@ -5554 ,6 +5602,11 @@ static int dentfill(char *path, struct entry **ppdents)
106
105
attron(COLOR_PAIR(cfg.curctx + 1));
107
106
}
108
107
109
108
+ char linkpath[PATH_MAX];
110
109
+ if ((git_statuses.len = get_git_statuses(path)))
111
- + realpath(path, linkpath);
110
+ + if (!realpath(path, linkpath))
111
+ + printwarn(NULL);
112
112
+
113
113
#if _POSIX_C_SOURCE >= 200112L
114
114
posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
115
115
#endif
116
- @@ -5655 ,6 +5708,34 @@ static int dentfill(char *path, struct entry **ppdents)
116
+ @@ -5752 ,6 +5805,36 @@ static int dentfill(char *path, struct entry **ppdents)
117
117
#endif
118
118
}
119
119
120
120
+ if (git_statuses.len) {
121
- + char dentpath[PATH_MAX];
122
- + size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
121
+ + char dentpath[PATH_MAX], prefix[PATH_MAX + 1];
122
+ + size_t pathlen = mkpath(linkpath, dentp->name, dentpath);
123
+ + snprintf(prefix, PATH_MAX + 1, "%s/", dentpath);
123
124
+ dentp->git_status[0] = dentp->git_status[1] = '-';
124
125
+
125
126
+ if (dentp->flags & DIR_OR_DIRLNK) {
126
127
+ 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] != '!') {
128
+ + if (is_prefix(git_statuses.statuses[i].path, prefix, pathlen)) {
129
+ + if ((dentp->git_status[0] == '-') && (git_statuses.statuses[i].status[0] != '-'))
130
+ + dentp->git_status[0] = git_statuses.statuses[i].status[0];
131
+ + if ((dentp->git_status[1] == '-') && (git_statuses.statuses[i].status[1] != '-'))
132
+ + dentp->git_status[1] = git_statuses.statuses[i].status[1];
133
+ + if (git_statuses.statuses[i].status[1] != '!')
131
134
+ git_statuses.show = TRUE;
132
- + if (dentp->git_status[1] == '?')
133
- + break;
134
- + }
135
+ + if ((dentp->git_status[0] != '-') && (dentp->git_status[1] != '-'))
136
+ + break;
135
137
+ }
136
138
+ } else {
137
139
+ for (size_t i = 0; i < git_statuses.len; ++i)
@@ -148,7 +150,7 @@ index 2d33716..b190177 100644
148
150
++ndents;
149
151
} while ((dp = readdir(dirp)));
150
152
151
- @@ -6157 ,7 +6238 ,8 @@ static int adjust_cols(int n)
153
+ @@ -6267 ,7 +6350 ,8 @@ static int adjust_cols(int n)
152
154
cfg.showdetail ^= 1;
153
155
else /* 2 more accounted for below */
154
156
n -= (dtls.maxentln - 2 - dtls.maxnameln);
@@ -158,7 +160,7 @@ index 2d33716..b190177 100644
158
160
159
161
/* 2 columns for preceding space and indicator */
160
162
return (n - 2);
161
- @@ -6312 ,7 +6394 ,7 @@ static void redraw(char *path)
163
+ @@ -6422 ,7 +6506 ,7 @@ static void redraw(char *path)
162
164
}
163
165
#endif
164
166
}
@@ -167,23 +169,23 @@ index 2d33716..b190177 100644
167
169
}
168
170
169
171
ncols = adjust_cols(ncols);
170
- @@ -7919 ,6 +8001 ,7 @@ static void usage(void)
172
+ @@ -8040 ,6 +8124 ,7 @@ static void usage(void)
171
173
" -F val fifo mode [0:preview 1:explore]\n"
172
174
#endif
173
175
" -g regex filters\n"
174
176
+ " -G always show git status\n"
175
177
" -H show hidden files\n"
176
178
" -J no auto-proceed on select\n"
177
179
" -K detect key collision\n"
178
- @@ -8057 ,6 +8140 ,7 @@ static void cleanup(void)
179
- fflush(stdout );
180
+ @@ -8180 ,6 +8265 ,7 @@ static void cleanup(void)
181
+ free(hostname );
180
182
}
181
183
#endif
182
184
+ free(git_statuses.statuses);
183
185
free(selpath);
184
186
free(plgpath);
185
187
free(cfgpath);
186
- @@ -8101 ,7 +8185 ,7 @@ int main(int argc, char *argv[])
188
+ @@ -8224 ,7 +8310 ,7 @@ int main(int argc, char *argv[])
187
189
188
190
while ((opt = (env_opts_id > 0
189
191
? env_opts[--env_opts_id]
@@ -192,7 +194,7 @@ index 2d33716..b190177 100644
192
194
switch (opt) {
193
195
#ifndef NOFIFO
194
196
case 'a':
195
- @@ -8152 ,6 +8236 ,9 @@ int main(int argc, char *argv[])
197
+ @@ -8275 ,6 +8361 ,9 @@ int main(int argc, char *argv[])
196
198
cfg.regex = 1;
197
199
filterfn = &visible_re;
198
200
break;
0 commit comments