Skip to content

Instantly share code, notes, and snippets.

@mclausen
Created September 10, 2024 07:33
Show Gist options
  • Save mclausen/de3eb9eded937b7bbd47d8ec3915be39 to your computer and use it in GitHub Desktop.
Save mclausen/de3eb9eded937b7bbd47d8ec3915be39 to your computer and use it in GitHub Desktop.
Create a git diff file between HEAD on your current branch and HEAD on main branch. Which can be used to generate Pull Requests Descriptions using ChatGPT (See: https://dev.to/martinhc/productivity-hack-of-the-day-creating-pull-requests-descriptions-from-git-diff-1pm0)
# Check if the git command is available
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
Write-Host "Git is not installed or not available in the PATH." -ForegroundColor Red
exit
}
# Ensure we're in a git repository
$gitStatus = git rev-parse --is-inside-work-tree 2>&1
if ($gitStatus -ne "true") {
Write-Host "This is not a git repository." -ForegroundColor Red
exit
}
# Get the current branch name
$currentBranch = git rev-parse --abbrev-ref HEAD
if ($currentBranch -eq "main") {
Write-Host "You are on the 'main' branch. Please switch to a feature branch." -ForegroundColor Yellow
exit
}
# Fetch the latest changes for the main branch
git fetch origin main
# Get the latest commit on the main branch
$mainCommit = git rev-parse origin/main
# Get the latest commit on the current branch
$currentBranchCommit = git rev-parse HEAD
# Generate the diff between the latest commit on main and the current branch
$diffFileName = "diff.patch"
git diff $mainCommit $currentBranchCommit > $diffFileName
# Check if the diff file was generated successfully
if (Test-Path $diffFileName) {
Write-Host "Diff file generated successfully: $diffFileName" -ForegroundColor Green
} else {
Write-Host "Failed to generate diff file." -ForegroundColor Red
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment