Class: Git

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

Constant Summary collapse

@@master_url =
""

Class Method Summary collapse

Class Method Details

.branch(directory = "") ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/apps/git.rb', line 15

def self.branch(directory = "")
  directory = Dir.pwd if directory.length.zero?
  Dir.chdir(directory) do
    `git branch`.scan(/\* ([.\w-]+)/)[0][0] if File.exist?(".git")
  rescue StandardError
    ""
  end
end

.clone_and_reset(uri, directory, tagname) ⇒ Object



143
144
145
146
147
148
149
150
# File 'lib/apps/git.rb', line 143

def self.clone_and_reset(uri, directory, tagname)
  unless File.exist?(directory)
    `git clone #{uri} #{directory}`
    Dir.chdir(directory) do
      `git reset --hard #{tagname}`
    end
  end
end

.copy(src_url, src_directory, branch, target_directory, filelist) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/apps/git.rb', line 166

def self.copy(src_url, src_directory, branch, target_directory, filelist)
  if !File.exist?(src_directory)
    puts "git clone #{src_url} #{src_directory}"
    # puts `git clone #{src_url} #{src_directory}`
  else
    puts "chdir #{src_directory}"
    Dir.chdir(src_directory) do
      puts "git pull"
      git_pull = Command.new("git pull")
      git_pull[:directory] = src_directory
      git_pull[:timeout] = 30
      git_pull[:ignore_failure] = true
      git_pull.execute
    end
  end

  puts "chdir #{src_directory}"
  Dir.chdir(src_directory) do
    puts "git checkout #{branch}"
    # puts `git checkout #{branch}`
    filelist.each do |f|
      dest = "#{target_directory}/#{f}"
      FileUtils.mkdir_p File.dirname(dest) unless File.exist? File.dirname(dest)
      puts "copying #{f} to #{dest}"
      FileUtils.cp(f, dest)
    end
  end
end

.copy_gsub(url, branch, glob, glob_search, glob_replace, destination_directory) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/apps/git.rb', line 195

def self.copy_gsub(url, branch, glob, glob_search, glob_replace, destination_directory)
  temp_dir = Dir.mktmpdir
  begin
    puts `git clone #{url} #{temp_dir}`
    puts "cd #{temp_dir}"
    Dir.chdir(temp_dir) do
      puts `git checkout #{branch}`
      puts "glob #{glob}"
      Dir.glob(glob).each do |f|
        relative_filename = f.gsub(glob_search, glob_replace)
        dest = "#{destination_directory}/#{relative_filename}"
        FileUtils.mkdir_p File.dirname(dest) unless Dir.exist?(File.dirname(dest))
        puts "copying #{f} to #{dest}"
        FileUtils.copy(f, dest)
      end
    end
  ensure
    FileUtils.remove_entry_secure temp_dir
  end
end

.has_changes?(directory = "") ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
# File 'lib/apps/git.rb', line 73

def self.has_changes?(directory = "")
  directory = Dir.pwd if directory.length.zero?
  Dir.chdir(directory) do
    if File.exist?(".git")
      return true if `git status`.include?("modified:")
      return true if `git status`.include?("new file:")
    end
  end
  false
end

.init(directory = "") ⇒ Object



84
85
86
87
88
89
90
91
# File 'lib/apps/git.rb', line 84

def self.init(directory = "")
  directory = Dir.pwd if directory.length.zero?
  parent = File.dirname(directory)
  FileUtils.mkdir_p parent if !File.exist?(parent) && parent.length.positive?
  Dir.chdir(parent) do
    `git init --bare`
  end
end

.is_fork?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/apps/git.rb', line 38

def self.is_fork?
  master_url != url
end

.latest_tag(directory = "") ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/apps/git.rb', line 152

def self.latest_tag(directory = "")
  if directory.length.zero?
    Command.output("git describe --abbrev=0 --tags").strip
    # `git describe --abbrev=0 --tags`.strip
  else
    result = ""
    Dir.chdir(directory) do
      result = Command.output("git describe --abbrev=0 --tags").strip
      # result=`git describe --abbrev=0 --tags`.strip
    end
    result
  end
end

.master_urlObject



30
31
32
# File 'lib/apps/git.rb', line 30

def self.master_url
  @@master_url
end

.master_url=(url) ⇒ Object



34
35
36
# File 'lib/apps/git.rb', line 34

def self.master_url=(url)
  @@master_url = url
end

.publish(destination, source_dir, source_filelist, tag) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/apps/git.rb', line 110

def self.publish(destination, source_dir, source_filelist, tag)
  puts "publish to #{destination}"
  tmp_dir = Dir.mktmpdir
  FileUtils.mkdir_p(File.dirname(tmp_dir)) unless File.exist?(File.dirname(tmp_dir))
  FileUtils.rm_r(tmp_dir) if File.exist?(tmp_dir)
  puts `git clone #{destination} #{tmp_dir}`

  puts "checking if tag #{tag} exists..."
  Dir.chdir(tmp_dir) do
    tags = `git tag`
    if tags.include?(tag)
      puts "tag #{tag} already exists."
    else
      puts "tag #{tag} does not exist."
      Dir.chdir(source_dir) do
        source_filelist.each do |f|
          dest = "#{tmp_dir}/#{f}"
          FileUtils.mkdir_p(File.dirname(dest)) unless File.exist?(File.dirname(dest))
          FileUtils.cp(f, dest)
          puts "copying file #{f} for publishing"
        end
      end
      puts "git add -A"
      puts `git add -A`
      puts 'git commit -m"add"'
      puts `git commit -m"add"`
      Git.tag tmp_dir, tag
    end
  end

  FileUtils.rm_r tmp_dir
end

.remote_origin(directory = "") ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/apps/git.rb', line 62

def self.remote_origin(directory = "")
  url = ""
  directory = Dir.pwd if directory.length.zero?
  Dir.chdir(directory) do
    url = `git remote show origin`.scan(%r{Fetch URL: ([.\-:/\w\d]+)})[0][0] if File.exist?(".git")
  rescue StandardError
    url = ""
  end
  url
end

.tag(directory, version) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/apps/git.rb', line 93

def self.tag(directory, version)
  directory = Dir.pwd if directory.length.zero?
  Dir.chdir(directory) do
    # `git pull`
    tags = `git tag`
    unless tags.include?(version)
      puts "tagging branch"
      puts `git tag #{version} -m'#{version}'`
      puts "committing"
      puts `git commit -m'#{version}'`
      # puts 'pushing'
      # puts `git push --tags`
      # puts `git push`
    end
  end
end

.urlObject



24
25
26
27
# File 'lib/apps/git.rb', line 24

def self.url
  url = ""
  url = `git config --get remote.origin.url` if File.exist?(".git")
end

.user_emailObject



42
43
44
45
46
47
48
49
50
# File 'lib/apps/git.rb', line 42

def self.user_email
  email = ""
  begin
    email = `git config --list`.scan(/user.email=([\d\w.@\-+]+)/)[0][0]
  rescue StandardError
    email = ""
  end
  email
end

.user_nameObject



52
53
54
55
56
57
58
59
60
# File 'lib/apps/git.rb', line 52

def self.user_name
  name = ""
  begin
    name = `git config --list`.scan(/user.name=([\d\w.@\-+]+)/)[0][0]
  rescue StandardError
    name = ""
  end
  name
end