Class: Rigup::GitRepo

Inherits:
Object
  • Object
show all
Includes:
Runability
Defined in:
lib/rigup/git_repo.rb

Constant Summary collapse

GIT_METHODS =
[:commit,:add,:reset_hard,:path,:clone,:log,:size,:branches,:status,:remotes,:pull,:fetch,:push,:merge]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Runability

#bash, #cd, #pwd, #run, #run_for_all

Constructor Details

#initialize(aContext = nil) ⇒ GitRepo

Returns a new instance of GitRepo.



12
13
14
# File 'lib/rigup/git_repo.rb', line 12

def initialize(aContext=nil)
  @context = (aContext || Rigup::Context.new)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/rigup/git_repo.rb', line 16

def method_missing(sym, *args, &block)
  if @git && GIT_METHODS.include?(sym)
    @git.send sym, *args, &block
  else
    super
  end
end

Instance Attribute Details

#configuredObject (readonly)

Returns the value of attribute configured.



8
9
10
# File 'lib/rigup/git_repo.rb', line 8

def configured
  @configured
end

#gitObject (readonly)

Returns the value of attribute git.



8
9
10
# File 'lib/rigup/git_repo.rb', line 8

def git
  @git
end

Class Method Details

.convertChangesToUploadsDeletes(changes) ⇒ Object

Added (A), Copied ©, Deleted (D), Modified (M), Renamed ®, have their type (i.e. regular file, symlink, submodule, …) changed (T)



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/rigup/git_repo.rb', line 126

def self.convertChangesToUploadsDeletes(changes)
  uploads = []
  deletes = []
  changes.each do |line|
    continue if line==""
    tabi = line.index("\t")
    status = line[0,tabi]
    path = line[tabi+1..-1]
    if status.index('D')
      deletes << path
    else
      uploads << path
    end
  end
  return uploads,deletes
end

Instance Method Details

#branchObject



105
106
107
# File 'lib/rigup/git_repo.rb', line 105

def branch
  @git.current_branch
end

#changes?Boolean

Returns:

  • (Boolean)

Raises:

  • (RuntimeError)


49
50
51
52
# File 'lib/rigup/git_repo.rb', line 49

def changes?
  raise RuntimeError.new('Repository must be open') unless open?
  status.length > 0
end

#changesBetweenCommits(aFromCommit, aToCommit) ⇒ Object

::Git –no-pager diff –name-status 26bb87c3981 191d64820f2b result is array of paths prefixed with status letter then a tab see git-scm.com/docs/git-diff under –diff-filter= Added (A), Copied ©, Deleted (D), Modified (M), Renamed ®, have their type (i.e. regular file, symlink, submodule, …) changed (T)



121
122
123
# File 'lib/rigup/git_repo.rb', line 121

def changesBetweenCommits(aFromCommit, aToCommit)
  @git.lib.command_lines('diff',['--name-status',aFromCommit,aToCommit])
end

#checkout(commit = nil, branch = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/rigup/git_repo.rb', line 87

def checkout(commit=nil,branch=nil)
  specific_commit = !!commit && !commit.index('HEAD')
  if specific_commit
    @git.checkout commit
  else
    branch ||= 'master'
    @git.checkout(branch)
  end
end

#clone(aUrl, aPath) ⇒ Object



32
33
34
# File 'lib/rigup/git_repo.rb', line 32

def clone(aUrl,aPath)
  @git = ::Git::clone(aUrl,aPath)
end

#commit_all(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rigup/git_repo.rb', line 54

def commit_all(*args)
  result = begin
    @git.commit_all(*args)
  rescue ::Git::GitExecuteError => e
    if e.message.index("nothing to commit (working directory clean)")
      nil
    else
      raise e
    end
  end
  result = commitFromString(result)
  result
end

#commitFromString(aString) ⇒ Object

“[master (root-commit) 6bdd9e1] first commit 1 files changed, 1 insertions(+), 0 deletions(-) create mode 100644 file1.txt”



69
70
71
72
73
# File 'lib/rigup/git_repo.rb', line 69

def commitFromString(aString)
  return nil if !aString || aString.empty?
  sha = aString.scan(/ ([a-f0-9]+)\]/).flatten.first
  @git.gcommit(sha)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rigup/git_repo.rb', line 40

def empty?
  !@git.branches[0]
end

#export(aDest) ⇒ Object



99
100
101
102
103
# File 'lib/rigup/git_repo.rb', line 99

def export(aDest)
  #rsync --exclude=.git /source/directory/ [email protected]:/target-directory
  FileUtils.cp_r(path,aDest)
  FileUtils.rm_rf(File.expand_path('.git',aDest))
end

#get_file_content(aPath, aCommitOrBranch = nil) ⇒ Object



143
144
145
# File 'lib/rigup/git_repo.rb', line 143

def get_file_content(aPath,aCommitOrBranch=nil)
  @git.lib.command('show',[[aCommitOrBranch||'master',aPath].join(':')]) rescue nil
end

#headObject



113
114
115
# File 'lib/rigup/git_repo.rb', line 113

def head
  @git.log.first
end

#init(*args) ⇒ Object



28
29
30
# File 'lib/rigup/git_repo.rb', line 28

def init(*args)
  @git = ::Git.init(*args)
end

#open(aPath) ⇒ Object



24
25
26
# File 'lib/rigup/git_repo.rb', line 24

def open(aPath)
  @git = ::Git.open(aPath)
end

#open?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/rigup/git_repo.rb', line 36

def open?
  !!@git
end

#originObject



79
80
81
# File 'lib/rigup/git_repo.rb', line 79

def origin
  @git.remotes.find {|r| r.name=='origin'}
end

#pathObject



75
76
77
# File 'lib/rigup/git_repo.rb', line 75

def path
  @git.dir.path
end

#shaObject



109
110
111
# File 'lib/rigup/git_repo.rb', line 109

def sha
  head.sha
end

#statusObject



44
45
46
47
# File 'lib/rigup/git_repo.rb', line 44

def status
  result = @git.lib.command_lines('status',['--porcelain'])
  result
end

#urlObject



83
84
85
# File 'lib/rigup/git_repo.rb', line 83

def url
  (o = origin) && o.url
end