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

How to commit without an index file?

From
Utku <ugultopu@gmail.com>
Date
Jan 18, 2021, 16:58 UTC
Message-ID
<A31FEB7F-0A81-4500-9B1D-F65A5F8823AA@gmail.com>

In my program, I have the file paths, permissions and Git object IDs of everything that I want to appear in the next commit. Hence, I want to create a commit without creating an index file.

Essentially it's like to combining `--cacheinfo` from `update-index` with `commit` like:

	git commit \
		-m "Commit message" \
		--branch my-branch \
		--cacheinfo "100644,0123456789abcdef0123456789abcdef01234567,path/to/first/file” \
		--cacheinfo "100755,123456789abcdef0123456789abcdef012345670,path/to/second/file” \
		--cacheinfo "100644,23456789abcdef0123456789abcdef0123456701,path/to/third/file"

In this example, the command would not read the index file, but would get the information necessary to create the commit tree as arguments (or from STDIN). The command would handle recursively creating trees for the intermediate directories. The object ID for the final tree would be used for the `tree` field of the commit.

I tried tricks for simulating standard streams as an index file to Git, but they didn't work out:

	$ GIT_INDEX_FILE=/dev/stdout git read-tree HEAD
	fatal: Unable to create '/dev/stdout.lock': No such file or directory

The best I could come up with is creating a no checkout worktree for `my-branch` and using a FIFO for the index file, instead of a conventional file. The shell script equivalent of it would be as follows (I would be happy if you can point out any errors in this script as well):

	set -e
	
	git_dir=$(git rev-parse --git-dir)
	worktree_name=my-program
	worktree_location=$git_dir/$worktree_name
	
	# Ran only once
	function init_worktree {
		branch_name=my-branch
		worktree_gitdir=$git_dir/worktrees/$worktree_name
		
		git worktree add "$worktree_location" "$branch_name" --no-checkout
		rm -f "$worktree_gitdir/index”
		mkfifo "$worktree_gitdir/index”
		
		echo "Initialized worktree at $worktree_location”
	}
	# Ran on every commit
	function commit_worktree {
		cd "$worktree_location”
		git read-tree —empty
		git update-index --add \
		--cacheinfo "100644,0123456789abcdef0123456789abcdef01234567,path/to/first/file” \
		--cacheinfo "100755,123456789abcdef0123456789abcdef012345670,path/to/second/file” \
		--cacheinfo "100644,23456789abcdef0123456789abcdef0123456701,path/to/third/file”
		# etc. Will be received as arguments or from STDIN.
		git commit -m "Commit message”
	}

So I guess this is the best it gets? If I wanna make it better, I guess I will need to use libgit2?

Thank you
Next: brian m. carlson
Message 1 of 2 in “How to commit without an index file?”
  1. UtkuJan 18, 2021
  2. brian m. carlsonJan 18, 2021

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.