Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git smudge/clean filter between branches

Tags:

There are many related questions involving smudge/clean filters - I have spent some hours reading them, and trying various options, but still failing. I hope I can ask in a way that I get an answer that works for me.

Specifically, I have read the page most of these answers link back to:

  • Customizing Git - Git Attributes

tl;dr

Its a detailed question, but the summary is:

  • Can I store DEBUG = false in a file on one branch, and DEBUG = true in another branch, using smudge/clean filters to manage that file? And how?

Background

I have various remote repos hosted at bitbucket. I am using SourceTree on Win8, to clone the remote repos to my laptop. I create different branches for development, features, releases etc (following A successful Git branching model for better or worse).

I have an Android java class called Dbug.java that contains a boolean which turns on/off various debug logging, mocking etc features in my code.

public static final boolean DEBUG = false; 

I would like this value to be false on my "production" (master) branch, and to be true on my feature branches.

  • Is this possible using filters, or have I already misunderstood the use case?
  • I am unsure if filters work like this between 2 branches of the same locally hosted repo, or if the filters only work between 2 repos.

Creating the filters

Working locally, I checked out the production branch. I created a test file called debug_flag.txt with the following contents:

// false on production branch // true on other branches DEBUG = false; 

I created a file in the root of my local repo called .gitattributes and added the filter reference to it:

debug_flag.txt filter=debug_on_off 

I updated the .git/config file with the filter definition:

[filter "debug_on_off"]     clean = sed -e 's/DEBUG = true/DEBUG = false/'     smudge = sed -s 's/DEBUG = false/DEBUG = true/' 
  • In my understanding, this should ensure that my file always has a false value in production, but will have a true value when I branch from production.
  • Is this a correct understanding?

Testing the filters

I created a new branch test using:

git checkout -b test 

I checked the contents of my file:

$ cat debug_flag.txt  // false on production branch // true on other branches DEBUG = false; 
  • I expected to see the value true in the file
  • Shouldn't the "smudge" filter have run when I checked out the file?

I added a new line to the file, and committed. I then switched back to the production branch, and this is where things get weird.

If I look at the file in SourceTree, there are no changes on this branch since it was created. That is what I would expect, since the only change was made on a different branch.

If I look at the file in the terminal, or Notepad++, I see my value has changed:

$ cat debug_flag.txt  // false on production branch // true on other branches DEBUG = true; 

I have not yet merged the change across from the test branch, I have not made a commit on the production branch, yet the file has changed.

  • it looks like the smudge filter was run on the file within this branch, but not across branches.

I'm missing a vital piece of the puzzle, and hopefully it is something simple that can be spotted by someone with experience doing this.

My bet is this is a simple misunderstanding of the concept.

Pls prompt for any missing info...


Update based on VonC's reply

Setting up the basic filters worked quite well. Defined the filter in the config file as:

[filter "debug_on_off"]     clean = sed -e 's/DEBUG = true/DEBUG = false/'     smudge = sed -s 's/DEBUG = false/DEBUG = true/' 

Creating a new branch fixes false -> true, merging back changes true -> false.

Confining the change to just the production (master) branch required custom scripts that were aware of the branch they are being run from. So the 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" ]; then     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" ]; then     sed -e s/DEBUG = false/DEBUG = true/ $1 else     cat $1 fi 

At this point, I am running into inconsistencies between what SourceTree is seeing, and what is being shown in Notepad++ for the contents of the debug file. SourceTree is showing the changes, but Notepad++ is not.

I am accepting VonC's answer, since it answers the basic question I posed.

However, I will likely be implementing the solution I wrote, since it solves the underlying problem that I am trying to solve, in an easier way (for me): retaining a different config file on separate branches.

like image 865
Richard Le Mesurier Avatar asked Apr 07 '14 10:04

Richard Le Mesurier


1 Answers

I expected to see the value true in the file

You just created a new branch, not checked out its content (sice its content is the same as the branch you were in)

To force the smudge to run, do at the top of the repo:

git checkout HEAD -- 

I have not yet merged the change across from the test branch, I have not made a commit on the production branch, yet the file has changed.

That is the idea of a content filter driver: it modifies the content, without affecting git status (which still reports the modified file as "unchanged").

To have a smudge acting differently per branch, I would recommend calling a script which starts by looking the name of the current branch.
See an example in my older answer "Best practice - Git + Build automation - Keeping configs separate".

#!/bin/sh branch=$(git rev-parse --symbolic --abbrev-ref HEAD) 
like image 191
VonC Avatar answered Oct 01 '22 12:10

VonC