From 77b0d645f58a2f20786aa2876e25ec56fef1be5d Mon Sep 17 00:00:00 2001
From: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
Date: Thu, 6 Jul 2023 17:58:18 +0100
Subject: [PATCH 1/5] Add gitignore anchors to denote generated files

These anchors encapsulate gitignore patterns which typically ignore
files generated, so that scripts can be used to comment and uncomment
these patterns for releases when we need the generated files in the
repository.

Signed-off-by: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
---
 library/.gitignore        |  3 ++-
 programs/.gitignore       | 10 ++++++----
 tests/.gitignore          | 12 +++++++-----
 visualc/VS2013/.gitignore | 10 ++++++----
 4 files changed, 21 insertions(+), 14 deletions(-)

diff --git a/library/.gitignore b/library/.gitignore
index b4dc918912..5a29a43b77 100644
--- a/library/.gitignore
+++ b/library/.gitignore
@@ -2,8 +2,9 @@ libmbed*
 *.sln
 *.vcxproj
 
-# Automatically generated files
+###START_GENERATED_FILES###
 /error.c
 /version_features.c
 /ssl_debug_helpers_generated.c
 /psa_crypto_driver_wrappers.c
+###END_GENERATED_FILES###
diff --git a/programs/.gitignore b/programs/.gitignore
index d11db9e6b8..a641c31c45 100644
--- a/programs/.gitignore
+++ b/programs/.gitignore
@@ -5,10 +5,6 @@
 *.sln
 *.vcxproj
 
-# Generated source files
-/psa/psa_constant_names_generated.c
-/test/query_config.c
-
 aes/crypt_and_hash
 cipher/cipher_aead_demo
 hash/generic_sum
@@ -75,5 +71,11 @@ x509/crl_app
 x509/load_roots
 x509/req_app
 
+###START_GENERATED_FILES###
+# Generated source files
+/psa/psa_constant_names_generated.c
+/test/query_config.c
+
 # Generated data files
 pkey/keyfile.key
+###END_GENERATED_FILES###
diff --git a/tests/.gitignore b/tests/.gitignore
index 6db65d1d38..973ebb5083 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,11 +1,6 @@
 *.sln
 *.vcxproj
 
-# Generated source files
-/suites/*.generated.data
-/suites/test_suite_psa_crypto_storage_format.v[0-9]*.data
-/suites/test_suite_psa_crypto_storage_format.current.data
-
 *.log
 /test_suite*
 data_files/mpi_write
@@ -20,3 +15,10 @@ include/test/instrument_record_status.h
 src/libmbed*
 
 libtestdriver1/*
+
+###START_GENERATED_FILES###
+# Generated source files
+/suites/*.generated.data
+/suites/test_suite_psa_crypto_storage_format.v[0-9]*.data
+/suites/test_suite_psa_crypto_storage_format.current.data
+###END_GENERATED_FILES###
diff --git a/visualc/VS2013/.gitignore b/visualc/VS2013/.gitignore
index d3da304f78..a9ded4aab2 100644
--- a/visualc/VS2013/.gitignore
+++ b/visualc/VS2013/.gitignore
@@ -1,7 +1,3 @@
-# Files automatically generated by generate_visualc_files.pl
-/mbedTLS.sln
-/*.vcxproj
-
 # Files that may be left over from check-generated-files.sh
 /*.bak
 
@@ -12,3 +8,9 @@
 /Release/
 /*.vcxproj.filters
 /*.vcxproj.user
+
+###START_GENERATED_FILES###
+# Files automatically generated by generate_visualc_files.pl
+/mbedTLS.sln
+/*.vcxproj
+###END_GENERATED_FILES###

From 237f91a9ef116a83b7d92daf8a492721a888829b Mon Sep 17 00:00:00 2001
From: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
Date: Thu, 6 Jul 2023 18:00:48 +0100
Subject: [PATCH 2/5] Add script to manage gitignore anchors

Added scripts which comment and uncomment out patterns relating to
generated files.

Signed-off-by: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
---
 scripts/gitignore_add_generated_files.sh    | 23 +++++++++++++++++++++
 scripts/gitignore_remove_generated_files.sh | 23 +++++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 scripts/gitignore_add_generated_files.sh
 create mode 100644 scripts/gitignore_remove_generated_files.sh

diff --git a/scripts/gitignore_add_generated_files.sh b/scripts/gitignore_add_generated_files.sh
new file mode 100644
index 0000000000..27c3480826
--- /dev/null
+++ b/scripts/gitignore_add_generated_files.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+set -eu
+
+GITIGNORES=$(find . -name ".gitignore")
+
+for GITIGNORE in $GITIGNORES; do
+    IN_GEN_BLOCK=false
+    while read -r line; do
+        if [ "$line" = "###START_COMMENTED_GENERATED_FILES###" ]; then
+            IN_GEN_BLOCK=true
+            echo "###START_GENERATED_FILES###"
+        elif [ "$line" = "###END_COMMENTED_GENERATED_FILES###" ]; then
+            IN_GEN_BLOCK=false
+            echo "###END_GENERATED_FILES###"
+        elif $IN_GEN_BLOCK ; then
+            echo "${line:1}"
+        else
+            echo "$line"
+        fi
+    done <$GITIGNORE > "$GITIGNORE.tmp"
+    mv "$GITIGNORE.tmp" $GITIGNORE
+done
diff --git a/scripts/gitignore_remove_generated_files.sh b/scripts/gitignore_remove_generated_files.sh
new file mode 100644
index 0000000000..8314b2c238
--- /dev/null
+++ b/scripts/gitignore_remove_generated_files.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+
+set -eu
+
+GITIGNORES=$(find . -name ".gitignore")
+
+for GITIGNORE in $GITIGNORES; do
+    IN_GEN_BLOCK=false
+    while read -r line; do
+        if [ "$line" = "###START_GENERATED_FILES###" ]; then
+            IN_GEN_BLOCK=true
+            echo "###START_COMMENTED_GENERATED_FILES###"
+        elif [ "$line" = "###END_GENERATED_FILES###" ]; then
+            IN_GEN_BLOCK=false
+            echo "###END_COMMENTED_GENERATED_FILES###"
+        elif $IN_GEN_BLOCK ; then
+            echo "#$line"
+        else
+            echo "$line"
+        fi
+    done <$GITIGNORE > "$GITIGNORE.tmp"
+    mv "$GITIGNORE.tmp" $GITIGNORE
+done

From cf3554b4e8fcddfbc708780a6cc5122599258f3e Mon Sep 17 00:00:00 2001
From: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
Date: Thu, 6 Jul 2023 18:10:19 +0100
Subject: [PATCH 3/5] Update file permissions

New scripts have updated executable permissions to be consistent with
project requirements.

Signed-off-by: Agathiyan Bragadeesh <agabra02@e127300.arm.com>
---
 scripts/gitignore_add_generated_files.sh    | 0
 scripts/gitignore_remove_generated_files.sh | 0
 2 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 scripts/gitignore_add_generated_files.sh
 mode change 100644 => 100755 scripts/gitignore_remove_generated_files.sh

diff --git a/scripts/gitignore_add_generated_files.sh b/scripts/gitignore_add_generated_files.sh
old mode 100644
new mode 100755
diff --git a/scripts/gitignore_remove_generated_files.sh b/scripts/gitignore_remove_generated_files.sh
old mode 100644
new mode 100755

From 3bcff5431a120e3057e0904df12beb80401735d1 Mon Sep 17 00:00:00 2001
From: Agathiyan Bragadeesh <agathiyan.bragadeesh2@arm.com>
Date: Fri, 4 Aug 2023 14:05:28 +0100
Subject: [PATCH 4/5] Put both gitignore modifications in one script

New file also contains a header file and uses sed

Signed-off-by: Agathiyan Bragadeesh <agathiyan.bragadeesh2@arm.com>
---
 scripts/gitignore_add_generated_files.sh    | 23 -------
 scripts/gitignore_patch.sh                  | 71 +++++++++++++++++++++
 scripts/gitignore_remove_generated_files.sh | 23 -------
 3 files changed, 71 insertions(+), 46 deletions(-)
 delete mode 100755 scripts/gitignore_add_generated_files.sh
 create mode 100755 scripts/gitignore_patch.sh
 delete mode 100755 scripts/gitignore_remove_generated_files.sh

diff --git a/scripts/gitignore_add_generated_files.sh b/scripts/gitignore_add_generated_files.sh
deleted file mode 100755
index 27c3480826..0000000000
--- a/scripts/gitignore_add_generated_files.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-set -eu
-
-GITIGNORES=$(find . -name ".gitignore")
-
-for GITIGNORE in $GITIGNORES; do
-    IN_GEN_BLOCK=false
-    while read -r line; do
-        if [ "$line" = "###START_COMMENTED_GENERATED_FILES###" ]; then
-            IN_GEN_BLOCK=true
-            echo "###START_GENERATED_FILES###"
-        elif [ "$line" = "###END_COMMENTED_GENERATED_FILES###" ]; then
-            IN_GEN_BLOCK=false
-            echo "###END_GENERATED_FILES###"
-        elif $IN_GEN_BLOCK ; then
-            echo "${line:1}"
-        else
-            echo "$line"
-        fi
-    done <$GITIGNORE > "$GITIGNORE.tmp"
-    mv "$GITIGNORE.tmp" $GITIGNORE
-done
diff --git a/scripts/gitignore_patch.sh b/scripts/gitignore_patch.sh
new file mode 100755
index 0000000000..d0fba6d6fa
--- /dev/null
+++ b/scripts/gitignore_patch.sh
@@ -0,0 +1,71 @@
+#!/bin/bash
+#
+# Copyright The Mbed TLS Contributors
+# SPDX-License-Identifier: Apache-2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Purpose
+#
+# For adapting gitignore files for releases so generated files can be included.
+#
+# Usage: gitignore_add_generated_files.sh  [ -h | --help ] etc
+#
+
+set -eu
+
+print_usage()
+{
+    echo "Usage: $0"
+    echo -e "  -h|--help\t\tPrint this help."
+    echo -e "  -i|--ignore\t\tAdd generated files to the gitignores."
+    echo -e "  -u|--unignore\t\tRemove generated files from the gitignores."
+}
+
+if [[ $# -eq 0 ]]; then
+    print_usage
+    exit 1
+elif [[ $# -ge 2 ]]; then
+    echo "Too many arguments!"
+    exit 1
+fi
+
+case "$1" in
+    -i | --ignore)
+        IGNORE=true
+        ;;
+    -u | --uignore)
+        IGNORE=false
+        ;;
+    -h | --help | "")
+        print_usage
+        exit 1
+        ;;
+    *)
+        echo "Unknown argument: $1"
+        echo "run '$0 --help' for options"
+        exit 1
+esac
+
+GITIGNORES=$(find . -name ".gitignore")
+for GITIGNORE in $GITIGNORES; do
+    if $IGNORE; then 
+        sed -i '/###START_COMMENTED_GENERATED_FILES###/,/###END_COMMENTED_GENERATED_FILES###/s/^# //' $GITIGNORE
+        sed -i 's/###START_COMMENTED_GENERATED_FILES###/###START_GENERATED_FILES###/' $GITIGNORE
+        sed -i 's/###END_COMMENTED_GENERATED_FILES###/###END_GENERATED_FILES###/' $GITIGNORE
+    else
+        sed -i '/###START_GENERATED_FILES###/,/###END_GENERATED_FILES###/s/^/# /' $GITIGNORE
+        sed -i 's/###START_GENERATED_FILES###/###START_COMMENTED_GENERATED_FILES###/' $GITIGNORE
+        sed -i 's/###END_GENERATED_FILES###/###END_COMMENTED_GENERATED_FILES###/' $GITIGNORE
+    fi
+done
diff --git a/scripts/gitignore_remove_generated_files.sh b/scripts/gitignore_remove_generated_files.sh
deleted file mode 100755
index 8314b2c238..0000000000
--- a/scripts/gitignore_remove_generated_files.sh
+++ /dev/null
@@ -1,23 +0,0 @@
-#!/bin/bash
-
-set -eu
-
-GITIGNORES=$(find . -name ".gitignore")
-
-for GITIGNORE in $GITIGNORES; do
-    IN_GEN_BLOCK=false
-    while read -r line; do
-        if [ "$line" = "###START_GENERATED_FILES###" ]; then
-            IN_GEN_BLOCK=true
-            echo "###START_COMMENTED_GENERATED_FILES###"
-        elif [ "$line" = "###END_GENERATED_FILES###" ]; then
-            IN_GEN_BLOCK=false
-            echo "###END_COMMENTED_GENERATED_FILES###"
-        elif $IN_GEN_BLOCK ; then
-            echo "#$line"
-        else
-            echo "$line"
-        fi
-    done <$GITIGNORE > "$GITIGNORE.tmp"
-    mv "$GITIGNORE.tmp" $GITIGNORE
-done

From b8bd604379eff106cd3ebf92c7a8462223cf51fc Mon Sep 17 00:00:00 2001
From: Agathiyan Bragadeesh <agathiyan.bragadeesh2@arm.com>
Date: Fri, 4 Aug 2023 14:14:11 +0100
Subject: [PATCH 5/5] Remove trailing whitespace

Signed-off-by: Agathiyan Bragadeesh <agathiyan.bragadeesh2@arm.com>
---
 scripts/gitignore_patch.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/scripts/gitignore_patch.sh b/scripts/gitignore_patch.sh
index d0fba6d6fa..74ec66c1dc 100755
--- a/scripts/gitignore_patch.sh
+++ b/scripts/gitignore_patch.sh
@@ -59,7 +59,7 @@ esac
 
 GITIGNORES=$(find . -name ".gitignore")
 for GITIGNORE in $GITIGNORES; do
-    if $IGNORE; then 
+    if $IGNORE; then
         sed -i '/###START_COMMENTED_GENERATED_FILES###/,/###END_COMMENTED_GENERATED_FILES###/s/^# //' $GITIGNORE
         sed -i 's/###START_COMMENTED_GENERATED_FILES###/###START_GENERATED_FILES###/' $GITIGNORE
         sed -i 's/###END_COMMENTED_GENERATED_FILES###/###END_GENERATED_FILES###/' $GITIGNORE