Skip to content

Commit 24c297d

Browse files
jeffhostetlermjcheetham
authored andcommitted
status: add status serialization mechanism
Teach STATUS to optionally serialize the results of a status computation to a file. Teach STATUS to optionally read an existing serialization file and simply print the results, rather than actually scanning. This is intended for immediate status results on extremely large repos and assumes the use of a service/daemon to maintain a fresh current status snapshot. 2021-10-30: packet_read() changed its prototype in ec9a37d (pkt-line.[ch]: remove unused packet_read_line_buf(), 2021-10-14). 2021-10-30: sscanf() now does an extra check that "%d" goes into an "int" and complains about "uint32_t". Replacing with "%u" fixes the compile-time error. 2021-10-30: string_list_init() was removed by abf897b (string-list.[ch]: remove string_list_init() compatibility function, 2021-09-28), so we need to initialize manually. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Derrick Stolee <[email protected]>
1 parent 4783edb commit 24c297d

16 files changed

+1356
-4
lines changed

Documentation/config/status.adoc

+6
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,9 @@ status.submoduleSummary::
7777
the --ignore-submodules=dirty command-line option or the 'git
7878
submodule summary' command, which shows a similar output but does
7979
not honor these settings.
80+
81+
status.deserializePath::
82+
EXPERIMENTAL, Pathname to a file containing cached status results
83+
generated by `--serialize`. This will be overridden by
84+
`--deserialize=<path>` on the command line. If the cache file is
85+
invalid or stale, git will fall-back and compute status normally.

Documentation/git-status.adoc

+33
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ ignored, then the directory is not shown, but all contents are shown.
151151
threshold.
152152
See also linkgit:git-diff[1] `--find-renames`.
153153

154+
--serialize[=<version>]::
155+
(EXPERIMENTAL) Serialize raw status results to stdout in a
156+
format suitable for use by `--deserialize`. Valid values for
157+
`<version>` are "1" and "v1".
158+
159+
--deserialize[=<path>]::
160+
(EXPERIMENTAL) Deserialize raw status results from a file or
161+
stdin rather than scanning the worktree. If `<path>` is omitted
162+
and `status.deserializePath` is unset, input is read from stdin.
163+
--no-deserialize::
164+
(EXPERIMENTAL) Disable implicit deserialization of status results
165+
from the value of `status.deserializePath`.
166+
154167
<pathspec>...::
155168
See the 'pathspec' entry in linkgit:gitglossary[7].
156169

@@ -424,6 +437,26 @@ quoted as explained for the configuration variable `core.quotePath`
424437
(see linkgit:git-config[1]).
425438
426439
440+
SERIALIZATION and DESERIALIZATION (EXPERIMENTAL)
441+
------------------------------------------------
442+
443+
The `--serialize` option allows git to cache the result of a
444+
possibly time-consuming status scan to a binary file. A local
445+
service/daemon watching file system events could use this to
446+
periodically pre-compute a fresh status result.
447+
448+
Interactive users could then use `--deserialize` to simply
449+
(and immediately) print the last-known-good result without
450+
waiting for the status scan.
451+
452+
The binary serialization file format includes some worktree state
453+
information allowing `--deserialize` to reject the cached data
454+
and force a normal status scan if, for example, the commit, branch,
455+
or status modes/options change. The format cannot, however, indicate
456+
when the cached data is otherwise stale -- that coordination belongs
457+
to the task driving the serializations.
458+
459+
427460
CONFIGURATION
428461
-------------
429462
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
Git status serialization format
2+
===============================
3+
4+
Git status serialization enables git to dump the results of a status scan
5+
to a binary file. This file can then be loaded by later status invocations
6+
to print the cached status results.
7+
8+
The file contains the essential fields from:
9+
() the index
10+
() the "struct wt_status" for the overall results
11+
() the contents of "struct wt_status_change_data" for tracked changed files
12+
() the list of untracked and ignored files
13+
14+
Version 1 Format:
15+
=================
16+
17+
The V1 file begins with a required header section followed by optional
18+
sections for each type of item (changed, untracked, ignored). Individual
19+
item sections are only present if necessary. Each item section begins
20+
with an item-type header with the number of items in the section.
21+
22+
Each "line" in the format is encoded using pkt-line with a final LF.
23+
Flush packets are used to terminate sections.
24+
25+
-----------------
26+
PKT-LINE("version" SP "1")
27+
<v1-header-section>
28+
[<v1-changed-item-section>]
29+
[<v1-untracked-item-section>]
30+
[<v1-ignored-item-section>]
31+
-----------------
32+
33+
34+
V1 Header
35+
---------
36+
37+
The v1-header-section fields are taken directly from "struct wt_status".
38+
Each field is printed on a separate pkt-line. Lines for NULL string
39+
values are omitted. All integers are printed with "%d". OIDs are
40+
printed in hex.
41+
42+
v1-header-section = <v1-index-headers>
43+
<v1-wt-status-headers>
44+
PKT-LINE(<flush>)
45+
46+
v1-index-headers = PKT-LINE("index_mtime" SP <sec> SP <nsec> LF)
47+
48+
v1-wt-status-headers = PKT-LINE("is_initial" SP <integer> LF)
49+
[ PKT-LINE("branch" SP <branch-name> LF) ]
50+
[ PKT-LINE("reference" SP <reference-name> LF) ]
51+
PKT-LINE("show_ignored_files" SP <integer> LF)
52+
PKT-LINE("show_untracked_files" SP <integer> LF)
53+
PKT-LINE("show_ignored_directory" SP <integer> LF)
54+
[ PKT-LINE("ignore_submodule_arg" SP <string> LF) ]
55+
PKT-LINE("detect_rename" SP <integer> LF)
56+
PKT-LINE("rename_score" SP <integer> LF)
57+
PKT-LINE("rename_limit" SP <integer> LF)
58+
PKT-LINE("detect_break" SP <integer> LF)
59+
PKT-LINE("sha1_commit" SP <oid> LF)
60+
PKT-LINE("committable" SP <integer> LF)
61+
PKT-LINE("workdir_dirty" SP <integer> LF)
62+
63+
64+
V1 Changed Items
65+
----------------
66+
67+
The v1-changed-item-section lists all of the changed items with one
68+
item per pkt-line. Each pkt-line contains: a binary block of data
69+
from "struct wt_status_serialize_data_fixed" in a fixed header where
70+
integers are in network byte order and OIDs are in raw (non-hex) form.
71+
This is followed by one or two raw pathnames (not c-quoted) with NUL
72+
terminators (both NULs are always present even if there is no rename).
73+
74+
v1-changed-item-section = PKT-LINE("changed" SP <count> LF)
75+
[ PKT-LINE(<changed_item> LF) ]+
76+
PKT-LINE(<flush>)
77+
78+
changed_item = <byte[4] worktree_status>
79+
<byte[4] index_status>
80+
<byte[4] stagemask>
81+
<byte[4] score>
82+
<byte[4] mode_head>
83+
<byte[4] mode_index>
84+
<byte[4] mode_worktree>
85+
<byte[4] dirty_submodule>
86+
<byte[4] new_submodule_commits>
87+
<byte[20] oid_head>
88+
<byte[20] oid_index>
89+
<byte[*] path>
90+
NUL
91+
[ <byte[*] src_path> ]
92+
NUL
93+
94+
95+
V1 Untracked and Ignored Items
96+
------------------------------
97+
98+
These sections are simple lists of pathnames. They ARE NOT
99+
c-quoted.
100+
101+
v1-untracked-item-section = PKT-LINE("untracked" SP <count> LF)
102+
[ PKT-LINE(<pathname> LF) ]+
103+
PKT-LINE(<flush>)
104+
105+
v1-ignored-item-section = PKT-LINE("ignored" SP <count> LF)
106+
[ PKT-LINE(<pathname> LF) ]+
107+
PKT-LINE(<flush>)

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,8 @@ LIB_OBJS += wrapper.o
12131213
LIB_OBJS += write-or-die.o
12141214
LIB_OBJS += ws.o
12151215
LIB_OBJS += wt-status.o
1216+
LIB_OBJS += wt-status-deserialize.o
1217+
LIB_OBJS += wt-status-serialize.o
12161218
LIB_OBJS += xdiff-interface.o
12171219

12181220
BUILTIN_OBJS += builtin/add.o

builtin/commit.c

+122-1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,70 @@ static int opt_parse_porcelain(const struct option *opt, const char *arg, int un
164164
return 0;
165165
}
166166

167+
static int do_serialize = 0;
168+
static int do_implicit_deserialize = 0;
169+
static int do_explicit_deserialize = 0;
170+
static char *deserialize_path = NULL;
171+
172+
/*
173+
* --serialize | --serialize=1 | --serialize=v1
174+
*
175+
* Request that we serialize our output rather than printing in
176+
* any of the established formats. Optionally specify serialization
177+
* version.
178+
*/
179+
static int opt_parse_serialize(const struct option *opt, const char *arg, int unset)
180+
{
181+
enum wt_status_format *value = (enum wt_status_format *)opt->value;
182+
if (unset || !arg)
183+
*value = STATUS_FORMAT_SERIALIZE_V1;
184+
else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
185+
*value = STATUS_FORMAT_SERIALIZE_V1;
186+
else
187+
die("unsupported serialize version '%s'", arg);
188+
189+
if (do_explicit_deserialize)
190+
die("cannot mix --serialize and --deserialize");
191+
do_implicit_deserialize = 0;
192+
193+
do_serialize = 1;
194+
return 0;
195+
}
196+
197+
/*
198+
* --deserialize | --deserialize=<path> |
199+
* --no-deserialize
200+
*
201+
* Request that we deserialize status data from some existing resource
202+
* rather than performing a status scan.
203+
*
204+
* The input source can come from stdin or a path given here -- or be
205+
* inherited from the config settings.
206+
*/
207+
static int opt_parse_deserialize(const struct option *opt UNUSED, const char *arg, int unset)
208+
{
209+
if (unset) {
210+
do_implicit_deserialize = 0;
211+
do_explicit_deserialize = 0;
212+
} else {
213+
if (do_serialize)
214+
die("cannot mix --serialize and --deserialize");
215+
if (arg) {
216+
/* override config or stdin */
217+
free(deserialize_path);
218+
deserialize_path = xstrdup(arg);
219+
}
220+
if (deserialize_path && *deserialize_path
221+
&& (access(deserialize_path, R_OK) != 0))
222+
die("cannot find serialization file '%s'",
223+
deserialize_path);
224+
225+
do_explicit_deserialize = 1;
226+
}
227+
228+
return 0;
229+
}
230+
167231
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
168232
{
169233
struct strbuf *buf = opt->value;
@@ -1188,6 +1252,8 @@ static enum untracked_status_type parse_untracked_setting_name(const char *u)
11881252
return SHOW_NORMAL_UNTRACKED_FILES;
11891253
else if (!strcmp(u, "all"))
11901254
return SHOW_ALL_UNTRACKED_FILES;
1255+
else if (!strcmp(u,"complete"))
1256+
return SHOW_COMPLETE_UNTRACKED_FILES;
11911257
else
11921258
return SHOW_UNTRACKED_FILES_ERROR;
11931259
}
@@ -1483,6 +1549,19 @@ static int git_status_config(const char *k, const char *v,
14831549
s->relative_paths = git_config_bool(k, v);
14841550
return 0;
14851551
}
1552+
if (!strcmp(k, "status.deserializepath")) {
1553+
/*
1554+
* Automatically assume deserialization if this is
1555+
* set in the config and the file exists. Do not
1556+
* complain if the file does not exist, because we
1557+
* silently fall back to normal mode.
1558+
*/
1559+
if (v && *v && access(v, R_OK) == 0) {
1560+
do_implicit_deserialize = 1;
1561+
deserialize_path = xstrdup(v);
1562+
}
1563+
return 0;
1564+
}
14861565
if (!strcmp(k, "status.showuntrackedfiles")) {
14871566
enum untracked_status_type u;
14881567

@@ -1522,7 +1601,8 @@ struct repository *repo UNUSED)
15221601
static const char *rename_score_arg = (const char *)-1;
15231602
static struct wt_status s;
15241603
unsigned int progress_flag = 0;
1525-
int fd;
1604+
int try_deserialize;
1605+
int fd = -1;
15261606
struct object_id oid;
15271607
static struct option builtin_status_options[] = {
15281608
OPT__VERBOSE(&verbose, N_("be verbose")),
@@ -1537,6 +1617,12 @@ struct repository *repo UNUSED)
15371617
OPT_CALLBACK_F(0, "porcelain", &status_format,
15381618
N_("version"), N_("machine-readable output"),
15391619
PARSE_OPT_OPTARG, opt_parse_porcelain),
1620+
{ OPTION_CALLBACK, 0, "serialize", &status_format,
1621+
N_("version"), N_("serialize raw status data to stdout"),
1622+
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_serialize },
1623+
{ OPTION_CALLBACK, 0, "deserialize", NULL,
1624+
N_("path"), N_("deserialize raw status data from file"),
1625+
PARSE_OPT_OPTARG, opt_parse_deserialize },
15401626
OPT_SET_INT(0, "long", &status_format,
15411627
N_("show status in long format (default)"),
15421628
STATUS_FORMAT_LONG),
@@ -1581,10 +1667,26 @@ struct repository *repo UNUSED)
15811667
s.show_untracked_files == SHOW_NO_UNTRACKED_FILES)
15821668
die(_("Unsupported combination of ignored and untracked-files arguments"));
15831669

1670+
if (s.show_untracked_files == SHOW_COMPLETE_UNTRACKED_FILES &&
1671+
s.show_ignored_mode == SHOW_NO_IGNORED)
1672+
die(_("Complete Untracked only supported with ignored files"));
1673+
15841674
parse_pathspec(&s.pathspec, 0,
15851675
PATHSPEC_PREFER_FULL,
15861676
prefix, argv);
15871677

1678+
/*
1679+
* If we want to try to deserialize status data from a cache file,
1680+
* we need to re-order the initialization code. The problem is that
1681+
* this makes for a very nasty diff and causes merge conflicts as we
1682+
* carry it forward. And it easy to mess up the merge, so we
1683+
* duplicate some code here to hopefully reduce conflicts.
1684+
*/
1685+
try_deserialize = (!do_serialize &&
1686+
(do_implicit_deserialize || do_explicit_deserialize));
1687+
if (try_deserialize)
1688+
goto skip_init;
1689+
15881690
enable_fscache(0);
15891691
if (status_format != STATUS_FORMAT_PORCELAIN &&
15901692
status_format != STATUS_FORMAT_PORCELAIN_V2)
@@ -1599,6 +1701,7 @@ struct repository *repo UNUSED)
15991701
else
16001702
fd = -1;
16011703

1704+
skip_init:
16021705
s.is_initial = repo_get_oid(the_repository, s.reference, &oid) ? 1 : 0;
16031706
if (!s.is_initial)
16041707
oidcpy(&s.oid_commit, &oid);
@@ -1615,6 +1718,24 @@ struct repository *repo UNUSED)
16151718
s.rename_score = parse_rename_score(&rename_score_arg);
16161719
}
16171720

1721+
if (try_deserialize) {
1722+
if (s.relative_paths)
1723+
s.prefix = prefix;
1724+
1725+
if (wt_status_deserialize(&s, deserialize_path) == DESERIALIZE_OK)
1726+
return 0;
1727+
1728+
/* deserialize failed, so force the initialization we skipped above. */
1729+
enable_fscache(1);
1730+
repo_read_index_preload(the_repository, &s.pathspec, 0);
1731+
refresh_index(the_repository->index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);
1732+
1733+
if (use_optional_locks())
1734+
fd = repo_hold_locked_index(the_repository, &index_lock, 0);
1735+
else
1736+
fd = -1;
1737+
}
1738+
16181739
wt_status_collect(&s);
16191740

16201741
if (0 <= fd)

contrib/completion/git-completion.bash

+1-1
Original file line numberDiff line numberDiff line change
@@ -1802,7 +1802,7 @@ _git_clone ()
18021802
esac
18031803
}
18041804

1805-
__git_untracked_file_modes="all no normal"
1805+
__git_untracked_file_modes="all no normal complete"
18061806

18071807
__git_trailer_tokens ()
18081808
{

meson.build

+2
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,8 @@ libgit_sources = [
489489
'write-or-die.c',
490490
'ws.c',
491491
'wt-status.c',
492+
'wt-status-deserialize.c',
493+
'wt-status-serialize.c',
492494
'xdiff-interface.c',
493495
'xdiff/xdiffi.c',
494496
'xdiff/xemit.c',

pkt-line.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ static int do_packet_write(const int fd_out, const char *buf, size_t size,
230230
return 0;
231231
}
232232

233-
static int packet_write_gently(const int fd_out, const char *buf, size_t size)
233+
int packet_write_gently(const int fd_out, const char *buf, size_t size)
234234
{
235235
struct strbuf err = STRBUF_INIT;
236236
if (do_packet_write(fd_out, buf, size, &err)) {

pkt-line.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void packet_write(int fd_out, const char *buf, size_t size);
2929
void packet_buf_write(struct strbuf *buf, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
3030
int packet_flush_gently(int fd);
3131
int packet_write_fmt_gently(int fd, const char *fmt, ...) __attribute__((format (printf, 2, 3)));
32+
int packet_write_gently(const int fd_out, const char *buf, size_t size);
3233
int write_packetized_from_fd_no_flush(int fd_in, int fd_out);
3334
int write_packetized_from_buf_no_flush_count(const char *src_in, size_t len,
3435
int fd_out, int *packet_counter);

0 commit comments

Comments
 (0)