Module: ModuleSync::Git

Includes:
Constants
Defined in:
lib/modulesync/git.rb

Constant Summary

Constants included from Constants

Constants::CONF_FILE, Constants::HOOK_FILE, Constants::MODULESYNC_CONF_FILE, Constants::MODULE_CONF_FILE, Constants::MODULE_FILES_DIR, Constants::PROJ_ROOT

Class Method Summary collapse

Class Method Details

.pull(git_user, git_provider_address, org, name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/modulesync/git.rb', line 7

def self.pull(git_user, git_provider_address, org, name)
  if ! Dir.exists?(PROJ_ROOT)
    Dir.mkdir(PROJ_ROOT)
  end

  # Repo needs to be cloned in the cwd
  if ! Dir.exists?("#{PROJ_ROOT}/#{name}") || ! Dir.exists?("#{PROJ_ROOT}/#{name}/.git")
    puts "Cloning repository fresh"
    remote = "#{git_user}@#{git_provider_address}:#{org}/#{name}.git"
    local = "#{PROJ_ROOT}/#{name}"
    puts "Cloning from #{remote}"
    repo = ::Git.clone(remote, local)

  # Repo already cloned, check out master and override local changes
  else
    puts "Overriding any local changes to repositories in #{PROJ_ROOT}"
    repo = ::Git.open("#{PROJ_ROOT}/#{name}")
    repo.branch('master').checkout
    repo.reset_hard
    repo.pull
  end
end

.untracked_unignored_files(repo) ⇒ Object

Needed because of a bug in the git gem that lists ignored files as untracked under some circumstances github.com/schacon/ruby-git/issues/130



57
58
59
60
# File 'lib/modulesync/git.rb', line 57

def self.untracked_unignored_files(repo)
  ignored = File.open("#{repo.dir.path}/.gitignore").read.split
  repo.status.untracked.keep_if{|f,_| !ignored.any?{|i| File.fnmatch(i, f)}}
end

.update(name, files, message, branch) ⇒ Object

Git add/rm, git commit, git push



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/modulesync/git.rb', line 31

def self.update(name, files, message, branch)
  repo = ::Git.open("#{PROJ_ROOT}/#{name}")
  repo.branch(branch).checkout
  files.each do |file|
    if repo.status.deleted.include?(file)
      repo.remove(file)
    else
      repo.add(file)
    end
  end
  begin
    repo.commit(message)
    repo.push
  rescue ::Git::GitExecuteError => git_error
    if git_error.message.include? "nothing to commit, working directory clean"
      puts "There were no files to update in #{name}. Not committing."
    else
      puts git_error
      exit
    end
  end
end

.update_noop(name, branch) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/modulesync/git.rb', line 62

def self.update_noop(name, branch)
  puts "Using no-op. Files in #{name} may be changed but will not be committed."

  repo = ::Git.open("#{PROJ_ROOT}/#{name}")
  repo.branch(branch).checkout

  puts "Files changed: "
  repo.diff('HEAD', '--').each do |diff|
    puts diff.patch
  end

  puts "Files added: "
  untracked_unignored_files(repo).each do |file,_|
    puts file
  end

  puts "\n\n"
  puts '--------------------------------'
end