ident.c 3.05 KB
Newer Older
Russell Belfer committed
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "common.h"

Russell Belfer committed
10 11 12
#include "git2/sys/filter.h"
#include "filter.h"
#include "buffer.h"
13
#include "buf_text.h"
Russell Belfer committed
14

15
static int ident_find_id(
Russell Belfer committed
16 17
	const char **id_start, const char **id_end, const char *start, size_t len)
{
Russell Belfer committed
18
	const char *end = start + len, *found = NULL;
Russell Belfer committed
19

Russell Belfer committed
20 21
	while (len > 3 && (found = memchr(start, '$', len)) != NULL) {
		size_t remaining = (size_t)(end - found) - 1;
Russell Belfer committed
22 23
		if (remaining < 3)
			return GIT_ENOTFOUND;
Russell Belfer committed
24

Russell Belfer committed
25
		start = found + 1;
Russell Belfer committed
26 27 28 29
		len   = remaining;

		if (start[0] == 'I' && start[1] == 'd')
			break;
Russell Belfer committed
30 31
	}

Russell Belfer committed
32
	if (len < 3 || !found)
Russell Belfer committed
33 34 35
		return GIT_ENOTFOUND;
	*id_start = found;

Russell Belfer committed
36
	if ((found = memchr(start + 2, '$', len - 2)) == NULL)
Russell Belfer committed
37 38 39 40 41 42 43
		return GIT_ENOTFOUND;

	*id_end = found + 1;
	return 0;
}

static int ident_insert_id(
44
	git_buf *to, const git_buf *from, const git_filter_source *src)
Russell Belfer committed
45 46 47 48 49 50 51 52
{
	char oid[GIT_OID_HEXSZ+1];
	const char *id_start, *id_end, *from_end = from->ptr + from->size;
	size_t need_size;

	/* replace $Id$ with blob id */

	if (!git_filter_source_id(src))
Russell Belfer committed
53
		return GIT_PASSTHROUGH;
Russell Belfer committed
54 55 56 57

	git_oid_tostr(oid, sizeof(oid), git_filter_source_id(src));

	if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
Russell Belfer committed
58
		return GIT_PASSTHROUGH;
Russell Belfer committed
59 60

	need_size = (size_t)(id_start - from->ptr) +
61
		5 /* "$Id: " */ + GIT_OID_HEXSZ + 2 /* " $" */ +
Russell Belfer committed
62 63
		(size_t)(from_end - id_end);

64
	if (git_buf_grow(to, need_size) < 0)
Russell Belfer committed
65 66
		return -1;

67 68 69
	git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
	git_buf_put(to, "$Id: ", 5);
	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));
Russell Belfer committed
72

73
	return git_buf_oom(to) ? -1 : 0;
Russell Belfer committed
74 75 76
}

static int ident_remove_id(
77
	git_buf *to, const git_buf *from)
Russell Belfer committed
78 79 80 81 82
{
	const char *id_start, *id_end, *from_end = from->ptr + from->size;
	size_t need_size;

	if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
Russell Belfer committed
83
		return GIT_PASSTHROUGH;
Russell Belfer committed
84 85 86 87

	need_size = (size_t)(id_start - from->ptr) +
		4 /* "$Id$" */ + (size_t)(from_end - id_end);

88
	if (git_buf_grow(to, need_size) < 0)
Russell Belfer committed
89 90
		return -1;

91 92 93
	git_buf_set(to, from->ptr, (size_t)(id_start - from->ptr));
	git_buf_put(to, "$Id$", 4);
	git_buf_put(to, id_end, (size_t)(from_end - id_end));
Russell Belfer committed
94

95
	return git_buf_oom(to) ? -1 : 0;
Russell Belfer committed
96 97 98
}

static int ident_apply(
99 100 101 102
	git_filter     *self,
	void          **payload,
	git_buf        *to,
	const git_buf  *from,
Russell Belfer committed
103 104 105 106
	const git_filter_source *src)
{
	GIT_UNUSED(self); GIT_UNUSED(payload);

107 108
	/* Don't filter binary files */
	if (git_buf_text_is_binary(from))
Russell Belfer committed
109
		return GIT_PASSTHROUGH;
110

Russell Belfer committed
111 112 113 114 115 116 117 118 119
	if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
		return ident_insert_id(to, from, src);
	else
		return ident_remove_id(to, from);
}

git_filter *git_ident_filter_new(void)
{
	git_filter *f = git__calloc(1, sizeof(git_filter));
Jacques Germishuys committed
120 121
	if (f == NULL)
		return NULL;
Russell Belfer committed
122 123 124 125 126 127 128 129

	f->version = GIT_FILTER_VERSION;
	f->attributes = "+ident"; /* apply to files with ident attribute set */
	f->shutdown = git_filter_free;
	f->apply    = ident_apply;

	return f;
}