@@ -26,7 +26,7 @@ if (error < 0) {
26
26
Anytime libgit2 fills in a non-` const ` pointer for you, you should be using a ` _free ` call to release the resource.
27
27
28
28
``` c
29
- git_repository *repo;
29
+ git_repository *repo = NULL ;
30
30
git_repository_init (&repo, "/tmp/…", false);
31
31
/* … * /
32
32
git_repository_free(repo);
@@ -40,7 +40,7 @@ git_repository_free(repo);
40
40
### Init (Simple)
41
41
42
42
```c
43
- git_repository *repo;
43
+ git_repository *repo = NULL ;
44
44
/* With working directory: */
45
45
int error = git_repository_init(&repo, "/tmp/…", false);
46
46
/* …or bare: */
@@ -54,7 +54,7 @@ int error = git_repository_init(&repo, "/tmp/…", true);
54
54
``` c
55
55
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
56
56
/* Customize options */
57
- git_repository *repo;
57
+ git_repository *repo = NULL ;
58
58
int error = git_repository_init_ext(&repo, " /tmp/…" , &opts);
59
59
```
60
60
@@ -64,7 +64,7 @@ int error = git_repository_init_ext(&repo, "/tmp/…", &opts);
64
64
### Clone (Simple)
65
65
66
66
``` c
67
- git_repository *repo;
67
+ git_repository *repo = NULL ;
68
68
const char *url = " http://…" ;
69
69
const char *path = " /tmp/…" ;
70
70
int error = git_clone(&repo, url, path, NULL );
@@ -102,6 +102,7 @@ checkout_opts.progress_cb = checkout_progress;
102
102
clone_opts.checkout_opts = checkout_opts;
103
103
clone_opts.remote_callbacks.transfer_progress = &fetch_progress;
104
104
105
+ git_repository * repo = NULL;
105
106
int error = git_clone(&repo, url, path, &opts);
106
107
```
107
108
@@ -112,11 +113,11 @@ int error = git_clone(&repo, url, path, &opts);
112
113
### Clone (Repo)
113
114
114
115
```c
115
- git_repository *repo;
116
+ git_repository *repo = NULL ;
116
117
error = git_repository_init(&repo, "/tmp/…", false);
117
118
/* Customize the repo */
118
119
119
- git_remote *origin;
120
+ git_remote *origin = NULL ;
120
121
error = git_remote_create(&origin, repo, "origin", "http://…");
121
122
/* Customize the remote, set callbacks, etc. */
122
123
@@ -130,7 +131,7 @@ error = git_clone_into(repo, origin, &co_opts, "master");
130
131
### Opening (Simple)
131
132
132
133
``` c
133
- git_repository *repo;
134
+ git_repository *repo = NULL ;
134
135
int error = git_repository_open(&repo, " /tmp/…" );
135
136
```
136
137
@@ -139,7 +140,7 @@ int error = git_repository_open(&repo, "/tmp/…");
139
140
### Opening (Options)
140
141
141
142
``` c
142
- git_repository *repo;
143
+ git_repository *repo = NULL ;
143
144
int error = git_repository_open_ext(&repo, " /tmp/…" ,
144
145
GIT_REPOSITORY_OPEN_NO_SEARCH, NULL );
145
146
```
@@ -156,7 +157,7 @@ int error = git_repository_open_ext(&repo, "/tmp/…",
156
157
Like ` git diff ` .
157
158
158
159
``` c
159
- git_diff *diff;
160
+ git_diff *diff = NULL ;
160
161
int error = git_diff_index_to_workdir(&diff, repo, NULL , NULL );
161
162
```
162
163
@@ -167,13 +168,13 @@ int error = git_diff_index_to_workdir(&diff, repo, NULL, NULL);
167
168
Like ` git diff --cached ` .
168
169
169
170
``` c
170
- git_object *obj;
171
+ git_object *obj = NULL ;
171
172
int error = git_revparse_single(&obj, repo, " HEAD^{tree}" );
172
173
173
- git_tree *tree;
174
+ git_tree *tree = NULL ;
174
175
error = git_tree_lookup(&tree, repo, git_object_id(obj));
175
176
176
- git_diff *diff;
177
+ git_diff *diff = NULL ;
177
178
error = git_diff_tree_to_index(&diff, repo, tree, NULL , NULL );
178
179
```
179
180
@@ -186,13 +187,13 @@ error = git_diff_tree_to_index(&diff, repo, tree, NULL, NULL);
186
187
Like ` git diff HEAD ` .
187
188
188
189
``` c
189
- git_object *obj;
190
+ git_object *obj = NULL ;
190
191
int error = git_revparse_single(&obj, repo, " HEAD^{tree}" );
191
192
192
- git_tree *tree;
193
+ git_tree *tree = NULL ;
193
194
error = git_tree_lookup(&tree, repo, git_object_id(obj));
194
195
195
- git_diff *diff;
196
+ git_diff *diff = NULL ;
196
197
error = git_diff_tree_to_workdir_with_index(&diff, repo, tree, NULL );
197
198
```
198
199
@@ -205,20 +206,20 @@ error = git_diff_tree_to_workdir_with_index(&diff, repo, tree, NULL);
205
206
Like ` git show <commit> ` .
206
207
207
208
``` c
208
- git_object *obj;
209
+ git_object *obj = NULL ;
209
210
int error = git_revparse_single(&obj, repo, " committish" );
210
211
211
- git_commit *commit;
212
+ git_commit *commit = NULL ;
212
213
error = git_commit_lookup(&commit, repo, git_object_id(obj));
213
214
214
- git_commit *parent;
215
+ git_commit *parent = NULL ;
215
216
error = git_commit_parent(&parent, commit, 0 );
216
217
217
- git_tree *commit_tree, *parent_tree;
218
+ git_tree *commit_tree = NULL , *parent_tree = NULL ;
218
219
error = git_commit_tree(&commit_tree, commit);
219
220
error = git_commit_tree(&parent_tree, parent);
220
221
221
- git_diff *diff;
222
+ git_diff *diff = NULL ;
222
223
error = git_diff_tree_to_tree(
223
224
&diff, repo, commit_tree, parent_tree, NULL );
224
225
```
@@ -251,13 +252,15 @@ int each_file_cb(const git_diff_delta *delta,
251
252
float progress,
252
253
void * payload)
253
254
{
255
+ diff_data * d = (diff_data* )payload;
254
256
/* … * /
255
257
}
256
258
257
259
int each_hunk_cb(const git_diff_delta * delta,
258
260
const git_diff_hunk * hunk,
259
261
void * payload)
260
262
{
263
+ diff_data * d = (diff_data* )payload;
261
264
/* … * /
262
265
}
263
266
@@ -266,14 +269,16 @@ int each_line_cb(const git_diff_delta *delta,
266
269
const git_diff_line * line,
267
270
void * payload)
268
271
{
272
+ diff_data * d = (diff_data* )payload;
269
273
/* … * /
270
274
}
271
275
276
+ diff_data d = {0};
272
277
int error = git_diff_foreach(diff,
273
278
each_file_cb,
274
279
each_hunk_cb,
275
280
each_line_cb,
276
- NULL );
281
+ &d );
277
282
```
278
283
279
284
([`git_diff_foreach`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_foreach),
0 commit comments