Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:59:36

0001 /*
0002  * Copyright (C) the libgit2 contributors. All rights reserved.
0003  *
0004  * This file is part of libgit2, distributed under the GNU GPL v2 with
0005  * a Linking Exception. For full terms see the included COPYING file.
0006  */
0007 #ifndef INCLUDE_git_branch_h__
0008 #define INCLUDE_git_branch_h__
0009 
0010 #include "common.h"
0011 #include "oid.h"
0012 #include "types.h"
0013 
0014 /**
0015  * @file git2/branch.h
0016  * @brief Git branch parsing routines
0017  * @defgroup git_branch Git branch management
0018  * @ingroup Git
0019  * @{
0020  */
0021 GIT_BEGIN_DECL
0022 
0023 /**
0024  * Create a new branch pointing at a target commit
0025  *
0026  * A new direct reference will be created pointing to
0027  * this target commit. If `force` is true and a reference
0028  * already exists with the given name, it'll be replaced.
0029  *
0030  * The returned reference must be freed by the user.
0031  *
0032  * The branch name will be checked for validity.
0033  * See `git_tag_create()` for rules about valid names.
0034  *
0035  * @param out Pointer where to store the underlying reference.
0036  *
0037  * @param repo the repository to create the branch in.
0038  *
0039  * @param branch_name Name for the branch; this name is
0040  * validated for consistency. It should also not conflict with
0041  * an already existing branch name.
0042  *
0043  * @param target Commit to which this branch should point. This object
0044  * must belong to the given `repo`.
0045  *
0046  * @param force Overwrite existing branch.
0047  *
0048  * @return 0, GIT_EINVALIDSPEC or an error code.
0049  * A proper reference is written in the refs/heads namespace
0050  * pointing to the provided target commit.
0051  */
0052 GIT_EXTERN(int) git_branch_create(
0053     git_reference **out,
0054     git_repository *repo,
0055     const char *branch_name,
0056     const git_commit *target,
0057     int force);
0058 
0059 /**
0060  * Create a new branch pointing at a target commit
0061  *
0062  * This behaves like `git_branch_create()` but takes an annotated
0063  * commit, which lets you specify which extended sha syntax string was
0064  * specified by a user, allowing for more exact reflog messages.
0065  *
0066  * See the documentation for `git_branch_create()`.
0067  *
0068  * @see git_branch_create
0069  */
0070 GIT_EXTERN(int) git_branch_create_from_annotated(
0071     git_reference **ref_out,
0072     git_repository *repository,
0073     const char *branch_name,
0074     const git_annotated_commit *commit,
0075     int force);
0076 
0077 /**
0078  * Delete an existing branch reference.
0079  *
0080  * Note that if the deletion succeeds, the reference object will not
0081  * be valid anymore, and should be freed immediately by the user using
0082  * `git_reference_free()`.
0083  *
0084  * @param branch A valid reference representing a branch
0085  * @return 0 on success, or an error code.
0086  */
0087 GIT_EXTERN(int) git_branch_delete(git_reference *branch);
0088 
0089 /** Iterator type for branches */
0090 typedef struct git_branch_iterator git_branch_iterator;
0091 
0092 /**
0093  * Create an iterator which loops over the requested branches.
0094  *
0095  * @param out the iterator
0096  * @param repo Repository where to find the branches.
0097  * @param list_flags Filtering flags for the branch
0098  * listing. Valid values are GIT_BRANCH_LOCAL, GIT_BRANCH_REMOTE
0099  * or GIT_BRANCH_ALL.
0100  *
0101  * @return 0 on success  or an error code
0102  */
0103 GIT_EXTERN(int) git_branch_iterator_new(
0104     git_branch_iterator **out,
0105     git_repository *repo,
0106     git_branch_t list_flags);
0107 
0108 /**
0109  * Retrieve the next branch from the iterator
0110  *
0111  * @param out the reference
0112  * @param out_type the type of branch (local or remote-tracking)
0113  * @param iter the branch iterator
0114  * @return 0 on success, GIT_ITEROVER if there are no more branches or an error code.
0115  */
0116 GIT_EXTERN(int) git_branch_next(git_reference **out, git_branch_t *out_type, git_branch_iterator *iter);
0117 
0118 /**
0119  * Free a branch iterator
0120  *
0121  * @param iter the iterator to free
0122  */
0123 GIT_EXTERN(void) git_branch_iterator_free(git_branch_iterator *iter);
0124 
0125 /**
0126  * Move/rename an existing local branch reference.
0127  *
0128  * The new branch name will be checked for validity.
0129  * See `git_tag_create()` for rules about valid names.
0130  *
0131  * Note that if the move succeeds, the old reference object will not
0132  * be valid anymore, and should be freed immediately by the user using
0133  * `git_reference_free()`.
0134  *
0135  * @param out New reference object for the updated name.
0136  *
0137  * @param branch Current underlying reference of the branch.
0138  *
0139  * @param new_branch_name Target name of the branch once the move
0140  * is performed; this name is validated for consistency.
0141  *
0142  * @param force Overwrite existing branch.
0143  *
0144  * @return 0 on success, GIT_EINVALIDSPEC or an error code.
0145  */
0146 GIT_EXTERN(int) git_branch_move(
0147     git_reference **out,
0148     git_reference *branch,
0149     const char *new_branch_name,
0150     int force);
0151 
0152 /**
0153  * Lookup a branch by its name in a repository.
0154  *
0155  * The generated reference must be freed by the user.
0156  * The branch name will be checked for validity.
0157  *
0158  * @see git_tag_create for rules about valid names.
0159  *
0160  * @param out pointer to the looked-up branch reference
0161  * @param repo the repository to look up the branch
0162  * @param branch_name Name of the branch to be looked-up;
0163  * this name is validated for consistency.
0164  * @param branch_type Type of the considered branch. This should
0165  * be valued with either GIT_BRANCH_LOCAL or GIT_BRANCH_REMOTE.
0166  *
0167  * @return 0 on success; GIT_ENOTFOUND when no matching branch
0168  * exists, GIT_EINVALIDSPEC, otherwise an error code.
0169  */
0170 GIT_EXTERN(int) git_branch_lookup(
0171     git_reference **out,
0172     git_repository *repo,
0173     const char *branch_name,
0174     git_branch_t branch_type);
0175 
0176 /**
0177  * Get the branch name
0178  *
0179  * Given a reference object, this will check that it really is a branch (ie.
0180  * it lives under "refs/heads/" or "refs/remotes/"), and return the branch part
0181  * of it.
0182  *
0183  * @param out Pointer to the abbreviated reference name.
0184  *        Owned by ref, do not free.
0185  *
0186  * @param ref A reference object, ideally pointing to a branch
0187  *
0188  * @return 0 on success; GIT_EINVALID if the reference isn't either a local or
0189  *         remote branch, otherwise an error code.
0190  */
0191 GIT_EXTERN(int) git_branch_name(
0192         const char **out,
0193         const git_reference *ref);
0194 
0195 /**
0196  * Get the upstream of a branch
0197  *
0198  * Given a reference, this will return a new reference object corresponding
0199  * to its remote tracking branch. The reference must be a local branch.
0200  *
0201  * @see git_branch_upstream_name for details on the resolution.
0202  *
0203  * @param out Pointer where to store the retrieved reference.
0204  * @param branch Current underlying reference of the branch.
0205  *
0206  * @return 0 on success; GIT_ENOTFOUND when no remote tracking
0207  *         reference exists, otherwise an error code.
0208  */
0209 GIT_EXTERN(int) git_branch_upstream(
0210     git_reference **out,
0211     const git_reference *branch);
0212 
0213 /**
0214  * Set a branch's upstream branch
0215  *
0216  * This will update the configuration to set the branch named `branch_name` as the upstream of `branch`.
0217  * Pass a NULL name to unset the upstream information.
0218  *
0219  * @note the actual tracking reference must have been already created for the
0220  * operation to succeed.
0221  *
0222  * @param branch the branch to configure
0223  * @param branch_name remote-tracking or local branch to set as upstream.
0224  *
0225  * @return 0 on success; GIT_ENOTFOUND if there's no branch named `branch_name`
0226  *         or an error code
0227  */
0228 GIT_EXTERN(int) git_branch_set_upstream(
0229     git_reference *branch,
0230     const char *branch_name);
0231 
0232 /**
0233  * Get the upstream name of a branch
0234  *
0235  * Given a local branch, this will return its remote-tracking branch information,
0236  * as a full reference name, ie. "feature/nice" would become
0237  * "refs/remote/origin/feature/nice", depending on that branch's configuration.
0238  *
0239  * @param out the buffer into which the name will be written.
0240  * @param repo the repository where the branches live.
0241  * @param refname reference name of the local branch.
0242  *
0243  * @return 0 on success, GIT_ENOTFOUND when no remote tracking reference exists,
0244  *         or an error code.
0245  */
0246 GIT_EXTERN(int) git_branch_upstream_name(
0247     git_buf *out,
0248     git_repository *repo,
0249     const char *refname);
0250 
0251 /**
0252  * Determine if HEAD points to the given branch
0253  *
0254  * @param branch A reference to a local branch.
0255  *
0256  * @return 1 if HEAD points at the branch, 0 if it isn't, or a negative value
0257  *         as an error code.
0258  */
0259 GIT_EXTERN(int) git_branch_is_head(
0260     const git_reference *branch);
0261 
0262 /**
0263  * Determine if any HEAD points to the current branch
0264  *
0265  * This will iterate over all known linked repositories (usually in the form of
0266  * worktrees) and report whether any HEAD is pointing at the current branch.
0267  *
0268  * @param branch A reference to a local branch.
0269  *
0270  * @return 1 if branch is checked out, 0 if it isn't, an error code otherwise.
0271  */
0272 GIT_EXTERN(int) git_branch_is_checked_out(
0273     const git_reference *branch);
0274 
0275 /**
0276  * Find the remote name of a remote-tracking branch
0277  *
0278  * This will return the name of the remote whose fetch refspec is matching
0279  * the given branch. E.g. given a branch "refs/remotes/test/master", it will
0280  * extract the "test" part. If refspecs from multiple remotes match,
0281  * the function will return GIT_EAMBIGUOUS.
0282  *
0283  * @param out The buffer into which the name will be written.
0284  * @param repo The repository where the branch lives.
0285  * @param refname complete name of the remote tracking branch.
0286  *
0287  * @return 0 on success, GIT_ENOTFOUND when no matching remote was found,
0288  *         GIT_EAMBIGUOUS when the branch maps to several remotes,
0289  *         otherwise an error code.
0290  */
0291 GIT_EXTERN(int) git_branch_remote_name(
0292     git_buf *out,
0293     git_repository *repo,
0294     const char *refname);
0295 
0296 /**
0297  * Retrieve the upstream remote of a local branch
0298  *
0299  * This will return the currently configured "branch.*.remote" for a given
0300  * branch. This branch must be local.
0301  *
0302  * @param buf the buffer into which to write the name
0303  * @param repo the repository in which to look
0304  * @param refname the full name of the branch
0305  * @return 0 or an error code
0306  */
0307  GIT_EXTERN(int) git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname);
0308 
0309 /**
0310  * Retrieve the upstream merge of a local branch
0311  *
0312  * This will return the currently configured "branch.*.merge" for a given
0313  * branch. This branch must be local.
0314  *
0315  * @param buf the buffer into which to write the name
0316  * @param repo the repository in which to look
0317  * @param refname the full name of the branch
0318  * @return 0 or an error code
0319  */
0320  GIT_EXTERN(int) git_branch_upstream_merge(git_buf *buf, git_repository *repo, const char *refname);
0321 
0322 /**
0323  * Determine whether a branch name is valid, meaning that (when prefixed
0324  * with `refs/heads/`) that it is a valid reference name, and that any
0325  * additional branch name restrictions are imposed (eg, it cannot start
0326  * with a `-`).
0327  *
0328  * @param valid output pointer to set with validity of given branch name
0329  * @param name a branch name to test
0330  * @return 0 on success or an error code
0331  */
0332 GIT_EXTERN(int) git_branch_name_is_valid(int *valid, const char *name);
0333 
0334 /** @} */
0335 GIT_END_DECL
0336 #endif