0

I have a set of files (file1.. fileN), they all have the same structure, but this structure can be different each time (json, xml, text... is undefined, but it is always the same type of information) I need to do a change in all of them so if I add a field in an xml it must be in all of them. The same when I add a new line in a plain text. I've thinking to use git for this purpose, I could do the change in one of the files and then merge this file in the other ones.

File1:

{name: "Harry", 
 lastname: "Potter"}

change:

{name: "Harry", 
 lastname: "Potter",
 age: 12}

file2:

{name: "Hermion", 
 lastname: "Granger"}

file2 after bing processed:

{name: "Hermion", 
 lastname: "Granger",
 age: 12}

I know you can use a json parser to do it but since the information can be plain text or another different kind of information it is not valid in this case, I need a way to do the changes when the structure is the same not based in the information encoding.

Is it possible? How can I do it?

David Marciel
  • 865
  • 1
  • 12
  • 29
  • 1
    Git is not the right tool at all for this. You'll need to write something up for each format. (Or use a single format and write a tool to convert from that to all the other formats you need.) – Mat Feb 24 '20 at 08:19

1 Answers1

0

I figured out a way:

You can get a git diff and apply it later:

https://stackoverflow.com/a/1191449/4670625

Also, you can get the ids of the previous git commits:

https://stackoverflow.com/a/32324539/4670625

When files are different or in different branches you can use:

Git: How to diff two different files in different branches?

There is a good explanation about diff and patch here:

https://www.pair.com/support/kb/paircloud-diff-and-patch/

All together, you get the id of the previous 2 commits, then you do a diff between them and apply it later.

EDIT:

you can get a diff using:

git diff file1 file1Modified > changes.patch

then you apply it using:

patch file2 changes.patch

David Marciel
  • 865
  • 1
  • 12
  • 29
  • The patch won't apply (at all, or not cleanly) if the context around it changes as it does in your example. – Mat Feb 24 '20 at 19:37