Commit 643b85bcb48e24fd2ca96d415d8e61e1ceef9aad

Stefan Sperling 2021-07-16T10:47:10

new -X option for removing backups created by got rebase and got histedit ok semarie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
diff --git a/got/got.1 b/got/got.1
index 905badf..64eb89a 100644
--- a/got/got.1
+++ b/got/got.1
@@ -1437,7 +1437,7 @@ conflicts must be resolved first.
 .It Cm bo
 Short alias for
 .Cm backout .
-.It Cm rebase Oo Fl a Oc Oo Fl c Oc Oo Fl l Oc Op Ar branch
+.It Cm rebase Oo Fl a Oc Oo Fl c Oc Oo Fl l Oc Oo Fl X Oc Op Ar branch
 Rebase commits on the specified
 .Ar branch
 onto the tip of the current branch of the work tree.
@@ -1585,11 +1585,34 @@ If this option is used,
 does not require a work tree.
 None of the other options can be used together with
 .Fl l .
+.It Fl X
+Delete backups created by past rebase operations, represented by references
+in the
+.Dq refs/got/backup/rebase
+reference namespace.
+.Pp
+If a
+.Ar branch
+is specified, only delete backups which at some point in time represented
+this branch.
+Otherwise, delete all references found within
+.Dq refs/got/backup/rebase .
+.Pp
+Any commit, tree, tag, and blob objects belonging to deleted backups
+remain in the repository and may be removed separately with
+Git's garbage collector or
+.Cm gotadmin cleanup .
+.Pp
+If this option is used,
+.Cm got rebase
+does not require a work tree.
+None of the other options can be used together with
+.Fl X .
 .El
 .It Cm rb
 Short alias for
 .Cm rebase .
-.It Cm histedit Oo Fl a Oc Oo Fl c Oc Oo Fl f Oc Oo Fl F Ar histedit-script Oc Oo Fl m Oc Oo Fl l Oc Op Ar branch
+.It Cm histedit Oo Fl a Oc Oo Fl c Oc Oo Fl f Oc Oo Fl F Ar histedit-script Oc Oo Fl m Oc Oo Fl l Oc Oo Fl X Oc Op Ar branch
 Edit commit history between the work tree's current base commit and
 the tip commit of the work tree's current branch.
 .Pp
@@ -1785,6 +1808,29 @@ If this option is used,
 does not require a work tree.
 None of the other options can be used together with
 .Fl l .
+.It Fl X
+Delete backups created by past histedit operations, represented by references
+in the
+.Dq refs/got/backup/histedit
+reference namespace.
+.Pp
+If a
+.Ar branch
+is specified, only delete backups which at some point in time represented
+this branch.
+Otherwise, delete all references found within
+.Dq refs/got/backup/histedit .
+.Pp
+Any commit, tree, tag, and blob objects belonging to deleted backups
+remain in the repository and may be removed separately with
+Git's garbage collector or
+.Cm gotadmin cleanup .
+.Pp
+If this option is used,
+.Cm got histedit
+does not require a work tree.
+None of the other options can be used together with
+.Fl X .
 .El
 .It Cm he
 Short alias for
diff --git a/got/got.c b/got/got.c
index 36887d9..8dc4d8f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -7518,7 +7518,7 @@ done:
 __dead static void
 usage_rebase(void)
 {
-	fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [branch]\n",
+	fprintf(stderr, "usage: %s rebase [-a] [-c] [-l] [-X] [branch]\n",
 	    getprogname());
 	exit(1);
 }
@@ -7834,6 +7834,27 @@ done:
 }
 
 static const struct got_error *
+delete_backup_ref(struct got_reference *ref, struct got_object_id *id,
+    struct got_repository *repo)
+{
+	const struct got_error *err;
+	char *id_str;
+
+	err = got_object_id_str(&id_str, id);
+	if (err)
+		return err;
+
+	err = got_ref_delete(ref, repo);
+	if (err)
+		goto done;
+
+	printf("Deleted %s: %s\n", got_ref_get_name(ref), id_str);
+done:
+	free(id_str);
+	return err;
+}
+
+static const struct got_error *
 print_backup_ref(const char *branch_name, const char *new_id_str,
     struct got_object_id *old_commit_id, struct got_commit_object *old_commit,
     struct got_reflist_object_id_map *refs_idmap,
@@ -7929,8 +7950,8 @@ done:
 }
 
 static const struct got_error *
-list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
-    struct got_repository *repo)
+process_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
+    int delete, struct got_repository *repo)
 {
 	const struct got_error *err;
 	struct got_reflist_head refs, backup_refs;
@@ -7967,6 +7988,10 @@ list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
 		const char *refname = got_ref_get_name(re->ref);
 		char *slash;
 
+		err = check_cancelled(NULL);
+		if (err)
+			break;
+
 		err = got_ref_resolve(&old_commit_id, repo, re->ref);
 		if (err)
 			break;
@@ -7997,8 +8022,14 @@ list_backup_refs(const char *backup_ref_prefix, const char *wanted_branch_name,
 		if (wanted_branch_name == NULL ||
 		    strcmp(wanted_branch_name, branch_name) == 0) {
 			wanted_branch_found = 1;
-			err = print_backup_ref(branch_name, refname,
-			   old_commit_id, old_commit, refs_idmap, repo);
+			if (delete) {
+				err = delete_backup_ref(re->ref,
+				    old_commit_id, repo);
+			} else {
+				err = print_backup_ref(branch_name, refname,
+				   old_commit_id, old_commit, refs_idmap,
+				   repo);
+			}
 			if (err)
 				break;
 		}
@@ -8043,6 +8074,7 @@ cmd_rebase(int argc, char *argv[])
 	struct got_commit_object *commit = NULL;
 	int ch, rebase_in_progress = 0, abort_rebase = 0, continue_rebase = 0;
 	int histedit_in_progress = 0, create_backup = 1, list_backups = 0;
+	int delete_backups = 0;
 	unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
 	struct got_object_id_queue commits;
 	struct got_pathlist_head merged_paths;
@@ -8052,7 +8084,7 @@ cmd_rebase(int argc, char *argv[])
 	STAILQ_INIT(&commits);
 	TAILQ_INIT(&merged_paths);
 
-	while ((ch = getopt(argc, argv, "acl")) != -1) {
+	while ((ch = getopt(argc, argv, "aclX")) != -1) {
 		switch (ch) {
 		case 'a':
 			abort_rebase = 1;
@@ -8063,6 +8095,9 @@ cmd_rebase(int argc, char *argv[])
 		case 'l':
 			list_backups = 1;
 			break;
+		case 'X':
+			delete_backups = 1;
+			break;
 		default:
 			usage_rebase();
 			/* NOTREACHED */
@@ -8082,6 +8117,17 @@ cmd_rebase(int argc, char *argv[])
 			option_conflict('l', 'a');
 		if (continue_rebase)
 			option_conflict('l', 'c');
+		if (delete_backups)
+			option_conflict('l', 'X');
+		if (argc != 0 && argc != 1)
+			usage_rebase();
+	} else if (delete_backups) {
+		if (abort_rebase)
+			option_conflict('X', 'a');
+		if (continue_rebase)
+			option_conflict('X', 'c');
+		if (list_backups)
+			option_conflict('l', 'X');
 		if (argc != 0 && argc != 1)
 			usage_rebase();
 	} else {
@@ -8101,7 +8147,7 @@ cmd_rebase(int argc, char *argv[])
 	}
 	error = got_worktree_open(&worktree, cwd);
 	if (error) {
-		if (list_backups) {
+		if (list_backups || delete_backups) {
 			if (error->code != GOT_ERR_NOT_WORKTREE)
 				goto done;
 		} else {
@@ -8122,9 +8168,10 @@ cmd_rebase(int argc, char *argv[])
 	if (error)
 		goto done;
 
-	if (list_backups) {
-		error = list_backup_refs(GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
-		    argc == 1 ? argv[0] : NULL, repo);
+	if (list_backups || delete_backups) {
+		error = process_backup_refs(
+		    GOT_WORKTREE_REBASE_BACKUP_REF_PREFIX,
+		    argc == 1 ? argv[0] : NULL, delete_backups, repo);
 		goto done; /* nothing else to do */
 	}
 
@@ -8359,7 +8406,8 @@ __dead static void
 usage_histedit(void)
 {
 	fprintf(stderr, "usage: %s histedit [-a] [-c] [-f] "
-	    "[-F histedit-script] [-m] [-l] [branch]\n", getprogname());
+	    "[-F histedit-script] [-m] [-l] [-X] [branch]\n",
+	    getprogname());
 	exit(1);
 }
 
@@ -9193,7 +9241,7 @@ cmd_histedit(int argc, char *argv[])
 	struct got_update_progress_arg upa;
 	int edit_in_progress = 0, abort_edit = 0, continue_edit = 0;
 	int edit_logmsg_only = 0, fold_only = 0;
-	int list_backups = 0;
+	int list_backups = 0, delete_backups = 0;
 	const char *edit_script_path = NULL;
 	unsigned char rebase_status = GOT_STATUS_NO_CHANGE;
 	struct got_object_id_queue commits;
@@ -9208,7 +9256,7 @@ cmd_histedit(int argc, char *argv[])
 	TAILQ_INIT(&merged_paths);
 	memset(&upa, 0, sizeof(upa));
 
-	while ((ch = getopt(argc, argv, "acfF:ml")) != -1) {
+	while ((ch = getopt(argc, argv, "acfF:mlX")) != -1) {
 		switch (ch) {
 		case 'a':
 			abort_edit = 1;
@@ -9228,6 +9276,9 @@ cmd_histedit(int argc, char *argv[])
 		case 'l':
 			list_backups = 1;
 			break;
+		case 'X':
+			delete_backups = 1;
+			break;
 		default:
 			usage_histedit();
 			/* NOTREACHED */
@@ -9269,6 +9320,23 @@ cmd_histedit(int argc, char *argv[])
 			option_conflict('l', 'm');
 		if (fold_only)
 			option_conflict('l', 'f');
+		if (delete_backups)
+			option_conflict('l', 'X');
+		if (argc != 0 && argc != 1)
+			usage_histedit();
+	} else if (delete_backups) {
+		if (abort_edit)
+			option_conflict('X', 'a');
+		if (continue_edit)
+			option_conflict('X', 'c');
+		if (edit_script_path)
+			option_conflict('X', 'F');
+		if (edit_logmsg_only)
+			option_conflict('X', 'm');
+		if (fold_only)
+			option_conflict('X', 'f');
+		if (list_backups)
+			option_conflict('X', 'l');
 		if (argc != 0 && argc != 1)
 			usage_histedit();
 	} else if (argc != 0)
@@ -9290,7 +9358,7 @@ cmd_histedit(int argc, char *argv[])
 	}
 	error = got_worktree_open(&worktree, cwd);
 	if (error) {
-		if (list_backups) {
+		if (list_backups || delete_backups) {
 			if (error->code != GOT_ERR_NOT_WORKTREE)
 				goto done;
 		} else {
@@ -9301,7 +9369,7 @@ cmd_histedit(int argc, char *argv[])
 		}
 	}
 
-	if (list_backups) {
+	if (list_backups || delete_backups) {
 		error = got_repo_open(&repo,
 		    worktree ? got_worktree_get_repo_path(worktree) : cwd,
 		    NULL);
@@ -9311,9 +9379,9 @@ cmd_histedit(int argc, char *argv[])
 		    worktree ? got_worktree_get_root_path(worktree) : NULL);
 		if (error)
 			goto done;
-		error = list_backup_refs(
+		error = process_backup_refs(
 		    GOT_WORKTREE_HISTEDIT_BACKUP_REF_PREFIX,
-		    argc == 1 ? argv[0] : NULL, repo);
+		    argc == 1 ? argv[0] : NULL, delete_backups, repo);
 		goto done; /* nothing else to do */
 	}
 
diff --git a/regress/cmdline/histedit.sh b/regress/cmdline/histedit.sh
index b6f5b85..15eedc6 100755
--- a/regress/cmdline/histedit.sh
+++ b/regress/cmdline/histedit.sh
@@ -171,6 +171,37 @@ EOF
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/repo && got histedit -X master \
+		> $testroot/stdout 2> $testroot/stderr)
+	echo -n "Deleted refs/got/backup/histedit/master/$new_commit2: " \
+		> $testroot/stdout.expected
+	echo "$old_commit2" >> $testroot/stdout.expected
+	echo -n > $testroot/stderr.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/repo && got histedit -l > $testroot/stdout)
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
 	fi
 	test_done "$testroot" "$ret"
 }
diff --git a/regress/cmdline/rebase.sh b/regress/cmdline/rebase.sh
index a1f81c4..d590abf 100755
--- a/regress/cmdline/rebase.sh
+++ b/regress/cmdline/rebase.sh
@@ -186,11 +186,44 @@ EOF
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stdout.expected $testroot/stdout
 		test_done "$testroot" "$ret"
+		return 1
 	fi
 	cmp -s $testroot/stderr.expected $testroot/stderr
 	ret="$?"
 	if [ "$ret" != "0" ]; then
 		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	# Delete all backup refs
+	(cd $testroot/repo && got rebase -X \
+		> $testroot/stdout 2> $testroot/stderr)
+	echo -n "Deleted refs/got/backup/rebase/newbranch/$new_commit2: " \
+		> $testroot/stdout.expected
+	echo "$orig_commit2" >> $testroot/stdout.expected
+	echo -n > $testroot/stderr.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	(cd $testroot/repo && got rebase -l > $testroot/stdout)
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret="$?"
+	if [ "$ret" != "0" ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
 	fi
 	test_done "$testroot" "$ret"
 }