From 629fc03581fb453dde7592fc9690def24da318b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Thu, 19 Mar 2020 15:33:26 +0100 Subject: [PATCH 1/7] Add first version of git-cp --- bin/git-cp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 bin/git-cp diff --git a/bin/git-cp b/bin/git-cp new file mode 100755 index 0000000..55f4bd0 --- /dev/null +++ b/bin/git-cp @@ -0,0 +1,61 @@ +#!/usr/bin/env bash +PROGRAM=$0 +CURRENT_FILENAME="" +DESTINATION_FILENAME="" + +function current_branch() { + git rev-parse --abbrev-ref HEAD +} + +function usage() +{ + echo "USAGE: ${PROGRAM} CURRENT_FILENAME DESTINATION_FILENAME" +} + +while [[ $# > 0 ]] +do + key="$1" + + if [[ "$CURRENT_FILENAME" == "" ]]; then + CURRENT_FILENAME=$key + elif [[ "$DESTINATION_FILENAME" == "" ]]; then + DESTINATION_FILENAME=$key + else + usage + exit 30 # Error during arguments parsing + fi + shift # past argument or value +done + +if [[ "$DESTINATION_FILENAME" == "" ]]; then + usage + exit 20 # Missing arguments CURRENT_FILENAME +elif [[ "$CURRENT_FILENAME" == "" ]]; then + usage + exit 10 # Missing arguments CURRENT_FILENAME +else + echo "Coping $CURRENT_FILENAME into $DESTINATION_FILENAME" + INTERMEDIATE_FILENAME="${CURRENT_FILENAME//\//__}-move-to-${DESTINATION_FILENAME//\//__}" + + # We keep the existing file on the side in a commit + git mv "${CURRENT_FILENAME}" "${INTERMEDIATE_FILENAME}" + git commit -nm "Keep $CURRENT_FILENAME" + SAVED=$(git rev-parse HEAD) + + # We come back to the previous state + git reset --hard HEAD^ # Revert that change + + # We move the file to its new destination + git mv "${CURRENT_FILENAME}" "${DESTINATION_FILENAME}" + git add "${DESTINATION_FILENAME}" + git commit -nm "Copy $CURRENT_FILENAME into $DESTINATION_FILENAME" + + # We get back our copy + rm -f "${DESTINATION_FILENAME}" # Make sure the file doesn't exists somehow + git merge ${SAVED} + git commit -a -m "Duplicate ${CURRENT_FILENAME} history." # Keeping both files + + # We get back our original name + git mv "${INTERMEDIATE_FILENAME}" "${CURRENT_FILENAME}" + git commit -nm "Set back $CURRENT_FILENAME file." +fi From 739ca052b4e4096b15fcdf3b925e783ce4a56b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Thu, 19 Mar 2020 16:10:20 +0100 Subject: [PATCH 2/7] Add documentation. --- Commands.md | 9 ++++ etc/bash_completion.sh | 4 ++ man/git-cp.1 | 24 +++++++++ man/git-cp.html | 114 +++++++++++++++++++++++++++++++++++++++++ man/git-cp.md | 30 +++++++++++ 5 files changed, 181 insertions(+) create mode 100644 man/git-cp.1 create mode 100644 man/git-cp.html create mode 100644 man/git-cp.md diff --git a/Commands.md b/Commands.md index 3c31168..b18f5a4 100644 --- a/Commands.md +++ b/Commands.md @@ -15,6 +15,7 @@ - [`git commits-since`](#git-commits-since) - [`git contrib`](#git-contrib) - [`git count`](#git-count) + - [`git cp`](#git-cp) - [`git create-branch`](#git-create-branch) - [`git delete-branch`](#git-delete-branch) - [`git delete-merged-branches`](#git-delete-merged-branches) @@ -786,6 +787,14 @@ If you wish to omit the config section, you may use `--no-config`: $ git info --no-config ``` +## git cp + +Copy a file to another one keeping its history and allowing for merge conflits handling. + +```bash +$ git cp README.md README.rst +``` + ## git create-branch Create local branch `name`: diff --git a/etc/bash_completion.sh b/etc/bash_completion.sh index 78343bf..0bf2975 100644 --- a/etc/bash_completion.sh +++ b/etc/bash_completion.sh @@ -49,6 +49,10 @@ _git_count(){ __gitcomp "--all" } +__git_cp(){ + __git_complete_file +} + _git_delete_branch(){ __gitcomp "$(__git_heads)" } diff --git a/man/git-cp.1 b/man/git-cp.1 new file mode 100644 index 0000000..7a48c93 --- /dev/null +++ b/man/git-cp.1 @@ -0,0 +1,24 @@ +.\" generated with Ronn-NG/v0.8.0 +.\" http://github.com/apjanke/ronn-ng/tree/0.8.0 +.TH "GIT\-CP" "1" "March 2020" "" "" +.SH "NAME" +\fBgit\-cp\fR \- Copy a file keeping its history +.SH "SYNOPSIS" +\fBgit\-cp\fR +.SH "DESCRIPTION" +Copy a file keeping its git history\. This allow merge conflict handling\. +.SH "EXAMPLES" +Copy README into README\.txt +.IP "" 4 +.nf +$ git cp README README\.txt +.fi +.IP "" 0 +.SH "AUTHOR" +Written by Rémy Hubscher <\fI\%mailto:hubscher\.remy@gmail\.com\fR> +.SH "REPORTING BUGS" +<\fI\%https://github\.com/tj/git\-extras/issues\fR> +.SH "SEE ALSO" +<\fI\%https://github\.com/tj/git\-extras\fR> +.P +<\fI\%https://stackoverflow\.com/questions/16937359/git\-copy\-file\-preserving\-history/44036771#44036771\fR> diff --git a/man/git-cp.html b/man/git-cp.html new file mode 100644 index 0000000..31de9ee --- /dev/null +++ b/man/git-cp.html @@ -0,0 +1,114 @@ + + + + + + git-cp(1) - Copy a file keeping its history + + + + +
+ + + +
    +
  1. git-cp(1)
  2. +
  3. +
  4. git-cp(1)
  5. +
+ + + +

NAME

+

+ git-cp - Copy a file keeping its history +

+

SYNOPSIS

+ +

git-cp <current_filename> <destination_filename>

+ +

DESCRIPTION

+ +

Copy a file keeping its git history. This allow merge conflict handling.

+ +

EXAMPLES

+ +

Copy README into README.txt

+ +
$ git cp README README.txt
+
+ +

AUTHOR

+ +

Written by Rémy Hubscher <hubscher.remy@gmail.com>

+ +

REPORTING BUGS

+ +

<https://github.com/tj/git-extras/issues>

+ +

SEE ALSO

+ +

<https://github.com/tj/git-extras>

+ +

<https://stackoverflow.com/questions/16937359/git-copy-file-preserving-history/44036771#44036771>

+ +
    +
  1. +
  2. March 2020
  3. +
  4. git-cp(1)
  5. +
+ +
+ + diff --git a/man/git-cp.md b/man/git-cp.md new file mode 100644 index 0000000..ed05fe8 --- /dev/null +++ b/man/git-cp.md @@ -0,0 +1,30 @@ +git-cp(1) -- Copy a file keeping its history +============================================ + +## SYNOPSIS + +`git-cp` <current_filename> <destination_filename> + +## DESCRIPTION + +Copy a file keeping its git history. This allow merge conflict handling. + +## EXAMPLES + + Copy README into README.txt + + $ git cp README README.txt + +## AUTHOR + +Written by Rémy Hubscher <> + +## REPORTING BUGS + +<> + +## SEE ALSO + +<> + +<> From bea25d1ceb5a6175a0cac4cf573cf2a9609e02b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Thu, 19 Mar 2020 16:11:39 +0100 Subject: [PATCH 3/7] @mschneiderwind review. --- bin/git-cp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/bin/git-cp b/bin/git-cp index 55f4bd0..3120225 100755 --- a/bin/git-cp +++ b/bin/git-cp @@ -3,16 +3,12 @@ PROGRAM=$0 CURRENT_FILENAME="" DESTINATION_FILENAME="" -function current_branch() { - git rev-parse --abbrev-ref HEAD -} - function usage() { echo "USAGE: ${PROGRAM} CURRENT_FILENAME DESTINATION_FILENAME" } -while [[ $# > 0 ]] +while [[ $# -gt 0 ]] do key="$1" @@ -52,7 +48,7 @@ else # We get back our copy rm -f "${DESTINATION_FILENAME}" # Make sure the file doesn't exists somehow - git merge ${SAVED} + git merge "${SAVED}" git commit -a -m "Duplicate ${CURRENT_FILENAME} history." # Keeping both files # We get back our original name From 2a5c08aaf8ebc143f34d4155fe71cb7134001ee7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Thu, 19 Mar 2020 16:24:15 +0100 Subject: [PATCH 4/7] Fix checks. --- man/git-cp.1 | 6 +++--- man/git-cp.html | 6 +++--- man/git-extras.md | 1 + man/index.txt | 1 + 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/man/git-cp.1 b/man/git-cp.1 index 7a48c93..3b64eac 100644 --- a/man/git-cp.1 +++ b/man/git-cp.1 @@ -1,6 +1,6 @@ -.\" generated with Ronn-NG/v0.8.0 -.\" http://github.com/apjanke/ronn-ng/tree/0.8.0 -.TH "GIT\-CP" "1" "March 2020" "" "" +.\" 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" .SH "NAME" \fBgit\-cp\fR \- Copy a file keeping its history .SH "SYNOPSIS" diff --git a/man/git-cp.html b/man/git-cp.html index 31de9ee..a04df9e 100644 --- a/man/git-cp.html +++ b/man/git-cp.html @@ -2,7 +2,7 @@ - + git-cp(1) - Copy a file keeping its history