Salman A. Kagzi
Salman A. Kagzi

Reputation: 4063

How to get a previous revision of a specific revision of a file in CVS

Is there any CVS command that can give me previous revision of a file, given the filename and its revision?

Upvotes: 0

Views: 231

Answers (2)

Salman A. Kagzi
Salman A. Kagzi

Reputation: 4063

There is no direct way to do this from CVS commands. But since I am using the output (of CVS log) IN java, following code snippet worked for me

if (currentRevision == null || currentRevision.trim().length() == 0) {
    return null;
}

String[] revComponents = currentRevision.trim().split("\\.");

if (revComponents == null || revComponents.length == 0) {
    log.warn("Failed to parse revision number: " + currentRevision
            + " for identification of previous revision");
    return null;
}

if (revComponents.length % 2 == 1) {
    log.warn("Odd number of components in revision: " + currentRevision);
    log.warn("Possibly a tag revision.");
    return null;
}

int lastComp;
try {
    lastComp = Integer.parseInt(revComponents[revComponents.length - 1]);
} catch (NumberFormatException nmfe) {
    log.warn("Failed to parse the last component of revision from " + currentRevision);
    log.warn("Identified presence of alphanumric values in the last component. Cannot predict previous revision for this");
    return null;
}

if (revComponents.length == 2 && lastComp == 1) {
    log.debug("Revision: " + currentRevision + " is the first revision for the current file. Cannot go further back");
    return null;
}

StringBuilder result = new StringBuilder();
if (lastComp == 1) {
    for (int i = 0; i < revComponents.length - 2; i++) {
        if (result.length() > 0)
            result.append('.');
        result.append(revComponents[i]);
    }
} else if (lastComp > 1) {
    for (int i = 0; i < revComponents.length - 1; i++) {
        result.append(revComponents[i]);
        result.append('.');
    }
    result.append((lastComp - 1));
} else {
    log.warn("Found invalid value for last revision number component in " + currentRevision);
        return null;
    }

    return result.toString();
}

Above code also handles branch revisions, for example, if the current revision (in picture) is the first one on branch that it will return the revision from which branch for that file was created.

Hope this helps.

Upvotes: 0

Arpit
Arpit

Reputation: 6260

The way CVS controls the version of a file is that the revision is increased only when there is a commit in that file. The changes in the tree or tagging etc will not affect the revision of a single file.

Although there is no definite way of getting the previous revision of a file, given the name and it's revision... But there is a hit and trial method around it. Blindly go to the previous revision. For eg, the prev revision of Rev 1.5 would be Rev 1.4

Another way to sort this out is, to write a shell script. Use cvs log to get the change log of a particular filename and grep the desired revision number.

Upvotes: 1

Related Questions