feat: ngcp last

This commit is contained in:
2026-08-11 17:26:16 +02:00
parent 9865dba535
commit 2b005aee35
+39 -18
View File
@@ -43,6 +43,7 @@ for flag in "$@"; do
echo "Usage: ngcp [mode] [options]" echo "Usage: ngcp [mode] [options]"
echo "Mode:" echo "Mode:"
echo " pick <commit 1> <commit2> <...> Cherry-pick commits for the remote branch." echo " pick <commit 1> <commit2> <...> Cherry-pick commits for the remote branch."
echo " last <int n> Cherry-pick the last n commits."
echo " pull Pulls the changes to the local branch." echo " pull Pulls the changes to the local branch."
echo "Options:" echo "Options:"
echo " --automatic Exit with no changes if merge conflict and instructs the user to pull manually. Use this option for automation." echo " --automatic Exit with no changes if merge conflict and instructs the user to pull manually. Use this option for automation."
@@ -52,7 +53,28 @@ done
# checks if connection # checks if connection
if [[ "$1" == "pick" ]]; then if [[ "$1" == "pick" || "$1" == "last" ]]; then
if [[ "$1" == "last" ]]; then
if [[ "$#" -lt 2 ]] || ! [[ "$2" =~ ^[1-9][0-9]*$ ]]; then
echo "Invalid arguments. Usage: ngcp last <number>"
exit 1
fi
mapfile -t commits < <(
git -C "$nixConfigPath" log --format='%H' --reverse -n "$2" HEAD
)
echo "Cherry-picking the last ${#commits[@]} commits:"
printf ' %s\n' "${commits[@]}"
else
if [[ "$#" -lt 2 ]]; then
echo "Invalid arguments, you must specify commit hashes"
exit 1
fi
commits=("${@:2}")
fi
if [[ "$#" -lt "2" ]]; then if [[ "$#" -lt "2" ]]; then
echo "Invalid arguments, you must specify commit hashes". echo "Invalid arguments, you must specify commit hashes".
exit exit
@@ -82,23 +104,22 @@ if [[ "$1" == "pick" ]]; then
done done
echo "Pull successful" echo "Pull successful"
fi fi
for commit in "${@:2}"; do for commit in "${commits[@]}"; do
if ! [[ $commit =~ --.* ]]; then # if not a flag if git -C "$nixConfigPath" cherry-pick "$commit"; then
if git -C "$nixConfigPath" cherry-pick "$commit"; then echo "Applied $commit successfully"
echo "Applied $commit successfully" else
else echo "Cherry-pick of $commit failed."
echo "Cherry-pick of $commit failed." echo "Resolve conflicts and run:"
echo "Resolve conflicts and run:" echo " git cherry-pick --continue"
echo " git cherry-pick --continue" echo
echo echo "Type 'y' when done."
echo "Type 'y' when done."
while git -C "$nixConfigPath" rev-parse --verify CHERRY_PICK_HEAD >/dev/null 2>&1; do
while git -C "$nixConfigPath" rev-parse --verify CHERRY_PICK_HEAD >/dev/null 2>&1; do read -r answer
read -r answer [[ "$answer" == "y" ]] || continue
[[ "$answer" == "y" ]] || continue done
done
echo "Cherry-pick for $commit resolved." echo "Cherry-pick for $commit resolved."
fi
fi fi
done done
git -C "$nixConfigPath" push --force origin "$remoteBranch" git -C "$nixConfigPath" push --force origin "$remoteBranch"