Permalink
Browse files

Initial commit of git-filename

  • Loading branch information...
1 parent 5ec1477 commit 42b93fe6034e4aaf91b6977b3c567745e52ce758 @cespare committed Jul 6, 2011
Showing with 56 additions and 2 deletions.
  1. +56 −0 git-filename
  2. +0 −2 git-sync
View
@@ -0,0 +1,56 @@
+#!/usr/bin/env ruby
+
+begin
+ require "trollop"
+rescue LoadError
+ abort "You need the trollop gem to use git-filename: $ gem install trollop"
+end
+
+options = Trollop::options do
+ banner <<EOS
+Print out relative filenames for changed files in a manner suitable for passing to other commands.
+Usage:
+ $ git filename [options] [pattern1] ... [patternN]
+where each pattern is a ruby regular expression and [options] are
+EOS
+
+ opt :staged, "Only matched filenames for staged changes", :default => false
+ opt :unstaged, "Only matched filenames for unstaged changes", :default => false
+ opt :untracked, "Only match untracked files", :default => false
+ opt :newlines, "Print each result on its own line (instead of just separated by spaces)", :default => false
+ opt :any, "Match any of the patterns (default is to match all patterns)", :default => false
+end
+
+STATUS_CODE_TO_STATUS = {
+ " " => :unmodified,
+ "M" => :modified,
+ "A" => :added,
+ "D" => :deleted,
+ "R" => :renamed,
+ "C" => :copied,
+ "U" => :updated_and_unmerged,
+ "?" => :untracked
+}
+
+# status_x and status_y should be statuses from the above map. See the "Short Format" section in the
+# git-status man page. before_path and after_path are the paths for this change (the same unless it is a
+# rename). merge_conflict indicates whether the ChangePath represents a merge conflict.
+ChangePath = Struct.new(:status_x, :status_y, :before_path, :after_path, :merge_conflict)
+
+raw_text = `git status --porcelain`
+change_paths = raw_text.split("\n").map do |line|
+ matches = line.match(/((^[ MADRCU])([ MADRCU])) (.*?)( -> (.*))?$/)
+ unless matches
+ STDERR.puts "bad line: #{line}"
+ next
+ end
+ #merge_conflict =
+ status_x = STATUS_CODE_TO_STATUS[matches[2]]
+ status_y = STATUS_CODE_TO_STATUS[matches[3]]
+ before_path = matches[4]
+ after_path = matches[6] || matches[4]
+ merge_conflict = ["DD", "AU", "UD", "UA", "DU", "AA", "UU"].include? matches[1]
+ ChangePath.new status_x, status_y, before_path, after_path, merge_conflict
+end
+
+change_paths.each { |x| p x }
View
@@ -2,11 +2,9 @@
USAGE =<<EOS
Usage:
-
$ git sync <newbase> [branch]
"git sync master topic" is shorthand for the following:
-
$ git rebase master topic
$ git checkout master
$ git merge --ff-only topic

0 comments on commit 42b93fe

Please sign in to comment.