@@ -144,10 +144,9 @@ git_blame *git_blame__alloc(
144144 gbr -> options = opts ;
145145
146146 if (git_vector_init (& gbr -> hunks , 8 , hunk_cmp ) < 0 ||
147- git_vector_init (& gbr -> paths , 8 , paths_cmp ) < 0 ||
148- (gbr -> path = git__strdup (path )) == NULL ||
149- git_vector_insert (& gbr -> paths , git__strdup (path )) < 0 )
150- {
147+ git_vector_init (& gbr -> paths , 8 , paths_cmp ) < 0 ||
148+ (gbr -> path = git__strdup (path )) == NULL ||
149+ git_vector_insert (& gbr -> paths , git__strdup (path )) < 0 ) {
151150 git_blame_free (gbr );
152151 return NULL ;
153152 }
@@ -170,7 +169,9 @@ void git_blame_free(git_blame *blame)
170169
171170 git_vector_foreach (& blame -> hunks , i , hunk )
172171 free_hunk (hunk );
172+
173173 git_vector_dispose (& blame -> hunks );
174+ git_array_clear (blame -> lines );
174175
175176 git_vector_dispose_deep (& blame -> paths );
176177
@@ -190,6 +191,23 @@ size_t git_blame_hunkcount(git_blame *blame)
190191 return blame -> hunks .length ;
191192}
192193
194+ size_t git_blame_linecount (git_blame * blame )
195+ {
196+ GIT_ASSERT_ARG (blame );
197+
198+ return git_array_size (blame -> line_index );
199+ }
200+
201+ const git_blame_line * git_blame_line_byindex (
202+ git_blame * blame ,
203+ size_t idx )
204+ {
205+ GIT_ASSERT_ARG_WITH_RETVAL (blame , NULL );
206+ GIT_ASSERT_WITH_RETVAL (idx > 0 && idx <= git_array_size (blame -> line_index ), NULL );
207+
208+ return git_array_get (blame -> lines , idx - 1 );
209+ }
210+
193211const git_blame_hunk * git_blame_hunk_byindex (
194212 git_blame * blame ,
195213 size_t index )
@@ -315,25 +333,48 @@ static int index_blob_lines(git_blame *blame)
315333 const char * buf = blame -> final_buf ;
316334 size_t len = blame -> final_buf_size ;
317335 int num = 0 , incomplete = 0 , bol = 1 ;
336+ git_blame_line * line = NULL ;
318337 size_t * i ;
319338
320339 if (len && buf [len - 1 ] != '\n' )
321340 incomplete ++ ; /* incomplete line at the end */
341+
322342 while (len -- ) {
323343 if (bol ) {
324344 i = git_array_alloc (blame -> line_index );
325345 GIT_ERROR_CHECK_ALLOC (i );
326346 * i = buf - blame -> final_buf ;
347+
348+ GIT_ASSERT (line == NULL );
349+ line = git_array_alloc (blame -> lines );
350+ GIT_ERROR_CHECK_ALLOC (line );
351+
352+ line -> ptr = buf ;
327353 bol = 0 ;
328354 }
355+
329356 if (* buf ++ == '\n' ) {
357+ GIT_ASSERT (line );
358+ line -> len = (buf - line -> ptr ) - 1 ;
359+ line = NULL ;
360+
330361 num ++ ;
331362 bol = 1 ;
332363 }
333364 }
365+
334366 i = git_array_alloc (blame -> line_index );
335367 GIT_ERROR_CHECK_ALLOC (i );
336368 * i = buf - blame -> final_buf ;
369+
370+ if (!bol ) {
371+ GIT_ASSERT (line );
372+ line -> len = buf - line -> ptr ;
373+ line = NULL ;
374+ }
375+
376+ GIT_ASSERT (!line );
377+
337378 blame -> num_lines = num + incomplete ;
338379 return blame -> num_lines ;
339380}
0 commit comments