| 594 | } |
| 595 | |
| 596 | void create_branch(struct repository *r, |
| 597 | const char *name, const char *start_name, |
| 598 | int force, int clobber_head_ok, int reflog, |
| 599 | int quiet, enum branch_track track, int dry_run) |
| 600 | { |
| 601 | struct object_id oid; |
| 602 | char *real_ref; |
| 603 | struct strbuf ref = STRBUF_INIT; |
| 604 | int forcing = 0; |
| 605 | struct ref_transaction *transaction; |
| 606 | struct strbuf err = STRBUF_INIT; |
| 607 | int flags = 0; |
| 608 | char *msg; |
| 609 | |
| 610 | if (track == BRANCH_TRACK_OVERRIDE) |
| 611 | BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?"); |
| 612 | if (clobber_head_ok && !force) |
| 613 | BUG("'clobber_head_ok' can only be used with 'force'"); |
| 614 | |
| 615 | if (clobber_head_ok ? |
| 616 | validate_branchname(name, &ref) : |
| 617 | validate_new_branchname(name, &ref, force)) { |
| 618 | forcing = 1; |
| 619 | } |
| 620 | |
| 621 | dwim_branch_start(r, start_name, track, &real_ref, &oid); |
| 622 | if (dry_run) |
| 623 | goto cleanup; |
| 624 | |
| 625 | if (reflog) |
| 626 | flags |= REF_FORCE_CREATE_REFLOG; |
| 627 | |
| 628 | if (forcing) |
| 629 | msg = xstrfmt("branch: Reset to %s", start_name); |
| 630 | else |
| 631 | msg = xstrfmt("branch: Created from %s", start_name); |
| 632 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
| 633 | 0, &err); |
| 634 | if (!transaction || |
| 635 | ref_transaction_update(transaction, ref.buf, |
| 636 | &oid, forcing ? NULL : null_oid(the_hash_algo), |
| 637 | NULL, NULL, flags, msg, &err) || |
| 638 | ref_transaction_commit(transaction, &err)) |
| 639 | die("%s", err.buf); |
| 640 | ref_transaction_free(transaction); |
| 641 | strbuf_release(&err); |
| 642 | free(msg); |
| 643 | |
| 644 | if (real_ref && track) |
| 645 | setup_tracking(ref.buf + 11, real_ref, track, quiet); |
| 646 | |
| 647 | cleanup: |
| 648 | strbuf_release(&ref); |
| 649 | free(real_ref); |
| 650 | } |
| 651 | |
| 652 | void dwim_and_setup_tracking(struct repository *r, const char *new_ref, |
| 653 | const char *orig_ref, enum branch_track track, |
no test coverage detected