View Javadoc

1   /*
2    *     $File:  $
3    *   $Change:  $
4    * $Revision:  $
5    * $DateTime:  $
6    *   $Author:  $
7    *
8    * Licensed Materials - Exclusive Property of TUI InfoTec GmbH, Hannover
9    * Copyright TUI InfoTec GmbH 2005 All Rights Reserved
10   */
11  package de.matthias_burbach.deputy.core.util;
12  
13  import java.util.regex.Matcher;
14  import java.util.regex.Pattern;
15  
16  /***
17   * Is a simple interface to the SCM tool Perforce (short: P4).
18   *
19   * @author Matthias Burbach
20   */
21  public class P4 {
22      /***
23       * @return <code>true</code> if the Perforce command line can be accessed
24       *         by this class.
25       */
26      public boolean isP4Installed() {
27          CommandLine cl = new CommandLine();
28          int exitCode = cl.execute("p4");
29          boolean result = (exitCode == 0) && cl.getStdErr().length() == 0;
30          return result;
31      }
32  
33      /***
34       * @param file The file to check.
35       * @return <code>true</code> if the file is under Perforce control
36       *         (also if only opened for add yet).
37       */
38      public boolean isP4Controlled(final String file) {
39          CommandLine cl = new CommandLine();
40          int exitCode = cl.execute("p4 fstat " + file);
41          boolean result = (exitCode == 0) && cl.getStdErr().length() == 0;
42          return result;
43      }
44  
45      /***
46       * @param file The file to get the synched revision for.
47       * @return The revision of the file that is currently synched on the client.
48       *         Returns -1 if no revision is synched or if Perforce access
49       *         failed.
50       */
51      public int getSynchedRevision(final String file) {
52          CommandLine cl = new CommandLine();
53          int exitCode = cl.execute("p4 have " + file);
54          int result = -1;
55          if (exitCode == 0 && cl.getStdErr().length() == 0) {
56              /*
57               * Try to parse the revision number from the stdout output.
58               */
59              String output = cl.getStdoutWithoutNewLines();
60              Matcher matcher =
61                  Pattern.compile(".*#([0-9]+) - .*").matcher(output);
62              if (matcher.matches()) {
63                  String revision = matcher.group(1);
64                  try {
65                      result = Integer.parseInt(revision);
66                  } catch (Exception e) {
67                      e.printStackTrace();
68                  }
69              }
70          }
71          return result;
72      }
73  
74      /***
75       * @param file The file to check.
76       * @return <code>true</code> if the file is now opened for edit.
77       */
78      public boolean openForEdit(final String file) {
79          CommandLine cl = new CommandLine();
80          int exitCode = cl.execute("p4 edit " + file);
81          boolean result = (exitCode == 0) && cl.getStdErr().length() == 0;
82          return result;
83      }
84  }