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