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?

Comments:

Hi Sandip, see this issue for a netbeans module implementation for cange CVSROOT for a project: http://www.netbeans.org/issues/show_bug.cgi?id=65366 This was implemented by Michael Nascimento (http://weblogs.java.net/blog/mister__m/), but I can't find this on any UC. If you can/need a nbm, let me know, that I send to you a.s.a.p. []'s

Posted by Michel Graciano on January 12, 2007 at 07:41 AM PST #

Thanks Michel.

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 07:55 AM PST #

BTW, if you need the module, get http://www.myjavaserver.com/~hmichel/netbeans/modules/net-java-dev-cvsrootselector.nbm. []'s

Posted by Michel Graciano on January 14, 2007 at 08:14 AM PST #

Hi Michel, I have already converted my program into a module. The module is sensitive to currently selected nodes directory. I will publish the module someday.

Thanks, Sandip

Posted by Sandip on January 14, 2007 at 03:03 PM PST #

I tried this in Ruby, it required only 16 lines of hopefully-not-to-obfuscated code, as compared to 83 for your Java solution: ruby version

Posted by Gregory Murphy on April 26, 2007 at 09:54 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

sandipchitale

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today