emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Renaming files with git not all that bad?


From: tomas
Subject: Re: Renaming files with git not all that bad?
Date: Thu, 9 Dec 2021 16:50:01 +0100

On Thu, Dec 09, 2021 at 03:55:34PM +0100, Stefan Kangas wrote:
> Yuri Khan <yuri.v.khan@gmail.com> writes:
> 
> > There is also a trick to split a file into multiple parts where each
> > part retains its history.
> 
> Thanks, I didn't know about this but will see if I can make it work
> for the splitting up of image-dired.el.

Based on [1], I once did:

  #+file: ~/bin/gitlib
  #+begin_src sh
    #!/bin/bash
    set -ex
    # FIXME sanity checks missing!
    # See git-sh-setup on some helpers for this
    if [ $# -lt 2 -o $# -gt 3 ] ; then
      echo "Usage: $0 orig copy [commit-message]"
      exit 1
    fi
    
    orig="$1"
    copy="$2"
    message=${3:-"split off $copy from $orig"}
    
    if [ -e "$copy" ] ; then
      echo "$copy already exists: bailing out"
      exit 1
    fi
    if [ ! -f "$orig" ] ; then
      echo "$orig isn't an existing file: bailing out"
      # FIXME might want to check that it /is/ a git file
      exit 1
    fi
    
    echo "$message"
    
    git mv "$orig" "$copy"
    git commit -nm "$message"
    REV=$(git rev-parse HEAD)
    git reset --hard HEAD^
    TMP=$(mktemp tmp-XXXXXX)
    # note that $TMP exists after the mktemp, thus -f
    git mv -f "$orig" "$TMP"
    git commit -nm "$message"
    # this fails because of conflicts: expected
    git merge "$REV" || true
    git commit -anm "$message"
    git mv "$TMP" "$orig"
    git commit -nm "$message"
  #+end_src

This is based on two things:
(1) if you happen to have a `git-foo' somewhere in your path, your git
   magically acquires a `foo' subcommand.
(2) I have ~/bin/libgit in my PATH.

It may be rough around the edges. It leaves a "smoke ring" in your
history -- I didn't try to conceal that (basically, it bifurcates the
repo, creating one version with the "first half" of a split and the
other with the "second half" -- which actually, at this point are just
copies under different names, and then merges this bifurcation):

  #+begin_quote
    * fb02315 (HEAD -> master) split-off keywords
    *   07fcba3 split-off keywords
    |\
    * | fb9944d split-off keywords
    | * 44b4849 split-off keywords
    |/
    * 8160976 Pass an expected failure
    * 86107d9 bulk-change all tests to our new test func
  #+end_quote

After having done all of that, I somehow decided that it ain't worth the
hassle and have used it very little. So please, use with care!

Cheers

[1] 
https://stackoverflow.com/questions/3887736/keep-git-history-when-splitting-a-file/53849651#53849651

-- 
t

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]