]> git.proxmox.com Git - libgit2.git/blob - src/branch.c
e6818a86df5ba162c08a7947348f5629f03a7fa4
[libgit2.git] / src / branch.c
1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "branch.h"
9
10 #include "commit.h"
11 #include "tag.h"
12 #include "config.h"
13 #include "refspec.h"
14 #include "refs.h"
15 #include "remote.h"
16 #include "annotated_commit.h"
17 #include "worktree.h"
18
19 #include "git2/branch.h"
20
21 static int retrieve_branch_reference(
22 git_reference **branch_reference_out,
23 git_repository *repo,
24 const char *branch_name,
25 bool is_remote)
26 {
27 git_reference *branch = NULL;
28 int error = 0;
29 char *prefix;
30 git_buf ref_name = GIT_BUF_INIT;
31
32 prefix = is_remote ? GIT_REFS_REMOTES_DIR : GIT_REFS_HEADS_DIR;
33
34 if ((error = git_buf_joinpath(&ref_name, prefix, branch_name)) < 0)
35 /* OOM */;
36 else if ((error = git_reference_lookup(&branch, repo, ref_name.ptr)) < 0)
37 git_error_set(
38 GIT_ERROR_REFERENCE, "cannot locate %s branch '%s'",
39 is_remote ? "remote-tracking" : "local", branch_name);
40
41 *branch_reference_out = branch; /* will be NULL on error */
42
43 git_buf_dispose(&ref_name);
44 return error;
45 }
46
47 static int not_a_local_branch(const char *reference_name)
48 {
49 git_error_set(
50 GIT_ERROR_INVALID,
51 "reference '%s' is not a local branch.", reference_name);
52 return -1;
53 }
54
55 static int create_branch(
56 git_reference **ref_out,
57 git_repository *repository,
58 const char *branch_name,
59 const git_commit *commit,
60 const char *from,
61 int force)
62 {
63 int is_unmovable_head = 0;
64 git_reference *branch = NULL;
65 git_buf canonical_branch_name = GIT_BUF_INIT,
66 log_message = GIT_BUF_INIT;
67 int error = -1;
68 int bare = git_repository_is_bare(repository);
69
70 GIT_ASSERT_ARG(branch_name);
71 GIT_ASSERT_ARG(commit);
72 GIT_ASSERT_ARG(ref_out);
73 GIT_ASSERT_ARG(git_commit_owner(commit) == repository);
74
75 if (!git__strcmp(branch_name, "HEAD")) {
76 git_error_set(GIT_ERROR_REFERENCE, "'HEAD' is not a valid branch name");
77 error = -1;
78 goto cleanup;
79 }
80
81 if (force && !bare && git_branch_lookup(&branch, repository, branch_name, GIT_BRANCH_LOCAL) == 0) {
82 error = git_branch_is_head(branch);
83 git_reference_free(branch);
84 branch = NULL;
85
86 if (error < 0)
87 goto cleanup;
88
89 is_unmovable_head = error;
90 }
91
92 if (is_unmovable_head && force) {
93 git_error_set(GIT_ERROR_REFERENCE, "cannot force update branch '%s' as it is "
94 "the current HEAD of the repository.", branch_name);
95 error = -1;
96 goto cleanup;
97 }
98
99 if (git_buf_joinpath(&canonical_branch_name, GIT_REFS_HEADS_DIR, branch_name) < 0)
100 goto cleanup;
101
102 if (git_buf_printf(&log_message, "branch: Created from %s", from) < 0)
103 goto cleanup;
104
105 error = git_reference_create(&branch, repository,
106 git_buf_cstr(&canonical_branch_name), git_commit_id(commit), force,
107 git_buf_cstr(&log_message));
108
109 if (!error)
110 *ref_out = branch;
111
112 cleanup:
113 git_buf_dispose(&canonical_branch_name);
114 git_buf_dispose(&log_message);
115 return error;
116 }
117
118 int git_branch_create(
119 git_reference **ref_out,
120 git_repository *repository,
121 const char *branch_name,
122 const git_commit *commit,
123 int force)
124 {
125 return create_branch(ref_out, repository, branch_name, commit, git_oid_tostr_s(git_commit_id(commit)), force);
126 }
127
128 int git_branch_create_from_annotated(
129 git_reference **ref_out,
130 git_repository *repository,
131 const char *branch_name,
132 const git_annotated_commit *commit,
133 int force)
134 {
135 return create_branch(ref_out,
136 repository, branch_name, commit->commit, commit->description, force);
137 }
138
139 static int branch_is_checked_out(git_repository *worktree, void *payload)
140 {
141 git_reference *branch = (git_reference *) payload;
142 git_reference *head = NULL;
143 int error;
144
145 if (git_repository_is_bare(worktree))
146 return 0;
147
148 if ((error = git_reference_lookup(&head, worktree, GIT_HEAD_FILE)) < 0) {
149 if (error == GIT_ENOTFOUND)
150 error = 0;
151 goto out;
152 }
153
154 if (git_reference_type(head) != GIT_REFERENCE_SYMBOLIC)
155 goto out;
156
157 error = !git__strcmp(head->target.symbolic, branch->name);
158
159 out:
160 git_reference_free(head);
161 return error;
162 }
163
164 int git_branch_is_checked_out(const git_reference *branch)
165 {
166 GIT_ASSERT_ARG(branch);
167
168 if (!git_reference_is_branch(branch))
169 return 0;
170 return git_repository_foreach_worktree(git_reference_owner(branch),
171 branch_is_checked_out, (void *)branch) == 1;
172 }
173
174 int git_branch_delete(git_reference *branch)
175 {
176 int is_head;
177 git_buf config_section = GIT_BUF_INIT;
178 int error = -1;
179
180 GIT_ASSERT_ARG(branch);
181
182 if (!git_reference_is_branch(branch) && !git_reference_is_remote(branch)) {
183 git_error_set(GIT_ERROR_INVALID, "reference '%s' is not a valid branch.",
184 git_reference_name(branch));
185 return GIT_ENOTFOUND;
186 }
187
188 if ((is_head = git_branch_is_head(branch)) < 0)
189 return is_head;
190
191 if (is_head) {
192 git_error_set(GIT_ERROR_REFERENCE, "cannot delete branch '%s' as it is "
193 "the current HEAD of the repository.", git_reference_name(branch));
194 return -1;
195 }
196
197 if (git_reference_is_branch(branch) && git_branch_is_checked_out(branch)) {
198 git_error_set(GIT_ERROR_REFERENCE, "Cannot delete branch '%s' as it is "
199 "the current HEAD of a linked repository.", git_reference_name(branch));
200 return -1;
201 }
202
203 if (git_buf_join(&config_section, '.', "branch",
204 git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR)) < 0)
205 goto on_error;
206
207 if (git_config_rename_section(
208 git_reference_owner(branch), git_buf_cstr(&config_section), NULL) < 0)
209 goto on_error;
210
211 error = git_reference_delete(branch);
212
213 on_error:
214 git_buf_dispose(&config_section);
215 return error;
216 }
217
218 typedef struct {
219 git_reference_iterator *iter;
220 unsigned int flags;
221 } branch_iter;
222
223 int git_branch_next(git_reference **out, git_branch_t *out_type, git_branch_iterator *_iter)
224 {
225 branch_iter *iter = (branch_iter *) _iter;
226 git_reference *ref;
227 int error;
228
229 while ((error = git_reference_next(&ref, iter->iter)) == 0) {
230 if ((iter->flags & GIT_BRANCH_LOCAL) &&
231 !git__prefixcmp(ref->name, GIT_REFS_HEADS_DIR)) {
232 *out = ref;
233 *out_type = GIT_BRANCH_LOCAL;
234
235 return 0;
236 } else if ((iter->flags & GIT_BRANCH_REMOTE) &&
237 !git__prefixcmp(ref->name, GIT_REFS_REMOTES_DIR)) {
238 *out = ref;
239 *out_type = GIT_BRANCH_REMOTE;
240
241 return 0;
242 } else {
243 git_reference_free(ref);
244 }
245 }
246
247 return error;
248 }
249
250 int git_branch_iterator_new(
251 git_branch_iterator **out,
252 git_repository *repo,
253 git_branch_t list_flags)
254 {
255 branch_iter *iter;
256
257 iter = git__calloc(1, sizeof(branch_iter));
258 GIT_ERROR_CHECK_ALLOC(iter);
259
260 iter->flags = list_flags;
261
262 if (git_reference_iterator_new(&iter->iter, repo) < 0) {
263 git__free(iter);
264 return -1;
265 }
266
267 *out = (git_branch_iterator *) iter;
268
269 return 0;
270 }
271
272 void git_branch_iterator_free(git_branch_iterator *_iter)
273 {
274 branch_iter *iter = (branch_iter *) _iter;
275
276 if (iter == NULL)
277 return;
278
279 git_reference_iterator_free(iter->iter);
280 git__free(iter);
281 }
282
283 int git_branch_move(
284 git_reference **out,
285 git_reference *branch,
286 const char *new_branch_name,
287 int force)
288 {
289 git_buf new_reference_name = GIT_BUF_INIT,
290 old_config_section = GIT_BUF_INIT,
291 new_config_section = GIT_BUF_INIT,
292 log_message = GIT_BUF_INIT;
293 int error;
294
295 GIT_ASSERT_ARG(branch);
296 GIT_ASSERT_ARG(new_branch_name);
297
298 if (!git_reference_is_branch(branch))
299 return not_a_local_branch(git_reference_name(branch));
300
301 if ((error = git_buf_joinpath(&new_reference_name, GIT_REFS_HEADS_DIR, new_branch_name)) < 0)
302 goto done;
303
304 if ((error = git_buf_printf(&log_message, "branch: renamed %s to %s",
305 git_reference_name(branch), git_buf_cstr(&new_reference_name))) < 0)
306 goto done;
307
308 /* first update ref then config so failure won't trash config */
309
310 error = git_reference_rename(
311 out, branch, git_buf_cstr(&new_reference_name), force,
312 git_buf_cstr(&log_message));
313 if (error < 0)
314 goto done;
315
316 git_buf_join(&old_config_section, '.', "branch",
317 git_reference_name(branch) + strlen(GIT_REFS_HEADS_DIR));
318 git_buf_join(&new_config_section, '.', "branch", new_branch_name);
319
320 error = git_config_rename_section(
321 git_reference_owner(branch),
322 git_buf_cstr(&old_config_section),
323 git_buf_cstr(&new_config_section));
324
325 done:
326 git_buf_dispose(&new_reference_name);
327 git_buf_dispose(&old_config_section);
328 git_buf_dispose(&new_config_section);
329 git_buf_dispose(&log_message);
330
331 return error;
332 }
333
334 int git_branch_lookup(
335 git_reference **ref_out,
336 git_repository *repo,
337 const char *branch_name,
338 git_branch_t branch_type)
339 {
340 int error = -1;
341
342 GIT_ASSERT_ARG(ref_out);
343 GIT_ASSERT_ARG(repo);
344 GIT_ASSERT_ARG(branch_name);
345
346 switch (branch_type) {
347 case GIT_BRANCH_LOCAL:
348 case GIT_BRANCH_REMOTE:
349 error = retrieve_branch_reference(ref_out, repo, branch_name, branch_type == GIT_BRANCH_REMOTE);
350 break;
351 case GIT_BRANCH_ALL:
352 error = retrieve_branch_reference(ref_out, repo, branch_name, false);
353 if (error == GIT_ENOTFOUND)
354 error = retrieve_branch_reference(ref_out, repo, branch_name, true);
355 break;
356 default:
357 GIT_ASSERT(false);
358 }
359 return error;
360 }
361
362 int git_branch_name(
363 const char **out,
364 const git_reference *ref)
365 {
366 const char *branch_name;
367
368 GIT_ASSERT_ARG(out);
369 GIT_ASSERT_ARG(ref);
370
371 branch_name = ref->name;
372
373 if (git_reference_is_branch(ref)) {
374 branch_name += strlen(GIT_REFS_HEADS_DIR);
375 } else if (git_reference_is_remote(ref)) {
376 branch_name += strlen(GIT_REFS_REMOTES_DIR);
377 } else {
378 git_error_set(GIT_ERROR_INVALID,
379 "reference '%s' is neither a local nor a remote branch.", ref->name);
380 return -1;
381 }
382 *out = branch_name;
383 return 0;
384 }
385
386 static int retrieve_upstream_configuration(
387 git_buf *out,
388 const git_config *config,
389 const char *canonical_branch_name,
390 const char *format)
391 {
392 git_buf buf = GIT_BUF_INIT;
393 int error;
394
395 if (git_buf_printf(&buf, format,
396 canonical_branch_name + strlen(GIT_REFS_HEADS_DIR)) < 0)
397 return -1;
398
399 error = git_config_get_string_buf(out, config, git_buf_cstr(&buf));
400 git_buf_dispose(&buf);
401 return error;
402 }
403
404 int git_branch_upstream_name(
405 git_buf *out,
406 git_repository *repo,
407 const char *refname)
408 {
409 git_buf remote_name = GIT_BUF_INIT;
410 git_buf merge_name = GIT_BUF_INIT;
411 git_buf buf = GIT_BUF_INIT;
412 int error = -1;
413 git_remote *remote = NULL;
414 const git_refspec *refspec;
415 git_config *config;
416
417 GIT_ASSERT_ARG(out);
418 GIT_ASSERT_ARG(refname);
419
420 if ((error = git_buf_sanitize(out)) < 0)
421 return error;
422
423 if (!git_reference__is_branch(refname))
424 return not_a_local_branch(refname);
425
426 if ((error = git_repository_config_snapshot(&config, repo)) < 0)
427 return error;
428
429 if ((error = retrieve_upstream_configuration(
430 &remote_name, config, refname, "branch.%s.remote")) < 0)
431 goto cleanup;
432
433 if ((error = retrieve_upstream_configuration(
434 &merge_name, config, refname, "branch.%s.merge")) < 0)
435 goto cleanup;
436
437 if (git_buf_len(&remote_name) == 0 || git_buf_len(&merge_name) == 0) {
438 git_error_set(GIT_ERROR_REFERENCE,
439 "branch '%s' does not have an upstream", refname);
440 error = GIT_ENOTFOUND;
441 goto cleanup;
442 }
443
444 if (strcmp(".", git_buf_cstr(&remote_name)) != 0) {
445 if ((error = git_remote_lookup(&remote, repo, git_buf_cstr(&remote_name))) < 0)
446 goto cleanup;
447
448 refspec = git_remote__matching_refspec(remote, git_buf_cstr(&merge_name));
449 if (!refspec) {
450 error = GIT_ENOTFOUND;
451 goto cleanup;
452 }
453
454 if (git_refspec_transform(&buf, refspec, git_buf_cstr(&merge_name)) < 0)
455 goto cleanup;
456 } else
457 if (git_buf_set(&buf, git_buf_cstr(&merge_name), git_buf_len(&merge_name)) < 0)
458 goto cleanup;
459
460 error = git_buf_set(out, git_buf_cstr(&buf), git_buf_len(&buf));
461
462 cleanup:
463 git_config_free(config);
464 git_remote_free(remote);
465 git_buf_dispose(&remote_name);
466 git_buf_dispose(&merge_name);
467 git_buf_dispose(&buf);
468 return error;
469 }
470
471 static int git_branch_upstream_with_format(git_buf *buf, git_repository *repo, const char *refname, const char *format, const char *format_name)
472 {
473 int error;
474 git_config *cfg;
475
476 if (!git_reference__is_branch(refname))
477 return not_a_local_branch(refname);
478
479 if ((error = git_repository_config__weakptr(&cfg, repo)) < 0)
480 return error;
481
482 if ((error = git_buf_sanitize(buf)) < 0 ||
483 (error = retrieve_upstream_configuration(buf, cfg, refname, format)) < 0)
484 return error;
485
486 if (git_buf_len(buf) == 0) {
487 git_error_set(GIT_ERROR_REFERENCE, "branch '%s' does not have an upstream %s", refname, format_name);
488 error = GIT_ENOTFOUND;
489 git_buf_clear(buf);
490 }
491
492 return error;
493 }
494
495 int git_branch_upstream_remote(git_buf *buf, git_repository *repo, const char *refname)
496 {
497 return git_branch_upstream_with_format(buf, repo, refname, "branch.%s.remote", "remote");
498 }
499
500 int git_branch_upstream_merge(git_buf *buf, git_repository *repo, const char *refname)
501 {
502 return git_branch_upstream_with_format(buf, repo, refname, "branch.%s.merge", "merge");
503 }
504
505 int git_branch_remote_name(git_buf *buf, git_repository *repo, const char *refname)
506 {
507 git_strarray remote_list = {0};
508 size_t i;
509 git_remote *remote;
510 const git_refspec *fetchspec;
511 int error = 0;
512 char *remote_name = NULL;
513
514 GIT_ASSERT_ARG(buf);
515 GIT_ASSERT_ARG(repo);
516 GIT_ASSERT_ARG(refname);
517
518 if ((error = git_buf_sanitize(buf)) < 0)
519 return error;
520
521 /* Verify that this is a remote branch */
522 if (!git_reference__is_remote(refname)) {
523 git_error_set(GIT_ERROR_INVALID, "reference '%s' is not a remote branch.",
524 refname);
525 error = GIT_ERROR;
526 goto cleanup;
527 }
528
529 /* Get the remotes */
530 if ((error = git_remote_list(&remote_list, repo)) < 0)
531 goto cleanup;
532
533 /* Find matching remotes */
534 for (i = 0; i < remote_list.count; i++) {
535 if ((error = git_remote_lookup(&remote, repo, remote_list.strings[i])) < 0)
536 continue;
537
538 fetchspec = git_remote__matching_dst_refspec(remote, refname);
539 if (fetchspec) {
540 /* If we have not already set out yet, then set
541 * it to the matching remote name. Otherwise
542 * multiple remotes match this reference, and it
543 * is ambiguous. */
544 if (!remote_name) {
545 remote_name = remote_list.strings[i];
546 } else {
547 git_remote_free(remote);
548
549 git_error_set(GIT_ERROR_REFERENCE,
550 "reference '%s' is ambiguous", refname);
551 error = GIT_EAMBIGUOUS;
552 goto cleanup;
553 }
554 }
555
556 git_remote_free(remote);
557 }
558
559 if (remote_name) {
560 git_buf_clear(buf);
561 error = git_buf_puts(buf, remote_name);
562 } else {
563 git_error_set(GIT_ERROR_REFERENCE,
564 "could not determine remote for '%s'", refname);
565 error = GIT_ENOTFOUND;
566 }
567
568 cleanup:
569 if (error < 0)
570 git_buf_dispose(buf);
571
572 git_strarray_dispose(&remote_list);
573 return error;
574 }
575
576 int git_branch_upstream(
577 git_reference **tracking_out,
578 const git_reference *branch)
579 {
580 int error;
581 git_buf tracking_name = GIT_BUF_INIT;
582
583 if ((error = git_branch_upstream_name(&tracking_name,
584 git_reference_owner(branch), git_reference_name(branch))) < 0)
585 return error;
586
587 error = git_reference_lookup(
588 tracking_out,
589 git_reference_owner(branch),
590 git_buf_cstr(&tracking_name));
591
592 git_buf_dispose(&tracking_name);
593 return error;
594 }
595
596 static int unset_upstream(git_config *config, const char *shortname)
597 {
598 git_buf buf = GIT_BUF_INIT;
599
600 if (git_buf_printf(&buf, "branch.%s.remote", shortname) < 0)
601 return -1;
602
603 if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
604 goto on_error;
605
606 git_buf_clear(&buf);
607 if (git_buf_printf(&buf, "branch.%s.merge", shortname) < 0)
608 goto on_error;
609
610 if (git_config_delete_entry(config, git_buf_cstr(&buf)) < 0)
611 goto on_error;
612
613 git_buf_dispose(&buf);
614 return 0;
615
616 on_error:
617 git_buf_dispose(&buf);
618 return -1;
619 }
620
621 int git_branch_set_upstream(git_reference *branch, const char *branch_name)
622 {
623 git_buf key = GIT_BUF_INIT, remote_name = GIT_BUF_INIT, merge_refspec = GIT_BUF_INIT;
624 git_reference *upstream;
625 git_repository *repo;
626 git_remote *remote = NULL;
627 git_config *config;
628 const char *refname, *shortname;
629 int local, error;
630 const git_refspec *fetchspec;
631
632 refname = git_reference_name(branch);
633 if (!git_reference__is_branch(refname))
634 return not_a_local_branch(refname);
635
636 if (git_repository_config__weakptr(&config, git_reference_owner(branch)) < 0)
637 return -1;
638
639 shortname = refname + strlen(GIT_REFS_HEADS_DIR);
640
641 /* We're unsetting, delegate and bail-out */
642 if (branch_name == NULL)
643 return unset_upstream(config, shortname);
644
645 repo = git_reference_owner(branch);
646
647 /* First we need to resolve name to a branch */
648 if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_LOCAL) == 0)
649 local = 1;
650 else if (git_branch_lookup(&upstream, repo, branch_name, GIT_BRANCH_REMOTE) == 0)
651 local = 0;
652 else {
653 git_error_set(GIT_ERROR_REFERENCE,
654 "cannot set upstream for branch '%s'", shortname);
655 return GIT_ENOTFOUND;
656 }
657
658 /*
659 * If it's a local-tracking branch, its remote is "." (as "the local
660 * repository"), and the branch name is simply the refname.
661 * Otherwise we need to figure out what the remote-tracking branch's
662 * name on the remote is and use that.
663 */
664 if (local)
665 error = git_buf_puts(&remote_name, ".");
666 else
667 error = git_branch_remote_name(&remote_name, repo, git_reference_name(upstream));
668
669 if (error < 0)
670 goto on_error;
671
672 /* Update the upsteam branch config with the new name */
673 if (git_buf_printf(&key, "branch.%s.remote", shortname) < 0)
674 goto on_error;
675
676 if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&remote_name)) < 0)
677 goto on_error;
678
679 if (local) {
680 /* A local branch uses the upstream refname directly */
681 if (git_buf_puts(&merge_refspec, git_reference_name(upstream)) < 0)
682 goto on_error;
683 } else {
684 /* We transform the upstream branch name according to the remote's refspecs */
685 if (git_remote_lookup(&remote, repo, git_buf_cstr(&remote_name)) < 0)
686 goto on_error;
687
688 fetchspec = git_remote__matching_dst_refspec(remote, git_reference_name(upstream));
689 if (!fetchspec || git_refspec_rtransform(&merge_refspec, fetchspec, git_reference_name(upstream)) < 0)
690 goto on_error;
691
692 git_remote_free(remote);
693 remote = NULL;
694 }
695
696 /* Update the merge branch config with the refspec */
697 git_buf_clear(&key);
698 if (git_buf_printf(&key, "branch.%s.merge", shortname) < 0)
699 goto on_error;
700
701 if (git_config_set_string(config, git_buf_cstr(&key), git_buf_cstr(&merge_refspec)) < 0)
702 goto on_error;
703
704 git_reference_free(upstream);
705 git_buf_dispose(&key);
706 git_buf_dispose(&remote_name);
707 git_buf_dispose(&merge_refspec);
708
709 return 0;
710
711 on_error:
712 git_reference_free(upstream);
713 git_buf_dispose(&key);
714 git_buf_dispose(&remote_name);
715 git_buf_dispose(&merge_refspec);
716 git_remote_free(remote);
717
718 return -1;
719 }
720
721 int git_branch_is_head(
722 const git_reference *branch)
723 {
724 git_reference *head;
725 bool is_same = false;
726 int error;
727
728 GIT_ASSERT_ARG(branch);
729
730 if (!git_reference_is_branch(branch))
731 return false;
732
733 error = git_repository_head(&head, git_reference_owner(branch));
734
735 if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND)
736 return false;
737
738 if (error < 0)
739 return -1;
740
741 is_same = strcmp(
742 git_reference_name(branch),
743 git_reference_name(head)) == 0;
744
745 git_reference_free(head);
746
747 return is_same;
748 }
749
750 int git_branch_name_is_valid(int *valid, const char *name)
751 {
752 git_buf ref_name = GIT_BUF_INIT;
753 int error = 0;
754
755 GIT_ASSERT(valid);
756
757 *valid = 0;
758
759 /*
760 * Discourage branch name starting with dash,
761 * https://github.com/git/git/commit/6348624010888b
762 * and discourage HEAD as branch name,
763 * https://github.com/git/git/commit/a625b092cc5994
764 */
765 if (!name || name[0] == '-' || !git__strcmp(name, "HEAD"))
766 goto done;
767
768 if ((error = git_buf_puts(&ref_name, GIT_REFS_HEADS_DIR)) < 0 ||
769 (error = git_buf_puts(&ref_name, name)) < 0)
770 goto done;
771
772 error = git_reference_name_is_valid(valid, ref_name.ptr);
773
774 done:
775 git_buf_dispose(&ref_name);
776 return error;
777 }