package com.codicesoftware.plugins.hudson.commands;

import com.codicesoftware.plugins.hudson.model.ChangeSetID;
import com.codicesoftware.plugins.hudson.util.MaskedArgumentListBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetWorkspaceStatusCommand implements ParseableCommand<List<ChangeSetID>>, Command {

    private static final Pattern statusRegex = Pattern.compile("^cs:(\\d+)@rep:(.+)@repserver:(.+):(\\d+)$");

    private final String workspacePath;

    public GetWorkspaceStatusCommand(String workspacePath) {
        this.workspacePath = workspacePath;
    }

    public MaskedArgumentListBuilder getArguments() {
        MaskedArgumentListBuilder arguments = new MaskedArgumentListBuilder();

        arguments.add("status");
        arguments.add("--cset");
        arguments.add(workspacePath);

        return arguments;
    }

    public List<ChangeSetID> parse(Reader r) throws IOException, ParseException {
        List<ChangeSetID> list = new ArrayList<>();
        BufferedReader reader = new BufferedReader(r);
        String line = reader.readLine();
        while (line != null) {
            Matcher matcher = statusRegex.matcher(line);
            if (matcher.find()) {
                ChangeSetID cs = new ChangeSetID(Integer.parseInt(matcher.group(1)), matcher.group(2),
                        matcher.group(3), Integer.parseInt(matcher.group(4)));
                list.add(cs);
            }
            line = reader.readLine();
        }

        return list;
    }
}

@@ -266,41 +266,8 @@
 er;%0A
-import java.text.ParseException;%0A
 impo
@@ -574,23 +574,16 @@
 ver:(.+)
-:(%5C%5Cd+)
 $%22);%0A%0A  
@@ -1072,24 +1072,8 @@
 tion
-, ParseException
  %7B%0A 
@@ -1178,16 +1178,17 @@
 der(r);%0A
+%0A
         
@@ -1308,16 +1308,55 @@
 (line);%0A
+            line = reader.readLine();%0A%0A
         
@@ -1363,16 +1363,17 @@
     if (
+!
 matcher.
@@ -1402,178 +1402,114 @@
     
-ChangeSetID cs = new ChangeSetID(Integer.parseInt(matcher.group(1)), matcher.group(2),%0A                        matcher.group(3), Integer.parseInt(matcher.group(4)));%0A    
+list.add(new ChangeSetID(-1, %22unknown_repo%22, %22unknown_server%22));%0A                continue;%0A            %7D%0A%0A
     
@@ -1529,12 +1529,24 @@
 add(
-cs);
+new ChangeSetID(
 %0A   
@@ -1551,21 +1551,16 @@
         
-   %7D%0A
         
@@ -1566,32 +1566,79 @@
     
- line = reader.readLine(
+Integer.parseInt(matcher.group(1)), matcher.group(2), matcher.group(3))
 );%0A 

Update the GetWorkspaceStatusCommand parser