Thread (6 messages) flat view 6 messages, 5 authors, 2025-08-07

Re: [PATCH v2] remote.c: convert if-else tower to switch

From: Ben Knoble <hidden>
Date: 2025-08-07 12:35:30

quoted hunk ↗ jump to hunk
Le 7 août 2025 à 05:20, Denton Liu [off-list ref] a écrit :

For better readability, convert the if-else tower into a switch
statement.

Signed-off-by: Denton Liu <redacted>
---
remote.c | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/remote.c b/remote.c
index 465e0ea0eb..029b1fa93b 100644
--- a/remote.c
+++ b/remote.c
@@ -1171,7 +1171,6 @@ static void show_push_unqualified_ref_name_error(const char *dst_value,
                        const char *matched_src_name)
{
   struct object_id oid;
-    enum object_type type;

   /*
    * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
@@ -1196,30 +1195,36 @@ static void show_push_unqualified_ref_name_error(const char *dst_value,
       BUG("'%s' is not a valid object, "
           "match_explicit_lhs() should catch this!",
           matched_src_name);
-    type = odb_read_object_info(the_repository->objects, &oid, NULL);
-    if (type == OBJ_COMMIT) {
+
+    switch (odb_read_object_info(the_repository->objects, &oid, NULL)) {
+    case OBJ_COMMIT:
       advise(_("The <src> part of the refspec is a commit object.\n"
            "Did you mean to create a new branch by pushing to\n"
            "'%s:refs/heads/%s'?"),
              matched_src_name, dst_value);
-    } else if (type == OBJ_TAG) {
+        break;
+    case OBJ_TAG:
       advise(_("The <src> part of the refspec is a tag object.\n"
            "Did you mean to create a new tag by pushing to\n"
            "'%s:refs/tags/%s'?"),
              matched_src_name, dst_value);
-    } else if (type == OBJ_TREE) {
+        break;
+    case OBJ_TREE:
       advise(_("The <src> part of the refspec is a tree object.\n"
            "Did you mean to tag a new tree by pushing to\n"
            "'%s:refs/tags/%s'?"),
              matched_src_name, dst_value);
-    } else if (type == OBJ_BLOB) {
+        break;
+    case OBJ_BLOB:
       advise(_("The <src> part of the refspec is a blob object.\n"
            "Did you mean to tag a new blob by pushing to\n"
            "'%s:refs/tags/%s'?"),
              matched_src_name, dst_value);
-    } else {
+        break;
+    default:
       advise(_("The <src> part of the refspec ('%s') is an object ID that doesn't exist.\n"),
              matched_src_name);
+        break;
   }
}


Range-diff against v1:
Don’t we normally put single-patch notes like a range-diff right after the triple dash? I have a feeling this format breaks git-am on the receiving side, though I haven’t actually tried it. 
1:  5866818859 ! 1:  54a16614e2 remote.c: convert if-else tower to switch
   @@ Commit message

     ## remote.c ##
    @@ remote.c: static void show_push_unqualified_ref_name_error(const char *dst_value,
   +                         const char *matched_src_name)
   + {
   +    struct object_id oid;
   +-    enum object_type type;
   +
   +    /*
   +     * TRANSLATORS: "matches '%s'%" is the <dst> part of "git push
   +@@ remote.c: static void show_push_unqualified_ref_name_error(const char *dst_value,
   +        BUG("'%s' is not a valid object, "
                 "match_explicit_lhs() should catch this!",
                 matched_src_name);
   -    type = odb_read_object_info(the_repository->objects, &oid, NULL);
   +-    type = odb_read_object_info(the_repository->objects, &oid, NULL);
    -    if (type == OBJ_COMMIT) {
   -+    switch (type) {
   ++
   ++    switch (odb_read_object_info(the_repository->objects, &oid, NULL)) {
    +    case OBJ_COMMIT:
             advise(_("The <src> part of the refspec is a commit object.\n"
                  "Did you mean to create a new branch by pushing to\n"
--
2.50.1
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help