git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH/RFC] Add [] as an alias for a reference to the empty tree

From
Peter Kjellerstedt <peter.kjellerstedt@axis.com>
Date
May 7, 2010, 16:37 UTC
Message-ID
<1273250247-20762-1-git-send-email-pkj@axis.com>

Instead of specifying the SHA1 for the empty tree (i.e., 4b825dc642cb6eb9a060e54bf8d69288fbee4904) one can now say [], e.g., 'git diff [] v1.7.1' would give all the changes between the empty tree and the tag v1.7.1.

The rationale for selecting [] as the alias for the empty tree was that it looks empty, the brackets are not used for anything related to references (AFAIK), they are not allowed in references according to 'man git-check-ref-format', and the syntax can easily be extended to allow other types of references by adding information between the brackets.

Signed-off-by: Peter Kjellerstedt <pkj@axis.com>
---
 sha1_name.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

This is in response to the discussion in [1]. I am not sure the use of '[]' as an alias for the empty tree will be accepted, but I think having an alias similar to this is much easier to implement than adding support for a --root option to all commands which can take a <tree-ish>...

Since this is mostly an RFC I have not included any new tests (though the existsing once still pass after this change).

[1] http://thread.gmane.org/gmane.comp.version-control.git/146468/focus=146484
diff --git a/sha1_name.c b/sha1_name.c
index bf92417..ba58eab 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -347,6 +347,9 @@ static int get_sha1_basic(const char *str, int len, unsigned char *sha1)
 	int refs_found = 0;
 	int at, reflog_len;
 
+	if (len == 2 && str[0] == '[' && str[1] == ']')
+		return get_sha1_hex(EMPTY_TREE_SHA1_HEX, sha1);
+
 	if (len == 40 && !get_sha1_hex(str, sha1))
 		return 0;
 
-- 
1.7.0.1
Next: Jeff King
Message 1 of 10 in “Add [] as an alias for a reference to the empty tree”
  1. Add [] as an alias for a reference to the empty treePeter Kjellerstedt, May 7, 2010
  2. Jeff KingMay 8, 2010
  3. Junio C HamanoMay 8, 2010
  4. Jeff KingMay 8, 2010
  5. Jonathan NiederMay 8, 2010
  6. Peter KjellerstedtMay 10, 2010
  7. Jeff KingMay 10, 2010
  8. Sverre RabbelierMay 10, 2010
  9. Jeff KingMay 10, 2010
  10. Sverre RabbelierMay 10, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.