Class: Absgit

Inherits:
Object
  • Object
show all
Includes:
Methadone::CLILogging, Utils
Defined in:
lib/absgit.rb,
lib/absgit/version.rb

Constant Summary collapse

GIT_DIR_BASE =
'.git'
PROGRAM_NAME =
'absgit'
VERSION =
'0.2.1'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Absgit

Returns a new instance of Absgit.



14
15
16
17
# File 'lib/absgit.rb', line 14

def initialize(args)
  @args = args.dup
  @options = {}
end

Class Method Details

.get_repo_path(file_name) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/absgit.rb', line 19

def self.get_repo_path(file_name)
  Pathname.new(file_name).ascend do |path|
    if path.exist?
      path.realpath.ascend do |path2|
        if (path2 + '.git').exist?
          return path2
        end
      end
    end
  end
end

Instance Method Details

#option_parserObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/absgit.rb', line 31

def option_parser
  OptionParser.new do |parser|
    parser.program_name = PROGRAM_NAME
    parser.banner = "Usage: #{PROGRAM_NAME} [options] [GIT_SUBCOMMAND]"

    parser.on('--debug', 'Show debugging information') do
      @options[:debug] = true
    end

    parser.on('--help', 'Show this usage summary') do
      @options[:help] = true
    end

    parser.on('--version', 'Show program name and version') do
      @options[:version] = true
    end
  end
end

#runObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/absgit.rb', line 50

def run
  begin
    option_parser.order!
  rescue OptionParser::ParseError
    $stderr.puts 'Error: incorrect usage'
    $stderr.puts ''
    $stderr.puts option_parser
    exit(1)
  end

  if @options[:debug]
    logger.error_level = Logger::Severity::DEBUG
  end

  debug(@options.inspect)
  debug(@args.inspect)

  if @options[:version]
    puts "#{PROGRAM_NAME} #{VERSION}"
  elsif @options[:help]
    puts option_parser
  else
    repo_path =
      @args[1..-1].find_all { |arg| repo_object_candidate?(arg) }.
      each_with_object(nil) { |arg|
        repo = self.class.get_repo_path(arg)
        break repo if !repo.nil?
      }

    if repo_path.nil?
      env = {}

      git_args = @args
    else
      env = {
        'GIT_DIR' => (repo_path + GIT_DIR_BASE).to_s,
        'GIT_WORK_TREE' => repo_path.to_s,
      }

      git_args = @args.map { |arg|
        make_tracked_files_relative_to_repo(repo_path, arg)
      }
    end

    command = ['git'] + git_args

    debug(command.inspect)
    debug(env.inspect)

    system(env, *command)
    #
    #< it seems better to use environment variables
    # rather than "--git-dir" and "--work-tree", because
    # environment variables will be inherited by processes
    # spawned by git aliases.

    exit($?.exited? ? $?.exitstatus: 1)
  end
end