Add --print-changes to print all changed lines (useful in dry-run mode) + Return the number of formatted files in this mode
This commit is contained in:
parent
91bff90bf2
commit
8458be2f70
@ -540,6 +540,13 @@ void ASConsole::formatFile(const string& fileName_)
|
|||||||
ASStreamIterator<stringstream> streamIterator(&in);
|
ASStreamIterator<stringstream> streamIterator(&in);
|
||||||
formatter.init(&streamIterator);
|
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
|
// format the file
|
||||||
while (formatter.hasMoreLines())
|
while (formatter.hasMoreLines())
|
||||||
{
|
{
|
||||||
@ -566,15 +573,23 @@ void ASConsole::formatFile(const string& fileName_)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (filesAreIdentical)
|
if (filesAreIdentical || printChanges)
|
||||||
{
|
{
|
||||||
if (streamIterator.checkForEmptyLine)
|
if (streamIterator.checkForEmptyLine)
|
||||||
{
|
{
|
||||||
if (nextLine.find_first_not_of(" \t") != string::npos)
|
if (nextLine.find_first_not_of(" \t") != string::npos)
|
||||||
|
{
|
||||||
|
if (printChanges)
|
||||||
|
printChangedLine(displayName, nextLine, linesOut);
|
||||||
filesAreIdentical = false;
|
filesAreIdentical = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (!streamIterator.compareToInputBuffer(nextLine))
|
else if (!streamIterator.compareToInputBuffer(nextLine))
|
||||||
|
{
|
||||||
|
if (printChanges)
|
||||||
|
printChangedLine(displayName, nextLine, linesOut);
|
||||||
filesAreIdentical = false;
|
filesAreIdentical = false;
|
||||||
|
}
|
||||||
streamIterator.checkForEmptyLine = false;
|
streamIterator.checkForEmptyLine = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -585,19 +600,14 @@ void ASConsole::formatFile(const string& fileName_)
|
|||||||
filesAreIdentical = false;
|
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 file has changed, write the new file
|
||||||
if (!filesAreIdentical || streamIterator.getLineEndChange(lineEndFormat))
|
if (!filesAreIdentical || streamIterator.getLineEndChange(lineEndFormat))
|
||||||
{
|
{
|
||||||
if (!isDryRun)
|
if (!isDryRun)
|
||||||
writeFile(fileName_, encoding, out);
|
writeFile(fileName_, encoding, out);
|
||||||
printMsg(_("Formatted %s\n"), displayName);
|
printMsg(_("Formatted %s\n"), displayName);
|
||||||
|
if (printChanges)
|
||||||
|
printSeparatingLine();
|
||||||
filesFormatted++;
|
filesFormatted++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@ -610,6 +620,20 @@ void ASConsole::formatFile(const string& fileName_)
|
|||||||
assert(formatter.getChecksumDiff() == 0);
|
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
|
// build a vector of argv options
|
||||||
// the program path argv[0] is excluded
|
// the program path argv[0] is excluded
|
||||||
vector<string> ASConsole::getArgvOptions(int argc, char** argv) const
|
vector<string> ASConsole::getArgvOptions(int argc, char** argv) const
|
||||||
@ -662,6 +686,10 @@ bool ASConsole::getIgnoreExcludeErrorsDisplay() const
|
|||||||
bool ASConsole::getIsDryRun() const
|
bool ASConsole::getIsDryRun() const
|
||||||
{ return isDryRun; }
|
{ return isDryRun; }
|
||||||
|
|
||||||
|
// for unit testing
|
||||||
|
bool ASConsole::getPrintChanges() const
|
||||||
|
{ return printChanges; }
|
||||||
|
|
||||||
// for unit testing
|
// for unit testing
|
||||||
bool ASConsole::getIsFormattedOnly() const
|
bool ASConsole::getIsFormattedOnly() const
|
||||||
{ return isFormattedOnly; }
|
{ return isFormattedOnly; }
|
||||||
@ -800,6 +828,9 @@ void ASConsole::setIsRecursive(bool state)
|
|||||||
void ASConsole::setIsDryRun(bool state)
|
void ASConsole::setIsDryRun(bool state)
|
||||||
{ isDryRun = state; }
|
{ isDryRun = state; }
|
||||||
|
|
||||||
|
void ASConsole::setPrintChanges(bool state)
|
||||||
|
{ printChanges = state; }
|
||||||
|
|
||||||
void ASConsole::setIsVerbose(bool state)
|
void ASConsole::setIsVerbose(bool state)
|
||||||
{ isVerbose = state; }
|
{ isVerbose = state; }
|
||||||
|
|
||||||
@ -3137,6 +3168,10 @@ void ASOptions::parseOption(const string& arg, const string& errorInfo)
|
|||||||
{
|
{
|
||||||
g_console->setIsDryRun(true);
|
g_console->setIsDryRun(true);
|
||||||
}
|
}
|
||||||
|
else if (isOption(arg, "print-changes"))
|
||||||
|
{
|
||||||
|
g_console->setPrintChanges(true);
|
||||||
|
}
|
||||||
else if ( isOption(arg, "Z", "preserve-date") )
|
else if ( isOption(arg, "Z", "preserve-date") )
|
||||||
{
|
{
|
||||||
g_console->setPreserveDate(true);
|
g_console->setPreserveDate(true);
|
||||||
@ -3806,8 +3841,16 @@ int main(int argc, char** argv)
|
|||||||
// process entries in the fileNameVector
|
// process entries in the fileNameVector
|
||||||
g_console->processFiles();
|
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;
|
delete g_console;
|
||||||
return EXIT_SUCCESS;
|
return return_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // ASTYLE_LIB
|
#endif // ASTYLE_LIB
|
||||||
|
@ -232,6 +232,7 @@ private: // variables
|
|||||||
// command line options
|
// command line options
|
||||||
bool isRecursive; // recursive option
|
bool isRecursive; // recursive option
|
||||||
bool isDryRun; // dry-run option
|
bool isDryRun; // dry-run option
|
||||||
|
bool printChanges; // print-changes option
|
||||||
bool noBackup; // suffix=none option
|
bool noBackup; // suffix=none option
|
||||||
bool preserveDate; // preserve-date option
|
bool preserveDate; // preserve-date option
|
||||||
bool isVerbose; // verbose option
|
bool isVerbose; // verbose option
|
||||||
@ -307,6 +308,7 @@ public: // functions
|
|||||||
bool getIgnoreExcludeErrors() const;
|
bool getIgnoreExcludeErrors() const;
|
||||||
bool getIgnoreExcludeErrorsDisplay() const;
|
bool getIgnoreExcludeErrorsDisplay() const;
|
||||||
bool getIsDryRun() const;
|
bool getIsDryRun() const;
|
||||||
|
bool getPrintChanges() const;
|
||||||
bool getIsFormattedOnly() const;
|
bool getIsFormattedOnly() const;
|
||||||
bool getIsQuiet() const;
|
bool getIsQuiet() const;
|
||||||
bool getIsRecursive() const;
|
bool getIsRecursive() const;
|
||||||
@ -325,6 +327,7 @@ public: // functions
|
|||||||
void setIgnoreExcludeErrors(bool state);
|
void setIgnoreExcludeErrors(bool state);
|
||||||
void setIgnoreExcludeErrorsAndDisplay(bool state);
|
void setIgnoreExcludeErrorsAndDisplay(bool state);
|
||||||
void setIsDryRun(bool state);
|
void setIsDryRun(bool state);
|
||||||
|
void setPrintChanges(bool state);
|
||||||
void setIsFormattedOnly(bool state);
|
void setIsFormattedOnly(bool state);
|
||||||
void setIsQuiet(bool state);
|
void setIsQuiet(bool state);
|
||||||
void setIsRecursive(bool state);
|
void setIsRecursive(bool state);
|
||||||
@ -347,6 +350,7 @@ private: // functions
|
|||||||
ASConsole& operator=(ASConsole&); // not to be implemented
|
ASConsole& operator=(ASConsole&); // not to be implemented
|
||||||
void correctMixedLineEnds(ostringstream& out);
|
void correctMixedLineEnds(ostringstream& out);
|
||||||
void formatFile(const string& fileName_);
|
void formatFile(const string& fileName_);
|
||||||
|
void printChangedLine(const string& fileName, const string& line, int lineNumber);
|
||||||
string getCurrentDirectory(const string& fileName_) const;
|
string getCurrentDirectory(const string& fileName_) const;
|
||||||
void getFileNames(const string& directory, const string& wildcard);
|
void getFileNames(const string& directory, const string& wildcard);
|
||||||
void getFilePaths(string& filePath);
|
void getFilePaths(string& filePath);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user