【问题标题】:Git Hook - Ruby Code - Interactive InputGit Hook - Ruby 代码 - 交互式输入
【发布时间】:2018-05-08 17:36:54
【问题描述】:

我正在尝试从 git 钩子执行代码(commit-msg 钩子)中获取输入。但是红宝石不能在输入点停止。并且它执行代码就好像输入就像一个 puts 语句。这是我尝试过但失败的代码。

#!/usr/bin/env ruby
require 'open3'

def take_input_here

  Open3.popen3("pwd", :chdir=>"/") {|stdin, stdout, stderr, thread|
    p stdout.read.chomp #=> "/"
  }

  input_val = gets.chomp
  puts input_val
  puts 'Hellow World!'
end
take_input_here
puts "Commit Aborted."
Process.exit(1)

请有人帮我接受这个交互式输入,或者建议我用一种很好的语言来编写 git 钩子。提前致谢。

【问题讨论】:

    标签: ruby git githooks popen3 ruby-thread


    【解决方案1】:

    大多数 Git 挂钩都使用来自 Git 写入信息的管道的标准输入,或者完全与终端断开连接的标准输入。 commit-msg 挂钩属于第二类。

    无论您使用哪种语言:在commit-msg 挂钩中读取标准输入将立即看到 EOF,因为标准输入连接到 /dev/null (Linux/Unix) 或 NUL: (Windows)。

    在类 Unix 系统上,您可以尝试打开 /dev/tty。请注意,如果 Git 是从没有/dev/tty(一些分离的进程,例如通过cron)的东西运行的,或者由于某些其他原因读取/dev/tty 是不好的,这可能会导致其他问题,因此请小心处理。

    【讨论】:

    • 感谢您的建议。那我就得另寻他路了。 :)
    猜你喜欢
    • 2012-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    相关资源
    最近更新 更多