[PATCH] branch: support sorting branches by mtime

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I lost in my branches again while looking for a topic that I feel like
working on tonight. Too old branches are out of question.

This throwaway patch demonstrates the idea. I should have looked into
reflog for this kind of information, but my laziness won over me.

OK Git's fun time is over..
---
 builtin/branch.c |   32 ++++++++++++++++++++++++++++----
 1 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/builtin/branch.c b/builtin/branch.c
index 87976f0..fdc2714 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -232,6 +232,7 @@ struct ref_item {
 	char *name;
 	char *dest;
 	unsigned int kind, len;
+	unsigned long mtime;
 	struct commit *commit;
 };
 
@@ -259,6 +260,7 @@ static char *resolve_symref(const char *src, const char *prefix)
 
 struct append_ref_cb {
 	struct ref_list *ref_list;
+	int collect_mtime;
 	int ret;
 };
 
@@ -270,6 +272,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	struct commit *commit;
 	int kind, i;
 	const char *prefix, *orig_refname = refname;
+	struct stat st;
 
 	static struct {
 		int kind;
@@ -334,6 +337,13 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if (newitem->len > ref_list->maxwidth)
 		ref_list->maxwidth = newitem->len;
 
+	kind = kind == REF_LOCAL_BRANCH ? 0 : 1;
+	if (stat(git_path("logs/%s%s", ref_kind[kind].prefix, refname), &st)) {
+		newitem->mtime = 0;
+	}
+	else
+		newitem->mtime = st.st_mtime;
+
 	return 0;
 }
 
@@ -358,6 +368,16 @@ static int ref_cmp(const void *r1, const void *r2)
 	return strcmp(c1->name, c2->name);
 }
 
+static int ref_mtime_cmp(const void *r1, const void *r2)
+{
+	struct ref_item *c1 = (struct ref_item *)(r1);
+	struct ref_item *c2 = (struct ref_item *)(r2);
+
+	if (c1->kind != c2->kind)
+		return c1->kind - c2->kind;
+	return c1->mtime - c2->mtime;
+}
+
 static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
 		int show_upstream_ref)
 {
@@ -492,7 +512,8 @@ static void show_detached(struct ref_list *ref_list)
 	}
 }
 
-static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
+static int print_ref_list(int kinds, int detached, int verbose, int abbrev,
+			  int sort_by_mtime, struct commit_list *with_commit)
 {
 	int i;
 	struct append_ref_cb cb;
@@ -507,6 +528,7 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
 		init_revisions(&ref_list.revs, NULL);
 	cb.ref_list = &ref_list;
 	cb.ret = 0;
+	cb.collect_mtime = sort_by_mtime;
 	for_each_rawref(append_ref, &cb);
 	if (merge_filter != NO_FILTER) {
 		struct commit *filter;
@@ -520,7 +542,8 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
 			ref_list.maxwidth = calc_maxwidth(&ref_list);
 	}
 
-	qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
+	qsort(ref_list.list, ref_list.index, sizeof(struct ref_item),
+	      sort_by_mtime ? ref_mtime_cmp : ref_cmp);
 
 	detached = (detached && (kinds & REF_LOCAL_BRANCH));
 	if (detached)
@@ -614,7 +637,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 {
 	int delete = 0, rename = 0, force_create = 0;
 	int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
-	int reflog = 0;
+	int reflog = 0, sort_by_mtime = 0;
 	enum branch_track track;
 	int kinds = REF_LOCAL_BRANCH;
 	struct commit_list *with_commit = NULL;
@@ -664,6 +687,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 			PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
 			opt_parse_merge_filter, (intptr_t) "HEAD",
 		},
+		OPT_BOOLEAN(0, "mtime", &sort_by_mtime, "sort branch list by modification time"),
 		OPT_END(),
 	};
 
@@ -695,7 +719,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
 	else if (argc == 0)
-		return print_ref_list(kinds, detached, verbose, abbrev, with_commit);
+		return print_ref_list(kinds, detached, verbose, abbrev, sort_by_mtime, with_commit);
 	else if (rename && (argc == 1))
 		rename_branch(head, argv[0], rename > 1);
 	else if (rename && (argc == 2))
-- 
1.7.3.2.210.g045198

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]