Skip to contentSkip to conversation

How to put tree into index

DISCUSSION Alexander GQ Gerasiov 3 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello there.

I need to realize the following scenario with git.

1. I have repository with tree like this:

dir1/
file1
file2
file3

dir2/
subdir1/
some files

2. Current branch is B.

3. I want to get dir1 from branch A, and save it's content on current branch (B) as dir2/subdir1

Quoted 1 line

From my point I should do the following

1. Get tree-object sha1 of dir1 from A with git ls-tree
2. Put into index new tree-object with the same sha1/content, but under the name dir2/subdir1
3. Commit index.

But it doesn't work for me. It works with blob object, but not with tree object.

I tried the following commands:

git ls-tree A dir1 |
sed 's#dir1#dir2/subdir1#' |
git update-index --index-info

But after this I see in the index not tree-object with mode 040000, but commit-object with mode 160000.

So my question is
How to put into index tree-object with known sha1 and given name?

PS I was able to do what I need when copied files, not tree-itself. Just add -r to git ls-tree, and put into index blobs/files, not tree. But I'm interested: is it possible to put tree-object into index?

#1 lore raw

On 2013-11-20 15:47, Alexander GQ Gerasiov wrote:
[...]

Quoted 2 lines

So my question is
How to put into index tree-object with known sha1 and given name?

I was just reading about something very similar in the Git book:
http://git-scm.com/book/en/Git-Internals-Git-Objects#Tree-Objects
(read the entire Tree Objects section), and then came up with this, which I think does what you want except it doesn't erase the previous contents of dir2/subdir1:

mattcen@wasp:repo$ ls -la
total 8
drwxrwxr-x 2 mattcen mattcen 4096 Nov 20 23:12 .
drwx------ 3 mattcen mattcen 4096 Nov 20 23:12 ..
mattcen@wasp:repo$ git init
Initialized empty Git repository in /tmp/with-temp-dir.ZDw19F/repo/.git/
mattcen@wasp:repo(master)$ touch .gitignore
mattcen@wasp:repo(master)$ git add .gitignore
mattcen@wasp:repo(master)$ git commit -m Initial\ commit
[master (root-commit) 43d7ee6] Initial commit
 0 files changed
 create mode 100644 .gitignore
mattcen@wasp:repo(master)$ mkdir -p dir1 dir2/subdir1
mattcen@wasp:repo(master)$ touch dir1/file{1..3} dir2/subdir1/some\ files
mattcen@wasp:repo(master)$ git add -A
mattcen@wasp:repo(master)$ git commit -m Add\ files
[master 2654d91] Add files
 0 files changed
 create mode 100644 dir1/file1
 create mode 100644 dir1/file2
 create mode 100644 dir1/file3
 create mode 100644 dir2/subdir1/some files
mattcen@wasp:repo(master)$ git branch A
mattcen@wasp:repo(master)$ git checkout -b B
Switched to a new branch 'B'
mattcen@wasp:repo(B)$ git cat-file -p A^{tree} | grep dir1
040000 tree 9599fdeb034597d90d72d2f58396dee096885b79    dir1
mattcen@wasp:repo(B)$ git read-tree --prefix=dir2/subdir1 9599fdeb034597d90d72d2f58396dee096885b79
mattcen@wasp:repo(B)$ git write-tree
e1d5abac94058d1f321a3aa087f18d796efc8a0e
mattcen@wasp:repo(B)$ git checkout .
mattcen@wasp:repo(B)$ find *
dir1
dir1/file3
dir1/file1
dir1/file2
dir2
dir2/subdir1
dir2/subdir1/some files
dir2/subdir1/file3
dir2/subdir1/file1
dir2/subdir1/file2

Hope this helps.

Signature · 1 line
Regards,
#2 lore raw
In reply to Alexander GQ Gerasiov

Am 11/20/2013 12:47, schrieb Alexander GQ Gerasiov:

Quoted 12 lines

1. I have repository with tree like this:

dir1/
file1
file2
file3

dir2/
subdir1/
some files

Quoted 7 lines

2. Current branch is B.

3. I want to get dir1 from branch A, and save it's content on current branch (B) as dir2/subdir1

So my question is
How to put into index tree-object with known sha1 and given name?

git rm -r --cached dir2/subdir1 &&
git read-tree --prefix=dir2/subdir1/ A:dir1

Note the trailing slash.

Quoted 3 lines

PS I was able to do what I need when copied files, not tree-itself. Just add -r to git ls-tree, and put into index blobs/files, not tree. But I'm interested: is it possible to put tree-object into index?

No, because the index does not store trees, only blobs.

-- Hannes