blob: d6c2fe2dfbd9e16a871ac960c27d9a68cc45102f [file] [log] [blame]
Daniel Barkalow95fc7512005-06-06 16:31:29 -04001#ifndef REFS_H
2#define REFS_H
3
Shawn Pearce4bd18c42006-05-17 05:55:02 -04004struct ref_lock {
Linus Torvalds434cd0c2006-09-14 10:14:47 -07005 char *ref_name;
Nicolas Pitre16557072007-01-26 17:26:06 -05006 char *orig_ref_name;
Junio C Hamanoc33d5172006-06-06 13:54:14 -07007 struct lock_file *lk;
Shawn Pearce4bd18c42006-05-17 05:55:02 -04008 unsigned char old_sha1[20];
9 int lock_fd;
Shawn Pearce732232a2006-05-19 03:29:05 -040010 int force_write;
Shawn Pearce4bd18c42006-05-17 05:55:02 -040011};
12
Junio C Hamano98ac34b2011-10-19 13:45:50 -070013#define REF_ISSYMREF 0x01
14#define REF_ISPACKED 0x02
15#define REF_ISBROKEN 0x04
Junio C Hamanof4204ab2006-11-21 23:36:35 -080016
Linus Torvalds8a65ff72005-07-02 20:23:36 -070017/*
Michael Haggerty432ad412012-04-10 07:30:26 +020018 * 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 Torvalds8a65ff72005-07-02 20:23:36 -070023 */
Junio C Hamano8da19772006-09-20 22:02:01 -070024typedef int each_ref_fn(const char *refname, const unsigned char *sha1, int flags, void *cb_data);
Junio C Hamanocb5d7092006-09-20 21:47:42 -070025extern int head_ref(each_ref_fn, void *);
26extern int for_each_ref(each_ref_fn, void *);
Christian Couder2a8177b2009-03-30 05:07:15 +020027extern int for_each_ref_in(const char *, each_ref_fn, void *);
Junio C Hamanocb5d7092006-09-20 21:47:42 -070028extern int for_each_tag_ref(each_ref_fn, void *);
29extern int for_each_branch_ref(each_ref_fn, void *);
30extern int for_each_remote_ref(each_ref_fn, void *);
Christian Couder29268702009-01-23 10:06:38 +010031extern int for_each_replace_ref(each_ref_fn, void *);
Ilari Liusvaarad08bae72010-01-20 11:48:25 +020032extern int for_each_glob_ref(each_ref_fn, const char *pattern, void *);
Ilari Liusvaarab09fe972010-01-20 11:48:26 +020033extern int for_each_glob_ref_in(each_ref_fn, const char *pattern, const char* prefix, void *);
Linus Torvalds8a65ff72005-07-02 20:23:36 -070034
Heiko Voigt9ef6aeb2010-07-07 15:39:12 +020035extern int head_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
36extern int for_each_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
37extern int for_each_ref_in_submodule(const char *submodule, const char *prefix,
38 each_ref_fn fn, void *cb_data);
39extern int for_each_tag_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
40extern int for_each_branch_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
41extern int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data);
42
Josh Tripletta1bea2c2011-07-05 10:54:44 -070043extern int head_ref_namespaced(each_ref_fn fn, void *cb_data);
44extern int for_each_namespaced_ref(each_ref_fn fn, void *cb_data);
45
Thomas Rast894a9d32010-03-12 18:04:26 +010046static inline const char *has_glob_specials(const char *pattern)
47{
48 return strpbrk(pattern, "?*[");
49}
50
Junio C Hamanof8948e22009-02-08 23:27:10 -080051/* can be used to learn about broken ref and symref */
52extern int for_each_rawref(each_ref_fn, void *);
53
Jay Soffian3cf61342009-11-10 00:03:32 -050054extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname);
Junio C Hamanof8948e22009-02-08 23:27:10 -080055
Daniel Barkalowe142a3c2008-04-27 13:39:24 -040056/*
Michael Haggerty30249ee2012-01-17 06:50:33 +010057 * 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 */
60extern void add_packed_ref(const char *refname, const unsigned char *sha1);
61
Junio C Hamano2c5c66b2011-10-10 15:56:19 -070062extern int ref_exists(const char *);
Daniel Barkalowe142a3c2008-04-27 13:39:24 -040063
Michael Haggertydfefa932011-12-12 06:38:09 +010064extern int peel_ref(const char *refname, unsigned char *sha1);
Junio C Hamanocf0adba2006-11-19 13:22:44 -080065
Shawn Pearce4bd18c42006-05-17 05:55:02 -040066/** Locks a "refs/" ref returning the lock on success and NULL on failure. **/
Michael Haggertydfefa932011-12-12 06:38:09 +010067extern struct ref_lock *lock_ref_sha1(const char *refname, const unsigned char *old_sha1);
Daniel Barkalow95fc7512005-06-06 16:31:29 -040068
Shawn Pearce4bd18c42006-05-17 05:55:02 -040069/** Locks any ref (for 'HEAD' type refs). */
Sven Verdoolaege68db31c2007-05-09 12:33:20 +020070#define REF_NODEREF 0x01
Michael Haggertydfefa932011-12-12 06:38:09 +010071extern struct ref_lock *lock_any_ref_for_update(const char *refname,
72 const unsigned char *old_sha1,
73 int flags);
Daniel Barkalow95fc7512005-06-06 16:31:29 -040074
Brandon Casey435fc852008-02-22 12:57:30 -060075/** Close the file descriptor owned by a lock and return the status */
76extern int close_ref(struct ref_lock *lock);
77
78/** Close and commit the ref locked by the lock */
79extern int commit_ref(struct ref_lock *lock);
80
Shawn Pearce4bd18c42006-05-17 05:55:02 -040081/** Release any lock taken but not written. **/
Junio C Hamanoe5f38ec2006-06-06 14:04:17 -070082extern void unlock_ref(struct ref_lock *lock);
Shawn Pearce4bd18c42006-05-17 05:55:02 -040083
84/** Writes sha1 into the ref specified by the lock. **/
85extern int write_ref_sha1(struct ref_lock *lock, const unsigned char *sha1, const char *msg);
Daniel Barkalow95fc7512005-06-06 16:31:29 -040086
Michael Haggerty8be8bde2011-10-17 04:38:07 +020087/*
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 */
93extern void invalidate_ref_cache(const char *submodule);
94
Erick Mattos859c3012010-05-21 21:28:36 -030095/** Setup reflog before using. **/
Thomas Rast157aaea2010-06-10 14:54:03 +020096int log_ref_setup(const char *ref_name, char *logfile, int bufsize);
Erick Mattos859c3012010-05-21 21:28:36 -030097
Shawn Pearced556fae2006-05-17 05:56:09 -040098/** Reads log for the value of ref during at_time. **/
Michael Haggertydfefa932011-12-12 06:38:09 +010099extern 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 Pearced556fae2006-05-17 05:56:09 -0400102
Junio C Hamano2ff81662006-12-18 01:18:16 -0800103/* iterate over reflog entries */
Johannes Schindelin883d60f2007-01-08 01:59:54 +0100104typedef int each_reflog_ent_fn(unsigned char *osha1, unsigned char *nsha1, const char *, unsigned long, int, const char *, void *);
Michael Haggertydfefa932011-12-12 06:38:09 +0100105int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data);
106int for_each_recent_reflog_ent(const char *refname, each_reflog_ent_fn fn, long, void *cb_data);
Junio C Hamano2ff81662006-12-18 01:18:16 -0800107
Nicolas Pitreeb8381c2007-02-03 13:25:43 -0500108/*
109 * Calls the specified function for each reflog file until it returns nonzero,
110 * and returns the value
111 */
112extern int for_each_reflog(each_ref_fn, void *);
113
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200114#define REFNAME_ALLOW_ONELEVEL 1
115#define REFNAME_REFSPEC_PATTERN 2
Michael Haggertydce4bab2011-09-15 23:10:43 +0200116#define REFNAME_DOT_COMPONENT 4
Michael Haggerty8d9c5012011-09-15 23:10:25 +0200117
118/*
Michael Haggertydfefa932011-12-12 06:38:09 +0100119 * 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 Haggerty8d9c5012011-09-15 23:10:25 +0200122 * reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
123 * allow a "*" wildcard character in place of one of the name
Michael Haggertydce4bab2011-09-15 23:10:43 +0200124 * 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 Haggerty8d9c5012011-09-15 23:10:25 +0200128 */
Michael Haggertydfefa932011-12-12 06:38:09 +0100129extern int check_refname_format(const char *refname, int flags);
Daniel Barkalow95fc7512005-06-06 16:31:29 -0400130
Felipe Contreras4577e482009-05-14 00:22:04 +0300131extern const char *prettify_refname(const char *refname);
Michael Haggertydfefa932011-12-12 06:38:09 +0100132extern char *shorten_unambiguous_ref(const char *refname, int strict);
Daniel Barkalowa9c37a72009-03-08 21:06:05 -0400133
Lars Hjemlic976d412006-11-28 15:47:40 +0100134/** rename ref, return 0 on success **/
Lars Hjemli678d0f42006-11-30 03:16:56 +0100135extern int rename_ref(const char *oldref, const char *newref, const char *logmsg);
Lars Hjemlic976d412006-11-28 15:47:40 +0100136
Michael Haggerty7f820bd2011-12-12 06:38:18 +0100137/**
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 */
142extern int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1);
Linus Torvalds0ebde322007-04-09 21:14:26 -0700143
Carlos Rica3d9f0372007-09-05 03:38:24 +0200144/** lock a ref and then write its file */
145enum action_on_err { MSG_ON_ERR, DIE_ON_ERR, QUIET_ON_ERR };
146int 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 Barkalow95fc7512005-06-06 16:31:29 -0400150#endif /* REFS_H */