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.



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

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

#remoteObject (readonly)

Returns the value of attribute remote.



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

def remote
  @remote
end

Class Method Details

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



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

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



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

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

Instance Method Details

#add_file(filename) ⇒ Object



100
101
102
103
# File 'lib/git_bundle/repository.rb', line 100

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

#checkout(args) ⇒ Object



91
92
93
94
# File 'lib/git_bundle/repository.rb', line 91

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

#commit(message, *files) ⇒ Object



105
106
107
108
# File 'lib/git_bundle/repository.rb', line 105

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

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



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

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

#commits_not_pushedObject



76
77
78
# File 'lib/git_bundle/repository.rb', line 76

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

#commits_not_pushed?Boolean

Returns:

  • (Boolean)


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

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

#commits_not_pushed_countObject



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

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

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



121
122
123
# File 'lib/git_bundle/repository.rb', line 121

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

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



125
126
127
# File 'lib/git_bundle/repository.rb', line 125

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

#file_changed?(filename) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/git_bundle/repository.rb', line 96

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

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



115
116
117
118
119
# File 'lib/git_bundle/repository.rb', line 115

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



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

def locked_revision
  @locked_revision || revision
end

#push(args, create_upstream: nil) ⇒ Object

Example: push([], create_upstream: ‘origin’)



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

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

#reference_exists?(reference) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/git_bundle/repository.rb', line 50

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

#refresh_branchObject



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

def refresh_branch
  @remote = execute(*git_command('rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}'), silence_err: true).split('/').first
  @branch = execute_git('rev-parse', '--abbrev-ref', 'HEAD').chomp
end

#remote_exists?(remote_name) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/git_bundle/repository.rb', line 55

def remote_exists?(remote_name)
  execute_git('remote').split("\n").include?(remote_name)
end

#revisionObject



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

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

#stale?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/git_bundle/repository.rb', line 46

def stale?
  revision != locked_revision
end

#stale_commitsObject



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

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

#stale_commits_countObject



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

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)


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

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