+sub upstream_commitish_search ($$) {
+ my ($upstream_version, $tried) = @_;
+ # todo: at some point maybe use git-deborig to do this
+ foreach my $tagpfx ('', 'v', 'upstream/') {
+ my $tag = $tagpfx.(dep14_version_mangle $upstream_version);
+ my $new_upstream = git_get_ref "refs/tags/$tag";
+ push @$tried, $tag;
+ return $new_upstream if length $new_upstream;
+ }
+}
+
+sub resolve_upstream_version ($$) {
+ my ($new_upstream, $upstream_version) = @_;
+
+ my $used = $new_upstream;
+ my $message = __ 'using specified upstream commitish';
+ if (!defined $new_upstream) {
+ my @tried;
+ $new_upstream = upstream_commitish_search $upstream_version, \@tried;
+ if (!length $new_upstream) {
+ fail f_
+ "Could not determine appropriate upstream commitish.\n".
+ " (Tried these tags: %s)\n".
+ " Check version, and specify upstream commitish explicitly.",
+ "@tried";
+ }
+ $used = $tried[-1];
+ $message = f_ 'using upstream from git tag %s', $used;
+ } elsif ($new_upstream =~ m{^refs/tags/($versiontag_re)$}s) {
+ $message = f_ 'using upstream from git tag %s', $1;
+ $used = $1;
+ }
+ $new_upstream = git_rev_parse $new_upstream;
+
+ return ($new_upstream, $used, $message);
+ # used is a human-readable idea of what we found
+}
+