@ShellMethod(key = "show archived commits", value = "Read commits from archived files and show details")
  public String showCommits(
      @ShellOption(value = {"--skipMetadata"}, help = "Skip displaying commit metadata",
          defaultValue = "true") boolean skipMetadata,
      @ShellOption(value = {"--limit"}, help = "Limit commits", defaultValue = "10") final Integer limit,
      @ShellOption(value = {"--sortBy"}, help = "Sorting Field", defaultValue = "") final String sortByField,
      @ShellOption(value = {"--desc"}, help = "Ordering", defaultValue = "false") final boolean descending,
      @ShellOption(value = {"--headeronly"}, help = "Print Header Only",
              defaultValue = "false") final boolean headerOnly)
      throws IOException {

    System.out.println("===============> Showing only " + limit + " archived commits <===============");
    HoodieTableMetaClient metaClient = HoodieCLI.getTableMetaClient();
    StoragePath basePath = metaClient.getBasePath();
    StoragePath archivePath =
        new StoragePath(metaClient.getArchivePath() + "/.commits_.archive*");
    List<StoragePathInfo> pathInfoList =
        HoodieStorageUtils.getStorage(basePath, HoodieCLI.conf).globEntries(archivePath);
    List<Comparable[]> allCommits = new ArrayList<>();
    for (StoragePathInfo pathInfo : pathInfoList) {
      // read the archived file
      try (HoodieLogFormat.Reader reader = HoodieLogFormat.newReader(HoodieStorageUtils.getStorage(basePath, HoodieCLI.conf),
          new HoodieLogFile(pathInfo.getPath()), HoodieArchivedMetaEntry.getClassSchema())) {
        List<IndexedRecord> readRecords = new ArrayList<>();
        // read the avro blocks
        while (reader.hasNext()) {
          HoodieAvroDataBlock blk = (HoodieAvroDataBlock) reader.next();
          try (ClosableIterator<HoodieRecord<IndexedRecord>> recordItr = blk.getRecordIterator(HoodieRecordType.AVRO)) {
            recordItr.forEachRemaining(r -> readRecords.add(r.getData()));
          }
        }
        List<Comparable[]> readCommits = readRecords.stream().map(r -> (GenericRecord) r)
            .map(r -> readCommit(r, skipMetadata)).collect(Collectors.toList());
        allCommits.addAll(readCommits);
      }
    }

    TableHeader header = new TableHeader().addTableHeaderField("CommitTime").addTableHeaderField("CommitType");

    if (!skipMetadata) {
      header = header.addTableHeaderField("CommitDetails");
    }

    return HoodiePrintHelper.print(header, new HashMap<>(), sortByField, descending, limit, headerOnly, allCommits);
  }
@Test
  public void testShowCommits() throws Exception {
    Object cmdResult = shell.evaluate(() -> "show archived commits --limit 5");
    assertTrue(ShellEvaluationResultUtil.isSuccess(cmdResult));
    final List<Comparable[]> rows = new ArrayList<>();

    // Test default skipMetadata and limit 5
    TableHeader header = new TableHeader().addTableHeaderField("CommitTime").addTableHeaderField("CommitType");
    Comparable[] result1 = new Comparable[] {"100", "commit"};
    Comparable[] result2 = new Comparable[] {"101", "commit"};
    rows.add(result1);
    rows.add(result1);
    rows.add(result1);
    rows.add(result2);
    rows.add(result2);
    String expected = HoodiePrintHelper.print(header, new HashMap<>(), "", false, 5, false, rows);
    expected = removeNonWordAndStripSpace(expected);
    String got = removeNonWordAndStripSpace(cmdResult.toString());
    assertEquals(expected, got);

    // Test with Metadata and no limit
    cmdResult = shell.evaluate(() -> "show archived commits --skipMetadata false --limit 0");
    assertTrue(ShellEvaluationResultUtil.isSuccess(cmdResult));

    rows.clear();

    for (int i = 100; i < 102; i++) {
      String instant = String.valueOf(i);
      // Since HoodiePrintHelper order data by default, need to order commitMetadata
      HoodieCommitMetadata metadata = HoodieTestCommitMetadataGenerator.generateCommitMetadata(tablePath, instant);
      Comparable[] result = new Comparable[] {
          instant, "commit", HoodieTestCommitUtilities.convertAndOrderCommitMetadata(metadata)};
      rows.add(result);
      rows.add(result);
      rows.add(result);
    }
    header = header.addTableHeaderField("CommitDetails");
    expected = HoodiePrintHelper.print(header, new HashMap<>(), "", false, 0, false, rows);
    expected = removeNonWordAndStripSpace(expected);
    got = removeNonWordAndStripSpace(cmdResult.toString());
    assertEquals(expected, got);
  }