TomSelleck
TomSelleck

Reputation: 6968

Getting a Date and description associated with a file revision - Java API Perforce

I am trying to write a method which takes a file path and a revision number as its arguments and returns the date the revision is associated with. The code I have works (although pretty slowely) however, when I put a revision number greater than 51 in, the output gets messed up.. Here is the API.

Input

String [] filePaths= {"//file/x/y/strings/somefile.csv"};
p4Client.getDateAssociatedWithFileRevision(filePaths, 52);

Output - This should just be one line...

Rev number: 2 :: Revision Date: Wed Aug 24 23:48:42 BST 2005

Rev number: 52 :: Revision Date: Wed Aug 24 23:52:53 BST 2005

Rev number: 51 :: Revision Date: Sat Aug 20 02:01:59 BST 2005

getDateAssociatedWithFileRevision

public Date getDateAssociatedWithFileRevision(String [] filePath, int revisionNumber) {

        List<IFileSpec> fileList = null;
        Map<IFileSpec,List<IFileRevisionData>> fileRevisionData = null;
        String currentFile = null;
        Date revisionDate = null;

        try 
        {
            String file = filePath[0] + "#" + revisionNumber;
            currentFile = file;
            fileList = getIFileSpecList(file);  //Get list of file(s) in path

            for(IFileSpec fileSpec: fileList)
            {
                if(file.toString() == null)
                {
                    System.out.println("\"" + currentFile +"\"" + " does not exist...");
                    break;
                }
                fileRevisionData = fileSpec.getRevisionHistory(0, true,false,true,false);
                int i = 0;
                for(List<IFileRevisionData> revisionData : fileRevisionData.values()) {
                    revisionDate = revisionData.get(0).getDate();
                    int revision = revisionData.get(0).getRevision();
                    System.out.println("Rev number: " +revision +" :: " + "Revision Date: " + revisionDate);
                    System.out.println(i);
                    i++;
                }

            }
        }
        catch(Exception e){e.printStackTrace();}
        return revisionDate;  
     }

GetIFileSpecList

public List<IFileSpec> getIFileSpecList(String file) {
        List<IFileSpec> fileList = null;
        try {
            fileList = iServer.getDepotFiles(
                        FileSpecBuilder.makeFileSpecList(new String[] {file}), false);  //Get list of file(s) in path
        }
        catch(Exception e){e.printStackTrace();}
        return fileList;
     }

Edit

Just figured out that the output is getting messed up after an integration, just need to find a way to handle them now..

Upvotes: 2

Views: 654

Answers (2)

Jeff A. Bowles
Jeff A. Bowles

Reputation: 21

Too much work. Take this command-line idea ("p4 files will get you that info, parsed") and make Perforce do the joins of the data for you. Then Java-ize it.

% p4 -Ztag files //guest/jeff_bowles/scripts/0228devbranch.html
... depotFile //guest/jeff_bowles/scripts/0228devbranch.html
... rev 2
... change 4421
... action edit
... type ktext
... time 1093044566

% p4 -Ztag files //guest/jeff_bowles/scripts/0228devbranch.html#1
... depotFile //guest/jeff_bowles/scripts/0228devbranch.html
... rev 1
... change 4420
... action add
... type ktext
... time 1093042787

Upvotes: 2

TomSelleck
TomSelleck

Reputation: 6968

I managed to return only the date I want by adding an if statement (marked below). I don't know how elegant this solution is... any comments are welcome.

getDateAssociatedWithFileRevision

public Date getDateAssociatedWithFileRevision(String [] filePath, int revisionNumber) {

        List<IFileSpec> fileList = null;
        Map<IFileSpec,List<IFileRevisionData>> fileRevisionData = null;
        String currentFile = null;
        Date revisionDate = null;

        try 
        {
            String file = filePath[0] + "#" + revisionNumber;
            currentFile = file;
            fileList = getIFileSpecList(file);  //Get list of file(s) in path

            for(IFileSpec fileSpec: fileList)
            {
                if(file.toString() == null)
                {
                    System.out.println("\"" + currentFile +"\"" + " does not exist...");
                    break;
                }
                fileRevisionData = fileSpec.getRevisionHistory(0, true,false,true,false);
                for(List<IFileRevisionData> revisionData : fileRevisionData.values()) {
                    int revision = revisionData.get(0).getRevision();
     -------------> if(revision.equals(revisionNumber))
                    {
                        revisionDate = revisionData.get(0).getDate();

                        System.out.println("Rev number: " +revision +" :: " + "Revision Date: " + revisionDate);
                        break;

                    }
                }

            }
        }
        catch(Exception e){e.printStackTrace();}
        return revisionDate;  
     }

Upvotes: 1

Related Questions