diff.h 4.48 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6 7 8 9
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#ifndef INCLUDE_diff_h__
#define INCLUDE_diff_h__

10 11 12
#include "git2/diff.h"
#include "git2/oid.h"

13 14 15
#include <stdio.h>
#include "vector.h"
#include "buffer.h"
16 17
#include "iterator.h"
#include "repository.h"
18
#include "pool.h"
19
#include "odb.h"
20

21 22
#define DIFF_OLD_PREFIX_DEFAULT "a/"
#define DIFF_NEW_PREFIX_DEFAULT "b/"
23

24 25
enum {
	GIT_DIFFCAPS_HAS_SYMLINKS     = (1 << 0), /* symlinks on platform? */
26
	GIT_DIFFCAPS_IGNORE_STAT      = (1 << 1), /* use stat? */
27
	GIT_DIFFCAPS_TRUST_MODE_BITS  = (1 << 2), /* use st_mode? */
28 29
	GIT_DIFFCAPS_TRUST_CTIME      = (1 << 3), /* use st_ctime? */
	GIT_DIFFCAPS_USE_DEV          = (1 << 4), /* use st_dev? */
30
	GIT_DIFFCAPS_TRUST_NANOSECS   = (1 << 5), /* use stat time nanoseconds */
31 32
};

33 34 35
#define DIFF_FLAGS_KNOWN_BINARY (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
#define DIFF_FLAGS_NOT_BINARY   (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)

36 37 38 39 40
enum {
	GIT_DIFF_FLAG__FREE_PATH  = (1 << 7),  /* `path` is allocated memory */
	GIT_DIFF_FLAG__FREE_DATA  = (1 << 8),  /* internal file data is allocated */
	GIT_DIFF_FLAG__UNMAP_DATA = (1 << 9),  /* internal file data is mmap'ed */
	GIT_DIFF_FLAG__NO_DATA    = (1 << 10), /* file data should not be loaded */
41 42
	GIT_DIFF_FLAG__FREE_BLOB  = (1 << 11), /* release the blob when done */
	GIT_DIFF_FLAG__LOADED     = (1 << 12), /* file data has been loaded */
43 44 45 46 47 48

	GIT_DIFF_FLAG__TO_DELETE  = (1 << 16), /* delete entry during rename det. */
	GIT_DIFF_FLAG__TO_SPLIT   = (1 << 17), /* split entry during rename det. */
	GIT_DIFF_FLAG__IS_RENAME_TARGET = (1 << 18),
	GIT_DIFF_FLAG__IS_RENAME_SOURCE = (1 << 19),
	GIT_DIFF_FLAG__HAS_SELF_SIMILARITY = (1 << 20),
49
};
50

51 52
#define GIT_DIFF_FLAG__CLEAR_INTERNAL(F) (F) = ((F) & 0x00FFFF)

53 54
#define GIT_DIFF__VERBOSE  (1 << 30)

55
struct git_diff {
Russell Belfer committed
56
	git_refcount     rc;
57 58
	git_repository   *repo;
	git_diff_options opts;
59
	git_vector       pathspec;
Russell Belfer committed
60
	git_vector       deltas;    /* vector of git_diff_delta */
61
	git_pool pool;
62 63
	git_iterator_type_t old_src;
	git_iterator_type_t new_src;
64
	uint32_t diffcaps;
65

66 67 68 69
	int (*strcomp)(const char *, const char *);
	int (*strncomp)(const char *, const char *, size_t);
	int (*pfxcomp)(const char *str, const char *pfx);
	int (*entrycomp)(const void *a, const void *b);
70 71
};

72 73 74
extern void git_diff__cleanup_modes(
	uint32_t diffcaps, uint32_t *omode, uint32_t *nmode);

75
extern void git_diff_addref(git_diff *diff);
76

77
extern int git_diff_delta__cmp(const void *a, const void *b);
78
extern int git_diff_delta__casecmp(const void *a, const void *b);
79

80 81
extern const char *git_diff_delta__path(const git_diff_delta *delta);

82
extern bool git_diff_delta__should_skip(
83
	const git_diff_options *opts, const git_diff_delta *delta);
84

85 86 87 88 89 90 91
extern int git_diff_delta__format_file_header(
	git_buf *out,
	const git_diff_delta *delta,
	const char *oldpfx,
	const char *newpfx,
	int oid_strlen);

92 93 94
extern int git_diff__oid_for_file(
	git_repository *, const char *, uint16_t, git_off_t, git_oid *);

Russell Belfer committed
95
extern int git_diff__from_iterators(
96
	git_diff **diff_ptr,
Russell Belfer committed
97 98 99 100
	git_repository *repo,
	git_iterator *old_iter,
	git_iterator *new_iter,
	const git_diff_options *opts);
101

102
extern int git_diff__paired_foreach(
103 104
	git_diff *idx2head,
	git_diff *wd2idx,
105 106 107
	int (*cb)(git_diff_delta *i2h, git_diff_delta *w2i, void *payload),
	void *payload);

108
extern int git_diff_find_similar__hashsig_for_file(
Edward Thomson committed
109 110
	void **out, const git_diff_file *f, const char *path, void *p);

111
extern int git_diff_find_similar__hashsig_for_buf(
Edward Thomson committed
112 113
	void **out, const git_diff_file *f, const char *buf, size_t len, void *p);

114
extern void git_diff_find_similar__hashsig_free(void *sig, void *payload);
Edward Thomson committed
115

116
extern int git_diff_find_similar__calc_similarity(
Edward Thomson committed
117 118
	int *score, void *siga, void *sigb, void *payload);

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
/*
 * Sometimes a git_diff_file will have a zero size; this attempts to
 * fill in the size without loading the blob if possible.  If that is
 * not possible, then it will return the git_odb_object that had to be
 * loaded and the caller can use it or dispose of it as needed.
 */
GIT_INLINE(int) git_diff_file__resolve_zero_size(
	git_diff_file *file, git_odb_object **odb_obj, git_repository *repo)
{
	int error;
	git_odb *odb;
	size_t len;
	git_otype type;

	if ((error = git_repository_odb(&odb, repo)) < 0)
		return error;

	error = git_odb__read_header_or_object(
		odb_obj, &len, &type, odb, &file->oid);

	git_odb_free(odb);

	if (!error)
		file->size = (git_off_t)len;

	return error;
}

147 148
#endif