]>
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"
15 static int ident_find_id(
16 const char **id_start
, const char **id_end
, const char *start
, size_t len
)
18 const char *end
= start
+ len
, *found
= NULL
;
20 while (len
> 3 && (found
= memchr(start
, '$', len
)) != NULL
) {
21 size_t remaining
= (size_t)(end
- found
) - 1;
28 if (start
[0] == 'I' && start
[1] == 'd')
32 if (len
< 3 || !found
)
36 if ((found
= memchr(start
+ 2, '$', len
- 2)) == NULL
)
43 static int ident_insert_id(
44 git_buf
*to
, const git_buf
*from
, const git_filter_source
*src
)
46 char oid
[GIT_OID_HEXSZ
+1];
47 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
50 /* replace $Id$ with blob id */
52 if (!git_filter_source_id(src
))
53 return GIT_PASSTHROUGH
;
55 git_oid_tostr(oid
, sizeof(oid
), git_filter_source_id(src
));
57 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
58 return GIT_PASSTHROUGH
;
60 need_size
= (size_t)(id_start
- from
->ptr
) +
61 5 /* "$Id: " */ + GIT_OID_HEXSZ
+ 2 /* " $" */ +
62 (size_t)(from_end
- id_end
);
64 if (git_buf_grow(to
, need_size
) < 0)
67 git_buf_set(to
, from
->ptr
, (size_t)(id_start
- from
->ptr
));
68 git_buf_put(to
, "$Id: ", 5);
69 git_buf_put(to
, oid
, GIT_OID_HEXSZ
);
70 git_buf_put(to
, " $", 2);
71 git_buf_put(to
, id_end
, (size_t)(from_end
- id_end
));
73 return git_buf_oom(to
) ? -1 : 0;
76 static int ident_remove_id(
77 git_buf
*to
, const git_buf
*from
)
79 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
82 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
83 return GIT_PASSTHROUGH
;
85 need_size
= (size_t)(id_start
- from
->ptr
) +
86 4 /* "$Id$" */ + (size_t)(from_end
- id_end
);
88 if (git_buf_grow(to
, need_size
) < 0)
91 git_buf_set(to
, from
->ptr
, (size_t)(id_start
- from
->ptr
));
92 git_buf_put(to
, "$Id$", 4);
93 git_buf_put(to
, id_end
, (size_t)(from_end
- id_end
));
95 return git_buf_oom(to
) ? -1 : 0;
98 static int ident_apply(
103 const git_filter_source
*src
)
105 GIT_UNUSED(self
); GIT_UNUSED(payload
);
107 /* Don't filter binary files */
108 if (git_buf_text_is_binary(from
))
109 return GIT_PASSTHROUGH
;
111 if (git_filter_source_mode(src
) == GIT_FILTER_SMUDGE
)
112 return ident_insert_id(to
, from
, src
);
114 return ident_remove_id(to
, from
);
117 git_filter
*git_ident_filter_new(void)
119 git_filter
*f
= git__calloc(1, sizeof(git_filter
));
123 f
->version
= GIT_FILTER_VERSION
;
124 f
->attributes
= "+ident"; /* apply to files with ident attribute set */
125 f
->shutdown
= git_filter_free
;
126 f
->apply
= ident_apply
;