直接提交到裸仓库 [英] Commit directly to a bare repository

查看:110
本文介绍了直接提交到裸仓库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Some Context



这适合什么



一个专注于协作的Web应用程序,提供git托管 bare repos)



我们想做什么



允许用户直接将一组文件添加到他们现有的存储库。



我的问题



工具或方法来手动创建只涉及添加新文件到git repo的提交?



我们现在可以使用临时服务器端稀疏签出来执行此操作,但是我们希望优化这个过程。 解决方案

页面有一个这样的例子,但我会尽量简化它。



看起来,裸回购仍然有一个索引,可以将其操作并进行提交。从头开始创建树对象也是可能的,但我不知道具体是怎么实现的。



如果别人可能冒险,您可能必须锁定存储库在同一时间访问它。我只需从这里的 procmail 包中使用 lockfile

 #!/ bin / bash 
cd myrepo.git

MY_BRANCH = master
MY_FILE_CONTENTS = $'Hello,world!\\ \\ n'

#请注意,这只是该脚本的锁。它不被其他工具所尊敬。
lockfile -1 -r 10锁定||退出1

PARENT_COMMIT =$(git show-ref -s$ MY_BRANCH)

#清空索引,不知道这一步是否必要
git read-tree --empty

#加载当前树。一个提交ref是好的,它会弄清楚。
git读取树$ {PARENT_COMMIT}

#创建一个blob对象。有些系统有shasum而不是sha1sum
#可能想检查它是否已经存在。作为锻炼留下。 :)
BLOB_ID = $(printfblob%d\0%s$(echo -n$ MY_FILE_CONTENTS| wc -c)$ MY_FILE_CONTENTS| sha1sum | cut -d''-f 1 )
mkdir -pobjects / $ {BLOB_ID:0:2}
printfblob%d \ 0%s$(echo -n$ MY_FILE_CONTENTS| wc -c) $ MY_FILE_CONTENTS| perl -MCompress :: Zlib -e'undef $ /;打印压缩(<>)'> objects / $ {BLOB_ID:0:2} / $ {BLOB_ID:2}

#现在将它添加到索引中。
git update-index --add --cacheinfo 100644$ BLOB_IDmyfile.txt

#从您的新索引创建一棵树
TREE_ID = $(git write -tree)

#提交它。
NEW_COMMIT = $(echoMy commit message| git commit-tree$ TREE_ID-p$ PARENT_COMMIT)

#更新分支
git update-ref refs / heads / $ MY_BRANCH$ NEW_COMMIT$ PARENT_COMMIT

#完成
rm -f锁定

如果有git命令创建blob会很好,但我没有找到。 perl命令取自另一个问题


Some Context

what this fits into

A collaboration focused web application that offers git hosting (as bare repos)

what we want to do

Allow users to add a set of files directly to their existing repositories.

My Question

Is there a tool or method to manually create a commit that only involves adding new files to a git repo?

We're able to do this now using temporary server-side sparse checkouts but we would like to optimize this process.

解决方案

The plumbing and porcelain page sort of have an example of this, but I'll try to simplify it.

It seems bare repos still have an index, which can be manipulated and made into a commit. It's probably also possible to create tree object from scratch, but I'm not aware of exactly how.

You probably have to lock the repository if there's a risk someone else might access it at the same time. I'll simply use lockfile from the procmail package here.

#!/bin/bash
cd myrepo.git

MY_BRANCH=master
MY_FILE_CONTENTS=$'Hello, world!\n'

# Note this is just a lock for this script. It's not honored by other tools.
lockfile -1 -r 10 lock || exit 1

PARENT_COMMIT="$(git show-ref -s "$MY_BRANCH")"

# Empty the index, not sure if this step is necessary
git read-tree --empty

# Load the current tree. A commit ref is fine, it'll figure it out.
git read-tree "${PARENT_COMMIT}"

# Create a blob object. Some systems have "shasum" instead of "sha1sum"
# Might want to check if it already exists. Left as an excercise. :)
BLOB_ID=$(printf "blob %d\0%s" $(echo -n "$MY_FILE_CONTENTS" | wc -c) "$MY_FILE_CONTENTS" | sha1sum | cut -d ' ' -f 1)
mkdir -p "objects/${BLOB_ID:0:2}"
printf "blob %d\0%s" $(echo -n "$MY_FILE_CONTENTS" | wc -c) "$MY_FILE_CONTENTS" | perl -MCompress::Zlib -e 'undef $/; print compress(<>)' > "objects/${BLOB_ID:0:2}/${BLOB_ID:2}"

# Now add it to the index.
git update-index --add --cacheinfo 100644 "$BLOB_ID" "myfile.txt"

# Create a tree from your new index
TREE_ID=$(git write-tree)

# Commit it.
NEW_COMMIT=$(echo "My commit message" | git commit-tree "$TREE_ID" -p "$PARENT_COMMIT")

# Update the branch
git update-ref "refs/heads/$MY_BRANCH" "$NEW_COMMIT" "$PARENT_COMMIT"

# Done
rm -f lock

It'd be nice if there were git commands to create blobs, but I didn't find one. The perl command was taken from another question.

这篇关于直接提交到裸仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆