+# **** Useful sanity checks ****
+
+# ---- UNRELEASED suite
+
+if [ "$target" = "UNRELEASED" ]; then
+ fail_check unreleased "UNRELEASED changelog"
+fi
+
+# ---- Pushing dgit view to maintainer view
+
+if ! [ "x$last_debian_tag" = "x" ] && ! [ "x$last_archive_tag" = "x" ]; then
+ last_debian_tag_c=$(git rev-parse "$last_debian_tag"^{})
+ last_archive_tag_c=$(git rev-parse "$last_archive_tag"^{})
+ if ! [ "$last_debian_tag_c" = "$last_archive_tag_c" ] \
+ && git merge-base --is-ancestor \
+ "$last_debian_tag" "$last_archive_tag"; then
+ fail_check dgit-view \
+"looks like you might be trying to push the dgit view to the maintainer branch?"
+ fi
+fi
+
+# ---- Targeting different suite
+
+if ! [ "x$last_debian_tag" = "x" ]; then
+ temp=$(mktemp -d)
+ trap cleanup EXIT
+ mkdir "$temp/debian"
+ git cat-file blob "$last_debian_tag":debian/changelog >"$temp/debian/changelog"
+ prev_target=$(cd $temp; dpkg-parsechangelog -SDistribution)
+ rm -rf "$temp"
+ trap - EXIT
+
+ if ! [ "$prev_target" = "$target" ] && ! [ "$target" = "UNRELEASED" ]; then
+ fail_check suite \
+"last upload targeted $prev_target, now targeting $target; might be a mistake?"
+ fi
+fi
+
+# ---- Upstream tag is not ancestor of $branch
+
+if ! [ "x$upstream_tag" = "x" ] \
+ && ! git merge-base --is-ancestor "$upstream_tag" "$branch" \
+ && ! [ "$quilt_mode" = "baredebian" ]; then
+ fail_check upstream-nonancestor \
+ "upstream tag $upstream_tag is not an ancestor of $branch; probably a mistake"
+fi
+
+# ---- Quilt mode-specific checks
+
+case "$quilt_mode" in
+ gbp)
+ check_treesame "$upstream_tag" "$branch" ':!debian' ':!**.gitignore' \
+ || fail_check_upstream_nonidentical
+ check_patches_apply false
+ ;;
+ unapplied)
+ check_treesame "$upstream_tag" "$branch" ':!debian' \
+ || fail_check_upstream_nonidentical
+ check_patches_apply false
+ ;;
+ baredebian)
+ check_patches_apply false
+ ;;
+ dpm|nofix)
+ check_patches_apply true
+ ;;
+esac
+
+# ---- git-debrebase branch format checks
+
+# only check branches, since you can't run `git debrebase conclude` on
+# non-branches
+case "$branch" in
+ refs/heads/*)
+ # see "STITCHING, PSEUDO-MERGES, FFQ RECORD" in git-debrebase(5)
+ ffq_prev_ref="refs/ffq-prev/${branch#refs/}"
+ if git show-ref --quiet --verify "$ffq_prev_ref"; then
+ fail_check unstitched \
+ "this looks like an unstitched git-debrebase branch, which should not be pushed"
+ fi
+esac
+
+# ---- Summary
+
+if $failed_check; then
+ # We don't mention the --force=check options here as those are
+ # mainly for use by scripts, or when you already know what check
+ # is going to fail before you invoke git-debpush. Keep the
+ # script's terminal output as simple as possible. No "see the
+ # manpage"!
+ fail "some check(s) failed; you can pass --force to ignore them"
+fi
+
+# **** Create the git tag ****
+