Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: MetOffice/CSET
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9b5477442d232fb39d56a4e8c600a2d3ec947110
Choose a base ref
..
head repository: MetOffice/CSET
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: a7c60f9fd47664a7af0e745a0402465c64bda3be
Choose a head ref
Showing with 35 additions and 13 deletions.
  1. +19 −10 .pre-commit-config.yaml
  2. +16 −3 scripts/sort_json.py
29 changes: 19 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -5,26 +5,35 @@ ci:

repos:
- repo: https://github.com/codespell-project/codespell
rev: 193cd7d27cd571f79358af09a8fb8997e54f8fff # frozen: v2.3.0
rev: 193cd7d27cd571f79358af09a8fb8997e54f8fff # frozen: v2.3.0
hooks:
- id: codespell
# Additional dependency needed until Python 3.11 is our minimum version.
additional_dependencies:
- tomli
- id: codespell
# Additional dependency needed until Python 3.11 is our minimum version.
additional_dependencies:
- tomli

- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: 73413df07b4ab0bf103ca1ae73c7cec5c0ace593 # frozen: v0.9.2
rev: 73413df07b4ab0bf103ca1ae73c7cec5c0ace593 # frozen: v0.9.2
hooks:
- id: ruff
args: [ --fix, --show-fixes, --exit-non-zero-on-fix ]
args: [--fix, --show-fixes, --exit-non-zero-on-fix]
- id: ruff-format

- repo: local
hooks:
- id: sort-json
name: Sort JSON
description: Recursively sort JSON files by key.
entry: ./scripts/sort_json.py
language: script
types: [json]
files: src/CSET/operators/_colorbar_definition.json

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
rev: cef0300fd0fc4d2a87a85fa2093c6b283ea36f4b # frozen: v5.0.0
hooks:
- id: check-added-large-files
args: ['--maxkb=10240']
args: ["--maxkb=10240"]
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
19 changes: 16 additions & 3 deletions scripts/sort_json.py
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@
"""Sort a JSON file."""

import argparse
import hashlib
import json
import os
import sys
@@ -52,9 +53,21 @@ def main():
# End file with a newline.
fp.write("\n")

# Atomically replace original file.
os.rename(output_file, args.input_file)
return 0
# Check if sorting changed the file.
with open(args.input_file, "rb") as fp:
original_digest = hashlib.file_digest(fp, "sha256").digest()
with open(output_file, "rb") as fp:
new_digest = hashlib.file_digest(fp, "sha256").digest()

if original_digest == new_digest:
# File unchanged, don't overwrite and return zero.
os.remove(output_file)
return 0
else:
# File changed, overwrite and return non-zero.
# Atomically replace original file via rename.
os.rename(output_file, args.input_file)
return 1


if __name__ == "__main__":