List for cgit developers and users
 help / color / mirror / Atom feed
* [PATCH] If no HEAD ref is found, use the newest branch
@ 2012-01-04 12:19 john
  0 siblings, 0 replies; only message in thread
From: john @ 2012-01-04 12:19 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-01-04 12:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-04 12:19 [PATCH] If no HEAD ref is found, use the newest branch john

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).