| 1700 | }; |
| 1701 | |
| 1702 | static struct commit *get_base_commit(const struct format_config *cfg, |
| 1703 | struct commit **list, |
| 1704 | size_t total) |
| 1705 | { |
| 1706 | struct commit *base = NULL; |
| 1707 | struct commit **rev; |
| 1708 | int auto_select, die_on_failure, ret; |
| 1709 | size_t i = 0, rev_nr = 0; |
| 1710 | |
| 1711 | switch (cfg->auto_base) { |
| 1712 | case AUTO_BASE_NEVER: |
| 1713 | if (cfg->base_commit) { |
| 1714 | auto_select = 0; |
| 1715 | die_on_failure = 1; |
| 1716 | } else { |
| 1717 | /* no base information is requested */ |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | break; |
| 1721 | case AUTO_BASE_ALWAYS: |
| 1722 | case AUTO_BASE_WHEN_ABLE: |
| 1723 | if (cfg->base_commit) { |
| 1724 | BUG("requested automatic base selection but a commit was provided"); |
| 1725 | } else { |
| 1726 | auto_select = 1; |
| 1727 | die_on_failure = cfg->auto_base == AUTO_BASE_ALWAYS; |
| 1728 | } |
| 1729 | break; |
| 1730 | default: |
| 1731 | BUG("unexpected automatic base selection method"); |
| 1732 | } |
| 1733 | |
| 1734 | if (!auto_select) { |
| 1735 | base = lookup_commit_reference_by_name(cfg->base_commit); |
| 1736 | if (!base) |
| 1737 | die(_("unknown commit %s"), cfg->base_commit); |
| 1738 | } else { |
| 1739 | struct branch *curr_branch = branch_get(NULL); |
| 1740 | const char *upstream = branch_get_upstream(curr_branch, NULL); |
| 1741 | if (upstream) { |
| 1742 | struct commit_list *base_list = NULL; |
| 1743 | struct commit *commit; |
| 1744 | struct object_id oid; |
| 1745 | |
| 1746 | if (repo_get_oid(the_repository, upstream, &oid)) { |
| 1747 | if (die_on_failure) |
| 1748 | die(_("failed to resolve '%s' as a valid ref"), upstream); |
| 1749 | else |
| 1750 | return NULL; |
| 1751 | } |
| 1752 | commit = lookup_commit_or_die(&oid, "upstream base"); |
| 1753 | if (repo_get_merge_bases_many(the_repository, |
| 1754 | commit, total, |
| 1755 | list, |
| 1756 | &base_list) < 0 || |
| 1757 | /* There should be one and only one merge base. */ |
| 1758 | !base_list || base_list->next) { |
| 1759 | if (die_on_failure) { |
no test coverage detected