Skip to content

Commit b2024bb

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 42e8e53 commit b2024bb

File tree

5 files changed

+245
-13
lines changed

5 files changed

+245
-13
lines changed

Documentation/config/status.txt

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
@@ -171,6 +171,9 @@ static int do_implicit_deserialize = 0;
171171
static int do_explicit_deserialize = 0;
172172
static char *deserialize_path = NULL;
173173

174+
static enum wt_status_deserialize_wait implicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
175+
static enum wt_status_deserialize_wait explicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
176+
174177
/*
175178
* --serialize | --serialize=<path>
176179
*
@@ -236,6 +239,40 @@ static int opt_parse_deserialize(const struct option *opt UNUSED, const char *ar
236239
return 0;
237240
}
238241

242+
static enum wt_status_deserialize_wait parse_dw(const char *arg)
243+
{
244+
int tenths;
245+
246+
if (!strcmp(arg, "fail"))
247+
return DESERIALIZE_WAIT__FAIL;
248+
else if (!strcmp(arg, "block"))
249+
return DESERIALIZE_WAIT__BLOCK;
250+
else if (!strcmp(arg, "no"))
251+
return DESERIALIZE_WAIT__NO;
252+
253+
/*
254+
* Otherwise, assume it is a timeout in tenths of a second.
255+
* If it contains a bogus value, atol() will return zero
256+
* which is OK.
257+
*/
258+
tenths = atol(arg);
259+
if (tenths < 0)
260+
tenths = DESERIALIZE_WAIT__NO;
261+
return tenths;
262+
}
263+
264+
static int opt_parse_deserialize_wait(const struct option *opt UNUSED,
265+
const char *arg,
266+
int unset)
267+
{
268+
if (unset)
269+
explicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
270+
else
271+
explicit_deserialize_wait = parse_dw(arg);
272+
273+
return 0;
274+
}
275+
239276
static int opt_parse_m(const struct option *opt, const char *arg, int unset)
240277
{
241278
struct strbuf *buf = opt->value;
@@ -1568,6 +1605,13 @@ static int git_status_config(const char *k, const char *v,
15681605
}
15691606
return 0;
15701607
}
1608+
if (!strcmp(k, "status.deserializewait")) {
1609+
if (!v || !*v)
1610+
implicit_deserialize_wait = DESERIALIZE_WAIT__UNSET;
1611+
else
1612+
implicit_deserialize_wait = parse_dw(v);
1613+
return 0;
1614+
}
15711615
if (!strcmp(k, "status.showuntrackedfiles")) {
15721616
enum untracked_status_type u;
15731617

@@ -1629,6 +1673,9 @@ struct repository *repo UNUSED)
16291673
{ OPTION_CALLBACK, 0, "deserialize", NULL,
16301674
N_("path"), N_("deserialize raw status data from file"),
16311675
PARSE_OPT_OPTARG, opt_parse_deserialize },
1676+
{ OPTION_CALLBACK, 0, "deserialize-wait", NULL,
1677+
N_("fail|block|no"), N_("how to wait if status cache file is invalid"),
1678+
PARSE_OPT_OPTARG, opt_parse_deserialize_wait },
16321679
OPT_SET_INT(0, "long", &status_format,
16331680
N_("show status in long format (default)"),
16341681
STATUS_FORMAT_LONG),
@@ -1725,11 +1772,21 @@ struct repository *repo UNUSED)
17251772
}
17261773

17271774
if (try_deserialize) {
1775+
int result;
1776+
enum wt_status_deserialize_wait dw = implicit_deserialize_wait;
1777+
if (explicit_deserialize_wait != DESERIALIZE_WAIT__UNSET)
1778+
dw = explicit_deserialize_wait;
1779+
if (dw == DESERIALIZE_WAIT__UNSET)
1780+
dw = DESERIALIZE_WAIT__NO;
1781+
17281782
if (s.relative_paths)
17291783
s.prefix = prefix;
17301784

1731-
if (wt_status_deserialize(&s, deserialize_path) == DESERIALIZE_OK)
1785+
result = wt_status_deserialize(&s, deserialize_path, dw);
1786+
if (result == DESERIALIZE_OK)
17321787
return 0;
1788+
if (dw == DESERIALIZE_WAIT__FAIL)
1789+
die(_("Rejected status serialization cache"));
17331790

17341791
/* deserialize failed, so force the initialization we skipped above. */
17351792
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)