 notifier = require '../notifier'
 OutputViewManager = require '../output-view-manager'
 
-runCommand = (args, workingDirectory) ->
+runCommand = (repo, args) ->
   view = OutputViewManager.create()
-  promise = git.cmd(args, cwd: workingDirectory, {color: true})
-  promise
-  .then (data) ->
+  promise = git.cmd(args, cwd: repo.getWorkingDirectory(), {color: true})
+  promise.then (data) ->
     msg = "git #{args.join(' ')} was successful"
     notifier.addSuccess(msg)
     if data?.length > 0

    else
      view.reset()
    view.finish()
  .catch (msg) =>
    if msg?.length > 0
      view.setContent msg
    else
      view.reset()
    view.finish()
  return promise

class InputView extends View

    @disposables.add atom.commands.add 'atom-text-editor', 'core:confirm', (e) =>
      @disposables.dispose()
      @panel?.destroy()
      runCommand(@repo, @commandEditor.getText().split(' ')).then =>
        @currentPane.activate()
        git.refresh @repo

module.exports = (repo, args=[]) ->
  if args.length > 0
    runCommand repo, args.split(' ')
  else
    new InputView(repo)
