| 1 | #!/bin/sh |
| 2 | # Clean up after a failed build. |
| 3 | # |
| 4 | # Requires access to .gitignore files excluding _all_ modified files. |
| 5 | # |
| 6 | # Requires a working /dev/fd (with more than just /dev/fd/0 and 1) |
| 7 | # or gawk. |
| 8 | |
| 9 | set -e |
| 10 | |
| 11 | splitgitignore='#!/usr/bin/awk |
| 12 | !/^#/ && !/^$/ { |
| 13 | glob = /[[*?]/; |
| 14 | directory = /\/$/; |
| 15 | sub(/\/$/, ""); |
| 16 | anchored = /\//; |
| 17 | sub(/^\//, ""); |
| 18 | |
| 19 | output = "nonexistent/nonsense"; |
| 20 | if (anchored) { |
| 21 | if (!directory && !glob) |
| 22 | output = "/dev/fd/1"; |
| 23 | else if (directory && !glob) |
| 24 | output = "/dev/fd/3"; |
| 25 | else if (!directory && glob) |
| 26 | output = "/dev/fd/4"; |
| 27 | else if (directory && glob) |
| 28 | output = "/dev/fd/5"; |
| 29 | } else { |
| 30 | if (!directory) |
| 31 | output = "/dev/fd/6"; |
| 32 | else |
| 33 | output = "/dev/fd/7"; |
| 34 | } |
| 35 | print >> output; |
| 36 | } |
| 37 | ' |
| 38 | |
| 39 | offlimits="-type d -name '.*' -prune -o -type d -name debian -prune" |
| 40 | |
| 41 | remove_file_globs() { |
| 42 | while read glob |
| 43 | do |
| 44 | eval "rm -f $glob" |
| 45 | done |
| 46 | } |
| 47 | |
| 48 | remove_directory_globs() { |
| 49 | while read glob |
| 50 | do |
| 51 | eval "rm -fr $glob" |
| 52 | done |
| 53 | } |
| 54 | |
| 55 | remove_file_findpatterns() { |
| 56 | while read pat |
| 57 | do |
| 58 | find . $offlimits -o \ |
| 59 | '(' -name "$pat" -execdir rm -f '{}' + ')' |
| 60 | done |
| 61 | } |
| 62 | |
| 63 | remove_directory_findpatterns() { |
| 64 | while read pat |
| 65 | do |
| 66 | find . $offlimits -o \ |
| 67 | '(' -type d -name "$pat" -execdir rm -fr '{}' + ')' |
| 68 | done |
| 69 | } |
| 70 | |
| 71 | find . $offlimits -o '(' -name .gitignore -print ')' | |
| 72 | while read file |
| 73 | do |
| 74 | ( |
| 75 | cd "$(dirname "$file")" |
| 76 | # Dispatch using pipes. Yuck. |
| 77 | { { { { { |
| 78 | awk "$splitgitignore" | |
| 79 | { |
| 80 | # anchored files (globless) |
| 81 | xargs -d '\n' rm -f |
| 82 | } |
| 83 | } 3>&1 >&2 | |
| 84 | { |
| 85 | # anchored directories (globless) |
| 86 | xargs -d '\n' rm -fr |
| 87 | } |
| 88 | } 4>&1 >&2 | |
| 89 | { |
| 90 | # anchored files |
| 91 | remove_file_globs |
| 92 | } |
| 93 | } 5>&1 >&2 | |
| 94 | { |
| 95 | # anchored directories |
| 96 | remove_directory_globs |
| 97 | } |
| 98 | } 6>&1 >&2 | |
| 99 | { |
| 100 | # unanchored files |
| 101 | remove_file_findpatterns |
| 102 | } |
| 103 | } 7>&1 >&2 | |
| 104 | { |
| 105 | remove_directory_findpatterns |
| 106 | } >&2 |
| 107 | ) < "$file" |
| 108 | done |
| 109 | |