   # This command *must* be passed through a formatter before its displayed
   register '_aheadBehind',
     update: (done) ->
-      done null, repo?.getAheadBehindCount()
+      done null, repo()?.getAheadBehindCount()
 
   # Get the number of commits you're ahead of the remote
   register 'ahead',

  # Returns null in newly-initialized repos (note that isRepo() still returns true in this case)
  register 'branch',
    update: (done) ->
      branch = repo?.getShortHead()
      done null, branch

  # Determine whether the repo is currently in a detached head state
  # This happens when you checkout, for example, a commit hash
  register 'isDetachedHead',
    update: (done) ->
      branch = repo?.getHead()
      done null, ! /^refs\/heads\//.test branch

  # Returns an object with 'ahead' and 'behind' keys

git = require 'git-utils'

# git.open() returns null when we're not in a repo
#
# We're using false to represent "not yet created" and null to
# represent "created and not in a repo"
repoObj = false

repo = ->
  repoObj = git.open '.' if repoObj is false
  repoObj

module.exports = (Impromptu, register, git) ->
  # Expose the repo object from the git-utils library
  # This will be null when we're not in a repo
  register '_repo',
    update: (done) ->
      done null, repo()

  # Helper to figure out if we're in a repo at all
  register 'isRepo',
    update: (done) ->
      done null, !! repo()

  # Root path to the repository
  register 'root',
    update: (done) ->
      root = repo()?.getPath().replace /\.git\/$/, ''
      done null, root

  # Branch name
