diff options
| author | Edward Thomson <ethomson@microsoft.com> | 2015-09-22 18:25:03 -0400 |
|---|---|---|
| committer | Edward Thomson <ethomson@github.com> | 2016-05-26 13:01:05 -0500 |
| commit | d68cb736776e0f2f9494b49e2da30a9c4b9fc2c7 (patch) | |
| tree | d8b8b7bffac2560bae167cb02dd3da8131e40d1e /src | |
| parent | e7ec327d4b94d8237f6238fb3d282bd3434b2b56 (diff) | |
| download | libgit2-d68cb736776e0f2f9494b49e2da30a9c4b9fc2c7.tar.gz | |
diff: include oid length in deltas
Now that `git_diff_delta` data can be produced by reading patch
file data, which may have an abbreviated oid, allow consumers to
know that the id is abbreviated.
Diffstat (limited to 'src')
| -rw-r--r-- | src/diff.c | 4 | ||||
| -rw-r--r-- | src/diff_file.c | 2 | ||||
| -rw-r--r-- | src/diff_print.c | 27 | ||||
| -rw-r--r-- | src/patch_parse.c | 12 |
4 files changed, 37 insertions, 8 deletions
diff --git a/src/diff.c b/src/diff.c index 26c0b895b..9c27511f6 100644 --- a/src/diff.c +++ b/src/diff.c @@ -151,11 +151,13 @@ static int diff_delta__from_one( delta->old_file.size = entry->file_size; delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; git_oid_cpy(&delta->old_file.id, &entry->id); + delta->old_file.id_abbrev = GIT_OID_HEXSZ; } 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_cpy(&delta->new_file.id, &entry->id); + delta->new_file.id_abbrev = GIT_OID_HEXSZ; } delta->old_file.flags |= GIT_DIFF_FLAG_VALID_ID; @@ -208,12 +210,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_HEXSZ; 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_HEXSZ; delta->new_file.size = new_entry->file_size; delta->new_file.mode = new_mode; delta->old_file.flags |= GIT_DIFF_FLAG_EXISTS; diff --git a/src/diff_file.c b/src/diff_file.c index ecc34cf55..8b945a5b7 100644 --- a/src/diff_file.c +++ b/src/diff_file.c @@ -149,12 +149,14 @@ int git_diff_file_content__init_from_src( if (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_HEXSZ; fc->map.len = (size_t)fc->file->size; fc->map.data = (char *)git_blob_rawcontent(src->blob); } else { fc->file->size = src->buflen; git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB); + fc->file->id_abbrev = GIT_OID_HEXSZ; fc->map.len = src->buflen; fc->map.data = (char *)src->buf; diff --git a/src/diff_print.c b/src/diff_print.c index 09bf77aef..7c9af2449 100644 --- a/src/diff_print.c +++ b/src/diff_print.c @@ -208,6 +208,7 @@ static int diff_print_one_raw( { diff_print_info *pi = data; git_buf *out = pi->buf; + int id_abbrev; char code = git_diff_status_char(delta->status); char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; @@ -218,6 +219,16 @@ static int diff_print_one_raw( git_buf_clear(out); + id_abbrev = delta->old_file.mode ? delta->old_file.id_abbrev : + delta->new_file.id_abbrev; + + if (pi->oid_strlen - 1 > id_abbrev) { + giterr_set(GITERR_PATCH, + "The patch input contains %d id characters (cannot print %d)", + id_abbrev, pi->oid_strlen); + return -1; + } + git_oid_tostr(start_oid, pi->oid_strlen, &delta->old_file.id); git_oid_tostr(end_oid, pi->oid_strlen, &delta->new_file.id); @@ -252,6 +263,22 @@ static int diff_print_oid_range( { char start_oid[GIT_OID_HEXSZ+1], end_oid[GIT_OID_HEXSZ+1]; + if (delta->old_file.mode && + oid_strlen - 1 > delta->old_file.id_abbrev) { + giterr_set(GITERR_PATCH, + "The patch input contains %d id characters (cannot print %d)", + delta->old_file.id_abbrev, oid_strlen); + return -1; + } + + if ((delta->new_file.mode && + oid_strlen - 1 > delta->new_file.id_abbrev)) { + giterr_set(GITERR_PATCH, + "The patch input contains %d id characters (cannot print %d)", + delta->new_file.id_abbrev, oid_strlen); + return -1; + } + git_oid_tostr(start_oid, oid_strlen, &delta->old_file.id); git_oid_tostr(end_oid, oid_strlen, &delta->new_file.id); diff --git a/src/patch_parse.c b/src/patch_parse.c index 11e26936c..323f8dc95 100644 --- a/src/patch_parse.c +++ b/src/patch_parse.c @@ -197,15 +197,11 @@ static int parse_header_oid( static int parse_header_git_index( git_patch_parsed *patch, patch_parse_ctx *ctx) { - /* - * TODO: we read the prefix provided in the diff into the delta's id - * field, but do not mark is at an abbreviated id. - */ - size_t oid_len, nid_len; - - if (parse_header_oid(&patch->base.delta->old_file.id, &oid_len, ctx) < 0 || + if (parse_header_oid(&patch->base.delta->old_file.id, + &patch->base.delta->old_file.id_abbrev, ctx) < 0 || parse_advance_expected(ctx, "..", 2) < 0 || - parse_header_oid(&patch->base.delta->new_file.id, &nid_len, ctx) < 0) + parse_header_oid(&patch->base.delta->new_file.id, + &patch->base.delta->new_file.id_abbrev, ctx) < 0) return -1; if (ctx->line_len > 0 && ctx->line[0] == ' ') { |
