def process(self, db, user, repository_id, commit_ids, branch, summary, reviewfilters, recipientfilters, applyfilters, applyparentfilters, description=None, frombranch=None, trackedbranch=None): if not branch.startswith("r/"): raise OperationFailure(code="invalidbranch", title="Invalid review branch name", message="'%s' is not a valid review branch name; it must have a \"r/\" prefix." % branch) repository = gitutils.Repository.fromId(db, repository_id) commits = [gitutils.Commit.fromId(db, repository, commit_id) for commit_id in commit_ids] commitset = CommitSet(commits) reviewfilters = parseReviewFilters(db, reviewfilters) recipientfilters = parseRecipientFilters(db, recipientfilters) review = createReview(db, user, repository, commits, branch, summary, description, from_branch_name=frombranch, reviewfilters=reviewfilters, recipientfilters=recipientfilters, applyfilters=applyfilters, applyparentfilters=applyparentfilters) extensions_output = StringIO() kwargs = {} if configuration.extensions.ENABLED: if executeProcessCommits(db, user, review, commits, None, commitset.getHeads().pop(), extensions_output): kwargs["extensions_output"] = extensions_output.getvalue().lstrip() if trackedbranch: cursor = db.cursor() cursor.execute("""INSERT INTO trackedbranches (repository, local_name, remote, remote_name, forced, delay) VALUES (%s, %s, %s, %s, false, '1 hour') RETURNING id""", (repository_id, branch, trackedbranch["remote"], trackedbranch["name"])) trackedbranch_id = cursor.fetchone()[0] cursor.execute("""INSERT INTO trackedbranchusers (branch, uid) VALUES (%s, %s)""", (trackedbranch_id, user.id)) db.commit() pid = int(open(configuration.services.BRANCHTRACKER["pidfile_path"]).read().strip()) os.kill(pid, signal.SIGHUP) return OperationResult(review_id=review.id, **kwargs)
def __init__(self, db, parent, child, file_ids=None, commits=None, changeset_cache=None): self.parent = parent self.child = child self.commitset = CommitSet.fromRange(db, parent, child, commits=commits) self.changesets = {} if not self.commitset: raise LineAnnotator.NotSupported commits = [] if not changeset_cache: changeset_cache = {} for commit in self.commitset: if len(commit.parents) > 1: raise LineAnnotator.NotSupported if commit in changeset_cache: self.changesets[commit.sha1] = changeset_cache[commit] else: commits.append(commit) for changeset in loadChangesetsForCommits(db, parent.repository, commits, filtered_file_ids=file_ids): self.changesets[changeset.child.sha1] = changeset_cache[changeset.child] = changeset for commit in set(self.commitset) - set(self.changesets.keys()): changesets = createChangeset(db, None, commit.repository, commit=commit, filtered_file_ids=file_ids, do_highlight=False) assert len(changesets) == 1 self.changesets[commit.sha1] = changeset_cache[commit] = changesets[0] self.commits = [parent] self.commit_index = { parent.sha1: 0 } for commit in self.commitset: self.commit_index[commit.sha1] = len(self.commits) self.commits.append(commit)
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)
def process(self, db, user, repository, branch, summary, commit_ids=None, commit_sha1s=None, applyfilters=True, applyparentfilters=True, reviewfilters=None, recipientfilters=None, description=None, frombranch=None, trackedbranch=None): if not branch.startswith("r/"): raise OperationFailure(code="invalidbranch", title="Invalid review branch name", message="'%s' is not a valid review branch name; it must have a \"r/\" prefix." % branch) if reviewfilters is None: reviewfilters = [] if recipientfilters is None: recipientfilters = {} components = branch.split("/") for index in range(1, len(components)): try: repository.revparse("refs/heads/%s" % "/".join(components[:index])) except gitutils.GitReferenceError: continue message = ("Cannot create branch with name<pre>%s</pre>since there is already a branch named<pre>%s</pre>in the repository." % (htmlutils.htmlify(branch), htmlutils.htmlify("/".join(components[:index])))) raise OperationFailure(code="invalidbranch", title="Invalid review branch name", message=message, is_html=True) if commit_sha1s is not None: commits = [gitutils.Commit.fromSHA1(db, repository, commit_sha1) for commit_sha1 in commit_sha1s] elif commit_ids is not None: commits = [gitutils.Commit.fromId(db, repository, commit_id) for commit_id in commit_ids] else: commits = [] commitset = CommitSet(commits) reviewfilters = parseReviewFilters(db, reviewfilters) recipientfilters = parseRecipientFilters(db, recipientfilters) review = createReview(db, user, repository, commits, branch, summary, description, from_branch_name=frombranch, reviewfilters=reviewfilters, recipientfilters=recipientfilters, applyfilters=applyfilters, applyparentfilters=applyparentfilters) extensions_output = StringIO() kwargs = {} if configuration.extensions.ENABLED: if extensions.role.processcommits.execute(db, user, review, commits, None, commitset.getHeads().pop(), extensions_output): kwargs["extensions_output"] = extensions_output.getvalue().lstrip() if trackedbranch: cursor = db.cursor() cursor.execute("""SELECT 1 FROM knownremotes WHERE url=%s AND pushing""", (trackedbranch["remote"],)) if cursor.fetchone(): delay = "1 week" else: delay = "1 hour" cursor.execute("""INSERT INTO trackedbranches (repository, local_name, remote, remote_name, forced, delay) VALUES (%s, %s, %s, %s, false, %s) RETURNING id""", (repository.id, branch, trackedbranch["remote"], trackedbranch["name"], delay)) trackedbranch_id = cursor.fetchone()[0] cursor.execute("""INSERT INTO trackedbranchusers (branch, uid) VALUES (%s, %s)""", (trackedbranch_id, user.id)) db.commit() pid = int(open(configuration.services.BRANCHTRACKER["pidfile_path"]).read().strip()) os.kill(pid, signal.SIGHUP) return OperationResult(review_id=review.id, **kwargs)
def updateBranch(user_name, repository_name, name, old, new, multiple): repository = gitutils.Repository.fromName(db, repository_name) processCommits(repository_name, new) try: branch = dbutils.Branch.fromName(db, repository, name) base_branch_id = branch.base.id if branch.base else None except: raise IndexException, "The branch '%s' is not in the database! (This should never happen.)" % name 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 critic %(current)s:%(name)s to resynchronize the Git repository with Critic's database.""" % data raise IndexException, textutils.reflow(message, line_length=80 - len("remote: ")) cursor = db.cursor() cursor.execute( "SELECT remote, remote_name, forced FROM trackedbranches WHERE repository=%s AND local_name=%s AND NOT disabled", (repository.id, name)) row = cursor.fetchone() if row: remote, remote_name, forced = row tracked_branch = "%s in %s" % (remote_name, remote) assert not forced or not name.startswith("r/") if user_name != configuration.base.SYSTEM_USER_NAME: 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) elif 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 USING commits WHERE reachable.branch=%s AND reachable.commit=commits.id AND commits.sha1=%s""", [(branch.id, sha1) for sha1 in conflicting]) else: output = "Non-fast-forward update detected; deleting and recreating branch." deleteBranch(repository.name, branch.name) createBranch(None, repository, branch.name, new) return output 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 += "Pruned %d conflicting commits." % len(conflicting) if added: output += "\nAdded %d new commits." % len(added) return output.strip() if output else None else: tracked_branch = False user = getUser(db, user_name) if isinstance(user, str): if not tracked_branch: commit = gitutils.Commit.fromSHA1(db, repository, new) user = dbutils.User.fromId(db, commit.committer.getUserId(db)) else: user = dbutils.User(0, configuration.base.SYSTEM_USER_NAME, configuration.base.SYSTEM_USER_EMAIL, "Critic System", "current") 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 isinstance(user, dbutils.User): if rebaser.id != user.id: if user_name == configuration.base.SYSTEM_USER_NAME: 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) else: assert user == configuration.base.SYSTEM_USER_NAME user = rebaser old_head = gitutils.Commit.fromId(db, repository, old_head_id) old_commitset = log.commitset.CommitSet(review.branch.commits) 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: if old_upstream.sha1 != repository.mergebase( [old_upstream.sha1, new_upstream.sha1]): raise IndexException, """\ Invalid rebase: The new upstream commit is not a descendant of the old upstream commit. 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 new_upstream.sha1 != repository.mergebase( [new_upstream.sha1, 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.""" old_upstream_name = repository.findInterestingTag( db, old_upstream.sha1) or old_upstream.sha1 new_upstream_name = repository.findInterestingTag( db, new_upstream.sha1) or new_upstream.sha1 if onto_branch: merged_thing = "branch '%s'" % onto_branch else: merged_thing = "commit '%s'" % new_upstream_name merge_sha1 = repository.run('commit-tree', new_head.tree, '-p', old_head.sha1, '-p', new_upstream.sha1, input="""\ Merge %s into %s This commit was generated automatically by Critic as an equivalent merge to the rebase of the commits %s..%s onto the %s.""" % (merged_thing, review.branch.name, old_upstream_name, old_head.sha1, merged_thing), env={ 'GIT_AUTHOR_NAME': user.fullname, 'GIT_AUTHOR_EMAIL': user.email, 'GIT_COMMITTER_NAME': user.fullname, 'GIT_COMMITTER_EMAIL': user.email }).strip() merge = gitutils.Commit.fromSHA1(db, repository, merge_sha1) gituser_id = merge.author.getGitUserId(db) cursor.execute( """INSERT INTO commits (sha1, author_gituser, commit_gituser, author_time, commit_time) VALUES (%s, %s, %s, %s, %s) RETURNING id""", (merge_sha1, gituser_id, gituser_id, timestamp( merge.author.time), timestamp(merge.committer.time))) merge.id = cursor.fetchone()[0] cursor.executemany( "INSERT INTO edges (parent, child) VALUES (%s, %s)", [(old_head.getId(db), merge.id), (new_upstream.getId(db), merge.id)]) # Have to commit to make the new commit available to other DB # sessions right away, specifically so that the changeset # creation server can see it. db.commit() cursor.execute( """UPDATE reviewrebases SET old_head=%s, new_head=%s, new_upstream=%s WHERE review=%s AND new_head IS NULL""", (merge.id, new_head.getId(db), new_upstream.getId(db), review.id)) new_sha1s = repository.revlist([new], [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] 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)) pending_mails = [] cursor.execute("SELECT uid FROM reviewusers WHERE review=%s", (review.id, )) recipients = [] for (user_id, ) in cursor.fetchall(): recipients.append(dbutils.User.fromId(db, user_id)) for to_user in recipients: pending_mails.extend( review_mail.sendReviewRebased(db, user, to_user, recipients, review, new_upstream_name, rebased_commits, onto_branch)) print "Rebase performed." review_utils.addCommitsToReview(db, user, review, [merge], pending_mails=pending_mails, silent_if_empty=set([merge]), full_merges=set([merge])) repository.keepalive(merge) else: old_commitset = log.commitset.CommitSet(review.branch.commits) 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: No commit on the rebased branch references the same tree as the old head of the branch.""" cursor.execute( """UPDATE reviewrebases SET new_head=%s WHERE review=%s AND new_head IS NULL""", (new_head.getId(db), review.id)) 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] 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)) pending_mails = [] cursor.execute("SELECT uid FROM reviewusers WHERE review=%s", (review.id, )) recipients = [] for (user_id, ) in cursor.fetchall(): recipients.append(dbutils.User.fromId(db, user_id)) for to_user in recipients: pending_mails.extend( review_mail.sendReviewRebased(db, user, to_user, recipients, review, None, rebased_commits)) print "History rewrite performed." if new_commits: review_utils.addCommitsToReview( db, user, review, new_commits, pending_mails=pending_mails) else: review_mail.sendPendingMails(pending_mails) repository.run('update-ref', 'refs/keepalive/%s' % old, old) 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 rescan: return False #if is_review: cursor.execute("SELECT 1 FROM commits, reachable, branches WHERE commits.sha1=%s AND commits.id=reachable.commit AND reachable.branch=branches.id AND branches.repository=%s", [sha1, repository.id]) if is_review and sha1 == branch.tail: return True if base_branch_id: cursor.execute( "SELECT 1 FROM commits, reachable WHERE commits.sha1=%s AND commits.id=reachable.commit AND reachable.branch IN (%s, %s, %s)", [sha1, branch.id, base_branch_id, root_branch_id]) else: cursor.execute( "SELECT 1 FROM commits, reachable WHERE commits.sha1=%s AND commits.id=reachable.commit 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() count = 0 while stack: sha1 = stack.pop() count += 1 if (count % 1000) == 0: stdout.write(".") if (count % 10000) == 0: stdout.write("\n") stdout.flush() if sha1 not in commits and not isreachable(sha1): commits.add(sha1) commit_list.append(sha1) #if is_review: # stack.append(gitutils.Commit.fromSHA1(repository, sha1).parents[0]) #else: 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?""" review_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.executeProcessCommits( db, user, review, all_commits, gitutils.Commit.fromSHA1(db, repository, old), gitutils.Commit.fromSHA1(db, repository, new), stdout)
def commitRangeFromReview(db, user, review, filter, file_ids): edges = cursor = db.cursor() if filter == "pending": cursor.execute("""SELECT DISTINCT changesets.parent, changesets.child FROM changesets JOIN reviewfiles ON (reviewfiles.changeset=changesets.id) JOIN reviewuserfiles ON (reviewuserfiles.file=reviewfiles.id) WHERE reviewfiles.review=%s AND reviewuserfiles.uid=%s AND reviewfiles.state='pending'""", (review.id, user.id)) elif filter == "reviewable": cursor.execute("""SELECT DISTINCT changesets.parent, changesets.child FROM changesets JOIN reviewfiles ON (reviewfiles.changeset=changesets.id) JOIN reviewuserfiles ON (reviewuserfiles.file=reviewfiles.id) WHERE reviewfiles.review=%s AND reviewuserfiles.uid=%s""", (review.id, user.id)) elif filter == "relevant": filters = review_filters.Filters() filters.load(db, review=review, user=user) cursor.execute("""SELECT DISTINCT changesets.parent, changesets.child, reviewfiles.file, reviewuserfiles.uid IS NOT NULL FROM changesets JOIN reviewfiles ON (reviewfiles.changeset=changesets.id) LEFT OUTER JOIN reviewuserfiles ON (reviewuserfiles.file=reviewfiles.id AND reviewuserfiles.uid=%s) WHERE reviewfiles.review=%s""", (user.id, review.id)) edges = set() for parent_id, child_id, file_id, is_reviewer in cursor: if is_reviewer or filters.isRelevant(db, user, file_id): edges.add((parent_id, child_id)) elif filter == "files": assert len(file_ids) != 0 cursor.execute("""SELECT DISTINCT changesets.parent, changesets.child FROM changesets JOIN reviewchangesets ON (reviewchangesets.changeset=changesets.id) JOIN fileversions ON (fileversions.changeset=changesets.id) WHERE reviewchangesets.review=%s AND fileversions.file=ANY (%s)""", (review.id, list(file_ids))) else: raise Exception, "invalid filter: %s" % filter listed_commits = set() with_pending = set() for parent_id, child_id in edges: listed_commits.add(child_id) with_pending.add((parent_id, child_id)) if len(listed_commits) == 1: return None, gitutils.Commit.fromId(db, review.repository, child_id).sha1, list(listed_commits), listed_commits if filter in ("reviewable", "relevant", "files"): cursor.execute("SELECT child FROM changesets JOIN reviewchangesets ON (changeset=id) WHERE review=%s", (review.id,)) all_commits = [gitutils.Commit.fromId(db, review.repository, commit_id) for (commit_id,) in cursor] commitset = CommitSet(review.branch.commits) tails = commitset.getFilteredTails(review.repository) if len(commitset) == 0: raise Exception, "empty commit-set" elif len(tails) > 1: ancestor = review.repository.getCommonAncestor(tails) paths = [] cursor.execute("SELECT DISTINCT file FROM reviewfiles WHERE review=%s", (review.id,)) files_in_review = set(file_id for (file_id,) in cursor) if filter == "files": files_in_review &= file_ids paths_in_review = set(dbutils.describe_file(db, file_id) for file_id in files_in_review) paths_in_upstreams = set() for tail in tails: paths_in_upstream = set(review.repository.run("diff", "--name-only", "%s..%s" % (ancestor, tail)).splitlines()) paths_in_upstreams |= paths_in_upstream paths.append((tail, paths_in_upstream)) overlapping_changes = paths_in_review & paths_in_upstreams if overlapping_changes: candidates = [] for index1, data in enumerate(paths): for index2, (tail, paths_in_upstream) in enumerate(paths): if index1 != index2 and paths_in_upstream & paths_in_review: break else: candidates.append(data) else: candidates = paths if not candidates: paths.sort(cmp=lambda a, b: cmp(len(a[1]), len(b[1]))) url = "/%s/%s..%s?file=%s" % (review.repository.name, paths[0][0][:8], review.branch.head.sha1[:8], ",".join(map(str, sorted(files_in_review)))) message = """\ <p>It is not possible to generate a diff of the requested set of commits that contains only changes from those commits.</p> <p>The following files would contain unrelated changes:<p> <pre style='padding-left: 2em'>%s</pre> <p>You can use the URL below if you want to view this diff anyway, including the unrelated changes.</p> <pre style='padding-left: 2em'><a href='%s'>%s%s</a></pre>""" % ("\n".join(sorted(overlapping_changes)), url, dbutils.getURLPrefix(db), url) raise page.utils.DisplayMessage(title="Impossible Diff", body=message, review=review, html=True) else: candidates.sort(cmp=lambda a, b: cmp(len(b[1]), len(a[1]))) return candidates[0][0], review.branch.head.sha1, all_commits, listed_commits elif len(tails) == 0: raise Exception, "impossible commit-set (%r)" % commitset return tails.pop(), review.branch.head.sha1, all_commits, listed_commits
def process(self, db, user, repository, branch, summary, commit_ids=None, commit_sha1s=None, applyfilters=True, applyparentfilters=True, reviewfilters=None, recipientfilters=None, description=None, frombranch=None, trackedbranch=None): if not branch.startswith("r/"): raise OperationFailure( code="invalidbranch", title="Invalid review branch name", message= "'%s' is not a valid review branch name; it must have a \"r/\" prefix." % branch) if reviewfilters is None: reviewfilters = [] if recipientfilters is None: recipientfilters = {} components = branch.split("/") for index in range(1, len(components)): try: repository.revparse("refs/heads/%s" % "/".join(components[:index])) except gitutils.GitReferenceError: continue message = ( "Cannot create branch with name<pre>%s</pre>since there is already a branch named<pre>%s</pre>in the repository." % (htmlutils.htmlify(branch), htmlutils.htmlify("/".join(components[:index])))) raise OperationFailure(code="invalidbranch", title="Invalid review branch name", message=message, is_html=True) if commit_sha1s is not None: commits = [ gitutils.Commit.fromSHA1(db, repository, commit_sha1) for commit_sha1 in commit_sha1s ] elif commit_ids is not None: commits = [ gitutils.Commit.fromId(db, repository, commit_id) for commit_id in commit_ids ] else: commits = [] commitset = CommitSet(commits) reviewfilters = parseReviewFilters(db, reviewfilters) recipientfilters = parseRecipientFilters(db, recipientfilters) review = createReview(db, user, repository, commits, branch, summary, description, from_branch_name=frombranch, reviewfilters=reviewfilters, recipientfilters=recipientfilters, applyfilters=applyfilters, applyparentfilters=applyparentfilters) extensions_output = StringIO() kwargs = {} if configuration.extensions.ENABLED: if extensions.role.processcommits.execute( db, user, review, commits, None, commitset.getHeads().pop(), extensions_output): kwargs["extensions_output"] = extensions_output.getvalue( ).lstrip() if trackedbranch: cursor = db.cursor() cursor.execute( """SELECT 1 FROM knownremotes WHERE url=%s AND pushing""", (trackedbranch["remote"], )) if cursor.fetchone(): delay = "1 week" else: delay = "1 hour" cursor.execute( """INSERT INTO trackedbranches (repository, local_name, remote, remote_name, forced, delay) VALUES (%s, %s, %s, %s, false, %s) RETURNING id""", (repository.id, branch, trackedbranch["remote"], trackedbranch["name"], delay)) trackedbranch_id = cursor.fetchone()[0] kwargs["trackedbranch_id"] = trackedbranch_id cursor.execute( """INSERT INTO trackedbranchusers (branch, uid) VALUES (%s, %s)""", (trackedbranch_id, user.id)) db.commit() pid = int( open(configuration.services.BRANCHTRACKER["pidfile_path"]). read().strip()) os.kill(pid, signal.SIGHUP) return OperationResult(review_id=review.id, **kwargs)
def getFilteredTails(self): commitset = CommitSet(self.branch.commits) return commitset.getFilteredTails(self.branch.repository)