
Thursday January 11, 2007
A simple program to toggle CVSROOT of existing checked out workspace
One may check out a cvs workspace using a CVSROOT e.g. :pserver:username@extranet.foo.com:/cvs. This value of CVSROOT is recorded in the CVS/Root file in the checkedout directories. At some later time, depending on the network connectivity, it may be faster to use an alternate (aliased) CVSROOT e.g. :pserver:username@intranet.foo.com:/cvs. The following simple program provides the functionality to fix the CVSROOT settings in the CVS/Root file. It skips the entries specified in the .cvsignore file.
CAUTION: Care must be taken to make sure that both cvs servers are really the same servers or mirros that are in sync.
Usage:
java fixcvsroot.Main NEWCVSROOT
DISCLAIMER: This is experimentalcode. So no guarantees. Use the code at your own risk.
package fixcvsroot;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
/**
* A simple program to fix the CVSROOT.
* <p>
* One may check out a cvs workspace using a CVSROOT e.g.
* <code>:pserver:username@extranet.foo.com:/cvs</code>. This value of CVSROOT
* is recorded in the <code>CVS/Root</code> file in the checkedout directories.
* At some later time, depending on the network connectivity, it may be faster
* to use an alternate (aliased) CVSROOT e.g.
* <code>:pserver:username@intranet.foo.com:/cvs</code>.
*
* This simple program provides the functionality to fix the CVSROOT settings
* in the <code>CVS/Root</code> file. It skips the entries specified in the
* <code>.cvsignore</code> file.
*
* <b>CAUTION:</b> Care must be taken to make sure that both cvs servers are
* really the same servers or mirros that are in sync.
*
* Usage:
* <code>java fixcvsroot.Main NEWCVSROOT</code>
*
* <b>DISCLAIMER:</b> This is an experimental program. Use at your own risk.
*
* @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
*/
public class Main {
private Main() {
}
public static void main(String[] args) {
if (args.length != 1) {
System.err.println("Usage: java fixcvsroot.Main NEWCVSROOT");
System.exit(1);
}
fixcvsroot(args[0]);
System.exit(0);
}
private static void fixcvsroot(String cvsroot) {
try {
fixcvsroot(new File(".").getAbsoluteFile(), cvsroot);
} catch (IOException ex) {
System.err.println(ex.getMessage());
return;
}
System.out.println();
System.out.println("Done.");
}
private static void fixcvsroot(File directory, String cvsroot)
throws IOException {
if (directory.isDirectory()) {
if ("CVS".equals(directory.getName()) &&
new File(directory, "Entries").exists() &&
new File(directory, "Repository").exists()) {
try {
if (Boolean.getBoolean("fixcvsroot.Main.log")) {
System.out.println("Writing '" + cvsroot + "' to "
+ directory.getAbsolutePath()
+ directory.separator + "Root");
} else {
System.out.print(".");
}
PrintWriter out = new PrintWriter(
new BufferedWriter(new FileWriter(
new File(directory, "Root"))));
out.println(cvsroot);
out.close();
} catch (IOException ex) {
throw new IOException("Error: " + ex.getMessage()
+ ": Writing '" + cvsroot
+ "' to " + directory.getAbsolutePath()
+ directory.separator + "Root");
}
} else {
// Load .cvsignore
Set<File> ignored = null;
File cvsIgnore = new File(directory, ".cvsignore");
if (cvsIgnore.exists() && cvsIgnore.isFile()) {
ignored = new HashSet<File>();
BufferedReader bufferedReader = new BufferedReader(
new FileReader(cvsIgnore));
String ignoredEntry = null;
while ((ignoredEntry = bufferedReader.readLine()) != null) {
ignored.add(new File(directory, ignoredEntry));
}
}
File[] files = directory.listFiles();
for (File subFile:files) {
if (subFile.isDirectory()) {
// skip ignored sub directories
if (ignored!= null &&
ignored.size() > 0 &&
ignored.contains(subFile)) {
continue;
}
fixcvsroot(subFile, cvsroot);
}
}
}
}
}
}
Does anyone know other better technics/scripts to solve this problem?
Posted by sandipchitale
( Jan 11 2007, 11:03:09 PM PST ) Permalink
Trackback URL: http://blogs.sun.com/scblog/entry/a_simple_program_to_toggle
Posted by Michel Graciano on January 12, 2007 at 03:41 PM PST #
I thought as much. It is precisely the use case 1 described in the bug I needed this for. In fact I am surprised one does not hear about this issue from more people. Thanks for the info.
Posted by Sandip on January 12, 2007 at 03:55 PM PST #
Posted by Michel Graciano on January 14, 2007 at 04:14 PM PST #
Thanks, Sandip
Posted by Sandip on January 14, 2007 at 11:03 PM PST #
Posted by Gregory Murphy on April 26, 2007 at 04:54 PM PDT #