0

I'm trying to write a simple application in java that will clone automatically a whole trunk from TFS to Git repo using git-tfs. To get data from user i'm using some jtextfields. In one of there jtextfields user must write the trunk name. Everything is working but, if trunk name contains spaces the whole git-tfs process doesn't start and git tfs logs say to respect synopsys.

To run git-tfs the synopsys is:

$ git-tfs.exe --username <username> --password <password> <server-url> <trunk-name> <working-folder-path>

es.:

$ git-tfs.exe --username=myusernamename --password=mypassword http://127.0.0.1:8080/DefaultCollection $/TrunkName C:\workingFolder

Here my code:

commands = new ArrayList<String>();
commands.add("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
commands.add("/c");
commands.add("git-tfs.exe");
commands.add("--username=" + usernameTextField.getText());
commands.add("--password=" + passwordTextField.getText());
commands.add(serverUrlTextField.getText());
commands.add(trunkTextField.getText());
commands.add(workingFolder.getText());
ProcessBuilder pb = new ProcessBuilder(this.commands);
Process process = pb.start();

Surfing on the net i've found a solution for git-tfs and it's to write the trunk name in quotas like $/"trunk name/some/path". Running git-tfs with quotas from powershell everything works fine but writing the same in jtextfield application can't run saying again about synopsis.

To test the process builder i tried to modify the ArrayList with commands by adding manually the modified string so it appears:

...
this.commands.add("$/\"trunk name\"");
...

not even like this works so i tried:

...
this.commands.add("$/\\\"trunk name\\\"");
..

and it works.

After this i tried to write the same on jtextfield but it can't work. How could i fix the problem and parse text from jTextField and let the processBuilder work?

Thank you!

Davide
  • 35
  • 5
  • try single quotes around 'trunc name' instead. – Serge Apr 20 '20 at 01:45
  • thank you, i forgot to write about this other test but i've done it with the same result – Davide Apr 20 '20 at 08:45
  • I would have used double quoted instead like `commands.add("\"" + trunkTextField.getText() + "\"");` – Philippe Apr 20 '20 at 09:14
  • you can also try to escape the space with no quotes `trunc\\ name`. The problem is not in java but in the underlying shell which the processBuilder runs. It strips quotes before it runs the git command. So, you might need to add more levels of quotes. for example `"\\\"'the name'\\\""`. you need to play with this. Which OS do you run on? – Serge Apr 20 '20 at 10:40
  • Ty @Serge i'm using windows 10 – Davide Apr 20 '20 at 11:41
  • Ty @Philippe but like that git-tfs can't accept the query. Project name must be like $/"my project name" and not "$/my project name" – Davide Apr 20 '20 at 11:54

1 Answers1

0

i solved the issue by changing from ProcessBuilder to Runtime so now my code is:

commands = new ArrayList<String>();
commands.add("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe");
commands.add("/c");
commands.add("git-tfs.exe");
commands.add("--username=" + usernameTextField.getText());
commands.add("--password=" + passwordTextField.getText());
commands.add(serverUrlTextField.getText());
commands.add(trunkTextField.getText());
commands.add(workingFolder.getText());
Runtime runtime = Runtime.getRuntime();
String[] commandsStringArray = commands.toArray(new String[0]);
Process process = runtime.exec(commandsStringArray);

and checking the projectName as follow:

    private String fixProjectName(String projectName) {
    if (projectName.contains(" ")) {
        String correct = "";
        correct = projectName.replace("$/", "");
        correct = "$/\\\"" + correct + "\\\" ";
        this.projectName = correct;
        return correct;
    }
    return projectName;
}

Btw i'd like same to how to let the ProcessBuilder work.

Each better idea is welcome.

Thank you.

Davide
  • 35
  • 5