review

review patchsets using your default editor
Log | Files | Refs

formatting.odin (1429B)


      1 package check
      2 
      3 // A commit that reformats and changes logic in one diff hides the logic
      4 // among the reformatting, and neither can be reverted alone. Git can count
      5 // which changed lines change only whitespace, and the count is the check.
      6 
      7 import "core:fmt"
      8 
      9 import "../finding"
     10 
     11 // formatting_share is the share of a change's lines that change only
     12 // whitespace at which the change is a reformatting with logic mixed in;
     13 // formatting_floor the fewest whitespace-only lines worth a word;
     14 // logic_floor the fewest lines that change something else.
     15 formatting_share :: 0.5
     16 formatting_floor :: 20
     17 logic_floor      :: 10
     18 
     19 // check_formatting reports a change whose diff is mostly whitespace and
     20 // yet carries logic too: two changes that should be two commits.
     21 check_formatting :: proc(s: Scope, out: ^[dynamic]finding.Finding) {
     22 	c := s.c
     23 	if c.changed == 0 || c.whitespace < formatting_floor {
     24 		return
     25 	}
     26 	logic := c.changed - c.whitespace
     27 	if logic < logic_floor {
     28 		return
     29 	}
     30 	if f64(c.whitespace) / f64(c.changed) < formatting_share {
     31 		return
     32 	}
     33 	append(
     34 		out,
     35 		static(
     36 			"formatting-mixed-in",
     37 			.Consider,
     38 			fmt.aprintf(
     39 				"%d of the change's %d lines change only whitespace, and %d change something else; a reformatting with logic in it hides the logic, and neither half can be reverted alone",
     40 				c.whitespace,
     41 				c.changed,
     42 				logic,
     43 			),
     44 			"commit the reformatting on its own, then the change",
     45 		),
     46 	)
     47 }