作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用下面的代码递归遍历目录和子文件夹的文件:
Dir.glob("**/*").each do |file|
filename = "#{File.basename(file)}"
output = `git log -1 -r -n 1 --pretty=format:\"%h: #{filename}\" -- #{filename}`
end
Actual Output:
<The format I choose>: folder1
<The format I choose>: a_file
<The format I choose>: folder2
<The format I choose>: a_file
<The format I choose>: another_file
<The format I choose>: folder3
<The format I choose>: a_file
Expected Output:
<The format I choose>: folder1/a_file
<The format I choose>: folder2/a_file
<The format I choose>: folder2/another_file
<The format I choose>: folder3/a_file
最佳答案
基本上原因是你正在运行 File.basename
它为您提供文件的“基本名称”而不是相对路径。
另外 .glob("**/*")
还包括目录,因此您需要考虑到这一点。
这就是我要做的...
Dir.glob("**/*").each do |file|
next if File.directory?(file) # skip the loop if the file is a directory
puts file
output = `git log -1 -r -n 1 --pretty=format:"%cd [%h]" -- #{file}`
puts output
end
关于ruby - 以递归方式遍历目录和子目录,在 ruby 中显示 'path/file',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40016856/
我是一名优秀的程序员,十分优秀!