soypat / gitaligned

Find out where you fall on the Open-Source Character Alignment Chart

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

gitaligned always crashes

MrJoy opened this issue · comments

panic: runtime error: index out of range [200] with length 200

goroutine 1 [running]:
main.walkCommits(0xc0001c6000, 0xc8, 0xc8, 0x11c5a70, 0x11b793e, 0x3)
	/home/pato/src/gitaligned/nlp.go:36 +0x5b0
main.SetAuthorAlignments(0xc0001c6000, 0xc8, 0xc8, 0xc0001ca000, 0x2, 0x14, 0x0, 0x0)
	/home/pato/src/gitaligned/align.go:51 +0x4f
main.run(0xc00003e748, 0x1724320)
	/home/pato/src/gitaligned/main.go:43 +0x2c8
main.main()
	/home/pato/src/gitaligned/main.go:48 +0x26

I get this whether I run without parameters, or with specifying a username, or -k or -y. I've tried it against two different repos, and it dies with exactly the same error, down to the length/index values. I tried it on a third repo and got a similar result but the index/length is 55 instead. And so on.

I've tried it on a bunch of repos and I've only gotten it to run on a handful.

I was able to get things working by creating a dummy repo with a couple empty commits:

git init .
git commit --allow-empty -m "Initial commit."
git commit --allow-empty -m "Hello, world."

I also got it running against a relatively trivial repo (19 commits, 1 author, no branches/tags), and a couple other repos that are mostly forks. For some reason though, any of the repos I'm actually primary contributor on it seems to really not like.

I was able to make it not crash with the following patch:

diff --git a/nlp.go b/nlp.go
index d05df56..acab348 100644
--- a/nlp.go
+++ b/nlp.go
@@ -30,9 +43,9 @@ func walkCommits(commits []commit, f func(*commit, []prose.Token)) error {
 	}
 	tokens := doc.Tokens()
 	atCommit := 0
-	last := 0
+	last := -1
 	for i := range tokens {
-		if tokens[i].Tag == "." {
+		if tokens[i].Text == "." {
 			f(&commits[atCommit], tokens[last+1:i])
 			last = i
 			atCommit++

last being off by one isn't related to the panic, I just noticed it wouldn't start at token zero otherwise.

The real fix was checking Text instead of Tag. If there were any other sentence enders in the commit message (like !) then they get tagged as . and the atCommit counter moves ahead too quickly, eventually overrunning the commits array.

Even with this change I noticed another error that I didn't know how to fix: if the commit ends with a single-quoted character (my specific commit ended like: make minutes 'm' and months 'M') then the prose library didn't count the synthetic . that gets added to the end of each commit message. This caused commits to "slip" and be off by one from then on. This error doesn't panic though, since it errs in the other direction.

Thank you for reporting this. I managed to reproduce the error on several repos. I'm looking into it.

@caldwell @MrJoy I've pushed an attempt at a patch today. I don't think I've covered all the cases. The problem seems to be that the tokens index was falling behind the commits index. If you can try it out with the same repositories. Thanks again.

I just tested it against all 78 repos on my laptop, and no more crashes!