[PATCH] D70553: [clang-apply-replacements] Add command line option to overwrite readonly files.

Zachary Turner via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 21 11:09:30 PST 2019


zturner created this revision.
zturner added reviewers: aaron.ballman, alexfh, hokein.
zturner added a project: clang-tools-extra.
Herald added a project: clang.

Some source code control systems attempt to prevent you from editing files unless you explicitly check them out.  This makes it impossible to use certain refactoring tools such as this, since only the tool itself is able to determine the set of files that need to be modified.  This patch adds a `--force` option which clears the read-only bit of the file so that it can be modified.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D70553

Files:
  clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp


Index: clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
===================================================================
--- clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
+++ clang-tools-extra/clang-apply-replacements/tool/ClangApplyReplacementsMain.cpp
@@ -48,6 +48,10 @@
              "Use -style to choose formatting style.\n"),
     cl::cat(FormattingCategory));
 
+static cl::opt<bool> ForceOverwriteReadOnly(
+    "force", cl::desc("Overwrite read-only files when applying replacements\n"),
+    cl::init(false), cl::cat(ReplacementCategory));
+
 // FIXME: Consider making the default behaviour for finding a style
 // configuration file to start the search anew for every file being changed to
 // handle situations where the style is different for different parts of a
@@ -152,6 +156,13 @@
 
     // Write new file to disk
     std::error_code EC;
+    if (ForceOverwriteReadOnly) {
+      using namespace llvm::sys::fs;
+      if (auto ErrorOrPerms = getPermissions(FileName)) {
+        perms P = ErrorOrPerms.get();
+        setPermissions(FileName, P | all_write);
+      }
+    }
     llvm::raw_fd_ostream FileStream(FileName, EC, llvm::sys::fs::OF_None);
     if (EC) {
       llvm::errs() << "Could not open " << FileName << " for writing\n";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D70553.230498.patch
Type: text/x-patch
Size: 1324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191121/c78ae634/attachment-0001.bin>


More information about the cfe-commits mailing list