Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-7618] potential fix for workspace path problem.
Using p4 -G where <depotFilePath> we can determine what the workspace path should be.
  • Loading branch information
Rob Petti committed Jun 22, 2011
1 parent 2d3fb9b commit 4f82c3a
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/main/java/com/tek42/perforce/model/Changelist.java
Expand Up @@ -104,6 +104,7 @@ public static enum Action {
Action action;
String filename;
String revision;
String workspacePath;
Changelist changelist;

private static Map<Action, EditType> registry = new HashMap<Action, EditType>();
Expand Down Expand Up @@ -171,6 +172,14 @@ public void setRevision(String revision) {
this.revision = revision;
}

public String getWorkspacePath() {
return workspacePath;
}

public void setWorkspacePath(String workspacePath) {
this.workspacePath = workspacePath;
}

public void setChangelist(Changelist changelist) {
this.changelist = changelist;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/tek42/perforce/parse/Changes.java
Expand Up @@ -38,6 +38,7 @@
import com.tek42.perforce.PerforceException;
import com.tek42.perforce.model.Changelist;
import com.tek42.perforce.model.Workspace;
import hudson.plugins.perforce.PerforceSCMHelper;

/**
* Base API object for interacting with changelists.
Expand All @@ -63,9 +64,22 @@ public Changelist getChangelist(int number) throws PerforceException {
Changelist change = builder.build(getPerforceResponse(builder.getBuildCmd(getP4Exe(), Integer.toString(number))));
if(change == null)
throw new PerforceException("Failed to retrieve changelist " + number);
calculateWorkspacePaths(change);
return change;
}

/**
* Calculates the workspace paths for every file in the changelist.
* @param change
*/
private void calculateWorkspacePaths(Changelist change) throws PerforceException{
for(Changelist.FileEntry file :change.getFiles()){
StringBuilder response = getPerforceResponse(new String[]{getP4Exe(),"-G","where",file.getFilename()});
PerforceSCMHelper.WhereMapping map = PerforceSCMHelper.parseWhereMapping(response.toString().getBytes());
file.setWorkspacePath(map.getWorkspacePath().replaceAll("^//\\S+?/",""));
}
}

/**
* Returns a list of changelists that match the parameters
*
Expand Down
Expand Up @@ -61,7 +61,7 @@ public Collection<Changelist.FileEntry> getAffectedFiles() {
public Collection<String> getAffectedPaths() {
List<String> paths = new ArrayList<String>(change.getFiles().size());
for (Changelist.FileEntry entry : change.getFiles()) {
paths.add(entry.getFilename());
paths.add(entry.getWorkspacePath());
}
return paths;
}
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/hudson/plugins/perforce/PerforceSCMHelper.java
@@ -1,7 +1,9 @@
package hudson.plugins.perforce;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

/**
Expand Down Expand Up @@ -51,4 +53,87 @@ static boolean projectPathIsValidForMultiviews(String projectPath) {
|| projectPath.indexOf('@') > -1; // labels ok {
}

static public class WhereMapping {
private String depot;
private String workspace;
private String filesystem;

public WhereMapping(String depot,String workspace,String filesystem){
this.depot = depot;
this.workspace = workspace;
this.filesystem = filesystem;
}

public String getDepotPath() {
return depot;
}

public String getFilesystemPath() {
return filesystem;
}

public String getWorkspacePath() {
return workspace;
}

}

static private int readInt(byte[] bytes, int offset){
int result=0;
for (int i=offset; i<offset+4; i++) {
result += (int) bytes[i] << (8*(i-offset));
}
return result;
}

static private Map<String,String> readPythonDictionary(byte[] dict){
int counter = 0;
Map<String,String> map = new HashMap<String,String>();
if(dict[0] == '{' && dict[1] == 's'){
//good to go
counter = 1;
while(counter < dict.length && dict[counter] != 0x30){
//read in pairs
String key,value;
if(dict[counter] == 's'){
counter++;
key = readPythonString(dict,counter);
counter += key.length() + 4;
} else {
//keys 'should' always be strings
return null;
}
if(dict[counter] == 's'){
counter++;
value = readPythonString(dict,counter);
counter += value.length() + 4;
} else {
//don't know how to handle non-string objects yet
return null;
}
map.put(key, value);
}
} else {
return null;
}
return map;
}

static private String readPythonString(byte[] bytes, int offset){
int length = readInt(bytes, offset);
String result = new String(bytes, offset+4, length);
return result;
}

static public WhereMapping parseWhereMapping(byte[] whereOutput){
String depot;
String workspace;
String filesystem;
Map<String,String> map = readPythonDictionary(whereOutput);
depot = map.get("depotFile");
workspace = map.get("clientFile");
filesystem = map.get("path");
return new WhereMapping(depot,workspace,filesystem);
}

}
39 changes: 39 additions & 0 deletions src/test/java/hudson/plugins/perforce/PerforceSCMHelperTest.java
@@ -1,5 +1,6 @@
package hudson.plugins.perforce;

import hudson.plugins.perforce.PerforceSCMHelper.WhereMapping;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -39,4 +40,42 @@ public void testProjectPathIsValid() {
.projectPathIsValidForMultiviews("//...@mylabel"));
}

public void testWhereParser() {
byte testOutput[] = {
(byte)0x7b, (byte)0x73, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x63, (byte)0x6f, (byte)0x64, (byte)0x65, (byte)0x73, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x73,
(byte)0x74, (byte)0x61, (byte)0x74, (byte)0x73, (byte)0x09, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x64, (byte)0x65, (byte)0x70, (byte)0x6f, (byte)0x74, (byte)0x46, (byte)0x69, (byte)0x6c,
(byte)0x65, (byte)0x73, (byte)0x2b, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f,
(byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e, (byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73,
(byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69, (byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x70, (byte)0x72, (byte)0x6f, (byte)0x70, (byte)0x65, (byte)0x72, (byte)0x74, (byte)0x69, (byte)0x65,
(byte)0x73, (byte)0x73, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x63, (byte)0x6c, (byte)0x69, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x46, (byte)0x69, (byte)0x6c, (byte)0x65,
(byte)0x73, (byte)0x32, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x2f, (byte)0x72, (byte)0x70, (byte)0x65, (byte)0x74, (byte)0x74, (byte)0x69, (byte)0x2f, (byte)0x49, (byte)0x6e,
(byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f, (byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e, (byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74,
(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73, (byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69, (byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x70, (byte)0x72, (byte)0x6f,
(byte)0x70, (byte)0x65, (byte)0x72, (byte)0x74, (byte)0x69, (byte)0x65, (byte)0x73, (byte)0x73, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x70, (byte)0x61, (byte)0x74, (byte)0x68,
(byte)0x73, (byte)0x40, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x68, (byte)0x6f, (byte)0x6d, (byte)0x65, (byte)0x2f, (byte)0x72, (byte)0x70, (byte)0x65, (byte)0x74, (byte)0x74,
(byte)0x69, (byte)0x2f, (byte)0x77, (byte)0x6f, (byte)0x72, (byte)0x6b, (byte)0x73, (byte)0x70, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74,
(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f, (byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e, (byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c,
(byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73, (byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69, (byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x70, (byte)0x72, (byte)0x6f, (byte)0x70, (byte)0x65,
(byte)0x72, (byte)0x74, (byte)0x69, (byte)0x65, (byte)0x73, (byte)0x30, (byte)0x7b, (byte)0x73, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x63, (byte)0x6f, (byte)0x64, (byte)0x65,
(byte)0x73, (byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x74, (byte)0x73, (byte)0x09, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x64, (byte)0x65,
(byte)0x70, (byte)0x6f, (byte)0x74, (byte)0x46, (byte)0x69, (byte)0x6c, (byte)0x65, (byte)0x73, (byte)0x24, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x2f, (byte)0x49, (byte)0x6e,
(byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f, (byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e, (byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74,
(byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73, (byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69, (byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x78, (byte)0x6d, (byte)0x6c,
(byte)0x73, (byte)0x0a, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x63, (byte)0x6c, (byte)0x69, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x46, (byte)0x69, (byte)0x6c, (byte)0x65, (byte)0x73,
(byte)0x2b, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x2f, (byte)0x72, (byte)0x70, (byte)0x65, (byte)0x74, (byte)0x74, (byte)0x69, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73,
(byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f, (byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e, (byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61,
(byte)0x6c, (byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73, (byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69, (byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x78, (byte)0x6d, (byte)0x6c, (byte)0x73,
(byte)0x04, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x70, (byte)0x61, (byte)0x74, (byte)0x68, (byte)0x73, (byte)0x39, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x2f, (byte)0x68, (byte)0x6f,
(byte)0x6d, (byte)0x65, (byte)0x2f, (byte)0x72, (byte)0x70, (byte)0x65, (byte)0x74, (byte)0x74, (byte)0x69, (byte)0x2f, (byte)0x77, (byte)0x6f, (byte)0x72, (byte)0x6b, (byte)0x73, (byte)0x70,
(byte)0x61, (byte)0x63, (byte)0x65, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x2f, (byte)0x74, (byte)0x72, (byte)0x75, (byte)0x6e,
(byte)0x6b, (byte)0x2f, (byte)0x49, (byte)0x6e, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x6c, (byte)0x6c, (byte)0x65, (byte)0x72, (byte)0x73, (byte)0x2f, (byte)0x62, (byte)0x75, (byte)0x69,
(byte)0x6c, (byte)0x64, (byte)0x2e, (byte)0x78, (byte)0x6d, (byte)0x6c, (byte)0x30
};
PerforceSCMHelper.WhereMapping map;
map = PerforceSCMHelper.parseWhereMapping(testOutput);
assertEquals("//Install/trunk/Installers/build.properties", map.getDepotPath());
assertEquals("//rpetti/Install/trunk/Installers/build.properties", map.getWorkspacePath());
assertEquals("/home/rpetti/workspace/Install/trunk/Installers/build.properties", map.getFilesystemPath());
}

}

0 comments on commit 4f82c3a

Please sign in to comment.