Monday, August 9, 2010

Using SmartBear's Code Collaborator and git

We're rolling SmartBears Code Collaborator into our development tool suite at my current job. We currently use IntelliJ IDEA and git. In this post, I'll outline 3 options you have for creating Code Collaborator reviews with git.

  1. Using the ccollabui, you can 'Add Changes...' This will look in your git index and create a new review based on the changes you have staged there. This approach will create a review before you actually do the commit to your local repo. One stumbling point I came across while using this with IDEA was that IDEA does not update the index until you actually perform the commit. So while your IDEA 'Changes' view may display the expected files, the 'Add Changes...' UI will not. You will have to go add the files to the index manually via 'git add'.
  2. Using the ccollabui, you can 'Add Git Diffs...' This performs a git diff at the root of your git repo. The diff command is customizable via the ccollabui interface, so you can tailor the diff to exactly what you need. The changed files are not required to be staged in your git index and this approach is also 'pre-commit', your files are not committed to your repo.
  3. If your code workflow requires that you commit locally first or you just do so out of habit, you can use the script. This script was provided to me from the excellent development staff at SmartBear. This script is a temporary solution that they intend to provide full support for in 'ccollab addchangelist' in the future.
    #!/bin/bash
    
    # addgitchangelist : Add a git commit to a Code Collaborator review, with its commit info.
    
    usage()
    {
    cat << EOF
    $0 review_id git_commit_id
    EOF
    }
    
    main()
    {
    REVIEW_ID="$1"
    COMMIT_ID="$2"
    if [ "$REVIEW_ID" = "" ]; then usage; exit 1; fi
    if [ "$COMMIT_ID" = "" ]; then usage; exit 1; fi
    
    ccollab addgitdiffs --upload-comment "$(git log -n 1 "$COMMIT_ID" --)" "$REVIEW_ID" "$COMMIT_ID^..$COMMIT_ID"
    }
    
    main $@
    
    Just place this in your path, make sure you 'chmod +x' it. You may now create reviews after you perform a 'git commit'. The script is typically invoked as follows:
    prompt> addgitchangelist new HEAD
    
    This will create a new review based on the git diffs from the HEAD^ revision to the HEAD revision. Since ccollab understands git syntax you can provide a specific hex commit ID, HEAD and even use the ^ operator. If you wanted to create a review based on the changes from 2 commits ago:
    prompt> addgitchangelist new HEAD^
    
    This will add all the diffs between HEAD^^ and HEAD^ to a new Code Collaborator review. After the script runs, it will open the web based UI where you can add reviewers, observers, supporting documentation and finally initiate the review. As a review progresses, you may need to update your source based on comments and bugs. This is also trivial with the addgitchangelist script. Simply make your changes, commit locally and run:
    prompt> addgitchangelist <REVIEW_ID> HEAD
    
    REVIEW_ID can be obtained from the web interface.


As demonstrated, you have a few options for creating a Code Collaborator review from git changes. Of the 3, I prefer the script method. It is flexible, quick and works great if you commit to your local repo more than you push to your remote.

No comments: