Skip to content

Commit e421b5c

Browse files
jeffhostetlervdye
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 1765859 commit e421b5c

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

Documentation/git-status.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ ignored, then the directory is not shown, but all contents are shown.
149149
threshold.
150150
See also linkgit:git-diff[1] `--find-renames`.
151151

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

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

builtin/commit.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,26 +173,34 @@ static int opt_parse_porcelain(const struct option *opt, const char *arg, int un
173173
}
174174

175175
static int do_serialize = 0;
176+
static char *serialize_path = NULL;
177+
176178
static int do_implicit_deserialize = 0;
177179
static int do_explicit_deserialize = 0;
178180
static char *deserialize_path = NULL;
179181

180182
/*
181-
* --serialize | --serialize=1 | --serialize=v1
183+
* --serialize | --serialize=<path>
184+
*
185+
* Request that we serialize status output rather than or in addition to
186+
* printing in any of the established formats.
187+
*
188+
* Without a path, we write binary serialization data to stdout (and omit
189+
* the normal status output).
182190
*
183-
* Request that we serialize our output rather than printing in
184-
* any of the established formats. Optionally specify serialization
185-
* version.
191+
* With a path, we write binary serialization data to the <path> and then
192+
* write normal status output.
186193
*/
187194
static int opt_parse_serialize(const struct option *opt, const char *arg, int unset)
188195
{
189196
enum wt_status_format *value = (enum wt_status_format *)opt->value;
190197
if (unset || !arg)
191198
*value = STATUS_FORMAT_SERIALIZE_V1;
192-
else if (!strcmp(arg, "v1") || !strcmp(arg, "1"))
193-
*value = STATUS_FORMAT_SERIALIZE_V1;
194-
else
195-
die("unsupported serialize version '%s'", arg);
199+
200+
if (arg) {
201+
free(serialize_path);
202+
serialize_path = xstrdup(arg);
203+
}
196204

197205
if (do_explicit_deserialize)
198206
die("cannot mix --serialize and --deserialize");
@@ -1595,7 +1603,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15951603
N_("version"), N_("machine-readable output"),
15961604
PARSE_OPT_OPTARG, opt_parse_porcelain),
15971605
{ OPTION_CALLBACK, 0, "serialize", &status_format,
1598-
N_("version"), N_("serialize raw status data to stdout"),
1606+
N_("path"), N_("serialize raw status data to path or stdout"),
15991607
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_serialize },
16001608
{ OPTION_CALLBACK, 0, "deserialize", NULL,
16011609
N_("path"), N_("deserialize raw status data from file"),
@@ -1721,6 +1729,16 @@ int cmd_status(int argc, const char **argv, const char *prefix)
17211729
if (s.relative_paths)
17221730
s.prefix = prefix;
17231731

1732+
if (serialize_path) {
1733+
int fd_serialize = xopen(serialize_path,
1734+
O_WRONLY | O_CREAT | O_TRUNC, 0666);
1735+
if (fd_serialize < 0)
1736+
die_errno(_("could not serialize to '%s'"),
1737+
serialize_path);
1738+
wt_status_serialize_v1(fd_serialize, &s);
1739+
close(fd_serialize);
1740+
}
1741+
17241742
wt_status_print(&s);
17251743
wt_status_collect_free_buffers(&s);
17261744

t/t7522-serialized-status.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,27 @@ test_expect_success 'verify no-ahead-behind and serialized status integration' '
170170
test_cmp expect output
171171
'
172172

173+
test_expect_success 'verify new --serialize=path mode' '
174+
#test_when_finished "rm serialized_status.dat expect new_change.txt output.1 output.2" &&
175+
cat >expect <<-\EOF &&
176+
? expect
177+
? output.1
178+
? untracked/
179+
? untracked_1.txt
180+
EOF
181+
182+
git checkout -b serialize_path_branch main --track >/dev/null &&
183+
touch alt_branch_changes.txt &&
184+
git add alt_branch_changes.txt &&
185+
test_tick &&
186+
git commit -m"New commit on serialize_path_branch" &&
187+
188+
git status --porcelain=v2 --serialize=serialized_status.dat >output.1 &&
189+
touch new_change.txt &&
190+
191+
git status --porcelain=v2 --deserialize=serialized_status.dat >output.2 &&
192+
test_cmp expect output.1 &&
193+
test_cmp expect output.2
194+
'
195+
173196
test_done

wt-status-serialize.c

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

169169
/*
170-
* Serialize the list of changes to stdout. The goal of this
170+
* Serialize the list of changes to the given file. The goal of this
171171
* is to just serialize the key fields in wt_status so that a
172172
* later command can rebuilt it and do the printing.
173173
*
@@ -176,9 +176,8 @@ static inline void wt_serialize_v1_ignored(struct wt_status *s, int fd,
176176
* is relatively quick for the status consumer to compute
177177
* as necessary.
178178
*/
179-
void wt_status_serialize_v1(struct wt_status *s)
179+
void wt_status_serialize_v1(int fd, struct wt_status *s)
180180
{
181-
int fd = 1; /* we always write to stdout */
182181
struct string_list_item *iter;
183182
int k;
184183

wt-status.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2569,7 +2569,7 @@ void wt_status_print(struct wt_status *s)
25692569
wt_longstatus_print(s);
25702570
break;
25712571
case STATUS_FORMAT_SERIALIZE_V1:
2572-
wt_status_serialize_v1(s);
2572+
wt_status_serialize_v1(1, s);
25732573
break;
25742574
}
25752575

wt-status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ struct wt_status_serialize_data
220220
* Serialize computed status scan results using "version 1" format
221221
* to the given file.
222222
*/
223-
void wt_status_serialize_v1(struct wt_status *s);
223+
void wt_status_serialize_v1(int fd, struct wt_status *s);
224224

225225
/*
226226
* Deserialize existing status results from the given file and

0 commit comments

Comments
 (0)