Skip to content

Commit 20d37ef

Browse files
paskyJunio C Hamano
authored andcommitted
Steal -t option to git-ls-files from Cogito fork.
This backports the -t option git-ls-files in Cogito added to the Linus version. Signed-off-by: Petr Baudis <pasky@ucw.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent e78d977 commit 20d37ef

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

Documentation/core-git.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,14 @@ shown:
10221022
the diff command:
10231023
git-ls-files --others --exclude-from=dontdiff
10241024

1025+
-t
1026+
Identify the file status with the following tags (followed by
1027+
a space) at the start of each line:
1028+
H cached
1029+
M unmerged
1030+
R removed/deleted
1031+
? other
1032+
10251033
Output
10261034
show files just outputs the filename unless --stage is specified in
10271035
which case it outputs:

ls-files.c

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ static int show_stage = 0;
1818
static int show_unmerged = 0;
1919
static int line_terminator = '\n';
2020

21+
static const char *tag_cached = "";
22+
static const char *tag_unmerged = "";
23+
static const char *tag_removed = "";
24+
static const char *tag_other = "";
25+
2126
static int nr_excludes;
2227
static const char **excludes;
2328
static int excludes_alloc;
@@ -143,7 +148,8 @@ static void read_directory(const char *path, const char *base, int baselen)
143148
/* fallthrough */
144149
case DT_DIR:
145150
memcpy(fullname + baselen + len, "/", 2);
146-
read_directory(fullname, fullname, baselen + len + 1);
151+
read_directory(fullname, fullname,
152+
baselen + len + 1);
147153
continue;
148154
case DT_REG:
149155
break;
@@ -172,7 +178,7 @@ static void show_files(void)
172178
read_directory(".", "", 0);
173179
qsort(dir, nr_dir, sizeof(char *), cmp_name);
174180
for (i = 0; i < nr_dir; i++)
175-
printf("%s%c", dir[i], line_terminator);
181+
printf("%s%s%c", tag_other, dir[i], line_terminator);
176182
}
177183
if (show_cached | show_stage) {
178184
for (i = 0; i < active_nr; i++) {
@@ -182,14 +188,17 @@ static void show_files(void)
182188
if (show_unmerged && !ce_stage(ce))
183189
continue;
184190
if (!show_stage)
185-
printf("%s%c", ce->name, line_terminator);
191+
printf("%s%s%c",
192+
ce_stage(ce) ? tag_unmerged :
193+
tag_cached,
194+
ce->name, line_terminator);
186195
else
187-
printf(/* "%06o %s %d %10d %s%c", */
188-
"%06o %s %d %s%c",
196+
printf("%s%06o %s %d %s%c",
197+
ce_stage(ce) ? tag_unmerged :
198+
tag_cached,
189199
ntohl(ce->ce_mode),
190200
sha1_to_hex(ce->sha1),
191201
ce_stage(ce),
192-
/* ntohl(ce->ce_size), */
193202
ce->name, line_terminator);
194203
}
195204
}
@@ -201,13 +210,14 @@ static void show_files(void)
201210
continue;
202211
if (!lstat(ce->name, &st))
203212
continue;
204-
printf("%s%c", ce->name, line_terminator);
213+
printf("%s%s%c", tag_removed, ce->name,
214+
line_terminator);
205215
}
206216
}
207217
}
208218

209219
static const char *ls_files_usage =
210-
"ls-files [-z] (--[cached|deleted|others|stage|unmerged])* "
220+
"ls-files [-z] [-t] (--[cached|deleted|others|stage|unmerged])* "
211221
"[ --ignored [--exclude=<pattern>] [--exclude-from=<file>) ]";
212222

213223
int main(int argc, char **argv)
@@ -219,6 +229,11 @@ int main(int argc, char **argv)
219229

220230
if (!strcmp(arg, "-z")) {
221231
line_terminator = 0;
232+
} else if (!strcmp(arg, "-t")) {
233+
tag_cached = "H ";
234+
tag_unmerged = "M ";
235+
tag_removed = "R ";
236+
tag_other = "? ";
222237
} else if (!strcmp(arg, "-c") || !strcmp(arg, "--cached")) {
223238
show_cached = 1;
224239
} else if (!strcmp(arg, "-d") || !strcmp(arg, "--deleted")) {
@@ -230,7 +245,9 @@ int main(int argc, char **argv)
230245
} else if (!strcmp(arg, "-s") || !strcmp(arg, "--stage")) {
231246
show_stage = 1;
232247
} else if (!strcmp(arg, "-u") || !strcmp(arg, "--unmerged")) {
233-
// There's no point in showing unmerged unless you also show the stage information
248+
/* There's no point in showing unmerged unless
249+
* you also show the stage information.
250+
*/
234251
show_stage = 1;
235252
show_unmerged = 1;
236253
} else if (!strcmp(arg, "-x") && i+1 < argc) {
@@ -246,7 +263,8 @@ int main(int argc, char **argv)
246263
}
247264

248265
if (show_ignored && !nr_excludes) {
249-
fprintf(stderr, "%s: --ignored needs some exclude pattern\n", argv[0]);
266+
fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
267+
argv[0]);
250268
exit(1);
251269
}
252270

0 commit comments

Comments
 (0)