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