Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git conflicts with JSON files

Tags:

Our website is localized using a bunch of JSON files with translations (one file per language). The content of the files looks like this:

{
    "Password": "Passwort",
    "Tables": "Tische"
}

Many team members edit these JSON files at the same time, adding new phrases and editing existing ones, and we get lots of conflicts even though people are changing different lines.

Is there a way to set up git in such a way that would help avoid merge conflicts?

P.S. I've found this script to help merge locally: https://gist.github.com/jphaas/ad7823b3469aac112a52. However, I'm interested in a solution that would fix the problem for everyone in the team (even for persons who edit JSONs through GitHub's web-interface).

like image 829
katspaugh Avatar asked Oct 14 '15 09:10

katspaugh


People also ask

How do I resolve stash merge conflicts?

The stash entry is kept in case you need it again. There's no magic remedy for such merge conflicts. The only option for developers is to edit the file by hand and keep what they want and dispose of what they don't want. Once they merge and save the file, they will have effectively resolved the git stash conflict.


2 Answers

we get lots of conflicts even though people are changing different lines

This shouldn't be the case, you only get conflicts if same line is modified by different people, committed and then later merged.

Oh, I actually tried this out and encountered some odd problems.

Commit 1 (master):

{
    "a": "1",
    "b": "2",
    "c": "3",
    "d": "4",
    "e": "5",
    "f": "6",
    "g": "7"
}

Commit 2 (tmp)

{
    "A": "1",
    "B": "2",
    "C": "3",
    "d": "4",
    "e": "5",
    "f": "6",
    "g": "7"
}

Commit 3 (master):

{
    "a": "1",
    "b": "2",
    "c": "3",
    "d": "4",
    "E": "5",
    "F": "6",
    "G": "7"
}

git merge tmp: correct result

{
    "A": "1",
    "B": "2",
    "C": "3",
    "d": "4",
    "E": "5",
    "F": "6",
    "G": "7"
}

However I get conflicts if also row "d" was modified, maybe git wasn't able to establish diff boundaries. My stupid suggestion to avoid this stupid git behavior is to add "padding" to the JSON file (ugly, isn't it? But no more conflicts):

{
    "a": "1",

    "b": "2",

    "c": "3",

    "d": "4",

    "e": "5",

    "f": "6",

    "g": "7"
}
like image 137
NikoNyrh Avatar answered Sep 21 '22 14:09

NikoNyrh


One thing I would do in such a scenario would be to maintain the configurations in a database table instead of a JSON file - if they change all that frequently. As others have already pointed out, there is not much you can do to avoid conflicts if you have that high number of changes happening to the config all the time. Your example anyway looks more like a mapping between word in English and some other language, so a three column table should suffice.

The JSON file, if needed could be generated either on the fly every time or generated once during deployment for each server from the database table.

like image 33
Anshul Goyal Avatar answered Sep 21 '22 14:09

Anshul Goyal