I need to refactor a file by replacing a message codes, with updated values. My original file is present in Ubuntu server, which I can mount and access as Windows files. I clone it through git in Ubuntu Server, then moved the file to Windows and in Windows, with help of small Java program refactor the value and write it. Then open the file in windows and the file to copy paste the file in Ubuntu server (since copy replace, or move replace of the file show git diff as all contents get changed).
The following is the Java code I have used to do the refactoring.
ruleInBR = new BufferedReader(new FileReader(ruleIn));
ruleOutBW = new BufferedWriter(new FileWriter(ruleOut));
csvOutBW = new BufferedWriter(new FileWriter(csvOut));
String readRule = "";
int lineNo = 1;
while((readRule = ruleInBR.readLine()) != null)
{
if(details.get(lineNo) != null)
{
AlterValuePair<String> avPair = details.get(lineNo);
String renamedRule = readRule.replace(avPair.getOldValue(),avPair.getNewValue());
String trimRenamedRule = renamedRule.replace("\r","");
csvOutBW.write(lineNo + ", " + avPair.getOldValue() + ", " + avPair.getNewValue() +"\n");
ruleOutBW.write(trimRenamedRule + "\n");
count++;
}
else {
String trimReadRule = readRule.replace("\r","");
ruleOutBW.write(trimReadRule +"\n");
}
lineNo++;
}
But in the GitDiff I come across the issues of presence of git diff for ‘^M’ or ‘\r’ which I actually not did and from my knowledge I know it is because I have opened and worked with some editors which leave those line ending. Since the file refactor will cause issues while compiling in Ubuntu due to the unexpected character. I have followed the following approaches which I learned previously and found in Stack Overflow.
I have adapted following choices in vim
- set ff=unix /set fileformat=unix
- set ff=dos /set fileformat=dos
- %s/\r\n/\n/ or %s/\r// or %s/\r//g
- dos2unix fileName
- perl -pi -e 's/\r//' or perl -pi -e 's/\r\n/\n/'
But all this cases it altered the whole file as new file and in git diff it shows all are new changes and old ones which I haven't changed are altered. Are there any ways to solve this problem?
I have go through following questions from Stack Overflow:
- gVim showing carriage return (^M) even when file mode is explicitly DOS
- Convert DOS line endings to Linux line endings in vim
- ^M at the end of every line in vim
- Remove a line in text file with java.BufferedReader
- https://its.ucsc.edu/unix-timeshare/tutorials/clean-ctrl-m.html
- https://www.garron.me/en/bits/get-rid-m-characters-vim.html
But none of them helped me in a positive way.
UPDATE
Finally after following an instruction from another Stack Overflow question which talking about omitting in the commit level, which will skip adding whitespaces and that solved my issues to a bit, but that also has some flaws because it has some parts of same files which are not committed (actually belongs the ones omitted in whitespace).
I don’t know actually how to handle this as I have to make changes to several branches and all this may or may not have to go through this problem. Are there any simple way rather than doing at git commit level. Where I have to ignore white space and commit and stash the uncommitted changes every time I am committing like this.
Btw this that Stack Overflow link : Add only non-whitespace changes