Commit e68e33f3 by Vicent Martí

Merge pull request #1233 from arrbee/file-similarity-metric

Add file similarity scoring to diff rename/copy detection
parents 9f9477d6 1be4ba98
......@@ -88,10 +88,9 @@ typedef enum {
GIT_DIFF_INCLUDE_UNTRACKED = (1 << 8),
/** Include unmodified files in the diff list */
GIT_DIFF_INCLUDE_UNMODIFIED = (1 << 9),
/** Even with the GIT_DIFF_INCLUDE_UNTRACKED flag, when an untracked
* directory is found, only a single entry for the directory is added
* to the diff list; with this flag, all files under the directory will
* be included, too.
/** Even with GIT_DIFF_INCLUDE_UNTRACKED, an entire untracked directory
* will be marked with only a single entry in the diff list; this flag
* adds all files under the directory as UNTRACKED entries, too.
*/
GIT_DIFF_RECURSE_UNTRACKED_DIRS = (1 << 10),
/** If the pathspec is set in the diff options, this flags means to
......@@ -120,6 +119,11 @@ typedef enum {
GIT_DIFF_INCLUDE_TYPECHANGE_TREES = (1 << 16),
/** Ignore file mode changes */
GIT_DIFF_IGNORE_FILEMODE = (1 << 17),
/** Even with GIT_DIFF_INCLUDE_IGNORED, an entire ignored directory
* will be marked with only a single entry in the diff list; this flag
* adds all files under the directory as IGNORED entries, too.
*/
GIT_DIFF_RECURSE_IGNORED_DIRS = (1 << 10),
} git_diff_option_t;
/**
......@@ -133,20 +137,18 @@ typedef enum {
typedef struct git_diff_list git_diff_list;
/**
* Flags for the file object on each side of a diff.
* Flags for the delta object and the file objects on each side.
*
* Note: most of these flags are just for **internal** consumption by
* libgit2, but some of them may be interesting to external users.
* These flags are used for both the `flags` value of the `git_diff_delta`
* and the flags for the `git_diff_file` objects representing the old and
* new sides of the delta. Values outside of this public range should be
* considered reserved for internal or future use.
*/
typedef enum {
GIT_DIFF_FILE_VALID_OID = (1 << 0), /** `oid` value is known correct */
GIT_DIFF_FILE_FREE_PATH = (1 << 1), /** `path` is allocated memory */
GIT_DIFF_FILE_BINARY = (1 << 2), /** should be considered binary data */
GIT_DIFF_FILE_NOT_BINARY = (1 << 3), /** should be considered text data */
GIT_DIFF_FILE_FREE_DATA = (1 << 4), /** internal file data is allocated */
GIT_DIFF_FILE_UNMAP_DATA = (1 << 5), /** internal file data is mmap'ed */
GIT_DIFF_FILE_NO_DATA = (1 << 6), /** file data should not be loaded */
} git_diff_file_flag_t;
GIT_DIFF_FLAG_BINARY = (1 << 0), /** file(s) treated as binary data */
GIT_DIFF_FLAG_NOT_BINARY = (1 << 1), /** file(s) treated as text data */
GIT_DIFF_FLAG_VALID_OID = (1 << 2), /** `oid` value is known correct */
} git_diff_flag_t;
/**
* What type of change is described by a git_diff_delta?
......@@ -186,18 +188,17 @@ typedef enum {
*
* `size` is the size of the entry in bytes.
*
* `flags` is a combination of the `git_diff_file_flag_t` types, but those
* are largely internal values.
* `flags` is a combination of the `git_diff_flag_t` types
*
* `mode` is, roughly, the stat() `st_mode` value for the item. This will
* be restricted to one of the `git_filemode_t` values.
*/
typedef struct {
git_oid oid;
git_oid oid;
const char *path;
git_off_t size;
unsigned int flags;
uint16_t mode;
git_off_t size;
uint32_t flags;
uint16_t mode;
} git_diff_file;
/**
......@@ -219,16 +220,17 @@ typedef struct {
*
* Under some circumstances, in the name of efficiency, not all fields will
* be filled in, but we generally try to fill in as much as possible. One
* example is that the "binary" field will not examine file contents if you
* do not pass in hunk and/or line callbacks to the diff foreach iteration
* function. It will just use the git attributes for those files.
* example is that the "flags" field may not have either the `BINARY` or the
* `NOT_BINARY` flag set to avoid examining file contents if you do not pass
* in hunk and/or line callbacks to the diff foreach iteration function. It
* will just use the git attributes for those files.
*/
typedef struct {
git_diff_file old_file;
git_diff_file new_file;
git_delta_t status;
unsigned int similarity; /**< for RENAMED and COPIED, value 0-100 */
int binary;
uint32_t similarity; /**< for RENAMED and COPIED, value 0-100 */
uint32_t flags;
} git_diff_delta;
/**
......@@ -377,7 +379,7 @@ typedef struct git_diff_patch git_diff_patch;
typedef enum {
/** look for renames? (`--find-renames`) */
GIT_DIFF_FIND_RENAMES = (1 << 0),
/** consider old size of modified for renames? (`--break-rewrites=N`) */
/** consider old side of modified for renames? (`--break-rewrites=N`) */
GIT_DIFF_FIND_RENAMES_FROM_REWRITES = (1 << 1),
/** look for copies? (a la `--find-copies`) */
......@@ -387,10 +389,49 @@ typedef enum {
/** split large rewrites into delete/add pairs (`--break-rewrites=/M`) */
GIT_DIFF_FIND_AND_BREAK_REWRITES = (1 << 4),
/** turn on all finding features */
GIT_DIFF_FIND_ALL = (0x1f),
/** measure similarity ignoring leading whitespace (default) */
GIT_DIFF_FIND_IGNORE_LEADING_WHITESPACE = 0,
/** measure similarity ignoring all whitespace */
GIT_DIFF_FIND_IGNORE_WHITESPACE = (1 << 6),
/** measure similarity including all data */
GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE = (1 << 7),
} git_diff_find_t;
/**
* Pluggable similarity metric
*/
typedef struct {
int (*file_signature)(
void **out, const git_diff_file *file,
const char *fullpath, void *payload);
int (*buffer_signature)(
void **out, const git_diff_file *file,
const char *buf, size_t buflen, void *payload);
void (*free_signature)(void *sig, void *payload);
int (*similarity)(int *score, void *siga, void *sigb, void *payload);
void *payload;
} git_diff_similarity_metric;
/**
* Control behavior of rename and copy detection
*
* These options mostly mimic parameters that can be passed to git-diff.
*
* - `rename_threshold` is the same as the -M option with a value
* - `copy_threshold` is the same as the -C option with a value
* - `rename_from_rewrite_threshold` matches the top of the -B option
* - `break_rewrite_threshold` matches the bottom of the -B option
* - `target_limit` matches the -l option
*
* The `metric` option allows you to plug in a custom similarity metric.
* Set it to NULL for the default internal metric which is based on sampling
* hashes of ranges of data in the file. The default metric is a pretty
* good similarity approximation that should work fairly well for both text
* and binary data, and is pretty fast with fixed memory overhead.
*/
typedef struct {
unsigned int version;
......@@ -411,6 +452,9 @@ typedef struct {
* the `diff.renameLimit` config) (default 200)
*/
unsigned int target_limit;
/** Pluggable similarity metric; pass NULL to use internal metric */
git_diff_similarity_metric *metric;
} git_diff_find_options;
#define GIT_DIFF_FIND_OPTIONS_VERSION 1
......@@ -856,11 +900,12 @@ GIT_EXTERN(int) git_diff_patch_to_str(
*
* NULL is allowed for either `old_blob` or `new_blob` and will be treated
* as an empty blob, with the `oid` set to NULL in the `git_diff_file` data.
* Passing NULL for both blobs is a noop; no callbacks will be made at all.
*
* We do run a binary content check on the two blobs and if either of the
* blobs looks like binary data, the `git_diff_delta` binary attribute will
* be set to 1 and no call to the hunk_cb nor line_cb will be made (unless
* you pass `GIT_DIFF_FORCE_TEXT` of course).
* We do run a binary content check on the blob content and if either blob
* looks like binary data, the `git_diff_delta` binary attribute will be set
* to 1 and no call to the hunk_cb nor line_cb will be made (unless you pass
* `GIT_DIFF_FORCE_TEXT` of course).
*
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
......@@ -880,6 +925,11 @@ GIT_EXTERN(int) git_diff_blobs(
* so the `git_diff_file` parameters to the callbacks will be faked a la the
* rules for `git_diff_blobs()`.
*
* Passing NULL for `old_blob` will be treated as an empty blob (i.e. the
* `file_cb` will be invoked with GIT_DELTA_ADDED and the diff will be the
* entire content of the buffer added). Passing NULL to the buffer will do
* the reverse, with GIT_DELTA_REMOVED and blob content removed.
*
* @return 0 on success, GIT_EUSER on non-zero callback, or error code
*/
GIT_EXTERN(int) git_diff_blob_to_buffer(
......
......@@ -78,7 +78,7 @@ static int checkout_notify(
git_oid_cpy(&wdfile.oid, &wditem->oid);
wdfile.path = wditem->path;
wdfile.size = wditem->file_size;
wdfile.flags = GIT_DIFF_FILE_VALID_OID;
wdfile.flags = GIT_DIFF_FLAG_VALID_OID;
wdfile.mode = wditem->mode;
workdir = &wdfile;
......
......@@ -92,11 +92,11 @@ static int diff_delta__from_one(
git_oid_cpy(&delta->new_file.oid, &entry->oid);
}
delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
if (delta->status == GIT_DELTA_DELETED ||
!git_oid_iszero(&delta->new_file.oid))
delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
notify_res = diff_notify(diff, delta, matched_pathspec);
......@@ -142,7 +142,7 @@ static int diff_delta__from_two(
git_oid_cpy(&delta->old_file.oid, &old_entry->oid);
delta->old_file.size = old_entry->file_size;
delta->old_file.mode = old_mode;
delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
git_oid_cpy(&delta->new_file.oid, &new_entry->oid);
delta->new_file.size = new_entry->file_size;
......@@ -156,7 +156,7 @@ static int diff_delta__from_two(
}
if (new_oid || !git_oid_iszero(&new_entry->oid))
delta->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
delta->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
notify_res = diff_notify(diff, delta, matched_pathspec);
......
......@@ -28,8 +28,14 @@ enum {
GIT_DIFFCAPS_USE_DEV = (1 << 4), /* use st_dev? */
};
#define GIT_DELTA__TO_DELETE 10
#define GIT_DELTA__TO_SPLIT 11
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 */
GIT_DIFF_FLAG__TO_DELETE = (1 << 11), /* delete entry during rename det. */
GIT_DIFF_FLAG__TO_SPLIT = (1 << 12), /* split entry during rename det. */
};
struct git_diff_list {
git_refcount rc;
......
......@@ -52,8 +52,8 @@ static int parse_hunk_header(git_diff_range *range, const char *header)
return 0;
}
#define KNOWN_BINARY_FLAGS (GIT_DIFF_FILE_BINARY|GIT_DIFF_FILE_NOT_BINARY)
#define NOT_BINARY_FLAGS (GIT_DIFF_FILE_NOT_BINARY|GIT_DIFF_FILE_NO_DATA)
#define KNOWN_BINARY_FLAGS (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)
#define NOT_BINARY_FLAGS (GIT_DIFF_FLAG_NOT_BINARY|GIT_DIFF_FLAG__NO_DATA)
static int update_file_is_binary_by_attr(
git_repository *repo, git_diff_file *file)
......@@ -68,9 +68,9 @@ static int update_file_is_binary_by_attr(
return -1;
if (GIT_ATTR_FALSE(value))
file->flags |= GIT_DIFF_FILE_BINARY;
file->flags |= GIT_DIFF_FLAG_BINARY;
else if (GIT_ATTR_TRUE(value))
file->flags |= GIT_DIFF_FILE_NOT_BINARY;
file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
/* otherwise leave file->flags alone */
return 0;
......@@ -78,15 +78,15 @@ static int update_file_is_binary_by_attr(
static void update_delta_is_binary(git_diff_delta *delta)
{
if ((delta->old_file.flags & GIT_DIFF_FILE_BINARY) != 0 ||
(delta->new_file.flags & GIT_DIFF_FILE_BINARY) != 0)
delta->binary = 1;
if ((delta->old_file.flags & GIT_DIFF_FLAG_BINARY) != 0 ||
(delta->new_file.flags & GIT_DIFF_FLAG_BINARY) != 0)
delta->flags |= GIT_DIFF_FLAG_BINARY;
else if ((delta->old_file.flags & NOT_BINARY_FLAGS) != 0 &&
(delta->new_file.flags & NOT_BINARY_FLAGS) != 0)
delta->binary = 0;
delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
/* otherwise leave delta->binary value untouched */
/* otherwise leave delta->flags binary value untouched */
}
/* returns if we forced binary setting (and no further checks needed) */
......@@ -95,24 +95,24 @@ static bool diff_delta_is_binary_forced(
git_diff_delta *delta)
{
/* return true if binary-ness has already been settled */
if (delta->binary != -1)
if ((delta->flags & KNOWN_BINARY_FLAGS) != 0)
return true;
/* make sure files are conceivably mmap-able */
if ((git_off_t)((size_t)delta->old_file.size) != delta->old_file.size ||
(git_off_t)((size_t)delta->new_file.size) != delta->new_file.size)
{
delta->old_file.flags |= GIT_DIFF_FILE_BINARY;
delta->new_file.flags |= GIT_DIFF_FILE_BINARY;
delta->binary = 1;
delta->old_file.flags |= GIT_DIFF_FLAG_BINARY;
delta->new_file.flags |= GIT_DIFF_FLAG_BINARY;
delta->flags |= GIT_DIFF_FLAG_BINARY;
return true;
}
/* check if user is forcing us to text diff these files */
if (ctxt->opts && (ctxt->opts->flags & GIT_DIFF_FORCE_TEXT) != 0) {
delta->old_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
delta->new_file.flags |= GIT_DIFF_FILE_NOT_BINARY;
delta->binary = 0;
delta->old_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
delta->new_file.flags |= GIT_DIFF_FLAG_NOT_BINARY;
delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
return true;
}
......@@ -125,8 +125,6 @@ static int diff_delta_is_binary_by_attr(
int error = 0, mirror_new;
git_diff_delta *delta = patch->delta;
delta->binary = -1;
if (diff_delta_is_binary_forced(ctxt, delta))
return 0;
......@@ -152,23 +150,21 @@ static int diff_delta_is_binary_by_content(
git_diff_file *file,
const git_map *map)
{
const git_buf search = { map->data, 0, min(map->len, 4000) };
if (diff_delta_is_binary_forced(ctxt, delta))
return 0;
if ((file->flags & KNOWN_BINARY_FLAGS) == 0) {
const git_buf search = { map->data, 0, min(map->len, 4000) };
/* TODO: provide encoding / binary detection callbacks that can
* be UTF-8 aware, etc. For now, instead of trying to be smart,
* let's just use the simple NUL-byte detection that core git uses.
*/
/* TODO: provide encoding / binary detection callbacks that can
* be UTF-8 aware, etc. For now, instead of trying to be smart,
* let's just use the simple NUL-byte detection that core git uses.
*/
/* previously was: if (git_buf_text_is_binary(&search)) */
if (git_buf_text_contains_nul(&search))
file->flags |= GIT_DIFF_FILE_BINARY;
else
file->flags |= GIT_DIFF_FILE_NOT_BINARY;
}
/* previously was: if (git_buf_text_is_binary(&search)) */
if (git_buf_text_contains_nul(&search))
file->flags |= GIT_DIFF_FLAG_BINARY;
else
file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
update_delta_is_binary(delta);
......@@ -192,7 +188,7 @@ static int diff_delta_is_binary_by_size(
}
if (file->size > threshold)
file->flags |= GIT_DIFF_FILE_BINARY;
file->flags |= GIT_DIFF_FLAG_BINARY;
update_delta_is_binary(delta);
......@@ -247,7 +243,7 @@ static int get_blob_content(
map->data = git_buf_detach(&content);
map->len = strlen(map->data);
file->flags |= GIT_DIFF_FILE_FREE_DATA;
file->flags |= GIT_DIFF_FLAG__FREE_DATA;
return 0;
}
......@@ -270,7 +266,7 @@ static int get_blob_content(
/* if blob is too large to diff, mark as binary */
if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0)
return error;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
return 0;
if (odb_obj != NULL) {
......@@ -306,14 +302,14 @@ static int get_workdir_sm_content(
return error;
/* update OID if we didn't have it previously */
if ((file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
if ((file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
const git_oid* sm_head;
if ((sm_head = git_submodule_wd_id(sm)) != NULL ||
(sm_head = git_submodule_head_id(sm)) != NULL)
{
git_oid_cpy(&file->oid, sm_head);
file->flags |= GIT_DIFF_FILE_VALID_OID;
file->flags |= GIT_DIFF_FLAG_VALID_OID;
}
}
......@@ -329,7 +325,7 @@ static int get_workdir_sm_content(
map->data = git_buf_detach(&content);
map->len = strlen(map->data);
file->flags |= GIT_DIFF_FILE_FREE_DATA;
file->flags |= GIT_DIFF_FLAG__FREE_DATA;
return 0;
}
......@@ -356,8 +352,8 @@ static int get_workdir_content(
if (S_ISLNK(file->mode)) {
ssize_t alloc_len, read_len;
file->flags |= GIT_DIFF_FILE_FREE_DATA;
file->flags |= GIT_DIFF_FILE_BINARY;
file->flags |= GIT_DIFF_FLAG__FREE_DATA;
file->flags |= GIT_DIFF_FLAG_BINARY;
/* link path on disk could be UTF-16, so prepare a buffer that is
* big enough to handle some UTF-8 data expansion
......@@ -389,7 +385,7 @@ static int get_workdir_content(
file->size = git_futils_filesize(fd);
if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
delta->binary == 1)
(delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto close_and_cleanup;
error = git_filters_load(
......@@ -402,7 +398,7 @@ static int get_workdir_content(
goto close_and_cleanup;
error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
file->flags |= GIT_DIFF_FILE_UNMAP_DATA;
file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
} else {
git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
......@@ -412,7 +408,7 @@ static int get_workdir_content(
map->len = git_buf_len(&filtered);
map->data = git_buf_detach(&filtered);
file->flags |= GIT_DIFF_FILE_FREE_DATA;
file->flags |= GIT_DIFF_FLAG__FREE_DATA;
}
git_buf_free(&raw);
......@@ -425,11 +421,11 @@ close_and_cleanup:
}
/* once data is loaded, update OID if we didn't have it previously */
if (!error && (file->flags & GIT_DIFF_FILE_VALID_OID) == 0) {
if (!error && (file->flags & GIT_DIFF_FLAG_VALID_OID) == 0) {
error = git_odb_hash(
&file->oid, map->data, map->len, GIT_OBJ_BLOB);
if (!error)
file->flags |= GIT_DIFF_FILE_VALID_OID;
file->flags |= GIT_DIFF_FLAG_VALID_OID;
}
if (!error)
......@@ -445,22 +441,22 @@ static void release_content(git_diff_file *file, git_map *map, git_blob *blob)
if (blob != NULL)
git_blob_free(blob);
if (file->flags & GIT_DIFF_FILE_FREE_DATA) {
if (file->flags & GIT_DIFF_FLAG__FREE_DATA) {
git__free(map->data);
map->data = "";
map->len = 0;
file->flags &= ~GIT_DIFF_FILE_FREE_DATA;
file->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
}
else if (file->flags & GIT_DIFF_FILE_UNMAP_DATA) {
else if (file->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
git_futils_mmap_free(map);
map->data = "";
map->len = 0;
file->flags &= ~GIT_DIFF_FILE_UNMAP_DATA;
file->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
}
}
static void diff_context_init(
static int diff_context_init(
diff_context *ctxt,
git_diff_list *diff,
git_repository *repo,
......@@ -472,6 +468,12 @@ static void diff_context_init(
{
memset(ctxt, 0, sizeof(diff_context));
if (!repo && diff)
repo = diff->repo;
if (!opts && diff)
opts = &diff->opts;
ctxt->repo = repo;
ctxt->diff = diff;
ctxt->opts = opts;
......@@ -482,6 +484,8 @@ static void diff_context_init(
ctxt->error = 0;
setup_xdiff_options(ctxt->opts, &ctxt->xdiff_config, &ctxt->xdiff_params);
return 0;
}
static int diff_delta_file_callback(
......@@ -555,7 +559,7 @@ static int diff_patch_load(
patch->new_data.len = 0;
patch->new_blob = NULL;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
if (!ctxt->hunk_cb &&
......@@ -565,25 +569,25 @@ static int diff_patch_load(
switch (delta->status) {
case GIT_DELTA_ADDED:
delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
break;
case GIT_DELTA_DELETED:
delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
break;
case GIT_DELTA_MODIFIED:
break;
case GIT_DELTA_UNTRACKED:
delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
if ((ctxt->opts->flags & GIT_DIFF_INCLUDE_UNTRACKED_CONTENT) == 0)
delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
break;
default:
delta->new_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->old_file.flags |= GIT_DIFF_FILE_NO_DATA;
delta->new_file.flags |= GIT_DIFF_FLAG__NO_DATA;
delta->old_file.flags |= GIT_DIFF_FLAG__NO_DATA;
break;
}
#define CHECK_UNMODIFIED (GIT_DIFF_FILE_NO_DATA | GIT_DIFF_FILE_VALID_OID)
#define CHECK_UNMODIFIED (GIT_DIFF_FLAG__NO_DATA | GIT_DIFF_FLAG_VALID_OID)
check_if_unmodified =
(delta->old_file.flags & CHECK_UNMODIFIED) == 0 &&
......@@ -594,41 +598,41 @@ static int diff_patch_load(
* memory footprint during diff.
*/
if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
patch->old_src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_workdir_content(
ctxt, delta, &delta->old_file, &patch->old_data)) < 0)
goto cleanup;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
patch->new_src == GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_workdir_content(
ctxt, delta, &delta->new_file, &patch->new_data)) < 0)
goto cleanup;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
if ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
if ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
patch->old_src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_blob_content(
ctxt, delta, &delta->old_file,
&patch->old_data, &patch->old_blob)) < 0)
goto cleanup;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
if ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0 &&
if ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0 &&
patch->new_src != GIT_ITERATOR_TYPE_WORKDIR) {
if ((error = get_blob_content(
ctxt, delta, &delta->new_file,
&patch->new_data, &patch->new_blob)) < 0)
goto cleanup;
if (delta->binary == 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
goto cleanup;
}
......@@ -646,13 +650,13 @@ static int diff_patch_load(
}
cleanup:
if (delta->binary == -1)
if ((delta->flags & KNOWN_BINARY_FLAGS) == 0)
update_delta_is_binary(delta);
if (!error) {
patch->flags |= GIT_DIFF_PATCH_LOADED;
if (delta->binary != 1 &&
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
delta->status != GIT_DELTA_UNMODIFIED &&
(patch->old_data.len || patch->new_data.len) &&
!git_oid_equal(&delta->old_file.oid, &delta->new_file.oid))
......@@ -926,6 +930,15 @@ static int diff_patch_line_cb(
return 0;
}
static int diff_required(git_diff_list *diff, const char *action)
{
if (!diff) {
giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
return -1;
}
return 0;
}
int git_diff_foreach(
git_diff_list *diff,
......@@ -939,9 +952,12 @@ int git_diff_foreach(
size_t idx;
git_diff_patch patch;
diff_context_init(
&ctxt, diff, diff->repo, &diff->opts,
file_cb, hunk_cb, data_cb, payload);
if (diff_required(diff, "git_diff_foreach") < 0)
return -1;
if (diff_context_init(
&ctxt, diff, NULL, NULL, file_cb, hunk_cb, data_cb, payload) < 0)
return -1;
diff_patch_init(&ctxt, &patch);
......@@ -1138,7 +1154,7 @@ static int print_patch_file(
newpath = "/dev/null";
}
if (delta->binary != 1) {
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0) {
git_buf_printf(pi->buf, "--- %s%s\n", oldpfx, oldpath);
git_buf_printf(pi->buf, "+++ %s%s\n", newpfx, newpath);
}
......@@ -1153,7 +1169,7 @@ static int print_patch_file(
return GIT_EUSER;
}
if (delta->binary != 1)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0)
return 0;
git_buf_clear(pi->buf);
......@@ -1268,7 +1284,7 @@ static void set_data_from_blob(
map->data = (char *)git_blob_rawcontent(blob);
} else {
file->size = 0;
file->flags |= GIT_DIFF_FILE_NO_DATA;
file->flags |= GIT_DIFF_FLAG__NO_DATA;
map->len = 0;
map->data = "";
......@@ -1283,7 +1299,7 @@ static void set_data_from_buffer(
map->len = buffer_len;
if (!buffer) {
file->flags |= GIT_DIFF_FILE_NO_DATA;
file->flags |= GIT_DIFF_FLAG__NO_DATA;
map->data = NULL;
} else {
map->data = (char *)buffer;
......@@ -1310,8 +1326,10 @@ static int diff_single_init(
memset(data, 0, sizeof(*data));
diff_context_init(
&data->ctxt, NULL, repo, opts, file_cb, hunk_cb, data_cb, payload);
if (diff_context_init(
&data->ctxt, NULL, repo, opts,
file_cb, hunk_cb, data_cb, payload) < 0)
return -1;
diff_patch_init(&data->ctxt, &data->patch);
......@@ -1322,13 +1340,13 @@ static int diff_single_apply(diff_single_data *data)
{
int error;
git_diff_delta *delta = &data->delta;
bool has_old = ((delta->old_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
bool has_new = ((delta->new_file.flags & GIT_DIFF_FILE_NO_DATA) == 0);
bool has_old = ((delta->old_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
bool has_new = ((delta->new_file.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
/* finish setting up fake git_diff_delta record and loaded data */
data->patch.delta = delta;
delta->binary = -1;
delta->flags = delta->flags & ~KNOWN_BINARY_FLAGS;
delta->status = has_new ?
(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
......@@ -1345,7 +1363,8 @@ static int diff_single_apply(diff_single_data *data)
data->patch.flags |= GIT_DIFF_PATCH_LOADED;
if (delta->binary != 1 && delta->status != GIT_DELTA_UNMODIFIED)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
delta->status != GIT_DELTA_UNMODIFIED)
data->patch.flags |= GIT_DIFF_PATCH_DIFFABLE;
/* do diffs */
......@@ -1377,6 +1396,9 @@ int git_diff_blobs(
new_blob ? git_object_owner((const git_object *)new_blob) :
old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
if (!repo) /* Hmm, given two NULL blobs, silently do no callbacks? */
return 0;
if ((error = diff_single_init(
&d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
return error;
......@@ -1408,6 +1430,9 @@ int git_diff_blob_to_buffer(
git_repository *repo =
old_blob ? git_object_owner((const git_object *)old_blob) : NULL;
if (!repo && !buf) /* Hmm, given NULLs, silently do no callbacks? */
return 0;
if ((error = diff_single_init(
&d, repo, options, file_cb, hunk_cb, data_cb, payload)) < 0)
return error;
......@@ -1456,11 +1481,19 @@ int git_diff_get_patch(
if (patch_ptr)
*patch_ptr = NULL;
if (delta_ptr)
*delta_ptr = NULL;
if (diff_required(diff, "git_diff_get_patch") < 0)
return -1;
if (diff_context_init(
&ctxt, diff, NULL, NULL,
NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL) < 0)
return -1;
delta = git_vector_get(&diff->deltas, idx);
if (!delta) {
if (delta_ptr)
*delta_ptr = NULL;
giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
return GIT_ENOTFOUND;
}
......@@ -1469,14 +1502,10 @@ int git_diff_get_patch(
*delta_ptr = delta;
if (!patch_ptr &&
(delta->binary != -1 ||
((delta->flags & KNOWN_BINARY_FLAGS) != 0 ||
(diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
return 0;
diff_context_init(
&ctxt, diff, diff->repo, &diff->opts,
NULL, diff_patch_hunk_cb, diff_patch_line_cb, NULL);
if (git_diff_delta__should_skip(ctxt.opts, delta))
return 0;
......
......@@ -7,6 +7,8 @@
#include "common.h"
#include "diff.h"
#include "git2/config.h"
#include "git2/blob.h"
#include "hashsig.h"
static git_diff_delta *diff_delta__dup(
const git_diff_delta *d, git_pool *pool)
......@@ -168,6 +170,36 @@ int git_diff_merge(
return error;
}
static int find_similar__hashsig_for_file(
void **out, const git_diff_file *f, const char *path, void *p)
{
git_hashsig_option_t opt = (git_hashsig_option_t)p;
GIT_UNUSED(f);
return git_hashsig_create_fromfile((git_hashsig **)out, path, opt);
}
static int find_similar__hashsig_for_buf(
void **out, const git_diff_file *f, const char *buf, size_t len, void *p)
{
git_hashsig_option_t opt = (git_hashsig_option_t)p;
GIT_UNUSED(f);
return git_hashsig_create((git_hashsig **)out, buf, len, opt);
}
static void find_similar__hashsig_free(void *sig, void *payload)
{
GIT_UNUSED(payload);
git_hashsig_free(sig);
}
static int find_similar__calc_similarity(
int *score, void *siga, void *sigb, void *payload)
{
GIT_UNUSED(payload);
*score = git_hashsig_compare(siga, sigb);
return 0;
}
#define DEFAULT_THRESHOLD 50
#define DEFAULT_BREAK_REWRITE_THRESHOLD 60
#define DEFAULT_TARGET_LIMIT 200
......@@ -178,7 +210,6 @@ static int normalize_find_opts(
git_diff_find_options *given)
{
git_config *cfg = NULL;
const char *val;
if (diff->repo != NULL &&
git_repository_config__weakptr(&cfg, diff->repo) < 0)
......@@ -187,8 +218,9 @@ static int normalize_find_opts(
if (given != NULL)
memcpy(opts, given, sizeof(*opts));
else {
git_diff_find_options init = GIT_DIFF_FIND_OPTIONS_INIT;
memmove(opts, &init, sizeof(init));
const char *val = NULL;
GIT_INIT_STRUCTURE(opts, GIT_DIFF_FIND_OPTIONS_VERSION);
opts->flags = GIT_DIFF_FIND_RENAMES;
......@@ -236,6 +268,24 @@ static int normalize_find_opts(
opts->target_limit = limit;
}
/* assign the internal metric with whitespace flag as payload */
if (!opts->metric) {
opts->metric = git__malloc(sizeof(git_diff_similarity_metric));
GITERR_CHECK_ALLOC(opts->metric);
opts->metric->file_signature = find_similar__hashsig_for_file;
opts->metric->buffer_signature = find_similar__hashsig_for_buf;
opts->metric->free_signature = find_similar__hashsig_free;
opts->metric->similarity = find_similar__calc_similarity;
if (opts->flags & GIT_DIFF_FIND_IGNORE_WHITESPACE)
opts->metric->payload = (void *)GIT_HASHSIG_IGNORE_WHITESPACE;
else if (opts->flags & GIT_DIFF_FIND_DONT_IGNORE_WHITESPACE)
opts->metric->payload = (void *)GIT_HASHSIG_NORMAL;
else
opts->metric->payload = (void *)GIT_HASHSIG_SMART_WHITESPACE;
}
return 0;
}
......@@ -250,10 +300,10 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
/* build new delta list without TO_DELETE and splitting TO_SPLIT */
git_vector_foreach(&diff->deltas, i, delta) {
if (delta->status == GIT_DELTA__TO_DELETE)
if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
continue;
if (delta->status == GIT_DELTA__TO_SPLIT) {
if ((delta->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0) {
git_diff_delta *deleted = diff_delta__dup(delta, &diff->pool);
if (!deleted)
goto on_error;
......@@ -261,7 +311,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
deleted->status = GIT_DELTA_DELETED;
memset(&deleted->new_file, 0, sizeof(deleted->new_file));
deleted->new_file.path = deleted->old_file.path;
deleted->new_file.flags |= GIT_DIFF_FILE_VALID_OID;
deleted->new_file.flags |= GIT_DIFF_FLAG_VALID_OID;
if (git_vector_insert(&onto, deleted) < 0)
goto on_error;
......@@ -269,7 +319,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
delta->status = GIT_DELTA_ADDED;
memset(&delta->old_file, 0, sizeof(delta->old_file));
delta->old_file.path = delta->new_file.path;
delta->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
delta->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
}
if (git_vector_insert(&onto, delta) < 0)
......@@ -278,7 +328,7 @@ static int apply_splits_and_deletes(git_diff_list *diff, size_t expected_size)
/* cannot return an error past this point */
git_vector_foreach(&diff->deltas, i, delta)
if (delta->status == GIT_DELTA__TO_DELETE)
if ((delta->flags & GIT_DIFF_FLAG__TO_DELETE) != 0)
git__free(delta);
/* swap new delta list into place */
......@@ -297,17 +347,86 @@ on_error:
return -1;
}
static unsigned int calc_similarity(
void *cache, git_diff_file *old_file, git_diff_file *new_file)
GIT_INLINE(git_diff_file *) similarity_get_file(git_diff_list *diff, size_t idx)
{
git_diff_delta *delta = git_vector_get(&diff->deltas, idx / 2);
return (idx & 1) ? &delta->new_file : &delta->old_file;
}
static int similarity_calc(
git_diff_list *diff,
git_diff_find_options *opts,
size_t file_idx,
void **cache)
{
GIT_UNUSED(cache);
int error = 0;
git_diff_file *file = similarity_get_file(diff, file_idx);
git_iterator_type_t src = (file_idx & 1) ? diff->old_src : diff->new_src;
if (src == GIT_ITERATOR_TYPE_WORKDIR) { /* compute hashsig from file */
git_buf path = GIT_BUF_INIT;
/* TODO: apply wd-to-odb filters to file data if necessary */
if (git_oid_cmp(&old_file->oid, &new_file->oid) == 0)
if (!(error = git_buf_joinpath(
&path, git_repository_workdir(diff->repo), file->path)))
error = opts->metric->file_signature(
&cache[file_idx], file, path.ptr, opts->metric->payload);
git_buf_free(&path);
} else { /* compute hashsig from blob buffer */
git_blob *blob = NULL;
/* TODO: add max size threshold a la diff? */
if ((error = git_blob_lookup(&blob, diff->repo, &file->oid)) < 0)
return error;
error = opts->metric->buffer_signature(
&cache[file_idx], file, git_blob_rawcontent(blob),
git_blob_rawsize(blob), opts->metric->payload);
git_blob_free(blob);
}
return error;
}
static int similarity_measure(
git_diff_list *diff,
git_diff_find_options *opts,
void **cache,
size_t a_idx,
size_t b_idx)
{
int score = 0;
git_diff_file *a_file = similarity_get_file(diff, a_idx);
git_diff_file *b_file = similarity_get_file(diff, b_idx);
if (GIT_MODE_TYPE(a_file->mode) != GIT_MODE_TYPE(b_file->mode))
return 0;
if (git_oid_cmp(&a_file->oid, &b_file->oid) == 0)
return 100;
/* TODO: insert actual similarity algo here */
/* update signature cache if needed */
if (!cache[a_idx] && similarity_calc(diff, opts, a_idx, cache) < 0)
return -1;
if (!cache[b_idx] && similarity_calc(diff, opts, b_idx, cache) < 0)
return -1;
return 0;
/* compare signatures */
if (opts->metric->similarity(
&score, cache[a_idx], cache[b_idx], opts->metric->payload) < 0)
return -1;
/* clip score */
if (score < 0)
score = 0;
else if (score > 100)
score = 100;
return score;
}
#define FLAG_SET(opts,flag_name) ((opts.flags & flag_name) != 0)
......@@ -316,109 +435,135 @@ int git_diff_find_similar(
git_diff_list *diff,
git_diff_find_options *given_opts)
{
unsigned int i, j, similarity;
size_t i, j, cache_size, *matches;
int error = 0, similarity;
git_diff_delta *from, *to;
git_diff_find_options opts;
unsigned int tried_targets, num_changes = 0;
git_vector matches = GIT_VECTOR_INIT;
size_t tried_targets, num_rewrites = 0;
void **cache;
if (normalize_find_opts(diff, &opts, given_opts) < 0)
return -1;
if ((error = normalize_find_opts(diff, &opts, given_opts)) < 0)
return error;
/* first do splits if requested */
/* TODO: maybe abort if deltas.length > target_limit ??? */
cache_size = diff->deltas.length * 2; /* must store b/c length may change */
cache = git__calloc(cache_size, sizeof(void *));
GITERR_CHECK_ALLOC(cache);
matches = git__calloc(diff->deltas.length, sizeof(size_t));
GITERR_CHECK_ALLOC(matches);
/* first break MODIFIED records that are too different (if requested) */
if (FLAG_SET(opts, GIT_DIFF_FIND_AND_BREAK_REWRITES)) {
git_vector_foreach(&diff->deltas, i, from) {
if (from->status != GIT_DELTA_MODIFIED)
continue;
/* Right now, this doesn't work right because the similarity
* algorithm isn't actually implemented...
*/
similarity = 100;
/* calc_similarity(NULL, &from->old_file, from->new_file); */
similarity = similarity_measure(
diff, &opts, cache, 2 * i, 2 * i + 1);
if (similarity < opts.break_rewrite_threshold) {
from->status = GIT_DELTA__TO_SPLIT;
num_changes++;
if (similarity < 0) {
error = similarity;
goto cleanup;
}
}
/* apply splits as needed */
if (num_changes > 0 &&
apply_splits_and_deletes(
diff, diff->deltas.length + num_changes) < 0)
return -1;
if ((unsigned int)similarity < opts.break_rewrite_threshold) {
from->flags |= GIT_DIFF_FLAG__TO_SPLIT;
num_rewrites++;
}
}
}
/* next find the most similar delta for each rename / copy candidate */
if (git_vector_init(&matches, diff->deltas.length, git_diff_delta__cmp) < 0)
return -1;
git_vector_foreach(&diff->deltas, i, from) {
tried_targets = 0;
/* skip things that aren't blobs */
if (GIT_MODE_TYPE(from->old_file.mode) !=
GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
continue;
/* don't check UNMODIFIED files as source unless given option */
if (from->status == GIT_DELTA_UNMODIFIED &&
!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
continue;
/* skip all but DELETED files unless copy detection is on */
if (!FLAG_SET(opts, GIT_DIFF_FIND_COPIES) &&
from->status != GIT_DELTA_DELETED &&
(from->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0)
continue;
git_vector_foreach(&diff->deltas, j, to) {
if (i == j)
continue;
/* skip things that aren't blobs */
if (GIT_MODE_TYPE(to->new_file.mode) !=
GIT_MODE_TYPE(GIT_FILEMODE_BLOB))
continue;
switch (to->status) {
case GIT_DELTA_ADDED:
case GIT_DELTA_UNTRACKED:
case GIT_DELTA_RENAMED:
case GIT_DELTA_COPIED:
break;
case GIT_DELTA_MODIFIED:
if ((to->flags & GIT_DIFF_FLAG__TO_SPLIT) == 0)
continue;
break;
default:
/* only the above status values should be checked */
continue;
}
/* skip all but DELETED files unless copy detection is on */
if (from->status != GIT_DELTA_DELETED &&
!FLAG_SET(opts, GIT_DIFF_FIND_COPIES))
continue;
/* don't check UNMODIFIED files as source unless given option */
if (from->status == GIT_DELTA_UNMODIFIED &&
!FLAG_SET(opts, GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED))
continue;
/* cap on maximum files we'll examine */
/* cap on maximum files we'll examine (per "from" file) */
if (++tried_targets > opts.target_limit)
break;
/* calculate similarity and see if this pair beats the
* similarity score of the current best pair.
*/
similarity = calc_similarity(NULL, &from->old_file, &to->new_file);
similarity = similarity_measure(
diff, &opts, cache, 2 * i, 2 * j + 1);
if (similarity < 0) {
error = similarity;
goto cleanup;
}
if (to->similarity < similarity) {
to->similarity = similarity;
if (git_vector_set(NULL, &matches, j, from) < 0)
return -1;
if (to->similarity < (unsigned int)similarity) {
to->similarity = (unsigned int)similarity;
matches[j] = i + 1;
}
}
}
/* next rewrite the diffs with renames / copies */
num_changes = 0;
git_vector_foreach(&diff->deltas, j, to) {
from = GIT_VECTOR_GET(&matches, j);
if (!from) {
if (!matches[j]) {
assert(to->similarity == 0);
continue;
}
/* three possible outcomes here:
i = matches[j] - 1;
from = GIT_VECTOR_GET(&diff->deltas, i);
assert(from);
/* four possible outcomes here:
* 1. old DELETED and if over rename threshold,
* new becomes RENAMED and old goes away
* 2. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and
* 2. old SPLIT and if over rename threshold,
* new becomes RENAMED and old becomes ADDED (clear SPLIT)
* 3. old was MODIFIED but FIND_RENAMES_FROM_REWRITES is on and
* old is more similar to new than it is to itself, in which
* case, new becomes RENAMED and old becomed ADDED
* 3. otherwise if over copy threshold, new becomes COPIED
* 4. otherwise if over copy threshold, new becomes COPIED
*/
if (from->status == GIT_DELTA_DELETED) {
......@@ -430,8 +575,27 @@ int git_diff_find_similar(
to->status = GIT_DELTA_RENAMED;
memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
from->status = GIT_DELTA__TO_DELETE;
num_changes++;
from->flags |= GIT_DIFF_FLAG__TO_DELETE;
num_rewrites++;
continue;
}
if (from->status == GIT_DELTA_MODIFIED &&
(from->flags & GIT_DIFF_FLAG__TO_SPLIT) != 0)
{
if (to->similarity < opts.rename_threshold) {
to->similarity = 0;
continue;
}
to->status = GIT_DELTA_RENAMED;
memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
from->status = GIT_DELTA_ADDED;
from->flags &= ~GIT_DIFF_FLAG__TO_SPLIT;
memset(&from->old_file, 0, sizeof(from->old_file));
num_rewrites--;
continue;
}
......@@ -440,17 +604,22 @@ int git_diff_find_similar(
FLAG_SET(opts, GIT_DIFF_FIND_RENAMES_FROM_REWRITES) &&
to->similarity > opts.rename_threshold)
{
similarity = 100;
/* calc_similarity(NULL, &from->old_file, from->new_file); */
similarity = similarity_measure(
diff, &opts, cache, 2 * i, 2 * i + 1);
if (similarity < 0) {
error = similarity;
goto cleanup;
}
if (similarity < opts.rename_from_rewrite_threshold) {
if ((unsigned int)similarity < opts.rename_from_rewrite_threshold) {
to->status = GIT_DELTA_RENAMED;
memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
from->status = GIT_DELTA_ADDED;
memset(&from->old_file, 0, sizeof(from->old_file));
from->old_file.path = to->old_file.path;
from->old_file.flags |= GIT_DIFF_FILE_VALID_OID;
from->old_file.flags |= GIT_DIFF_FLAG_VALID_OID;
continue;
}
......@@ -466,17 +635,26 @@ int git_diff_find_similar(
memcpy(&to->old_file, &from->old_file, sizeof(to->old_file));
}
git_vector_free(&matches);
if (num_rewrites > 0) {
assert(num_rewrites < diff->deltas.length);
if (num_changes > 0) {
assert(num_changes < diff->deltas.length);
error = apply_splits_and_deletes(
diff, diff->deltas.length - num_rewrites);
}
cleanup:
git__free(matches);
if (apply_splits_and_deletes(
diff, diff->deltas.length - num_changes) < 0)
return -1;
for (i = 0; i < cache_size; ++i) {
if (cache[i] != NULL)
opts.metric->free_signature(cache[i], opts.metric->payload);
}
git__free(cache);
return 0;
if (!given_opts || !given_opts->metric)
git__free(opts.metric);
return error;
}
#undef FLAG_SET
/*
* 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.
*/
#include "hashsig.h"
#include "fileops.h"
typedef uint32_t hashsig_t;
typedef uint64_t hashsig_state;
#define HASHSIG_SCALE 100
#define HASHSIG_HASH_WINDOW 32
#define HASHSIG_HASH_START 0
#define HASHSIG_HASH_SHIFT 5
#define HASHSIG_HASH_MASK 0x7FFFFFFF
#define HASHSIG_HEAP_SIZE ((1 << 7) - 1)
typedef int (*hashsig_cmp)(const void *a, const void *b);
typedef struct {
int size, asize;
hashsig_cmp cmp;
hashsig_t values[HASHSIG_HEAP_SIZE];
} hashsig_heap;
typedef struct {
hashsig_state state, shift_n;
char window[HASHSIG_HASH_WINDOW];
int win_len, win_pos, saw_lf;
} hashsig_in_progress;
#define HASHSIG_IN_PROGRESS_INIT { HASHSIG_HASH_START, 1, {0}, 0, 0, 1 }
struct git_hashsig {
hashsig_heap mins;
hashsig_heap maxs;
git_hashsig_option_t opt;
int considered;
};
#define HEAP_LCHILD_OF(I) (((I)*2)+1)
#define HEAP_RCHILD_OF(I) (((I)*2)+2)
#define HEAP_PARENT_OF(I) (((I)-1)>>1)
static void hashsig_heap_init(hashsig_heap *h, hashsig_cmp cmp)
{
h->size = 0;
h->asize = HASHSIG_HEAP_SIZE;
h->cmp = cmp;
}
static int hashsig_cmp_max(const void *a, const void *b)
{
hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
return (av < bv) ? -1 : (av > bv) ? 1 : 0;
}
static int hashsig_cmp_min(const void *a, const void *b)
{
hashsig_t av = *(const hashsig_t *)a, bv = *(const hashsig_t *)b;
return (av > bv) ? -1 : (av < bv) ? 1 : 0;
}
static void hashsig_heap_up(hashsig_heap *h, int el)
{
int parent_el = HEAP_PARENT_OF(el);
while (el > 0 && h->cmp(&h->values[parent_el], &h->values[el]) > 0) {
hashsig_t t = h->values[el];
h->values[el] = h->values[parent_el];
h->values[parent_el] = t;
el = parent_el;
parent_el = HEAP_PARENT_OF(el);
}
}
static void hashsig_heap_down(hashsig_heap *h, int el)
{
hashsig_t v, lv, rv;
/* 'el < h->size / 2' tests if el is bottom row of heap */
while (el < h->size / 2) {
int lel = HEAP_LCHILD_OF(el), rel = HEAP_RCHILD_OF(el), swapel;
v = h->values[el];
lv = h->values[lel];
rv = h->values[rel];
if (h->cmp(&v, &lv) < 0 && h->cmp(&v, &rv) < 0)
break;
swapel = (h->cmp(&lv, &rv) < 0) ? lel : rel;
h->values[el] = h->values[swapel];
h->values[swapel] = v;
el = swapel;
}
}
static void hashsig_heap_sort(hashsig_heap *h)
{
/* only need to do this at the end for signature comparison */
qsort(h->values, h->size, sizeof(hashsig_t), h->cmp);
}
static void hashsig_heap_insert(hashsig_heap *h, hashsig_t val)
{
/* if heap is full, pop top if new element should replace it */
if (h->size == h->asize && h->cmp(&val, &h->values[0]) > 0) {
h->size--;
h->values[0] = h->values[h->size];
hashsig_heap_down(h, 0);
}
/* if heap is not full, insert new element */
if (h->size < h->asize) {
h->values[h->size++] = val;
hashsig_heap_up(h, h->size - 1);
}
}
GIT_INLINE(bool) hashsig_include_char(
char ch, git_hashsig_option_t opt, int *saw_lf)
{
if ((opt & GIT_HASHSIG_IGNORE_WHITESPACE) && git__isspace(ch))
return false;
if (opt & GIT_HASHSIG_SMART_WHITESPACE) {
if (ch == '\r' || (*saw_lf && git__isspace(ch)))
return false;
*saw_lf = (ch == '\n');
}
return true;
}
static void hashsig_initial_window(
git_hashsig *sig,
const char **data,
size_t size,
hashsig_in_progress *prog)
{
hashsig_state state, shift_n;
int win_len;
const char *scan, *end;
/* init until we have processed at least HASHSIG_HASH_WINDOW data */
if (prog->win_len >= HASHSIG_HASH_WINDOW)
return;
state = prog->state;
win_len = prog->win_len;
shift_n = prog->shift_n;
scan = *data;
end = scan + size;
while (scan < end && win_len < HASHSIG_HASH_WINDOW) {
char ch = *scan++;
if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf))
continue;
state = (state * HASHSIG_HASH_SHIFT + ch) & HASHSIG_HASH_MASK;
if (!win_len)
shift_n = 1;
else
shift_n = (shift_n * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK;
prog->window[win_len++] = ch;
}
/* insert initial hash if we just finished */
if (win_len == HASHSIG_HASH_WINDOW) {
hashsig_heap_insert(&sig->mins, state);
hashsig_heap_insert(&sig->maxs, state);
sig->considered = 1;
}
prog->state = state;
prog->win_len = win_len;
prog->shift_n = shift_n;
*data = scan;
}
static int hashsig_add_hashes(
git_hashsig *sig,
const char *data,
size_t size,
hashsig_in_progress *prog)
{
const char *scan = data, *end = data + size;
hashsig_state state, shift_n, rmv;
if (prog->win_len < HASHSIG_HASH_WINDOW)
hashsig_initial_window(sig, &scan, size, prog);
state = prog->state;
shift_n = prog->shift_n;
/* advance window, adding new chars and removing old */
for (; scan < end; ++scan) {
char ch = *scan;
if (!hashsig_include_char(ch, sig->opt, &prog->saw_lf))
continue;
rmv = shift_n * prog->window[prog->win_pos];
state = (state - rmv) & HASHSIG_HASH_MASK;
state = (state * HASHSIG_HASH_SHIFT) & HASHSIG_HASH_MASK;
state = (state + ch) & HASHSIG_HASH_MASK;
hashsig_heap_insert(&sig->mins, state);
hashsig_heap_insert(&sig->maxs, state);
sig->considered++;
prog->window[prog->win_pos] = ch;
prog->win_pos = (prog->win_pos + 1) % HASHSIG_HASH_WINDOW;
}
prog->state = state;
return 0;
}
static int hashsig_finalize_hashes(git_hashsig *sig)
{
if (sig->mins.size < HASHSIG_HEAP_SIZE) {
giterr_set(GITERR_INVALID,
"File too small for similarity signature calculation");
return GIT_EBUFS;
}
hashsig_heap_sort(&sig->mins);
hashsig_heap_sort(&sig->maxs);
return 0;
}
static git_hashsig *hashsig_alloc(git_hashsig_option_t opts)
{
git_hashsig *sig = git__calloc(1, sizeof(git_hashsig));
if (!sig)
return NULL;
hashsig_heap_init(&sig->mins, hashsig_cmp_min);
hashsig_heap_init(&sig->maxs, hashsig_cmp_max);
sig->opt = opts;
return sig;
}
int git_hashsig_create(
git_hashsig **out,
const char *buf,
size_t buflen,
git_hashsig_option_t opts)
{
int error;
hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT;
git_hashsig *sig = hashsig_alloc(opts);
GITERR_CHECK_ALLOC(sig);
error = hashsig_add_hashes(sig, buf, buflen, &prog);
if (!error)
error = hashsig_finalize_hashes(sig);
if (!error)
*out = sig;
else
git_hashsig_free(sig);
return error;
}
int git_hashsig_create_fromfile(
git_hashsig **out,
const char *path,
git_hashsig_option_t opts)
{
char buf[4096];
ssize_t buflen = 0;
int error = 0, fd;
hashsig_in_progress prog = HASHSIG_IN_PROGRESS_INIT;
git_hashsig *sig = hashsig_alloc(opts);
GITERR_CHECK_ALLOC(sig);
if ((fd = git_futils_open_ro(path)) < 0) {
git__free(sig);
return fd;
}
while (!error) {
if ((buflen = p_read(fd, buf, sizeof(buf))) <= 0) {
if ((error = buflen) < 0)
giterr_set(GITERR_OS,
"Read error on '%s' calculating similarity hashes", path);
break;
}
error = hashsig_add_hashes(sig, buf, buflen, &prog);
}
p_close(fd);
if (!error)
error = hashsig_finalize_hashes(sig);
if (!error)
*out = sig;
else
git_hashsig_free(sig);
return error;
}
void git_hashsig_free(git_hashsig *sig)
{
git__free(sig);
}
static int hashsig_heap_compare(const hashsig_heap *a, const hashsig_heap *b)
{
int matches = 0, i, j, cmp;
assert(a->cmp == b->cmp);
/* hash heaps are sorted - just look for overlap vs total */
for (i = 0, j = 0; i < a->size && j < b->size; ) {
cmp = a->cmp(&a->values[i], &b->values[j]);
if (cmp < 0)
++i;
else if (cmp > 0)
++j;
else {
++i; ++j; ++matches;
}
}
return HASHSIG_SCALE * (matches * 2) / (a->size + b->size);
}
int git_hashsig_compare(const git_hashsig *a, const git_hashsig *b)
{
return (hashsig_heap_compare(&a->mins, &b->mins) +
hashsig_heap_compare(&a->maxs, &b->maxs)) / 2;
}
/*
* 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.
*/
#ifndef INCLUDE_hashsig_h__
#define INCLUDE_hashsig_h__
#include "common.h"
/**
* Similarity signature of line hashes for a buffer
*/
typedef struct git_hashsig git_hashsig;
typedef enum {
GIT_HASHSIG_NORMAL = 0, /* use all data */
GIT_HASHSIG_IGNORE_WHITESPACE = 1, /* ignore whitespace */
GIT_HASHSIG_SMART_WHITESPACE = 2, /* ignore \r and all space after \n */
} git_hashsig_option_t;
/**
* Build a similarity signature for a buffer
*
* If you have passed a whitespace-ignoring buffer, then the whitespace
* will be removed from the buffer while it is being processed, modifying
* the buffer in place. Sorry about that!
*
* This will return an error if the buffer doesn't contain enough data to
* compute a valid signature.
*
* @param out The array of hashed runs representing the file content
* @param buf The contents of the file to hash
* @param buflen The length of the data at `buf`
* @param generate_pairwise_hashes Should pairwise runs be hashed
*/
extern int git_hashsig_create(
git_hashsig **out,
const char *buf,
size_t buflen,
git_hashsig_option_t opts);
/**
* Build a similarity signature from a file
*
* This walks through the file, only loading a maximum of 4K of file data at
* a time. Otherwise, it acts just like `git_hashsig_create`.
*
* This will return an error if the file doesn't contain enough data to
* compute a valid signature.
*/
extern int git_hashsig_create_fromfile(
git_hashsig **out,
const char *path,
git_hashsig_option_t opts);
/**
* Release memory for a content similarity signature
*/
extern void git_hashsig_free(git_hashsig *sig);
/**
* Measure similarity between two files
*
* @return <0 for error, [0 to 100] as similarity score
*/
extern int git_hashsig_compare(
const git_hashsig *a,
const git_hashsig *b);
#endif
#include "clar_libgit2.h"
#include "buffer.h"
#include "buf_text.h"
#include "hashsig.h"
#include "fileops.h"
#define TESTSTR "Have you seen that? Have you seeeen that??"
const char *test_string = TESTSTR;
......@@ -730,3 +732,175 @@ void test_core_buffer__classify_with_utf8(void)
cl_assert(git_buf_text_is_binary(&b));
cl_assert(git_buf_text_contains_nul(&b));
}
#define SIMILARITY_TEST_DATA_1 \
"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n" \
"is this enough?\nthere has to be enough data to fill the hash array!\n" \
"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n" \
"Let's make sure we've got plenty to go with here.\n smile \n"
void test_core_buffer__similarity_metric(void)
{
git_hashsig *a, *b;
git_buf buf = GIT_BUF_INIT;
int sim;
/* in the first case, we compare data to itself and expect 100% match */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_assert_equal_i(100, git_hashsig_compare(a, b));
git_hashsig_free(a);
git_hashsig_free(b);
/* if we change just a single byte, how much does that change magnify? */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf,
"Test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
"is this enough?\nthere has to be enough data to fill the hash array!\n"
"Apparently 191 bytes is the minimum amount of data needed.\nHere goes!\n"
"Let's make sure we've got plenty to go with here.\n smile \n"));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
cl_assert(95 < sim && sim < 100); /* expect >95% similarity */
git_hashsig_free(a);
git_hashsig_free(b);
/* let's try comparing data to a superset of itself */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1
"and if I add some more, it should still be pretty similar, yes?\n"));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
cl_assert(70 < sim && sim < 80); /* expect in the 70-80% similarity range */
git_hashsig_free(a);
git_hashsig_free(b);
/* what if we keep about half the original data and add half new */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_buf_sets(&buf,
"test data\nright here\ninline\ntada\nneeds more data\nlots of data\n"
"is this enough?\nthere has to be enough data to fill the hash array!\n"
"okay, that's half the original\nwhat else can we add?\nmore data\n"
"one more line will complete this\nshort\nlines\ndon't\nmatter\n"));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
sim = git_hashsig_compare(a, b);
cl_assert(40 < sim && sim < 60); /* expect in the 40-60% similarity range */
git_hashsig_free(a);
git_hashsig_free(b);
/* lastly, let's check that we can hash file content as well */
cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH));
cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
cl_git_pass(git_hashsig_create_fromfile(
&b, "scratch/testdata", GIT_HASHSIG_NORMAL));
cl_assert_equal_i(100, git_hashsig_compare(a, b));
git_hashsig_free(a);
git_hashsig_free(b);
git_buf_free(&buf);
git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES);
}
void test_core_buffer__similarity_metric_whitespace(void)
{
git_hashsig *a, *b;
git_buf buf = GIT_BUF_INIT;
int sim, i, j;
git_hashsig_option_t opt;
const char *tabbed =
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
" separator = sep[s];\n"
" expect = expect_values[s];\n"
"\n"
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
" git_buf_join(&buf, separator, a[i], b[j]);\n"
" cl_assert_equal_s(*expect, buf.ptr);\n"
" expect++;\n"
" }\n"
" }\n"
" }\n";
const char *spaced =
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
" separator = sep[s];\n"
" expect = expect_values[s];\n"
"\n"
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
" git_buf_join(&buf, separator, a[i], b[j]);\n"
" cl_assert_equal_s(*expect, buf.ptr);\n"
" expect++;\n"
" }\n"
" }\n"
" }\n";
const char *crlf_spaced2 =
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
" separator = sep[s];\r\n"
" expect = expect_values[s];\r\n"
"\r\n"
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
" git_buf_join(&buf, separator, a[i], b[j]);\r\n"
" cl_assert_equal_s(*expect, buf.ptr);\r\n"
" expect++;\r\n"
" }\r\n"
" }\r\n"
" }\r\n";
const char *text[3] = { tabbed, spaced, crlf_spaced2 };
/* let's try variations of our own code with whitespace changes */
for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) {
for (i = 0; i < 3; ++i) {
for (j = 0; j < 3; ++j) {
cl_git_pass(git_buf_sets(&buf, text[i]));
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
cl_git_pass(git_buf_sets(&buf, text[j]));
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
sim = git_hashsig_compare(a, b);
if (opt == GIT_HASHSIG_NORMAL) {
if (i == j)
cl_assert_equal_i(100, sim);
else
cl_assert(sim < 30); /* expect pretty different */
} else {
cl_assert_equal_i(100, sim);
}
git_hashsig_free(a);
git_hashsig_free(b);
}
}
}
git_buf_free(&buf);
}
......@@ -196,7 +196,7 @@ void test_diff_blob__can_compare_identical_blobs(void)
NULL, NULL, &opts, diff_file_cb, diff_hunk_cb, diff_line_cb, &expected));
cl_assert_equal_i(0, expected.files_binary);
assert_identical_blobs_comparison(&expected);
cl_assert_equal_i(0, expected.files); /* NULLs mean no callbacks, period */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blobs(
......@@ -399,7 +399,7 @@ void test_diff_blob__can_compare_blob_to_buffer(void)
assert_identical_blobs_comparison(&expected);
/* diff from NULL blob to content of b */
/* diff from NULL blob to content of a */
memset(&expected, 0, sizeof(expected));
cl_git_pass(git_diff_blob_to_buffer(
NULL, a_content, strlen(a_content),
......
......@@ -32,7 +32,7 @@ int diff_file_cb(
e->files++;
if (delta->binary)
if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
e->files_binary++;
cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);
......@@ -126,7 +126,8 @@ int diff_foreach_via_iterator(
/* if there are no changes, then the patch will be NULL */
if (!patch) {
cl_assert(delta->status == GIT_DELTA_UNMODIFIED || delta->binary == 1);
cl_assert(delta->status == GIT_DELTA_UNMODIFIED ||
(delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
continue;
}
......
......@@ -152,8 +152,8 @@ void test_diff_diffiter__max_size_threshold(void)
file_count++;
hunk_count += (int)git_diff_patch_num_hunks(patch);
assert(delta->binary == 0 || delta->binary == 1);
binary_count += delta->binary;
assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
git_diff_patch_free(patch);
}
......@@ -185,8 +185,8 @@ void test_diff_diffiter__max_size_threshold(void)
file_count++;
hunk_count += (int)git_diff_patch_num_hunks(patch);
assert(delta->binary == 0 || delta->binary == 1);
binary_count += delta->binary;
assert((delta->flags & (GIT_DIFF_FLAG_BINARY|GIT_DIFF_FLAG_NOT_BINARY)) != 0);
binary_count += ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
git_diff_patch_free(patch);
}
......
......@@ -144,11 +144,11 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
size_t hdrlen, hunklen, textlen;
char origin;
int oldno, newno;
const char *new_content = "The Song of Seven Cities\n========================\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were—the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them—\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built—\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n";
const char *new_content = "The Song of Seven Cities\n------------------------\n\nI WAS Lord of Cities very sumptuously builded.\nSeven roaring Cities paid me tribute from afar.\nIvory their outposts were--the guardrooms of them gilded,\nAnd garrisoned with Amazons invincible in war.\n\nThis is some new text;\nNot as good as the old text;\nBut here it is.\n\nSo they warred and trafficked only yesterday, my Cities.\nTo-day there is no mark or mound of where my Cities stood.\nFor the River rose at midnight and it washed away my Cities.\nThey are evened with Atlantis and the towns before the Flood.\n\nRain on rain-gorged channels raised the water-levels round them,\nFreshet backed on freshet swelled and swept their world from sight,\nTill the emboldened floods linked arms and, flashing forward, drowned them--\nDrowned my Seven Cities and their peoples in one night!\n\nLow among the alders lie their derelict foundations,\nThe beams wherein they trusted and the plinths whereon they built--\nMy rulers and their treasure and their unborn populations,\nDead, destroyed, aborted, and defiled with mud and silt!\n\nAnother replacement;\nBreaking up the poem;\nGenerating some hunks.\n\nTo the sound of trumpets shall their seed restore my Cities\nWealthy and well-weaponed, that once more may I behold\nAll the world go softly when it walks before my Cities,\nAnd the horses and the chariots fleeing from them as of old!\n\n -- Rudyard Kipling\n";
g_repo = cl_git_sandbox_init("renames");
cl_git_rewritefile("renames/songofseven.txt", new_content);
cl_git_rewritefile("renames/songof7cities.txt", new_content);
cl_git_pass(git_repository_head_tree(&head, g_repo));
......@@ -178,14 +178,14 @@ void test_diff_patch__hunks_have_correct_line_numbers(void)
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 0));
cl_assert_equal_i(GIT_DIFF_LINE_CONTEXT, (int)origin);
cl_assert(strncmp("Ivory their outposts werethe guardrooms of them gilded,\n", text, textlen) == 0);
cl_assert(strncmp("Ivory their outposts were--the guardrooms of them gilded,\n", text, textlen) == 0);
cl_assert_equal_i(6, oldno);
cl_assert_equal_i(6, newno);
cl_git_pass(git_diff_patch_get_line_in_hunk(
&origin, &text, &textlen, &oldno, &newno, patch, 0, 3));
cl_assert_equal_i(GIT_DIFF_LINE_DELETION, (int)origin);
cl_assert(strncmp("All the world went softly when it walked before my Cities\n", text, textlen) == 0);
cl_assert(strncmp("All the world went softly when it walked before my Cities--\n", text, textlen) == 0);
cl_assert_equal_i(9, oldno);
cl_assert_equal_i(-1, newno);
......@@ -271,32 +271,32 @@ void test_diff_patch__line_counts_with_eofnl(void)
g_repo = cl_git_sandbox_init("renames");
cl_git_pass(git_futils_readbuffer(&content, "renames/songofseven.txt"));
cl_git_pass(git_futils_readbuffer(&content, "renames/songof7cities.txt"));
/* remove first line */
end = git_buf_cstr(&content) + git_buf_find(&content, '\n') + 1;
git_buf_consume(&content, end);
cl_git_rewritefile("renames/songofseven.txt", content.ptr);
cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
check_single_patch_stats(g_repo, 1, 0, 1);
/* remove trailing whitespace */
git_buf_rtrim(&content);
cl_git_rewritefile("renames/songofseven.txt", content.ptr);
cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
check_single_patch_stats(g_repo, 2, 1, 2);
/* add trailing whitespace */
cl_git_pass(git_repository_index(&index, g_repo));
cl_git_pass(git_index_add_bypath(index, "songofseven.txt"));
cl_git_pass(git_index_add_bypath(index, "songof7cities.txt"));
cl_git_pass(git_index_write(index));
git_index_free(index);
cl_git_pass(git_buf_putc(&content, '\n'));
cl_git_rewritefile("renames/songofseven.txt", content.ptr);
cl_git_rewritefile("renames/songof7cities.txt", content.ptr);
check_single_patch_stats(g_repo, 1, 1, 1);
......
......@@ -23,8 +23,16 @@ void test_diff_rename__cleanup(void)
* serving.txt -> sixserving.txt (rename, no change, 100% match)
* sevencities.txt -> sevencities.txt (no change)
* sevencities.txt -> songofseven.txt (copy, no change, 100% match)
*
* TODO: add commits with various % changes of copy / rename
* commit 1c068dee5790ef1580cfc4cd670915b48d790084
* songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
* sixserving.txt -> sixserving.txt (indentation change)
* sixserving.txt -> ikeepsix.txt (copy, add title, >80% match)
* sevencities.txt (no change)
* commit 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
* songofseven.txt -> untimely.txt (rename, convert to crlf)
* ikeepsix.txt -> ikeepsix.txt (reorder sections in file)
* sixserving.txt -> sixserving.txt (whitespace change - not just indent)
* sevencities.txt -> songof7cities.txt (rename, small text changes)
*/
void test_diff_rename__match_oid(void)
......@@ -133,3 +141,219 @@ void test_diff_rename__checks_options_version(void)
git_tree_free(old_tree);
git_tree_free(new_tree);
}
void test_diff_rename__not_exact_match(void)
{
const char *sha0 = "2bc7f351d20b53f1c72c16c4b036e491c478c49a";
const char *sha1 = "1c068dee5790ef1580cfc4cd670915b48d790084";
const char *sha2 = "19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13";
git_tree *old_tree, *new_tree;
git_diff_list *diff;
git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT;
git_diff_find_options opts = GIT_DIFF_FIND_OPTIONS_INIT;
diff_expects exp;
/* == Changes =====================================================
* songofseven.txt -> songofseven.txt (major rewrite, <20% match - split)
* sixserving.txt -> sixserving.txt (indentation change)
* sixserving.txt -> ikeepsix.txt (copy, add title, >80% match)
* sevencities.txt (no change)
*/
old_tree = resolve_commit_oid_to_tree(g_repo, sha0);
new_tree = resolve_commit_oid_to_tree(g_repo, sha1);
/* Must pass GIT_DIFF_INCLUDE_UNMODIFIED if you expect to emulate
* --find-copies-harder during rename transformion...
*/
diffopts.flags |= GIT_DIFF_INCLUDE_UNMODIFIED;
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
/* git diff --no-renames \
* 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
* 1c068dee5790ef1580cfc4cd670915b48d790084
*/
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
/* git diff -M 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
* 1c068dee5790ef1580cfc4cd670915b48d790084
*
* must not pass NULL for opts because it will pick up environment
* values for "diff.renames" and test won't be consistent.
*/
opts.flags = GIT_DIFF_FIND_RENAMES;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
git_diff_list_free(diff);
/* git diff -M -C \
* 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
* 1c068dee5790ef1580cfc4cd670915b48d790084
*/
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
git_diff_list_free(diff);
/* git diff -M -C --find-copies-harder --break-rewrites \
* 2bc7f351d20b53f1c72c16c4b036e491c478c49a \
* 1c068dee5790ef1580cfc4cd670915b48d790084
*/
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_ALL;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(5, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_UNMODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_COPIED]);
git_diff_list_free(diff);
/* == Changes =====================================================
* songofseven.txt -> untimely.txt (rename, convert to crlf)
* ikeepsix.txt -> ikeepsix.txt (reorder sections in file)
* sixserving.txt -> sixserving.txt (whitespace - not just indent)
* sevencities.txt -> songof7cities.txt (rename, small text changes)
*/
git_tree_free(old_tree);
old_tree = new_tree;
new_tree = resolve_commit_oid_to_tree(g_repo, sha2);
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
/* git diff --no-renames \
* 1c068dee5790ef1580cfc4cd670915b48d790084 \
* 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
*/
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(6, exp.files);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
/* git diff -M -C \
* 1c068dee5790ef1580cfc4cd670915b48d790084 \
* 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
*/
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_RENAMES | GIT_DIFF_FIND_COPIES;
cl_git_pass(git_diff_find_similar(diff, &opts));
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
git_diff_list_free(diff);
/* git diff -M -C --find-copies-harder --break-rewrites \
* 1c068dee5790ef1580cfc4cd670915b48d790084 \
* 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
* with libgit2 default similarity comparison...
*/
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_ALL;
cl_git_pass(git_diff_find_similar(diff, &opts));
/* the default match algorithm is going to find the internal
* whitespace differences in the lines of sixserving.txt to be
* significant enough that this will decide to split it into
* an ADD and a DELETE
*/
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(5, exp.files);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_ADDED]);
cl_assert_equal_i(1, exp.file_status[GIT_DELTA_DELETED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
git_diff_list_free(diff);
/* git diff -M -C --find-copies-harder --break-rewrites \
* 1c068dee5790ef1580cfc4cd670915b48d790084 \
* 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
* with ignore_space whitespace comparision
*/
cl_git_pass(git_diff_tree_to_tree(
&diff, g_repo, old_tree, new_tree, &diffopts));
opts.flags = GIT_DIFF_FIND_ALL | GIT_DIFF_FIND_IGNORE_WHITESPACE;
cl_git_pass(git_diff_find_similar(diff, &opts));
/* Ignoring whitespace, this should no longer split sixserver.txt */
memset(&exp, 0, sizeof(exp));
cl_git_pass(git_diff_foreach(
diff, diff_file_cb, diff_hunk_cb, diff_line_cb, &exp));
cl_assert_equal_i(4, exp.files);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
cl_assert_equal_i(2, exp.file_status[GIT_DELTA_RENAMED]);
git_diff_list_free(diff);
git_tree_free(old_tree);
git_tree_free(new_tree);
}
void test_diff_rename__working_directory_changes(void)
{
/* let's rewrite some files in the working directory on demand */
/* and with / without CRLF changes */
}
0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer <rb@github.com> 1351024687 -0700 commit (initial): Initial commit
31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer <rb@github.com> 1351024817 -0700 commit: copy and rename with no change
2bc7f351d20b53f1c72c16c4b036e491c478c49a 1c068dee5790ef1580cfc4cd670915b48d790084 Russell Belfer <rb@github.com> 1361485758 -0800 commit: rewrites, copies with changes, etc.
1c068dee5790ef1580cfc4cd670915b48d790084 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 Russell Belfer <rb@github.com> 1361486360 -0800 commit: more renames and smallish modifications
0000000000000000000000000000000000000000 31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 Russell Belfer <rb@github.com> 1351024687 -0700 commit (initial): Initial commit
31e47d8c1fa36d7f8d537b96158e3f024de0a9f2 2bc7f351d20b53f1c72c16c4b036e491c478c49a Russell Belfer <rb@github.com> 1351024817 -0700 commit: copy and rename with no change
2bc7f351d20b53f1c72c16c4b036e491c478c49a 1c068dee5790ef1580cfc4cd670915b48d790084 Russell Belfer <rb@github.com> 1361485758 -0800 commit: rewrites, copies with changes, etc.
1c068dee5790ef1580cfc4cd670915b48d790084 19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13 Russell Belfer <rb@github.com> 1361486360 -0800 commit: more renames and smallish modifications
2bc7f351d20b53f1c72c16c4b036e491c478c49a
19dd32dfb1520a64e5bbaae8dce6ef423dfa2f13
I Keep Six Honest Serving-Men
=============================
She sends'em abroad on her own affairs,
From the second she opens her eyes—
One million Hows, two million Wheres,
And seven million Whys!
I let them rest from nine till five,
For I am busy then,
As well as breakfast, lunch, and tea,
For they are hungry men.
But different folk have different views;
I know a person small—
She keeps ten million serving-men,
Who get no rest at all!
-- Rudyard Kipling
I KEEP six honest serving-men
(They taught me all I knew);
Their names are What and Why and When
And How and Where and Who.
I send them over land and sea,
I send them east and west;
But after they have worked for me,
I give them all a rest.
I KEEP six honest serving-men
(They taught me all I knew);
Their names are What and Why and When
And How and Where and Who.
I send them over land and sea,
I send them east and west;
But after they have worked for me,
I give them all a rest.
I KEEP six honest serving-men
(They taught me all I knew);
Their names are What and Why and When
And How and Where and Who.
I send them over land and sea,
I send them east and west;
But after they have worked for me,
I give them all a rest.
I let them rest from nine till five,
For I am busy then,
As well as breakfast, lunch, and tea,
For they are hungry men.
But different folk have different views;
I know a person small—
She keeps ten million serving-men,
Who get no rest at all!
I let them rest from nine till five,
For I am busy then,
As well as breakfast, lunch, and tea,
For they are hungry men.
But different folk have different views;
I know a person small—
She keeps ten million serving-men,
Who get no rest at all!
She sends'em abroad on her own affairs,
From the second she opens her eyes—
One million Hows, two million Wheres,
And seven million Whys!
She sends'em abroad on her own affairs,
From the second she opens her eyes—
One million Hows, two million Wheres,
And seven million Whys!
-- Rudyard Kipling
-- Rudyard Kipling
The Song of Seven Cities
========================
------------------------
I WAS Lord of Cities very sumptuously builded.
Seven roaring Cities paid me tribute from afar.
Ivory their outposts werethe guardrooms of them gilded,
Ivory their outposts were--the guardrooms of them gilded,
And garrisoned with Amazons invincible in war.
All the world went softly when it walked before my Cities
All the world went softly when it walked before my Cities--
Neither King nor Army vexed my peoples at their toil,
Never horse nor chariot irked or overbore my Cities,
Never Mob nor Ruler questioned whence they drew their spoil.
......@@ -23,20 +23,20 @@ They are evened with Atlantis and the towns before the Flood.
Rain on rain-gorged channels raised the water-levels round them,
Freshet backed on freshet swelled and swept their world from sight,
Till the emboldened floods linked arms and, flashing forward, drowned them
Till the emboldened floods linked arms and, flashing forward, drowned them--
Drowned my Seven Cities and their peoples in one night!
Low among the alders lie their derelict foundations,
The beams wherein they trusted and the plinths whereon they built
The beams wherein they trusted and the plinths whereon they built--
My rulers and their treasure and their unborn populations,
Dead, destroyed, aborted, and defiled with mud and silt!
The Daughters of the Palace whom they cherished in my Cities,
My silver-tongued Princesses, and the promise of their May
Their bridegrooms of the June-tideall have perished in my Cities,
My silver-tongued Princesses, and the promise of their May--
Their bridegrooms of the June-tide--all have perished in my Cities,
With the harsh envenomed virgins that can neither love nor play.
I was Lord of CitiesI will build anew my Cities,
I was Lord of Cities--I will build anew my Cities,
Seven, set on rocks, above the wrath of any flood.
Nor will I rest from search till I have filled anew my Cities
With peoples undefeated of the dark, enduring blood.
......@@ -46,4 +46,4 @@ Wealthy and well-weaponed, that once more may I behold
All the world go softly when it walks before my Cities,
And the horses and the chariots fleeing from them as of old!
-- Rudyard Kipling
-- Rudyard Kipling
The Song of Seven Cities
========================
I WAS Lord of Cities very sumptuously builded.
Seven roaring Cities paid me tribute from afar.
Ivory their outposts were—the guardrooms of them gilded,
And garrisoned with Amazons invincible in war.
All the world went softly when it walked before my Cities—
Neither King nor Army vexed my peoples at their toil,
Never horse nor chariot irked or overbore my Cities,
Never Mob nor Ruler questioned whence they drew their spoil.
Banded, mailed and arrogant from sunrise unto sunset;
Singing while they sacked it, they possessed the land at large.
Yet when men would rob them, they resisted, they made onset
And pierced the smoke of battle with a thousand-sabred charge.
So they warred and trafficked only yesterday, my Cities.
To-day there is no mark or mound of where my Cities stood.
For the River rose at midnight and it washed away my Cities.
They are evened with Atlantis and the towns before the Flood.
Rain on rain-gorged channels raised the water-levels round them,
Freshet backed on freshet swelled and swept their world from sight,
Till the emboldened floods linked arms and, flashing forward, drowned them—
Drowned my Seven Cities and their peoples in one night!
Low among the alders lie their derelict foundations,
The beams wherein they trusted and the plinths whereon they built—
My rulers and their treasure and their unborn populations,
Dead, destroyed, aborted, and defiled with mud and silt!
The Daughters of the Palace whom they cherished in my Cities,
My silver-tongued Princesses, and the promise of their May—
Their bridegrooms of the June-tide—all have perished in my Cities,
With the harsh envenomed virgins that can neither love nor play.
I was Lord of Cities—I will build anew my Cities,
Seven, set on rocks, above the wrath of any flood.
Nor will I rest from search till I have filled anew my Cities
With peoples undefeated of the dark, enduring blood.
To the sound of trumpets shall their seed restore my Cities
Wealthy and well-weaponed, that once more may I behold
All the world go softly when it walks before my Cities,
And the horses and the chariots fleeing from them as of old!
-- Rudyard Kipling
Untimely
========
Nothing in life has been made by man for man's using
But it was shown long since to man in ages
Lost as the name of the maker of it,
Who received oppression and shame for his wages--
Hate, avoidance, and scorn in his daily dealings--
Until he perished, wholly confounded
More to be pitied than he are the wise
Souls which foresaw the evil of loosing
Knowledge or Art before time, and aborted
Noble devices and deep-wrought healings,
Lest offense should arise.
Heaven delivers on earth the Hour that cannot be
thwarted,
Neither advanced, at the price of a world nor a soul,
and its Prophet
Comes through the blood of the vanguards who
dreamed--too soon--it had sounded.
-- Rudyard Kipling
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment