scalar: 'unregister' stops background maintenance
Just like `scalar register` starts the scheduled background maintenance, `scalar unregister` stops it. Note that we use `git maintenance start` in `scalar register`, but we do not use `git maintenance stop` in `scalar unregister`: this would stop maintenance for _all_ repositories, not just for the one we want to unregister. The `unregister` command also removes the corresponding entry from the `[scalar]` section in the global Git config. Co-authored-by: Victoria Dye <vdye@github.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Dec 3, 2021 at 13:34 UTC
c76a53eb71c5d0d931b1f95a9933913ecef9e4ad
2 files changed
+50
-8
contrib/scalar/scalar.c
+42
-8
@@ -198,12 +198,12 @@ static int set_recommended_config(void)
198
return 0;
199
}
200
201
-static int start_maintenance(void)
201
+static int toggle_maintenance(int enable)
202
{
203
- return run_git("maintenance", "start", NULL);
203
+ return run_git("maintenance", enable ? "start" : "unregister", NULL);
204
}
205
206
-static int add_enlistment(void)
206
+static int add_or_remove_enlistment(int add)
207
{
208
int res;
209
@@ -214,24 +214,39 @@ static int add_enlistment(void)
214
"scalar.repo", the_repository->worktree, NULL);
215
216
/*
217
- * If the setting is already there, then do nothing.
217
+ * If we want to add and the setting is already there, then do nothing.
218
+ * If we want to remove and the setting is not there, then do nothing.
219
*/
219
- if (!res)
220
+ if ((add && !res) || (!add && res))
221
return 0;
222
222
- return run_git("config", "--global", "--add",
223
+ return run_git("config", "--global", add ? "--add" : "--unset",
224
+ add ? "--no-fixed-value" : "--fixed-value",
225
"scalar.repo", the_repository->worktree, NULL);
226
}
227
228
static int register_dir(void)
229
{
228
- int res = add_enlistment();
230
+ int res = add_or_remove_enlistment(1);
231
232
if (!res)
233
res = set_recommended_config();
234
235
if (!res)
234
- res = start_maintenance();
236
+ res = toggle_maintenance(1);
237
+
238
+ return res;
239
+}
240
+
241
+static int unregister_dir(void)
242
+{
243
+ int res = 0;
244
+
245
+ if (toggle_maintenance(0) < 0)
246
+ res = -1;
247
+
248
+ if (add_or_remove_enlistment(0) < 0)
249
+ res = -1;
250
251
return res;
252
}
@@ -254,11 +269,30 @@ static int cmd_register(int argc, const char **argv)
269
return register_dir();
270
}
271
272
+static int cmd_unregister(int argc, const char **argv)
273
+{
274
+ struct option options[] = {
275
+ OPT_END(),
276
+ };
277
+ const char * const usage[] = {
278
+ N_("scalar unregister [<enlistment>]"),
279
+ NULL
280
+ };
281
+
282
+ argc = parse_options(argc, argv, NULL, options,
283
+ usage, 0);
284
+
285
+ setup_enlistment_directory(argc, argv, usage, options, NULL);
286
+
287
+ return unregister_dir();
288
+}
289
+
290
static struct {
291
const char *name;
292
int (*fn)(int, const char **);
293
} builtins[] = {
294
{ "register", cmd_register },
295
+ { "unregister", cmd_unregister },
296
{ NULL, NULL},
297
};
298
contrib/scalar/scalar.txt
+8
@@ -9,6 +9,7 @@ SYNOPSIS
9
--------
10
[verse]
11
scalar register [<enlistment>]
12
+scalar unregister [<enlistment>]
13
14
DESCRIPTION
15
-----------
@@ -45,6 +46,13 @@ Note: when this subcommand is called in a worktree that is called `src/`, its
46
parent directory is considered to be the Scalar enlistment. If the worktree is
47
_not_ called `src/`, it itself will be considered to be the Scalar enlistment.
48
49
+Unregister
50
+~~~~~~~~~~
51
+
52
+unregister [<enlistment>]::
53
+ Remove the specified repository from the list of repositories
54
+ registered with Scalar and stop the scheduled background maintenance.
55
+
56
SEE ALSO
57
--------
58
linkgit:git-maintenance[1].