#!/usr/bin/env ruby
# Usage: git filter-branch -f --parent-filter ~/git-filters/parent-prune-empty-merge HEAD

# Function to determine if commit 'c' already an ancestor of any of
# the commits given in 'commits'
def subsumed_by(c,commits)
  commits.any? do |c2|
    c!=c2 && c==`git merge-base #{c} #{c2}`.chomp()
  end
end

# Get the current list of parents
parents = $stdin.read.split.select{|a|a!='-p'}

# Pnly keep parents that are not subsumed by other parents.
parents = parents.select do |p|
  not subsumed_by(p,parents)
end

puts parents.uniq.map{|p|"-p #{p}"}.join(' ')