Reputation: 599
I'm working on a Django project whose __init__.py
file contains a variable, DOCUMENTS_CACHE = True
, which causes a report's output to be cached when it is run. This is the way the variable is declared in our dev
and master
branches.
When I am developing and testing locally, I often need to run the report many times, re-using the same inputs, to make sure the output is changing. To avoid having the report cached (and seeing the report output as it looked after the first run, despite making changes), I set DOCUMENTS_CACHE = False
.
For each feature I work on, I create a new branch with Git based on the current state of the dev
branch, so I'm always starting a new feature with DOCUMENTS_CACHE = True
and I always need to manually change it to DOCUMENTS_CACHE = False
.
I can't push this change to dev
since it would mess things up for my colleagues.
Questions
How can I make this small change to each new branch I create? How do I avoid accidentally adding, committing and pushing this? The two options I'm aware of are below, but both rely on me remembering to do stuff. Is there a programmatic way to replace the dev
version of the file in each new branch while I develop locally, and then re-introduce the dev
version of the file before I push?
git checkout __init.py__
at the end to restore it to its original state before pushing?git stash
and git stash apply
every time I switch branches?It may seem like I'm overthinking such a small thing, but this is my first software engineering role and I'm really trying to learn as much about Git and proper workflow as possible. Thanks!
Upvotes: 2
Views: 52
Reputation: 3322
I agree that solving this problem with environment variables is a much cleaner solution, but just for completion, here is how you would achieve this with git.
Git has hooks, which are bash scripts that run on git events. You would need to configure three.
First post-checkout
and post-commit
You will want to do something like
BRANCH=$(git branch | grep '^\*' | cut -b3- | cut -f1,2 -d'-')
BRANCHTYPE=${BRANCH%/*}
if [[ $BRANCHTYPE == 'master' ]] || [[ $BRANCHTYPE == 'develop' ]]; then
echo 'DOCUMENTS_CACHE = True' > __init.py__
else
echo 'DOCUMENTS_CACHE = False' > __init.py__
fi
And in pre-commit
echo 'DOCUMENTS_CACHE = True' > __init.py__
Upvotes: 1