X-Git-Url: http://git.ipfire.org/?a=blobdiff_plain;f=refs.c;h=f48c58a7b672920d3975c21a2bcd61f6b55ab8ec;hb=03b32623d82d20835d8dda3f9ce9900f09485d11;hp=132eff52ca4092eae4c2e1c0f86a8c380f88778a;hpb=95b86a60cfe36055671006aa06c8d9ef27cb5bd6;p=thirdparty%2Fgit.git diff --git a/refs.c b/refs.c index 132eff52ca..f48c58a7b6 100644 --- a/refs.c +++ b/refs.c @@ -341,13 +341,17 @@ static struct ref_dir *get_ref_dir(struct ref_entry *entry) } /* - * Check if a refname is safe. - * For refs that start with "refs/" we consider it safe as long they do - * not try to resolve to outside of refs/. + * Return true iff refname is minimally safe. "Safe" here means that + * deleting a loose reference by this name will not do any damage, for + * example by causing a file that is not a reference to be deleted. + * This function does not check that the reference name is legal; for + * that, use check_refname_format(). * - * For all other refs we only consider them safe iff they only contain - * upper case characters and '_' (like "HEAD" AND "MERGE_HEAD", and not like - * "config"). + * We consider a refname that starts with "refs/" to be safe as long + * as any ".." components that it might contain do not escape "refs/". + * Names that do not start with "refs/" are considered safe iff they + * consist entirely of upper case characters and '_' (like "HEAD" and + * "MERGE_HEAD" but not "config" or "FOO/BAR"). */ static int refname_is_safe(const char *refname) { @@ -897,25 +901,13 @@ static int nonmatching_ref_fn(struct ref_entry *entry, void *vdata) /* * Return 0 if a reference named refname could be created without * conflicting with the name of an existing reference in dir. - * Otherwise, return a negative value and write an explanation to err. - * If extras is non-NULL, it is a list of additional refnames with - * which refname is not allowed to conflict. If skip is non-NULL, - * ignore potential conflicts with refs in skip (e.g., because they - * are scheduled for deletion in the same operation). Behavior is - * undefined if the same name is listed in both extras and skip. - * - * Two reference names conflict if one of them exactly matches the - * leading components of the other; e.g., "refs/foo/bar" conflicts - * with both "refs/foo" and with "refs/foo/bar/baz" but not with - * "refs/foo/bar" or "refs/foo/barbados". - * - * extras and skip must be sorted. + * See verify_refname_available for more information. */ -static int verify_refname_available(const char *refname, - const struct string_list *extras, - const struct string_list *skip, - struct ref_dir *dir, - struct strbuf *err) +static int verify_refname_available_dir(const char *refname, + const struct string_list *extras, + const struct string_list *skip, + struct ref_dir *dir, + struct strbuf *err) { const char *slash; int pos; @@ -2465,8 +2457,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, strbuf_git_path(&orig_ref_file, "%s", orig_refname); if (remove_empty_directories(&orig_ref_file)) { last_errno = errno; - if (!verify_refname_available(orig_refname, extras, skip, - get_loose_refs(&ref_cache), err)) + if (!verify_refname_available_dir(orig_refname, extras, skip, + get_loose_refs(&ref_cache), err)) strbuf_addf(err, "there are still refs under '%s'", orig_refname); goto error_return; @@ -2479,8 +2471,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, if (!refname) { last_errno = errno; if (last_errno != ENOTDIR || - !verify_refname_available(orig_refname, extras, skip, - get_loose_refs(&ref_cache), err)) + !verify_refname_available_dir(orig_refname, extras, skip, + get_loose_refs(&ref_cache), err)) strbuf_addf(err, "unable to resolve reference %s: %s", orig_refname, strerror(last_errno)); @@ -2493,8 +2485,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname, * our refname. */ if (is_null_oid(&lock->old_oid) && - verify_refname_available(refname, extras, skip, - get_packed_refs(&ref_cache), err)) { + verify_refname_available_dir(refname, extras, skip, + get_packed_refs(&ref_cache), err)) { last_errno = ENOTDIR; goto error_return; } @@ -2683,8 +2675,6 @@ struct pack_refs_cb_data { struct ref_to_prune *ref_to_prune; }; -static int is_per_worktree_ref(const char *refname); - /* * An each_ref_entry_fn that is run over loose references only. If * the loose reference can be packed, add an entry in the packed ref @@ -2699,7 +2689,7 @@ static int pack_if_possible_fn(struct ref_entry *entry, void *cb_data) int is_tag_ref = starts_with(entry->name, "refs/tags/"); /* Do not pack per-worktree refs: */ - if (is_per_worktree_ref(entry->name)) + if (ref_type(entry->name) != REF_TYPE_NORMAL) return 0; /* ALWAYS pack tags */ @@ -3120,6 +3110,40 @@ out: return ret; } +/* + * Return 0 if a reference named refname could be created without + * conflicting with the name of an existing reference. Otherwise, + * return a negative value and write an explanation to err. If extras + * is non-NULL, it is a list of additional refnames with which refname + * is not allowed to conflict. If skip is non-NULL, ignore potential + * conflicts with refs in skip (e.g., because they are scheduled for + * deletion in the same operation). Behavior is undefined if the same + * name is listed in both extras and skip. + * + * Two reference names conflict if one of them exactly matches the + * leading components of the other; e.g., "foo/bar" conflicts with + * both "foo" and with "foo/bar/baz" but not with "foo/bar" or + * "foo/barbados". + * + * extras and skip must be sorted. + */ +static int verify_refname_available(const char *newname, + struct string_list *extras, + struct string_list *skip, + struct strbuf *err) +{ + struct ref_dir *packed_refs = get_packed_refs(&ref_cache); + struct ref_dir *loose_refs = get_loose_refs(&ref_cache); + + if (verify_refname_available_dir(newname, extras, skip, + packed_refs, err) || + verify_refname_available_dir(newname, extras, skip, + loose_refs, err)) + return -1; + + return 0; +} + static int rename_ref_available(const char *oldname, const char *newname) { struct string_list skip = STRING_LIST_INIT_NODUP; @@ -3127,10 +3151,7 @@ static int rename_ref_available(const char *oldname, const char *newname) int ret; string_list_insert(&skip, oldname); - ret = !verify_refname_available(newname, NULL, &skip, - get_packed_refs(&ref_cache), &err) - && !verify_refname_available(newname, NULL, &skip, - get_loose_refs(&ref_cache), &err); + ret = !verify_refname_available(newname, NULL, &skip, &err); if (!ret) error("%s", err.buf); @@ -3268,7 +3289,7 @@ static int commit_ref(struct ref_lock *lock) * large, while cleaning up the whitespaces. Especially, convert LF to space, * because reflog file is one line per entry. */ -static int copy_msg(char *buf, const char *msg) +static int copy_reflog_msg(char *buf, const char *msg) { char *cp = buf; char c; @@ -3372,7 +3393,7 @@ static int log_ref_write_fd(int fd, const unsigned char *old_sha1, sha1_to_hex(new_sha1), committer); if (msglen) - len += copy_msg(logrec + len - 1, msg) - 1; + len += copy_reflog_msg(logrec + len - 1, msg) - 1; written = len <= maxlen ? write_in_full(fd, logrec, len) : -1; free(logrec); @@ -4334,8 +4355,6 @@ static int ref_present(const char *refname, int initial_ref_transaction_commit(struct ref_transaction *transaction, struct strbuf *err) { - struct ref_dir *loose_refs = get_loose_refs(&ref_cache); - struct ref_dir *packed_refs = get_packed_refs(&ref_cache); int ret = 0, i; int n = transaction->nr; struct ref_update **updates = transaction->updates; @@ -4378,10 +4397,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction, die("BUG: initial ref transaction with old_sha1 set"); if (verify_refname_available(update->refname, &affected_refnames, NULL, - loose_refs, err) || - verify_refname_available(update->refname, - &affected_refnames, NULL, - packed_refs, err)) { + err)) { ret = TRANSACTION_NAME_CONFLICT; goto cleanup; }