[PATCH] If no HEAD ref is found, use the newest branch

John Keeping john at metanate.com
Wed Jan 4 13:19:03 CET 2012


This changes the behaviour for selecting the log to be shown on the
summary screen so that the newest branch is shown, rather than the first
alphabetically, when the HEAD ref is not present in the repository.

Signed-off-by: John Keeping <john at metanate.com>
---
 cgit.c |   38 ++++++++++++++++++++++++++++++++------
 1 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/cgit.c b/cgit.c
index b7807ad..4f3de4e 100644
--- a/cgit.c
+++ b/cgit.c
@@ -381,7 +381,6 @@ static void prepare_context(struct cgit_context *ctx)
 
 struct refmatch {
 	char *req_ref;
-	char *first_ref;
 	int match;
 };
 
@@ -393,26 +392,53 @@ int find_current_ref(const char *refname, const unsigned char *sha1,
 	info = (struct refmatch *)cb_data;
 	if (!strcmp(refname, info->req_ref))
 		info->match = 1;
-	if (!info->first_ref)
-		info->first_ref = xstrdup(refname);
 	return info->match;
 }
 
+struct newestref {
+	char *ref;
+	unsigned long date;
+};
+
+static int find_newest_ref(const char *refname, const unsigned char *sha1,
+			   int flags, void *cb_data)
+{
+	struct newestref *newest;
+	struct object *object;
+	unsigned long date;
+
+	newest = (struct newestref *)cb_data;
+	object = parse_object(sha1);
+	if (object->type == OBJ_COMMIT) {
+		date = ((struct commit *)object)->date;
+		if (date > newest->date) {
+			newest->ref = xstrdup(refname);
+			newest->date = date;
+		}
+	}
+	return 0;
+}
+
 char *find_default_branch(struct cgit_repo *repo)
 {
 	struct refmatch info;
+	struct newestref newest;
 	char *ref;
 
+	newest.ref = NULL;
 	info.req_ref = repo->defbranch;
-	info.first_ref = NULL;
 	info.match = 0;
 	for_each_branch_ref(find_current_ref, &info);
 	if (info.match)
 		ref = info.req_ref;
-	else
-		ref = info.first_ref;
+	else {
+		newest.date = 0;
+		for_each_branch_ref(find_newest_ref, &newest);
+		ref = newest.ref;
+	}
 	if (ref)
 		ref = xstrdup(ref);
+	free(newest.ref);
 	return ref;
 }
 
-- 
1.7.8.2





More information about the CGit mailing list