hackade.org/ code/ shell/ commands/ export git commits.sh

#!/usr/bin/env bash

if [[ $# -ne 1 ]]; then
  echo "Usage: $0 <repo_path>"
  exit 1
fi

REPO_PATH="${1}"
PATCHES_PATH="$(mktemp -d)"

read -r -p 'Select your first commit to export'
FIRST_COMMIT_LINE=$(git -C "${REPO_PATH}" log --pretty=format:'%h: %cn <%ce>, %ah %ar, %s' | fzf)
read -r -p 'Select your last commit to export'
LAST_COMMIT_LINE=$(git -C "${REPO_PATH}" log --pretty=format:'%h: %cn <%ce>, %ah %ar, %s' | fzf)

FIRST_COMMIT_HASH=$(echo "${FIRST_COMMIT_LINE}" | cut -d':' -f1)
LAST_COMMIT_HASH=$(echo "${LAST_COMMIT_LINE}" | cut -d':' -f1)

git -C "${REPO_PATH}" format-patch --output-directory "${PATCHES_PATH}" "${LAST_COMMIT_HASH}~..${FIRST_COMMIT_HASH}"