diff options
Diffstat (limited to 'refs.h')
-rw-r--r-- | refs.h | 461 |
1 files changed, 346 insertions, 115 deletions
@@ -1,6 +1,146 @@ #ifndef REFS_H #define REFS_H +struct object_id; +struct ref_store; +struct strbuf; +struct string_list; + +/* + * Resolve a reference, recursively following symbolic refererences. + * + * Store the referred-to object's name in sha1 and return the name of + * the non-symbolic reference that ultimately pointed at it. The + * return value, if not NULL, is a pointer into either a static buffer + * or the input ref. + * + * If the reference cannot be resolved to an object, the behavior + * depends on the RESOLVE_REF_READING flag: + * + * - If RESOLVE_REF_READING is set, return NULL. + * + * - If RESOLVE_REF_READING is not set, clear sha1 and return the name of + * the last reference name in the chain, which will either be a non-symbolic + * reference or an undefined reference. If this is a prelude to + * "writing" to the ref, the return value is the name of the ref + * that will actually be created or changed. + * + * If the RESOLVE_REF_NO_RECURSE flag is passed, only resolves one + * level of symbolic reference. The value stored in sha1 for a symbolic + * reference will always be null_sha1 in this case, and the return + * value is the reference that the symref refers to directly. + * + * If flags is non-NULL, set the value that it points to the + * combination of REF_ISPACKED (if the reference was found among the + * packed references), REF_ISSYMREF (if the initial reference was a + * symbolic reference), REF_BAD_NAME (if the reference name is ill + * formed --- see RESOLVE_REF_ALLOW_BAD_NAME below), and REF_ISBROKEN + * (if the ref is malformed or has a bad name). See refs.h for more detail + * on each flag. + * + * If ref is not a properly-formatted, normalized reference, return + * NULL. If more than MAXDEPTH recursive symbolic lookups are needed, + * give up and return NULL. + * + * RESOLVE_REF_ALLOW_BAD_NAME allows resolving refs even when their + * name is invalid according to git-check-ref-format(1). If the name + * is bad then the value stored in sha1 will be null_sha1 and the two + * flags REF_ISBROKEN and REF_BAD_NAME will be set. + * + * Even with RESOLVE_REF_ALLOW_BAD_NAME, names that escape the refs/ + * directory and do not consist of all caps and underscores cannot be + * resolved. The function returns NULL for such ref names. + * Caps and underscores refers to the special refs, such as HEAD, + * FETCH_HEAD and friends, that all live outside of the refs/ directory. + */ +#define RESOLVE_REF_READING 0x01 +#define RESOLVE_REF_NO_RECURSE 0x02 +#define RESOLVE_REF_ALLOW_BAD_NAME 0x04 + +const char *refs_resolve_ref_unsafe(struct ref_store *refs, + const char *refname, + int resolve_flags, + unsigned char *sha1, + int *flags); +const char *resolve_ref_unsafe(const char *refname, int resolve_flags, + unsigned char *sha1, int *flags); + +char *refs_resolve_refdup(struct ref_store *refs, + const char *refname, int resolve_flags, + unsigned char *sha1, int *flags); +char *resolve_refdup(const char *refname, int resolve_flags, + unsigned char *sha1, int *flags); + +int refs_read_ref_full(struct ref_store *refs, const char *refname, + int resolve_flags, unsigned char *sha1, int *flags); +int read_ref_full(const char *refname, int resolve_flags, + unsigned char *sha1, int *flags); +int read_ref(const char *refname, unsigned char *sha1); + +/* + * 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. + */ + +int refs_verify_refname_available(struct ref_store *refs, + const char *refname, + const struct string_list *extras, + const struct string_list *skip, + struct strbuf *err); + +int ref_exists(const char *refname); + +int should_autocreate_reflog(const char *refname); + +int is_branch(const char *refname); + +extern int refs_init_db(struct strbuf *err); + +/* + * If refname is a non-symbolic reference that refers to a tag object, + * and the tag can be (recursively) dereferenced to a non-tag object, + * store the SHA1 of the referred-to object to sha1 and return 0. If + * any of these conditions are not met, return a non-zero value. + * Symbolic references are considered unpeelable, even if they + * ultimately resolve to a peelable tag. + */ +int refs_peel_ref(struct ref_store *refs, const char *refname, + unsigned char *sha1); +int peel_ref(const char *refname, unsigned char *sha1); + +/** + * Resolve refname in the nested "gitlink" repository in the specified + * submodule (which must be non-NULL). If the resolution is + * successful, return 0 and set sha1 to the name of the object; + * otherwise, return a non-zero value. + */ +int resolve_gitlink_ref(const char *submodule, const char *refname, + unsigned char *sha1); + +/* + * Return true iff abbrev_name is a possible abbreviation for + * full_name according to the rules defined by ref_rev_parse_rules in + * refs.c. + */ +int refname_match(const char *abbrev_name, const char *full_name); + +int expand_ref(const char *str, int len, unsigned char *sha1, char **ref); +int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref); +int dwim_log(const char *str, int len, unsigned char *sha1, char **ref); + /* * A ref_transaction represents a collection of ref updates * that should succeed or fail together. @@ -17,6 +157,11 @@ * If this succeeds, the ref updates will have taken place and * the transaction cannot be rolled back. * + * - Instead of `ref_transaction_commit`, use + * `initial_ref_transaction_commit()` if the ref database is known + * to be empty (e.g. during clone). This is likely to be much + * faster. + * * - At any time call `ref_transaction_free()` to discard the * transaction and free associated resources. In particular, * this rolls back the transaction if it has not been @@ -32,11 +177,20 @@ * * The message is appended to err without first clearing err. * err will not be '\n' terminated. + * + * Caveats + * ------- + * + * Note that no locks are taken, and no refs are read, until + * `ref_transaction_commit` is called. So `ref_transaction_verify` + * won't report a verification failure until the commit is attempted. */ struct ref_transaction; /* - * Bit values set in the flags argument passed to each_ref_fn(): + * Bit values set in the flags argument passed to each_ref_fn() and + * stored in ref_iterator::flags. Other bits are for internal use + * only: */ /* Reference is a symbolic reference. */ @@ -67,7 +221,7 @@ struct ref_transaction; * single callback invocation. */ typedef int each_ref_fn(const char *refname, - const unsigned char *sha1, int flags, void *cb_data); + const struct object_id *oid, int flags, void *cb_data); /* * The following functions invoke the specified callback function for @@ -76,69 +230,59 @@ typedef int each_ref_fn(const char *refname, * it is not safe to modify references while an iteration is in * progress, unless the same callback function invocation that * modifies the reference also returns a nonzero value to immediately - * stop the iteration. - */ -extern int head_ref(each_ref_fn, void *); -extern int for_each_ref(each_ref_fn, void *); -extern int for_each_ref_in(const char *, each_ref_fn, void *); -extern int for_each_tag_ref(each_ref_fn, void *); -extern int for_each_branch_ref(each_ref_fn, void *); -extern int for_each_remote_ref(each_ref_fn, void *); -extern int for_each_replace_ref(each_ref_fn, void *); -extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *); -extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *); - -extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); -extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); -extern int for_each_ref_in_submodule(const char *submodule, const char *prefix, + * stop the iteration. Returned references are sorted. + */ +int refs_for_each_ref(struct ref_store *refs, + each_ref_fn fn, void *cb_data); +int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, + each_ref_fn fn, void *cb_data); +int refs_for_each_tag_ref(struct ref_store *refs, + each_ref_fn fn, void *cb_data); +int refs_for_each_branch_ref(struct ref_store *refs, + each_ref_fn fn, void *cb_data); +int refs_for_each_remote_ref(struct ref_store *refs, + each_ref_fn fn, void *cb_data); + +int head_ref(each_ref_fn fn, void *cb_data); +int for_each_ref(each_ref_fn fn, void *cb_data); +int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data); +int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, + unsigned int broken); +int for_each_tag_ref(each_ref_fn fn, void *cb_data); +int for_each_branch_ref(each_ref_fn fn, void *cb_data); +int for_each_remote_ref(each_ref_fn fn, void *cb_data); +int for_each_replace_ref(each_ref_fn fn, void *cb_data); +int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data); +int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, + const char *prefix, void *cb_data); + +int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); +int for_each_ref_submodule(const char *submodule, + each_ref_fn fn, void *cb_data); +int for_each_ref_in_submodule(const char *submodule, const char *prefix, each_ref_fn fn, void *cb_data); -extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); -extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); -extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); +int for_each_tag_ref_submodule(const char *submodule, + each_ref_fn fn, void *cb_data); +int for_each_branch_ref_submodule(const char *submodule, + each_ref_fn fn, void *cb_data); +int for_each_remote_ref_submodule(const char *submodule, + each_ref_fn fn, void *cb_data); + +int head_ref_namespaced(each_ref_fn fn, void *cb_data); +int for_each_namespaced_ref(each_ref_fn fn, void *cb_data); -extern int head_ref_namespaced(each_ref_fn fn, void *cb_data); -extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data); +/* can be used to learn about broken ref and symref */ +int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data); +int for_each_rawref(each_ref_fn fn, void *cb_data); static inline const char *has_glob_specials(const char *pattern) { return strpbrk(pattern, "?*["); } -/* can be used to learn about broken ref and symref */ -extern int for_each_rawref(each_ref_fn, void *); - -extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname); -extern void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames); - -/* - * Lock the packed-refs file for writing. Flags is passed to - * hold_lock_file_for_update(). Return 0 on success. - * Errno is set to something meaningful on error. - */ -extern int lock_packed_refs(int flags); - -/* - * Add a reference to the in-memory packed reference cache. This may - * only be called while the packed-refs file is locked (see - * lock_packed_refs()). To actually write the packed-refs file, call - * commit_packed_refs(). - */ -extern void add_packed_ref(const char *refname, const unsigned char *sha1); - -/* - * Write the current version of the packed refs cache from memory to - * disk. The packed-refs file must already be locked for writing (see - * lock_packed_refs()). Return zero on success. - * Sets errno to something meaningful on error. - */ -extern int commit_packed_refs(void); - -/* - * Rollback the lockfile for the packed-refs file, and discard the - * in-memory packed reference cache. (The packed-refs file will be - * read anew if it is needed again after this function is called.) - */ -extern void rollback_packed_refs(void); +void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname); +void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, + const struct string_list *refnames); /* * Flags for controlling behaviour of pack_refs() @@ -152,32 +296,7 @@ extern void rollback_packed_refs(void); * Write a packed-refs file for the current repository. * flags: Combination of the above PACK_REFS_* flags. */ -int pack_refs(unsigned int flags); - -/* - * Rewrite the packed-refs file, omitting any refs listed in - * 'refnames'. On error, packed-refs will be unchanged, the return - * value is nonzero, and a message about the error is written to the - * 'err' strbuf. - * - * The refs in 'refnames' needn't be sorted. `err` must not be NULL. - */ -extern int repack_without_refs(struct string_list *refnames, - struct strbuf *err); - -extern int ref_exists(const char *); - -extern int is_branch(const char *refname); - -/* - * If refname is a non-symbolic reference that refers to a tag object, - * and the tag can be (recursively) dereferenced to a non-tag object, - * store the SHA1 of the referred-to object to sha1 and return 0. If - * any of these conditions are not met, return a non-zero value. - * Symbolic references are considered unpeelable, even if they - * ultimately resolve to a peelable tag. - */ -extern int peel_ref(const char *refname, unsigned char *sha1); +int refs_pack_refs(struct ref_store *refs, unsigned int flags); /* * Flags controlling ref_transaction_update(), ref_transaction_create(), etc. @@ -187,34 +306,74 @@ extern int peel_ref(const char *refname, unsigned char *sha1); * Other flags are reserved for internal use. */ #define REF_NODEREF 0x01 +#define REF_FORCE_CREATE_REFLOG 0x40 /* - * Setup reflog before using. Set errno to something meaningful on failure. + * Setup reflog before using. Fill in err and return -1 on failure. */ -int log_ref_setup(const char *refname, char *logfile, int bufsize); +int refs_create_reflog(struct ref_store *refs, const char *refname, + int force_create, struct strbuf *err); +int safe_create_reflog(const char *refname, int force_create, struct strbuf *err); /** Reads log for the value of ref during at_time. **/ -extern int read_ref_at(const char *refname, unsigned int flags, - unsigned long at_time, int cnt, - unsigned char *sha1, char **msg, - unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt); +int read_ref_at(const char *refname, unsigned int flags, + unsigned long at_time, int cnt, + unsigned char *sha1, char **msg, + unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt); /** Check if a particular reflog exists */ -extern int reflog_exists(const char *refname); +int refs_reflog_exists(struct ref_store *refs, const char *refname); +int reflog_exists(const char *refname); + +/* + * Delete the specified reference. If old_sha1 is non-NULL, then + * verify that the current value of the reference is old_sha1 before + * deleting it. If old_sha1 is NULL, delete the reference if it + * exists, regardless of its old value. It is an error for old_sha1 to + * be NULL_SHA1. flags is passed through to ref_transaction_delete(). + */ +int refs_delete_ref(struct ref_store *refs, const char *msg, + const char *refname, + const unsigned char *old_sha1, + unsigned int flags); +int delete_ref(const char *msg, const char *refname, + const unsigned char *old_sha1, unsigned int flags); + +/* + * Delete the specified references. If there are any problems, emit + * errors but attempt to keep going (i.e., the deletes are not done in + * an all-or-nothing transaction). flags is passed through to + * ref_transaction_delete(). + */ +int refs_delete_refs(struct ref_store *refs, struct string_list *refnames, + unsigned int flags); +int delete_refs(struct string_list *refnames, unsigned int flags); /** Delete a reflog */ -extern int delete_reflog(const char *refname); +int refs_delete_reflog(struct ref_store *refs, const char *refname); +int delete_reflog(const char *refname); /* iterate over reflog entries */ -typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *); +typedef int each_reflog_ent_fn( + struct object_id *old_oid, struct object_id *new_oid, + const char *committer, unsigned long timestamp, + int tz, const char *msg, void *cb_data); + +int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, + each_reflog_ent_fn fn, void *cb_data); +int refs_for_each_reflog_ent_reverse(struct ref_store *refs, + const char *refname, + each_reflog_ent_fn fn, + void *cb_data); int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data); int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data); /* * Calls the specified function for each reflog file until it returns nonzero, - * and returns the value + * and returns the value. Reflog file order is unspecified. */ -extern int for_each_reflog(each_ref_fn, void *); +int refs_for_each_reflog(struct ref_store *refs, each_ref_fn fn, void *cb_data); +int for_each_reflog(each_ref_fn fn, void *cb_data); #define REFNAME_ALLOW_ONELEVEL 1 #define REFNAME_REFSPEC_PATTERN 2 @@ -224,23 +383,33 @@ extern int for_each_reflog(each_ref_fn, void *); * to the rules described in Documentation/git-check-ref-format.txt. * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then - * allow a "*" wildcard character in place of one of the name - * components. No leading or repeated slashes are accepted. + * allow a single "*" wildcard character in the refspec. No leading or + * repeated slashes are accepted. */ -extern int check_refname_format(const char *refname, int flags); +int check_refname_format(const char *refname, int flags); + +const char *prettify_refname(const char *refname); -extern const char *prettify_refname(const char *refname); -extern char *shorten_unambiguous_ref(const char *refname, int strict); +char *shorten_unambiguous_ref(const char *refname, int strict); /** rename ref, return 0 on success **/ -extern int rename_ref(const char *oldref, const char *newref, const char *logmsg); +int refs_rename_ref(struct ref_store *refs, const char *oldref, + const char *newref, const char *logmsg); +int rename_ref(const char *oldref, const char *newref, const char *logmsg); -/** - * Resolve refname in the nested "gitlink" repository that is located - * at path. If the resolution is successful, return 0 and set sha1 to - * the name of the object; otherwise, return a non-zero value. - */ -extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1); +int refs_create_symref(struct ref_store *refs, const char *refname, + const char *target, const char *logmsg); +int create_symref(const char *refname, const char *target, const char *logmsg); + +/* + * Update HEAD of the specified gitdir. + * Similar to create_symref("relative-git-dir/HEAD", target, NULL), but + * this can update the main working tree's HEAD regardless of where + * $GIT_DIR points to. + * Return 0 if successful, non-zero otherwise. + * */ +int set_worktree_head_symref(const char *gitdir, const char *target, + const char *logmsg); enum action_on_err { UPDATE_REFS_MSG_ON_ERR, @@ -252,6 +421,8 @@ enum action_on_err { * Begin a reference transaction. The reference transaction must * be freed by calling ref_transaction_free(). */ +struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, + struct strbuf *err); struct ref_transaction *ref_transaction_begin(struct strbuf *err); /* @@ -272,7 +443,7 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); * msg -- a message describing the change (for the reflog). * * err -- a strbuf for receiving a description of any error that - * might have occured. + * might have occurred. * * The functions make internal copies of refname and msg, so the * caller retains ownership of these parameters. @@ -360,6 +531,20 @@ int ref_transaction_commit(struct ref_transaction *transaction, struct strbuf *err); /* + * Like ref_transaction_commit(), but optimized for creating + * references when originally initializing a repository (e.g., by "git + * clone"). It writes the new references directly to packed-refs + * without locking the individual references. + * + * It is a bug to call this function when there might be other + * processes accessing the repository or if there are existing + * references that might conflict with the ones being created. All + * old_sha1 values must either be absent or NULL_SHA1. + */ +int initial_ref_transaction_commit(struct ref_transaction *transaction, + struct strbuf *err); + +/* * Free an existing transaction and all associated data. */ void ref_transaction_free(struct ref_transaction *transaction); @@ -372,12 +557,35 @@ void ref_transaction_free(struct ref_transaction *transaction); * ref_transaction_update(). Handle errors as requested by the `onerr` * argument. */ +int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname, + const unsigned char *new_sha1, const unsigned char *old_sha1, + unsigned int flags, enum action_on_err onerr); int update_ref(const char *msg, const char *refname, const unsigned char *new_sha1, const unsigned char *old_sha1, unsigned int flags, enum action_on_err onerr); +int update_ref_oid(const char *msg, const char *refname, + const struct object_id *new_oid, const struct object_id *old_oid, + unsigned int flags, enum action_on_err onerr); + +int parse_hide_refs_config(const char *var, const char *value, const char *); + +/* + * Check whether a ref is hidden. If no namespace is set, both the first and + * the second parameter point to the full ref name. If a namespace is set and + * the ref is inside that namespace, the first parameter is a pointer to the + * name of the ref with the namespace prefix removed. If a namespace is set and + * the ref is outside that namespace, the first parameter is NULL. The second + * parameter always points to the full ref name. + */ +int ref_is_hidden(const char *, const char *); + +enum ref_type { + REF_TYPE_PER_WORKTREE, + REF_TYPE_PSEUDOREF, + REF_TYPE_NORMAL, +}; -extern int parse_hide_refs_config(const char *var, const char *value, const char *); -extern int ref_is_hidden(const char *); +enum ref_type ref_type(const char *refname); enum expire_reflog_flags { EXPIRE_REFLOGS_DRY_RUN = 1 << 0, @@ -418,11 +626,34 @@ typedef void reflog_expiry_cleanup_fn(void *cb_data); * enum expire_reflog_flags. The three function pointers are described * above. On success, return zero. */ -extern int reflog_expire(const char *refname, const unsigned char *sha1, - unsigned int flags, - reflog_expiry_prepare_fn prepare_fn, - reflog_expiry_should_prune_fn should_prune_fn, - reflog_expiry_cleanup_fn cleanup_fn, - void *policy_cb_data); +int refs_reflog_expire(struct ref_store *refs, + const char *refname, + const unsigned char *sha1, + unsigned int flags, + reflog_expiry_prepare_fn prepare_fn, + reflog_expiry_should_prune_fn should_prune_fn, + reflog_expiry_cleanup_fn cleanup_fn, + void *policy_cb_data); +int reflog_expire(const char *refname, const unsigned char *sha1, + unsigned int flags, + reflog_expiry_prepare_fn prepare_fn, + reflog_expiry_should_prune_fn should_prune_fn, + reflog_expiry_cleanup_fn cleanup_fn, + void *policy_cb_data); + +int ref_storage_backend_exists(const char *name); + +struct ref_store *get_main_ref_store(void); +/* + * Return the ref_store instance for the specified submodule. For the + * main repository, use submodule==NULL; such a call cannot fail. For + * a submodule, the submodule must exist and be a nonbare repository, + * otherwise return NULL. If the requested reference store has not yet + * been initialized, initialize it first. + * + * For backwards compatibility, submodule=="" is treated the same as + * submodule==NULL. + */ +struct ref_store *get_submodule_ref_store(const char *submodule); #endif /* REFS_H */ |