Moving code from another repo#

In the process of writing code in other DLS repos you may come to realise that it makes more sense to be in dodal. It is a good idea to keep the history for this code, you can do this by doing the following (we will be using moving devices from DiamondLightSource/hyperion as an example):

  1. Clone the codebase you are copying from:

    git clone git@github.com:DiamondLightSource/hyperion.git clone_for_history
    cd clone_for_history/
    
  2. Remove the remote to avoid any mistaken pushes:

    git remote rm origin
    
  3. Filter out only the directory/file you want to move:

    pip install git-filter-repo
    git-filter-repo --path file/to/move --path /other/file/to/move -f
    
  4. Clean everything up:

    git reset --hard
    git gc --aggressive
    git prune
    git clean -fd
    
  5. Add a note to every commit message to mention it’s been moved:

    git filter-branch --msg-filter 'sed "$ a \
    NOTE: Commit originally came from https://github.com/DiamondLightSource/hyperion"' -f -- --all
    
  6. If you have been using Github issue references in the old repository modify these to point to be more explicit (Note that this assumes the old repo uses #123 notation and only ever references issues from it’s own repo):

    git filter-branch -f --msg-filter 'sed "s|#[0-9]\+|DiamondLightSource/hyperion&|g"' -- --all
    
  7. Prepare the code to be in the correct structure for dodal:

    mkdir -p src/dodal/devices
    mv path/to/device src/dodal/devices/
    
  8. At this point it’s a good idea to check the log git log and the general directory structure to ensure it looks mostly correct

  9. Add and commit this (locally):

    git add .
    git commit -m "Prepare for import into dodal"
    
  10. In a different folder clone dodal, remove the origin (for now) to be safe and create a branch:

    git clone git@github.com:DiamondLightSource/dodal.git
    cd dodal
    git remote rm origin
    git checkout -b add_code_from_hyperion
    
  11. Add the source repo as a remote for dodal:

    git remote add source /path/to/source/old_repo/.git
    
  12. Pull from the source repo:

    git pull --no-rebase source main --allow-unrelated-histories
    
  13. This is another point where it’s a good idea to check the log git log and the general directory structure to ensure it looks mostly correct

  14. Remove the source remote and re-add origin:

    git remote rm source
    git remote add origin git@github.com:DiamondLightSource/dodal.git
    
  15. Tidy up the code so that it fits into the dodal repo e.g. in the Hyperion case we had to change the tests to import from hyperion to import from dodal and add some more dependencies.