git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "brian m. carlson" <sandals@crustytoothpaste.net>
To: Utku <ugultopu@gmail.com>
Cc: git@vger.kernel.org
Subject: Re: How to commit without an index file?
Date: Mon, 18 Jan 2021 17:59:54 +0000	[thread overview]
Message-ID: <YAXMmtIXB9gXUi9C@camp.crustytoothpaste.net> (raw)
In-Reply-To: <A31FEB7F-0A81-4500-9B1D-F65A5F8823AA@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2271 bytes --]

On 2021-01-18 at 16:58:56, Utku wrote:
> 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.

You could use git mktree, git commit-tree, and git update-ref for this.
If you have the object IDs for all the existing blobs and trees, then
you can create any necessary trees, pass the root tree to git
commit-tree, and then use git update-ref to update the branch from the
old value to the new value.

Note that if you're doing this in an otherwise busy repository, you
should definitely specify the old value of the ref to avoid a race
condition where someone else updates it at the same time.

If this seems like a lot of inconvenient work, then you'll see why we
have the index.

If you just want to create a commit without modifying the _main_ index
file, then you can use the GIT_INDEX_FILE environment variable to use a
temporary file as the index and then read data into there and commit
using git read-tree, git update-index, git commit-tree, and git
update-ref.  We do that elsewhere in Git, and this will definitely be
easier than writing your own trees using git mktree.

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

This may be slightly easier with libgit2, depending on your needs.
-- 
brian m. carlson (he/him or they/them)
Houston, Texas, US

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 263 bytes --]

      reply	other threads:[~2021-01-18 18:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-18 16:58 How to commit without an index file? Utku
2021-01-18 17:59 ` brian m. carlson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YAXMmtIXB9gXUi9C@camp.crustytoothpaste.net \
    --to=sandals@crustytoothpaste.net \
    --cc=git@vger.kernel.org \
    --cc=ugultopu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).