Skip to contentSkip to conversation

branch: borrow --sort and --count from for-each-ref

PATCH Superseded Nguyễn Thái Ngọc Duy 2 participants 4 messages Jun 15, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 branch: borrow --sort and --count from for-each-ref

---
Some time ago, I posted a patch that added date sort to git-branch and Peff pointed me to for-each-ref. I did not look at it closely. Now it does not seem hard to lend some code from for-each-ref to git-branch. I can list 10 most recently touched branches with

  git branch --sort=-committerdate -v --count=10

kind of cool. I don't think adding --format is necessary because git-branch already has its own formatting.

 Documentation/git-branch.txt |   13 +++++++++
 Makefile                     |    1 +
 builtin/branch.c             |   61 +++++++++++++++++++++++++++++++++--------
 builtin/for-each-ref.c       |   33 +++-------------------
 builtin/for-each-ref.h       |   32 ++++++++++++++++++++++
 5 files changed, 100 insertions(+), 40 deletions(-)
 create mode 100644 builtin/for-each-ref.h
Documentation/git-branch.txt +13 -0
index 0427e80..b6f2826 100644
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--count=<count>] [(--sort=<key>)...]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -192,6 +193,18 @@ start-point is either a local or remote-tracking branch.
 	The new name for an existing branch. The same restrictions as for
 	<branchname> apply.
 
+<count>::
+	By default the command shows all refs that match
+	`<pattern>`.  This option makes it stop after showing
+	that many refs.
+
+<key>::
+	A field name to sort on.  Prefix `-` to sort in descending
+	order of the value.  When unspecified, `refname` is used.  You
+	may use the --sort=<key> option multiple times, in which case
+	the last key becomes the primary key. See
+	linkgit:for-each-ref[1] for field name details.
+
 
 Examples
 --------
Makefile +1 -0
index a782409..daf3e46 100644
@@ -2108,6 +2108,7 @@ builtin/log.o builtin/shortlog.o: shortlog.h
 builtin/prune.o builtin/reflog.o reachable.o: reachable.h
 builtin/commit.o builtin/revert.o wt-status.o: wt-status.h
 builtin/tar-tree.o archive-tar.o: tar.h
+builtin/branch.o builtin/for-each-ref.o: builtin/for-each-ref.h
 connect.o transport.o url.o http-backend.o: url.h
 http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
 http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
builtin/branch.c +49 -12

123 lines not on this page · raw message

builtin/for-each-ref.c +5 -28
index b01d76a..7b25c54 100644
@@ -9,6 +9,7 @@
 #include "quote.h"
 #include "parse-options.h"
 #include "remote.h"
+#include "for-each-ref.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -19,25 +20,6 @@
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
-struct atom_value {
-	const char *s;
-	unsigned long ul; /* used for sorting when not FIELD_STR */
-};
-
-struct ref_sort {
-	struct ref_sort *next;
-	int atom; /* index into used_atom array */
-	unsigned reverse : 1;
-};
-
-struct refinfo {
-	char *refname;
-	unsigned char objectname[20];
-	int flag;
-	const char *symref;
-	struct atom_value *value;
-};
-
 static struct {
 	const char *name;
 	cmp_type cmp_type;
@@ -765,17 +747,12 @@ static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
 	*v = &ref->value[atom];
 }
 
-struct grab_ref_cbdata {
-	struct refinfo **grab_array;
-	const char **grab_pattern;
-	int grab_cnt;
-};
-
 /*
  * A call-back given to for_each_ref().  Filter refs and keep them for
  * later object processing.
  */
-static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+int grab_single_ref(const char *refname, const unsigned char *sha1,
+		    int flag, void *cb_data)
 {
 	struct grab_ref_cbdata *cb = cb_data;
 	struct refinfo *ref;
@@ -858,7 +835,7 @@ static int compare_refs(const void *a_, const void *b_)
 	return 0;
 }
 
-static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
+void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
 {
 	ref_sort = sort;
 	qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
@@ -953,7 +930,7 @@ static struct ref_sort *default_sort(void)
 	return sort;
 }
 
-static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
+int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 {
 	struct ref_sort **sort_tail = opt->value;
 	struct ref_sort *s;
builtin/for-each-ref.h new file +32 -0
new file mode 100644
index 0000000..8542d66
@@ -0,0 +1,32 @@
+struct atom_value {
+	const char *s;
+	unsigned long ul; /* used for sorting when not FIELD_STR */
+};
+
+struct ref_sort {
+	struct ref_sort *next;
+	int atom; /* index into used_atom array */
+	unsigned reverse : 1;
+};
+
+struct refinfo {
+	char *refname;
+	unsigned char objectname[20];
+	int flag;
+	const char *symref;
+	struct atom_value *value;
+};
+
+struct grab_ref_cbdata {
+	struct refinfo **grab_array;
+	const char **grab_pattern;
+	int grab_cnt;
+};
+
+extern int grab_single_ref(const char *refname,
+			   const unsigned char *sha1,
+			   int flag, void *cb_data);
+extern int opt_parse_sort(const struct option *opt,
+			  const char *arg, int unset);
+extern void sort_refs(struct ref_sort *sort,
+		      struct refinfo **refs, int num_refs);
Signature · 1 line
1.7.8.36.g69ee2
#1 lore raw
Quoted 11 lines · Nguyễn Thái Ngọc Duy

---
Some time ago, I posted a patch that added date sort to git-branch and Peff pointed me to for-each-ref. I did not look at it closely. Now it does not seem hard to lend some code from for-each-ref to git-branch. I can list 10 most recently touched branches with

  git branch --sort=-committerdate -v --count=10

kind of cool. I don't think adding --format is necessary because git-branch already has its own formatting.

Why do we even need this for "git branch", when "git for-each-ref refs/heads" already does this?

#2 lore raw

2012/1/18 Junio C Hamano <[email protected]>:

Quoted 9 lines
Quoted 7 lines · Nguyễn Thái Ngọc Duy

---
 Some time ago, I posted a patch that added date sort to git-branch and Peff pointed me to for-each-ref. I did not look at it closely. Now it does not seem hard to lend some code from for-each-ref to git-branch. I can list 10 most recently touched branches with

Quoted 7 lines
Quoted 4 lines

  git branch --sort=-committerdate -v --count=10

 kind of cool. I don't think adding --format is necessary because git-branch already has its own formatting.

Why do we even need this for "git branch", when "git for-each-ref refs/heads" already does this?

coloring, current branch marking, "branch -v", more accessible command (I did not know about for-each-ref until Jeff told me)

Signature · 1 line
Duy
#3 lore raw

PATCH v2 1/1 branch: borrow --sort and --count from for-each-ref

In reply to Nguyễn Thái Ngọc Duy

---
Fix sorted refs being fnmatch()'d twice.

 Documentation/git-branch.txt |   13 +++++++++
 Makefile                     |    1 +
 builtin/branch.c             |   60 +++++++++++++++++++++++++++++++++--------
 builtin/for-each-ref.c       |   33 +++-------------------
 builtin/for-each-ref.h       |   32 ++++++++++++++++++++++
 5 files changed, 99 insertions(+), 40 deletions(-)
 create mode 100644 builtin/for-each-ref.h
Documentation/git-branch.txt +13 -0
index 0427e80..b6f2826 100644
@@ -10,6 +10,7 @@ SYNOPSIS
 [verse]
 'git branch' [--color[=<when>] | --no-color] [-r | -a]
 	[--list] [-v [--abbrev=<length> | --no-abbrev]]
+	[--count=<count>] [(--sort=<key>)...]
 	[(--merged | --no-merged | --contains) [<commit>]] [<pattern>...]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
 'git branch' (-m | -M) [<oldbranch>] <newbranch>
@@ -192,6 +193,18 @@ start-point is either a local or remote-tracking branch.
 	The new name for an existing branch. The same restrictions as for
 	<branchname> apply.
 
+<count>::
+	By default the command shows all refs that match
+	`<pattern>`.  This option makes it stop after showing
+	that many refs.
+
+<key>::
+	A field name to sort on.  Prefix `-` to sort in descending
+	order of the value.  When unspecified, `refname` is used.  You
+	may use the --sort=<key> option multiple times, in which case
+	the last key becomes the primary key. See
+	linkgit:for-each-ref[1] for field name details.
+
 
 Examples
 --------
Makefile +1 -0
index a782409..daf3e46 100644
@@ -2108,6 +2108,7 @@ builtin/log.o builtin/shortlog.o: shortlog.h
 builtin/prune.o builtin/reflog.o reachable.o: reachable.h
 builtin/commit.o builtin/revert.o wt-status.o: wt-status.h
 builtin/tar-tree.o archive-tar.o: tar.h
+builtin/branch.o builtin/for-each-ref.o: builtin/for-each-ref.h
 connect.o transport.o url.o http-backend.o: url.h
 http-fetch.o http-walker.o remote-curl.o transport.o walker.o: walker.h
 http.o http-walker.o http-push.o http-fetch.o remote-curl.o: http.h url.h
builtin/branch.c +48 -12

122 lines not on this page · raw message

builtin/for-each-ref.c +5 -28
index b01d76a..7b25c54 100644
@@ -9,6 +9,7 @@
 #include "quote.h"
 #include "parse-options.h"
 #include "remote.h"
+#include "for-each-ref.h"
 
 /* Quoting styles */
 #define QUOTE_NONE 0
@@ -19,25 +20,6 @@
 
 typedef enum { FIELD_STR, FIELD_ULONG, FIELD_TIME } cmp_type;
 
-struct atom_value {
-	const char *s;
-	unsigned long ul; /* used for sorting when not FIELD_STR */
-};
-
-struct ref_sort {
-	struct ref_sort *next;
-	int atom; /* index into used_atom array */
-	unsigned reverse : 1;
-};
-
-struct refinfo {
-	char *refname;
-	unsigned char objectname[20];
-	int flag;
-	const char *symref;
-	struct atom_value *value;
-};
-
 static struct {
 	const char *name;
 	cmp_type cmp_type;
@@ -765,17 +747,12 @@ static void get_value(struct refinfo *ref, int atom, struct atom_value **v)
 	*v = &ref->value[atom];
 }
 
-struct grab_ref_cbdata {
-	struct refinfo **grab_array;
-	const char **grab_pattern;
-	int grab_cnt;
-};
-
 /*
  * A call-back given to for_each_ref().  Filter refs and keep them for
  * later object processing.
  */
-static int grab_single_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+int grab_single_ref(const char *refname, const unsigned char *sha1,
+		    int flag, void *cb_data)
 {
 	struct grab_ref_cbdata *cb = cb_data;
 	struct refinfo *ref;
@@ -858,7 +835,7 @@ static int compare_refs(const void *a_, const void *b_)
 	return 0;
 }
 
-static void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
+void sort_refs(struct ref_sort *sort, struct refinfo **refs, int num_refs)
 {
 	ref_sort = sort;
 	qsort(refs, num_refs, sizeof(struct refinfo *), compare_refs);
@@ -953,7 +930,7 @@ static struct ref_sort *default_sort(void)
 	return sort;
 }
 
-static int opt_parse_sort(const struct option *opt, const char *arg, int unset)
+int opt_parse_sort(const struct option *opt, const char *arg, int unset)
 {
 	struct ref_sort **sort_tail = opt->value;
 	struct ref_sort *s;
builtin/for-each-ref.h new file +32 -0
new file mode 100644
index 0000000..8542d66
@@ -0,0 +1,32 @@
+struct atom_value {
+	const char *s;
+	unsigned long ul; /* used for sorting when not FIELD_STR */
+};
+
+struct ref_sort {
+	struct ref_sort *next;
+	int atom; /* index into used_atom array */
+	unsigned reverse : 1;
+};
+
+struct refinfo {
+	char *refname;
+	unsigned char objectname[20];
+	int flag;
+	const char *symref;
+	struct atom_value *value;
+};
+
+struct grab_ref_cbdata {
+	struct refinfo **grab_array;
+	const char **grab_pattern;
+	int grab_cnt;
+};
+
+extern int grab_single_ref(const char *refname,
+			   const unsigned char *sha1,
+			   int flag, void *cb_data);
+extern int opt_parse_sort(const struct option *opt,
+			  const char *arg, int unset);
+extern void sort_refs(struct ref_sort *sort,
+		      struct refinfo **refs, int num_refs);
Signature · 1 line
1.7.8.36.g69ee2