I have the following script for adding files to git. I am running it on git bash using tclsh
proc git_add {file_ext file_type folder} {
try {
set results [exec git add $file_ext]
} trap CHILDSTATUS {results options} {
puts "Warning: No $file_type files update has been found in $folder"
}
}
puts "Files will be added to git now "
set path "src"
cd $path
git_add "*.vhd" "Project" $path
git_add "*.v" "Project" $path
git_add "*.sv" "Project" $path
when I try to run the script, if the a file has LF instead CRLF, it gives warning and it stops.
the error is as follow:
Files will be added to git now warning: LF will be replaced by CRLF in intel/fpgas/fpga_link2/rtl/prbstest.vhd. The file will have its original line endings in your working directory.
while executing "exec git add $file_ext"
(procedure "git_add" line 2)
invoked from within "git_add *.vhd "VHD" $path"
How to fix that?
CodePudding user response:
The exec
command throws an error in two cases:
- If the pipeline (often just a single process) exits with a non-zero error.
- If the pipeline writes to standard error and
exec
captures that.
It's the second case that you're hitting. You need to decide what you want to happen with that message.
One easy option is to redirect it so that the user can see it in their terminal.
proc git_add {file_ext file_type folder} {
try {
set results [exec git add $file_ext 2>@stdout]
} trap CHILDSTATUS {results options} {
puts "Warning: No $file_type files update has been found in $folder"
}
}
This (the 2>@stderr
) redirects it to the Tcl stdout
channel (any channel will do provided it has an OS file descriptor).
If you use open |
to launch the pipeline and have access to its input and/or output, the errors are thrown when you close
the channel. exec
is approximately this (except with more error handling and it is written in C):
proc exec args {
set f [open |$args]
set data [read $f]
close $f
return $data
}
CodePudding user response:
By default, Tcl treats output on the standard error channel as an error. Most likely the warning from the git command appears on stderr. Unfortunately, such a case does not produce a useful error code that you can trap via the try
command. So, either you need to catch general errors with an on error
clause, or you add the -ignorestderr
option to the exec
command to disable this behavior.
set results [exec -ignorestderr git add $file_ext]