review/extension_ui.py @ 1ef154bb1a4f

Add file-level commenting.
author Steve Losh <steve@stevelosh.com>
date Mon, 05 Oct 2009 20:50:06 -0400
parents 7e437c5261bb
children 34caeeba9ae2
'''The review extension's UI.'''

import os
import messages
from api import *
from mercurial import util
from mercurial.node import short

def _init_command(ui, repo, **opts):
    ui.note(messages.INIT_START)
    try:
        ReviewDatastore(ui, repo, lpath=opts.pop('local_path'),
            rpath=opts.pop('remote_path'), create=True)
        ui.status(messages.INIT_SUCCESS)
        return
    except PreexistingDatastore, e:
        if e.committed:
            ui.note(messages.INIT_EXISTS_COMMITTED)
        else:
            raise util.Abort(messages.INIT_EXISTS_UNCOMMITTED)

def _comment_command(ui, repo, *fnames, **opts):
    rev = opts.pop('rev')
    filename = fnames[0] if fnames else ''
    message = opts.pop('message')
    
    rd = ReviewDatastore(ui, repo)
    rcset = rd[rev]
    
    if filename:
        filename = os.path.relpath(filename, start=repo.root)
        
        if filename not in repo[rcset.node].files():
            raise util.Abort(
                messages.COMMENT_FILE_DOES_NOT_EXIST % (filename, repo[rev].rev())
            )
    
    if not message:
        raise util.Abort(messages.COMMENT_REQUIRES_MESSAGE)
    
    rcset.add_comment(message=message, filename=filename)

def _signoff_command(ui, repo, **opts):
    rd = ReviewDatastore(ui, repo)
    rcset = rd[opts.pop('rev')]
    message = opts.pop('message')
    
    if not message:
        raise util.Abort(messages.SIGNOFF_REQUIRES_MESSAGE)
    
    yes, no = opts.pop('yes'), opts.pop('no')
    if yes and no:
        raise util.Abort(messages.SIGNOFF_OPINION_CONFLICT)
    opinion = 'yes' if yes else ('no' if no else '')
    
    try:
        rcset.add_signoff(message=message, opinion=opinion,
            force=opts.pop('force'))
    except SignoffExists:
        raise util.Abort(messages.SIGNOFF_EXISTS)

def _review_command(ui, repo, *fnames, **opts):
    rev = opts.pop('rev')
    
    rd = ReviewDatastore(ui, repo)
    cset = repo[rev]
    rcset = rd[rev]
    
    comment_count = len(rcset.comments)
    author_count = len(set(comment.author for comment in rcset.comments))
    
    ui.write(messages.REVIEW_LOG_CSET % (cset.rev(), short(cset.node())))
    ui.write(messages.REVIEW_LOG_AUTHOR % cset.user())
    ui.write(messages.REVIEW_LOG_SUMMARY % cset.description().split('\n')[0])
    
    ui.write(messages.REVIEW_LOG_SIGNOFFS % len(rcset.signoffs))
    ui.write(messages.REVIEW_LOG_COMMENTS % (comment_count, author_count))
    
    review_level_comments = filter(lambda c: not c.filename, rcset.comments)
    if review_level_comments:
        ui.write('\n')
    for comment in review_level_comments:
        ui.write('\n' + messages.REVIEW_LOG_COMMENT_AUTHOR % comment.author)
        for line in comment.message.splitlines():
            ui.write(messages.REVIEW_LOG_COMMENT_LINE % line)
    
    diffs = rcset.full_diffs(fnames, opts)
    
    for filename in diffs:
        header = messages.REVIEW_LOG_FILE_HEADER % filename
        print '\n\n%s %s' % (header, '-'*(80-(len(header)+1)))
        
        file_level_comments = filter(lambda c: filename == c.filename, rcset.comments)
        for comment in file_level_comments:
            ui.write(messages.REVIEW_LOG_COMMENT_AUTHOR % comment.author)
            for line in comment.message.splitlines():
                ui.write(messages.REVIEW_LOG_COMMENT_LINE % line)
            
        
        content = diffs[filename].splitlines()[2:]
        
        prefix = '%%%dd: ' % len(str(len(content)-1))
        for n, line in enumerate(content):
            ui.write('%s %s\n' % (prefix % n, line))


def review(ui, repo, *fnames, **opts):
    '''code review a changeset in the current repository
    '''
    if opts.pop('init'):
        return _init_command(ui, repo, **opts)
    elif opts.pop('comment'):
        return _comment_command(ui, repo, *fnames, **opts)
    elif opts.pop('signoff'):
        return _signoff_command(ui, repo, **opts)
    else:
        return _review_command(ui, repo, *fnames, **opts)


cmdtable = {
    'review': (review, [
        ('i', 'init',        False, 'start code reviewing this repository'),
        ('',  'local-path',  '',    'the local path to the code review data'),
        ('',  'remote-path', '',    'the remote path to code review data'),
        ('c', 'comment',     False, 'add a comment'),
        ('s', 'signoff',     False, 'sign off'),
        ('',  'yes',         False, 'sign off as stating the changeset is good'),
        ('',  'no',          False, 'sign off as stating the changeset is bad'),
        ('m', 'message',     '',    'use <text> as the comment or signoff message'),
        ('',  'force',       False, 'overwrite an existing signoff'),
        ('f', 'file',        '',    'comment on <file>'),
        ('r', 'rev',         '.',   'the revision to review'),
    ],
    'hg review')
}