Class: Externals::GitProject

Inherits:
Project
  • Object
show all
Defined in:
lib/externals/scms/git_project.rb

Instance Attribute Summary

Attributes inherited from Project

#parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Project

#assert_e_dne_i_ni, attr_attr_accessor, #attributes, #checkout, #export, inherited, #initialize, #main_project?, #name, #scm, scm, #scm_opts, #scm_opts=, #update_ignore

Constructor Details

This class inherits a constructor from Externals::Project

Class Method Details

.add_all ⇒ Object

this is a test helper method TODO: can we move it to the test suite, then?



215
216
217
218
219
220
# File 'lib/externals/scms/git_project.rb', line 215

def self.add_all
  # simplecov:disable
  puts `git add .`
  raise unless $? == 0
  # simplecov:enable
end

.detected? ⇒ Boolean

Returns:

  • (Boolean)


209
210
211
# File 'lib/externals/scms/git_project.rb', line 209

def self.detected?
  File.exist?(".git")
end

.fill_in_opts(opts, main_options, sub_options, options) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/externals/scms/git_project.rb', line 201

def self.fill_in_opts opts, main_options, sub_options, options
  opts.on("--git", "-g",
          Integer,
          *"same as '--scm git'  Uses git to
    checkout/export the main project".lines_by_width(options[:summary_width])
  ) {sub_options[:scm] = main_options[:scm] = 'git'}
end

.scm_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/externals/scms/git_project.rb', line 197

def self.scm_path? path
  path =~ /^git:/ || path =~ /.git$/
end

Instance Method Details

#append_ignore(path) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
# File 'lib/externals/scms/git_project.rb', line 247

def append_ignore path
  rows = ignore_rows(path)

  return if rows.index path.strip

  rows << path.strip

  open('.gitignore', 'w') do |f|
    f.write "#{rows.compact.join("\n")}\n"
  end
end

#change_to_branch_revision(command = "") ⇒ Object

this method fetches/pulls/changes branches/changes revisions/changes the oil in your geo metro/brings world peace



47
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/externals/scms/git_project.rb', line 47

def change_to_branch_revision command = ""
  opts = resolve_opts(command)

  pulled = false

  project_path = if path == "."
                   name || "."
                 else
                   path
                 end

  Dir.chdir project_path do
    do_fetch command
  end

  if branch
    cb = current_branch

    # This allows the main project to be checked out to a directory
    # that doesn't match it's name.
    Dir.chdir project_path do
      if cb != branch
        # let's see if the branch exists in the remote repository
        # and if not, fetch it.
        if !branch_exists("origin/#{branch}")
          do_fetch command
        end

        # if the local branch doens't exist, add --track -b
        if branch_exists(branch)
          puts `git #{opts} checkout #{branch}`
        else
          puts `git #{opts} checkout --track -b #{branch} origin/#{branch}`
        end
        unless $? == 0
          # simplecov:disable
          raise "Could not checkout origin/#{branch}"
          # simplecov:enable
        end
      end
    end

    # on the right branch, let's pull
    Dir.chdir project_path do
      `git #{opts} pull`
      raise unless $? == 0

      pulled = true
    end
  end

  if revision
    Dir.chdir project_path do
      puts `git #{opts} checkout #{revision}`
      unless $? == 0
        # simplecov:disable
        raise "Could not checkout #{revision}"
        # simplecov:enable
      end
    end
  else
    unless pulled
      Dir.chdir project_path do
        `git #{opts} pull`
        raise unless $? == 0
      end
    end
  end
end

#co(*_args) ⇒ Object



30
31
32
# File 'lib/externals/scms/git_project.rb', line 30

def co *_args
  do_up "co"
end

#current_branch ⇒ Object



288
289
290
291
292
293
294
# File 'lib/externals/scms/git_project.rb', line 288

def current_branch
  Dir.chdir path do
    if `git #{scm_opts} branch -a` =~ /^\s*\*\s*([^\s]*)\s*$/
      $1
    end
  end
end

#current_revision ⇒ Object



280
281
282
283
284
285
286
# File 'lib/externals/scms/git_project.rb', line 280

def current_revision
  Dir.chdir path do
    if `git #{scm_opts} show HEAD` =~ /^\s*commit\s*([0-9a-fA-F]*)\s*$/i
      $1
    end
  end
end

#drop_from_ignore(path) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/externals/scms/git_project.rb', line 259

def drop_from_ignore path
  ir = ignore_rows(path)
  rows = ir.reject {|row| row.strip == path.strip}

  if rows.size == ir.size
    # simplecov:disable
    raise "row not found matching #{path} in .gitignore"
    # simplecov:enable
  end

  if ir.size - rows.size != 1
    # simplecov:disable
    raise "More than one row found matching #{path} in .gitignore"
    # simplecov:enable
  end

  open('.gitignore', 'w') do |f|
    f.write "#{rows.compact.join("\n")}\n"
  end
end

#ex(*args) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/externals/scms/git_project.rb', line 140

def ex *args
  if revision
    # No clean reliable way to clone something that's not a branch or tag.
    # just call up instead.
    up(*args)
  else
    clone_opts = "--depth 1"

    if branch
      clone_opts = "#{clone_opts} -b #{branch}"
    end

    do_clone "ex", clone_opts
  end
end

#extract_name(string) ⇒ Object



296
297
298
299
300
# File 'lib/externals/scms/git_project.rb', line 296

def extract_name string
  if string =~ /([^\/:]+?)(?:\.git|\.bundle)?$/
    $1
  end
end

#ignore_contains?(path) ⇒ Boolean

Returns:

  • (Boolean)


222
223
224
225
# File 'lib/externals/scms/git_project.rb', line 222

def ignore_contains? path
  text = ignore_text(path)
  text.split("\n").detect {|r| r.strip == path.strip}
end

#ignore_rows(path) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'lib/externals/scms/git_project.rb', line 237

def ignore_rows(path)
  rows = ignore_text(path) || ''

  rows = rows.split("\n")

  rows.delete_if {|row| row =~ /^\s*$/}

  rows
end

#ignore_text(_path = nil) ⇒ Object



227
228
229
230
231
232
233
234
235
# File 'lib/externals/scms/git_project.rb', line 227

def ignore_text(_path = nil)
  return '' unless File.exist?('.gitignore')

  retval = ''
  open('.gitignore') do |f|
    retval = f.read
  end
  retval
end

#st(*_args) ⇒ Object



190
191
192
193
194
195
# File 'lib/externals/scms/git_project.rb', line 190

def st *_args
  puts "\nstatus for #{path}:"
  Dir.chdir path do
    puts `git #{scm_opts_st} status`
  end
end

#switch(branch_name, _options = {}) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/externals/scms/git_project.rb', line 117

def switch branch_name, _options = {}
  # This allows the main project to be checked out to a directory
  # that doesn't match it's name.
  Dir.chdir path do
    # let's see if the branch exists in the remote repository
    # and if not, fetch it.
    if !branch_exists("origin/#{branch_name}")
      puts `git #{scm_opts} fetch`
    end

    if branch_exists(branch_name)
      puts `git #{scm_opts} checkout #{branch_name}`
    else
      puts `git #{resolve_opts("co")} checkout --track -b #{branch_name}`
    end
    unless $? == 0
      # simplecov:disable
      raise "Could not checkout origin/#{branch_name}"
      # simplecov:enable
    end
  end
end

#up(*_args) ⇒ Object



156
157
158
# File 'lib/externals/scms/git_project.rb', line 156

def up *_args
  do_up "up"
end