1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_refs_h__
8 #define INCLUDE_refs_h__
9 
10 #include "common.h"
11 
12 #include "git2/oid.h"
13 #include "git2/refs.h"
14 #include "git2/refdb.h"
15 #include "strmap.h"
16 #include "buffer.h"
17 #include "oid.h"
18 
19 extern bool git_reference__enable_symbolic_ref_target_validation;
20 
21 #define GIT_REFS_DIR "refs/"
22 #define GIT_REFS_HEADS_DIR GIT_REFS_DIR "heads/"
23 #define GIT_REFS_TAGS_DIR GIT_REFS_DIR "tags/"
24 #define GIT_REFS_REMOTES_DIR GIT_REFS_DIR "remotes/"
25 #define GIT_REFS_NOTES_DIR GIT_REFS_DIR "notes/"
26 #define GIT_REFS_DIR_MODE 0777
27 #define GIT_REFS_FILE_MODE 0666
28 
29 #define GIT_RENAMED_REF_FILE GIT_REFS_DIR "RENAMED-REF"
30 
31 #define GIT_SYMREF "ref: "
32 #define GIT_PACKEDREFS_FILE "packed-refs"
33 #define GIT_PACKEDREFS_HEADER "# pack-refs with: peeled fully-peeled sorted "
34 #define GIT_PACKEDREFS_FILE_MODE 0666
35 
36 #define GIT_HEAD_FILE "HEAD"
37 #define GIT_ORIG_HEAD_FILE "ORIG_HEAD"
38 #define GIT_FETCH_HEAD_FILE "FETCH_HEAD"
39 #define GIT_MERGE_HEAD_FILE "MERGE_HEAD"
40 #define GIT_REVERT_HEAD_FILE "REVERT_HEAD"
41 #define GIT_CHERRYPICK_HEAD_FILE "CHERRY_PICK_HEAD"
42 #define GIT_BISECT_LOG_FILE "BISECT_LOG"
43 #define GIT_REBASE_MERGE_DIR "rebase-merge/"
44 #define GIT_REBASE_MERGE_INTERACTIVE_FILE GIT_REBASE_MERGE_DIR "interactive"
45 #define GIT_REBASE_APPLY_DIR "rebase-apply/"
46 #define GIT_REBASE_APPLY_REBASING_FILE GIT_REBASE_APPLY_DIR "rebasing"
47 #define GIT_REBASE_APPLY_APPLYING_FILE GIT_REBASE_APPLY_DIR "applying"
48 #define GIT_REFS_HEADS_MASTER_FILE GIT_REFS_HEADS_DIR "master"
49 
50 #define GIT_SEQUENCER_DIR "sequencer/"
51 #define GIT_SEQUENCER_HEAD_FILE GIT_SEQUENCER_DIR "head"
52 #define GIT_SEQUENCER_OPTIONS_FILE GIT_SEQUENCER_DIR "options"
53 #define GIT_SEQUENCER_TODO_FILE GIT_SEQUENCER_DIR "todo"
54 
55 #define GIT_STASH_FILE "stash"
56 #define GIT_REFS_STASH_FILE GIT_REFS_DIR GIT_STASH_FILE
57 
58 #define GIT_REF_FORMAT__PRECOMPOSE_UNICODE	(1u << 16)
59 #define GIT_REF_FORMAT__VALIDATION_DISABLE	(1u << 15)
60 
61 #define GIT_REFNAME_MAX 1024
62 
63 typedef char git_refname_t[GIT_REFNAME_MAX];
64 
65 struct git_reference {
66 	git_refdb *db;
67 	git_ref_t type;
68 
69 	union {
70 		git_oid oid;
71 		char *symbolic;
72 	} target;
73 
74 	git_oid peel;
75 	char name[GIT_FLEX_ARRAY];
76 };
77 
78 git_reference *git_reference__set_name(git_reference *ref, const char *name);
79 
80 int git_reference__normalize_name(git_buf *buf, const char *name, unsigned int flags);
81 int git_reference__update_terminal(git_repository *repo, const char *ref_name, const git_oid *oid, const git_signature *sig, const char *log_message);
82 int git_reference__is_valid_name(const char *refname, unsigned int flags);
83 int git_reference__is_branch(const char *ref_name);
84 int git_reference__is_remote(const char *ref_name);
85 int git_reference__is_tag(const char *ref_name);
86 const char *git_reference__shorthand(const char *name);
87 
88 /**
89  * Lookup a reference by name and try to resolve to an OID.
90  *
91  * You can control how many dereferences this will attempt to resolve the
92  * reference with the `max_deref` parameter, or pass -1 to use a sane
93  * default.  If you pass 0 for `max_deref`, this will not attempt to resolve
94  * the reference.  For any value of `max_deref` other than 0, not
95  * successfully resolving the reference will be reported as an error.
96 
97  * The generated reference must be freed by the user.
98  *
99  * @param reference_out Pointer to the looked-up reference
100  * @param repo The repository to look up the reference
101  * @param name The long name for the reference (e.g. HEAD, ref/heads/master, refs/tags/v0.1.0, ...)
102  * @param max_deref Maximum number of dereferences to make of symbolic refs, 0 means simple lookup, < 0 means use default reasonable value
103  * @return 0 on success or < 0 on error; not being able to resolve the reference is an error unless 0 was passed for max_deref
104  */
105 int git_reference_lookup_resolved(
106 	git_reference **reference_out,
107 	git_repository *repo,
108 	const char *name,
109 	int max_deref);
110 
111 /**
112  * Read reference from a file.
113  *
114  * This function will read in the file at `path`. If it is a
115  * symref, it will return a new unresolved symbolic reference
116  * with the given name pointing to the reference pointed to by
117  * the file. If it is not a symbolic reference, it will return
118  * the resolved reference.
119  *
120  * Note that because the refdb is not involved for symbolic references, they
121  * won't be owned, hence you should either not make the returned reference
122  * 'externally visible', or perform the lookup before returning it to the user.
123  */
124 int git_reference__read_head(
125 	git_reference **out,
126 	git_repository *repo,
127 	const char *path);
128 
129 int git_reference__log_signature(git_signature **out, git_repository *repo);
130 
131 /** Update a reference after a commit. */
132 int git_reference__update_for_commit(
133 	git_repository *repo,
134 	git_reference *ref,
135 	const char *ref_name,
136 	const git_oid *id,
137 	const char *operation);
138 
139 #endif
140