Class: GitBundle::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_bundle/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, path, main_repository, locked_branch, locked_revision) ⇒ Repository

Returns a new instance of Repository.



15
16
17
18
19
20
21
# File 'lib/git_bundle/repository.rb', line 15

def initialize(name, path, main_repository, locked_branch, locked_revision)
  @name = name
  @path = path
  @main = main_repository
  @locked_branch = locked_branch
  @locked_revision = locked_revision
end

Instance Attribute Details

#mainObject (readonly)

Returns the value of attribute main.



3
4
5
# File 'lib/git_bundle/repository.rb', line 3

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/git_bundle/repository.rb', line 3

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/git_bundle/repository.rb', line 3

def path
  @path
end

Class Method Details

.new_dependant(name, path, locked_branch, locked_revision) ⇒ Object



11
12
13
# File 'lib/git_bundle/repository.rb', line 11

def self.new_dependant(name, path, locked_branch, locked_revision)
  GitBundle::Repository.new(name, path, false, locked_branch, locked_revision)
end

.new_main(name, path) ⇒ Object



7
8
9
# File 'lib/git_bundle/repository.rb', line 7

def self.new_main(name, path)
  GitBundle::Repository.new(name, path, true, nil, nil)
end

Instance Method Details

#add_file(filename) ⇒ Object



57
58
59
60
# File 'lib/git_bundle/repository.rb', line 57

def add_file(filename)
  execute_git('add', filename)
  $?.exitstatus == 0
end

#branchObject



23
24
25
# File 'lib/git_bundle/repository.rb', line 23

def branch
  @branch ||= execute_git('rev-parse', '--abbrev-ref', 'HEAD').chomp
end

#commit(message, *files) ⇒ Object



62
63
64
65
# File 'lib/git_bundle/repository.rb', line 62

def commit(message, *files)
  execute_git('commit', '-m', message, files)
  $?.exitstatus == 0
end

#commit_messages_not_pushedObject



43
44
45
46
# File 'lib/git_bundle/repository.rb', line 43

def commit_messages_not_pushed
  count = execute_git('rev-list', "origin/#{branch}..#{branch}", '--count').to_i
  count.times.map { |num| execute_git('rev-list', '--pretty=oneline', "--skip=#{num}", '--max-count=1', "origin/#{branch}..#{branch}").sub(/\h*\s/, '').strip }
end

#commits_not_pushedObject



39
40
41
# File 'lib/git_bundle/repository.rb', line 39

def commits_not_pushed
  execute_git('rev-list', '--pretty=oneline', '--abbrev-commit', "origin/#{branch}..#{branch}")
end

#execute(*args) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/git_bundle/repository.rb', line 74

def execute(*args)
  puts args.map{ |arg| "'#{arg}'" }.join(' ') if ENV['DEBUG'] == 'true'

  pipe_out, pipe_in = IO.pipe
  system *args, out: pipe_in, err: pipe_in
  pipe_in.close
  pipe_out.read
end

#execute_git(*args, **options) ⇒ Object



67
68
69
70
71
72
# File 'lib/git_bundle/repository.rb', line 67

def execute_git(*args, **options)
  git_command = ['git', '-C', @path]
  git_command += %w(-c color.status=always -c color.ui=always) if options.fetch(:color, false)
  git_command += args.flatten
  execute(*git_command)
end

#file_changed?(filename) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/git_bundle/repository.rb', line 53

def file_changed?(filename)
  !execute_git('diff', '--name-only', filename).empty? && $?.exitstatus == 0
end

#locked_branchObject



27
28
29
# File 'lib/git_bundle/repository.rb', line 27

def locked_branch
  @locked_branch || branch
end

#locked_revisionObject



35
36
37
# File 'lib/git_bundle/repository.rb', line 35

def locked_revision
  @locked_revision || revision
end

#push(args) ⇒ Object



48
49
50
51
# File 'lib/git_bundle/repository.rb', line 48

def push(args)
  puts execute_git('push', args)
  $?.exitstatus == 0
end

#revisionObject



31
32
33
# File 'lib/git_bundle/repository.rb', line 31

def revision
  @revision ||= execute_git('rev-parse', '--verify', 'HEAD').gsub("\n", '')
end