Top Level Namespace

Defined Under Namespace

Modules: Gitan

Constant Summary collapse

OPTIONS =

main

{}
OPTION_PARSER =
OptionParser.new

Instance Method Summary collapse

Instance Method Details

#add_argument_optionObject



54
55
56
57
58
# File 'bin/gitan', line 54

def add_argument_option
  OPTION_PARSER.on("-a str", "--arguments=str", "supply argument to command"){ |str|
    OPTIONS[:argument] = str
  }
end

#add_remote_optionObject



47
48
49
50
51
52
# File 'bin/gitan', line 47

def add_remote_option
  OPTION_PARSER.on("-r remote_dir", "--remote=dir", "with remote info"){ |remote_dir|
    server, path = remote_dir.split(":")
    OPTIONS[:remote] = Gitan.remote_heads(server, path)
  }
end

#commitObject



121
122
123
124
125
126
127
# File 'bin/gitan', line 121

def commit
  add_argument_option
  OPTION_PARSER.parse!(ARGV)
  repositories.select {|repo| repo.to_be_commited?}.each do |repo|
    execute(repo.path, "git commit #{OPTIONS[:argument]}")
  end
end

#execute(path, command) ⇒ Object



76
77
78
79
80
81
# File 'bin/gitan', line 76

def execute(path, command)
  print "#{path}: "
  Dir.chdir path
  puts command
  system command unless OPTIONS[:debug]
end

#headsObject



113
114
115
116
117
118
119
# File 'bin/gitan', line 113

def heads
  results = {}
  repositories.each do |repo|
    results[repo.path] = repo.head
  end
  YAML.dump(results, $stdout)
end

#pullObject



138
139
140
141
142
143
144
145
146
# File 'bin/gitan', line 138

def pull
  add_remote_option
  add_argument_option
  OPTION_PARSER.parse!(ARGV)

  repositories.select {|repo| repo.to_be_pulled?}.each do |repo|
    execute(repo.path, "git pull #{OPTIONS[:argument]}")
  end
end

#pushObject



129
130
131
132
133
134
135
136
# File 'bin/gitan', line 129

def push
  add_argument_option
  OPTION_PARSER.parse!(ARGV)

  repositories.select {|repo| repo.to_be_pushed?}.each do |repo|
    execute(repo.path, "git push #{OPTIONS[:argument]}")
  end
end

#repositories(remote_heads = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'bin/gitan', line 60

def repositories(remote_heads = {})
  git_dir = ENV["HOME"] + "/git"
  git_dir = File::expand_path(ARGV[0]) if ARGV[0]

  dirs = Dir.glob(git_dir + "/*").sort.map do |path|
    if File.directory? path
      remote_head = nil
      remote_head = OPTIONS[:remote][File.basename(path)] if OPTIONS[:remote]
      Gitan::Repo.new(path, remote_head)
    else
      nil
    end
  end
  dirs.select{|dir| dir}
end

#show_usageObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'bin/gitan', line 27

def show_usage
  puts <<-HERE
USAGE
  gitan heads  [path]
  gitan status [path] [--argument=str] [-r remote_dir]
  gitan commit [path] [--argument=str]
  gitan push   [path] [--argument=str]
  gitan pull   [path] [--argument=str] [-r remote_dir]

  Default value of 'path' is ~/git.

  Examples:
    gitan heads /home/git
    gitan status -r example.com:/home/git --argument="-s"
    gitan commit --argument='-am "commit message"'
    gitan push -a "origin master"
    gitan pull ~/git -r example.com:/home/git -a "origin master"
  HERE
end

#statusObject



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
109
110
111
# File 'bin/gitan', line 83

def status
  add_remote_option
  add_argument_option
  OPTION_PARSER.on("-A", "--all", "show all repositories."){ OPTIONS[:all] = true}
  OPTION_PARSER.on("-e", "--explain", "show explanation for abbreviation."){ OPTIONS[:explain] = true}
  OPTION_PARSER.parse!(ARGV)

  repos = repositories
  unless OPTIONS[:all]
    repos = repos.select do |repo|
      result = false
      result = true if repo.multiple_branch?
      result = true if repo.to_be_staged?
      result = true if repo.to_be_commited?
      result = true if repo.to_be_pushed?
      result = true if (OPTIONS[:remote] && repo.to_be_pulled?)
      result
    end
  end

  Gitan::Repo.show_abbreviation if OPTIONS[:explain]
  repos.each do |repo|
    puts repo.short_status
    #pp OPTION
    if OPTIONS[:argument]
      execute(repo.path, "git status #{OPTIONS[:argument]}")
    end
  end
end