Skip to content

Commit fe1e0bd

Browse files
jeffhostetlerdscho
authored andcommitted
status: deserialization wait
Teach `git status --deserialize` to either wait indefintely or immediately fail if the status serialization cache file is stale. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 48fcb32 commit fe1e0bd

File tree

5 files changed

+245
-13
lines changed

5 files changed

+245
-13
lines changed

Documentation/config/status.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,19 @@ status.deserializePath::
8383
generated by `--serialize`. This will be overridden by
8484
`--deserialize=<path>` on the command line. If the cache file is
8585
invalid or stale, git will fall-back and compute status normally.
86+
87+
status.deserializeWait::
88+
EXPERIMENTAL, Specifies what `git status --deserialize` should do
89+
if the serialization cache file is stale and whether it should
90+
fall-back and compute status normally. This will be overridden by
91+
`--deserialize-wait=<value>` on the command line.
92+
+
93+
--
94+
* `fail` - cause git to exit with an error when the status cache file
95+
is stale; this is intended for testing and debugging.
96+
* `block` - cause git to spin and periodically retry the cache file
97+
every 100 ms; this is intended to help coordinate with another git
98+
instance concurrently computing the cache file.
99+
* `no` - to immediately fall-back if cache file is stale. This is the default.
100+
* `<timeout>` - time (in tenths of a second) to spin and retry.
101+
--

builtin/commit.c

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ static int do_implicit_deserialize = 0;
173173
static int do_explicit_deserialize = 0;
174174
static char *deserialize_path = NULL;
175175

176+
static enum wt_status_deserialize_wait implicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
177+
static enum wt_status_deserialize_wait explicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
178+
176179
/*
177180
* --serialize | --serialize=<path>
178181
*
@@ -238,6 +241,40 @@ static int opt_parse_deserialize(const struct option *opt UNUSED, const char *ar
238241
return 0;
239242
}
240243

244+
static enum wt_status_deserialize_wait parse_dw(const char *arg)
245+
{
246+
int tenths;
247+
248+
if (!strcmp(arg, "fail"))
249+
return DESERIALIZE_WAIT__FAIL;
250+
else if (!strcmp(arg, "block"))
251+
return DESERIALIZE_WAIT__BLOCK;
252+
else if (!strcmp(arg, "no"))
253+
return DESERIALIZE_WAIT__NO;
254+
255+
/*
256+
* Otherwise, assume it is a timeout in tenths of a second.
257+
* If it contains a bogus value, atol() will return zero
258+
* which is OK.
259+
*/
260+
tenths = atol(arg);
261+
if (tenths < 0)
262+
tenths = DESERIALIZE_WAIT__NO;
263+
return tenths;
264+
}
265+
266+
static int opt_parse_deserialize_wait(const struct option *opt UNUSED,
267+
const char *arg,
268+
int unset)
269+
{
270+
if (unset)
271+
explicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
272+
else
273+
explicit_deserialize_wait = parse_dw(arg);
274+
275+
return 0;
276+
}
277+
241278
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
242279
{
243280
struct strbuf *buf = opt->value;
@@ -1586,6 +1623,13 @@ static int git_status_config(const char *k, const char *v,
15861623
}
15871624
return 0;
15881625
}
1626+
if (!strcmp(k, "status.deserializewait")) {
1627+
if (!v || !*v)
1628+
implicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
1629+
else
1630+
implicit_deserialize_wait = parse_dw(v);
1631+
return 0;
1632+
}
15891633
if (!strcmp(k, "status.showuntrackedfiles")) {
15901634
enum untracked_status_type u;
15911635

@@ -1647,6 +1691,9 @@ struct repository *repo UNUSED)
16471691
OPT_CALLBACK_F(0, "deserialize", NULL,
16481692
N_("path"), N_("deserialize raw status data from file"),
16491693
PARSE_OPT_OPTARG, opt_parse_deserialize),
1694+
OPT_CALLBACK_F(0, "deserialize-wait", NULL,
1695+
N_("fail|block|no"), N_("how to wait if status cache file is invalid"),
1696+
PARSE_OPT_OPTARG, opt_parse_deserialize_wait),
16501697
OPT_SET_INT(0, "long", &status_format,
16511698
N_("show status in long format (default)"),
16521699
STATUS_FORMAT_LONG),
@@ -1760,11 +1807,21 @@ struct repository *repo UNUSED)
17601807
}
17611808

17621809
if (try_deserialize) {
1810+
int result;
1811+
enum wt_status_deserialize_wait dw = implicit_deserialize_wait;
1812+
if (explicit_deserialize_wait != DESERIALIZE_WAIT__UNSET)
1813+
dw = explicit_deserialize_wait;
1814+
if (dw == DESERIALIZE_WAIT__UNSET)
1815+
dw = DESERIALIZE_WAIT__NO;
1816+
17631817
if (s.relative_paths)
17641818
s.prefix = prefix;
17651819

1766-
if (wt_status_deserialize(&s, deserialize_path) == DESERIALIZE_OK)
1820+
result = wt_status_deserialize(&s, deserialize_path, dw);
1821+
if (result == DESERIALIZE_OK)
17671822
return 0;
1823+
if (dw == DESERIALIZE_WAIT__FAIL)
1824+
die(_("Rejected status serialization cache"));
17681825

17691826
/* deserialize failed, so force the initialization we skipped above. */
17701827
enable_fscache(1);

t/t7522-serialized-status.sh

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,58 @@ test_expect_success 'verify new --serialize=path mode' '
199199
test_cmp expect output.2
200200
'
201201

202+
test_expect_success 'try deserialize-wait feature' '
203+
test_when_finished "rm -f serialized_status.dat dirt expect.* output.* trace.*" &&
204+
205+
git status --serialize=serialized_status.dat >output.1 &&
206+
207+
# make status cache stale by updating the mtime on the index. confirm that
208+
# deserialize fails when requested.
209+
sleep 1 &&
210+
touch .git/index &&
211+
test_must_fail git status --deserialize=serialized_status.dat --deserialize-wait=fail &&
212+
test_must_fail git -c status.deserializeWait=fail status --deserialize=serialized_status.dat &&
213+
214+
cat >expect.1 <<-\EOF &&
215+
? expect.1
216+
? output.1
217+
? serialized_status.dat
218+
? untracked/
219+
? untracked_1.txt
220+
EOF
221+
222+
# refresh the status cache.
223+
git status --porcelain=v2 --serialize=serialized_status.dat >output.1 &&
224+
test_cmp expect.1 output.1 &&
225+
226+
# create some dirt. confirm deserialize used the existing status cache.
227+
echo x >dirt &&
228+
git status --porcelain=v2 --deserialize=serialized_status.dat >output.2 &&
229+
test_cmp output.1 output.2 &&
230+
231+
# make the cache stale and try the timeout feature and wait upto
232+
# 2 tenths of a second. confirm deserialize timed out and rejected
233+
# the status cache and did a normal scan.
234+
235+
cat >expect.2 <<-\EOF &&
236+
? dirt
237+
? expect.1
238+
? expect.2
239+
? output.1
240+
? output.2
241+
? serialized_status.dat
242+
? trace.2
243+
? untracked/
244+
? untracked_1.txt
245+
EOF
246+
247+
sleep 1 &&
248+
touch .git/index &&
249+
GIT_TRACE_DESERIALIZE=1 git status --porcelain=v2 --deserialize=serialized_status.dat --deserialize-wait=2 >output.2 2>trace.2 &&
250+
test_cmp expect.2 output.2 &&
251+
grep "wait polled=2 result=1" trace.2 >trace.2g
252+
'
253+
202254
test_expect_success 'merge conflicts' '
203255
204256
# create a merge conflict.

wt-status-deserialize.c

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ static int my_validate_index(const char *path, const struct cache_time *mtime_re
6262
mtime_observed_on_disk.nsec = ST_MTIME_NSEC(st);
6363
if ((mtime_observed_on_disk.sec != mtime_reported->sec) ||
6464
(mtime_observed_on_disk.nsec != mtime_reported->nsec)) {
65-
trace_printf_key(&trace_deserialize, "index mtime changed [des %d.%d][obs %d.%d]",
65+
trace_printf_key(&trace_deserialize,
66+
"index mtime changed [des %d %d][obs %d %d]",
6667
mtime_reported->sec, mtime_reported->nsec,
6768
mtime_observed_on_disk.sec, mtime_observed_on_disk.nsec);
6869
return DESERIALIZE_ERR;
@@ -552,6 +553,8 @@ static inline int my_strcmp_null(const char *a, const char *b)
552553

553554
static int wt_deserialize_fd(const struct wt_status *cmd_s, struct wt_status *des_s, int fd)
554555
{
556+
memset(des_s, 0, sizeof(*des_s));
557+
555558
/*
556559
* Check the path spec on the current command
557560
*/
@@ -681,34 +684,128 @@ static int wt_deserialize_fd(const struct wt_status *cmd_s, struct wt_status *de
681684
return DESERIALIZE_OK;
682685
}
683686

687+
static struct cache_time deserialize_prev_mtime = { 0, 0 };
688+
689+
static int try_deserialize_read_from_file_1(const struct wt_status *cmd_s,
690+
const char *path,
691+
struct wt_status *des_s)
692+
{
693+
struct stat st;
694+
int result;
695+
int fd;
696+
697+
/*
698+
* If we are spinning waiting for the status cache to become
699+
* valid, skip re-reading it if the mtime has not changed
700+
* since the last time we read it.
701+
*/
702+
if (lstat(path, &st)) {
703+
trace_printf_key(&trace_deserialize,
704+
"could not lstat '%s'", path);
705+
return DESERIALIZE_ERR;
706+
}
707+
if ((uint32_t)st.st_mtime == deserialize_prev_mtime.sec &&
708+
ST_MTIME_NSEC(st) == deserialize_prev_mtime.nsec) {
709+
trace_printf_key(&trace_deserialize,
710+
"mtime has not changed '%s'", path);
711+
return DESERIALIZE_ERR;
712+
}
713+
714+
fd = xopen(path, O_RDONLY);
715+
if (fd == -1) {
716+
trace_printf_key(&trace_deserialize,
717+
"could not read '%s'", path);
718+
return DESERIALIZE_ERR;
719+
}
720+
721+
deserialize_prev_mtime.sec = st.st_mtime;
722+
deserialize_prev_mtime.nsec = ST_MTIME_NSEC(st);
723+
724+
trace_printf_key(&trace_deserialize,
725+
"reading serialization file (%d %d) '%s'",
726+
deserialize_prev_mtime.sec,
727+
deserialize_prev_mtime.nsec,
728+
path);
729+
730+
result = wt_deserialize_fd(cmd_s, des_s, fd);
731+
close(fd);
732+
733+
return result;
734+
}
735+
736+
static int try_deserialize_read_from_file(const struct wt_status *cmd_s,
737+
const char *path,
738+
enum wt_status_deserialize_wait dw,
739+
struct wt_status *des_s)
740+
{
741+
int k, limit;
742+
int result = DESERIALIZE_ERR;
743+
744+
/*
745+
* For "fail" or "no", try exactly once to read the status cache.
746+
* Return an error if the file is stale.
747+
*/
748+
if (dw == DESERIALIZE_WAIT__FAIL || dw == DESERIALIZE_WAIT__NO)
749+
return try_deserialize_read_from_file_1(cmd_s, path, des_s);
750+
751+
/*
752+
* Wait for the status cache file to refresh. Wait duration can
753+
* be in tenths of a second or unlimited. Poll every 100ms.
754+
*/
755+
if (dw == DESERIALIZE_WAIT__BLOCK) {
756+
/*
757+
* Convert "unlimited" to 1 day.
758+
*/
759+
limit = 10 * 60 * 60 * 24;
760+
} else {
761+
/* spin for dw tenths of a second */
762+
limit = dw;
763+
}
764+
for (k = 0; k < limit; k++) {
765+
result = try_deserialize_read_from_file_1(
766+
cmd_s, path, des_s);
767+
768+
if (result == DESERIALIZE_OK)
769+
break;
770+
771+
sleep_millisec(100);
772+
}
773+
774+
trace_printf_key(&trace_deserialize,
775+
"wait polled=%d result=%d '%s'",
776+
k, result, path);
777+
return result;
778+
}
779+
684780
/*
685-
* Read raw serialized status data from the given file
781+
* Read raw serialized status data from the given file (or STDIN).
686782
*
687783
* Verify that the args specified in the current command
688784
* are compatible with the deserialized data (such as "-uno").
689785
*
690786
* Copy display-related fields from the current command
691787
* into the deserialized data (so that the user can request
692788
* long or short as they please).
789+
*
790+
* Print status report using cached data.
693791
*/
694792
int wt_status_deserialize(const struct wt_status *cmd_s,
695-
const char *path)
793+
const char *path,
794+
enum wt_status_deserialize_wait dw)
696795
{
697796
struct wt_status des_s;
698797
int result;
699798
struct string_list_item *change;
700799

701800
if (path && *path && strcmp(path, "0")) {
702-
int fd = xopen(path, O_RDONLY);
703-
if (fd == -1) {
704-
trace_printf_key(&trace_deserialize, "could not read '%s'", path);
705-
return DESERIALIZE_ERR;
706-
}
707-
trace_printf_key(&trace_deserialize, "reading serialization file '%s'", path);
708-
result = wt_deserialize_fd(cmd_s, &des_s, fd);
709-
close(fd);
801+
result = try_deserialize_read_from_file(cmd_s, path, dw, &des_s);
710802
} else {
711803
trace_printf_key(&trace_deserialize, "reading stdin");
804+
805+
/*
806+
* Read status cache data from stdin. Ignore the deserialize-wait
807+
* term, since we cannot read stdin multiple times.
808+
*/
712809
result = wt_deserialize_fd(cmd_s, &des_s, 0);
713810
}
714811

wt-status.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,15 @@ struct wt_status_serialize_data
220220
- sizeof(struct wt_status_serialize_data_fixed)];
221221
};
222222

223+
enum wt_status_deserialize_wait
224+
{
225+
DESERIALIZE_WAIT__UNSET = -3,
226+
DESERIALIZE_WAIT__FAIL = -2, /* return error, do not fallback */
227+
DESERIALIZE_WAIT__BLOCK = -1, /* unlimited timeout */
228+
DESERIALIZE_WAIT__NO = 0, /* immediately fallback */
229+
/* any positive value is a timeout in tenths of a second */
230+
};
231+
223232
/*
224233
* Serialize computed status scan results using "version 1" format
225234
* to the given file.
@@ -234,7 +243,8 @@ void wt_status_serialize_v1(int fd, struct wt_status *s);
234243
* fields.
235244
*/
236245
int wt_status_deserialize(const struct wt_status *cmd_s,
237-
const char *path);
246+
const char *path,
247+
enum wt_status_deserialize_wait dw);
238248

239249
/*
240250
* A helper routine for serialize and deserialize to compute

0 commit comments

Comments
 (0)