Agathiyan Bragadeesh | 3bcff54 | 2023-08-04 14:05:28 +0100 | [diff] [blame^] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright The Mbed TLS Contributors |
| 4 | # SPDX-License-Identifier: Apache-2.0 |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | # |
| 18 | # Purpose |
| 19 | # |
| 20 | # For adapting gitignore files for releases so generated files can be included. |
| 21 | # |
| 22 | # Usage: gitignore_add_generated_files.sh [ -h | --help ] etc |
| 23 | # |
| 24 | |
| 25 | set -eu |
| 26 | |
| 27 | print_usage() |
| 28 | { |
| 29 | echo "Usage: $0" |
| 30 | echo -e " -h|--help\t\tPrint this help." |
| 31 | echo -e " -i|--ignore\t\tAdd generated files to the gitignores." |
| 32 | echo -e " -u|--unignore\t\tRemove generated files from the gitignores." |
| 33 | } |
| 34 | |
| 35 | if [[ $# -eq 0 ]]; then |
| 36 | print_usage |
| 37 | exit 1 |
| 38 | elif [[ $# -ge 2 ]]; then |
| 39 | echo "Too many arguments!" |
| 40 | exit 1 |
| 41 | fi |
| 42 | |
| 43 | case "$1" in |
| 44 | -i | --ignore) |
| 45 | IGNORE=true |
| 46 | ;; |
| 47 | -u | --uignore) |
| 48 | IGNORE=false |
| 49 | ;; |
| 50 | -h | --help | "") |
| 51 | print_usage |
| 52 | exit 1 |
| 53 | ;; |
| 54 | *) |
| 55 | echo "Unknown argument: $1" |
| 56 | echo "run '$0 --help' for options" |
| 57 | exit 1 |
| 58 | esac |
| 59 | |
| 60 | GITIGNORES=$(find . -name ".gitignore") |
| 61 | for GITIGNORE in $GITIGNORES; do |
| 62 | if $IGNORE; then |
| 63 | sed -i '/###START_COMMENTED_GENERATED_FILES###/,/###END_COMMENTED_GENERATED_FILES###/s/^# //' $GITIGNORE |
| 64 | sed -i 's/###START_COMMENTED_GENERATED_FILES###/###START_GENERATED_FILES###/' $GITIGNORE |
| 65 | sed -i 's/###END_COMMENTED_GENERATED_FILES###/###END_GENERATED_FILES###/' $GITIGNORE |
| 66 | else |
| 67 | sed -i '/###START_GENERATED_FILES###/,/###END_GENERATED_FILES###/s/^/# /' $GITIGNORE |
| 68 | sed -i 's/###START_GENERATED_FILES###/###START_COMMENTED_GENERATED_FILES###/' $GITIGNORE |
| 69 | sed -i 's/###END_GENERATED_FILES###/###END_COMMENTED_GENERATED_FILES###/' $GITIGNORE |
| 70 | fi |
| 71 | done |