Commit e2e879a0865a8984188e95cb05b86191c8cfd59b

Stefan Sperling 2019-03-11T16:58:21

introduce got_ref_list_free() and use it in got and tog

diff --git a/got/got.c b/got/got.c
index 35d5afb..0e10c1f 100644
--- a/got/got.c
+++ b/got/got.c
@@ -945,6 +945,7 @@ done:
 		if (error == NULL)
 			error = repo_error;
 	}
+	got_ref_list_free(&refs);
 	return error;
 }
 
@@ -1704,6 +1705,7 @@ list_refs(struct got_repository *repo)
 		free(refstr);
 	}
 
+	got_ref_list_free(&refs);
 	return NULL;
 }
 
diff --git a/include/got_reference.h b/include/got_reference.h
index f5c67f2..239c9df 100644
--- a/include/got_reference.h
+++ b/include/got_reference.h
@@ -74,6 +74,9 @@ SIMPLEQ_HEAD(got_reflist_head, got_reflist_entry);
 const struct got_error *got_ref_list(struct got_reflist_head *,
     struct got_repository *);
 
+/* Free all references on a ref list. */
+void got_ref_list_free(struct got_reflist_head *);
+
 /* Write a reference to its on-disk path in the repository. */
 const struct got_error *got_ref_write(struct got_reference *,
     struct got_repository *);
diff --git a/lib/reference.c b/lib/reference.c
index ae3b86b..f66bd47 100644
--- a/lib/reference.c
+++ b/lib/reference.c
@@ -702,6 +702,21 @@ done:
 	return err;
 }
 
+void
+got_ref_list_free(struct got_reflist_head *refs)
+{
+	struct got_reflist_entry *re;
+
+	while (!SIMPLEQ_EMPTY(refs)) {
+		re = SIMPLEQ_FIRST(refs);
+		SIMPLEQ_REMOVE_HEAD(refs, entry);
+		got_ref_close(re->ref);
+		free(re->id);
+		free(re);
+	}
+
+}
+
 const struct got_error *
 got_ref_write(struct got_reference *ref, struct got_repository *repo)
 {
diff --git a/tog/tog.c b/tog/tog.c
index 421f8fe..9086729 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -1923,6 +1923,7 @@ done:
 		got_repo_close(repo);
 	if (worktree)
 		got_worktree_close(worktree);
+	got_ref_list_free(&refs);
 	return error;
 }
 
@@ -2517,6 +2518,7 @@ cmd_diff(int argc, char *argv[])
 	error = view_loop(view);
 done:
 	got_repo_close(repo);
+	got_ref_list_free(&refs);
 	return error;
 }
 
@@ -3288,6 +3290,7 @@ done:
 		got_worktree_close(worktree);
 	if (repo)
 		got_repo_close(repo);
+	got_ref_list_free(&refs);
 	return error;
 }
 
@@ -3938,6 +3941,7 @@ done:
 		got_object_tree_close(tree);
 	if (repo)
 		got_repo_close(repo);
+	got_ref_list_free(&refs);
 	return error;
 }