Skip to content

Commit 8a62616

Browse files
authored
Merge pull request libgit2#6549 from libgit2/ethomson/sha256_experimental
sha256: less hardcoded SHA1 types and lengths
2 parents abb0b31 + b899fda commit 8a62616

File tree

173 files changed

+2589
-657
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+2589
-657
lines changed

examples/diff.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,17 @@ static void compute_diff_no_index(git_diff **diff, struct diff_options *o) {
188188
check_lg2(
189189
git_patch_to_buf(&buf, patch),
190190
"patch to buf", NULL);
191+
192+
#ifdef GIT_EXPERIMENTAL_SHA256
193+
check_lg2(
194+
git_diff_from_buffer(diff, buf.ptr, buf.size, NULL),
195+
"diff from patch", NULL);
196+
#else
191197
check_lg2(
192198
git_diff_from_buffer(diff, buf.ptr, buf.size),
193199
"diff from patch", NULL);
200+
#endif
201+
194202
git_patch_free(patch);
195203
git_buf_dispose(&buf);
196204
free(file1_str);

examples/show-index.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ int lg2_show_index(git_repository *repo, int argc, char **argv)
3030

3131
dirlen = strlen(dir);
3232
if (dirlen > 5 && strcmp(dir + dirlen - 5, "index") == 0) {
33+
#ifdef GIT_EXPERIMENTAL_SHA256
34+
check_lg2(git_index_open(&index, dir, GIT_OID_SHA1), "could not open index", dir);
35+
#else
3336
check_lg2(git_index_open(&index, dir), "could not open index", dir);
37+
#endif
3438
} else {
3539
check_lg2(git_repository_open_ext(&repo, dir, 0, NULL), "could not open repository", dir);
3640
check_lg2(git_repository_index(&index, repo), "could not open repository index", NULL);

include/git2/diff.h

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,22 @@ typedef struct {
421421
*/
422422
uint32_t interhunk_lines;
423423

424+
/**
425+
* The object ID type to emit in diffs; this is used by functions
426+
* that operate without a repository - namely `git_diff_buffers`,
427+
* or `git_diff_blobs` and `git_diff_blob_to_buffer` when one blob
428+
* is `NULL`.
429+
*
430+
* This may be omitted (set to `0`). If a repository is available,
431+
* the object ID format of the repository will be used. If no
432+
* repository is available then the default is `GIT_OID_SHA`.
433+
*
434+
* If this is specified and a repository is available, then the
435+
* specified `oid_type` must match the repository's object ID
436+
* format.
437+
*/
438+
git_oid_t oid_type;
439+
424440
/**
425441
* The abbreviation length to use when formatting object ids.
426442
* Defaults to the value of 'core.abbrev' from the config, or 7 if unset.
@@ -1153,9 +1169,8 @@ GIT_EXTERN(int) git_diff_to_buf(
11531169

11541170
/**@}*/
11551171

1156-
11571172
/*
1158-
* Misc
1173+
* Low-level file comparison, invoking callbacks per difference.
11591174
*/
11601175

11611176
/**
@@ -1271,6 +1286,25 @@ GIT_EXTERN(int) git_diff_buffers(
12711286
git_diff_line_cb line_cb,
12721287
void *payload);
12731288

1289+
/* Patch file parsing. */
1290+
1291+
/**
1292+
* Options for parsing a diff / patch file.
1293+
*/
1294+
typedef struct {
1295+
unsigned int version;
1296+
git_oid_t oid_type;
1297+
} git_diff_parse_options;
1298+
1299+
/* The current version of the diff parse options structure */
1300+
#define GIT_DIFF_PARSE_OPTIONS_VERSION 1
1301+
1302+
/* Stack initializer for diff parse options. Alternatively use
1303+
* `git_diff_parse_options_init` programmatic initialization.
1304+
*/
1305+
#define GIT_DIFF_PARSE_OPTIONS_INIT \
1306+
{ GIT_DIFF_PARSE_OPTIONS_VERSION, GIT_OID_DEFAULT }
1307+
12741308
/**
12751309
* Read the contents of a git patch file into a `git_diff` object.
12761310
*
@@ -1293,7 +1327,11 @@ GIT_EXTERN(int) git_diff_buffers(
12931327
GIT_EXTERN(int) git_diff_from_buffer(
12941328
git_diff **out,
12951329
const char *content,
1296-
size_t content_len);
1330+
size_t content_len
1331+
#ifdef GIT_EXPERIMENTAL_SHA256
1332+
, git_diff_parse_options *opts
1333+
#endif
1334+
);
12971335

12981336
/**
12991337
* This is an opaque structure which is allocated by `git_diff_get_stats`.

include/git2/index.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,12 @@ typedef enum {
184184
* @param index_path the path to the index file in disk
185185
* @return 0 or an error code
186186
*/
187+
188+
#ifdef GIT_EXPERIMENTAL_SHA256
189+
GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path, git_oid_t oid_type);
190+
#else
187191
GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path);
192+
#endif
188193

189194
/**
190195
* Create an in-memory index object.
@@ -197,7 +202,11 @@ GIT_EXTERN(int) git_index_open(git_index **out, const char *index_path);
197202
* @param out the pointer for the new index
198203
* @return 0 or an error code
199204
*/
205+
#ifdef GIT_EXPERIMENTAL_SHA256
206+
GIT_EXTERN(int) git_index_new(git_index **out, git_oid_t oid_type);
207+
#else
200208
GIT_EXTERN(int) git_index_new(git_index **out);
209+
#endif
201210

202211
/**
203212
* Free an existing index object.

include/git2/sys/commit_graph.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ GIT_BEGIN_DECL
2828
* @param objects_dir the path to a git objects directory.
2929
* @return Zero on success; -1 on failure.
3030
*/
31-
GIT_EXTERN(int) git_commit_graph_open(git_commit_graph **cgraph_out, const char *objects_dir);
31+
GIT_EXTERN(int) git_commit_graph_open(
32+
git_commit_graph **cgraph_out,
33+
const char *objects_dir
34+
#ifdef GIT_EXPERIMENTAL_SHA256
35+
, git_oid_t oid_type
36+
#endif
37+
);
3238

3339
/**
3440
* Frees commit-graph data. This should only be called when memory allocated
@@ -50,7 +56,11 @@ GIT_EXTERN(void) git_commit_graph_free(git_commit_graph *cgraph);
5056
*/
5157
GIT_EXTERN(int) git_commit_graph_writer_new(
5258
git_commit_graph_writer **out,
53-
const char *objects_info_dir);
59+
const char *objects_info_dir
60+
#ifdef GIT_EXPERIMENTAL_SHA256
61+
, git_oid_t oid_type
62+
#endif
63+
);
5464

5565
/**
5666
* Free the commit-graph writer and its resources.

include/git2/sys/midx.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ GIT_BEGIN_DECL
2929
*/
3030
GIT_EXTERN(int) git_midx_writer_new(
3131
git_midx_writer **out,
32-
const char *pack_dir);
32+
const char *pack_dir
33+
#ifdef GIT_EXPERIMENTAL_SHA256
34+
, git_oid_t oid_type
35+
#endif
36+
);
3337

3438
/**
3539
* Free the multi-pack-index writer and its resources.

src/libgit2/annotated_commit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ static int annotated_commit_init(
3939
if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0)
4040
goto done;
4141

42-
git_oid_fmt(annotated_commit->id_str, git_commit_id(commit));
43-
annotated_commit->id_str[GIT_OID_SHA1_HEXSIZE] = '\0';
42+
git_oid_tostr(annotated_commit->id_str, GIT_OID_MAX_HEXSIZE + 1,
43+
git_commit_id(commit));
4444

4545
if (!description)
4646
description = annotated_commit->id_str;

src/libgit2/annotated_commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ struct git_annotated_commit {
4141
const char *ref_name;
4242
const char *remote_url;
4343

44-
char id_str[GIT_OID_SHA1_HEXSIZE+1];
44+
char id_str[GIT_OID_MAX_HEXSIZE + 1];
4545
};
4646

4747
extern int git_annotated_commit_from_head(git_annotated_commit **out,

src/libgit2/apply.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "zstream.h"
2020
#include "reader.h"
2121
#include "index.h"
22+
#include "repository.h"
2223
#include "apply.h"
2324

2425
typedef struct {
@@ -644,7 +645,7 @@ int git_apply_to_tree(
644645
* put the current tree into the postimage as-is - the diff will
645646
* replace any entries contained therein
646647
*/
647-
if ((error = git_index_new(&postimage)) < 0 ||
648+
if ((error = git_index__new(&postimage, repo->oid_type)) < 0 ||
648649
(error = git_index_read_tree(postimage, preimage)) < 0 ||
649650
(error = git_reader_for_index(&post_reader, repo, postimage)) < 0)
650651
goto done;
@@ -851,8 +852,8 @@ int git_apply(
851852
* having the full repo index, so we will limit our checkout
852853
* to only write these files that were affected by the diff.
853854
*/
854-
if ((error = git_index_new(&preimage)) < 0 ||
855-
(error = git_index_new(&postimage)) < 0 ||
855+
if ((error = git_index__new(&preimage, repo->oid_type)) < 0 ||
856+
(error = git_index__new(&postimage, repo->oid_type)) < 0 ||
856857
(error = git_reader_for_index(&post_reader, repo, postimage)) < 0)
857858
goto done;
858859

src/libgit2/blame.c

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,11 @@ static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line)
6060
}
6161

6262
static git_blame_hunk *new_hunk(
63-
size_t start,
64-
size_t lines,
65-
size_t orig_start,
66-
const char *path)
63+
size_t start,
64+
size_t lines,
65+
size_t orig_start,
66+
const char *path,
67+
git_blame *blame)
6768
{
6869
git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk));
6970
if (!hunk) return NULL;
@@ -72,8 +73,8 @@ static git_blame_hunk *new_hunk(
7273
hunk->final_start_line_number = start;
7374
hunk->orig_start_line_number = orig_start;
7475
hunk->orig_path = path ? git__strdup(path) : NULL;
75-
git_oid_clear(&hunk->orig_commit_id, GIT_OID_SHA1);
76-
git_oid_clear(&hunk->final_commit_id, GIT_OID_SHA1);
76+
git_oid_clear(&hunk->orig_commit_id, blame->repository->oid_type);
77+
git_oid_clear(&hunk->final_commit_id, blame->repository->oid_type);
7778

7879
return hunk;
7980
}
@@ -86,13 +87,14 @@ static void free_hunk(git_blame_hunk *hunk)
8687
git__free(hunk);
8788
}
8889

89-
static git_blame_hunk *dup_hunk(git_blame_hunk *hunk)
90+
static git_blame_hunk *dup_hunk(git_blame_hunk *hunk, git_blame *blame)
9091
{
9192
git_blame_hunk *newhunk = new_hunk(
9293
hunk->final_start_line_number,
9394
hunk->lines_in_hunk,
9495
hunk->orig_start_line_number,
95-
hunk->orig_path);
96+
hunk->orig_path,
97+
blame);
9698

9799
if (!newhunk)
98100
return NULL;
@@ -237,7 +239,8 @@ static git_blame_hunk *split_hunk_in_vector(
237239
git_vector *vec,
238240
git_blame_hunk *hunk,
239241
size_t rel_line,
240-
bool return_new)
242+
bool return_new,
243+
git_blame *blame)
241244
{
242245
size_t new_line_count;
243246
git_blame_hunk *nh;
@@ -250,8 +253,9 @@ static git_blame_hunk *split_hunk_in_vector(
250253
}
251254

252255
new_line_count = hunk->lines_in_hunk - rel_line;
253-
nh = new_hunk(hunk->final_start_line_number + rel_line, new_line_count,
254-
hunk->orig_start_line_number + rel_line, hunk->orig_path);
256+
nh = new_hunk(hunk->final_start_line_number + rel_line,
257+
new_line_count, hunk->orig_start_line_number + rel_line,
258+
hunk->orig_path, blame);
255259

256260
if (!nh)
257261
return NULL;
@@ -304,7 +308,8 @@ static int index_blob_lines(git_blame *blame)
304308
static git_blame_hunk *hunk_from_entry(git_blame__entry *e, git_blame *blame)
305309
{
306310
git_blame_hunk *h = new_hunk(
307-
e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path);
311+
e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path,
312+
blame);
308313

309314
if (!h)
310315
return NULL;
@@ -445,14 +450,16 @@ static int buffer_hunk_cb(
445450
blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line);
446451
if (!blame->current_hunk) {
447452
/* Line added at the end of the file */
448-
blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, blame->path);
453+
blame->current_hunk = new_hunk(wedge_line, 0, wedge_line,
454+
blame->path, blame);
449455
GIT_ERROR_CHECK_ALLOC(blame->current_hunk);
450456

451457
git_vector_insert(&blame->hunks, blame->current_hunk);
452458
} else if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){
453459
/* If this hunk doesn't start between existing hunks, split a hunk up so it does */
454460
blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk,
455-
wedge_line - blame->current_hunk->orig_start_line_number, true);
461+
wedge_line - blame->current_hunk->orig_start_line_number, true,
462+
blame);
456463
GIT_ERROR_CHECK_ALLOC(blame->current_hunk);
457464
}
458465

@@ -481,7 +488,7 @@ static int buffer_line_cb(
481488
} else {
482489
/* Create a new buffer-blame hunk with this line */
483490
shift_hunks_by(&blame->hunks, blame->current_diff_line, 1);
484-
blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path);
491+
blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path, blame);
485492
GIT_ERROR_CHECK_ALLOC(blame->current_hunk);
486493

487494
git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL);
@@ -529,7 +536,7 @@ int git_blame_buffer(
529536

530537
/* Duplicate all of the hunk structures in the reference blame */
531538
git_vector_foreach(&reference->hunks, i, hunk) {
532-
git_blame_hunk *h = dup_hunk(hunk);
539+
git_blame_hunk *h = dup_hunk(hunk, blame);
533540
GIT_ERROR_CHECK_ALLOC(h);
534541

535542
git_vector_insert(&blame->hunks, h);

0 commit comments

Comments
 (0)