@@ -57,27 +57,24 @@ wrap_diff(git_diff *diff, Repository *repo)
5757 return (PyObject * ) py_diff ;
5858}
5959
60- PyObject *
61- diff_get_patch_byindex ( git_diff * diff , size_t idx )
60+ PyObject *
61+ wrap_patch ( git_patch * patch )
6262{
63- const git_diff_delta * delta ;
64- const git_diff_hunk * hunk ;
65- const git_diff_line * line ;
66- git_patch * patch = NULL ;
67- size_t i , j , hunk_amounts , lines_in_hunk , additions , deletions ;
68- int err ;
69- Hunk * py_hunk = NULL ;
70- Patch * py_patch = NULL ;
71- PyObject * py_line_origin = NULL , * py_line = NULL ;
72-
73- err = git_patch_from_diff (& patch , diff , idx );
74- if (err < 0 )
75- return Error_set (err );
63+ Patch * py_patch ;
7664
77- delta = git_patch_get_delta (patch );
65+ if (!patch )
66+ Py_RETURN_NONE ;
7867
7968 py_patch = PyObject_New (Patch , & PatchType );
80- if (py_patch != NULL ) {
69+ if (py_patch ) {
70+ size_t i , j , hunk_amounts , lines_in_hunk , additions , deletions ;
71+ const git_diff_delta * delta ;
72+ const git_diff_hunk * hunk ;
73+ const git_diff_line * line ;
74+ int err ;
75+
76+ delta = git_patch_get_delta (patch );
77+
8178 py_patch -> old_file_path = delta -> old_file .path ;
8279 py_patch -> new_file_path = delta -> new_file .path ;
8380 py_patch -> status = git_diff_status_char (delta -> status );
@@ -92,11 +89,12 @@ diff_get_patch_byindex(git_diff* diff, size_t idx)
9289
9390 hunk_amounts = git_patch_num_hunks (patch );
9491 py_patch -> hunks = PyList_New (hunk_amounts );
95- for (i = 0 ; i < hunk_amounts ; ++ i ) {
96- err = git_patch_get_hunk ( & hunk , & lines_in_hunk , patch , i ) ;
92+ for (i = 0 ; i < hunk_amounts ; ++ i ) {
93+ Hunk * py_hunk = NULL ;
9794
95+ err = git_patch_get_hunk (& hunk , & lines_in_hunk , patch , i );
9896 if (err < 0 )
99- goto cleanup ;
97+ return Error_set ( err ) ;
10098
10199 py_hunk = PyObject_New (Hunk , & HunkType );
102100 if (py_hunk != NULL ) {
@@ -106,20 +104,20 @@ diff_get_patch_byindex(git_diff* diff, size_t idx)
106104 py_hunk -> new_lines = hunk -> new_lines ;
107105
108106 py_hunk -> lines = PyList_New (lines_in_hunk );
109- for (j = 0 ; j < lines_in_hunk ; ++ j ) {
110- err = git_patch_get_line_in_hunk ( & line , patch , i , j ) ;
107+ for (j = 0 ; j < lines_in_hunk ; ++ j ) {
108+ PyObject * py_line_origin = NULL , * py_line = NULL ;
111109
110+ err = git_patch_get_line_in_hunk (& line , patch , i , j );
112111 if (err < 0 )
113- goto cleanup ;
112+ return Error_set ( err ) ;
114113
115- py_line_origin = to_unicode_n (& line -> origin , 1 , NULL , NULL );
116- py_line = to_unicode_n (line -> content , line -> content_len , NULL , NULL );
114+ py_line_origin = to_unicode_n (& line -> origin , 1 ,
115+ NULL , NULL );
116+ py_line = to_unicode_n (line -> content , line -> content_len ,
117+ NULL , NULL );
117118 PyList_SetItem (py_hunk -> lines , j ,
118- Py_BuildValue ("OO" ,
119- py_line_origin ,
120- py_line
121- )
122- );
119+ Py_BuildValue ("OO" , py_line_origin , py_line ));
120+
123121 Py_DECREF (py_line_origin );
124122 Py_DECREF (py_line );
125123 }
@@ -130,10 +128,20 @@ diff_get_patch_byindex(git_diff* diff, size_t idx)
130128 }
131129 }
132130
133- cleanup :
134- git_patch_free (patch );
131+ return (PyObject * ) py_patch ;
132+ }
133+
134+ PyObject *
135+ diff_get_patch_byindex (git_diff * diff , size_t idx )
136+ {
137+ git_patch * patch = NULL ;
138+ int err ;
139+
140+ err = git_patch_from_diff (& patch , diff , idx );
141+ if (err < 0 )
142+ return Error_set (err );
135143
136- return (err < 0 ) ? Error_set ( err ) : ( PyObject * ) py_patch ;
144+ return (PyObject * ) wrap_patch ( patch ) ;
137145}
138146
139147static void
0 commit comments