【问题标题】:how to take diff of two versions of perforce depot file如何获取两个版本的 perforce depot 文件的差异
【发布时间】:2016-07-28 04:32:04
【问题描述】:

如何检查文件内容是否与服务器 perforce JAVA API 中的修订版本相同。在将任何文件更新到 perforce depot 之前,我想检查本地文件和 depot 文件的内容是否有任何差异。如果没有区别,则忽略提交该文件。

【问题讨论】:

标签: java compare perforce


【解决方案1】:

我认为你想要 getDiffFiles() 方法:

https://www.perforce.com/perforce/r15.1/manuals/p4java-javadoc/com/perforce/p4java/impl/mapbased/client/Client.html#getDiffFiles

或者,对于您正在做的特定事情(不提交未更改的文件),只需使用“leaveUnchanged”提交选项,而不是自己做同样的工作。

【讨论】:

  • 我喜欢这个。我会尝试设置此选项“leaveUnchanged”
  • 请注意,您可以为每个提交命令单独设置SubmitOptions,或者您也可以为您的整个工作区设置SubmitOptions,然后每个提交都以这种方式运行。
【解决方案2】:

是的,做起来很简单。只需生成原始文件的 MD5 哈希,然后在再次更新之前生成新文件的 MD5 哈希。

现在比较两个文件的哈希值。如果两者相同,则两个文件的内容相同,否则它们不同,您可以更新。

这是一个可以轻松生成和检查 MD5 的实用程序,

public class MD5Utils {
    private static final String TAG = "MD5";

    public static boolean checkMD5(String md5, File updateFile) {
        if (TextUtils.isEmpty(md5) || updateFile == null) {
            Log.e(TAG, "MD5 string empty or updateFile null");
            return false;
        }

        String calculatedDigest = calculateMD5(updateFile);
        if (calculatedDigest == null) {
            Log.e(TAG, "calculatedDigest null");
            return false;
        }

        Log.v(TAG, "Calculated digest: " + calculatedDigest);
        Log.v(TAG, "Provided digest: " + md5);

        return calculatedDigest.equalsIgnoreCase(md5);
    }

    public static String calculateMD5(File updateFile) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "Exception while getting digest", e);
            return null;
        }

        InputStream is;
        try {
            is = new FileInputStream(updateFile);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Exception while getting FileInputStream", e);
            return null;
        }

        byte[] buffer = new byte[8192];
        int read;
        try {
            while ((read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            // Fill to 32 chars
            output = String.format("%32s", output).replace(' ', '0');
            return output;
        } catch (IOException e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                Log.e(TAG, "Exception on closing MD5 input stream", e);
            }
        }
    }
}

【讨论】:

  • 感谢您的回复。但是我们不能使用 Perforce JAVA API 做同样的事情吗??
  • 恐怕你不能。为此,您必须使用标准的 JAVA api。
  • 感谢您的及时回复
  • 你当然可以让 Perforce 服务器告诉你它记录了哪些文件修订版本的 MD5 哈希值。例如运行服务器的fstat 命令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-14
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 2023-03-13
相关资源
最近更新 更多