Class: DTK::Common::GritAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/grit_adapter.rb,
lib/grit_adapter/file_access.rb,
lib/grit_adapter/object_access.rb,
lib/grit_adapter/file_access/status.rb

Direct Known Subclasses

FileAccess, ObjectAccess

Defined Under Namespace

Classes: FileAccess, ObjectAccess

Constant Summary collapse

Git_command__push_mutex =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo_dir, branch = nil, opts = {}) ⇒ GritAdapter

Returns a new instance of GritAdapter.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/grit_adapter.rb', line 30

def initialize(repo_dir,branch=nil,opts={})
  @repo_dir = repo_dir
  @branch = branch
  @grit_repo = nil
  begin
    @grit_repo = (opts[:init] ?  init(repo_dir,branch,opts) : create_for_existing_repo(repo_dir,opts))
    @branch ||= default_branch()
  rescue ::Grit::NoSuchPathError
    repo_name = repo_dir.split("/").last.gsub("\.git","")
    #TODO: change to usage error
    raise Error.new("repo (#{repo_name}) does not exist")
  rescue => e
    raise e
  end
end

Instance Attribute Details

#branchObject (readonly)

Returns the value of attribute branch.



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

def branch
  @branch
end

#repo_dirObject (readonly)

Returns the value of attribute repo_dir.



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

def repo_dir
  @repo_dir
end

Class Method Details

.clone(target_repo_dir, git_server_url, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/grit_adapter.rb', line 48

def self.clone(target_repo_dir,git_server_url,opts={})
  if File.directory?(target_repo_dir)
    if opts[:delete_if_exists]
      FileUtils.rm_rf target_repo_dir
    else
      # raise Error.new("trying to create a repo directory (#{target_repo_dir}) that exists already")
      raise DTK::Client::DtkError, "trying to create a repo directory (#{target_repo_dir}) that exists already"
    end
  end
  clone_cmd_opts = {:raise => true, :timeout => 60}
  clone_args = [git_server_url,target_repo_dir]
  if branch = opts[:branch]
    clone_args += ["-b",branch]
  end
  ::Grit::Git.new("").clone(clone_cmd_opts,*clone_args)
  ret = new(*[target_repo_dir,opts[:branch]].compact)
  #make sure remote branch exists; ::Grit::Git.new("").clone silently uses master if remote branch does not exist
  if branch = opts[:branch]
    branches = ret.branches()
    unless branches.include?(opts[:branch])
      FileUtils.rm_rf target_repo_dir
      raise Error.new("Remote branch (#{opts[:branch]}) does not exist")
    end
  end
  ret
end

Instance Method Details

#add_or_update_remote(remote_name, remote_url) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/grit_adapter.rb', line 124

def add_or_update_remote(remote_name,remote_url)
  if remote_exists?(remote_name)
    git_command(:remote,"set-url",remote_name,remote_url)
  else
    add_remote(remote_name,remote_url)
  end
end

#add_remote(remote_name, remote_url) ⇒ Object



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

def add_remote(remote_name,remote_url)
  git_command(:remote,"add",remote_name,remote_url)
end

#add_remote?(remote_name, remote_url) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/grit_adapter.rb', line 116

def add_remote?(remote_name,remote_url)
  unless remote_exists?(remote_name)
    add_remote(remote_name,remote_url)
  end
end

#branchesObject



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

def branches()
  @grit_repo.branches.map{|h|h.name}
end

#file_content(path) ⇒ Object



92
93
94
95
# File 'lib/grit_adapter.rb', line 92

def file_content(path)
  tree_or_blob = tree/path
  tree_or_blob && tree_or_blob.kind_of?(::Grit::Blob) && tree_or_blob.data
end

#ls_r(depth = nil, opts = {}) ⇒ Object



83
84
85
86
# File 'lib/grit_adapter.rb', line 83

def ls_r(depth=nil,opts={})
  tree_contents = tree.contents
  ls_r_aux(depth,tree_contents,opts)
end

#path_exists?(path) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/grit_adapter.rb', line 88

def path_exists?(path)
  not (tree/path).nil?
end

#push(remote_branch_ref = nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/grit_adapter.rb', line 97

def push(remote_branch_ref=nil)
  remote_repo,remote_branch = parse_remote_branch_ref(remote_branch_ref)
  Git_command__push_mutex.synchronize do
    git_command(:push,remote_repo||"origin", "#{@branch}:refs/heads/#{remote_branch||@branch}")
  end
end

#remotesObject



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

def remotes()
  @grit_repo.remotes
end