]> granicus.if.org Git - git/commitdiff
Merge branch 'cc/starts-n-ends-with'
authorJunio C Hamano <gitster@pobox.com>
Tue, 17 Dec 2013 19:47:35 +0000 (11:47 -0800)
committerJunio C Hamano <gitster@pobox.com>
Tue, 17 Dec 2013 20:02:44 +0000 (12:02 -0800)
Remove a few duplicate implementations of prefix/suffix comparison
functions, and rename them to starts_with and ends_with.

* cc/starts-n-ends-with:
  replace {pre,suf}fixcmp() with {starts,ends}_with()
  strbuf: introduce starts_with() and ends_with()
  builtin/remote: remove postfixcmp() and use suffixcmp() instead
  environment: normalize use of prefixcmp() by removing " != 0"

25 files changed:
1  2 
builtin/branch.c
builtin/checkout.c
builtin/commit.c
builtin/fast-export.c
builtin/fetch.c
builtin/for-each-ref.c
builtin/name-rev.c
builtin/remote.c
builtin/rev-parse.c
builtin/show-branch.c
commit.c
config.c
diff.c
fetch-pack.c
http.c
log-tree.c
notes-utils.c
pathspec.c
remote-curl.c
remote.c
revision.c
send-pack.c
sha1_name.c
upload-pack.c
wt-status.c

Simple merge
Simple merge
Simple merge
Simple merge
diff --cc builtin/fetch.c
index 3d978eb58ec5cc1e2629db10562319c666bc031e,4dd82504c764e96b3af5210c994b3f5277b3d8a5..e3ac84a0dd14612afdb1eca9f265767a701bc810
@@@ -160,109 -160,13 +160,109 @@@ static void add_merge_config(struct re
        }
  }
  
 +static int add_existing(const char *refname, const unsigned char *sha1,
 +                      int flag, void *cbdata)
 +{
 +      struct string_list *list = (struct string_list *)cbdata;
 +      struct string_list_item *item = string_list_insert(list, refname);
 +      item->util = xmalloc(20);
 +      hashcpy(item->util, sha1);
 +      return 0;
 +}
 +
 +static int will_fetch(struct ref **head, const unsigned char *sha1)
 +{
 +      struct ref *rm = *head;
 +      while (rm) {
 +              if (!hashcmp(rm->old_sha1, sha1))
 +                      return 1;
 +              rm = rm->next;
 +      }
 +      return 0;
 +}
 +
  static void find_non_local_tags(struct transport *transport,
                        struct ref **head,
 -                      struct ref ***tail);
 +                      struct ref ***tail)
 +{
 +      struct string_list existing_refs = STRING_LIST_INIT_DUP;
 +      struct string_list remote_refs = STRING_LIST_INIT_NODUP;
 +      const struct ref *ref;
 +      struct string_list_item *item = NULL;
 +
 +      for_each_ref(add_existing, &existing_refs);
 +      for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
-               if (prefixcmp(ref->name, "refs/tags/"))
++              if (!starts_with(ref->name, "refs/tags/"))
 +                      continue;
 +
 +              /*
 +               * The peeled ref always follows the matching base
 +               * ref, so if we see a peeled ref that we don't want
 +               * to fetch then we can mark the ref entry in the list
 +               * as one to ignore by setting util to NULL.
 +               */
-               if (!suffixcmp(ref->name, "^{}")) {
++              if (ends_with(ref->name, "^{}")) {
 +                      if (item && !has_sha1_file(ref->old_sha1) &&
 +                          !will_fetch(head, ref->old_sha1) &&
 +                          !has_sha1_file(item->util) &&
 +                          !will_fetch(head, item->util))
 +                              item->util = NULL;
 +                      item = NULL;
 +                      continue;
 +              }
 +
 +              /*
 +               * If item is non-NULL here, then we previously saw a
 +               * ref not followed by a peeled reference, so we need
 +               * to check if it is a lightweight tag that we want to
 +               * fetch.
 +               */
 +              if (item && !has_sha1_file(item->util) &&
 +                  !will_fetch(head, item->util))
 +                      item->util = NULL;
 +
 +              item = NULL;
 +
 +              /* skip duplicates and refs that we already have */
 +              if (string_list_has_string(&remote_refs, ref->name) ||
 +                  string_list_has_string(&existing_refs, ref->name))
 +                      continue;
 +
 +              item = string_list_insert(&remote_refs, ref->name);
 +              item->util = (void *)ref->old_sha1;
 +      }
 +      string_list_clear(&existing_refs, 1);
 +
 +      /*
 +       * We may have a final lightweight tag that needs to be
 +       * checked to see if it needs fetching.
 +       */
 +      if (item && !has_sha1_file(item->util) &&
 +          !will_fetch(head, item->util))
 +              item->util = NULL;
 +
 +      /*
 +       * For all the tags in the remote_refs string list,
 +       * add them to the list of refs to be fetched
 +       */
 +      for_each_string_list_item(item, &remote_refs) {
 +              /* Unless we have already decided to ignore this item... */
 +              if (item->util)
 +              {
 +                      struct ref *rm = alloc_ref(item->string);
 +                      rm->peer_ref = alloc_ref(item->string);
 +                      hashcpy(rm->old_sha1, item->util);
 +                      **tail = rm;
 +                      *tail = &rm->next;
 +              }
 +      }
 +
 +      string_list_clear(&remote_refs, 0);
 +}
  
  static struct ref *get_ref_map(struct transport *transport,
 -                             struct refspec *refs, int ref_count, int tags,
 -                             int *autotags)
 +                             struct refspec *refspecs, int refspec_count,
 +                             int tags, int *autotags)
  {
        int i;
        struct ref *rm;
index 875bd813d5ed6d09622fc3473fdb574e9714bf7f,883d383201b7a67b36875ffe4b74b190a6492ae2..6551e7b39b6039032852d6ec0d0dd8da0d7031dc
@@@ -668,13 -655,14 +668,13 @@@ static void populate_value(struct refin
                        name++;
                }
  
-               if (!prefixcmp(name, "refname"))
+               if (starts_with(name, "refname"))
                        refname = ref->refname;
-               else if (!prefixcmp(name, "symref"))
+               else if (starts_with(name, "symref"))
                        refname = ref->symref ? ref->symref : "";
-               else if (!prefixcmp(name, "upstream")) {
+               else if (starts_with(name, "upstream")) {
 -                      struct branch *branch;
                        /* only local branches may have an upstream */
-                       if (prefixcmp(ref->refname, "refs/heads/"))
+                       if (!starts_with(ref->refname, "refs/heads/"))
                                continue;
                        branch = branch_get(ref->refname + 11);
  
                            !branch->merge[0]->dst)
                                continue;
                        refname = branch->merge[0]->dst;
-               } else if (!prefixcmp(name, "color:")) {
 -              }
 -              else if (!strcmp(name, "flag")) {
++              } else if (starts_with(name, "color:")) {
 +                      char color[COLOR_MAXLEN] = "";
 +
 +                      color_parse(name + 6, "--format", color);
 +                      v->s = xstrdup(color);
 +                      continue;
 +              } else if (!strcmp(name, "flag")) {
                        char buf[256], *cp = buf;
                        if (ref->flag & REF_ISSYMREF)
                                cp = copy_advance(cp, ",symref");
                        if (!strcmp(formatp, "short"))
                                refname = shorten_unambiguous_ref(refname,
                                                      warn_ambiguous_refs);
 -                      else
 +                      else if (!strcmp(formatp, "track") &&
-                               !prefixcmp(name, "upstream")) {
++                               starts_with(name, "upstream")) {
 +                              char buf[40];
 +
 +                              stat_tracking_info(branch, &num_ours, &num_theirs);
 +                              if (!num_ours && !num_theirs)
 +                                      v->s = "";
 +                              else if (!num_ours) {
 +                                      sprintf(buf, "[behind %d]", num_theirs);
 +                                      v->s = xstrdup(buf);
 +                              } else if (!num_theirs) {
 +                                      sprintf(buf, "[ahead %d]", num_ours);
 +                                      v->s = xstrdup(buf);
 +                              } else {
 +                                      sprintf(buf, "[ahead %d, behind %d]",
 +                                              num_ours, num_theirs);
 +                                      v->s = xstrdup(buf);
 +                              }
 +                              continue;
 +                      } else if (!strcmp(formatp, "trackshort") &&
-                               !prefixcmp(name, "upstream")) {
++                                 starts_with(name, "upstream")) {
 +                              assert(branch);
 +                              stat_tracking_info(branch, &num_ours, &num_theirs);
 +                              if (!num_ours && !num_theirs)
 +                                      v->s = "=";
 +                              else if (!num_ours)
 +                                      v->s = "<";
 +                              else if (!num_theirs)
 +                                      v->s = ">";
 +                              else
 +                                      v->s = "<>";
 +                              continue;
 +                      } else
                                die("unknown %.*s format %s",
                                    (int)(formatp - name), name, formatp);
                }
Simple merge
index 119e9151ad7fb2a9bcc52d5673a7d51c8d24aacf,b3fd92f6d04a43574e074932c35f41c26f67d6cd..b3ab4cf8f6517c715845db7e17c67cad5f50d730
@@@ -78,14 -77,9 +78,6 @@@ static const char * const builtin_remot
  
  static int verbose;
  
- static inline int postfixcmp(const char *string, const char *postfix)
- {
-       int len1 = strlen(string), len2 = strlen(postfix);
-       if (len1 < len2)
-               return 1;
-       return strcmp(string + len1 - len2, postfix);
- }
 -static int show_all(void);
 -static int prune_remote(const char *remote, int dry_run);
--
  static int fetch_remote(const char *name)
  {
        const char *argv[] = { "fetch", name, NULL, NULL };
index 1d9ecafd417cf763d0c33438b74cec28c463ed74,6b894027320648cc2efd9ea9b9d7e32963892bbd..6e802fdeb8bce0f7fdce925f47fc239462f6a459
@@@ -627,46 -615,35 +627,46 @@@ int cmd_rev_parse(int argc, const char 
                                for_each_ref_in("refs/bisect/good", anti_reference, NULL);
                                continue;
                        }
-                       if (!prefixcmp(arg, "--branches=")) {
+                       if (starts_with(arg, "--branches=")) {
                                for_each_glob_ref_in(show_reference, arg + 11,
                                        "refs/heads/", NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--branches")) {
                                for_each_branch_ref(show_reference, NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
-                       if (!prefixcmp(arg, "--tags=")) {
+                       if (starts_with(arg, "--tags=")) {
                                for_each_glob_ref_in(show_reference, arg + 7,
                                        "refs/tags/", NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--tags")) {
                                for_each_tag_ref(show_reference, NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
-                       if (!prefixcmp(arg, "--glob=")) {
+                       if (starts_with(arg, "--glob=")) {
                                for_each_glob_ref(show_reference, arg + 7, NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
-                       if (!prefixcmp(arg, "--remotes=")) {
+                       if (starts_with(arg, "--remotes=")) {
                                for_each_glob_ref_in(show_reference, arg + 10,
                                        "refs/remotes/", NULL);
 +                              clear_ref_exclusion(&ref_excludes);
                                continue;
                        }
                        if (!strcmp(arg, "--remotes")) {
                                for_each_remote_ref(show_reference, NULL);
-                       if (!prefixcmp(arg, "--exclude=")) {
 +                              clear_ref_exclusion(&ref_excludes);
 +                              continue;
 +                      }
++                      if (starts_with(arg, "--exclude=")) {
 +                              add_ref_exclusion(&ref_excludes, arg + 10);
                                continue;
                        }
                        if (!strcmp(arg, "--local-env-vars")) {
Simple merge
diff --cc commit.c
Simple merge
diff --cc config.c
Simple merge
diff --cc diff.c
index 3950e01910674c15d8af2c0d5d114402f43b1a9c,d53029b6275c4299db59d489f17d887e12bfcaae..b79432b1ec97909e20eb76fceb5530b2f7be647f
--- 1/diff.c
--- 2/diff.c
+++ b/diff.c
@@@ -3391,10 -3391,10 +3391,10 @@@ int parse_long_opt(const char *opt, con
        if (arg[0] != '-' || arg[1] != '-')
                return 0;
        arg += strlen("--");
-       if (prefixcmp(arg, opt))
+       if (!starts_with(arg, opt))
                return 0;
        arg += strlen(opt);
 -      if (*arg == '=') { /* sticked form: --option=value */
 +      if (*arg == '=') { /* stuck form: --option=value */
                *optarg = arg + 1;
                return 1;
        }
diff --cc fetch-pack.c
Simple merge
diff --cc http.c
Simple merge
diff --cc log-tree.c
Simple merge
diff --cc notes-utils.c
Simple merge
diff --cc pathspec.c
Simple merge
diff --cc remote-curl.c
Simple merge
diff --cc remote.c
Simple merge
diff --cc revision.c
index 05d2d7763a3c9a2bd555cb19ef9af01c5767adb8,10854b07071db090c7422dfb16e60dd87d305c0e..a68fde6e959c3f305fa7dba285783bd4300b8c0f
@@@ -2022,22 -1983,16 +2022,22 @@@ static int handle_revision_pseudo_opt(c
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref(handle_one_ref, optarg, &cb);
 +              clear_ref_exclusion(&revs->ref_excludes);
 +              return argcount;
 +      } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
 +              add_ref_exclusion(&revs->ref_excludes, optarg);
                return argcount;
-       } else if (!prefixcmp(arg, "--branches=")) {
+       } else if (starts_with(arg, "--branches=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
-       } else if (!prefixcmp(arg, "--tags=")) {
 +              clear_ref_exclusion(&revs->ref_excludes);
+       } else if (starts_with(arg, "--tags=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
-       } else if (!prefixcmp(arg, "--remotes=")) {
 +              clear_ref_exclusion(&revs->ref_excludes);
+       } else if (starts_with(arg, "--remotes=")) {
                struct all_refs_cb cb;
                init_all_refs_cb(&cb, revs, *flags);
                for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
diff --cc send-pack.c
Simple merge
diff --cc sha1_name.c
Simple merge
diff --cc upload-pack.c
Simple merge
diff --cc wt-status.c
Simple merge