upload fixes
authorFrantisek Hrbata <frantisek@hrbata.com>
Thu, 19 May 2022 09:22:13 +0000 (11:22 +0200)
committerFrantisek Hrbata <frantisek@hrbata.com>
Thu, 19 May 2022 09:22:13 +0000 (11:22 +0200)
Signed-off-by: Frantisek Hrbata <frantisek@hrbata.com>
src/upload.c

index ec2b3398c171793840523e33a20196d1bdf97152..1e6652c06ff28bd9a767b0eccae657d540a2f241 100644 (file)
@@ -151,10 +151,6 @@ static void set_remote_ref_status(struct ref *ref)
        if (is_null_oid(&ref->old_oid))
                return;
 
-       /*
-       if (ends_with(ref->name, "head"))
-               ref->status = REF_STATUS_REJECT_ALREADY_EXISTS;
-       */
        if (!has_object_file(&ref->old_oid))
                ref->status = REF_STATUS_REJECT_FETCH_FIRST;
        else if (!lookup_commit_reference_gently(the_repository, &ref->old_oid, 1))
@@ -226,7 +222,7 @@ static void print_ref_status(struct ref *r, const char *flag, const char *msg,
        type = find_nth_component(r->name, 4, &type_len);
        id = find_nth_component(r->name, 2, &id_len);
        ns = find_nth_component(r->name, 1, &ns_len);
-       ns--;
+       ns_len--;
 
        if (err_msg)
                fprintf(stderr, " %s [%s %.*s %.*s] %.*s (%s)\n", flag, msg, ns_len,
@@ -260,6 +256,26 @@ static void print_upload_status(struct ref *r, int err)
        }
 }
 
+static void delete_local_refs(struct ref *refs)
+{
+       struct ref *r;
+       int type_len, id_len, ns_len;
+       const char *type, *id, *ns;
+
+       for (r = refs; r; r = r->next) {
+               type = find_nth_component(r->name, 6, &type_len);
+               id = find_nth_component(r->name, 4, &id_len);
+               ns = find_nth_component(r->name, 1, &ns_len);
+               ns_len--;
+
+               if (verbose)
+                       fprintf(stderr, "deleting local ref for "
+                                       "%.*s %.*s '%.*s'\n",
+                                       ns_len, ns, type_len, type, id_len,
+                                       id);
+               delete_ref(NULL, r->name, NULL, 0);
+       }
+}
 static void update_tracking_refs(struct ref *refs)
 {
        struct ref *r;
@@ -287,13 +303,6 @@ static void update_tracking_refs(struct ref *refs)
                strbuf_reset(&buf);
                strbuf_addf(&buf, "refs/mrs/remote/%s", remote);
                update_ref("update by gimrr", buf.buf, &r->new_oid, NULL, 0, 0);
-
-               if (verbose)
-                       fprintf(stderr, "deleting local ref for "
-                                       "%.*s %.*s '%.*s'\n",
-                                       ns_len, ns, type_len, type, id_len,
-                                       id);
-               delete_ref(NULL, r->peer_ref->name, NULL, 0);
        }
 
        strbuf_release(&buf);
@@ -305,15 +314,15 @@ static int upload_local_refs_url(struct remote *remote, const char *url,
        struct transport *t = transport_get(remote, url);
        struct transport_ls_refs_options opts = TRANSPORT_LS_REFS_OPTIONS_INIT;
        char *anon_url = transport_anonymize_url(url);
-       struct ref *remote_refs;
-       int err = 0;
+       struct ref *remote_refs = NULL;
+       int err = 1;
 
        if (verbose > 0)
-               fprintf(stderr, _("uploading to remote '%s' with url '%s'\n"),
+               fprintf(stderr, _("Uploading to remote '%s' with url '%s'\n"),
                                remote->name, anon_url);
 
        if (!t->vtable->push_refs)
-               goto err;
+               goto done;
 
        transport_set_verbosity(t, verbosity, progress);
        set_remote_prefixes(local_refs, &opts.ref_prefixes);
@@ -323,22 +332,21 @@ static int upload_local_refs_url(struct remote *remote, const char *url,
        err = t->vtable->push_refs(t, remote_refs, 0);
        print_upload_status(remote_refs, err);
        update_tracking_refs(remote_refs);
-       err += transport_disconnect(t);
-       if (err)
-               goto err;
 
-       if (!quiet)
-               fprintf(stderr, "everything already uploaded for "
+       if (!quiet && !err && !transport_refs_pushed(remote_refs))
+               fprintf(stderr, "Everything up-to-date for "
                                "remote '%s' with url '%s'\n",
                                remote->name, anon_url);
 
+       err += transport_disconnect(t);
+done:
+       if (err)
+               error(_("failed to upload some refs to remote '%s' "
+                       "with url '%s'"), remote->name, anon_url);
+
+       free_refs(remote_refs);
        free(anon_url);
-       return 0;
-err:
-       error(_("failed to upload some refs to remote '%s' "
-               "with url '%s'"), remote->name, anon_url);
-       free(anon_url);
-       return 1;
+       return err;
 }
 
 static int upload_local_refs(struct remote *remote, struct ref *local_refs)
@@ -349,6 +357,8 @@ static int upload_local_refs(struct remote *remote, struct ref *local_refs)
 
        for (i = 0; i < url_nr; ++i)
                rv += upload_local_refs_url(remote, url[i], local_refs);
+       if (!rv)
+               delete_local_refs(local_refs);
 
        return !!rv;
 }