summaryrefslogtreecommitdiff
path: root/blame.c
diff options
context:
space:
mode:
authorJeff Smith <whydoubt@gmail.com>2017-05-24 00:15:33 -0500
committerJunio C Hamano <gitster@pobox.com>2017-05-25 13:08:22 +0900
commitf5dd754c36f7d9cc7dd4c3f1a22e0e4a557b2021 (patch)
tree1aaed09c905f168d3c02eda686df00f1f6b5be93 /blame.c
parentdc076ae5d9fe8c2fbf238b265c1bc1f6f089fd16 (diff)
downloadgit-f5dd754c36f7d9cc7dd4c3f1a22e0e4a557b2021.tar.gz
blame: move origin-related methods to libgit
Signed-off-by: Jeff Smith <whydoubt@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'blame.c')
-rw-r--r--blame.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/blame.c b/blame.c
new file mode 100644
index 0000000000..4855d6d6e9
--- /dev/null
+++ b/blame.c
@@ -0,0 +1,62 @@
+#include "blame.h"
+
+void blame_origin_decref(struct blame_origin *o)
+{
+ if (o && --o->refcnt <= 0) {
+ struct blame_origin *p, *l = NULL;
+ if (o->previous)
+ blame_origin_decref(o->previous);
+ free(o->file.ptr);
+ /* Should be present exactly once in commit chain */
+ for (p = o->commit->util; p; l = p, p = p->next) {
+ if (p == o) {
+ if (l)
+ l->next = p->next;
+ else
+ o->commit->util = p->next;
+ free(o);
+ return;
+ }
+ }
+ die("internal error in blame_origin_decref");
+ }
+}
+
+/*
+ * 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.
+ */
+struct blame_origin *make_origin(struct commit *commit, const char *path)
+{
+ struct blame_origin *o;
+ FLEX_ALLOC_STR(o, path, path);
+ o->commit = commit;
+ o->refcnt = 1;
+ o->next = commit->util;
+ commit->util = o;
+ return o;
+}
+
+/*
+ * Locate an existing origin or create a new one.
+ * This moves the origin to front position in the commit util list.
+ */
+struct blame_origin *get_origin(struct commit *commit, const char *path)
+{
+ struct blame_origin *o, *l;
+
+ for (o = commit->util, l = NULL; o; l = o, o = o->next) {
+ if (!strcmp(o->path, path)) {
+ /* bump to front */
+ if (l) {
+ l->next = o->next;
+ o->next = commit->util;
+ commit->util = o;
+ }
+ return blame_origin_incref(o);
+ }
+ }
+ return make_origin(commit, path);
+}