How can you execute a command outside of your current working directory in WinCvs?

1.2k views Asked by At

I'm working on a Perl wrapper to execute commands within WinCvs. I have been unable to find a way to execute Cvs commands without doing a chdir to the directory that you wish to execute the command on.

This is very annoying because it means that every time I want to do anything with Cvs in the perl script I need to get the current working directory, change directories to the Cvs path, execute my command, and then change directories back to the original working directory.

Is there a way to pass the path to a Cvs command so you can issue a command on a directory that you are not currently in?

For example, if my current working directory in my Perl script is C:\test\ and I want to execute a Cvs update command on C:\some_other_directory how can I execute that command without doing a chdir to C:\some_other_directory first?

Example of how I would currently execute the command:

#!/usr/bin/perl
use strict;
use warnings;
use Cwd;

my $cwd = cwd();
chdir "C:\some_other_directory" or die $!;
system 'cvs update';
chdir $cwd or die $!;

What I would like is to find a way to be able to pass "C:\some_other_directory" to the Cvs command directly and get rid of all of this chdir nonsense...

2

There are 2 answers

0
tjwrona On BEST ANSWER

Flavio's answer works, but I also found this alternate solution. It lets you change directories to issue the command, but is less dangerous than using chdir and less likely to leave you confused about what directory your script may be in at any given time.

The module File::Chdir can solve the problem quite easily:

use File::Chdir;

# Set the current working directory
$CWD = '/foo/bar/baz';

# Locally scope this section
{
    local $CWD = '/some/other/directory';

    # Updates /some/other/directory
    system 'cvs update';
}

# current working directory is still /foo/bar/baz
0
Flavio On

Another way to do this is by calling multiple commands in a single system call:

system("cd C:\some_other_directory && cvs update");