git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] branch: support sorting branches by mtime
@ 2010-11-18 14:22 Nguyễn Thái Ngọc Duy
  2010-11-18 16:13 ` Jeff King
  2010-11-20  4:22 ` Joshua Juran
  0 siblings, 2 replies; 3+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2010-11-18 14:22 UTC (permalink / raw)
  To: git; +Cc: 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(-)

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

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] branch: support sorting branches by mtime
  2010-11-18 14:22 [PATCH] branch: support sorting branches by mtime Nguyễn Thái Ngọc Duy
@ 2010-11-18 16:13 ` Jeff King
  2010-11-20  4:22 ` Joshua Juran
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff King @ 2010-11-18 16:13 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

On Thu, Nov 18, 2010 at 09:22:06PM +0700, Nguyễn Thái Ngọc Duy wrote:

> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] branch: support sorting branches by mtime
  2010-11-18 14:22 [PATCH] branch: support sorting branches by mtime Nguyễn Thái Ngọc Duy
  2010-11-18 16:13 ` Jeff King
@ 2010-11-20  4:22 ` Joshua Juran
  1 sibling, 0 replies; 3+ messages in thread
From: Joshua Juran @ 2010-11-20  4:22 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git

On Nov 18, 2010, at 6:22 AM, Nguyễn Thái Ngọc Duy wrote:

> 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;
> };

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

Josh

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-11-20  4:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-11-18 14:22 [PATCH] branch: support sorting branches by mtime Nguyễn Thái Ngọc Duy
2010-11-18 16:13 ` Jeff King
2010-11-20  4:22 ` Joshua Juran

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).