From 2532c903439d3116833146a89221c91a0023c3fe Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 5 Feb 2013 15:01:30 -0800 Subject: [PATCH 01/35] Initial blame API --- include/git2.h | 1 + include/git2/blame.h | 145 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 include/git2/blame.h diff --git a/include/git2.h b/include/git2.h index 5f9fc4824..0623a77a7 100644 --- a/include/git2.h +++ b/include/git2.h @@ -56,5 +56,6 @@ #include "git2/message.h" #include "git2/pack.h" #include "git2/stash.h" +#include "git2/blame.h" #endif diff --git a/include/git2/blame.h b/include/git2/blame.h new file mode 100644 index 000000000..871806792 --- /dev/null +++ b/include/git2/blame.h @@ -0,0 +1,145 @@ +/* + * 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_git_blame_h__ +#define INCLUDE_git_blame_h__ + +#include "common.h" +#include "oid.h" + +/** + * @file git2/blame.h + * @brief Git blame routines + * @defgroup git_blame Git blame routines + * @ingroup Git + * @{ + */ +GIT_BEGIN_DECL + +/** + * Flags for indicating option behavior for git_blame APIs. + */ +typedef enum { + /** Normal blame, the default */ + GIT_BLAME_NORMAL = 0, + /** Track lines that have moved within a file (like `git blame -M`) */ + GIT_BLAME_TRACK_COPIES_SAME_FILE = (1<<0), + /** Track lines that have moved across files in the same commit (like `git blame -C`) */ + GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES = (1<<1), + /** Track lines that have been copied from another file that exists in the + * same commit (like `git blame -CC`) */ + GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<1 | 1<<2), + /** Track lines that have been copied from another file that exists in *any* + * commit (like `git blame -CCC`) */ + GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<1 | 1<<2 | 1<<3), +} git_blame_flag_t; + +/** + * Blame options structure + * + * Use zeros to indicate default settings. It's easiest to use the + * `GIT_BLAME_OPTIONS_INIT` macro: + * git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + * + * - `flags` is a combination fo teh `git_blame_flag_t` values above. + * - `min_match_characters` is the lower bound on the number of alphanumeric + * characters that must be detected as moving/copying within a file for it to + * associate those lines with the parent commit. The default value is 20. + * This value only takes effect if any of the `GIT_BLAME_TRACK_COPIES_*` + * flags are specified. + * - `newest_commitish` is a rev-parse spec that resolves to the most recent + * commit to consider. The default is HEAD. + * - `oldest_commitish` is a rev-parse spec that resolves to the oldest commit + * to consider. The default is the first commit encountered with a NULL + * parent. + * - `min_line` is the first line in the file to blame. The default is 1 (line + * numbers start with 1). + * - `max_line` is the last line in the file to blame. The default is the last + * line of the file. + */ + +typedef struct git_blame_options { + unsigned int version; + + uint32_t flags; + uint16_t min_match_characters; + const char *newest_commitish; + const char *oldest_commitish; + uint32_t min_line; + uint32_t max_line; +} git_blame_options; + +#define GIT_BLAME_OPTIONS_VERSION 1 +#define GIT_BLAME_OPTIONS_INIT {GIT_BLAME_OPTIONS_VERSION} + +/** + * Structure that represents a blame hunk. + * + * - `lines_in_hunk` is the number of lines in this hunk + * - `final_commit_id` is the OID of the commit where this line was last + * changed. + * - `final_start_line_number` is the 1-based line number where this hunk + * begins, in the final version of the file + * - `orig_commit_id` is the OID of the commit where this hunk was found. This + * will usually be the same as `final_commit_id`, except when + * `GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES` has been specified. + * - `orig_path` is the path to the file where this hunk originated, as of the + * commit specified by `orig_commit_id`. + * - `orig_start_line_number` is the 1-based line number where this hunk begins + * in the file named by `orig_path` in the commit specified by + * `orig_commit_id`. + */ +typedef struct git_blame_hunk { + uint16_t lines_in_hunk; + + git_oid final_commit_id; + uint16_t final_start_line_number; + + git_oid orig_commit_id; + const char *orig_path; + uint16_t orig_start_line_number; +} git_blame_hunk; + + +/** + * Results structure for git_blame. + * + * - `hunks` is an array of hunks. + * - `hunk_count` is the number of hunk structures in `hunks`. + */ +typedef struct git_blame_results { + const git_blame_hunk * const hunks; + uint32_t hunk_count; +} git_blame_results; + +/** + * Get the blame for a single file. + * + * @param out pointer that will receive the results object + * @param path path to file to consider + * @param options options for the blame operation. If NULL, this is treated as + * though GIT_BLAME_OPTIONS_INIT were passed. + * @return 0 on success, or an error code. (use giterr_last for information + * about the error.) + */ +GIT_EXTERN(int) git_blame_file( + git_blame_results **out, + const char *path, + git_blame_options *options); + + +/** + * Free memory allocated by git_blame. + * + * @param results results structure to free + */ +GIT_EXTERN(void) git_blame_free(git_blame_results *results); + +/** @} */ +GIT_END_DECL +#endif + From 168e9d746e906a900c55e2de161d8941baf2a2d2 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 5 Feb 2013 19:24:01 -0800 Subject: [PATCH 02/35] Change API based on @arrbee's feedback --- include/git2/blame.h | 48 +++++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/include/git2/blame.h b/include/git2/blame.h index 871806792..1dfff1719 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -51,11 +51,11 @@ typedef enum { * associate those lines with the parent commit. The default value is 20. * This value only takes effect if any of the `GIT_BLAME_TRACK_COPIES_*` * flags are specified. - * - `newest_commitish` is a rev-parse spec that resolves to the most recent + * - `newest_commit` is a rev-parse spec that resolves to the most recent * commit to consider. The default is HEAD. - * - `oldest_commitish` is a rev-parse spec that resolves to the oldest commit - * to consider. The default is the first commit encountered with a NULL - * parent. + * - `newest_commit` is the newest commit to consider. The default is HEAD. + * - `oldest_commit` is the oldest commit to consider. The default is the + * first commit encountered with a NULL parent. * - `min_line` is the first line in the file to blame. The default is 1 (line * numbers start with 1). * - `max_line` is the last line in the file to blame. The default is the last @@ -67,8 +67,8 @@ typedef struct git_blame_options { uint32_t flags; uint16_t min_match_characters; - const char *newest_commitish; - const char *oldest_commitish; + const git_commit *newest_commit; + const git_commit *oldest_commit; uint32_t min_line; uint32_t max_line; } git_blame_options; @@ -105,21 +105,40 @@ typedef struct git_blame_hunk { } git_blame_hunk; +typedef struct git_blame_results git_blame_results; + /** - * Results structure for git_blame. - * - * - `hunks` is an array of hunks. - * - `hunk_count` is the number of hunk structures in `hunks`. + * Gets the number of hunks that exist in the results structure. */ -typedef struct git_blame_results { - const git_blame_hunk * const hunks; - uint32_t hunk_count; -} git_blame_results; +GIT_EXTERN(uint32_t) git_blame_results_hunk_count(git_blame_results *results); + +/** + * Gets the blame hunk at the given index. + * + * @param results the results structure to query + * @param index index of the hunk to retrieve + * @return the hunk at the given index, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk*) git_blame_results_hunk_byindex( + git_blame_results *results, + uint32_t index); + +/** + * Gets the hunk that relates to the given line number in the newest commit. + * + * @param results the results structure to query + * @param lineno the (1-based) line number to find a hunk for + * @return the hunk that contains the given line, or NULL on error + */ +GIT_EXTERN(const git_blame_hunk*) git_blame_results_hunk_byline( + git_blame_results *results, + uint32_t lineno); /** * Get the blame for a single file. * * @param out pointer that will receive the results object + * @param repo repository whose history is to be walked * @param path path to file to consider * @param options options for the blame operation. If NULL, this is treated as * though GIT_BLAME_OPTIONS_INIT were passed. @@ -128,6 +147,7 @@ typedef struct git_blame_results { */ GIT_EXTERN(int) git_blame_file( git_blame_results **out, + git_repository *repo, const char *path, git_blame_options *options); From edcb6ee649b5f18586cfa61252e371fbaa72d090 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 7 Feb 2013 10:33:56 -0800 Subject: [PATCH 03/35] Introduce git_blame_buffer --- include/git2/blame.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/git2/blame.h b/include/git2/blame.h index 1dfff1719..7f0fb2617 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -152,6 +152,20 @@ GIT_EXTERN(int) git_blame_file( git_blame_options *options); +/** + * Get blame data for a file that has been modified. + * + * @param out pointer that will receive the results object + * @param reference output from git_blame_file for the file in question + * @param buffer the (possibly) modified contents of the file + * @return 0 on success, or an error code. (use giterr_last for information + * about the error) + */ +GIT_EXTERN(int) git_blame_buffer( + git_blame_results **out, + git_blame_results *reference, + const char *buffer); + /** * Free memory allocated by git_blame. * From 1a68c168a6cdbe0db6e44fb582a7026a7d536c9d Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 7 Feb 2013 19:22:31 -0800 Subject: [PATCH 04/35] Fix spelling, remove pesky consts --- include/git2/blame.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/git2/blame.h b/include/git2/blame.h index 7f0fb2617..443b33503 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -45,7 +45,7 @@ typedef enum { * `GIT_BLAME_OPTIONS_INIT` macro: * git_blame_options opts = GIT_BLAME_OPTIONS_INIT; * - * - `flags` is a combination fo teh `git_blame_flag_t` values above. + * - `flags` is a combination of the `git_blame_flag_t` values above. * - `min_match_characters` is the lower bound on the number of alphanumeric * characters that must be detected as moving/copying within a file for it to * associate those lines with the parent commit. The default value is 20. @@ -67,8 +67,8 @@ typedef struct git_blame_options { uint32_t flags; uint16_t min_match_characters; - const git_commit *newest_commit; - const git_commit *oldest_commit; + git_commit *newest_commit; + git_commit *oldest_commit; uint32_t min_line; uint32_t max_line; } git_blame_options; From ceab4e260637dc8374d0348ee9a078ab1c16e4ad Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 16 Sep 2013 16:20:38 -0700 Subject: [PATCH 05/35] Port blame from git.git --- include/git2/blame.h | 69 +- include/git2/object.h | 19 +- src/blame.c | 492 +++++++++++++++ src/blame.h | 37 ++ src/blame_git.c | 591 ++++++++++++++++++ src/blame_git.h | 93 +++ src/object.c | 36 ++ tests-clar/blame/blame_helpers.c | 58 ++ tests-clar/blame/blame_helpers.h | 15 + tests-clar/blame/buffer.c | 130 ++++ tests-clar/blame/getters.c | 56 ++ tests-clar/blame/harder.c | 71 +++ tests-clar/blame/simple.c | 204 ++++++ tests-clar/object/lookupbypath.c | 83 +++ tests-clar/resources/blametest.git/HEAD | 1 + tests-clar/resources/blametest.git/config | 5 + .../resources/blametest.git/description | 1 + .../0c/bab4d45fd61e55a1c9697f9f9cb07a12e15448 | Bin 0 -> 46 bytes .../1a/ac69ae5d96461afc4d81d0066cb12f5b05a35b | Bin 0 -> 28 bytes .../1b/5f0775af166331c854bd8d1bca3450eaf2532a | Bin 0 -> 35 bytes .../48/2f2c370e35c2c314fc1f96db2beb33f955a26a | Bin 0 -> 35 bytes .../63/d671eb32d250e4a83766ebbc60e818c1e1e93a | 3 + .../63/eb57322e363e18d460da5ea8284f3cd2340b36 | Bin 0 -> 76 bytes .../8b/137891791fe96927ad78e64b0aad7bded08bdc | Bin 0 -> 16 bytes .../96/679d59cf9f74d69b3c920f258559b5e8c9a18a | Bin 0 -> 47 bytes .../98/89d6e5557761aa8e3607e80c874a6dc51ada7c | Bin 0 -> 43 bytes .../aa/06ecca6c4ad6432ab9313e556ca92ba4bcf9e9 | 1 + .../b1/76dfc3a4dc8734e4c579f77236a9c8d0a965d2 | Bin 0 -> 76 bytes .../b9/0bb887b7c03750ae6b352ffe76ab9d2e86ee7d | Bin 0 -> 56 bytes .../b9/9f7ac0b88909253d829554c14af488c3b0f3a5 | 2 + .../bc/7c5ac2bafe828a68e9d1d460343718d6fbe136 | 3 + .../cf/e0e1e1e3ba18f149fd47f5e1aef6016b2260c3 | Bin 0 -> 76 bytes .../d0/67729932057cdb7527a833d6799c4ddc520640 | 1 + .../da/237394e6132d20d30f175b9b73c8638fddddda | 4 + .../e5/b41c1ea533f87388ab69b13baf0b5a562d6243 | Bin 0 -> 76 bytes .../ef/32df4d259143933715c74951f932d9892364d1 | Bin 0 -> 42 bytes .../resources/blametest.git/refs/heads/master | 1 + 37 files changed, 1946 insertions(+), 30 deletions(-) create mode 100644 src/blame.c create mode 100644 src/blame.h create mode 100644 src/blame_git.c create mode 100644 src/blame_git.h create mode 100644 tests-clar/blame/blame_helpers.c create mode 100644 tests-clar/blame/blame_helpers.h create mode 100644 tests-clar/blame/buffer.c create mode 100644 tests-clar/blame/getters.c create mode 100644 tests-clar/blame/harder.c create mode 100644 tests-clar/blame/simple.c create mode 100644 tests-clar/object/lookupbypath.c create mode 100644 tests-clar/resources/blametest.git/HEAD create mode 100644 tests-clar/resources/blametest.git/config create mode 100644 tests-clar/resources/blametest.git/description create mode 100644 tests-clar/resources/blametest.git/objects/0c/bab4d45fd61e55a1c9697f9f9cb07a12e15448 create mode 100644 tests-clar/resources/blametest.git/objects/1a/ac69ae5d96461afc4d81d0066cb12f5b05a35b create mode 100644 tests-clar/resources/blametest.git/objects/1b/5f0775af166331c854bd8d1bca3450eaf2532a create mode 100644 tests-clar/resources/blametest.git/objects/48/2f2c370e35c2c314fc1f96db2beb33f955a26a create mode 100644 tests-clar/resources/blametest.git/objects/63/d671eb32d250e4a83766ebbc60e818c1e1e93a create mode 100644 tests-clar/resources/blametest.git/objects/63/eb57322e363e18d460da5ea8284f3cd2340b36 create mode 100644 tests-clar/resources/blametest.git/objects/8b/137891791fe96927ad78e64b0aad7bded08bdc create mode 100644 tests-clar/resources/blametest.git/objects/96/679d59cf9f74d69b3c920f258559b5e8c9a18a create mode 100644 tests-clar/resources/blametest.git/objects/98/89d6e5557761aa8e3607e80c874a6dc51ada7c create mode 100644 tests-clar/resources/blametest.git/objects/aa/06ecca6c4ad6432ab9313e556ca92ba4bcf9e9 create mode 100644 tests-clar/resources/blametest.git/objects/b1/76dfc3a4dc8734e4c579f77236a9c8d0a965d2 create mode 100644 tests-clar/resources/blametest.git/objects/b9/0bb887b7c03750ae6b352ffe76ab9d2e86ee7d create mode 100644 tests-clar/resources/blametest.git/objects/b9/9f7ac0b88909253d829554c14af488c3b0f3a5 create mode 100644 tests-clar/resources/blametest.git/objects/bc/7c5ac2bafe828a68e9d1d460343718d6fbe136 create mode 100644 tests-clar/resources/blametest.git/objects/cf/e0e1e1e3ba18f149fd47f5e1aef6016b2260c3 create mode 100644 tests-clar/resources/blametest.git/objects/d0/67729932057cdb7527a833d6799c4ddc520640 create mode 100644 tests-clar/resources/blametest.git/objects/da/237394e6132d20d30f175b9b73c8638fddddda create mode 100644 tests-clar/resources/blametest.git/objects/e5/b41c1ea533f87388ab69b13baf0b5a562d6243 create mode 100644 tests-clar/resources/blametest.git/objects/ef/32df4d259143933715c74951f932d9892364d1 create mode 100644 tests-clar/resources/blametest.git/refs/heads/master diff --git a/include/git2/blame.h b/include/git2/blame.h index 443b33503..fe1311362 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -36,6 +36,8 @@ typedef enum { /** Track lines that have been copied from another file that exists in *any* * commit (like `git blame -CCC`) */ GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<1 | 1<<2 | 1<<3), + /** Track through file renames */ + GIT_BLAME_TRACK_FILE_RENAMES = (1<<4), } git_blame_flag_t; /** @@ -51,15 +53,14 @@ typedef enum { * associate those lines with the parent commit. The default value is 20. * This value only takes effect if any of the `GIT_BLAME_TRACK_COPIES_*` * flags are specified. - * - `newest_commit` is a rev-parse spec that resolves to the most recent - * commit to consider. The default is HEAD. - * - `newest_commit` is the newest commit to consider. The default is HEAD. - * - `oldest_commit` is the oldest commit to consider. The default is the - * first commit encountered with a NULL parent. + * - `newest_commit` is the id of the newest commit to consider. The default + * is HEAD. + * - `oldest_commit` is the id of the oldest commit to consider. The default + * is the first commit encountered with a NULL parent. * - `min_line` is the first line in the file to blame. The default is 1 (line - * numbers start with 1). + * numbers start with 1). * - `max_line` is the last line in the file to blame. The default is the last - * line of the file. + * line of the file. */ typedef struct git_blame_options { @@ -67,8 +68,8 @@ typedef struct git_blame_options { uint32_t flags; uint16_t min_match_characters; - git_commit *newest_commit; - git_commit *oldest_commit; + git_oid newest_commit; + git_oid oldest_commit; uint32_t min_line; uint32_t max_line; } git_blame_options; @@ -105,39 +106,40 @@ typedef struct git_blame_hunk { } git_blame_hunk; -typedef struct git_blame_results git_blame_results; +/* Opaque structure to hold blame results */ +typedef struct git_blame git_blame; /** - * Gets the number of hunks that exist in the results structure. + * Gets the number of hunks that exist in the blame structure. */ -GIT_EXTERN(uint32_t) git_blame_results_hunk_count(git_blame_results *results); +GIT_EXTERN(uint32_t) git_blame_get_hunk_count(git_blame *blame); /** * Gets the blame hunk at the given index. * - * @param results the results structure to query + * @param blame the blame structure to query * @param index index of the hunk to retrieve * @return the hunk at the given index, or NULL on error */ -GIT_EXTERN(const git_blame_hunk*) git_blame_results_hunk_byindex( - git_blame_results *results, +GIT_EXTERN(const git_blame_hunk*) git_blame_get_hunk_byindex( + git_blame *blame, uint32_t index); /** * Gets the hunk that relates to the given line number in the newest commit. * - * @param results the results structure to query + * @param blame the blame structure to query * @param lineno the (1-based) line number to find a hunk for * @return the hunk that contains the given line, or NULL on error */ -GIT_EXTERN(const git_blame_hunk*) git_blame_results_hunk_byline( - git_blame_results *results, +GIT_EXTERN(const git_blame_hunk*) git_blame_get_hunk_byline( + git_blame *blame, uint32_t lineno); /** * Get the blame for a single file. * - * @param out pointer that will receive the results object + * @param out pointer that will receive the blame object * @param repo repository whose history is to be walked * @param path path to file to consider * @param options options for the blame operation. If NULL, this is treated as @@ -146,32 +148,41 @@ GIT_EXTERN(const git_blame_hunk*) git_blame_results_hunk_byline( * about the error.) */ GIT_EXTERN(int) git_blame_file( - git_blame_results **out, + git_blame **out, git_repository *repo, const char *path, git_blame_options *options); /** - * Get blame data for a file that has been modified. + * Get blame data for a file that has been modified in memory. The `reference` + * parameter is a pre-calculated blame for the in-odb history of the file. This + * means that once a file blame is completed (which can be expensive), updating + * the buffer blame is very fast. * - * @param out pointer that will receive the results object - * @param reference output from git_blame_file for the file in question + * Lines that differ between the buffer and the committed version are marked as + * having a zero OID for their final_commit_id. + * + * @param out pointer that will receive the resulting blame data + * @param reference cached blame from the history of the file (usually the output + * from git_blame_file) * @param buffer the (possibly) modified contents of the file + * @param buffer_len number of valid bytes in the buffer * @return 0 on success, or an error code. (use giterr_last for information * about the error) */ GIT_EXTERN(int) git_blame_buffer( - git_blame_results **out, - git_blame_results *reference, - const char *buffer); + git_blame **out, + git_blame *reference, + const char *buffer, + size_t buffer_len); /** - * Free memory allocated by git_blame. + * Free memory allocated by git_blame_file or git_blame_buffer. * - * @param results results structure to free + * @param blame the blame structure to free */ -GIT_EXTERN(void) git_blame_free(git_blame_results *results); +GIT_EXTERN(void) git_blame_free(git_blame *blame); /** @} */ GIT_END_DECL diff --git a/include/git2/object.h b/include/git2/object.h index f74f3dfd1..7ec17cd8e 100644 --- a/include/git2/object.h +++ b/include/git2/object.h @@ -36,7 +36,7 @@ GIT_BEGIN_DECL * @param repo the repository to look up the object * @param id the unique identifier for the object * @param type the type of the object - * @return 0 or an error code + * @return a reference to the object */ GIT_EXTERN(int) git_object_lookup( git_object **object, @@ -78,6 +78,23 @@ GIT_EXTERN(int) git_object_lookup_prefix( size_t len, git_otype type); + +/** + * Lookup an object that represents a tree entry. + * + * @param out buffer that receives a pointer to the object (which must be freed + * by the caller) + * @param treeish root object that can be peeled to a tree + * @param path relative path from the root object to the desired object + * @param type type of object desired + * @return 0 on success, or an error code + */ +GIT_EXTERN(int) git_object_lookup_bypath( + git_object **out, + const git_object *treeish, + const char *path, + git_otype type); + /** * Get the id (SHA1) of a repository object * diff --git a/src/blame.c b/src/blame.c new file mode 100644 index 000000000..30d65f02c --- /dev/null +++ b/src/blame.c @@ -0,0 +1,492 @@ +/* + * 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 "blame.h" +#include "git2/commit.h" +#include "git2/revparse.h" +#include "git2/revwalk.h" +#include "git2/tree.h" +#include "git2/diff.h" +#include "git2/blob.h" +#include "util.h" +#include "repository.h" +#include "blame_git.h" + + +static int hunk_search_cmp_helper(const void *key, size_t start_line, size_t num_lines) +{ + uint32_t lineno = *(size_t*)key; + if (lineno < start_line) + return -1; + if (lineno >= ((uint32_t)start_line + num_lines)) + return 1; + return 0; +} +static int hunk_byfinalline_search_cmp(const void *key, const void *entry) +{ + git_blame_hunk *hunk = (git_blame_hunk*)entry; + return hunk_search_cmp_helper(key, hunk->final_start_line_number, hunk->lines_in_hunk); +} +static int hunk_sort_cmp_by_start_line(const void *_a, const void *_b) +{ + git_blame_hunk *a = (git_blame_hunk*)_a, + *b = (git_blame_hunk*)_b; + + return a->final_start_line_number - b->final_start_line_number; +} + +static bool hunk_ends_at_or_before_line(git_blame_hunk *hunk, size_t line) +{ + return line >= (size_t)(hunk->final_start_line_number + hunk->lines_in_hunk - 1); +} + +static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line) +{ + return line <= hunk->final_start_line_number; +} + +static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_start, const char *path) +{ + git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk)); + if (!hunk) return NULL; + + hunk->lines_in_hunk = lines; + hunk->final_start_line_number = start; + hunk->orig_start_line_number = orig_start; + hunk->orig_path = path ? git__strdup(path) : NULL; + + return hunk; +} + +git_blame_hunk* git_blame__alloc_hunk() +{ + return new_hunk(0,0,0,NULL); +} + +static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) +{ + git_blame_hunk *newhunk = new_hunk(hunk->final_start_line_number, hunk->lines_in_hunk, hunk->orig_start_line_number, hunk->orig_path); + git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); + git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); + return newhunk; +} + +static void free_hunk(git_blame_hunk *hunk) +{ + git__free((void*)hunk->orig_path); + git__free(hunk); +} + +/* Starting with the hunk that includes start_line, shift all following hunks' + * final_start_line by shift_by lines */ +static void shift_hunks_by_final(git_vector *v, size_t start_line, int shift_by) +{ + size_t i; + + if (!git_vector_bsearch2( &i, v, hunk_byfinalline_search_cmp, &start_line)) { + for (; i < v->length; i++) { + git_blame_hunk *hunk = (git_blame_hunk*)v->contents[i]; + hunk->final_start_line_number += shift_by; + } + } +} +static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); } +git_blame* git_blame__alloc( + git_repository *repo, + git_blame_options opts, + const char *path) +{ + git_blame *gbr = (git_blame*)git__calloc(1, sizeof(git_blame)); + if (!gbr) { + giterr_set_oom(); + return NULL; + } + git_vector_init(&gbr->hunks, 8, hunk_sort_cmp_by_start_line); + git_vector_init(&gbr->unclaimed_hunks, 8, hunk_sort_cmp_by_start_line); + git_vector_init(&gbr->paths, 8, paths_cmp); + gbr->repository = repo; + gbr->options = opts; + gbr->path = git__strdup(path); + git_vector_insert(&gbr->paths, git__strdup(path)); + gbr->final_blob = NULL; + return gbr; +} + +void git_blame_free(git_blame *blame) +{ + size_t i; + git_blame_hunk *hunk; + char *path; + + if (!blame) return; + + git_vector_foreach(&blame->hunks, i, hunk) + free_hunk(hunk); + git_vector_free(&blame->hunks); + + git_vector_foreach(&blame->unclaimed_hunks, i, hunk) + free_hunk(hunk); + git_vector_free(&blame->unclaimed_hunks); + + git_vector_foreach(&blame->paths, i, path) + git__free(path); + git_vector_free(&blame->paths); + + git__free((void*)blame->path); + git_blob_free(blame->final_blob); + git__free(blame); +} + +uint32_t git_blame_get_hunk_count(git_blame *blame) +{ + assert(blame); + return blame->hunks.length; +} + +const git_blame_hunk *git_blame_get_hunk_byindex(git_blame *blame, uint32_t index) +{ + assert(blame); + return (git_blame_hunk*)git_vector_get(&blame->hunks, index); +} + +const git_blame_hunk *git_blame_get_hunk_byline(git_blame *blame, uint32_t lineno) +{ + size_t i; + assert(blame); + + if (!git_vector_bsearch2( &i, &blame->hunks, hunk_byfinalline_search_cmp, &lineno)) { + return git_blame_get_hunk_byindex(blame, i); + } + + return NULL; +} + +static void normalize_options( + git_blame_options *out, + const git_blame_options *in, + git_repository *repo) +{ + git_blame_options dummy = GIT_BLAME_OPTIONS_INIT; + if (!in) in = &dummy; + + memcpy(out, in, sizeof(git_blame_options)); + + /* No newest_commit => HEAD */ + if (git_oid_iszero(&out->newest_commit)) { + git_reference_name_to_id(&out->newest_commit, repo, "HEAD"); + } +} + +static git_blame_hunk *split_hunk_in_vector( + git_vector *vec, + git_blame_hunk *hunk, + size_t rel_line, + bool return_new) +{ + size_t new_line_count; + git_blame_hunk *nh; + + /* Don't split if already at a boundary */ + if (rel_line <= 0 || + rel_line >= hunk->lines_in_hunk) + { + return hunk; + } + + new_line_count = hunk->lines_in_hunk - rel_line; + nh = new_hunk(hunk->final_start_line_number+rel_line, new_line_count, + hunk->orig_start_line_number+rel_line, hunk->orig_path); + git_oid_cpy(&nh->final_commit_id, &hunk->final_commit_id); + git_oid_cpy(&nh->orig_commit_id, &hunk->orig_commit_id); + + /* Adjust hunk that was split */ + hunk->lines_in_hunk -= new_line_count; + git_vector_insert_sorted(vec, nh, NULL); + { + git_blame_hunk *ret = return_new ? nh : hunk; + return ret; + } +} + + +/* + * To allow quick access to the contents of nth line in the + * final image, prepare an index in the scoreboard. + */ +static int prepare_lines(struct scoreboard *sb) +{ + const char *buf = sb->final_buf; + git_off_t len = sb->final_buf_size; + int num = 0, incomplete = 0, bol = 1; + + if (len && buf[len-1] != '\n') + incomplete++; /* incomplete line at the end */ + while (len--) { + if (bol) { + bol = 0; + } + if (*buf++ == '\n') { + num++; + bol = 1; + } + } + sb->num_lines = num + incomplete; + return sb->num_lines; +} + +static git_blame_hunk* hunk_from_entry(struct blame_entry *e) +{ + git_blame_hunk *h = new_hunk( + e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); + git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); + return h; +} + +static void free_if_not_already_freed(git_vector *already, struct origin *o) +{ + size_t i; + + if (!o) return; + if (!git_vector_search(&i, already, o)) + return; + + git_vector_insert(already, o); + free_if_not_already_freed(already, o->previous); + git_blob_free(o->blob); + git_commit_free(o->commit); + git__free(o); +} + +static int walk_and_mark(git_blame *blame) +{ + int error; + + struct scoreboard sb = {0}; + struct blame_entry *ent = NULL; + git_blob *blob = NULL; + struct origin *o; + git_vector already = GIT_VECTOR_INIT; + + if ((error = git_commit_lookup(&sb.final, blame->repository, &blame->options.newest_commit)) < 0 || + (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)sb.final, blame->path, GIT_OBJ_BLOB)) < 0) + goto cleanup; + sb.final_buf = git_blob_rawcontent(blob); + sb.final_buf_size = git_blob_rawsize(blob); + if ((error = get_origin(&o, &sb, sb.final, blame->path)) < 0) + goto cleanup; + + ent = git__calloc(1, sizeof(*ent)); + ent->num_lines = prepare_lines(&sb); + ent->suspect = o; + sb.ent = ent; + sb.path = blame->path; + sb.blame = blame; + + assign_blame(&sb, blame->options.flags); + coalesce(&sb); + + for (ent = sb.ent; ent; ) { + git_vector_insert(&blame->hunks, hunk_from_entry(ent)); + ent = ent->next; + } + +cleanup: + for (ent = sb.ent; ent; ) { + struct blame_entry *e = ent->next; + struct origin *o = ent->suspect; + + /* Linkages might not be ordered, so we only free pointers we haven't + * seen before. */ + free_if_not_already_freed(&already, o); + + git__free(ent); + ent = e; + } + + git_vector_free(&already); + git_commit_free(sb.final); + git_blob_free(blob); + return error; +} + +static int load_blob(git_blame *blame, git_repository *repo, git_oid *commit_id, const char *path) +{ + int retval = -1; + git_commit *commit = NULL; + git_tree *tree = NULL; + git_tree_entry *tree_entry = NULL; + git_object *obj = NULL; + + if (((retval = git_commit_lookup(&commit, repo, commit_id)) < 0) || + ((retval = git_object_lookup_bypath(&obj, (git_object*)commit, path, GIT_OBJ_BLOB)) < 0) || + ((retval = git_object_type(obj)) != GIT_OBJ_BLOB)) + goto cleanup; + blame->final_blob = (git_blob*)obj; + +cleanup: + git_tree_entry_free(tree_entry); + git_tree_free(tree); + git_commit_free(commit); + return retval; +} + +/******************************************************************************* + * File blaming + ******************************************************************************/ + +int git_blame_file( + git_blame **out, + git_repository *repo, + const char *path, + git_blame_options *options) +{ + int error = -1; + git_blame_options normOptions = GIT_BLAME_OPTIONS_INIT; + git_blame *blame = NULL; + + assert(out && repo && path); + normalize_options(&normOptions, options, repo); + + blame = git_blame__alloc(repo, normOptions, path); + GITERR_CHECK_ALLOC(blame); + + if ((error = load_blob(blame, repo, &normOptions.newest_commit, path)) < 0) + goto on_error; + + if ((error = walk_and_mark(blame)) < 0) + goto on_error; + + *out = blame; + return 0; + +on_error: + git_blame_free(blame); + return error; +} + +/******************************************************************************* + * Buffer blaming + *******************************************************************************/ + +static bool hunk_is_bufferblame(git_blame_hunk *hunk) +{ + return git_oid_iszero(&hunk->final_commit_id); +} + +static int buffer_hunk_cb( + const git_diff_delta *delta, + const git_diff_range *range, + const char *header, + size_t header_len, + void *payload) +{ + git_blame *blame = (git_blame*)payload; + size_t wedge_line; + + GIT_UNUSED(delta); + GIT_UNUSED(header); + GIT_UNUSED(header_len); + + wedge_line = (range->old_lines == 0) ? range->new_start : range->old_start; + blame->current_diff_line = wedge_line; + + /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ + blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byline(blame, wedge_line); + if (!hunk_starts_at_or_after_line(blame->current_hunk, wedge_line)){ + blame->current_hunk = split_hunk_in_vector(&blame->hunks, blame->current_hunk, + wedge_line - blame->current_hunk->orig_start_line_number, true); + } + + return 0; +} + +static int ptrs_equal_cmp(const void *a, const void *b) { return ab ? 1 : 0; } +static int buffer_line_cb( + const git_diff_delta *delta, + const git_diff_range *range, + char line_origin, + const char *content, + size_t content_len, + void *payload) +{ + git_blame *blame = (git_blame*)payload; + + GIT_UNUSED(delta); + GIT_UNUSED(range); + GIT_UNUSED(content); + GIT_UNUSED(content_len); + +#ifdef DO_DEBUG + { + char *str = git__substrdup(content, content_len); + git__free(str); + } +#endif + + if (line_origin == GIT_DIFF_LINE_ADDITION) { + if (hunk_is_bufferblame(blame->current_hunk) && + hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { + /* Append to the current buffer-blame hunk */ + blame->current_hunk->lines_in_hunk++; + shift_hunks_by_final(&blame->hunks, blame->current_diff_line+1, 1); + } else { + /* Create a new buffer-blame hunk with this line */ + shift_hunks_by_final(&blame->hunks, blame->current_diff_line, 1); + blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path); + git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); + } + blame->current_diff_line++; + } + + if (line_origin == GIT_DIFF_LINE_DELETION) { + /* Trim the line from the current hunk; remove it if it's now empty */ + size_t shift_base = blame->current_diff_line + blame->current_hunk->lines_in_hunk+1; + + if (--(blame->current_hunk->lines_in_hunk) == 0) { + size_t i; + shift_base--; + if (!git_vector_search2(&i, &blame->hunks, ptrs_equal_cmp, blame->current_hunk)) { + git_vector_remove(&blame->hunks, i); + free_hunk(blame->current_hunk); + blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byindex(blame, i); + } + } + shift_hunks_by_final(&blame->hunks, shift_base, -1); + } + return 0; +} + +int git_blame_buffer( + git_blame **out, + git_blame *reference, + const char *buffer, + size_t buffer_len) +{ + git_blame *blame; + git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; + size_t i; + git_blame_hunk *hunk; + + diffopts.context_lines = 0; + + assert(out && reference && buffer && buffer_len); + + blame = git_blame__alloc(reference->repository, reference->options, reference->path); + + /* Duplicate all of the hunk structures in the reference blame */ + git_vector_foreach(&reference->hunks, i, hunk) { + git_vector_insert(&blame->hunks, dup_hunk(hunk)); + } + + /* Diff to the reference blob */ + git_diff_blob_to_buffer(reference->final_blob, blame->path, + buffer, buffer_len, blame->path, + &diffopts, NULL, buffer_hunk_cb, buffer_line_cb, blame); + + *out = blame; + return 0; +} diff --git a/src/blame.h b/src/blame.h new file mode 100644 index 000000000..260d5b5a1 --- /dev/null +++ b/src/blame.h @@ -0,0 +1,37 @@ +#ifndef INCLUDE_blame_h__ +#define INCLUDE_blame_h__ + +#include "git2/blame.h" +#include "common.h" +#include "vector.h" +#include "diff.h" +#include "array.h" +#include "git2/oid.h" + +struct git_blame { + const char *path; + git_repository *repository; + git_blame_options options; + + git_vector hunks; + git_vector unclaimed_hunks; + git_vector paths; + + git_blob *final_blob; + size_t num_lines; + + git_oid current_commit; + git_oid parent_commit; + size_t current_diff_line; + size_t current_blame_line; + git_blame_hunk *current_hunk; +}; + +git_blame *git_blame__alloc( + git_repository *repo, + git_blame_options opts, + const char *path); + +git_blame_hunk *git_blame__alloc_hunk(); + +#endif diff --git a/src/blame_git.c b/src/blame_git.c new file mode 100644 index 000000000..0dae2d9c1 --- /dev/null +++ b/src/blame_git.c @@ -0,0 +1,591 @@ +/* + * 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 "blame_git.h" +#include "commit.h" + +/* + * Locate an existing origin or create a new one. + */ +int get_origin(struct origin **out, struct scoreboard *sb, git_commit *commit, const char *path) +{ + struct blame_entry *e; + + for (e = sb->ent; e; e = e->next) + if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) + *out = origin_incref(e->suspect); + return make_origin(out, commit, path); +} + +/* + * Given a commit and a path in it, create a new origin structure. + * The callers that add blame to the scoreboard should use + * get_origin() to obtain shared, refcounted copy instead of calling + * this function directly. + */ +int make_origin(struct origin **out, git_commit *commit, const char *path) +{ + int error = 0; + struct origin *o; + o = git__calloc(1, sizeof(*o) + strlen(path) + 1); + GITERR_CHECK_ALLOC(o); + o->commit = commit; + o->refcnt = 1; + strcpy(o->path, path); + + if (!(error = git_object_lookup_bypath((git_object**)&o->blob, (git_object*)commit, + path, GIT_OBJ_BLOB))) + *out = o; + else + origin_decref(o); + return error; +} + +struct blame_chunk_cb_data { + struct scoreboard *sb; + struct origin *target; + struct origin *parent; + long plno; + long tlno; +}; + +static bool same_suspect(struct origin *a, struct origin *b) +{ + if (a == b) + return true; + if (git_oid_cmp(git_commit_id(a->commit), git_commit_id(b->commit))) + return false; + return 0 == strcmp(a->path, b->path); +} + +/* Find the line number of the last line the target is suspected for */ +static int find_last_in_target(struct scoreboard *sb, struct origin *target) +{ + struct blame_entry *e; + int last_in_target = -1; + + for (e=sb->ent; e; e=e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (last_in_target < e->s_lno + e->num_lines) + last_in_target = e->s_lno + e->num_lines; + } + return last_in_target; +} + +/* + * It is known that lines between tlno to same came from parent, and e + * has an overlap with that range. it also is known that parent's + * line plno corresponds to e's line tlno. + * + * <---- e -----> + * <------> + * <------------> + * <------------> + * <------------------> + * + * Split e into potentially three parts; before this chunk, the chunk + * to be blamed for the parent, and after that portion. + */ +static void split_overlap(struct blame_entry *split, struct blame_entry *e, + int tlno, int plno, int same, struct origin *parent) +{ + int chunk_end_lno; + + if (e->s_lno < tlno) { + /* there is a pre-chunk part not blamed on the parent */ + split[0].suspect = origin_incref(e->suspect); + split[0].lno = e->lno; + split[0].s_lno = e->s_lno; + split[0].num_lines = tlno - e->s_lno; + split[1].lno = e->lno + tlno - e->s_lno; + split[1].s_lno = plno; + } else { + split[1].lno = e->lno; + split[1].s_lno = plno + (e->s_lno - tlno); + } + + if (same < e->s_lno + e->num_lines) { + /* there is a post-chunk part not blamed on parent */ + split[2].suspect = origin_incref(e->suspect); + split[2].lno = e->lno + (same - e->s_lno); + split[2].s_lno = e->s_lno + (same - e->s_lno); + split[2].num_lines = e->s_lno + e->num_lines - same; + chunk_end_lno = split[2].lno; + } else { + chunk_end_lno = e->lno + e->num_lines; + } + split[1].num_lines = chunk_end_lno - split[1].lno; + + /* + * if it turns out there is nothing to blame the parent for, forget about + * the splitting. !split[1].suspect signals this. + */ + if (split[1].num_lines < 1) + return; + split[1].suspect = origin_incref(parent); +} + +/* + * Link in a new blame entry to the scoreboard. Entries that cover the same + * line range have been removed from the scoreboard previously. + */ +static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) +{ + struct blame_entry *ent, *prev = NULL; + + origin_incref(e->suspect); + + for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) + prev = ent; + + /* prev, if not NULL, is the last one that is below e */ + e->prev = prev; + if (prev) { + e->next = prev->next; + prev->next = e; + } else { + e->next = sb->ent; + sb->ent = e; + } + if (e->next) + e->next->prev = e; +} + +/* + * src typically is on-stack; we want to copy the information in it to + * a malloced blame_entry that is already on the linked list of the scoreboard. + * The origin of dst loses a refcnt while the origin of src gains one. + */ +static void dup_entry(struct blame_entry *dst, struct blame_entry *src) +{ + struct blame_entry *p, *n; + + p = dst->prev; + n = dst->next; + origin_incref(src->suspect); + origin_decref(dst->suspect); + memcpy(dst, src, sizeof(*src)); + dst->prev = p; + dst->next = n; + dst->score = 0; +} + +/* + * split_overlap() divided an existing blame e into up to three parts in split. + * Adjust the linked list of blames in the scoreboard to reflect the split. + */ +static void split_blame(struct scoreboard *sb, struct blame_entry *split, struct blame_entry *e) +{ + struct blame_entry *new_entry; + + if (split[0].suspect && split[2].suspect) { + /* The first part (reuse storage for the existing entry e */ + dup_entry(e, &split[0]); + + /* The last part -- me */ + new_entry = git__malloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + + /* ... and the middle part -- parent */ + new_entry = git__malloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } else if (!split[0].suspect && !split[2].suspect) { + /* + * The parent covers the entire area; reuse storage for e and replace it + * with the parent + */ + dup_entry(e, &split[1]); + } else if (split[0].suspect) { + /* me and then parent */ + dup_entry(e, &split[0]); + new_entry = git__malloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } else { + /* parent and then me */ + dup_entry(e, &split[1]); + new_entry = git__malloc(sizeof(*new_entry)); + memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + add_blame_entry(sb, new_entry); + } +} + +/* + * After splitting the blame, the origins used by the on-stack blame_entry + * should lose one refcnt each. + */ +static void decref_split(struct blame_entry *split) +{ + int i; + for (i=0; i<3; i++) + origin_decref(split[i].suspect); +} + +/* + * Helper for blame_chunk(). blame_entry e is known to overlap with the patch + * hunk; split it and pass blame to the parent. + */ +static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, int tlno, int plno, int same, struct origin *parent) +{ + struct blame_entry split[3] = {{0}}; + + split_overlap(split, e, tlno, plno, same, parent); + if (split[1].suspect) + split_blame(sb, split, e); + decref_split(split); +} + +/* + * Process one hunk from the patch between the current suspect for blame_entry + * e and its parent. Find and split the overlap, and pass blame to the + * overlapping part to the parent. + */ +static void blame_chunk(struct scoreboard *sb, int tlno, int plno, int same, struct origin *target, struct origin *parent) +{ + struct blame_entry *e; + + for (e = sb->ent; e; e = e->next) { + if (e->guilty || !same_suspect(e->suspect, target)) + continue; + if (same <= e->s_lno) + continue; + if (tlno < e->s_lno + e->num_lines) { + blame_overlap(sb, e, tlno, plno, same, parent); + } + } +} + +static void blame_chunk_cb(long start_a, long count_a, long start_b, long count_b, void *data) +{ + struct blame_chunk_cb_data *d = data; + blame_chunk(d->sb, d->tlno, d->plno, start_b, d->target, d->parent); + d->plno = start_a + count_a; + d->tlno = start_b + count_b; +} + +static int my_emit_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg) +{ + xdchange_t *xch = xscr; + GIT_UNUSED(xe); + GIT_UNUSED(xecfg); + while (xch) { + blame_chunk_cb(xch->i1, xch->chg1, xch->i2, xch->chg2, ecb->priv); + xch = xch->next; + } + return 0; +} + +static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx) +{ + const int blk = 1024; + long trimmed = 0, recovered = 0; + char *ap = a->ptr + a->size; + char *bp = b->ptr + b->size; + long smaller = (a->size < b->size) ? a->size : b->size; + + if (ctx) + return; + + while (blk + trimmed <= smaller && !memcmp(ap - blk, bp - blk, blk)) { + trimmed += blk; + ap -= blk; + bp -= blk; + } + + while (recovered < trimmed) + if (ap[recovered++] == '\n') + break; + a->size -= trimmed - recovered; + b->size -= trimmed - recovered; +} + +static int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb) +{ + mmfile_t a = *mf1; + mmfile_t b = *mf2; + + trim_common_tail(&a, &b, xecfg->ctxlen); + + return xdl_diff(&a, &b, xpp, xecfg, xecb); +} + + +static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, void *cb_data) +{ + xpparam_t xpp = {0}; + xdemitconf_t xecfg = {0}; + xdemitcb_t ecb = {0}; + + xecfg.emit_func = (void(*)(void))my_emit_func; + ecb.priv = cb_data; + return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); +} + +static void fill_origin_blob(struct origin *o, mmfile_t *file) +{ + memset(file, 0, sizeof(*file)); + if (o->blob) { + file->ptr = (char*)git_blob_rawcontent(o->blob); + file->size = (size_t)git_blob_rawsize(o->blob); + } +} + +static int pass_blame_to_parent(struct scoreboard *sb, + struct origin *target, + struct origin *parent) +{ + int last_in_target; + mmfile_t file_p, file_o; + struct blame_chunk_cb_data d = { sb, target, parent, 0, 0 }; + + last_in_target = find_last_in_target(sb, target); + if (last_in_target < 0) + return 1; /* nothing remains for this target */ + + fill_origin_blob(parent, &file_p); + fill_origin_blob(target, &file_o); + + diff_hunks(&file_p, &file_o, &d); + /* The reset (i.e. anything after tlno) are the same as the parent */ + blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent); + + return 0; +} + +static int paths_on_dup(void **old, void *new) +{ + GIT_UNUSED(old); + git__free(new); + return -1; +} +static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, + struct origin *origin) +{ + struct origin *porigin = NULL; + git_diff_list *difflist = NULL; + git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; + git_tree *otree=NULL, *ptree=NULL; + + /* Get the trees from this commit and its parent */ + // TODO: check errors + git_commit_tree(&otree, origin->commit); + git_commit_tree(&ptree, parent); + + /* Configure the diff */ + diffopts.context_lines = 0; + diffopts.flags = GIT_DIFF_SKIP_BINARY_CHECK; + + /* Check to see if files we're interested have changed */ + diffopts.pathspec.count = sb->blame->paths.length; + diffopts.pathspec.strings = (char**)sb->blame->paths.contents; + // TODO: check error + git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts); + + if (!git_diff_num_deltas(difflist)) { + /* No changes; copy data */ + // TODO: check error + get_origin(&porigin, sb, parent, origin->path); + } else { + git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; + int i; + + /* Generate a full diff between the two trees */ + git_diff_list_free(difflist); + diffopts.pathspec.count = 0; + // TODO: check error + git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts); + + /* Let diff find renames */ + findopts.flags = GIT_DIFF_FIND_RENAMES; + // TODO: check error + git_diff_find_similar(difflist, &findopts); + + /* Find one that matches */ + for (i=0; i<(int)git_diff_num_deltas(difflist); i++) { + const git_diff_delta *delta; + git_diff_get_patch(NULL, &delta, difflist, i); + if (git_vector_bsearch(NULL, &sb->blame->paths, delta->new_file.path) != 0) + continue; + + git_vector_insert_sorted(&sb->blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup); + // TODO: check error + make_origin(&porigin, parent, delta->old_file.path); + } + } + + git_diff_list_free(difflist); + git_tree_free(otree); + git_tree_free(ptree); + return porigin; + +} + +/* + * The blobs of origin and porigin exactly match, so everything origin is + * suspected for can be blamed on the parent. + */ +static void pass_whole_blame(struct scoreboard *sb, + struct origin *origin, struct origin *porigin) +{ + struct blame_entry *e; + + if (!porigin->blob) + git_object_lookup((git_object**)&porigin->blob, sb->blame->repository, git_blob_id(origin->blob), + GIT_OBJ_BLOB); + for (e=sb->ent; e; e=e->next) { + if (!same_suspect(e->suspect, origin)) + continue; + origin_incref(porigin); + origin_decref(e->suspect); + e->suspect = porigin; + } +} + +static void pass_blame(struct scoreboard *sb, struct origin *origin, uint32_t opt) +{ + git_commit *commit = origin->commit; + int i, num_sg; + struct origin *sg_buf[16]; + struct origin *porigin, **sg_origin = sg_buf; + + GIT_UNUSED(opt); + + num_sg = git_commit_parentcount(commit); + if (!num_sg) + goto finish; + else if (num_sg < (int)ARRAY_SIZE(sg_buf)) + memset(sg_buf, 0, sizeof(sg_buf)); + else + sg_origin = git__calloc(num_sg, sizeof(*sg_origin)); + + for (i=0; icommit, i); + porigin = find_origin(sb, p, origin); + + if (!porigin) + continue; + if (porigin->blob && origin->blob && + !git_oid_cmp(git_blob_id(porigin->blob), git_blob_id(origin->blob))) { + pass_whole_blame(sb, origin, porigin); + origin_decref(porigin); + goto finish; + } + for (j = same = 0; jblob), git_blob_id(porigin->blob))) { + same = 1; + break; + } + if (!same) + sg_origin[i] = porigin; + else + origin_decref(porigin); + } + + for (i=0; iprevious) { + origin_incref(porigin); + origin->previous = porigin; + } + if (pass_blame_to_parent(sb, origin, porigin)) + goto finish; + } + + /* TODO: optionally find moves in parents' files */ + + /* TODO: optionally find copies in parents' files */ + +finish: + for (i=0; irefcnt++; + return o; +} + +void origin_decref(struct origin *o) +{ + if (o && --o->refcnt <= 0) { + if (o->previous) + origin_decref(o->previous); + git_blob_free(o->blob); + git_commit_free(o->commit); + git__free(o); + } +} + +void assign_blame(struct scoreboard *sb, uint32_t opt) +{ + while (true) { + struct blame_entry *ent; + struct origin *suspect = NULL; + + /* Find a suspect to break down */ + for (ent = sb->ent; !suspect && ent; ent = ent->next) + if (!ent->guilty) + suspect = ent->suspect; + if (!suspect) + return; /* all done */ + + /* We'll use this suspect later in the loop, so hold on to it for now. */ + origin_incref(suspect); + pass_blame(sb, suspect, opt); + + /* Take responsibility for the remaining entries */ + for (ent = sb->ent; ent; ent = ent->next) + if (same_suspect(ent->suspect, suspect)) + ent->guilty = 1; + origin_decref(suspect); + } +} + +void coalesce(struct scoreboard *sb) +{ + struct blame_entry *ent, *next; + + for (ent=sb->ent; ent && (next = ent->next); ent = next) { + if (same_suspect(ent->suspect, next->suspect) && + ent->guilty == next->guilty && + ent->s_lno + ent->num_lines == next->s_lno) + { + ent->num_lines += next->num_lines; + ent->next = next->next; + if (ent->next) + ent->next->prev = ent; + origin_decref(next->suspect); + git__free(next); + ent->score = 0; + next = ent; /* again */ + } + } +} + diff --git a/src/blame_git.h b/src/blame_git.h new file mode 100644 index 000000000..89ebc3228 --- /dev/null +++ b/src/blame_git.h @@ -0,0 +1,93 @@ + +#ifndef INCLUDE_blame_git__ +#define INCLUDE_blame_git__ + +#include "git2.h" +#include "xdiff/xinclude.h" +#include "blame.h" + +/* + * One blob in a commit that is being suspected + */ +struct origin { + int refcnt; + struct origin *previous; + git_commit *commit; + git_blob *blob; + char path[]; +}; + +/* + * Each group of lines is described by a blame_entry; it can be split + * as we pass blame to the parents. They form a linked list in the + * scoreboard structure, sorted by the target line number. + */ +struct blame_entry { + struct blame_entry *prev; + struct blame_entry *next; + + /* the first line of this group in the final image; + * internally all line numbers are 0 based. + */ + int lno; + + /* how many lines this group has */ + int num_lines; + + /* the commit that introduced this group into the final image */ + struct origin *suspect; + + /* true if the suspect is truly guilty; false while we have not + * checked if the group came from one of its parents. + */ + char guilty; + + /* true if the entry has been scanned for copies in the current parent + */ + char scanned; + + /* the line number of the first line of this group in the + * suspect's file; internally all line numbers are 0 based. + */ + int s_lno; + + /* how significant this entry is -- cached to avoid + * scanning the lines over and over. + */ + unsigned score; +}; + +/* + * The current state of the blame assignment. + */ +struct scoreboard { + /* the final commit (i.e. where we started digging from) */ + git_commit *final; + const char *path; + + /* + * The contents in the final image. + * Used by many functions to obtain contents of the nth line, + * indexed with scoreboard.lineno[blame_entry.lno]. + */ + const char *final_buf; + git_off_t final_buf_size; + + /* linked list of blames */ + struct blame_entry *ent; + + /* look-up a line in the final buffer */ + int num_lines; + + git_blame *blame; +}; + + +int get_origin(struct origin **out, struct scoreboard *sb, git_commit *commit, const char *path); +int make_origin(struct origin **out, git_commit *commit, const char *path); +struct origin *origin_incref(struct origin *o); +void origin_decref(struct origin *o); +void assign_blame(struct scoreboard *sb, uint32_t flags); +void coalesce(struct scoreboard *sb); + +#endif diff --git a/src/object.c b/src/object.c index 9b8ccdd3e..53666ffe4 100644 --- a/src/object.c +++ b/src/object.c @@ -364,3 +364,39 @@ int git_object_dup(git_object **dest, git_object *source) *dest = source; return 0; } + +int git_object_lookup_bypath( + git_object **out, + const git_object *treeish, + const char *path, + git_otype type) +{ + int error = -1; + git_tree *tree = NULL; + git_tree_entry *entry = NULL; + git_object *tmpobj = NULL; + + assert(out && treeish && path); + + if (((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJ_TREE)) < 0) || + ((error = git_tree_entry_bypath(&entry, tree, path)) < 0) || + ((error = git_tree_entry_to_object(&tmpobj, git_object_owner(treeish), entry)) < 0)) + { + goto cleanup; + } + + if (type == GIT_OBJ_ANY || git_object_type(tmpobj) == type) { + *out = tmpobj; + } else { + giterr_set(GITERR_OBJECT, + "object at path '%s' is not of the asked-for type %d", + path, type); + error = GIT_EINVALIDSPEC; + git_object_free(tmpobj); + } + +cleanup: + git_tree_entry_free(entry); + git_tree_free(tree); + return error; +} diff --git a/tests-clar/blame/blame_helpers.c b/tests-clar/blame/blame_helpers.c new file mode 100644 index 000000000..25c62a1ff --- /dev/null +++ b/tests-clar/blame/blame_helpers.c @@ -0,0 +1,58 @@ +#include "blame_helpers.h" + +void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...) +{ + va_list arglist; + + printf("Hunk %zd (line %d +%d): ", idx, + hunk->final_start_line_number, hunk->lines_in_hunk-1); + + va_start(arglist, fmt); + vprintf(fmt, arglist); + va_end(arglist); + + printf("\n"); +} + +void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx, + int start_line, int len, const char *commit_id, const char *orig_path) +{ + char expected[41] = {0}, actual[41] = {0}; + const git_blame_hunk *hunk = git_blame_get_hunk_byindex(blame, idx); + cl_assert(hunk); + + if (!strncmp(commit_id, "0000", 4)) { + strcpy(expected, "0000000000000000000000000000000000000000"); + } else { + git_object *obj; + cl_git_pass(git_revparse_single(&obj, repo, commit_id)); + git_oid_fmt(expected, git_object_id(obj)); + git_object_free(obj); + } + + if (hunk->final_start_line_number != start_line) { + hunk_message(idx, hunk, "mismatched start line number: expected %d, got %d", + start_line, hunk->final_start_line_number); + } + cl_assert_equal_i(hunk->final_start_line_number, start_line); + + if (hunk->lines_in_hunk != len) { + hunk_message(idx, hunk, "mismatched line count: expected %d, got %d", + len, hunk->lines_in_hunk); + } + cl_assert_equal_i(hunk->lines_in_hunk, len); + + git_oid_fmt(actual, &hunk->final_commit_id); + if (strcmp(expected, actual)) { + hunk_message(idx, hunk, "has mismatched original id (got %s, expected %s)\n", + actual, expected); + } + cl_assert_equal_s(actual, expected); + if (strcmp(hunk->orig_path, orig_path)) { + hunk_message(idx, hunk, "has mismatched original path (got '%s', expected '%s')\n", + hunk->orig_path, orig_path); + } + cl_assert_equal_s(hunk->orig_path, orig_path); +} + + diff --git a/tests-clar/blame/blame_helpers.h b/tests-clar/blame/blame_helpers.h new file mode 100644 index 000000000..15fa1c33e --- /dev/null +++ b/tests-clar/blame/blame_helpers.h @@ -0,0 +1,15 @@ +#include "clar_libgit2.h" +#include "blame.h" + +void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...); + +void check_blame_hunk_index( + git_repository *repo, + git_blame *blame, + int idx, + int start_line, + int len, + const char *commit_id, + const char *orig_path); + + diff --git a/tests-clar/blame/buffer.c b/tests-clar/blame/buffer.c new file mode 100644 index 000000000..20435eb6c --- /dev/null +++ b/tests-clar/blame/buffer.c @@ -0,0 +1,130 @@ +#include "blame_helpers.h" + +git_repository *g_repo; +git_blame *g_fileblame, *g_bufferblame; + +void test_blame_buffer__initialize(void) +{ + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); + cl_git_pass(git_blame_file(&g_fileblame, g_repo, "b.txt", NULL)); + g_bufferblame = NULL; +} + +void test_blame_buffer__cleanup(void) +{ + git_blame_free(g_fileblame); + git_blame_free(g_bufferblame); + git_repository_free(g_repo); +} + +void test_blame_buffer__added_line(void) +{ + const char *buffer = "\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +\n\ +abcdefg\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; + + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame)); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "000000", "b.txt"); +} + +void test_blame_buffer__deleted_line(void) +{ + const char *buffer = "\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; + + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 3, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 9, 1, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 10, 5, "aa06ecca", "b.txt"); +} + +void test_blame_buffer__add_splits_hunk(void) +{ + const char *buffer = "\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +abc\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; + + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 2, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 8, 1, "00000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 9, 3, "63d671eb", "b.txt"); +} + +void test_blame_buffer__delete_crosses_hunk_boundary(void) +{ + const char *buffer = "\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; + + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 2, "aa06ecca", "b.txt"); +} + +void test_blame_buffer__replace_line(void) +{ + const char *buffer = "\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\n\ +\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +abc\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n\ +\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\ +CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; + + cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 1, "00000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, "63d671eb", "b.txt"); +} diff --git a/tests-clar/blame/getters.c b/tests-clar/blame/getters.c new file mode 100644 index 000000000..82e0de59e --- /dev/null +++ b/tests-clar/blame/getters.c @@ -0,0 +1,56 @@ +#include "clar_libgit2.h" + +#include "blame.h" + +git_blame *g_blame; + +void test_blame_getters__initialize(void) +{ + size_t i; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + git_blame_hunk hunks[] = { + { 3, {{0}}, 1, {{0}}, "a", 0}, + { 3, {{0}}, 4, {{0}}, "b", 0}, + { 3, {{0}}, 7, {{0}}, "c", 0}, + { 3, {{0}}, 10, {{0}}, "d", 0}, + { 3, {{0}}, 13, {{0}}, "e", 0}, + }; + + g_blame = git_blame__alloc(NULL, opts, ""); + + for (i=0; i<5; i++) { + git_blame_hunk *h = git_blame__alloc_hunk(); + h->final_start_line_number = hunks[i].final_start_line_number; + h->orig_path = git__strdup(hunks[i].orig_path); + h->lines_in_hunk = hunks[i].lines_in_hunk; + + git_vector_insert(&g_blame->hunks, h); + } +} + +void test_blame_getters__cleanup(void) +{ + git_blame_free(g_blame); +} + + +void test_blame_getters__byindex(void) +{ + const git_blame_hunk *h = git_blame_get_hunk_byindex(g_blame, 2); + cl_assert(h); + cl_assert_equal_s(h->orig_path, "c"); + + h = git_blame_get_hunk_byindex(g_blame, 95); + cl_assert_equal_p(h, NULL); +} + +void test_blame_getters__byline(void) +{ + const git_blame_hunk *h = git_blame_get_hunk_byline(g_blame, 5); + cl_assert(h); + cl_assert_equal_s(h->orig_path, "b"); + + h = git_blame_get_hunk_byline(g_blame, 95); + cl_assert_equal_p(h, NULL); +} diff --git a/tests-clar/blame/harder.c b/tests-clar/blame/harder.c new file mode 100644 index 000000000..7c4dd4f74 --- /dev/null +++ b/tests-clar/blame/harder.c @@ -0,0 +1,71 @@ +#include "clar_libgit2.h" + +#include "blame.h" + + +/** + * The test repo has a history that looks like this: + * + * * (A) bc7c5ac + * |\ + * | * (B) aa06ecc + * * | (C) 63d671e + * |/ + * * (D) da23739 + * * (E) b99f7ac + * + */ + +static git_repository *g_repo = NULL; + +void test_blame_harder__initialize(void) +{ + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); +} + +void test_blame_harder__cleanup(void) +{ + git_repository_free(g_repo); + g_repo = NULL; +} + + + +void test_blame_harder__m(void) +{ + /* TODO */ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + opts.flags = GIT_BLAME_TRACK_COPIES_SAME_FILE; +} + + +void test_blame_harder__c(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + /* Attribute the first hunk in b.txt to (E), since it was cut/pasted from + * a.txt in (D). + */ + opts.flags = GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; +} + +void test_blame_harder__cc(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + /* Attribute the second hunk in b.txt to (E), since it was copy/pasted from + * a.txt in (C). + */ + opts.flags = GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; +} + +void test_blame_harder__ccc(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + /* Attribute the third hunk in b.txt to (E). This hunk was deleted from + * a.txt in (D), but reintroduced in (B). + */ + opts.flags = GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES; +} diff --git a/tests-clar/blame/simple.c b/tests-clar/blame/simple.c new file mode 100644 index 000000000..4e2d88653 --- /dev/null +++ b/tests-clar/blame/simple.c @@ -0,0 +1,204 @@ +#include "blame_helpers.h" + +/* + * $ git blame -s branch_file.txt + * orig line no final line no + * commit V author timestamp V + * c47800c7 1 (Scott Chacon 2010-05-25 11:58:14 -0700 1 + * a65fedf3 2 (Scott Chacon 2011-08-09 19:33:46 -0700 2 + */ +void test_blame_simple__trivial_testrepo(void) +{ + git_blame *blame = NULL; + git_repository *repo; + cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo/.gitted"))); + cl_git_pass(git_blame_file(&blame, repo, "branch_file.txt", NULL)); + + cl_assert_equal_i(2, git_blame_get_hunk_count(blame)); + check_blame_hunk_index(repo, blame, 0, 1, 1, "c47800c7", "branch_file.txt"); + check_blame_hunk_index(repo, blame, 1, 2, 1, "a65fedf3", "branch_file.txt"); + + git_blame_free(blame); + git_repository_free(repo); +} + +/* + * $ git blame -n b.txt + * orig line no final line no + * commit V author timestamp V + * da237394 1 (Ben Straub 2013-02-12 15:11:30 -0800 1 + * da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2 + * da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3 + * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 + * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 + * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 + * 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7 + * 63d671eb 8 (Ben Straub 2013-02-12 15:13:04 -0800 8 + * 63d671eb 9 (Ben Straub 2013-02-12 15:13:04 -0800 9 + * 63d671eb 10 (Ben Straub 2013-02-12 15:13:04 -0800 10 + * aa06ecca 6 (Ben Straub 2013-02-12 15:14:46 -0800 11 + * aa06ecca 7 (Ben Straub 2013-02-12 15:14:46 -0800 12 + * aa06ecca 8 (Ben Straub 2013-02-12 15:14:46 -0800 13 + * aa06ecca 9 (Ben Straub 2013-02-12 15:14:46 -0800 14 + * aa06ecca 10 (Ben Straub 2013-02-12 15:14:46 -0800 15 + */ +void test_blame_simple__trivial_blamerepo(void) +{ + git_blame *blame = NULL; + git_repository *repo; + cl_git_pass(git_repository_open(&repo, cl_fixture("blametest.git"))); + cl_git_pass(git_blame_file(&blame, repo, "b.txt", NULL)); + + cl_assert_equal_i(4, git_blame_get_hunk_count(blame)); + check_blame_hunk_index(repo, blame, 0, 1, 4, "da237394", "b.txt"); + check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(repo, blame, 2, 6, 5, "63d671eb", "b.txt"); + check_blame_hunk_index(repo, blame, 3, 11, 5, "aa06ecca", "b.txt"); + + git_blame_free(blame); + git_repository_free(repo); +} + + +/* + * $ git blame -n 359fc2d -- include/git2.h + * orig line no final line no + * commit orig path V author timestamp V + * d12299fe src/git.h 1 (Vicent Martí 2010-12-03 22:22:10 +0200 1 + * 359fc2d2 include/git2.h 2 (Edward Thomson 2013-01-08 17:07:25 -0600 2 + * d12299fe src/git.h 5 (Vicent Martí 2010-12-03 22:22:10 +0200 3 + * bb742ede include/git2.h 4 (Vicent Martí 2011-09-19 01:54:32 +0300 4 + * bb742ede include/git2.h 5 (Vicent Martí 2011-09-19 01:54:32 +0300 5 + * d12299fe src/git.h 24 (Vicent Martí 2010-12-03 22:22:10 +0200 6 + * d12299fe src/git.h 25 (Vicent Martí 2010-12-03 22:22:10 +0200 7 + * d12299fe src/git.h 26 (Vicent Martí 2010-12-03 22:22:10 +0200 8 + * d12299fe src/git.h 27 (Vicent Martí 2010-12-03 22:22:10 +0200 9 + * d12299fe src/git.h 28 (Vicent Martí 2010-12-03 22:22:10 +0200 10 + * 96fab093 include/git2.h 11 (Sven Strickroth 2011-10-09 18:37:41 +0200 11 + * 9d1dcca2 src/git2.h 33 (Vicent Martí 2011-02-07 10:35:58 +0200 12 + * 44908fe7 src/git2.h 29 (Vicent Martí 2010-12-06 23:03:16 +0200 13 + * a15c550d include/git2.h 14 (Vicent Martí 2011-11-16 14:09:44 +0100 14 + * 44908fe7 src/git2.h 30 (Vicent Martí 2010-12-06 23:03:16 +0200 15 + * d12299fe src/git.h 32 (Vicent Martí 2010-12-03 22:22:10 +0200 16 + * 44908fe7 src/git2.h 33 (Vicent Martí 2010-12-06 23:03:16 +0200 17 + * d12299fe src/git.h 34 (Vicent Martí 2010-12-03 22:22:10 +0200 18 + * 44908fe7 src/git2.h 35 (Vicent Martí 2010-12-06 23:03:16 +0200 19 + * 638c2ca4 src/git2.h 36 (Vicent Martí 2010-12-18 02:10:25 +0200 20 + * 44908fe7 src/git2.h 36 (Vicent Martí 2010-12-06 23:03:16 +0200 21 + * d12299fe src/git.h 37 (Vicent Martí 2010-12-03 22:22:10 +0200 22 + * 44908fe7 src/git2.h 38 (Vicent Martí 2010-12-06 23:03:16 +0200 23 + * 44908fe7 src/git2.h 39 (Vicent Martí 2010-12-06 23:03:16 +0200 24 + * bf787bd8 include/git2.h 25 (Carlos Martín Nieto 2012-04-08 18:56:50 +0200 25 + * 0984c876 include/git2.h 26 (Scott J. Goldman 2012-11-28 18:27:43 -0800 26 + * 2f8a8ab2 src/git2.h 41 (Vicent Martí 2011-01-29 01:56:25 +0200 27 + * 27df4275 include/git2.h 47 (Michael Schubert 2011-06-28 14:13:12 +0200 28 + * a346992f include/git2.h 28 (Ben Straub 2012-05-10 09:47:14 -0700 29 + * d12299fe src/git.h 40 (Vicent Martí 2010-12-03 22:22:10 +0200 30 + * 44908fe7 src/git2.h 41 (Vicent Martí 2010-12-06 23:03:16 +0200 31 + * 44908fe7 src/git2.h 42 (Vicent Martí 2010-12-06 23:03:16 +0200 32 + * 44908fe7 src/git2.h 43 (Vicent Martí 2010-12-06 23:03:16 +0200 33 + * 44908fe7 src/git2.h 44 (Vicent Martí 2010-12-06 23:03:16 +0200 34 + * 44908fe7 src/git2.h 45 (Vicent Martí 2010-12-06 23:03:16 +0200 35 + * 65b09b1d include/git2.h 33 (Russell Belfer 2012-02-02 18:03:43 -0800 36 + * d12299fe src/git.h 46 (Vicent Martí 2010-12-03 22:22:10 +0200 37 + * 44908fe7 src/git2.h 47 (Vicent Martí 2010-12-06 23:03:16 +0200 38 + * 5d4cd003 include/git2.h 55 (Carlos Martín Nieto 2011-03-28 17:02:45 +0200 39 + * 41fb1ca0 include/git2.h 39 (Philip Kelley 2012-10-29 13:41:14 -0400 40 + * 2dc31040 include/git2.h 56 (Carlos Martín Nieto 2011-06-20 18:58:57 +0200 41 + * 764df57e include/git2.h 40 (Ben Straub 2012-06-15 13:14:43 -0700 42 + * 5280f4e6 include/git2.h 41 (Ben Straub 2012-07-31 19:39:06 -0700 43 + * 613d5eb9 include/git2.h 43 (Philip Kelley 2012-11-28 11:42:37 -0500 44 + * d12299fe src/git.h 48 (Vicent Martí 2010-12-03 22:22:10 +0200 45 + * 111ee3fe include/git2.h 41 (Vicent Martí 2012-07-11 14:37:26 +0200 46 + * f004c4a8 include/git2.h 44 (Russell Belfer 2012-08-21 17:26:39 -0700 47 + * 111ee3fe include/git2.h 42 (Vicent Martí 2012-07-11 14:37:26 +0200 48 + * 9c82357b include/git2.h 58 (Carlos Martín Nieto 2011-06-17 18:13:14 +0200 49 + * d6258deb include/git2.h 61 (Carlos Martín Nieto 2011-06-25 15:10:09 +0200 50 + * b311e313 include/git2.h 63 (Julien Miotte 2011-07-27 18:31:13 +0200 51 + * 3412391d include/git2.h 63 (Carlos Martín Nieto 2011-07-07 11:47:31 +0200 52 + * bfc9ca59 include/git2.h 43 (Russell Belfer 2012-03-28 16:45:36 -0700 53 + * bf477ed4 include/git2.h 44 (Michael Schubert 2012-02-15 00:33:38 +0100 54 + * edebceff include/git2.h 46 (nulltoken 2012-05-01 13:57:45 +0200 55 + * 743a4b3b include/git2.h 48 (nulltoken 2012-06-15 22:24:59 +0200 56 + * 0a32dca5 include/git2.h 54 (Michael Schubert 2012-08-19 22:26:32 +0200 57 + * 590fb68b include/git2.h 55 (nulltoken 2012-10-04 13:47:45 +0200 58 + * bf477ed4 include/git2.h 45 (Michael Schubert 2012-02-15 00:33:38 +0100 59 + * d12299fe src/git.h 49 (Vicent Martí 2010-12-03 22:22:10 +0200 60 + */ +void test_blame_simple__trivial_libgit2(void) +{ + git_repository *repo; + git_blame *blame; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + git_object *obj; + + cl_git_pass(git_repository_open(&repo, cl_fixture("../.."))); + + /* This test can't work on a shallow clone */ + if (git_repository_is_shallow(repo)) { + git_repository_free(repo); + return; + } + + cl_git_pass(git_revparse_single(&obj, repo, "359fc2d")); + git_oid_cpy(&opts.newest_commit, git_object_id(obj)); + git_object_free(obj); + + cl_git_pass(git_blame_file(&blame, repo, "include/git2.h", &opts)); + + check_blame_hunk_index(repo, blame, 0, 1, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 1, 2, 1, "359fc2d2", "include/git2.h"); + check_blame_hunk_index(repo, blame, 2, 3, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 3, 4, 2, "bb742ede", "include/git2.h"); + check_blame_hunk_index(repo, blame, 4, 6, 5, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 5, 11, 1, "96fab093", "include/git2.h"); + check_blame_hunk_index(repo, blame, 6, 12, 1, "9d1dcca2", "src/git2.h"); + check_blame_hunk_index(repo, blame, 7, 13, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 8, 14, 1, "a15c550d", "include/git2.h"); + check_blame_hunk_index(repo, blame, 9, 15, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 10, 16, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 11, 17, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 12, 18, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 13, 19, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 14, 20, 1, "638c2ca4", "src/git2.h"); + check_blame_hunk_index(repo, blame, 15, 21, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 16, 22, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 17, 23, 2, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 18, 25, 1, "bf787bd8", "include/git2.h"); + check_blame_hunk_index(repo, blame, 19, 26, 1, "0984c876", "include/git2.h"); + check_blame_hunk_index(repo, blame, 20, 27, 1, "2f8a8ab2", "src/git2.h"); + check_blame_hunk_index(repo, blame, 21, 28, 1, "27df4275", "include/git2.h"); + check_blame_hunk_index(repo, blame, 22, 29, 1, "a346992f", "include/git2.h"); + check_blame_hunk_index(repo, blame, 23, 30, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 24, 31, 5, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 25, 36, 1, "65b09b1d", "include/git2.h"); + check_blame_hunk_index(repo, blame, 26, 37, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 27, 38, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(repo, blame, 28, 39, 1, "5d4cd003", "include/git2.h"); + check_blame_hunk_index(repo, blame, 29, 40, 1, "41fb1ca0", "include/git2.h"); + check_blame_hunk_index(repo, blame, 30, 41, 1, "2dc31040", "include/git2.h"); + check_blame_hunk_index(repo, blame, 31, 42, 1, "764df57e", "include/git2.h"); + check_blame_hunk_index(repo, blame, 32, 43, 1, "5280f4e6", "include/git2.h"); + check_blame_hunk_index(repo, blame, 33, 44, 1, "613d5eb9", "include/git2.h"); + check_blame_hunk_index(repo, blame, 34, 45, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(repo, blame, 35, 46, 1, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(repo, blame, 36, 47, 1, "f004c4a8", "include/git2.h"); + check_blame_hunk_index(repo, blame, 37, 48, 1, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(repo, blame, 38, 49, 1, "9c82357b", "include/git2.h"); + check_blame_hunk_index(repo, blame, 39, 50, 1, "d6258deb", "include/git2.h"); + check_blame_hunk_index(repo, blame, 40, 51, 1, "b311e313", "include/git2.h"); + check_blame_hunk_index(repo, blame, 41, 52, 1, "3412391d", "include/git2.h"); + check_blame_hunk_index(repo, blame, 42, 53, 1, "bfc9ca59", "include/git2.h"); + check_blame_hunk_index(repo, blame, 43, 54, 1, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(repo, blame, 44, 55, 1, "edebceff", "include/git2.h"); + check_blame_hunk_index(repo, blame, 45, 56, 1, "743a4b3b", "include/git2.h"); + check_blame_hunk_index(repo, blame, 46, 57, 1, "0a32dca5", "include/git2.h"); + check_blame_hunk_index(repo, blame, 47, 58, 1, "590fb68b", "include/git2.h"); + check_blame_hunk_index(repo, blame, 48, 59, 1, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(repo, blame, 49, 60, 1, "d12299fe", "src/git.h"); + + git_blame_free(blame); + git_repository_free(repo); +} + +/* TODO: no newline at end of file? */ diff --git a/tests-clar/object/lookupbypath.c b/tests-clar/object/lookupbypath.c new file mode 100644 index 000000000..31aac7647 --- /dev/null +++ b/tests-clar/object/lookupbypath.c @@ -0,0 +1,83 @@ +#include "clar_libgit2.h" + +#include "repository.h" + +static git_repository *g_repo; +static git_tree *g_root_tree; +static git_commit *g_head_commit; +static git_object *g_expectedobject, + *g_actualobject; + +void test_object_lookupbypath__initialize(void) +{ + git_reference *head; + git_tree_entry *tree_entry; + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("attr/.gitted"))); + + cl_git_pass(git_repository_head(&head, g_repo)); + cl_git_pass(git_reference_peel((git_object**)&g_head_commit, head, GIT_OBJ_COMMIT)); + cl_git_pass(git_commit_tree(&g_root_tree, g_head_commit)); + cl_git_pass(git_tree_entry_bypath(&tree_entry, g_root_tree, "subdir/subdir_test2.txt")); + cl_git_pass(git_object_lookup(&g_expectedobject, g_repo, git_tree_entry_id(tree_entry), + GIT_OBJ_ANY)); + + git_tree_entry_free(tree_entry); + git_reference_free(head); + + g_actualobject = NULL; +} +void test_object_lookupbypath__cleanup(void) +{ + git_object_free(g_actualobject); + git_object_free(g_expectedobject); + git_tree_free(g_root_tree); + git_commit_free(g_head_commit); + g_expectedobject = NULL; + git_repository_free(g_repo); + g_repo = NULL; +} + +void test_object_lookupbypath__errors(void) +{ + cl_assert_equal_i(GIT_EINVALIDSPEC, + git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree, + "subdir/subdir_test2.txt", GIT_OBJ_TREE)); // It's not a tree + cl_assert_equal_i(GIT_ENOTFOUND, + git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree, + "file/doesnt/exist", GIT_OBJ_ANY)); +} + +void test_object_lookupbypath__from_root_tree(void) +{ + cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_root_tree, + "subdir/subdir_test2.txt", GIT_OBJ_BLOB)); + cl_assert_equal_i(0, git_oid_cmp(git_object_id(g_expectedobject), + git_object_id(g_actualobject))); +} + +void test_object_lookupbypath__from_head_commit(void) +{ + cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)g_head_commit, + "subdir/subdir_test2.txt", GIT_OBJ_BLOB)); + cl_assert_equal_i(0, git_oid_cmp(git_object_id(g_expectedobject), + git_object_id(g_actualobject))); +} + +void test_object_lookupbypath__from_subdir_tree(void) +{ + git_tree_entry *entry = NULL; + git_tree *tree = NULL; + + cl_git_pass(git_tree_entry_bypath(&entry, g_root_tree, "subdir")); + cl_git_pass(git_tree_lookup(&tree, g_repo, git_tree_entry_id(entry))); + + cl_git_pass(git_object_lookup_bypath(&g_actualobject, (git_object*)tree, + "subdir_test2.txt", GIT_OBJ_BLOB)); + cl_assert_equal_i(0, git_oid_cmp(git_object_id(g_expectedobject), + git_object_id(g_actualobject))); + + git_tree_entry_free(entry); + git_tree_free(tree); +} + diff --git a/tests-clar/resources/blametest.git/HEAD b/tests-clar/resources/blametest.git/HEAD new file mode 100644 index 000000000..cb089cd89 --- /dev/null +++ b/tests-clar/resources/blametest.git/HEAD @@ -0,0 +1 @@ +ref: refs/heads/master diff --git a/tests-clar/resources/blametest.git/config b/tests-clar/resources/blametest.git/config new file mode 100644 index 000000000..c53d818dd --- /dev/null +++ b/tests-clar/resources/blametest.git/config @@ -0,0 +1,5 @@ +[core] + repositoryformatversion = 0 + filemode = true + bare = true + ignorecase = true diff --git a/tests-clar/resources/blametest.git/description b/tests-clar/resources/blametest.git/description new file mode 100644 index 000000000..498b267a8 --- /dev/null +++ b/tests-clar/resources/blametest.git/description @@ -0,0 +1 @@ +Unnamed repository; edit this file 'description' to name the repository. diff --git a/tests-clar/resources/blametest.git/objects/0c/bab4d45fd61e55a1c9697f9f9cb07a12e15448 b/tests-clar/resources/blametest.git/objects/0c/bab4d45fd61e55a1c9697f9f9cb07a12e15448 new file mode 100644 index 0000000000000000000000000000000000000000..90331cef9e2a034476d47724607ec51c6f3c331b GIT binary patch literal 46 zcmbsD%Uw5aL{8&niX*d0IDPmDF6Tf literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/1b/5f0775af166331c854bd8d1bca3450eaf2532a b/tests-clar/resources/blametest.git/objects/1b/5f0775af166331c854bd8d1bca3450eaf2532a new file mode 100644 index 0000000000000000000000000000000000000000..e664306379cc7a955ee09ed741426c94ca112c06 GIT binary patch literal 35 rcmbsD%Uw5aL`lNYMz*(AkH9ey}BL%0EG>N literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/48/2f2c370e35c2c314fc1f96db2beb33f955a26a b/tests-clar/resources/blametest.git/objects/48/2f2c370e35c2c314fc1f96db2beb33f955a26a new file mode 100644 index 0000000000000000000000000000000000000000..7da4cf5d4ee517f5b98dd5fe020b7ef5b486ab5d GIT binary patch literal 35 rcmbsD%Uw5aL`NFYMz*(AkOf_dd+?S2Kx@u literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/63/d671eb32d250e4a83766ebbc60e818c1e1e93a b/tests-clar/resources/blametest.git/objects/63/d671eb32d250e4a83766ebbc60e818c1e1e93a new file mode 100644 index 000000000..5f41f2936 --- /dev/null +++ b/tests-clar/resources/blametest.git/objects/63/d671eb32d250e4a83766ebbc60e818c1e1e93a @@ -0,0 +1,3 @@ +x•A E]sŠÙu¥Ê@bŒ±GðÀP뢭Azk<ù’—¼Ÿ×y~60­–I³“1S´’=“-6÷FfC.†ìcp½õе, $b +¶8MF +ᨹO!1eïÈò]TqkÓZáV¸··çô¾>žmÚÒ)¯ó49dì-Ñ#ªîm­üg©áw©ºTK@Î \ No newline at end of file diff --git a/tests-clar/resources/blametest.git/objects/63/eb57322e363e18d460da5ea8284f3cd2340b36 b/tests-clar/resources/blametest.git/objects/63/eb57322e363e18d460da5ea8284f3cd2340b36 new file mode 100644 index 0000000000000000000000000000000000000000..c6c285eeb494bad55823949cb8c8a20769b6a058 GIT binary patch literal 76 zcmV-S0JHyi0V^p=O;s>6V=y!@Ff%bxNYpE-C}G&iy`z2m0rP-$*{1sc%2v;(lYc)^IP?*j=^`M8z1a1bUN)>Ma Dwn-5* literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/98/89d6e5557761aa8e3607e80c874a6dc51ada7c b/tests-clar/resources/blametest.git/objects/98/89d6e5557761aa8e3607e80c874a6dc51ada7c new file mode 100644 index 0000000000000000000000000000000000000000..12407f662d3eec83787ce6518f9d9bfb160bdf2d GIT binary patch literal 43 zcmb6V=y!@Ff%bxNYpE-C}EhEJ~#6G{E}<4Z6@)nwnlD!adKf7 iR6!D0fpk23>3Xqb!xJHUd!6H!x*zq%z>5vU!dp(LgRIDua<|AeS?h!ALZa%Z18dBpS%& ON@Xw-4Fmw90J^4*t`?2} literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/b9/9f7ac0b88909253d829554c14af488c3b0f3a5 b/tests-clar/resources/blametest.git/objects/b9/9f7ac0b88909253d829554c14af488c3b0f3a5 new file mode 100644 index 000000000..a428fd63b --- /dev/null +++ b/tests-clar/resources/blametest.git/objects/b9/9f7ac0b88909253d829554c14af488c3b0f3a5 @@ -0,0 +1,2 @@ +x•ŽK +1]ç½›•ÒIg2Yˆˆâ ­3‹L vîoÀ¸{õR-eÐΤ1ƒ#ŽóBÆ0©}¶s˜9xãí‹R6d1’S¡ËZÜx‡§´Ð#œãçúÞdíñ”j¹€&‡‹F§ Ñ#ªAGKø?KݧÇôgõ2r \ No newline at end of file diff --git a/tests-clar/resources/blametest.git/objects/bc/7c5ac2bafe828a68e9d1d460343718d6fbe136 b/tests-clar/resources/blametest.git/objects/bc/7c5ac2bafe828a68e9d1d460343718d6fbe136 new file mode 100644 index 000000000..4e6ad159e --- /dev/null +++ b/tests-clar/resources/blametest.git/objects/bc/7c5ac2bafe828a68e9d1d460343718d6fbe136 @@ -0,0 +1,3 @@ +x•AnÃ0 {Ö+tË))J´AÛ6V=y!@Ff%bxNYpE-C}H5)wdG3uHM!7*Co}8k&)HBV^f1H& isvrrhz(ZfhoX_;oVUa)b({5|OHvSp9C<_3_8ynl&3L^Lb literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/d0/67729932057cdb7527a833d6799c4ddc520640 b/tests-clar/resources/blametest.git/objects/d0/67729932057cdb7527a833d6799c4ddc520640 new file mode 100644 index 000000000..926c4bbb0 --- /dev/null +++ b/tests-clar/resources/blametest.git/objects/d0/67729932057cdb7527a833d6799c4ddc520640 @@ -0,0 +1 @@ +x+)JMU03c040031QHÔ+©(a˜Ñyíihyâª>3ö<í^¹G¥nÕ@$H­É\;ÍMêoã¶œúѬ‹£Ƥ \ No newline at end of file diff --git a/tests-clar/resources/blametest.git/objects/da/237394e6132d20d30f175b9b73c8638fddddda b/tests-clar/resources/blametest.git/objects/da/237394e6132d20d30f175b9b73c8638fddddda new file mode 100644 index 000000000..e9e13833f --- /dev/null +++ b/tests-clar/resources/blametest.git/objects/da/237394e6132d20d30f175b9b73c8638fddddda @@ -0,0 +1,4 @@ +x•K +Â0@]÷³ëJ™|'"¢ÞÀ$“ÔvÑVbz žÀíƒïÉ:ÏS­ðÐj)Ñif£Ñ‘äDNS ÆdOÌbs§Ñ[ìÞ±–¥Ab( +¦Y;“ƒfç¬(‚˜„ƒ‰®‹[× +·²À³Õ¸%8§Ïõ5µqK'Yç (ã‘zF8b@ìvº·µòŸÕÝKý£ÿc–?S \ No newline at end of file diff --git a/tests-clar/resources/blametest.git/objects/e5/b41c1ea533f87388ab69b13baf0b5a562d6243 b/tests-clar/resources/blametest.git/objects/e5/b41c1ea533f87388ab69b13baf0b5a562d6243 new file mode 100644 index 0000000000000000000000000000000000000000..7e5586c2b4c5f245ac6d70cf03894bc70a7087ee GIT binary patch literal 76 zcmV-S0JHyi0V^p=O;s>6V=y!@Ff%bxNYpE-C}H5)wdG3uHM!7*Co}8k&)HBV^f1H& isvrrh;Jwj(U)71ulg&ksdj|eAy4k6mauER7KOAA>86!;q literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/objects/ef/32df4d259143933715c74951f932d9892364d1 b/tests-clar/resources/blametest.git/objects/ef/32df4d259143933715c74951f932d9892364d1 new file mode 100644 index 0000000000000000000000000000000000000000..d021ccfde398a7a28a468512c73378ba42d41e46 GIT binary patch literal 42 zcmV+_0M-9^0ZYosPg1ZnH)U|8GT@@Jd9FjiKrSaLgOO+;mot^YNHmZO0Keb=dyAkF A$N&HU literal 0 HcmV?d00001 diff --git a/tests-clar/resources/blametest.git/refs/heads/master b/tests-clar/resources/blametest.git/refs/heads/master new file mode 100644 index 000000000..b763025d8 --- /dev/null +++ b/tests-clar/resources/blametest.git/refs/heads/master @@ -0,0 +1 @@ +bc7c5ac2bafe828a68e9d1d460343718d6fbe136 From 4c7fdb4d3d7b24e4bca69a710d347cfeb05ac78f Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 16 Sep 2013 16:27:10 -0700 Subject: [PATCH 06/35] Add blame example --- examples/.gitignore | 1 + examples/Makefile | 2 +- examples/blame.c | 131 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 examples/blame.c diff --git a/examples/.gitignore b/examples/.gitignore index e8e0820a5..2b693f1ef 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -2,4 +2,5 @@ general showindex diff rev-list +blame *.dSYM diff --git a/examples/Makefile b/examples/Makefile index d53ed8241..79c57d7d7 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ CC = gcc CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes -Wno-missing-field-initializers LFLAGS = -L../build -lgit2 -lz -APPS = general showindex diff rev-list cat-file status log rev-parse init +APPS = general blame showindex diff rev-list cat-file status log rev-parse all: $(APPS) diff --git a/examples/blame.c b/examples/blame.c new file mode 100644 index 000000000..1b08ec487 --- /dev/null +++ b/examples/blame.c @@ -0,0 +1,131 @@ +#include +#include +#include +#include + +static void check(int error, const char *msg) +{ + if (error) { + fprintf(stderr, "%s (%d)\n", msg, error); + exit(error); + } +} + +static void usage(const char *msg, const char *arg) +{ + if (msg && arg) + fprintf(stderr, "%s: %s\n", msg, arg); + else if (msg) + fprintf(stderr, "%s\n", msg); + fprintf(stderr, "usage: blame [options] []\n"); + fprintf(stderr, "\n"); + fprintf(stderr, " example: `HEAD~10..HEAD`, or `1234abcd`\n"); + fprintf(stderr, " -L process only line range n-m, counting from 1\n"); + fprintf(stderr, " -M fine line moves within and across files\n"); + fprintf(stderr, " -C find line copies within and across files\n"); + fprintf(stderr, "\n"); + exit(1); +} + +int main(int argc, char *argv[]) +{ + int i; + char *path = NULL, *a; + const char *rawdata, *commitspec=NULL; + git_repository *repo = NULL; + git_revspec revspec = {0}; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + git_blame *blame = NULL; + git_commit *commit; + git_tree *tree; + git_tree_entry *entry; + git_blob *blob; + + if (argc < 2) usage(NULL, NULL); + path = argv[1]; + + for (i=2; i= argc) check(-1, "Not enough arguments to -L"); + check(sscanf(a, "%d,%d", &opts.min_line, &opts.max_line)-2, "-L format error"); + } + else { + /* commit range */ + if (commitspec) check(-1, "Only one commit spec allowed"); + commitspec = a; + } + } + + /* Open the repo */ + check(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository"); + + /* Parse the end points */ + if (commitspec) { + check(git_revparse(&revspec, repo, commitspec), "Couldn't parse commit spec"); + if (revspec.flags & GIT_REVPARSE_SINGLE) { + git_oid_cpy(&opts.newest_commit, git_object_id(revspec.from)); + git_object_free(revspec.from); + } else { + git_oid_cpy(&opts.oldest_commit, git_object_id(revspec.from)); + git_oid_cpy(&opts.newest_commit, git_object_id(revspec.to)); + git_object_free(revspec.from); + git_object_free(revspec.to); + } + } + + /* Run the blame */ + check(git_blame_file(&blame, repo, path, &opts), "Blame error"); + + /* Get the raw data for output */ + if (git_oid_iszero(&opts.newest_commit)) { + git_object *obj; + check(git_revparse_single(&obj, repo, "HEAD"), "Can't find HEAD"); + git_oid_cpy(&opts.newest_commit, git_object_id(obj)); + git_object_free(obj); + } + check(git_commit_lookup(&commit, repo, &opts.newest_commit), "Commit lookup error"); + check(git_commit_tree(&tree, commit), "Commit tree lookup error"); + check(git_tree_entry_bypath(&entry, tree, path), "Tree entry lookup error"); + check(git_blob_lookup(&blob, repo, git_tree_entry_id(entry)), "Blob lookup error"); + rawdata = git_blob_rawcontent(blob); + + /* Produce the output */ + i = 1; + while (rawdata[0]) { + const char *eol = strchr(rawdata, '\n'); + char oid[10] = {0}; + const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, i); + git_commit *hunkcommit; + const git_signature *sig; + + git_oid_tostr(oid, 10, &hunk->final_commit_id); + check(git_commit_lookup(&hunkcommit, repo, &hunk->final_commit_id), "Commit lookup error"); + sig = git_commit_author(hunkcommit); + + printf("%s ( %-30s %3d) %.*s\n", + oid, + sig->name, + i, + (int)(eol-rawdata), + rawdata); + + git_commit_free(hunkcommit); + rawdata = eol+1; + i++; + } + + /* Cleanup */ + git_blob_free(blob); + git_tree_entry_free(entry); + git_tree_free(tree); + git_commit_free(commit); + git_blame_free(blame); + git_repository_free(repo); +} From 0afe9996483387d282821958974320078923306c Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Tue, 17 Sep 2013 16:46:27 -0700 Subject: [PATCH 07/35] Check errors from libgit2 calls --- src/blame_git.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/blame_git.c b/src/blame_git.c index 0dae2d9c1..bff36c50c 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -374,9 +374,9 @@ static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, git_tree *otree=NULL, *ptree=NULL; /* Get the trees from this commit and its parent */ - // TODO: check errors - git_commit_tree(&otree, origin->commit); - git_commit_tree(&ptree, parent); + if (0 != git_commit_tree(&otree, origin->commit) || + 0 != git_commit_tree(&ptree, parent)) + goto cleanup; /* Configure the diff */ diffopts.context_lines = 0; @@ -385,12 +385,11 @@ static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, /* Check to see if files we're interested have changed */ diffopts.pathspec.count = sb->blame->paths.length; diffopts.pathspec.strings = (char**)sb->blame->paths.contents; - // TODO: check error - git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts); + if (0 != git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts)) + goto cleanup; if (!git_diff_num_deltas(difflist)) { /* No changes; copy data */ - // TODO: check error get_origin(&porigin, sb, parent, origin->path); } else { git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; @@ -399,13 +398,13 @@ static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, /* Generate a full diff between the two trees */ git_diff_list_free(difflist); diffopts.pathspec.count = 0; - // TODO: check error - git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts); + if (0 != git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts)) + goto cleanup; /* Let diff find renames */ findopts.flags = GIT_DIFF_FIND_RENAMES; - // TODO: check error - git_diff_find_similar(difflist, &findopts); + if (0 != git_diff_find_similar(difflist, &findopts)) + goto cleanup; /* Find one that matches */ for (i=0; i<(int)git_diff_num_deltas(difflist); i++) { @@ -415,16 +414,15 @@ static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, continue; git_vector_insert_sorted(&sb->blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup); - // TODO: check error make_origin(&porigin, parent, delta->old_file.path); } } +cleanup: git_diff_list_free(difflist); git_tree_free(otree); git_tree_free(ptree); return porigin; - } /* @@ -472,7 +470,6 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, uint32_t op if (sg_origin[i]) continue; - // TODO: check error git_commit_parent(&p, origin->commit, i); porigin = find_origin(sb, p, origin); From 3e0cf2a18057af11e7d8ba43bb43c4b399f89a27 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 19 Sep 2013 10:27:37 -0700 Subject: [PATCH 08/35] Stop being crazy about freeing memory --- src/blame.c | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/src/blame.c b/src/blame.c index 30d65f02c..33477d619 100644 --- a/src/blame.c +++ b/src/blame.c @@ -246,21 +246,6 @@ static git_blame_hunk* hunk_from_entry(struct blame_entry *e) return h; } -static void free_if_not_already_freed(git_vector *already, struct origin *o) -{ - size_t i; - - if (!o) return; - if (!git_vector_search(&i, already, o)) - return; - - git_vector_insert(already, o); - free_if_not_already_freed(already, o->previous); - git_blob_free(o->blob); - git_commit_free(o->commit); - git__free(o); -} - static int walk_and_mark(git_blame *blame) { int error; @@ -269,7 +254,6 @@ static int walk_and_mark(git_blame *blame) struct blame_entry *ent = NULL; git_blob *blob = NULL; struct origin *o; - git_vector already = GIT_VECTOR_INIT; if ((error = git_commit_lookup(&sb.final, blame->repository, &blame->options.newest_commit)) < 0 || (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)sb.final, blame->path, GIT_OBJ_BLOB)) < 0) @@ -289,26 +273,18 @@ static int walk_and_mark(git_blame *blame) assign_blame(&sb, blame->options.flags); coalesce(&sb); - for (ent = sb.ent; ent; ) { - git_vector_insert(&blame->hunks, hunk_from_entry(ent)); - ent = ent->next; - } - cleanup: for (ent = sb.ent; ent; ) { struct blame_entry *e = ent->next; struct origin *o = ent->suspect; - /* Linkages might not be ordered, so we only free pointers we haven't - * seen before. */ - free_if_not_already_freed(&already, o); + git_vector_insert(&blame->hunks, hunk_from_entry(ent)); + origin_decref(o); git__free(ent); ent = e; } - git_vector_free(&already); - git_commit_free(sb.final); git_blob_free(blob); return error; } From d1228f1c8711edcb7f5fbb3a31c9018b57021ca6 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 19 Sep 2013 14:18:51 -0700 Subject: [PATCH 09/35] blame: allow restriction to line range --- src/blame.c | 11 +++++++ tests-clar/blame/simple.c | 68 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/src/blame.c b/src/blame.c index 33477d619..a4ac84fde 100644 --- a/src/blame.c +++ b/src/blame.c @@ -179,6 +179,10 @@ static void normalize_options( if (git_oid_iszero(&out->newest_commit)) { git_reference_name_to_id(&out->newest_commit, repo, "HEAD"); } + + /* min_line 0 really means 1 */ + if (!out->min_line) out->min_line = 1; + /* max_line 0 really means N, but we don't know N yet */ } static git_blame_hunk *split_hunk_in_vector( @@ -265,7 +269,14 @@ static int walk_and_mark(git_blame *blame) ent = git__calloc(1, sizeof(*ent)); ent->num_lines = prepare_lines(&sb); + ent->lno = blame->options.min_line - 1; + ent->num_lines = ent->num_lines - blame->options.min_line + 1; + if (blame->options.max_line > 0) { + ent->num_lines = blame->options.max_line - blame->options.min_line + 1; + } + ent->s_lno = ent->lno; ent->suspect = o; + sb.ent = ent; sb.path = blame->path; sb.blame = blame; diff --git a/tests-clar/blame/simple.c b/tests-clar/blame/simple.c index 4e2d88653..1de4a51d0 100644 --- a/tests-clar/blame/simple.c +++ b/tests-clar/blame/simple.c @@ -201,4 +201,72 @@ void test_blame_simple__trivial_libgit2(void) git_repository_free(repo); } + +/* + * $ git blame -n b.txt -L 8 + * orig line no final line no + * commit V author timestamp V + * 63d671eb 8 (Ben Straub 2013-02-12 15:13:04 -0800 8 + * 63d671eb 9 (Ben Straub 2013-02-12 15:13:04 -0800 9 + * 63d671eb 10 (Ben Straub 2013-02-12 15:13:04 -0800 10 + * aa06ecca 6 (Ben Straub 2013-02-12 15:14:46 -0800 11 + * aa06ecca 7 (Ben Straub 2013-02-12 15:14:46 -0800 12 + * aa06ecca 8 (Ben Straub 2013-02-12 15:14:46 -0800 13 + * aa06ecca 9 (Ben Straub 2013-02-12 15:14:46 -0800 14 + * aa06ecca 10 (Ben Straub 2013-02-12 15:14:46 -0800 15 + * + * $ git blame -n b.txt -L ,6 + * orig line no final line no + * commit V author timestamp V + * da237394 1 (Ben Straub 2013-02-12 15:11:30 -0800 1 + * da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2 + * da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3 + * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 + * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 + * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 + * + * $ git blame -n b.txt -L 2,7 + * orig line no final line no + * commit V author timestamp V + * da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2 + * da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3 + * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 + * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 + * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 + * 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7 + */ +void test_blame_simple__can_restrict_to_lines(void) +{ + git_blame *blame = NULL; + git_repository *repo; + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + cl_git_pass(git_repository_open(&repo, cl_fixture("blametest.git"))); + + opts.min_line = 8; + cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); + cl_assert_equal_i(2, git_blame_get_hunk_count(blame)); + check_blame_hunk_index(repo, blame, 0, 8, 3, "63d671eb", "b.txt"); + check_blame_hunk_index(repo, blame, 1, 11, 5, "aa06ecca", "b.txt"); + + opts.min_line = 0; + opts.max_line = 6; + cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); + cl_assert_equal_i(3, git_blame_get_hunk_count(blame)); + check_blame_hunk_index(repo, blame, 0, 1, 4, "da237394", "b.txt"); + check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(repo, blame, 2, 6, 1, "63d671eb", "b.txt"); + + opts.min_line = 2; + opts.max_line = 7; + cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); + cl_assert_equal_i(3, git_blame_get_hunk_count(blame)); + check_blame_hunk_index(repo, blame, 0, 2, 3, "da237394", "b.txt"); + check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(repo, blame, 2, 6, 2, "63d671eb", "b.txt"); + + git_blame_free(blame); + git_repository_free(repo); +} + /* TODO: no newline at end of file? */ From 9d42fcbef349c101d12410d62cda7ee9f23bdf8f Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 19 Sep 2013 14:27:44 -0700 Subject: [PATCH 10/35] Blame: fixturize tests --- tests-clar/blame/simple.c | 236 ++++++++++++++++++++------------------ 1 file changed, 126 insertions(+), 110 deletions(-) diff --git a/tests-clar/blame/simple.c b/tests-clar/blame/simple.c index 1de4a51d0..ad5cf8fb4 100644 --- a/tests-clar/blame/simple.c +++ b/tests-clar/blame/simple.c @@ -1,5 +1,20 @@ #include "blame_helpers.h" +static git_repository *g_repo; +static git_blame *g_blame; + +void test_blame_simple__initialize(void) +{ + g_repo = NULL; + g_blame = NULL; +} + +void test_blame_simple__cleanup(void) +{ + git_blame_free(g_blame); + git_repository_free(g_repo); +} + /* * $ git blame -s branch_file.txt * orig line no final line no @@ -9,17 +24,12 @@ */ void test_blame_simple__trivial_testrepo(void) { - git_blame *blame = NULL; - git_repository *repo; - cl_git_pass(git_repository_open(&repo, cl_fixture("testrepo/.gitted"))); - cl_git_pass(git_blame_file(&blame, repo, "branch_file.txt", NULL)); + cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo/.gitted"))); + cl_git_pass(git_blame_file(&g_blame, g_repo, "branch_file.txt", NULL)); - cl_assert_equal_i(2, git_blame_get_hunk_count(blame)); - check_blame_hunk_index(repo, blame, 0, 1, 1, "c47800c7", "branch_file.txt"); - check_blame_hunk_index(repo, blame, 1, 2, 1, "a65fedf3", "branch_file.txt"); - - git_blame_free(blame); - git_repository_free(repo); + cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, "c47800c7", "branch_file.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, "a65fedf3", "branch_file.txt"); } /* @@ -44,19 +54,14 @@ void test_blame_simple__trivial_testrepo(void) */ void test_blame_simple__trivial_blamerepo(void) { - git_blame *blame = NULL; - git_repository *repo; - cl_git_pass(git_repository_open(&repo, cl_fixture("blametest.git"))); - cl_git_pass(git_blame_file(&blame, repo, "b.txt", NULL)); + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); + cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", NULL)); - cl_assert_equal_i(4, git_blame_get_hunk_count(blame)); - check_blame_hunk_index(repo, blame, 0, 1, 4, "da237394", "b.txt"); - check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(repo, blame, 2, 6, 5, "63d671eb", "b.txt"); - check_blame_hunk_index(repo, blame, 3, 11, 5, "aa06ecca", "b.txt"); - - git_blame_free(blame); - git_repository_free(repo); + cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, "aa06ecca", "b.txt"); } @@ -127,78 +132,71 @@ void test_blame_simple__trivial_blamerepo(void) */ void test_blame_simple__trivial_libgit2(void) { - git_repository *repo; - git_blame *blame; git_blame_options opts = GIT_BLAME_OPTIONS_INIT; git_object *obj; - cl_git_pass(git_repository_open(&repo, cl_fixture("../.."))); + cl_git_pass(git_repository_open(&g_repo, cl_fixture("../.."))); /* This test can't work on a shallow clone */ - if (git_repository_is_shallow(repo)) { - git_repository_free(repo); + if (git_repository_is_shallow(g_repo)) return; - } - cl_git_pass(git_revparse_single(&obj, repo, "359fc2d")); + cl_git_pass(git_revparse_single(&obj, g_repo, "359fc2d")); git_oid_cpy(&opts.newest_commit, git_object_id(obj)); git_object_free(obj); - cl_git_pass(git_blame_file(&blame, repo, "include/git2.h", &opts)); + cl_git_pass(git_blame_file(&g_blame, g_repo, "include/git2.h", &opts)); - check_blame_hunk_index(repo, blame, 0, 1, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 1, 2, 1, "359fc2d2", "include/git2.h"); - check_blame_hunk_index(repo, blame, 2, 3, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 3, 4, 2, "bb742ede", "include/git2.h"); - check_blame_hunk_index(repo, blame, 4, 6, 5, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 5, 11, 1, "96fab093", "include/git2.h"); - check_blame_hunk_index(repo, blame, 6, 12, 1, "9d1dcca2", "src/git2.h"); - check_blame_hunk_index(repo, blame, 7, 13, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 8, 14, 1, "a15c550d", "include/git2.h"); - check_blame_hunk_index(repo, blame, 9, 15, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 10, 16, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 11, 17, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 12, 18, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 13, 19, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 14, 20, 1, "638c2ca4", "src/git2.h"); - check_blame_hunk_index(repo, blame, 15, 21, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 16, 22, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 17, 23, 2, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 18, 25, 1, "bf787bd8", "include/git2.h"); - check_blame_hunk_index(repo, blame, 19, 26, 1, "0984c876", "include/git2.h"); - check_blame_hunk_index(repo, blame, 20, 27, 1, "2f8a8ab2", "src/git2.h"); - check_blame_hunk_index(repo, blame, 21, 28, 1, "27df4275", "include/git2.h"); - check_blame_hunk_index(repo, blame, 22, 29, 1, "a346992f", "include/git2.h"); - check_blame_hunk_index(repo, blame, 23, 30, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 24, 31, 5, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 25, 36, 1, "65b09b1d", "include/git2.h"); - check_blame_hunk_index(repo, blame, 26, 37, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 27, 38, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(repo, blame, 28, 39, 1, "5d4cd003", "include/git2.h"); - check_blame_hunk_index(repo, blame, 29, 40, 1, "41fb1ca0", "include/git2.h"); - check_blame_hunk_index(repo, blame, 30, 41, 1, "2dc31040", "include/git2.h"); - check_blame_hunk_index(repo, blame, 31, 42, 1, "764df57e", "include/git2.h"); - check_blame_hunk_index(repo, blame, 32, 43, 1, "5280f4e6", "include/git2.h"); - check_blame_hunk_index(repo, blame, 33, 44, 1, "613d5eb9", "include/git2.h"); - check_blame_hunk_index(repo, blame, 34, 45, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(repo, blame, 35, 46, 1, "111ee3fe", "include/git2.h"); - check_blame_hunk_index(repo, blame, 36, 47, 1, "f004c4a8", "include/git2.h"); - check_blame_hunk_index(repo, blame, 37, 48, 1, "111ee3fe", "include/git2.h"); - check_blame_hunk_index(repo, blame, 38, 49, 1, "9c82357b", "include/git2.h"); - check_blame_hunk_index(repo, blame, 39, 50, 1, "d6258deb", "include/git2.h"); - check_blame_hunk_index(repo, blame, 40, 51, 1, "b311e313", "include/git2.h"); - check_blame_hunk_index(repo, blame, 41, 52, 1, "3412391d", "include/git2.h"); - check_blame_hunk_index(repo, blame, 42, 53, 1, "bfc9ca59", "include/git2.h"); - check_blame_hunk_index(repo, blame, 43, 54, 1, "bf477ed4", "include/git2.h"); - check_blame_hunk_index(repo, blame, 44, 55, 1, "edebceff", "include/git2.h"); - check_blame_hunk_index(repo, blame, 45, 56, 1, "743a4b3b", "include/git2.h"); - check_blame_hunk_index(repo, blame, 46, 57, 1, "0a32dca5", "include/git2.h"); - check_blame_hunk_index(repo, blame, 47, 58, 1, "590fb68b", "include/git2.h"); - check_blame_hunk_index(repo, blame, 48, 59, 1, "bf477ed4", "include/git2.h"); - check_blame_hunk_index(repo, blame, 49, 60, 1, "d12299fe", "src/git.h"); - - git_blame_free(blame); - git_repository_free(repo); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, "359fc2d2", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 2, 3, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 3, 4, 2, "bb742ede", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 4, 6, 5, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 5, 11, 1, "96fab093", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 6, 12, 1, "9d1dcca2", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 7, 13, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 8, 14, 1, "a15c550d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 9, 15, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 10, 16, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 11, 17, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 12, 18, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 13, 19, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 14, 20, 1, "638c2ca4", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 15, 21, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 16, 22, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 17, 23, 2, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 18, 25, 1, "bf787bd8", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 19, 26, 1, "0984c876", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 20, 27, 1, "2f8a8ab2", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 21, 28, 1, "27df4275", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 22, 29, 1, "a346992f", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 23, 30, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 24, 31, 5, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 25, 36, 1, "65b09b1d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 26, 37, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 27, 38, 1, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 28, 39, 1, "5d4cd003", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 29, 40, 1, "41fb1ca0", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 30, 41, 1, "2dc31040", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 31, 42, 1, "764df57e", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 32, 43, 1, "5280f4e6", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 33, 44, 1, "613d5eb9", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 34, 45, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 35, 46, 1, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 36, 47, 1, "f004c4a8", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 37, 48, 1, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 38, 49, 1, "9c82357b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 39, 50, 1, "d6258deb", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 40, 51, 1, "b311e313", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 41, 52, 1, "3412391d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 42, 53, 1, "bfc9ca59", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 43, 54, 1, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 44, 55, 1, "edebceff", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 45, 56, 1, "743a4b3b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 46, 57, 1, "0a32dca5", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 47, 58, 1, "590fb68b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 48, 59, 1, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 49, 60, 1, "d12299fe", "src/git.h"); } @@ -214,7 +212,21 @@ void test_blame_simple__trivial_libgit2(void) * aa06ecca 8 (Ben Straub 2013-02-12 15:14:46 -0800 13 * aa06ecca 9 (Ben Straub 2013-02-12 15:14:46 -0800 14 * aa06ecca 10 (Ben Straub 2013-02-12 15:14:46 -0800 15 - * + */ +void test_blame_simple__can_restrict_lines_min(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); + + opts.min_line = 8; + cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); + cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 8, 3, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, "aa06ecca", "b.txt"); +} + +/* * $ git blame -n b.txt -L ,6 * orig line no final line no * commit V author timestamp V @@ -235,38 +247,42 @@ void test_blame_simple__trivial_libgit2(void) * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 * 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7 */ -void test_blame_simple__can_restrict_to_lines(void) +void test_blame_simple__can_restrict_lines_max(void) { - git_blame *blame = NULL; - git_repository *repo; git_blame_options opts = GIT_BLAME_OPTIONS_INIT; - cl_git_pass(git_repository_open(&repo, cl_fixture("blametest.git"))); + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); - opts.min_line = 8; - cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); - cl_assert_equal_i(2, git_blame_get_hunk_count(blame)); - check_blame_hunk_index(repo, blame, 0, 8, 3, "63d671eb", "b.txt"); - check_blame_hunk_index(repo, blame, 1, 11, 5, "aa06ecca", "b.txt"); - - opts.min_line = 0; opts.max_line = 6; - cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); - cl_assert_equal_i(3, git_blame_get_hunk_count(blame)); - check_blame_hunk_index(repo, blame, 0, 1, 4, "da237394", "b.txt"); - check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(repo, blame, 2, 6, 1, "63d671eb", "b.txt"); + cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); + cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 1, "63d671eb", "b.txt"); +} + +/* + * $ git blame -n b.txt -L 2,7 + * orig line no final line no + * commit V author timestamp V + * da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2 + * da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3 + * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 + * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 + * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 + * 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7 + */ +void test_blame_simple__can_restrict_lines_both(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("blametest.git"))); opts.min_line = 2; opts.max_line = 7; - cl_git_pass(git_blame_file(&blame, repo, "b.txt", &opts)); - cl_assert_equal_i(3, git_blame_get_hunk_count(blame)); - check_blame_hunk_index(repo, blame, 0, 2, 3, "da237394", "b.txt"); - check_blame_hunk_index(repo, blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(repo, blame, 2, 6, 2, "63d671eb", "b.txt"); - - git_blame_free(blame); - git_repository_free(repo); + cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); + cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 2, 3, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 2, "63d671eb", "b.txt"); } - -/* TODO: no newline at end of file? */ From 25c47aaee2c36d78a11dcddf066b82abb53819b9 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 20 Sep 2013 14:31:51 -0700 Subject: [PATCH 11/35] Detect boundaries, support limiting commit range --- include/git2/blame.h | 4 + src/blame.c | 1 + src/blame_git.c | 15 ++- src/blame_git.h | 4 + tests-clar/blame/blame_helpers.c | 8 +- tests-clar/blame/blame_helpers.h | 1 + tests-clar/blame/buffer.c | 24 ++--- tests-clar/blame/simple.c | 165 +++++++++++++++++-------------- 8 files changed, 132 insertions(+), 90 deletions(-) diff --git a/include/git2/blame.h b/include/git2/blame.h index fe1311362..652bc8180 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -93,6 +93,8 @@ typedef struct git_blame_options { * - `orig_start_line_number` is the 1-based line number where this hunk begins * in the file named by `orig_path` in the commit specified by * `orig_commit_id`. + * - `boundary` is 1 iff the hunk has been tracked to a boundary commit (the + * root, or the commit specified in git_blame_options.oldest_commit) */ typedef struct git_blame_hunk { uint16_t lines_in_hunk; @@ -103,6 +105,8 @@ typedef struct git_blame_hunk { git_oid orig_commit_id; const char *orig_path; uint16_t orig_start_line_number; + + char boundary; } git_blame_hunk; diff --git a/src/blame.c b/src/blame.c index a4ac84fde..b7886aa5c 100644 --- a/src/blame.c +++ b/src/blame.c @@ -247,6 +247,7 @@ static git_blame_hunk* hunk_from_entry(struct blame_entry *e) git_blame_hunk *h = new_hunk( e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); + h->boundary = e->is_boundary ? 1 : 0; return h; } diff --git a/src/blame_git.c b/src/blame_git.c index bff36c50c..02628c4ba 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -456,8 +456,12 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, uint32_t op GIT_UNUSED(opt); num_sg = git_commit_parentcount(commit); - if (!num_sg) + if (!git_oid_cmp(git_commit_id(commit), &sb->blame->options.oldest_commit)) + num_sg = 0; + if (!num_sg) { + git_oid_cpy(&sb->blame->options.oldest_commit, git_commit_id(commit)); goto finish; + } else if (num_sg < (int)ARRAY_SIZE(sg_buf)) memset(sg_buf, 0, sizeof(sg_buf)); else @@ -558,9 +562,14 @@ void assign_blame(struct scoreboard *sb, uint32_t opt) pass_blame(sb, suspect, opt); /* Take responsibility for the remaining entries */ - for (ent = sb->ent; ent; ent = ent->next) - if (same_suspect(ent->suspect, suspect)) + for (ent = sb->ent; ent; ent = ent->next) { + if (same_suspect(ent->suspect, suspect)) { ent->guilty = 1; + ent->is_boundary = !git_oid_cmp( + git_commit_id(suspect->commit), + &sb->blame->options.oldest_commit); + } + } origin_decref(suspect); } } diff --git a/src/blame_git.h b/src/blame_git.h index 89ebc3228..6af804db2 100644 --- a/src/blame_git.h +++ b/src/blame_git.h @@ -55,6 +55,10 @@ struct blame_entry { * scanning the lines over and over. */ unsigned score; + + /* Whether this entry has been tracked to a boundary commit. + */ + bool is_boundary; }; /* diff --git a/tests-clar/blame/blame_helpers.c b/tests-clar/blame/blame_helpers.c index 25c62a1ff..d64bb5c4c 100644 --- a/tests-clar/blame/blame_helpers.c +++ b/tests-clar/blame/blame_helpers.c @@ -15,7 +15,7 @@ void hunk_message(size_t idx, const git_blame_hunk *hunk, const char *fmt, ...) } void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx, - int start_line, int len, const char *commit_id, const char *orig_path) + int start_line, int len, char boundary, const char *commit_id, const char *orig_path) { char expected[41] = {0}, actual[41] = {0}; const git_blame_hunk *hunk = git_blame_get_hunk_byindex(blame, idx); @@ -53,6 +53,12 @@ void check_blame_hunk_index(git_repository *repo, git_blame *blame, int idx, hunk->orig_path, orig_path); } cl_assert_equal_s(hunk->orig_path, orig_path); + + if (hunk->boundary != boundary) { + hunk_message(idx, hunk, "doesn't match boundary flag (got %d, expected %d)\n", + hunk->boundary, boundary); + } + cl_assert_equal_i(boundary, hunk->boundary); } diff --git a/tests-clar/blame/blame_helpers.h b/tests-clar/blame/blame_helpers.h index 15fa1c33e..94321a5b5 100644 --- a/tests-clar/blame/blame_helpers.h +++ b/tests-clar/blame/blame_helpers.h @@ -9,6 +9,7 @@ void check_blame_hunk_index( int idx, int start_line, int len, + char boundary, const char *commit_id, const char *orig_path); diff --git a/tests-clar/blame/buffer.c b/tests-clar/blame/buffer.c index 20435eb6c..69b2d5440 100644 --- a/tests-clar/blame/buffer.c +++ b/tests-clar/blame/buffer.c @@ -38,7 +38,7 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); cl_assert_equal_i(5, git_blame_get_hunk_count(g_bufferblame)); - check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "000000", "b.txt"); } void test_blame_buffer__deleted_line(void) @@ -59,9 +59,9 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); - check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 3, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 3, 9, 1, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 4, 10, 5, "aa06ecca", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 3, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 9, 1, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 10, 5, 0, "aa06ecca", "b.txt"); } void test_blame_buffer__add_splits_hunk(void) @@ -84,9 +84,9 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); - check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 2, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 3, 8, 1, "00000000", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 4, 9, 3, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 2, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 8, 1, 0, "00000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 9, 3, 0, "63d671eb", "b.txt"); } void test_blame_buffer__delete_crosses_hunk_boundary(void) @@ -101,8 +101,8 @@ BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); - check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 2, "aa06ecca", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 2, 0, "aa06ecca", "b.txt"); } void test_blame_buffer__replace_line(void) @@ -124,7 +124,7 @@ CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\n\n"; cl_git_pass(git_blame_buffer(&g_bufferblame, g_fileblame, buffer, strlen(buffer))); - check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 1, "00000000", "b.txt"); - check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 2, 6, 1, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 3, 7, 1, 0, "00000000", "b.txt"); + check_blame_hunk_index(g_repo, g_bufferblame, 4, 8, 3, 0, "63d671eb", "b.txt"); } diff --git a/tests-clar/blame/simple.c b/tests-clar/blame/simple.c index ad5cf8fb4..79bd56b83 100644 --- a/tests-clar/blame/simple.c +++ b/tests-clar/blame/simple.c @@ -28,8 +28,8 @@ void test_blame_simple__trivial_testrepo(void) cl_git_pass(git_blame_file(&g_blame, g_repo, "branch_file.txt", NULL)); cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame)); - check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, "c47800c7", "branch_file.txt"); - check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, "a65fedf3", "branch_file.txt"); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 0, "c47800c7", "branch_file.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf3", "branch_file.txt"); } /* @@ -58,10 +58,10 @@ void test_blame_simple__trivial_blamerepo(void) cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", NULL)); cl_assert_equal_i(4, git_blame_get_hunk_count(g_blame)); - check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, "da237394", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, "aa06ecca", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 5, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 3, 11, 5, 0, "aa06ecca", "b.txt"); } @@ -147,56 +147,56 @@ void test_blame_simple__trivial_libgit2(void) cl_git_pass(git_blame_file(&g_blame, g_repo, "include/git2.h", &opts)); - check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, "359fc2d2", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 2, 3, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 3, 4, 2, "bb742ede", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 4, 6, 5, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 5, 11, 1, "96fab093", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 6, 12, 1, "9d1dcca2", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 7, 13, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 8, 14, 1, "a15c550d", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 9, 15, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 10, 16, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 11, 17, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 12, 18, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 13, 19, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 14, 20, 1, "638c2ca4", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 15, 21, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 16, 22, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 17, 23, 2, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 18, 25, 1, "bf787bd8", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 19, 26, 1, "0984c876", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 20, 27, 1, "2f8a8ab2", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 21, 28, 1, "27df4275", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 22, 29, 1, "a346992f", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 23, 30, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 24, 31, 5, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 25, 36, 1, "65b09b1d", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 26, 37, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 27, 38, 1, "44908fe7", "src/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 28, 39, 1, "5d4cd003", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 29, 40, 1, "41fb1ca0", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 30, 41, 1, "2dc31040", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 31, 42, 1, "764df57e", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 32, 43, 1, "5280f4e6", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 33, 44, 1, "613d5eb9", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 34, 45, 1, "d12299fe", "src/git.h"); - check_blame_hunk_index(g_repo, g_blame, 35, 46, 1, "111ee3fe", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 36, 47, 1, "f004c4a8", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 37, 48, 1, "111ee3fe", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 38, 49, 1, "9c82357b", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 39, 50, 1, "d6258deb", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 40, 51, 1, "b311e313", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 41, 52, 1, "3412391d", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 42, 53, 1, "bfc9ca59", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 43, 54, 1, "bf477ed4", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 44, 55, 1, "edebceff", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 45, 56, 1, "743a4b3b", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 46, 57, 1, "0a32dca5", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 47, 58, 1, "590fb68b", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 48, 59, 1, "bf477ed4", "include/git2.h"); - check_blame_hunk_index(g_repo, g_blame, 49, 60, 1, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "359fc2d2", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 2, 3, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 3, 4, 2, 0, "bb742ede", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 4, 6, 5, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 5, 11, 1, 0, "96fab093", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 6, 12, 1, 0, "9d1dcca2", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 7, 13, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 8, 14, 1, 0, "a15c550d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 9, 15, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 10, 16, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 11, 17, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 12, 18, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 13, 19, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 14, 20, 1, 0, "638c2ca4", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 15, 21, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 16, 22, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 17, 23, 2, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 18, 25, 1, 0, "bf787bd8", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 19, 26, 1, 0, "0984c876", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 20, 27, 1, 0, "2f8a8ab2", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 21, 28, 1, 0, "27df4275", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 22, 29, 1, 0, "a346992f", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 23, 30, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 24, 31, 5, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 25, 36, 1, 0, "65b09b1d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 26, 37, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 27, 38, 1, 0, "44908fe7", "src/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 28, 39, 1, 0, "5d4cd003", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 29, 40, 1, 0, "41fb1ca0", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 30, 41, 1, 0, "2dc31040", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 31, 42, 1, 0, "764df57e", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 32, 43, 1, 0, "5280f4e6", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 33, 44, 1, 0, "613d5eb9", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 34, 45, 1, 0, "d12299fe", "src/git.h"); + check_blame_hunk_index(g_repo, g_blame, 35, 46, 1, 0, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 36, 47, 1, 0, "f004c4a8", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 37, 48, 1, 0, "111ee3fe", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 38, 49, 1, 0, "9c82357b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 39, 50, 1, 0, "d6258deb", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 40, 51, 1, 0, "b311e313", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 41, 52, 1, 0, "3412391d", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 42, 53, 1, 0, "bfc9ca59", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 43, 54, 1, 0, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 44, 55, 1, 0, "edebceff", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 45, 56, 1, 0, "743a4b3b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 46, 57, 1, 0, "0a32dca5", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 47, 58, 1, 0, "590fb68b", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 48, 59, 1, 0, "bf477ed4", "include/git2.h"); + check_blame_hunk_index(g_repo, g_blame, 49, 60, 1, 0, "d12299fe", "src/git.h"); } @@ -222,8 +222,8 @@ void test_blame_simple__can_restrict_lines_min(void) opts.min_line = 8; cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame)); - check_blame_hunk_index(g_repo, g_blame, 0, 8, 3, "63d671eb", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, "aa06ecca", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 0, 8, 3, 0, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 11, 5, 0, "aa06ecca", "b.txt"); } /* @@ -236,16 +236,6 @@ void test_blame_simple__can_restrict_lines_min(void) * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 - * - * $ git blame -n b.txt -L 2,7 - * orig line no final line no - * commit V author timestamp V - * da237394 2 (Ben Straub 2013-02-12 15:11:30 -0800 2 - * da237394 3 (Ben Straub 2013-02-12 15:11:30 -0800 3 - * da237394 4 (Ben Straub 2013-02-12 15:11:30 -0800 4 - * ^b99f7ac 1 (Ben Straub 2013-02-12 15:10:12 -0800 5 - * 63d671eb 6 (Ben Straub 2013-02-12 15:13:04 -0800 6 - * 63d671eb 7 (Ben Straub 2013-02-12 15:13:04 -0800 7 */ void test_blame_simple__can_restrict_lines_max(void) { @@ -256,9 +246,9 @@ void test_blame_simple__can_restrict_lines_max(void) opts.max_line = 6; cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame)); - check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, "da237394", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 2, 6, 1, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 4, 0, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 1, 0, "63d671eb", "b.txt"); } /* @@ -282,7 +272,34 @@ void test_blame_simple__can_restrict_lines_both(void) opts.max_line = 7; cl_git_pass(git_blame_file(&g_blame, g_repo, "b.txt", &opts)); cl_assert_equal_i(3, git_blame_get_hunk_count(g_blame)); - check_blame_hunk_index(g_repo, g_blame, 0, 2, 3, "da237394", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, "b99f7ac0", "b.txt"); - check_blame_hunk_index(g_repo, g_blame, 2, 6, 2, "63d671eb", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 0, 2, 3, 0, "da237394", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 5, 1, 1, "b99f7ac0", "b.txt"); + check_blame_hunk_index(g_repo, g_blame, 2, 6, 2, 0, "63d671eb", "b.txt"); +} + +/* + * $ git blame -n branch_file.txt be3563a..HEAD + * orig line no final line no + * commit V author timestamp V + * ^be3563a 1 (Scott Chacon 2010-05-25 11:58:27 -0700 1) hi + * a65fedf3 2 (Scott Chacon 2011-08-09 19:33:46 -0700 2) bye! + */ +void test_blame_simple__can_restrict_to_newish_commits(void) +{ + git_blame_options opts = GIT_BLAME_OPTIONS_INIT; + + cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git"))); + + { + git_object *obj; + cl_git_pass(git_revparse_single(&obj, g_repo, "be3563a")); + git_oid_cpy(&opts.oldest_commit, git_object_id(obj)); + git_object_free(obj); + } + + cl_git_pass(git_blame_file(&g_blame, g_repo, "branch_file.txt", &opts)); + + cl_assert_equal_i(2, git_blame_get_hunk_count(g_blame)); + check_blame_hunk_index(g_repo, g_blame, 0, 1, 1, 1, "be3563a", "branch_file.txt"); + check_blame_hunk_index(g_repo, g_blame, 1, 2, 1, 0, "a65fedf", "branch_file.txt"); } From a121e580113cb3206166b432ad8ebfd85c0c4d5f Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 20 Sep 2013 15:20:03 -0700 Subject: [PATCH 12/35] Add typedefs for internal structs --- src/blame.c | 14 +++--- src/blame_git.c | 110 +++++++++++++++++++++++++----------------------- src/blame_git.h | 36 ++++++++-------- 3 files changed, 82 insertions(+), 78 deletions(-) diff --git a/src/blame.c b/src/blame.c index b7886aa5c..3b9d3db3b 100644 --- a/src/blame.c +++ b/src/blame.c @@ -221,7 +221,7 @@ static git_blame_hunk *split_hunk_in_vector( * To allow quick access to the contents of nth line in the * final image, prepare an index in the scoreboard. */ -static int prepare_lines(struct scoreboard *sb) +static int prepare_lines(git_blame__scoreboard *sb) { const char *buf = sb->final_buf; git_off_t len = sb->final_buf_size; @@ -242,7 +242,7 @@ static int prepare_lines(struct scoreboard *sb) return sb->num_lines; } -static git_blame_hunk* hunk_from_entry(struct blame_entry *e) +static git_blame_hunk* hunk_from_entry(git_blame__entry *e) { git_blame_hunk *h = new_hunk( e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); @@ -255,10 +255,10 @@ static int walk_and_mark(git_blame *blame) { int error; - struct scoreboard sb = {0}; - struct blame_entry *ent = NULL; + git_blame__scoreboard sb = {0}; + git_blame__entry *ent = NULL; git_blob *blob = NULL; - struct origin *o; + git_blame__origin *o; if ((error = git_commit_lookup(&sb.final, blame->repository, &blame->options.newest_commit)) < 0 || (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)sb.final, blame->path, GIT_OBJ_BLOB)) < 0) @@ -287,8 +287,8 @@ static int walk_and_mark(git_blame *blame) cleanup: for (ent = sb.ent; ent; ) { - struct blame_entry *e = ent->next; - struct origin *o = ent->suspect; + git_blame__entry *e = ent->next; + git_blame__origin *o = ent->suspect; git_vector_insert(&blame->hunks, hunk_from_entry(ent)); diff --git a/src/blame_git.c b/src/blame_git.c index 02628c4ba..8fd06dac3 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -11,13 +11,15 @@ /* * Locate an existing origin or create a new one. */ -int get_origin(struct origin **out, struct scoreboard *sb, git_commit *commit, const char *path) +int get_origin(git_blame__origin **out, git_blame__scoreboard *sb, git_commit *commit, const char *path) { - struct blame_entry *e; + git_blame__entry *e; - for (e = sb->ent; e; e = e->next) - if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) + for (e = sb->ent; e; e = e->next) { + if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) { *out = origin_incref(e->suspect); + } + } return make_origin(out, commit, path); } @@ -27,10 +29,11 @@ int get_origin(struct origin **out, struct scoreboard *sb, git_commit *commit, c * get_origin() to obtain shared, refcounted copy instead of calling * this function directly. */ -int make_origin(struct origin **out, git_commit *commit, const char *path) +int make_origin(git_blame__origin **out, git_commit *commit, const char *path) { int error = 0; - struct origin *o; + git_blame__origin *o; + o = git__calloc(1, sizeof(*o) + strlen(path) + 1); GITERR_CHECK_ALLOC(o); o->commit = commit; @@ -38,22 +41,23 @@ int make_origin(struct origin **out, git_commit *commit, const char *path) strcpy(o->path, path); if (!(error = git_object_lookup_bypath((git_object**)&o->blob, (git_object*)commit, - path, GIT_OBJ_BLOB))) + path, GIT_OBJ_BLOB))) { *out = o; - else + } else { origin_decref(o); + } return error; } struct blame_chunk_cb_data { - struct scoreboard *sb; - struct origin *target; - struct origin *parent; - long plno; + git_blame__scoreboard *sb; + git_blame__origin *target; + git_blame__origin *parent; long tlno; + long plno; }; -static bool same_suspect(struct origin *a, struct origin *b) +static bool same_suspect(git_blame__origin *a, git_blame__origin *b) { if (a == b) return true; @@ -63,9 +67,9 @@ static bool same_suspect(struct origin *a, struct origin *b) } /* Find the line number of the last line the target is suspected for */ -static int find_last_in_target(struct scoreboard *sb, struct origin *target) +static int find_last_in_target(git_blame__scoreboard *sb, git_blame__origin *target) { - struct blame_entry *e; + git_blame__entry *e; int last_in_target = -1; for (e=sb->ent; e; e=e->next) { @@ -91,8 +95,8 @@ static int find_last_in_target(struct scoreboard *sb, struct origin *target) * Split e into potentially three parts; before this chunk, the chunk * to be blamed for the parent, and after that portion. */ -static void split_overlap(struct blame_entry *split, struct blame_entry *e, - int tlno, int plno, int same, struct origin *parent) +static void split_overlap(git_blame__entry *split, git_blame__entry *e, + int tlno, int plno, int same, git_blame__origin *parent) { int chunk_end_lno; @@ -134,9 +138,9 @@ static void split_overlap(struct blame_entry *split, struct blame_entry *e, * Link in a new blame entry to the scoreboard. Entries that cover the same * line range have been removed from the scoreboard previously. */ -static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) +static void add_blame_entry(git_blame__scoreboard *sb, git_blame__entry *e) { - struct blame_entry *ent, *prev = NULL; + git_blame__entry *ent, *prev = NULL; origin_incref(e->suspect); @@ -161,9 +165,9 @@ static void add_blame_entry(struct scoreboard *sb, struct blame_entry *e) * a malloced blame_entry that is already on the linked list of the scoreboard. * The origin of dst loses a refcnt while the origin of src gains one. */ -static void dup_entry(struct blame_entry *dst, struct blame_entry *src) +static void dup_entry(git_blame__entry *dst, git_blame__entry *src) { - struct blame_entry *p, *n; + git_blame__entry *p, *n; p = dst->prev; n = dst->next; @@ -179,9 +183,9 @@ static void dup_entry(struct blame_entry *dst, struct blame_entry *src) * split_overlap() divided an existing blame e into up to three parts in split. * Adjust the linked list of blames in the scoreboard to reflect the split. */ -static void split_blame(struct scoreboard *sb, struct blame_entry *split, struct blame_entry *e) +static void split_blame(git_blame__scoreboard *sb, git_blame__entry *split, git_blame__entry *e) { - struct blame_entry *new_entry; + git_blame__entry *new_entry; if (split[0].suspect && split[2].suspect) { /* The first part (reuse storage for the existing entry e */ @@ -189,12 +193,12 @@ static void split_blame(struct scoreboard *sb, struct blame_entry *split, struct /* The last part -- me */ new_entry = git__malloc(sizeof(*new_entry)); - memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); add_blame_entry(sb, new_entry); /* ... and the middle part -- parent */ new_entry = git__malloc(sizeof(*new_entry)); - memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); add_blame_entry(sb, new_entry); } else if (!split[0].suspect && !split[2].suspect) { /* @@ -206,13 +210,13 @@ static void split_blame(struct scoreboard *sb, struct blame_entry *split, struct /* me and then parent */ dup_entry(e, &split[0]); new_entry = git__malloc(sizeof(*new_entry)); - memcpy(new_entry, &(split[1]), sizeof(struct blame_entry)); + memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); add_blame_entry(sb, new_entry); } else { /* parent and then me */ dup_entry(e, &split[1]); new_entry = git__malloc(sizeof(*new_entry)); - memcpy(new_entry, &(split[2]), sizeof(struct blame_entry)); + memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); add_blame_entry(sb, new_entry); } } @@ -221,7 +225,7 @@ static void split_blame(struct scoreboard *sb, struct blame_entry *split, struct * After splitting the blame, the origins used by the on-stack blame_entry * should lose one refcnt each. */ -static void decref_split(struct blame_entry *split) +static void decref_split(git_blame__entry *split) { int i; for (i=0; i<3; i++) @@ -232,9 +236,9 @@ static void decref_split(struct blame_entry *split) * Helper for blame_chunk(). blame_entry e is known to overlap with the patch * hunk; split it and pass blame to the parent. */ -static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, int tlno, int plno, int same, struct origin *parent) +static void blame_overlap(git_blame__scoreboard *sb, git_blame__entry *e, int tlno, int plno, int same, git_blame__origin *parent) { - struct blame_entry split[3] = {{0}}; + git_blame__entry split[3] = {{0}}; split_overlap(split, e, tlno, plno, same, parent); if (split[1].suspect) @@ -247,9 +251,9 @@ static void blame_overlap(struct scoreboard *sb, struct blame_entry *e, int tlno * e and its parent. Find and split the overlap, and pass blame to the * overlapping part to the parent. */ -static void blame_chunk(struct scoreboard *sb, int tlno, int plno, int same, struct origin *target, struct origin *parent) +static void blame_chunk(git_blame__scoreboard *sb, int tlno, int plno, int same, git_blame__origin *target, git_blame__origin *parent) { - struct blame_entry *e; + git_blame__entry *e; for (e = sb->ent; e; e = e->next) { if (e->guilty || !same_suspect(e->suspect, target)) @@ -328,7 +332,7 @@ static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, void *cb_data) return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); } -static void fill_origin_blob(struct origin *o, mmfile_t *file) +static void fill_origin_blob(git_blame__origin *o, mmfile_t *file) { memset(file, 0, sizeof(*file)); if (o->blob) { @@ -337,9 +341,9 @@ static void fill_origin_blob(struct origin *o, mmfile_t *file) } } -static int pass_blame_to_parent(struct scoreboard *sb, - struct origin *target, - struct origin *parent) +static int pass_blame_to_parent(git_blame__scoreboard *sb, + git_blame__origin *target, + git_blame__origin *parent) { int last_in_target; mmfile_t file_p, file_o; @@ -365,10 +369,10 @@ static int paths_on_dup(void **old, void *new) git__free(new); return -1; } -static struct origin* find_origin(struct scoreboard *sb, git_commit *parent, - struct origin *origin) +static git_blame__origin* find_origin(git_blame__scoreboard *sb, git_commit *parent, + git_blame__origin *origin) { - struct origin *porigin = NULL; + git_blame__origin *porigin = NULL; git_diff_list *difflist = NULL; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_tree *otree=NULL, *ptree=NULL; @@ -429,10 +433,10 @@ cleanup: * The blobs of origin and porigin exactly match, so everything origin is * suspected for can be blamed on the parent. */ -static void pass_whole_blame(struct scoreboard *sb, - struct origin *origin, struct origin *porigin) +static void pass_whole_blame(git_blame__scoreboard *sb, + git_blame__origin *origin, git_blame__origin *porigin) { - struct blame_entry *e; + git_blame__entry *e; if (!porigin->blob) git_object_lookup((git_object**)&porigin->blob, sb->blame->repository, git_blob_id(origin->blob), @@ -446,12 +450,12 @@ static void pass_whole_blame(struct scoreboard *sb, } } -static void pass_blame(struct scoreboard *sb, struct origin *origin, uint32_t opt) +static void pass_blame(git_blame__scoreboard *sb, git_blame__origin *origin, uint32_t opt) { git_commit *commit = origin->commit; int i, num_sg; - struct origin *sg_buf[16]; - struct origin *porigin, **sg_origin = sg_buf; + git_blame__origin *sg_buf[16]; + git_blame__origin *porigin, **sg_origin = sg_buf; GIT_UNUSED(opt); @@ -498,7 +502,7 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, uint32_t op } for (i=0; iprevious) { @@ -526,14 +530,14 @@ finish: * Origin is refcounted and usually we keep the blob contents to be * reused. */ -struct origin *origin_incref(struct origin *o) +git_blame__origin *origin_incref(git_blame__origin *o) { if (o) o->refcnt++; return o; } -void origin_decref(struct origin *o) +void origin_decref(git_blame__origin *o) { if (o && --o->refcnt <= 0) { if (o->previous) @@ -544,11 +548,11 @@ void origin_decref(struct origin *o) } } -void assign_blame(struct scoreboard *sb, uint32_t opt) +void assign_blame(git_blame__scoreboard *sb, uint32_t opt) { while (true) { - struct blame_entry *ent; - struct origin *suspect = NULL; + git_blame__entry *ent; + git_blame__origin *suspect = NULL; /* Find a suspect to break down */ for (ent = sb->ent; !suspect && ent; ent = ent->next) @@ -574,9 +578,9 @@ void assign_blame(struct scoreboard *sb, uint32_t opt) } } -void coalesce(struct scoreboard *sb) +void coalesce(git_blame__scoreboard *sb) { - struct blame_entry *ent, *next; + git_blame__entry *ent, *next; for (ent=sb->ent; ent && (next = ent->next); ent = next) { if (same_suspect(ent->suspect, next->suspect) && diff --git a/src/blame_git.h b/src/blame_git.h index 6af804db2..7d229c664 100644 --- a/src/blame_git.h +++ b/src/blame_git.h @@ -9,22 +9,22 @@ /* * One blob in a commit that is being suspected */ -struct origin { +typedef struct git_blame__origin { int refcnt; - struct origin *previous; + struct git_blame__origin *previous; git_commit *commit; git_blob *blob; char path[]; -}; +} git_blame__origin; /* - * Each group of lines is described by a blame_entry; it can be split + * Each group of lines is described by a git_blame__entry; it can be split * as we pass blame to the parents. They form a linked list in the * scoreboard structure, sorted by the target line number. */ -struct blame_entry { - struct blame_entry *prev; - struct blame_entry *next; +typedef struct git_blame__entry { + struct git_blame__entry *prev; + struct git_blame__entry *next; /* the first line of this group in the final image; * internally all line numbers are 0 based. @@ -35,7 +35,7 @@ struct blame_entry { int num_lines; /* the commit that introduced this group into the final image */ - struct origin *suspect; + git_blame__origin *suspect; /* true if the suspect is truly guilty; false while we have not * checked if the group came from one of its parents. @@ -59,12 +59,12 @@ struct blame_entry { /* Whether this entry has been tracked to a boundary commit. */ bool is_boundary; -}; +} git_blame__entry; /* * The current state of the blame assignment. */ -struct scoreboard { +typedef struct git_blame__scoreboard { /* the final commit (i.e. where we started digging from) */ git_commit *final; const char *path; @@ -78,20 +78,20 @@ struct scoreboard { git_off_t final_buf_size; /* linked list of blames */ - struct blame_entry *ent; + git_blame__entry *ent; /* look-up a line in the final buffer */ int num_lines; git_blame *blame; -}; +} git_blame__scoreboard; -int get_origin(struct origin **out, struct scoreboard *sb, git_commit *commit, const char *path); -int make_origin(struct origin **out, git_commit *commit, const char *path); -struct origin *origin_incref(struct origin *o); -void origin_decref(struct origin *o); -void assign_blame(struct scoreboard *sb, uint32_t flags); -void coalesce(struct scoreboard *sb); +int get_origin(git_blame__origin **out, git_blame__scoreboard *sb, git_commit *commit, const char *path); +int make_origin(git_blame__origin **out, git_commit *commit, const char *path); +git_blame__origin *origin_incref(git_blame__origin *o); +void origin_decref(git_blame__origin *o); +void assign_blame(git_blame__scoreboard *sb, uint32_t flags); +void coalesce(git_blame__scoreboard *sb); #endif From ef03d040cf6cc81d71d340cf3915735724a77449 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 20 Sep 2013 15:38:15 -0700 Subject: [PATCH 13/35] Trim fat from git_blame struct --- src/blame.c | 11 ----------- src/blame.h | 7 ------- tests-clar/blame/getters.c | 2 +- 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/src/blame.c b/src/blame.c index 3b9d3db3b..e3460ba3b 100644 --- a/src/blame.c +++ b/src/blame.c @@ -62,11 +62,6 @@ static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_st return hunk; } -git_blame_hunk* git_blame__alloc_hunk() -{ - return new_hunk(0,0,0,NULL); -} - static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) { git_blame_hunk *newhunk = new_hunk(hunk->final_start_line_number, hunk->lines_in_hunk, hunk->orig_start_line_number, hunk->orig_path); @@ -106,13 +101,11 @@ git_blame* git_blame__alloc( return NULL; } git_vector_init(&gbr->hunks, 8, hunk_sort_cmp_by_start_line); - git_vector_init(&gbr->unclaimed_hunks, 8, hunk_sort_cmp_by_start_line); git_vector_init(&gbr->paths, 8, paths_cmp); gbr->repository = repo; gbr->options = opts; gbr->path = git__strdup(path); git_vector_insert(&gbr->paths, git__strdup(path)); - gbr->final_blob = NULL; return gbr; } @@ -128,10 +121,6 @@ void git_blame_free(git_blame *blame) free_hunk(hunk); git_vector_free(&blame->hunks); - git_vector_foreach(&blame->unclaimed_hunks, i, hunk) - free_hunk(hunk); - git_vector_free(&blame->unclaimed_hunks); - git_vector_foreach(&blame->paths, i, path) git__free(path); git_vector_free(&blame->paths); diff --git a/src/blame.h b/src/blame.h index 260d5b5a1..1b107320b 100644 --- a/src/blame.h +++ b/src/blame.h @@ -14,16 +14,11 @@ struct git_blame { git_blame_options options; git_vector hunks; - git_vector unclaimed_hunks; git_vector paths; git_blob *final_blob; - size_t num_lines; - git_oid current_commit; - git_oid parent_commit; size_t current_diff_line; - size_t current_blame_line; git_blame_hunk *current_hunk; }; @@ -32,6 +27,4 @@ git_blame *git_blame__alloc( git_blame_options opts, const char *path); -git_blame_hunk *git_blame__alloc_hunk(); - #endif diff --git a/tests-clar/blame/getters.c b/tests-clar/blame/getters.c index 82e0de59e..8d67e3e45 100644 --- a/tests-clar/blame/getters.c +++ b/tests-clar/blame/getters.c @@ -20,7 +20,7 @@ void test_blame_getters__initialize(void) g_blame = git_blame__alloc(NULL, opts, ""); for (i=0; i<5; i++) { - git_blame_hunk *h = git_blame__alloc_hunk(); + git_blame_hunk *h = git__calloc(1, sizeof(git_blame_hunk)); h->final_start_line_number = hunks[i].final_start_line_number; h->orig_path = git__strdup(hunks[i].orig_path); h->lines_in_hunk = hunks[i].lines_in_hunk; From 0a0f0558a44d3eb176c32bff57b84f135d65db7a Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Fri, 20 Sep 2013 15:51:22 -0700 Subject: [PATCH 14/35] git_blame is a scoreboard --- src/blame.c | 34 ++++++++--------- src/blame.h | 62 +++++++++++++++++++++++++++++++ src/blame_git.c | 99 +++++++++++++++++++++++++------------------------ src/blame_git.h | 89 ++------------------------------------------ 4 files changed, 132 insertions(+), 152 deletions(-) diff --git a/src/blame.c b/src/blame.c index e3460ba3b..c85aa7d30 100644 --- a/src/blame.c +++ b/src/blame.c @@ -210,10 +210,10 @@ static git_blame_hunk *split_hunk_in_vector( * To allow quick access to the contents of nth line in the * final image, prepare an index in the scoreboard. */ -static int prepare_lines(git_blame__scoreboard *sb) +static int prepare_lines(git_blame *blame) { - const char *buf = sb->final_buf; - git_off_t len = sb->final_buf_size; + const char *buf = blame->final_buf; + git_off_t len = blame->final_buf_size; int num = 0, incomplete = 0, bol = 1; if (len && buf[len-1] != '\n') @@ -227,8 +227,8 @@ static int prepare_lines(git_blame__scoreboard *sb) bol = 1; } } - sb->num_lines = num + incomplete; - return sb->num_lines; + blame->num_lines = num + incomplete; + return blame->num_lines; } static git_blame_hunk* hunk_from_entry(git_blame__entry *e) @@ -244,21 +244,20 @@ static int walk_and_mark(git_blame *blame) { int error; - git_blame__scoreboard sb = {0}; git_blame__entry *ent = NULL; git_blob *blob = NULL; git_blame__origin *o; - if ((error = git_commit_lookup(&sb.final, blame->repository, &blame->options.newest_commit)) < 0 || - (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)sb.final, blame->path, GIT_OBJ_BLOB)) < 0) + if ((error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit)) < 0 || + (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final, blame->path, GIT_OBJ_BLOB)) < 0) goto cleanup; - sb.final_buf = git_blob_rawcontent(blob); - sb.final_buf_size = git_blob_rawsize(blob); - if ((error = get_origin(&o, &sb, sb.final, blame->path)) < 0) + blame->final_buf = git_blob_rawcontent(blob); + blame->final_buf_size = git_blob_rawsize(blob); + if ((error = get_origin(&o, blame, blame->final, blame->path)) < 0) goto cleanup; ent = git__calloc(1, sizeof(*ent)); - ent->num_lines = prepare_lines(&sb); + ent->num_lines = prepare_lines(blame); ent->lno = blame->options.min_line - 1; ent->num_lines = ent->num_lines - blame->options.min_line + 1; if (blame->options.max_line > 0) { @@ -267,15 +266,14 @@ static int walk_and_mark(git_blame *blame) ent->s_lno = ent->lno; ent->suspect = o; - sb.ent = ent; - sb.path = blame->path; - sb.blame = blame; + blame->ent = ent; + blame->path = blame->path; - assign_blame(&sb, blame->options.flags); - coalesce(&sb); + assign_blame(blame, blame->options.flags); + coalesce(blame); cleanup: - for (ent = sb.ent; ent; ) { + for (ent = blame->ent; ent; ) { git_blame__entry *e = ent->next; git_blame__origin *o = ent->suspect; diff --git a/src/blame.h b/src/blame.h index 1b107320b..d730cfb89 100644 --- a/src/blame.h +++ b/src/blame.h @@ -8,6 +8,61 @@ #include "array.h" #include "git2/oid.h" +/* + * One blob in a commit that is being suspected + */ +typedef struct git_blame__origin { + int refcnt; + struct git_blame__origin *previous; + git_commit *commit; + git_blob *blob; + char path[]; +} git_blame__origin; + +/* + * Each group of lines is described by a git_blame__entry; it can be split + * as we pass blame to the parents. They form a linked list in the + * scoreboard structure, sorted by the target line number. + */ +typedef struct git_blame__entry { + struct git_blame__entry *prev; + struct git_blame__entry *next; + + /* the first line of this group in the final image; + * internally all line numbers are 0 based. + */ + int lno; + + /* how many lines this group has */ + int num_lines; + + /* the commit that introduced this group into the final image */ + git_blame__origin *suspect; + + /* true if the suspect is truly guilty; false while we have not + * checked if the group came from one of its parents. + */ + char guilty; + + /* true if the entry has been scanned for copies in the current parent + */ + char scanned; + + /* the line number of the first line of this group in the + * suspect's file; internally all line numbers are 0 based. + */ + int s_lno; + + /* how significant this entry is -- cached to avoid + * scanning the lines over and over. + */ + unsigned score; + + /* Whether this entry has been tracked to a boundary commit. + */ + bool is_boundary; +} git_blame__entry; + struct git_blame { const char *path; git_repository *repository; @@ -20,6 +75,13 @@ struct git_blame { size_t current_diff_line; git_blame_hunk *current_hunk; + + /* Scoreboard fields */ + git_commit *final; + git_blame__entry *ent; + int num_lines; + const char *final_buf; + git_off_t final_buf_size; }; git_blame *git_blame__alloc( diff --git a/src/blame_git.c b/src/blame_git.c index 8fd06dac3..3f5fe8d3a 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -7,15 +7,16 @@ #include "blame_git.h" #include "commit.h" +#include "xdiff/xinclude.h" /* * Locate an existing origin or create a new one. */ -int get_origin(git_blame__origin **out, git_blame__scoreboard *sb, git_commit *commit, const char *path) +int get_origin(git_blame__origin **out, git_blame *blame, git_commit *commit, const char *path) { git_blame__entry *e; - for (e = sb->ent; e; e = e->next) { + for (e = blame->ent; e; e = e->next) { if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) { *out = origin_incref(e->suspect); } @@ -50,7 +51,7 @@ int make_origin(git_blame__origin **out, git_commit *commit, const char *path) } struct blame_chunk_cb_data { - git_blame__scoreboard *sb; + git_blame *blame; git_blame__origin *target; git_blame__origin *parent; long tlno; @@ -66,13 +67,13 @@ static bool same_suspect(git_blame__origin *a, git_blame__origin *b) return 0 == strcmp(a->path, b->path); } -/* Find the line number of the last line the target is suspected for */ -static int find_last_in_target(git_blame__scoreboard *sb, git_blame__origin *target) +/* find the line number of the last line the target is suspected for */ +static int find_last_in_target(git_blame *blame, git_blame__origin *target) { git_blame__entry *e; int last_in_target = -1; - for (e=sb->ent; e; e=e->next) { + for (e=blame->ent; e; e=e->next) { if (e->guilty || !same_suspect(e->suspect, target)) continue; if (last_in_target < e->s_lno + e->num_lines) @@ -138,13 +139,13 @@ static void split_overlap(git_blame__entry *split, git_blame__entry *e, * Link in a new blame entry to the scoreboard. Entries that cover the same * line range have been removed from the scoreboard previously. */ -static void add_blame_entry(git_blame__scoreboard *sb, git_blame__entry *e) +static void add_blame_entry(git_blame *blame, git_blame__entry *e) { git_blame__entry *ent, *prev = NULL; origin_incref(e->suspect); - for (ent = sb->ent; ent && ent->lno < e->lno; ent = ent->next) + for (ent = blame->ent; ent && ent->lno < e->lno; ent = ent->next) prev = ent; /* prev, if not NULL, is the last one that is below e */ @@ -153,8 +154,8 @@ static void add_blame_entry(git_blame__scoreboard *sb, git_blame__entry *e) e->next = prev->next; prev->next = e; } else { - e->next = sb->ent; - sb->ent = e; + e->next = blame->ent; + blame->ent = e; } if (e->next) e->next->prev = e; @@ -183,7 +184,7 @@ static void dup_entry(git_blame__entry *dst, git_blame__entry *src) * split_overlap() divided an existing blame e into up to three parts in split. * Adjust the linked list of blames in the scoreboard to reflect the split. */ -static void split_blame(git_blame__scoreboard *sb, git_blame__entry *split, git_blame__entry *e) +static void split_blame(git_blame *blame, git_blame__entry *split, git_blame__entry *e) { git_blame__entry *new_entry; @@ -194,12 +195,12 @@ static void split_blame(git_blame__scoreboard *sb, git_blame__entry *split, git_ /* The last part -- me */ new_entry = git__malloc(sizeof(*new_entry)); memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); - add_blame_entry(sb, new_entry); + add_blame_entry(blame, new_entry); /* ... and the middle part -- parent */ new_entry = git__malloc(sizeof(*new_entry)); memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); - add_blame_entry(sb, new_entry); + add_blame_entry(blame, new_entry); } else if (!split[0].suspect && !split[2].suspect) { /* * The parent covers the entire area; reuse storage for e and replace it @@ -211,13 +212,13 @@ static void split_blame(git_blame__scoreboard *sb, git_blame__entry *split, git_ dup_entry(e, &split[0]); new_entry = git__malloc(sizeof(*new_entry)); memcpy(new_entry, &(split[1]), sizeof(git_blame__entry)); - add_blame_entry(sb, new_entry); + add_blame_entry(blame, new_entry); } else { /* parent and then me */ dup_entry(e, &split[1]); new_entry = git__malloc(sizeof(*new_entry)); memcpy(new_entry, &(split[2]), sizeof(git_blame__entry)); - add_blame_entry(sb, new_entry); + add_blame_entry(blame, new_entry); } } @@ -236,13 +237,13 @@ static void decref_split(git_blame__entry *split) * Helper for blame_chunk(). blame_entry e is known to overlap with the patch * hunk; split it and pass blame to the parent. */ -static void blame_overlap(git_blame__scoreboard *sb, git_blame__entry *e, int tlno, int plno, int same, git_blame__origin *parent) +static void blame_overlap(git_blame *blame, git_blame__entry *e, int tlno, int plno, int same, git_blame__origin *parent) { git_blame__entry split[3] = {{0}}; split_overlap(split, e, tlno, plno, same, parent); if (split[1].suspect) - split_blame(sb, split, e); + split_blame(blame, split, e); decref_split(split); } @@ -251,17 +252,17 @@ static void blame_overlap(git_blame__scoreboard *sb, git_blame__entry *e, int tl * e and its parent. Find and split the overlap, and pass blame to the * overlapping part to the parent. */ -static void blame_chunk(git_blame__scoreboard *sb, int tlno, int plno, int same, git_blame__origin *target, git_blame__origin *parent) +static void blame_chunk(git_blame *blame, int tlno, int plno, int same, git_blame__origin *target, git_blame__origin *parent) { git_blame__entry *e; - for (e = sb->ent; e; e = e->next) { + for (e = blame->ent; e; e = e->next) { if (e->guilty || !same_suspect(e->suspect, target)) continue; if (same <= e->s_lno) continue; if (tlno < e->s_lno + e->num_lines) { - blame_overlap(sb, e, tlno, plno, same, parent); + blame_overlap(blame, e, tlno, plno, same, parent); } } } @@ -269,7 +270,7 @@ static void blame_chunk(git_blame__scoreboard *sb, int tlno, int plno, int same, static void blame_chunk_cb(long start_a, long count_a, long start_b, long count_b, void *data) { struct blame_chunk_cb_data *d = data; - blame_chunk(d->sb, d->tlno, d->plno, start_b, d->target, d->parent); + blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent); d->plno = start_a + count_a; d->tlno = start_b + count_b; } @@ -341,15 +342,15 @@ static void fill_origin_blob(git_blame__origin *o, mmfile_t *file) } } -static int pass_blame_to_parent(git_blame__scoreboard *sb, +static int pass_blame_to_parent(git_blame *blame, git_blame__origin *target, git_blame__origin *parent) { int last_in_target; mmfile_t file_p, file_o; - struct blame_chunk_cb_data d = { sb, target, parent, 0, 0 }; + struct blame_chunk_cb_data d = { blame, target, parent, 0, 0 }; - last_in_target = find_last_in_target(sb, target); + last_in_target = find_last_in_target(blame, target); if (last_in_target < 0) return 1; /* nothing remains for this target */ @@ -358,7 +359,7 @@ static int pass_blame_to_parent(git_blame__scoreboard *sb, diff_hunks(&file_p, &file_o, &d); /* The reset (i.e. anything after tlno) are the same as the parent */ - blame_chunk(sb, d.tlno, d.plno, last_in_target, target, parent); + blame_chunk(blame, d.tlno, d.plno, last_in_target, target, parent); return 0; } @@ -369,7 +370,7 @@ static int paths_on_dup(void **old, void *new) git__free(new); return -1; } -static git_blame__origin* find_origin(git_blame__scoreboard *sb, git_commit *parent, +static git_blame__origin* find_origin(git_blame *blame, git_commit *parent, git_blame__origin *origin) { git_blame__origin *porigin = NULL; @@ -387,14 +388,14 @@ static git_blame__origin* find_origin(git_blame__scoreboard *sb, git_commit *par diffopts.flags = GIT_DIFF_SKIP_BINARY_CHECK; /* Check to see if files we're interested have changed */ - diffopts.pathspec.count = sb->blame->paths.length; - diffopts.pathspec.strings = (char**)sb->blame->paths.contents; - if (0 != git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts)) + diffopts.pathspec.count = blame->paths.length; + diffopts.pathspec.strings = (char**)blame->paths.contents; + if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts)) goto cleanup; if (!git_diff_num_deltas(difflist)) { /* No changes; copy data */ - get_origin(&porigin, sb, parent, origin->path); + get_origin(&porigin, blame, parent, origin->path); } else { git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; int i; @@ -402,7 +403,7 @@ static git_blame__origin* find_origin(git_blame__scoreboard *sb, git_commit *par /* Generate a full diff between the two trees */ git_diff_list_free(difflist); diffopts.pathspec.count = 0; - if (0 != git_diff_tree_to_tree(&difflist, sb->blame->repository, ptree, otree, &diffopts)) + if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts)) goto cleanup; /* Let diff find renames */ @@ -414,10 +415,10 @@ static git_blame__origin* find_origin(git_blame__scoreboard *sb, git_commit *par for (i=0; i<(int)git_diff_num_deltas(difflist); i++) { const git_diff_delta *delta; git_diff_get_patch(NULL, &delta, difflist, i); - if (git_vector_bsearch(NULL, &sb->blame->paths, delta->new_file.path) != 0) + if (git_vector_bsearch(NULL, &blame->paths, delta->new_file.path) != 0) continue; - git_vector_insert_sorted(&sb->blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup); + git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup); make_origin(&porigin, parent, delta->old_file.path); } } @@ -433,15 +434,15 @@ cleanup: * The blobs of origin and porigin exactly match, so everything origin is * suspected for can be blamed on the parent. */ -static void pass_whole_blame(git_blame__scoreboard *sb, +static void pass_whole_blame(git_blame *blame, git_blame__origin *origin, git_blame__origin *porigin) { git_blame__entry *e; if (!porigin->blob) - git_object_lookup((git_object**)&porigin->blob, sb->blame->repository, git_blob_id(origin->blob), + git_object_lookup((git_object**)&porigin->blob, blame->repository, git_blob_id(origin->blob), GIT_OBJ_BLOB); - for (e=sb->ent; e; e=e->next) { + for (e=blame->ent; e; e=e->next) { if (!same_suspect(e->suspect, origin)) continue; origin_incref(porigin); @@ -450,7 +451,7 @@ static void pass_whole_blame(git_blame__scoreboard *sb, } } -static void pass_blame(git_blame__scoreboard *sb, git_blame__origin *origin, uint32_t opt) +static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt) { git_commit *commit = origin->commit; int i, num_sg; @@ -460,10 +461,10 @@ static void pass_blame(git_blame__scoreboard *sb, git_blame__origin *origin, uin GIT_UNUSED(opt); num_sg = git_commit_parentcount(commit); - if (!git_oid_cmp(git_commit_id(commit), &sb->blame->options.oldest_commit)) + if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit)) num_sg = 0; if (!num_sg) { - git_oid_cpy(&sb->blame->options.oldest_commit, git_commit_id(commit)); + git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit)); goto finish; } else if (num_sg < (int)ARRAY_SIZE(sg_buf)) @@ -479,13 +480,13 @@ static void pass_blame(git_blame__scoreboard *sb, git_blame__origin *origin, uin continue; git_commit_parent(&p, origin->commit, i); - porigin = find_origin(sb, p, origin); + porigin = find_origin(blame, p, origin); if (!porigin) continue; if (porigin->blob && origin->blob && !git_oid_cmp(git_blob_id(porigin->blob), git_blob_id(origin->blob))) { - pass_whole_blame(sb, origin, porigin); + pass_whole_blame(blame, origin, porigin); origin_decref(porigin); goto finish; } @@ -509,7 +510,7 @@ static void pass_blame(git_blame__scoreboard *sb, git_blame__origin *origin, uin origin_incref(porigin); origin->previous = porigin; } - if (pass_blame_to_parent(sb, origin, porigin)) + if (pass_blame_to_parent(blame, origin, porigin)) goto finish; } @@ -548,14 +549,14 @@ void origin_decref(git_blame__origin *o) } } -void assign_blame(git_blame__scoreboard *sb, uint32_t opt) +void assign_blame(git_blame *blame, uint32_t opt) { while (true) { git_blame__entry *ent; git_blame__origin *suspect = NULL; /* Find a suspect to break down */ - for (ent = sb->ent; !suspect && ent; ent = ent->next) + for (ent = blame->ent; !suspect && ent; ent = ent->next) if (!ent->guilty) suspect = ent->suspect; if (!suspect) @@ -563,26 +564,26 @@ void assign_blame(git_blame__scoreboard *sb, uint32_t opt) /* We'll use this suspect later in the loop, so hold on to it for now. */ origin_incref(suspect); - pass_blame(sb, suspect, opt); + pass_blame(blame, suspect, opt); /* Take responsibility for the remaining entries */ - for (ent = sb->ent; ent; ent = ent->next) { + for (ent = blame->ent; ent; ent = ent->next) { if (same_suspect(ent->suspect, suspect)) { ent->guilty = 1; ent->is_boundary = !git_oid_cmp( git_commit_id(suspect->commit), - &sb->blame->options.oldest_commit); + &blame->options.oldest_commit); } } origin_decref(suspect); } } -void coalesce(git_blame__scoreboard *sb) +void coalesce(git_blame *blame) { git_blame__entry *ent, *next; - for (ent=sb->ent; ent && (next = ent->next); ent = next) { + for (ent=blame->ent; ent && (next = ent->next); ent = next) { if (same_suspect(ent->suspect, next->suspect) && ent->guilty == next->guilty && ent->s_lno + ent->num_lines == next->s_lno) diff --git a/src/blame_git.h b/src/blame_git.h index 7d229c664..28d6422b2 100644 --- a/src/blame_git.h +++ b/src/blame_git.h @@ -3,95 +3,14 @@ #define INCLUDE_blame_git__ #include "git2.h" -#include "xdiff/xinclude.h" #include "blame.h" +#include "xdiff/xinclude.h" -/* - * One blob in a commit that is being suspected - */ -typedef struct git_blame__origin { - int refcnt; - struct git_blame__origin *previous; - git_commit *commit; - git_blob *blob; - char path[]; -} git_blame__origin; - -/* - * Each group of lines is described by a git_blame__entry; it can be split - * as we pass blame to the parents. They form a linked list in the - * scoreboard structure, sorted by the target line number. - */ -typedef struct git_blame__entry { - struct git_blame__entry *prev; - struct git_blame__entry *next; - - /* the first line of this group in the final image; - * internally all line numbers are 0 based. - */ - int lno; - - /* how many lines this group has */ - int num_lines; - - /* the commit that introduced this group into the final image */ - git_blame__origin *suspect; - - /* true if the suspect is truly guilty; false while we have not - * checked if the group came from one of its parents. - */ - char guilty; - - /* true if the entry has been scanned for copies in the current parent - */ - char scanned; - - /* the line number of the first line of this group in the - * suspect's file; internally all line numbers are 0 based. - */ - int s_lno; - - /* how significant this entry is -- cached to avoid - * scanning the lines over and over. - */ - unsigned score; - - /* Whether this entry has been tracked to a boundary commit. - */ - bool is_boundary; -} git_blame__entry; - -/* - * The current state of the blame assignment. - */ -typedef struct git_blame__scoreboard { - /* the final commit (i.e. where we started digging from) */ - git_commit *final; - const char *path; - - /* - * The contents in the final image. - * Used by many functions to obtain contents of the nth line, - * indexed with scoreboard.lineno[blame_entry.lno]. - */ - const char *final_buf; - git_off_t final_buf_size; - - /* linked list of blames */ - git_blame__entry *ent; - - /* look-up a line in the final buffer */ - int num_lines; - - git_blame *blame; -} git_blame__scoreboard; - - -int get_origin(git_blame__origin **out, git_blame__scoreboard *sb, git_commit *commit, const char *path); +int get_origin(git_blame__origin **out, git_blame *sb, git_commit *commit, const char *path); int make_origin(git_blame__origin **out, git_commit *commit, const char *path); git_blame__origin *origin_incref(git_blame__origin *o); void origin_decref(git_blame__origin *o); -void assign_blame(git_blame__scoreboard *sb, uint32_t flags); -void coalesce(git_blame__scoreboard *sb); +void assign_blame(git_blame *sb, uint32_t flags); +void coalesce(git_blame *sb); #endif From f0c9d8ba1c7b9a5913248ea5228700da2eb76aaa Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Sat, 21 Sep 2013 21:19:33 -0700 Subject: [PATCH 15/35] Clean up old methods, format long lines Added back the line index. We'll need it later. --- src/blame.c | 125 +++++++++++++++++++++++++++++----------------------- src/blame.h | 1 + 2 files changed, 72 insertions(+), 54 deletions(-) diff --git a/src/blame.c b/src/blame.c index c85aa7d30..ccfbb1ee7 100644 --- a/src/blame.c +++ b/src/blame.c @@ -17,21 +17,20 @@ #include "blame_git.h" -static int hunk_search_cmp_helper(const void *key, size_t start_line, size_t num_lines) +static int hunk_byfinalline_search_cmp(const void *key, const void *entry) { uint32_t lineno = *(size_t*)key; - if (lineno < start_line) + git_blame_hunk *hunk = (git_blame_hunk*)entry; + + if (lineno < hunk->final_start_line_number) return -1; - if (lineno >= ((uint32_t)start_line + num_lines)) + if (lineno >= hunk->final_start_line_number + hunk->lines_in_hunk) return 1; return 0; } -static int hunk_byfinalline_search_cmp(const void *key, const void *entry) -{ - git_blame_hunk *hunk = (git_blame_hunk*)entry; - return hunk_search_cmp_helper(key, hunk->final_start_line_number, hunk->lines_in_hunk); -} -static int hunk_sort_cmp_by_start_line(const void *_a, const void *_b) + +static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); } +static int hunk_cmp(const void *_a, const void *_b) { git_blame_hunk *a = (git_blame_hunk*)_a, *b = (git_blame_hunk*)_b; @@ -49,7 +48,11 @@ static bool hunk_starts_at_or_after_line(git_blame_hunk *hunk, size_t line) return line <= hunk->final_start_line_number; } -static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_start, const char *path) +static git_blame_hunk* new_hunk( + uint16_t start, + uint16_t lines, + uint16_t orig_start, + const char *path) { git_blame_hunk *hunk = git__calloc(1, sizeof(git_blame_hunk)); if (!hunk) return NULL; @@ -64,7 +67,11 @@ static git_blame_hunk* new_hunk(uint16_t start, uint16_t lines, uint16_t orig_st static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) { - git_blame_hunk *newhunk = new_hunk(hunk->final_start_line_number, hunk->lines_in_hunk, hunk->orig_start_line_number, hunk->orig_path); + git_blame_hunk *newhunk = new_hunk( + hunk->final_start_line_number, + hunk->lines_in_hunk, + hunk->orig_start_line_number, + hunk->orig_path); git_oid_cpy(&newhunk->orig_commit_id, &hunk->orig_commit_id); git_oid_cpy(&newhunk->final_commit_id, &hunk->final_commit_id); return newhunk; @@ -78,7 +85,7 @@ static void free_hunk(git_blame_hunk *hunk) /* Starting with the hunk that includes start_line, shift all following hunks' * final_start_line by shift_by lines */ -static void shift_hunks_by_final(git_vector *v, size_t start_line, int shift_by) +static void shift_hunks_by(git_vector *v, size_t start_line, int shift_by) { size_t i; @@ -89,7 +96,7 @@ static void shift_hunks_by_final(git_vector *v, size_t start_line, int shift_by) } } } -static int paths_cmp(const void *a, const void *b) { return git__strcmp((char*)a, (char*)b); } + git_blame* git_blame__alloc( git_repository *repo, git_blame_options opts, @@ -100,7 +107,7 @@ git_blame* git_blame__alloc( giterr_set_oom(); return NULL; } - git_vector_init(&gbr->hunks, 8, hunk_sort_cmp_by_start_line); + git_vector_init(&gbr->hunks, 8, hunk_cmp); git_vector_init(&gbr->paths, 8, paths_cmp); gbr->repository = repo; gbr->options = opts; @@ -125,6 +132,8 @@ void git_blame_free(git_blame *blame) git__free(path); git_vector_free(&blame->paths); + git_array_clear(blame->line_index); + git__free((void*)blame->path); git_blob_free(blame->final_blob); git__free(blame); @@ -205,32 +214,39 @@ static git_blame_hunk *split_hunk_in_vector( } } - /* - * To allow quick access to the contents of nth line in the - * final image, prepare an index in the scoreboard. + * Construct a list of char indices for where lines begin + * Adapted from core git: + * https://github.com/gitster/git/blob/be5c9fb9049ed470e7005f159bb923a5f4de1309/builtin/blame.c#L1760-L1789 */ -static int prepare_lines(git_blame *blame) +static int index_blob_lines(git_blame *blame) { - const char *buf = blame->final_buf; - git_off_t len = blame->final_buf_size; - int num = 0, incomplete = 0, bol = 1; - - if (len && buf[len-1] != '\n') - incomplete++; /* incomplete line at the end */ - while (len--) { - if (bol) { - bol = 0; - } - if (*buf++ == '\n') { - num++; - bol = 1; - } - } - blame->num_lines = num + incomplete; - return blame->num_lines; + const char *buf = blame->final_buf; + git_off_t len = blame->final_buf_size; + int num = 0, incomplete = 0, bol = 1; + size_t *i; + + if (len && buf[len-1] != '\n') + incomplete++; /* incomplete line at the end */ + while (len--) { + if (bol) { + i = git_array_alloc(blame->line_index); + GITERR_CHECK_ALLOC(i); + *i = buf - blame->final_buf; + bol = 0; + } + if (*buf++ == '\n') { + num++; + bol = 1; + } + } + i = git_array_alloc(blame->line_index); + GITERR_CHECK_ALLOC(i); + *i = buf - blame->final_buf; + blame->num_lines = num + incomplete; + return blame->num_lines; } - + static git_blame_hunk* hunk_from_entry(git_blame__entry *e) { git_blame_hunk *h = new_hunk( @@ -240,29 +256,31 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e) return h; } -static int walk_and_mark(git_blame *blame) +static int blame_internal(git_blame *blame) { int error; - git_blame__entry *ent = NULL; git_blob *blob = NULL; git_blame__origin *o; - if ((error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit)) < 0 || - (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final, blame->path, GIT_OBJ_BLOB)) < 0) + if ((error = git_commit_lookup(&blame->final, blame->repository, + &blame->options.newest_commit)) + < 0 || + (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final, + blame->path, GIT_OBJ_BLOB)) + < 0) goto cleanup; blame->final_buf = git_blob_rawcontent(blob); blame->final_buf_size = git_blob_rawsize(blob); if ((error = get_origin(&o, blame, blame->final, blame->path)) < 0) goto cleanup; - ent = git__calloc(1, sizeof(*ent)); - ent->num_lines = prepare_lines(blame); + ent = git__calloc(1, sizeof(git_blame__entry)); + ent->num_lines = index_blob_lines(blame); ent->lno = blame->options.min_line - 1; ent->num_lines = ent->num_lines - blame->options.min_line + 1; - if (blame->options.max_line > 0) { + if (blame->options.max_line > 0) ent->num_lines = blame->options.max_line - blame->options.min_line + 1; - } ent->s_lno = ent->lno; ent->suspect = o; @@ -275,11 +293,10 @@ static int walk_and_mark(git_blame *blame) cleanup: for (ent = blame->ent; ent; ) { git_blame__entry *e = ent->next; - git_blame__origin *o = ent->suspect; git_vector_insert(&blame->hunks, hunk_from_entry(ent)); - origin_decref(o); + origin_decref(ent->suspect); git__free(ent); ent = e; } @@ -288,6 +305,10 @@ cleanup: return error; } +/******************************************************************************* + * File blaming + ******************************************************************************/ + static int load_blob(git_blame *blame, git_repository *repo, git_oid *commit_id, const char *path) { int retval = -1; @@ -309,10 +330,6 @@ cleanup: return retval; } -/******************************************************************************* - * File blaming - ******************************************************************************/ - int git_blame_file( git_blame **out, git_repository *repo, @@ -332,7 +349,7 @@ int git_blame_file( if ((error = load_blob(blame, repo, &normOptions.newest_commit, path)) < 0) goto on_error; - if ((error = walk_and_mark(blame)) < 0) + if ((error = blame_internal(blame)) < 0) goto on_error; *out = blame; @@ -407,10 +424,10 @@ static int buffer_line_cb( hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { /* Append to the current buffer-blame hunk */ blame->current_hunk->lines_in_hunk++; - shift_hunks_by_final(&blame->hunks, blame->current_diff_line+1, 1); + shift_hunks_by(&blame->hunks, blame->current_diff_line+1, 1); } else { /* Create a new buffer-blame hunk with this line */ - shift_hunks_by_final(&blame->hunks, blame->current_diff_line, 1); + shift_hunks_by(&blame->hunks, blame->current_diff_line, 1); blame->current_hunk = new_hunk(blame->current_diff_line, 1, 0, blame->path); git_vector_insert_sorted(&blame->hunks, blame->current_hunk, NULL); } @@ -430,7 +447,7 @@ static int buffer_line_cb( blame->current_hunk = (git_blame_hunk*)git_blame_get_hunk_byindex(blame, i); } } - shift_hunks_by_final(&blame->hunks, shift_base, -1); + shift_hunks_by(&blame->hunks, shift_base, -1); } return 0; } diff --git a/src/blame.h b/src/blame.h index d730cfb89..09d349d7b 100644 --- a/src/blame.h +++ b/src/blame.h @@ -72,6 +72,7 @@ struct git_blame { git_vector paths; git_blob *final_blob; + git_array_t(size_t) line_index; size_t current_diff_line; git_blame_hunk *current_hunk; From 77db6ff5c7b5a233b6dae24347b660f2752c1359 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Sat, 21 Sep 2013 22:01:53 -0700 Subject: [PATCH 16/35] Simplify blob loading logic --- src/blame.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/blame.c b/src/blame.c index ccfbb1ee7..ed816da52 100644 --- a/src/blame.c +++ b/src/blame.c @@ -256,6 +256,22 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e) return h; } +static int load_blob(git_blame *blame) +{ + int error; + + error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit); + if (error < 0) + goto cleanup; + error = git_object_lookup_bypath((git_object**)&blame->final_blob, + (git_object*)blame->final, blame->path, GIT_OBJ_BLOB); + if (error < 0) + goto cleanup; + +cleanup: + return error; +} + static int blame_internal(git_blame *blame) { int error; @@ -309,27 +325,6 @@ cleanup: * File blaming ******************************************************************************/ -static int load_blob(git_blame *blame, git_repository *repo, git_oid *commit_id, const char *path) -{ - int retval = -1; - git_commit *commit = NULL; - git_tree *tree = NULL; - git_tree_entry *tree_entry = NULL; - git_object *obj = NULL; - - if (((retval = git_commit_lookup(&commit, repo, commit_id)) < 0) || - ((retval = git_object_lookup_bypath(&obj, (git_object*)commit, path, GIT_OBJ_BLOB)) < 0) || - ((retval = git_object_type(obj)) != GIT_OBJ_BLOB)) - goto cleanup; - blame->final_blob = (git_blob*)obj; - -cleanup: - git_tree_entry_free(tree_entry); - git_tree_free(tree); - git_commit_free(commit); - return retval; -} - int git_blame_file( git_blame **out, git_repository *repo, @@ -346,7 +341,7 @@ int git_blame_file( blame = git_blame__alloc(repo, normOptions, path); GITERR_CHECK_ALLOC(blame); - if ((error = load_blob(blame, repo, &normOptions.newest_commit, path)) < 0) + if ((error = load_blob(blame)) < 0) goto on_error; if ((error = blame_internal(blame)) < 0) From b6f60a4d964d66b562092fdeb8f76e902bf05fa4 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Sat, 21 Sep 2013 22:02:23 -0700 Subject: [PATCH 17/35] Clean up ported code --- src/blame.c | 24 ++--- src/blame_git.c | 227 ++++++++++++++++++++++++++---------------------- src/blame_git.h | 21 +++-- 3 files changed, 144 insertions(+), 128 deletions(-) diff --git a/src/blame.c b/src/blame.c index ed816da52..0722ba005 100644 --- a/src/blame.c +++ b/src/blame.c @@ -260,6 +260,8 @@ static int load_blob(git_blame *blame) { int error; + if (blame->final_blob) return 0; + error = git_commit_lookup(&blame->final, blame->repository, &blame->options.newest_commit); if (error < 0) goto cleanup; @@ -276,20 +278,13 @@ static int blame_internal(git_blame *blame) { int error; git_blame__entry *ent = NULL; - git_blob *blob = NULL; git_blame__origin *o; - if ((error = git_commit_lookup(&blame->final, blame->repository, - &blame->options.newest_commit)) - < 0 || - (error = git_object_lookup_bypath((git_object**)&blob, (git_object*)blame->final, - blame->path, GIT_OBJ_BLOB)) - < 0) - goto cleanup; - blame->final_buf = git_blob_rawcontent(blob); - blame->final_buf_size = git_blob_rawsize(blob); - if ((error = get_origin(&o, blame, blame->final, blame->path)) < 0) + if ((error = load_blob(blame)) < 0 || + (error = git_blame__get_origin(&o, blame, blame->final, blame->path)) < 0) goto cleanup; + blame->final_buf = git_blob_rawcontent(blame->final_blob); + blame->final_buf_size = git_blob_rawsize(blame->final_blob); ent = git__calloc(1, sizeof(git_blame__entry)); ent->num_lines = index_blob_lines(blame); @@ -303,8 +298,7 @@ static int blame_internal(git_blame *blame) blame->ent = ent; blame->path = blame->path; - assign_blame(blame, blame->options.flags); - coalesce(blame); + git_blame__like_git(blame, blame->options.flags); cleanup: for (ent = blame->ent; ent; ) { @@ -312,12 +306,10 @@ cleanup: git_vector_insert(&blame->hunks, hunk_from_entry(ent)); - origin_decref(ent->suspect); - git__free(ent); + git_blame__free_entry(ent); ent = e; } - git_blob_free(blob); return error; } diff --git a/src/blame_git.c b/src/blame_git.c index 3f5fe8d3a..94e13f8ef 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -7,30 +7,32 @@ #include "blame_git.h" #include "commit.h" -#include "xdiff/xinclude.h" +#include "blob.h" /* - * Locate an existing origin or create a new one. + * Origin is refcounted and usually we keep the blob contents to be + * reused. */ -int get_origin(git_blame__origin **out, git_blame *blame, git_commit *commit, const char *path) +static git_blame__origin *origin_incref(git_blame__origin *o) { - git_blame__entry *e; - - for (e = blame->ent; e; e = e->next) { - if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) { - *out = origin_incref(e->suspect); - } - } - return make_origin(out, commit, path); + if (o) + o->refcnt++; + return o; } -/* - * Given a commit and a path in it, create a new origin structure. - * The callers that add blame to the scoreboard should use - * get_origin() to obtain shared, refcounted copy instead of calling - * this function directly. - */ -int make_origin(git_blame__origin **out, git_commit *commit, const char *path) +static void origin_decref(git_blame__origin *o) +{ + if (o && --o->refcnt <= 0) { + if (o->previous) + origin_decref(o->previous); + git_blob_free(o->blob); + git_commit_free(o->commit); + git__free(o); + } +} + +/* Given a commit and a path in it, create a new origin structure. */ +static int make_origin(git_blame__origin **out, git_commit *commit, const char *path) { int error = 0; git_blame__origin *o; @@ -50,13 +52,30 @@ int make_origin(git_blame__origin **out, git_commit *commit, const char *path) return error; } -struct blame_chunk_cb_data { +/* Locate an existing origin or create a new one. */ +int git_blame__get_origin( + git_blame__origin **out, + git_blame *blame, + git_commit *commit, + const char *path) +{ + git_blame__entry *e; + + for (e = blame->ent; e; e = e->next) { + if (e->suspect->commit == commit && !strcmp(e->suspect->path, path)) { + *out = origin_incref(e->suspect); + } + } + return make_origin(out, commit, path); +} + +typedef struct blame_chunk_cb_data { git_blame *blame; git_blame__origin *target; git_blame__origin *parent; long tlno; long plno; -}; +}blame_chunk_cb_data; static bool same_suspect(git_blame__origin *a, git_blame__origin *b) { @@ -88,10 +107,10 @@ static int find_last_in_target(git_blame *blame, git_blame__origin *target) * line plno corresponds to e's line tlno. * * <---- e -----> - * <------> - * <------------> - * <------------> - * <------------------> + * <------> (entirely within) + * <------------> (overlaps after) + * <------------> (overlaps before) + * <------------------> (overlaps both) * * Split e into potentially three parts; before this chunk, the chunk * to be blamed for the parent, and after that portion. @@ -237,7 +256,13 @@ static void decref_split(git_blame__entry *split) * Helper for blame_chunk(). blame_entry e is known to overlap with the patch * hunk; split it and pass blame to the parent. */ -static void blame_overlap(git_blame *blame, git_blame__entry *e, int tlno, int plno, int same, git_blame__origin *parent) +static void blame_overlap( + git_blame *blame, + git_blame__entry *e, + int tlno, + int plno, + int same, + git_blame__origin *parent) { git_blame__entry split[3] = {{0}}; @@ -252,7 +277,13 @@ static void blame_overlap(git_blame *blame, git_blame__entry *e, int tlno, int p * e and its parent. Find and split the overlap, and pass blame to the * overlapping part to the parent. */ -static void blame_chunk(git_blame *blame, int tlno, int plno, int same, git_blame__origin *target, git_blame__origin *parent) +static void blame_chunk( + git_blame *blame, + int tlno, + int plno, + int same, + git_blame__origin *target, + git_blame__origin *parent) { git_blame__entry *e; @@ -267,21 +298,20 @@ static void blame_chunk(git_blame *blame, int tlno, int plno, int same, git_blam } } -static void blame_chunk_cb(long start_a, long count_a, long start_b, long count_b, void *data) -{ - struct blame_chunk_cb_data *d = data; - blame_chunk(d->blame, d->tlno, d->plno, start_b, d->target, d->parent); - d->plno = start_a + count_a; - d->tlno = start_b + count_b; -} - -static int my_emit_func(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb, xdemitconf_t const *xecfg) +static int my_emit( + xdfenv_t *xe, + xdchange_t *xscr, + xdemitcb_t *ecb, + xdemitconf_t const *xecfg) { xdchange_t *xch = xscr; GIT_UNUSED(xe); GIT_UNUSED(xecfg); while (xch) { - blame_chunk_cb(xch->i1, xch->chg1, xch->i2, xch->chg2, ecb->priv); + blame_chunk_cb_data *d = ecb->priv; + blame_chunk(d->blame, d->tlno, d->plno, xch->i2, d->target, d->parent); + d->plno = xch->i1 + xch->chg1; + d->tlno = xch->i2 + xch->chg2; xch = xch->next; } return 0; @@ -311,26 +341,17 @@ static void trim_common_tail(mmfile_t *a, mmfile_t *b, long ctx) b->size -= trimmed - recovered; } -static int xdi_diff(mmfile_t *mf1, mmfile_t *mf2, xpparam_t const *xpp, xdemitconf_t const *xecfg, xdemitcb_t *xecb) -{ - mmfile_t a = *mf1; - mmfile_t b = *mf2; - - trim_common_tail(&a, &b, xecfg->ctxlen); - - return xdl_diff(&a, &b, xpp, xecfg, xecb); -} - - -static int diff_hunks(mmfile_t *file_a, mmfile_t *file_b, void *cb_data) +static int diff_hunks(mmfile_t file_a, mmfile_t file_b, void *cb_data) { xpparam_t xpp = {0}; xdemitconf_t xecfg = {0}; xdemitcb_t ecb = {0}; - xecfg.emit_func = (void(*)(void))my_emit_func; + xecfg.emit_func = (void(*)(void))my_emit; ecb.priv = cb_data; - return xdi_diff(file_a, file_b, &xpp, &xecfg, &ecb); + + trim_common_tail(&file_a, &file_b, 0); + return xdl_diff(&file_a, &file_b, &xpp, &xecfg, &ecb); } static void fill_origin_blob(git_blame__origin *o, mmfile_t *file) @@ -342,13 +363,14 @@ static void fill_origin_blob(git_blame__origin *o, mmfile_t *file) } } -static int pass_blame_to_parent(git_blame *blame, - git_blame__origin *target, - git_blame__origin *parent) +static int pass_blame_to_parent( + git_blame *blame, + git_blame__origin *target, + git_blame__origin *parent) { int last_in_target; mmfile_t file_p, file_o; - struct blame_chunk_cb_data d = { blame, target, parent, 0, 0 }; + blame_chunk_cb_data d = { blame, target, parent, 0, 0 }; last_in_target = find_last_in_target(blame, target); if (last_in_target < 0) @@ -357,7 +379,7 @@ static int pass_blame_to_parent(git_blame *blame, fill_origin_blob(parent, &file_p); fill_origin_blob(target, &file_o); - diff_hunks(&file_p, &file_o, &d); + diff_hunks(file_p, file_o, &d); /* The reset (i.e. anything after tlno) are the same as the parent */ blame_chunk(blame, d.tlno, d.plno, last_in_target, target, parent); @@ -370,7 +392,10 @@ static int paths_on_dup(void **old, void *new) git__free(new); return -1; } -static git_blame__origin* find_origin(git_blame *blame, git_commit *parent, + +static git_blame__origin* find_origin( + git_blame *blame, + git_commit *parent, git_blame__origin *origin) { git_blame__origin *porigin = NULL; @@ -395,7 +420,7 @@ static git_blame__origin* find_origin(git_blame *blame, git_commit *parent, if (!git_diff_num_deltas(difflist)) { /* No changes; copy data */ - get_origin(&porigin, blame, parent, origin->path); + git_blame__get_origin(&porigin, blame, parent, origin->path); } else { git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT; int i; @@ -418,7 +443,8 @@ static git_blame__origin* find_origin(git_blame *blame, git_commit *parent, if (git_vector_bsearch(NULL, &blame->paths, delta->new_file.path) != 0) continue; - git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), paths_on_dup); + git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), + paths_on_dup); make_origin(&porigin, parent, delta->old_file.path); } } @@ -440,8 +466,8 @@ static void pass_whole_blame(git_blame *blame, git_blame__entry *e; if (!porigin->blob) - git_object_lookup((git_object**)&porigin->blob, blame->repository, git_blob_id(origin->blob), - GIT_OBJ_BLOB); + git_object_lookup((git_object**)&porigin->blob, blame->repository, + git_blob_id(origin->blob), GIT_OBJ_BLOB); for (e=blame->ent; e; e=e->next) { if (!same_suspect(e->suspect, origin)) continue; @@ -454,25 +480,26 @@ static void pass_whole_blame(git_blame *blame, static void pass_blame(git_blame *blame, git_blame__origin *origin, uint32_t opt) { git_commit *commit = origin->commit; - int i, num_sg; + int i, num_parents; git_blame__origin *sg_buf[16]; git_blame__origin *porigin, **sg_origin = sg_buf; GIT_UNUSED(opt); - num_sg = git_commit_parentcount(commit); + num_parents = git_commit_parentcount(commit); if (!git_oid_cmp(git_commit_id(commit), &blame->options.oldest_commit)) - num_sg = 0; - if (!num_sg) { + /* Stop at oldest specified commit */ + num_parents = 0; + if (!num_parents) { git_oid_cpy(&blame->options.oldest_commit, git_commit_id(commit)); goto finish; } - else if (num_sg < (int)ARRAY_SIZE(sg_buf)) + else if (num_parents < (int)ARRAY_SIZE(sg_buf)) memset(sg_buf, 0, sizeof(sg_buf)); else - sg_origin = git__calloc(num_sg, sizeof(*sg_origin)); + sg_origin = git__calloc(num_parents, sizeof(*sg_origin)); - for (i=0; i pair), + * merge them together. */ -git_blame__origin *origin_incref(git_blame__origin *o) +static void coalesce(git_blame *blame) { - if (o) - o->refcnt++; - return o; -} + git_blame__entry *ent, *next; -void origin_decref(git_blame__origin *o) -{ - if (o && --o->refcnt <= 0) { - if (o->previous) - origin_decref(o->previous); - git_blob_free(o->blob); - git_commit_free(o->commit); - git__free(o); + for (ent=blame->ent; ent && (next = ent->next); ent = next) { + if (same_suspect(ent->suspect, next->suspect) && + ent->guilty == next->guilty && + ent->s_lno + ent->num_lines == next->s_lno) + { + ent->num_lines += next->num_lines; + ent->next = next->next; + if (ent->next) + ent->next->prev = ent; + origin_decref(next->suspect); + git__free(next); + ent->score = 0; + next = ent; /* again */ + } } } -void assign_blame(git_blame *blame, uint32_t opt) +void git_blame__like_git(git_blame *blame, uint32_t opt) { while (true) { git_blame__entry *ent; @@ -577,26 +609,13 @@ void assign_blame(git_blame *blame, uint32_t opt) } origin_decref(suspect); } + + coalesce(blame); } -void coalesce(git_blame *blame) +void git_blame__free_entry(git_blame__entry *ent) { - git_blame__entry *ent, *next; - - for (ent=blame->ent; ent && (next = ent->next); ent = next) { - if (same_suspect(ent->suspect, next->suspect) && - ent->guilty == next->guilty && - ent->s_lno + ent->num_lines == next->s_lno) - { - ent->num_lines += next->num_lines; - ent->next = next->next; - if (ent->next) - ent->next->prev = ent; - origin_decref(next->suspect); - git__free(next); - ent->score = 0; - next = ent; /* again */ - } - } + if (!ent) return; + origin_decref(ent->suspect); + git__free(ent); } - diff --git a/src/blame_git.h b/src/blame_git.h index 28d6422b2..2b780116b 100644 --- a/src/blame_git.h +++ b/src/blame_git.h @@ -1,16 +1,21 @@ - +/* + * 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_blame_git__ #define INCLUDE_blame_git__ -#include "git2.h" #include "blame.h" #include "xdiff/xinclude.h" -int get_origin(git_blame__origin **out, git_blame *sb, git_commit *commit, const char *path); -int make_origin(git_blame__origin **out, git_commit *commit, const char *path); -git_blame__origin *origin_incref(git_blame__origin *o); -void origin_decref(git_blame__origin *o); -void assign_blame(git_blame *sb, uint32_t flags); -void coalesce(git_blame *sb); +int git_blame__get_origin( + git_blame__origin **out, + git_blame *sb, + git_commit *commit, + const char *path); +void git_blame__free_entry(git_blame__entry *ent); +void git_blame__like_git(git_blame *sb, uint32_t flags); #endif From 49781a03f0ea2d33b465010c5533b6026cbc1141 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 25 Sep 2013 14:40:19 -0700 Subject: [PATCH 18/35] Blame: minor cleanup --- include/git2/object.h | 2 +- src/blame_git.c | 7 ++++--- src/blame_git.h | 1 - 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/git2/object.h b/include/git2/object.h index 7ec17cd8e..c40631fa6 100644 --- a/include/git2/object.h +++ b/include/git2/object.h @@ -36,7 +36,7 @@ GIT_BEGIN_DECL * @param repo the repository to look up the object * @param id the unique identifier for the object * @param type the type of the object - * @return a reference to the object + * @return 0 or an error code */ GIT_EXTERN(int) git_object_lookup( git_object **object, diff --git a/src/blame_git.c b/src/blame_git.c index 94e13f8ef..4df65139a 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -8,6 +8,7 @@ #include "blame_git.h" #include "commit.h" #include "blob.h" +#include "xdiff/xinclude.h" /* * Origin is refcounted and usually we keep the blob contents to be @@ -108,9 +109,9 @@ static int find_last_in_target(git_blame *blame, git_blame__origin *target) * * <---- e -----> * <------> (entirely within) - * <------------> (overlaps after) - * <------------> (overlaps before) - * <------------------> (overlaps both) + * <------------> (extends past) + * <------------> (starts before) + * <------------------> (entirely encloses) * * Split e into potentially three parts; before this chunk, the chunk * to be blamed for the parent, and after that portion. diff --git a/src/blame_git.h b/src/blame_git.h index 2b780116b..3ec2710b8 100644 --- a/src/blame_git.h +++ b/src/blame_git.h @@ -8,7 +8,6 @@ #define INCLUDE_blame_git__ #include "blame.h" -#include "xdiff/xinclude.h" int git_blame__get_origin( git_blame__origin **out, From f7db1b6f26837b44a5cb8956c23bbbdff28ba4f7 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 25 Sep 2013 14:46:59 -0700 Subject: [PATCH 19/35] Trim API, document which parts aren't done --- include/git2/blame.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/include/git2/blame.h b/include/git2/blame.h index 652bc8180..ba3580fcf 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -26,18 +26,20 @@ GIT_BEGIN_DECL typedef enum { /** Normal blame, the default */ GIT_BLAME_NORMAL = 0, - /** Track lines that have moved within a file (like `git blame -M`) */ + /** Track lines that have moved within a file (like `git blame -M`). + * NOT IMPLEMENTED. */ GIT_BLAME_TRACK_COPIES_SAME_FILE = (1<<0), - /** Track lines that have moved across files in the same commit (like `git blame -C`) */ + /** Track lines that have moved across files in the same commit (like `git blame -C`). + * NOT IMPLEMENTED. */ GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES = (1<<1), /** Track lines that have been copied from another file that exists in the - * same commit (like `git blame -CC`) */ + * same commit (like `git blame -CC`). + * NOT IMPLEMENTED. */ GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<1 | 1<<2), /** Track lines that have been copied from another file that exists in *any* - * commit (like `git blame -CCC`) */ + * commit (like `git blame -CCC`). + * NOT IMPLEMENTED. */ GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<1 | 1<<2 | 1<<3), - /** Track through file renames */ - GIT_BLAME_TRACK_FILE_RENAMES = (1<<4), } git_blame_flag_t; /** From de8fe729efd350ae5cb1ef25ffb08f92c6f706b9 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Sun, 29 Sep 2013 10:46:41 -0700 Subject: [PATCH 20/35] Fix typo --- examples/blame.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/blame.c b/examples/blame.c index 1b08ec487..6f6a2749d 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -21,7 +21,7 @@ static void usage(const char *msg, const char *arg) fprintf(stderr, "\n"); fprintf(stderr, " example: `HEAD~10..HEAD`, or `1234abcd`\n"); fprintf(stderr, " -L process only line range n-m, counting from 1\n"); - fprintf(stderr, " -M fine line moves within and across files\n"); + fprintf(stderr, " -M find line moves within and across files\n"); fprintf(stderr, " -C find line copies within and across files\n"); fprintf(stderr, "\n"); exit(1); From 370d1d166e3a77a5f21e3f7a73870fbb7def2249 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 9 Oct 2013 14:41:44 -0700 Subject: [PATCH 21/35] Un-remove init example --- examples/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Makefile b/examples/Makefile index 79c57d7d7..c9c667d16 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -3,7 +3,7 @@ CC = gcc CFLAGS = -g -I../include -I../src -Wall -Wextra -Wmissing-prototypes -Wno-missing-field-initializers LFLAGS = -L../build -lgit2 -lz -APPS = general blame showindex diff rev-list cat-file status log rev-parse +APPS = general showindex diff rev-list cat-file status log rev-parse init blame all: $(APPS) From cb45dafaf85053c6ff4f3e8ac86f8b9cbc174033 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 9 Oct 2013 16:07:17 -0700 Subject: [PATCH 22/35] Initialize threading, fix broken strncmp --- examples/blame.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/blame.c b/examples/blame.c index 6f6a2749d..88bdb7c76 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -41,6 +41,8 @@ int main(int argc, char *argv[]) git_tree_entry *entry; git_blob *blob; + git_threads_init(); + if (argc < 2) usage(NULL, NULL); path = argv[1]; @@ -51,7 +53,7 @@ int main(int argc, char *argv[]) opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; else if (!strcmp(a, "-C")) opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; - else if (!strncmp(a, "-L", 3)) { + else if (!strcmp(a, "-L")) { i++; a = argv[i]; if (i >= argc) check(-1, "Not enough arguments to -L"); check(sscanf(a, "%d,%d", &opts.min_line, &opts.max_line)-2, "-L format error"); @@ -128,4 +130,5 @@ int main(int argc, char *argv[]) git_commit_free(commit); git_blame_free(blame); git_repository_free(repo); + git_threads_shutdown(); } From 2ccc84d2efaf8adf4886cfc42d8bd16e2ba98188 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 9 Oct 2013 16:07:36 -0700 Subject: [PATCH 23/35] Allow null bytes in blob --- examples/blame.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/examples/blame.c b/examples/blame.c index 88bdb7c76..5efb19930 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -29,7 +29,7 @@ static void usage(const char *msg, const char *arg) int main(int argc, char *argv[]) { - int i; + int i, line; char *path = NULL, *a; const char *rawdata, *commitspec=NULL; git_repository *repo = NULL; @@ -99,11 +99,12 @@ int main(int argc, char *argv[]) rawdata = git_blob_rawcontent(blob); /* Produce the output */ - i = 1; - while (rawdata[0]) { - const char *eol = strchr(rawdata, '\n'); + line = 1; + i = 0; + while (i < git_blob_rawsize(blob)) { + const char *eol = strchr(rawdata+i, '\n'); char oid[10] = {0}; - const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, i); + const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); git_commit *hunkcommit; const git_signature *sig; @@ -114,13 +115,13 @@ int main(int argc, char *argv[]) printf("%s ( %-30s %3d) %.*s\n", oid, sig->name, - i, - (int)(eol-rawdata), - rawdata); + line, + (int)(eol-rawdata-i), + rawdata+i); git_commit_free(hunkcommit); - rawdata = eol+1; - i++; + i = eol - rawdata + 1; + line++; } /* Cleanup */ From 43a07b860b9c172081e3c9dee18aeead9091f2cf Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 9 Oct 2013 16:16:43 -0700 Subject: [PATCH 24/35] Simplify loading blob --- examples/blame.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/examples/blame.c b/examples/blame.c index 5efb19930..a7d0a12a5 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -32,13 +32,11 @@ int main(int argc, char *argv[]) int i, line; char *path = NULL, *a; const char *rawdata, *commitspec=NULL; + char spec[1024] = {0}; git_repository *repo = NULL; git_revspec revspec = {0}; git_blame_options opts = GIT_BLAME_OPTIONS_INIT; git_blame *blame = NULL; - git_commit *commit; - git_tree *tree; - git_tree_entry *entry; git_blob *blob; git_threads_init(); @@ -86,16 +84,19 @@ int main(int argc, char *argv[]) check(git_blame_file(&blame, repo, path, &opts), "Blame error"); /* Get the raw data for output */ - if (git_oid_iszero(&opts.newest_commit)) { + if (git_oid_iszero(&opts.newest_commit)) + strcpy(spec, "HEAD"); + else + git_oid_tostr(spec, sizeof(spec), &opts.newest_commit); + strcat(spec, ":"); + strcat(spec, path); + + { git_object *obj; - check(git_revparse_single(&obj, repo, "HEAD"), "Can't find HEAD"); - git_oid_cpy(&opts.newest_commit, git_object_id(obj)); + check(git_revparse_single(&obj, repo, spec), "Object lookup error"); + check(git_blob_lookup(&blob, repo, git_object_id(obj)), "Blob lookup error"); git_object_free(obj); } - check(git_commit_lookup(&commit, repo, &opts.newest_commit), "Commit lookup error"); - check(git_commit_tree(&tree, commit), "Commit tree lookup error"); - check(git_tree_entry_bypath(&entry, tree, path), "Tree entry lookup error"); - check(git_blob_lookup(&blob, repo, git_tree_entry_id(entry)), "Blob lookup error"); rawdata = git_blob_rawcontent(blob); /* Produce the output */ @@ -126,9 +127,6 @@ int main(int argc, char *argv[]) /* Cleanup */ git_blob_free(blob); - git_tree_entry_free(entry); - git_tree_free(tree); - git_commit_free(commit); git_blame_free(blame); git_repository_free(repo); git_threads_shutdown(); From 0a23d2058dc61a6d11e9da6aadf431d69e738601 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Wed, 9 Oct 2013 16:18:32 -0700 Subject: [PATCH 25/35] Ignore more built examples --- examples/.gitignore | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/.gitignore b/examples/.gitignore index 2b693f1ef..711493994 100644 --- a/examples/.gitignore +++ b/examples/.gitignore @@ -3,4 +3,9 @@ showindex diff rev-list blame +cat-file +init +log +rev-parse +status *.dSYM From d2e7532f620acd467ebeef0b3c4318420f445a13 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 13:56:28 -0700 Subject: [PATCH 26/35] Be more flexible with argument order and format --- examples/blame.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/examples/blame.c b/examples/blame.c index a7d0a12a5..6423b3ca6 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -17,7 +17,7 @@ static void usage(const char *msg, const char *arg) fprintf(stderr, "%s: %s\n", msg, arg); else if (msg) fprintf(stderr, "%s\n", msg); - fprintf(stderr, "usage: blame [options] []\n"); + fprintf(stderr, "usage: blame [options] [] \n"); fprintf(stderr, "\n"); fprintf(stderr, " example: `HEAD~10..HEAD`, or `1234abcd`\n"); fprintf(stderr, " -L process only line range n-m, counting from 1\n"); @@ -30,8 +30,8 @@ static void usage(const char *msg, const char *arg) int main(int argc, char *argv[]) { int i, line; - char *path = NULL, *a; - const char *rawdata, *commitspec=NULL; + const char *path = NULL, *a; + const char *rawdata, *commitspec=NULL, *bare_args[3] = {0}; char spec[1024] = {0}; git_repository *repo = NULL; git_revspec revspec = {0}; @@ -42,16 +42,24 @@ int main(int argc, char *argv[]) git_threads_init(); if (argc < 2) usage(NULL, NULL); - path = argv[1]; - for (i=2; i= 3) + usage("Invalid argument set", NULL); + bare_args[i] = a; + } + else if (!strcmp(a, "--")) + continue; + else if (!strcasecmp(a, "-M")) opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; - else if (!strcmp(a, "-C")) + else if (!strcasecmp(a, "-C")) opts.flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; - else if (!strcmp(a, "-L")) { + else if (!strcasecmp(a, "-L")) { i++; a = argv[i]; if (i >= argc) check(-1, "Not enough arguments to -L"); check(sscanf(a, "%d,%d", &opts.min_line, &opts.max_line)-2, "-L format error"); @@ -63,6 +71,21 @@ int main(int argc, char *argv[]) } } + /* Handle the bare arguments */ + if (!bare_args[0]) usage("Please specify a path", NULL); + path = bare_args[0]; + if (bare_args[1]) { + /* */ + path = bare_args[1]; + commitspec = bare_args[0]; + } + if (bare_args[2]) { + /* */ + path = bare_args[2]; + sprintf(spec, "%s..%s", bare_args[0], bare_args[1]); + commitspec = spec; + } + /* Open the repo */ check(git_repository_open_ext(&repo, ".", 0, NULL), "Couldn't open repository"); From ebd67243d5152e2c38811ba42fcdf99eecd8ef60 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 13:56:45 -0700 Subject: [PATCH 27/35] Only show lines that had blame run on them --- examples/blame.c | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/examples/blame.c b/examples/blame.c index 6423b3ca6..df687a71a 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -128,22 +128,24 @@ int main(int argc, char *argv[]) while (i < git_blob_rawsize(blob)) { const char *eol = strchr(rawdata+i, '\n'); char oid[10] = {0}; - const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); git_commit *hunkcommit; const git_signature *sig; + const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); - git_oid_tostr(oid, 10, &hunk->final_commit_id); - check(git_commit_lookup(&hunkcommit, repo, &hunk->final_commit_id), "Commit lookup error"); - sig = git_commit_author(hunkcommit); + if (hunk) { + git_oid_tostr(oid, 10, &hunk->final_commit_id); + check(git_commit_lookup(&hunkcommit, repo, &hunk->final_commit_id), "Commit lookup error"); + sig = git_commit_author(hunkcommit); - printf("%s ( %-30s %3d) %.*s\n", - oid, - sig->name, - line, - (int)(eol-rawdata-i), - rawdata+i); + printf("%s ( %-30s %3d) %.*s\n", + oid, + sig->name, + line, + (int)(eol-rawdata-i), + rawdata+i); + git_commit_free(hunkcommit); + } - git_commit_free(hunkcommit); i = eol - rawdata + 1; line++; } From c1ca2b67e1747ff4d2ba5a10ee300497d1d28dbe Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 14:30:05 -0700 Subject: [PATCH 28/35] Include signatures in blame hunks --- examples/blame.c | 10 ++++------ include/git2/blame.h | 2 ++ src/blame.c | 4 ++++ 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/examples/blame.c b/examples/blame.c index df687a71a..8ecf2970e 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -128,22 +128,20 @@ int main(int argc, char *argv[]) while (i < git_blob_rawsize(blob)) { const char *eol = strchr(rawdata+i, '\n'); char oid[10] = {0}; - git_commit *hunkcommit; - const git_signature *sig; const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); if (hunk) { + char sig[128] = {0}; + git_oid_tostr(oid, 10, &hunk->final_commit_id); - check(git_commit_lookup(&hunkcommit, repo, &hunk->final_commit_id), "Commit lookup error"); - sig = git_commit_author(hunkcommit); + snprintf(sig, 30, "%s <%s>", hunk->final_signature->name, hunk->final_signature->email); printf("%s ( %-30s %3d) %.*s\n", oid, - sig->name, + sig, line, (int)(eol-rawdata-i), rawdata+i); - git_commit_free(hunkcommit); } i = eol - rawdata + 1; diff --git a/include/git2/blame.h b/include/git2/blame.h index ba3580fcf..fe51371ab 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -103,10 +103,12 @@ typedef struct git_blame_hunk { git_oid final_commit_id; uint16_t final_start_line_number; + git_signature *final_signature; git_oid orig_commit_id; const char *orig_path; uint16_t orig_start_line_number; + git_signature *orig_signature; char boundary; } git_blame_hunk; diff --git a/src/blame.c b/src/blame.c index 0722ba005..5b0ebd4fe 100644 --- a/src/blame.c +++ b/src/blame.c @@ -12,6 +12,7 @@ #include "git2/tree.h" #include "git2/diff.h" #include "git2/blob.h" +#include "git2/signature.h" #include "util.h" #include "repository.h" #include "blame_git.h" @@ -80,6 +81,8 @@ static git_blame_hunk* dup_hunk(git_blame_hunk *hunk) static void free_hunk(git_blame_hunk *hunk) { git__free((void*)hunk->orig_path); + git_signature_free(hunk->final_signature); + git_signature_free(hunk->orig_signature); git__free(hunk); } @@ -252,6 +255,7 @@ static git_blame_hunk* hunk_from_entry(git_blame__entry *e) git_blame_hunk *h = new_hunk( e->lno+1, e->num_lines, e->s_lno+1, e->suspect->path); git_oid_cpy(&h->final_commit_id, git_commit_id(e->suspect->commit)); + h->final_signature = git_signature_dup(git_commit_author(e->suspect->commit)); h->boundary = e->is_boundary ? 1 : 0; return h; } From 607fe733700109ac171661975b60735c481ed3fc Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 14:30:31 -0700 Subject: [PATCH 29/35] Fix post-line-range iteration --- examples/blame.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/blame.c b/examples/blame.c index 8ecf2970e..450e98aee 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -29,7 +29,7 @@ static void usage(const char *msg, const char *arg) int main(int argc, char *argv[]) { - int i, line; + int i, line, break_on_null_hunk; const char *path = NULL, *a; const char *rawdata, *commitspec=NULL, *bare_args[3] = {0}; char spec[1024] = {0}; @@ -125,12 +125,16 @@ int main(int argc, char *argv[]) /* Produce the output */ line = 1; i = 0; + break_on_null_hunk = 0; while (i < git_blob_rawsize(blob)) { const char *eol = strchr(rawdata+i, '\n'); char oid[10] = {0}; const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); + if (break_on_null_hunk && !hunk) break; + if (hunk) { + break_on_null_hunk = 1; char sig[128] = {0}; git_oid_tostr(oid, 10, &hunk->final_commit_id); From 4fd847bb94b5bf8c1e4dce4e0c4798068977f916 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 14:38:15 -0700 Subject: [PATCH 30/35] Fix initializer error --- tests-clar/blame/getters.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests-clar/blame/getters.c b/tests-clar/blame/getters.c index 8d67e3e45..66eaeecf9 100644 --- a/tests-clar/blame/getters.c +++ b/tests-clar/blame/getters.c @@ -10,11 +10,11 @@ void test_blame_getters__initialize(void) git_blame_options opts = GIT_BLAME_OPTIONS_INIT; git_blame_hunk hunks[] = { - { 3, {{0}}, 1, {{0}}, "a", 0}, - { 3, {{0}}, 4, {{0}}, "b", 0}, - { 3, {{0}}, 7, {{0}}, "c", 0}, - { 3, {{0}}, 10, {{0}}, "d", 0}, - { 3, {{0}}, 13, {{0}}, "e", 0}, + { 3, {{0}}, 1, NULL, {{0}}, "a", 0}, + { 3, {{0}}, 4, NULL, {{0}}, "b", 0}, + { 3, {{0}}, 7, NULL, {{0}}, "c", 0}, + { 3, {{0}}, 10, NULL, {{0}}, "d", 0}, + { 3, {{0}}, 13, NULL, {{0}}, "e", 0}, }; g_blame = git_blame__alloc(NULL, opts, ""); From 364d800b018325914ac01cb5f1b06beb1cab528f Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Thu, 10 Oct 2013 14:53:07 -0700 Subject: [PATCH 31/35] Move flag dependencies into docs and code. --- include/git2/blame.h | 8 ++++---- src/blame.c | 8 ++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/include/git2/blame.h b/include/git2/blame.h index fe51371ab..b98c6f0d7 100644 --- a/include/git2/blame.h +++ b/include/git2/blame.h @@ -33,13 +33,13 @@ typedef enum { * NOT IMPLEMENTED. */ GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES = (1<<1), /** Track lines that have been copied from another file that exists in the - * same commit (like `git blame -CC`). + * same commit (like `git blame -CC`). Implies SAME_FILE. * NOT IMPLEMENTED. */ - GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<1 | 1<<2), + GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES = (1<<2), /** Track lines that have been copied from another file that exists in *any* - * commit (like `git blame -CCC`). + * commit (like `git blame -CCC`). Implies SAME_COMMIT_COPIES. * NOT IMPLEMENTED. */ - GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<1 | 1<<2 | 1<<3), + GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES = (1<<3), } git_blame_flag_t; /** diff --git a/src/blame.c b/src/blame.c index 5b0ebd4fe..e698d3941 100644 --- a/src/blame.c +++ b/src/blame.c @@ -184,6 +184,14 @@ static void normalize_options( /* min_line 0 really means 1 */ if (!out->min_line) out->min_line = 1; /* max_line 0 really means N, but we don't know N yet */ + + /* Fix up option implications */ + if (out->flags & GIT_BLAME_TRACK_COPIES_ANY_COMMIT_COPIES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES; + if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_COPIES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES; + if (out->flags & GIT_BLAME_TRACK_COPIES_SAME_COMMIT_MOVES) + out->flags |= GIT_BLAME_TRACK_COPIES_SAME_FILE; } static git_blame_hunk *split_hunk_in_vector( From ba02079f2d97411c3ac6f1a7c29874b0aec68b08 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 28 Oct 2013 05:01:33 -0700 Subject: [PATCH 32/35] Avoid temporary object in lookup routine --- src/object.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/object.c b/src/object.c index 53666ffe4..3fc984b45 100644 --- a/src/object.c +++ b/src/object.c @@ -374,27 +374,26 @@ int git_object_lookup_bypath( int error = -1; git_tree *tree = NULL; git_tree_entry *entry = NULL; - git_object *tmpobj = NULL; assert(out && treeish && path); - if (((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJ_TREE)) < 0) || - ((error = git_tree_entry_bypath(&entry, tree, path)) < 0) || - ((error = git_tree_entry_to_object(&tmpobj, git_object_owner(treeish), entry)) < 0)) + if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJ_TREE) < 0) || + (error = git_tree_entry_bypath(&entry, tree, path)) < 0) { goto cleanup; } - if (type == GIT_OBJ_ANY || git_object_type(tmpobj) == type) { - *out = tmpobj; - } else { + if (type != GIT_OBJ_ANY && git_tree_entry_type(entry) != type) + { giterr_set(GITERR_OBJECT, "object at path '%s' is not of the asked-for type %d", path, type); error = GIT_EINVALIDSPEC; - git_object_free(tmpobj); + goto cleanup; } + error = git_tree_entry_to_object(out, git_object_owner(treeish), entry); + cleanup: git_tree_entry_free(entry); git_tree_free(tree); From 7f6db0ad121a3fdfdb9249c71b6650f771f4f6b3 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 28 Oct 2013 05:19:30 -0700 Subject: [PATCH 33/35] Mmmm, GIT_FLEX_ARRAY --- src/blame.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/blame.h b/src/blame.h index 09d349d7b..b50021a24 100644 --- a/src/blame.h +++ b/src/blame.h @@ -16,7 +16,7 @@ typedef struct git_blame__origin { struct git_blame__origin *previous; git_commit *commit; git_blob *blob; - char path[]; + char path[GIT_FLEX_ARRAY]; } git_blame__origin; /* From a7d28f40a2a01382b76c55ca0a0672c177adaf69 Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 28 Oct 2013 05:22:37 -0700 Subject: [PATCH 34/35] :heart: bool --- src/blame.h | 4 ++-- src/blame_git.c | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/blame.h b/src/blame.h index b50021a24..637e43985 100644 --- a/src/blame.h +++ b/src/blame.h @@ -42,11 +42,11 @@ typedef struct git_blame__entry { /* true if the suspect is truly guilty; false while we have not * checked if the group came from one of its parents. */ - char guilty; + bool guilty; /* true if the entry has been scanned for copies in the current parent */ - char scanned; + bool scanned; /* the line number of the first line of this group in the * suspect's file; internally all line numbers are 0 based. diff --git a/src/blame_git.c b/src/blame_git.c index 4df65139a..2b02443de 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -602,7 +602,7 @@ void git_blame__like_git(git_blame *blame, uint32_t opt) /* Take responsibility for the remaining entries */ for (ent = blame->ent; ent; ent = ent->next) { if (same_suspect(ent->suspect, suspect)) { - ent->guilty = 1; + ent->guilty = true; ent->is_boundary = !git_oid_cmp( git_commit_id(suspect->commit), &blame->options.oldest_commit); From 7dcb1c452582d2a83ca1ad8858cb95ab20d6e13d Mon Sep 17 00:00:00 2001 From: Ben Straub Date: Mon, 28 Oct 2013 11:21:23 -0700 Subject: [PATCH 35/35] Adjust for diff API changes --- src/blame.c | 23 ++++++++--------------- src/blame_git.c | 20 ++++++++++---------- 2 files changed, 18 insertions(+), 25 deletions(-) diff --git a/src/blame.c b/src/blame.c index e698d3941..192e296d8 100644 --- a/src/blame.c +++ b/src/blame.c @@ -370,19 +370,15 @@ static bool hunk_is_bufferblame(git_blame_hunk *hunk) static int buffer_hunk_cb( const git_diff_delta *delta, - const git_diff_range *range, - const char *header, - size_t header_len, + const git_diff_hunk *hunk, void *payload) { git_blame *blame = (git_blame*)payload; size_t wedge_line; GIT_UNUSED(delta); - GIT_UNUSED(header); - GIT_UNUSED(header_len); - wedge_line = (range->old_lines == 0) ? range->new_start : range->old_start; + wedge_line = (hunk->old_lines == 0) ? hunk->new_start : hunk->old_start; blame->current_diff_line = wedge_line; /* If this hunk doesn't start between existing hunks, split a hunk up so it does */ @@ -398,18 +394,15 @@ static int buffer_hunk_cb( static int ptrs_equal_cmp(const void *a, const void *b) { return ab ? 1 : 0; } static int buffer_line_cb( const git_diff_delta *delta, - const git_diff_range *range, - char line_origin, - const char *content, - size_t content_len, + const git_diff_hunk *hunk, + const git_diff_line *line, void *payload) { git_blame *blame = (git_blame*)payload; GIT_UNUSED(delta); - GIT_UNUSED(range); - GIT_UNUSED(content); - GIT_UNUSED(content_len); + GIT_UNUSED(hunk); + GIT_UNUSED(line); #ifdef DO_DEBUG { @@ -418,7 +411,7 @@ static int buffer_line_cb( } #endif - if (line_origin == GIT_DIFF_LINE_ADDITION) { + if (line->origin == GIT_DIFF_LINE_ADDITION) { if (hunk_is_bufferblame(blame->current_hunk) && hunk_ends_at_or_before_line(blame->current_hunk, blame->current_diff_line)) { /* Append to the current buffer-blame hunk */ @@ -433,7 +426,7 @@ static int buffer_line_cb( blame->current_diff_line++; } - if (line_origin == GIT_DIFF_LINE_DELETION) { + if (line->origin == GIT_DIFF_LINE_DELETION) { /* Trim the line from the current hunk; remove it if it's now empty */ size_t shift_base = blame->current_diff_line + blame->current_hunk->lines_in_hunk+1; diff --git a/src/blame_git.c b/src/blame_git.c index 2b02443de..84ffa29f0 100644 --- a/src/blame_git.c +++ b/src/blame_git.c @@ -400,7 +400,7 @@ static git_blame__origin* find_origin( git_blame__origin *origin) { git_blame__origin *porigin = NULL; - git_diff_list *difflist = NULL; + git_diff *difflist = NULL; git_diff_options diffopts = GIT_DIFF_OPTIONS_INIT; git_tree *otree=NULL, *ptree=NULL; @@ -427,7 +427,7 @@ static git_blame__origin* find_origin( int i; /* Generate a full diff between the two trees */ - git_diff_list_free(difflist); + git_diff_free(difflist); diffopts.pathspec.count = 0; if (0 != git_diff_tree_to_tree(&difflist, blame->repository, ptree, otree, &diffopts)) goto cleanup; @@ -439,19 +439,19 @@ static git_blame__origin* find_origin( /* Find one that matches */ for (i=0; i<(int)git_diff_num_deltas(difflist); i++) { - const git_diff_delta *delta; - git_diff_get_patch(NULL, &delta, difflist, i); - if (git_vector_bsearch(NULL, &blame->paths, delta->new_file.path) != 0) - continue; + const git_diff_delta *delta = git_diff_get_delta(difflist, i); - git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), - paths_on_dup); - make_origin(&porigin, parent, delta->old_file.path); + if (!git_vector_bsearch(NULL, &blame->paths, delta->new_file.path)) + { + git_vector_insert_sorted(&blame->paths, (void*)git__strdup(delta->old_file.path), + paths_on_dup); + make_origin(&porigin, parent, delta->old_file.path); + } } } cleanup: - git_diff_list_free(difflist); + git_diff_free(difflist); git_tree_free(otree); git_tree_free(ptree); return porigin;