Geordee Naliyath
Geordee Naliyath

Reputation: 1859

How do I find the files changed in a commit using Rugged?

I am planning to use Rugged for programmatically accessing Git repositories.

I need to find out the files changed in a specific commit.

Commit object provides the following, according to the documentation.

I tried the "tree" route, but could not succeed.

I see a similar question asked in SO, in relation to Grit. But, I would like to use Rugged.

In ruby/grit, how do I get a list of files changed in a specific commit?

Upvotes: 4

Views: 1098

Answers (3)

applewiskey
applewiskey

Reputation: 11

A simpler way to get all files in specific commit based on HEAD.

require "rugged"

repo = Rugged::Repository.new('.')
commit = repo.head.target

paths = commit.diff(commit.parents.first).deltas.map { |d| [d.old_file[:path], d.new_file[:path]] }.flatten.uniq

Upvotes: 1

Geordee Naliyath
Geordee Naliyath

Reputation: 1859

Here is a snippet based on Arthur's answer.

require 'rugged'

paths = [];

repo = Rugged::Repository.new('/Users/geordee/Code/HasMenu/data')

walker = Rugged::Walker.new(repo)

walker.sorting(Rugged::SORT_TOPO | Rugged::SORT_REVERSE)
walker.push(repo.head.target)
walker.each do |commit|
  # skip merges
  next if commit.parents.count != 1

  diffs = commit.parents[0].diff(commit)
  diffs.each_delta do |delta|
    paths += [delta.old_file[:path], delta.new_file[:path]]
  end

end

puts paths

This probably skips the first commit.

Upvotes: 2

Arthur Schreiber
Arthur Schreiber

Reputation: 531

You can use Rugged::Commit#diff to get the changes between the commit and its first parent or another Rugged::Commit or Rugged::Tree.

Upvotes: 3

Related Questions