summaryrefslogtreecommitdiffstats
path: root/patches/gitstatus/mainline.diff
blob: 64d39f9fd6712b3fc163c684871b0877f3cb5499 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# 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.
#
# Authors: Luuk van Baal, @crides

diff --git a/src/nnn.c b/src/nnn.c
index fe5d650..059c7bf 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -277,6 +277,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 {
 	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 {
 } session_header_t;
 #endif

+typedef struct {
+	char status[2];
+	char path[PATH_MAX];
+} git_status_t;
+
 /* GLOBALS */
+struct {
+	bool show;
+	size_t len;
+	git_status_t *statuses;
+} git_statuses;

 /* Configuration, contexts */
 static settings cfg = {
@@ -413,6 +425,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;
 }

+static size_t get_git_statuses(const char *path)
+{
+	static char gitrev[] = "git rev-parse --show-toplevel 2>/dev/null";
+	char workdir[PATH_MAX];
+	FILE *fp = popen(gitrev, "r");
+
+	fgets(workdir, PATH_MAX, fp);
+	pclose(fp);
+
+	if (!workdir[0])
+		return 0;
+
+	static char gitstat[] = "git -c core.quotePath= status --porcelain --ignored=matching -u ";
+	char pathspec[PATH_MAX], status[PATH_MAX];
+	size_t i = -1;
+	git_statuses.show = FALSE;
+	workdir[xstrlen(workdir) - 1] = '\0';
+	snprintf(pathspec, PATH_MAX, "%s\"%s\"%s 2>/dev/null", gitstat, path, cfg.showhidden ? "" : "/*");
+	fp = popen(pathspec, "r");
+
+	while (fgets(status, PATH_MAX, fp)) {
+		size_t pathindex = (status[3] == '"') ? 4 : 3;
+		status[xstrlen(status) - pathindex + 2] = '\0';
+		git_statuses.statuses = xrealloc(git_statuses.statuses, sizeof(git_status_t) * (++i + 1));
+		git_statuses.statuses[i].status[0] = (status[0] == ' ') ? '-' : status[0];
+		git_statuses.statuses[i].status[1] = (status[1] == ' ') ? '-' : status[1];
+		mkpath(workdir, status + pathindex, git_statuses.statuses[i].path);
+	}
+
+	pclose(fp);
+	return (i + 1);
+}
+
 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)

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

+	if (git_statuses.show && (cfg.showdetail || cfg.normalgit))
+		printw("%*s%c%c", (cfg.normalgit && !cfg.showdetail) ? 1 : 0, "",
+				ent->git_status[0], ent->git_status[1]);
+
 	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)
 		attron(COLOR_PAIR(cfg.curctx + 1));
 	}

+	char linkpath[PATH_MAX];
+	if ((git_statuses.len = get_git_statuses(path)))
+		realpath(path, linkpath);
+
 #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)
 #endif
 		}

+		if (git_statuses.len) {
+			char dentpath[PATH_MAX];
+			size_t pathlen = mkpath(linkpath, dentp->name, dentpath) - 1;
+			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] != '!') {
+							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].path, dentpath)) {
+						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] != '!')
+							git_statuses.show = TRUE;
+						break;
+					}
+			}
+		}
+
 		++ndents;
 	} while ((dp = readdir(dirp)));

@@ -6160,11 +6242,12 @@ 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 /* 2 more accounted for below */
-			n -= 32;
-	}
+			n -= (git_statuses.show ? 34 : 32);
+	} else if (cfg.normalgit && git_statuses.show)
+		n -= 3;

 	/* 2 columns for preceding space and indicator */
 	return (n - 2);
@@ -7913,6 +7996,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);
 	}
 #endif
+	free(git_statuses.statuses);
 	free(selpath);
 	free(plgpath);
 	free(cfgpath);
@@ -8095,7 +8180,7 @@ int main(int argc, char *argv[])

 	while ((opt = (env_opts_id > 0
 		       ? env_opts[--env_opts_id]
-		       : 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) {
 		switch (opt) {
 #ifndef NOFIFO
 		case 'a':
@@ -8146,6 +8231,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;