| 1 |
|
| /* |
| 2 |
|
| * Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
|
| * |
| 4 |
|
| * This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
|
| * a Linking Exception. For full terms see the included COPYING file. |
| 6 |
|
| */ |
| 7 |
|
| |
| 8 |
|
| #include "common.h" |
| 9 |
|
| |
| 10 |
|
| #include "git2/sys/filter.h" |
| 11 |
|
| #include "filter.h" |
| 12 |
|
| #include "str.h" |
| 13 |
|
| |
| 14 |
0 |
| static int ident_find_id( |
| 15 |
|
| const char **id_start, const char **id_end, const char *start, size_t len) |
| 16 |
|
| { |
| 17 |
0 |
| const char *end = start + len, *found = NULL; |
| 18 |
|
| |
| 19 |
0 |
▶ | while (len > 3 && (found = memchr(start, '$', len)) != NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (len > 3 && (found = memchr(start, '$', len)) != NULL) { |
0 |
0 |
0 |
| while (len > 3 && (found = memchr(start, '$', len)) != NULL) { |
0 |
0 |
0 |
|
| 20 |
0 |
| size_t remaining = (size_t)(end - found) - 1; |
| 21 |
0 |
▶ | if (remaining < 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (remaining < 3) |
0 |
0 |
0 |
|
| 22 |
0 |
| return GIT_ENOTFOUND; |
| 23 |
|
| |
| 24 |
0 |
| start = found + 1; |
| 25 |
0 |
| len = remaining; |
| 26 |
|
| |
| 27 |
0 |
▶ | if (start[0] == 'I' && start[1] == 'd') |
Branch: 2 branches
| Branch | True | False | Total |
| if (start[0] == 'I' && start[1] == 'd') |
0 |
0 |
0 |
| if (start[0] == 'I' && start[1] == 'd') |
0 |
0 |
0 |
|
| 28 |
0 |
| break; |
| 29 |
|
| } |
| 30 |
|
| |
| 31 |
0 |
▶ | if (len < 3 || !found) |
Branch: 2 branches
| Branch | True | False | Total |
| if (len < 3 || !found) |
0 |
0 |
0 |
| if (len < 3 || !found) |
0 |
0 |
0 |
|
| 32 |
0 |
| return GIT_ENOTFOUND; |
| 33 |
0 |
| *id_start = found; |
| 34 |
|
| |
| 35 |
0 |
▶ | if ((found = memchr(start + 2, '$', len - 2)) == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((found = memchr(start + 2, '$', len - 2)) == NULL) |
0 |
0 |
0 |
|
| 36 |
0 |
| return GIT_ENOTFOUND; |
| 37 |
|
| |
| 38 |
0 |
| *id_end = found + 1; |
| 39 |
0 |
| return 0; |
| 40 |
|
| } |
| 41 |
|
| |
| 42 |
0 |
| static int ident_insert_id( |
| 43 |
|
| git_str *to, const git_str *from, const git_filter_source *src) |
| 44 |
|
| { |
| 45 |
|
| char oid[GIT_OID_HEXSZ+1]; |
| 46 |
0 |
| const char *id_start, *id_end, *from_end = from->ptr + from->size; |
| 47 |
|
| size_t need_size; |
| 48 |
|
| |
| 49 |
|
| /* replace $Id$ with blob id */ |
| 50 |
|
| |
| 51 |
0 |
▶ | if (!git_filter_source_id(src)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!git_filter_source_id(src)) |
0 |
0 |
0 |
|
| 52 |
0 |
| return GIT_PASSTHROUGH; |
| 53 |
|
| |
| 54 |
0 |
| git_oid_tostr(oid, sizeof(oid), git_filter_source_id(src)); |
| 55 |
|
| |
| 56 |
0 |
▶ | if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) |
0 |
0 |
0 |
|
| 57 |
0 |
| return GIT_PASSTHROUGH; |
| 58 |
|
| |
| 59 |
0 |
| need_size = (size_t)(id_start - from->ptr) + |
| 60 |
0 |
| 5 /* "$Id: " */ + GIT_OID_HEXSZ + 2 /* " $" */ + |
| 61 |
0 |
| (size_t)(from_end - id_end); |
| 62 |
|
| |
| 63 |
0 |
▶ | if (git_str_grow(to, need_size) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_grow(to, need_size) < 0) |
0 |
0 |
0 |
|
| 64 |
0 |
| return -1; |
| 65 |
|
| |
| 66 |
0 |
| git_str_set(to, from->ptr, (size_t)(id_start - from->ptr)); |
| 67 |
0 |
| git_str_put(to, "$Id: ", 5); |
| 68 |
0 |
| git_str_put(to, oid, GIT_OID_HEXSZ); |
| 69 |
0 |
| git_str_put(to, " $", 2); |
| 70 |
0 |
| git_str_put(to, id_end, (size_t)(from_end - id_end)); |
| 71 |
|
| |
| 72 |
0 |
▶ | return git_str_oom(to) ? -1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return git_str_oom(to) ? -1 : 0; |
0 |
0 |
0 |
|
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
0 |
| static int ident_remove_id( |
| 76 |
|
| git_str *to, const git_str *from) |
| 77 |
|
| { |
| 78 |
0 |
| const char *id_start, *id_end, *from_end = from->ptr + from->size; |
| 79 |
|
| size_t need_size; |
| 80 |
|
| |
| 81 |
0 |
▶ | if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) |
0 |
0 |
0 |
|
| 82 |
0 |
| return GIT_PASSTHROUGH; |
| 83 |
|
| |
| 84 |
0 |
| need_size = (size_t)(id_start - from->ptr) + |
| 85 |
0 |
| 4 /* "$Id$" */ + (size_t)(from_end - id_end); |
| 86 |
|
| |
| 87 |
0 |
▶ | if (git_str_grow(to, need_size) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_grow(to, need_size) < 0) |
0 |
0 |
0 |
|
| 88 |
0 |
| return -1; |
| 89 |
|
| |
| 90 |
0 |
| git_str_set(to, from->ptr, (size_t)(id_start - from->ptr)); |
| 91 |
0 |
| git_str_put(to, "$Id$", 4); |
| 92 |
0 |
| git_str_put(to, id_end, (size_t)(from_end - id_end)); |
| 93 |
|
| |
| 94 |
0 |
▶ | return git_str_oom(to) ? -1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return git_str_oom(to) ? -1 : 0; |
0 |
0 |
0 |
|
| 95 |
|
| } |
| 96 |
|
| |
| 97 |
0 |
| static int ident_apply( |
| 98 |
|
| git_filter *self, |
| 99 |
|
| void **payload, |
| 100 |
|
| git_str *to, |
| 101 |
|
| const git_str *from, |
| 102 |
|
| const git_filter_source *src) |
| 103 |
|
| { |
| 104 |
0 |
| GIT_UNUSED(self); GIT_UNUSED(payload); |
| 105 |
|
| |
| 106 |
|
| /* Don't filter binary files */ |
| 107 |
0 |
▶ | if (git_str_is_binary(from)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_str_is_binary(from)) |
0 |
0 |
0 |
|
| 108 |
0 |
| return GIT_PASSTHROUGH; |
| 109 |
|
| |
| 110 |
0 |
▶ | if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) |
0 |
0 |
0 |
|
| 111 |
0 |
| return ident_insert_id(to, from, src); |
| 112 |
|
| else |
| 113 |
0 |
| return ident_remove_id(to, from); |
| 114 |
|
| } |
| 115 |
|
| |
| 116 |
0 |
| static int ident_stream( |
| 117 |
|
| git_writestream **out, |
| 118 |
|
| git_filter *self, |
| 119 |
|
| void **payload, |
| 120 |
|
| const git_filter_source *src, |
| 121 |
|
| git_writestream *next) |
| 122 |
|
| { |
| 123 |
0 |
| return git_filter_buffered_stream_new(out, |
| 124 |
|
| self, ident_apply, NULL, payload, src, next); |
| 125 |
|
| } |
| 126 |
|
| |
| 127 |
87 |
| git_filter *git_ident_filter_new(void) |
| 128 |
|
| { |
| 129 |
87 |
| git_filter *f = git__calloc(1, sizeof(git_filter)); |
| 130 |
87 |
▶ | if (f == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f == NULL) |
0 |
87 |
87 |
|
| 131 |
0 |
| return NULL; |
| 132 |
|
| |
| 133 |
87 |
| f->version = GIT_FILTER_VERSION; |
| 134 |
87 |
| f->attributes = "+ident"; /* apply to files with ident attribute set */ |
| 135 |
87 |
| f->shutdown = git_filter_free; |
| 136 |
87 |
| f->stream = ident_stream; |
| 137 |
|
| |
| 138 |
87 |
| return f; |
| 139 |
|
| } |