<commit_before>PathWatcher = require 'pathwatcher'
File = PathWatcher.File
{BufferedProcess} = require 'atom'
StatusView = require './status-view'

currentPane = null
dir = ''
commitFilePath = -> dir + '/.git/COMMIT_EDITMSG'
commitEditor = null

gitCommit = ->
  currentPane = atom.workspace.getActivePane()
  dir = atom.project.getRepo().getWorkingDirectory()
  new BufferedProcess({
    command: 'git'
    args: ['status']
    options:
      cwd: dir
    stdout: (data) =>
      prepFile data.toString()
    stderror: (data) =>
      new StatusView(type: 'alert', message: data.toString())
  })

# FIXME?: maybe I shouldn't use the COMMIT file in .git/
# TODO?: Strip out the git tips that 'git status' prints in message
prepFile = (text) ->
  PathWatcher.closeAllWatchers()
  # format the text to be ignored in the commit message
  text = text.replace(/\n/g, "\n# ")
  # in order to make sure each line doesn't start with a space, the preceding
  #   line should end with a backslash
  new File(commitFilePath())
    .write " \n\
     # Please enter the commit message for your changes. Lines starting\n\
     # with '#' will be ignored, and an empty message aborts the commit.\n\
     # #{text}"
  showFile()

showFile = ->
  atom.workspace
    .open(commitFilePath(), split: 'right', activatePane: true)
    # ::open returns a promise resolving to the editor
    .done (editor) -> commitEditor = editor
  PathWatcher.watch commitFilePath(), (event) =>
    # only commit if there isn't already an editor for the commitFile
    commit() if event is 'change'

commit = ->
  PathWatcher.closeAllWatchers()
  cleanFile()
  new BufferedProcess({
    command: 'git'
    args: ['commit', "--file=#{commitFilePath()}"]
    options:
      cwd: dir
    stdout: (data) =>
      atom.workspace.destroyActivePane()
      currentPane.activate()
      new StatusView(type: 'success', message: data.toString())
      # reset editor for commitFile
      currentEditor = null
    stderror: (data) =>
      new StatusView(message: data.toString())
      atom.beep()
  })

cleanFile = ->
  file = new File(commitFilePath())
  text = file.readSync()
  stripOut = text.indexOf "\n# Please enter"
  text = text.slice(0, stripOut)
  file.write text
<commit_msg>Pass 'type' option to StatusView in GitCommit line 64<commit_after>PathWatcher = require 'pathwatcher'
File = PathWatcher.File
{BufferedProcess} = require 'atom'
StatusView = require './status-view'

currentPane = null
dir = ''
commitFilePath = -> dir + '/.git/COMMIT_EDITMSG'
commitEditor = null

gitCommit = ->
  currentPane = atom.workspace.getActivePane()
  dir = atom.project.getRepo().getWorkingDirectory()
  new BufferedProcess({
    command: 'git'
    args: ['status']
    options:
      cwd: dir
    stdout: (data) =>
      prepFile data.toString()
    stderror: (data) =>
      new StatusView(type: 'alert', message: data.toString())
  })

# FIXME?: maybe I shouldn't use the COMMIT file in .git/
# TODO?: Strip out the git tips that 'git status' prints in message
prepFile = (text) ->
  PathWatcher.closeAllWatchers()
  # format the text to be ignored in the commit message
  text = text.replace(/\n/g, "\n# ")
  # in order to make sure each line doesn't start with a space, the preceding
  #   line should end with a backslash
  new File(commitFilePath())
    .write " \n\
     # Please enter the commit message for your changes. Lines starting\n\
     # with '#' will be ignored, and an empty message aborts the commit.\n\
     # #{text}"
  showFile()

showFile = ->
  atom.workspace
    .open(commitFilePath(), split: 'right', activatePane: true)
    # ::open returns a promise resolving to the editor
    .done (editor) -> commitEditor = editor
  PathWatcher.watch commitFilePath(), (event) =>
    # only commit if there isn't already an editor for the commitFile
    commit() if event is 'change'

commit = ->
  PathWatcher.closeAllWatchers()
  cleanFile()
  new BufferedProcess({
    command: 'git'
    args: ['commit', "--file=#{commitFilePath()}"]
    options:
      cwd: dir
    stdout: (data) =>
      atom.workspace.destroyActivePane()
      currentPane.activate()
      new StatusView(type: 'success', message: data.toString())
      # reset editor for commitFile
      currentEditor = null
    stderror: (data) =>
      new StatusView(type: 'alert', message: data.toString())
      atom.beep()
  })

cleanFile = ->
  file = new File(commitFilePath())
  text = file.readSync()
  stripOut = text.indexOf "\n# Please enter"
  text = text.slice(0, stripOut)
  file.write text

{'fraction_non_alphanumeric': 0.12563367864227462, 'fraction_numerical': 0.0008816398501212255, 'mean_word_length': 3.301421800947867, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '880953', 'n_tokens_mistral': 1382, 'n_tokens_neox': 1370, 'n_words': 444}