Exemple #1
0
def deleteBranch(db, user, repository, name, old):
    try:
        update(repository.path, "refs/heads/" + name, old, None)
    except Reject as rejected:
        raise IndexException(str(rejected))
    except Exception:
        pass

    branch = dbutils.Branch.fromName(db, repository, name)

    if branch:
        review = dbutils.Review.fromBranch(db, branch)

        if review:
            raise IndexException("This is Critic refusing to delete a branch that belongs to a review.")

        cursor = db.cursor()
        cursor.execute("SELECT COUNT(*) FROM reachable WHERE branch=%s", (branch.id,))

        ncommits = cursor.fetchone()[0]

        if branch.base:
            cursor.execute("UPDATE branches SET base=%s WHERE base=%s", (branch.base.id, branch.id))

        cursor.execute("DELETE FROM branches WHERE id=%s", (branch.id,))

        # Suppress the "user friendly" feedback if the push is performed by the
        # Critic system user, since there wouldn't be a human being reading it.
        if not user.isSystem():
            print "Deleted branch containing %d commit%s." % (ncommits, "s" if ncommits > 1 else "")
Exemple #2
0
def createBranch(db, user, repository, name, head, multiple, flags):
    try:
        update(repository.path, "refs/heads/" + name, None, head)
    except Reject as rejected:
        raise IndexException(str(rejected))
    except Exception:
        pass

    cursor = db.cursor()

    # Check if a branch with this name already "exists".
    branch = dbutils.Branch.fromName(db, repository, name, load_review=True)
    if branch is not None:
        if branch.archived:
            # This is a (review) branch that has been archived.  It's expected
            # that Git thinks the user is creating a new branch.
            message = """\
This repository already contains a branch named '%s', but it has been archived,
meaning it has been hidden from view to reduce the number of visible refs in
this repository.""" % name

            if branch.review:
                message += """

To continue working on this branch, you need to first reopen the review that is
associated with the branch.  You can do this from the review's front-page:

%s""" % branch.review.getURL(db, user, indent=2)

            raise IndexException(reflow(message))
        else:
            # This is a branch that's not supposed to have been archived,
            # meaning it appears to have just gone missing from the repository.
            # Handle this the same way we handle updates where Git's idea of the
            # branches current value doesn't match what we think it should be.
            #
            # We can trigger that handling by calling updateBranch() with any
            # "wrong" old value.
            updateBranch(db, user, repository, name, "0" * 40, head, multiple, flags)
            return

    def commit_id(sha1):
        cursor.execute("SELECT id FROM commits WHERE sha1=%s", [sha1])
        return cursor.fetchone()[0]

    components = name.split("/")
    for index in range(1, len(components)):
        try: repository.revparse("refs/heads/%s" % "/".join(components[:index]))
        except: continue

        message = ("Cannot create branch with name '%s' since there is already a branch named '%s' in the repository." %
                   (name, "/".join(components[:index])))
        raise IndexException(reflow(message))

    if name.startswith("r/"):
        try:
            review_id = int(name[2:])

            cursor.execute("SELECT branches.name FROM reviews JOIN branches ON (branches.id=reviews.branch) WHERE reviews.id=%s", (review_id,))
            row = cursor.fetchone()

            message = "Refusing to create review named as a number."

            if row:
                message += "\nDid you mean to push to the branch '%s', perhaps?" % row[0]

            raise IndexException(message)
        except ValueError:
            pass

        if user.isSystem():
            raise IndexException("Refusing to create review this way.")
        elif user.getPreference(db, "review.createViaPush"):
            the_commit = gitutils.Commit.fromSHA1(db, repository, head, commit_id(head))
            all_commits = [the_commit]

            review = reviewing.utils.createReview(
                db, user, repository, all_commits, name,
                the_commit.niceSummary(include_tag=False), None, via_push=True)

            print "Submitted review:"
            print review.getURL(db, user, indent=2)

            if review.reviewers:
                print "  Reviewers:"
                for reviewer in review.reviewers:
                    print "    %s <%s>" % (reviewer.fullname, reviewer.email)

            if review.watchers:
                print "  Watchers:"
                for watcher in review.watchers:
                    print "    %s <%s>" % (watcher.fullname, watcher.email)

            if configuration.extensions.ENABLED:
                if extensions.role.processcommits.execute(db, user, review, all_commits, None, the_commit, sys.stdout):
                    print

            print "Thank you!"
            return True
        else:
            raise IndexException("Refusing to create review; user preference 'review.createViaPush' is not enabled.")

    sha1 = head
    base = None
    tail = None

    cursor.execute("""SELECT 1
                        FROM reachable
                        JOIN branches ON (branches.id=reachable.branch)
                        JOIN repositories ON (repositories.id=branches.repository)
                       WHERE repositories.id=%s
                       LIMIT 1""",
                   (repository.id,))

    if cursor.fetchone():
        def reachable(sha1):
            cursor.execute("""SELECT branches.id
                                FROM branches
                                JOIN reachable ON (reachable.branch=branches.id)
                                JOIN commits ON (commits.id=reachable.commit)
                               WHERE branches.repository=%s
                                 AND branches.type='normal'
                                 AND commits.sha1=%s
                            ORDER BY reachable.branch ASC
                               LIMIT 1""",
                           (repository.id, sha1))
            return cursor.fetchone()
    else:
        def reachable(sha1):
            return None

    commit_map = {}
    commit_list = []

    row = reachable(sha1)
    if row:
        # Head of branch is reachable from an existing branch.  Could be because
        # this branch is actually empty (just created with no "own" commits) or
        # it could have been merged into some other already existing branch.  We
        # can't tell, so we just record it as empty.

        base = row[0]
        tail = sha1
    else:
        stack = []

        while True:
            if sha1 not in commit_map:
                commit = gitutils.Commit.fromSHA1(db, repository, sha1)
                commit_map[sha1] = commit
                commit_list.append(commit)

                for sha1 in commit.parents:
                    if sha1 not in commit_map:
                        row = reachable(sha1)
                        if not row:
                            stack.append(sha1)
                        elif base is None:
                            base = row[0]
                            tail = sha1

                            base_chain = [base]

                            while True:
                                cursor.execute("SELECT base FROM branches WHERE id=%s", (base_chain[-1],))
                                next = cursor.fetchone()[0]
                                if next is None: break
                                else: base_chain.append(next)

                            def reachable(sha1):
                                cursor.execute("""SELECT 1
                                                    FROM reachable
                                                    JOIN commits ON (commits.id=reachable.commit)
                                                   WHERE reachable.branch=ANY (%s)
                                                     AND commits.sha1=%s""",
                                               (base_chain, sha1))
                                return cursor.fetchone()

            if stack: sha1 = stack.pop(0)
            else: break

    if not base:
        cursor.execute("INSERT INTO branches (repository, name, head) VALUES (%s, %s, %s) RETURNING id", (repository.id, name, commit_id(head)))
        branch_id = cursor.fetchone()[0]
    else:
        cursor.execute("INSERT INTO branches (repository, name, head, base, tail) VALUES (%s, %s, %s, %s, %s) RETURNING id", (repository.id, name, commit_id(head), base, commit_id(tail)))
        branch_id = cursor.fetchone()[0]

        # Suppress the "user friendly" feedback if the push is performed by the
        # Critic system user, since there wouldn't be a human being reading it.
        if not user.isSystem():
            cursor.execute("SELECT name FROM branches WHERE id=%s", [base])

            print "Added branch based on %s containing %d commit%s:" % (cursor.fetchone()[0], len(commit_list), "s" if len(commit_list) > 1 else "")
            for url_prefix in user.getCriticURLs(db):
                print "  %s/log?repository=%d&branch=%s" % (url_prefix, repository.id, name)
            if len(commit_list) > 1:
                print "To create a review of all %d commits:" % len(commit_list)
            else:
                print "To create a review of the commit:"
            for url_prefix in user.getCriticURLs(db):
                print "  %s/createreview?repository=%d&branch=%s" % (url_prefix, repository.id, name)

    reachable_values = [(branch_id, commit.sha1) for commit in commit_list]
    cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, id FROM commits WHERE sha1=%s", reachable_values)
Exemple #3
0
def updateBranch(db, user, repository, name, old, new, multiple, flags):
    try:
        update(repository.path, "refs/heads/" + name, old, new)
    except Reject as rejected:
        raise IndexException(str(rejected))
    except Exception:
        pass

    try:
        branch = dbutils.Branch.fromName(db, repository, name, for_update=dbutils.NOWAIT)
    except dbutils.FailedToLock:
        raise IndexException(reflow(
                "The branch '%s' is currently locked since it is being updated "
                "by another push.  Please fetch and try again." % name))
    else:
        if not branch:
            # FIXME: We should handle this better.  Maybe just redirect to
            # createBranch()?
            raise IndexException("The branch '%s' is not in the database!" % name)
        base_branch_id = branch.base.id if branch.base else None

    if branch.head_sha1 != old:
        if new == branch.head_sha1:
            # This is what we think the ref ought to be already.  Do nothing,
            # and let the repository "catch up."
            return
        else:
            data = { "name": name,
                     "old": old[:8],
                     "new": new[:8],
                     "current": branch.head_sha1[:8] }

            message = """CONFUSED!  Git thinks %(name)s points to %(old)s, but Critic thinks it points to %(current)s.  Rejecting push since it would only makes matters worse.  To resolve this problem, use

  git push -f critic %(current)s:%(name)s

to resynchronize the Git repository with Critic's database.  Note that 'critic' above must be replaced by the actual name of your Critic remote, if not 'critic'.""" % data

            raise IndexException(textutils.reflow(message, line_length=80 - len("remote: ")))

    cursor = db.cursor()
    cursor.execute("""SELECT id, remote, remote_name, forced, updating
                        FROM trackedbranches
                       WHERE repository=%s
                         AND local_name=%s
                         AND NOT disabled""",
                   (repository.id, name))
    row = cursor.fetchone()

    if row:
        trackedbranch_id, remote, remote_name, forced, updating = row
        tracked_branch = "%s in %s" % (remote_name, remote)

        assert not forced or not name.startswith("r/")

        if not user.isSystem() \
                or flags.get("trackedbranch_id") != str(trackedbranch_id):
            raise IndexException("""\
The branch '%s' is set up to track '%s' in
  %s
Please don't push it manually to this repository.""" % (name, remote_name, remote))

        assert updating

        if not name.startswith("r/"):
            conflicting = repository.revlist([branch.head_sha1], [new])
            added = repository.revlist([new], [branch.head_sha1])

            if conflicting:
                if forced:
                    if branch.base is None:
                        cursor.executemany("""DELETE FROM reachable
                                                    WHERE branch=%s
                                                      AND commit IN (SELECT id
                                                                       FROM commits
                                                                      WHERE sha1=%s)""",
                                           [(branch.id, sha1) for sha1 in conflicting])
                    else:
                        print "Non-fast-forward update detected; deleting and recreating branch."

                        deleteBranch(db, user, repository, branch.name, old)
                        createBranches(db, user, repository, [(branch.name, new)], flags)

                        return
                else:
                    raise IndexException("""\
Rejecting non-fast-forward update of branch.  To perform the update, you
can delete the branch using
  git push critic :%s
first, and then repeat this push.""" % name)

            cursor.executemany("""INSERT INTO reachable (branch, commit)
                                       SELECT %s, commits.id
                                         FROM commits
                                        WHERE sha1=%s""",
                               [(branch.id, sha1) for sha1 in added])

            new_head = gitutils.Commit.fromSHA1(db, repository, new)

            cursor.execute("UPDATE branches SET head=%s WHERE id=%s",
                           (new_head.getId(db), branch.id))

            output = []

            if conflicting:
                output.append("Pruned %d conflicting commits." % len(conflicting))
            if added:
                output.append("Added %d new commits." % len(added))

            if output:
                print "\n".join(output)

            return
    else:
        tracked_branch = False

    cursor.execute("SELECT id FROM reviews WHERE branch=%s", (branch.id,))
    row = cursor.fetchone()

    is_review = bool(row)

    if is_review:
        if multiple:
            raise IndexException("""\
Refusing to update review in push of multiple refs.  Please push one
review branch at a time.""")

        review_id = row[0]

        cursor.execute("""SELECT id, old_head, old_upstream, new_upstream, uid, branch
                            FROM reviewrebases
                           WHERE review=%s AND new_head IS NULL""",
                       (review_id,))
        row = cursor.fetchone()

        if row:
            if tracked_branch:
                raise IndexException("Refusing to perform a review rebase via an automatic update.")

            rebase_id, old_head_id, old_upstream_id, new_upstream_id, rebaser_id, onto_branch = row

            review = dbutils.Review.fromId(db, review_id)
            rebaser = dbutils.User.fromId(db, rebaser_id)

            if rebaser.id != user.id:
                if user.isSystem():
                    user = rebaser
                else:
                    raise IndexException("""\
This review is currently being rebased by
  %s <%s>
and can't be otherwise updated right now.""" % (rebaser.fullname, rebaser.email))

            old_head = gitutils.Commit.fromId(db, repository, old_head_id)
            old_commitset = log.commitset.CommitSet(review.branch.getCommits(db))

            if old_head.sha1 != old:
                raise IndexException("""\
Unexpected error.  The branch appears to have been updated since your
rebase was prepared.  You need to cancel the rebase via the review
front-page and then try again, and/or report a bug about this error.""")

            if old_upstream_id is not None:
                new_head = gitutils.Commit.fromSHA1(db, repository, new)

                old_upstream = gitutils.Commit.fromId(db, repository, old_upstream_id)

                if new_upstream_id is not None:
                    new_upstream = gitutils.Commit.fromId(db, repository, new_upstream_id)
                else:
                    if len(new_head.parents) != 1:
                        raise IndexException("Invalid rebase: New head can't be a merge commit.")

                    new_upstream = gitutils.Commit.fromSHA1(db, repository, new_head.parents[0])

                    if new_upstream in old_commitset.getTails():
                        old_upstream = new_upstream = None
            else:
                old_upstream = None

            if old_upstream:
                unrelated_move = False

                if not new_upstream.isAncestorOf(new):
                    raise IndexException("""\
Invalid rebase: The new upstream commit you specified when the rebase
was prepared is not an ancestor of the commit now pushed.  You may want
to cancel the rebase via the review front-page, and prepare another one
specifying the correct new upstream commit; or rebase the branch onto
the new upstream specified and then push that instead.""")

                if not old_upstream.isAncestorOf(new_upstream):
                    unrelated_move = True

                equivalent_merge = replayed_rebase = None

                if unrelated_move:
                    replayed_rebase = reviewing.rebase.replayRebase(
                        db, review, user, old_head, old_upstream, new_head,
                        new_upstream, onto_branch)
                else:
                    equivalent_merge = reviewing.rebase.createEquivalentMergeCommit(
                        db, review, user, old_head, old_upstream, new_head,
                        new_upstream, onto_branch)

                new_sha1s = repository.revlist([new_head.sha1], [new_upstream.sha1], '--topo-order')
                rebased_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in new_sha1s]
                reachable_values = [(review.branch.id, sha1) for sha1 in new_sha1s]

                pending_mails = []

                recipients = review.getRecipients(db)
                for to_user in recipients:
                    pending_mails.extend(reviewing.mail.sendReviewRebased(
                            db, user, to_user, recipients, review,
                            new_upstream, rebased_commits, onto_branch))

                print "Rebase performed."

                review.setPerformedRebase(old_head, new_head, old_upstream, new_upstream, user,
                                          equivalent_merge, replayed_rebase)

                if unrelated_move:
                    reviewing.utils.addCommitsToReview(
                        db, user, review, [replayed_rebase],
                        pending_mails=pending_mails,
                        silent_if_empty=set([replayed_rebase]),
                        replayed_rebases={ replayed_rebase: new_head })

                    repository.keepalive(old_head)
                    repository.keepalive(replayed_rebase)

                    cursor.execute("""UPDATE reviewrebases
                                         SET replayed_rebase=%s
                                       WHERE id=%s""",
                                   (replayed_rebase.getId(db), rebase_id))
                else:
                    reviewing.utils.addCommitsToReview(
                        db, user, review, [equivalent_merge], pending_mails=pending_mails,
                        silent_if_empty=set([equivalent_merge]), full_merges=set([equivalent_merge]))

                    repository.keepalive(equivalent_merge)

                    cursor.execute("""UPDATE reviewrebases
                                         SET equivalent_merge=%s
                                       WHERE id=%s""",
                                   (equivalent_merge.getId(db), rebase_id))

                cursor.execute("""UPDATE reviewrebases
                                     SET new_head=%s,
                                         new_upstream=%s
                                   WHERE id=%s""",
                               (new_head.getId(db), new_upstream.getId(db), rebase_id))

                cursor.execute("""INSERT INTO previousreachable (rebase, commit)
                                       SELECT %s, commit
                                         FROM reachable
                                        WHERE branch=%s""",
                               (rebase_id, review.branch.id))
                cursor.execute("DELETE FROM reachable WHERE branch=%s",
                               (review.branch.id,))
                cursor.executemany("""INSERT INTO reachable (branch, commit)
                                           SELECT %s, commits.id
                                             FROM commits
                                            WHERE commits.sha1=%s""",
                                   reachable_values)
                cursor.execute("UPDATE branches SET head=%s WHERE id=%s",
                               (new_head.getId(db), review.branch.id))
            else:
                old_commitset = log.commitset.CommitSet(review.branch.getCommits(db))
                new_sha1s = repository.revlist([new], old_commitset.getTails(), '--topo-order')

                if old_head.sha1 in new_sha1s:
                    raise IndexException("""\
Invalid history rewrite: Old head of the branch reachable from the
pushed ref; no history rewrite performed.  (Cancel the rebase via
the review front-page if you've changed your mind.)""")

                for new_sha1 in new_sha1s:
                    new_head = gitutils.Commit.fromSHA1(db, repository, new_sha1)
                    if new_head.tree == old_head.tree: break
                else:
                    raise IndexException("""\
Invalid history rewrite: The rebase introduced unexpected code changes.
Use git diff between the review branch in Critic's repository and
the rebased local branch to see what those changes are.""")

                rebased_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in repository.revlist([new_head], old_commitset.getTails(), '--topo-order')]
                new_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in repository.revlist([new], [new_head], '--topo-order')]
                reachable_values = [(review.branch.id, sha1) for sha1 in new_sha1s]

                pending_mails = []

                recipients = review.getRecipients(db)
                for to_user in recipients:
                    pending_mails.extend(reviewing.mail.sendReviewRebased(db, user, to_user, recipients, review, None, rebased_commits))

                print "History rewrite performed."

                if new_commits:
                    reviewing.utils.addCommitsToReview(db, user, review, new_commits, pending_mails=pending_mails)
                else:
                    reviewing.mail.sendPendingMails(pending_mails)

                cursor.execute("""UPDATE reviewrebases
                                     SET new_head=%s
                                   WHERE id=%s""",
                               (new_head.getId(db), rebase_id))

                cursor.execute("""INSERT INTO previousreachable (rebase, commit)
                                       SELECT %s, commit
                                         FROM reachable
                                        WHERE branch=%s""",
                               (rebase_id, review.branch.id))
                cursor.execute("DELETE FROM reachable WHERE branch=%s",
                               (review.branch.id,))
                cursor.executemany("""INSERT INTO reachable (branch, commit)
                                           SELECT %s, commits.id
                                             FROM commits
                                            WHERE commits.sha1=%s""",
                                   reachable_values)
                cursor.execute("UPDATE branches SET head=%s WHERE id=%s",
                               (gitutils.Commit.fromSHA1(db, repository, new).getId(db),
                                review.branch.id))

                repository.keepalive(old)

            review.incrementSerial(db)

            return True
        elif old != repository.mergebase([old, new]):
            raise IndexException("Rejecting non-fast-forward update of review branch.")
    elif old != repository.mergebase([old, new]):
        raise IndexException("""\
Rejecting non-fast-forward update of branch.  To perform the update, you
can delete the branch using
  git push critic :%s
first, and then repeat this push.""" % name)

    cursor.execute("SELECT id FROM branches WHERE repository=%s AND base IS NULL ORDER BY id ASC LIMIT 1", (repository.id,))
    root_branch_id = cursor.fetchone()[0]

    def isreachable(sha1):
        if is_review and sha1 == branch.tail_sha1:
            return True
        if base_branch_id:
            cursor.execute("""SELECT 1
                                FROM commits
                                JOIN reachable ON (reachable.commit=commits.id)
                               WHERE commits.sha1=%s
                                 AND reachable.branch IN (%s, %s, %s)""",
                           (sha1, branch.id, base_branch_id, root_branch_id))
        else:
            cursor.execute("""SELECT 1
                                FROM commits
                                JOIN reachable ON (reachable.commit=commits.id)
                               WHERE commits.sha1=%s
                                 AND reachable.branch IN (%s, %s)""",
                           (sha1, branch.id, root_branch_id))
        return cursor.fetchone() is not None

    stack = [new]
    commits = set()
    commit_list = []
    processed = set()

    while stack:
        sha1 = stack.pop()

        if sha1 not in commits and not isreachable(sha1):
            commits.add(sha1)
            commit_list.append(sha1)

            stack.extend([parent_sha1 for parent_sha1 in gitutils.Commit.fromSHA1(db, repository, sha1).parents if parent_sha1 not in processed])

        processed.add(sha1)

    branch = dbutils.Branch.fromName(db, repository, name)
    review = dbutils.Review.fromBranch(db, branch)

    if review:
        if review.state != "open":
            raise IndexException("""\
The review is closed and can't be extended.  You need to reopen it at
%s
before you can add commits to it.""" % review.getURL(db, user, 2))

        all_commits = [gitutils.Commit.fromSHA1(db, repository, sha1) for sha1 in reversed(commit_list)]

        tails = CommitSet(all_commits).getTails()

        if old not in tails:
            raise IndexException("""\
Push rejected; would break the review.

It looks like some of the pushed commits are reachable from the
repository's main branch, and thus consequently the commits currently
included in the review are too.

Perhaps you should request a new review of the follow-up commits?""")

        reviewing.utils.addCommitsToReview(db, user, review, all_commits, commitset=commits, tracked_branch=tracked_branch)

    reachable_values = [(branch.id, sha1) for sha1 in reversed(commit_list) if sha1 in commits]

    cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, commits.id FROM commits WHERE commits.sha1=%s", reachable_values)
    cursor.execute("UPDATE branches SET head=%s WHERE id=%s", (gitutils.Commit.fromSHA1(db, repository, new).getId(db), branch.id))

    db.commit()

    if configuration.extensions.ENABLED and review:
        extensions.role.processcommits.execute(db, user, review, all_commits,
                                               gitutils.Commit.fromSHA1(db, repository, old),
                                               gitutils.Commit.fromSHA1(db, repository, new),
                                               sys.stdout)
Exemple #4
0
def createBranch(user, repository, name, head, flags):
    processCommits(repository.name, head)

    try:
        update(repository.path, "refs/heads/" + name, None, head)
    except Reject as rejected:
        raise IndexException(str(rejected))
    except Exception:
        pass

    cursor = db.cursor()

    def commit_id(sha1):
        cursor.execute("SELECT id FROM commits WHERE sha1=%s", [sha1])
        return cursor.fetchone()[0]

    components = name.split("/")
    for index in range(1, len(components)):
        try: repository.revparse("refs/heads/%s" % "/".join(components[:index]))
        except: continue

        message = ("Cannot create branch with name '%s' since there is already a branch named '%s' in the repository." %
                   (name, "/".join(components[:index])))
        raise IndexException(textutils.reflow(message, line_length=80 - len("remote: ")))

    if name.startswith("r/"):
        try:
            review_id = int(name[2:])

            cursor.execute("SELECT branches.name FROM reviews JOIN branches ON (branches.id=reviews.branch) WHERE reviews.id=%s", (review_id,))
            row = cursor.fetchone()

            message = "Refusing to create review named as a number."

            if row:
                message += "\nDid you mean to push to the branch '%s', perhaps?" % row[0]

            raise IndexException(message)
        except ValueError:
            pass

        if user.getPreference(db, "review.createViaPush"):
            the_commit = gitutils.Commit.fromSHA1(db, repository, head, commit_id(head))
            all_commits = [the_commit]

            review = reviewing.utils.createReview(
                db, user, repository, all_commits, name,
                the_commit.niceSummary(include_tag=False), None, via_push=True)

            print "Submitted review:"
            print review.getURL(db, user, indent=2)

            if review.reviewers:
                print "  Reviewers:"
                for reviewer in review.reviewers:
                    print "    %s <%s>" % (reviewer.fullname, reviewer.email)

            if review.watchers:
                print "  Watchers:"
                for watcher in review.watchers:
                    print "    %s <%s>" % (watcher.fullname, watcher.email)

            if configuration.extensions.ENABLED:
                if extensions.role.processcommits.execute(db, user, review, all_commits, None, the_commit, sys.stdout):
                    print

            print "Thank you!"
            return True
        else:
            raise IndexException("Refusing to create review; user preference 'review.createViaPush' is not enabled.")

    sha1 = head
    base = None
    tail = None

    cursor.execute("""SELECT 1
                        FROM reachable
                        JOIN branches ON (branches.id=reachable.branch)
                        JOIN repositories ON (repositories.id=branches.repository)
                       WHERE repositories.id=%s
                       LIMIT 1""",
                   (repository.id,))

    if cursor.fetchone():
        def reachable(sha1):
            cursor.execute("""SELECT branches.id
                                FROM branches
                                JOIN reachable ON (reachable.branch=branches.id)
                                JOIN commits ON (commits.id=reachable.commit)
                               WHERE branches.repository=%s
                                 AND branches.type='normal'
                                 AND commits.sha1=%s
                            ORDER BY reachable.branch ASC
                               LIMIT 1""",
                           (repository.id, sha1))
            return cursor.fetchone()
    else:
        def reachable(sha1):
            return None

    commit_map = {}
    commit_list = []

    row = reachable(sha1)
    if row:
        # Head of branch is reachable from an existing branch.  Could be because
        # this branch is actually empty (just created with no "own" commits) or
        # it could have been merged into some other already existing branch.  We
        # can't tell, so we just record it as empty.

        base = row[0]
        tail = sha1
    else:
        stack = []

        while True:
            if sha1 not in commit_map:
                commit = gitutils.Commit.fromSHA1(db, repository, sha1)
                commit_map[sha1] = commit
                commit_list.append(commit)

                for sha1 in commit.parents:
                    if sha1 not in commit_map:
                        row = reachable(sha1)
                        if not row:
                            stack.append(sha1)
                        elif base is None:
                            base = row[0]
                            tail = sha1

                            base_chain = [base]

                            while True:
                                cursor.execute("SELECT base FROM branches WHERE id=%s", (base_chain[-1],))
                                next = cursor.fetchone()[0]
                                if next is None: break
                                else: base_chain.append(next)

                            def reachable(sha1):
                                cursor.execute("""SELECT 1
                                                    FROM reachable
                                                    JOIN commits ON (commits.id=reachable.commit)
                                                   WHERE reachable.branch=ANY (%s)
                                                     AND commits.sha1=%s""",
                                               (base_chain, sha1))
                                return cursor.fetchone()

            if stack: sha1 = stack.pop(0)
            else: break

    if isinstance(user, dbutils.User):
        # Push by regular user.
        user_name = user.name
    else:
        # Push by the Critic system user, i.e. by the branch tracker service or
        # other internal mechanism.
        user_name = user

    if not base:
        cursor.execute("INSERT INTO branches (repository, name, head) VALUES (%s, %s, %s) RETURNING id", (repository.id, name, commit_id(head)))
        branch_id = cursor.fetchone()[0]
    else:
        cursor.execute("INSERT INTO branches (repository, name, head, base, tail) VALUES (%s, %s, %s, %s, %s) RETURNING id", (repository.id, name, commit_id(head), base, commit_id(tail)))
        branch_id = cursor.fetchone()[0]

        # Suppress the "user friendly" feedback if the push is performed by the
        # Critic system user, since there wouldn't be a human being reading it.
        #
        # Also, the calls to user.getCriticURLs() obvious don't work if 'user'
        # isn't a dbutils.User object, which it isn't in that case.
        if user_name != configuration.base.SYSTEM_USER_NAME:
            cursor.execute("SELECT name FROM branches WHERE id=%s", [base])

            print "Added branch based on %s containing %d commit%s:" % (cursor.fetchone()[0], len(commit_list), "s" if len(commit_list) > 1 else "")
            for url_prefix in user.getCriticURLs(db):
                print "  %s/log?repository=%d&branch=%s" % (url_prefix, repository.id, name)
            if len(commit_list) > 1:
                print "To create a review of all %d commits:" % len(commit_list)
            else:
                print "To create a review of the commit:"
            for url_prefix in user.getCriticURLs(db):
                print "  %s/createreview?repository=%d&branch=%s" % (url_prefix, repository.id, name)

    reachable_values = [(branch_id, commit.sha1) for commit in commit_list]
    cursor.executemany("INSERT INTO reachable (branch, commit) SELECT %s, id FROM commits WHERE sha1=%s", reachable_values)

    if not repository.hasMainBranch() and user_name == configuration.base.SYSTEM_USER_NAME:
        cursor.execute("UPDATE repositories SET branch=%s WHERE id=%s", (branch_id, repository.id))