microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/dev-tools/pr-ref-gen.sh
110lines · modeblame
4927284dBill Berry8 months ago | 1 | #!/usr/bin/env bash |
284b456dlittleKitchen5 months ago | 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT | |
4927284dBill Berry8 months ago | 4 | |
| 5 | # Script to generate a PR reference file with commit history and full diff | |
| 6 | # This file will be used by GitHub Copilot to generate accurate PR descriptions | |
| 7 | # | |
| 8 | # The script compares the current branch with a specified base branch (default: main) | |
| 9 | # and generates an XML file with commit history and diff information | |
| 10 | | |
| 11 | # Display usage information | |
| 12 | function show_usage { | |
| 13 | echo "Usage: $0 [--no-md-diff] [--base-branch BRANCH] [--output FILE]" | |
| 14 | echo "" | |
| 15 | echo "Options:" | |
| 16 | echo " --no-md-diff Exclude markdown files (*.md) from the diff output" | |
| 17 | echo " --base-branch Specify the base branch to compare against (default: main)" | |
| 18 | echo " --output Specify output file path (default: .copilot-tracking/pr/pr-reference.xml)" | |
| 19 | exit 1 | |
| 20 | } | |
| 21 | | |
| 22 | # Get the repository root directory | |
| 23 | REPO_ROOT=$(git rev-parse --show-toplevel) | |
| 24 | | |
| 25 | # Process command line arguments | |
| 26 | NO_MD_DIFF=false | |
| 27 | BASE_BRANCH="origin/main" | |
| 28 | OUTPUT_FILE="${REPO_ROOT}/.copilot-tracking/pr/pr-reference.xml" | |
| 29 | while [[ $# -gt 0 ]]; do | |
| 30 | case "$1" in | |
| 31 | --no-md-diff) | |
| 32 | NO_MD_DIFF=true | |
| 33 | shift | |
| 34 | ;; | |
| 35 | --base-branch) | |
| 36 | if [[ -z $2 || $2 == --* ]]; then | |
| 37 | echo "Error: --base-branch requires an argument" | |
| 38 | show_usage | |
| 39 | fi | |
| 40 | BASE_BRANCH="$2" | |
| 41 | shift 2 | |
| 42 | ;; | |
| 43 | --output) | |
| 44 | if [[ -z $2 || $2 == --* ]]; then | |
| 45 | echo "Error: --output requires an argument" | |
| 46 | show_usage | |
| 47 | fi | |
| 48 | OUTPUT_FILE="$2" | |
| 49 | shift 2 | |
| 50 | ;; | |
| 51 | --help | -h) | |
| 52 | show_usage | |
| 53 | ;; | |
| 54 | *) | |
| 55 | echo "Unknown option: $1" | |
| 56 | show_usage | |
| 57 | ;; | |
| 58 | esac | |
| 59 | done | |
| 60 | | |
| 61 | # Verify the base branch exists | |
| 62 | if ! git rev-parse --verify "${BASE_BRANCH}" &>/dev/null; then | |
| 63 | echo "Error: Branch '${BASE_BRANCH}' does not exist or is not accessible" | |
| 64 | exit 1 | |
| 65 | fi | |
| 66 | | |
| 67 | # Set output file path | |
| 68 | PR_REF_FILE="${OUTPUT_FILE}" | |
| 69 | mkdir -p "$(dirname "$PR_REF_FILE")" | |
| 70 | | |
| 71 | # Create the reference file with commit history using XML tags | |
| 72 | { | |
| 73 | echo "<commit_history>" | |
| 74 | echo " <current_branch>" | |
| 75 | git --no-pager branch --show-current | |
| 76 | echo " </current_branch>" | |
| 77 | echo "" | |
| 78 | | |
| 79 | echo " <base_branch>" | |
| 80 | echo " ${BASE_BRANCH}" | |
| 81 | echo " </base_branch>" | |
| 82 | echo "" | |
| 83 | | |
| 84 | echo " <commits>" | |
| 85 | # Output commit information including subject and body | |
| 86 | git --no-pager log --pretty=format:"<commit hash=\"%h\" date=\"%cd\"><message><subject><\![CDATA[%s]]></subject><body><\![CDATA[%b]]></body></message></commit>" --date=short "${BASE_BRANCH}"..HEAD | |
| 87 | echo " </commits>" | |
| 88 | echo "" | |
| 89 | | |
| 90 | # Add the full diff, excluding specified files | |
| 91 | echo " <full_diff>" | |
| 92 | # Exclude prompts and this file from diff history | |
| 93 | if [ "$NO_MD_DIFF" = true ]; then | |
| 94 | git --no-pager diff "${BASE_BRANCH}" -- ':!*.md' | |
| 95 | else | |
| 96 | git --no-pager diff "${BASE_BRANCH}" | |
| 97 | fi | |
| 98 | echo " </full_diff>" | |
| 99 | echo "</commit_history>" | |
| 100 | } >"${PR_REF_FILE}" | |
| 101 | | |
| 102 | LINE_COUNT=$(wc -l <"${PR_REF_FILE}" | awk '{print $1}') | |
| 103 | | |
| 104 | echo "Created ${PR_REF_FILE}" | |
| 105 | if [ "$NO_MD_DIFF" = true ]; then | |
| 106 | echo "Note: Markdown files were excluded from diff output" | |
| 107 | fi | |
| 108 | echo "Lines: $LINE_COUNT" | |
| 109 | echo "Base branch: $BASE_BRANCH" | |
| 110 | echo "File name: $PR_REF_FILE" |