| 1873 | } |
| 1874 | |
| 1875 | int refs_for_each_ref_ext(struct ref_store *refs, |
| 1876 | refs_for_each_cb cb, void *cb_data, |
| 1877 | const struct refs_for_each_ref_options *opts) |
| 1878 | { |
| 1879 | struct strvec namespaced_exclude_patterns = STRVEC_INIT; |
| 1880 | struct strbuf namespaced_prefix = STRBUF_INIT; |
| 1881 | struct strbuf real_pattern = STRBUF_INIT; |
| 1882 | struct for_each_ref_filter filter; |
| 1883 | struct ref_iterator *iter; |
| 1884 | size_t trim_prefix = opts->trim_prefix; |
| 1885 | const char **exclude_patterns; |
| 1886 | const char *prefix; |
| 1887 | int ret; |
| 1888 | |
| 1889 | if (!refs) |
| 1890 | BUG("no ref store passed"); |
| 1891 | |
| 1892 | if (opts->trim_prefix) { |
| 1893 | size_t prefix_len; |
| 1894 | |
| 1895 | if (!opts->prefix) |
| 1896 | BUG("trimming only allowed with a prefix"); |
| 1897 | |
| 1898 | prefix_len = strlen(opts->prefix); |
| 1899 | if (prefix_len == opts->trim_prefix && opts->prefix[prefix_len - 1] != '/') |
| 1900 | BUG("ref pattern must end in a trailing slash when trimming"); |
| 1901 | } |
| 1902 | |
| 1903 | if (opts->pattern) { |
| 1904 | if (!opts->prefix && !starts_with(opts->pattern, "refs/")) |
| 1905 | strbuf_addstr(&real_pattern, "refs/"); |
| 1906 | else if (opts->prefix) |
| 1907 | strbuf_addstr(&real_pattern, opts->prefix); |
| 1908 | strbuf_addstr(&real_pattern, opts->pattern); |
| 1909 | |
| 1910 | if (!has_glob_specials(opts->pattern)) { |
| 1911 | /* Append implied '/' '*' if not present. */ |
| 1912 | strbuf_complete(&real_pattern, '/'); |
| 1913 | /* No need to check for '*', there is none. */ |
| 1914 | strbuf_addch(&real_pattern, '*'); |
| 1915 | } |
| 1916 | |
| 1917 | filter.pattern = real_pattern.buf; |
| 1918 | filter.trim_prefix = opts->trim_prefix; |
| 1919 | filter.fn = cb; |
| 1920 | filter.cb_data = cb_data; |
| 1921 | |
| 1922 | /* |
| 1923 | * We need to trim the prefix in the callback function as the |
| 1924 | * pattern is expected to match on the full refname. |
| 1925 | */ |
| 1926 | trim_prefix = 0; |
| 1927 | |
| 1928 | cb = for_each_filter_refs; |
| 1929 | cb_data = &filter; |
| 1930 | } |
| 1931 | |
| 1932 | if (opts->namespace) { |