Module: RJGit::Porcelain

Defined in:
lib/rjgit.rb

Class Method Summary collapse

Class Method Details

.add(repository, file_pattern) ⇒ Object



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

def self.add(repository, file_pattern)
  repository.add(file_pattern)
end

.blame(repository, file_path, options = {}) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rjgit.rb', line 99

def self.blame(repository, file_path, options={})
  options = {:print => false, :io => $stdout}.merge(options)
  jrepo = RJGit.repository_type(repository)
  return nil unless jrepo

  blame_command = BlameCommand.new(jrepo)
  blame_command.set_file_path(file_path)
  result = blame_command.call
  content = result.get_result_contents
  blame = []
  for index in (0..content.size - 1) do
    blameline = {}
    blameline[:actor] = Actor.new_from_person_ident(result.get_source_author(index))
    blameline[:line] = result.get_source_line(index)
    blameline[:commit] = Commit.new(repository, result.get_source_commit(index))
    blameline[:line] = content.get_string(index)
    blame << blameline
  end
  options[:io].puts RJGit.stringify(blame) if options[:print]
  return blame
end

.cat_file(repository, blob) ⇒ Object



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

def self.cat_file(repository, blob)
  jrepo = RJGit.repository_type(repository)
  jblob = RJGit.blob_type(blob)
  # Try to resolve symlinks; return nil otherwise
  mode = RJGit.get_file_mode(jrepo, jblob)
  if mode == SYMLINK_TYPE
    symlink_source = jrepo.open(jblob.id).get_bytes.to_a.pack('c*').force_encoding('UTF-8')
    blob = Blob.find_blob(jrepo, symlink_source)
    return nil if blob.nil?
    jblob = blob.jblob
  end
  bytes = jrepo.open(jblob.id).get_bytes
  return bytes.to_a.pack('c*').force_encoding('UTF-8')
end

.commit(repository, message = "") ⇒ Object



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

def self.commit(repository, message="")
  repository.commit(message)
end

.diff(repository, options = {}) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rjgit.rb', line 121

def self.diff(repository, options = {})
  options = {:namestatus => false, :patch => false}.merge(options)
  git = repository.git.jgit
  repo = RJGit.repository_type(repository)
  diff_command = git.diff
    if options[:old_rev] then
      reader = repo.new_object_reader
      old_tree = repo.resolve("#{options[:old_rev]}^{tree}")
      old_tree_iter = CanonicalTreeParser.new
      old_tree_iter.reset(reader, old_tree)
      diff_command.set_old_tree(old_tree_iter)
    end
    if options[:new_rev] then
      reader = repo.new_object_reader unless reader
      new_tree = repo.resolve("#{options[:new_rev]}^{tree}")
      new_tree_iter = CanonicalTreeParser.new
      new_tree_iter.reset(reader, new_tree)
      diff_command.set_new_tree(new_tree_iter)
    end
  diff_command.set_path_filter(PathFilter.create(options[:file_path])) if options[:file_path]
  diff_command.set_show_name_and_status_only(true) if options[:namestatus] 
  diff_command.set_cached(true) if options[:cached]
  diff_entries = diff_command.call
  diff_entries = diff_entries.to_array.to_ary
    if options[:patch] then
      result = []
      out_stream = ByteArrayOutputStream.new
      formatter = DiffFormatter.new(out_stream)
      formatter.set_repository(repo)
      diff_entries.each do |diff_entry|
        formatter.format(diff_entry)
        result.push [diff_entry, out_stream.to_string]
        out_stream.reset
      end
    end
  diff_entries = options[:patch] ? result : diff_entries.map {|entry| [entry]}
  RJGit.convert_diff_entries(diff_entries)
end

.ls_tree(repository, tree = nil, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rjgit.rb', line 68

def self.ls_tree(repository, tree=nil, options={})
  options = {:recursive => false, :print => false, :io => $stdout, :ref => Constants::HEAD}.merge options
  jrepo = RJGit.repository_type(repository)
  return nil unless jrepo
  if tree 
    jtree = RJGit.tree_type(tree)
  else
    last_commit_hash = jrepo.resolve(options[:ref])
    return nil unless last_commit_hash
    walk = RevWalk.new(jrepo)
    jcommit = walk.parse_commit(last_commit_hash)
    jtree = jcommit.get_tree
  end
  treewalk = TreeWalk.new(jrepo)
  treewalk.set_recursive(options[:recursive])
  treewalk.set_filter(PathFilter.create(options[:file_path])) if options[:file_path]
  treewalk.add_tree(jtree)
  entries = []
  while treewalk.next
    entry = {}
    mode = treewalk.get_file_mode(0)
    entry[:mode] = mode.get_bits
    entry[:type] = Constants.type_string(mode.get_object_type)
    entry[:id]   = treewalk.get_object_id(0).name
    entry[:path] = treewalk.get_path_string
    entries << entry
  end
  options[:io].puts RJGit.stringify(entries) if options[:print]
  return entries
end

.object_for_tag(repository, tag) ⇒ Object



48
49
50
# File 'lib/rjgit.rb', line 48

def self.object_for_tag(repository, tag)
  repository.find(tag.object.name, RJGit.sym_for_type(tag.object_type))
end