]> git.proxmox.com Git - libgit2.git/blob - src/ident.c
Merge branch 'pr/2740'
[libgit2.git] / src / ident.c
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"
11 #include "buf_text.h"
12
13 static int ident_find_id(
14 const char **id_start, const char **id_end, const char *start, size_t len)
15 {
16 const char *end = start + len, *found = NULL;
17
18 while (len > 3 && (found = memchr(start, '$', len)) != NULL) {
19 size_t remaining = (size_t)(end - found) - 1;
20 if (remaining < 3)
21 return GIT_ENOTFOUND;
22
23 start = found + 1;
24 len = remaining;
25
26 if (start[0] == 'I' && start[1] == 'd')
27 break;
28 }
29
30 if (len < 3 || !found)
31 return GIT_ENOTFOUND;
32 *id_start = found;
33
34 if ((found = memchr(start + 2, '$', len - 2)) == NULL)
35 return GIT_ENOTFOUND;
36
37 *id_end = found + 1;
38 return 0;
39 }
40
41 static int ident_insert_id(
42 git_buf *to, const git_buf *from, const git_filter_source *src)
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;
47
48 /* replace $Id$ with blob id */
49
50 if (!git_filter_source_id(src))
51 return GIT_PASSTHROUGH;
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)
56 return GIT_PASSTHROUGH;
57
58 need_size = (size_t)(id_start - from->ptr) +
59 5 /* "$Id: " */ + GIT_OID_HEXSZ + 1 /* "$" */ +
60 (size_t)(from_end - id_end);
61
62 if (git_buf_grow(to, need_size) < 0)
63 return -1;
64
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);
68 git_buf_putc(to, '$');
69 git_buf_put(to, id_end, (size_t)(from_end - id_end));
70
71 return git_buf_oom(to) ? -1 : 0;
72 }
73
74 static int ident_remove_id(
75 git_buf *to, const git_buf *from)
76 {
77 const char *id_start, *id_end, *from_end = from->ptr + from->size;
78 size_t need_size;
79
80 if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
81 return GIT_PASSTHROUGH;
82
83 need_size = (size_t)(id_start - from->ptr) +
84 4 /* "$Id$" */ + (size_t)(from_end - id_end);
85
86 if (git_buf_grow(to, need_size) < 0)
87 return -1;
88
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));
92
93 return git_buf_oom(to) ? -1 : 0;
94 }
95
96 static int ident_apply(
97 git_filter *self,
98 void **payload,
99 git_buf *to,
100 const git_buf *from,
101 const git_filter_source *src)
102 {
103 GIT_UNUSED(self); GIT_UNUSED(payload);
104
105 /* Don't filter binary files */
106 if (git_buf_text_is_binary(from))
107 return GIT_PASSTHROUGH;
108
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));
118 if (f == NULL)
119 return NULL;
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 }