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 module libgit2_d.branch;
8 
9 
10 private static import libgit2_d.buffer;
11 private static import libgit2_d.types;
12 
13 /**
14  * @file git2/branch.h
15  * @brief Git branch parsing routines
16  * @defgroup git_branch Git branch management
17  * @ingroup Git
18  * @{
19  */
20 extern (C):
21 nothrow @nogc:
22 public:
23 
24 /**
25  * Create a new branch pointing at a target commit
26  *
27  * A new direct reference will be created pointing to
28  * this target commit. If `force` is true and a reference
29  * already exists with the given name, it'll be replaced.
30  *
31  * The returned reference must be freed by the user.
32  *
33  * The branch name will be checked for validity.
34  * See `git_tag_create()` for rules about valid names.
35  *
36  * @param out_ Pointer where to store the underlying reference.
37  *
38  * @param branch_name Name for the branch; this name is
39  * validated for consistency. It should also not conflict with
40  * an already existing branch name.
41  *
42  * @param target Commit to which this branch should point. This object
43  * must belong to the given `repo`.
44  *
45  * @param force Overwrite existing branch.
46  *
47  * @return 0, git_error_code.GIT_EINVALIDSPEC or an error code.
48  * A proper reference is written in the refs/heads namespace
49  * pointing to the provided target commit.
50  */
51 //GIT_EXTERN
52 int git_branch_create(libgit2_d.types.git_reference** out_, libgit2_d.types.git_repository* repo, const (char)* branch_name, const (libgit2_d.types.git_commit)* target, int force);
53 
54 /**
55  * Create a new branch pointing at a target commit
56  *
57  * This behaves like `git_branch_create()` but takes an annotated
58  * commit, which lets you specify which extended sha syntax string was
59  * specified by a user, allowing for more exact reflog messages.
60  *
61  * See the documentation for `git_branch_create()`.
62  *
63  * @see git_branch_create
64  */
65 //GIT_EXTERN
66 int git_branch_create_from_annotated(libgit2_d.types.git_reference** ref_out, libgit2_d.types.git_repository* repository, const (char)* branch_name, const (libgit2_d.types.git_annotated_commit)* commit, int force);
67 
68 /**
69  * Delete an existing branch reference.
70  *
71  * Note that if the deletion succeeds, the reference object will not
72  * be valid anymore, and should be freed immediately by the user using
73  * `git_reference_free()`.
74  *
75  * @param branch A valid reference representing a branch
76  * @return 0 on success, or an error code.
77  */
78 //GIT_EXTERN
79 int git_branch_delete(libgit2_d.types.git_reference* branch);
80 
81 /**
82  * Iterator type for branches
83  */
84 struct git_branch_iterator;
85 
86 /**
87  * Create an iterator which loops over the requested branches.
88  *
89  * @param out_ the iterator
90  * @param repo Repository where to find the branches.
91  * @param list_flags Filtering flags for the branch
92  * listing. Valid values are git_branch_t.GIT_BRANCH_LOCAL, git_branch_t.GIT_BRANCH_REMOTE
93  * or git_branch_t.GIT_BRANCH_ALL.
94  *
95  * @return 0 on success  or an error code
96  */
97 //GIT_EXTERN
98 int git_branch_iterator_new(.git_branch_iterator** out_, libgit2_d.types.git_repository* repo, libgit2_d.types.git_branch_t list_flags);
99 
100 /**
101  * Retrieve the next branch from the iterator
102  *
103  * @param out_ the reference
104  * @param out_type the type of branch (local or remote-tracking)
105  * @param iter the branch iterator
106  * @return 0 on success, git_error_code.GIT_ITEROVER if there are no more branches or an error
107  * code.
108  */
109 //GIT_EXTERN
110 int git_branch_next(libgit2_d.types.git_reference** out_, libgit2_d.types.git_branch_t* out_type, .git_branch_iterator* iter);
111 
112 /**
113  * Free a branch iterator
114  *
115  * @param iter the iterator to free
116  */
117 //GIT_EXTERN
118 void git_branch_iterator_free(.git_branch_iterator* iter);
119 
120 /**
121  * Move/rename an existing local branch reference.
122  *
123  * The new branch name will be checked for validity.
124  * See `git_tag_create()` for rules about valid names.
125  *
126  * Note that if the move succeeds, the old reference object will not
127  + be valid anymore, and should be freed immediately by the user using
128  + `git_reference_free()`.
129  *
130  * @param out_ New reference object for the updated name.
131  *
132  * @param branch Current underlying reference of the branch.
133  *
134  * @param new_branch_name Target name of the branch once the move
135  * is performed; this name is validated for consistency.
136  *
137  * @param force Overwrite existing branch.
138  *
139  * @return 0 on success, git_error_code.GIT_EINVALIDSPEC or an error code.
140  */
141 //GIT_EXTERN
142 int git_branch_move(libgit2_d.types.git_reference** out_, libgit2_d.types.git_reference* branch, const (char)* new_branch_name, int force);
143 
144 /**
145  * Lookup a branch by its name in a repository.
146  *
147  * The generated reference must be freed by the user.
148  * The branch name will be checked for validity.
149  *
150  * @see git_tag_create for rules about valid names.
151  *
152  * @param out_ pointer to the looked-up branch reference
153  * @param repo the repository to look up the branch
154  * @param branch_name Name of the branch to be looked-up;
155  * this name is validated for consistency.
156  * @param branch_type Type of the considered branch. This should
157  * be valued with either git_branch_t.GIT_BRANCH_LOCAL or git_branch_t.GIT_BRANCH_REMOTE.
158  *
159  * @return 0 on success; git_error_code.GIT_ENOTFOUND when no matching branch
160  * exists, git_error_code.GIT_EINVALIDSPEC, otherwise an error code.
161  */
162 //GIT_EXTERN
163 int git_branch_lookup(libgit2_d.types.git_reference** out_, libgit2_d.types.git_repository* repo, const (char)* branch_name, libgit2_d.types.git_branch_t branch_type);
164 
165 /**
166  * Get the branch name
167  *
168  * Given a reference object, this will check that it really is a branch (ie.
169  * it lives under "refs/heads/" or "refs/remotes/"), and return the branch part
170  * of it.
171  *
172  * @param out_ Pointer to the abbreviated reference name.
173  *        Owned by ref, do not free.
174  *
175  * @param ref A reference object, ideally pointing to a branch
176  *
177  * @return 0 on success; GIT_EINVALID if the reference isn't either a local or
178  *         remote branch, otherwise an error code.
179  */
180 //GIT_EXTERN
181 int git_branch_name(const (char)** out_, const (libgit2_d.types.git_reference)* ref_);
182 
183 /**
184  * Get the upstream of a branch
185  *
186  * Given a reference, this will return a new reference object corresponding
187  * to its remote tracking branch. The reference must be a local branch.
188  *
189  * @see git_branch_upstream_name for details on the resolution.
190  *
191  * @param out_ Pointer where to store the retrieved reference.
192  * @param branch Current underlying reference of the branch.
193  *
194  * @return 0 on success; GIT_ENOTFOUND when no remote tracking
195  *         reference exists, otherwise an error code.
196  */
197 //GIT_EXTERN
198 int git_branch_upstream(libgit2_d.types.git_reference** out_, const (libgit2_d.types.git_reference)* branch);
199 
200 /**
201  * Set a branch's upstream branch
202  *
203  * This will update the configuration to set the branch named `branch_name` as the upstream of `branch`.
204  * Pass a NULL name to unset the upstream information.
205  *
206  * @note the actual tracking reference must have been already created for the
207  * operation to succeed.
208  *
209  * @param branch the branch to configure
210  * @param branch_name remote-tracking or local branch to set as upstream.
211  *
212  * @return 0 on success; GIT_ENOTFOUND if there's no branch named `branch_name`
213  *         or an error code
214  */
215 //GIT_EXTERN
216 int git_branch_set_upstream(libgit2_d.types.git_reference* branch, const (char)* branch_name);
217 
218 /**
219  * Get the upstream name of a branch
220  *
221  * Given a local branch, this will return its remote-tracking branch information,
222  * as a full reference name, ie. "feature/nice" would become
223  * "refs/remote/origin/feature/nice", depending on that branch's configuration.
224  *
225  * @param out_ the buffer into which the name will be written.
226  * @param repo the repository where the branches live.
227  * @param refname reference name of the local branch.
228  *
229  * @return 0 on success, GIT_ENOTFOUND when no remote tracking reference exists,
230  *         or an error code.
231  */
232 //GIT_EXTERN
233 int git_branch_upstream_name(libgit2_d.buffer.git_buf* out_, libgit2_d.types.git_repository* repo, const (char)* refname);
234 
235 /**
236  * Determine if HEAD points to the given branch
237  *
238  * @param branch A reference to a local branch.
239  *
240  * @return 1 if HEAD points at the branch, 0 if it isn't, or a negative value
241  * 		   as an error code.
242  */
243 //GIT_EXTERN
244 int git_branch_is_head(const (libgit2_d.types.git_reference)* branch);
245 
246 /**
247  * Determine if any HEAD points to the current branch
248  *
249  * This will iterate over all known linked repositories (usually in the form of
250  * worktrees) and report whether any HEAD is pointing at the current branch.
251  *
252  * @param branch A reference to a local branch.
253  *
254  * @return 1 if branch is checked out, 0 if it isn't, an error code otherwise.
255  */
256 //GIT_EXTERN
257 int git_branch_is_checked_out(const (libgit2_d.types.git_reference)* branch);
258 
259 /**
260  * Find the remote name of a remote-tracking branch
261  *
262  * This will return the name of the remote whose fetch refspec is matching
263  * the given branch. E.g. given a branch "refs/remotes/test/master", it will
264  * extract the "test" part. If refspecs from multiple remotes match,
265  * the function will return GIT_EAMBIGUOUS.
266  *
267  * @param out_ The buffer into which the name will be written.
268  * @param repo The repository where the branch lives.
269  * @param refname complete name of the remote tracking branch.
270  *
271  * @return 0 on success, GIT_ENOTFOUND when no matching remote was found,
272  *         GIT_EAMBIGUOUS when the branch maps to several remotes,
273  *         otherwise an error code.
274  */
275 //GIT_EXTERN
276 int git_branch_remote_name(libgit2_d.buffer.git_buf* out_, libgit2_d.types.git_repository* repo, const (char)* refname);
277 
278 /**
279  * Retrieve the upstream remote of a local branch
280  *
281  * This will return the currently configured "branch.*.remote" for a given
282  * branch. This branch must be local.
283  *
284  * @param buf the buffer into which to write the name
285  * @param repo the repository in which to look
286  * @param refname the full name of the branch
287  * @return 0 or an error code
288  */
289 //GIT_EXTERN
290 int git_branch_upstream_remote(libgit2_d.buffer.git_buf* buf, libgit2_d.types.git_repository* repo, const (char)* refname);
291 
292 /** @} */