2

In Ruby, using the rugged gem, how does one do the equivalent of the following?

%x(git diff --name-only master)

I need to list changed files that are either staged or unstaged.

jwfearn
  • 28,781
  • 28
  • 95
  • 122
  • 1
    rugged's README has an example of listing the [files that changed](https://github.com/libgit2/rugged#diffs) by going through the deltas, which include the paths. What isn't working for you there? – Carlos Martín Nieto Aug 18 '15 at 10:34
  • @carlos-martín-nieto, thanks. I read the README but I couldn't get it to work since I didn't "already have a diff object" yet and I wasn't sure how to get one that included both staged and unstaged changes. – jwfearn Aug 18 '15 at 15:15
  • @carlos-martín-nieto, I edited my question to explicitly mentioned staged and unstaged files. – jwfearn Aug 18 '15 at 15:41

1 Answers1

1

Here's the solution I came up with:

files0 = %x(git diff --name-only master).split($RS)

require 'rugged'
files1 = []
changed = %i(index_modified index_new worktree_modified worktree_new)
repo = Rugged::Repository.new(Dir.pwd)
repo.status { |f, d| files1 << f unless (changed & d).empty? }

puts(files0.sort == files1.sort ? "PASS" : "FAIL")
jwfearn
  • 28,781
  • 28
  • 95
  • 122