]>
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.
8 #include "git2/sys/filter.h"
13 static int ident_find_id(
14 const char **id_start
, const char **id_end
, const char *start
, size_t len
)
16 const char *end
= start
+ len
, *found
= NULL
;
18 while (len
> 3 && (found
= memchr(start
, '$', len
)) != NULL
) {
19 size_t remaining
= (size_t)(end
- found
) - 1;
26 if (start
[0] == 'I' && start
[1] == 'd')
30 if (len
< 3 || !found
)
34 if ((found
= memchr(start
+ 2, '$', len
- 2)) == NULL
)
41 static int ident_insert_id(
42 git_buf
*to
, const git_buf
*from
, const git_filter_source
*src
)
44 char oid
[GIT_OID_HEXSZ
+1];
45 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
48 /* replace $Id$ with blob id */
50 if (!git_filter_source_id(src
))
51 return GIT_PASSTHROUGH
;
53 git_oid_tostr(oid
, sizeof(oid
), git_filter_source_id(src
));
55 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
56 return GIT_PASSTHROUGH
;
58 need_size
= (size_t)(id_start
- from
->ptr
) +
59 5 /* "$Id: " */ + GIT_OID_HEXSZ
+ 1 /* "$" */ +
60 (size_t)(from_end
- id_end
);
62 if (git_buf_grow(to
, need_size
) < 0)
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
));
71 return git_buf_oom(to
) ? -1 : 0;
74 static int ident_remove_id(
75 git_buf
*to
, const git_buf
*from
)
77 const char *id_start
, *id_end
, *from_end
= from
->ptr
+ from
->size
;
80 if (ident_find_id(&id_start
, &id_end
, from
->ptr
, from
->size
) < 0)
81 return GIT_PASSTHROUGH
;
83 need_size
= (size_t)(id_start
- from
->ptr
) +
84 4 /* "$Id$" */ + (size_t)(from_end
- id_end
);
86 if (git_buf_grow(to
, need_size
) < 0)
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
));
93 return git_buf_oom(to
) ? -1 : 0;
96 static int ident_apply(
101 const git_filter_source
*src
)
103 GIT_UNUSED(self
); GIT_UNUSED(payload
);
105 /* Don't filter binary files */
106 if (git_buf_text_is_binary(from
))
107 return GIT_PASSTHROUGH
;
109 if (git_filter_source_mode(src
) == GIT_FILTER_SMUDGE
)
110 return ident_insert_id(to
, from
, src
);
112 return ident_remove_id(to
, from
);
115 git_filter
*git_ident_filter_new(void)
117 git_filter
*f
= git__calloc(1, sizeof(git_filter
));
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
;