Class: GitBundle::Repository

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shell

#execute, #execute_live, #execute_pipe

Constructor Details

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

Returns a new instance of Repository.



19
20
21
22
23
24
25
26
# File 'lib/git_bundle/repository.rb', line 19

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
  refresh_branch
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



5
6
7
# File 'lib/git_bundle/repository.rb', line 5

def branch
  @branch
end

#locked_branchObject (readonly)

Returns the value of attribute locked_branch.



5
6
7
# File 'lib/git_bundle/repository.rb', line 5

def locked_branch
  @locked_branch
end

#mainObject (readonly)

Returns the value of attribute main.



5
6
7
# File 'lib/git_bundle/repository.rb', line 5

def main
  @main
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/git_bundle/repository.rb', line 5

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/git_bundle/repository.rb', line 5

def path
  @path
end

Class Method Details

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



15
16
17
# File 'lib/git_bundle/repository.rb', line 15

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



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

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

Instance Method Details

#add_file(filename) ⇒ Object



93
94
95
96
# File 'lib/git_bundle/repository.rb', line 93

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

#checkout(args) ⇒ Object



84
85
86
87
# File 'lib/git_bundle/repository.rb', line 84

def checkout(args)
  execute_git_output('checkout', args)
  $?.exitstatus == 0
end

#commit(message, *files) ⇒ Object



98
99
100
101
# File 'lib/git_bundle/repository.rb', line 98

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

#commit_with_description(message, description, *files) ⇒ Object



103
104
105
106
# File 'lib/git_bundle/repository.rb', line 103

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

#commits_not_pushedObject



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

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

#commits_not_pushed?Boolean

Returns:

  • (Boolean)


65
66
67
68
# File 'lib/git_bundle/repository.rb', line 65

def commits_not_pushed?
  return true unless upstream_branch_exists?
  commits_not_pushed_count > 0
end

#commits_not_pushed_countObject



74
75
76
# File 'lib/git_bundle/repository.rb', line 74

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

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



114
115
116
# File 'lib/git_bundle/repository.rb', line 114

def execute_git(*args, **options)
  execute(*git_command(*args, **options))
end

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



118
119
120
# File 'lib/git_bundle/repository.rb', line 118

def execute_git_output(*args, **options)
  execute_live(*git_command(*args, **options))
end

#file_changed?(filename) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/git_bundle/repository.rb', line 89

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

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



108
109
110
111
112
# File 'lib/git_bundle/repository.rb', line 108

def git_command(*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
end

#locked_revisionObject



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

def locked_revision
  @locked_revision || revision
end

#push(args, create_upstream: false) ⇒ Object



78
79
80
81
82
# File 'lib/git_bundle/repository.rb', line 78

def push(args, create_upstream: false)
  args = args.dup + ['--set-upstream', 'origin', branch] if create_upstream
  execute_git_output('push', args)
  $?.exitstatus == 0 || (create_upstream && $?.exitstatus == 128)
end

#reference_exists?(reference) ⇒ Boolean

Returns:

  • (Boolean)


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

def reference_exists?(reference)
  execute_git('cat-file', '-e', reference)
  $?.exitstatus == 0
end

#refresh_branchObject



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

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

#revisionObject



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

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

#stale?Boolean

Returns:

  • (Boolean)


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

def stale?
  revision != locked_revision
end

#stale_commitsObject



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

def stale_commits
  execute_git('rev-list', '--pretty=oneline', '--abbrev-commit', "#{locked_revision}..#{revision}")
end

#stale_commits_countObject



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

def stale_commits_count
  execute_git('rev-list', '--pretty=oneline', '--abbrev-commit', '--count', "#{locked_revision}..#{revision}").to_i
end

#upstream_branch_exists?Boolean

Returns:

  • (Boolean)


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

def upstream_branch_exists?
  reference_exists?("origin/#{branch}")
end