From af0855b52fea03f19bc7c29064eb09d9acdb0a6c Mon Sep 17 00:00:00 2001 From: vjymisal0 Date: Wed, 30 Sep 2026 22:56:08 +0530 Subject: [PATCH 1/2] feat(cp): support custom commit message prefixes Signed-off-by: vjymisal0 --- bin/git-cp | 19 ++++++++++++++----- man/git-cp.md | 8 +++++++- tests/git-cp.bats | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 tests/git-cp.bats diff --git a/bin/git-cp b/bin/git-cp index cabb564b..4ed47f5a 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -4,17 +4,26 @@ set -euo pipefail PROGRAM=$0 CURRENT_FILENAME="" DESTINATION_FILENAME="" +MESSAGE_PREFIX="" function usage() { - echo 1>&2 "USAGE: ${PROGRAM} CURRENT_FILENAME DESTINATION_FILENAME" + echo 1>&2 "USAGE: ${PROGRAM} [--message-prefix PREFIX] CURRENT_FILENAME DESTINATION_FILENAME" } while [[ $# -gt 0 ]] do key="$1" - if [[ "$CURRENT_FILENAME" == "" ]]; then + if [[ "$key" == "--message-prefix" ]]; then + if [[ $# -lt 2 ]]; then + usage + exit 30 + fi + MESSAGE_PREFIX=$2 + shift 2 + continue + elif [[ "$CURRENT_FILENAME" == "" ]]; then CURRENT_FILENAME=$key elif [[ "$DESTINATION_FILENAME" == "" ]]; then DESTINATION_FILENAME=$key @@ -62,15 +71,15 @@ else # Move the original file to the new destination, in this branch git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" - git commit -nm "--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME" + git commit -nm "${MESSAGE_PREFIX}--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME" # Restore the original file to keep it in the history git checkout HEAD~ "${CURRENT_FILENAME}" - git commit -nm "--Restore $CURRENT_FILENAME" + git commit -nm "${MESSAGE_PREFIX}--Restore $CURRENT_FILENAME" # Switch to the original branch and merge this back in. git checkout - - git merge --no-ff "$BRANCH_NAME" -m "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" + git merge --no-ff "$BRANCH_NAME" -m "${MESSAGE_PREFIX}Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" # We're now done with the branch, so delete it. # We shouldn't need -D here, as we've already merged it back in. diff --git a/man/git-cp.md b/man/git-cp.md index 9e333d28..16e89422 100644 --- a/man/git-cp.md +++ b/man/git-cp.md @@ -3,18 +3,24 @@ git-cp(1) -- Copy a file keeping its history ## SYNOPSIS -`git-cp` <current_filename> <destination_filename> +`git-cp` [--message-prefix <prefix>] <current_filename> <destination_filename> ## DESCRIPTION Copy a file keeping its git history. This allows merge conflict handling. +Use `--message-prefix` to prepend text to each of the three commits created by the copy. Without it, commit messages remain unchanged. + ## EXAMPLES Copy README into README.txt $ git cp README README.txt + Associate the copy commits with issue #1252 + + $ git cp --message-prefix '#1252 ' README README.txt + ## AUTHOR Written by Rémy Hubscher <> diff --git a/tests/git-cp.bats b/tests/git-cp.bats new file mode 100644 index 00000000..6e99b650 --- /dev/null +++ b/tests/git-cp.bats @@ -0,0 +1,45 @@ +# shellcheck shell=bash + +source "$BATS_TEST_DIRNAME/test_util.sh" + +setup_file() { + test_util.setup_file + test_util.install_command cp +} + +setup() { + test_util.cd_test + test_util.git_init + printf 'original\n' > original.txt + git add original.txt + git commit -m 'initial' +} + +@test "prefixes all three copy commits when requested" { + run git cp --message-prefix '#1252 ' original.txt duplicate.txt + assert_success + run git log --format=%s + assert_success + assert_line '#1252 Copy original.txt into duplicate.txt' + assert_line '#1252 --Restore original.txt' + assert_line '#1252 --Duplicate original.txt history into duplicate.txt' + [ -f original.txt ] + [ -f duplicate.txt ] +} + +@test "rejects a missing prefix without changing the repository" { + run git cp original.txt duplicate.txt --message-prefix + assert_failure 30 + [ ! -e duplicate.txt ] + [ "$(git rev-list --count HEAD)" -eq 1 ] +} + +@test "keeps existing commit messages without the option" { + run git cp original.txt duplicate.txt + assert_success + run git log --format=%s + assert_success + assert_line 'Copy original.txt into duplicate.txt' + assert_line '--Restore original.txt' + assert_line '--Duplicate original.txt history into duplicate.txt' +} From 31824f624f63b63b77c894faa96b74315c8fcd77 Mon Sep 17 00:00:00 2001 From: vjymisal0 Date: Fri, 2 Oct 2026 15:53:44 +0530 Subject: [PATCH 2/2] feat(cp): replace --message-prefix with --commit-template Use a {} placeholder for the default commit message so text can be added before or after it, as suggested in review. Regenerate the git-cp man pages. Signed-off-by: vjymisal0 --- bin/git-cp | 20 +++++++++++++------- man/git-cp.1 | 20 ++++++++++++++++++-- man/git-cp.html | 16 ++++++++++++++-- man/git-cp.md | 10 +++++++--- tests/git-cp.bats | 18 ++++++++++++++---- 5 files changed, 66 insertions(+), 18 deletions(-) diff --git a/bin/git-cp b/bin/git-cp index 4ed47f5a..25732243 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -4,23 +4,29 @@ set -euo pipefail PROGRAM=$0 CURRENT_FILENAME="" DESTINATION_FILENAME="" -MESSAGE_PREFIX="" +COMMIT_TEMPLATE="{}" + +function commit_message() +{ + # Replace every {} in the template with the default commit message + printf '%s\n' "${COMMIT_TEMPLATE//\{\}/"$1"}" +} function usage() { - echo 1>&2 "USAGE: ${PROGRAM} [--message-prefix PREFIX] CURRENT_FILENAME DESTINATION_FILENAME" + echo 1>&2 "USAGE: ${PROGRAM} [--commit-template TEMPLATE] CURRENT_FILENAME DESTINATION_FILENAME" } while [[ $# -gt 0 ]] do key="$1" - if [[ "$key" == "--message-prefix" ]]; then + if [[ "$key" == "--commit-template" ]]; then if [[ $# -lt 2 ]]; then usage exit 30 fi - MESSAGE_PREFIX=$2 + COMMIT_TEMPLATE=$2 shift 2 continue elif [[ "$CURRENT_FILENAME" == "" ]]; then @@ -71,15 +77,15 @@ else # Move the original file to the new destination, in this branch git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" - git commit -nm "${MESSAGE_PREFIX}--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME" + git commit -nm "$(commit_message "--Duplicate $CURRENT_FILENAME history into $DESTINATION_FILENAME")" # Restore the original file to keep it in the history git checkout HEAD~ "${CURRENT_FILENAME}" - git commit -nm "${MESSAGE_PREFIX}--Restore $CURRENT_FILENAME" + git commit -nm "$(commit_message "--Restore $CURRENT_FILENAME")" # Switch to the original branch and merge this back in. git checkout - - git merge --no-ff "$BRANCH_NAME" -m "${MESSAGE_PREFIX}Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" + git merge --no-ff "$BRANCH_NAME" -m "$(commit_message "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME")" # We're now done with the branch, so delete it. # We shouldn't need -D here, as we've already merged it back in. diff --git a/man/git-cp.1 b/man/git-cp.1 index d9a17093..15f52fcd 100644 --- a/man/git-cp.1 +++ b/man/git-cp.1 @@ -1,12 +1,14 @@ .\" generated with Ronn-NG/v0.9.0 .\" http://github.com/apjanke/ronn-ng/tree/0.9.0 -.TH "GIT\-CP" "1" "March 2020" "" "Git Extras" +.TH "GIT\-CP" "1" "October 2026" "" "Git Extras" .SH "NAME" \fBgit\-cp\fR \- Copy a file keeping its history .SH "SYNOPSIS" -\fBgit\-cp\fR +\fBgit\-cp\fR [\-\-commit\-template