Module: RJGit::Porcelain

Defined in:
lib/rjgit.rb

Class Method Summary collapse

Class Method Details

.add(repository, file_pattern) ⇒ Object



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

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

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rjgit.rb', line 112

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



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

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



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

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

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



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
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rjgit.rb', line 134

def self.diff(repository, options = {})
  options = {:namestatus => false, :patch => false}.merge(options)
  repo = RJGit.repository_type(repository)
  git = RubyGit.new(repo).jgit
  diff_command = git.diff
    if options[:old_rev]
      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]
      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]
      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, path = nil, treeish = Constants::HEAD, options = {}) ⇒ Object



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
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rjgit.rb', line 69

def self.ls_tree(repository, path=nil, treeish=Constants::HEAD, options={})
  options = {recursive: false, print: false, io: $stdout, path_filter: nil}.merge options
  jrepo = RJGit.repository_type(repository)
  ref = treeish.respond_to?(:get_name) ? treeish.get_name : treeish

  begin
    obj = jrepo.resolve(ref)
    walk = RevWalk.new(jrepo)
    revobj = walk.parse_any(obj)
    jtree = case revobj.get_type
    when Constants::OBJ_TREE
      walk.parse_tree(obj)
    when Constants::OBJ_COMMIT
      walk.parse_commit(obj).get_tree
    end
  rescue Java::OrgEclipseJgitErrors::MissingObjectException, Java::JavaLang::IllegalArgumentException, Java::JavaLang::NullPointerException
    return nil
  end
  if path
    treewalk = TreeWalk.forPath(jrepo, path, jtree)
    return nil unless treewalk
    treewalk.enter_subtree
  else
    treewalk = TreeWalk.new(jrepo)
    treewalk.add_tree(jtree)
  end
  treewalk.set_recursive(options[:recursive])
  treewalk.set_filter(PathFilter.create(options[:path_filter])) if options[:path_filter]
  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]
  entries
end

.object_for_tag(repository, tag) ⇒ Object



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

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