From 71af7209d92838ebd9bf0e5095ef4077e27b1b4c Mon Sep 17 00:00:00 2001
From: Deanna Garcia <deannagarcia@google.com>
Date: Wed, 10 Aug 2022 20:37:27 +0000
Subject: [PATCH] Use protoc version for --version

---
 .../protobuf/compiler/command_line_interface.cc      |  2 +-
 src/google/protobuf/stubs/common.cc                  | 12 ++++++++++--
 src/google/protobuf/stubs/common.h                   |  7 ++++++-
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/src/google/protobuf/compiler/command_line_interface.cc b/src/google/protobuf/compiler/command_line_interface.cc
index f5cc9a9173..652ce04495 100644
--- a/src/google/protobuf/compiler/command_line_interface.cc
+++ b/src/google/protobuf/compiler/command_line_interface.cc
@@ -1844,7 +1844,7 @@ CommandLineInterface::InterpretArgument(const std::string& name,
     if (!version_info_.empty()) {
       std::cout << version_info_ << std::endl;
     }
-    std::cout << "libprotoc " << internal::VersionString(PROTOBUF_VERSION)
+    std::cout << "libprotoc " << internal::ProtocVersionString(PROTOBUF_VERSION)
               << PROTOBUF_VERSION_SUFFIX << std::endl;
     return PARSE_ARGUMENT_DONE_AND_EXIT;  // Exit without running compiler.
 
diff --git a/src/google/protobuf/stubs/common.cc b/src/google/protobuf/stubs/common.cc
index f1a815022d..5e6ce70828 100644
--- a/src/google/protobuf/stubs/common.cc
+++ b/src/google/protobuf/stubs/common.cc
@@ -92,7 +92,7 @@ void VerifyVersion(int headerVersion,
   }
 }
 
-std::string VersionString(int version) {
+std::string VersionString(int version, bool cppMajor) {
   int major = version / 1000000;
   int minor = (version / 1000) % 1000;
   int micro = version % 1000;
@@ -100,7 +100,11 @@ std::string VersionString(int version) {
   // 128 bytes should always be enough, but we use snprintf() anyway to be
   // safe.
   char buffer[128];
-  snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
+  if (cppMajor) {
+    snprintf(buffer, sizeof(buffer), "%d.%d.%d", major, minor, micro);
+  } else {
+    snprintf(buffer, sizeof(buffer), "%d.%d", minor, micro);
+  }
 
   // Guard against broken MSVC snprintf().
   buffer[sizeof(buffer)-1] = '\0';
@@ -108,6 +112,10 @@ std::string VersionString(int version) {
   return buffer;
 }
 
+std::string ProtocVersionString(int version) {
+  return VersionString(version, false);
+}
+
 }  // namespace internal
 
 // ===================================================================
diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h
index 427df67d4f..2d6bcf8615 100644
--- a/src/google/protobuf/stubs/common.h
+++ b/src/google/protobuf/stubs/common.h
@@ -106,7 +106,12 @@ void PROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
                                    const char* filename);
 
 // Converts a numeric version number to a string.
-std::string PROTOBUF_EXPORT VersionString(int version);
+// If cppMajor is true the string will have the C++ major version, otherwise
+// the string will be the protoc version.
+std::string PROTOBUF_EXPORT VersionString(int version, bool cppMajor = true);
+
+// Prints the protoc compiler version (no major version)
+std::string PROTOBUF_EXPORT ProtocVersionString(int version);
 
 }  // namespace internal