Raw
1 #ifndef BRANCH_H
2 #define BRANCH_H
3
4 struct repository;
5 struct strbuf;
6
7 enum branch_track {
8 BRANCH_TRACK_UNSPECIFIED = -1,
9 BRANCH_TRACK_NEVER = 0,
10 BRANCH_TRACK_REMOTE,
11 BRANCH_TRACK_ALWAYS,
12 BRANCH_TRACK_EXPLICIT,
13 BRANCH_TRACK_OVERRIDE,
14 BRANCH_TRACK_INHERIT,
15 BRANCH_TRACK_SIMPLE,
16 };
17
18 /* Functions for acting on the information about branches. */
19
20 /**
21 * Sets branch.<new_ref>.{remote,merge} config settings such that
22 * new_ref tracks orig_ref according to the specified tracking mode.
23 *
24 * - new_ref is the name of the branch that we are setting tracking
25 * for.
26 *
27 * - orig_ref is the name of the ref that is 'upstream' of new_ref.
28 * orig_ref will be expanded with DWIM so that the config settings
29 * are in the correct format e.g. "refs/remotes/origin/main" instead
30 * of "origin/main".
31 *
32 * - track is the tracking mode e.g. BRANCH_TRACK_REMOTE causes
33 * new_ref to track orig_ref directly, whereas BRANCH_TRACK_INHERIT
34 * causes new_ref to track whatever orig_ref tracks.
35 *
36 * - quiet suppresses tracking information.
37 */
38 void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
39 const char *orig_ref, enum branch_track track,
40 int quiet);
41
42 /*
43 * Creates a new branch, where:
44 *
45 * - r is the repository to add a branch to
46 *
47 * - name is the new branch name
48 *
49 * - start_name is the name of the existing branch that the new branch should
50 * start from
51 *
52 * - force enables overwriting an existing (non-head) branch
53 *
54 * - clobber_head_ok, when enabled with 'force', allows the currently
55 * checked out (head) branch to be overwritten
56 *
57 * - reflog creates a reflog for the branch
58 *
59 * - quiet suppresses tracking information
60 *
61 * - track causes the new branch to be configured to merge the remote branch
62 * that start_name is a tracking branch for (if any).
63 *
64 * - dry_run causes the branch to be validated but not created.
65 *
66 */
67 void create_branch(struct repository *r,
68 const char *name, const char *start_name,
69 int force, int clobber_head_ok,
70 int reflog, int quiet, enum branch_track track,
71 int dry_run);
72
73 /*
74 * Creates a new branch in a repository and its submodules (and its
75 * submodules, recursively). The parameters are mostly analogous to
76 * those of create_branch() except for start_name, which is represented
77 * by two different parameters:
78 *
79 * - start_committish is the commit-ish, in repository r, that determines
80 * which commits the branches will point to. The superproject branch
81 * will point to the commit of start_committish and the submodule
82 * branches will point to the gitlink commit oids in start_committish's
83 * tree.
84 *
85 * - tracking_name is the name of the ref, in repository r, that will be
86 * used to set up tracking information. This value is propagated to
87 * all submodules, which will evaluate the ref using their own ref
88 * stores. If NULL, this defaults to start_committish.
89 *
90 * When this function is called on the superproject, start_committish
91 * can be any user-provided ref and tracking_name can be NULL (similar
92 * to create_branches()). But when recursing through submodules,
93 * start_committish is the plain gitlink commit oid. Since the oid cannot
94 * be used for tracking information, tracking_name is propagated and
95 * used for tracking instead.
96 */
97 void create_branches_recursively(struct repository *r, const char *name,
98 const char *start_committish,
99 const char *tracking_name, int force,
100 int reflog, int quiet, enum branch_track track,
101 int dry_run);
102
103 /*
104 * If the branch at 'refname' is currently checked out in a worktree,
105 * then return the path to that worktree.
106 */
107 const char *branch_checked_out(const char *refname);
108
109 /*
110 * If the branch at 'refname' is currently used for bisecting in a
111 * worktree, then return the path to that worktree.
112 */
113 const char *branch_bisecting(const char *refname);
114
115 /*
116 * Check if 'name' can be a valid name for a branch; die otherwise.
117 * Return 1 if the named branch already exists; return 0 otherwise.
118 * Fill ref with the full refname for the branch.
119 */
120 int validate_branchname(const char *name, struct strbuf *ref);
121
122 /*
123 * Check if a branch 'name' can be created as a new branch; die otherwise.
124 * 'force' can be used when it is OK for the named branch already exists.
125 * Return 1 if the named branch already exists; return 0 otherwise.
126 * Fill ref with the full refname for the branch.
127 */
128 int validate_new_branchname(const char *name, struct strbuf *ref, int force);
129
130 /*
131 * Remove information about the merge state on the current
132 * branch. (E.g., MERGE_HEAD)
133 */
134 void remove_merge_branch_state(struct repository *r);
135
136 /*
137 * Remove information about the state of working on the current
138 * branch. (E.g., MERGE_HEAD)
139 */
140 void remove_branch_state(struct repository *r, int verbose);
141
142 /*
143 * Configure local branch "local" as downstream to branch "remote"
144 * from remote "origin". Used by git branch --set-upstream.
145 * Returns 0 on success.
146 */
147 #define BRANCH_CONFIG_VERBOSE 01
148 int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
149
150 /*
151 * Read branch description
152 */
153 int read_branch_desc(struct strbuf *, const char *branch_name);
154
155 /*
156 * Check if a branch is checked out in the main worktree or any linked
157 * worktree and die (with a message describing its checkout location) if
158 * it is.
159 */
160 void die_if_checked_out(const char *branch, int ignore_current_worktree);
161
162 #endif