Sha256: e3b035349198e07d6e0763a3b6511ffa0a0a3790ea083f45bb452d81077b1523

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

require_relative 'logger'
require 'open3'

module GitCrecord
  module Git
    def self.stage(files)
      selected_files = files.select(&:selected)
      add_files(selected_files.select{ |file| file.type == :untracked })
      diff = selected_files.map(&:generate_diff).join("\n")
      _stage(diff).success?
    end

    def self._stage(diff)
      cmd = 'git apply --cached --unidiff-zero - '
      content, status = Open3.capture2e(cmd, stdin_data: diff)
      LOGGER.info(cmd)
      LOGGER.info(diff)
      LOGGER.info(diff.lines.size)
      LOGGER.info('stdout/stderr:')
      LOGGER.info(content)
      LOGGER.info("return code: #{status}")
      status
    end

    def self.add_files(files)
      files.each do |file|
        success = add_file(file.filename_a)
        raise "could not add file #{file.filename_a}" unless success
      end
    end

    def self.add_file(filename)
      system("git add -N #{filename}")
    end

    def self.status
      `git status --porcelain`
    end

    def self.commit
      exec('git commit')
    end

    def self.diff
      `git diff --no-ext-diff --no-color`
    end

    def self.toplevel_dir
      `git rev-parse --show-toplevel`.chomp
    end

    def self.tab
      @tab ||= ' ' * [2, `git config crecord.tabwidth`.to_i].max
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
git-crecord-1.0.8 lib/git_crecord/git.rb
git-crecord-1.0.7 lib/git_crecord/git.rb
git-crecord-1.0.6 lib/git_crecord/git.rb
git-crecord-1.0.5 lib/git_crecord/git.rb