Class: Webbynode::Git

Inherits:
Object show all
Defined in:
lib/webbynode/git.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



9
10
11
# File 'lib/webbynode/git.rb', line 9

def config
  @config
end

#remote_ipObject

Returns the value of attribute remote_ip.



9
10
11
# File 'lib/webbynode/git.rb', line 9

def remote_ip
  @remote_ip
end

#remote_portObject

Returns the value of attribute remote_port.



9
10
11
# File 'lib/webbynode/git.rb', line 9

def remote_port
  @remote_port
end

#remote_userObject

Returns the value of attribute remote_user.



9
10
11
# File 'lib/webbynode/git.rb', line 9

def remote_user
  @remote_user
end

Instance Method Details

#add(what) ⇒ Object



44
45
46
# File 'lib/webbynode/git.rb', line 44

def add(what)
  exec "git add #{what}"
end

#add_git_ignoreObject



36
37
38
# File 'lib/webbynode/git.rb', line 36

def add_git_ignore
  io.create_from_template(".gitignore", "gitignore")
end

#add_remote(user, name, host, repo, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/webbynode/git.rb', line 48

def add_remote(user, name, host, repo, options={})
  port = options[:port] || 22
  home = options[:home] || RemoteExecutor.new(host, user, port).remote_home

  exec("git remote add #{name} ssh://#{user}@#{host}:#{port}#{home}/#{repo}") do |output|
    # raise an exception if remote already exists
    raise GitRemoteAlreadyExistsError, output if output =~ /remote \w+ already exists/
    
    # success if output is empty
    output.nil? or output.empty?
  end
end

#add_to_git_ignore(*entries) ⇒ Object



40
41
42
# File 'lib/webbynode/git.rb', line 40

def add_to_git_ignore(*entries)
  entries.each { |e| io.add_line ".gitignore", e }
end

#clean?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/webbynode/git.rb', line 26

def clean?
  io.exec("git status") =~ /nothing to commit/
end

#commit(comments) ⇒ Object



96
97
98
99
100
101
# File 'lib/webbynode/git.rb', line 96

def commit(comments)
  comments.gsub! /"/, '\"'
  exec("git commit -m \"#{comments}\"") do |output|
    output =~ /#{comments}/ or output =~ /nothing to commit/
  end
end

#commit2(comments) ⇒ Object



86
87
88
89
# File 'lib/webbynode/git.rb', line 86

def commit2(comments)
  comments.gsub! /"/, '\"'
  io.exec2("git commit -m \"#{comments}\"") == 0
end

#commit3(comments) ⇒ Object



91
92
93
94
# File 'lib/webbynode/git.rb', line 91

def commit3(comments)
  comments.gsub! /"/, '\"'
  io.exec3("git commit -m \"#{comments}\"")
end

#current_branchObject



71
72
73
74
75
76
# File 'lib/webbynode/git.rb', line 71

def current_branch
  contents = io.exec("git symbolic-ref HEAD")
  return nil if contents.empty?
  
  contents.gsub(/^refs\/heads\//, "")
end

#delete_file(file) ⇒ Object



30
31
32
33
34
# File 'lib/webbynode/git.rb', line 30

def delete_file(file)
  return unless io.file_exists?(file)
  io.delete_file(file)
  exec "git rm #{file} > /dev/null 2>&1"
end

#delete_remote(name) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/webbynode/git.rb', line 61

def delete_remote(name)
  exec("git remote rm #{name}") do |output|
    # raise an exception if cannot remove
    raise GitRemoteCouldNotRemoveError, output if output =~ /Could not remove config section/
    
    # success if output is empty
    output.nil? or output.empty?
  end
end

#initObject



19
20
21
22
23
24
# File 'lib/webbynode/git.rb', line 19

def init
  exec("git init") do |output|
    # this indicates init was properly executed
    output =~ /^Initialized empty Git repository in/
  end
end

#init_configObject



126
127
128
# File 'lib/webbynode/git.rb', line 126

def init_config
  @config ||= parse_config
end

#ioObject



15
16
17
# File 'lib/webbynode/git.rb', line 15

def io
  @@io ||= Webbynode::Io.new
end

#parse_configObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/webbynode/git.rb', line 103

def parse_config
  return @config if defined?(@config)
  if present? and remote_webbynode?
    config = {}
    current = {}
    File.open(".git/config").each_line do |line|
      case line
      when /^\[(\w+)(?: "(.+)")*\]/
        key, subkey = $1, $2
        current = (config[key] ||= {})
        current = (current[subkey] = {}) if subkey
      else
        key, value = line.strip.split(' = ')
        current[key] = value
      end
    end
    @config = config
  else
    raise Webbynode::GitNotRepoError, "Git repository does not exist." unless present?
    raise Webbynode::GitRemoteDoesNotExistError, "Webbynode has not been initialized." unless remote_webbynode?
  end
end

#parse_remote_ipObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/webbynode/git.rb', line 135

def parse_remote_ip
  init_config
  
  # new remote format
  if parse_remote_url =~ /^ssh:\/\/(\w+)@(.+)\/(.+)$/
    @remote_user = $1
    if $2 =~ /(.*):(\d*)\/(.*)$/
      @remote_ip   ||= $1
      @remote_port ||= $2.to_i
      @remote_home ||= "/#{$3}"
    end
  else
    if @config
      if @config["remote"]["webbynode"]["url"] =~ /^(\w+)@(.+):(.+)$/
        @remote_user ||= $1
        @remote_ip   ||= $2
      end
      @remote_port ||= 22
    end
  end
  
  @remote_ip
end

#parse_remote_urlObject



130
131
132
133
# File 'lib/webbynode/git.rb', line 130

def parse_remote_url
  init_config
  @remote_url ||= @config["remote"]["webbynode"]["url"]
end

#present?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/webbynode/git.rb', line 11

def present?
  io.directory?(".git")
end

#remote_exists?(remote) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
163
164
165
166
# File 'lib/webbynode/git.rb', line 159

def remote_exists?(remote)
  config = parse_config
  config['remote'].key?(remote)
rescue Webbynode::GitNotRepoError
  return false
rescue Webbynode::GitRemoteDoesNotExistError
  return false
end

#remote_webbynode?Boolean

Returns:

  • (Boolean)


168
169
170
171
# File 'lib/webbynode/git.rb', line 168

def remote_webbynode?
  return true if io.exec('git remote') =~ /webbynode/
  false 
end

#remove(file) ⇒ Object



82
83
84
# File 'lib/webbynode/git.rb', line 82

def remove(file)
  io.exec2("git rm --cached #{file}") == 0
end

#tracks?(file) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/webbynode/git.rb', line 78

def tracks?(file)
  io.exec2("git ls-files #{file} --error-unmatch") != 1
end