ident.c 3.26 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
#include "git2/sys/filter.h"
#include "filter.h"
12
#include "str.h"
Russell Belfer committed
13

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

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

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

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

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

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

	*id_end = found + 1;
	return 0;
}

static int ident_insert_id(
43
	git_str *to, const git_str *from, const git_filter_source *src)
Russell Belfer committed
44 45 46 47 48 49 50 51
{
	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
52
		return GIT_PASSTHROUGH;
Russell Belfer committed
53 54 55 56

	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
57
		return GIT_PASSTHROUGH;
Russell Belfer committed
58 59

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

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

66 67 68 69 70
	git_str_set(to, from->ptr, (size_t)(id_start - from->ptr));
	git_str_put(to, "$Id: ", 5);
	git_str_put(to, oid, GIT_OID_HEXSZ);
	git_str_put(to, " $", 2);
	git_str_put(to, id_end, (size_t)(from_end - id_end));
Russell Belfer committed
71

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

static int ident_remove_id(
76
	git_str *to, const git_str *from)
Russell Belfer committed
77 78 79 80 81
{
	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
82
		return GIT_PASSTHROUGH;
Russell Belfer committed
83 84 85 86

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

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

90 91 92
	git_str_set(to, from->ptr, (size_t)(id_start - from->ptr));
	git_str_put(to, "$Id$", 4);
	git_str_put(to, id_end, (size_t)(from_end - id_end));
Russell Belfer committed
93

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

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

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

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

116 117 118 119 120 121 122 123 124 125 126
static int ident_stream(
	git_writestream **out,
	git_filter *self,
	void **payload,
	const git_filter_source *src,
	git_writestream *next)
{
	return git_filter_buffered_stream_new(out,
		self, ident_apply, NULL, payload, src, next);
}

Russell Belfer committed
127 128 129
git_filter *git_ident_filter_new(void)
{
	git_filter *f = git__calloc(1, sizeof(git_filter));
Jacques Germishuys committed
130 131
	if (f == NULL)
		return NULL;
Russell Belfer committed
132 133 134 135

	f->version = GIT_FILTER_VERSION;
	f->attributes = "+ident"; /* apply to files with ident attribute set */
	f->shutdown = git_filter_free;
136
	f->stream   = ident_stream;
Russell Belfer committed
137 138 139

	return f;
}