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