Skip to content

Commit a74a553

Browse files
jeffhostetlerdscho
authored andcommitted
status: serialize to path
Teach status serialization to take an optional pathname on the command line to direct that cache data be written there rather than to stdout. When used this way, normal status results will still be written to stdout. When no path is given, only binary serialization data is written to stdout. Usage: git status --serialize[=<path>] Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com>
1 parent ffc0fb8 commit a74a553

6 files changed

Lines changed: 58 additions & 18 deletions

File tree

Documentation/git-status.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,12 @@ ignored, then the directory is not shown, but all contents are shown.
152152
update it afterwards if any changes were detected. Defaults to
153153
`--lock-index`.
154154

155-
--serialize[=<version>]::
156-
(EXPERIMENTAL) Serialize raw status results to stdout in a
157-
format suitable for use by `--deserialize`. Valid values for
158-
`<version>` are "1" and "v1".
155+
--serialize[=<path>]::
156+
(EXPERIMENTAL) Serialize raw status results to a file or stdout
157+
in a format suitable for use by `--deserialize`. If a path is
158+
given, serialize data will be written to that path *and* normal
159+
status output will be written to stdout. If path is omitted,
160+
only binary serialization data will be written to stdout.
159161

160162
--deserialize[=<path>]::
161163
(EXPERIMENTAL) Deserialize raw status results from a file or

builtin/commit.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,32 @@ static int opt_parse_porcelain(const struct option *opt, const char *arg, int un
143143
}
144144

145145
static int do_serialize = 0;
146+
static char *serialize_path = NULL;
147+
146148
static int do_implicit_deserialize = 0;
147149
static int do_explicit_deserialize = 0;
148150
static char *deserialize_path = NULL;
149151

150152
/*
151-
* --serialize | --serialize=1 | --serialize=v1
153+
* --serialize | --serialize=<path>
154+
*
155+
* Request that we serialize status output rather than or in addition to
156+
* printing in any of the established formats.
157+
*
158+
* Without a path, we write binary serialization data to stdout (and omit
159+
* the normal status output).
152160
*
153-
* Request that we serialize our output rather than printing in
154-
* any of the established formats. Optionally specify serialization
155-
* version.
161+
* With a path, we write binary serialization data to the <path> and then
162+
* write normal status output.
156163
*/
157164
static int opt_parse_serialize(const struct option *opt, const char *arg, int unset)
158165
{
159166
enum wt_status_format *value = (enum wt_status_format *)opt->value;
160167
if (unset || !arg)
161168
*value = STATUS_FORMAT_SERIALIZE_V1;
162-
else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
163-
*value = STATUS_FORMAT_SERIALIZE_V1;
164-
else
165-
die("unsupported serialize version '%s'", arg);
169+
170+
if (arg)
171+
serialize_path = xstrdup(arg);
166172

167173
if (do_explicit_deserialize)
168174
die("cannot mix --serialize and --deserialize");
@@ -1415,7 +1421,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14151421
N_("version"), N_("machine-readable output"),
14161422
PARSE_OPT_OPTARG, opt_parse_porcelain },
14171423
{ OPTION_CALLBACK, 0, "serialize", &status_format,
1418-
N_("version"), N_("serialize raw status data to stdout"),
1424+
N_("path"), N_("serialize raw status data to path or stdout"),
14191425
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_serialize },
14201426
{ OPTION_CALLBACK, 0, "deserialize", NULL,
14211427
N_("path"), N_("deserialize raw status data from file"),
@@ -1557,6 +1563,16 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15571563
if (s.relative_paths)
15581564
s.prefix = prefix;
15591565

1566+
if (serialize_path) {
1567+
int fd_serialize = xopen(serialize_path,
1568+
O_WRONLY | O_CREAT | O_TRUNC, 0666);
1569+
if (fd_serialize < 0)
1570+
die_errno(_("could not serialize to '%s'"),
1571+
serialize_path);
1572+
wt_status_serialize_v1(fd_serialize, &s);
1573+
close(fd_serialize);
1574+
}
1575+
15601576
wt_status_print(&s);
15611577
wt_status_collect_free_buffers(&s);
15621578

t/t7524-serialized-status.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,27 @@ test_expect_success 'verify no-ahead-behind and serialized status integration' '
163163
test_i18ncmp expect output
164164
'
165165

166+
test_expect_success 'verify new --serialize=path mode' '
167+
#test_when_finished "rm serialized_status.dat expect new_change.txt output.1 output.2" &&
168+
cat >expect <<-\EOF &&
169+
? expect
170+
? output.1
171+
? untracked/
172+
? untracked_1.txt
173+
EOF
174+
175+
git checkout -b serialize_path_branch master --track >/dev/null &&
176+
touch alt_branch_changes.txt &&
177+
git add alt_branch_changes.txt &&
178+
test_tick &&
179+
git commit -m"New commit on serialize_path_branch" &&
180+
181+
git status --porcelain=v2 --serialize=serialized_status.dat >output.1 &&
182+
touch new_change.txt &&
183+
184+
git status --porcelain=v2 --deserialize=serialized_status.dat >output.2 &&
185+
test_i18ncmp expect output.1 &&
186+
test_i18ncmp expect output.2
187+
'
188+
166189
test_done

wt-status-serialize.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ static inline void wt_serialize_v1_ignored(struct wt_status *s, int fd,
162162
}
163163

164164
/*
165-
* Serialize the list of changes to stdout. The goal of this
165+
* Serialize the list of changes to the given file. The goal of this
166166
* is to just serialize the key fields in wt_status so that a
167167
* later command can rebuilt it and do the printing.
168168
*
@@ -171,9 +171,8 @@ static inline void wt_serialize_v1_ignored(struct wt_status *s, int fd,
171171
* is relatively quick for the status consumer to compute
172172
* as necessary.
173173
*/
174-
void wt_status_serialize_v1(struct wt_status *s)
174+
void wt_status_serialize_v1(int fd, struct wt_status *s)
175175
{
176-
int fd = 1; /* we always write to stdout */
177176
struct string_list_item *iter;
178177
int k;
179178

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2322,7 +2322,7 @@ void wt_status_print(struct wt_status *s)
23222322
wt_longstatus_print(s);
23232323
break;
23242324
case STATUS_FORMAT_SERIALIZE_V1:
2325-
wt_status_serialize_v1(s);
2325+
wt_status_serialize_v1(1, s);
23262326
break;
23272327
}
23282328
}

wt-status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct wt_status_serialize_data
187187
* Serialize computed status scan results using "version 1" format
188188
* to the given file.
189189
*/
190-
void wt_status_serialize_v1(struct wt_status *s);
190+
void wt_status_serialize_v1(int fd, struct wt_status *s);
191191

192192
/*
193193
* Deserialize existing status results from the given file and

0 commit comments

Comments
 (0)