{"thread":{"id":"35366","subject":"How to put tree into index","startedAt":"2013-11-20T11:47:14Z","lastAt":"2013-11-20T12:41:10Z","messageCount":3,"participants":["Alexander GQ Gerasiov","Matthew Cengia","Johannes Sixt"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"230835","messageId":"20131120154714.39fc5202@snail","threadId":"35366","inReplyTo":null,"subject":"How to put tree into index","fromName":"Alexander GQ Gerasiov","fromEmail":"gq@cs.msu.su","sentAt":"2013-11-20T11:47:14Z","receivedAt":"2013-11-20T11:47:14Z","isPatch":false,"sender":{"key":"gq@cs.msu.su","avatar":null},"body":"Hello there.\n\nI need to realize the following scenario with git.\n\n1. I have repository with tree like this:\n\ndir1/\n\tfile1\n\tfile2\n\tfile3\n\ndir2/\n\tsubdir1/\n\t\tsome files\n\n\n2. Current branch is B.\n\n3. I want to get dir1 from branch A, and save it's content on current\nbranch (B) as dir2/subdir1\n\n\n\n>From my point I should do the following\n\n1. Get tree-object sha1 of dir1 from A with git ls-tree\n2. Put into index new tree-object with the same sha1/content, but under\nthe name dir2/subdir1\n3. Commit index.\n\n\nBut it doesn't work for me. It works with blob object, but not with\ntree object.\n\nI tried the following commands:\n\ngit ls-tree A dir1 | \nsed 's#dir1#dir2/subdir1#' | \ngit update-index --index-info\n\nBut after this I see in the index not tree-object with mode 040000, but\ncommit-object with mode 160000.\n\n\n\nSo my question is\nHow to put into index tree-object with known sha1 and given name?\n\n\nPS I was able to do what I need when copied files, not tree-itself.\nJust add -r to git ls-tree, and put into index blobs/files, not tree.\nBut I'm interested: is it possible to put tree-object into index?\n"},{"id":"230838","messageId":"20131120122537.GE26413@debian.cengia.id.au","threadId":"35366","inReplyTo":"20131120154714.39fc5202@snail","subject":"Re: How to put tree into index","fromName":"Matthew Cengia","fromEmail":"mattcen@gmail.com","sentAt":"2013-11-20T12:25:38Z","receivedAt":"2013-11-20T12:25:38Z","isPatch":false,"sender":{"key":"mattcen@gmail.com","avatar":"https://gravatar.com/avatar/4b0ef07b3df21906e69c4a91c5482463e8cb3f10a08a662686bd4e8915704adc?d=mp&s=160"},"body":"On 2013-11-20 15:47, Alexander GQ Gerasiov wrote:\n[...]\n> So my question is\n> How to put into index tree-object with known sha1 and given name?\n\nI was just reading about something very similar in the Git book:\nhttp://git-scm.com/book/en/Git-Internals-Git-Objects#Tree-Objects\n(read the entire Tree Objects section), and then came up with this,\nwhich I think does what you want except it doesn't erase the previous\ncontents of dir2/subdir1:\n\nmattcen@wasp:repo$ ls -la\ntotal 8\ndrwxrwxr-x 2 mattcen mattcen 4096 Nov 20 23:12 .\ndrwx------ 3 mattcen mattcen 4096 Nov 20 23:12 ..\n\nmattcen@wasp:repo$ git init\nInitialized empty Git repository in /tmp/with-temp-dir.ZDw19F/repo/.git/\n\nmattcen@wasp:repo(master)$ touch .gitignore\n\nmattcen@wasp:repo(master)$ git add .gitignore\n\nmattcen@wasp:repo(master)$ git commit -m Initial\\ commit\n[master (root-commit) 43d7ee6] Initial commit\n 0 files changed\n create mode 100644 .gitignore\n\nmattcen@wasp:repo(master)$ mkdir -p dir1 dir2/subdir1\n\nmattcen@wasp:repo(master)$ touch dir1/file{1..3} dir2/subdir1/some\\ files\n\nmattcen@wasp:repo(master)$ git add -A\n\nmattcen@wasp:repo(master)$ git commit -m Add\\ files\n[master 2654d91] Add files\n 0 files changed\n create mode 100644 dir1/file1\n create mode 100644 dir1/file2\n create mode 100644 dir1/file3\n create mode 100644 dir2/subdir1/some files\n\nmattcen@wasp:repo(master)$ git branch A\n\nmattcen@wasp:repo(master)$ git checkout -b B\nSwitched to a new branch 'B'\n\nmattcen@wasp:repo(B)$ git cat-file -p A^{tree} | grep dir1\n040000 tree 9599fdeb034597d90d72d2f58396dee096885b79    dir1\n\nmattcen@wasp:repo(B)$ git read-tree --prefix=dir2/subdir1 9599fdeb034597d90d72d2f58396dee096885b79\n\nmattcen@wasp:repo(B)$ git write-tree\ne1d5abac94058d1f321a3aa087f18d796efc8a0e\n\nmattcen@wasp:repo(B)$ git checkout .\n\nmattcen@wasp:repo(B)$ find *\ndir1\ndir1/file3\ndir1/file1\ndir1/file2\ndir2\ndir2/subdir1\ndir2/subdir1/some files\ndir2/subdir1/file3\ndir2/subdir1/file1\ndir2/subdir1/file2\n\n\nHope this helps.\n\n-- \nRegards,\n"},{"id":"230841","messageId":"528CADE6.3020604@viscovery.net","threadId":"35366","inReplyTo":"20131120154714.39fc5202@snail","subject":"Re: How to put tree into index","fromName":"Johannes Sixt","fromEmail":"j.sixt@viscovery.net","sentAt":"2013-11-20T12:41:10Z","receivedAt":"2013-11-20T12:41:10Z","isPatch":false,"sender":{"key":"j6t@kdbg.org","avatar":"https://avatars.githubusercontent.com/u/14810926?v=4"},"body":"Am 11/20/2013 12:47, schrieb Alexander GQ Gerasiov:\n> 1. I have repository with tree like this:\n> \n> dir1/\n> \tfile1\n> \tfile2\n> \tfile3\n> \n> dir2/\n> \tsubdir1/\n> \t\tsome files\n> \n> \n> 2. Current branch is B.\n> \n> 3. I want to get dir1 from branch A, and save it's content on current\n> branch (B) as dir2/subdir1\n\n> So my question is\n> How to put into index tree-object with known sha1 and given name?\n\ngit rm -r --cached dir2/subdir1 &&\ngit read-tree --prefix=dir2/subdir1/ A:dir1\n\nNote the trailing slash.\n\n> PS I was able to do what I need when copied files, not tree-itself.\n> Just add -r to git ls-tree, and put into index blobs/files, not tree.\n> But I'm interested: is it possible to put tree-object into index?\n\nNo, because the index does not store trees, only blobs.\n\n-- Hannes\n"}]}