tcl 脚本在 git bash 中出现 LF 警告后停止

发布于 2025-01-11 09:46:27 字数 755 浏览 0 评论 0原文

我有以下脚本用于将文件添加到 git。 当我尝试运行脚本时,我使用 tclsh 在 git bash 上运行它

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

,如果文件有 LF 而不是 CRLF,它会发出警告并停止。

错误如下:

文件将立即添加到 git 警告:LF 将被 CRLF 替换 英特尔/fpgas/fpga_link2/rtl/prbstest.vhd。该文件将有其 工作目录中的原始行结尾。

执行“exec git add $file_ext”时

(过程“git_add”第 2 行)

从“git_add *.vhd”VHD”$path”中调用

如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2025-01-18 09:46:27

默认情况下,Tcl 将标准错误通道上的输出视为错误。 git 命令的警告很可能出现在 stderr 上。不幸的是,这种情况不会产生可以通过 try 命令捕获的有用错误代码。因此,您要么需要使用 on error 子句捕获一般错误,要么将 -ignorestderr 选项添加到 exec 命令中以禁用此功能行为。

set results [exec -ignorestderr git add $file_ext]

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]
别念他 2025-01-18 09:46:27

exec 命令在两种情况下抛出错误:

  1. 如果管道(通常只是一个进程)以非零错误退出。
  2. 如果管道写入标准错误并且 exec 捕获该错误。

这是你遇到的第二个案例。您需要决定您希望通过该消息发生什么。

一种简单的选择是重定向它,以便用户可以在终端中看到它。

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"
  }
}

此(2>@stderr)将其重定向到 Tcl stdout 通道(任何通道都可以,只要它具有操作系统文件描述符)。


如果您使用 open | 启动管道并有权访问其输入和/或输出,则当您关闭 通道时会引发错误。 exec 大约是这样的(除了更多的错误处理并且它是用 C 编写的):

proc exec args {
    set f [open |$args]
    set data [read $f]
    close $f
    return $data
}

The exec command throws an error in two cases:

  1. If the pipeline (often just a single process) exits with a non-zero error.
  2. 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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文