|
| 1 | +#!/bin/bash |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under the BSD-style license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +set -eux |
| 9 | + |
| 10 | +# Check if dir1's files are also found in dir2 with the same |
| 11 | +# contents. Exempt files named BUCK, CMakeLists.txt, TARGETS, or |
| 12 | +# targets.bzl. |
| 13 | + |
| 14 | +if [ $# -ne 2 ]; then |
| 15 | + echo "Usage: $0 dir1 dir2" >&2 |
| 16 | + exit 1 |
| 17 | +fi |
| 18 | +dir1="$1" |
| 19 | +dir2="$2" |
| 20 | + |
| 21 | +if [ ! -d "$dir1" ] || [ ! -d "$dir2" ]; then |
| 22 | + echo "Error: Both directories must exist" >&2 |
| 23 | + exit 1 |
| 24 | +fi |
| 25 | + |
| 26 | +exit_status=0 |
| 27 | +while IFS= read -r -d '' file; do |
| 28 | + base=$(basename "$file") |
| 29 | + case "$base" in |
| 30 | + "BUCK"|"CMakeLists.txt"|"TARGETS"|"targets.bzl") |
| 31 | + continue |
| 32 | + ;; |
| 33 | + esac |
| 34 | + # Construct the corresponding path in the second directory |
| 35 | + file2="$dir2/${file#$dir1/}" |
| 36 | + # Check if the corresponding file exists in the second directory |
| 37 | + if [ ! -f "$file2" ]; then |
| 38 | + echo "Error: File '$file' found in '$dir1' but not found in '$dir2'" >&2 |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + # Compare the contents of the two files using diff |
| 42 | + set +ex |
| 43 | + differences=$(diff -u -p "$file" "$file2") |
| 44 | + set -e # leave x off |
| 45 | + # If there are any differences, print an error message and exit with failure status |
| 46 | + if [ -n "$differences" ]; then |
| 47 | + echo "Error: Mismatch detected in file '$file':" >&2 |
| 48 | + echo "$differences" >&2 |
| 49 | + exit_status=1 |
| 50 | + fi |
| 51 | + set -x |
| 52 | +done < <(find "$dir1" -type f -print0) |
| 53 | + |
| 54 | +exit $exit_status |
0 commit comments