threads / patch / 3591

patchls-tree: add --abbrev[=<n>] option

Subject: [PATCH] ls-tree: add --abbrev[=<n>] option

## tl;dr

4 messages between Mar 7, 2006 and Mar 7, 2006. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Eric Wong· Mar 7, 2006, 13:52 UTC · lore
Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 Documentation/git-ls-tree.txt |    9 ++++++++-
 ls-tree.c                     |   19 +++++++++++++++++--
 2 files changed, 25 insertions(+), 3 deletions(-)
24fa229085e3090eea54adbb18e906fed8a63a40
Show changes to 2 files +25 −3

Documentation/git-ls-tree.txt, ls-tree.c

diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
index b92a8b2..b30b766 100644
--- a/Documentation/git-ls-tree.txt
+++ b/Documentation/git-ls-tree.txt
@@ -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
diff --git a/ls-tree.c b/ls-tree.c
index d005643..97f09bd 100644
--- a/ls-tree.c
+++ b/ls-tree.c
@@ -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 *sha1
 		return 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);
-- 
1.2.4.g76f4
Junio C Hamano· Mar 7, 2006, 17:56 UTC · re: Eric Wong · lore

Re: [PATCH] ls-tree: add --abbrev[=<n>] option

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.

Eric Wong· Mar 7, 2006, 19:58 UTC · re: Junio C Hamano · lore

Re: [PATCH] ls-tree: add --abbrev[=<n>] option

Junio C Hamano <junkio@cox.net> wrote:
Show 11 quoted lines
> 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 :>
> 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.

-- 
Eric Wong
Eric Wong· Mar 7, 2006, 19:59 UTC · re: Eric Wong · lore

[PATCH] ls-files: add --abbrev[=<n>] option

Signed-off-by: Eric Wong <normalperson@yhbt.net>
---
 Documentation/git-ls-files.txt |    7 ++++++-
 ls-files.c                     |   19 +++++++++++++++++--
 2 files changed, 23 insertions(+), 3 deletions(-)
1aa70bab7a4af7b79db3c86b9f88884c1dfad6ee
Show changes to 2 files +23 −3

Documentation/git-ls-files.txt, ls-files.c

diff --git a/Documentation/git-ls-files.txt b/Documentation/git-ls-files.txt
index e813f84..68d32a8 100644
--- a/Documentation/git-ls-files.txt
+++ b/Documentation/git-ls-files.txt
@@ -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.
 
diff --git a/ls-files.c b/ls-files.c
index df25c8c..585f6a7 100644
--- a/ls-files.c
+++ b/ls-files.c
@@ -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 *ta
 		printf("%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;
-- 
1.2.4.gc279

← back to recent threads