Skip to content

Commit 60518e7

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 94e7e1f commit 60518e7

6 files changed

Lines changed: 57 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: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,31 @@ static int opt_parse_porcelain(const struct option *opt, const char *arg, int un
144144
}
145145

146146
static int do_serialize = 0;
147+
static char *serialize_path = NULL;
148+
147149
static int do_implicit_deserialize = 0;
148150
static int do_explicit_deserialize = 0;
149151
static char *deserialize_path = NULL;
150152

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

168173
if (do_explicit_deserialize)
169174
die("cannot mix --serialize and --deserialize");
@@ -1418,7 +1423,7 @@ int cmd_status(int argc, const char **argv, const char *prefix)
14181423
N_("version"), N_("machine-readable output"),
14191424
PARSE_OPT_OPTARG, opt_parse_porcelain },
14201425
{ OPTION_CALLBACK, 0, "serialize", &status_format,
1421-
N_("version"), N_("serialize raw status data to stdout"),
1426+
N_("path"), N_("serialize raw status data to path or stdout"),
14221427
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, opt_parse_serialize },
14231428
{ OPTION_CALLBACK, 0, "deserialize", NULL,
14241429
N_("path"), N_("deserialize raw status data from file"),
@@ -1560,6 +1565,16 @@ int cmd_status(int argc, const char **argv, const char *prefix)
15601565
if (s.relative_paths)
15611566
s.prefix = prefix;
15621567

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

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
@@ -2329,7 +2329,7 @@ void wt_status_print(struct wt_status *s)
23292329
wt_longstatus_print(s);
23302330
break;
23312331
case STATUS_FORMAT_SERIALIZE_V1:
2332-
wt_status_serialize_v1(s);
2332+
wt_status_serialize_v1(1, s);
23332333
break;
23342334
}
23352335
}

wt-status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ struct wt_status_serialize_data
196196
* Serialize computed status scan results using "version 1" format
197197
* to the given file.
198198
*/
199-
void wt_status_serialize_v1(struct wt_status *s);
199+
void wt_status_serialize_v1(int fd, struct wt_status *s);
200200

201201
/*
202202
* Deserialize existing status results from the given file and

0 commit comments

Comments
 (0)