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