2

I am writing a front-end for git fast-import. I have to use java (more specifically Java 7) as the system I am extracting from really only has a java api. I have everything working the way I think it should except when I pipe my stdout over to fast-import I am getting this error:

fatal: Branch name doesn't conform to GIT standards: refs/heads/master 

When I look at the line endings I see that there is a cr lf versus the lf that should be there. I tried using a StringBuilder to build an output string inserting \n:

StringBuilder sb = new Stringbuilder("commit ref/heads/master\n");

that didnt work....

So thinking it was something with StringBuilder I tried just writing to stdout using print() leaving the sepcified line endings as \n.

System.out.print("commit refs/heads/master\n");

That did not seem to do the trick....

as a last ditch effort I tried:

System.setProperty("line.seperator","\n");
System.out.println("commit refs/heads/master");

that also failed failed...

So is there a way to write a simple lf in place of the standard cr lf in in a windows console?

UPDATE:

System.out.print("some text\n") still yields a cr lf.

for more reference here is the code snippet:

if(!isdevPath)
    System.out.print("commit refs/heads/master\n");
else
    System.out.print("commit refs/heads/devpath/" + devPathName + "\n");        
System.out.print("mark " + rev.getRevision() + "\n");
System.out.print("committer " + rev.getAuthor() + " <> " + convertDate(rev.date) + "\n");
System.out.print(exportData(rev.getDescription()) + " \n" );

I ran this in powershell like this: Java -jar >> text.txt and I am still getting the same error when I pipe to git fast-import

and here is a snap shot from notepad++ where you can see line endings

enter image description here

Solution The answer to this is simply you can't do it in a windows based shell. If I run this through bash in a windows system it retains the line feeds from a print() statement. So the answer is use print() statements with a \n where you want your line feeds. Then run the jar and pipe to git fast-import from bash.

Cureck
  • 23
  • 4
  • 1
    'That did not seem to do the trick': it certainly should have. Try it again. – user207421 Jul 29 '20 at 01:41
  • 1
    You have typo in line.separator. You have seperator :) – gtiwari333 Jul 29 '20 at 01:48
  • @MarquisofLorne I have done some more testing and `print("\n")` and changing the `line.separator` property (thank you @gtiwari333) seemed to work for a `PrintWriter` but I am still getting the same results for the System.out `Stream` – Cureck Jul 29 '20 at 03:22
  • There is nothing in `PrintStream` that would do that, or in the `BufferedWriter` that it uses either. – user207421 Jul 29 '20 at 04:01
  • You may be able to use `java -Dline.separator=` to achieve what you need with System.out.println, but I cannot see how to provide escape code for `\n` when using -D parameter on Windows. My answer below works but uses PrintWriter. See also https://stackoverflow.com/questions/3708991/setting-java-vm-line-separator – DuncG Jul 29 '20 at 11:41

2 Answers2

1

Don't print lines:

System.out.print("some text\n"); // note print, not println
Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

PrintStream uses System.lineSeparator(). Any code you add which sets a new value for line.separator (note correct spelling) System.setProperty("line.separator","\n"); will not change System.lineSeparator().

You can make a new PrintStream but there are many places to override to make it work for all println() calls. Instead it is easier to switch your code to use PrintWriter which requires one override println method to ensure you always get right endings you wish:

static PrintWriter newPrintWriter(Writer out, String lineSep)
{
    return new PrintWriter(out) {
        public void println() {
            print(lineSep);
        }
    };
}

This code will write out correctly:

    PrintWriter out = newPrintWriter(new OutputStreamWriter(System.out), "\n");
    out.println("commit refs/heads/master");
    out.println("mark XYZ");
    out.println("committer ABC");
    out.flush();

You can test with this to show the line ending changes:

    try(PrintWriter out = newPrintWriter(new FileWriter("unix.txt"), "\n"))
    {
        out.println("commit refs/heads/master");
        out.println("mark XYZ");
        out.println("committer ABC");
    }
    try(PrintWriter out = newPrintWriter(new FileWriter("pc.txt"), "\r\n"))
    {
        out.println("commit refs/heads/master");
        out.println("mark XYZ");
        out.println("committer ABC");
    }
DuncG
  • 12,137
  • 2
  • 21
  • 33