Class: Absgit

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

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Absgit

Returns a new instance of Absgit.



12
13
14
15
# File 'lib/absgit.rb', line 12

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

Class Method Details

.get_repo_path(file_name) ⇒ Object



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

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



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

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



48
49
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
# File 'lib/absgit.rb', line 48

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?
      }

    env =
      if repo_path.nil?
        {}
      else
        {
          'GIT_DIR' => (repo_path + GIT_DIR_BASE).to_s,
          'GIT_WORK_TREE' => repo_path.to_s,
        }
      end

    #< 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.

    command = ['git'] + @args
    debug(command.inspect)
    debug(env.inspect)
    system(env, *command)
  end
end