add 'got fetch -X' option for deleting references created by 'got fetch'
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
diff --git a/got/got.1 b/got/got.1
index 64eb89a..2fc7881 100644
--- a/got/got.1
+++ b/got/got.1
@@ -319,7 +319,7 @@ namespace.
.It Cm cl
Short alias for
.Cm clone .
-.It Cm fetch Oo Fl a Oc Oo Fl b Ar branch Oc Oo Fl d Oc Oo Fl l Oc Oo Fl r Ar repository-path Oc Oo Fl t Oc Oo Fl q Oc Oo Fl v Oc Oo Fl R Ar reference Oc Op Ar remote-repository
+.It Cm fetch Oo Fl a Oc Oo Fl b Ar branch Oc Oo Fl d Oc Oo Fl l Oc Oo Fl r Ar repository-path Oc Oo Fl t Oc Oo Fl q Oc Oo Fl v Oc Oo Fl R Ar reference Oc Oo Fl X Oc Op Ar remote-repository
Fetch new changes from a remote repository.
If no
.Ar remote-repository
@@ -466,6 +466,29 @@ will refuse to fetch references from the remote repository's
or
.Dq refs/got/
namespace.
+.It Fl X
+Delete all references which correspond to a particular
+.Ar remote-repository
+instead of fetching new changes.
+This can be useful when a remote repository is being removed from
+.Xr got.conf 5 .
+.Pp
+With
+.Fl X ,
+the
+.Ar remote-repository
+argument is mandatory and no other options except
+.Fl r ,
+.Fl v ,
+and
+.Fl q
+are allowed.
+.Pp
+Only references are deleted.
+Any commit, tree, tag, and blob objects fetched from a remote repository
+will generally be stored in pack files and may be removed separately with
+.Xr git-repack 1
+and Git's garbage collector.
.El
.It Cm fe
Short alias for
diff --git a/got/got.c b/got/got.c
index f4cf4eb..eead62c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1949,7 +1949,7 @@ __dead static void
usage_fetch(void)
{
fprintf(stderr, "usage: %s fetch [-a] [-b branch] [-d] [-l] "
- "[-r repository-path] [-t] [-q] [-v] [-R reference] "
+ "[-r repository-path] [-t] [-q] [-v] [-R reference] [-X] "
"[remote-repository-name]\n",
getprogname());
exit(1);
@@ -2116,6 +2116,62 @@ done:
}
static const struct got_error *
+delete_ref(struct got_repository *repo, struct got_reference *ref)
+{
+ const struct got_error *err = NULL;
+ struct got_object_id *id = NULL;
+ char *id_str = NULL;
+ const char *target;
+
+ if (got_ref_is_symbolic(ref)) {
+ target = got_ref_get_symref_target(ref);
+ } else {
+ err = got_ref_resolve(&id, repo, ref);
+ if (err)
+ goto done;
+ err = got_object_id_str(&id_str, id);
+ if (err)
+ goto done;
+ target = id_str;
+ }
+
+ err = got_ref_delete(ref, repo);
+ if (err)
+ goto done;
+
+ printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
+done:
+ free(id);
+ free(id_str);
+ return err;
+}
+
+static const struct got_error *
+delete_refs_for_remote(struct got_repository *repo, const char *remote_name)
+{
+ const struct got_error *err = NULL;
+ struct got_reflist_head refs;
+ struct got_reflist_entry *re;
+ char *prefix;
+
+ TAILQ_INIT(&refs);
+
+ if (asprintf(&prefix, "refs/remotes/%s", remote_name) == -1) {
+ err = got_error_from_errno("asprintf");
+ goto done;
+ }
+ err = got_ref_list(&refs, repo, prefix, got_ref_cmp_by_name, NULL);
+ if (err)
+ goto done;
+
+ TAILQ_FOREACH(re, &refs, entry)
+ delete_ref(repo, re->ref);
+done:
+ got_ref_list_free(&refs);
+ return err;
+}
+
+static const struct got_error *
cmd_fetch(int argc, char *argv[])
{
const struct got_error *error = NULL, *unlock_err;
@@ -2136,14 +2192,14 @@ cmd_fetch(int argc, char *argv[])
pid_t fetchpid = -1;
struct got_fetch_progress_arg fpa;
int verbosity = 0, fetch_all_branches = 0, list_refs_only = 0;
- int delete_refs = 0, replace_tags = 0;
+ int delete_refs = 0, replace_tags = 0, delete_remote = 0;
TAILQ_INIT(&refs);
TAILQ_INIT(&symrefs);
TAILQ_INIT(&wanted_branches);
TAILQ_INIT(&wanted_refs);
- while ((ch = getopt(argc, argv, "ab:dlr:tvqR:")) != -1) {
+ while ((ch = getopt(argc, argv, "ab:dlr:tvqR:X")) != -1) {
switch (ch) {
case 'a':
fetch_all_branches = 1;
@@ -2185,6 +2241,9 @@ cmd_fetch(int argc, char *argv[])
if (error)
return error;
break;
+ case 'X':
+ delete_remote = 1;
+ break;
default:
usage_fetch();
break;
@@ -2202,11 +2261,27 @@ cmd_fetch(int argc, char *argv[])
option_conflict('l', 'a');
if (delete_refs)
option_conflict('l', 'd');
+ if (delete_remote)
+ option_conflict('l', 'X');
+ }
+ if (delete_remote) {
+ if (fetch_all_branches)
+ option_conflict('X', 'a');
+ if (!TAILQ_EMPTY(&wanted_branches))
+ option_conflict('X', 'b');
+ if (delete_refs)
+ option_conflict('X', 'd');
+ if (replace_tags)
+ option_conflict('X', 't');
+ if (!TAILQ_EMPTY(&wanted_refs))
+ option_conflict('X', 'R');
}
- if (argc == 0)
+ if (argc == 0) {
+ if (delete_remote)
+ errx(1, "-X option requires a remote name");
remote_name = GOT_FETCH_DEFAULT_REMOTE_NAME;
- else if (argc == 1)
+ } else if (argc == 1)
remote_name = argv[0];
else
usage_fetch();
@@ -2243,6 +2318,11 @@ cmd_fetch(int argc, char *argv[])
if (error)
goto done;
+ if (delete_remote) {
+ error = delete_refs_for_remote(repo, remote_name);
+ goto done; /* nothing else to do */
+ }
+
if (worktree) {
worktree_conf = got_worktree_get_gotconfig(worktree);
if (worktree_conf) {
@@ -5273,39 +5353,17 @@ list_refs(struct got_repository *repo, const char *refname)
}
static const struct got_error *
-delete_ref(struct got_repository *repo, const char *refname)
+delete_ref_by_name(struct got_repository *repo, const char *refname)
{
- const struct got_error *err = NULL;
+ const struct got_error *err;
struct got_reference *ref;
- struct got_object_id *id = NULL;
- char *id_str = NULL;
- const char *target;
err = got_ref_open(&ref, repo, refname, 0);
if (err)
return err;
- if (got_ref_is_symbolic(ref)) {
- target = got_ref_get_symref_target(ref);
- } else {
- err = got_ref_resolve(&id, repo, ref);
- if (err)
- goto done;
- err = got_object_id_str(&id_str, id);
- if (err)
- goto done;
- target = id_str;
- }
-
- err = got_ref_delete(ref, repo);
- if (err)
- goto done;
-
- printf("Deleted %s: %s\n", got_ref_get_name(ref), target);
-done:
+ err = delete_ref(repo, ref);
got_ref_close(ref);
- free(id);
- free(id_str);
return err;
}
@@ -5512,7 +5570,7 @@ cmd_ref(int argc, char *argv[])
if (do_list)
error = list_refs(repo, refname);
else if (do_delete)
- error = delete_ref(repo, refname);
+ error = delete_ref_by_name(repo, refname);
else if (symref_target)
error = add_symref(repo, refname, symref_target);
else {
diff --git a/regress/cmdline/fetch.sh b/regress/cmdline/fetch.sh
index 5292407..c0be768 100755
--- a/regress/cmdline/fetch.sh
+++ b/regress/cmdline/fetch.sh
@@ -1090,6 +1090,100 @@ EOF
test_done "$testroot" "$ret"
}
+test_fetch_delete_remote_refs() {
+ local testroot=`test_init fetch_basic`
+ local testurl=ssh://127.0.0.1/$testroot
+ local commit_id=`git_show_head $testroot/repo`
+
+ got clone -q $testurl/repo $testroot/repo-clone
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got clone command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+ if [ "$ret" != "0" ]; then
+ echo "got ref command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+ echo "refs/remotes/origin/HEAD: refs/remotes/origin/master" \
+ >> $testroot/stdout.expected
+ echo "refs/remotes/origin/master: $commit_id" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got fetch -q -r $testroot/repo-clone -X > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "got fetch command succeeded unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: -X option requires a remote name" > $testroot/stderr.expected
+ cmp -s $testroot/stderr $testroot/stderr.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got fetch -q -r $testroot/repo-clone -X origin > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "got fetch command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo -n "Deleted refs/remotes/origin/HEAD: " > $testroot/stdout.expected
+ echo "refs/remotes/origin/master" >> $testroot/stdout.expected
+ echo "Deleted refs/remotes/origin/master: $commit_id" \
+ >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ got ref -l -r $testroot/repo-clone > $testroot/stdout
+ if [ "$ret" != "0" ]; then
+ echo "got ref command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "HEAD: refs/heads/master" > $testroot/stdout.expected
+ echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
+
+ cmp -s $testroot/stdout $testroot/stdout.expected
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
+
test_parseargs "$@"
run_test test_fetch_basic
run_test test_fetch_list
@@ -1103,3 +1197,4 @@ run_test test_fetch_replace_symref
run_test test_fetch_update_headref
run_test test_fetch_headref_deleted_locally
run_test test_fetch_gotconfig_remote_repo
+run_test test_fetch_delete_remote_refs