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


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";
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to