stagit

static git page generator  https://git.ce9e.org
git clone https://git.ce9e.org/stagit.git

commit
7c8f970c172342b22788097a1437bb1139fd23f0
parent
63e04b9f58b6030864d1c9746da3a24f10d35fae
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-12-05 07:30
refactor process_files

Diffstat

M src/stagit.c.in 74 ++++++++++++++++++++-----------------------------------------

1 files changed, 24 insertions, 50 deletions


diff --git a/src/stagit.c.in b/src/stagit.c.in

@@ -161,6 +161,18 @@ err:
  161   161 	return NULL;
  162   162 }
  163   163 
   -1   164 int git_get_tree(git_tree **tree, git_repository *repo, const char *name) {
   -1   165 	git_reference *branch = NULL;
   -1   166 	int ret;
   -1   167 
   -1   168 	if (git_reference_lookup(&branch, repo, name) < 0) {
   -1   169 		return -1;
   -1   170 	}
   -1   171 	ret = git_reference_peel((git_object**)tree, branch, GIT_OBJECT_TREE);
   -1   172 	git_reference_free(branch);
   -1   173 	return ret;
   -1   174 }
   -1   175 
  164   176 int git_commit_get_diff(git_diff **diff, git_commit *commit) {
  165   177 	git_commit *parent;
  166   178 	git_tree *commit_tree;
@@ -654,49 +666,24 @@ void process_readme(FILE *fp) {
  654   666 	}
  655   667 }
  656   668 
  657    -1 int _process_files(FILE *fp, git_tree *tree, const char *path) {
  658    -1 	const git_tree_entry *entry = NULL;
   -1   669 int process_file(const char *root, const git_tree_entry *entry, void *payload) {
  659   670 	git_object *obj = NULL;
  660   671 	git_off_t filesize;
  661   672 	const char *entryname;
  662   673 	char filepath[PATH_MAX], entrypath[PATH_MAX];
  663    -1 	size_t count, i;
  664    -1 	int ret;
   -1   674 	FILE *fp = payload;
  665   675 
  666    -1 	count = git_tree_entrycount(tree);
  667    -1 	for (i = 0; i < count; i++) {
  668    -1 		if (
  669    -1 			!(entry = git_tree_entry_byindex(tree, i))
  670    -1 			|| !(entryname = git_tree_entry_name(entry))
  671    -1 		)
  672    -1 			return -1;
  673    -1 		joinpath(entrypath, sizeof(entrypath), path, entryname);
  674    -1 		joinpath(filepath, sizeof(filepath), "blob", entrypath);
  675    -1 
  676    -1 		if (!git_tree_entry_to_object(&obj, repo, entry)) {
  677    -1 			switch (git_object_type(obj)) {
  678    -1 			case GIT_OBJ_BLOB:
  679    -1 				break;
  680    -1 			case GIT_OBJ_TREE:
  681    -1 				/* NOTE: recurses */
  682    -1 				ret = _process_files(fp, (git_tree *)obj, entrypath);
  683    -1 				git_object_free(obj);
  684    -1 				if (ret) {
  685    -1 					return ret;
  686    -1 				}
  687    -1 				continue;
  688    -1 			default:
  689    -1 				git_object_free(obj);
  690    -1 				continue;
  691    -1 			}
   -1   676 	entryname = git_tree_entry_name(entry);
   -1   677 	joinpath(entrypath, sizeof(entrypath), root, entryname);
   -1   678 	joinpath(filepath, sizeof(filepath), "blob", entrypath);
  692   679 
   -1   680 	if (!git_tree_entry_to_object(&obj, repo, entry)) {
   -1   681 		if (git_object_type(obj) == GIT_OBJ_BLOB) {
  693   682 			copy_blob(obj, filepath);
  694    -1 
  695   683 			filesize = git_blob_rawsize((git_blob *)obj);
  696   684 			write_files_line(fp, entrypath, filepath, filesize);
  697    -1 
  698    -1 			git_object_free(obj);
  699   685 		}
   -1   686 		git_object_free(obj);
  700   687 	}
  701   688 
  702   689 	return 0;
@@ -704,26 +691,13 @@ int _process_files(FILE *fp, git_tree *tree, const char *path) {
  704   691 
  705   692 int process_files(FILE *fp) {
  706   693 	git_tree *tree = NULL;
  707    -1 	git_commit *commit = NULL;
  708    -1 	git_object *obj = NULL;
  709    -1 	const git_oid *head = NULL;
  710    -1 	int ret = -1;
  711    -1 
  712    -1 	/* find HEAD */
  713    -1 	if (!git_revparse_single(&obj, repo, "HEAD")) {
  714    -1 		head = git_object_id(obj);
  715    -1 	}
  716    -1 	git_object_free(obj);
  717    -1 	if (!head) {
  718    -1 		fprintf(stderr, "no HEAD found\n");
  719    -1 		return 1;
  720    -1 	}
   -1   694 	int ret;
  721   695 
  722    -1 	if (!git_commit_lookup(&commit, repo, head) && !git_commit_tree(&tree, commit)) {
  723    -1 		ret = _process_files(fp, tree, "");
   -1   696 	if (git_get_tree(&tree, repo, "HEAD") < 0) {
   -1   697 		return -1;
  724   698 	}
  725   699 
  726    -1 	git_commit_free(commit);
   -1   700 	ret = git_tree_walk(tree, GIT_TREEWALK_POST, process_file, fp);
  727   701 	git_tree_free(tree);
  728   702 
  729   703 	return ret;