}
/*
- * 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)
{
/*
* 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;
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;
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));
* 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;
}
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
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 */
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;
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);
* 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;
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);
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;
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;
}