public
Last active

Procedure to take a library that contains files for multiple programming languages, and split off the language specific features into separate branches

  • Download Gist
update-branches.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
cat << EOF > /dev/null
 
These procedures are helpful if you have a structure like this in your source tree and it can be anything like
Google protobuf, grammar syntax, shared source code of some sort that gets transformed to Python and C++, or
really anything that has a common base from which it is derived but when included (subtree'd/submoduled in git)
we only need the language specific part, not the rest.
 
What we do is create completely new branches in which we place a copy from the proper subdirectory from the
master branch. Yes that technically means we are duplicating files/commits in two different locations, but it
is a better option that having your Python specific source tree carrying around C++ files it does not need.
 
master -\
|- cpp
\ - file1.cpp
|- python
\ - file1.py
|- src
|- parser.sh
 
lang/python -\
| - file1.py
 
lang/cpp -\
| - file1.cpp
 
Then when you use parser.sh to transform your src to either cpp or python, you can use the following steps
to update the branches.
 
EOF
 
# +++
git clone ... # If you don't already have a copy of the mainline sitting around
 
# or
 
git checkout master
git fetch origin
git pull origin
# +++
 
# +++
# Create the branches for the first time
 
git branch lang/python master
git branch lang/cpp master
 
git checkout lang/python
git filter-branch -f --subdirectory-filter python HEAD
 
# Push the local branch to origin and set it to track the remote
# in the next update you can then use git push instead...
git push -u origin lang/python
 
git checkout lang/cpp
git filter-branch -f --subdirectory-flter cpp HEAD
 
git push -u origin lang/cpp
 
# +++
# or update the branches you currently have
 
# Create local branches that track the remote branches
git branch lang/python origin/lang/python
git branch lang/cpp origin/lang/cpp
 
# Checkout the entire source code from master so we can transform it...
git checkout -b lang/python-update master
git filter-branch -f --subdirectory-filter python HEAD
git checkout lang/python
git merge lang/python-update
 
# Move back to the master
git checkout master
 
# Checkout the entire source code from master so we can transform it...
git checkout -b lang/cpp-update master
git filter-branch -f --subdirectory-filter cpp HEAD
git checkout lang/cpp
git merge lang/cpp-update
 
# Move back to master
git checkout master
 
# This will push lang/python, lang/cpp because we set them up to track the remote when we branched them...
git push
 
# Delete the old branches, uses uppercase D because otherwise it will complain about not fully merged
# (mainly because you are on master, and master off course has no record of these branches since they are stand-alone)
git branch -D lang/python-update
git branch -D lang/cpp-update
 
# +++

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.