Skip to content

Don't be fooled by strings.Split(..., "\n") result always having empty "" last element

Kirill Smelkov requested to merge splitlines into master

By definition of strings.Split(..., sep) it "slices s into all substrings separated by sep and returns a slice of the substrings between those separators". That means that

string.Split("hello\nworld\n", "\n") -> ["hello", "world", ""])     # NOTE the last ""

when parsing file by lines, it is handy though to do not get last empty "" after last "\n". #6 (closed) shows how we missed to do that filtering-out for case of empty backup.refs file and errored-out because of that.

To fix let's introduce a helper - splitlines(), which does the job of filtering-out last empty entry after last separator. By using this helper everywhere we can hopefully avoid problems while pulling only empty repositories (#6 (closed) case), and also similar ones.

Fixes #6 (closed) /reported-by @iv

Merge request reports