Skip to content

Commit 1605777

Browse files
committed
Initialize pointers, use payload structures
1 parent 316f89d commit 1605777

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

docs/guides/101-samples/index.md

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (error < 0) {
2626
Anytime libgit2 fills in a non-`const` pointer for you, you should be using a `_free` call to release the resource.
2727

2828
```c
29-
git_repository *repo;
29+
git_repository *repo = NULL;
3030
git_repository_init(&repo, "/tmp/…", false);
3131
/**/
3232
git_repository_free(repo);
@@ -40,7 +40,7 @@ git_repository_free(repo);
4040
### Init (Simple)
4141
4242
```c
43-
git_repository *repo;
43+
git_repository *repo = NULL;
4444
/* With working directory: */
4545
int error = git_repository_init(&repo, "/tmp/…", false);
4646
/* …or bare: */
@@ -54,7 +54,7 @@ int error = git_repository_init(&repo, "/tmp/…", true);
5454
```c
5555
git_repository_init_options opts = GIT_REPOSITORY_INIT_OPTIONS_INIT;
5656
/* Customize options */
57-
git_repository *repo;
57+
git_repository *repo = NULL;
5858
int error = git_repository_init_ext(&repo, "/tmp/…", &opts);
5959
```
6060

@@ -64,7 +64,7 @@ int error = git_repository_init_ext(&repo, "/tmp/…", &opts);
6464
### Clone (Simple)
6565

6666
```c
67-
git_repository *repo;
67+
git_repository *repo = NULL;
6868
const char *url = "http://…";
6969
const char *path = "/tmp/…";
7070
int error = git_clone(&repo, url, path, NULL);
@@ -102,6 +102,7 @@ checkout_opts.progress_cb = checkout_progress;
102102
clone_opts.checkout_opts = checkout_opts;
103103
clone_opts.remote_callbacks.transfer_progress = &fetch_progress;
104104

105+
git_repository *repo = NULL;
105106
int error = git_clone(&repo, url, path, &opts);
106107
```
107108
@@ -112,11 +113,11 @@ int error = git_clone(&repo, url, path, &opts);
112113
### Clone (Repo)
113114
114115
```c
115-
git_repository *repo;
116+
git_repository *repo = NULL;
116117
error = git_repository_init(&repo, "/tmp/…", false);
117118
/* Customize the repo */
118119
119-
git_remote *origin;
120+
git_remote *origin = NULL;
120121
error = git_remote_create(&origin, repo, "origin", "http://…");
121122
/* Customize the remote, set callbacks, etc. */
122123
@@ -130,7 +131,7 @@ error = git_clone_into(repo, origin, &co_opts, "master");
130131
### Opening (Simple)
131132

132133
```c
133-
git_repository *repo;
134+
git_repository *repo = NULL;
134135
int error = git_repository_open(&repo, "/tmp/…");
135136
```
136137

@@ -139,7 +140,7 @@ int error = git_repository_open(&repo, "/tmp/…");
139140
### Opening (Options)
140141

141142
```c
142-
git_repository *repo;
143+
git_repository *repo = NULL;
143144
int error = git_repository_open_ext(&repo, "/tmp/…",
144145
GIT_REPOSITORY_OPEN_NO_SEARCH, NULL);
145146
```
@@ -156,7 +157,7 @@ int error = git_repository_open_ext(&repo, "/tmp/…",
156157
Like `git diff`.
157158

158159
```c
159-
git_diff *diff;
160+
git_diff *diff = NULL;
160161
int error = git_diff_index_to_workdir(&diff, repo, NULL, NULL);
161162
```
162163

@@ -167,13 +168,13 @@ int error = git_diff_index_to_workdir(&diff, repo, NULL, NULL);
167168
Like `git diff --cached`.
168169

169170
```c
170-
git_object *obj;
171+
git_object *obj = NULL;
171172
int error = git_revparse_single(&obj, repo, "HEAD^{tree}");
172173

173-
git_tree *tree;
174+
git_tree *tree = NULL;
174175
error = git_tree_lookup(&tree, repo, git_object_id(obj));
175176

176-
git_diff *diff;
177+
git_diff *diff = NULL;
177178
error = git_diff_tree_to_index(&diff, repo, tree, NULL, NULL);
178179
```
179180

@@ -186,13 +187,13 @@ error = git_diff_tree_to_index(&diff, repo, tree, NULL, NULL);
186187
Like `git diff HEAD`.
187188

188189
```c
189-
git_object *obj;
190+
git_object *obj = NULL;
190191
int error = git_revparse_single(&obj, repo, "HEAD^{tree}");
191192

192-
git_tree *tree;
193+
git_tree *tree = NULL;
193194
error = git_tree_lookup(&tree, repo, git_object_id(obj));
194195

195-
git_diff *diff;
196+
git_diff *diff = NULL;
196197
error = git_diff_tree_to_workdir_with_index(&diff, repo, tree, NULL);
197198
```
198199

@@ -205,20 +206,20 @@ error = git_diff_tree_to_workdir_with_index(&diff, repo, tree, NULL);
205206
Like `git show <commit>`.
206207

207208
```c
208-
git_object *obj;
209+
git_object *obj = NULL;
209210
int error = git_revparse_single(&obj, repo, "committish");
210211

211-
git_commit *commit;
212+
git_commit *commit = NULL;
212213
error = git_commit_lookup(&commit, repo, git_object_id(obj));
213214

214-
git_commit *parent;
215+
git_commit *parent = NULL;
215216
error = git_commit_parent(&parent, commit, 0);
216217

217-
git_tree *commit_tree, *parent_tree;
218+
git_tree *commit_tree = NULL, *parent_tree = NULL;
218219
error = git_commit_tree(&commit_tree, commit);
219220
error = git_commit_tree(&parent_tree, parent);
220221

221-
git_diff *diff;
222+
git_diff *diff = NULL;
222223
error = git_diff_tree_to_tree(
223224
&diff, repo, commit_tree, parent_tree, NULL);
224225
```
@@ -251,13 +252,15 @@ int each_file_cb(const git_diff_delta *delta,
251252
float progress,
252253
void *payload)
253254
{
255+
diff_data *d = (diff_data*)payload;
254256
/**/
255257
}
256258

257259
int each_hunk_cb(const git_diff_delta *delta,
258260
const git_diff_hunk *hunk,
259261
void *payload)
260262
{
263+
diff_data *d = (diff_data*)payload;
261264
/**/
262265
}
263266

@@ -266,14 +269,16 @@ int each_line_cb(const git_diff_delta *delta,
266269
const git_diff_line *line,
267270
void *payload)
268271
{
272+
diff_data *d = (diff_data*)payload;
269273
/**/
270274
}
271275

276+
diff_data d = {0};
272277
int error = git_diff_foreach(diff,
273278
each_file_cb,
274279
each_hunk_cb,
275280
each_line_cb,
276-
NULL);
281+
&d);
277282
```
278283
279284
([`git_diff_foreach`](http://libgit2.github.com/libgit2/#HEAD/group/diff/git_diff_foreach),

0 commit comments

Comments
 (0)