ls-tree: add --abbrev[=<n>] option
PATCH 1/1 ls-tree: add --abbrev[=<n>] option
---
Documentation/git-ls-tree.txt | 9 ++++++++- ls-tree.c | 19 +++++++++++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-)
24fa229085e3090eea54adbb18e906fed8a63a40
Documentation/git-ls-tree.txt +8 -1
@@ -8,7 +8,9 @@ git-ls-tree - Lists the contents of a tr SYNOPSIS -------- -'git-ls-tree' [-d] [-r] [-t] [-z] [--name-only] [--name-status] <tree-ish> [paths...] +'git-ls-tree' [-d] [-r] [-t] [-z] + [--name-only] [--name-status] [--full-name] [--abbrev=[<n>]] + <tree-ish> [paths...] DESCRIPTION -----------
@@ -40,6 +42,11 @@ OPTIONS --name-status:: List only filenames (instead of the "long" output), one per line. +--abbrev[=<n>]:: + Instead of showing the full 40-byte hexadecimal object + lines, show only handful hexdigits prefix. + Non default number of digits can be specified with --abbrev=<n>. + paths:: When paths are given, show them (note that this isn't really raw pathnames, but rather a list of patterns to match). Otherwise
ls-tree.c +17 -2
@@ -13,13 +13,14 @@ static int line_termination = '\n';#define LS_TREE_ONLY 2#define LS_SHOW_TREES 4#define LS_NAME_ONLY 8 +static int abbrev =0 ;static int ls_options =0 ;const char **pathspec;static int chomp_prefix =0 ;static const char *prefix;static const char ls_tree_usage[] = -"git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] <tree-ish> [path...]" ; +"git-ls-tree [-d] [-r] [-t] [-z] [--name-only] [--name-status] [--full-name] [--abbrev[=<n>]] <tree-ish> [path...]" ;static int show_recursive (const char *base,int baselen,const char *pathname) {
@@ -73,7 +74,9 @@ static int show_tree(unsigned char *sha1return 0 ;if (!(ls_options &LS_NAME_ONLY )) -printf ("%06o %s %s\t" , mode, type,sha1_to_hex (sha1)); +printf ("%06o %s %s\t" , mode, type, + abbrev ?find_unique_abbrev (sha1,abbrev) + :sha1_to_hex (sha1));write_name_quoted (base + chomp_prefix, baselen - chomp_prefix, pathname, line_termination, stdout);
@@ -113,6 +116,18 @@ int main(int argc, const char **argv) chomp_prefix =0 ;break ; } +if (!strncmp (argv[1 ]+2 ,"abbrev=" ,7 )) { + abbrev =strtoul (argv[1 ]+9 ,NULL ,10 ); +if (abbrev && abbrev <MINIMUM_ABBREV ) + abbrev =MINIMUM_ABBREV ; +else if (abbrev >40 ) + abbrev =40 ; +break ; + } +if (!strcmp (argv[1 ]+2 ,"abbrev" )) { + abbrev =DEFAULT_ABBREV ; +break ; + }/* otherwise fallthru */ default :usage (ls_tree_usage);
Signature · 1 line
1.2.4.g76f4
Output from ls-tree and ls-files -s are primarily for script consumption. diff-raw output which already has abbrev is also primarily for scripts, but we have an excuse there that human eyes can spot differences easier in abbreviated form and two values on the same row being different is the only information we are conveying in "git whatchanged" default output, and not abbreviating the object names makes pathnames pushed to far right of the display which will be chopped by "less -S".
So I am not quite sure if I should take this, and a subsequent one you would probably send for ls-files ;-).
Care to point out a use case I might have missed that this is useful? I've never done ls-tree to be read by _me_ (not by script) unless I am debugging ls-tree itself.
Although I doubt this adds much practical value, this might make things consistent, which by itself might be a reason to do this, though.
Quoted 11 lines · Junio C Hamano
Output from ls-tree and ls-files -s are primarily for script consumption. diff-raw output which already has abbrev is also primarily for scripts, but we have an excuse there that human eyes can spot differences easier in abbreviated form and two values on the same row being different is the only information we are conveying in "git whatchanged" default output, and not abbreviating the object names makes pathnames pushed to far right of the display which will be chopped by "less -S".
So I am not quite sure if I should take this, and a subsequent one you would probably send for ls-files ;-).
I've never used ls-files with -s, but it's a simple patch on its way :>
Quoted 3 lines
Care to point out a use case I might have missed that this is useful? I've never done ls-tree to be read by _me_ (not by script) unless I am debugging ls-tree itself.
I'm a weirdo and I use vim as a repository browser:
(ref: http://www.gelato.unsw.edu.au/archives/git/0511/13088.html , I've since renamed my 'git-show' script to 'git-cat-any').
My usual buffer width is 80 columns. So when I want to see a file from a certain tree that's not checked out: I'll run git-ls-tree -r <tree> and output it to my vim buffer, find the corresponding sha1 for the file, and hit my ',G' macro with my cursor over the blob sha1 to display its contents. Abbreviating the sha1 is very useful for seeing longer file paths on an 80 column buffer.
Signature · 1 line
Eric Wong
PATCH 1/1 ls-files: add --abbrev[=<n>] option
---
Documentation/git-ls-files.txt | 7 ++++++- ls-files.c | 19 +++++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-)
1aa70bab7a4af7b79db3c86b9f88884c1dfad6ee
Documentation/git-ls-files.txt +6 -1
@@ -16,7 +16,7 @@ SYNOPSIS [-X <file>|--exclude-from=<file>] [--exclude-per-directory=<file>] [--error-unmatch] - [--full-name] [--] [<file>]\* + [--full-name] [--abbrev] [--] [<file>]\* DESCRIPTION -----------
@@ -98,6 +98,11 @@ OPTIONS option forces paths to be output relative to the project top directory. +--abbrev[=<n>]:: + Instead of showing the full 40-byte hexadecimal object + lines, show only handful hexdigits prefix. + Non default number of digits can be specified with --abbrev=<n>. + --:: Do not interpret any more arguments as options.
ls-files.c +17 -2
@@ -11,6 +11,7 @@#include "cache.h" #include "quote.h" +static int abbrev =0 ;static int show_deleted =0 ;static int show_cached =0 ;static int show_others =0 ;
@@ -488,7 +489,8 @@ static void show_ce_entry(const char *taprintf ("%s%06o %s %d\t" , tag,ntohl (ce->ce_mode), -sha1_to_hex (ce->sha1), + abbrev ?find_unique_abbrev (ce->sha1,abbrev) + :sha1_to_hex (ce->sha1),ce_stage (ce));write_name_quoted ("" ,0 , ce->name + offset, line_terminator, stdout);
@@ -629,7 +631,8 @@ static void verify_pathspec(void)static const char ls_files_usage[] ="git-ls-files [-z] [-t] [-v] (--[cached|deleted|others|stage|unmerged|killed|modified])* " "[ --ignored ] [--exclude=<pattern>] [--exclude-from=<file>] " -"[ --exclude-per-directory=<filename> ] [--full-name] [--] [<file>]*" ; +"[ --exclude-per-directory=<filename> ] [--full-name] [--abbrev] " +"[--] [<file>]*" ;int main (int argc,const char **argv) {
@@ -736,6 +739,18 @@ int main(int argc, const char **argv) error_unmatch =1 ;continue ; } +if (!strncmp (arg,"--abbrev=" ,9 )) { + abbrev =strtoul (arg+9 ,NULL ,10 ); +if (abbrev && abbrev <MINIMUM_ABBREV ) + abbrev =MINIMUM_ABBREV ; +else if (abbrev >40 ) + abbrev =40 ; +continue ; + } +if (!strcmp (arg,"--abbrev" )) { + abbrev =DEFAULT_ABBREV ; +continue ; + }if (*arg =='-' )usage (ls_files_usage);break ;
Signature · 1 line
1.2.4.gc279