Guest

Shell Scripting: Generating Diff of GitLab Merge Request

Dec 12th, 2025
13
0
Never
Not a member of gistpad yet? Sign Up, it unlocks many cool features!
Bash 5.04 KB | Source Code | 0 0
  1. #!/bin/bash
  2. set -euo pipefail
  3.  
  4. # Check if an ID was provided
  5. if [ -z "${1:-}" ]; then
  6. echo "Usage: get_mr_diff <MR_ID> [BASE_REF]"
  7. echo "Example: get_mr_diff 1234 origin/develop"
  8. exit 1
  9. fi
  10.  
  11. mr_id="$1"
  12. base_ref="${2:-origin/develop}" # target branch ref (default)
  13.  
  14. output_dir="/Users/arun/Downloads/fulldiff"
  15. output_file="${output_dir}/MR-${mr_id}.diff"
  16.  
  17. mkdir -p "$output_dir"
  18.  
  19. # Make sure base ref exists/updated locally
  20. # If base_ref is origin/<branch>, fetch that branch
  21. if [[ "$base_ref" == origin/* ]]; then
  22. base_branch="${base_ref#origin/}"
  23. git fetch origin "$base_branch" >/dev/null 2>&1 || true
  24. fi
  25.  
  26. # Fetch the specific MR head (Hidden GitLab Ref)
  27. echo "⬇️ Fetching changes for MR ${mr_id}..."
  28. git fetch origin "refs/merge-requests/${mr_id}/head" >/dev/null 2>&1
  29.  
  30. # Generate the NET diff (final changes only)
  31. # Triple-dot uses merge-base(base_ref, FETCH_HEAD) -> FETCH_HEAD
  32. echo "📝 Writing NET diff to file..."
  33. git diff "$base_ref...FETCH_HEAD" > "$output_file"
  34.  
  35. echo "✅ Done! File saved at:"
  36. echo "$output_file"
RAW Gist Data Copied