3

I have following json file:

{
  "UseSqlite": false,
  "UsersAvatarsFolder": "uploads",
  "UserDefaultPhoto": "no_image.jpg"
}

Now I want to tell git to ignore the "UseSqlite": false, line So I followed this solution to ignore this specific line using .gitattributesfile:

*.json filter=ignoreSqlite

Then defined this filter in the gitconfig:

git config --global filter.ignoreSqlite.clean 'sed "s/"UseSqlite": .*/"UseSqlite": true/"'
git config --global filter.ignoreSqlite.smudge cat

But it seems that, it doesn't work:

'sed: -c: line 0: unexpected EOF while looking for matching `''
'sed: -c: line 1: syntax error: unexpected end of file
error: external filter 'sed failed 1
error: external filter 'sed failed
On branch master

I am not sure about sed syntax. Could you please take a look at it and let me know what's the correct syntax for it?

Update:

I finally fixed the syntax:

git config --global filter.ignoreSqlite.smudge "sed 's/"UseSqlite": .*/"UseSqlite": true,/'"
git config --global filter.ignoreSqlite.clean "sed 's/"UseSqlite": .*/"UseSqlite": false,/'"

But it also doesn't work, I want when I push the modification, the "UseSqlite" be true and when I pull the "UseSqlite" be false. But these filters don't work like that, Any idea?

Community
  • 1
  • 1
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
  • Getting the `sed` and shell syntax right is tricky because each step—Git itself reading your configurations, the shell running the filter, and sed itself interpreting a command—adds its own layer of quote, white-space, backslash and/or other meta-character actions. But as Schwern notes, separating this into "default, and version-controlled" and "localized, but emphatically *not* versioned as part of the software" config files is the way to go here. (Note that the *local* config can be a symbolic link into a local configuration repository work-tree, if you wish.) – torek Feb 26 '17 at 21:57

1 Answers1

1

I'm gonna guess what you've got is a config file where you want to make sure local modifications in the checkout don't get accidentally checked in. In this case, I'm going to bet you're using SQLite for testing. There are easier ways to do that which also make the system more flexible.

Simplest thing to do is have two config files: one for production and one for testing. The system defaults to the production one, but your test harness chooses the testing one.

But that introduces duplication, so what you really want is to merge together several config files. You have a config file full of defaults that generally isn't touched, then you have a local config file. The system merges the two together. For example...

config/default.json
    {
      "UseSqlite": false,
      "UsersAvatarsFolder": "uploads",
      "UserDefaultPhoto": "no_image.jpg"
    }

config/local.json
    {
      "UseSqlite": true
    }

The system loads both and the resulting config is:

    {
      "UseSqlite": true,
      "UsersAvatarsFolder": "uploads",
      "UserDefaultPhoto": "no_image.jpg"
    }

Then config/local.json can be ignored by git.

This is both useful for testing and for your users. Now when they update they won't lose their changes to the configuration. config/default.json gets updated and they'll get any new or changed defaults, while keeping their own customizations in config/local.json.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thanks, Yes that's exactly what I'm going to achieve, But I wonder why the config file doesn't go through the filter (please see the updated question)? – Sirwan Afifi Feb 26 '17 at 17:47
  • @SirwanAfifi You still have unescaped double quotes inside double quotes. – Schwern Feb 26 '17 at 17:53
  • you are right, Should I use \" to escape it, something like this: `"sed 's/\""UseSqlite\"": .*/\""UseSqlite\"": true,/'"` – Sirwan Afifi Feb 26 '17 at 18:00
  • @SirwanAfifi You're doubling up. It's probably `"sed 's/\"UseSqlite\": .*/\"UseSqlite\": true,/'"`. – Schwern Feb 26 '17 at 18:22
  • 1
    @SirwanAfifi Sorry, I don't know `sed` nor the Git content filter. It's not a very good fix anyway, improving your config handling is. – Schwern Feb 26 '17 at 18:28