+sub ffq_prev_branchinfo () {
+ # => ('status', "message", [$current, $ffq_prev])
+ # 'status' may be
+ # branch message is undef
+ # weird-symref } no $current,
+ # notbranch } no $ffq_prev
+ my $current = git_get_symref();
+ return ('detached', 'detached HEAD') unless defined $current;
+ return ('weird-symref', 'HEAD symref is not to refs/')
+ unless $current =~ m{^refs/};
+ my $ffq_prev = "refs/$ffq_refprefix/$'";
+ printdebug "ffq_prev_branchinfo branch current $current\n";
+ return ('branch', undef, $current, $ffq_prev);
+}
+
+sub record_ffq_prev_deferred () {
+ # => ('status', "message")
+ # 'status' may be
+ # deferred message is undef
+ # exists
+ # detached
+ # weird-symref
+ # notbranch
+ # if not ff from some branch we should be ff from, is an fproblem
+ # if "deferred", will have added something about that to
+ # @deferred_update_messages, and also maybe printed (already)
+ # some messages about ff checks
+ my ($status, $message, $current, $ffq_prev) = ffq_prev_branchinfo();
+ return ($status, $message) unless $status eq 'branch';
+
+ my $currentval = get_head();
+
+ my $exists = git_get_ref $ffq_prev;
+ return ('exists',"$ffq_prev already exists") if $exists;
+
+ return ('not-branch', 'HEAD symref is not to refs/heads/')
+ unless $current =~ m{^refs/heads/};
+ my $branch = $';
+
+ my @check_specs = split /\;/, (cfg "branch.$branch.ffq-ffrefs",1) // '*';
+ my %checked;
+
+ printdebug "ffq check_specs @check_specs\n";
+
+ my $check = sub {
+ my ($lrref, $desc) = @_;
+ printdebug "ffq might check $lrref ($desc)\n";
+ my $invert;
+ for my $chk (@check_specs) {
+ my $glob = $chk;
+ $invert = $glob =~ s{^[!^]}{};
+ last if fnmatch $glob, $lrref;
+ }
+ return if $invert;
+ my $lrval = git_get_ref $lrref;
+ return unless defined $lrval;
+
+ if (is_fast_fwd $lrval, $currentval) {
+ print "OK, you are ahead of $lrref\n" or die $!;
+ $checked{$lrref} = 1;
+ } elsif (is_fast_fwd $currentval, $lrval) {
+ $checked{$lrref} = -1;
+ fproblem 'behind', "you are behind $lrref, divergence risk";
+ } else {
+ $checked{$lrref} = -1;
+ fproblem 'diverged', "you have diverged from $lrref";
+ }
+ };
+
+ my $merge = cfg "branch.$branch.merge",1;
+ if (defined $merge and $merge =~ m{^refs/heads/}) {
+ my $rhs = $';
+ printdebug "ffq merge $rhs\n";
+ my $check_remote = sub {
+ my ($remote, $desc) = @_;
+ printdebug "ffq check_remote ".($remote//'undef')." $desc\n";
+ return unless defined $remote;
+ $check->("refs/remotes/$remote/$rhs", $desc);
+ };
+ $check_remote->((scalar cfg "branch.$branch.remote",1),
+ 'remote fetch/merge branch');
+ $check_remote->((scalar cfg "branch.$branch.pushRemote",1) //
+ (scalar cfg "branch.$branch.pushDefault",1),
+ 'remote push branch');
+ }
+ if ($branch =~ m{^dgit/}) {
+ $check->("refs/remotes/dgit/$branch", 'remote dgit branch');
+ } elsif ($branch =~ m{^master$}) {
+ $check->("refs/remotes/dgit/dgit/sid", 'remote dgit branch for sid');
+ }
+
+ fproblems_maybe_bail();
+
+ push @deferred_updates, "update $ffq_prev $currentval $git_null_obj";
+ push @deferred_update_messages, "Recorded current head for preservation";
+ return ('deferred', undef);
+}
+
+sub record_ffq_auto () {
+ my ($status, $message) = record_ffq_prev_deferred();
+ if ($status eq 'deferred' || $status eq 'exists') {
+ } else {
+ fproblem $status, "could not record ffq-prev: $message";
+ fproblems_maybe_bail();
+ }
+}
+