Commit 58379602a09f7b61fab5d50b3c69f713a99af8d1

Stefan Sperling 2018-04-02T11:40:53

clean up commit list in print_commits() after any error in the loop

diff --git a/got/got.c b/got/got.c
index 598997c..bbfab87 100644
--- a/got/got.c
+++ b/got/got.c
@@ -367,16 +367,8 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
 
 		entry = TAILQ_FIRST(&commits);
 		err = print_commit(entry->commit, entry->id, repo, show_patch);
-		if (err) {
-			while (!TAILQ_EMPTY(&commits)) {
-				entry = TAILQ_FIRST(&commits);
-				TAILQ_REMOVE(&commits, entry, entry);
-				got_object_commit_close(entry->commit);
-				free(entry->id);
-				free(entry);
-			}
+		if (err)
 			break;
-		}
 
 		if (limit && --limit == 0)
 			break;
@@ -421,6 +413,16 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
 		free(entry);
 	}
 
+	if (err) {
+		while (!TAILQ_EMPTY(&commits)) {
+			entry = TAILQ_FIRST(&commits);
+			TAILQ_REMOVE(&commits, entry, entry);
+			got_object_commit_close(entry->commit);
+			free(entry->id);
+			free(entry);
+		}
+	}
+
 	return err;
 }