diff options
author | Russell Belfer <rb@github.com> | 2013-01-30 11:10:39 -0800 |
---|---|---|
committer | Russell Belfer <rb@github.com> | 2013-01-30 11:10:39 -0800 |
commit | f1e2735c74d03105592a282e2c32f45033db0e8d (patch) | |
tree | 6bb4ac08b1b90022c61339db67fd5949dc6fced8 /src/diff_output.c | |
parent | d2041216578de4e6fbb466d439fac5de7f35caf3 (diff) | |
download | libgit2-f1e2735c74d03105592a282e2c32f45033db0e8d.tar.gz |
Add helper for diff line stats
This adds a `git_diff_patch_line_stats()` API that gets the total
number of adds, deletes, and context lines in a patch. This will
make it a little easier to emulate `git diff --stat` and the like.
Right now, this relies on generating the `git_diff_patch` object,
which is a pretty heavyweight way to get stat information. At
some future point, it would probably be nice to be able to get
this information without allocating the entire `git_diff_patch`,
but that's a much larger project.
Diffstat (limited to 'src/diff_output.c')
-rw-r--r-- | src/diff_output.c | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/diff_output.c b/src/diff_output.c index 8a7a7a2a1..4f1064b7f 100644 --- a/src/diff_output.c +++ b/src/diff_output.c @@ -1501,6 +1501,39 @@ size_t git_diff_patch_num_hunks(git_diff_patch *patch) return patch->hunks_size; } +int git_diff_patch_line_stats( + size_t *total_ctxt, + size_t *total_adds, + size_t *total_dels, + const git_diff_patch *patch) +{ + size_t totals[3], idx; + + memset(totals, 0, sizeof(totals)); + + for (idx = 0; idx < patch->lines_size; ++idx) { + switch (patch->lines[idx].origin) { + case GIT_DIFF_LINE_CONTEXT: totals[0]++; break; + case GIT_DIFF_LINE_ADDITION: totals[1]++; break; + case GIT_DIFF_LINE_DELETION: totals[2]++; break; + default: + /* diff --stat and --numstat don't count EOFNL marks because + * they will always be paired with a ADDITION or DELETION line. + */ + break; + } + } + + if (total_ctxt) + *total_ctxt = totals[0]; + if (total_adds) + *total_adds = totals[1]; + if (total_dels) + *total_dels = totals[2]; + + return 0; +} + int git_diff_patch_get_hunk( const git_diff_range **range, const char **header, @@ -1706,4 +1739,3 @@ int git_diff__paired_foreach( return 0; } - |