Commit 3a72345b65a2bfec323fe3d06463645ae0a9babe

Edward Thomson 2020-10-04T18:25:32

Merge pull request #5581 from libgit2/ethomson/mainbranch Respect `init.defaultBranch` setting

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
diff --git a/src/clone.c b/src/clone.c
index 8ca6cea..a662a76 100644
--- a/src/clone.c
+++ b/src/clone.c
@@ -137,6 +137,31 @@ static int update_head_to_new_branch(
 	return error;
 }
 
+static int update_head_to_default(git_repository *repo)
+{
+	git_buf initialbranch = GIT_BUF_INIT;
+	const char *branch_name;
+	int error = 0;
+
+	if ((error = git_repository_initialbranch(&initialbranch, repo)) < 0)
+		goto done;
+
+	if (git__prefixcmp(initialbranch.ptr, GIT_REFS_HEADS_DIR) != 0) {
+		git_error_set(GIT_ERROR_INVALID, "invalid initial branch '%s'", initialbranch.ptr);
+		error = -1;
+		goto done;
+	}
+
+	branch_name = initialbranch.ptr + strlen(GIT_REFS_HEADS_DIR);
+
+	error = setup_tracking_config(repo, branch_name, GIT_REMOTE_ORIGIN,
+		initialbranch.ptr);
+
+done:
+	git_buf_dispose(&initialbranch);
+	return error;
+}
+
 static int update_head_to_remote(
 		git_repository *repo,
 		git_remote *remote,
@@ -147,7 +172,7 @@ static int update_head_to_remote(
 	git_refspec *refspec;
 	const git_remote_head *remote_head, **refs;
 	const git_oid *remote_head_id;
-	git_buf remote_master_name = GIT_BUF_INIT;
+	git_buf remote_branch_name = GIT_BUF_INIT;
 	git_buf branch = GIT_BUF_INIT;
 
 	if ((error = git_remote_ls(&refs, &refs_len, remote)) < 0)
@@ -155,8 +180,7 @@ static int update_head_to_remote(
 
 	/* We cloned an empty repository or one with an unborn HEAD */
 	if (refs_len == 0 || strcmp(refs[0]->name, GIT_HEAD_FILE))
-		return setup_tracking_config(
-			repo, "master", GIT_REMOTE_ORIGIN, GIT_REFS_HEADS_MASTER_FILE);
+		return update_head_to_default(repo);
 
 	/* We know we have HEAD, let's see where it points */
 	remote_head = refs[0];
@@ -179,9 +203,9 @@ static int update_head_to_remote(
 		goto cleanup;
 	}
 
-	/* Determine the remote tracking reference name from the local master */
+	/* Determine the remote tracking ref name from the local branch */
 	if ((error = git_refspec_transform(
-		&remote_master_name,
+		&remote_branch_name,
 		refspec,
 		git_buf_cstr(&branch))) < 0)
 		goto cleanup;
@@ -193,7 +217,7 @@ static int update_head_to_remote(
 		reflog_message);
 
 cleanup:
-	git_buf_dispose(&remote_master_name);
+	git_buf_dispose(&remote_branch_name);
 	git_buf_dispose(&branch);
 
 	return error;
diff --git a/src/refs.h b/src/refs.h
index b8d6df7..e0ee03b 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -45,7 +45,6 @@ extern bool git_reference__enable_symbolic_ref_target_validation;
 #define GIT_REBASE_APPLY_DIR "rebase-apply/"
 #define GIT_REBASE_APPLY_REBASING_FILE GIT_REBASE_APPLY_DIR "rebasing"
 #define GIT_REBASE_APPLY_APPLYING_FILE GIT_REBASE_APPLY_DIR "applying"
-#define GIT_REFS_HEADS_MASTER_FILE GIT_REFS_HEADS_DIR "master"
 
 #define GIT_SEQUENCER_DIR "sequencer/"
 #define GIT_SEQUENCER_HEAD_FILE GIT_SEQUENCER_DIR "head"
diff --git a/src/remote.c b/src/remote.c
index 23054c8..51e99dc 100644
--- a/src/remote.c
+++ b/src/remote.c
@@ -2375,29 +2375,36 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
 	const git_remote_head *guess = NULL;
 	const git_oid *head_id;
 	size_t heads_len, i;
+	git_buf local_default = GIT_BUF_INIT;
 	int error;
 
 	assert(out);
 
 	if ((error = git_remote_ls(&heads, &heads_len, remote)) < 0)
-		return error;
-
-	if (heads_len == 0)
-		return GIT_ENOTFOUND;
+		goto done;
 
-	if (strcmp(heads[0]->name, GIT_HEAD_FILE))
-		return GIT_ENOTFOUND;
+	if (heads_len == 0 || strcmp(heads[0]->name, GIT_HEAD_FILE)) {
+		error = GIT_ENOTFOUND;
+		goto done;
+	}
 
 	git_buf_sanitize(out);
+
 	/* the first one must be HEAD so if that has the symref info, we're done */
-	if (heads[0]->symref_target)
-		return git_buf_puts(out, heads[0]->symref_target);
+	if (heads[0]->symref_target) {
+		error = git_buf_puts(out, heads[0]->symref_target);
+		goto done;
+	}
 
 	/*
 	 * If there's no symref information, we have to look over them
-	 * and guess. We return the first match unless the master
-	 * branch is a candidate. Then we return the master branch.
+	 * and guess. We return the first match unless the default
+	 * branch is a candidate. Then we return the default branch.
 	 */
+
+	if ((error = git_repository_initialbranch(&local_default, remote->repo)) < 0)
+		goto done;
+
 	head_id = &heads[0]->oid;
 
 	for (i = 1; i < heads_len; i++) {
@@ -2412,16 +2419,22 @@ int git_remote_default_branch(git_buf *out, git_remote *remote)
 			continue;
 		}
 
-		if (!git__strcmp(GIT_REFS_HEADS_MASTER_FILE, heads[i]->name)) {
+		if (!git__strcmp(local_default.ptr, heads[i]->name)) {
 			guess = heads[i];
 			break;
 		}
 	}
 
-	if (!guess)
-		return GIT_ENOTFOUND;
+	if (!guess) {
+		error = GIT_ENOTFOUND;
+		goto done;
+	}
+
+	error = git_buf_puts(out, guess->name);
 
-	return git_buf_puts(out, guess->name);
+done:
+	git_buf_dispose(&local_default);
+	return error;
 }
 
 int git_remote_upload(git_remote *remote, const git_strarray *refspecs, const git_push_options *opts)
diff --git a/src/repository.c b/src/repository.c
index ebb9daa..a118683 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -70,7 +70,7 @@ static int check_extensions(git_config *config, int version);
 
 #define GIT_FILE_CONTENT_PREFIX "gitdir:"
 
-#define GIT_BRANCH_MASTER "master"
+#define GIT_BRANCH_DEFAULT "master"
 
 #define GIT_REPO_VERSION 0
 #define GIT_REPO_MAX_VERSION 1
@@ -1408,9 +1408,6 @@ int git_repository_create_head(const char *git_dir, const char *ref_name)
 	    (error = git_filebuf_open(&ref, ref_path.ptr, 0, GIT_REFS_FILE_MODE)) < 0)
 		goto out;
 
-	if (!ref_name)
-		ref_name = GIT_BRANCH_MASTER;
-
 	if (git__prefixcmp(ref_name, GIT_REFS_DIR) == 0)
 		fmt = "ref: %s\n";
 	else
@@ -2061,6 +2058,43 @@ static int repo_init_directories(
 	return error;
 }
 
+static int repo_init_head(const char *repo_dir, const char *given)
+{
+	git_config *cfg = NULL;
+	git_buf head_path = GIT_BUF_INIT, cfg_branch = GIT_BUF_INIT;
+	const char *initial_head = NULL;
+	int error;
+
+	if ((error = git_buf_joinpath(&head_path, repo_dir, GIT_HEAD_FILE)) < 0)
+		goto out;
+
+	/*
+	 * A template may have set a HEAD; use that unless it's been
+	 * overridden by the caller's given initial head setting.
+	 */
+	if (git_path_exists(head_path.ptr) && !given)
+		goto out;
+
+	if (given) {
+		initial_head = given;
+	} else if ((error = git_config_open_default(&cfg)) >= 0 &&
+	           (error = git_config_get_string_buf(&cfg_branch, cfg, "init.defaultbranch")) >= 0) {
+		initial_head = cfg_branch.ptr;
+	}
+
+	if (!initial_head)
+		initial_head = GIT_BRANCH_DEFAULT;
+
+	error = git_repository_create_head(repo_dir, initial_head);
+
+out:
+	git_config_free(cfg);
+	git_buf_dispose(&head_path);
+	git_buf_dispose(&cfg_branch);
+
+	return error;
+}
+
 static int repo_init_create_origin(git_repository *repo, const char *url)
 {
 	int error;
@@ -2091,7 +2125,7 @@ int git_repository_init_ext(
 	git_repository_init_options *opts)
 {
 	git_buf repo_path = GIT_BUF_INIT, wd_path = GIT_BUF_INIT,
-		common_path = GIT_BUF_INIT, head_path = GIT_BUF_INIT;
+		common_path = GIT_BUF_INIT;
 	const char *wd;
 	bool is_valid;
 	int error;
@@ -2125,16 +2159,7 @@ int git_repository_init_ext(
 	} else {
 		if ((error = repo_init_structure(repo_path.ptr, wd, opts)) < 0 ||
 		    (error = repo_init_config(repo_path.ptr, wd, opts->flags, opts->mode)) < 0 ||
-		    (error = git_buf_joinpath(&head_path, repo_path.ptr, GIT_HEAD_FILE)) < 0)
-			goto out;
-
-		/*
-		 * Only set the new HEAD if the file does not exist already via
-		 * a template or if the caller has explicitly supplied an
-		 * initial HEAD value.
-		 */
-		if ((!git_path_exists(head_path.ptr) || opts->initial_head) &&
-		    (error = git_repository_create_head(repo_path.ptr, opts->initial_head)) < 0)
+		    (error = repo_init_head(repo_path.ptr, opts->initial_head)) < 0)
 			goto out;
 	}
 
@@ -2146,7 +2171,6 @@ int git_repository_init_ext(
 		goto out;
 
 out:
-	git_buf_dispose(&head_path);
 	git_buf_dispose(&common_path);
 	git_buf_dispose(&repo_path);
 	git_buf_dispose(&wd_path);
@@ -2330,23 +2354,59 @@ static int repo_contains_no_reference(git_repository *repo)
 	return error;
 }
 
+int git_repository_initialbranch(git_buf *out, git_repository *repo)
+{
+	git_config *config;
+	git_config_entry *entry = NULL;
+	const char *branch;
+	int error;
+
+	if ((error = git_repository_config__weakptr(&config, repo)) < 0)
+		return error;
+
+	if ((error = git_config_get_entry(&entry, config, "init.defaultbranch")) == 0) {
+		branch = entry->value;
+	}
+	else if (error == GIT_ENOTFOUND) {
+		branch = GIT_BRANCH_DEFAULT;
+	}
+	else {
+		goto done;
+	}
+
+	if ((error = git_buf_puts(out, GIT_REFS_HEADS_DIR)) < 0 ||
+	    (error = git_buf_puts(out, branch)) < 0)
+	    goto done;
+
+	if (!git_reference_is_valid_name(out->ptr)) {
+		git_error_set(GIT_ERROR_INVALID, "the value of init.defaultBranch is not a valid reference name");
+		error = -1;
+	}
+
+done:
+	git_config_entry_free(entry);
+	return error;
+}
+
 int git_repository_is_empty(git_repository *repo)
 {
 	git_reference *head = NULL;
-	int is_empty = 0;
+	git_buf initialbranch = GIT_BUF_INIT;
+	int result = 0;
 
-	if (git_reference_lookup(&head, repo, GIT_HEAD_FILE) < 0)
-		return -1;
+	if ((result = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0 ||
+	    (result = git_repository_initialbranch(&initialbranch, repo)) < 0)
+		goto done;
 
-	if (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC)
-		is_empty =
-			(strcmp(git_reference_symbolic_target(head),
-					GIT_REFS_HEADS_DIR "master") == 0) &&
-			repo_contains_no_reference(repo);
+	result = (git_reference_type(head) == GIT_REFERENCE_SYMBOLIC &&
+	          strcmp(git_reference_symbolic_target(head), initialbranch.ptr) == 0 &&
+	          repo_contains_no_reference(repo));
 
+done:
 	git_reference_free(head);
+	git_buf_dispose(&initialbranch);
 
-	return is_empty;
+	return result;
 }
 
 static const char *resolved_parent_path(const git_repository *repo, git_repository_item_t item, git_repository_item_t fallback)
diff --git a/src/repository.h b/src/repository.h
index d73e77d..de009ba 100644
--- a/src/repository.h
+++ b/src/repository.h
@@ -232,4 +232,10 @@ extern size_t git_repository__reserved_names_posix_len;
 bool git_repository__reserved_names(
 	git_buf **out, size_t *outlen, git_repository *repo, bool include_ntfs);
 
+/*
+ * The default branch for the repository; the `init.defaultBranch`
+ * configuration option, if set, or `master` if it is not.
+ */
+int git_repository_initialbranch(git_buf *out, git_repository *repo);
+
 #endif
diff --git a/tests/clone/empty.c b/tests/clone/empty.c
index 8e59d2b..94847bc 100644
--- a/tests/clone/empty.c
+++ b/tests/clone/empty.c
@@ -2,6 +2,7 @@
 
 #include "git2/clone.h"
 #include "repository.h"
+#include "repo/repo_helpers.h"
 
 static git_clone_options g_options;
 static git_repository *g_repo;
@@ -22,6 +23,7 @@ void test_clone_empty__initialize(void)
 
 void test_clone_empty__cleanup(void)
 {
+	cl_fixture_cleanup("tmp_global_path");
 	cl_git_sandbox_cleanup();
 }
 
@@ -66,6 +68,21 @@ void test_clone_empty__can_clone_an_empty_local_repo_barely(void)
 		expected_tracked_branch_name));
 }
 
+void test_clone_empty__respects_initialbranch_config(void)
+{
+	git_buf buf = GIT_BUF_INIT;
+
+	create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch");
+
+	cl_set_cleanup(&cleanup_repository, "./empty");
+
+	g_options.bare = true;
+	cl_git_pass(git_clone(&g_repo_cloned, "./empty_bare.git", "./empty", &g_options));
+	cl_git_pass(git_branch_upstream_name(&buf, g_repo_cloned, "refs/heads/my_default_branch"));
+	cl_assert_equal_s("refs/remotes/origin/my_default_branch", buf.ptr);
+	git_buf_dispose(&buf);
+}
+
 void test_clone_empty__can_clone_an_empty_local_repo(void)
 {
 	cl_set_cleanup(&cleanup_repository, "./empty");
diff --git a/tests/repo/init.c b/tests/repo/init.c
index 5a95229..ba00d29 100644
--- a/tests/repo/init.c
+++ b/tests/repo/init.c
@@ -665,3 +665,19 @@ void test_repo_init__unwriteable_directory(void)
 	clar__skip();
 #endif
 }
+
+void test_repo_init__defaultbranch_config(void)
+{
+	git_reference *head;
+
+	cl_set_cleanup(&cleanup_repository, "repo");
+
+	create_tmp_global_config("tmp_global_path", "init.defaultbranch", "my_default_branch");
+
+	cl_git_pass(git_repository_init(&_repo, "repo", 0));
+	cl_git_pass(git_reference_lookup(&head, _repo, "HEAD"));
+
+	cl_assert_equal_s("refs/heads/my_default_branch", git_reference_symbolic_target(head));
+
+	git_reference_free(head);
+}