Documentation: Typo / spelling / formatting fixes
[git/git.git] / branch.c
CommitLineData
e496c003
DB
1#include "cache.h"
2#include "branch.h"
3#include "refs.h"
4#include "remote.h"
5#include "commit.h"
6
7struct tracking {
8 struct refspec spec;
9 char *src;
10 const char *remote;
11 int matches;
12};
13
14static int find_tracked_branch(struct remote *remote, void *priv)
15{
16 struct tracking *tracking = priv;
17
18 if (!remote_find_tracking(remote, &tracking->spec)) {
19 if (++tracking->matches == 1) {
20 tracking->src = tracking->spec.src;
21 tracking->remote = remote->name;
22 } else {
23 free(tracking->spec.src);
24 if (tracking->src) {
25 free(tracking->src);
26 tracking->src = NULL;
27 }
28 }
29 tracking->spec.src = NULL;
30 }
31
32 return 0;
33}
34
c998ae9b
DS
35static int should_setup_rebase(const struct tracking *tracking)
36{
37 switch (autorebase) {
38 case AUTOREBASE_NEVER:
39 return 0;
40 case AUTOREBASE_LOCAL:
41 return tracking->remote == NULL;
42 case AUTOREBASE_REMOTE:
43 return tracking->remote != NULL;
44 case AUTOREBASE_ALWAYS:
45 return 1;
46 }
47 return 0;
48}
49
e496c003
DB
50/*
51 * This is called when new_ref is branched off of orig_ref, and tries
52 * to infer the settings for branch.<new_ref>.{remote,merge} from the
53 * config.
54 */
9ed36cfa
JS
55static int setup_tracking(const char *new_ref, const char *orig_ref,
56 enum branch_track track)
e496c003
DB
57{
58 char key[1024];
59 struct tracking tracking;
60
61 if (strlen(new_ref) > 1024 - 7 - 7 - 1)
62 return error("Tracking not set up: name too long: %s",
63 new_ref);
64
65 memset(&tracking, 0, sizeof(tracking));
66 tracking.spec.dst = (char *)orig_ref;
9ed36cfa 67 if (for_each_remote(find_tracked_branch, &tracking))
e496c003
DB
68 return 1;
69
9ed36cfa
JS
70 if (!tracking.matches)
71 switch (track) {
72 case BRANCH_TRACK_ALWAYS:
73 case BRANCH_TRACK_EXPLICIT:
74 break;
75 default:
76 return 1;
77 }
78
e496c003
DB
79 if (tracking.matches > 1)
80 return error("Not tracking: ambiguous information for ref %s",
81 orig_ref);
82
9ed36cfa
JS
83 sprintf(key, "branch.%s.remote", new_ref);
84 git_config_set(key, tracking.remote ? tracking.remote : ".");
85 sprintf(key, "branch.%s.merge", new_ref);
86 git_config_set(key, tracking.src ? tracking.src : orig_ref);
9ed36cfa
JS
87 printf("Branch %s set up to track %s branch %s.\n", new_ref,
88 tracking.remote ? "remote" : "local", orig_ref);
c998ae9b
DS
89 if (should_setup_rebase(&tracking)) {
90 sprintf(key, "branch.%s.rebase", new_ref);
91 git_config_set(key, "true");
92 printf("This branch will rebase on pull.\n");
93 }
94 free(tracking.src);
e496c003
DB
95
96 return 0;
97}
98
99void create_branch(const char *head,
100 const char *name, const char *start_name,
9ed36cfa 101 int force, int reflog, enum branch_track track)
e496c003
DB
102{
103 struct ref_lock *lock;
104 struct commit *commit;
105 unsigned char sha1[20];
8415d5c7
JH
106 char *real_ref, msg[PATH_MAX + 20];
107 struct strbuf ref = STRBUF_INIT;
e496c003 108 int forcing = 0;
8415d5c7 109 int len;
e496c003 110
8415d5c7
JH
111 len = strlen(name);
112 if (interpret_nth_last_branch(name, &ref) != len) {
113 strbuf_reset(&ref);
114 strbuf_add(&ref, name, len);
115 }
116 strbuf_splice(&ref, 0, 0, "refs/heads/", 11);
117
118 if (check_ref_format(ref.buf))
e496c003
DB
119 die("'%s' is not a valid branch name.", name);
120
8415d5c7 121 if (resolve_ref(ref.buf, sha1, 1, NULL)) {
e496c003
DB
122 if (!force)
123 die("A branch named '%s' already exists.", name);
124 else if (!is_bare_repository() && !strcmp(head, name))
125 die("Cannot force update the current branch.");
126 forcing = 1;
127 }
128
129 real_ref = NULL;
130 if (get_sha1(start_name, sha1))
131 die("Not a valid object name: '%s'.", start_name);
132
133 switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) {
134 case 0:
135 /* Not branching from any existing branch */
9ed36cfa
JS
136 if (track == BRANCH_TRACK_EXPLICIT)
137 die("Cannot setup tracking information; starting point is not a branch.");
e496c003
DB
138 break;
139 case 1:
51a94af8
JH
140 /* Unique completion -- good, only if it is a real ref */
141 if (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, "HEAD"))
142 die("Cannot setup tracking information; starting point is not a branch.");
e496c003
DB
143 break;
144 default:
145 die("Ambiguous object name: '%s'.", start_name);
146 break;
147 }
148
149 if ((commit = lookup_commit_reference(sha1)) == NULL)
150 die("Not a valid branch point: '%s'.", start_name);
151 hashcpy(sha1, commit->object.sha1);
152
8415d5c7 153 lock = lock_any_ref_for_update(ref.buf, NULL, 0);
e496c003
DB
154 if (!lock)
155 die("Failed to lock ref for update: %s.", strerror(errno));
156
157 if (reflog)
158 log_all_ref_updates = 1;
159
160 if (forcing)
161 snprintf(msg, sizeof msg, "branch: Reset from %s",
162 start_name);
163 else
164 snprintf(msg, sizeof msg, "branch: Created from %s",
165 start_name);
166
e496c003 167 if (real_ref && track)
9ed36cfa 168 setup_tracking(name, real_ref, track);
e496c003
DB
169
170 if (write_ref_sha1(lock, sha1, msg) < 0)
171 die("Failed to write ref: %s.", strerror(errno));
172
8415d5c7 173 strbuf_release(&ref);
9ed36cfa 174 free(real_ref);
e496c003 175}
c369e7b8
DB
176
177void remove_branch_state(void)
178{
179 unlink(git_path("MERGE_HEAD"));
0af0ac7e 180 unlink(git_path("MERGE_RR"));
c369e7b8 181 unlink(git_path("MERGE_MSG"));
cf10f9fd 182 unlink(git_path("MERGE_MODE"));
c369e7b8
DB
183 unlink(git_path("SQUASH_MSG"));
184}