Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 1 | #ifndef REFS_H |
| 2 | #define REFS_H |
| 3 | |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 4 | struct ref_lock { |
Linus Torvalds | 434cd0c | 2006-09-14 10:14:47 -0700 | [diff] [blame] | 5 | char *ref_name; |
Nicolas Pitre | 1655707 | 2007-01-26 17:26:06 -0500 | [diff] [blame] | 6 | char *orig_ref_name; |
Junio C Hamano | c33d517 | 2006-06-06 13:54:14 -0700 | [diff] [blame] | 7 | struct lock_file *lk; |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 8 | unsigned char old_sha1[20]; |
| 9 | int lock_fd; |
Shawn Pearce | 732232a | 2006-05-19 03:29:05 -0400 | [diff] [blame] | 10 | int force_write; |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 11 | }; |
| 12 | |
Junio C Hamano | 98ac34b | 2011-10-19 13:45:50 -0700 | [diff] [blame] | 13 | #define REF_ISSYMREF 0x01 |
| 14 | #define REF_ISPACKED 0x02 |
| 15 | #define REF_ISBROKEN 0x04 |
Junio C Hamano | f4204ab | 2006-11-21 23:36:35 -0800 | [diff] [blame] | 16 | |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 17 | /* |
Michael Haggerty | 432ad41 | 2012-04-10 07:30:26 +0200 | [diff] [blame] | 18 | * Calls the specified function for each ref file until it returns |
| 19 | * nonzero, and returns the value. Please note that it is not safe to |
| 20 | * modify references while an iteration is in progress, unless the |
| 21 | * same callback function invocation that modifies the reference also |
| 22 | * returns a nonzero value to immediately stop the iteration. |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 23 | */ |
Junio C Hamano | 8da1977 | 2006-09-20 22:02:01 -0700 | [diff] [blame] | 24 | typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data); |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 25 | extern int head_ref(each_ref_fn, void *); |
| 26 | extern int for_each_ref(each_ref_fn, void *); |
Christian Couder | 2a8177b | 2009-03-30 05:07:15 +0200 | [diff] [blame] | 27 | extern int for_each_ref_in(const char *, each_ref_fn, void *); |
Junio C Hamano | cb5d709 | 2006-09-20 21:47:42 -0700 | [diff] [blame] | 28 | extern int for_each_tag_ref(each_ref_fn, void *); |
| 29 | extern int for_each_branch_ref(each_ref_fn, void *); |
| 30 | extern int for_each_remote_ref(each_ref_fn, void *); |
Christian Couder | 2926870 | 2009-01-23 10:06:38 +0100 | [diff] [blame] | 31 | extern int for_each_replace_ref(each_ref_fn, void *); |
Ilari Liusvaara | d08bae7 | 2010-01-20 11:48:25 +0200 | [diff] [blame] | 32 | extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *); |
Ilari Liusvaara | b09fe97 | 2010-01-20 11:48:26 +0200 | [diff] [blame] | 33 | extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *); |
Linus Torvalds | 8a65ff7 | 2005-07-02 20:23:36 -0700 | [diff] [blame] | 34 | |
Heiko Voigt | 9ef6aeb | 2010-07-07 15:39:12 +0200 | [diff] [blame] | 35 | extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); |
| 36 | extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); |
| 37 | extern int for_each_ref_in_submodule(const char *submodule, const char *prefix, |
| 38 | each_ref_fn fn, void *cb_data); |
| 39 | extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); |
| 40 | extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); |
| 41 | extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data); |
| 42 | |
Josh Triplett | a1bea2c | 2011-07-05 10:54:44 -0700 | [diff] [blame] | 43 | extern int head_ref_namespaced(each_ref_fn fn, void *cb_data); |
| 44 | extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data); |
| 45 | |
Thomas Rast | 894a9d3 | 2010-03-12 18:04:26 +0100 | [diff] [blame] | 46 | static inline const char *has_glob_specials(const char *pattern) |
| 47 | { |
| 48 | return strpbrk(pattern, "?*["); |
| 49 | } |
| 50 | |
Junio C Hamano | f8948e2 | 2009-02-08 23:27:10 -0800 | [diff] [blame] | 51 | /* can be used to learn about broken ref and symref */ |
| 52 | extern int for_each_rawref(each_ref_fn, void *); |
| 53 | |
Jay Soffian | 3cf6134 | 2009-11-10 00:03:32 -0500 | [diff] [blame] | 54 | extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname); |
Junio C Hamano | f8948e2 | 2009-02-08 23:27:10 -0800 | [diff] [blame] | 55 | |
Daniel Barkalow | e142a3c | 2008-04-27 13:39:24 -0400 | [diff] [blame] | 56 | /* |
Michael Haggerty | 30249ee | 2012-01-17 06:50:33 +0100 | [diff] [blame] | 57 | * Add a reference to the in-memory packed reference cache. To actually |
| 58 | * write the reference to the packed-refs file, call pack_refs(). |
| 59 | */ |
| 60 | extern void add_packed_ref(const char *refname, const unsigned char *sha1); |
| 61 | |
Junio C Hamano | 2c5c66b | 2011-10-10 15:56:19 -0700 | [diff] [blame] | 62 | extern int ref_exists(const char *); |
Daniel Barkalow | e142a3c | 2008-04-27 13:39:24 -0400 | [diff] [blame] | 63 | |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 64 | extern int peel_ref(const char *refname, unsigned char *sha1); |
Junio C Hamano | cf0adba | 2006-11-19 13:22:44 -0800 | [diff] [blame] | 65 | |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 66 | /** Locks a "refs/" ref returning the lock on success and NULL on failure. **/ |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 67 | extern struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1); |
Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 68 | |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 69 | /** Locks any ref (for 'HEAD' type refs). */ |
Sven Verdoolaege | 68db31c | 2007-05-09 12:33:20 +0200 | [diff] [blame] | 70 | #define REF_NODEREF 0x01 |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 71 | extern struct ref_lock *lock_any_ref_for_update(const char *refname, |
| 72 | const unsigned char *old_sha1, |
| 73 | int flags); |
Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 74 | |
Brandon Casey | 435fc85 | 2008-02-22 12:57:30 -0600 | [diff] [blame] | 75 | /** Close the file descriptor owned by a lock and return the status */ |
| 76 | extern int close_ref(struct ref_lock *lock); |
| 77 | |
| 78 | /** Close and commit the ref locked by the lock */ |
| 79 | extern int commit_ref(struct ref_lock *lock); |
| 80 | |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 81 | /** Release any lock taken but not written. **/ |
Junio C Hamano | e5f38ec | 2006-06-06 14:04:17 -0700 | [diff] [blame] | 82 | extern void unlock_ref(struct ref_lock *lock); |
Shawn Pearce | 4bd18c4 | 2006-05-17 05:55:02 -0400 | [diff] [blame] | 83 | |
| 84 | /** Writes sha1 into the ref specified by the lock. **/ |
| 85 | extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg); |
Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 86 | |
Michael Haggerty | 8be8bde | 2011-10-17 04:38:07 +0200 | [diff] [blame] | 87 | /* |
| 88 | * Invalidate the reference cache for the specified submodule. Use |
| 89 | * submodule=NULL to invalidate the cache for the main module. This |
| 90 | * function must be called if references are changed via a mechanism |
| 91 | * other than the refs API. |
| 92 | */ |
| 93 | extern void invalidate_ref_cache(const char *submodule); |
| 94 | |
Erick Mattos | 859c301 | 2010-05-21 21:28:36 -0300 | [diff] [blame] | 95 | /** Setup reflog before using. **/ |
Thomas Rast | 157aaea | 2010-06-10 14:54:03 +0200 | [diff] [blame] | 96 | int log_ref_setup(const char *ref_name, char *logfile, int bufsize); |
Erick Mattos | 859c301 | 2010-05-21 21:28:36 -0300 | [diff] [blame] | 97 | |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 98 | /** Reads log for the value of ref during at_time. **/ |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 99 | extern int read_ref_at(const char *refname, unsigned long at_time, int cnt, |
| 100 | unsigned char *sha1, char **msg, |
| 101 | unsigned long *cutoff_time, int *cutoff_tz, int *cutoff_cnt); |
Shawn Pearce | d556fae | 2006-05-17 05:56:09 -0400 | [diff] [blame] | 102 | |
Junio C Hamano | 2ff8166 | 2006-12-18 01:18:16 -0800 | [diff] [blame] | 103 | /* iterate over reflog entries */ |
Johannes Schindelin | 883d60f | 2007-01-08 01:59:54 +0100 | [diff] [blame] | 104 | typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *); |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 105 | int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data); |
| 106 | int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long, void *cb_data); |
Junio C Hamano | 2ff8166 | 2006-12-18 01:18:16 -0800 | [diff] [blame] | 107 | |
Nicolas Pitre | eb8381c | 2007-02-03 13:25:43 -0500 | [diff] [blame] | 108 | /* |
| 109 | * Calls the specified function for each reflog file until it returns nonzero, |
| 110 | * and returns the value |
| 111 | */ |
| 112 | extern int for_each_reflog(each_ref_fn, void *); |
| 113 | |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 114 | #define REFNAME_ALLOW_ONELEVEL 1 |
| 115 | #define REFNAME_REFSPEC_PATTERN 2 |
Michael Haggerty | dce4bab | 2011-09-15 23:10:43 +0200 | [diff] [blame] | 116 | #define REFNAME_DOT_COMPONENT 4 |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 117 | |
| 118 | /* |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 119 | * Return 0 iff refname has the correct format for a refname according |
| 120 | * to the rules described in Documentation/git-check-ref-format.txt. |
| 121 | * If REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 122 | * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then |
| 123 | * allow a "*" wildcard character in place of one of the name |
Michael Haggerty | dce4bab | 2011-09-15 23:10:43 +0200 | [diff] [blame] | 124 | * components. No leading or repeated slashes are accepted. If |
| 125 | * REFNAME_DOT_COMPONENT is set in flags, then allow refname |
| 126 | * components to start with "." (but not a whole component equal to |
| 127 | * "." or ".."). |
Michael Haggerty | 8d9c501 | 2011-09-15 23:10:25 +0200 | [diff] [blame] | 128 | */ |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 129 | extern int check_refname_format(const char *refname, int flags); |
Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 130 | |
Felipe Contreras | 4577e48 | 2009-05-14 00:22:04 +0300 | [diff] [blame] | 131 | extern const char *prettify_refname(const char *refname); |
Michael Haggerty | dfefa93 | 2011-12-12 06:38:09 +0100 | [diff] [blame] | 132 | extern char *shorten_unambiguous_ref(const char *refname, int strict); |
Daniel Barkalow | a9c37a7 | 2009-03-08 21:06:05 -0400 | [diff] [blame] | 133 | |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 134 | /** rename ref, return 0 on success **/ |
Lars Hjemli | 678d0f4 | 2006-11-30 03:16:56 +0100 | [diff] [blame] | 135 | extern int rename_ref(const char *oldref, const char *newref, const char *logmsg); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 136 | |
Michael Haggerty | 7f820bd | 2011-12-12 06:38:18 +0100 | [diff] [blame] | 137 | /** |
| 138 | * Resolve refname in the nested "gitlink" repository that is located |
| 139 | * at path. If the resolution is successful, return 0 and set sha1 to |
| 140 | * the name of the object; otherwise, return a non-zero value. |
| 141 | */ |
| 142 | extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1); |
Linus Torvalds | 0ebde32 | 2007-04-09 21:14:26 -0700 | [diff] [blame] | 143 | |
Carlos Rica | 3d9f037 | 2007-09-05 03:38:24 +0200 | [diff] [blame] | 144 | /** lock a ref and then write its file */ |
| 145 | enum action_on_err { MSG_ON_ERR, DIE_ON_ERR, QUIET_ON_ERR }; |
| 146 | int update_ref(const char *action, const char *refname, |
| 147 | const unsigned char *sha1, const unsigned char *oldval, |
| 148 | int flags, enum action_on_err onerr); |
| 149 | |
Daniel Barkalow | 95fc751 | 2005-06-06 16:31:29 -0400 | [diff] [blame] | 150 | #endif /* REFS_H */ |