Skip to contentSkip to conversation

branch: support sorting branches by mtime

PATCH In review Nguyễn Thái Ngọc Duy 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 branch: support sorting branches by mtime

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(-)
builtin/branch.c +28 -4
index 87976f0..fdc2714 100644
@@ -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))
Signature · 1 line
1.7.3.2.210.g045198
#1 lore raw
Quoted 5 lines · Nguyễn Thái Ngọc Duy

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.

I do something similar, but rather than using mtime, I sort by authordate, which for-each-ref supports already. In fact, the script I use is:

-- >8 --
#!/bin/sh

format='%(refname:short)'
sortby=refname
set -- $(getopt vt "$@")
while test $# -gt 0; do

	case "$1" in
	-v) format='%(refname:short) %(subject)' ;;
	-t) sortby=-authordate ;;
	--) break ;;
	 *) echo >&2 "unknown option: $1"; exit 100 ;;
	esac
	shift

done

git for-each-ref --sort=$sortby --format="$format" refs/heads | egrep '^[a-z]{2,3}/' |
perl -pe '
s/(\S+) /$1 . " " x (22 - length($1))/e; # line up subjects s/^(.{79}).*/$1/; # truncate long lines
'
-- 8< --

It might be nice if git-branch supported the same sortby that for-each-ref does.

-Peff

#2 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 16 lines · Nguyễn Thái Ngọc Duy

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(-)
builtin/branch.c +0 -0
index 87976f0..fdc2714 100644

builtin/branch.c struct ref_item { · line 237

Quoted 6 lines
builtin/branch.c +1 -0
@@ -232,6 +232,7 @@ struct ref_item {
 	char *name;
 	char *dest;
 	unsigned int kind, len;
+	unsigned long mtime;
 	struct commit *commit;
 };

It's possible to have 32-bit long and 64-bit time_t.

Josh