Skip to content

Commit bc690d4

Browse files
authored
Merge pull request #4415 from jepler/pre-commit-no-run-if-empty
pre-commit: code formatter fixes
2 parents f7a988b + 1af6aa2 commit bc690d4

File tree

11 files changed

+101
-80
lines changed

11 files changed

+101
-80
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ repos:
2121
language: system
2222
- id: formatting
2323
name: Formatting
24-
entry: sh -c "git diff --staged --name-only | xargs python3 tools/codeformat.py"
24+
entry: python3 tools/codeformat.py
2525
types_or: [c, python]
2626
language: system

BUILDING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,23 @@ Example:
8585

8686
If your port/build includes `arm-none-eabi-gdb-py`, consider using it instead, as it can be used for better register
8787
debugging with https://github.com/bnahill/PyCortexMDebug
88+
89+
# Code Quality Checks
90+
91+
We apply code quality checks using pre-commit. Install pre-commit once per system with
92+
93+
python3 -mpip install pre-commit
94+
95+
Activate it once per git clone with
96+
97+
pre-commit --install
98+
99+
Pre-commit also requires some additional programs to be installed through your package manager:
100+
101+
* Standard Unix tools such as make, find, etc
102+
* The gettext package, any modern version
103+
* uncrustify version 0.71 (0.72 is also tested)
104+
105+
Each time you create a git commit, the pre-commit quality checks will be run. You can also run them e.g., with `pre-commit run foo.c` or `pre-commit run --all` to run on all files whether modified or not.
106+
107+
Some pre-commit quality checks require your active attention to resolve, others (such as the formatting checks of uncrustify) are made automatically and must simply be incorporated into your code changes by committing them.

ports/atmel-samd/eic_handler.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "common-hal/rotaryio/IncrementalEncoder.h"
3030
#include "common-hal/countio/Counter.h"
3131
#include "shared-bindings/microcontroller/__init__.h"
32-
//#include "samd/external_interrupts.h"
32+
// #include "samd/external_interrupts.h"
3333
#include "eic_handler.h"
3434

3535
// Which handler should be called for a particular channel?
@@ -42,31 +42,31 @@ void set_eic_handler(uint8_t channel, uint8_t eic_handler) {
4242
void shared_eic_handler(uint8_t channel) {
4343
uint8_t handler = eic_channel_handler[channel];
4444
switch (handler) {
45-
#if CIRCUITPY_PULSEIO
46-
case EIC_HANDLER_PULSEIN:
47-
pulsein_interrupt_handler(channel);
48-
break;
49-
#endif
45+
#if CIRCUITPY_PULSEIO
46+
case EIC_HANDLER_PULSEIN:
47+
pulsein_interrupt_handler(channel);
48+
break;
49+
#endif
5050

51-
#if CIRCUITPY_PS2IO
52-
case EIC_HANDLER_PS2:
53-
ps2_interrupt_handler(channel);
54-
break;
55-
#endif
51+
#if CIRCUITPY_PS2IO
52+
case EIC_HANDLER_PS2:
53+
ps2_interrupt_handler(channel);
54+
break;
55+
#endif
5656

57-
#if CIRCUITPY_ROTARYIO
58-
case EIC_HANDLER_INCREMENTAL_ENCODER:
59-
incrementalencoder_interrupt_handler(channel);
60-
break;
61-
#endif
57+
#if CIRCUITPY_ROTARYIO
58+
case EIC_HANDLER_INCREMENTAL_ENCODER:
59+
incrementalencoder_interrupt_handler(channel);
60+
break;
61+
#endif
6262

63-
#if CIRCUITPY_COUNTIO
64-
case EIC_HANDLER_COUNTER:
65-
counter_interrupt_handler(channel);
66-
break;
67-
#endif
63+
#if CIRCUITPY_COUNTIO
64+
case EIC_HANDLER_COUNTER:
65+
counter_interrupt_handler(channel);
66+
break;
67+
#endif
6868

69-
default:
70-
break;
69+
default:
70+
break;
7171
}
7272
}

ports/atmel-samd/fatfs_port.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@
3535
#endif
3636

3737
DWORD get_fattime(void) {
38-
#if CIRCUITPY_RTC
38+
#if CIRCUITPY_RTC
3939
timeutils_struct_time_t tm;
4040
common_hal_rtc_get_time(&tm);
4141
return ((tm.tm_year - 1980) << 25) | (tm.tm_mon << 21) | (tm.tm_mday << 16) |
42-
(tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1);
43-
#else
42+
(tm.tm_hour << 11) | (tm.tm_min << 5) | (tm.tm_sec >> 1);
43+
#else
4444
return ((2016 - 1980) << 25) | ((9) << 21) | ((1) << 16) | ((16) << 11) | ((43) << 5) | (35 / 2);
45-
#endif
45+
#endif
4646

4747

4848
}

ports/atmel-samd/ld_defines.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@
99
// The next line is a marker to start looking for definitions. Lines above the next line are ignored.
1010
// START_LD_DEFINES
1111

12-
/*RAM_SIZE=*/ RAM_SIZE;
13-
/*FLASH_SIZE=*/ FLASH_SIZE;
12+
/*RAM_SIZE=*/ RAM_SIZE;
13+
/*FLASH_SIZE=*/ FLASH_SIZE;
1414

15-
/*BOOTLOADER_SIZE=*/ BOOTLOADER_SIZE;
16-
/*BOOTLOADER_START_ADDR=*/ BOOTLOADER_START_ADDR;
15+
/*BOOTLOADER_SIZE=*/ BOOTLOADER_SIZE;
16+
/*BOOTLOADER_START_ADDR=*/ BOOTLOADER_START_ADDR;
1717

18-
/*CIRCUITPY_DEFAULT_STACK_SIZE=*/ CIRCUITPY_DEFAULT_STACK_SIZE;
18+
/*CIRCUITPY_DEFAULT_STACK_SIZE=*/ CIRCUITPY_DEFAULT_STACK_SIZE;
1919

20-
/*CIRCUITPY_FIRMWARE_START_ADDR=*/ CIRCUITPY_FIRMWARE_START_ADDR;
21-
/*CIRCUITPY_FIRMWARE_SIZE=*/ CIRCUITPY_FIRMWARE_SIZE;
20+
/*CIRCUITPY_FIRMWARE_START_ADDR=*/ CIRCUITPY_FIRMWARE_START_ADDR;
21+
/*CIRCUITPY_FIRMWARE_SIZE=*/ CIRCUITPY_FIRMWARE_SIZE;
2222

23-
/*CIRCUITPY_INTERNAL_CONFIG_START_ADDR=*/ CIRCUITPY_INTERNAL_CONFIG_START_ADDR;
24-
/*CIRCUITPY_INTERNAL_CONFIG_SIZE=*/ CIRCUITPY_INTERNAL_CONFIG_SIZE;
23+
/*CIRCUITPY_INTERNAL_CONFIG_START_ADDR=*/ CIRCUITPY_INTERNAL_CONFIG_START_ADDR;
24+
/*CIRCUITPY_INTERNAL_CONFIG_SIZE=*/ CIRCUITPY_INTERNAL_CONFIG_SIZE;
2525

26-
/*CIRCUITPY_INTERNAL_NVM_START_ADDR=*/ CIRCUITPY_INTERNAL_NVM_START_ADDR;
27-
/*CIRCUITPY_INTERNAL_NVM_SIZE=*/ CIRCUITPY_INTERNAL_NVM_SIZE;
26+
/*CIRCUITPY_INTERNAL_NVM_START_ADDR=*/ CIRCUITPY_INTERNAL_NVM_START_ADDR;
27+
/*CIRCUITPY_INTERNAL_NVM_SIZE=*/ CIRCUITPY_INTERNAL_NVM_SIZE;
2828

2929
/*CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_START_ADDR=*/ CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_START_ADDR;
30-
/*CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE=*/ CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE;
30+
/*CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE=*/ CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE;

ports/atmel-samd/mpconfigport.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
// HMCRAMC0_SIZE is defined in the ASF4 include files for each SAMD21 chip.
4040
#define RAM_SIZE HMCRAMC0_SIZE
41-
#define BOOTLOADER_SIZE (8*1024)
41+
#define BOOTLOADER_SIZE (8 * 1024)
4242
#define CIRCUITPY_MCU_FAMILY samd21
4343
#define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21"
4444
#define SPI_FLASH_MAX_BAUDRATE 8000000
@@ -67,7 +67,7 @@
6767

6868
// HSRAM_SIZE is defined in the ASF4 include files for each SAM_D5X_E5X chip.
6969
#define RAM_SIZE HSRAM_SIZE
70-
#define BOOTLOADER_SIZE (16*1024)
70+
#define BOOTLOADER_SIZE (16 * 1024)
7171
#define CIRCUITPY_MCU_FAMILY samd51
7272
#ifdef SAMD51
7373
#define MICROPY_PY_SYS_PLATFORM "MicroChip SAMD51"
@@ -96,7 +96,7 @@
9696
#ifdef SAMD21
9797

9898
#if INTERNAL_FLASH_FILESYSTEM
99-
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (64*1024)
99+
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (64 * 1024)
100100
#else
101101
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (0)
102102
#endif
@@ -131,7 +131,7 @@
131131
#endif
132132

133133
#ifndef CIRCUITPY_DEFAULT_STACK_SIZE
134-
#define CIRCUITPY_DEFAULT_STACK_SIZE (24*1024)
134+
#define CIRCUITPY_DEFAULT_STACK_SIZE (24 * 1024)
135135
#endif
136136

137137
#ifndef SAMD5x_E5x_BOD33_LEVEL
@@ -148,7 +148,7 @@
148148
// If CIRCUITPY is internal, use half of flash for it.
149149
#if INTERNAL_FLASH_FILESYSTEM
150150
#ifndef CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE
151-
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (FLASH_SIZE/2)
151+
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (FLASH_SIZE / 2)
152152
#endif
153153
#else
154154
#define CIRCUITPY_INTERNAL_FLASH_FILESYSTEM_SIZE (0)

ports/atmel-samd/mphalport.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ extern uint32_t common_hal_mcu_processor_get_frequency(void);
5656
// Testing done at 48 MHz on SAMD21 and 120 MHz on SAMD51, multiplication and division cancel out.
5757
// But get the frequency just in case.
5858
#ifdef SAMD21
59-
#define DELAY_LOOP_ITERATIONS_PER_US ( (10U*48000000U) / common_hal_mcu_processor_get_frequency())
59+
#define DELAY_LOOP_ITERATIONS_PER_US ((10U * 48000000U) / common_hal_mcu_processor_get_frequency())
6060
#endif
6161
#ifdef SAM_D5X_E5X
62-
#define DELAY_LOOP_ITERATIONS_PER_US ( (30U*120000000U) / common_hal_mcu_processor_get_frequency())
62+
#define DELAY_LOOP_ITERATIONS_PER_US ((30U * 120000000U) / common_hal_mcu_processor_get_frequency())
6363
#endif
6464

6565
void mp_hal_delay_us(mp_uint_t delay) {
66-
for (uint32_t i = delay*DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
67-
asm volatile("nop");
66+
for (uint32_t i = delay * DELAY_LOOP_ITERATIONS_PER_US; i > 0; i--) {
67+
asm volatile ("nop");
6868
}
6969
}
7070

ports/atmel-samd/mphalport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "supervisor/shared/tick.h"
3535

3636
// Global millisecond tick count (driven by SysTick interrupt).
37-
#define mp_hal_ticks_ms() ((mp_uint_t) supervisor_ticks_ms32())
37+
#define mp_hal_ticks_ms() ((mp_uint_t)supervisor_ticks_ms32())
3838

3939
// Number of bytes in receive buffer
4040
extern volatile uint8_t usb_rx_count;

ports/atmel-samd/timer_handler.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,31 @@ void shared_timer_handler(bool is_tc, uint8_t index) {
4949
// Make sure to add the handler #define to timer_handler.h
5050
if (is_tc) {
5151
uint8_t handler = tc_handler[index];
52-
switch(handler) {
52+
switch (handler) {
5353
case TC_HANDLER_PULSEIN:
54-
#if CIRCUITPY_PULSEIO
54+
#if CIRCUITPY_PULSEIO
5555
pulsein_timer_interrupt_handler(index);
56-
#endif
56+
#endif
5757
break;
5858
case TC_HANDLER_PULSEOUT:
59-
#if CIRCUITPY_PULSEIO
59+
#if CIRCUITPY_PULSEIO
6060
pulseout_interrupt_handler(index);
61-
#endif
61+
#endif
6262
break;
6363
case TC_HANDLER_PEW:
64-
#if CIRCUITPY_PEW
64+
#if CIRCUITPY_PEW
6565
pewpew_interrupt_handler(index);
66-
#endif
66+
#endif
6767
break;
6868
case TC_HANDLER_FREQUENCYIN:
69-
#if CIRCUITPY_FREQUENCYIO
69+
#if CIRCUITPY_FREQUENCYIO
7070
frequencyin_interrupt_handler(index);
71-
#endif
71+
#endif
7272
break;
7373
case TC_HANDLER_RGBMATRIX:
74-
#if CIRCUITPY_RGBMATRIX
74+
#if CIRCUITPY_RGBMATRIX
7575
_PM_IRQ_HANDLER();
76-
#endif
76+
#endif
7777
break;
7878
default:
7979
break;

ports/cxd56/background.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#include "supervisor/filesystem.h"
3131
#include "supervisor/shared/stack.h"
3232

33-
void port_background_task(void) {}
34-
void port_start_background_task(void) {}
35-
void port_finish_background_task(void) {}
33+
void port_background_task(void) {
34+
}
35+
void port_start_background_task(void) {
36+
}
37+
void port_finish_background_task(void) {
38+
}

tools/codeformat.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727

2828
import argparse
2929
import glob
30+
import fnmatch
3031
import itertools
3132
import os
33+
import pathlib
3234
import re
3335
import subprocess
3436

@@ -75,12 +77,13 @@
7577
PY_EXTS = (".py",)
7678

7779

78-
def list_files(paths, exclusions=None, prefix=""):
80+
def list_files(args, paths, exclusions=None, prefix=""):
7981
files = set()
82+
args = [os.path.join(prefix, arg) for arg in args]
8083
for pattern in paths:
81-
files.update(glob.glob(os.path.join(prefix, pattern), recursive=True))
84+
files.update(arg for arg in args if pathlib.Path(arg).match(pattern))
8285
for pattern in exclusions or []:
83-
files.difference_update(glob.fnmatch.filter(files, os.path.join(prefix, pattern)))
86+
files.difference_update(fnmatch.filter(files, os.path.join(prefix, pattern)))
8487
return sorted(files)
8588

8689

@@ -96,6 +99,10 @@ def fixup_c(filename):
9699
# Get next line.
97100
l = lines.pop(0)
98101

102+
# Revert "// |" back to "//| "
103+
if l.startswith("// |"):
104+
l = "//|" + l[4:]
105+
99106
# Dedent #'s to match indent of following line (not previous line).
100107
m = re.match(r"( +)#(if |ifdef |ifndef |elif |else|endif)", l)
101108
if m:
@@ -128,23 +135,19 @@ def fixup_c(filename):
128135

129136

130137
def main():
131-
cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files.")
138+
cmd_parser = argparse.ArgumentParser(description="Auto-format C and Python files -- to be used via pre-commit only.")
132139
cmd_parser.add_argument("-c", action="store_true", help="Format C code only")
133140
cmd_parser.add_argument("-p", action="store_true", help="Format Python code only")
134141
cmd_parser.add_argument("-v", action="store_true", help="Enable verbose output")
135-
cmd_parser.add_argument("files", nargs="*", help="Run on specific globs")
142+
cmd_parser.add_argument("files", nargs="+", help="Run on specific globs")
136143
args = cmd_parser.parse_args()
137144

138145
# Setting only one of -c or -p disables the other. If both or neither are set, then do both.
139146
format_c = args.c or not args.p
140147
format_py = args.p or not args.c
141148

142-
# Expand the globs passed on the command line, or use the default globs above.
143-
files = []
144-
if args.files:
145-
files = list_files(args.files)
146-
else:
147-
files = list_files(PATHS, EXCLUSIONS, TOP)
149+
# Expand the arguments passed on the command line, subject to the PATHS and EXCLUSIONS
150+
files = list_files(args.files, PATHS, EXCLUSIONS, TOP)
148151

149152
# Extract files matching a specific language.
150153
def lang_files(exts):
@@ -168,11 +171,6 @@ def batch(cmd, files, N=200):
168171
batch(command, lang_files(C_EXTS))
169172
for file in lang_files(C_EXTS):
170173
fixup_c(file)
171-
# Revert "// |" back to "//|"
172-
subprocess.call(
173-
"find shared-bindings ports/*/bindings -name '*.c' -exec sed -i 's/\/ |/\/|/' {} \;",
174-
shell=True,
175-
)
176174

177175
# Format Python files with black.
178176
if format_py:

0 commit comments

Comments
 (0)