summaryrefslogtreecommitdiff
path: root/tool
diff options
context:
space:
mode:
authorTakashi Kokubun <takashikkbn@gmail.com>2022-12-12 23:34:30 -0800
committerGitHub <noreply@github.com>2022-12-12 23:34:30 -0800
commit7edcdc380f3bb6f5a81bfb5798c339acd844a0d3 (patch)
tree4a3b22f1765fe2edc6525595af264c3970201799 /tool
parent3e00cb8256cfe0640cb1d744d07d496128edb7e9 (diff)
Avoid overwriting revision.h when .git doesn't exist (#6915)
* Avoid overwriting revision.h when .git doesn't exist * Overwrite revision.h if it's blank
Notes
Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
Diffstat (limited to 'tool')
-rwxr-xr-xtool/file2lastrev.rb6
-rw-r--r--tool/lib/output.rb6
2 files changed, 8 insertions, 4 deletions
diff --git a/tool/file2lastrev.rb b/tool/file2lastrev.rb
index 1dfb01ab9e..6200e78a56 100755
--- a/tool/file2lastrev.rb
+++ b/tool/file2lastrev.rb
@@ -25,6 +25,7 @@ end
time_format = '%Y-%m-%dT%H:%M:%S%z'
vcs = nil
+create_only = false
OptionParser.new {|opts|
opts.banner << " paths..."
vcs_options = VCS.define_options(opts)
@@ -62,6 +63,9 @@ OptionParser.new {|opts|
abort "#{File.basename(Program)}: #{e.message}" unless @suppress_not_found
opts.remove
(vcs = VCS::Null.new(nil)).set_options(vcs_options)
+ if @format == :revision_h
+ create_only = true # don't overwrite existing revision.h when .git doesn't exist
+ end
end
}
@@ -92,7 +96,7 @@ ok = true
begin
data = formatter[*vcs.get_revisions(arg)]
data.sub!(/(?<!\A|\n)\z/, "\n")
- @output.write(data, overwrite: true)
+ @output.write(data, overwrite: true, create_only: create_only)
rescue => e
warn "#{File.basename(Program)}: #{e.message}"
ok = false
diff --git a/tool/lib/output.rb b/tool/lib/output.rb
index 3a9f3d87ae..b1c4f4a866 100644
--- a/tool/lib/output.rb
+++ b/tool/lib/output.rb
@@ -18,7 +18,7 @@ class Output
@vpath.def_options(opt)
end
- def write(data, overwrite: false)
+ def write(data, overwrite: false, create_only: false)
unless @path
$stdout.print data
return true
@@ -28,8 +28,8 @@ class Output
updated = color.fail("updated")
outpath = nil
- if (@ifchange or overwrite) and
- (@vpath.open(@path, "rb") {|f| outpath = f.path; f.read == data if @ifchange} rescue false)
+ if (@ifchange or overwrite or create_only) and
+ (@vpath.open(@path, "rb") {|f| outpath = f.path; (@ifchange and f.read == data) or (create_only and !f.read.empty?)} rescue false)
puts "#{outpath} #{unchanged}"
written = false
else