summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-01-30 11:10:39 -0800
committerRussell Belfer <rb@github.com>2013-01-30 11:10:39 -0800
commitf1e2735c74d03105592a282e2c32f45033db0e8d (patch)
tree6bb4ac08b1b90022c61339db67fd5949dc6fced8 /src
parentd2041216578de4e6fbb466d439fac5de7f35caf3 (diff)
downloadlibgit2-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')
-rw-r--r--src/buffer.h7
-rw-r--r--src/diff_output.c34
2 files changed, 40 insertions, 1 deletions
diff --git a/src/buffer.h b/src/buffer.h
index 4efd240b5..6e73895b4 100644
--- a/src/buffer.h
+++ b/src/buffer.h
@@ -134,6 +134,13 @@ GIT_INLINE(ssize_t) git_buf_rfind(git_buf *buf, char ch)
return idx;
}
+GIT_INLINE(ssize_t) git_buf_find(git_buf *buf, char ch)
+{
+ size_t idx = 0;
+ while (idx < buf->size && buf->ptr[idx] != ch) idx++;
+ return (idx == buf->size) ? -1 : (ssize_t)idx;
+}
+
/* Remove whitespace from the end of the buffer */
void git_buf_rtrim(git_buf *buf);
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;
}
-