gitattributes - git smudge/clean filter between branches -
there many related questions involving smudge/clean filters - have spent hours reading them, , trying various options, still failing. hope can ask in way answer works me.
specifically, have read page of these answers link to:
tl;dr
its detailed question, summary is:
- can store
debug = false
in file on 1 branch, ,debug = true
in branch, using smudge/clean filters manage file? , how?
background
i have various remote repos hosted @ bitbucket. using sourcetree on win8, clone remote repos laptop. create different branches development, features, releases etc (following a successful git branching model better or worse).
i have android java class called dbug.java
contains boolean turns on/off various debug logging, mocking etc features in code.
public static final boolean debug = false;
i value false
on "production" (master) branch, , true
on feature branches.
- is possible using filters, or have misunderstood use case?
- i unsure if filters work between 2 branches of same locally hosted repo, or if filters work between 2 repos.
creating filters
working locally, checked out production branch. created test file called debug_flag.txt
following contents:
// false on production branch // true on other branches debug = false;
i created file in root of local repo called .gitattributes
, added filter reference it:
debug_flag.txt filter=debug_on_off
i updated .git/config
file filter definition:
[filter "debug_on_off"] clean = sed -e 's/debug = true/debug = false/' smudge = sed -s 's/debug = false/debug = true/'
- in understanding, should ensure file has false value in production, have true value when branch production.
- is correct understanding?
testing filters
i created new branch test
using:
git checkout -b test
i checked contents of file:
$ cat debug_flag.txt // false on production branch // true on other branches debug = false;
- i expected see value
true
in file - shouldn't "smudge" filter have run when checked out file?
i added new line file, , committed. switched production branch, , things weird.
if @ file in sourcetree, there no changes on branch since created. expect, since change made on different branch.
if @ file in terminal, or notepad++, see value has changed:
$ cat debug_flag.txt // false on production branch // true on other branches debug = true;
i have not yet merged change across test branch, have not made commit on production branch, yet the file has changed.
- it looks smudge filter run on file within branch, not across branches.
i'm missing vital piece of puzzle, , simple can spotted experience doing this.
my bet simple misunderstanding of concept.
pls prompt missing info...
update based on vonc's reply
setting basic filters worked quite well. defined filter in config
file as:
[filter "debug_on_off"] clean = sed -e 's/debug = true/debug = false/' smudge = sed -s 's/debug = false/debug = true/'
creating new branch fixes false -> true, merging changes true -> false.
confining change production (master) branch required custom scripts aware of branch being run from. config
file became:
[filter "debug_on_off"] clean = ./scripts/master_clean.sh smudge = ./scripts/master_smudge.sh
master_clean.sh:
#!/bin/sh branch=$(git rev-parse --symbolic --abbrev-ref head) if [ "master" = "$branch" ]; sed -e s/debug = true/debug = false/ $1 else cat $1 fi
master_smudge.sh:
#!/bin/sh branch=$(git rev-parse --symbolic --abbrev-ref head) if [ "master" = "$branch" ]; sed -e s/debug = false/debug = true/ $1 else cat $1 fi
at point, running inconsistencies between sourcetree seeing, , being shown in notepad++ contents of debug file. sourcetree showing changes, notepad++ not.
i accepting vonc's answer, since answers basic question posed.
however, implementing solution wrote, since solves underlying problem trying solve, in easier way (for me): retaining different config file on separate branches.
i expected see value true in file
you created new branch, not checked out content (sice content same branch in)
to force smudge run, @ top of repo:
git checkout head --
i have not yet merged change across test branch, have not made commit on production branch, yet file has changed.
that idea of content filter driver: modifies content, without affecting git status
(which still reports modified file "unchanged").
to have smudge acting differently per branch, recommend calling script starts looking name of current branch.
see example in older answer "best practice - git + build automation - keeping configs separate".
#!/bin/sh branch=$(git rev-parse --symbolic --abbrev-ref head)
Comments
Post a Comment