3

I want to update comments of checkedin files in Clearcase with Perl script.
This script works fine if the comment I'm passing from command line doesn't contain spaces.
But when I use comments with spaces, I run into error -

Error: Pathname not found: "C:/Views/view1/some_dir/test.c@@/main/dbg_test/1

Following is command line input to script:

>>./appendcomments.pl dbg_test "\"scr1234, Test Scr\""  

Here is my code.

if ($#ARGV != 1 )
{
    print "usage: addcomments.pl <base branch> <comment>\n";
    print "     For example addcomments.pl rel5.0 \"This is an example\"\n";
    exit;
}

my $base_branch =   $ARGV[0];
my $comment   =     $ARGV[1];

my ($output, @FILE_LIST, $file, $desr);

@FILE_LIST = `cleartool find -avobs -version "version(.../$base_branch/LATEST)" -print`;

FILE: foreach $file (@FILE_LIST) 
{
    $file =~ s/\\/\//g;
    $desr =`cleartool describe -fmt %Nc $file`;

    if ($desr !~ /scr\s*\#*\s*(\d+)/img)
    {
        chomp($file);
        $output = `cleartool chevent -c $comment -replace $file`; 
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325

2 Answers2

2

Use double quotes around the comments (assuming there are no double quotes in the comments):

$output = `cleartool chevent -c "$comment" -replace $file`; 

If you've got to worry about double quote (or single quotes, or both) appearing in the comment text, then you need to do some work on the variable comment. So for a single quote comment you should consider:

$comment =~ s/'/'\\''/g;  # Once, outside the loop

$output = `cleartool chevent -c '$comment' -replace $file`; 

In a shell script, the single quotes around $comment would prevent the shell expanding the variable, but this is Perl doing the expanding. The first substitution replaces every single quote in the comment string with the sequence '\''. The substitution in the command wraps single quotes around the whole. This means that there's a single-quoted string, and each '\'' sequence stops the current single quoted string, outputs an escaped single quote, and then starts a new single-quoted string, repeating as necessary up to the single quote at the end of the comment.

You said the script is 'appendcomments.pl', but you're using -replace instead of -append in the command. Your decision, but the name doesn't match the action.

nowox
  • 25,978
  • 39
  • 143
  • 293
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • I tried that didn't work. You're right about name of script. I intend to change it ones the script starts working. – user1792727 Nov 01 '12 at 22:51
  • OK...love the term "didn't work". What do you mean by "didn't work". What did happen? What was the error message? Since you're on Cygwin, there could be a problem I'm not expecting. Which of the two versions (simple or complex) did you try? What was your command line? – Jonathan Leffler Nov 01 '12 at 22:52
  • Same error as before Error: Pathname not found: "C:/Views/view1/some_dir/test.c@@/main/dbg_test/1 – user1792727 Nov 01 '12 at 22:57
  • Is the trouble that you mapped the backslashes in the file name to slashes? It appears to be wittering about the file name, not the comments. – Jonathan Leffler Nov 01 '12 at 23:12
  • Jon, that's correct. But perl complains about the file name only when the comment passed in as argument contains white spaces. I tried to pass on quotes by escaping them like this: $output = `cleartool chevent -c \"$comment"\ -replace \"$file\"`; I run into foll. error: ".eartool: Error: Pathname not found: "C:/Views/view1/some_dir/test.c@@/main/dbg_test/1 – user1792727 Nov 02 '12 at 14:35
  • Is there any danger that a Windows `cmd.exe` is involved. You've tagged it Cygwin, so I assumed not, but Windows has its own rather odd quoting conventions. (I work with ClearCase on Unix, so my experience there is only indirectly a help.) Do you need to capture the output from the `chevent` command? If not, you could use Perl's `system` with the arguments in a list (rather than as a single string), which bypasses the shell and gives the arguments directly to the program without issues with shell splitting them up again. – Jonathan Leffler Nov 02 '12 at 14:43
  • I don't need to capture the output. So I tried sending out as system (@cmd = `cleartool chevent -c $comment -replace $file`). But same error ".eartool: Error: Pathname not found: "C:Views/view1/some_dir/test.c@@/main/dbg_test/1. I'm wondering if file path is overwriting some buffer. Did you notice that the error clips 'Cl' from Cleartool? Maybe windows is doing something to the cmd I'm trying to send out. – user1792727 Nov 02 '12 at 15:36
1

As in this thread or this thread, try to escape those quotes:

$output = `cleartool chevent -c \"$comment\" -replace \"$file\"`; 

The being said, the actual issue is that "C:/Views/view1/some_dir/test.c@@/main/dbg_test/1" will never exist (cygwin or not): ClearCase would access an extended path only in a dynamic view (M:\...), not a snapshot one (C:\...), as in this example, which uses a dynamic view.

More precisely, from "pathnames_ccase":

From a dynamic view, you can use the path name forms described here as arguments to any cleartool command that takes a path name.
From a snapshot view, you can use the VOB-extended path name forms as arguments to those cleartool commands that return information about elements and versions (for example, describe, ls, lshistory, and diff). Such operations do not require the MVFS.
However, you cannot use VOB-extended path names forms to check out an element version that is not loaded into your view.

Note for Windows users: cleartool is case-sensitive. In cleartool subcommands, path names to MVFS objects, including view-private files in the MVFS namespace, must be case-correct.

So before fighting any longer with quotes:

  • Use a dynamic view
  • Make sure the case of the path bis correct (with a simple cleartool descr M:/path/to/file@/main/aVersion in a cygwin session)
  • Then try your ccperl script.
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250