From 8458be2f70c134cb24ca3bdecc8f8f436b121299 Mon Sep 17 00:00:00 2001 From: Peter Vingelmann Date: Wed, 14 Sep 2016 20:19:13 +0200 Subject: [PATCH] Add --print-changes to print all changed lines (useful in dry-run mode) + Return the number of formatted files in this mode --- src/astyle_main.cpp | 63 ++++++++++++++++++++++++++++++++++++++------- src/astyle_main.h | 4 +++ 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/astyle_main.cpp b/src/astyle_main.cpp index 567370c..952340c 100644 --- a/src/astyle_main.cpp +++ b/src/astyle_main.cpp @@ -540,6 +540,13 @@ void ASConsole::formatFile(const string& fileName_) ASStreamIterator streamIterator(&in); formatter.init(&streamIterator); + // remove targetDirectory from filename if required by print + string displayName; + if (hasWildcard) + displayName = fileName_.substr(targetDirectory.length() + 1); + else + displayName = fileName_; + // format the file while (formatter.hasMoreLines()) { @@ -566,15 +573,23 @@ void ASConsole::formatFile(const string& fileName_) } } - if (filesAreIdentical) + if (filesAreIdentical || printChanges) { if (streamIterator.checkForEmptyLine) { if (nextLine.find_first_not_of(" \t") != string::npos) + { + if (printChanges) + printChangedLine(displayName, nextLine, linesOut); filesAreIdentical = false; + } } else if (!streamIterator.compareToInputBuffer(nextLine)) - filesAreIdentical = false; + { + if (printChanges) + printChangedLine(displayName, nextLine, linesOut); + filesAreIdentical = false; + } streamIterator.checkForEmptyLine = false; } } @@ -585,19 +600,14 @@ void ASConsole::formatFile(const string& fileName_) filesAreIdentical = false; } - // remove targetDirectory from filename if required by print - string displayName; - if (hasWildcard) - displayName = fileName_.substr(targetDirectory.length() + 1); - else - displayName = fileName_; - // if file has changed, write the new file if (!filesAreIdentical || streamIterator.getLineEndChange(lineEndFormat)) { if (!isDryRun) writeFile(fileName_, encoding, out); printMsg(_("Formatted %s\n"), displayName); + if (printChanges) + printSeparatingLine(); filesFormatted++; } else @@ -610,6 +620,20 @@ void ASConsole::formatFile(const string& fileName_) assert(formatter.getChecksumDiff() == 0); } +void ASConsole::printChangedLine(const string& fileName, const string& line, int lineNumber) +{ + if (printChanges && !isQuiet) + { + // Print the filename, as this is the first change for file + if (filesAreIdentical) + { + printSeparatingLine(); + printf("Changes in %s\n", fileName.c_str()); + } + printf("Line %5d: %s\n", lineNumber, line.c_str()); + } +} + // build a vector of argv options // the program path argv[0] is excluded vector ASConsole::getArgvOptions(int argc, char** argv) const @@ -662,6 +686,10 @@ bool ASConsole::getIgnoreExcludeErrorsDisplay() const bool ASConsole::getIsDryRun() const { return isDryRun; } +// for unit testing +bool ASConsole::getPrintChanges() const +{ return printChanges; } + // for unit testing bool ASConsole::getIsFormattedOnly() const { return isFormattedOnly; } @@ -800,6 +828,9 @@ void ASConsole::setIsRecursive(bool state) void ASConsole::setIsDryRun(bool state) { isDryRun = state; } +void ASConsole::setPrintChanges(bool state) +{ printChanges = state; } + void ASConsole::setIsVerbose(bool state) { isVerbose = state; } @@ -3137,6 +3168,10 @@ void ASOptions::parseOption(const string& arg, const string& errorInfo) { g_console->setIsDryRun(true); } + else if (isOption(arg, "print-changes")) + { + g_console->setPrintChanges(true); + } else if ( isOption(arg, "Z", "preserve-date") ) { g_console->setPreserveDate(true); @@ -3806,8 +3841,16 @@ int main(int argc, char** argv) // process entries in the fileNameVector g_console->processFiles(); + int return_code = EXIT_SUCCESS; + if (g_console->getPrintChanges() && g_console->getFilesFormatted() > 0) + { + // Use the number of formatted files as a return code with the + // --print-changes option + return_code = g_console->getFilesFormatted(); + } + delete g_console; - return EXIT_SUCCESS; + return return_code; } #endif // ASTYLE_LIB diff --git a/src/astyle_main.h b/src/astyle_main.h index bfbc0e2..43d5fd2 100644 --- a/src/astyle_main.h +++ b/src/astyle_main.h @@ -232,6 +232,7 @@ private: // variables // command line options bool isRecursive; // recursive option bool isDryRun; // dry-run option + bool printChanges; // print-changes option bool noBackup; // suffix=none option bool preserveDate; // preserve-date option bool isVerbose; // verbose option @@ -307,6 +308,7 @@ public: // functions bool getIgnoreExcludeErrors() const; bool getIgnoreExcludeErrorsDisplay() const; bool getIsDryRun() const; + bool getPrintChanges() const; bool getIsFormattedOnly() const; bool getIsQuiet() const; bool getIsRecursive() const; @@ -325,6 +327,7 @@ public: // functions void setIgnoreExcludeErrors(bool state); void setIgnoreExcludeErrorsAndDisplay(bool state); void setIsDryRun(bool state); + void setPrintChanges(bool state); void setIsFormattedOnly(bool state); void setIsQuiet(bool state); void setIsRecursive(bool state); @@ -347,6 +350,7 @@ private: // functions ASConsole& operator=(ASConsole&); // not to be implemented void correctMixedLineEnds(ostringstream& out); void formatFile(const string& fileName_); + void printChangedLine(const string& fileName, const string& line, int lineNumber); string getCurrentDirectory(const string& fileName_) const; void getFileNames(const string& directory, const string& wildcard); void getFilePaths(string& filePath);