Skip to content

Commit 50186ce

Browse files
authored
Merge pull request libgit2#4374 from pks-t/pks/pack-file-verify
Pack file verification
2 parents 8856337 + 261267e commit 50186ce

File tree

18 files changed

+529
-150
lines changed

18 files changed

+529
-150
lines changed

examples/network/index-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int index_pack(git_repository *repo, int argc, char **argv)
4848
return EXIT_FAILURE;
4949
}
5050

51-
if (git_indexer_new(&idx, ".", 0, NULL, NULL, NULL) < 0) {
51+
if (git_indexer_new(&idx, ".", 0, NULL, NULL) < 0) {
5252
puts("bad idx");
5353
return -1;
5454
}

include/git2/indexer.h

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@ GIT_BEGIN_DECL
1515

1616
typedef struct git_indexer git_indexer;
1717

18+
typedef struct git_indexer_options {
19+
unsigned int version;
20+
21+
/** progress_cb function to call with progress information */
22+
git_transfer_progress_cb progress_cb;
23+
/** progress_cb_payload payload for the progress callback */
24+
void *progress_cb_payload;
25+
26+
/** Do connectivity checks for the received pack */
27+
unsigned char verify;
28+
} git_indexer_options;
29+
30+
#define GIT_INDEXER_OPTIONS_VERSION 1
31+
#define GIT_INDEXER_OPTIONS_INIT { GIT_INDEXER_OPTIONS_VERSION }
32+
33+
/**
34+
* Initializes a `git_indexer_options` with default values. Equivalent to
35+
* creating an instance with GIT_INDEXER_OPTIONS_INIT.
36+
*
37+
* @param opts the `git_indexer_options` struct to initialize.
38+
* @param version Version of struct; pass `GIT_INDEXER_OPTIONS_VERSION`
39+
* @return Zero on success; -1 on failure.
40+
*/
41+
GIT_EXTERN(int) git_indexer_init_options(
42+
git_indexer_options *opts,
43+
unsigned int version);
44+
1845
/**
1946
* Create a new indexer instance
2047
*
@@ -24,16 +51,15 @@ typedef struct git_indexer git_indexer;
2451
* @param odb object database from which to read base objects when
2552
* fixing thin packs. Pass NULL if no thin pack is expected (an error
2653
* will be returned if there are bases missing)
27-
* @param progress_cb function to call with progress information
28-
* @param progress_cb_payload payload for the progress callback
54+
* @param opts Optional structure containing additional options. See
55+
* `git_indexer_options` above.
2956
*/
3057
GIT_EXTERN(int) git_indexer_new(
3158
git_indexer **out,
3259
const char *path,
3360
unsigned int mode,
3461
git_odb *odb,
35-
git_transfer_progress_cb progress_cb,
36-
void *progress_cb_payload);
62+
git_indexer_options *opts);
3763

3864
/**
3965
* Add data to the indexer

src/blob.c

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,54 @@
1919
const void *git_blob_rawcontent(const git_blob *blob)
2020
{
2121
assert(blob);
22-
return git_odb_object_data(blob->odb_object);
22+
if (blob->raw)
23+
return blob->data.raw.data;
24+
else
25+
return git_odb_object_data(blob->data.odb);
2326
}
2427

2528
git_off_t git_blob_rawsize(const git_blob *blob)
2629
{
2730
assert(blob);
28-
return (git_off_t)git_odb_object_size(blob->odb_object);
31+
if (blob->raw)
32+
return blob->data.raw.size;
33+
else
34+
return (git_off_t)git_odb_object_size(blob->data.odb);
2935
}
3036

3137
int git_blob__getbuf(git_buf *buffer, git_blob *blob)
3238
{
3339
return git_buf_set(
3440
buffer,
35-
git_odb_object_data(blob->odb_object),
36-
git_odb_object_size(blob->odb_object));
41+
git_blob_rawcontent(blob),
42+
git_blob_rawsize(blob));
3743
}
3844

39-
void git_blob__free(void *blob)
45+
void git_blob__free(void *_blob)
4046
{
41-
git_odb_object_free(((git_blob *)blob)->odb_object);
47+
git_blob *blob = (git_blob *) _blob;
48+
if (!blob->raw)
49+
git_odb_object_free(blob->data.odb);
4250
git__free(blob);
4351
}
4452

45-
int git_blob__parse(void *blob, git_odb_object *odb_obj)
53+
int git_blob__parse_raw(void *_blob, const char *data, size_t size)
4654
{
55+
git_blob *blob = (git_blob *) _blob;
56+
assert(blob);
57+
blob->raw = 1;
58+
blob->data.raw.data = data;
59+
blob->data.raw.size = size;
60+
return 0;
61+
}
62+
63+
int git_blob__parse(void *_blob, git_odb_object *odb_obj)
64+
{
65+
git_blob *blob = (git_blob *) _blob;
4766
assert(blob);
4867
git_cached_obj_incref((git_cached_obj *)odb_obj);
49-
((git_blob *)blob)->odb_object = odb_obj;
68+
blob->raw = 0;
69+
blob->data.odb = odb_obj;
5070
return 0;
5171
}
5272

@@ -372,8 +392,8 @@ int git_blob_is_binary(const git_blob *blob)
372392

373393
assert(blob);
374394

375-
git_buf_attach_notowned(&content, blob->odb_object->buffer,
376-
min(blob->odb_object->cached.size,
395+
git_buf_attach_notowned(&content, git_blob_rawcontent(blob),
396+
min(git_blob_rawsize(blob),
377397
GIT_FILTER_BYTES_TO_CHECK_NUL));
378398
return git_buf_text_is_binary(&content);
379399
}

src/blob.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@
1616

1717
struct git_blob {
1818
git_object object;
19-
git_odb_object *odb_object;
19+
20+
union {
21+
git_odb_object *odb;
22+
struct {
23+
const char *data;
24+
git_off_t size;
25+
} raw;
26+
} data;
27+
unsigned int raw:1;
2028
};
2129

2230
void git_blob__free(void *blob);
2331
int git_blob__parse(void *blob, git_odb_object *obj);
32+
int git_blob__parse_raw(void *blob, const char *data, size_t size);
2433
int git_blob__getbuf(git_buf *buffer, git_blob *blob);
2534

2635
extern int git_blob__create_from_paths(

src/commit.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,11 @@ int git_commit_amend(
383383
return error;
384384
}
385385

386-
int git_commit__parse(void *_commit, git_odb_object *odb_obj)
386+
int git_commit__parse_raw(void *_commit, const char *data, size_t size)
387387
{
388388
git_commit *commit = _commit;
389-
const char *buffer_start = git_odb_object_data(odb_obj), *buffer;
390-
const char *buffer_end = buffer_start + git_odb_object_size(odb_obj);
389+
const char *buffer_start = data, *buffer;
390+
const char *buffer_end = buffer_start + size;
391391
git_oid parent_id;
392392
size_t header_len;
393393
git_signature dummy_sig;
@@ -477,6 +477,13 @@ int git_commit__parse(void *_commit, git_odb_object *odb_obj)
477477
return -1;
478478
}
479479

480+
int git_commit__parse(void *_commit, git_odb_object *odb_obj)
481+
{
482+
return git_commit__parse_raw(_commit,
483+
git_odb_object_data(odb_obj),
484+
git_odb_object_size(odb_obj));
485+
}
486+
480487
#define GIT_COMMIT_GETTER(_rvalue, _name, _return) \
481488
_rvalue git_commit_##_name(const git_commit *commit) \
482489
{\

src/commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@ struct git_commit {
3535

3636
void git_commit__free(void *commit);
3737
int git_commit__parse(void *commit, git_odb_object *obj);
38+
int git_commit__parse_raw(void *commit, const char *data, size_t size);
3839

3940
#endif

0 commit comments

Comments
 (0)