]>
Commit | Line | Data |
---|---|---|
4b11f25a RB |
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 "git2/sys/filter.h" | |
9 | #include "filter.h" | |
10 | #include "buffer.h" | |
eab3746b | 11 | #include "buf_text.h" |
4b11f25a | 12 | |
a9f51e43 | 13 | static int ident_find_id( |
4b11f25a RB |
14 | const char **id_start, const char **id_end, const char *start, size_t len) |
15 | { | |
eefc32d5 | 16 | const char *end = start + len, *found = NULL; |
4b11f25a | 17 | |
eefc32d5 RB |
18 | while (len > 3 && (found = memchr(start, '$', len)) != NULL) { |
19 | size_t remaining = (size_t)(end - found) - 1; | |
4b11f25a RB |
20 | if (remaining < 3) |
21 | return GIT_ENOTFOUND; | |
eefc32d5 | 22 | |
4b11f25a | 23 | start = found + 1; |
eefc32d5 RB |
24 | len = remaining; |
25 | ||
26 | if (start[0] == 'I' && start[1] == 'd') | |
27 | break; | |
4b11f25a RB |
28 | } |
29 | ||
eefc32d5 | 30 | if (len < 3 || !found) |
4b11f25a RB |
31 | return GIT_ENOTFOUND; |
32 | *id_start = found; | |
33 | ||
eefc32d5 | 34 | if ((found = memchr(start + 2, '$', len - 2)) == NULL) |
4b11f25a RB |
35 | return GIT_ENOTFOUND; |
36 | ||
37 | *id_end = found + 1; | |
38 | return 0; | |
39 | } | |
40 | ||
41 | static int ident_insert_id( | |
a9f51e43 | 42 | git_buf *to, const git_buf *from, const git_filter_source *src) |
4b11f25a RB |
43 | { |
44 | char oid[GIT_OID_HEXSZ+1]; | |
45 | const char *id_start, *id_end, *from_end = from->ptr + from->size; | |
46 | size_t need_size; | |
4b11f25a RB |
47 | |
48 | /* replace $Id$ with blob id */ | |
49 | ||
50 | if (!git_filter_source_id(src)) | |
eefc32d5 | 51 | return GIT_PASSTHROUGH; |
4b11f25a RB |
52 | |
53 | git_oid_tostr(oid, sizeof(oid), git_filter_source_id(src)); | |
54 | ||
55 | if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) | |
eefc32d5 | 56 | return GIT_PASSTHROUGH; |
4b11f25a RB |
57 | |
58 | need_size = (size_t)(id_start - from->ptr) + | |
1ecbcd8e | 59 | 5 /* "$Id: " */ + GIT_OID_HEXSZ + 2 /* " $" */ + |
4b11f25a RB |
60 | (size_t)(from_end - id_end); |
61 | ||
a9f51e43 | 62 | if (git_buf_grow(to, need_size) < 0) |
4b11f25a RB |
63 | return -1; |
64 | ||
a9f51e43 RB |
65 | git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr)); |
66 | git_buf_put(to, "$Id: ", 5); | |
67 | git_buf_put(to, oid, GIT_OID_HEXSZ); | |
1ecbcd8e | 68 | git_buf_put(to, " $", 2); |
a9f51e43 | 69 | git_buf_put(to, id_end, (size_t)(from_end - id_end)); |
4b11f25a | 70 | |
a9f51e43 | 71 | return git_buf_oom(to) ? -1 : 0; |
4b11f25a RB |
72 | } |
73 | ||
74 | static int ident_remove_id( | |
a9f51e43 | 75 | git_buf *to, const git_buf *from) |
4b11f25a RB |
76 | { |
77 | const char *id_start, *id_end, *from_end = from->ptr + from->size; | |
78 | size_t need_size; | |
4b11f25a RB |
79 | |
80 | if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0) | |
eefc32d5 | 81 | return GIT_PASSTHROUGH; |
4b11f25a RB |
82 | |
83 | need_size = (size_t)(id_start - from->ptr) + | |
84 | 4 /* "$Id$" */ + (size_t)(from_end - id_end); | |
85 | ||
a9f51e43 | 86 | if (git_buf_grow(to, need_size) < 0) |
4b11f25a RB |
87 | return -1; |
88 | ||
a9f51e43 RB |
89 | git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr)); |
90 | git_buf_put(to, "$Id$", 4); | |
91 | git_buf_put(to, id_end, (size_t)(from_end - id_end)); | |
4b11f25a | 92 | |
a9f51e43 | 93 | return git_buf_oom(to) ? -1 : 0; |
4b11f25a RB |
94 | } |
95 | ||
96 | static int ident_apply( | |
a9f51e43 RB |
97 | git_filter *self, |
98 | void **payload, | |
99 | git_buf *to, | |
100 | const git_buf *from, | |
4b11f25a RB |
101 | const git_filter_source *src) |
102 | { | |
103 | GIT_UNUSED(self); GIT_UNUSED(payload); | |
104 | ||
eab3746b RB |
105 | /* Don't filter binary files */ |
106 | if (git_buf_text_is_binary(from)) | |
eefc32d5 | 107 | return GIT_PASSTHROUGH; |
eab3746b | 108 | |
4b11f25a RB |
109 | if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) |
110 | return ident_insert_id(to, from, src); | |
111 | else | |
112 | return ident_remove_id(to, from); | |
113 | } | |
114 | ||
115 | git_filter *git_ident_filter_new(void) | |
116 | { | |
117 | git_filter *f = git__calloc(1, sizeof(git_filter)); | |
dfda1cf5 JG |
118 | if (f == NULL) |
119 | return NULL; | |
4b11f25a RB |
120 | |
121 | f->version = GIT_FILTER_VERSION; | |
122 | f->attributes = "+ident"; /* apply to files with ident attribute set */ | |
123 | f->shutdown = git_filter_free; | |
124 | f->apply = ident_apply; | |
125 | ||
126 | return f; | |
127 | } |