filter.c 3.63 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Copyright (C) 2009-2012 the libgit2 contributors
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "common.h"
#include "fileops.h"
#include "hash.h"
#include "filter.h"
12 13
#include "repository.h"
#include "git2/config.h"
14

15
/* Tweaked from Core Git. I wonder what we could use this for... */
16
void git_text_gather_stats(git_text_stats *stats, const git_buf *text)
17 18 19 20 21
{
	size_t i;

	memset(stats, 0, sizeof(*stats));

nulltoken committed
22
	for (i = 0; i < git_buf_len(text); i++) {
23 24 25 26 27
		unsigned char c = text->ptr[i];

		if (c == '\r') {
			stats->cr++;

nulltoken committed
28
			if (i + 1 < git_buf_len(text) && text->ptr[i + 1] == '\n')
29 30 31
				stats->crlf++;
		}

32
		else if (c == '\n')
33 34
			stats->lf++;

35 36 37 38 39
		else if (c == 0x85)
			/* Unicode CR+LF */
			stats->crlf++;

		else if (c == 127)
40 41 42
			/* DEL */
			stats->nonprintable++;

43
		else if (c <= 0x1F || (c >= 0x80 && c <= 0x9F)) {
44 45 46 47 48 49 50 51 52 53 54 55
			switch (c) {
				/* BS, HT, ESC and FF */
			case '\b': case '\t': case '\033': case '\014':
				stats->printable++;
				break;
			case 0:
				stats->nul++;
				/* fall through */
			default:
				stats->nonprintable++;
			}
		}
56

57 58 59 60 61
		else
			stats->printable++;
	}

	/* If file ends with EOF then don't count this EOF as non-printable. */
nulltoken committed
62
	if (git_buf_len(text) >= 1 && text->ptr[text->size - 1] == '\032')
63 64 65 66 67 68
		stats->nonprintable--;
}

/*
 * Fresh from Core Git
 */
69
int git_text_is_binary(git_text_stats *stats)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
	if (stats->nul)
		return 1;

	if ((stats->printable >> 7) < stats->nonprintable)
		return 1;
	/*
	 * Other heuristics? Average line length might be relevant,
	 * as might LF vs CR vs CRLF counts..
	 *
	 * NOTE! It might be normal to have a low ratio of CRLF to LF
	 * (somebody starts with a LF-only file and edits it with an editor
	 * that adds CRLF only to lines that are added..). But do  we
	 * want to support CR-only? Probably not.
	 */
	return 0;
}

88
int git_filters_load(git_vector *filters, git_repository *repo, const char *path, int mode)
89
{
90 91 92
	int error;

	if (mode == GIT_FILTER_TO_ODB) {
93 94
		/* Load the CRLF cleanup filter when writing to the ODB */
		error = git_filter_add__crlf_to_odb(filters, repo, path);
95 96 97
		if (error < GIT_SUCCESS)
			return error;
	} else {
98 99
		giterr_set(GITERR_INVALID, "Worktree filters are not implemented yet");
		return GIT_ENOTIMPLEMENTED;
100 101
	}

102
	return (int)filters->length;
103 104
}

105
void git_filters_free(git_vector *filters)
106 107 108 109 110 111 112 113
{
	size_t i;
	git_filter *filter;

	git_vector_foreach(filters, i, filter) {
		if (filter->do_free != NULL)
			filter->do_free(filter);
		else
114
			git__free(filter);
115 116 117 118 119
	}

	git_vector_free(filters);
}

120
int git_filters_apply(git_buf *dest, git_buf *source, git_vector *filters)
121
{
122
	unsigned int i, src;
123 124 125 126 127 128 129
	git_buf *dbuffer[2];

	dbuffer[0] = source;
	dbuffer[1] = dest;

	src = 0;

nulltoken committed
130
	if (git_buf_len(source) == 0) {
131 132 133 134
		git_buf_clear(dest);
		return GIT_SUCCESS;
	}

135 136
	/* Pre-grow the destination buffer to more or less the size
	 * we expect it to have */
nulltoken committed
137
	if (git_buf_grow(dest, git_buf_len(source)) < 0)
138 139 140
		return GIT_ENOMEM;

	for (i = 0; i < filters->length; ++i) {
141
		git_filter *filter = git_vector_get(filters, i);
142
		unsigned int dst = 1 - src;
143 144 145

		git_buf_clear(dbuffer[dst]);

146
		/* Apply the filter from dbuffer[src] to the other buffer;
147 148 149 150 151
		 * if the filtering is canceled by the user mid-filter,
		 * we skip to the next filter without changing the source
		 * of the double buffering (so that the text goes through
		 * cleanly).
		 */
152 153
		if (filter->apply(filter, dbuffer[dst], dbuffer[src]) == 0)
			src = dst;
154 155 156 157 158 159

		if (git_buf_oom(dbuffer[dst]))
			return GIT_ENOMEM;
	}

	/* Ensure that the output ends up in dbuffer[1] (i.e. the dest) */
160
	if (src != 1)
161 162 163 164
		git_buf_swap(dest, source);

	return GIT_SUCCESS;
}
165