diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-22 23:12:00 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2023-04-22 23:17:32 +0100 |
commit | 190a4c55df72b32adf4d60f77cbc47276b74f84b (patch) | |
tree | 896d6d41c2ea6305992bd646bbbe79a3fdb5f6fe /src/libgit2 | |
parent | e288f874a3a73ef31f88bb524f6d25d5ff3c5a3a (diff) | |
parent | 8a62616f43fe5ea37d41296f40293ff97aa88cfa (diff) | |
download | libgit2-190a4c55df72b32adf4d60f77cbc47276b74f84b.tar.gz |
Merge remote-tracking branch 'origin/main' into shallow-clone-network
Diffstat (limited to 'src/libgit2')
91 files changed, 2712 insertions, 5307 deletions
diff --git a/src/libgit2/CMakeLists.txt b/src/libgit2/CMakeLists.txt index 03b571212..876a703e8 100644 --- a/src/libgit2/CMakeLists.txt +++ b/src/libgit2/CMakeLists.txt @@ -10,7 +10,6 @@ include(PkgBuildConfig) set(LIBGIT2_INCLUDES "${PROJECT_BINARY_DIR}/src/util" "${PROJECT_BINARY_DIR}/include" - "${PROJECT_BINARY_DIR}/include/git2" "${PROJECT_SOURCE_DIR}/src/libgit2" "${PROJECT_SOURCE_DIR}/src/util" "${PROJECT_SOURCE_DIR}/include") @@ -25,8 +24,7 @@ target_sources(libgit2 PRIVATE ${SRC_H}) file(GLOB SRC_GIT2 *.c *.h streams/*.c streams/*.h - transports/*.c transports/*.h - xdiff/*.c xdiff/*.h) + transports/*.c transports/*.h) list(SORT SRC_GIT2) target_sources(libgit2 PRIVATE ${SRC_GIT2}) @@ -40,23 +38,6 @@ if(APPLE) set_source_files_properties(streams/stransport.c PROPERTIES COMPILE_FLAGS -Wno-deprecated) endif() -# the xdiff dependency is not (yet) warning-free, disable warnings -# as errors for the xdiff sources until we've sorted them out -if(MSVC) - set_source_files_properties(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS -WX-) - set_source_files_properties(xdiff/xemit.c PROPERTIES COMPILE_FLAGS -WX-) - set_source_files_properties(xdiff/xhistogram.c PROPERTIES COMPILE_FLAGS -WX-) - set_source_files_properties(xdiff/xmerge.c PROPERTIES COMPILE_FLAGS -WX-) - set_source_files_properties(xdiff/xutils.c PROPERTIES COMPILE_FLAGS -WX-) - set_source_files_properties(xdiff/xpatience.c PROPERTIES COMPILE_FLAGS -WX-) -else() - set_source_files_properties(xdiff/xdiffi.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter") - set_source_files_properties(xdiff/xemit.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare -Wno-unused-parameter") - set_source_files_properties(xdiff/xhistogram.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") - set_source_files_properties(xdiff/xutils.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") - set_source_files_properties(xdiff/xpatience.c PROPERTIES COMPILE_FLAGS "-Wno-sign-compare") -endif() - ide_split_sources(libgit2) list(APPEND LIBGIT2_OBJECTS $<TARGET_OBJECTS:util> $<TARGET_OBJECTS:libgit2> ${LIBGIT2_DEPENDENCY_OBJECTS}) list(APPEND LIBGIT2_INCLUDES ${LIBGIT2_DEPENDENCY_INCLUDES}) diff --git a/src/libgit2/annotated_commit.c b/src/libgit2/annotated_commit.c index 7bd8b6077..c5c8ace78 100644 --- a/src/libgit2/annotated_commit.c +++ b/src/libgit2/annotated_commit.c @@ -39,8 +39,8 @@ static int annotated_commit_init( if ((error = git_commit_dup(&annotated_commit->commit, commit)) < 0) goto done; - git_oid_fmt(annotated_commit->id_str, git_commit_id(commit)); - annotated_commit->id_str[GIT_OID_SHA1_HEXSIZE] = '\0'; + git_oid_tostr(annotated_commit->id_str, GIT_OID_MAX_HEXSIZE + 1, + git_commit_id(commit)); if (!description) description = annotated_commit->id_str; diff --git a/src/libgit2/annotated_commit.h b/src/libgit2/annotated_commit.h index c87eaa805..1f805fe9b 100644 --- a/src/libgit2/annotated_commit.h +++ b/src/libgit2/annotated_commit.h @@ -41,7 +41,7 @@ struct git_annotated_commit { const char *ref_name; const char *remote_url; - char id_str[GIT_OID_SHA1_HEXSIZE+1]; + char id_str[GIT_OID_MAX_HEXSIZE + 1]; }; extern int git_annotated_commit_from_head(git_annotated_commit **out, diff --git a/src/libgit2/apply.c b/src/libgit2/apply.c index 18304da4d..6b55b812f 100644 --- a/src/libgit2/apply.c +++ b/src/libgit2/apply.c @@ -19,6 +19,7 @@ #include "zstream.h" #include "reader.h" #include "index.h" +#include "repository.h" #include "apply.h" typedef struct { @@ -644,7 +645,7 @@ int git_apply_to_tree( * put the current tree into the postimage as-is - the diff will * replace any entries contained therein */ - if ((error = git_index_new(&postimage)) < 0 || + if ((error = git_index__new(&postimage, repo->oid_type)) < 0 || (error = git_index_read_tree(postimage, preimage)) < 0 || (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) goto done; @@ -851,8 +852,8 @@ int git_apply( * having the full repo index, so we will limit our checkout * to only write these files that were affected by the diff. */ - if ((error = git_index_new(&preimage)) < 0 || - (error = git_index_new(&postimage)) < 0 || + if ((error = git_index__new(&preimage, repo->oid_type)) < 0 || + (error = git_index__new(&postimage, repo->oid_type)) < 0 || (error = git_reader_for_index(&post_reader, repo, postimage)) < 0) goto done; diff --git a/src/libgit2/blame.c b/src/libgit2/blame.c index b70cd615e..d93dd5e76 100644 --- a/src/libgit2/blame.c +++ b/src/libgit2/blame.c @@ -60,10 +60,11 @@ static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line) } static git_blame_hunk *new_hunk( - size_t start, - size_t lines, - size_t orig_start, - const char *path) + size_t start, + size_t lines, + size_t orig_start, + const char *path, + git_blame *blame) { git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk)); if (!hunk) return NULL; @@ -72,8 +73,8 @@ static git_blame_hunk *new_hunk( hunk->final_start_line_number = start; hunk->orig_start_line_number = orig_start; hunk->orig_path = path ? git__strdup(path) : NULL; - git_oid_clear(&hunk->orig_commit_id, GIT_OID_SHA1); - git_oid_clear(&hunk->final_commit_id, GIT_OID_SHA1); + git_oid_clear(&hunk->orig_commit_id, blame->repository->oid_type); + git_oid_clear(&hunk->final_commit_id, blame->repository->oid_type); return hunk; } @@ -86,13 +87,14 @@ static void free_hunk(git_blame_hunk *hunk) git__free(hunk); } -static git_blame_hunk *dup_hunk(git_blame_hunk *hunk) +static git_blame_hunk *dup_hunk(git_blame_hunk *hunk, git_blame *blame) { git_blame_hunk *newhunk = new_hunk( hunk->final_start_line_number, hunk->lines_in_hunk, hunk->orig_start_line_number, - hunk->orig_path); + hunk->orig_path, + blame); if (!newhunk) return NULL; @@ -237,7 +239,8 @@ static git_blame_hunk *split_hunk_in_vector( git_vector *vec, git_blame_hunk *hunk, size_t rel_line, - bool return_new) + bool return_new, + git_blame *blame) { size_t new_line_count; git_blame_hunk *nh; @@ -250,8 +253,9 @@ static git_blame_hunk *split_hunk_in_vector( } new_line_count = hunk->lines_in_hunk - rel_line; - nh = new_hunk(hunk->final_start_line_number + rel_line, new_line_count, - hunk->orig_start_line_number + rel_line, hunk->orig_path); + nh = new_hunk(hunk->final_start_line_number + rel_line, + new_line_count, hunk->orig_start_line_number + rel_line, + hunk->orig_path, blame); if (!nh) return NULL; @@ -304,7 +308,8 @@ static int index_blob_lines(git_blame *blame) static git_blame_hunk *hunk_from_entry(git_blame__entry *e, git_blame *blame) { git_blame_hunk *h = new_hunk( - e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); + e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path, + blame); if (!h) return NULL; @@ -445,14 +450,16 @@ static int buffer_hunk_cb( blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line); if (!blame->current_hunk) { /* Line added at the end of the file */ - blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, blame->path); + blame->current_hunk = new_hunk(wedge_line, 0, wedge_line, + blame->path, blame); GIT_ERROR_CHECK_ALLOC(blame->current_hunk); git_vector_insert(&blame->hunks, blame->current_hunk); } else if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){ /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk, - wedge_line - blame->current_hunk->orig_start_line_number, true); + wedge_line - blame->current_hunk->orig_start_line_number, true, + blame); GIT_ERROR_CHECK_ALLOC(blame->current_hunk); } @@ -481,7 +488,7 @@ static int buffer_line_cb( } else { /* Create a new buffer-blame hunk with this line */ shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); - blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path); + blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path, blame); GIT_ERROR_CHECK_ALLOC(blame->current_hunk); git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); @@ -529,7 +536,7 @@ int git_blame_buffer( /* Duplicate all of the hunk structures in the reference blame */ git_vector_foreach(&reference->hunks, i, hunk) { - git_blame_hunk *h = dup_hunk(hunk); + git_blame_hunk *h = dup_hunk(hunk, blame); GIT_ERROR_CHECK_ALLOC(h); git_vector_insert(&blame->hunks, h); diff --git a/src/libgit2/blame_git.c b/src/libgit2/blame_git.c index 2504b338a..69897b386 100644 --- a/src/libgit2/blame_git.c +++ b/src/libgit2/blame_git.c @@ -9,7 +9,6 @@ #include "commit.h" #include "blob.h" -#include "xdiff/xinclude.h" #include "diff_xdiff.h" /* diff --git a/src/libgit2/branch.c b/src/libgit2/branch.c index 4cbd1e26f..9a31c9c6f 100644 --- a/src/libgit2/branch.c +++ b/src/libgit2/branch.c @@ -134,9 +134,9 @@ int git_branch_create( const git_commit *commit, int force) { - char commit_id[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; - git_oid_tostr(commit_id, GIT_OID_SHA1_HEXSIZE + 1, git_commit_id(commit)); + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); return create_branch(ref_out, repository, branch_name, commit, commit_id, force); } diff --git a/src/libgit2/cherrypick.c b/src/libgit2/cherrypick.c index 04812b1c6..3ef42d5e7 100644 --- a/src/libgit2/cherrypick.c +++ b/src/libgit2/cherrypick.c @@ -106,10 +106,10 @@ static int cherrypick_state_cleanup(git_repository *repo) static int cherrypick_seterr(git_commit *commit, const char *fmt) { - char commit_oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; git_error_set(GIT_ERROR_CHERRYPICK, fmt, - git_oid_tostr(commit_oidstr, GIT_OID_SHA1_HEXSIZE + 1, git_commit_id(commit))); + git_oid_tostr(commit_oidstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit))); return -1; } @@ -173,7 +173,7 @@ int git_cherrypick( git_cherrypick_options opts; git_reference *our_ref = NULL; git_commit *our_commit = NULL; - char commit_oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; const char *commit_msg, *commit_summary; git_str their_label = GIT_STR_INIT; git_index *index = NULL; diff --git a/src/libgit2/commit_graph.c b/src/libgit2/commit_graph.c index bf557f7ad..4edd71106 100644 --- a/src/libgit2/commit_graph.c +++ b/src/libgit2/commit_graph.c @@ -138,19 +138,22 @@ static int commit_graph_parse_oid_lookup( struct git_commit_graph_chunk *chunk_oid_lookup) { uint32_t i; - unsigned char *oid, *prev_oid, zero_oid[GIT_OID_SHA1_SIZE] = {0}; + unsigned char *oid, *prev_oid, zero_oid[GIT_OID_MAX_SIZE] = {0}; + size_t oid_size; + + oid_size = git_oid_size(file->oid_type); if (chunk_oid_lookup->offset == 0) return commit_graph_error("missing OID Lookup chunk"); if (chunk_oid_lookup->length == 0) return commit_graph_error("empty OID Lookup chunk"); - if (chunk_oid_lookup->length != file->num_commits * GIT_OID_SHA1_SIZE) + if (chunk_oid_lookup->length != file->num_commits * oid_size) return commit_graph_error("OID Lookup chunk has wrong length"); file->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset); prev_oid = zero_oid; - for (i = 0; i < file->num_commits; ++i, oid += GIT_OID_SHA1_SIZE) { - if (git_oid_raw_cmp(prev_oid, oid, GIT_OID_SHA1_SIZE) >= 0) + for (i = 0; i < file->num_commits; ++i, oid += oid_size) { + if (git_oid_raw_cmp(prev_oid, oid, oid_size) >= 0) return commit_graph_error("OID Lookup index is non-monotonic"); prev_oid = oid; } @@ -163,11 +166,13 @@ static int commit_graph_parse_commit_data( const unsigned char *data, struct git_commit_graph_chunk *chunk_commit_data) { + size_t oid_size = git_oid_size(file->oid_type); + if (chunk_commit_data->offset == 0) return commit_graph_error("missing Commit Data chunk"); if (chunk_commit_data->length == 0) return commit_graph_error("empty Commit Data chunk"); - if (chunk_commit_data->length != file->num_commits * (GIT_OID_SHA1_SIZE + 16)) + if (chunk_commit_data->length != file->num_commits * (oid_size + 16)) return commit_graph_error("Commit Data chunk has wrong length"); file->commit_data = data + chunk_commit_data->offset; @@ -209,7 +214,9 @@ int git_commit_graph_file_parse( GIT_ASSERT_ARG(file); - if (size < sizeof(struct git_commit_graph_header) + GIT_OID_SHA1_SIZE) + checksum_size = git_oid_size(file->oid_type); + + if (size < sizeof(struct git_commit_graph_header) + checksum_size) return commit_graph_error("commit-graph is too short"); hdr = ((struct git_commit_graph_header *)data); @@ -226,8 +233,7 @@ int git_commit_graph_file_parse( * headers, and a special zero chunk. */ last_chunk_offset = sizeof(struct git_commit_graph_header) + (1 + hdr->chunks) * 12; - trailer_offset = size - GIT_OID_SHA1_SIZE; - checksum_size = GIT_HASH_SHA1_SIZE; + trailer_offset = size - checksum_size; if (trailer_offset < last_chunk_offset) return commit_graph_error("wrong commit-graph size"); @@ -295,25 +301,35 @@ int git_commit_graph_file_parse( return 0; } -int git_commit_graph_new(git_commit_graph **cgraph_out, const char *objects_dir, bool open_file) +int git_commit_graph_new( + git_commit_graph **cgraph_out, + const char *objects_dir, + bool open_file, + git_oid_t oid_type) { git_commit_graph *cgraph = NULL; int error = 0; GIT_ASSERT_ARG(cgraph_out); GIT_ASSERT_ARG(objects_dir); + GIT_ASSERT_ARG(oid_type); cgraph = git__calloc(1, sizeof(git_commit_graph)); GIT_ERROR_CHECK_ALLOC(cgraph); + cgraph->oid_type = oid_type; + error = git_str_joinpath(&cgraph->filename, objects_dir, "info/commit-graph"); if (error < 0) goto error; if (open_file) { - error = git_commit_graph_file_open(&cgraph->file, git_str_cstr(&cgraph->filename)); + error = git_commit_graph_file_open(&cgraph->file, + git_str_cstr(&cgraph->filename), oid_type); + if (error < 0) goto error; + cgraph->checked = 1; } @@ -326,14 +342,18 @@ error: } int git_commit_graph_validate(git_commit_graph *cgraph) { - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; - size_t trailer_offset = cgraph->file->graph_map.len - checksum_size; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + git_hash_algorithm_t checksum_type; + size_t checksum_size, trailer_offset; + + checksum_type = git_oid_algorithm(cgraph->oid_type); + checksum_size = git_hash_size(checksum_type); + trailer_offset = cgraph->file->graph_map.len - checksum_size; if (cgraph->file->graph_map.len < checksum_size) return commit_graph_error("map length too small"); - if (git_hash_buf(checksum, cgraph->file->graph_map.data, trailer_offset, GIT_HASH_ALGORITHM_SHA1) < 0) + if (git_hash_buf(checksum, cgraph->file->graph_map.data, trailer_offset, checksum_type) < 0) return commit_graph_error("could not calculate signature"); if (memcmp(checksum, cgraph->file->checksum, checksum_size) != 0) return commit_graph_error("index signature mismatch"); @@ -341,16 +361,32 @@ int git_commit_graph_validate(git_commit_graph *cgraph) { return 0; } -int git_commit_graph_open(git_commit_graph **cgraph_out, const char *objects_dir) +int git_commit_graph_open( + git_commit_graph **cgraph_out, + const char *objects_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_oid_t oid_type +#endif + ) { - int error = git_commit_graph_new(cgraph_out, objects_dir, true); - if (!error) { +#ifndef GIT_EXPERIMENTAL_SHA256 + git_oid_t oid_type = GIT_OID_SHA1; +#endif + int error; + + error = git_commit_graph_new(cgraph_out, objects_dir, true, + oid_type); + + if (!error) return git_commit_graph_validate(*cgraph_out); - } + return error; } -int git_commit_graph_file_open(git_commit_graph_file **file_out, const char *path) +int git_commit_graph_file_open( + git_commit_graph_file **file_out, + const char *path, + git_oid_t oid_type) { git_commit_graph_file *file; git_file fd = -1; @@ -379,6 +415,8 @@ int git_commit_graph_file_open(git_commit_graph_file **file_out, const char *pat file = git__calloc(1, sizeof(git_commit_graph_file)); GIT_ERROR_CHECK_ALLOC(file); + file->oid_type = oid_type; + error = git_futils_mmap_ro(&file->graph_map, fd, 0, cgraph_size); p_close(fd); if (error < 0) { @@ -395,7 +433,9 @@ int git_commit_graph_file_open(git_commit_graph_file **file_out, const char *pat return 0; } -int git_commit_graph_get_file(git_commit_graph_file **file_out, git_commit_graph *cgraph) +int git_commit_graph_get_file( + git_commit_graph_file **file_out, + git_commit_graph *cgraph) { if (!cgraph->checked) { int error = 0; @@ -405,7 +445,8 @@ int git_commit_graph_get_file(git_commit_graph_file **file_out, git_commit_graph cgraph->checked = 1; /* Best effort */ - error = git_commit_graph_file_open(&result, git_str_cstr(&cgraph->filename)); + error = git_commit_graph_file_open(&result, + git_str_cstr(&cgraph->filename), cgraph->oid_type); if (error < 0) return error; @@ -441,6 +482,7 @@ static int git_commit_graph_entry_get_byindex( size_t pos) { const unsigned char *commit_data; + size_t oid_size = git_oid_size(file->oid_type); GIT_ASSERT_ARG(e); GIT_ASSERT_ARG(file); @@ -450,15 +492,15 @@ static int git_commit_graph_entry_get_byindex( return GIT_ENOTFOUND; } - commit_data = file->commit_data + pos * (GIT_OID_SHA1_SIZE + 4 * sizeof(uint32_t)); - git_oid__fromraw(&e->tree_oid, commit_data, GIT_OID_SHA1); - e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE))); + commit_data = file->commit_data + pos * (oid_size + 4 * sizeof(uint32_t)); + git_oid__fromraw(&e->tree_oid, commit_data, file->oid_type); + e->parent_indices[0] = ntohl(*((uint32_t *)(commit_data + oid_size))); e->parent_indices[1] = ntohl( - *((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + sizeof(uint32_t)))); + *((uint32_t *)(commit_data + oid_size + sizeof(uint32_t)))); e->parent_count = (e->parent_indices[0] != GIT_COMMIT_GRAPH_MISSING_PARENT) + (e->parent_indices[1] != GIT_COMMIT_GRAPH_MISSING_PARENT); - e->generation = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + 2 * sizeof(uint32_t)))); - e->commit_time = ntohl(*((uint32_t *)(commit_data + GIT_OID_SHA1_SIZE + 3 * sizeof(uint32_t)))); + e->generation = ntohl(*((uint32_t *)(commit_data + oid_size + 2 * sizeof(uint32_t)))); + e->commit_time = ntohl(*((uint32_t *)(commit_data + oid_size + 3 * sizeof(uint32_t)))); e->commit_time |= (e->generation & UINT64_C(0x3)) << UINT64_C(32); e->generation >>= 2u; @@ -485,7 +527,7 @@ static int git_commit_graph_entry_get_byindex( } } - git_oid__fromraw(&e->sha1, &file->oid_lookup[pos * GIT_OID_SHA1_SIZE], GIT_OID_SHA1); + git_oid__fromraw(&e->sha1, &file->oid_lookup[pos * oid_size], file->oid_type); return 0; } @@ -494,8 +536,8 @@ bool git_commit_graph_file_needs_refresh(const git_commit_graph_file *file, cons git_file fd = -1; struct stat st; ssize_t bytes_read; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_oid_size(file->oid_type); /* TODO: properly open the file without access time using O_NOATIME */ fd = git_futils_open_ro(path); @@ -530,35 +572,40 @@ int git_commit_graph_entry_find( int pos, found = 0; uint32_t hi, lo; const unsigned char *current = NULL; + size_t oid_size, oid_hexsize; GIT_ASSERT_ARG(e); GIT_ASSERT_ARG(file); GIT_ASSERT_ARG(short_oid); + oid_size = git_oid_size(file->oid_type); + oid_hexsize = git_oid_hexsize(file->oid_type); + hi = ntohl(file->oid_fanout[(int)short_oid->id[0]]); lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(file->oid_fanout[(int)short_oid->id[0] - 1])); - pos = git_pack__lookup_id(file->oid_lookup, GIT_OID_SHA1_SIZE, lo, hi, short_oid->id, GIT_OID_SHA1); + pos = git_pack__lookup_id(file->oid_lookup, oid_size, lo, hi, + short_oid->id, file->oid_type); if (pos >= 0) { /* An object matching exactly the oid was found */ found = 1; - current = file->oid_lookup + (pos * GIT_OID_SHA1_SIZE); + current = file->oid_lookup + (pos * oid_size); } else { /* No object was found */ /* pos refers to the object with the "closest" oid to short_oid */ pos = -1 - pos; if (pos < (int)file->num_commits) { - current = file->oid_lookup + (pos * GIT_OID_SHA1_SIZE); + current = file->oid_lookup + (pos * oid_size); if (!git_oid_raw_ncmp(short_oid->id, current, len)) found = 1; } } - if (found && len != GIT_OID_SHA1_HEXSIZE && pos + 1 < (int)file->num_commits) { + if (found && len != oid_hexsize && pos + 1 < (int)file->num_commits) { /* Check for ambiguousity */ - const unsigned char *next = current + GIT_OID_SHA1_SIZE; + const unsigned char *next = current + oid_size; if (!git_oid_raw_ncmp(short_oid->id, next, len)) found = 2; @@ -637,11 +684,27 @@ static int packed_commit__cmp(const void *a_, const void *b_) return git_oid_cmp(&a->sha1, &b->sha1); } -int git_commit_graph_writer_new(git_commit_graph_writer **out, const char *objects_info_dir) +int git_commit_graph_writer_new( + git_commit_graph_writer **out, + const char *objects_info_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_oid_t oid_type +#endif + ) { - git_commit_graph_writer *w = git__calloc(1, sizeof(git_commit_graph_writer)); + git_commit_graph_writer *w; + +#ifndef GIT_EXPERIMENTAL_SHA256 + git_oid_t oid_type = GIT_OID_SHA1; +#endif + + GIT_ASSERT_ARG(out && objects_info_dir && oid_type); + + w = git__calloc(1, sizeof(git_commit_graph_writer)); GIT_ERROR_CHECK_ALLOC(w); + w->oid_type = oid_type; + if (git_str_sets(&w->objects_info_dir, objects_info_dir) < 0) { git__free(w); return -1; @@ -993,8 +1056,9 @@ static int commit_graph_write( off64_t offset; git_str oid_lookup = GIT_STR_INIT, commit_data = GIT_STR_INIT, extra_edge_list = GIT_STR_INIT; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + git_hash_algorithm_t checksum_type; + size_t checksum_size, oid_size; git_hash_ctx ctx; struct commit_graph_write_hash_context hash_cb_data = {0}; @@ -1007,8 +1071,11 @@ static int commit_graph_write( hash_cb_data.cb_data = cb_data; hash_cb_data.ctx = &ctx; - checksum_size = GIT_HASH_SHA1_SIZE; - error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1); + oid_size = git_oid_size(w->oid_type); + checksum_type = git_oid_algorithm(w->oid_type); + checksum_size = git_hash_size(checksum_type); + + error = git_hash_ctx_init(&ctx, checksum_type); if (error < 0) return error; cb_data = &hash_cb_data; @@ -1035,7 +1102,7 @@ static int commit_graph_write( git_vector_foreach (&w->commits, i, packed_commit) { error = git_str_put(&oid_lookup, (const char *)&packed_commit->sha1.id, - GIT_OID_SHA1_SIZE); + oid_size); if (error < 0) goto cleanup; @@ -1052,7 +1119,7 @@ static int commit_graph_write( error = git_str_put(&commit_data, (const char *)&packed_commit->tree_oid.id, - GIT_OID_SHA1_SIZE); + oid_size); if (error < 0) goto cleanup; diff --git a/src/libgit2/commit_graph.h b/src/libgit2/commit_graph.h index 517abb239..ecf4379bd 100644 --- a/src/libgit2/commit_graph.h +++ b/src/libgit2/commit_graph.h @@ -30,6 +30,9 @@ typedef struct git_commit_graph_file { git_map graph_map; + /* The type of object IDs in the commit graph file. */ + git_oid_t oid_type; + /* The OID Fanout table. */ const uint32_t *oid_fanout; /* The total number of commits in the graph. */ @@ -84,10 +87,10 @@ typedef struct git_commit_graph_entry { /* The index within the Extra Edge List of any parent after the first two. */ size_t extra_parents_index; - /* The SHA-1 hash of the root tree of the commit. */ + /* The object ID of the root tree of the commit. */ git_oid tree_oid; - /* The SHA-1 hash of the requested commit. */ + /* The object ID hash of the requested commit. */ git_oid sha1; } git_commit_graph_entry; @@ -99,18 +102,28 @@ struct git_commit_graph { /* The underlying commit-graph file. */ git_commit_graph_file *file; + /* The object ID types in the commit graph. */ + git_oid_t oid_type; + /* Whether the commit-graph file was already checked for validity. */ bool checked; }; /** Create a new commit-graph, optionally opening the underlying file. */ -int git_commit_graph_new(git_commit_graph **cgraph_out, const char *objects_dir, bool open_file); +int git_commit_graph_new( + git_commit_graph **cgraph_out, + const char *objects_dir, + bool open_file, + git_oid_t oid_type); /** Validate the checksum of a commit graph */ int git_commit_graph_validate(git_commit_graph *cgraph); /** Open and validate a commit-graph file. */ -int git_commit_graph_file_open(git_commit_graph_file **file_out, const char *path); +int git_commit_graph_file_open( + git_commit_graph_file **file_out, + const char *path, + git_oid_t oid_type); /* * Attempt to get the git_commit_graph's commit-graph file. This object is @@ -134,6 +147,9 @@ struct git_commit_graph_writer { */ git_str objects_info_dir; + /* The object ID type of the commit graph. */ + git_oid_t oid_type; + /* The list of packed commits. */ git_vector commits; }; diff --git a/src/libgit2/commit_list.c b/src/libgit2/commit_list.c index 12b329b25..485871db3 100644 --- a/src/libgit2/commit_list.c +++ b/src/libgit2/commit_list.c @@ -125,7 +125,7 @@ static int commit_quick_parse( git_oid *parent_oid; git_commit *commit; git_commit__parse_options parse_opts = { - GIT_OID_SHA1, + walk->repo->oid_type, GIT_COMMIT_PARSE_QUICK }; size_t i; @@ -176,7 +176,9 @@ int git_commit_list_parse(git_revwalk *walk, git_commit_list_node *commit) if (cgraph_file) { git_commit_graph_entry e; - error = git_commit_graph_entry_find(&e, cgraph_file, &commit->oid, GIT_OID_SHA1_SIZE); + error = git_commit_graph_entry_find(&e, cgraph_file, + &commit->oid, git_oid_size(walk->repo->oid_type)); + if (error == 0 && git__is_uint16(e.parent_count)) { size_t i; commit->generation = (uint32_t)e.generation; diff --git a/src/libgit2/config.c b/src/libgit2/config.c index 6d15a8db6..23a8f9ffa 100644 --- a/src/libgit2/config.c +++ b/src/libgit2/config.c @@ -1174,9 +1174,12 @@ int git_config__find_programdata(git_str *path) GIT_FS_PATH_OWNER_CURRENT_USER | GIT_FS_PATH_OWNER_ADMINISTRATOR; bool is_safe; + int error; + + if ((error = git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA)) < 0) + return error; - if (git_sysdir_find_programdata_file(path, GIT_CONFIG_FILENAME_PROGRAMDATA) < 0 || - git_fs_path_owner_is(&is_safe, path->ptr, owner_level) < 0) + if (git_fs_path_owner_is(&is_safe, path->ptr, owner_level) < 0) return -1; if (!is_safe) { diff --git a/src/libgit2/config_file.c b/src/libgit2/config_file.c index 932ca7601..716924de6 100644 --- a/src/libgit2/config_file.c +++ b/src/libgit2/config_file.c @@ -26,7 +26,7 @@ typedef struct config_file { git_futils_filestamp stamp; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_SHA256_SIZE]; char *path; git_array_t(struct config_file) includes; } config_file; @@ -133,7 +133,7 @@ static int config_file_is_modified(int *modified, config_file *file) { config_file *include; git_str buf = GIT_STR_INIT; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_SHA256_SIZE]; uint32_t i; int error = 0; @@ -145,10 +145,10 @@ static int config_file_is_modified(int *modified, config_file *file) if ((error = git_futils_readbuffer(&buf, file->path)) < 0) goto out; - if ((error = git_hash_buf(checksum, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA1)) < 0) + if ((error = git_hash_buf(checksum, buf.ptr, buf.size, GIT_HASH_ALGORITHM_SHA256)) < 0) goto out; - if (memcmp(checksum, file->checksum, GIT_HASH_SHA1_SIZE) != 0) { + if (memcmp(checksum, file->checksum, GIT_HASH_SHA256_SIZE) != 0) { *modified = 1; goto out; } @@ -881,7 +881,7 @@ static int config_file_read( goto out; git_futils_filestamp_set_from_stat(&file->stamp, &st); - if ((error = git_hash_buf(file->checksum, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA1)) < 0) + if ((error = git_hash_buf(file->checksum, contents.ptr, contents.size, GIT_HASH_ALGORITHM_SHA256)) < 0) goto out; if ((error = config_file_read_buffer(entries, repo, file, level, depth, @@ -1116,7 +1116,12 @@ static int write_on_eof( /* * This is pretty much the parsing, except we write out anything we don't have */ -static int config_file_write(config_file_backend *cfg, const char *orig_key, const char *key, const git_regexp *preg, const char *value) +static int config_file_write( + config_file_backend *cfg, + const char *orig_key, + const char *key, + const git_regexp *preg, + const char *value) { char *orig_section = NULL, *section = NULL, *orig_name, *name, *ldot; @@ -1131,8 +1136,9 @@ static int config_file_write(config_file_backend *cfg, const char *orig_key, con if (cfg->locked) { error = git_str_puts(&contents, git_str_cstr(&cfg->locked_content) == NULL ? "" : git_str_cstr(&cfg->locked_content)); } else { - if ((error = git_filebuf_open(&file, cfg->file.path, GIT_FILEBUF_HASH_CONTENTS, - GIT_CONFIG_FILE_MODE)) < 0) + if ((error = git_filebuf_open(&file, cfg->file.path, + GIT_FILEBUF_HASH_SHA256, + GIT_CONFIG_FILE_MODE)) < 0) goto done; /* We need to read in our own config file */ diff --git a/src/libgit2/describe.c b/src/libgit2/describe.c index 3f73d87d6..044534723 100644 --- a/src/libgit2/describe.c +++ b/src/libgit2/describe.c @@ -363,12 +363,15 @@ static int find_unique_abbrev_size( size_t size = abbreviated_size; git_odb *odb; git_oid dummy; + size_t hexsize; int error; if ((error = git_repository_odb__weakptr(&odb, repo)) < 0) return error; - while (size < GIT_OID_SHA1_HEXSIZE) { + hexsize = git_oid_hexsize(repo->oid_type); + + while (size < hexsize) { if ((error = git_odb_exists_prefix(&dummy, odb, oid_in, size)) == 0) { *out = (int) size; return 0; @@ -383,7 +386,7 @@ static int find_unique_abbrev_size( } /* If we didn't find any shorter prefix, we have to do the whole thing */ - *out = GIT_OID_SHA1_HEXSIZE; + *out = (int)hexsize; return 0; } @@ -397,7 +400,7 @@ static int show_suffix( { int error, size = 0; - char hex_oid[GIT_OID_SHA1_HEXSIZE]; + char hex_oid[GIT_OID_MAX_HEXSIZE]; if ((error = find_unique_abbrev_size(&size, repo, id, abbrev_size)) < 0) return error; @@ -414,7 +417,7 @@ static int show_suffix( #define MAX_CANDIDATES_TAGS FLAG_BITS - 1 static int describe_not_found(const git_oid *oid, const char *message_format) { - char oid_str[GIT_OID_SHA1_HEXSIZE + 1]; + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; git_oid_tostr(oid_str, sizeof(oid_str), oid); git_error_set(GIT_ERROR_DESCRIBE, message_format, oid_str); @@ -525,7 +528,7 @@ static int describe( if (annotated_cnt && (git_pqueue_size(&list) == 0)) { /* if (debug) { - char oid_str[GIT_OID_SHA1_HEXSIZE + 1]; + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; git_oid_tostr(oid_str, sizeof(oid_str), &c->oid); fprintf(stderr, "finished search at %s\n", oid_str); @@ -592,7 +595,7 @@ static int describe( "head", "lightweight", "annotated", }; - char oid_str[GIT_OID_SHA1_HEXSIZE + 1]; + char oid_str[GIT_OID_MAX_HEXSIZE + 1]; if (debug) { for (cur_match = 0; cur_match < match_cnt; cur_match++) { @@ -816,7 +819,7 @@ static int git_describe__format( /* If we didn't find *any* tags, we fall back to the commit's id */ if (result->fallback_to_id) { - char hex_oid[GIT_OID_SHA1_HEXSIZE + 1] = {0}; + char hex_oid[GIT_OID_MAX_HEXSIZE + 1] = {0}; int size = 0; if ((error = find_unique_abbrev_size( diff --git a/src/libgit2/diff.c b/src/libgit2/diff.c index 20a18c4b9..db12ccd68 100644 --- a/src/libgit2/diff.c +++ b/src/libgit2/diff.c @@ -19,8 +19,10 @@ #include "git2/email.h" struct patch_id_args { + git_diff *diff; git_hash_ctx ctx; git_oid result; + git_oid_t oid_type; int first_file; }; @@ -280,17 +282,19 @@ int git_diff_find_options_init( return 0; } -static int flush_hunk(git_oid *result, git_hash_ctx *ctx) +static int flush_hunk(git_oid *result, struct patch_id_args *args) { + git_hash_ctx *ctx = &args->ctx; git_oid hash; unsigned short carry = 0; - int error, i; + size_t i; + int error; if ((error = git_hash_final(hash.id, ctx)) < 0 || (error = git_hash_init(ctx)) < 0) return error; - for (i = 0; i < GIT_OID_SHA1_SIZE; i++) { + for (i = 0; i < git_oid_size(args->oid_type); i++) { carry += result->id[i] + hash.id[i]; result->id[i] = (unsigned char)carry; carry >>= 8; @@ -338,7 +342,7 @@ static int diff_patchid_print_callback_to_buf( if (line->origin == GIT_DIFF_LINE_FILE_HDR && !args->first_file && - (error = flush_hunk(&args->result, &args->ctx) < 0)) + (error = flush_hunk(&args->result, args) < 0)) goto out; if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0) @@ -362,14 +366,19 @@ int git_diff_patchid_options_init(git_diff_patchid_options *opts, unsigned int v int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts) { struct patch_id_args args; + git_hash_algorithm_t algorithm; int error; GIT_ERROR_CHECK_VERSION( opts, GIT_DIFF_PATCHID_OPTIONS_VERSION, "git_diff_patchid_options"); + algorithm = git_oid_algorithm(diff->opts.oid_type); + memset(&args, 0, sizeof(args)); + args.diff = diff; args.first_file = 1; - if ((error = git_hash_ctx_init(&args.ctx, GIT_HASH_ALGORITHM_SHA1)) < 0) + args.oid_type = diff->opts.oid_type; + if ((error = git_hash_ctx_init(&args.ctx, algorithm)) < 0) goto out; if ((error = git_diff_print(diff, @@ -378,11 +387,11 @@ int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opt &args)) < 0) goto out; - if ((error = (flush_hunk(&args.result, &args.ctx))) < 0) + if ((error = (flush_hunk(&args.result, &args))) < 0) goto out; #ifdef GIT_EXPERIMENTAL_SHA256 - args.result.type = GIT_OID_SHA1; + args.result.type = diff->opts.oid_type; #endif git_oid_cpy(out, &args.result); diff --git a/src/libgit2/diff.h b/src/libgit2/diff.h index 2cc35e65b..f21b27645 100644 --- a/src/libgit2/diff.h +++ b/src/libgit2/diff.h @@ -30,15 +30,15 @@ typedef enum { } git_diff_origin_t; struct git_diff { - git_refcount rc; + git_refcount rc; git_repository *repo; - git_attr_session attrsession; + git_attr_session attrsession; git_diff_origin_t type; - git_diff_options opts; - git_vector deltas; /* vector of git_diff_delta */ + git_diff_options opts; + git_vector deltas; /* vector of git_diff_delta */ git_pool pool; - git_iterator_t old_src; - git_iterator_t new_src; + git_iterator_t old_src; + git_iterator_t new_src; git_diff_perfdata perf; int (*strcomp)(const char *, const char *); diff --git a/src/libgit2/diff_file.c b/src/libgit2/diff_file.c index c2d08675a..6b7f9590c 100644 --- a/src/libgit2/diff_file.c +++ b/src/libgit2/diff_file.c @@ -144,7 +144,7 @@ int git_diff_file_content__init_from_src( if (!src->blob && !src->buf) { fc->flags |= GIT_DIFF_FLAG__NO_DATA; - git_oid_clear(&fc->file->id, GIT_OID_SHA1); + git_oid_clear(&fc->file->id, opts->oid_type); } else { fc->flags |= GIT_DIFF_FLAG__LOADED; fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; @@ -154,7 +154,7 @@ int git_diff_file_content__init_from_src( git_blob_dup((git_blob **)&fc->blob, (git_blob *) src->blob); fc->file->size = git_blob_rawsize(src->blob); git_oid_cpy(&fc->file->id, git_blob_id(src->blob)); - fc->file->id_abbrev = GIT_OID_SHA1_HEXSIZE; + fc->file->id_abbrev = (uint16_t)git_oid_hexsize(repo->oid_type); fc->map.len = (size_t)fc->file->size; fc->map.data = (char *)git_blob_rawcontent(src->blob); @@ -162,10 +162,10 @@ int git_diff_file_content__init_from_src( fc->flags |= GIT_DIFF_FLAG__FREE_BLOB; } else { int error; - if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0) + if ((error = git_odb__hash(&fc->file->id, src->buf, src->buflen, GIT_OBJECT_BLOB, opts->oid_type)) < 0) return error; fc->file->size = src->buflen; - fc->file->id_abbrev = GIT_OID_SHA1_HEXSIZE; + fc->file->id_abbrev = (uint16_t)git_oid_hexsize(opts->oid_type); fc->map.len = src->buflen; fc->map.data = (char *)src->buf; @@ -178,7 +178,7 @@ int git_diff_file_content__init_from_src( static int diff_file_content_commit_to_str( git_diff_file_content *fc, bool check_status) { - char oid[GIT_OID_SHA1_HEXSIZE+1]; + char oid[GIT_OID_MAX_HEXSIZE+1]; git_str content = GIT_STR_INIT; const char *status = ""; @@ -420,7 +420,7 @@ static int diff_file_content_load_workdir( if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) { error = git_odb__hash( &fc->file->id, fc->map.data, fc->map.len, - GIT_OBJECT_BLOB, GIT_OID_SHA1); + GIT_OBJECT_BLOB, diff_opts->oid_type); fc->file->flags |= GIT_DIFF_FLAG_VALID_ID; } diff --git a/src/libgit2/diff_generate.c b/src/libgit2/diff_generate.c index a88ce8c32..78fe510e7 100644 --- a/src/libgit2/diff_generate.c +++ b/src/libgit2/diff_generate.c @@ -61,8 +61,8 @@ static git_diff_delta *diff_delta__alloc( } delta->status = status; - git_oid_clear(&delta->old_file.id, GIT_OID_SHA1); - git_oid_clear(&delta->new_file.id, GIT_OID_SHA1); + git_oid_clear(&delta->old_file.id, diff->base.opts.oid_type); + git_oid_clear(&delta->new_file.id, diff->base.opts.oid_type); return delta; } @@ -149,10 +149,13 @@ static int diff_delta__from_one( const git_index_entry *entry = nitem; bool has_old = false; git_diff_delta *delta; + git_oid_t oid_type; const char *matched_pathspec; GIT_ASSERT_ARG((oitem != NULL) ^ (nitem != NULL)); + oid_type = diff->base.opts.oid_type; + if (oitem) { entry = oitem; has_old = true; @@ -186,20 +189,23 @@ static int diff_delta__from_one( GIT_ASSERT(status != GIT_DELTA_MODIFIED); delta->nfiles = 1; + git_oid_clear(&delta->old_file.id, diff->base.opts.oid_type); + git_oid_clear(&delta->new_file.id, diff->base.opts.oid_type); + if (has_old) { delta->old_file.mode = entry->mode; delta->old_file.size = entry->file_size; delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; git_oid_cpy(&delta->old_file.id, &entry->id); - git_oid_clear(&delta->new_file.id, GIT_OID_SHA1); - delta->old_file.id_abbrev = GIT_OID_SHA1_HEXSIZE; + git_oid_clear(&delta->new_file.id, oid_type); + delta->old_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); } else /* ADDED, IGNORED, UNTRACKED */ { delta->new_file.mode = entry->mode; delta->new_file.size = entry->file_size; delta->new_file.flags |= GIT_DIFF_FLAG_EXISTS; - git_oid_clear(&delta->old_file.id, GIT_OID_SHA1); + git_oid_clear(&delta->old_file.id, oid_type); git_oid_cpy(&delta->new_file.id, &entry->id); - delta->new_file.id_abbrev = GIT_OID_SHA1_HEXSIZE; + delta->new_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); } delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; @@ -225,6 +231,9 @@ static int diff_delta__from_two( const git_oid *old_id = &old_entry->id; git_diff_delta *delta; const char *canonical_path = old_entry->path; + git_oid_t oid_type; + + oid_type = diff->base.opts.oid_type; if (status == GIT_DELTA_UNMODIFIED && DIFF_FLAG_ISNT_SET(diff, GIT_DIFF_INCLUDE_UNMODIFIED)) @@ -254,14 +263,14 @@ static int diff_delta__from_two( delta->old_file.size = old_entry->file_size; delta->old_file.mode = old_mode; git_oid_cpy(&delta->old_file.id, old_id); - delta->old_file.id_abbrev = GIT_OID_SHA1_HEXSIZE; + delta->old_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID | GIT_DIFF_FLAG_EXISTS; } if (!git_index_entry_is_conflict(new_entry)) { git_oid_cpy(&delta->new_file.id, new_id); - delta->new_file.id_abbrev = GIT_OID_SHA1_HEXSIZE; + delta->new_file.id_abbrev = (uint16_t)git_oid_hexsize(oid_type); delta->new_file.size = new_entry->file_size; delta->new_file.mode = new_mode; delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; @@ -490,6 +499,14 @@ static int diff_generated_apply_options( return -1; } + if (!diff->base.opts.oid_type) { + diff->base.opts.oid_type = repo->oid_type; + } else if (diff->base.opts.oid_type != repo->oid_type) { + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + return -1; + } + /* flag INCLUDE_TYPECHANGE_TREES implies INCLUDE_TYPECHANGE */ if (DIFF_FLAG_IS_SET(diff, GIT_DIFF_INCLUDE_TYPECHANGE_TREES)) diff->base.opts.flags |= GIT_DIFF_INCLUDE_TYPECHANGE; @@ -603,7 +620,7 @@ int git_diff__oid_for_file( entry.mode = mode; entry.file_size = (uint32_t)size; entry.path = (char *)path; - git_oid_clear(&entry.id, GIT_OID_SHA1); + git_oid_clear(&entry.id, diff->opts.oid_type); return git_diff__oid_for_entry(out, diff, &entry, mode, NULL); } @@ -624,7 +641,7 @@ int git_diff__oid_for_entry( GIT_ASSERT(d->type == GIT_DIFF_TYPE_GENERATED); diff = (git_diff_generated *)d; - git_oid_clear(out, GIT_OID_SHA1); + git_oid_clear(out, diff->base.opts.oid_type); if (git_repository_workdir_path(&full_path, diff->base.repo, entry.path) < 0) return -1; @@ -660,7 +677,8 @@ int git_diff__oid_for_entry( git_error_clear(); } } else if (S_ISLNK(mode)) { - error = git_odb__hashlink(out, full_path.ptr, GIT_OID_SHA1); + error = git_odb__hashlink(out, full_path.ptr, + diff->base.opts.oid_type); diff->base.perf.oid_calculations++; } else if (!git__is_sizet(entry.file_size)) { git_error_set(GIT_ERROR_NOMEMORY, "file size overflow (for 32-bits) on '%s'", @@ -676,7 +694,8 @@ int git_diff__oid_for_entry( else { error = git_odb__hashfd_filtered( out, fd, (size_t)entry.file_size, - GIT_OBJECT_BLOB, GIT_OID_SHA1, fl); + GIT_OBJECT_BLOB, diff->base.opts.oid_type, + fl); p_close(fd); diff->base.perf.oid_calculations++; } @@ -785,7 +804,7 @@ static int maybe_modified( git_diff_generated *diff, diff_in_progress *info) { - git_oid noid = GIT_OID_SHA1_ZERO; + git_oid noid; git_delta_t status = GIT_DELTA_MODIFIED; const git_index_entry *oitem = info->oitem; const git_index_entry *nitem = info->nitem; @@ -796,6 +815,8 @@ static int maybe_modified( const char *matched_pathspec; int error = 0; + git_oid_clear(&noid, diff->base.opts.oid_type); + if (!diff_pathspec_match(&matched_pathspec, diff, oitem)) return 0; @@ -1700,11 +1721,11 @@ int git_diff__commit( *out = NULL; if ((parents = git_commit_parentcount(commit)) > 1) { - char commit_oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_oidstr[GIT_OID_MAX_HEXSIZE + 1]; error = -1; git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit", - git_oid_tostr(commit_oidstr, GIT_OID_SHA1_HEXSIZE + 1, git_commit_id(commit))); + git_oid_tostr(commit_oidstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit))); goto on_error; } diff --git a/src/libgit2/diff_parse.c b/src/libgit2/diff_parse.c index 75e41a544..04603969e 100644 --- a/src/libgit2/diff_parse.c +++ b/src/libgit2/diff_parse.c @@ -29,7 +29,7 @@ static void diff_parsed_free(git_diff *d) git__free(diff); } -static git_diff_parsed *diff_parsed_alloc(void) +static git_diff_parsed *diff_parsed_alloc(git_oid_t oid_type) { git_diff_parsed *diff; @@ -51,6 +51,7 @@ static git_diff_parsed *diff_parsed_alloc(void) } diff->base.opts.flags &= ~GIT_DIFF_IGNORE_CASE; + diff->base.opts.oid_type = oid_type; if (git_pool_init(&diff->base.pool, 1) < 0 || git_vector_init(&diff->patches, 0, NULL) < 0 || @@ -67,19 +68,34 @@ static git_diff_parsed *diff_parsed_alloc(void) int git_diff_from_buffer( git_diff **out, const char *content, - size_t content_len) + size_t content_len +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_diff_parse_options *opts +#endif + ) { git_diff_parsed *diff; git_patch *patch; git_patch_parse_ctx *ctx = NULL; + git_patch_options patch_opts = GIT_PATCH_OPTIONS_INIT; + git_oid_t oid_type; int error = 0; *out = NULL; - diff = diff_parsed_alloc(); +#ifdef GIT_EXPERIMENTAL_SHA256 + oid_type = (opts && opts->oid_type) ? opts->oid_type : + GIT_OID_DEFAULT; +#else + oid_type = GIT_OID_DEFAULT; +#endif + + patch_opts.oid_type = oid_type; + + diff = diff_parsed_alloc(oid_type); GIT_ERROR_CHECK_ALLOC(diff); - ctx = git_patch_parse_ctx_init(content, content_len, NULL); + ctx = git_patch_parse_ctx_init(content, content_len, &patch_opts); GIT_ERROR_CHECK_ALLOC(ctx); while (ctx->parse_ctx.remain_len) { diff --git a/src/libgit2/diff_print.c b/src/libgit2/diff_print.c index 3077e11e1..32c936826 100644 --- a/src/libgit2/diff_print.c +++ b/src/libgit2/diff_print.c @@ -29,6 +29,7 @@ typedef struct { const char *new_prefix; uint32_t flags; int id_strlen; + git_oid_t oid_type; int (*strcomp)(const char *, const char *); } diff_print_info; @@ -46,6 +47,8 @@ static int diff_print_info_init__common( pi->payload = payload; pi->buf = out; + GIT_ASSERT(pi->oid_type); + if (!pi->id_strlen) { if (!repo) pi->id_strlen = GIT_ABBREV_DEFAULT; @@ -53,8 +56,9 @@ static int diff_print_info_init__common( return -1; } - if (pi->id_strlen > GIT_OID_SHA1_HEXSIZE) - pi->id_strlen = GIT_OID_SHA1_HEXSIZE; + if (pi->id_strlen > 0 && + (size_t)pi->id_strlen > git_oid_hexsize(pi->oid_type)) + pi->id_strlen = (int)git_oid_hexsize(pi->oid_type); memset(&pi->line, 0, sizeof(pi->line)); pi->line.old_lineno = -1; @@ -78,6 +82,7 @@ static int diff_print_info_init_fromdiff( if (diff) { pi->flags = diff->opts.flags; + pi->oid_type = diff->opts.oid_type; pi->id_strlen = diff->opts.id_abbrev; pi->old_prefix = diff->opts.old_prefix; pi->new_prefix = diff->opts.new_prefix; @@ -101,6 +106,7 @@ static int diff_print_info_init_frompatch( memset(pi, 0, sizeof(diff_print_info)); pi->flags = patch->diff_opts.flags; + pi->oid_type = patch->diff_opts.oid_type; pi->id_strlen = patch->diff_opts.id_abbrev; pi->old_prefix = patch->diff_opts.old_prefix; pi->new_prefix = patch->diff_opts.new_prefix; @@ -212,7 +218,10 @@ static int diff_print_one_raw( git_str *out = pi->buf; int id_abbrev; char code = git_diff_status_char(delta->status); - char start_oid[GIT_OID_SHA1_HEXSIZE+1], end_oid[GIT_OID_SHA1_HEXSIZE+1]; + char start_oid[GIT_OID_MAX_HEXSIZE + 1], + end_oid[GIT_OID_MAX_HEXSIZE + 1]; + size_t oid_hexsize; + bool id_is_abbrev; GIT_UNUSED(progress); @@ -231,12 +240,21 @@ static int diff_print_one_raw( return -1; } +#ifdef GIT_EXPERIMENTAL_SHA256 + GIT_ASSERT(delta->old_file.id.type == delta->new_file.id.type); + oid_hexsize = git_oid_hexsize(delta->old_file.id.type); +#else + oid_hexsize = GIT_OID_SHA1_HEXSIZE; +#endif + + id_is_abbrev = (pi->id_strlen > 0 && + (size_t)pi->id_strlen <= oid_hexsize); + git_oid_tostr(start_oid, pi->id_strlen + 1, &delta->old_file.id); git_oid_tostr(end_oid, pi->id_strlen + 1, &delta->new_file.id); - git_str_printf( - out, (pi->id_strlen <= GIT_OID_SHA1_HEXSIZE) ? - ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c", + git_str_printf(out, + id_is_abbrev ? ":%06o %06o %s... %s... %c" : ":%06o %06o %s %s %c", delta->old_file.mode, delta->new_file.mode, start_oid, end_oid, code); if (delta->similarity > 0) @@ -273,7 +291,8 @@ static int diff_print_oid_range( git_str *out, const git_diff_delta *delta, int id_strlen, bool print_index) { - char start_oid[GIT_OID_SHA1_HEXSIZE+1], end_oid[GIT_OID_SHA1_HEXSIZE+1]; + char start_oid[GIT_OID_MAX_HEXSIZE + 1], + end_oid[GIT_OID_MAX_HEXSIZE + 1]; if (delta->old_file.mode && id_strlen > delta->old_file.id_abbrev) { diff --git a/src/libgit2/diff_tform.c b/src/libgit2/diff_tform.c index 8c0c1b7fc..4a156c7a3 100644 --- a/src/libgit2/diff_tform.c +++ b/src/libgit2/diff_tform.c @@ -364,7 +364,7 @@ static int insert_delete_side_of_split( memset(&deleted->new_file, 0, sizeof(deleted->new_file)); deleted->new_file.path = deleted->old_file.path; deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; - git_oid_clear(&deleted->new_file.id, GIT_OID_SHA1); + git_oid_clear(&deleted->new_file.id, diff->opts.oid_type); return git_vector_insert(onto, deleted); } @@ -398,7 +398,7 @@ static int apply_splits_and_deletes( memset(&delta->old_file, 0, sizeof(delta->old_file)); delta->old_file.path = delta->new_file.path; delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; - git_oid_clear(&delta->old_file.id, GIT_OID_SHA1); + git_oid_clear(&delta->old_file.id, diff->opts.oid_type); } /* clean up delta before inserting into new list */ @@ -997,7 +997,7 @@ find_best_matches: memset(&src->new_file, 0, sizeof(src->new_file)); src->new_file.path = src->old_file.path; src->new_file.flags |= GIT_DIFF_FLAG_VALID_ID; - git_oid_clear(&src->new_file.id, GIT_OID_SHA1); + git_oid_clear(&src->new_file.id, diff->opts.oid_type); num_updates++; @@ -1023,7 +1023,7 @@ find_best_matches: memset(&src->old_file, 0, sizeof(src->old_file)); src->old_file.path = src->new_file.path; src->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; - git_oid_clear(&src->old_file.id, GIT_OID_SHA1); + git_oid_clear(&src->old_file.id, diff->opts.oid_type); src->flags &= ~GIT_DIFF_FLAG__TO_SPLIT; num_rewrites--; diff --git a/src/libgit2/diff_xdiff.h b/src/libgit2/diff_xdiff.h index 9b303e9dc..327dc7c4a 100644 --- a/src/libgit2/diff_xdiff.h +++ b/src/libgit2/diff_xdiff.h @@ -10,7 +10,7 @@ #include "common.h" #include "diff.h" -#include "xdiff/xdiff.h" +#include "xdiff.h" #include "patch_generate.h" /* xdiff cannot cope with large files. these files should not be passed to diff --git a/src/libgit2/email.c b/src/libgit2/email.c index 0a75021c8..8a10a12b7 100644 --- a/src/libgit2/email.c +++ b/src/libgit2/email.c @@ -130,11 +130,12 @@ static int append_header( const git_signature *author, git_email_create_options *opts) { - char id[GIT_OID_SHA1_HEXSIZE]; + char id[GIT_OID_MAX_HEXSIZE + 1]; int error; - if ((error = git_oid_fmt(id, commit_id)) < 0 || - (error = git_str_printf(out, "From %.*s %s\n", GIT_OID_SHA1_HEXSIZE, id, EMAIL_TIMESTAMP)) < 0 || + git_oid_tostr(id, GIT_OID_MAX_HEXSIZE + 1, commit_id); + + if ((error = git_str_printf(out, "From %s %s\n", id, EMAIL_TIMESTAMP)) < 0 || (error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 || (error = append_date(out, &author->when)) < 0 || (error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0) diff --git a/src/libgit2/fetch.c b/src/libgit2/fetch.c index 6bf16bc0f..d66892ca0 100644 --- a/src/libgit2/fetch.c +++ b/src/libgit2/fetch.c @@ -79,7 +79,7 @@ static int maybe_want_oid(git_remote *remote, git_refspec *spec) oid_head = git__calloc(1, sizeof(git_remote_head)); GIT_ERROR_CHECK_ALLOC(oid_head); - git_oid__fromstr(&oid_head->oid, spec->src, GIT_OID_SHA1); + git_oid__fromstr(&oid_head->oid, spec->src, remote->repo->oid_type); if (spec->dst) { oid_head->name = git__strdup(spec->dst); @@ -140,7 +140,7 @@ static int filter_wants(git_remote *remote, const git_fetch_options *opts) /* Handle explicitly specified OID specs */ git_vector_foreach(&remote->active_refspecs, i, spec) { - if (!git_oid__is_hexstr(spec->src, GIT_OID_SHA1)) + if (!git_oid__is_hexstr(spec->src, remote->repo->oid_type)) continue; if (!(remote_caps & oid_mask)) { diff --git a/src/libgit2/fetchhead.c b/src/libgit2/fetchhead.c index 0ebfe5c43..2f276e526 100644 --- a/src/libgit2/fetchhead.c +++ b/src/libgit2/fetchhead.c @@ -105,15 +105,14 @@ static int fetchhead_ref_write( git_filebuf *file, git_fetchhead_ref *fetchhead_ref) { - char oid[GIT_OID_SHA1_HEXSIZE + 1]; + char oid[GIT_OID_MAX_HEXSIZE + 1]; const char *type, *name; int head = 0; GIT_ASSERT_ARG(file); GIT_ASSERT_ARG(fetchhead_ref); - git_oid_fmt(oid, &fetchhead_ref->oid); - oid[GIT_OID_SHA1_HEXSIZE] = '\0'; + git_oid_tostr(oid, GIT_OID_MAX_HEXSIZE + 1, &fetchhead_ref->oid); if (git__prefixcmp(fetchhead_ref->ref_name, GIT_REFS_HEADS_DIR) == 0) { type = "branch "; @@ -174,7 +173,8 @@ static int fetchhead_ref_parse( git_str *ref_name, const char **remote_url, char *line, - size_t line_num) + size_t line_num, + git_oid_t oid_type) { char *oid_str, *is_merge_str, *desc, *name = NULL; const char *type = NULL; @@ -196,13 +196,13 @@ static int fetchhead_ref_parse( *is_merge = 1; } - if (strlen(oid_str) != GIT_OID_SHA1_HEXSIZE) { + if (strlen(oid_str) != git_oid_hexsize(oid_type)) { git_error_set(GIT_ERROR_FETCHHEAD, "invalid object ID in FETCH_HEAD line %"PRIuZ, line_num); return -1; } - if (git_oid__fromstr(oid, oid_str, GIT_OID_SHA1) < 0) { + if (git_oid__fromstr(oid, oid_str, oid_type) < 0) { const git_error *oid_err = git_error_last(); const char *err_msg = oid_err ? oid_err->message : "invalid object ID"; @@ -269,7 +269,8 @@ static int fetchhead_ref_parse( return error; } -int git_repository_fetchhead_foreach(git_repository *repo, +int git_repository_fetchhead_foreach( + git_repository *repo, git_repository_fetchhead_foreach_cb cb, void *payload) { @@ -296,8 +297,9 @@ int git_repository_fetchhead_foreach(git_repository *repo, while ((line = git__strsep(&buffer, "\n")) != NULL) { ++line_num; - if ((error = fetchhead_ref_parse( - &oid, &is_merge, &name, &remote_url, line, line_num)) < 0) + if ((error = fetchhead_ref_parse(&oid, &is_merge, &name, + &remote_url, line, line_num, + repo->oid_type)) < 0) goto done; if (git_str_len(&name) > 0) diff --git a/src/libgit2/ident.c b/src/libgit2/ident.c index bf9a4998e..97110c664 100644 --- a/src/libgit2/ident.c +++ b/src/libgit2/ident.c @@ -42,7 +42,7 @@ static int ident_find_id( static int ident_insert_id( git_str *to, const git_str *from, const git_filter_source *src) { - char oid[GIT_OID_SHA1_HEXSIZE+1]; + char oid[GIT_OID_MAX_HEXSIZE + 1]; const char *id_start, *id_end, *from_end = from->ptr + from->size; size_t need_size; @@ -57,7 +57,7 @@ static int ident_insert_id( return GIT_PASSTHROUGH; need_size = (size_t)(id_start - from->ptr) + - 5 /* "$Id: " */ + GIT_OID_SHA1_HEXSIZE + 2 /* " $" */ + + 5 /* "$Id: " */ + GIT_OID_MAX_HEXSIZE + 2 /* " $" */ + (size_t)(from_end - id_end); if (git_str_grow(to, need_size) < 0) @@ -65,7 +65,7 @@ static int ident_insert_id( git_str_set(to, from->ptr, (size_t)(id_start - from->ptr)); git_str_put(to, "$Id: ", 5); - git_str_put(to, oid, GIT_OID_SHA1_HEXSIZE); + git_str_puts(to, oid); git_str_put(to, " $", 2); git_str_put(to, id_end, (size_t)(from_end - id_end)); diff --git a/src/libgit2/index.c b/src/libgit2/index.c index d4532c005..9d919093b 100644 --- a/src/libgit2/index.c +++ b/src/libgit2/index.c @@ -32,8 +32,6 @@ static int index_apply_to_wd_diff(git_index *index, int action, const git_strarr unsigned int flags, git_index_matched_path_cb cb, void *payload); -#define minimal_entry_size (offsetof(struct entry_short, path)) - static const size_t INDEX_HEADER_SIZE = 12; static const unsigned int INDEX_VERSION_NUMBER_DEFAULT = 2; @@ -65,7 +63,7 @@ struct entry_time { uint32_t nanoseconds; }; -struct entry_short { +struct entry_common { struct entry_time ctime; struct entry_time mtime; uint32_t dev; @@ -74,25 +72,35 @@ struct entry_short { uint32_t uid; uint32_t gid; uint32_t file_size; - unsigned char oid[GIT_OID_SHA1_SIZE]; - uint16_t flags; - char path[1]; /* arbitrary length */ }; -struct entry_long { - struct entry_time ctime; - struct entry_time mtime; - uint32_t dev; - uint32_t ino; - uint32_t mode; - uint32_t uid; - uint32_t gid; - uint32_t file_size; - unsigned char oid[GIT_OID_SHA1_SIZE]; - uint16_t flags; - uint16_t flags_extended; - char path[1]; /* arbitrary length */ -}; +#define entry_short(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + char path[1]; /* arbitrary length */ \ + } + +#define entry_long(oid_size) \ + struct { \ + struct entry_common common; \ + unsigned char oid[oid_size]; \ + uint16_t flags; \ + uint16_t flags_extended; \ + char path[1]; /* arbitrary length */ \ + } + +typedef entry_short(GIT_OID_SHA1_SIZE) index_entry_short_sha1; +typedef entry_long(GIT_OID_SHA1_SIZE) index_entry_long_sha1; + +#ifdef GIT_EXPERIMENTAL_SHA256 +typedef entry_short(GIT_OID_SHA256_SIZE) index_entry_short_sha256; +typedef entry_long(GIT_OID_SHA256_SIZE) index_entry_long_sha256; +#endif + +#undef entry_short +#undef entry_long struct entry_srch_key { const char *path; @@ -115,12 +123,12 @@ struct reuc_entry_internal { bool git_index__enforce_unsaved_safety = false; /* local declarations */ -static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size); +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size); static int read_header(struct index_header *dest, const void *buffer); static int parse_index(git_index *index, const char *buffer, size_t buffer_size); static bool is_index_extended(git_index *index); -static int write_index(unsigned char checksum[GIT_HASH_SHA1_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file); +static int write_index(unsigned char checksum[GIT_HASH_MAX_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file); static void index_entry_free(git_index_entry *entry); static void index_entry_reuc_free(git_index_reuc_entry *reuc); @@ -401,7 +409,10 @@ void git_index__set_ignore_case(git_index *index, bool ignore_case) git_vector_sort(&index->reuc); } -int git_index_open(git_index **index_out, const char *index_path) +int git_index__open( + git_index **index_out, + const char *index_path, + git_oid_t oid_type) { git_index *index; int error = -1; @@ -411,6 +422,8 @@ int git_index_open(git_index **index_out, const char *index_path) index = git__calloc(1, sizeof(git_index)); GIT_ERROR_CHECK_ALLOC(index); + index->oid_type = oid_type; + if (git_pool_init(&index->tree_pool, 1) < 0) goto fail; @@ -451,10 +464,34 @@ fail: return error; } +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_index_open(git_index **index_out, const char *index_path, git_oid_t oid_type) +{ + return git_index__open(index_out, index_path, oid_type); +} +#else +int git_index_open(git_index **index_out, const char *index_path) +{ + return git_index__open(index_out, index_path, GIT_OID_SHA1); +} +#endif + +int git_index__new(git_index **out, git_oid_t oid_type) +{ + return git_index__open(out, NULL, oid_type); +} + +#ifdef GIT_EXPERIMENTAL_SHA256 +int git_index_new(git_index **out, git_oid_t oid_type) +{ + return git_index__new(out, oid_type); +} +#else int git_index_new(git_index **out) { - return git_index_open(out, NULL); + return git_index__new(out, GIT_OID_SHA1); } +#endif static void index_free(git_index *index) { @@ -620,8 +657,8 @@ static int compare_checksum(git_index *index) { int fd; ssize_t bytes_read; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_oid_size(index->oid_type); if ((fd = p_open(index->index_file_path, O_RDONLY)) < 0) return fd; @@ -2306,6 +2343,7 @@ static int index_error_invalid(const char *message) static int read_reuc(git_index *index, const char *buffer, size_t size) { const char *endptr; + size_t oid_size = git_oid_size(index->oid_type); size_t len; int i; @@ -2354,16 +2392,16 @@ static int read_reuc(git_index *index, const char *buffer, size_t size) for (i = 0; i < 3; i++) { if (!lost->mode[i]) continue; - if (size < GIT_OID_SHA1_SIZE) { + if (size < oid_size) { index_entry_reuc_free(lost); return index_error_invalid("reading reuc entry oid"); } - if (git_oid__fromraw(&lost->oid[i], (const unsigned char *) buffer, GIT_OID_SHA1) < 0) + if (git_oid__fromraw(&lost->oid[i], (const unsigned char *) buffer, index->oid_type) < 0) return -1; - size -= GIT_OID_SHA1_SIZE; - buffer += GIT_OID_SHA1_SIZE; + size -= oid_size; + buffer += oid_size; } /* entry was read successfully - insert into reuc vector */ @@ -2433,73 +2471,157 @@ out_err: return 0; } -static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags) +GIT_INLINE(size_t) index_entry_path_offset( + git_oid_t oid_type, + uint32_t flags) +{ + if (oid_type == GIT_OID_SHA1) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha1, path) : + offsetof(index_entry_short_sha1, path); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return (flags & GIT_INDEX_ENTRY_EXTENDED) ? + offsetof(index_entry_long_sha256, path) : + offsetof(index_entry_short_sha256, path); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +GIT_INLINE(size_t) index_entry_flags_offset(git_oid_t oid_type) { + if (oid_type == GIT_OID_SHA1) + return offsetof(index_entry_long_sha1, flags_extended); + +#ifdef GIT_EXPERIMENTAL_SHA256 + else if (oid_type == GIT_OID_SHA256) + return offsetof(index_entry_long_sha256, flags_extended); +#endif + + git_error_set(GIT_ERROR_INTERNAL, "invalid oid type"); + return 0; +} + +static size_t index_entry_size( + size_t path_len, + size_t varint_len, + git_oid_t oid_type, + uint32_t flags) +{ + size_t offset, size; + + if (!(offset = index_entry_path_offset(oid_type, flags))) + return 0; + if (varint_len) { - if (flags & GIT_INDEX_ENTRY_EXTENDED) - return offsetof(struct entry_long, path) + path_len + 1 + varint_len; - else - return offsetof(struct entry_short, path) + path_len + 1 + varint_len; + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 1) || + GIT_ADD_SIZET_OVERFLOW(&size, size, varint_len)) + return 0; } else { -#define entry_size(type,len) ((offsetof(type, path) + (len) + 8) & ~7) - if (flags & GIT_INDEX_ENTRY_EXTENDED) - return entry_size(struct entry_long, path_len); - else - return entry_size(struct entry_short, path_len); -#undef entry_size + if (GIT_ADD_SIZET_OVERFLOW(&size, offset, path_len) || + GIT_ADD_SIZET_OVERFLOW(&size, size, 8)) + return 0; + + size &= ~7; } + + return size; } static int read_entry( git_index_entry **out, size_t *out_size, git_index *index, + size_t checksum_size, const void *buffer, size_t buffer_size, const char *last) { - size_t path_length, entry_size; + size_t path_length, path_offset, entry_size; const char *path_ptr; - struct entry_short source; + struct entry_common *source_common; + index_entry_short_sha1 source_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 source_sha256; +#endif git_index_entry entry = {{0}}; bool compressed = index->version >= INDEX_VERSION_NUMBER_COMP; char *tmp_path = NULL; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + + size_t minimal_entry_size = index_entry_path_offset(index->oid_type, 0); if (checksum_size + minimal_entry_size > buffer_size) return -1; /* buffer is not guaranteed to be aligned */ - memcpy(&source, buffer, sizeof(struct entry_short)); - - entry.ctime.seconds = (git_time_t)ntohl(source.ctime.seconds); - entry.ctime.nanoseconds = ntohl(source.ctime.nanoseconds); - entry.mtime.seconds = (git_time_t)ntohl(source.mtime.seconds); - entry.mtime.nanoseconds = ntohl(source.mtime.nanoseconds); - entry.dev = ntohl(source.dev); - entry.ino = ntohl(source.ino); - entry.mode = ntohl(source.mode); - entry.uid = ntohl(source.uid); - entry.gid = ntohl(source.gid); - entry.file_size = ntohl(source.file_size); - entry.flags = ntohs(source.flags); - - if (git_oid__fromraw(&entry.id, source.oid, GIT_OID_SHA1) < 0) + switch (index->oid_type) { + case GIT_OID_SHA1: + source_common = &source_sha1.common; + memcpy(&source_sha1, buffer, sizeof(source_sha1)); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + source_common = &source_sha256.common; + memcpy(&source_sha256, buffer, sizeof(source_sha256)); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + entry.ctime.seconds = (git_time_t)ntohl(source_common->ctime.seconds); + entry.ctime.nanoseconds = ntohl(source_common->ctime.nanoseconds); + entry.mtime.seconds = (git_time_t)ntohl(source_common->mtime.seconds); + entry.mtime.nanoseconds = ntohl(source_common->mtime.nanoseconds); + entry.dev = ntohl(source_common->dev); + entry.ino = ntohl(source_common->ino); + entry.mode = ntohl(source_common->mode); + entry.uid = ntohl(source_common->uid); + entry.gid = ntohl(source_common->gid); + entry.file_size = ntohl(source_common->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + if (git_oid__fromraw(&entry.id, source_sha1.oid, + GIT_OID_SHA1) < 0) + return -1; + entry.flags = ntohs(source_sha1.flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + if (git_oid__fromraw(&entry.id, source_sha256.oid, + GIT_OID_SHA256) < 0) + return -1; + entry.flags = ntohs(source_sha256.flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + if (!(path_offset = index_entry_path_offset(index->oid_type, entry.flags))) return -1; + if (entry.flags & GIT_INDEX_ENTRY_EXTENDED) { uint16_t flags_raw; size_t flags_offset; - flags_offset = offsetof(struct entry_long, flags_extended); - memcpy(&flags_raw, (const char *) buffer + flags_offset, - sizeof(flags_raw)); + if (!(flags_offset = index_entry_flags_offset(index->oid_type))) + return -1; + + memcpy(&flags_raw, (const char *)buffer + flags_offset, sizeof(flags_raw)); flags_raw = ntohs(flags_raw); memcpy(&entry.flags_extended, &flags_raw, sizeof(flags_raw)); - path_ptr = (const char *) buffer + offsetof(struct entry_long, path); - } else - path_ptr = (const char *) buffer + offsetof(struct entry_short, path); + path_ptr = (const char *)buffer + path_offset; + } else { + path_ptr = (const char *)buffer + path_offset; + } if (!compressed) { path_length = entry.flags & GIT_INDEX_ENTRY_NAMEMASK; @@ -2511,12 +2633,12 @@ static int read_entry( path_end = memchr(path_ptr, '\0', buffer_size); if (path_end == NULL) - return -1; + return index_error_invalid("invalid path name"); path_length = path_end - path_ptr; } - entry_size = index_entry_size(path_length, 0, entry.flags); + entry_size = index_entry_size(path_length, 0, index->oid_type, entry.flags); entry.path = (char *)path_ptr; } else { size_t varint_len, last_len, prefix_len, suffix_len, path_len; @@ -2542,15 +2664,18 @@ static int read_entry( memcpy(tmp_path, last, prefix_len); memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1); - entry_size = index_entry_size(suffix_len, varint_len, entry.flags); + + entry_size = index_entry_size(suffix_len, varint_len, index->oid_type, entry.flags); entry.path = tmp_path; } if (entry_size == 0) return -1; - if (checksum_size + entry_size > buffer_size) + if (checksum_size + entry_size > buffer_size) { + git_error_set(GIT_ERROR_INTERNAL, "invalid index checksum"); return -1; + } if (index_entry_dup(out, index, &entry) < 0) { git__free(tmp_path); @@ -2579,11 +2704,10 @@ static int read_header(struct index_header *dest, const void *buffer) return 0; } -static int read_extension(size_t *read_len, git_index *index, const char *buffer, size_t buffer_size) +static int read_extension(size_t *read_len, git_index *index, size_t checksum_size, const char *buffer, size_t buffer_size) { struct index_extension dest; size_t total_size; - size_t checksum_size = GIT_HASH_SHA1_SIZE; /* buffer is not guaranteed to be aligned */ memcpy(&dest, buffer, sizeof(struct index_extension)); @@ -2602,7 +2726,7 @@ static int read_extension(size_t *read_len, git_index *index, const char *buffer if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') { /* tree cache */ if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) { - if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, &index->tree_pool) < 0) + if (git_tree_cache_read(&index->tree, buffer + 8, dest.extension_size, index->oid_type, &index->tree_pool) < 0) return -1; } else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) { if (read_reuc(index, buffer + 8, dest.extension_size) < 0) @@ -2630,8 +2754,8 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) int error = 0; unsigned int i; struct index_header header = { 0 }; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size = GIT_HASH_SHA1_SIZE; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); const char *last = NULL; const char *empty = ""; @@ -2646,9 +2770,12 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) if (buffer_size < INDEX_HEADER_SIZE + checksum_size) return index_error_invalid("insufficient buffer space"); - /* Precalculate the SHA1 of the files's contents -- we'll match it to - * the provided SHA1 in the footer */ - git_hash_buf(checksum, buffer, buffer_size - checksum_size, GIT_HASH_ALGORITHM_SHA1); + /* + * Precalculate the hash of the files's contents -- we'll match + * it to the provided checksum in the footer. + */ + git_hash_buf(checksum, buffer, buffer_size - checksum_size, + git_oid_algorithm(index->oid_type)); /* Parse header */ if ((error = read_header(&header, buffer)) < 0) @@ -2670,7 +2797,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) git_index_entry *entry = NULL; size_t entry_size; - if ((error = read_entry(&entry, &entry_size, index, buffer, buffer_size, last)) < 0) { + if ((error = read_entry(&entry, &entry_size, index, checksum_size, buffer, buffer_size, last)) < 0) { error = index_error_invalid("invalid entry"); goto done; } @@ -2701,7 +2828,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) while (buffer_size > checksum_size) { size_t extension_size; - if ((error = read_extension(&extension_size, index, buffer, buffer_size)) < 0) { + if ((error = read_extension(&extension_size, index, checksum_size, buffer, buffer_size)) < 0) { goto done; } @@ -2714,7 +2841,10 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size) goto done; } - /* 160-bit SHA-1 over the content of the index file before this checksum. */ + /* + * SHA-1 or SHA-256 (depending on the repository's object format) + * over the content of the index file before this checksum. + */ if (memcmp(checksum, buffer, checksum_size) != 0) { error = index_error_invalid( "calculated checksum does not match expected"); @@ -2754,16 +2884,40 @@ static bool is_index_extended(git_index *index) return (extended > 0); } -static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const char *last) +static int write_disk_entry( + git_index *index, + git_filebuf *file, + git_index_entry *entry, + const char *last) { void *mem = NULL; - struct entry_short ondisk; - size_t path_len, disk_size; + struct entry_common *ondisk_common; + size_t path_len, path_offset, disk_size; int varint_len = 0; char *path; const char *path_start = entry->path; size_t same_len = 0; + index_entry_short_sha1 ondisk_sha1; + index_entry_long_sha1 ondisk_ext_sha1; +#ifdef GIT_EXPERIMENTAL_SHA256 + index_entry_short_sha256 ondisk_sha256; + index_entry_long_sha256 ondisk_ext_sha256; +#endif + + switch (index->oid_type) { + case GIT_OID_SHA1: + ondisk_common = &ondisk_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + ondisk_common = &ondisk_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + path_len = ((struct entry_internal *)entry)->pathlen; if (last) { @@ -2780,9 +2934,9 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha varint_len = git_encode_varint(NULL, 0, strlen(last) - same_len); } - disk_size = index_entry_size(path_len, varint_len, entry->flags); + disk_size = index_entry_size(path_len, varint_len, index->oid_type, entry->flags); - if (git_filebuf_reserve(file, &mem, disk_size) < 0) + if (!disk_size || git_filebuf_reserve(file, &mem, disk_size) < 0) return -1; memset(mem, 0x0, disk_size); @@ -2797,35 +2951,77 @@ static int write_disk_entry(git_filebuf *file, git_index_entry *entry, const cha * * In 2038 I will be either too dead or too rich to care about this */ - ondisk.ctime.seconds = htonl((uint32_t)entry->ctime.seconds); - ondisk.mtime.seconds = htonl((uint32_t)entry->mtime.seconds); - ondisk.ctime.nanoseconds = htonl(entry->ctime.nanoseconds); - ondisk.mtime.nanoseconds = htonl(entry->mtime.nanoseconds); - ondisk.dev = htonl(entry->dev); - ondisk.ino = htonl(entry->ino); - ondisk.mode = htonl(entry->mode); - ondisk.uid = htonl(entry->uid); - ondisk.gid = htonl(entry->gid); - ondisk.file_size = htonl((uint32_t)entry->file_size); - git_oid_raw_cpy(ondisk.oid, entry->id.id, GIT_OID_SHA1_SIZE); - ondisk.flags = htons(entry->flags); + ondisk_common->ctime.seconds = htonl((uint32_t)entry->ctime.seconds); + ondisk_common->mtime.seconds = htonl((uint32_t)entry->mtime.seconds); + ondisk_common->ctime.nanoseconds = htonl(entry->ctime.nanoseconds); + ondisk_common->mtime.nanoseconds = htonl(entry->mtime.nanoseconds); + ondisk_common->dev = htonl(entry->dev); + ondisk_common->ino = htonl(entry->ino); + ondisk_common->mode = htonl(entry->mode); + ondisk_common->uid = htonl(entry->uid); + ondisk_common->gid = htonl(entry->gid); + ondisk_common->file_size = htonl((uint32_t)entry->file_size); + + switch (index->oid_type) { + case GIT_OID_SHA1: + git_oid_raw_cpy(ondisk_sha1.oid, entry->id.id, GIT_OID_SHA1_SIZE); + ondisk_sha1.flags = htons(entry->flags); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + git_oid_raw_cpy(ondisk_sha256.oid, entry->id.id, GIT_OID_SHA256_SIZE); + ondisk_sha256.flags = htons(entry->flags); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + path_offset = index_entry_path_offset(index->oid_type, entry->flags); if (entry->flags & GIT_INDEX_ENTRY_EXTENDED) { - const size_t path_offset = offsetof(struct entry_long, path); - struct entry_long ondisk_ext; - memcpy(&ondisk_ext, &ondisk, sizeof(struct entry_short)); - ondisk_ext.flags_extended = htons(entry->flags_extended & + struct entry_common *ondisk_ext; + uint16_t flags_extended = htons(entry->flags_extended & GIT_INDEX_ENTRY_EXTENDED_FLAGS); - memcpy(mem, &ondisk_ext, path_offset); - path = (char *)mem + path_offset; - disk_size -= path_offset; + + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(&ondisk_ext_sha1, &ondisk_sha1, + sizeof(index_entry_short_sha1)); + ondisk_ext_sha1.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha1.common; + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(&ondisk_ext_sha256, &ondisk_sha256, + sizeof(index_entry_short_sha256)); + ondisk_ext_sha256.flags_extended = flags_extended; + ondisk_ext = &ondisk_ext_sha256.common; + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } + + memcpy(mem, ondisk_ext, path_offset); } else { - const size_t path_offset = offsetof(struct entry_short, path); - memcpy(mem, &ondisk, path_offset); - path = (char *)mem + path_offset; - disk_size -= path_offset; + switch (index->oid_type) { + case GIT_OID_SHA1: + memcpy(mem, &ondisk_sha1, path_offset); + break; +#ifdef GIT_EXPERIMENTAL_SHA256 + case GIT_OID_SHA256: + memcpy(mem, &ondisk_sha256, path_offset); + break; +#endif + default: + GIT_ASSERT(!"invalid oid type"); + } } + path = (char *)mem + path_offset; + disk_size -= path_offset; + if (last) { varint_len = git_encode_varint((unsigned char *) path, disk_size, strlen(last) - same_len); @@ -2877,7 +3073,7 @@ static int write_entries(git_index *index, git_filebuf *file) last = ""; git_vector_foreach(entries, i, entry) { - if ((error = write_disk_entry(file, entry, last)) < 0) + if ((error = write_disk_entry(index, file, entry, last)) < 0) break; if (index->version >= INDEX_VERSION_NUMBER_COMP) last = entry->path; @@ -2955,8 +3151,9 @@ done: return error; } -static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *reuc) +static int create_reuc_extension_data(git_str *reuc_buf, git_index *index, git_index_reuc_entry *reuc) { + size_t oid_size = git_oid_size(index->oid_type); int i; int error = 0; @@ -2970,7 +3167,7 @@ static int create_reuc_extension_data(git_str *reuc_buf, git_index_reuc_entry *r } for (i = 0; i < 3; i++) { - if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, GIT_OID_SHA1_SIZE)) < 0) + if (reuc->mode[i] && (error = git_str_put(reuc_buf, (char *)&reuc->oid[i].id, oid_size)) < 0) return error; } @@ -2987,7 +3184,7 @@ static int write_reuc_extension(git_index *index, git_filebuf *file) int error = 0; git_vector_foreach(out, i, reuc) { - if ((error = create_reuc_extension_data(&reuc_buf, reuc)) < 0) + if ((error = create_reuc_extension_data(&reuc_buf, index, reuc)) < 0) goto done; } @@ -3036,7 +3233,7 @@ static void clear_uptodate(git_index *index) } static int write_index( - unsigned char checksum[GIT_HASH_SHA1_SIZE], + unsigned char checksum[GIT_HASH_MAX_SIZE], size_t *checksum_size, git_index *index, git_filebuf *file) @@ -3048,7 +3245,9 @@ static int write_index( GIT_ASSERT_ARG(index); GIT_ASSERT_ARG(file); - *checksum_size = GIT_HASH_SHA1_SIZE; + GIT_ASSERT(index->oid_type); + + *checksum_size = git_hash_size(git_oid_algorithm(index->oid_type)); if (index->version <= INDEX_VERSION_NUMBER_EXT) { is_extended = is_index_extended(index); @@ -3209,7 +3408,7 @@ cleanup: if (error < 0) return error; - error = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); + error = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); return error; } @@ -3668,19 +3867,23 @@ int git_indexwriter_init( git_indexwriter *writer, git_index *index) { - int error; + int filebuf_hash, error; GIT_REFCOUNT_INC(index); writer->index = index; + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(index->oid_type)); + GIT_ASSERT(filebuf_hash); + if (!index->index_file_path) return create_index_error(-1, "failed to write index: The index is in-memory only"); - if ((error = git_filebuf_open( - &writer->file, index->index_file_path, GIT_FILEBUF_HASH_CONTENTS, GIT_INDEX_FILE_MODE)) < 0) { - + if ((error = git_filebuf_open(&writer->file, + index->index_file_path, + git_filebuf_hash_flags(filebuf_hash), + GIT_INDEX_FILE_MODE)) < 0) { if (error == GIT_ELOCKED) git_error_set(GIT_ERROR_INDEX, "the index is locked; this might be due to a concurrent or crashed process"); @@ -3712,7 +3915,7 @@ int git_indexwriter_init_for_operation( int git_indexwriter_commit(git_indexwriter *writer) { - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_MAX_SIZE]; size_t checksum_size; int error; diff --git a/src/libgit2/index.h b/src/libgit2/index.h index 71bb096f7..53c29977d 100644 --- a/src/libgit2/index.h +++ b/src/libgit2/index.h @@ -27,7 +27,7 @@ struct git_index { char *index_file_path; git_futils_filestamp stamp; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_MAX_SIZE]; git_vector entries; git_idxmap *entries_map; @@ -35,6 +35,8 @@ struct git_index { git_vector deleted; /* deleted entries if readers > 0 */ git_atomic32 readers; /* number of active iterators */ + git_oid_t oid_type; + unsigned int on_disk:1; unsigned int ignore_case:1; unsigned int distrust_filemode:1; @@ -141,6 +143,17 @@ GIT_INLINE(unsigned char *) git_index__checksum(git_index *index) return index->checksum; } +/* SHA256-aware internal functions */ + +extern int git_index__new( + git_index **index_out, + git_oid_t oid_type); + +extern int git_index__open( + git_index **index_out, + const char *index_path, + git_oid_t oid_type); + /* Copy the current entries vector *and* increment the index refcount. * Call `git_index__release_snapshot` when done. */ diff --git a/src/libgit2/indexer.c b/src/libgit2/indexer.c index fa55fb5ea..7357a4aa5 100644 --- a/src/libgit2/indexer.c +++ b/src/libgit2/indexer.c @@ -1232,6 +1232,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) git_filebuf index_file = {0}; void *packfile_trailer; size_t checksum_size; + int filebuf_hash; bool mismatch; if (!idx->parsed_header) { @@ -1240,6 +1241,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) } checksum_size = git_hash_size(indexer_hash_algorithm(idx)); + filebuf_hash = git_filebuf_hash_flags(indexer_hash_algorithm(idx)); GIT_ASSERT(checksum_size); /* Test for this before resolve_deltas(), as it plays with idx->off */ @@ -1314,8 +1316,7 @@ int git_indexer_commit(git_indexer *idx, git_indexer_progress *stats) return -1; if (git_filebuf_open(&index_file, filename.ptr, - GIT_FILEBUF_HASH_CONTENTS | - (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), + filebuf_hash | (idx->do_fsync ? GIT_FILEBUF_FSYNC : 0), idx->mode) < 0) goto on_error; diff --git a/src/libgit2/iterator.c b/src/libgit2/iterator.c index 1ee8e25f5..95ded1046 100644 --- a/src/libgit2/iterator.c +++ b/src/libgit2/iterator.c @@ -1036,6 +1036,8 @@ typedef struct { git_index *index; git_vector index_snapshot; + git_oid_t oid_type; + git_array_t(filesystem_iterator_frame) frames; git_ignores ignores; @@ -1271,7 +1273,7 @@ static int filesystem_iterator_entry_hash( int error; if (S_ISDIR(entry->st.st_mode)) { - memset(&entry->id, 0, GIT_OID_SHA1_SIZE); + memset(&entry->id, 0, git_oid_size(iter->oid_type)); return 0; } @@ -1281,7 +1283,7 @@ static int filesystem_iterator_entry_hash( if (!(error = git_str_joinpath(&fullpath, iter->root, entry->path)) && !(error = git_path_validate_str_length(iter->base.repo, &fullpath))) - error = git_odb__hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, GIT_OID_SHA1); + error = git_odb__hashfile(&entry->id, fullpath.ptr, GIT_OBJECT_BLOB, iter->oid_type); git_str_dispose(&fullpath); return error; @@ -1530,7 +1532,7 @@ static void filesystem_iterator_set_current( if (iter->base.flags & GIT_ITERATOR_INCLUDE_HASH) git_oid_cpy(&iter->entry.id, &entry->id); else - git_oid_clear(&iter->entry.id, GIT_OID_SHA1); + git_oid_clear(&iter->entry.id, iter->oid_type); iter->entry.path = entry->path; @@ -1975,6 +1977,8 @@ static int iterator_for_filesystem( (iterator__flag(&iter->base, PRECOMPOSE_UNICODE) ? GIT_FS_PATH_DIR_PRECOMPOSE_UNICODE : 0); + iter->oid_type = options->oid_type; + if ((error = filesystem_iterator_init(iter)) < 0) goto on_error; @@ -1989,10 +1993,15 @@ on_error: int git_iterator_for_filesystem( git_iterator **out, const char *root, - git_iterator_options *options) + git_iterator_options *given_opts) { + git_iterator_options options = GIT_ITERATOR_OPTIONS_INIT; + + if (given_opts) + memcpy(&options, given_opts, sizeof(git_iterator_options)); + return iterator_for_filesystem(out, - NULL, root, NULL, NULL, GIT_ITERATOR_FS, options); + NULL, root, NULL, NULL, GIT_ITERATOR_FS, &options); } int git_iterator_for_workdir_ext( @@ -2019,6 +2028,12 @@ int git_iterator_for_workdir_ext( options.flags |= GIT_ITERATOR_HONOR_IGNORES | GIT_ITERATOR_IGNORE_DOT_GIT; + if (!options.oid_type) + options.oid_type = repo->oid_type; + else if (options.oid_type != repo->oid_type) + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + return iterator_for_filesystem(out, repo, repo_workdir, index, tree, GIT_ITERATOR_WORKDIR, &options); } diff --git a/src/libgit2/iterator.h b/src/libgit2/iterator.h index 6bb8489d0..7963ce7e2 100644 --- a/src/libgit2/iterator.h +++ b/src/libgit2/iterator.h @@ -63,6 +63,9 @@ typedef struct { /* flags, from above */ unsigned int flags; + + /* oid type - necessary for non-workdir filesystem iterators */ + git_oid_t oid_type; } git_iterator_options; #define GIT_ITERATOR_OPTIONS_INIT {0} diff --git a/src/libgit2/libgit2.c b/src/libgit2/libgit2.c index 65f19e3ca..c5a9287fb 100644 --- a/src/libgit2/libgit2.c +++ b/src/libgit2/libgit2.c @@ -31,6 +31,7 @@ #include "streams/registry.h" #include "streams/mbedtls.h" #include "streams/openssl.h" +#include "streams/socket.h" #include "transports/smart.h" #include "transports/http.h" #include "transports/ssh.h" @@ -79,6 +80,7 @@ int git_libgit2_init(void) git_merge_driver_global_init, git_transport_ssh_global_init, git_stream_registry_global_init, + git_socket_stream_global_init, git_openssl_stream_global_init, git_mbedtls_stream_global_init, git_mwindow_global_init, diff --git a/src/libgit2/merge.c b/src/libgit2/merge.c index df2cefb29..0114e4b75 100644 --- a/src/libgit2/merge.c +++ b/src/libgit2/merge.c @@ -611,13 +611,13 @@ int git_repository_mergehead_foreach( buffer = merge_head_file.ptr; while ((line = git__strsep(&buffer, "\n")) != NULL) { - if (strlen(line) != GIT_OID_SHA1_HEXSIZE) { + if (strlen(line) != git_oid_hexsize(repo->oid_type)) { git_error_set(GIT_ERROR_INVALID, "unable to parse OID - invalid length"); error = -1; goto cleanup; } - if ((error = git_oid__fromstr(&oid, line, GIT_OID_SHA1)) < 0) + if ((error = git_oid__fromstr(&oid, line, repo->oid_type)) < 0) goto cleanup; if ((error = cb(&oid, payload)) != 0) { @@ -1061,7 +1061,7 @@ static int index_entry_similarity_calc( const git_merge_options *opts) { git_blob *blob; - git_diff_file diff_file = { GIT_OID_SHA1_ZERO }; + git_diff_file diff_file; git_object_size_t blobsize; int error; @@ -1070,6 +1070,8 @@ static int index_entry_similarity_calc( *out = NULL; + git_oid_clear(&diff_file.id, repo->oid_type); + if ((error = git_blob_lookup(&blob, repo, &entry->id)) < 0) return error; @@ -1997,8 +1999,11 @@ static int index_update_reuc(git_index *index, git_merge_diff_list *diff_list) return 0; } -static int index_from_diff_list(git_index **out, - git_merge_diff_list *diff_list, bool skip_reuc) +static int index_from_diff_list( + git_index **out, + git_merge_diff_list *diff_list, + git_oid_t oid_type, + bool skip_reuc) { git_index *index; size_t i; @@ -2007,7 +2012,7 @@ static int index_from_diff_list(git_index **out, *out = NULL; - if ((error = git_index_new(&index)) < 0) + if ((error = git_index__new(&index, oid_type)) < 0) return error; if ((error = git_index__fill(index, &diff_list->staged)) < 0) @@ -2157,7 +2162,7 @@ int git_merge__iterators( } } - error = index_from_diff_list(out, diff_list, + error = index_from_diff_list(out, diff_list, repo->oid_type, (opts.flags & GIT_MERGE_SKIP_REUC)); done: @@ -2200,8 +2205,8 @@ int git_merge_trees( result = our_tree; if (result) { - if ((error = git_index_new(out)) == 0) - error = git_index_read_tree(*out, result); + if ((error = git_index__new(out, repo->oid_type)) == 0) + error = git_index_read_tree(*out, result); return error; } diff --git a/src/libgit2/merge_file.c b/src/libgit2/merge_file.c index 732a047b1..ffe83cf2a 100644 --- a/src/libgit2/merge_file.c +++ b/src/libgit2/merge_file.c @@ -19,8 +19,6 @@ #include "git2/index.h" #include "git2/merge.h" -#include "xdiff/xdiff.h" - /* only examine the first 8000 bytes for binaryness. * https://github.com/git/git/blob/77bd3ea9f54f1584147b594abc04c26ca516d987/xdiff-interface.c#L197 */ diff --git a/src/libgit2/midx.c b/src/libgit2/midx.c index b09055237..f6071f0bb 100644 --- a/src/libgit2/midx.c +++ b/src/libgit2/midx.c @@ -115,19 +115,20 @@ static int midx_parse_oid_lookup( struct git_midx_chunk *chunk_oid_lookup) { uint32_t i; - unsigned char *oid, *prev_oid, zero_oid[GIT_OID_SHA1_SIZE] = {0}; + unsigned char *oid, *prev_oid, zero_oid[GIT_OID_MAX_SIZE] = {0}; + size_t oid_size = git_oid_size(idx->oid_type); if (chunk_oid_lookup->offset == 0) return midx_error("missing OID Lookup chunk"); if (chunk_oid_lookup->length == 0) return midx_error("empty OID Lookup chunk"); - if (chunk_oid_lookup->length != idx->num_objects * GIT_OID_SHA1_SIZE) + if (chunk_oid_lookup->length != idx->num_objects * oid_size) return midx_error("OID Lookup chunk has wrong length"); idx->oid_lookup = oid = (unsigned char *)(data + chunk_oid_lookup->offset); prev_oid = zero_oid; - for (i = 0; i < idx->num_objects; ++i, oid += GIT_OID_SHA1_SIZE) { - if (git_oid_raw_cmp(prev_oid, oid, GIT_OID_SHA1_SIZE) >= 0) + for (i = 0; i < idx->num_objects; ++i, oid += oid_size) { + if (git_oid_raw_cmp(prev_oid, oid, oid_size) >= 0) return midx_error("OID Lookup index is non-monotonic"); prev_oid = oid; } @@ -178,7 +179,7 @@ int git_midx_parse( struct git_midx_chunk *last_chunk; uint32_t i; off64_t last_chunk_offset, chunk_offset, trailer_offset; - size_t checksum_size; + size_t checksum_size, oid_size; int error; struct git_midx_chunk chunk_packfile_names = {0}, chunk_oid_fanout = {0}, @@ -188,7 +189,9 @@ int git_midx_parse( GIT_ASSERT_ARG(idx); - if (size < sizeof(struct git_midx_header) + GIT_OID_SHA1_SIZE) + oid_size = git_oid_size(idx->oid_type); + + if (size < sizeof(struct git_midx_header) + oid_size) return midx_error("multi-pack index is too short"); hdr = ((struct git_midx_header *)data); @@ -209,7 +212,7 @@ int git_midx_parse( sizeof(struct git_midx_header) + (1 + hdr->chunks) * 12; - checksum_size = GIT_HASH_SHA1_SIZE; + checksum_size = oid_size; trailer_offset = size - checksum_size; if (trailer_offset < last_chunk_offset) @@ -287,8 +290,9 @@ int git_midx_parse( } int git_midx_open( - git_midx_file **idx_out, - const char *path) + git_midx_file **idx_out, + const char *path, + git_oid_t oid_type) { git_midx_file *idx; git_file fd = -1; @@ -296,6 +300,8 @@ int git_midx_open( struct stat st; int error; + GIT_ASSERT_ARG(idx_out && path && oid_type); + /* TODO: properly open the file without access time using O_NOATIME */ fd = git_futils_open_ro(path); if (fd < 0) @@ -317,6 +323,8 @@ int git_midx_open( idx = git__calloc(1, sizeof(git_midx_file)); GIT_ERROR_CHECK_ALLOC(idx); + idx->oid_type = oid_type; + error = git_str_sets(&idx->filename, path); if (error < 0) return error; @@ -344,7 +352,7 @@ bool git_midx_needs_refresh( git_file fd = -1; struct stat st; ssize_t bytes_read; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + unsigned char checksum[GIT_HASH_MAX_SIZE]; size_t checksum_size; /* TODO: properly open the file without access time using O_NOATIME */ @@ -364,8 +372,8 @@ bool git_midx_needs_refresh( return true; } - checksum_size = GIT_HASH_SHA1_SIZE; - bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - GIT_OID_SHA1_SIZE); + checksum_size = git_oid_size(idx->oid_type); + bytes_read = p_pread(fd, checksum, checksum_size, st.st_size - checksum_size); p_close(fd); if (bytes_read != (ssize_t)checksum_size) @@ -381,7 +389,7 @@ int git_midx_entry_find( size_t len) { int pos, found = 0; - size_t pack_index; + size_t pack_index, oid_size, oid_hexsize; uint32_t hi, lo; unsigned char *current = NULL; const unsigned char *object_offset; @@ -389,30 +397,33 @@ int git_midx_entry_find( GIT_ASSERT_ARG(idx); + oid_size = git_oid_size(idx->oid_type); + oid_hexsize = git_oid_hexsize(idx->oid_type); + hi = ntohl(idx->oid_fanout[(int)short_oid->id[0]]); lo = ((short_oid->id[0] == 0x0) ? 0 : ntohl(idx->oid_fanout[(int)short_oid->id[0] - 1])); - pos = git_pack__lookup_id(idx->oid_lookup, GIT_OID_SHA1_SIZE, lo, hi, short_oid->id, GIT_OID_SHA1); + pos = git_pack__lookup_id(idx->oid_lookup, oid_size, lo, hi, short_oid->id, idx->oid_type); if (pos >= 0) { /* An object matching exactly the oid was found */ found = 1; - current = idx->oid_lookup + (pos * GIT_OID_SHA1_SIZE); + current = idx->oid_lookup + (pos * oid_size); } else { /* No object was found */ /* pos refers to the object with the "closest" oid to short_oid */ pos = -1 - pos; if (pos < (int)idx->num_objects) { - current = idx->oid_lookup + (pos * GIT_OID_SHA1_SIZE); + current = idx->oid_lookup + (pos * oid_size); if (!git_oid_raw_ncmp(short_oid->id, current, len)) found = 1; } } - if (found && len != GIT_OID_SHA1_HEXSIZE && pos + 1 < (int)idx->num_objects) { + if (found && len != oid_hexsize && pos + 1 < (int)idx->num_objects) { /* Check for ambiguousity */ - const unsigned char *next = current + GIT_OID_SHA1_SIZE; + const unsigned char *next = current + oid_size; if (!git_oid_raw_ncmp(short_oid->id, next, len)) found = 2; @@ -443,7 +454,7 @@ int git_midx_entry_find( return midx_error("invalid index into the packfile names table"); e->pack_index = pack_index; e->offset = offset; - git_oid__fromraw(&e->sha1, current, GIT_OID_SHA1); + git_oid__fromraw(&e->sha1, current, idx->oid_type); return 0; } @@ -453,13 +464,15 @@ int git_midx_foreach_entry( void *data) { git_oid oid; - size_t i; + size_t oid_size, i; int error; GIT_ASSERT_ARG(idx); + oid_size = git_oid_size(idx->oid_type); + for (i = 0; i < idx->num_objects; ++i) { - if ((error = git_oid__fromraw(&oid, &idx->oid_lookup[i * GIT_OID_SHA1_SIZE], GIT_OID_SHA1)) < 0) + if ((error = git_oid__fromraw(&oid, &idx->oid_lookup[i * oid_size], idx->oid_type)) < 0) return error; if ((error = cb(&oid, data)) != 0) @@ -501,9 +514,21 @@ static int packfile__cmp(const void *a_, const void *b_) int git_midx_writer_new( git_midx_writer **out, - const char *pack_dir) + const char *pack_dir +#ifdef GIT_EXPERIMENTAL_SHA256 + , git_oid_t oid_type +#endif + ) { - git_midx_writer *w = git__calloc(1, sizeof(git_midx_writer)); + git_midx_writer *w; + +#ifndef GIT_EXPERIMENTAL_SHA256 + git_oid_t oid_type = GIT_OID_SHA1; +#endif + + GIT_ASSERT_ARG(out && pack_dir && oid_type); + + w = git__calloc(1, sizeof(git_midx_writer)); GIT_ERROR_CHECK_ALLOC(w); if (git_str_sets(&w->pack_dir, pack_dir) < 0) { @@ -518,6 +543,8 @@ int git_midx_writer_new( return -1; } + w->oid_type = oid_type; + *out = w; return 0; } @@ -662,12 +689,13 @@ static int midx_write( oid_lookup = GIT_STR_INIT, object_offsets = GIT_STR_INIT, object_large_offsets = GIT_STR_INIT; - unsigned char checksum[GIT_HASH_SHA1_SIZE]; - size_t checksum_size; + unsigned char checksum[GIT_HASH_MAX_SIZE]; + size_t checksum_size, oid_size; git_midx_entry *entry; object_entry_array_t object_entries_array = GIT_ARRAY_INIT; git_vector object_entries = GIT_VECTOR_INIT; git_hash_ctx ctx; + git_hash_algorithm_t checksum_type; struct midx_write_hash_context hash_cb_data = {0}; hdr.signature = htonl(MIDX_SIGNATURE); @@ -679,10 +707,14 @@ static int midx_write( hash_cb_data.cb_data = cb_data; hash_cb_data.ctx = &ctx; - checksum_size = GIT_HASH_SHA1_SIZE; - error = git_hash_ctx_init(&ctx, GIT_HASH_ALGORITHM_SHA1); - if (error < 0) + oid_size = git_oid_size(w->oid_type); + + GIT_ASSERT((checksum_type = git_oid_algorithm(w->oid_type))); + checksum_size = git_hash_size(checksum_type); + + if ((error = git_hash_ctx_init(&ctx, checksum_type)) < 0) return error; + cb_data = &hash_cb_data; write_cb = midx_write_hash; @@ -749,7 +781,9 @@ static int midx_write( /* Fill the OID Lookup table. */ git_vector_foreach (&object_entries, i, entry) { - error = git_str_put(&oid_lookup, (char *)&entry->sha1.id, GIT_OID_SHA1_SIZE); + error = git_str_put(&oid_lookup, + (char *)&entry->sha1.id, oid_size); + if (error < 0) goto cleanup; } diff --git a/src/libgit2/midx.h b/src/libgit2/midx.h index bcb0d9a0a..5107f69cb 100644 --- a/src/libgit2/midx.h +++ b/src/libgit2/midx.h @@ -51,8 +51,14 @@ typedef struct git_midx_file { /* The number of entries in the Object Large Offsets table. Each entry has an 8-byte with an offset */ size_t num_object_large_offsets; - /* The trailer of the file. Contains the SHA1-checksum of the whole file. */ - unsigned char checksum[GIT_HASH_SHA1_SIZE]; + /* + * The trailer of the file. Contains the checksum of the whole + * file, in the repository's object format hash. + */ + unsigned char checksum[GIT_HASH_MAX_SIZE]; + + /* The type of object IDs in the midx. */ + git_oid_t oid_type; /* something like ".git/objects/pack/multi-pack-index". */ git_str filename; @@ -82,11 +88,15 @@ struct git_midx_writer { /* The list of `git_pack_file`s. */ git_vector packs; + + /* The object ID type of the writer. */ + git_oid_t oid_type; }; int git_midx_open( git_midx_file **idx_out, - const char *path); + const char *path, + git_oid_t oid_type); bool git_midx_needs_refresh( const git_midx_file *idx, const char *path); diff --git a/src/libgit2/notes.c b/src/libgit2/notes.c index 1b1935330..13ca3824b 100644 --- a/src/libgit2/notes.c +++ b/src/libgit2/notes.c @@ -460,7 +460,7 @@ int git_note_commit_read( { int error; git_tree *tree = NULL; - char target[GIT_OID_SHA1_HEXSIZE + 1]; + char target[GIT_OID_MAX_HEXSIZE + 1]; git_oid_tostr(target, sizeof(target), oid); @@ -507,7 +507,7 @@ int git_note_commit_create( { int error; git_tree *tree = NULL; - char target[GIT_OID_SHA1_HEXSIZE + 1]; + char target[GIT_OID_MAX_HEXSIZE + 1]; git_oid_tostr(target, sizeof(target), oid); @@ -578,7 +578,7 @@ int git_note_commit_remove( { int error; git_tree *tree = NULL; - char target[GIT_OID_SHA1_HEXSIZE + 1]; + char target[GIT_OID_MAX_HEXSIZE + 1]; git_oid_tostr(target, sizeof(target), oid); @@ -665,8 +665,9 @@ void git_note_free(git_note *note) } static int process_entry_path( - const char *entry_path, - git_oid *annotated_object_id) + git_oid *annotated_object_id, + git_note_iterator *it, + const char *entry_path) { int error = 0; size_t i = 0, j = 0, len; @@ -698,12 +699,12 @@ static int process_entry_path( buf.ptr[j] = '\0'; buf.size = j; - if (j != GIT_OID_SHA1_HEXSIZE) { + if (j != git_oid_hexsize(it->repo->oid_type)) { /* This is not a note entry */ goto cleanup; } - error = git_oid__fromstr(annotated_object_id, buf.ptr, GIT_OID_SHA1); + error = git_oid__fromstr(annotated_object_id, buf.ptr, it->repo->oid_type); cleanup: git_str_dispose(&buf); @@ -799,7 +800,7 @@ int git_note_next( git_oid_cpy(note_id, &item->id); - if ((error = process_entry_path(item->path, annotated_id)) < 0) + if ((error = process_entry_path(annotated_id, it, item->path)) < 0) return error; if ((error = git_iterator_advance(NULL, it)) < 0 && error != GIT_ITEROVER) diff --git a/src/libgit2/object.c b/src/libgit2/object.c index 3d544cfff..5fab77e6a 100644 --- a/src/libgit2/object.c +++ b/src/libgit2/object.c @@ -196,6 +196,7 @@ int git_object_lookup_prefix( git_object *object = NULL; git_odb *odb = NULL; git_odb_object *odb_obj = NULL; + size_t oid_hexsize; int error = 0; GIT_ASSERT_ARG(repo); @@ -211,10 +212,12 @@ int git_object_lookup_prefix( if (error < 0) return error; - if (len > GIT_OID_SHA1_HEXSIZE) - len = GIT_OID_SHA1_HEXSIZE; + oid_hexsize = git_oid_hexsize(repo->oid_type); - if (len == GIT_OID_SHA1_HEXSIZE) { + if (len > oid_hexsize) + len = oid_hexsize; + + if (len == oid_hexsize) { git_cached_obj *cached = NULL; /* We want to match the full id : we can first look up in the cache, @@ -248,8 +251,9 @@ int git_object_lookup_prefix( error = git_odb_read(&odb_obj, odb, id); } } else { - git_oid short_oid = GIT_OID_SHA1_ZERO; + git_oid short_oid; + git_oid_clear(&short_oid, repo->oid_type); git_oid__cpy_prefix(&short_oid, id, len); /* If len < GIT_OID_SHA1_HEXSIZE (a strict short oid was given), we have @@ -277,7 +281,8 @@ int git_object_lookup_prefix( } int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) { - return git_object_lookup_prefix(object_out, repo, id, GIT_OID_SHA1_HEXSIZE, type); + return git_object_lookup_prefix(object_out, + repo, id, git_oid_hexsize(repo->oid_type), type); } void git_object_free(git_object *object) @@ -518,31 +523,36 @@ cleanup: static int git_object__short_id(git_str *out, const git_object *obj) { git_repository *repo; - int len = GIT_ABBREV_DEFAULT, error; - git_oid id = GIT_OID_SHA1_ZERO; + git_oid id; git_odb *odb; + size_t oid_hexsize; + int len = GIT_ABBREV_DEFAULT, error; GIT_ASSERT_ARG(out); GIT_ASSERT_ARG(obj); repo = git_object_owner(obj); + git_oid_clear(&id, repo->oid_type); + oid_hexsize = git_oid_hexsize(repo->oid_type); + if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0) return error; + if (len < 0 || (size_t)len > oid_hexsize) { + git_error_set(GIT_ERROR_CONFIG, "invalid oid abbreviation setting: '%d'", len); + return -1; + } + if ((error = git_repository_odb(&odb, repo)) < 0) return error; - while (len < GIT_OID_SHA1_HEXSIZE) { + while ((size_t)len < oid_hexsize) { /* set up short oid */ memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2); if (len & 1) id.id[len / 2] &= 0xf0; -#ifdef GIT_EXPERIMENTAL_SHA256 - id.type = GIT_OID_SHA1; -#endif - error = git_odb_exists_prefix(NULL, odb, &id, len); if (error != GIT_EAMBIGUOUS) break; diff --git a/src/libgit2/odb.c b/src/libgit2/odb.c index 0fc48035a..68872e1a1 100644 --- a/src/libgit2/odb.c +++ b/src/libgit2/odb.c @@ -748,7 +748,8 @@ int git_odb__add_default_backends( git_error_set(GIT_ERROR_ODB, "failed to acquire the odb lock"); return -1; } - if (!db->cgraph && git_commit_graph_new(&db->cgraph, objects_dir, false) < 0) { + if (!db->cgraph && + git_commit_graph_new(&db->cgraph, objects_dir, false, db->options.oid_type) < 0) { git_mutex_unlock(&db->lock); return -1; } diff --git a/src/libgit2/odb_pack.c b/src/libgit2/odb_pack.c index 1b1d122b0..fc533ae83 100644 --- a/src/libgit2/odb_pack.c +++ b/src/libgit2/odb_pack.c @@ -309,11 +309,17 @@ static int pack_entry_find_prefix( { int error; size_t i; - git_oid found_full_oid = GIT_OID_SHA1_ZERO; + git_oid found_full_oid; bool found = false; struct git_pack_file *last_found = backend->last_found, *p; git_midx_entry midx_entry; +#ifdef GIT_EXPERIMENTAL_SHA256 + git_oid_clear(&found_full_oid, short_oid->type); +#else + git_oid_clear(&found_full_oid, GIT_OID_SHA1); +#endif + if (backend->midx) { error = git_midx_entry_find(&midx_entry, backend->midx, short_oid, len); if (error == GIT_EAMBIGUOUS) @@ -473,7 +479,9 @@ static int refresh_multi_pack_index(struct pack_backend *backend) } } - error = git_midx_open(&backend->midx, git_str_cstr(&midx_path)); + error = git_midx_open(&backend->midx, git_str_cstr(&midx_path), + backend->opts.oid_type); + git_str_dispose(&midx_path); if (error < 0) return error; @@ -792,7 +800,12 @@ static int pack_backend__writemidx(git_odb_backend *_backend) backend = (struct pack_backend *)_backend; - error = git_midx_writer_new(&w, backend->pack_folder); + error = git_midx_writer_new(&w, backend->pack_folder +#ifdef GIT_EXPERIMENTAL_SHA256 + , backend->opts.oid_type +#endif + ); + if (error < 0) return error; diff --git a/src/libgit2/pack-objects.c b/src/libgit2/pack-objects.c index 20a5dfcbd..d6fd60326 100644 --- a/src/libgit2/pack-objects.c +++ b/src/libgit2/pack-objects.c @@ -127,6 +127,7 @@ out: int git_packbuilder_new(git_packbuilder **out, git_repository *repo) { + git_hash_algorithm_t hash_algorithm; git_packbuilder *pb; *out = NULL; @@ -134,6 +135,11 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) pb = git__calloc(1, sizeof(*pb)); GIT_ERROR_CHECK_ALLOC(pb); + pb->oid_type = repo->oid_type; + + hash_algorithm = git_oid_algorithm(pb->oid_type); + GIT_ASSERT(hash_algorithm); + if (git_oidmap_new(&pb->object_ix) < 0 || git_oidmap_new(&pb->walk_objects) < 0 || git_pool_init(&pb->object_pool, sizeof(struct walk_object)) < 0) @@ -142,7 +148,7 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) pb->repo = repo; pb->nr_threads = 1; /* do not spawn any thread by default */ - if (git_hash_ctx_init(&pb->ctx, GIT_HASH_ALGORITHM_SHA1) < 0 || + if (git_hash_ctx_init(&pb->ctx, hash_algorithm) < 0 || git_zstream_init(&pb->zstream, GIT_ZSTREAM_DEFLATE) < 0 || git_repository_odb(&pb->odb, repo) < 0 || packbuilder_config(pb) < 0) @@ -315,9 +321,11 @@ static int write_object( git_object_t type; unsigned char hdr[10], *zbuf = NULL; void *data = NULL; - size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len; + size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len, oid_size; int error; + oid_size = git_oid_size(pb->oid_type); + /* * If we have a delta base, let's use the delta to save space. * Otherwise load the whole object. 'data' ends up pointing to @@ -347,8 +355,8 @@ static int write_object( goto done; if (type == GIT_OBJECT_REF_DELTA) { - if ((error = write_cb(po->delta->id.id, GIT_OID_SHA1_SIZE, cb_data)) < 0 || - (error = git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_SHA1_SIZE)) < 0) + if ((error = write_cb(po->delta->id.id, oid_size, cb_data)) < 0 || + (error = git_hash_update(&pb->ctx, po->delta->id.id, oid_size)) < 0) goto done; } @@ -668,7 +676,7 @@ static int write_pack(git_packbuilder *pb, if ((error = git_hash_final(entry_oid.id, &pb->ctx)) < 0) goto done; - error = write_cb(entry_oid.id, GIT_OID_SHA1_SIZE, cb_data); + error = write_cb(entry_oid.id, git_oid_size(pb->oid_type), cb_data); done: /* if callback cancelled writing, we must still free delta_data */ diff --git a/src/libgit2/pack-objects.h b/src/libgit2/pack-objects.h index 2faa3ec7f..c6bc52fdc 100644 --- a/src/libgit2/pack-objects.h +++ b/src/libgit2/pack-objects.h @@ -56,6 +56,8 @@ struct git_packbuilder { git_repository *repo; /* associated repository */ git_odb *odb; /* associated object database */ + git_oid_t oid_type; + git_hash_ctx ctx; git_zstream zstream; diff --git a/src/libgit2/pack.c b/src/libgit2/pack.c index c30801844..d59973aa9 100644 --- a/src/libgit2/pack.c +++ b/src/libgit2/pack.c @@ -200,7 +200,7 @@ static void pack_index_free(struct git_pack_file *p) static int pack_index_check_locked(const char *path, struct git_pack_file *p) { struct git_pack_idx_header *hdr; - uint32_t version, nr, i, *index; + uint32_t version, nr = 0, i, *index; void *idx_map; size_t idx_size; struct stat st; @@ -246,7 +246,6 @@ static int pack_index_check_locked(const char *path, struct git_pack_file *p) version = 1; } - nr = 0; index = idx_map; if (version > 1) @@ -269,7 +268,7 @@ static int pack_index_check_locked(const char *path, struct git_pack_file *p) * - 20/32-byte SHA of the packfile * - 20/32-byte SHA file checksum */ - if (idx_size != (4 * 256 + (nr * (p->oid_size + 4)) + (p->oid_size * 2))) { + if (idx_size != (4 * 256 + ((uint64_t) nr * (p->oid_size + 4)) + (p->oid_size * 2))) { git_futils_mmap_free(&p->index_map); return packfile_error("index is corrupted"); } @@ -287,8 +286,8 @@ static int pack_index_check_locked(const char *path, struct git_pack_file *p) * variable sized table containing 8-byte entries * for offsets larger than 2^31. */ - unsigned long min_size = 8 + (4 * 256) + (nr * (p->oid_size + 4 + 4)) + (p->oid_size * 2); - unsigned long max_size = min_size; + uint64_t min_size = 8 + (4 * 256) + ((uint64_t)nr * (p->oid_size + 4 + 4)) + (p->oid_size * 2); + uint64_t max_size = min_size; if (nr) max_size += (nr - 1)*8; diff --git a/src/libgit2/parse.c b/src/libgit2/parse.c index 55d3cb10e..9eb86a3f5 100644 --- a/src/libgit2/parse.c +++ b/src/libgit2/parse.c @@ -102,13 +102,16 @@ int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base) return 0; } -int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx) +int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx, git_oid_t oid_type) { - if (ctx->line_len < GIT_OID_SHA1_HEXSIZE) + size_t oid_hexsize = git_oid_hexsize(oid_type); + GIT_ASSERT(oid_hexsize); + + if (ctx->line_len < oid_hexsize) return -1; - if ((git_oid__fromstrn(out, ctx->line, GIT_OID_SHA1_HEXSIZE, GIT_OID_SHA1)) < 0) + if ((git_oid__fromstrn(out, ctx->line, oid_hexsize, oid_type)) < 0) return -1; - git_parse_advance_chars(ctx, GIT_OID_SHA1_HEXSIZE); + git_parse_advance_chars(ctx, oid_hexsize); return 0; } diff --git a/src/libgit2/parse.h b/src/libgit2/parse.h index 0ecb7c103..beef1de12 100644 --- a/src/libgit2/parse.h +++ b/src/libgit2/parse.h @@ -50,7 +50,7 @@ int git_parse_advance_expected( int git_parse_advance_ws(git_parse_ctx *ctx); int git_parse_advance_nl(git_parse_ctx *ctx); int git_parse_advance_digit(int64_t *out, git_parse_ctx *ctx, int base); -int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx); +int git_parse_advance_oid(git_oid *out, git_parse_ctx *ctx, git_oid_t oid_type); enum GIT_PARSE_PEEK_FLAGS { GIT_PARSE_PEEK_SKIP_WHITESPACE = (1 << 0) diff --git a/src/libgit2/patch.h b/src/libgit2/patch.h index 1e1471ed6..86328e886 100644 --- a/src/libgit2/patch.h +++ b/src/libgit2/patch.h @@ -59,9 +59,15 @@ typedef struct { * This prefix will be removed when looking for files. The default is 1. */ uint32_t prefix_len; + + /** + * The type of object IDs in the patch file. The default is + * `GIT_OID_DEFAULT`. + */ + git_oid_t oid_type; } git_patch_options; -#define GIT_PATCH_OPTIONS_INIT { 1 } +#define GIT_PATCH_OPTIONS_INIT { 1, GIT_OID_DEFAULT } extern int git_patch__to_buf(git_str *out, git_patch *patch); extern void git_patch_free(git_patch *patch); diff --git a/src/libgit2/patch_generate.c b/src/libgit2/patch_generate.c index bc598fea8..079bc53ae 100644 --- a/src/libgit2/patch_generate.c +++ b/src/libgit2/patch_generate.c @@ -81,7 +81,8 @@ static void patch_generated_init_common(git_patch_generated *patch) static int patch_generated_normalize_options( git_diff_options *out, - const git_diff_options *opts) + const git_diff_options *opts, + git_repository *repo) { if (opts) { GIT_ERROR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options"); @@ -91,6 +92,23 @@ static int patch_generated_normalize_options( memcpy(out, &default_opts, sizeof(git_diff_options)); } + if (repo && opts && opts->oid_type && repo->oid_type != opts->oid_type) { + /* + * This limitation feels unnecessary - we should consider + * allowing users to generate diffs with a different object + * ID format than the repository. + */ + git_error_set(GIT_ERROR_INVALID, + "specified object ID type does not match repository object ID type"); + return -1; + } else if (repo) { + out->oid_type = repo->oid_type; + } else if (opts && opts->oid_type) { + out->oid_type = opts->oid_type; + } else { + out->oid_type = GIT_OID_DEFAULT; + } + out->old_prefix = opts && opts->old_prefix ? git__strdup(opts->old_prefix) : git__strdup(DIFF_OLD_PREFIX_DEFAULT); @@ -118,7 +136,7 @@ static int patch_generated_init( patch->delta_index = delta_index; if ((error = patch_generated_normalize_options( - &patch->base.diff_opts, &diff->opts)) < 0 || + &patch->base.diff_opts, &diff->opts, diff->repo)) < 0 || (error = git_diff_file_content__init_from_diff( &patch->ofile, diff, patch->base.delta, true)) < 0 || (error = git_diff_file_content__init_from_diff( @@ -449,7 +467,7 @@ static int patch_generated_from_sources( git_xdiff_output *xo, git_diff_file_content_src *oldsrc, git_diff_file_content_src *newsrc, - const git_diff_options *opts) + const git_diff_options *given_opts) { int error = 0; git_repository *repo = @@ -457,11 +475,12 @@ static int patch_generated_from_sources( newsrc->blob ? git_blob_owner(newsrc->blob) : NULL; git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file; git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile; + git_diff_options *opts = &pd->patch.base.diff_opts; - if ((error = patch_generated_normalize_options(&pd->patch.base.diff_opts, opts)) < 0) + if ((error = patch_generated_normalize_options(opts, given_opts, repo)) < 0) return error; - if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) { + if ((opts->flags & GIT_DIFF_REVERSE) != 0) { void *tmp = lfile; lfile = rfile; rfile = tmp; tmp = ldata; ldata = rdata; rdata = tmp; } diff --git a/src/libgit2/patch_parse.c b/src/libgit2/patch_parse.c index ffdb99231..c06915537 100644 --- a/src/libgit2/patch_parse.c +++ b/src/libgit2/patch_parse.c @@ -166,15 +166,19 @@ static int parse_header_oid( uint16_t *oid_len, git_patch_parse_ctx *ctx) { - size_t len; + size_t hexsize, len; + + hexsize = git_oid_hexsize(ctx->opts.oid_type); - for (len = 0; len < ctx->parse_ctx.line_len && len < GIT_OID_SHA1_HEXSIZE; len++) { + for (len = 0; + len < ctx->parse_ctx.line_len && len < hexsize; + len++) { if (!git__isxdigit(ctx->parse_ctx.line[len])) break; } - if (len < GIT_OID_MINPREFIXLEN || len > GIT_OID_SHA1_HEXSIZE || - git_oid__fromstrn(oid, ctx->parse_ctx.line, len, GIT_OID_SHA1) < 0) + if (len < GIT_OID_MINPREFIXLEN || len > hexsize || + git_oid__fromstrn(oid, ctx->parse_ctx.line, len, ctx->opts.oid_type) < 0) return git_parse_err("invalid hex formatted object id at line %"PRIuZ, ctx->parse_ctx.line_num); @@ -1065,12 +1069,14 @@ static int check_patch(git_patch_parsed *patch) return git_parse_err("patch with no hunks"); if (delta->status == GIT_DELTA_ADDED) { - git_oid_clear(&delta->old_file.id, GIT_OID_SHA1); + git_oid_clear(&delta->old_file.id, + patch->base.diff_opts.oid_type); delta->old_file.id_abbrev = 0; } if (delta->status == GIT_DELTA_DELETED) { - git_oid_clear(&delta->new_file.id, GIT_OID_SHA1); + git_oid_clear(&delta->new_file.id, + patch->base.diff_opts.oid_type); delta->new_file.id_abbrev = 0; } @@ -1187,11 +1193,13 @@ int git_patch_parse( patch->base.delta->status = GIT_DELTA_MODIFIED; patch->base.delta->nfiles = 2; + patch->base.diff_opts.oid_type = ctx->opts.oid_type; + start = ctx->parse_ctx.remain_len; if ((error = parse_patch_header(patch, ctx)) < 0 || - (error = parse_patch_body(patch, ctx)) < 0 || - (error = check_patch(patch)) < 0) + (error = parse_patch_body(patch, ctx)) < 0 || + (error = check_patch(patch)) < 0) goto done; used = start - ctx->parse_ctx.remain_len; diff --git a/src/libgit2/push.c b/src/libgit2/push.c index e25681870..8b47abc24 100644 --- a/src/libgit2/push.c +++ b/src/libgit2/push.c @@ -118,8 +118,8 @@ static int parse_refspec(git_push *push, push_spec **spec, const char *str) s = git__calloc(1, sizeof(*s)); GIT_ERROR_CHECK_ALLOC(s); - git_oid_clear(&s->loid, GIT_OID_SHA1); - git_oid_clear(&s->roid, GIT_OID_SHA1); + git_oid_clear(&s->loid, push->repo->oid_type); + git_oid_clear(&s->roid, push->repo->oid_type); if (git_refspec__parse(&s->refspec, str, false) < 0) { git_error_set(GIT_ERROR_INVALID, "invalid refspec %s", str); diff --git a/src/libgit2/reader.c b/src/libgit2/reader.c index be29bb41c..df2b2807f 100644 --- a/src/libgit2/reader.c +++ b/src/libgit2/reader.c @@ -125,7 +125,7 @@ static int workdir_reader_read( goto done; if (out_id || reader->index) { - if ((error = git_odb__hash(&id, out->ptr, out->size, GIT_OBJECT_BLOB, GIT_OID_SHA1)) < 0) + if ((error = git_odb__hash(&id, out->ptr, out->size, GIT_OBJECT_BLOB, reader->repo->oid_type)) < 0) goto done; } diff --git a/src/libgit2/rebase.c b/src/libgit2/rebase.c index 1970d5ddc..77e442e98 100644 --- a/src/libgit2/rebase.c +++ b/src/libgit2/rebase.c @@ -65,6 +65,9 @@ struct git_rebase { git_rebase_t type; char *state_path; + /* Temporary buffer for paths within the state path. */ + git_str state_filename; + unsigned int head_detached:1, inmemory:1, quiet:1, @@ -134,33 +137,42 @@ done: GIT_INLINE(int) rebase_readfile( git_str *out, - git_str *state_path, + git_rebase *rebase, const char *filename) { - size_t state_path_len = state_path->size; + /* + * `rebase->state_filename` is a temporary buffer to avoid + * unnecessary allocations and copies of `rebase->state_path`. + * At the start and end of this function it always contains the + * contents of `rebase->state_path` itself. + */ + size_t state_path_len = rebase->state_filename.size; int error; git_str_clear(out); - if ((error = git_str_joinpath(state_path, state_path->ptr, filename)) < 0 || - (error = git_futils_readbuffer(out, state_path->ptr)) < 0) + if ((error = git_str_joinpath(&rebase->state_filename, rebase->state_filename.ptr, filename)) < 0 || + (error = git_futils_readbuffer(out, rebase->state_filename.ptr)) < 0) goto done; git_str_rtrim(out); done: - git_str_truncate(state_path, state_path_len); + git_str_truncate(&rebase->state_filename, state_path_len); return error; } GIT_INLINE(int) rebase_readint( - size_t *out, git_str *asc_out, git_str *state_path, const char *filename) + size_t *out, + git_str *asc_out, + git_rebase *rebase, + const char *filename) { int32_t num; const char *eol; int error = 0; - if ((error = rebase_readfile(asc_out, state_path, filename)) < 0) + if ((error = rebase_readfile(asc_out, rebase, filename)) < 0) return error; if (git__strntol32(&num, asc_out->ptr, asc_out->size, &eol, 10) < 0 || num < 0 || *eol) { @@ -174,15 +186,18 @@ GIT_INLINE(int) rebase_readint( } GIT_INLINE(int) rebase_readoid( - git_oid *out, git_str *str_out, git_str *state_path, const char *filename) + git_oid *out, + git_str *str_out, + git_rebase *rebase, + const char *filename) { int error; - if ((error = rebase_readfile(str_out, state_path, filename)) < 0) + if ((error = rebase_readfile(str_out, rebase, filename)) < 0) return error; - if (str_out->size != GIT_OID_SHA1_HEXSIZE || - git_oid__fromstr(out, str_out->ptr, GIT_OID_SHA1) < 0) { + if (str_out->size != git_oid_hexsize(rebase->repo->oid_type) || + git_oid__fromstr(out, str_out->ptr, rebase->repo->oid_type) < 0) { git_error_set(GIT_ERROR_REBASE, "the file '%s' contains an invalid object ID", filename); return -1; } @@ -213,17 +228,14 @@ static git_rebase_operation *rebase_operation_alloc( static int rebase_open_merge(git_rebase *rebase) { - git_str state_path = GIT_STR_INIT, buf = GIT_STR_INIT, cmt = GIT_STR_INIT; + git_str buf = GIT_STR_INIT, cmt = GIT_STR_INIT; git_oid id; git_rebase_operation *operation; size_t i, msgnum = 0, end; int error; - if ((error = git_str_puts(&state_path, rebase->state_path)) < 0) - goto done; - /* Read 'msgnum' if it exists (otherwise, let msgnum = 0) */ - if ((error = rebase_readint(&msgnum, &buf, &state_path, MSGNUM_FILE)) < 0 && + if ((error = rebase_readint(&msgnum, &buf, rebase, MSGNUM_FILE)) < 0 && error != GIT_ENOTFOUND) goto done; @@ -233,11 +245,11 @@ static int rebase_open_merge(git_rebase *rebase) } /* Read 'end' */ - if ((error = rebase_readint(&end, &buf, &state_path, END_FILE)) < 0) + if ((error = rebase_readint(&end, &buf, rebase, END_FILE)) < 0) goto done; /* Read 'current' if it exists */ - if ((error = rebase_readoid(&id, &buf, &state_path, CURRENT_FILE)) < 0 && + if ((error = rebase_readoid(&id, &buf, rebase, CURRENT_FILE)) < 0 && error != GIT_ENOTFOUND) goto done; @@ -249,7 +261,7 @@ static int rebase_open_merge(git_rebase *rebase) git_str_clear(&cmt); if ((error = git_str_printf(&cmt, "cmt.%" PRIuZ, (i+1))) < 0 || - (error = rebase_readoid(&id, &buf, &state_path, cmt.ptr)) < 0) + (error = rebase_readoid(&id, &buf, rebase, cmt.ptr)) < 0) goto done; operation = rebase_operation_alloc(rebase, GIT_REBASE_OPERATION_PICK, &id, NULL); @@ -257,14 +269,13 @@ static int rebase_open_merge(git_rebase *rebase) } /* Read 'onto_name' */ - if ((error = rebase_readfile(&buf, &state_path, ONTO_NAME_FILE)) < 0) + if ((error = rebase_readfile(&buf, rebase, ONTO_NAME_FILE)) < 0) goto done; rebase->onto_name = git_str_detach(&buf); done: git_str_dispose(&cmt); - git_str_dispose(&state_path); git_str_dispose(&buf); return error; @@ -308,9 +319,9 @@ int git_rebase_open( const git_rebase_options *given_opts) { git_rebase *rebase; - git_str path = GIT_STR_INIT, orig_head_name = GIT_STR_INIT, - orig_head_id = GIT_STR_INIT, onto_id = GIT_STR_INIT; - size_t state_path_len; + git_str orig_head_name = GIT_STR_INIT, + orig_head_id = GIT_STR_INIT, + onto_id = GIT_STR_INIT; int error; GIT_ASSERT_ARG(repo); @@ -332,13 +343,10 @@ int git_rebase_open( goto done; } - if ((error = git_str_puts(&path, rebase->state_path)) < 0) + if ((error = git_str_puts(&rebase->state_filename, rebase->state_path)) < 0) goto done; - state_path_len = git_str_len(&path); - - if ((error = git_str_joinpath(&path, path.ptr, HEAD_NAME_FILE)) < 0 || - (error = git_futils_readbuffer(&orig_head_name, path.ptr)) < 0) + if ((error = rebase_readfile(&orig_head_name, rebase, HEAD_NAME_FILE)) < 0) goto done; git_str_rtrim(&orig_head_name); @@ -346,36 +354,16 @@ int git_rebase_open( if (strcmp(ORIG_DETACHED_HEAD, orig_head_name.ptr) == 0) rebase->head_detached = 1; - git_str_truncate(&path, state_path_len); - - if ((error = git_str_joinpath(&path, path.ptr, ORIG_HEAD_FILE)) < 0) - goto done; - - if (!git_fs_path_isfile(path.ptr)) { + if ((error = rebase_readoid(&rebase->orig_head_id, &orig_head_id, rebase, ORIG_HEAD_FILE)) < 0) { /* Previous versions of git.git used 'head' here; support that. */ - git_str_truncate(&path, state_path_len); + if (error == GIT_ENOTFOUND) + error = rebase_readoid(&rebase->orig_head_id, &orig_head_id, rebase, HEAD_FILE); - if ((error = git_str_joinpath(&path, path.ptr, HEAD_FILE)) < 0) + if (error < 0) goto done; } - if ((error = git_futils_readbuffer(&orig_head_id, path.ptr)) < 0) - goto done; - - git_str_rtrim(&orig_head_id); - - if ((error = git_oid__fromstr(&rebase->orig_head_id, orig_head_id.ptr, GIT_OID_SHA1)) < 0) - goto done; - - git_str_truncate(&path, state_path_len); - - if ((error = git_str_joinpath(&path, path.ptr, ONTO_FILE)) < 0 || - (error = git_futils_readbuffer(&onto_id, path.ptr)) < 0) - goto done; - - git_str_rtrim(&onto_id); - - if ((error = git_oid__fromstr(&rebase->onto_id, onto_id.ptr, GIT_OID_SHA1)) < 0) + if ((error = rebase_readoid(&rebase->onto_id, &onto_id, rebase, ONTO_FILE)) < 0) goto done; if (!rebase->head_detached) @@ -403,7 +391,6 @@ done: else git_rebase_free(rebase); - git_str_dispose(&path); git_str_dispose(&orig_head_name); git_str_dispose(&orig_head_id); git_str_dispose(&onto_id); @@ -453,13 +440,13 @@ static const char *rebase_onto_name(const git_annotated_commit *onto) static int rebase_setupfiles_merge(git_rebase *rebase) { git_str commit_filename = GIT_STR_INIT; - char id_str[GIT_OID_SHA1_HEXSIZE]; + char id_str[GIT_OID_MAX_HEXSIZE + 1]; git_rebase_operation *operation; size_t i; int error = 0; if ((error = rebase_setupfile(rebase, END_FILE, 0, "%" PRIuZ "\n", git_array_size(rebase->operations))) < 0 || - (error = rebase_setupfile(rebase, ONTO_NAME_FILE, 0, "%s\n", rebase->onto_name)) < 0) + (error = rebase_setupfile(rebase, ONTO_NAME_FILE, 0, "%s\n", rebase->onto_name)) < 0) goto done; for (i = 0; i < git_array_size(rebase->operations); i++) { @@ -468,10 +455,9 @@ static int rebase_setupfiles_merge(git_rebase *rebase) git_str_clear(&commit_filename); git_str_printf(&commit_filename, CMT_FILE_FMT, i+1); - git_oid_fmt(id_str, &operation->id); + git_oid_tostr(id_str, GIT_OID_MAX_HEXSIZE + 1, &operation->id); - if ((error = rebase_setupfile(rebase, commit_filename.ptr, 0, - "%.*s\n", GIT_OID_SHA1_HEXSIZE, id_str)) < 0) + if ((error = rebase_setupfile(rebase, commit_filename.ptr, 0, "%s\n", id_str)) < 0) goto done; } @@ -482,11 +468,11 @@ done: static int rebase_setupfiles(git_rebase *rebase) { - char onto[GIT_OID_SHA1_HEXSIZE], orig_head[GIT_OID_SHA1_HEXSIZE]; + char onto[GIT_OID_MAX_HEXSIZE + 1], orig_head[GIT_OID_MAX_HEXSIZE + 1]; const char *orig_head_name; - git_oid_fmt(onto, &rebase->onto_id); - git_oid_fmt(orig_head, &rebase->orig_head_id); + git_oid_tostr(onto, GIT_OID_MAX_HEXSIZE + 1, &rebase->onto_id); + git_oid_tostr(orig_head, GIT_OID_MAX_HEXSIZE + 1, &rebase->orig_head_id); if (p_mkdir(rebase->state_path, REBASE_DIR_MODE) < 0) { git_error_set(GIT_ERROR_OS, "failed to create rebase directory '%s'", rebase->state_path); @@ -498,8 +484,8 @@ static int rebase_setupfiles(git_rebase *rebase) if (git_repository__set_orig_head(rebase->repo, &rebase->orig_head_id) < 0 || rebase_setupfile(rebase, HEAD_NAME_FILE, 0, "%s\n", orig_head_name) < 0 || - rebase_setupfile(rebase, ONTO_FILE, 0, "%.*s\n", GIT_OID_SHA1_HEXSIZE, onto) < 0 || - rebase_setupfile(rebase, ORIG_HEAD_FILE, 0, "%.*s\n", GIT_OID_SHA1_HEXSIZE, orig_head) < 0 || + rebase_setupfile(rebase, ONTO_FILE, 0, "%s\n", onto) < 0 || + rebase_setupfile(rebase, ORIG_HEAD_FILE, 0, "%s\n", orig_head) < 0 || rebase_setupfile(rebase, QUIET_FILE, 0, rebase->quiet ? "t\n" : "\n") < 0) return -1; @@ -644,7 +630,8 @@ static int rebase_init_merge( GIT_UNUSED(upstream); - if ((error = git_str_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0) + if ((error = git_str_joinpath(&state_path, repo->gitdir, REBASE_MERGE_DIR)) < 0 || + (error = git_str_put(&rebase->state_filename, state_path.ptr, state_path.size)) < 0) goto done; rebase->state_path = git_str_detach(&state_path); @@ -814,7 +801,7 @@ static int rebase_next_merge( git_indexwriter indexwriter = GIT_INDEXWRITER_INIT; git_rebase_operation *operation; git_checkout_options checkout_opts; - char current_idstr[GIT_OID_SHA1_HEXSIZE]; + char current_idstr[GIT_OID_MAX_HEXSIZE + 1]; unsigned int parent_count; int error; @@ -837,13 +824,13 @@ static int rebase_next_merge( goto done; } - git_oid_fmt(current_idstr, &operation->id); + git_oid_tostr(current_idstr, GIT_OID_MAX_HEXSIZE + 1, &operation->id); normalize_checkout_options_for_apply(&checkout_opts, rebase, current_commit); if ((error = git_indexwriter_init_for_operation(&indexwriter, rebase->repo, &checkout_opts.checkout_strategy)) < 0 || (error = rebase_setupfile(rebase, MSGNUM_FILE, 0, "%" PRIuZ "\n", rebase->current+1)) < 0 || - (error = rebase_setupfile(rebase, CURRENT_FILE, 0, "%.*s\n", GIT_OID_SHA1_HEXSIZE, current_idstr)) < 0 || + (error = rebase_setupfile(rebase, CURRENT_FILE, 0, "%s\n", current_idstr)) < 0 || (error = git_merge_trees(&index, rebase->repo, parent_tree, head_tree, current_tree, &rebase->options.merge_options)) < 0 || (error = git_merge__check_result(rebase->repo, index)) < 0 || (error = git_checkout_index(rebase->repo, index, &checkout_opts)) < 0 || @@ -1103,7 +1090,7 @@ static int rebase_commit_merge( git_reference *head = NULL; git_commit *head_commit = NULL, *commit = NULL; git_index *index = NULL; - char old_idstr[GIT_OID_SHA1_HEXSIZE], new_idstr[GIT_OID_SHA1_HEXSIZE]; + char old_idstr[GIT_OID_MAX_HEXSIZE + 1], new_idstr[GIT_OID_MAX_HEXSIZE + 1]; int error; operation = git_array_get(rebase->operations, rebase->current); @@ -1119,11 +1106,11 @@ static int rebase_commit_merge( rebase->repo, NULL, "HEAD", git_commit_id(commit), "rebase")) < 0) goto done; - git_oid_fmt(old_idstr, &operation->id); - git_oid_fmt(new_idstr, git_commit_id(commit)); + git_oid_tostr(old_idstr, GIT_OID_MAX_HEXSIZE + 1, &operation->id); + git_oid_tostr(new_idstr, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); if ((error = rebase_setupfile(rebase, REWRITTEN_FILE, O_CREAT|O_WRONLY|O_APPEND, - "%.*s %.*s\n", GIT_OID_SHA1_HEXSIZE, old_idstr, GIT_OID_SHA1_HEXSIZE, new_idstr)) < 0) + "%s %s\n", old_idstr, new_idstr)) < 0) goto done; git_oid_cpy(commit_id, git_commit_id(commit)); @@ -1306,7 +1293,9 @@ static int rebase_copy_notes( git_rebase *rebase, const git_signature *committer) { - git_str path = GIT_STR_INIT, rewritten = GIT_STR_INIT, notes_ref = GIT_STR_INIT; + git_str path = GIT_STR_INIT, + rewritten = GIT_STR_INIT, + notes_ref = GIT_STR_INIT; char *pair_list, *fromstr, *tostr, *end; git_oid from, to; unsigned int linenum = 1; @@ -1342,10 +1331,10 @@ static int rebase_copy_notes( tostr = end+1; *end = '\0'; - if (strlen(fromstr) != GIT_OID_SHA1_HEXSIZE || - strlen(tostr) != GIT_OID_SHA1_HEXSIZE || - git_oid__fromstr(&from, fromstr, GIT_OID_SHA1) < 0 || - git_oid__fromstr(&to, tostr, GIT_OID_SHA1) < 0) + if (strlen(fromstr) != git_oid_hexsize(rebase->repo->oid_type) || + strlen(tostr) != git_oid_hexsize(rebase->repo->oid_type) || + git_oid__fromstr(&from, fromstr, rebase->repo->oid_type) < 0 || + git_oid__fromstr(&to, tostr, rebase->repo->oid_type) < 0) goto on_error; if ((error = rebase_copy_note(rebase, notes_ref.ptr, &from, &to, committer)) < 0) @@ -1373,17 +1362,15 @@ static int return_to_orig_head(git_rebase *rebase) git_reference *terminal_ref = NULL, *branch_ref = NULL, *head_ref = NULL; git_commit *terminal_commit = NULL; git_str branch_msg = GIT_STR_INIT, head_msg = GIT_STR_INIT; - char onto[GIT_OID_SHA1_HEXSIZE]; + char onto[GIT_OID_MAX_HEXSIZE + 1]; int error = 0; - git_oid_fmt(onto, &rebase->onto_id); + git_oid_tostr(onto, GIT_OID_MAX_HEXSIZE + 1, &rebase->onto_id); if ((error = git_str_printf(&branch_msg, - "rebase finished: %s onto %.*s", - rebase->orig_head_name, GIT_OID_SHA1_HEXSIZE, onto)) == 0 && + "rebase finished: %s onto %s", rebase->orig_head_name, onto)) == 0 && (error = git_str_printf(&head_msg, - "rebase finished: returning to %s", - rebase->orig_head_name)) == 0 && + "rebase finished: returning to %s", rebase->orig_head_name)) == 0 && (error = git_repository_head(&terminal_ref, rebase->repo)) == 0 && (error = git_reference_peel((git_object **)&terminal_commit, terminal_ref, GIT_OBJECT_COMMIT)) == 0 && @@ -1475,6 +1462,7 @@ void git_rebase_free(git_rebase *rebase) git__free(rebase->onto_name); git__free(rebase->orig_head_name); git__free(rebase->state_path); + git_str_dispose(&rebase->state_filename); git_array_clear(rebase->operations); git__free((char *)rebase->options.rewrite_notes_ref); git__free(rebase); diff --git a/src/libgit2/refdb_fs.c b/src/libgit2/refdb_fs.c index 9ce1a9608..c5b292ae3 100644 --- a/src/libgit2/refdb_fs.c +++ b/src/libgit2/refdb_fs.c @@ -805,7 +805,9 @@ static void refdb_fs_backend__iterator_free(git_reference_iterator *_iter) git__free(iter); } -static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter) +static int iter_load_loose_paths( + refdb_fs_backend *backend, + refdb_fs_iter *iter) { int error = 0; git_str path = GIT_STR_INIT; @@ -819,6 +821,7 @@ static int iter_load_loose_paths(refdb_fs_backend *backend, refdb_fs_iter *iter) return 0; fsit_opts.flags = backend->iterator_flags; + fsit_opts.oid_type = backend->oid_type; if (iter->glob) { const char *last_sep = NULL; @@ -1949,9 +1952,9 @@ static int reflog_parse(git_reflog *log, const char *buf, size_t buf_size) entry->committer = git__calloc(1, sizeof(*entry->committer)); GIT_ERROR_CHECK_ALLOC(entry->committer); - if (git_parse_advance_oid(&entry->oid_old, &parser) < 0 || + if (git_parse_advance_oid(&entry->oid_old, &parser, log->oid_type) < 0 || git_parse_advance_expected(&parser, " ", 1) < 0 || - git_parse_advance_oid(&entry->oid_cur, &parser) < 0) + git_parse_advance_oid(&entry->oid_cur, &parser, log->oid_type) < 0) goto next; sig = parser.line; diff --git a/src/libgit2/refs.c b/src/libgit2/refs.c index 8e4abaccc..5e2fe97e7 100644 --- a/src/libgit2/refs.c +++ b/src/libgit2/refs.c @@ -72,6 +72,7 @@ git_reference *git_reference__alloc( const git_oid *oid, const git_oid *peel) { + git_oid_t oid_type; git_reference *ref; GIT_ASSERT_ARG_WITH_RETVAL(name, NULL); @@ -84,10 +85,16 @@ git_reference *git_reference__alloc( ref->type = GIT_REFERENCE_DIRECT; git_oid_cpy(&ref->target.oid, oid); +#ifdef GIT_EXPERIMENTAL_SHA256 + oid_type = oid->type; +#else + oid_type = GIT_OID_SHA1; +#endif + if (peel != NULL) git_oid_cpy(&ref->peel, peel); else - git_oid_clear(&ref->peel, GIT_OID_SHA1); + git_oid_clear(&ref->peel, oid_type); return ref; } diff --git a/src/libgit2/remote.c b/src/libgit2/remote.c index e1bc6c694..ef414209f 100644 --- a/src/libgit2/remote.c +++ b/src/libgit2/remote.c @@ -1632,7 +1632,10 @@ int git_remote_prune(git_remote *remote, const git_remote_callbacks *callbacks) const git_refspec *spec; const char *refname; int error; - git_oid zero_id = GIT_OID_SHA1_ZERO; + git_oid zero_id; + + GIT_ASSERT(remote && remote->repo); + git_oid_clear(&zero_id, remote->repo->oid_type); if (callbacks) GIT_ERROR_CHECK_VERSION(callbacks, GIT_REMOTE_CALLBACKS_VERSION, "git_remote_callbacks"); @@ -1734,9 +1737,12 @@ static int update_ref( const git_remote_callbacks *callbacks) { git_reference *ref; - git_oid old_id = GIT_OID_SHA1_ZERO; + git_oid old_id; int error; + GIT_ASSERT(remote && remote->repo); + git_oid_clear(&old_id, remote->repo->oid_type); + error = git_reference_name_to_id(&old_id, remote->repo, ref_name); if (error < 0 && error != GIT_ENOTFOUND) @@ -1780,6 +1786,8 @@ static int update_one_tip( int valid; int error; + GIT_ASSERT(remote && remote->repo); + if ((error = git_repository_odb__weakptr(&odb, remote->repo)) < 0) goto done; @@ -1840,7 +1848,7 @@ static int update_one_tip( } if (error == GIT_ENOTFOUND) { - git_oid_clear(&old, GIT_OID_SHA1); + git_oid_clear(&old, remote->repo->oid_type); error = 0; if (autotag && (error = git_vector_insert(update_heads, head)) < 0) @@ -1886,7 +1894,7 @@ static int update_tips_for_spec( int error = 0; size_t i; - GIT_ASSERT_ARG(remote); + GIT_ASSERT_ARG(remote && remote->repo); if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0) return -1; @@ -1902,10 +1910,10 @@ static int update_tips_for_spec( } /* Handle specified oid sources */ - if (git_oid__is_hexstr(spec->src, GIT_OID_SHA1)) { + if (git_oid__is_hexstr(spec->src, remote->repo->oid_type)) { git_oid id; - if ((error = git_oid__fromstr(&id, spec->src, GIT_OID_SHA1)) < 0) + if ((error = git_oid__fromstr(&id, spec->src, remote->repo->oid_type)) < 0) goto on_error; if (spec->dst && diff --git a/src/libgit2/repository.c b/src/libgit2/repository.c index bb6ae1b0f..1e356c84e 100644 --- a/src/libgit2/repository.c +++ b/src/libgit2/repository.c @@ -67,7 +67,7 @@ static const struct { static int check_repositoryformatversion(int *version, git_config *config); static int check_extensions(git_config *config, int version); -static int load_global_config(git_config **config); +static int load_global_config(git_config **config, bool use_env); static int load_objectformat(git_repository *repo, git_config *config); #define GIT_COMMONDIR_FILE "commondir" @@ -194,11 +194,23 @@ void git_repository_free(git_repository *repo) } /* Check if we have a separate commondir (e.g. we have a worktree) */ -static int lookup_commondir(bool *separate, git_str *commondir, git_str *repository_path) +static int lookup_commondir( + bool *separate, + git_str *commondir, + git_str *repository_path, + uint32_t flags) { - git_str common_link = GIT_STR_INIT; + git_str common_link = GIT_STR_INIT; int error; + /* Environment variable overrides configuration */ + if ((flags & GIT_REPOSITORY_OPEN_FROM_ENV)) { + error = git__getenv(commondir, "GIT_COMMON_DIR"); + + if (!error || error != GIT_ENOTFOUND) + goto done; + } + /* * If there's no commondir file, the repository path is the * common path, but it needs a trailing slash. @@ -225,12 +237,11 @@ static int lookup_commondir(bool *separate, git_str *commondir, git_str *reposit git_str_swap(commondir, &common_link); } - git_str_dispose(&common_link); - /* Make sure the commondir path always has a trailing slash */ error = git_fs_path_prettify_dir(commondir, commondir->ptr, NULL); done: + git_str_dispose(&common_link); return error; } @@ -255,14 +266,19 @@ GIT_INLINE(int) validate_repo_path(git_str *path) * * Open a repository object from its path */ -static int is_valid_repository_path(bool *out, git_str *repository_path, git_str *common_path) +static int is_valid_repository_path( + bool *out, + git_str *repository_path, + git_str *common_path, + uint32_t flags) { bool separate_commondir = false; int error; *out = false; - if ((error = lookup_commondir(&separate_commondir, common_path, repository_path)) < 0) + if ((error = lookup_commondir(&separate_commondir, + common_path, repository_path, flags)) < 0) return error; /* Ensure HEAD file exists */ @@ -340,19 +356,42 @@ static int load_config_data(git_repository *repo, const git_config *config) return 0; } -static int load_workdir(git_repository *repo, git_config *config, git_str *parent_path) +static int load_workdir( + git_repository *repo, + git_config *config, + git_str *parent_path) { - int error; - git_config_entry *ce; + git_config_entry *ce = NULL; git_str worktree = GIT_STR_INIT; git_str path = GIT_STR_INIT; + git_str workdir_env = GIT_STR_INIT; + const char *value = NULL; + int error; if (repo->is_bare) return 0; - if ((error = git_config__lookup_entry( - &ce, config, "core.worktree", false)) < 0) - return error; + /* Environment variables are preferred */ + if (repo->use_env) { + error = git__getenv(&workdir_env, "GIT_WORK_TREE"); + + if (error == 0) + value = workdir_env.ptr; + else if (error == GIT_ENOTFOUND) + error = 0; + else + goto cleanup; + } + + /* Examine configuration values if necessary */ + if (!value) { + if ((error = git_config__lookup_entry(&ce, config, + "core.worktree", false)) < 0) + return error; + + if (ce && ce->value) + value = ce->value; + } if (repo->is_worktree) { char *gitlink = git_worktree__read_link(repo->gitdir, GIT_GITDIR_FILE); @@ -370,17 +409,21 @@ static int load_workdir(git_repository *repo, git_config *config, git_str *paren } repo->workdir = git_str_detach(&worktree); - } - else if (ce && ce->value) { - if ((error = git_fs_path_prettify_dir( - &worktree, ce->value, repo->gitdir)) < 0) + } else if (value) { + if (!*value) { + git_error_set(GIT_ERROR_NET, "working directory cannot be set to empty path"); + error = -1; + goto cleanup; + } + + if ((error = git_fs_path_prettify_dir(&worktree, + value, repo->gitdir)) < 0) goto cleanup; repo->workdir = git_str_detach(&worktree); - } - else if (parent_path && git_fs_path_isdir(parent_path->ptr)) + } else if (parent_path && git_fs_path_isdir(parent_path->ptr)) { repo->workdir = git_str_detach(parent_path); - else { + } else { if (git_fs_path_dirname_r(&worktree, repo->gitdir) < 0 || git_fs_path_to_dir(&worktree) < 0) { error = -1; @@ -391,8 +434,10 @@ static int load_workdir(git_repository *repo, git_config *config, git_str *paren } GIT_ERROR_CHECK_ALLOC(repo->workdir); + cleanup: git_str_dispose(&path); + git_str_dispose(&workdir_env); git_config_entry_free(ce); return error; } @@ -544,7 +589,10 @@ static int validate_ownership_cb(const git_config_entry *entry, void *payload) return 0; } -static int validate_ownership_config(bool *is_safe, const char *path) +static int validate_ownership_config( + bool *is_safe, + const char *path, + bool use_env) { validate_ownership_data ownership_data = { path, GIT_STR_INIT, is_safe @@ -552,7 +600,7 @@ static int validate_ownership_config(bool *is_safe, const char *path) git_config *config; int error; - if (load_global_config(&config) != 0) + if (load_global_config(&config, use_env) != 0) return 0; error = git_config_get_multivar_foreach(config, @@ -626,7 +674,8 @@ static int validate_ownership(git_repository *repo) } if (is_safe || - (error = validate_ownership_config(&is_safe, validation_paths[0])) < 0) + (error = validate_ownership_config( + &is_safe, validation_paths[0], repo->use_env)) < 0) goto done; if (!is_safe) { @@ -640,14 +689,28 @@ done: return error; } -static int find_repo( - git_str *gitdir_path, - git_str *workdir_path, - git_str *gitlink_path, - git_str *commondir_path, +struct repo_paths { + git_str gitdir; + git_str workdir; + git_str gitlink; + git_str commondir; +}; + +#define REPO_PATHS_INIT { GIT_STR_INIT } + +GIT_INLINE(void) repo_paths_dispose(struct repo_paths *paths) +{ + git_str_dispose(&paths->gitdir); + git_str_dispose(&paths->workdir); + git_str_dispose(&paths->gitlink); + git_str_dispose(&paths->commondir); +} + +static int find_repo_traverse( + struct repo_paths *out, const char *start_path, - uint32_t flags, - const char *ceiling_dirs) + const char *ceiling_dirs, + uint32_t flags) { git_str path = GIT_STR_INIT; git_str repo_link = GIT_STR_INIT; @@ -659,19 +722,23 @@ static int find_repo( size_t ceiling_offset = 0; int error; - git_str_clear(gitdir_path); + git_str_clear(&out->gitdir); - error = git_fs_path_prettify(&path, start_path, NULL); - if (error < 0) + if ((error = git_fs_path_prettify(&path, start_path, NULL)) < 0) return error; - /* in_dot_git toggles each loop: + /* + * In each loop we look first for a `.git` dir within the + * directory, then to see if the directory itself is a repo. + * + * In other words: if we start in /a/b/c, then we look at: * /a/b/c/.git, /a/b/c, /a/b/.git, /a/b, /a/.git, /a - * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, we - * assume we started with /a/b/c.git and don't append .git the first - * time through. - * min_iterations indicates the number of iterations left before going - * further counts as a search. */ + * + * With GIT_REPOSITORY_OPEN_BARE or GIT_REPOSITORY_OPEN_NO_DOTGIT, + * we assume we started with /a/b/c.git and don't append .git the + * first time through. min_iterations indicates the number of + * iterations left before going further counts as a search. + */ if (flags & (GIT_REPOSITORY_OPEN_BARE | GIT_REPOSITORY_OPEN_NO_DOTGIT)) { in_dot_git = true; min_iterations = 1; @@ -698,48 +765,51 @@ static int find_repo( break; if (S_ISDIR(st.st_mode)) { - if ((error = is_valid_repository_path(&is_valid, &path, &common_link)) < 0) + if ((error = is_valid_repository_path(&is_valid, &path, &common_link, flags)) < 0) goto out; if (is_valid) { if ((error = git_fs_path_to_dir(&path)) < 0 || - (error = git_str_set(gitdir_path, path.ptr, path.size)) < 0) + (error = git_str_set(&out->gitdir, path.ptr, path.size)) < 0) + goto out; + + if ((error = git_str_attach(&out->gitlink, git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0)) < 0) goto out; - if (gitlink_path) - if ((error = git_str_attach(gitlink_path, git_worktree__read_link(path.ptr, GIT_GITDIR_FILE), 0)) < 0) - goto out; - if (commondir_path) - git_str_swap(&common_link, commondir_path); + git_str_swap(&common_link, &out->commondir); break; } } else if (S_ISREG(st.st_mode) && git__suffixcmp(path.ptr, "/" DOT_GIT) == 0) { if ((error = read_gitfile(&repo_link, path.ptr)) < 0 || - (error = is_valid_repository_path(&is_valid, &repo_link, &common_link)) < 0) + (error = is_valid_repository_path(&is_valid, &repo_link, &common_link, flags)) < 0) goto out; if (is_valid) { - git_str_swap(gitdir_path, &repo_link); + git_str_swap(&out->gitdir, &repo_link); - if (gitlink_path) - if ((error = git_str_put(gitlink_path, path.ptr, path.size)) < 0) - goto out; - if (commondir_path) - git_str_swap(&common_link, commondir_path); + if ((error = git_str_put(&out->gitlink, path.ptr, path.size)) < 0) + goto out; + + git_str_swap(&common_link, &out->commondir); } break; } } - /* Move up one directory. If we're in_dot_git, we'll search the - * parent itself next. If we're !in_dot_git, we'll search .git - * in the parent directory next (added at the top of the loop). */ + /* + * Move up one directory. If we're in_dot_git, we'll + * search the parent itself next. If we're !in_dot_git, + * we'll search .git in the parent directory next (added + * at the top of the loop). + */ if ((error = git_fs_path_dirname_r(&path, path.ptr)) < 0) goto out; - /* Once we've checked the directory (and .git if applicable), - * find the ceiling for a search. */ + /* + * Once we've checked the directory (and .git if + * applicable), find the ceiling for a search. + */ if (min_iterations && (--min_iterations == 0)) ceiling_offset = find_ceiling_dir_offset(path.ptr, ceiling_dirs); @@ -749,23 +819,26 @@ static int find_repo( break; } - if (workdir_path && !(flags & GIT_REPOSITORY_OPEN_BARE)) { - if (!git_str_len(gitdir_path)) - git_str_clear(workdir_path); - else if ((error = git_fs_path_dirname_r(workdir_path, path.ptr)) < 0 || - (error = git_fs_path_to_dir(workdir_path)) < 0) + if (!(flags & GIT_REPOSITORY_OPEN_BARE)) { + if (!git_str_len(&out->gitdir)) + git_str_clear(&out->workdir); + else if ((error = git_fs_path_dirname_r(&out->workdir, path.ptr)) < 0 || + (error = git_fs_path_to_dir(&out->workdir)) < 0) goto out; } - /* If we didn't find the repository, and we don't have any other error - * to report, report that. */ - if (!git_str_len(gitdir_path)) { - git_error_set(GIT_ERROR_REPOSITORY, "could not find repository from '%s'", start_path); + /* If we didn't find the repository, and we don't have any other + * error to report, report that. */ + if (!git_str_len(&out->gitdir)) { + git_error_set(GIT_ERROR_REPOSITORY, "could not find repository at '%s'", start_path); error = GIT_ENOTFOUND; goto out; } out: + if (error) + repo_paths_dispose(out); + git_str_dispose(&path); git_str_dispose(&repo_link); git_str_dispose(&common_link); @@ -793,6 +866,70 @@ error: return error; } +static int find_repo( + struct repo_paths *out, + const char *start_path, + const char *ceiling_dirs, + uint32_t flags) +{ + bool use_env = !!(flags & GIT_REPOSITORY_OPEN_FROM_ENV); + git_str gitdir_buf = GIT_STR_INIT, + ceiling_dirs_buf = GIT_STR_INIT, + across_fs_buf = GIT_STR_INIT; + int error; + + if (use_env && !start_path) { + error = git__getenv(&gitdir_buf, "GIT_DIR"); + + if (!error) { + start_path = gitdir_buf.ptr; + flags |= GIT_REPOSITORY_OPEN_NO_SEARCH; + flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT; + } else if (error == GIT_ENOTFOUND) { + start_path = "."; + } else { + goto done; + } + } + + if (use_env && !ceiling_dirs) { + error = git__getenv(&ceiling_dirs_buf, + "GIT_CEILING_DIRECTORIES"); + + if (!error) + ceiling_dirs = ceiling_dirs_buf.ptr; + else if (error != GIT_ENOTFOUND) + goto done; + } + + if (use_env) { + error = git__getenv(&across_fs_buf, + "GIT_DISCOVERY_ACROSS_FILESYSTEM"); + + if (!error) { + int across_fs = 0; + + if ((error = git_config_parse_bool(&across_fs, + git_str_cstr(&across_fs_buf))) < 0) + goto done; + + if (across_fs) + flags |= GIT_REPOSITORY_OPEN_CROSS_FS; + } else if (error != GIT_ENOTFOUND) { + goto done; + } + } + + error = find_repo_traverse(out, start_path, ceiling_dirs, flags); + +done: + git_str_dispose(&gitdir_buf); + git_str_dispose(&ceiling_dirs_buf); + git_str_dispose(&across_fs_buf); + + return error; +} + static int obtain_config_and_set_oid_type( git_config **config_ptr, git_repository *repo) @@ -811,7 +948,7 @@ static int obtain_config_and_set_oid_type( goto out; if (config && - (error = check_repositoryformatversion(&version, config)) < 0) + (error = check_repositoryformatversion(&version, config)) < 0) goto out; if ((error = check_extensions(config, version)) < 0) @@ -821,7 +958,7 @@ static int obtain_config_and_set_oid_type( if ((error = load_objectformat(repo, config)) < 0) goto out; } else { - repo->oid_type = GIT_OID_SHA1; + repo->oid_type = GIT_OID_DEFAULT; } out: @@ -841,7 +978,7 @@ int git_repository_open_bare( git_config *config; if ((error = git_fs_path_prettify_dir(&path, bare_path, NULL)) < 0 || - (error = is_valid_repository_path(&is_valid, &path, &common_path)) < 0) + (error = is_valid_repository_path(&is_valid, &path, &common_path, 0)) < 0) return error; if (!is_valid) { @@ -875,173 +1012,22 @@ cleanup: return error; } -static int _git_repository_open_ext_from_env( - git_repository **out, - const char *start_path) +static int repo_load_namespace(git_repository *repo) { - git_repository *repo = NULL; - git_index *index = NULL; - git_odb *odb = NULL; - git_str dir_buf = GIT_STR_INIT; - git_str ceiling_dirs_buf = GIT_STR_INIT; - git_str across_fs_buf = GIT_STR_INIT; - git_str index_file_buf = GIT_STR_INIT; git_str namespace_buf = GIT_STR_INIT; - git_str object_dir_buf = GIT_STR_INIT; - git_str alts_buf = GIT_STR_INIT; - git_str work_tree_buf = GIT_STR_INIT; - git_str common_dir_buf = GIT_STR_INIT; - const char *ceiling_dirs = NULL; - unsigned flags = 0; int error; - if (!start_path) { - error = git__getenv(&dir_buf, "GIT_DIR"); - if (error == GIT_ENOTFOUND) { - git_error_clear(); - start_path = "."; - } else if (error < 0) - goto error; - else { - start_path = git_str_cstr(&dir_buf); - flags |= GIT_REPOSITORY_OPEN_NO_SEARCH; - flags |= GIT_REPOSITORY_OPEN_NO_DOTGIT; - } - } - - error = git__getenv(&ceiling_dirs_buf, "GIT_CEILING_DIRECTORIES"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else - ceiling_dirs = git_str_cstr(&ceiling_dirs_buf); - - error = git__getenv(&across_fs_buf, "GIT_DISCOVERY_ACROSS_FILESYSTEM"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else { - int across_fs = 0; - error = git_config_parse_bool(&across_fs, git_str_cstr(&across_fs_buf)); - if (error < 0) - goto error; - if (across_fs) - flags |= GIT_REPOSITORY_OPEN_CROSS_FS; - } - - error = git__getenv(&index_file_buf, "GIT_INDEX_FILE"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else { - error = git_index_open(&index, git_str_cstr(&index_file_buf)); - if (error < 0) - goto error; - } + if (!repo->use_env) + return 0; error = git__getenv(&namespace_buf, "GIT_NAMESPACE"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - - error = git__getenv(&object_dir_buf, "GIT_OBJECT_DIRECTORY"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else { - error = git_odb__open(&odb, git_str_cstr(&object_dir_buf), NULL); - if (error < 0) - goto error; - } - error = git__getenv(&work_tree_buf, "GIT_WORK_TREE"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else { - git_error_set(GIT_ERROR_INVALID, "GIT_WORK_TREE unimplemented"); - error = GIT_ERROR; - goto error; - } - - error = git__getenv(&work_tree_buf, "GIT_COMMON_DIR"); - if (error == GIT_ENOTFOUND) - git_error_clear(); - else if (error < 0) - goto error; - else { - git_error_set(GIT_ERROR_INVALID, "GIT_COMMON_DIR unimplemented"); - error = GIT_ERROR; - goto error; - } - - error = git_repository_open_ext(&repo, start_path, flags, ceiling_dirs); - if (error < 0) - goto error; - - if (odb) - git_repository_set_odb(repo, odb); - - error = git__getenv(&alts_buf, "GIT_ALTERNATE_OBJECT_DIRECTORIES"); - if (error == GIT_ENOTFOUND) { - git_error_clear(); - error = 0; - } else if (error < 0) - goto error; - else { - const char *end; - char *alt, *sep; - if (!odb) { - error = git_repository_odb(&odb, repo); - if (error < 0) - goto error; - } - - end = git_str_cstr(&alts_buf) + git_str_len(&alts_buf); - for (sep = alt = alts_buf.ptr; sep != end; alt = sep+1) { - for (sep = alt; *sep && *sep != GIT_PATH_LIST_SEPARATOR; sep++) - ; - if (*sep) - *sep = '\0'; - error = git_odb_add_disk_alternate(odb, alt); - if (error < 0) - goto error; - } - } - - if (git_str_len(&namespace_buf)) { - error = git_repository_set_namespace(repo, git_str_cstr(&namespace_buf)); - if (error < 0) - goto error; - } - - git_repository_set_index(repo, index); + if (error == 0) + repo->namespace = git_str_detach(&namespace_buf); + else if (error != GIT_ENOTFOUND) + return error; - if (out) { - *out = repo; - goto success; - } -error: - git_repository_free(repo); -success: - git_odb_free(odb); - git_index_free(index); - git_str_dispose(&common_dir_buf); - git_str_dispose(&work_tree_buf); - git_str_dispose(&alts_buf); - git_str_dispose(&object_dir_buf); - git_str_dispose(&namespace_buf); - git_str_dispose(&index_file_buf); - git_str_dispose(&across_fs_buf); - git_str_dispose(&ceiling_dirs_buf); - git_str_dispose(&dir_buf); - return error; + return 0; } static int repo_is_worktree(unsigned *out, const git_repository *repo) @@ -1073,21 +1059,16 @@ int git_repository_open_ext( unsigned int flags, const char *ceiling_dirs) { - int error; - unsigned is_worktree; - git_str gitdir = GIT_STR_INIT, workdir = GIT_STR_INIT, - gitlink = GIT_STR_INIT, commondir = GIT_STR_INIT; + struct repo_paths paths = { GIT_STR_INIT }; git_repository *repo = NULL; git_config *config = NULL; - - if (flags & GIT_REPOSITORY_OPEN_FROM_ENV) - return _git_repository_open_ext_from_env(repo_ptr, start_path); + unsigned is_worktree; + int error; if (repo_ptr) *repo_ptr = NULL; - error = find_repo( - &gitdir, &workdir, &gitlink, &commondir, start_path, flags, ceiling_dirs); + error = find_repo(&paths, start_path, ceiling_dirs, flags); if (error < 0 || !repo_ptr) goto cleanup; @@ -1095,20 +1076,23 @@ int git_repository_open_ext( repo = repository_alloc(); GIT_ERROR_CHECK_ALLOC(repo); - repo->gitdir = git_str_detach(&gitdir); + repo->use_env = !!(flags & GIT_REPOSITORY_OPEN_FROM_ENV); + + repo->gitdir = git_str_detach(&paths.gitdir); GIT_ERROR_CHECK_ALLOC(repo->gitdir); - if (gitlink.size) { - repo->gitlink = git_str_detach(&gitlink); + if (paths.gitlink.size) { + repo->gitlink = git_str_detach(&paths.gitlink); GIT_ERROR_CHECK_ALLOC(repo->gitlink); } - if (commondir.size) { - repo->commondir = git_str_detach(&commondir); + if (paths.commondir.size) { + repo->commondir = git_str_detach(&paths.commondir); GIT_ERROR_CHECK_ALLOC(repo->commondir); } if ((error = repo_is_worktree(&is_worktree, repo)) < 0) goto cleanup; + repo->is_worktree = is_worktree; error = obtain_config_and_set_oid_type(&config, repo); @@ -1123,10 +1107,13 @@ int git_repository_open_ext( } else { if (config && ((error = load_config_data(repo, config)) < 0 || - (error = load_workdir(repo, config, &workdir)) < 0)) + (error = load_workdir(repo, config, &paths.workdir)) < 0)) goto cleanup; } + if ((error = repo_load_namespace(repo)) < 0) + goto cleanup; + /* * Ensure that the git directory and worktree are * owned by the current user. @@ -1136,10 +1123,7 @@ int git_repository_open_ext( goto cleanup; cleanup: - git_str_dispose(&gitdir); - git_str_dispose(&workdir); - git_str_dispose(&gitlink); - git_str_dispose(&commondir); + repo_paths_dispose(&paths); git_config_free(config); if (error < 0) @@ -1207,11 +1191,17 @@ int git_repository_discover( int across_fs, const char *ceiling_dirs) { + struct repo_paths paths = { GIT_STR_INIT }; uint32_t flags = across_fs ? GIT_REPOSITORY_OPEN_CROSS_FS : 0; + int error; GIT_ASSERT_ARG(start_path); - GIT_BUF_WRAP_PRIVATE(out, find_repo, NULL, NULL, NULL, start_path, flags, ceiling_dirs); + if ((error = find_repo(&paths, start_path, ceiling_dirs, flags)) == 0) + error = git_buf_fromstr(out, &paths.gitdir); + + repo_paths_dispose(&paths); + return error; } static int load_config( @@ -1282,32 +1272,81 @@ static const char *path_unless_empty(git_str *buf) return git_str_len(buf) > 0 ? git_str_cstr(buf) : NULL; } +GIT_INLINE(int) config_path_system(git_str *out, bool use_env) +{ + if (use_env) { + git_str no_system_buf = GIT_STR_INIT; + int no_system = 0; + int error; + + error = git__getenv(&no_system_buf, "GIT_CONFIG_NOSYSTEM"); + + if (error && error != GIT_ENOTFOUND) + return error; + + error = git_config_parse_bool(&no_system, no_system_buf.ptr); + git_str_dispose(&no_system_buf); + + if (no_system) + return 0; + + error = git__getenv(out, "GIT_CONFIG_SYSTEM"); + + if (error == 0 || error != GIT_ENOTFOUND) + return 0; + } + + git_config__find_system(out); + return 0; +} + +GIT_INLINE(int) config_path_global(git_str *out, bool use_env) +{ + if (use_env) { + int error = git__getenv(out, "GIT_CONFIG_GLOBAL"); + + if (error == 0 || error != GIT_ENOTFOUND) + return 0; + } + + git_config__find_global(out); + return 0; +} + int git_repository_config__weakptr(git_config **out, git_repository *repo) { int error = 0; if (repo->_config == NULL) { + git_str system_buf = GIT_STR_INIT; git_str global_buf = GIT_STR_INIT; git_str xdg_buf = GIT_STR_INIT; - git_str system_buf = GIT_STR_INIT; git_str programdata_buf = GIT_STR_INIT; + bool use_env = repo->use_env; git_config *config; - git_config__find_global(&global_buf); - git_config__find_xdg(&xdg_buf); - git_config__find_system(&system_buf); - git_config__find_programdata(&programdata_buf); + if (!(error = config_path_system(&system_buf, use_env)) && + !(error = config_path_global(&global_buf, use_env))) { + git_config__find_xdg(&xdg_buf); + git_config__find_programdata(&programdata_buf); + } - /* If there is no global file, open a backend for it anyway */ - if (git_str_len(&global_buf) == 0) - git_config__global_location(&global_buf); + if (!error) { + /* + * If there is no global file, open a backend + * for it anyway. + */ + if (git_str_len(&global_buf) == 0) + git_config__global_location(&global_buf); + + error = load_config( + &config, repo, + path_unless_empty(&global_buf), + path_unless_empty(&xdg_buf), + path_unless_empty(&system_buf), + path_unless_empty(&programdata_buf)); + } - error = load_config( - &config, repo, - path_unless_empty(&global_buf), - path_unless_empty(&xdg_buf), - path_unless_empty(&system_buf), - path_unless_empty(&programdata_buf)); if (!error) { GIT_REFCOUNT_OWN(config, repo); @@ -1356,6 +1395,56 @@ int git_repository_set_config(git_repository *repo, git_config *config) return 0; } +static int repository_odb_path(git_str *out, git_repository *repo) +{ + int error = GIT_ENOTFOUND; + + if (repo->use_env) + error = git__getenv(out, "GIT_OBJECT_DIRECTORY"); + + if (error == GIT_ENOTFOUND) + error = git_repository__item_path(out, repo, + GIT_REPOSITORY_ITEM_OBJECTS); + + return error; +} + +static int repository_odb_alternates( + git_odb *odb, + git_repository *repo) +{ + git_str alternates = GIT_STR_INIT; + char *sep, *alt; + int error; + + if (!repo->use_env) + return 0; + + error = git__getenv(&alternates, "GIT_ALTERNATE_OBJECT_DIRECTORIES"); + + if (error != 0) + return (error == GIT_ENOTFOUND) ? 0 : error; + + alt = alternates.ptr; + + while (*alt) { + sep = strchr(alt, GIT_PATH_LIST_SEPARATOR); + + if (sep) + *sep = '\0'; + + error = git_odb_add_disk_alternate(odb, alt); + + if (sep) + alt = sep + 1; + else + break; + } + + git_str_dispose(&alternates); + return 0; +} + int git_repository_odb__weakptr(git_odb **out, git_repository *repo) { int error = 0; @@ -1371,9 +1460,9 @@ int git_repository_odb__weakptr(git_odb **out, git_repository *repo) odb_opts.oid_type = repo->oid_type; - if ((error = git_repository__item_path(&odb_path, repo, - GIT_REPOSITORY_ITEM_OBJECTS)) < 0 || - (error = git_odb__new(&odb, &odb_opts)) < 0) + if ((error = repository_odb_path(&odb_path, repo)) < 0 || + (error = git_odb__new(&odb, &odb_opts)) < 0 || + (error = repository_odb_alternates(odb, repo)) < 0) return error; GIT_REFCOUNT_OWN(odb, repo); @@ -1457,6 +1546,20 @@ int git_repository_set_refdb(git_repository *repo, git_refdb *refdb) return 0; } +static int repository_index_path(git_str *out, git_repository *repo) +{ + int error = GIT_ENOTFOUND; + + if (repo->use_env) + error = git__getenv(out, "GIT_INDEX_FILE"); + + if (error == GIT_ENOTFOUND) + error = git_repository__item_path(out, repo, + GIT_REPOSITORY_ITEM_INDEX); + + return error; +} + int git_repository_index__weakptr(git_index **out, git_repository *repo) { int error = 0; @@ -1468,10 +1571,11 @@ int git_repository_index__weakptr(git_index **out, git_repository *repo) git_str index_path = GIT_STR_INIT; git_index *index; - if ((error = git_str_joinpath(&index_path, repo->gitdir, GIT_INDEX_FILE)) < 0) + if ((error = repository_index_path(&index_path, repo)) < 0) return error; - error = git_index_open(&index, index_path.ptr); + error = git_index__open(&index, index_path.ptr, repo->oid_type); + if (!error) { GIT_REFCOUNT_OWN(index, repo); @@ -1673,7 +1777,7 @@ static const char *builtin_extensions[] = { "objectformat" }; -static git_vector user_extensions = GIT_VECTOR_INIT; +static git_vector user_extensions = { 0, git__strcmp_cb }; static int check_valid_extension(const git_config_entry *entry, void *payload) { @@ -1741,7 +1845,7 @@ static int load_objectformat(git_repository *repo, git_config *config) if ((error = git_config_get_entry(&entry, config, "extensions.objectformat")) < 0) { if (error == GIT_ENOTFOUND) { - repo->oid_type = GIT_OID_SHA1; + repo->oid_type = GIT_OID_DEFAULT; git_error_clear(); error = 0; } @@ -1814,7 +1918,7 @@ int git_repository__extensions(char ***out, size_t *out_len) char *extension; size_t i, j; - if (git_vector_init(&extensions, 8, NULL) < 0) + if (git_vector_init(&extensions, 8, git__strcmp_cb) < 0) return -1; for (i = 0; i < ARRAY_SIZE(builtin_extensions); i++) { @@ -1846,21 +1950,49 @@ int git_repository__extensions(char ***out, size_t *out_len) return -1; } + git_vector_sort(&extensions); + *out = (char **)git_vector_detach(out_len, NULL, &extensions); return 0; } +static int dup_ext_err(void **old, void *extension) +{ + GIT_UNUSED(old); + GIT_UNUSED(extension); + return GIT_EEXISTS; +} + int git_repository__set_extensions(const char **extensions, size_t len) { char *extension; - size_t i; + size_t i, j; + int error; git_repository__free_extensions(); for (i = 0; i < len; i++) { - if ((extension = git__strdup(extensions[i])) == NULL || - git_vector_insert(&user_extensions, extension) < 0) + bool is_builtin = false; + + for (j = 0; j < ARRAY_SIZE(builtin_extensions); j++) { + if (strcmp(builtin_extensions[j], extensions[i]) == 0) { + is_builtin = true; + break; + } + } + + if (is_builtin) + continue; + + if ((extension = git__strdup(extensions[i])) == NULL) return -1; + + if ((error = git_vector_insert_sorted(&user_extensions, extension, dup_ext_err)) < 0) { + git__free(extension); + + if (error != GIT_EEXISTS) + return -1; + } } return 0; @@ -1929,7 +2061,7 @@ static bool is_filesystem_case_insensitive(const char *gitdir_path) * Return a configuration object with only the global and system * configurations; no repository-level configuration. */ -static int load_global_config(git_config **config) +static int load_global_config(git_config **config, bool use_env) { git_str global_buf = GIT_STR_INIT; git_str xdg_buf = GIT_STR_INIT; @@ -1937,16 +2069,17 @@ static int load_global_config(git_config **config) git_str programdata_buf = GIT_STR_INIT; int error; - git_config__find_global(&global_buf); - git_config__find_xdg(&xdg_buf); - git_config__find_system(&system_buf); - git_config__find_programdata(&programdata_buf); + if (!(error = config_path_system(&system_buf, use_env)) && + !(error = config_path_global(&global_buf, use_env))) { + git_config__find_xdg(&xdg_buf); + git_config__find_programdata(&programdata_buf); - error = load_config(config, NULL, - path_unless_empty(&global_buf), - path_unless_empty(&xdg_buf), - path_unless_empty(&system_buf), - path_unless_empty(&programdata_buf)); + error = load_config(config, NULL, + path_unless_empty(&global_buf), + path_unless_empty(&xdg_buf), + path_unless_empty(&system_buf), + path_unless_empty(&programdata_buf)); + } git_str_dispose(&global_buf); git_str_dispose(&xdg_buf); @@ -1956,7 +2089,7 @@ static int load_global_config(git_config **config) return error; } -static bool are_symlinks_supported(const char *wd_path) +static bool are_symlinks_supported(const char *wd_path, bool use_env) { git_config *config = NULL; int symlinks = 0; @@ -1969,10 +2102,12 @@ static bool are_symlinks_supported(const char *wd_path) * _not_ set, then we do not test or enable symlink support. */ #ifdef GIT_WIN32 - if (load_global_config(&config) < 0 || + if (load_global_config(&config, use_env) < 0 || git_config_get_bool(&symlinks, config, "core.symlinks") < 0 || !symlinks) goto done; +#else + GIT_UNUSED(use_env); #endif if (!(symlinks = git_fs_path_supports_symlinks(wd_path))) @@ -2045,7 +2180,8 @@ static int repo_init_fs_configs( const char *cfg_path, const char *repo_dir, const char *work_dir, - bool update_ignorecase) + bool update_ignorecase, + bool use_env) { int error = 0; @@ -2056,7 +2192,7 @@ static int repo_init_fs_configs( cfg, "core.filemode", is_chmod_supported(cfg_path))) < 0) return error; - if (!are_symlinks_supported(work_dir)) { + if (!are_symlinks_supported(work_dir, use_env)) { if ((error = git_config_set_bool(cfg, "core.symlinks", false)) < 0) return error; } else if (git_config_delete_entry(cfg, "core.symlinks") < 0) @@ -2093,6 +2229,7 @@ static int repo_init_config( git_config *config = NULL; bool is_bare = ((flags & GIT_REPOSITORY_INIT_BARE) != 0); bool is_reinit = ((flags & GIT_REPOSITORY_INIT__IS_REINIT) != 0); + bool use_env = ((flags & GIT_REPOSITORY_OPEN_FROM_ENV) != 0); int version = GIT_REPO_VERSION_DEFAULT; if ((error = repo_local_config(&config, &cfg_path, NULL, repo_dir)) < 0) @@ -2113,7 +2250,8 @@ static int repo_init_config( SET_REPO_CONFIG(int32, "core.repositoryformatversion", version); if ((error = repo_init_fs_configs( - config, cfg_path.ptr, repo_dir, work_dir, !is_reinit)) < 0) + config, cfg_path.ptr, repo_dir, work_dir, + !is_reinit, use_env)) < 0) goto cleanup; if (!is_bare) { @@ -2143,7 +2281,7 @@ static int repo_init_config( SET_REPO_CONFIG(bool, "receive.denyNonFastforwards", true); } - if (oid_type != GIT_OID_SHA1) { + if (oid_type != GIT_OID_DEFAULT) { SET_REPO_CONFIG(int32, "core.repositoryformatversion", 1); SET_REPO_CONFIG(string, "extensions.objectformat", git_oid_type_name(oid_type)); } @@ -2177,8 +2315,8 @@ int git_repository_reinit_filesystem(git_repository *repo, int recurse) const char *repo_dir = git_repository_path(repo); if (!(error = repo_local_config(&config, &path, repo, repo_dir))) - error = repo_init_fs_configs( - config, path.ptr, repo_dir, git_repository_workdir(repo), true); + error = repo_init_fs_configs(config, path.ptr, repo_dir, + git_repository_workdir(repo), true, repo->use_env); git_config_free(config); git_str_dispose(&path); @@ -2647,7 +2785,7 @@ int git_repository_init_ext( wd = (opts->flags & GIT_REPOSITORY_INIT_BARE) ? NULL : git_str_cstr(&wd_path); - if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path)) < 0) + if ((error = is_valid_repository_path(&is_valid, &repo_path, &common_path, opts->flags)) < 0) goto out; if (is_valid) { @@ -3530,16 +3668,19 @@ int git_repository__shallow_roots_write(git_repository *repo, git_array_oid_t ro { git_filebuf file = GIT_FILEBUF_INIT; git_str path = GIT_STR_INIT; - int error = 0; size_t idx; git_oid *oid; + int filebuf_hash, error = 0; assert(repo); + filebuf_hash = git_filebuf_hash_flags(git_oid_algorithm(GIT_OID_SHA1)); + GIT_ASSERT(filebuf_hash); + if ((error = git_str_joinpath(&path, repo->gitdir, "shallow")) < 0) goto on_error; - if ((error = git_filebuf_open(&file, git_str_cstr(&path), GIT_FILEBUF_HASH_CONTENTS, 0666)) < 0) + if ((error = git_filebuf_open(&file, git_str_cstr(&path), filebuf_hash, 0666)) < 0) goto on_error; git_array_foreach(roots, idx, oid) { diff --git a/src/libgit2/repository.h b/src/libgit2/repository.h index 16458e416..d86b36271 100644 --- a/src/libgit2/repository.h +++ b/src/libgit2/repository.h @@ -152,8 +152,9 @@ struct git_repository { git_array_t(git_str) reserved_names; - unsigned is_bare:1; - unsigned is_worktree:1; + unsigned use_env:1, + is_bare:1, + is_worktree:1; git_oid_t oid_type; unsigned int lru_counter; diff --git a/src/libgit2/reset.c b/src/libgit2/reset.c index 9574819cb..605c4afd5 100644 --- a/src/libgit2/reset.c +++ b/src/libgit2/reset.c @@ -188,9 +188,9 @@ int git_reset( git_reset_t reset_type, const git_checkout_options *checkout_opts) { - char to[GIT_OID_SHA1_HEXSIZE + 1]; + char to[GIT_OID_MAX_HEXSIZE + 1]; - git_oid_tostr(to, GIT_OID_SHA1_HEXSIZE + 1, git_object_id(target)); + git_oid_tostr(to, GIT_OID_MAX_HEXSIZE + 1, git_object_id(target)); return reset(repo, target, to, reset_type, checkout_opts); } diff --git a/src/libgit2/revert.c b/src/libgit2/revert.c index 1106dfe2f..4a31ad40a 100644 --- a/src/libgit2/revert.c +++ b/src/libgit2/revert.c @@ -107,12 +107,10 @@ static int revert_state_cleanup(git_repository *repo) static int revert_seterr(git_commit *commit, const char *fmt) { - char commit_oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; - git_oid_fmt(commit_oidstr, git_commit_id(commit)); - commit_oidstr[GIT_OID_SHA1_HEXSIZE] = '\0'; - - git_error_set(GIT_ERROR_REVERT, fmt, commit_oidstr); + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); + git_error_set(GIT_ERROR_REVERT, fmt, commit_id); return -1; } @@ -176,7 +174,7 @@ int git_revert( git_revert_options opts; git_reference *our_ref = NULL; git_commit *our_commit = NULL; - char commit_oidstr[GIT_OID_SHA1_HEXSIZE + 1]; + char commit_id[GIT_OID_MAX_HEXSIZE + 1]; const char *commit_msg; git_str their_label = GIT_STR_INIT; git_index *index = NULL; @@ -191,19 +189,18 @@ int git_revert( if ((error = git_repository__ensure_not_bare(repo, "revert")) < 0) return error; - git_oid_fmt(commit_oidstr, git_commit_id(commit)); - commit_oidstr[GIT_OID_SHA1_HEXSIZE] = '\0'; + git_oid_tostr(commit_id, GIT_OID_MAX_HEXSIZE + 1, git_commit_id(commit)); if ((commit_msg = git_commit_summary(commit)) == NULL) { error = -1; goto on_error; } - if ((error = git_str_printf(&their_label, "parent of %.7s... %s", commit_oidstr, commit_msg)) < 0 || + if ((error = git_str_printf(&their_label, "parent of %.7s... %s", commit_id, commit_msg)) < 0 || (error = revert_normalize_opts(repo, &opts, given_opts, git_str_cstr(&their_label))) < 0 || (error = git_indexwriter_init_for_operation(&indexwriter, repo, &opts.checkout_opts.checkout_strategy)) < 0 || - (error = write_revert_head(repo, commit_oidstr)) < 0 || - (error = write_merge_msg(repo, commit_oidstr, commit_msg)) < 0 || + (error = write_revert_head(repo, commit_id)) < 0 || + (error = write_merge_msg(repo, commit_id, commit_msg)) < 0 || (error = git_repository_head(&our_ref, repo)) < 0 || (error = git_reference_peel((git_object **)&our_commit, our_ref, GIT_OBJECT_COMMIT)) < 0 || (error = git_revert_commit(&index, repo, commit, our_commit, opts.mainline, &opts.merge_opts)) < 0 || diff --git a/src/libgit2/stash.c b/src/libgit2/stash.c index 319ae3a3f..b49e95cdb 100644 --- a/src/libgit2/stash.c +++ b/src/libgit2/stash.c @@ -284,7 +284,7 @@ static int build_untracked_tree( struct stash_update_rules data = {0}; int error; - if ((error = git_index_new(&i_index)) < 0) + if ((error = git_index__new(&i_index, repo->oid_type)) < 0) goto cleanup; if (flags & GIT_STASH_INCLUDE_UNTRACKED) { @@ -487,7 +487,7 @@ static int commit_worktree( int error = 0, ignorecase; if ((error = git_repository_index(&r_index, repo) < 0) || - (error = git_index_new(&i_index)) < 0 || + (error = git_index__new(&i_index, repo->oid_type)) < 0 || (error = git_index__fill(i_index, &r_index->entries) < 0) || (error = git_repository__configmap_lookup(&ignorecase, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) goto cleanup; @@ -732,7 +732,7 @@ int git_stash_save_with_opts( i_commit, b_commit, u_commit)) < 0) goto cleanup; } else { - if ((error = git_index_new(&paths_index)) < 0 || + if ((error = git_index__new(&paths_index, repo->oid_type)) < 0 || (error = retrieve_head(&head, repo)) < 0 || (error = git_reference_peel((git_object**)&tree, head, GIT_OBJECT_TREE)) < 0 || (error = git_index_read_tree(paths_index, tree)) < 0 || @@ -1003,6 +1003,7 @@ static int stage_new_file(const git_index_entry **entries, void *data) static int stage_new_files( git_index **out, + git_repository *repo, git_tree *parent_tree, git_tree *tree) { @@ -1011,7 +1012,7 @@ static int stage_new_files( git_index *index = NULL; int error; - if ((error = git_index_new(&index)) < 0 || + if ((error = git_index__new(&index, repo->oid_type)) < 0 || (error = git_iterator_for_tree( &iterators[0], parent_tree, &iterator_options)) < 0 || (error = git_iterator_for_tree( @@ -1095,10 +1096,10 @@ int git_stash_apply( * previously unstaged contents are staged, not the previously staged.) */ } else if ((opts.flags & GIT_STASH_APPLY_REINSTATE_INDEX) == 0) { - if ((error = stage_new_files( - &stash_adds, stash_parent_tree, stash_tree)) < 0 || - (error = merge_indexes( - &unstashed_index, repo, stash_parent_tree, repo_index, stash_adds)) < 0) + if ((error = stage_new_files(&stash_adds, repo, + stash_parent_tree, stash_tree)) < 0 || + (error = merge_indexes(&unstashed_index, repo, + stash_parent_tree, repo_index, stash_adds)) < 0) goto cleanup; } diff --git a/src/libgit2/streams/schannel.c b/src/libgit2/streams/schannel.c new file mode 100644 index 000000000..f09615819 --- /dev/null +++ b/src/libgit2/streams/schannel.c @@ -0,0 +1,715 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "streams/schannel.h" + +#ifdef GIT_SCHANNEL + +#define SECURITY_WIN32 + +#include <security.h> +#include <schannel.h> +#include <sspi.h> + +#include "stream.h" +#include "streams/socket.h" + +#ifndef SP_PROT_TLS1_2_CLIENT +# define SP_PROT_TLS1_2_CLIENT 2048 +#endif + +#ifndef SP_PROT_TLS1_3_CLIENT +# define SP_PROT_TLS1_3_CLIENT 8192 +#endif + +#ifndef SECBUFFER_ALERT +# define SECBUFFER_ALERT 17 +#endif + +#define READ_BLOCKSIZE (16 * 1024) + +typedef enum { + STATE_NONE = 0, + STATE_CRED = 1, + STATE_CONTEXT = 2, + STATE_CERTIFICATE = 3 +} schannel_state; + +typedef struct { + git_stream parent; + git_stream *io; + int owned; + bool connected; + wchar_t *host_w; + + schannel_state state; + + CredHandle cred; + CtxtHandle context; + SecPkgContext_StreamSizes stream_sizes; + + CERT_CONTEXT *certificate; + const CERT_CHAIN_CONTEXT *cert_chain; + git_cert_x509 x509; + + git_str plaintext_in; + git_str ciphertext_in; +} schannel_stream; + +static int connect_context(schannel_stream *st) +{ + SCHANNEL_CRED cred = { 0 }; + SECURITY_STATUS status = SEC_E_INTERNAL_ERROR; + DWORD context_flags; + static size_t MAX_RETRIES = 1024; + size_t retries; + ssize_t read_len; + int error = 0; + + if (st->owned && (error = git_stream_connect(st->io)) < 0) + return error; + + cred.dwVersion = SCHANNEL_CRED_VERSION; + cred.dwFlags = SCH_CRED_IGNORE_NO_REVOCATION_CHECK | + SCH_CRED_IGNORE_REVOCATION_OFFLINE | + SCH_CRED_MANUAL_CRED_VALIDATION | + SCH_CRED_NO_DEFAULT_CREDS | + SCH_CRED_NO_SERVERNAME_CHECK; + cred.grbitEnabledProtocols = SP_PROT_TLS1_2_CLIENT | + SP_PROT_TLS1_3_CLIENT; + + if (AcquireCredentialsHandleW(NULL, SCHANNEL_NAME_W, + SECPKG_CRED_OUTBOUND, NULL, &cred, NULL, + NULL, &st->cred, NULL) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not acquire credentials handle"); + return -1; + } + + st->state = STATE_CRED; + + context_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_STREAM; + + for (retries = 0; retries < MAX_RETRIES; retries++) { + SecBuffer input_buf[] = { + { (unsigned long)st->ciphertext_in.size, + SECBUFFER_TOKEN, + st->ciphertext_in.size ? st->ciphertext_in.ptr : NULL }, + { 0, SECBUFFER_EMPTY, NULL } + }; + SecBuffer output_buf[] = { { 0, SECBUFFER_TOKEN, NULL }, + { 0, SECBUFFER_ALERT, NULL } }; + + SecBufferDesc input_buf_desc = { SECBUFFER_VERSION, 2, input_buf }; + SecBufferDesc output_buf_desc = { SECBUFFER_VERSION, 2, output_buf }; + + status = InitializeSecurityContextW(&st->cred, + retries ? &st->context : NULL, st->host_w, + context_flags, 0, 0, retries ? &input_buf_desc : NULL, 0, + retries ? NULL : &st->context, &output_buf_desc, + &context_flags, NULL); + + if (status == SEC_E_OK || status == SEC_I_CONTINUE_NEEDED) { + st->state = STATE_CONTEXT; + + if (output_buf[0].cbBuffer > 0) { + error = git_stream__write_full(st->io, + output_buf[0].pvBuffer, + output_buf[0].cbBuffer, 0); + + FreeContextBuffer(output_buf[0].pvBuffer); + } + + /* handle any leftover, unprocessed data */ + if (input_buf[1].BufferType == SECBUFFER_EXTRA) { + GIT_ASSERT(st->ciphertext_in.size > input_buf[1].cbBuffer); + + git_str_consume_bytes(&st->ciphertext_in, + st->ciphertext_in.size - input_buf[1].cbBuffer); + } else { + git_str_clear(&st->ciphertext_in); + } + + if (error < 0 || status == SEC_E_OK) + break; + } else if (status == SEC_E_INCOMPLETE_MESSAGE) { + /* we need additional data from the client; */ + if (git_str_grow_by(&st->ciphertext_in, READ_BLOCKSIZE) < 0) { + error = -1; + break; + } + + if ((read_len = git_stream_read(st->io, + st->ciphertext_in.ptr + st->ciphertext_in.size, + (st->ciphertext_in.asize - st->ciphertext_in.size))) < 0) { + error = -1; + break; + } + + GIT_ASSERT((size_t)read_len <= + st->ciphertext_in.asize - st->ciphertext_in.size); + st->ciphertext_in.size += read_len; + } else { + git_error_set(GIT_ERROR_OS, + "could not initialize security context"); + error = -1; + break; + } + + GIT_ASSERT(st->ciphertext_in.size < ULONG_MAX); + } + + if (retries == MAX_RETRIES) { + git_error_set(GIT_ERROR_SSL, + "could not initialize security context: too many retries"); + error = -1; + } + + if (!error) { + if (QueryContextAttributesW(&st->context, + SECPKG_ATTR_STREAM_SIZES, + &st->stream_sizes) != SEC_E_OK) { + git_error_set(GIT_ERROR_SSL, + "could not query stream sizes"); + error = -1; + } + } + + return error; +} + +static int set_certificate_lookup_error(DWORD status) +{ + switch (status) { + case CERT_TRUST_IS_NOT_TIME_VALID: + git_error_set(GIT_ERROR_SSL, + "certificate is expired or not yet valid"); + break; + case CERT_TRUST_IS_REVOKED: + git_error_set(GIT_ERROR_SSL, "certificate is revoked"); + break; + case CERT_TRUST_IS_NOT_SIGNATURE_VALID: + case CERT_TRUST_IS_NOT_VALID_FOR_USAGE: + case CERT_TRUST_INVALID_EXTENSION: + case CERT_TRUST_INVALID_POLICY_CONSTRAINTS: + case CERT_TRUST_INVALID_BASIC_CONSTRAINTS: + case CERT_TRUST_INVALID_NAME_CONSTRAINTS: + case CERT_TRUST_HAS_NOT_SUPPORTED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_NOT_DEFINED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_NOT_PERMITTED_NAME_CONSTRAINT: + case CERT_TRUST_HAS_EXCLUDED_NAME_CONSTRAINT: + case CERT_TRUST_NO_ISSUANCE_CHAIN_POLICY: + case CERT_TRUST_HAS_NOT_SUPPORTED_CRITICAL_EXT: + git_error_set(GIT_ERROR_SSL, "certificate is not valid"); + break; + case CERT_TRUST_IS_UNTRUSTED_ROOT: + case CERT_TRUST_IS_CYCLIC: + case CERT_TRUST_IS_EXPLICIT_DISTRUST: + git_error_set(GIT_ERROR_SSL, "certificate is not trusted"); + break; + case CERT_TRUST_REVOCATION_STATUS_UNKNOWN: + git_error_set(GIT_ERROR_SSL, + "certificate revocation status could not be verified"); + break; + case CERT_TRUST_IS_OFFLINE_REVOCATION: + git_error_set(GIT_ERROR_SSL, + "certificate revocation is offline or stale"); + break; + case CERT_TRUST_HAS_WEAK_SIGNATURE: + git_error_set(GIT_ERROR_SSL, "certificate has a weak signature"); + break; + default: + git_error_set(GIT_ERROR_SSL, + "unknown certificate lookup failure: %d", status); + return -1; + } + + return GIT_ECERTIFICATE; +} + +static int set_certificate_validation_error(DWORD status) +{ + switch (status) { + case TRUST_E_CERT_SIGNATURE: + git_error_set(GIT_ERROR_SSL, + "the certificate cannot be verified"); + break; + case CRYPT_E_REVOKED: + git_error_set(GIT_ERROR_SSL, + "the certificate or signature has been revoked"); + break; + case CERT_E_UNTRUSTEDROOT: + git_error_set(GIT_ERROR_SSL, + "the certificate root is not trusted"); + break; + case CERT_E_UNTRUSTEDTESTROOT: + git_error_set(GIT_ERROR_SSL, + "the certificate root is a test certificate"); + break; + case CERT_E_CHAINING: + git_error_set(GIT_ERROR_SSL, + "the certificate chain is invalid"); + break; + case CERT_E_WRONG_USAGE: + case CERT_E_PURPOSE: + git_error_set(GIT_ERROR_SSL, + "the certificate is not valid for this usage"); + break; + case CERT_E_EXPIRED: + git_error_set(GIT_ERROR_SSL, + "certificate is expired or not yet valid"); + break; + case CERT_E_INVALID_NAME: + case CERT_E_CN_NO_MATCH: + git_error_set(GIT_ERROR_SSL, + "certificate is not valid for this hostname"); + break; + case CERT_E_INVALID_POLICY: + case TRUST_E_BASIC_CONSTRAINTS: + case CERT_E_CRITICAL: + case CERT_E_VALIDITYPERIODNESTING: + git_error_set(GIT_ERROR_SSL, "certificate is not valid"); + break; + case CRYPT_E_NO_REVOCATION_CHECK: + git_error_set(GIT_ERROR_SSL, + "certificate revocation status could not be verified"); + break; + case CRYPT_E_REVOCATION_OFFLINE: + git_error_set(GIT_ERROR_SSL, + "certificate revocation is offline or stale"); + break; + case CERT_E_ROLE: + git_error_set(GIT_ERROR_SSL, "certificate authority is not valid"); + break; + default: + git_error_set(GIT_ERROR_SSL, + "unknown certificate policy checking failure: %d", + status); + return -1; + } + + return GIT_ECERTIFICATE; +} + +static int check_certificate(schannel_stream* st) +{ + CERT_CHAIN_PARA cert_chain_parameters; + SSL_EXTRA_CERT_CHAIN_POLICY_PARA ssl_policy_parameters; + CERT_CHAIN_POLICY_PARA cert_policy_parameters = + { sizeof(CERT_CHAIN_POLICY_PARA), 0, &ssl_policy_parameters }; + CERT_CHAIN_POLICY_STATUS cert_policy_status; + + memset(&cert_chain_parameters, 0, sizeof(CERT_CHAIN_PARA)); + cert_chain_parameters.cbSize = sizeof(CERT_CHAIN_PARA); + + if (QueryContextAttributesW(&st->context, + SECPKG_ATTR_REMOTE_CERT_CONTEXT, + &st->certificate) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, + "could not query remote certificate context"); + return -1; + } + + /* TODO: do we really want to do revokcation checking ? */ + if (!CertGetCertificateChain(NULL, st->certificate, NULL, + st->certificate->hCertStore, &cert_chain_parameters, + CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT, + NULL, &st->cert_chain)) { + git_error_set(GIT_ERROR_OS, "could not query remote certificate chain"); + CertFreeCertificateContext(st->certificate); + return -1; + } + + st->state = STATE_CERTIFICATE; + + /* Set up the x509 certificate data for future callbacks */ + + st->x509.parent.cert_type = GIT_CERT_X509; + st->x509.data = st->certificate->pbCertEncoded; + st->x509.len = st->certificate->cbCertEncoded; + + /* Handle initial certificate validation */ + + if (st->cert_chain->TrustStatus.dwErrorStatus != CERT_TRUST_NO_ERROR) + return set_certificate_lookup_error(st->cert_chain->TrustStatus.dwErrorStatus); + + ssl_policy_parameters.cbSize = sizeof(SSL_EXTRA_CERT_CHAIN_POLICY_PARA); + ssl_policy_parameters.dwAuthType = AUTHTYPE_SERVER; + ssl_policy_parameters.pwszServerName = st->host_w; + + if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_SSL, + st->cert_chain, &cert_policy_parameters, + &cert_policy_status)) { + git_error_set(GIT_ERROR_OS, "could not verify certificate chain policy"); + return -1; + } + + if (cert_policy_status.dwError != SEC_E_OK) + return set_certificate_validation_error(cert_policy_status.dwError); + + return 0; +} + +static int schannel_connect(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + int error; + + GIT_ASSERT(st->state == STATE_NONE); + + if ((error = connect_context(st)) < 0 || + (error = check_certificate(st)) < 0) + return error; + + st->connected = 1; + return 0; +} + +static int schannel_certificate(git_cert **out, git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + + *out = &st->x509.parent; + return 0; +} + +static int schannel_set_proxy( + git_stream *stream, + const git_proxy_options *proxy_options) +{ + schannel_stream *st = (schannel_stream *)stream; + return git_stream_set_proxy(st->io, proxy_options); +} + +static ssize_t schannel_write( + git_stream *stream, + const char *data, + size_t data_len, + int flags) +{ + schannel_stream *st = (schannel_stream *)stream; + SecBuffer encrypt_buf[3]; + SecBufferDesc encrypt_buf_desc = { SECBUFFER_VERSION, 3, encrypt_buf }; + git_str ciphertext_out = GIT_STR_INIT; + ssize_t total_len = 0; + + GIT_UNUSED(flags); + + if (data_len > SSIZE_MAX) + data_len = SSIZE_MAX; + + git_str_init(&ciphertext_out, + st->stream_sizes.cbHeader + + st->stream_sizes.cbMaximumMessage + + st->stream_sizes.cbTrailer); + + while (data_len > 0) { + size_t message_len = min(data_len, st->stream_sizes.cbMaximumMessage); + size_t ciphertext_len, ciphertext_written = 0; + + encrypt_buf[0].BufferType = SECBUFFER_STREAM_HEADER; + encrypt_buf[0].cbBuffer = st->stream_sizes.cbHeader; + encrypt_buf[0].pvBuffer = ciphertext_out.ptr; + + encrypt_buf[1].BufferType = SECBUFFER_DATA; + encrypt_buf[1].cbBuffer = (unsigned long)message_len; + encrypt_buf[1].pvBuffer = + ciphertext_out.ptr + st->stream_sizes.cbHeader; + + encrypt_buf[2].BufferType = SECBUFFER_STREAM_TRAILER; + encrypt_buf[2].cbBuffer = st->stream_sizes.cbTrailer; + encrypt_buf[2].pvBuffer = + ciphertext_out.ptr + st->stream_sizes.cbHeader + + message_len; + + memcpy(ciphertext_out.ptr + st->stream_sizes.cbHeader, data, message_len); + + if (EncryptMessage(&st->context, 0, &encrypt_buf_desc, 0) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not encrypt tls message"); + total_len = -1; + goto done; + } + + ciphertext_len = encrypt_buf[0].cbBuffer + + encrypt_buf[1].cbBuffer + + encrypt_buf[2].cbBuffer; + + while (ciphertext_written < ciphertext_len) { + ssize_t chunk_len = git_stream_write(st->io, + ciphertext_out.ptr + ciphertext_written, + ciphertext_len - ciphertext_written, 0); + + if (chunk_len < 0) { + total_len = -1; + goto done; + } + + ciphertext_len -= chunk_len; + ciphertext_written += chunk_len; + } + + total_len += message_len; + + data += message_len; + data_len -= message_len; + } + +done: + git_str_dispose(&ciphertext_out); + return total_len; +} + +static ssize_t schannel_read(git_stream *stream, void *_data, size_t data_len) +{ + schannel_stream *st = (schannel_stream *)stream; + char *data = (char *)_data; + SecBuffer decrypt_buf[4]; + SecBufferDesc decrypt_buf_desc = { SECBUFFER_VERSION, 4, decrypt_buf }; + SECURITY_STATUS status; + ssize_t chunk_len, total_len = 0; + + if (data_len > SSIZE_MAX) + data_len = SSIZE_MAX; + + /* + * Loop until we have some bytes to return - we may have decrypted + * bytes queued or ciphertext from the wire that we can decrypt and + * return. Return any queued bytes if they're available to avoid a + * network read, which may block. We may return less than the + * caller requested, and they can retry for an actual network + */ + while ((size_t)total_len < data_len) { + if (st->plaintext_in.size > 0) { + size_t copy_len = min(st->plaintext_in.size, data_len); + + memcpy(data, st->plaintext_in.ptr, copy_len); + git_str_consume_bytes(&st->plaintext_in, copy_len); + + data += copy_len; + data_len -= copy_len; + + total_len += copy_len; + + continue; + } + + if (st->ciphertext_in.size > 0) { + decrypt_buf[0].BufferType = SECBUFFER_DATA; + decrypt_buf[0].cbBuffer = (unsigned long)min(st->ciphertext_in.size, ULONG_MAX); + decrypt_buf[0].pvBuffer = st->ciphertext_in.ptr; + + decrypt_buf[1].BufferType = SECBUFFER_EMPTY; + decrypt_buf[1].cbBuffer = 0; + decrypt_buf[1].pvBuffer = NULL; + + decrypt_buf[2].BufferType = SECBUFFER_EMPTY; + decrypt_buf[2].cbBuffer = 0; + decrypt_buf[2].pvBuffer = NULL; + + decrypt_buf[3].BufferType = SECBUFFER_EMPTY; + decrypt_buf[3].cbBuffer = 0; + decrypt_buf[3].pvBuffer = NULL; + + status = DecryptMessage(&st->context, &decrypt_buf_desc, 0, NULL); + + if (status == SEC_E_OK) { + GIT_ASSERT(decrypt_buf[0].BufferType == SECBUFFER_STREAM_HEADER); + GIT_ASSERT(decrypt_buf[1].BufferType == SECBUFFER_DATA); + GIT_ASSERT(decrypt_buf[2].BufferType == SECBUFFER_STREAM_TRAILER); + + if (git_str_put(&st->plaintext_in, decrypt_buf[1].pvBuffer, decrypt_buf[1].cbBuffer) < 0) { + total_len = -1; + goto done; + } + + if (decrypt_buf[3].BufferType == SECBUFFER_EXTRA) { + git_str_consume_bytes(&st->ciphertext_in, (st->ciphertext_in.size - decrypt_buf[3].cbBuffer)); + } else { + git_str_clear(&st->ciphertext_in); + } + + continue; + } else if (status == SEC_E_CONTEXT_EXPIRED) { + break; + } else if (status != SEC_E_INCOMPLETE_MESSAGE) { + git_error_set(GIT_ERROR_SSL, "could not decrypt tls message"); + total_len = -1; + goto done; + } + } + + if (total_len != 0) + break; + + if (git_str_grow_by(&st->ciphertext_in, READ_BLOCKSIZE) < 0) { + total_len = -1; + goto done; + } + + if ((chunk_len = git_stream_read(st->io, st->ciphertext_in.ptr + st->ciphertext_in.size, st->ciphertext_in.asize - st->ciphertext_in.size)) < 0) { + total_len = -1; + goto done; + } + + st->ciphertext_in.size += chunk_len; + } + +done: + return total_len; +} + +static int schannel_close(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + int error = 0; + + if (st->connected) { + SecBuffer shutdown_buf; + SecBufferDesc shutdown_buf_desc = + { SECBUFFER_VERSION, 1, &shutdown_buf }; + DWORD shutdown_message = SCHANNEL_SHUTDOWN, shutdown_flags; + + shutdown_buf.BufferType = SECBUFFER_TOKEN; + shutdown_buf.cbBuffer = sizeof(DWORD); + shutdown_buf.pvBuffer = &shutdown_message; + + if (ApplyControlToken(&st->context, &shutdown_buf_desc) != SEC_E_OK) { + git_error_set(GIT_ERROR_SSL, "could not shutdown stream"); + error = -1; + } + + shutdown_buf.BufferType = SECBUFFER_TOKEN; + shutdown_buf.cbBuffer = 0; + shutdown_buf.pvBuffer = NULL; + + shutdown_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_REPLAY_DETECT | + ISC_REQ_SEQUENCE_DETECT | + ISC_REQ_STREAM; + + if (InitializeSecurityContext(&st->cred, &st->context, + NULL, shutdown_flags, 0, 0, + &shutdown_buf_desc, 0, NULL, + &shutdown_buf_desc, &shutdown_flags, + NULL) == SEC_E_OK) { + if (shutdown_buf.cbBuffer > 0) { + if (git_stream__write_full(st->io, + shutdown_buf.pvBuffer, + shutdown_buf.cbBuffer, 0) < 0) + error = -1; + + FreeContextBuffer(shutdown_buf.pvBuffer); + } + } + } + + st->connected = false; + + if (st->owned && git_stream_close(st->io) < 0) + error = -1; + + return error; +} + +static void schannel_free(git_stream *stream) +{ + schannel_stream *st = (schannel_stream *)stream; + + if (st->state >= STATE_CERTIFICATE) { + CertFreeCertificateContext(st->certificate); + CertFreeCertificateChain(st->cert_chain); + } + + if (st->state >= STATE_CONTEXT) + DeleteSecurityContext(&st->context); + + if (st->state >= STATE_CRED) + FreeCredentialsHandle(&st->cred); + + st->state = STATE_NONE; + + git_str_dispose(&st->ciphertext_in); + git_str_dispose(&st->plaintext_in); + + git__free(st->host_w); + + if (st->owned) + git_stream_free(st->io); + + git__free(st); +} + +static int schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host, + int owned) +{ + schannel_stream *st; + + st = git__calloc(1, sizeof(schannel_stream)); + GIT_ERROR_CHECK_ALLOC(st); + + st->io = in; + st->owned = owned; + + if (git_utf8_to_16_alloc(&st->host_w, host) < 0) { + git__free(st); + return -1; + } + + st->parent.version = GIT_STREAM_VERSION; + st->parent.encrypted = 1; + st->parent.proxy_support = git_stream_supports_proxy(st->io); + st->parent.connect = schannel_connect; + st->parent.certificate = schannel_certificate; + st->parent.set_proxy = schannel_set_proxy; + st->parent.read = schannel_read; + st->parent.write = schannel_write; + st->parent.close = schannel_close; + st->parent.free = schannel_free; + + *out = (git_stream *)st; + return 0; +} + +extern int git_schannel_stream_new( + git_stream **out, + const char *host, + const char *port) +{ + git_stream *stream; + int error; + + GIT_ASSERT_ARG(out); + GIT_ASSERT_ARG(host); + GIT_ASSERT_ARG(port); + + if ((error = git_socket_stream_new(&stream, host, port)) < 0) + return error; + + if ((error = schannel_stream_wrap(out, stream, host, 1)) < 0) { + git_stream_close(stream); + git_stream_free(stream); + } + + return error; +} + +extern int git_schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host) +{ + return schannel_stream_wrap(out, in, host, 0); +} + +#endif diff --git a/src/libgit2/streams/schannel.h b/src/libgit2/streams/schannel.h new file mode 100644 index 000000000..3584970d1 --- /dev/null +++ b/src/libgit2/streams/schannel.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ +#ifndef INCLUDE_steams_schannel_h__ +#define INCLUDE_steams_schannel_h__ + +#include "common.h" + +#include "git2/sys/stream.h" + +#ifdef GIT_SCHANNEL + +extern int git_schannel_stream_new( + git_stream **out, + const char *host, + const char *port); + +extern int git_schannel_stream_wrap( + git_stream **out, + git_stream *in, + const char *host); + +#endif + +#endif diff --git a/src/libgit2/streams/socket.c b/src/libgit2/streams/socket.c index 908e8c02f..8f23e746e 100644 --- a/src/libgit2/streams/socket.c +++ b/src/libgit2/streams/socket.c @@ -10,22 +10,23 @@ #include "posix.h" #include "netops.h" #include "registry.h" +#include "runtime.h" #include "stream.h" #ifndef _WIN32 -# include <sys/types.h> -# include <sys/socket.h> -# include <sys/select.h> -# include <sys/time.h> -# include <netdb.h> -# include <netinet/in.h> -# include <arpa/inet.h> +# include <sys/types.h> +# include <sys/socket.h> +# include <sys/select.h> +# include <sys/time.h> +# include <netdb.h> +# include <netinet/in.h> +# include <arpa/inet.h> #else -# include <winsock2.h> -# include <ws2tcpip.h> -# ifdef _MSC_VER -# pragma comment(lib, "ws2_32") -# endif +# include <winsock2.h> +# include <ws2tcpip.h> +# ifdef _MSC_VER +# pragma comment(lib, "ws2_32") +# endif #endif #ifdef GIT_WIN32 @@ -54,11 +55,8 @@ static int close_socket(GIT_SOCKET s) return 0; #ifdef GIT_WIN32 - if (SOCKET_ERROR == closesocket(s)) - return -1; - - if (0 != WSACleanup()) { - git_error_set(GIT_ERROR_OS, "winsock cleanup failed"); + if (closesocket(s) != 0) { + net_set_error("could not close socket"); return -1; } @@ -77,23 +75,6 @@ static int socket_connect(git_stream *stream) GIT_SOCKET s = INVALID_SOCKET; int ret; -#ifdef GIT_WIN32 - /* on win32, the WSA context needs to be initialized - * before any socket calls can be performed */ - WSADATA wsd; - - if (WSAStartup(MAKEWORD(2,2), &wsd) != 0) { - git_error_set(GIT_ERROR_OS, "winsock init failed"); - return -1; - } - - if (LOBYTE(wsd.wVersion) != 2 || HIBYTE(wsd.wVersion) != 2) { - WSACleanup(); - git_error_set(GIT_ERROR_OS, "winsock init failed"); - return -1; - } -#endif - memset(&hints, 0x0, sizeof(struct addrinfo)); hints.ai_socktype = SOCK_STREAM; hints.ai_family = AF_UNSPEC; @@ -240,3 +221,42 @@ int git_socket_stream_new( return init(out, host, port); } + +#ifdef GIT_WIN32 + +static void socket_stream_global_shutdown(void) +{ + WSACleanup(); +} + +int git_socket_stream_global_init(void) +{ + WORD winsock_version; + WSADATA wsa_data; + + winsock_version = MAKEWORD(2, 2); + + if (WSAStartup(winsock_version, &wsa_data) != 0) { + git_error_set(GIT_ERROR_OS, "could not initialize Windows Socket Library"); + return -1; + } + + if (LOBYTE(wsa_data.wVersion) != 2 || + HIBYTE(wsa_data.wVersion) != 2) { + git_error_set(GIT_ERROR_SSL, "Windows Socket Library does not support Winsock 2.2"); + return -1; + } + + return git_runtime_shutdown_register(socket_stream_global_shutdown); +} + +#else + +#include "stream.h" + +int git_socket_stream_global_init(void) +{ + return 0; +} + + #endif diff --git a/src/libgit2/streams/socket.h b/src/libgit2/streams/socket.h index 3235f3167..300e70893 100644 --- a/src/libgit2/streams/socket.h +++ b/src/libgit2/streams/socket.h @@ -20,4 +20,6 @@ typedef struct { extern int git_socket_stream_new(git_stream **out, const char *host, const char *port); +extern int git_socket_stream_global_init(void); + #endif diff --git a/src/libgit2/streams/tls.c b/src/libgit2/streams/tls.c index e063a33f9..246ac9ca7 100644 --- a/src/libgit2/streams/tls.c +++ b/src/libgit2/streams/tls.c @@ -13,6 +13,7 @@ #include "streams/mbedtls.h" #include "streams/openssl.h" #include "streams/stransport.h" +#include "streams/schannel.h" int git_tls_stream_new(git_stream **out, const char *host, const char *port) { @@ -33,6 +34,8 @@ int git_tls_stream_new(git_stream **out, const char *host, const char *port) init = git_openssl_stream_new; #elif defined(GIT_MBEDTLS) init = git_mbedtls_stream_new; +#elif defined(GIT_SCHANNEL) + init = git_schannel_stream_new; #endif } else { return error; @@ -63,6 +66,8 @@ int git_tls_stream_wrap(git_stream **out, git_stream *in, const char *host) wrap = git_openssl_stream_wrap; #elif defined(GIT_MBEDTLS) wrap = git_mbedtls_stream_wrap; +#elif defined(GIT_SCHANNEL) + wrap = git_schannel_stream_wrap; #endif } diff --git a/src/libgit2/submodule.h b/src/libgit2/submodule.h index 7fa982486..40b7b70f7 100644 --- a/src/libgit2/submodule.h +++ b/src/libgit2/submodule.h @@ -69,9 +69,9 @@ * - `repo` is the parent repository that contains this submodule. * - `flags` after for internal use, tracking where this submodule has been * found (head, index, config, workdir) and known status info, etc. - * - `head_oid` is the SHA1 for the submodule path in the repo HEAD. - * - `index_oid` is the SHA1 for the submodule recorded in the index. - * - `wd_oid` is the SHA1 for the HEAD of the checked out submodule. + * - `head_oid` is the oid for the submodule path in the repo HEAD. + * - `index_oid` is the oid for the submodule recorded in the index. + * - `wd_oid` is the oid for the HEAD of the checked out submodule. * * If the submodule has been added to .gitmodules but not yet git added, * then the `index_oid` will be zero but still marked valid. If the diff --git a/src/libgit2/threadstate.h b/src/libgit2/threadstate.h index f9e7ba7bf..65edec717 100644 --- a/src/libgit2/threadstate.h +++ b/src/libgit2/threadstate.h @@ -13,7 +13,7 @@ typedef struct { git_error *last_error; git_error error_t; git_str error_buf; - char oid_fmt[GIT_OID_SHA1_HEXSIZE+1]; + char oid_fmt[GIT_OID_MAX_HEXSIZE+1]; } git_threadstate; extern int git_threadstate_global_init(void); diff --git a/src/libgit2/transports/auth_negotiate.c b/src/libgit2/transports/auth_gssapi.c index 6380504be..500553841 100644 --- a/src/libgit2/transports/auth_negotiate.c +++ b/src/libgit2/transports/auth_gssapi.c @@ -20,13 +20,13 @@ #include <krb5.h> #endif -static gss_OID_desc negotiate_oid_spnego = +static gss_OID_desc gssapi_oid_spnego = { 6, (void *) "\x2b\x06\x01\x05\x05\x02" }; -static gss_OID_desc negotiate_oid_krb5 = +static gss_OID_desc gssapi_oid_krb5 = { 9, (void *) "\x2a\x86\x48\x86\xf7\x12\x01\x02\x02" }; -static gss_OID negotiate_oids[] = - { &negotiate_oid_spnego, &negotiate_oid_krb5, NULL }; +static gss_OID gssapi_oids[] = + { &gssapi_oid_spnego, &gssapi_oid_krb5, NULL }; typedef struct { git_http_auth_context parent; @@ -36,9 +36,9 @@ typedef struct { char *challenge; gss_ctx_id_t gss_context; gss_OID oid; -} http_auth_negotiate_context; +} http_auth_gssapi_context; -static void negotiate_err_set( +static void gssapi_err_set( OM_uint32 status_major, OM_uint32 status_minor, const char *message) @@ -58,11 +58,11 @@ static void negotiate_err_set( } } -static int negotiate_set_challenge( +static int gssapi_set_challenge( git_http_auth_context *c, const char *challenge) { - http_auth_negotiate_context *ctx = (http_auth_negotiate_context *)c; + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; GIT_ASSERT_ARG(ctx); GIT_ASSERT_ARG(challenge); @@ -76,7 +76,7 @@ static int negotiate_set_challenge( return 0; } -static void negotiate_context_dispose(http_auth_negotiate_context *ctx) +static void gssapi_context_dispose(http_auth_gssapi_context *ctx) { OM_uint32 status_minor; @@ -92,12 +92,12 @@ static void negotiate_context_dispose(http_auth_negotiate_context *ctx) ctx->challenge = NULL; } -static int negotiate_next_token( +static int gssapi_next_token( git_str *buf, git_http_auth_context *c, git_credential *cred) { - http_auth_negotiate_context *ctx = (http_auth_negotiate_context *)c; + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; OM_uint32 status_major, status_minor; gss_buffer_desc target_buffer = GSS_C_EMPTY_BUFFER, input_token = GSS_C_EMPTY_BUFFER, @@ -126,7 +126,7 @@ static int negotiate_next_token( GSS_C_NT_HOSTBASED_SERVICE, &server); if (GSS_ERROR(status_major)) { - negotiate_err_set(status_major, status_minor, + gssapi_err_set(status_major, status_minor, "could not parse principal"); error = -1; goto done; @@ -152,10 +152,10 @@ static int negotiate_next_token( input_token.length = input_buf.size; input_token_ptr = &input_token; } else if (ctx->gss_context != GSS_C_NO_CONTEXT) { - negotiate_context_dispose(ctx); + gssapi_context_dispose(ctx); } - mech = &negotiate_oid_spnego; + mech = &gssapi_oid_spnego; status_major = gss_init_sec_context( &status_minor, @@ -173,14 +173,14 @@ static int negotiate_next_token( NULL); if (GSS_ERROR(status_major)) { - negotiate_err_set(status_major, status_minor, "negotiate failure"); + gssapi_err_set(status_major, status_minor, "negotiate failure"); error = -1; goto done; } /* This message merely told us auth was complete; we do not respond. */ if (status_major == GSS_S_COMPLETE) { - negotiate_context_dispose(ctx); + gssapi_context_dispose(ctx); ctx->complete = 1; goto done; } @@ -204,20 +204,20 @@ done: return error; } -static int negotiate_is_complete(git_http_auth_context *c) +static int gssapi_is_complete(git_http_auth_context *c) { - http_auth_negotiate_context *ctx = (http_auth_negotiate_context *)c; + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; GIT_ASSERT_ARG(ctx); return (ctx->complete == 1); } -static void negotiate_context_free(git_http_auth_context *c) +static void gssapi_context_free(git_http_auth_context *c) { - http_auth_negotiate_context *ctx = (http_auth_negotiate_context *)c; + http_auth_gssapi_context *ctx = (http_auth_gssapi_context *)c; - negotiate_context_dispose(ctx); + gssapi_context_dispose(ctx); ctx->configured = 0; ctx->complete = 0; @@ -226,8 +226,8 @@ static void negotiate_context_free(git_http_auth_context *c) git__free(ctx); } -static int negotiate_init_context( - http_auth_negotiate_context *ctx, +static int gssapi_init_context( + http_auth_gssapi_context *ctx, const git_net_url *url) { OM_uint32 status_major, status_minor; @@ -239,13 +239,13 @@ static int negotiate_init_context( status_major = gss_indicate_mechs(&status_minor, &mechanism_list); if (GSS_ERROR(status_major)) { - negotiate_err_set(status_major, status_minor, + gssapi_err_set(status_major, status_minor, "could not query mechanisms"); return -1; } if (mechanism_list) { - for (oid = negotiate_oids; *oid; oid++) { + for (oid = gssapi_oids; *oid; oid++) { for (i = 0; i < mechanism_list->count; i++) { item = &mechanism_list->elements[i]; @@ -285,14 +285,14 @@ int git_http_auth_negotiate( git_http_auth_context **out, const git_net_url *url) { - http_auth_negotiate_context *ctx; + http_auth_gssapi_context *ctx; *out = NULL; - ctx = git__calloc(1, sizeof(http_auth_negotiate_context)); + ctx = git__calloc(1, sizeof(http_auth_gssapi_context)); GIT_ERROR_CHECK_ALLOC(ctx); - if (negotiate_init_context(ctx, url) < 0) { + if (gssapi_init_context(ctx, url) < 0) { git__free(ctx); return -1; } @@ -300,10 +300,10 @@ int git_http_auth_negotiate( ctx->parent.type = GIT_HTTP_AUTH_NEGOTIATE; ctx->parent.credtypes = GIT_CREDENTIAL_DEFAULT; ctx->parent.connection_affinity = 1; - ctx->parent.set_challenge = negotiate_set_challenge; - ctx->parent.next_token = negotiate_next_token; - ctx->parent.is_complete = negotiate_is_complete; - ctx->parent.free = negotiate_context_free; + ctx->parent.set_challenge = gssapi_set_challenge; + ctx->parent.next_token = gssapi_next_token; + ctx->parent.is_complete = gssapi_is_complete; + ctx->parent.free = gssapi_context_free; *out = (git_http_auth_context *)ctx; diff --git a/src/libgit2/transports/auth_negotiate.h b/src/libgit2/transports/auth_negotiate.h index 34aff295b..4360785c5 100644 --- a/src/libgit2/transports/auth_negotiate.h +++ b/src/libgit2/transports/auth_negotiate.h @@ -12,7 +12,7 @@ #include "git2.h" #include "auth.h" -#if defined(GIT_GSSAPI) || defined(GIT_GSSFRAMEWORK) +#if defined(GIT_GSSAPI) || defined(GIT_GSSFRAMEWORK) || defined(GIT_WIN32) extern int git_http_auth_negotiate( git_http_auth_context **out, diff --git a/src/libgit2/transports/auth_ntlm.h b/src/libgit2/transports/auth_ntlm.h index 40689498c..33406ae94 100644 --- a/src/libgit2/transports/auth_ntlm.h +++ b/src/libgit2/transports/auth_ntlm.h @@ -13,7 +13,7 @@ /* NTLM requires a full request/challenge/response */ #define GIT_AUTH_STEPS_NTLM 2 -#ifdef GIT_NTLM +#if defined(GIT_NTLM) || defined(GIT_WIN32) #if defined(GIT_OPENSSL) # define CRYPT_OPENSSL diff --git a/src/libgit2/transports/auth_ntlm.c b/src/libgit2/transports/auth_ntlmclient.c index f49ce101a..6f26a6179 100644 --- a/src/libgit2/transports/auth_ntlm.c +++ b/src/libgit2/transports/auth_ntlmclient.c @@ -23,7 +23,7 @@ typedef struct { bool complete; } http_auth_ntlm_context; -static int ntlm_set_challenge( +static int ntlmclient_set_challenge( git_http_auth_context *c, const char *challenge) { @@ -40,7 +40,7 @@ static int ntlm_set_challenge( return 0; } -static int ntlm_set_credentials(http_auth_ntlm_context *ctx, git_credential *_cred) +static int ntlmclient_set_credentials(http_auth_ntlm_context *ctx, git_credential *_cred) { git_credential_userpass_plaintext *cred; const char *sep, *username; @@ -76,7 +76,7 @@ done: return error; } -static int ntlm_next_token( +static int ntlmclient_next_token( git_str *buf, git_http_auth_context *c, git_credential *cred) @@ -104,7 +104,7 @@ static int ntlm_next_token( */ ctx->complete = true; - if (cred && ntlm_set_credentials(ctx, cred) != 0) + if (cred && ntlmclient_set_credentials(ctx, cred) != 0) goto done; if (challenge_len < 4) { @@ -162,7 +162,7 @@ done: return error; } -static int ntlm_is_complete(git_http_auth_context *c) +static int ntlmclient_is_complete(git_http_auth_context *c) { http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; @@ -170,7 +170,7 @@ static int ntlm_is_complete(git_http_auth_context *c) return (ctx->complete == true); } -static void ntlm_context_free(git_http_auth_context *c) +static void ntlmclient_context_free(git_http_auth_context *c) { http_auth_ntlm_context *ctx = (http_auth_ntlm_context *)c; @@ -179,7 +179,7 @@ static void ntlm_context_free(git_http_auth_context *c) git__free(ctx); } -static int ntlm_init_context( +static int ntlmclient_init_context( http_auth_ntlm_context *ctx, const git_net_url *url) { @@ -206,7 +206,7 @@ int git_http_auth_ntlm( ctx = git__calloc(1, sizeof(http_auth_ntlm_context)); GIT_ERROR_CHECK_ALLOC(ctx); - if (ntlm_init_context(ctx, url) < 0) { + if (ntlmclient_init_context(ctx, url) < 0) { git__free(ctx); return -1; } @@ -214,10 +214,10 @@ int git_http_auth_ntlm( ctx->parent.type = GIT_HTTP_AUTH_NTLM; ctx->parent.credtypes = GIT_CREDENTIAL_USERPASS_PLAINTEXT; ctx->parent.connection_affinity = 1; - ctx->parent.set_challenge = ntlm_set_challenge; - ctx->parent.next_token = ntlm_next_token; - ctx->parent.is_complete = ntlm_is_complete; - ctx->parent.free = ntlm_context_free; + ctx->parent.set_challenge = ntlmclient_set_challenge; + ctx->parent.next_token = ntlmclient_next_token; + ctx->parent.is_complete = ntlmclient_is_complete; + ctx->parent.free = ntlmclient_context_free; *out = (git_http_auth_context *)ctx; diff --git a/src/libgit2/transports/auth_sspi.c b/src/libgit2/transports/auth_sspi.c new file mode 100644 index 000000000..f8269365d --- /dev/null +++ b/src/libgit2/transports/auth_sspi.c @@ -0,0 +1,341 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include "auth_ntlm.h" +#include "auth_negotiate.h" + +#ifdef GIT_WIN32 + +#define SECURITY_WIN32 + +#include "git2.h" +#include "auth.h" +#include "git2/sys/credential.h" + +#include <windows.h> +#include <security.h> + +typedef struct { + git_http_auth_context parent; + wchar_t *target; + + const char *package_name; + size_t package_name_len; + wchar_t *package_name_w; + SecPkgInfoW *package_info; + SEC_WINNT_AUTH_IDENTITY_W identity; + CredHandle cred; + CtxtHandle context; + + int has_identity : 1, + has_credentials : 1, + has_context : 1, + complete : 1; + git_str challenge; +} http_auth_sspi_context; + +static void sspi_reset_context(http_auth_sspi_context *ctx) +{ + if (ctx->has_identity) { + git__free(ctx->identity.User); + git__free(ctx->identity.Domain); + git__free(ctx->identity.Password); + + memset(&ctx->identity, 0, sizeof(SEC_WINNT_AUTH_IDENTITY_W)); + + ctx->has_identity = 0; + } + + if (ctx->has_credentials) { + FreeCredentialsHandle(&ctx->cred); + memset(&ctx->cred, 0, sizeof(CredHandle)); + + ctx->has_credentials = 0; + } + + if (ctx->has_context) { + DeleteSecurityContext(&ctx->context); + memset(&ctx->context, 0, sizeof(CtxtHandle)); + + ctx->has_context = 0; + } + + ctx->complete = 0; + + git_str_dispose(&ctx->challenge); +} + +static int sspi_set_challenge( + git_http_auth_context *c, + const char *challenge) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + size_t challenge_len = strlen(challenge); + + git_str_clear(&ctx->challenge); + + if (strncmp(challenge, ctx->package_name, ctx->package_name_len) != 0) { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + /* + * A package type indicator without a base64 payload indicates the + * mechanism; it's not an actual challenge. Ignore it. + */ + if (challenge[ctx->package_name_len] == 0) { + return 0; + } else if (challenge[ctx->package_name_len] != ' ') { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + if (git_str_decode_base64(&ctx->challenge, + challenge + (ctx->package_name_len + 1), + challenge_len - (ctx->package_name_len + 1)) < 0) { + git_error_set(GIT_ERROR_NET, "invalid %s challenge from server", ctx->package_name); + return -1; + } + + GIT_ASSERT(ctx->challenge.size <= ULONG_MAX); + return 0; +} + +static int create_identity( + SEC_WINNT_AUTH_IDENTITY_W **out, + http_auth_sspi_context *ctx, + git_credential *cred) +{ + git_credential_userpass_plaintext *userpass; + wchar_t *username = NULL, *domain = NULL, *password = NULL; + int username_len = 0, domain_len = 0, password_len = 0; + const char *sep; + + if (cred->credtype == GIT_CREDENTIAL_DEFAULT) { + *out = NULL; + return 0; + } + + if (cred->credtype != GIT_CREDENTIAL_USERPASS_PLAINTEXT) { + git_error_set(GIT_ERROR_NET, "unknown credential type: %d", cred->credtype); + return -1; + } + + userpass = (git_credential_userpass_plaintext *)cred; + + if ((sep = strchr(userpass->username, '\\')) != NULL) { + GIT_ASSERT(sep - userpass->username < INT_MAX); + + username_len = git_utf8_to_16_alloc(&username, sep + 1); + domain_len = git_utf8_to_16_alloc_with_len(&domain, + userpass->username, (int)(sep - userpass->username)); + } else { + username_len = git_utf8_to_16_alloc(&username, + userpass->username); + } + + password_len = git_utf8_to_16_alloc(&password, userpass->password); + + if (username_len < 0 || domain_len < 0 || password_len < 0) { + git__free(username); + git__free(domain); + git__free(password); + return -1; + } + + ctx->identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE; + ctx->identity.User = username; + ctx->identity.UserLength = (unsigned long)username_len; + ctx->identity.Password = password; + ctx->identity.PasswordLength = (unsigned long)password_len; + ctx->identity.Domain = domain; + ctx->identity.DomainLength = (unsigned long)domain_len; + + ctx->has_identity = 1; + + *out = &ctx->identity; + + return 0; +} + +static int sspi_next_token( + git_str *buf, + git_http_auth_context *c, + git_credential *cred) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + SEC_WINNT_AUTH_IDENTITY_W *identity = NULL; + TimeStamp timestamp; + DWORD context_flags; + SecBuffer input_buf = { 0, SECBUFFER_TOKEN, NULL }; + SecBuffer output_buf = { 0, SECBUFFER_TOKEN, NULL }; + SecBufferDesc input_buf_desc = { SECBUFFER_VERSION, 1, &input_buf }; + SecBufferDesc output_buf_desc = { SECBUFFER_VERSION, 1, &output_buf }; + SECURITY_STATUS status; + + if (ctx->complete) + sspi_reset_context(ctx); + + if (!ctx->has_context) { + if (create_identity(&identity, ctx, cred) < 0) + return -1; + + status = AcquireCredentialsHandleW(NULL, ctx->package_name_w, + SECPKG_CRED_BOTH, NULL, identity, NULL, + NULL, &ctx->cred, ×tamp); + + if (status != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not acquire credentials"); + return -1; + } + + ctx->has_credentials = 1; + } + + context_flags = ISC_REQ_ALLOCATE_MEMORY | + ISC_REQ_CONFIDENTIALITY | + ISC_REQ_MUTUAL_AUTH; + + if (ctx->challenge.size > 0) { + input_buf.BufferType = SECBUFFER_TOKEN; + input_buf.cbBuffer = (unsigned long)ctx->challenge.size; + input_buf.pvBuffer = ctx->challenge.ptr; + } + + status = InitializeSecurityContextW(&ctx->cred, + ctx->has_context ? &ctx->context : NULL, + ctx->target, + context_flags, + 0, + SECURITY_NETWORK_DREP, + ctx->has_context ? &input_buf_desc : NULL, + 0, + ctx->has_context ? NULL : &ctx->context, + &output_buf_desc, + &context_flags, + NULL); + + if (status == SEC_I_COMPLETE_AND_CONTINUE || + status == SEC_I_COMPLETE_NEEDED) + status = CompleteAuthToken(&ctx->context, &output_buf_desc); + + if (status == SEC_E_OK) { + ctx->complete = 1; + } else if (status != SEC_I_CONTINUE_NEEDED) { + git_error_set(GIT_ERROR_OS, "could not initialize security context"); + return -1; + } + + ctx->has_context = 1; + git_str_clear(&ctx->challenge); + + if (output_buf.cbBuffer > 0) { + git_str_put(buf, ctx->package_name, ctx->package_name_len); + git_str_putc(buf, ' '); + git_str_encode_base64(buf, output_buf.pvBuffer, output_buf.cbBuffer); + + FreeContextBuffer(output_buf.pvBuffer); + + if (git_str_oom(buf)) + return -1; + } + + return 0; +} + +static int sspi_is_complete(git_http_auth_context *c) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + + return ctx->complete; +} + +static void sspi_context_free(git_http_auth_context *c) +{ + http_auth_sspi_context *ctx = (http_auth_sspi_context *)c; + + sspi_reset_context(ctx); + + FreeContextBuffer(ctx->package_info); + git__free(ctx->target); + git__free(ctx); +} + +static int sspi_init_context( + git_http_auth_context **out, + git_http_auth_t type, + const git_net_url *url) +{ + http_auth_sspi_context *ctx; + git_str target = GIT_STR_INIT; + + *out = NULL; + + ctx = git__calloc(1, sizeof(http_auth_sspi_context)); + GIT_ERROR_CHECK_ALLOC(ctx); + + switch (type) { + case GIT_HTTP_AUTH_NTLM: + ctx->package_name = "NTLM"; + ctx->package_name_len = CONST_STRLEN("NTLM"); + ctx->package_name_w = L"NTLM"; + ctx->parent.credtypes = GIT_CREDENTIAL_USERPASS_PLAINTEXT | + GIT_CREDENTIAL_DEFAULT; + break; + case GIT_HTTP_AUTH_NEGOTIATE: + ctx->package_name = "Negotiate"; + ctx->package_name_len = CONST_STRLEN("Negotiate"); + ctx->package_name_w = L"Negotiate"; + ctx->parent.credtypes = GIT_CREDENTIAL_DEFAULT; + break; + default: + git_error_set(GIT_ERROR_NET, "unknown SSPI auth type: %d", ctx->parent.type); + git__free(ctx); + return -1; + } + + if (QuerySecurityPackageInfoW(ctx->package_name_w, &ctx->package_info) != SEC_E_OK) { + git_error_set(GIT_ERROR_OS, "could not query security package"); + git__free(ctx); + return -1; + } + + if (git_str_printf(&target, "http/%s", url->host) < 0 || + git_utf8_to_16_alloc(&ctx->target, target.ptr) < 0) { + FreeContextBuffer(ctx->package_info); + git__free(ctx); + return -1; + } + + ctx->parent.type = type; + ctx->parent.connection_affinity = 1; + ctx->parent.set_challenge = sspi_set_challenge; + ctx->parent.next_token = sspi_next_token; + ctx->parent.is_complete = sspi_is_complete; + ctx->parent.free = sspi_context_free; + + *out = (git_http_auth_context *)ctx; + + git_str_dispose(&target); + return 0; +} + +int git_http_auth_negotiate( + git_http_auth_context **out, + const git_net_url *url) +{ + return sspi_init_context(out, GIT_HTTP_AUTH_NEGOTIATE, url); +} + +int git_http_auth_ntlm( + git_http_auth_context **out, + const git_net_url *url) +{ + return sspi_init_context(out, GIT_HTTP_AUTH_NTLM, url); +} + +#endif /* GIT_WIN32 */ diff --git a/src/libgit2/transports/winhttp.c b/src/libgit2/transports/winhttp.c index 098227607..de24a2a41 100644 --- a/src/libgit2/transports/winhttp.c +++ b/src/libgit2/transports/winhttp.c @@ -158,10 +158,10 @@ static int apply_userpass_credentials(HINTERNET request, DWORD target, int mecha goto done; } - if ((error = user_len = git__utf8_to_16_alloc(&user, c->username)) < 0) + if ((error = user_len = git_utf8_to_16_alloc(&user, c->username)) < 0) goto done; - if ((error = pass_len = git__utf8_to_16_alloc(&pass, c->password)) < 0) + if ((error = pass_len = git_utf8_to_16_alloc(&pass, c->password)) < 0) goto done; if (!WinHttpSetCredentials(request, target, native_scheme, user, pass, NULL)) { @@ -242,7 +242,7 @@ static int acquire_fallback_cred( HRESULT hCoInitResult; /* Convert URL to wide characters */ - if (git__utf8_to_16_alloc(&wide_url, url) < 0) { + if (git_utf8_to_16_alloc(&wide_url, url) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert string to wide form"); return -1; } @@ -397,7 +397,7 @@ static int winhttp_stream_connect(winhttp_stream *s) return -1; /* Convert URL to wide characters */ - if (git__utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) { + if (git_utf8_to_16_alloc(&s->request_uri, git_str_cstr(&buf)) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert string to wide form"); goto on_error; } @@ -473,7 +473,7 @@ static int winhttp_stream_connect(winhttp_stream *s) } /* Convert URL to wide characters */ - error = git__utf8_to_16_alloc(&proxy_wide, processed_url.ptr); + error = git_utf8_to_16_alloc(&proxy_wide, processed_url.ptr); git_str_dispose(&processed_url); if (error < 0) goto on_error; @@ -531,7 +531,7 @@ static int winhttp_stream_connect(winhttp_stream *s) s->service) < 0) goto on_error; - if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { + if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert content-type to wide characters"); goto on_error; } @@ -548,7 +548,7 @@ static int winhttp_stream_connect(winhttp_stream *s) s->service) < 0) goto on_error; - if (git__utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { + if (git_utf8_to_16(ct, MAX_CONTENT_TYPE_LEN, git_str_cstr(&buf)) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert accept header to wide characters"); goto on_error; } @@ -568,7 +568,7 @@ static int winhttp_stream_connect(winhttp_stream *s) git_str_puts(&buf, t->owner->connect_opts.custom_headers.strings[i]); /* Convert header to wide characters */ - if ((error = git__utf8_to_16_alloc(&custom_header_wide, git_str_cstr(&buf))) < 0) + if ((error = git_utf8_to_16_alloc(&custom_header_wide, git_str_cstr(&buf))) < 0) goto on_error; if (!WinHttpAddRequestHeaders(s->request, custom_header_wide, (ULONG)-1L, @@ -783,7 +783,7 @@ static int winhttp_connect( } /* Prepare host */ - if (git__utf8_to_16_alloc(&wide_host, host) < 0) { + if (git_utf8_to_16_alloc(&wide_host, host) < 0) { git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters"); goto on_error; } @@ -792,7 +792,7 @@ static int winhttp_connect( if (git_http__user_agent(&ua) < 0) goto on_error; - if (git__utf8_to_16_alloc(&wide_ua, git_str_cstr(&ua)) < 0) { + if (git_utf8_to_16_alloc(&wide_ua, git_str_cstr(&ua)) < 0) { git_error_set(GIT_ERROR_OS, "unable to convert host to wide characters"); goto on_error; } @@ -1182,7 +1182,7 @@ replay: } /* Convert the Location header to UTF-8 */ - if (git__utf16_to_8_alloc(&location8, location) < 0) { + if (git_utf8_from_16_alloc(&location8, location) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert Location header to UTF-8"); git__free(location); return -1; @@ -1254,7 +1254,7 @@ replay: else p_snprintf(expected_content_type_8, MAX_CONTENT_TYPE_LEN, "application/x-git-%s-advertisement", s->service); - if (git__utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) { + if (git_utf8_to_16(expected_content_type, MAX_CONTENT_TYPE_LEN, expected_content_type_8) < 0) { git_error_set(GIT_ERROR_OS, "failed to convert expected content-type to wide characters"); return -1; } diff --git a/src/libgit2/tree-cache.c b/src/libgit2/tree-cache.c index 19fad85ae..95d879860 100644 --- a/src/libgit2/tree-cache.c +++ b/src/libgit2/tree-cache.c @@ -71,12 +71,16 @@ const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char } } -static int read_tree_internal(git_tree_cache **out, - const char **buffer_in, const char *buffer_end, - git_pool *pool) +static int read_tree_internal( + git_tree_cache **out, + const char **buffer_in, + const char *buffer_end, + git_oid_t oid_type, + git_pool *pool) { git_tree_cache *tree = NULL; const char *name_start, *buffer; + size_t oid_size = git_oid_size(oid_type); int count; buffer = name_start = *buffer_in; @@ -87,7 +91,7 @@ static int read_tree_internal(git_tree_cache **out, if (++buffer >= buffer_end) goto corrupted; - if (git_tree_cache_new(&tree, name_start, pool) < 0) + if (git_tree_cache_new(&tree, name_start, oid_type, pool) < 0) return -1; /* Blank-terminated ASCII decimal number of entries in this tree */ @@ -108,14 +112,14 @@ static int read_tree_internal(git_tree_cache **out, if (*buffer != '\n' || ++buffer > buffer_end) goto corrupted; - /* The SHA1 is only there if it's not invalidated */ + /* The OID is only there if it's not invalidated */ if (tree->entry_count >= 0) { /* 160-bit SHA-1 for this tree and it's children */ - if (buffer + GIT_OID_SHA1_SIZE > buffer_end) + if (buffer + oid_size > buffer_end) goto corrupted; - git_oid__fromraw(&tree->oid, (const unsigned char *)buffer, GIT_OID_SHA1); - buffer += GIT_OID_SHA1_SIZE; + git_oid__fromraw(&tree->oid, (const unsigned char *)buffer, oid_type); + buffer += oid_size; } /* Parse children: */ @@ -130,7 +134,7 @@ static int read_tree_internal(git_tree_cache **out, memset(tree->children, 0x0, bufsize); for (i = 0; i < tree->children_count; ++i) { - if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0) + if (read_tree_internal(&tree->children[i], &buffer, buffer_end, oid_type, pool) < 0) goto corrupted; } } @@ -144,11 +148,16 @@ static int read_tree_internal(git_tree_cache **out, return -1; } -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool) +int git_tree_cache_read( + git_tree_cache **tree, + const char *buffer, + size_t buffer_size, + git_oid_t oid_type, + git_pool *pool) { const char *buffer_end = buffer + buffer_size; - if (read_tree_internal(tree, &buffer, buffer_end, pool) < 0) + if (read_tree_internal(tree, &buffer, buffer_end, oid_type, pool) < 0) return -1; if (buffer < buffer_end) { @@ -201,7 +210,7 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_ continue; } - if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), pool)) < 0) + if ((error = git_tree_cache_new(&cache->children[j], git_tree_entry_name(entry), cache->oid_type, pool)) < 0) return error; if ((error = git_tree_lookup(&subtree, repo, git_tree_entry_id(entry))) < 0) @@ -219,12 +228,12 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_ return 0; } -int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool) +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool) { int error; git_tree_cache *cache; - if ((error = git_tree_cache_new(&cache, "", pool)) < 0) + if ((error = git_tree_cache_new(&cache, "", oid_type, pool)) < 0) return error; if ((error = read_tree_recursive(cache, tree, pool)) < 0) @@ -234,7 +243,7 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo return 0; } -int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool) { size_t name_len, alloc_size; git_tree_cache *tree; @@ -248,6 +257,7 @@ int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool) memset(tree, 0x0, sizeof(git_tree_cache)); /* NUL-terminated tree name */ + tree->oid_type = oid_type; tree->namelen = name_len; memcpy(tree->name, name, name_len); tree->name[name_len] = '\0'; @@ -263,7 +273,7 @@ static void write_tree(git_str *out, git_tree_cache *tree) git_str_printf(out, "%s%c%"PRIdZ" %"PRIuZ"\n", tree->name, 0, tree->entry_count, tree->children_count); if (tree->entry_count != -1) - git_str_put(out, (char *)&tree->oid.id, GIT_OID_SHA1_SIZE); + git_str_put(out, (char *)&tree->oid.id, git_oid_size(tree->oid_type)); for (i = 0; i < tree->children_count; i++) write_tree(out, tree->children[i]); diff --git a/src/libgit2/tree-cache.h b/src/libgit2/tree-cache.h index a27e30466..e4a73f277 100644 --- a/src/libgit2/tree-cache.h +++ b/src/libgit2/tree-cache.h @@ -18,6 +18,8 @@ typedef struct git_tree_cache { struct git_tree_cache **children; size_t children_count; + git_oid_t oid_type; + ssize_t entry_count; git_oid oid; size_t namelen; @@ -25,14 +27,14 @@ typedef struct git_tree_cache { } git_tree_cache; int git_tree_cache_write(git_str *out, git_tree_cache *tree); -int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_pool *pool); +int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size, git_oid_t oid_type, git_pool *pool); void git_tree_cache_invalidate_path(git_tree_cache *tree, const char *path); const git_tree_cache *git_tree_cache_get(const git_tree_cache *tree, const char *path); -int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool); +int git_tree_cache_new(git_tree_cache **out, const char *name, git_oid_t oid_type, git_pool *pool); /** * Read a tree as the root of the tree cache (like for `git read-tree`) */ -int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_pool *pool); +int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_oid_t oid_type, git_pool *pool); void git_tree_cache_free(git_tree_cache *tree); #endif diff --git a/src/libgit2/tree.c b/src/libgit2/tree.c index 9293d9422..236a87f7e 100644 --- a/src/libgit2/tree.c +++ b/src/libgit2/tree.c @@ -731,7 +731,7 @@ int git_tree__write_index( return ret; /* Read the tree cache into the index */ - ret = git_tree_cache_read_tree(&index->tree, tree, &index->tree_pool); + ret = git_tree_cache_read_tree(&index->tree, tree, index->oid_type, &index->tree_pool); git_tree_free(tree); return ret; diff --git a/src/libgit2/xdiff/git-xdiff.h b/src/libgit2/xdiff/git-xdiff.h deleted file mode 100644 index b75dba819..000000000 --- a/src/libgit2/xdiff/git-xdiff.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (C) the libgit2 contributors. All rights reserved. - * - * This file is part of libgit2, distributed under the GNU GPL v2 with - * a Linking Exception. For full terms see the included COPYING file. - */ - -/* - * This file provides the necessary indirection between xdiff and - * libgit2. libgit2-specific functionality should live here, so - * that git and libgit2 can share a common xdiff implementation. - */ - -#ifndef INCLUDE_git_xdiff_h__ -#define INCLUDE_git_xdiff_h__ - -#include "regexp.h" - -/* Work around C90-conformance issues */ -#if !defined(__STDC_VERSION__) || (__STDC_VERSION__ < 199901L) -# if defined(_MSC_VER) -# define inline __inline -# elif defined(__GNUC__) -# define inline __inline__ -# else -# define inline -# endif -#endif - -#define xdl_malloc(x) git__malloc(x) -#define xdl_free(ptr) git__free(ptr) -#define xdl_realloc(ptr, x) git__realloc(ptr, x) - -#define XDL_BUG(msg) GIT_ASSERT(msg) - -#define xdl_regex_t git_regexp -#define xdl_regmatch_t git_regmatch - -GIT_INLINE(int) xdl_regexec_buf( - const xdl_regex_t *preg, const char *buf, size_t size, - size_t nmatch, xdl_regmatch_t pmatch[], int eflags) -{ - GIT_UNUSED(preg); - GIT_UNUSED(buf); - GIT_UNUSED(size); - GIT_UNUSED(nmatch); - GIT_UNUSED(pmatch); - GIT_UNUSED(eflags); - GIT_ASSERT("not implemented"); - return -1; -} - -#endif diff --git a/src/libgit2/xdiff/xdiff.h b/src/libgit2/xdiff/xdiff.h deleted file mode 100644 index fb47f63fb..000000000 --- a/src/libgit2/xdiff/xdiff.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XDIFF_H) -#define XDIFF_H - -#ifdef __cplusplus -extern "C" { -#endif /* #ifdef __cplusplus */ - -#include "git-xdiff.h" - -/* xpparm_t.flags */ -#define XDF_NEED_MINIMAL (1 << 0) - -#define XDF_IGNORE_WHITESPACE (1 << 1) -#define XDF_IGNORE_WHITESPACE_CHANGE (1 << 2) -#define XDF_IGNORE_WHITESPACE_AT_EOL (1 << 3) -#define XDF_IGNORE_CR_AT_EOL (1 << 4) -#define XDF_WHITESPACE_FLAGS (XDF_IGNORE_WHITESPACE | \ - XDF_IGNORE_WHITESPACE_CHANGE | \ - XDF_IGNORE_WHITESPACE_AT_EOL | \ - XDF_IGNORE_CR_AT_EOL) - -#define XDF_IGNORE_BLANK_LINES (1 << 7) - -#define XDF_PATIENCE_DIFF (1 << 14) -#define XDF_HISTOGRAM_DIFF (1 << 15) -#define XDF_DIFF_ALGORITHM_MASK (XDF_PATIENCE_DIFF | XDF_HISTOGRAM_DIFF) -#define XDF_DIFF_ALG(x) ((x) & XDF_DIFF_ALGORITHM_MASK) - -#define XDF_INDENT_HEURISTIC (1 << 23) - -/* xdemitconf_t.flags */ -#define XDL_EMIT_FUNCNAMES (1 << 0) -#define XDL_EMIT_NO_HUNK_HDR (1 << 1) -#define XDL_EMIT_FUNCCONTEXT (1 << 2) - -/* merge simplification levels */ -#define XDL_MERGE_MINIMAL 0 -#define XDL_MERGE_EAGER 1 -#define XDL_MERGE_ZEALOUS 2 -#define XDL_MERGE_ZEALOUS_ALNUM 3 - -/* merge favor modes */ -#define XDL_MERGE_FAVOR_OURS 1 -#define XDL_MERGE_FAVOR_THEIRS 2 -#define XDL_MERGE_FAVOR_UNION 3 - -/* merge output styles */ -#define XDL_MERGE_DIFF3 1 -#define XDL_MERGE_ZEALOUS_DIFF3 2 - -typedef struct s_mmfile { - char *ptr; - long size; -} mmfile_t; - -typedef struct s_mmbuffer { - char *ptr; - long size; -} mmbuffer_t; - -typedef struct s_xpparam { - unsigned long flags; - - /* -I<regex> */ - xdl_regex_t **ignore_regex; - size_t ignore_regex_nr; - - /* See Documentation/diff-options.txt. */ - char **anchors; - size_t anchors_nr; -} xpparam_t; - -typedef struct s_xdemitcb { - void *priv; - int (*out_hunk)(void *, - long old_begin, long old_nr, - long new_begin, long new_nr, - const char *func, long funclen); - int (*out_line)(void *, mmbuffer_t *, int); -} xdemitcb_t; - -typedef long (*find_func_t)(const char *line, long line_len, char *buffer, long buffer_size, void *priv); - -typedef int (*xdl_emit_hunk_consume_func_t)(long start_a, long count_a, - long start_b, long count_b, - void *cb_data); - -typedef struct s_xdemitconf { - long ctxlen; - long interhunkctxlen; - unsigned long flags; - find_func_t find_func; - void *find_func_priv; - xdl_emit_hunk_consume_func_t hunk_func; -} xdemitconf_t; - -typedef struct s_bdiffparam { - long bsize; -} bdiffparam_t; - - -void *xdl_mmfile_first(mmfile_t *mmf, long *size); -long xdl_mmfile_size(mmfile_t *mmf); - -int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdemitconf_t const *xecfg, xdemitcb_t *ecb); - -typedef struct s_xmparam { - xpparam_t xpp; - int marker_size; - int level; - int favor; - int style; - const char *ancestor; /* label for orig */ - const char *file1; /* label for mf1 */ - const char *file2; /* label for mf2 */ -} xmparam_t; - -#define DEFAULT_CONFLICT_MARKER_SIZE 7 - -int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, - xmparam_t const *xmp, mmbuffer_t *result); - -#ifdef __cplusplus -} -#endif /* #ifdef __cplusplus */ - -#endif /* #if !defined(XDIFF_H) */ diff --git a/src/libgit2/xdiff/xdiffi.c b/src/libgit2/xdiff/xdiffi.c deleted file mode 100644 index af31b7f4b..000000000 --- a/src/libgit2/xdiff/xdiffi.c +++ /dev/null @@ -1,1088 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - -#define XDL_MAX_COST_MIN 256 -#define XDL_HEUR_MIN_COST 256 -#define XDL_LINE_MAX (long)((1UL << (CHAR_BIT * sizeof(long) - 1)) - 1) -#define XDL_SNAKE_CNT 20 -#define XDL_K_HEUR 4 - -typedef struct s_xdpsplit { - long i1, i2; - int min_lo, min_hi; -} xdpsplit_t; - -/* - * See "An O(ND) Difference Algorithm and its Variations", by Eugene Myers. - * Basically considers a "box" (off1, off2, lim1, lim2) and scan from both - * the forward diagonal starting from (off1, off2) and the backward diagonal - * starting from (lim1, lim2). If the K values on the same diagonal crosses - * returns the furthest point of reach. We might encounter expensive edge cases - * using this algorithm, so a little bit of heuristic is needed to cut the - * search and to return a suboptimal point. - */ -static long xdl_split(unsigned long const *ha1, long off1, long lim1, - unsigned long const *ha2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdpsplit_t *spl, - xdalgoenv_t *xenv) { - long dmin = off1 - lim2, dmax = lim1 - off2; - long fmid = off1 - off2, bmid = lim1 - lim2; - long odd = (fmid - bmid) & 1; - long fmin = fmid, fmax = fmid; - long bmin = bmid, bmax = bmid; - long ec, d, i1, i2, prev1, best, dd, v, k; - - /* - * Set initial diagonal values for both forward and backward path. - */ - kvdf[fmid] = off1; - kvdb[bmid] = lim1; - - for (ec = 1;; ec++) { - int got_snake = 0; - - /* - * We need to extend the diagonal "domain" by one. If the next - * values exits the box boundaries we need to change it in the - * opposite direction because (max - min) must be a power of - * two. - * - * Also we initialize the external K value to -1 so that we can - * avoid extra conditions in the check inside the core loop. - */ - if (fmin > dmin) - kvdf[--fmin - 1] = -1; - else - ++fmin; - if (fmax < dmax) - kvdf[++fmax + 1] = -1; - else - --fmax; - - for (d = fmax; d >= fmin; d -= 2) { - if (kvdf[d - 1] >= kvdf[d + 1]) - i1 = kvdf[d - 1] + 1; - else - i1 = kvdf[d + 1]; - prev1 = i1; - i2 = i1 - d; - for (; i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2]; i1++, i2++); - if (i1 - prev1 > xenv->snake_cnt) - got_snake = 1; - kvdf[d] = i1; - if (odd && bmin <= d && d <= bmax && kvdb[d] <= i1) { - spl->i1 = i1; - spl->i2 = i2; - spl->min_lo = spl->min_hi = 1; - return ec; - } - } - - /* - * We need to extend the diagonal "domain" by one. If the next - * values exits the box boundaries we need to change it in the - * opposite direction because (max - min) must be a power of - * two. - * - * Also we initialize the external K value to -1 so that we can - * avoid extra conditions in the check inside the core loop. - */ - if (bmin > dmin) - kvdb[--bmin - 1] = XDL_LINE_MAX; - else - ++bmin; - if (bmax < dmax) - kvdb[++bmax + 1] = XDL_LINE_MAX; - else - --bmax; - - for (d = bmax; d >= bmin; d -= 2) { - if (kvdb[d - 1] < kvdb[d + 1]) - i1 = kvdb[d - 1]; - else - i1 = kvdb[d + 1] - 1; - prev1 = i1; - i2 = i1 - d; - for (; i1 > off1 && i2 > off2 && ha1[i1 - 1] == ha2[i2 - 1]; i1--, i2--); - if (prev1 - i1 > xenv->snake_cnt) - got_snake = 1; - kvdb[d] = i1; - if (!odd && fmin <= d && d <= fmax && i1 <= kvdf[d]) { - spl->i1 = i1; - spl->i2 = i2; - spl->min_lo = spl->min_hi = 1; - return ec; - } - } - - if (need_min) - continue; - - /* - * If the edit cost is above the heuristic trigger and if - * we got a good snake, we sample current diagonals to see - * if some of them have reached an "interesting" path. Our - * measure is a function of the distance from the diagonal - * corner (i1 + i2) penalized with the distance from the - * mid diagonal itself. If this value is above the current - * edit cost times a magic factor (XDL_K_HEUR) we consider - * it interesting. - */ - if (got_snake && ec > xenv->heur_min) { - for (best = 0, d = fmax; d >= fmin; d -= 2) { - dd = d > fmid ? d - fmid: fmid - d; - i1 = kvdf[d]; - i2 = i1 - d; - v = (i1 - off1) + (i2 - off2) - dd; - - if (v > XDL_K_HEUR * ec && v > best && - off1 + xenv->snake_cnt <= i1 && i1 < lim1 && - off2 + xenv->snake_cnt <= i2 && i2 < lim2) { - for (k = 1; ha1[i1 - k] == ha2[i2 - k]; k++) - if (k == xenv->snake_cnt) { - best = v; - spl->i1 = i1; - spl->i2 = i2; - break; - } - } - } - if (best > 0) { - spl->min_lo = 1; - spl->min_hi = 0; - return ec; - } - - for (best = 0, d = bmax; d >= bmin; d -= 2) { - dd = d > bmid ? d - bmid: bmid - d; - i1 = kvdb[d]; - i2 = i1 - d; - v = (lim1 - i1) + (lim2 - i2) - dd; - - if (v > XDL_K_HEUR * ec && v > best && - off1 < i1 && i1 <= lim1 - xenv->snake_cnt && - off2 < i2 && i2 <= lim2 - xenv->snake_cnt) { - for (k = 0; ha1[i1 + k] == ha2[i2 + k]; k++) - if (k == xenv->snake_cnt - 1) { - best = v; - spl->i1 = i1; - spl->i2 = i2; - break; - } - } - } - if (best > 0) { - spl->min_lo = 0; - spl->min_hi = 1; - return ec; - } - } - - /* - * Enough is enough. We spent too much time here and now we - * collect the furthest reaching path using the (i1 + i2) - * measure. - */ - if (ec >= xenv->mxcost) { - long fbest, fbest1, bbest, bbest1; - - fbest = fbest1 = -1; - for (d = fmax; d >= fmin; d -= 2) { - i1 = XDL_MIN(kvdf[d], lim1); - i2 = i1 - d; - if (lim2 < i2) - i1 = lim2 + d, i2 = lim2; - if (fbest < i1 + i2) { - fbest = i1 + i2; - fbest1 = i1; - } - } - - bbest = bbest1 = XDL_LINE_MAX; - for (d = bmax; d >= bmin; d -= 2) { - i1 = XDL_MAX(off1, kvdb[d]); - i2 = i1 - d; - if (i2 < off2) - i1 = off2 + d, i2 = off2; - if (i1 + i2 < bbest) { - bbest = i1 + i2; - bbest1 = i1; - } - } - - if ((lim1 + lim2) - bbest < fbest - (off1 + off2)) { - spl->i1 = fbest1; - spl->i2 = fbest - fbest1; - spl->min_lo = 1; - spl->min_hi = 0; - } else { - spl->i1 = bbest1; - spl->i2 = bbest - bbest1; - spl->min_lo = 0; - spl->min_hi = 1; - } - return ec; - } - } -} - - -/* - * Rule: "Divide et Impera" (divide & conquer). Recursively split the box in - * sub-boxes by calling the box splitting function. Note that the real job - * (marking changed lines) is done in the two boundary reaching checks. - */ -int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, - diffdata_t *dd2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv) { - unsigned long const *ha1 = dd1->ha, *ha2 = dd2->ha; - - /* - * Shrink the box by walking through each diagonal snake (SW and NE). - */ - for (; off1 < lim1 && off2 < lim2 && ha1[off1] == ha2[off2]; off1++, off2++); - for (; off1 < lim1 && off2 < lim2 && ha1[lim1 - 1] == ha2[lim2 - 1]; lim1--, lim2--); - - /* - * If one dimension is empty, then all records on the other one must - * be obviously changed. - */ - if (off1 == lim1) { - char *rchg2 = dd2->rchg; - long *rindex2 = dd2->rindex; - - for (; off2 < lim2; off2++) - rchg2[rindex2[off2]] = 1; - } else if (off2 == lim2) { - char *rchg1 = dd1->rchg; - long *rindex1 = dd1->rindex; - - for (; off1 < lim1; off1++) - rchg1[rindex1[off1]] = 1; - } else { - xdpsplit_t spl; - spl.i1 = spl.i2 = 0; - - /* - * Divide ... - */ - if (xdl_split(ha1, off1, lim1, ha2, off2, lim2, kvdf, kvdb, - need_min, &spl, xenv) < 0) { - - return -1; - } - - /* - * ... et Impera. - */ - if (xdl_recs_cmp(dd1, off1, spl.i1, dd2, off2, spl.i2, - kvdf, kvdb, spl.min_lo, xenv) < 0 || - xdl_recs_cmp(dd1, spl.i1, lim1, dd2, spl.i2, lim2, - kvdf, kvdb, spl.min_hi, xenv) < 0) { - - return -1; - } - } - - return 0; -} - - -int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *xe) { - long ndiags; - long *kvd, *kvdf, *kvdb; - xdalgoenv_t xenv; - diffdata_t dd1, dd2; - - if (XDF_DIFF_ALG(xpp->flags) == XDF_PATIENCE_DIFF) - return xdl_do_patience_diff(mf1, mf2, xpp, xe); - - if (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF) - return xdl_do_histogram_diff(mf1, mf2, xpp, xe); - - if (xdl_prepare_env(mf1, mf2, xpp, xe) < 0) { - - return -1; - } - - /* - * Allocate and setup K vectors to be used by the differential - * algorithm. - * - * One is to store the forward path and one to store the backward path. - */ - ndiags = xe->xdf1.nreff + xe->xdf2.nreff + 3; - if (!(kvd = (long *) xdl_malloc((2 * ndiags + 2) * sizeof(long)))) { - - xdl_free_env(xe); - return -1; - } - kvdf = kvd; - kvdb = kvdf + ndiags; - kvdf += xe->xdf2.nreff + 1; - kvdb += xe->xdf2.nreff + 1; - - xenv.mxcost = xdl_bogosqrt(ndiags); - if (xenv.mxcost < XDL_MAX_COST_MIN) - xenv.mxcost = XDL_MAX_COST_MIN; - xenv.snake_cnt = XDL_SNAKE_CNT; - xenv.heur_min = XDL_HEUR_MIN_COST; - - dd1.nrec = xe->xdf1.nreff; - dd1.ha = xe->xdf1.ha; - dd1.rchg = xe->xdf1.rchg; - dd1.rindex = xe->xdf1.rindex; - dd2.nrec = xe->xdf2.nreff; - dd2.ha = xe->xdf2.ha; - dd2.rchg = xe->xdf2.rchg; - dd2.rindex = xe->xdf2.rindex; - - if (xdl_recs_cmp(&dd1, 0, dd1.nrec, &dd2, 0, dd2.nrec, - kvdf, kvdb, (xpp->flags & XDF_NEED_MINIMAL) != 0, &xenv) < 0) { - - xdl_free(kvd); - xdl_free_env(xe); - return -1; - } - - xdl_free(kvd); - - return 0; -} - - -static xdchange_t *xdl_add_change(xdchange_t *xscr, long i1, long i2, long chg1, long chg2) { - xdchange_t *xch; - - if (!(xch = (xdchange_t *) xdl_malloc(sizeof(xdchange_t)))) - return NULL; - - xch->next = xscr; - xch->i1 = i1; - xch->i2 = i2; - xch->chg1 = chg1; - xch->chg2 = chg2; - xch->ignore = 0; - - return xch; -} - - -static int recs_match(xrecord_t *rec1, xrecord_t *rec2) -{ - return (rec1->ha == rec2->ha); -} - -/* - * If a line is indented more than this, get_indent() just returns this value. - * This avoids having to do absurd amounts of work for data that are not - * human-readable text, and also ensures that the output of get_indent fits - * within an int. - */ -#define MAX_INDENT 200 - -/* - * Return the amount of indentation of the specified line, treating TAB as 8 - * columns. Return -1 if line is empty or contains only whitespace. Clamp the - * output value at MAX_INDENT. - */ -static int get_indent(xrecord_t *rec) -{ - long i; - int ret = 0; - - for (i = 0; i < rec->size; i++) { - char c = rec->ptr[i]; - - if (!XDL_ISSPACE(c)) - return ret; - else if (c == ' ') - ret += 1; - else if (c == '\t') - ret += 8 - ret % 8; - /* ignore other whitespace characters */ - - if (ret >= MAX_INDENT) - return MAX_INDENT; - } - - /* The line contains only whitespace. */ - return -1; -} - -/* - * If more than this number of consecutive blank rows are found, just return - * this value. This avoids requiring O(N^2) work for pathological cases, and - * also ensures that the output of score_split fits in an int. - */ -#define MAX_BLANKS 20 - -/* Characteristics measured about a hypothetical split position. */ -struct split_measurement { - /* - * Is the split at the end of the file (aside from any blank lines)? - */ - int end_of_file; - - /* - * How much is the line immediately following the split indented (or -1 - * if the line is blank): - */ - int indent; - - /* - * How many consecutive lines above the split are blank? - */ - int pre_blank; - - /* - * How much is the nearest non-blank line above the split indented (or - * -1 if there is no such line)? - */ - int pre_indent; - - /* - * How many lines after the line following the split are blank? - */ - int post_blank; - - /* - * How much is the nearest non-blank line after the line following the - * split indented (or -1 if there is no such line)? - */ - int post_indent; -}; - -struct split_score { - /* The effective indent of this split (smaller is preferred). */ - int effective_indent; - - /* Penalty for this split (smaller is preferred). */ - int penalty; -}; - -/* - * Fill m with information about a hypothetical split of xdf above line split. - */ -static void measure_split(const xdfile_t *xdf, long split, - struct split_measurement *m) -{ - long i; - - if (split >= xdf->nrec) { - m->end_of_file = 1; - m->indent = -1; - } else { - m->end_of_file = 0; - m->indent = get_indent(xdf->recs[split]); - } - - m->pre_blank = 0; - m->pre_indent = -1; - for (i = split - 1; i >= 0; i--) { - m->pre_indent = get_indent(xdf->recs[i]); - if (m->pre_indent != -1) - break; - m->pre_blank += 1; - if (m->pre_blank == MAX_BLANKS) { - m->pre_indent = 0; - break; - } - } - - m->post_blank = 0; - m->post_indent = -1; - for (i = split + 1; i < xdf->nrec; i++) { - m->post_indent = get_indent(xdf->recs[i]); - if (m->post_indent != -1) - break; - m->post_blank += 1; - if (m->post_blank == MAX_BLANKS) { - m->post_indent = 0; - break; - } - } -} - -/* - * The empirically-determined weight factors used by score_split() below. - * Larger values means that the position is a less favorable place to split. - * - * Note that scores are only ever compared against each other, so multiplying - * all of these weight/penalty values by the same factor wouldn't change the - * heuristic's behavior. Still, we need to set that arbitrary scale *somehow*. - * In practice, these numbers are chosen to be large enough that they can be - * adjusted relative to each other with sufficient precision despite using - * integer math. - */ - -/* Penalty if there are no non-blank lines before the split */ -#define START_OF_FILE_PENALTY 1 - -/* Penalty if there are no non-blank lines after the split */ -#define END_OF_FILE_PENALTY 21 - -/* Multiplier for the number of blank lines around the split */ -#define TOTAL_BLANK_WEIGHT (-30) - -/* Multiplier for the number of blank lines after the split */ -#define POST_BLANK_WEIGHT 6 - -/* - * Penalties applied if the line is indented more than its predecessor - */ -#define RELATIVE_INDENT_PENALTY (-4) -#define RELATIVE_INDENT_WITH_BLANK_PENALTY 10 - -/* - * Penalties applied if the line is indented less than both its predecessor and - * its successor - */ -#define RELATIVE_OUTDENT_PENALTY 24 -#define RELATIVE_OUTDENT_WITH_BLANK_PENALTY 17 - -/* - * Penalties applied if the line is indented less than its predecessor but not - * less than its successor - */ -#define RELATIVE_DEDENT_PENALTY 23 -#define RELATIVE_DEDENT_WITH_BLANK_PENALTY 17 - -/* - * We only consider whether the sum of the effective indents for splits are - * less than (-1), equal to (0), or greater than (+1) each other. The resulting - * value is multiplied by the following weight and combined with the penalty to - * determine the better of two scores. - */ -#define INDENT_WEIGHT 60 - -/* - * How far do we slide a hunk at most? - */ -#define INDENT_HEURISTIC_MAX_SLIDING 100 - -/* - * Compute a badness score for the hypothetical split whose measurements are - * stored in m. The weight factors were determined empirically using the tools - * and corpus described in - * - * https://github.com/mhagger/diff-slider-tools - * - * Also see that project if you want to improve the weights based on, for - * example, a larger or more diverse corpus. - */ -static void score_add_split(const struct split_measurement *m, struct split_score *s) -{ - /* - * A place to accumulate penalty factors (positive makes this index more - * favored): - */ - int post_blank, total_blank, indent, any_blanks; - - if (m->pre_indent == -1 && m->pre_blank == 0) - s->penalty += START_OF_FILE_PENALTY; - - if (m->end_of_file) - s->penalty += END_OF_FILE_PENALTY; - - /* - * Set post_blank to the number of blank lines following the split, - * including the line immediately after the split: - */ - post_blank = (m->indent == -1) ? 1 + m->post_blank : 0; - total_blank = m->pre_blank + post_blank; - - /* Penalties based on nearby blank lines: */ - s->penalty += TOTAL_BLANK_WEIGHT * total_blank; - s->penalty += POST_BLANK_WEIGHT * post_blank; - - if (m->indent != -1) - indent = m->indent; - else - indent = m->post_indent; - - any_blanks = (total_blank != 0); - - /* Note that the effective indent is -1 at the end of the file: */ - s->effective_indent += indent; - - if (indent == -1) { - /* No additional adjustments needed. */ - } else if (m->pre_indent == -1) { - /* No additional adjustments needed. */ - } else if (indent > m->pre_indent) { - /* - * The line is indented more than its predecessor. - */ - s->penalty += any_blanks ? - RELATIVE_INDENT_WITH_BLANK_PENALTY : - RELATIVE_INDENT_PENALTY; - } else if (indent == m->pre_indent) { - /* - * The line has the same indentation level as its predecessor. - * No additional adjustments needed. - */ - } else { - /* - * The line is indented less than its predecessor. It could be - * the block terminator of the previous block, but it could - * also be the start of a new block (e.g., an "else" block, or - * maybe the previous block didn't have a block terminator). - * Try to distinguish those cases based on what comes next: - */ - if (m->post_indent != -1 && m->post_indent > indent) { - /* - * The following line is indented more. So it is likely - * that this line is the start of a block. - */ - s->penalty += any_blanks ? - RELATIVE_OUTDENT_WITH_BLANK_PENALTY : - RELATIVE_OUTDENT_PENALTY; - } else { - /* - * That was probably the end of a block. - */ - s->penalty += any_blanks ? - RELATIVE_DEDENT_WITH_BLANK_PENALTY : - RELATIVE_DEDENT_PENALTY; - } - } -} - -static int score_cmp(struct split_score *s1, struct split_score *s2) -{ - /* -1 if s1.effective_indent < s2->effective_indent, etc. */ - int cmp_indents = ((s1->effective_indent > s2->effective_indent) - - (s1->effective_indent < s2->effective_indent)); - - return INDENT_WEIGHT * cmp_indents + (s1->penalty - s2->penalty); -} - -/* - * Represent a group of changed lines in an xdfile_t (i.e., a contiguous group - * of lines that was inserted or deleted from the corresponding version of the - * file). We consider there to be such a group at the beginning of the file, at - * the end of the file, and between any two unchanged lines, though most such - * groups will usually be empty. - * - * If the first line in a group is equal to the line following the group, then - * the group can be slid down. Similarly, if the last line in a group is equal - * to the line preceding the group, then the group can be slid up. See - * group_slide_down() and group_slide_up(). - * - * Note that loops that are testing for changed lines in xdf->rchg do not need - * index bounding since the array is prepared with a zero at position -1 and N. - */ -struct xdlgroup { - /* - * The index of the first changed line in the group, or the index of - * the unchanged line above which the (empty) group is located. - */ - long start; - - /* - * The index of the first unchanged line after the group. For an empty - * group, end is equal to start. - */ - long end; -}; - -/* - * Initialize g to point at the first group in xdf. - */ -static void group_init(xdfile_t *xdf, struct xdlgroup *g) -{ - g->start = g->end = 0; - while (xdf->rchg[g->end]) - g->end++; -} - -/* - * Move g to describe the next (possibly empty) group in xdf and return 0. If g - * is already at the end of the file, do nothing and return -1. - */ -static inline int group_next(xdfile_t *xdf, struct xdlgroup *g) -{ - if (g->end == xdf->nrec) - return -1; - - g->start = g->end + 1; - for (g->end = g->start; xdf->rchg[g->end]; g->end++) - ; - - return 0; -} - -/* - * Move g to describe the previous (possibly empty) group in xdf and return 0. - * If g is already at the beginning of the file, do nothing and return -1. - */ -static inline int group_previous(xdfile_t *xdf, struct xdlgroup *g) -{ - if (g->start == 0) - return -1; - - g->end = g->start - 1; - for (g->start = g->end; xdf->rchg[g->start - 1]; g->start--) - ; - - return 0; -} - -/* - * If g can be slid toward the end of the file, do so, and if it bumps into a - * following group, expand this group to include it. Return 0 on success or -1 - * if g cannot be slid down. - */ -static int group_slide_down(xdfile_t *xdf, struct xdlgroup *g) -{ - if (g->end < xdf->nrec && - recs_match(xdf->recs[g->start], xdf->recs[g->end])) { - xdf->rchg[g->start++] = 0; - xdf->rchg[g->end++] = 1; - - while (xdf->rchg[g->end]) - g->end++; - - return 0; - } else { - return -1; - } -} - -/* - * If g can be slid toward the beginning of the file, do so, and if it bumps - * into a previous group, expand this group to include it. Return 0 on success - * or -1 if g cannot be slid up. - */ -static int group_slide_up(xdfile_t *xdf, struct xdlgroup *g) -{ - if (g->start > 0 && - recs_match(xdf->recs[g->start - 1], xdf->recs[g->end - 1])) { - xdf->rchg[--g->start] = 1; - xdf->rchg[--g->end] = 0; - - while (xdf->rchg[g->start - 1]) - g->start--; - - return 0; - } else { - return -1; - } -} - -/* - * Move back and forward change groups for a consistent and pretty diff output. - * This also helps in finding joinable change groups and reducing the diff - * size. - */ -int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags) { - struct xdlgroup g, go; - long earliest_end, end_matching_other; - long groupsize; - - group_init(xdf, &g); - group_init(xdfo, &go); - - while (1) { - /* - * If the group is empty in the to-be-compacted file, skip it: - */ - if (g.end == g.start) - goto next; - - /* - * Now shift the change up and then down as far as possible in - * each direction. If it bumps into any other changes, merge - * them. - */ - do { - groupsize = g.end - g.start; - - /* - * Keep track of the last "end" index that causes this - * group to align with a group of changed lines in the - * other file. -1 indicates that we haven't found such - * a match yet: - */ - end_matching_other = -1; - - /* Shift the group backward as much as possible: */ - while (!group_slide_up(xdf, &g)) - if (group_previous(xdfo, &go)) - XDL_BUG("group sync broken sliding up"); - - /* - * This is this highest that this group can be shifted. - * Record its end index: - */ - earliest_end = g.end; - - if (go.end > go.start) - end_matching_other = g.end; - - /* Now shift the group forward as far as possible: */ - while (1) { - if (group_slide_down(xdf, &g)) - break; - if (group_next(xdfo, &go)) - XDL_BUG("group sync broken sliding down"); - - if (go.end > go.start) - end_matching_other = g.end; - } - } while (groupsize != g.end - g.start); - - /* - * If the group can be shifted, then we can possibly use this - * freedom to produce a more intuitive diff. - * - * The group is currently shifted as far down as possible, so - * the heuristics below only have to handle upwards shifts. - */ - - if (g.end == earliest_end) { - /* no shifting was possible */ - } else if (end_matching_other != -1) { - /* - * Move the possibly merged group of changes back to - * line up with the last group of changes from the - * other file that it can align with. - */ - while (go.end == go.start) { - if (group_slide_up(xdf, &g)) - XDL_BUG("match disappeared"); - if (group_previous(xdfo, &go)) - XDL_BUG("group sync broken sliding to match"); - } - } else if (flags & XDF_INDENT_HEURISTIC) { - /* - * Indent heuristic: a group of pure add/delete lines - * implies two splits, one between the end of the - * "before" context and the start of the group, and - * another between the end of the group and the - * beginning of the "after" context. Some splits are - * aesthetically better and some are worse. We compute - * a badness "score" for each split, and add the scores - * for the two splits to define a "score" for each - * position that the group can be shifted to. Then we - * pick the shift with the lowest score. - */ - long shift, best_shift = -1; - struct split_score best_score; - - shift = earliest_end; - if (g.end - groupsize - 1 > shift) - shift = g.end - groupsize - 1; - if (g.end - INDENT_HEURISTIC_MAX_SLIDING > shift) - shift = g.end - INDENT_HEURISTIC_MAX_SLIDING; - for (; shift <= g.end; shift++) { - struct split_measurement m; - struct split_score score = {0, 0}; - - measure_split(xdf, shift, &m); - score_add_split(&m, &score); - measure_split(xdf, shift - groupsize, &m); - score_add_split(&m, &score); - if (best_shift == -1 || - score_cmp(&score, &best_score) <= 0) { - best_score.effective_indent = score.effective_indent; - best_score.penalty = score.penalty; - best_shift = shift; - } - } - - while (g.end > best_shift) { - if (group_slide_up(xdf, &g)) - XDL_BUG("best shift unreached"); - if (group_previous(xdfo, &go)) - XDL_BUG("group sync broken sliding to blank line"); - } - } - - next: - /* Move past the just-processed group: */ - if (group_next(xdf, &g)) - break; - if (group_next(xdfo, &go)) - XDL_BUG("group sync broken moving to next group"); - } - - if (!group_next(xdfo, &go)) - XDL_BUG("group sync broken at end of file"); - - return 0; -} - - -int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr) { - xdchange_t *cscr = NULL, *xch; - char *rchg1 = xe->xdf1.rchg, *rchg2 = xe->xdf2.rchg; - long i1, i2, l1, l2; - - /* - * Trivial. Collects "groups" of changes and creates an edit script. - */ - for (i1 = xe->xdf1.nrec, i2 = xe->xdf2.nrec; i1 >= 0 || i2 >= 0; i1--, i2--) - if (rchg1[i1 - 1] || rchg2[i2 - 1]) { - for (l1 = i1; rchg1[i1 - 1]; i1--); - for (l2 = i2; rchg2[i2 - 1]; i2--); - - if (!(xch = xdl_add_change(cscr, i1, i2, l1 - i1, l2 - i2))) { - xdl_free_script(cscr); - return -1; - } - cscr = xch; - } - - *xscr = cscr; - - return 0; -} - - -void xdl_free_script(xdchange_t *xscr) { - xdchange_t *xch; - - while ((xch = xscr) != NULL) { - xscr = xscr->next; - xdl_free(xch); - } -} - -static int xdl_call_hunk_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg) -{ - xdchange_t *xch, *xche; - - for (xch = xscr; xch; xch = xche->next) { - xche = xdl_get_hunk(&xch, xecfg); - if (!xch) - break; - if (xecfg->hunk_func(xch->i1, xche->i1 + xche->chg1 - xch->i1, - xch->i2, xche->i2 + xche->chg2 - xch->i2, - ecb->priv) < 0) - return -1; - } - return 0; -} - -static void xdl_mark_ignorable_lines(xdchange_t *xscr, xdfenv_t *xe, long flags) -{ - xdchange_t *xch; - - for (xch = xscr; xch; xch = xch->next) { - int ignore = 1; - xrecord_t **rec; - long i; - - rec = &xe->xdf1.recs[xch->i1]; - for (i = 0; i < xch->chg1 && ignore; i++) - ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); - - rec = &xe->xdf2.recs[xch->i2]; - for (i = 0; i < xch->chg2 && ignore; i++) - ignore = xdl_blankline(rec[i]->ptr, rec[i]->size, flags); - - xch->ignore = ignore; - } -} - -static int record_matches_regex(xrecord_t *rec, xpparam_t const *xpp) { - xdl_regmatch_t regmatch; - int i; - - for (i = 0; i < xpp->ignore_regex_nr; i++) - if (!xdl_regexec_buf(xpp->ignore_regex[i], rec->ptr, rec->size, 1, - ®match, 0)) - return 1; - - return 0; -} - -static void xdl_mark_ignorable_regex(xdchange_t *xscr, const xdfenv_t *xe, - xpparam_t const *xpp) -{ - xdchange_t *xch; - - for (xch = xscr; xch; xch = xch->next) { - xrecord_t **rec; - int ignore = 1; - long i; - - /* - * Do not override --ignore-blank-lines. - */ - if (xch->ignore) - continue; - - rec = &xe->xdf1.recs[xch->i1]; - for (i = 0; i < xch->chg1 && ignore; i++) - ignore = record_matches_regex(rec[i], xpp); - - rec = &xe->xdf2.recs[xch->i2]; - for (i = 0; i < xch->chg2 && ignore; i++) - ignore = record_matches_regex(rec[i], xpp); - - xch->ignore = ignore; - } -} - -int xdl_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdemitconf_t const *xecfg, xdemitcb_t *ecb) { - xdchange_t *xscr; - xdfenv_t xe; - emit_func_t ef = xecfg->hunk_func ? xdl_call_hunk_func : xdl_emit_diff; - - if (xdl_do_diff(mf1, mf2, xpp, &xe) < 0) { - - return -1; - } - if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe, &xscr) < 0) { - - xdl_free_env(&xe); - return -1; - } - if (xscr) { - if (xpp->flags & XDF_IGNORE_BLANK_LINES) - xdl_mark_ignorable_lines(xscr, &xe, xpp->flags); - - if (xpp->ignore_regex) - xdl_mark_ignorable_regex(xscr, &xe, xpp); - - if (ef(&xe, xscr, ecb, xecfg) < 0) { - - xdl_free_script(xscr); - xdl_free_env(&xe); - return -1; - } - xdl_free_script(xscr); - } - xdl_free_env(&xe); - - return 0; -} diff --git a/src/libgit2/xdiff/xdiffi.h b/src/libgit2/xdiff/xdiffi.h deleted file mode 100644 index 8f1c7c8b0..000000000 --- a/src/libgit2/xdiff/xdiffi.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XDIFFI_H) -#define XDIFFI_H - - -typedef struct s_diffdata { - long nrec; - unsigned long const *ha; - long *rindex; - char *rchg; -} diffdata_t; - -typedef struct s_xdalgoenv { - long mxcost; - long snake_cnt; - long heur_min; -} xdalgoenv_t; - -typedef struct s_xdchange { - struct s_xdchange *next; - long i1, i2; - long chg1, chg2; - int ignore; -} xdchange_t; - - - -int xdl_recs_cmp(diffdata_t *dd1, long off1, long lim1, - diffdata_t *dd2, long off2, long lim2, - long *kvdf, long *kvdb, int need_min, xdalgoenv_t *xenv); -int xdl_do_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *xe); -int xdl_change_compact(xdfile_t *xdf, xdfile_t *xdfo, long flags); -int xdl_build_script(xdfenv_t *xe, xdchange_t **xscr); -void xdl_free_script(xdchange_t *xscr); -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); -int xdl_do_patience_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *env); -int xdl_do_histogram_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *env); - -#endif /* #if !defined(XDIFFI_H) */ diff --git a/src/libgit2/xdiff/xemit.c b/src/libgit2/xdiff/xemit.c deleted file mode 100644 index 1cbf2b982..000000000 --- a/src/libgit2/xdiff/xemit.c +++ /dev/null @@ -1,330 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - -static long xdl_get_rec(xdfile_t *xdf, long ri, char const **rec) { - - *rec = xdf->recs[ri]->ptr; - - return xdf->recs[ri]->size; -} - - -static int xdl_emit_record(xdfile_t *xdf, long ri, char const *pre, xdemitcb_t *ecb) { - long size, psize = strlen(pre); - char const *rec; - - size = xdl_get_rec(xdf, ri, &rec); - if (xdl_emit_diffrec(rec, size, pre, psize, ecb) < 0) { - - return -1; - } - - return 0; -} - - -/* - * Starting at the passed change atom, find the latest change atom to be included - * inside the differential hunk according to the specified configuration. - * Also advance xscr if the first changes must be discarded. - */ -xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg) -{ - xdchange_t *xch, *xchp, *lxch; - long max_common = 2 * xecfg->ctxlen + xecfg->interhunkctxlen; - long max_ignorable = xecfg->ctxlen; - unsigned long ignored = 0; /* number of ignored blank lines */ - - /* remove ignorable changes that are too far before other changes */ - for (xchp = *xscr; xchp && xchp->ignore; xchp = xchp->next) { - xch = xchp->next; - - if (xch == NULL || - xch->i1 - (xchp->i1 + xchp->chg1) >= max_ignorable) - *xscr = xch; - } - - if (*xscr == NULL) - return NULL; - - lxch = *xscr; - - for (xchp = *xscr, xch = xchp->next; xch; xchp = xch, xch = xch->next) { - long distance = xch->i1 - (xchp->i1 + xchp->chg1); - if (distance > max_common) - break; - - if (distance < max_ignorable && (!xch->ignore || lxch == xchp)) { - lxch = xch; - ignored = 0; - } else if (distance < max_ignorable && xch->ignore) { - ignored += xch->chg2; - } else if (lxch != xchp && - xch->i1 + ignored - (lxch->i1 + lxch->chg1) > max_common) { - break; - } else if (!xch->ignore) { - lxch = xch; - ignored = 0; - } else { - ignored += xch->chg2; - } - } - - return lxch; -} - - -static long def_ff(const char *rec, long len, char *buf, long sz, void *priv) -{ - if (len > 0 && - (isalpha((unsigned char)*rec) || /* identifier? */ - *rec == '_' || /* also identifier? */ - *rec == '$')) { /* identifiers from VMS and other esoterico */ - if (len > sz) - len = sz; - while (0 < len && isspace((unsigned char)rec[len - 1])) - len--; - memcpy(buf, rec, len); - return len; - } - return -1; -} - -static long match_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri, - char *buf, long sz) -{ - const char *rec; - long len = xdl_get_rec(xdf, ri, &rec); - if (!xecfg->find_func) - return def_ff(rec, len, buf, sz, xecfg->find_func_priv); - return xecfg->find_func(rec, len, buf, sz, xecfg->find_func_priv); -} - -static int is_func_rec(xdfile_t *xdf, xdemitconf_t const *xecfg, long ri) -{ - char dummy[1]; - return match_func_rec(xdf, xecfg, ri, dummy, sizeof(dummy)) >= 0; -} - -struct func_line { - long len; - char buf[80]; -}; - -static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg, - struct func_line *func_line, long start, long limit) -{ - long l, size, step = (start > limit) ? -1 : 1; - char *buf, dummy[1]; - - buf = func_line ? func_line->buf : dummy; - size = func_line ? sizeof(func_line->buf) : sizeof(dummy); - - for (l = start; l != limit && 0 <= l && l < xe->xdf1.nrec; l += step) { - long len = match_func_rec(&xe->xdf1, xecfg, l, buf, size); - if (len >= 0) { - if (func_line) - func_line->len = len; - return l; - } - } - return -1; -} - -static int is_empty_rec(xdfile_t *xdf, long ri) -{ - const char *rec; - long len = xdl_get_rec(xdf, ri, &rec); - - while (len > 0 && XDL_ISSPACE(*rec)) { - rec++; - len--; - } - return !len; -} - -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg) { - long s1, s2, e1, e2, lctx; - xdchange_t *xch, *xche; - long funclineprev = -1; - struct func_line func_line = { 0 }; - - for (xch = xscr; xch; xch = xche->next) { - xdchange_t *xchp = xch; - xche = xdl_get_hunk(&xch, xecfg); - if (!xch) - break; - -pre_context_calculation: - s1 = XDL_MAX(xch->i1 - xecfg->ctxlen, 0); - s2 = XDL_MAX(xch->i2 - xecfg->ctxlen, 0); - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fs1, i1 = xch->i1; - - /* Appended chunk? */ - if (i1 >= xe->xdf1.nrec) { - long i2 = xch->i2; - - /* - * We don't need additional context if - * a whole function was added. - */ - while (i2 < xe->xdf2.nrec) { - if (is_func_rec(&xe->xdf2, xecfg, i2)) - goto post_context_calculation; - i2++; - } - - /* - * Otherwise get more context from the - * pre-image. - */ - i1 = xe->xdf1.nrec - 1; - } - - fs1 = get_func_line(xe, xecfg, NULL, i1, -1); - while (fs1 > 0 && !is_empty_rec(&xe->xdf1, fs1 - 1) && - !is_func_rec(&xe->xdf1, xecfg, fs1 - 1)) - fs1--; - if (fs1 < 0) - fs1 = 0; - if (fs1 < s1) { - s2 = XDL_MAX(s2 - (s1 - fs1), 0); - s1 = fs1; - - /* - * Did we extend context upwards into an - * ignored change? - */ - while (xchp != xch && - xchp->i1 + xchp->chg1 <= s1 && - xchp->i2 + xchp->chg2 <= s2) - xchp = xchp->next; - - /* If so, show it after all. */ - if (xchp != xch) { - xch = xchp; - goto pre_context_calculation; - } - } - } - - post_context_calculation: - lctx = xecfg->ctxlen; - lctx = XDL_MIN(lctx, xe->xdf1.nrec - (xche->i1 + xche->chg1)); - lctx = XDL_MIN(lctx, xe->xdf2.nrec - (xche->i2 + xche->chg2)); - - e1 = xche->i1 + xche->chg1 + lctx; - e2 = xche->i2 + xche->chg2 + lctx; - - if (xecfg->flags & XDL_EMIT_FUNCCONTEXT) { - long fe1 = get_func_line(xe, xecfg, NULL, - xche->i1 + xche->chg1, - xe->xdf1.nrec); - while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1)) - fe1--; - if (fe1 < 0) - fe1 = xe->xdf1.nrec; - if (fe1 > e1) { - e2 = XDL_MIN(e2 + (fe1 - e1), xe->xdf2.nrec); - e1 = fe1; - } - - /* - * Overlap with next change? Then include it - * in the current hunk and start over to find - * its new end. - */ - if (xche->next) { - long l = XDL_MIN(xche->next->i1, - xe->xdf1.nrec - 1); - if (l - xecfg->ctxlen <= e1 || - get_func_line(xe, xecfg, NULL, l, e1) < 0) { - xche = xche->next; - goto post_context_calculation; - } - } - } - - /* - * Emit current hunk header. - */ - - if (xecfg->flags & XDL_EMIT_FUNCNAMES) { - get_func_line(xe, xecfg, &func_line, - s1 - 1, funclineprev); - funclineprev = s1 - 1; - } - if (!(xecfg->flags & XDL_EMIT_NO_HUNK_HDR) && - xdl_emit_hunk_hdr(s1 + 1, e1 - s1, s2 + 1, e2 - s2, - func_line.buf, func_line.len, ecb) < 0) - return -1; - - /* - * Emit pre-context. - */ - for (; s2 < xch->i2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - for (s1 = xch->i1, s2 = xch->i2;; xch = xch->next) { - /* - * Merge previous with current change atom. - */ - for (; s1 < xch->i1 && s2 < xch->i2; s1++, s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - - /* - * Removes lines from the first file. - */ - for (s1 = xch->i1; s1 < xch->i1 + xch->chg1; s1++) - if (xdl_emit_record(&xe->xdf1, s1, "-", ecb) < 0) - return -1; - - /* - * Adds lines from the second file. - */ - for (s2 = xch->i2; s2 < xch->i2 + xch->chg2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, "+", ecb) < 0) - return -1; - - if (xch == xche) - break; - s1 = xch->i1 + xch->chg1; - s2 = xch->i2 + xch->chg2; - } - - /* - * Emit post-context. - */ - for (s2 = xche->i2 + xche->chg2; s2 < e2; s2++) - if (xdl_emit_record(&xe->xdf2, s2, " ", ecb) < 0) - return -1; - } - - return 0; -} diff --git a/src/libgit2/xdiff/xemit.h b/src/libgit2/xdiff/xemit.h deleted file mode 100644 index 1b9887e67..000000000 --- a/src/libgit2/xdiff/xemit.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XEMIT_H) -#define XEMIT_H - - -typedef int (*emit_func_t)(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); - -xdchange_t *xdl_get_hunk(xdchange_t **xscr, xdemitconf_t const *xecfg); -int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, - xdemitconf_t const *xecfg); - - - -#endif /* #if !defined(XEMIT_H) */ diff --git a/src/libgit2/xdiff/xhistogram.c b/src/libgit2/xdiff/xhistogram.c deleted file mode 100644 index 80794748b..000000000 --- a/src/libgit2/xdiff/xhistogram.c +++ /dev/null @@ -1,381 +0,0 @@ -/* - * Copyright (C) 2010, Google Inc. - * and other copyright owners as documented in JGit's IP log. - * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php - * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "xinclude.h" - -#define MAX_PTR UINT_MAX -#define MAX_CNT UINT_MAX - -#define LINE_END(n) (line##n + count##n - 1) -#define LINE_END_PTR(n) (*line##n + *count##n - 1) - -struct histindex { - struct record { - unsigned int ptr, cnt; - struct record *next; - } **records, /* an occurrence */ - **line_map; /* map of line to record chain */ - chastore_t rcha; - unsigned int *next_ptrs; - unsigned int table_bits, - records_size, - line_map_size; - - unsigned int max_chain_length, - key_shift, - ptr_shift; - - unsigned int cnt, - has_common; - - xdfenv_t *env; - xpparam_t const *xpp; -}; - -struct region { - unsigned int begin1, end1; - unsigned int begin2, end2; -}; - -#define LINE_MAP(i, a) (i->line_map[(a) - i->ptr_shift]) - -#define NEXT_PTR(index, ptr) \ - (index->next_ptrs[(ptr) - index->ptr_shift]) - -#define CNT(index, ptr) \ - ((LINE_MAP(index, ptr))->cnt) - -#define REC(env, s, l) \ - (env->xdf##s.recs[l - 1]) - -static int cmp_recs(xrecord_t *r1, xrecord_t *r2) -{ - return r1->ha == r2->ha; - -} - -#define CMP(i, s1, l1, s2, l2) \ - (cmp_recs(REC(i->env, s1, l1), REC(i->env, s2, l2))) - -#define TABLE_HASH(index, side, line) \ - XDL_HASHLONG((REC(index->env, side, line))->ha, index->table_bits) - -static int scanA(struct histindex *index, int line1, int count1) -{ - unsigned int ptr, tbl_idx; - unsigned int chain_len; - struct record **rec_chain, *rec; - - for (ptr = LINE_END(1); line1 <= ptr; ptr--) { - tbl_idx = TABLE_HASH(index, 1, ptr); - rec_chain = index->records + tbl_idx; - rec = *rec_chain; - - chain_len = 0; - while (rec) { - if (CMP(index, 1, rec->ptr, 1, ptr)) { - /* - * ptr is identical to another element. Insert - * it onto the front of the existing element - * chain. - */ - NEXT_PTR(index, ptr) = rec->ptr; - rec->ptr = ptr; - /* cap rec->cnt at MAX_CNT */ - rec->cnt = XDL_MIN(MAX_CNT, rec->cnt + 1); - LINE_MAP(index, ptr) = rec; - goto continue_scan; - } - - rec = rec->next; - chain_len++; - } - - if (chain_len == index->max_chain_length) - return -1; - - /* - * This is the first time we have ever seen this particular - * element in the sequence. Construct a new chain for it. - */ - if (!(rec = xdl_cha_alloc(&index->rcha))) - return -1; - rec->ptr = ptr; - rec->cnt = 1; - rec->next = *rec_chain; - *rec_chain = rec; - LINE_MAP(index, ptr) = rec; - -continue_scan: - ; /* no op */ - } - - return 0; -} - -static int try_lcs(struct histindex *index, struct region *lcs, int b_ptr, - int line1, int count1, int line2, int count2) -{ - unsigned int b_next = b_ptr + 1; - struct record *rec = index->records[TABLE_HASH(index, 2, b_ptr)]; - unsigned int as, ae, bs, be, np, rc; - int should_break; - - for (; rec; rec = rec->next) { - if (rec->cnt > index->cnt) { - if (!index->has_common) - index->has_common = CMP(index, 1, rec->ptr, 2, b_ptr); - continue; - } - - as = rec->ptr; - if (!CMP(index, 1, as, 2, b_ptr)) - continue; - - index->has_common = 1; - for (;;) { - should_break = 0; - np = NEXT_PTR(index, as); - bs = b_ptr; - ae = as; - be = bs; - rc = rec->cnt; - - while (line1 < as && line2 < bs - && CMP(index, 1, as - 1, 2, bs - 1)) { - as--; - bs--; - if (1 < rc) - rc = XDL_MIN(rc, CNT(index, as)); - } - while (ae < LINE_END(1) && be < LINE_END(2) - && CMP(index, 1, ae + 1, 2, be + 1)) { - ae++; - be++; - if (1 < rc) - rc = XDL_MIN(rc, CNT(index, ae)); - } - - if (b_next <= be) - b_next = be + 1; - if (lcs->end1 - lcs->begin1 < ae - as || rc < index->cnt) { - lcs->begin1 = as; - lcs->begin2 = bs; - lcs->end1 = ae; - lcs->end2 = be; - index->cnt = rc; - } - - if (np == 0) - break; - - while (np <= ae) { - np = NEXT_PTR(index, np); - if (np == 0) { - should_break = 1; - break; - } - } - - if (should_break) - break; - - as = np; - } - } - return b_next; -} - -static int fall_back_to_classic_diff(xpparam_t const *xpp, xdfenv_t *env, - int line1, int count1, int line2, int count2) -{ - xpparam_t xpparam; - - memset(&xpparam, 0, sizeof(xpparam)); - xpparam.flags = xpp->flags & ~XDF_DIFF_ALGORITHM_MASK; - - return xdl_fall_back_diff(env, &xpparam, - line1, count1, line2, count2); -} - -static inline void free_index(struct histindex *index) -{ - xdl_free(index->records); - xdl_free(index->line_map); - xdl_free(index->next_ptrs); - xdl_cha_free(&index->rcha); -} - -static int find_lcs(xpparam_t const *xpp, xdfenv_t *env, - struct region *lcs, - int line1, int count1, int line2, int count2) -{ - int b_ptr; - int sz, ret = -1; - struct histindex index; - - memset(&index, 0, sizeof(index)); - - index.env = env; - index.xpp = xpp; - - index.records = NULL; - index.line_map = NULL; - /* in case of early xdl_cha_free() */ - index.rcha.head = NULL; - - index.table_bits = xdl_hashbits(count1); - sz = index.records_size = 1 << index.table_bits; - sz *= sizeof(struct record *); - if (!(index.records = (struct record **) xdl_malloc(sz))) - goto cleanup; - memset(index.records, 0, sz); - - sz = index.line_map_size = count1; - sz *= sizeof(struct record *); - if (!(index.line_map = (struct record **) xdl_malloc(sz))) - goto cleanup; - memset(index.line_map, 0, sz); - - sz = index.line_map_size; - sz *= sizeof(unsigned int); - if (!(index.next_ptrs = (unsigned int *) xdl_malloc(sz))) - goto cleanup; - memset(index.next_ptrs, 0, sz); - - /* lines / 4 + 1 comes from xprepare.c:xdl_prepare_ctx() */ - if (xdl_cha_init(&index.rcha, sizeof(struct record), count1 / 4 + 1) < 0) - goto cleanup; - - index.ptr_shift = line1; - index.max_chain_length = 64; - - if (scanA(&index, line1, count1)) - goto cleanup; - - index.cnt = index.max_chain_length + 1; - - for (b_ptr = line2; b_ptr <= LINE_END(2); ) - b_ptr = try_lcs(&index, lcs, b_ptr, line1, count1, line2, count2); - - if (index.has_common && index.max_chain_length < index.cnt) - ret = 1; - else - ret = 0; - -cleanup: - free_index(&index); - return ret; -} - -static int histogram_diff(xpparam_t const *xpp, xdfenv_t *env, - int line1, int count1, int line2, int count2) -{ - struct region lcs; - int lcs_found; - int result; -redo: - result = -1; - - if (count1 <= 0 && count2 <= 0) - return 0; - - if (LINE_END(1) >= MAX_PTR) - return -1; - - if (!count1) { - while(count2--) - env->xdf2.rchg[line2++ - 1] = 1; - return 0; - } else if (!count2) { - while(count1--) - env->xdf1.rchg[line1++ - 1] = 1; - return 0; - } - - memset(&lcs, 0, sizeof(lcs)); - lcs_found = find_lcs(xpp, env, &lcs, line1, count1, line2, count2); - if (lcs_found < 0) - goto out; - else if (lcs_found) - result = fall_back_to_classic_diff(xpp, env, line1, count1, line2, count2); - else { - if (lcs.begin1 == 0 && lcs.begin2 == 0) { - while (count1--) - env->xdf1.rchg[line1++ - 1] = 1; - while (count2--) - env->xdf2.rchg[line2++ - 1] = 1; - result = 0; - } else { - result = histogram_diff(xpp, env, - line1, lcs.begin1 - line1, - line2, lcs.begin2 - line2); - if (result) - goto out; - /* - * result = histogram_diff(xpp, env, - * lcs.end1 + 1, LINE_END(1) - lcs.end1, - * lcs.end2 + 1, LINE_END(2) - lcs.end2); - * but let's optimize tail recursion ourself: - */ - count1 = LINE_END(1) - lcs.end1; - line1 = lcs.end1 + 1; - count2 = LINE_END(2) - lcs.end2; - line2 = lcs.end2 + 1; - goto redo; - } - } -out: - return result; -} - -int xdl_do_histogram_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env) -{ - if (xdl_prepare_env(file1, file2, xpp, env) < 0) - return -1; - - return histogram_diff(xpp, env, - env->xdf1.dstart + 1, env->xdf1.dend - env->xdf1.dstart + 1, - env->xdf2.dstart + 1, env->xdf2.dend - env->xdf2.dstart + 1); -} diff --git a/src/libgit2/xdiff/xinclude.h b/src/libgit2/xdiff/xinclude.h deleted file mode 100644 index 75db1d8f3..000000000 --- a/src/libgit2/xdiff/xinclude.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XINCLUDE_H) -#define XINCLUDE_H - -#include "git-xdiff.h" -#include "xmacros.h" -#include "xdiff.h" -#include "xtypes.h" -#include "xutils.h" -#include "xprepare.h" -#include "xdiffi.h" -#include "xemit.h" - - -#endif /* #if !defined(XINCLUDE_H) */ diff --git a/src/libgit2/xdiff/xmacros.h b/src/libgit2/xdiff/xmacros.h deleted file mode 100644 index 2809a28ca..000000000 --- a/src/libgit2/xdiff/xmacros.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XMACROS_H) -#define XMACROS_H - - - - -#define XDL_MIN(a, b) ((a) < (b) ? (a): (b)) -#define XDL_MAX(a, b) ((a) > (b) ? (a): (b)) -#define XDL_ABS(v) ((v) >= 0 ? (v): -(v)) -#define XDL_ISDIGIT(c) ((c) >= '0' && (c) <= '9') -#define XDL_ISSPACE(c) (isspace((unsigned char)(c))) -#define XDL_ADDBITS(v,b) ((v) + ((v) >> (b))) -#define XDL_MASKBITS(b) ((1UL << (b)) - 1) -#define XDL_HASHLONG(v,b) (XDL_ADDBITS((unsigned long)(v), b) & XDL_MASKBITS(b)) -#define XDL_PTRFREE(p) do { if (p) { xdl_free(p); (p) = NULL; } } while (0) -#define XDL_LE32_PUT(p, v) \ -do { \ - unsigned char *__p = (unsigned char *) (p); \ - *__p++ = (unsigned char) (v); \ - *__p++ = (unsigned char) ((v) >> 8); \ - *__p++ = (unsigned char) ((v) >> 16); \ - *__p = (unsigned char) ((v) >> 24); \ -} while (0) -#define XDL_LE32_GET(p, v) \ -do { \ - unsigned char const *__p = (unsigned char const *) (p); \ - (v) = (unsigned long) __p[0] | ((unsigned long) __p[1]) << 8 | \ - ((unsigned long) __p[2]) << 16 | ((unsigned long) __p[3]) << 24; \ -} while (0) - - -#endif /* #if !defined(XMACROS_H) */ diff --git a/src/libgit2/xdiff/xmerge.c b/src/libgit2/xdiff/xmerge.c deleted file mode 100644 index 433e2d741..000000000 --- a/src/libgit2/xdiff/xmerge.c +++ /dev/null @@ -1,737 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003-2006 Davide Libenzi, Johannes E. Schindelin - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - -typedef struct s_xdmerge { - struct s_xdmerge *next; - /* - * 0 = conflict, - * 1 = no conflict, take first, - * 2 = no conflict, take second. - * 3 = no conflict, take both. - */ - int mode; - /* - * These point at the respective postimages. E.g. <i1,chg1> is - * how side #1 wants to change the common ancestor; if there is no - * overlap, lines before i1 in the postimage of side #1 appear - * in the merge result as a region touched by neither side. - */ - long i1, i2; - long chg1, chg2; - /* - * These point at the preimage; of course there is just one - * preimage, that is from the shared common ancestor. - */ - long i0; - long chg0; -} xdmerge_t; - -static int xdl_append_merge(xdmerge_t **merge, int mode, - long i0, long chg0, - long i1, long chg1, - long i2, long chg2) -{ - xdmerge_t *m = *merge; - if (m && (i1 <= m->i1 + m->chg1 || i2 <= m->i2 + m->chg2)) { - if (mode != m->mode) - m->mode = 0; - m->chg0 = i0 + chg0 - m->i0; - m->chg1 = i1 + chg1 - m->i1; - m->chg2 = i2 + chg2 - m->i2; - } else { - m = xdl_malloc(sizeof(xdmerge_t)); - if (!m) - return -1; - m->next = NULL; - m->mode = mode; - m->i0 = i0; - m->chg0 = chg0; - m->i1 = i1; - m->chg1 = chg1; - m->i2 = i2; - m->chg2 = chg2; - if (*merge) - (*merge)->next = m; - *merge = m; - } - return 0; -} - -static int xdl_cleanup_merge(xdmerge_t *c) -{ - int count = 0; - xdmerge_t *next_c; - - /* were there conflicts? */ - for (; c; c = next_c) { - if (c->mode == 0) - count++; - next_c = c->next; - xdl_free(c); - } - return count; -} - -static int xdl_merge_cmp_lines(xdfenv_t *xe1, int i1, xdfenv_t *xe2, int i2, - int line_count, long flags) -{ - int i; - xrecord_t **rec1 = xe1->xdf2.recs + i1; - xrecord_t **rec2 = xe2->xdf2.recs + i2; - - for (i = 0; i < line_count; i++) { - int result = xdl_recmatch(rec1[i]->ptr, rec1[i]->size, - rec2[i]->ptr, rec2[i]->size, flags); - if (!result) - return -1; - } - return 0; -} - -static int xdl_recs_copy_0(int use_orig, xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) -{ - xrecord_t **recs; - int size = 0; - - recs = (use_orig ? xe->xdf1.recs : xe->xdf2.recs) + i; - - if (count < 1) - return 0; - - for (i = 0; i < count; size += recs[i++]->size) - if (dest) - memcpy(dest + size, recs[i]->ptr, recs[i]->size); - if (add_nl) { - i = recs[count - 1]->size; - if (i == 0 || recs[count - 1]->ptr[i - 1] != '\n') { - if (needs_cr) { - if (dest) - dest[size] = '\r'; - size++; - } - - if (dest) - dest[size] = '\n'; - size++; - } - } - return size; -} - -static int xdl_recs_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) -{ - return xdl_recs_copy_0(0, xe, i, count, needs_cr, add_nl, dest); -} - -static int xdl_orig_copy(xdfenv_t *xe, int i, int count, int needs_cr, int add_nl, char *dest) -{ - return xdl_recs_copy_0(1, xe, i, count, needs_cr, add_nl, dest); -} - -/* - * Returns 1 if the i'th line ends in CR/LF (if it is the last line and - * has no eol, the preceding line, if any), 0 if it ends in LF-only, and - * -1 if the line ending cannot be determined. - */ -static int is_eol_crlf(xdfile_t *file, int i) -{ - long size; - - if (i < file->nrec - 1) - /* All lines before the last *must* end in LF */ - return (size = file->recs[i]->size) > 1 && - file->recs[i]->ptr[size - 2] == '\r'; - if (!file->nrec) - /* Cannot determine eol style from empty file */ - return -1; - if ((size = file->recs[i]->size) && - file->recs[i]->ptr[size - 1] == '\n') - /* Last line; ends in LF; Is it CR/LF? */ - return size > 1 && - file->recs[i]->ptr[size - 2] == '\r'; - if (!i) - /* The only line has no eol */ - return -1; - /* Determine eol from second-to-last line */ - return (size = file->recs[i - 1]->size) > 1 && - file->recs[i - 1]->ptr[size - 2] == '\r'; -} - -static int is_cr_needed(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m) -{ - int needs_cr; - - /* Match post-images' preceding, or first, lines' end-of-line style */ - needs_cr = is_eol_crlf(&xe1->xdf2, m->i1 ? m->i1 - 1 : 0); - if (needs_cr) - needs_cr = is_eol_crlf(&xe2->xdf2, m->i2 ? m->i2 - 1 : 0); - /* Look at pre-image's first line, unless we already settled on LF */ - if (needs_cr) - needs_cr = is_eol_crlf(&xe1->xdf1, 0); - /* If still undecided, use LF-only */ - return needs_cr < 0 ? 0 : needs_cr; -} - -static int fill_conflict_hunk(xdfenv_t *xe1, const char *name1, - xdfenv_t *xe2, const char *name2, - const char *name3, - int size, int i, int style, - xdmerge_t *m, char *dest, int marker_size) -{ - int marker1_size = (name1 ? strlen(name1) + 1 : 0); - int marker2_size = (name2 ? strlen(name2) + 1 : 0); - int marker3_size = (name3 ? strlen(name3) + 1 : 0); - int needs_cr = is_cr_needed(xe1, xe2, m); - - if (marker_size <= 0) - marker_size = DEFAULT_CONFLICT_MARKER_SIZE; - - /* Before conflicting part */ - size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0, - dest ? dest + size : NULL); - - if (!dest) { - size += marker_size + 1 + needs_cr + marker1_size; - } else { - memset(dest + size, '<', marker_size); - size += marker_size; - if (marker1_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name1, marker1_size - 1); - size += marker1_size; - } - if (needs_cr) - dest[size++] = '\r'; - dest[size++] = '\n'; - } - - /* Postimage from side #1 */ - size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, 1, - dest ? dest + size : NULL); - - if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) { - /* Shared preimage */ - if (!dest) { - size += marker_size + 1 + needs_cr + marker3_size; - } else { - memset(dest + size, '|', marker_size); - size += marker_size; - if (marker3_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name3, marker3_size - 1); - size += marker3_size; - } - if (needs_cr) - dest[size++] = '\r'; - dest[size++] = '\n'; - } - size += xdl_orig_copy(xe1, m->i0, m->chg0, needs_cr, 1, - dest ? dest + size : NULL); - } - - if (!dest) { - size += marker_size + 1 + needs_cr; - } else { - memset(dest + size, '=', marker_size); - size += marker_size; - if (needs_cr) - dest[size++] = '\r'; - dest[size++] = '\n'; - } - - /* Postimage from side #2 */ - size += xdl_recs_copy(xe2, m->i2, m->chg2, needs_cr, 1, - dest ? dest + size : NULL); - if (!dest) { - size += marker_size + 1 + needs_cr + marker2_size; - } else { - memset(dest + size, '>', marker_size); - size += marker_size; - if (marker2_size) { - dest[size] = ' '; - memcpy(dest + size + 1, name2, marker2_size - 1); - size += marker2_size; - } - if (needs_cr) - dest[size++] = '\r'; - dest[size++] = '\n'; - } - return size; -} - -static int xdl_fill_merge_buffer(xdfenv_t *xe1, const char *name1, - xdfenv_t *xe2, const char *name2, - const char *ancestor_name, - int favor, - xdmerge_t *m, char *dest, int style, - int marker_size) -{ - int size, i; - - for (size = i = 0; m; m = m->next) { - if (favor && !m->mode) - m->mode = favor; - - if (m->mode == 0) - size = fill_conflict_hunk(xe1, name1, xe2, name2, - ancestor_name, - size, i, style, m, dest, - marker_size); - else if (m->mode & 3) { - /* Before conflicting part */ - size += xdl_recs_copy(xe1, i, m->i1 - i, 0, 0, - dest ? dest + size : NULL); - /* Postimage from side #1 */ - if (m->mode & 1) { - int needs_cr = is_cr_needed(xe1, xe2, m); - - size += xdl_recs_copy(xe1, m->i1, m->chg1, needs_cr, (m->mode & 2), - dest ? dest + size : NULL); - } - /* Postimage from side #2 */ - if (m->mode & 2) - size += xdl_recs_copy(xe2, m->i2, m->chg2, 0, 0, - dest ? dest + size : NULL); - } else - continue; - i = m->i1 + m->chg1; - } - size += xdl_recs_copy(xe1, i, xe1->xdf2.nrec - i, 0, 0, - dest ? dest + size : NULL); - return size; -} - -static int recmatch(xrecord_t *rec1, xrecord_t *rec2, unsigned long flags) -{ - return xdl_recmatch(rec1->ptr, rec1->size, - rec2->ptr, rec2->size, flags); -} - -/* - * Remove any common lines from the beginning and end of the conflicted region. - */ -static void xdl_refine_zdiff3_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, - xpparam_t const *xpp) -{ - xrecord_t **rec1 = xe1->xdf2.recs, **rec2 = xe2->xdf2.recs; - for (; m; m = m->next) { - /* let's handle just the conflicts */ - if (m->mode) - continue; - - while(m->chg1 && m->chg2 && - recmatch(rec1[m->i1], rec2[m->i2], xpp->flags)) { - m->chg1--; - m->chg2--; - m->i1++; - m->i2++; - } - while (m->chg1 && m->chg2 && - recmatch(rec1[m->i1 + m->chg1 - 1], - rec2[m->i2 + m->chg2 - 1], xpp->flags)) { - m->chg1--; - m->chg2--; - } - } -} - -/* - * Sometimes, changes are not quite identical, but differ in only a few - * lines. Try hard to show only these few lines as conflicting. - */ -static int xdl_refine_conflicts(xdfenv_t *xe1, xdfenv_t *xe2, xdmerge_t *m, - xpparam_t const *xpp) -{ - for (; m; m = m->next) { - mmfile_t t1, t2; - xdfenv_t xe; - xdchange_t *xscr, *x; - int i1 = m->i1, i2 = m->i2; - - /* let's handle just the conflicts */ - if (m->mode) - continue; - - /* no sense refining a conflict when one side is empty */ - if (m->chg1 == 0 || m->chg2 == 0) - continue; - - /* - * This probably does not work outside git, since - * we have a very simple mmfile structure. - */ - t1.ptr = (char *)xe1->xdf2.recs[m->i1]->ptr; - t1.size = xe1->xdf2.recs[m->i1 + m->chg1 - 1]->ptr - + xe1->xdf2.recs[m->i1 + m->chg1 - 1]->size - t1.ptr; - t2.ptr = (char *)xe2->xdf2.recs[m->i2]->ptr; - t2.size = xe2->xdf2.recs[m->i2 + m->chg2 - 1]->ptr - + xe2->xdf2.recs[m->i2 + m->chg2 - 1]->size - t2.ptr; - if (xdl_do_diff(&t1, &t2, xpp, &xe) < 0) - return -1; - if (xdl_change_compact(&xe.xdf1, &xe.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe.xdf2, &xe.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe, &xscr) < 0) { - xdl_free_env(&xe); - return -1; - } - if (!xscr) { - /* If this happens, the changes are identical. */ - xdl_free_env(&xe); - m->mode = 4; - continue; - } - x = xscr; - m->i1 = xscr->i1 + i1; - m->chg1 = xscr->chg1; - m->i2 = xscr->i2 + i2; - m->chg2 = xscr->chg2; - while (xscr->next) { - xdmerge_t *m2 = xdl_malloc(sizeof(xdmerge_t)); - if (!m2) { - xdl_free_env(&xe); - xdl_free_script(x); - return -1; - } - xscr = xscr->next; - m2->next = m->next; - m->next = m2; - m = m2; - m->mode = 0; - m->i1 = xscr->i1 + i1; - m->chg1 = xscr->chg1; - m->i2 = xscr->i2 + i2; - m->chg2 = xscr->chg2; - } - xdl_free_env(&xe); - xdl_free_script(x); - } - return 0; -} - -static int line_contains_alnum(const char *ptr, long size) -{ - while (size--) - if (isalnum((unsigned char)*(ptr++))) - return 1; - return 0; -} - -static int lines_contain_alnum(xdfenv_t *xe, int i, int chg) -{ - for (; chg; chg--, i++) - if (line_contains_alnum(xe->xdf2.recs[i]->ptr, - xe->xdf2.recs[i]->size)) - return 1; - return 0; -} - -/* - * This function merges m and m->next, marking everything between those hunks - * as conflicting, too. - */ -static void xdl_merge_two_conflicts(xdmerge_t *m) -{ - xdmerge_t *next_m = m->next; - m->chg1 = next_m->i1 + next_m->chg1 - m->i1; - m->chg2 = next_m->i2 + next_m->chg2 - m->i2; - m->next = next_m->next; - xdl_free(next_m); -} - -/* - * If there are less than 3 non-conflicting lines between conflicts, - * it appears simpler -- because it takes up less (or as many) lines -- - * if the lines are moved into the conflicts. - */ -static int xdl_simplify_non_conflicts(xdfenv_t *xe1, xdmerge_t *m, - int simplify_if_no_alnum) -{ - int result = 0; - - if (!m) - return result; - for (;;) { - xdmerge_t *next_m = m->next; - int begin, end; - - if (!next_m) - return result; - - begin = m->i1 + m->chg1; - end = next_m->i1; - - if (m->mode != 0 || next_m->mode != 0 || - (end - begin > 3 && - (!simplify_if_no_alnum || - lines_contain_alnum(xe1, begin, end - begin)))) { - m = next_m; - } else { - result++; - xdl_merge_two_conflicts(m); - } - } -} - -/* - * level == 0: mark all overlapping changes as conflict - * level == 1: mark overlapping changes as conflict only if not identical - * level == 2: analyze non-identical changes for minimal conflict set - * level == 3: analyze non-identical changes for minimal conflict set, but - * treat hunks not containing any letter or number as conflicting - * - * returns < 0 on error, == 0 for no conflicts, else number of conflicts - */ -static int xdl_do_merge(xdfenv_t *xe1, xdchange_t *xscr1, - xdfenv_t *xe2, xdchange_t *xscr2, - xmparam_t const *xmp, mmbuffer_t *result) -{ - xdmerge_t *changes, *c; - xpparam_t const *xpp = &xmp->xpp; - const char *const ancestor_name = xmp->ancestor; - const char *const name1 = xmp->file1; - const char *const name2 = xmp->file2; - int i0, i1, i2, chg0, chg1, chg2; - int level = xmp->level; - int style = xmp->style; - int favor = xmp->favor; - - /* - * XDL_MERGE_DIFF3 does not attempt to refine conflicts by looking - * at common areas of sides 1 & 2, because the base (side 0) does - * not match and is being shown. Similarly, simplification of - * non-conflicts is also skipped due to the skipping of conflict - * refinement. - * - * XDL_MERGE_ZEALOUS_DIFF3, on the other hand, will attempt to - * refine conflicts looking for common areas of sides 1 & 2. - * However, since the base is being shown and does not match, - * it will only look for common areas at the beginning or end - * of the conflict block. Since XDL_MERGE_ZEALOUS_DIFF3's - * conflict refinement is much more limited in this fashion, the - * conflict simplification will be skipped. - */ - if (style == XDL_MERGE_DIFF3 || style == XDL_MERGE_ZEALOUS_DIFF3) { - /* - * "diff3 -m" output does not make sense for anything - * more aggressive than XDL_MERGE_EAGER. - */ - if (XDL_MERGE_EAGER < level) - level = XDL_MERGE_EAGER; - } - - c = changes = NULL; - - while (xscr1 && xscr2) { - if (!changes) - changes = c; - if (xscr1->i1 + xscr1->chg1 < xscr2->i1) { - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr2->i2 - xscr2->i1 + xscr1->i1; - chg0 = xscr1->chg1; - chg1 = xscr1->chg2; - chg2 = xscr1->chg1; - if (xdl_append_merge(&c, 1, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr1 = xscr1->next; - continue; - } - if (xscr2->i1 + xscr2->chg1 < xscr1->i1) { - i0 = xscr2->i1; - i1 = xscr1->i2 - xscr1->i1 + xscr2->i1; - i2 = xscr2->i2; - chg0 = xscr2->chg1; - chg1 = xscr2->chg1; - chg2 = xscr2->chg2; - if (xdl_append_merge(&c, 2, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr2 = xscr2->next; - continue; - } - if (level == XDL_MERGE_MINIMAL || xscr1->i1 != xscr2->i1 || - xscr1->chg1 != xscr2->chg1 || - xscr1->chg2 != xscr2->chg2 || - xdl_merge_cmp_lines(xe1, xscr1->i2, - xe2, xscr2->i2, - xscr1->chg2, xpp->flags)) { - /* conflict */ - int off = xscr1->i1 - xscr2->i1; - int ffo = off + xscr1->chg1 - xscr2->chg1; - - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr2->i2; - if (off > 0) { - i0 -= off; - i1 -= off; - } - else - i2 += off; - chg0 = xscr1->i1 + xscr1->chg1 - i0; - chg1 = xscr1->i2 + xscr1->chg2 - i1; - chg2 = xscr2->i2 + xscr2->chg2 - i2; - if (ffo < 0) { - chg0 -= ffo; - chg1 -= ffo; - } else - chg2 += ffo; - if (xdl_append_merge(&c, 0, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - } - - i1 = xscr1->i1 + xscr1->chg1; - i2 = xscr2->i1 + xscr2->chg1; - - if (i1 >= i2) - xscr2 = xscr2->next; - if (i2 >= i1) - xscr1 = xscr1->next; - } - while (xscr1) { - if (!changes) - changes = c; - i0 = xscr1->i1; - i1 = xscr1->i2; - i2 = xscr1->i1 + xe2->xdf2.nrec - xe2->xdf1.nrec; - chg0 = xscr1->chg1; - chg1 = xscr1->chg2; - chg2 = xscr1->chg1; - if (xdl_append_merge(&c, 1, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr1 = xscr1->next; - } - while (xscr2) { - if (!changes) - changes = c; - i0 = xscr2->i1; - i1 = xscr2->i1 + xe1->xdf2.nrec - xe1->xdf1.nrec; - i2 = xscr2->i2; - chg0 = xscr2->chg1; - chg1 = xscr2->chg1; - chg2 = xscr2->chg2; - if (xdl_append_merge(&c, 2, - i0, chg0, i1, chg1, i2, chg2)) { - xdl_cleanup_merge(changes); - return -1; - } - xscr2 = xscr2->next; - } - if (!changes) - changes = c; - /* refine conflicts */ - if (style == XDL_MERGE_ZEALOUS_DIFF3) { - xdl_refine_zdiff3_conflicts(xe1, xe2, changes, xpp); - } else if (XDL_MERGE_ZEALOUS <= level && - (xdl_refine_conflicts(xe1, xe2, changes, xpp) < 0 || - xdl_simplify_non_conflicts(xe1, changes, - XDL_MERGE_ZEALOUS < level) < 0)) { - xdl_cleanup_merge(changes); - return -1; - } - /* output */ - if (result) { - int marker_size = xmp->marker_size; - int size = xdl_fill_merge_buffer(xe1, name1, xe2, name2, - ancestor_name, - favor, changes, NULL, style, - marker_size); - result->ptr = xdl_malloc(size); - if (!result->ptr) { - xdl_cleanup_merge(changes); - return -1; - } - result->size = size; - xdl_fill_merge_buffer(xe1, name1, xe2, name2, - ancestor_name, favor, changes, - result->ptr, style, marker_size); - } - return xdl_cleanup_merge(changes); -} - -int xdl_merge(mmfile_t *orig, mmfile_t *mf1, mmfile_t *mf2, - xmparam_t const *xmp, mmbuffer_t *result) -{ - xdchange_t *xscr1, *xscr2; - xdfenv_t xe1, xe2; - int status; - xpparam_t const *xpp = &xmp->xpp; - - result->ptr = NULL; - result->size = 0; - - if (xdl_do_diff(orig, mf1, xpp, &xe1) < 0) { - return -1; - } - if (xdl_do_diff(orig, mf2, xpp, &xe2) < 0) { - xdl_free_env(&xe1); - return -1; - } - if (xdl_change_compact(&xe1.xdf1, &xe1.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe1.xdf2, &xe1.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe1, &xscr1) < 0) { - xdl_free_env(&xe1); - return -1; - } - if (xdl_change_compact(&xe2.xdf1, &xe2.xdf2, xpp->flags) < 0 || - xdl_change_compact(&xe2.xdf2, &xe2.xdf1, xpp->flags) < 0 || - xdl_build_script(&xe2, &xscr2) < 0) { - xdl_free_script(xscr1); - xdl_free_env(&xe1); - xdl_free_env(&xe2); - return -1; - } - status = 0; - if (!xscr1) { - result->ptr = xdl_malloc(mf2->size); - memcpy(result->ptr, mf2->ptr, mf2->size); - result->size = mf2->size; - } else if (!xscr2) { - result->ptr = xdl_malloc(mf1->size); - memcpy(result->ptr, mf1->ptr, mf1->size); - result->size = mf1->size; - } else { - status = xdl_do_merge(&xe1, xscr1, - &xe2, xscr2, - xmp, result); - } - xdl_free_script(xscr1); - xdl_free_script(xscr2); - - xdl_free_env(&xe1); - xdl_free_env(&xe2); - - return status; -} diff --git a/src/libgit2/xdiff/xpatience.c b/src/libgit2/xdiff/xpatience.c deleted file mode 100644 index c5d48e80a..000000000 --- a/src/libgit2/xdiff/xpatience.c +++ /dev/null @@ -1,382 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003-2016 Davide Libenzi, Johannes E. Schindelin - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ -#include "xinclude.h" - -/* - * The basic idea of patience diff is to find lines that are unique in - * both files. These are intuitively the ones that we want to see as - * common lines. - * - * The maximal ordered sequence of such line pairs (where ordered means - * that the order in the sequence agrees with the order of the lines in - * both files) naturally defines an initial set of common lines. - * - * Now, the algorithm tries to extend the set of common lines by growing - * the line ranges where the files have identical lines. - * - * Between those common lines, the patience diff algorithm is applied - * recursively, until no unique line pairs can be found; these line ranges - * are handled by the well-known Myers algorithm. - */ - -#define NON_UNIQUE ULONG_MAX - -/* - * This is a hash mapping from line hash to line numbers in the first and - * second file. - */ -struct hashmap { - int nr, alloc; - struct entry { - unsigned long hash; - /* - * 0 = unused entry, 1 = first line, 2 = second, etc. - * line2 is NON_UNIQUE if the line is not unique - * in either the first or the second file. - */ - unsigned long line1, line2; - /* - * "next" & "previous" are used for the longest common - * sequence; - * initially, "next" reflects only the order in file1. - */ - struct entry *next, *previous; - - /* - * If 1, this entry can serve as an anchor. See - * Documentation/diff-options.txt for more information. - */ - unsigned anchor : 1; - } *entries, *first, *last; - /* were common records found? */ - unsigned long has_matches; - mmfile_t *file1, *file2; - xdfenv_t *env; - xpparam_t const *xpp; -}; - -static int is_anchor(xpparam_t const *xpp, const char *line) -{ - int i; - for (i = 0; i < xpp->anchors_nr; i++) { - if (!strncmp(line, xpp->anchors[i], strlen(xpp->anchors[i]))) - return 1; - } - return 0; -} - -/* The argument "pass" is 1 for the first file, 2 for the second. */ -static void insert_record(xpparam_t const *xpp, int line, struct hashmap *map, - int pass) -{ - xrecord_t **records = pass == 1 ? - map->env->xdf1.recs : map->env->xdf2.recs; - xrecord_t *record = records[line - 1]; - /* - * After xdl_prepare_env() (or more precisely, due to - * xdl_classify_record()), the "ha" member of the records (AKA lines) - * is _not_ the hash anymore, but a linearized version of it. In - * other words, the "ha" member is guaranteed to start with 0 and - * the second record's ha can only be 0 or 1, etc. - * - * So we multiply ha by 2 in the hope that the hashing was - * "unique enough". - */ - int index = (int)((record->ha << 1) % map->alloc); - - while (map->entries[index].line1) { - if (map->entries[index].hash != record->ha) { - if (++index >= map->alloc) - index = 0; - continue; - } - if (pass == 2) - map->has_matches = 1; - if (pass == 1 || map->entries[index].line2) - map->entries[index].line2 = NON_UNIQUE; - else - map->entries[index].line2 = line; - return; - } - if (pass == 2) - return; - map->entries[index].line1 = line; - map->entries[index].hash = record->ha; - map->entries[index].anchor = is_anchor(xpp, map->env->xdf1.recs[line - 1]->ptr); - if (!map->first) - map->first = map->entries + index; - if (map->last) { - map->last->next = map->entries + index; - map->entries[index].previous = map->last; - } - map->last = map->entries + index; - map->nr++; -} - -/* - * This function has to be called for each recursion into the inter-hunk - * parts, as previously non-unique lines can become unique when being - * restricted to a smaller part of the files. - * - * It is assumed that env has been prepared using xdl_prepare(). - */ -static int fill_hashmap(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, - struct hashmap *result, - int line1, int count1, int line2, int count2) -{ - result->file1 = file1; - result->file2 = file2; - result->xpp = xpp; - result->env = env; - - /* We know exactly how large we want the hash map */ - result->alloc = count1 * 2; - result->entries = (struct entry *) - xdl_malloc(result->alloc * sizeof(struct entry)); - if (!result->entries) - return -1; - memset(result->entries, 0, result->alloc * sizeof(struct entry)); - - /* First, fill with entries from the first file */ - while (count1--) - insert_record(xpp, line1++, result, 1); - - /* Then search for matches in the second file */ - while (count2--) - insert_record(xpp, line2++, result, 2); - - return 0; -} - -/* - * Find the longest sequence with a smaller last element (meaning a smaller - * line2, as we construct the sequence with entries ordered by line1). - */ -static int binary_search(struct entry **sequence, int longest, - struct entry *entry) -{ - int left = -1, right = longest; - - while (left + 1 < right) { - int middle = left + (right - left) / 2; - /* by construction, no two entries can be equal */ - if (sequence[middle]->line2 > entry->line2) - right = middle; - else - left = middle; - } - /* return the index in "sequence", _not_ the sequence length */ - return left; -} - -/* - * The idea is to start with the list of common unique lines sorted by - * the order in file1. For each of these pairs, the longest (partial) - * sequence whose last element's line2 is smaller is determined. - * - * For efficiency, the sequences are kept in a list containing exactly one - * item per sequence length: the sequence with the smallest last - * element (in terms of line2). - */ -static struct entry *find_longest_common_sequence(struct hashmap *map) -{ - struct entry **sequence = xdl_malloc(map->nr * sizeof(struct entry *)); - int longest = 0, i; - struct entry *entry; - - /* - * If not -1, this entry in sequence must never be overridden. - * Therefore, overriding entries before this has no effect, so - * do not do that either. - */ - int anchor_i = -1; - - for (entry = map->first; entry; entry = entry->next) { - if (!entry->line2 || entry->line2 == NON_UNIQUE) - continue; - i = binary_search(sequence, longest, entry); - entry->previous = i < 0 ? NULL : sequence[i]; - ++i; - if (i <= anchor_i) - continue; - sequence[i] = entry; - if (entry->anchor) { - anchor_i = i; - longest = anchor_i + 1; - } else if (i == longest) { - longest++; - } - } - - /* No common unique lines were found */ - if (!longest) { - xdl_free(sequence); - return NULL; - } - - /* Iterate starting at the last element, adjusting the "next" members */ - entry = sequence[longest - 1]; - entry->next = NULL; - while (entry->previous) { - entry->previous->next = entry; - entry = entry->previous; - } - xdl_free(sequence); - return entry; -} - -static int match(struct hashmap *map, int line1, int line2) -{ - xrecord_t *record1 = map->env->xdf1.recs[line1 - 1]; - xrecord_t *record2 = map->env->xdf2.recs[line2 - 1]; - return record1->ha == record2->ha; -} - -static int patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, - int line1, int count1, int line2, int count2); - -static int walk_common_sequence(struct hashmap *map, struct entry *first, - int line1, int count1, int line2, int count2) -{ - int end1 = line1 + count1, end2 = line2 + count2; - int next1, next2; - - for (;;) { - /* Try to grow the line ranges of common lines */ - if (first) { - next1 = first->line1; - next2 = first->line2; - while (next1 > line1 && next2 > line2 && - match(map, next1 - 1, next2 - 1)) { - next1--; - next2--; - } - } else { - next1 = end1; - next2 = end2; - } - while (line1 < next1 && line2 < next2 && - match(map, line1, line2)) { - line1++; - line2++; - } - - /* Recurse */ - if (next1 > line1 || next2 > line2) { - if (patience_diff(map->file1, map->file2, - map->xpp, map->env, - line1, next1 - line1, - line2, next2 - line2)) - return -1; - } - - if (!first) - return 0; - - while (first->next && - first->next->line1 == first->line1 + 1 && - first->next->line2 == first->line2 + 1) - first = first->next; - - line1 = first->line1 + 1; - line2 = first->line2 + 1; - - first = first->next; - } -} - -static int fall_back_to_classic_diff(struct hashmap *map, - int line1, int count1, int line2, int count2) -{ - xpparam_t xpp; - - memset(&xpp, 0, sizeof(xpp)); - xpp.flags = map->xpp->flags & ~XDF_DIFF_ALGORITHM_MASK; - - return xdl_fall_back_diff(map->env, &xpp, - line1, count1, line2, count2); -} - -/* - * Recursively find the longest common sequence of unique lines, - * and if none was found, ask xdl_do_diff() to do the job. - * - * This function assumes that env was prepared with xdl_prepare_env(). - */ -static int patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env, - int line1, int count1, int line2, int count2) -{ - struct hashmap map; - struct entry *first; - int result = 0; - - /* trivial case: one side is empty */ - if (!count1) { - while(count2--) - env->xdf2.rchg[line2++ - 1] = 1; - return 0; - } else if (!count2) { - while(count1--) - env->xdf1.rchg[line1++ - 1] = 1; - return 0; - } - - memset(&map, 0, sizeof(map)); - if (fill_hashmap(file1, file2, xpp, env, &map, - line1, count1, line2, count2)) - return -1; - - /* are there any matching lines at all? */ - if (!map.has_matches) { - while(count1--) - env->xdf1.rchg[line1++ - 1] = 1; - while(count2--) - env->xdf2.rchg[line2++ - 1] = 1; - xdl_free(map.entries); - return 0; - } - - first = find_longest_common_sequence(&map); - if (first) - result = walk_common_sequence(&map, first, - line1, count1, line2, count2); - else - result = fall_back_to_classic_diff(&map, - line1, count1, line2, count2); - - xdl_free(map.entries); - return result; -} - -int xdl_do_patience_diff(mmfile_t *file1, mmfile_t *file2, - xpparam_t const *xpp, xdfenv_t *env) -{ - if (xdl_prepare_env(file1, file2, xpp, env) < 0) - return -1; - - /* environment is cleaned up in xdl_diff() */ - return patience_diff(file1, file2, xpp, env, - 1, env->xdf1.nrec, 1, env->xdf2.nrec); -} diff --git a/src/libgit2/xdiff/xprepare.c b/src/libgit2/xdiff/xprepare.c deleted file mode 100644 index 4527a4a07..000000000 --- a/src/libgit2/xdiff/xprepare.c +++ /dev/null @@ -1,478 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - - -#define XDL_KPDIS_RUN 4 -#define XDL_MAX_EQLIMIT 1024 -#define XDL_SIMSCAN_WINDOW 100 -#define XDL_GUESS_NLINES1 256 -#define XDL_GUESS_NLINES2 20 - - -typedef struct s_xdlclass { - struct s_xdlclass *next; - unsigned long ha; - char const *line; - long size; - long idx; - long len1, len2; -} xdlclass_t; - -typedef struct s_xdlclassifier { - unsigned int hbits; - long hsize; - xdlclass_t **rchash; - chastore_t ncha; - xdlclass_t **rcrecs; - long alloc; - long count; - long flags; -} xdlclassifier_t; - - - - -static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags); -static void xdl_free_classifier(xdlclassifier_t *cf); -static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash, - unsigned int hbits, xrecord_t *rec); -static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp, - xdlclassifier_t *cf, xdfile_t *xdf); -static void xdl_free_ctx(xdfile_t *xdf); -static int xdl_clean_mmatch(char const *dis, long i, long s, long e); -static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2); -static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2); -static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2); - - - - -static int xdl_init_classifier(xdlclassifier_t *cf, long size, long flags) { - cf->flags = flags; - - cf->hbits = xdl_hashbits((unsigned int) size); - cf->hsize = 1 << cf->hbits; - - if (xdl_cha_init(&cf->ncha, sizeof(xdlclass_t), size / 4 + 1) < 0) { - - return -1; - } - if (!(cf->rchash = (xdlclass_t **) xdl_malloc(cf->hsize * sizeof(xdlclass_t *)))) { - - xdl_cha_free(&cf->ncha); - return -1; - } - memset(cf->rchash, 0, cf->hsize * sizeof(xdlclass_t *)); - - cf->alloc = size; - if (!(cf->rcrecs = (xdlclass_t **) xdl_malloc(cf->alloc * sizeof(xdlclass_t *)))) { - - xdl_free(cf->rchash); - xdl_cha_free(&cf->ncha); - return -1; - } - - cf->count = 0; - - return 0; -} - - -static void xdl_free_classifier(xdlclassifier_t *cf) { - - xdl_free(cf->rcrecs); - xdl_free(cf->rchash); - xdl_cha_free(&cf->ncha); -} - - -static int xdl_classify_record(unsigned int pass, xdlclassifier_t *cf, xrecord_t **rhash, - unsigned int hbits, xrecord_t *rec) { - long hi; - char const *line; - xdlclass_t *rcrec; - xdlclass_t **rcrecs; - - line = rec->ptr; - hi = (long) XDL_HASHLONG(rec->ha, cf->hbits); - for (rcrec = cf->rchash[hi]; rcrec; rcrec = rcrec->next) - if (rcrec->ha == rec->ha && - xdl_recmatch(rcrec->line, rcrec->size, - rec->ptr, rec->size, cf->flags)) - break; - - if (!rcrec) { - if (!(rcrec = xdl_cha_alloc(&cf->ncha))) { - - return -1; - } - rcrec->idx = cf->count++; - if (cf->count > cf->alloc) { - cf->alloc *= 2; - if (!(rcrecs = (xdlclass_t **) xdl_realloc(cf->rcrecs, cf->alloc * sizeof(xdlclass_t *)))) { - - return -1; - } - cf->rcrecs = rcrecs; - } - cf->rcrecs[rcrec->idx] = rcrec; - rcrec->line = line; - rcrec->size = rec->size; - rcrec->ha = rec->ha; - rcrec->len1 = rcrec->len2 = 0; - rcrec->next = cf->rchash[hi]; - cf->rchash[hi] = rcrec; - } - - (pass == 1) ? rcrec->len1++ : rcrec->len2++; - - rec->ha = (unsigned long) rcrec->idx; - - hi = (long) XDL_HASHLONG(rec->ha, hbits); - rec->next = rhash[hi]; - rhash[hi] = rec; - - return 0; -} - - -static int xdl_prepare_ctx(unsigned int pass, mmfile_t *mf, long narec, xpparam_t const *xpp, - xdlclassifier_t *cf, xdfile_t *xdf) { - unsigned int hbits; - long nrec, hsize, bsize; - unsigned long hav; - char const *blk, *cur, *top, *prev; - xrecord_t *crec; - xrecord_t **recs, **rrecs; - xrecord_t **rhash; - unsigned long *ha; - char *rchg; - long *rindex; - - ha = NULL; - rindex = NULL; - rchg = NULL; - rhash = NULL; - recs = NULL; - - if (xdl_cha_init(&xdf->rcha, sizeof(xrecord_t), narec / 4 + 1) < 0) - goto abort; - if (!(recs = (xrecord_t **) xdl_malloc(narec * sizeof(xrecord_t *)))) - goto abort; - - hbits = xdl_hashbits((unsigned int) narec); - hsize = 1 << hbits; - if (!(rhash = (xrecord_t **) xdl_malloc(hsize * sizeof(xrecord_t *)))) - goto abort; - memset(rhash, 0, hsize * sizeof(xrecord_t *)); - - nrec = 0; - if ((cur = blk = xdl_mmfile_first(mf, &bsize)) != NULL) { - for (top = blk + bsize; cur < top; ) { - prev = cur; - hav = xdl_hash_record(&cur, top, xpp->flags); - if (nrec >= narec) { - narec *= 2; - if (!(rrecs = (xrecord_t **) xdl_realloc(recs, narec * sizeof(xrecord_t *)))) - goto abort; - recs = rrecs; - } - if (!(crec = xdl_cha_alloc(&xdf->rcha))) - goto abort; - crec->ptr = prev; - crec->size = (long) (cur - prev); - crec->ha = hav; - recs[nrec++] = crec; - if (xdl_classify_record(pass, cf, rhash, hbits, crec) < 0) - goto abort; - } - } - - if (!(rchg = (char *) xdl_malloc((nrec + 2) * sizeof(char)))) - goto abort; - memset(rchg, 0, (nrec + 2) * sizeof(char)); - - if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && - (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF)) { - if (!(rindex = xdl_malloc((nrec + 1) * sizeof(*rindex)))) - goto abort; - if (!(ha = xdl_malloc((nrec + 1) * sizeof(*ha)))) - goto abort; - } - - xdf->nrec = nrec; - xdf->recs = recs; - xdf->hbits = hbits; - xdf->rhash = rhash; - xdf->rchg = rchg + 1; - xdf->rindex = rindex; - xdf->nreff = 0; - xdf->ha = ha; - xdf->dstart = 0; - xdf->dend = nrec - 1; - - return 0; - -abort: - xdl_free(ha); - xdl_free(rindex); - xdl_free(rchg); - xdl_free(rhash); - xdl_free(recs); - xdl_cha_free(&xdf->rcha); - return -1; -} - - -static void xdl_free_ctx(xdfile_t *xdf) { - - xdl_free(xdf->rhash); - xdl_free(xdf->rindex); - xdl_free(xdf->rchg - 1); - xdl_free(xdf->ha); - xdl_free(xdf->recs); - xdl_cha_free(&xdf->rcha); -} - - -int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *xe) { - long enl1, enl2, sample; - xdlclassifier_t cf; - - memset(&cf, 0, sizeof(cf)); - - /* - * For histogram diff, we can afford a smaller sample size and - * thus a poorer estimate of the number of lines, as the hash - * table (rhash) won't be filled up/grown. The number of lines - * (nrecs) will be updated correctly anyway by - * xdl_prepare_ctx(). - */ - sample = (XDF_DIFF_ALG(xpp->flags) == XDF_HISTOGRAM_DIFF - ? XDL_GUESS_NLINES2 : XDL_GUESS_NLINES1); - - enl1 = xdl_guess_lines(mf1, sample) + 1; - enl2 = xdl_guess_lines(mf2, sample) + 1; - - if (xdl_init_classifier(&cf, enl1 + enl2 + 1, xpp->flags) < 0) - return -1; - - if (xdl_prepare_ctx(1, mf1, enl1, xpp, &cf, &xe->xdf1) < 0) { - - xdl_free_classifier(&cf); - return -1; - } - if (xdl_prepare_ctx(2, mf2, enl2, xpp, &cf, &xe->xdf2) < 0) { - - xdl_free_ctx(&xe->xdf1); - xdl_free_classifier(&cf); - return -1; - } - - if ((XDF_DIFF_ALG(xpp->flags) != XDF_PATIENCE_DIFF) && - (XDF_DIFF_ALG(xpp->flags) != XDF_HISTOGRAM_DIFF) && - xdl_optimize_ctxs(&cf, &xe->xdf1, &xe->xdf2) < 0) { - - xdl_free_ctx(&xe->xdf2); - xdl_free_ctx(&xe->xdf1); - xdl_free_classifier(&cf); - return -1; - } - - xdl_free_classifier(&cf); - - return 0; -} - - -void xdl_free_env(xdfenv_t *xe) { - - xdl_free_ctx(&xe->xdf2); - xdl_free_ctx(&xe->xdf1); -} - - -static int xdl_clean_mmatch(char const *dis, long i, long s, long e) { - long r, rdis0, rpdis0, rdis1, rpdis1; - - /* - * Limits the window the is examined during the similar-lines - * scan. The loops below stops when dis[i - r] == 1 (line that - * has no match), but there are corner cases where the loop - * proceed all the way to the extremities by causing huge - * performance penalties in case of big files. - */ - if (i - s > XDL_SIMSCAN_WINDOW) - s = i - XDL_SIMSCAN_WINDOW; - if (e - i > XDL_SIMSCAN_WINDOW) - e = i + XDL_SIMSCAN_WINDOW; - - /* - * Scans the lines before 'i' to find a run of lines that either - * have no match (dis[j] == 0) or have multiple matches (dis[j] > 1). - * Note that we always call this function with dis[i] > 1, so the - * current line (i) is already a multimatch line. - */ - for (r = 1, rdis0 = 0, rpdis0 = 1; (i - r) >= s; r++) { - if (!dis[i - r]) - rdis0++; - else if (dis[i - r] == 2) - rpdis0++; - else - break; - } - /* - * If the run before the line 'i' found only multimatch lines, we - * return 0 and hence we don't make the current line (i) discarded. - * We want to discard multimatch lines only when they appear in the - * middle of runs with nomatch lines (dis[j] == 0). - */ - if (rdis0 == 0) - return 0; - for (r = 1, rdis1 = 0, rpdis1 = 1; (i + r) <= e; r++) { - if (!dis[i + r]) - rdis1++; - else if (dis[i + r] == 2) - rpdis1++; - else - break; - } - /* - * If the run after the line 'i' found only multimatch lines, we - * return 0 and hence we don't make the current line (i) discarded. - */ - if (rdis1 == 0) - return 0; - rdis1 += rdis0; - rpdis1 += rpdis0; - - return rpdis1 * XDL_KPDIS_RUN < (rpdis1 + rdis1); -} - - -/* - * Try to reduce the problem complexity, discard records that have no - * matches on the other file. Also, lines that have multiple matches - * might be potentially discarded if they happear in a run of discardable. - */ -static int xdl_cleanup_records(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) { - long i, nm, nreff, mlim; - xrecord_t **recs; - xdlclass_t *rcrec; - char *dis, *dis1, *dis2; - - if (!(dis = (char *) xdl_malloc(xdf1->nrec + xdf2->nrec + 2))) { - - return -1; - } - memset(dis, 0, xdf1->nrec + xdf2->nrec + 2); - dis1 = dis; - dis2 = dis1 + xdf1->nrec + 1; - - if ((mlim = xdl_bogosqrt(xdf1->nrec)) > XDL_MAX_EQLIMIT) - mlim = XDL_MAX_EQLIMIT; - for (i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; i <= xdf1->dend; i++, recs++) { - rcrec = cf->rcrecs[(*recs)->ha]; - nm = rcrec ? rcrec->len2 : 0; - dis1[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; - } - - if ((mlim = xdl_bogosqrt(xdf2->nrec)) > XDL_MAX_EQLIMIT) - mlim = XDL_MAX_EQLIMIT; - for (i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; i <= xdf2->dend; i++, recs++) { - rcrec = cf->rcrecs[(*recs)->ha]; - nm = rcrec ? rcrec->len1 : 0; - dis2[i] = (nm == 0) ? 0: (nm >= mlim) ? 2: 1; - } - - for (nreff = 0, i = xdf1->dstart, recs = &xdf1->recs[xdf1->dstart]; - i <= xdf1->dend; i++, recs++) { - if (dis1[i] == 1 || - (dis1[i] == 2 && !xdl_clean_mmatch(dis1, i, xdf1->dstart, xdf1->dend))) { - xdf1->rindex[nreff] = i; - xdf1->ha[nreff] = (*recs)->ha; - nreff++; - } else - xdf1->rchg[i] = 1; - } - xdf1->nreff = nreff; - - for (nreff = 0, i = xdf2->dstart, recs = &xdf2->recs[xdf2->dstart]; - i <= xdf2->dend; i++, recs++) { - if (dis2[i] == 1 || - (dis2[i] == 2 && !xdl_clean_mmatch(dis2, i, xdf2->dstart, xdf2->dend))) { - xdf2->rindex[nreff] = i; - xdf2->ha[nreff] = (*recs)->ha; - nreff++; - } else - xdf2->rchg[i] = 1; - } - xdf2->nreff = nreff; - - xdl_free(dis); - - return 0; -} - - -/* - * Early trim initial and terminal matching records. - */ -static int xdl_trim_ends(xdfile_t *xdf1, xdfile_t *xdf2) { - long i, lim; - xrecord_t **recs1, **recs2; - - recs1 = xdf1->recs; - recs2 = xdf2->recs; - for (i = 0, lim = XDL_MIN(xdf1->nrec, xdf2->nrec); i < lim; - i++, recs1++, recs2++) - if ((*recs1)->ha != (*recs2)->ha) - break; - - xdf1->dstart = xdf2->dstart = i; - - recs1 = xdf1->recs + xdf1->nrec - 1; - recs2 = xdf2->recs + xdf2->nrec - 1; - for (lim -= i, i = 0; i < lim; i++, recs1--, recs2--) - if ((*recs1)->ha != (*recs2)->ha) - break; - - xdf1->dend = xdf1->nrec - i - 1; - xdf2->dend = xdf2->nrec - i - 1; - - return 0; -} - - -static int xdl_optimize_ctxs(xdlclassifier_t *cf, xdfile_t *xdf1, xdfile_t *xdf2) { - - if (xdl_trim_ends(xdf1, xdf2) < 0 || - xdl_cleanup_records(cf, xdf1, xdf2) < 0) { - - return -1; - } - - return 0; -} diff --git a/src/libgit2/xdiff/xprepare.h b/src/libgit2/xdiff/xprepare.h deleted file mode 100644 index 947d9fc1b..000000000 --- a/src/libgit2/xdiff/xprepare.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XPREPARE_H) -#define XPREPARE_H - - - -int xdl_prepare_env(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, - xdfenv_t *xe); -void xdl_free_env(xdfenv_t *xe); - - - -#endif /* #if !defined(XPREPARE_H) */ diff --git a/src/libgit2/xdiff/xtypes.h b/src/libgit2/xdiff/xtypes.h deleted file mode 100644 index 8442bd436..000000000 --- a/src/libgit2/xdiff/xtypes.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XTYPES_H) -#define XTYPES_H - - - -typedef struct s_chanode { - struct s_chanode *next; - long icurr; -} chanode_t; - -typedef struct s_chastore { - chanode_t *head, *tail; - long isize, nsize; - chanode_t *ancur; - chanode_t *sncur; - long scurr; -} chastore_t; - -typedef struct s_xrecord { - struct s_xrecord *next; - char const *ptr; - long size; - unsigned long ha; -} xrecord_t; - -typedef struct s_xdfile { - chastore_t rcha; - long nrec; - unsigned int hbits; - xrecord_t **rhash; - long dstart, dend; - xrecord_t **recs; - char *rchg; - long *rindex; - long nreff; - unsigned long *ha; -} xdfile_t; - -typedef struct s_xdfenv { - xdfile_t xdf1, xdf2; -} xdfenv_t; - - - -#endif /* #if !defined(XTYPES_H) */ diff --git a/src/libgit2/xdiff/xutils.c b/src/libgit2/xdiff/xutils.c deleted file mode 100644 index cfa6e2220..000000000 --- a/src/libgit2/xdiff/xutils.c +++ /dev/null @@ -1,434 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#include "xinclude.h" - - -long xdl_bogosqrt(long n) { - long i; - - /* - * Classical integer square root approximation using shifts. - */ - for (i = 1; n > 0; n >>= 2) - i <<= 1; - - return i; -} - - -int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, - xdemitcb_t *ecb) { - int i = 2; - mmbuffer_t mb[3]; - - mb[0].ptr = (char *) pre; - mb[0].size = psize; - mb[1].ptr = (char *) rec; - mb[1].size = size; - if (size > 0 && rec[size - 1] != '\n') { - mb[2].ptr = (char *) "\n\\ No newline at end of file\n"; - mb[2].size = strlen(mb[2].ptr); - i++; - } - if (ecb->out_line(ecb->priv, mb, i) < 0) { - - return -1; - } - - return 0; -} - -void *xdl_mmfile_first(mmfile_t *mmf, long *size) -{ - *size = mmf->size; - return mmf->ptr; -} - - -long xdl_mmfile_size(mmfile_t *mmf) -{ - return mmf->size; -} - - -int xdl_cha_init(chastore_t *cha, long isize, long icount) { - - cha->head = cha->tail = NULL; - cha->isize = isize; - cha->nsize = icount * isize; - cha->ancur = cha->sncur = NULL; - cha->scurr = 0; - - return 0; -} - - -void xdl_cha_free(chastore_t *cha) { - chanode_t *cur, *tmp; - - for (cur = cha->head; (tmp = cur) != NULL;) { - cur = cur->next; - xdl_free(tmp); - } -} - - -void *xdl_cha_alloc(chastore_t *cha) { - chanode_t *ancur; - void *data; - - if (!(ancur = cha->ancur) || ancur->icurr == cha->nsize) { - if (!(ancur = (chanode_t *) xdl_malloc(sizeof(chanode_t) + cha->nsize))) { - - return NULL; - } - ancur->icurr = 0; - ancur->next = NULL; - if (cha->tail) - cha->tail->next = ancur; - if (!cha->head) - cha->head = ancur; - cha->tail = ancur; - cha->ancur = ancur; - } - - data = (char *) ancur + sizeof(chanode_t) + ancur->icurr; - ancur->icurr += cha->isize; - - return data; -} - -long xdl_guess_lines(mmfile_t *mf, long sample) { - long nl = 0, size, tsize = 0; - char const *data, *cur, *top; - - if ((cur = data = xdl_mmfile_first(mf, &size)) != NULL) { - for (top = data + size; nl < sample && cur < top; ) { - nl++; - if (!(cur = memchr(cur, '\n', top - cur))) - cur = top; - else - cur++; - } - tsize += (long) (cur - data); - } - - if (nl && tsize) - nl = xdl_mmfile_size(mf) / (tsize / nl); - - return nl + 1; -} - -int xdl_blankline(const char *line, long size, long flags) -{ - long i; - - if (!(flags & XDF_WHITESPACE_FLAGS)) - return (size <= 1); - - for (i = 0; i < size && XDL_ISSPACE(line[i]); i++) - ; - - return (i == size); -} - -/* - * Have we eaten everything on the line, except for an optional - * CR at the very end? - */ -static int ends_with_optional_cr(const char *l, long s, long i) -{ - int complete = s && l[s-1] == '\n'; - - if (complete) - s--; - if (s == i) - return 1; - /* do not ignore CR at the end of an incomplete line */ - if (complete && s == i + 1 && l[i] == '\r') - return 1; - return 0; -} - -int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags) -{ - int i1, i2; - - if (s1 == s2 && !memcmp(l1, l2, s1)) - return 1; - if (!(flags & XDF_WHITESPACE_FLAGS)) - return 0; - - i1 = 0; - i2 = 0; - - /* - * -w matches everything that matches with -b, and -b in turn - * matches everything that matches with --ignore-space-at-eol, - * which in turn matches everything that matches with --ignore-cr-at-eol. - * - * Each flavor of ignoring needs different logic to skip whitespaces - * while we have both sides to compare. - */ - if (flags & XDF_IGNORE_WHITESPACE) { - goto skip_ws; - while (i1 < s1 && i2 < s2) { - if (l1[i1++] != l2[i2++]) - return 0; - skip_ws: - while (i1 < s1 && XDL_ISSPACE(l1[i1])) - i1++; - while (i2 < s2 && XDL_ISSPACE(l2[i2])) - i2++; - } - } else if (flags & XDF_IGNORE_WHITESPACE_CHANGE) { - while (i1 < s1 && i2 < s2) { - if (XDL_ISSPACE(l1[i1]) && XDL_ISSPACE(l2[i2])) { - /* Skip matching spaces and try again */ - while (i1 < s1 && XDL_ISSPACE(l1[i1])) - i1++; - while (i2 < s2 && XDL_ISSPACE(l2[i2])) - i2++; - continue; - } - if (l1[i1++] != l2[i2++]) - return 0; - } - } else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL) { - while (i1 < s1 && i2 < s2 && l1[i1] == l2[i2]) { - i1++; - i2++; - } - } else if (flags & XDF_IGNORE_CR_AT_EOL) { - /* Find the first difference and see how the line ends */ - while (i1 < s1 && i2 < s2 && l1[i1] == l2[i2]) { - i1++; - i2++; - } - return (ends_with_optional_cr(l1, s1, i1) && - ends_with_optional_cr(l2, s2, i2)); - } - - /* - * After running out of one side, the remaining side must have - * nothing but whitespace for the lines to match. Note that - * ignore-whitespace-at-eol case may break out of the loop - * while there still are characters remaining on both lines. - */ - if (i1 < s1) { - while (i1 < s1 && XDL_ISSPACE(l1[i1])) - i1++; - if (s1 != i1) - return 0; - } - if (i2 < s2) { - while (i2 < s2 && XDL_ISSPACE(l2[i2])) - i2++; - return (s2 == i2); - } - return 1; -} - -static unsigned long xdl_hash_record_with_whitespace(char const **data, - char const *top, long flags) { - unsigned long ha = 5381; - char const *ptr = *data; - int cr_at_eol_only = (flags & XDF_WHITESPACE_FLAGS) == XDF_IGNORE_CR_AT_EOL; - - for (; ptr < top && *ptr != '\n'; ptr++) { - if (cr_at_eol_only) { - /* do not ignore CR at the end of an incomplete line */ - if (*ptr == '\r' && - (ptr + 1 < top && ptr[1] == '\n')) - continue; - } - else if (XDL_ISSPACE(*ptr)) { - const char *ptr2 = ptr; - int at_eol; - while (ptr + 1 < top && XDL_ISSPACE(ptr[1]) - && ptr[1] != '\n') - ptr++; - at_eol = (top <= ptr + 1 || ptr[1] == '\n'); - if (flags & XDF_IGNORE_WHITESPACE) - ; /* already handled */ - else if (flags & XDF_IGNORE_WHITESPACE_CHANGE - && !at_eol) { - ha += (ha << 5); - ha ^= (unsigned long) ' '; - } - else if (flags & XDF_IGNORE_WHITESPACE_AT_EOL - && !at_eol) { - while (ptr2 != ptr + 1) { - ha += (ha << 5); - ha ^= (unsigned long) *ptr2; - ptr2++; - } - } - continue; - } - ha += (ha << 5); - ha ^= (unsigned long) *ptr; - } - *data = ptr < top ? ptr + 1: ptr; - - return ha; -} - -unsigned long xdl_hash_record(char const **data, char const *top, long flags) { - unsigned long ha = 5381; - char const *ptr = *data; - - if (flags & XDF_WHITESPACE_FLAGS) - return xdl_hash_record_with_whitespace(data, top, flags); - - for (; ptr < top && *ptr != '\n'; ptr++) { - ha += (ha << 5); - ha ^= (unsigned long) *ptr; - } - *data = ptr < top ? ptr + 1: ptr; - - return ha; -} - -unsigned int xdl_hashbits(unsigned int size) { - unsigned int val = 1, bits = 0; - - for (; val < size && bits < CHAR_BIT * sizeof(unsigned int); val <<= 1, bits++); - return bits ? bits: 1; -} - - -int xdl_num_out(char *out, long val) { - char *ptr, *str = out; - char buf[32]; - - ptr = buf + sizeof(buf) - 1; - *ptr = '\0'; - if (val < 0) { - *--ptr = '-'; - val = -val; - } - for (; val && ptr > buf; val /= 10) - *--ptr = "0123456789"[val % 10]; - if (*ptr) - for (; *ptr; ptr++, str++) - *str = *ptr; - else - *str++ = '0'; - *str = '\0'; - - return str - out; -} - -static int xdl_format_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, - xdemitcb_t *ecb) { - int nb = 0; - mmbuffer_t mb; - char buf[128]; - - memcpy(buf, "@@ -", 4); - nb += 4; - - nb += xdl_num_out(buf + nb, c1 ? s1: s1 - 1); - - if (c1 != 1) { - memcpy(buf + nb, ",", 1); - nb += 1; - - nb += xdl_num_out(buf + nb, c1); - } - - memcpy(buf + nb, " +", 2); - nb += 2; - - nb += xdl_num_out(buf + nb, c2 ? s2: s2 - 1); - - if (c2 != 1) { - memcpy(buf + nb, ",", 1); - nb += 1; - - nb += xdl_num_out(buf + nb, c2); - } - - memcpy(buf + nb, " @@", 3); - nb += 3; - if (func && funclen) { - buf[nb++] = ' '; - if (funclen > sizeof(buf) - nb - 1) - funclen = sizeof(buf) - nb - 1; - memcpy(buf + nb, func, funclen); - nb += funclen; - } - buf[nb++] = '\n'; - - mb.ptr = buf; - mb.size = nb; - if (ecb->out_line(ecb->priv, &mb, 1) < 0) - return -1; - return 0; -} - -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, - xdemitcb_t *ecb) { - if (!ecb->out_hunk) - return xdl_format_hunk_hdr(s1, c1, s2, c2, func, funclen, ecb); - if (ecb->out_hunk(ecb->priv, - c1 ? s1 : s1 - 1, c1, - c2 ? s2 : s2 - 1, c2, - func, funclen) < 0) - return -1; - return 0; -} - -int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, - int line1, int count1, int line2, int count2) -{ - /* - * This probably does not work outside Git, since - * we have a very simple mmfile structure. - * - * Note: ideally, we would reuse the prepared environment, but - * the libxdiff interface does not (yet) allow for diffing only - * ranges of lines instead of the whole files. - */ - mmfile_t subfile1, subfile2; - xdfenv_t env; - - subfile1.ptr = (char *)diff_env->xdf1.recs[line1 - 1]->ptr; - subfile1.size = diff_env->xdf1.recs[line1 + count1 - 2]->ptr + - diff_env->xdf1.recs[line1 + count1 - 2]->size - subfile1.ptr; - subfile2.ptr = (char *)diff_env->xdf2.recs[line2 - 1]->ptr; - subfile2.size = diff_env->xdf2.recs[line2 + count2 - 2]->ptr + - diff_env->xdf2.recs[line2 + count2 - 2]->size - subfile2.ptr; - if (xdl_do_diff(&subfile1, &subfile2, xpp, &env) < 0) - return -1; - - memcpy(diff_env->xdf1.rchg + line1 - 1, env.xdf1.rchg, count1); - memcpy(diff_env->xdf2.rchg + line2 - 1, env.xdf2.rchg, count2); - - xdl_free_env(&env); - - return 0; -} diff --git a/src/libgit2/xdiff/xutils.h b/src/libgit2/xdiff/xutils.h deleted file mode 100644 index fba7bae03..000000000 --- a/src/libgit2/xdiff/xutils.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * LibXDiff by Davide Libenzi ( File Differential Library ) - * Copyright (C) 2003 Davide Libenzi - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, see - * <http://www.gnu.org/licenses/>. - * - * Davide Libenzi <davidel@xmailserver.org> - * - */ - -#if !defined(XUTILS_H) -#define XUTILS_H - - - -long xdl_bogosqrt(long n); -int xdl_emit_diffrec(char const *rec, long size, char const *pre, long psize, - xdemitcb_t *ecb); -int xdl_cha_init(chastore_t *cha, long isize, long icount); -void xdl_cha_free(chastore_t *cha); -void *xdl_cha_alloc(chastore_t *cha); -long xdl_guess_lines(mmfile_t *mf, long sample); -int xdl_blankline(const char *line, long size, long flags); -int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags); -unsigned long xdl_hash_record(char const **data, char const *top, long flags); -unsigned int xdl_hashbits(unsigned int size); -int xdl_num_out(char *out, long val); -int xdl_emit_hunk_hdr(long s1, long c1, long s2, long c2, - const char *func, long funclen, xdemitcb_t *ecb); -int xdl_fall_back_diff(xdfenv_t *diff_env, xpparam_t const *xpp, - int line1, int count1, int line2, int count2); - - - -#endif /* #if !defined(XUTILS_H) */ |