is_refname_available(): use dirname in first loop

In the first loop (over prefixes of refname), use dirname to keep track of the current prefix. This is not an improvement in itself, but in a moment we will start using dirname for a role where a NUL-terminated string is needed. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>

Michael Haggerty committed May 11, 2015 at 17:25 UTC 61da59699263afcbf8f1e3c66763237fe35ba670
1 file changed +26 -14
refs.c
+26 -14
@@ -878,26 +878,30 @@ static int is_refname_available(const char *refname,
878 const char *slash;
879 int pos;
880 struct strbuf dirname = STRBUF_INIT;
881 + int ret = 0;
882
883 /*
884 * For the sake of comments in this function, suppose that
885 * refname is "refs/foo/bar".
886 */
887
888 + strbuf_grow(&dirname, strlen(refname) + 1);
889 for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
890 + /* Expand dirname to the new prefix, not including the trailing slash: */
891 + strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
892 +
893 /*
894 * We are still at a leading dir of the refname (e.g.,
895 * "refs/foo"; if there is a reference with that name,
896 * it is a conflict, *unless* it is in skip.
897 */
893 - pos = search_ref_dir(dir, refname, slash - refname);
898 + pos = search_ref_dir(dir, dirname.buf, dirname.len);
899 if (pos >= 0) {
900 /*
901 * We found a reference whose name is a proper
902 * prefix of refname; e.g., "refs/foo".
903 */
899 - struct ref_entry *entry = dir->entries[pos];
900 - if (skip && string_list_has_string(skip, entry->name)) {
904 + if (skip && string_list_has_string(skip, dirname.buf)) {
905 /*
906 * The reference we just found, e.g.,
907 * "refs/foo", is also in skip, so it
@@ -910,10 +914,11 @@ static int is_refname_available(const char *refname,
914 * "refs/foo"). So we can stop looking
915 * now and return true.
916 */
913 - return 1;
917 + ret = 1;
918 + goto cleanup;
919 }
915 - error("'%s' exists; cannot create '%s'", entry->name, refname);
916 - return 0;
920 + error("'%s' exists; cannot create '%s'", dirname.buf, refname);
921 + goto cleanup;
922 }
923
924
@@ -922,14 +927,16 @@ static int is_refname_available(const char *refname,
927 * the next component. So try to look up the
928 * directory, e.g., "refs/foo/".
929 */
925 - pos = search_ref_dir(dir, refname, slash + 1 - refname);
930 + strbuf_addch(&dirname, '/');
931 + pos = search_ref_dir(dir, dirname.buf, dirname.len);
932 if (pos < 0) {
933 /*
934 * There was no directory "refs/foo/", so
935 * there is nothing under this whole prefix,
936 * and we are OK.
937 */
932 - return 1;
938 + ret = 1;
939 + goto cleanup;
940 }
941
942 dir = get_ref_dir(dir->entries[pos]);
@@ -943,10 +950,9 @@ static int is_refname_available(const char *refname,
950 * names are in the "refs/foo/bar/" namespace, because they
951 * *do* conflict.
952 */
946 - strbuf_addstr(&dirname, refname);
953 + strbuf_addstr(&dirname, refname + dirname.len);
954 strbuf_addch(&dirname, '/');
955 pos = search_ref_dir(dir, dirname.buf, dirname.len);
949 - strbuf_release(&dirname);
956
957 if (pos >= 0) {
958 /*
@@ -960,15 +966,21 @@ static int is_refname_available(const char *refname,
966 dir = get_ref_dir(entry);
967 data.skip = skip;
968 sort_ref_dir(dir);
963 - if (!do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data))
964 - return 1;
969 + if (!do_for_each_entry_in_dir(dir, 0, nonmatching_ref_fn, &data)) {
970 + ret = 1;
971 + goto cleanup;
972 + }
973
974 error("'%s' exists; cannot create '%s'",
975 data.conflicting_refname, refname);
968 - return 0;
976 + goto cleanup;
977 }
978
971 - return 1;
979 + ret = 1;
980 +
981 +cleanup:
982 + strbuf_release(&dirname);
983 + return ret;
984 }
985
986 struct packed_ref_cache {