Tools for working in the OpenStack Oslo community.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter_git_history.sh 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/bin/sh
  2. #
  3. # Filter the history of a git repository to only include the named
  4. # files.
  5. set -e
  6. if [ $# -lt 1 ]; then
  7. echo "Usage $0 <files to keep>"
  8. exit 1
  9. fi
  10. set -x
  11. files_to_keep="$@"
  12. # Build the grep pattern for ignoring files that we want to keep
  13. keep_pattern="\($(echo $files_to_keep | sed -e 's/ /\\|/g')\)"
  14. # Prune all other files in every commit
  15. pruner="git ls-files | grep -v \"$keep_pattern\" | git update-index --force-remove --stdin; git ls-files > /dev/stderr"
  16. # Find all first commits with listed files and find a subset of them that
  17. # predates all others
  18. roots=""
  19. for file in $files_to_keep; do
  20. file_root=$(git rev-list --reverse HEAD -- $file | head -n1)
  21. fail=0
  22. for root in $roots; do
  23. if git merge-base --is-ancestor $root $file_root; then
  24. fail=1
  25. break
  26. elif ! git merge-base --is-ancestor $file_root $root; then
  27. new_roots="$new_roots $root"
  28. fi
  29. done
  30. if [ $fail -ne 1 ]; then
  31. roots="$new_roots $file_root"
  32. fi
  33. done
  34. # Purge all parents for those commits
  35. set_roots="
  36. if [ 1 -eq 0 $(for root in $roots; do echo " -o \"\$GIT_COMMIT\" = '$root' "; done) ]; then
  37. echo '';
  38. else
  39. cat;
  40. fi"
  41. # Enhance git_commit_non_empty_tree to skip merges with:
  42. # a) either two equal parents (commit that was about to land got purged as well
  43. # as all commits on mainline);
  44. # b) or with second parent being an ancestor to the first one (just as with a)
  45. # but when there are some commits on mainline).
  46. # In both cases drop second parent and let git_commit_non_empty_tree to decide
  47. # if commit worth doing (most likely not).
  48. skip_empty=$(cat << \EOF
  49. if [ $# = 5 ] && git merge-base --is-ancestor $5 $3; then
  50. git_commit_non_empty_tree $1 -p $3
  51. else
  52. git_commit_non_empty_tree "$@"
  53. fi
  54. EOF
  55. )
  56. # Filter out commits for unrelated files
  57. echo "Pruning commits for unrelated files..."
  58. git filter-branch --index-filter "$pruner" --parent-filter "$set_roots" --commit-filter "$skip_empty" HEAD