2

I am trying to create a commit history using git plumbing commands with a bare repo. I can create commits with a single unnamed tree object containing blobs, but I cannot figure out how to get this unnamed tree object to contain other tree objects.

I tried using git read-tree --prefix=tree_name tree_sha and it tells me: fatal: This operation must be run in a work tree

I tried using git mktree (like it is shown on this page) like this: cat ../info.txt | git mktree

info.txt being a file containing 1 line:

040000 tree aa8c07e1371022a183b011d5d41517ef54780a17    test_tree

and it tells me:

fatal: input format error: 040000 tree aa8c07e1371022a183b011d5d41517ef54780a17    tree_name

Can anyone tell me a way to create and name trees?

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
prw56
  • 326
  • 2
  • 12

2 Answers2

1

You need four spaces instead of one between the hash and test_tree. If it still doesn't work try a tab.

040000 tree aa8c07e1371022a183b011d5d41517ef54780a17    test_tree
Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
ThePyroEagle
  • 153
  • 12
  • I am not sure if I pasted it here wrong, but I did have 4 spaces between the sha and the tree name (test_tree). I double checked and tried again, I still got the same error. – prw56 Jan 13 '15 at 16:12
  • I know how Git works internally, but I don't know what most of the git commands do. You could try doing it manually, but that's not so easy to do. – ThePyroEagle Jan 13 '15 at 16:48
  • I apologize, when I tested this before I had assumed that I was initially using a tab between the hash and the tree name, so I thought that that wasn't the issue, turns out it was. replacing the 4 spaces between aa8c07e1371022a183b011d5d41517ef54780a17 and test_tree I had with a tab fixed it. – prw56 Jan 14 '15 at 14:30
0

I assume a tree with hash aa8c07e1371022a183b011d5d41517ef54780a17 already exists in your repo? If not, git mktree will fail. Also, do you have a trailing newline in info.txt? My reading of the docs suggests this is also required.

I have successfully used git mktree like this:

$ cat tree.txt
040000 tree 4d5fcadc293a348e88f777dc0920f11e7d71441c    foo

$ git mktree < tree.txt
0a7f38a609340d0b8eede2f6debf8bad4191738f

Where the first two spaces are spaces, and the third is a tab, and with a trailing newline. If in doubt, run git ls-tree and examine its output, possibly with xxd.

Andrew Aylett
  • 39,182
  • 5
  • 68
  • 95
  • I finally got it to work! It turns out that the issue was that I was using 4 spaces instead of a tab between the hash and the name(I need to apologize to ThePyroEagle, since he already suggested this). Also if there is a trailing newline in the file there is an odd artifact that occurs when I look at the tree using "git cat-file -p ". If I used mktree on the file with a trailing newline the trees name is listed as: "test_tree\r" (with quotations and a /r), but if there is no trailing newline it looks normal: test_tree (no quotations or /r) – prw56 Jan 14 '15 at 14:28
  • Sounds like you've got a Windows newline (`\r\n`) rather than a Unix one (`\n`). Glad you could fix it, maybe give upvotes as well as accepting the answer? – Andrew Aylett Jan 14 '15 at 16:48
  • As soon as I have at least 15 rep I intend to upvote your answer as well as his. Also double thank you because your comment about it being a windows newline was correct and you helped me fix another issue. – prw56 Jan 14 '15 at 21:38
  • Well if that tree existed, then there's absolutely no point in adding *it* again. If it already exists, chances are the content are the exact same. – ThePyroEagle Jan 19 '15 at 19:31
  • @ThePyroEagle, the tree I was asking about was the one being put as a sub-tree of the new tree, not the new tree itself. – Andrew Aylett Jan 20 '15 at 09:17