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

Re: In tree object, Must the

From
Carlos Martín Nieto <cmn@elego.de>
Date
Apr 8, 2012, 13:20 UTC
Message-ID
<1333891217.7419.5.camel@centaur.lab.cmartin.tk>
In-Reply-To
<CAFT+Tg-+s5=pFNwY7ZMxem4z6YHu53kyOs0B_rkmphq=mEtpTg@mail.gmail.com>
On Sun, 2012-04-08 at 12:43 +0900, Yi, EungJun wrote:
Show 17 quoted lines
> Hello,
> 
> I'm implementing Git using node.js, and I have a question while I
> write some code to store tree object.
> 
> Tree object looks a table consists of three fields: blob's mode, name
> and id, as below.
> 
> e.g.)
> $ git cat-file -p 45799547
> 100644 blob cd242b1e5bb403500feb49a1aa656c21c6c0be69	Makefile
> 100644 blob bf382321749577d52bd2fbf2281df0510b4bad31	README.md
> 100644 blob 5441bb48428611a3cb140d8192d39484fcf3b742	fsutil.js
> 100644 blob 0af680a5c0dd4482b09aa7f8e837234bed0b7cfa	package.json
> 040000 tree 39a4d45669addfb1e8f0a499deebc5b97b4edfa0	test
> 
> It seems that the table is stored in order by blob's name.

Yes, the entries in the tree are alpha-sorted. The exception are trees, where you have to pretend that there is a trailing slash. In other words, the order is the same as you'd see in the index (as there, the test/ directory in your example would be stored with a slash and the name of the subdirs and files in it.

> 
> If it is true, what happens if it is not ordered?
fsck complains for one.
> 
> Does that cause any troubles to users to use a git repository created
> and managed by my Git implementation?

How does your implementation store things? You haven't said (maybe hinted that you may be writing trees with the wrong order). Depending on the particular implementation of whatever is reading the git repository, it might not be able to find an entry in your tree, as it's wrongly sorted, but that depends on the exact implementation and possibly luck.

Do you need to write this in pure js? There are some bindings for node.js[0] already for libgit2 so you don't need to redo the low-level work.

Cheers,
   cmn
[0] https://github.com/libgit2/node-gitteh
Previous: Yi, EungJunNext: Yi, EungJun
Message 2 of 7 in “In tree object, Must the”
  1. Yi, EungJunApr 8, 2012
  2. Carlos Martín NietoApr 8, 2012
  3. Yi, EungJunApr 8, 2012
  4. Junio C HamanoApr 9, 2012
  5. Shawn PearceApr 9, 2012
  6. Junio C HamanoApr 9, 2012
  7. Shawn PearceApr 9, 2012

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.