blob: 560b6b96a8f307baac31ea699321e17ed2c22dbe [file] [log] [blame]
Daniel Barkalowe496c002008-02-07 11:40:08 -05001#ifndef BRANCH_H
2#define BRANCH_H
3
Nguyễn Thái Ngọc Duy4edce172018-11-10 06:49:00 +01004struct repository;
Elijah Newrenef3ca952018-08-15 10:54:05 -07005struct strbuf;
6
Elijah Newrene730b812018-08-15 10:54:07 -07007enum branch_track {
8 BRANCH_TRACK_UNSPECIFIED = -1,
9 BRANCH_TRACK_NEVER = 0,
10 BRANCH_TRACK_REMOTE,
11 BRANCH_TRACK_ALWAYS,
12 BRANCH_TRACK_EXPLICIT,
Josh Steadmond3115662021-12-20 19:30:23 -080013 BRANCH_TRACK_OVERRIDE,
14 BRANCH_TRACK_INHERIT,
Tao Klerksbdaf1df2022-04-29 09:56:44 +000015 BRANCH_TRACK_SIMPLE,
Elijah Newrene730b812018-08-15 10:54:07 -070016};
17
18extern enum branch_track git_branch_track;
19
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050020/* Functions for acting on the information about branches. */
21
Glen Chooe89f1512022-01-28 16:04:41 -080022/**
23 * Sets branch.<new_ref>.{remote,merge} config settings such that
24 * new_ref tracks orig_ref according to the specified tracking mode.
25 *
26 * - new_ref is the name of the branch that we are setting tracking
27 * for.
28 *
29 * - orig_ref is the name of the ref that is 'upstream' of new_ref.
30 * orig_ref will be expanded with DWIM so that the config settings
31 * are in the correct format e.g. "refs/remotes/origin/main" instead
32 * of "origin/main".
33 *
34 * - track is the tracking mode e.g. BRANCH_TRACK_REMOTE causes
35 * new_ref to track orig_ref directly, whereas BRANCH_TRACK_INHERIT
36 * causes new_ref to track whatever orig_ref tracks.
37 *
38 * - quiet suppresses tracking information.
39 */
40void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
41 const char *orig_ref, enum branch_track track,
42 int quiet);
43
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050044/*
Jeff King4bd488e2016-11-04 12:30:12 -040045 * Creates a new branch, where:
46 *
Nguyễn Thái Ngọc Duy4edce172018-11-10 06:49:00 +010047 * - r is the repository to add a branch to
48 *
Jeff King4bd488e2016-11-04 12:30:12 -040049 * - name is the new branch name
50 *
51 * - start_name is the name of the existing branch that the new branch should
52 * start from
53 *
54 * - force enables overwriting an existing (non-head) branch
55 *
Glen Choobc0893c2022-01-28 16:04:42 -080056 * - clobber_head_ok, when enabled with 'force', allows the currently
57 * checked out (head) branch to be overwritten
Kaartic Sivaraamf6cea742017-11-18 22:56:45 +053058 *
Jeff King4bd488e2016-11-04 12:30:12 -040059 * - reflog creates a reflog for the branch
60 *
Kaartic Sivaraamf6cea742017-11-18 22:56:45 +053061 * - quiet suppresses tracking information
62 *
Jeff King4bd488e2016-11-04 12:30:12 -040063 * - track causes the new branch to be configured to merge the remote branch
64 * that start_name is a tracking branch for (if any).
Kaartic Sivaraame2bbd0c2017-11-18 22:56:46 +053065 *
Glen Choo3f3e7602022-01-28 16:04:43 -080066 * - dry_run causes the branch to be validated but not created.
67 *
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050068 */
Nguyễn Thái Ngọc Duy4edce172018-11-10 06:49:00 +010069void create_branch(struct repository *r,
70 const char *name, const char *start_name,
Kaartic Sivaraame2bbd0c2017-11-18 22:56:46 +053071 int force, int clobber_head_ok,
Glen Choo3f3e7602022-01-28 16:04:43 -080072 int reflog, int quiet, enum branch_track track,
73 int dry_run);
Daniel Barkalowe496c002008-02-07 11:40:08 -050074
Daniel Barkalowc369e7b2008-02-07 11:40:16 -050075/*
Glen Choo961b1302022-01-28 16:04:45 -080076 * Creates a new branch in a repository and its submodules (and its
77 * submodules, recursively). The parameters are mostly analogous to
78 * those of create_branch() except for start_name, which is represented
79 * by two different parameters:
80 *
81 * - start_commitish is the commit-ish, in repository r, that determines
82 * which commits the branches will point to. The superproject branch
83 * will point to the commit of start_commitish and the submodule
84 * branches will point to the gitlink commit oids in start_commitish's
85 * tree.
86 *
87 * - tracking_name is the name of the ref, in repository r, that will be
88 * used to set up tracking information. This value is propagated to
89 * all submodules, which will evaluate the ref using their own ref
90 * stores. If NULL, this defaults to start_commitish.
91 *
92 * When this function is called on the superproject, start_commitish
93 * can be any user-provided ref and tracking_name can be NULL (similar
94 * to create_branches()). But when recursing through submodules,
95 * start_commitish is the plain gitlink commit oid. Since the oid cannot
96 * be used for tracking information, tracking_name is propagated and
97 * used for tracking instead.
98 */
99void create_branches_recursively(struct repository *r, const char *name,
100 const char *start_commitish,
101 const char *tracking_name, int force,
102 int reflog, int quiet, enum branch_track track,
103 int dry_run);
104/*
Junio C Hamanobc1c9c02017-10-13 13:45:40 +0900105 * Check if 'name' can be a valid name for a branch; die otherwise.
106 * Return 1 if the named branch already exists; return 0 otherwise.
107 * Fill ref with the full refname for the branch.
Conrad Irwin55c4a672011-08-20 14:49:48 -0700108 */
Denton Liu55454422019-04-29 04:28:14 -0400109int validate_branchname(const char *name, struct strbuf *ref);
Junio C Hamanobc1c9c02017-10-13 13:45:40 +0900110
111/*
112 * Check if a branch 'name' can be created as a new branch; die otherwise.
113 * 'force' can be used when it is OK for the named branch already exists.
114 * Return 1 if the named branch already exists; return 0 otherwise.
115 * Fill ref with the full refname for the branch.
116 */
Denton Liu55454422019-04-29 04:28:14 -0400117int validate_new_branchname(const char *name, struct strbuf *ref, int force);
Conrad Irwin55c4a672011-08-20 14:49:48 -0700118
119/*
Nguyễn Thái Ngọc Duyb6433552019-05-09 17:10:27 +0700120 * Remove information about the merge state on the current
121 * branch. (E.g., MERGE_HEAD)
122 */
123void remove_merge_branch_state(struct repository *r);
124
125/*
Daniel Barkalowc369e7b2008-02-07 11:40:16 -0500126 * Remove information about the state of working on the current
127 * branch. (E.g., MERGE_HEAD)
128 */
Nguyễn Thái Ngọc Duyf4a4b9a2019-03-29 17:38:59 +0700129void remove_branch_state(struct repository *r, int verbose);
Daniel Barkalowc369e7b2008-02-07 11:40:16 -0500130
Junio C Hamanoa9f2c132009-03-03 22:29:55 -0800131/*
Matthieu Moy13931232010-11-02 16:31:25 +0100132 * Configure local branch "local" as downstream to branch "remote"
133 * from remote "origin". Used by git branch --set-upstream.
Patrick Steinhardt27852b22016-02-22 12:23:23 +0100134 * Returns 0 on success.
Junio C Hamanoa9f2c132009-03-03 22:29:55 -0800135 */
136#define BRANCH_CONFIG_VERBOSE 01
Denton Liu55454422019-04-29 04:28:14 -0400137int install_branch_config(int flag, const char *local, const char *origin, const char *remote);
Junio C Hamanoa9f2c132009-03-03 22:29:55 -0800138
Junio C Hamano6f9a3322011-09-21 20:19:38 -0700139/*
140 * Read branch description
141 */
Denton Liu55454422019-04-29 04:28:14 -0400142int read_branch_desc(struct strbuf *, const char *branch_name);
Junio C Hamano6f9a3322011-09-21 20:19:38 -0700143
Eric Sunshineed89f842015-07-17 19:00:04 -0400144/*
145 * Check if a branch is checked out in the main worktree or any linked
146 * worktree and die (with a message describing its checkout location) if
147 * it is.
148 */
Denton Liu55454422019-04-29 04:28:14 -0400149void die_if_checked_out(const char *branch, int ignore_current_worktree);
Eric Sunshineed89f842015-07-17 19:00:04 -0400150
Kazuki Yamaguchi70999e92016-03-27 23:37:14 +0900151/*
152 * Update all per-worktree HEADs pointing at the old ref to point the new ref.
153 * This will be used when renaming a branch. Returns 0 if successful, non-zero
154 * otherwise.
155 */
Denton Liu55454422019-04-29 04:28:14 -0400156int replace_each_worktree_head_symref(const char *oldref, const char *newref,
Denton Liuad6dad02019-04-29 04:28:23 -0400157 const char *logmsg);
Kazuki Yamaguchi70999e92016-03-27 23:37:14 +0900158
Daniel Barkalowe496c002008-02-07 11:40:08 -0500159#endif