MrDuk
MrDuk

Reputation: 18242

Return values from Git commands (git status)

I'd like to have a script that runs make on all dirty directories for my source - is there a way to have what's output from git status be returned via cmd line values somehow?

Basically, I'd like to be able to pop a set of changes, and run this script to build each directory rather than navigating to each directory individually and building them.

Upvotes: 0

Views: 1000

Answers (1)

Renato Zannon
Renato Zannon

Reputation: 29941

Try this out:

dirs = `git diff --name-only | xargs dirname | sort -u`.lines.map(&:chomp)
puts dirs

Breaking down the pipeline:

  • git diff --name-only: Lists the names of the modified files
  • xargs dirname: Takes the list of files from stdin and outputs their directories (turns foo/bar.c into foo)
  • sort -u: Removes duplicates from the list of directories.

On the ruby side:

  • `command`: Runs the command (as in the shell) and returns its output (as a string)
  • String#lines: Returns an array of the string split into lines
  • String#chomp: Removes any newline from the end of the string (String#lines doesn't).

Upvotes: 3

Related Questions