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