]>
git.proxmox.com Git - libgit2.git/blob - src/ident.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
10 #include "git2/sys/filter.h"
14 static int ident_find_id(
15 const char **id_start
, const char **id_end
, const char *start
, size_t len
)
17 const char *end
= start
+ len
, *found
= NULL
;
19 while (len
> 3 && (found
= memchr(start
, '$', len
)) != NULL
) {
20 size_t remaining
= (size_t)(end
- found
) - 1;
27 if (start
[0] == 'I' && start
[1] == 'd')
31 if (len
< 3 || !found
)
35 if ((found
= memchr(start
+ 2, '$', len
- 2)) == NULL
)
42 static int ident_insert_id(
43 git_str
*to
, const git_str
*from
, const git_filter_source
*src
)
45 char oid
[GIT_OID_HEXSZ
+1];
46 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
49 /* replace $Id$ with blob id */
51 if (!git_filter_source_id(src
))
52 return GIT_PASSTHROUGH
;
54 git_oid_tostr(oid
, sizeof(oid
), git_filter_source_id(src
));
56 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
57 return GIT_PASSTHROUGH
;
59 need_size
= (size_t)(id_start
- from
->ptr
) +
60 5 /* "$Id: " */ + GIT_OID_HEXSZ
+ 2 /* " $" */ +
61 (size_t)(from_end
- id_end
);
63 if (git_str_grow(to
, need_size
) < 0)
66 git_str_set(to
, from
->ptr
, (size_t)(id_start
- from
->ptr
));
67 git_str_put(to
, "$Id: ", 5);
68 git_str_put(to
, oid
, GIT_OID_HEXSZ
);
69 git_str_put(to
, " $", 2);
70 git_str_put(to
, id_end
, (size_t)(from_end
- id_end
));
72 return git_str_oom(to
) ? -1 : 0;
75 static int ident_remove_id(
76 git_str
*to
, const git_str
*from
)
78 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
81 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
82 return GIT_PASSTHROUGH
;
84 need_size
= (size_t)(id_start
- from
->ptr
) +
85 4 /* "$Id$" */ + (size_t)(from_end
- id_end
);
87 if (git_str_grow(to
, need_size
) < 0)
90 git_str_set(to
, from
->ptr
, (size_t)(id_start
- from
->ptr
));
91 git_str_put(to
, "$Id$", 4);
92 git_str_put(to
, id_end
, (size_t)(from_end
- id_end
));
94 return git_str_oom(to
) ? -1 : 0;
97 static int ident_apply(
102 const git_filter_source
*src
)
104 GIT_UNUSED(self
); GIT_UNUSED(payload
);
106 /* Don't filter binary files */
107 if (git_str_is_binary(from
))
108 return GIT_PASSTHROUGH
;
110 if (git_filter_source_mode(src
) == GIT_FILTER_SMUDGE
)
111 return ident_insert_id(to
, from
, src
);
113 return ident_remove_id(to
, from
);
116 static int ident_stream(
117 git_writestream
**out
,
120 const git_filter_source
*src
,
121 git_writestream
*next
)
123 return git_filter_buffered_stream_new(out
,
124 self
, ident_apply
, NULL
, payload
, src
, next
);
127 git_filter
*git_ident_filter_new(void)
129 git_filter
*f
= git__calloc(1, sizeof(git_filter
));
133 f
->version
= GIT_FILTER_VERSION
;
134 f
->attributes
= "+ident"; /* apply to files with ident attribute set */
135 f
->shutdown
= git_filter_free
;
136 f
->stream
= ident_stream
;