duanfazhun0383 2016-05-04 11:23
浏览 57
已采纳

Git2go:在CreateCommit()之后,所有文件看起来都像添加了要删除的一样

With git2go I do a AddAll() and a CreateCommit() on a newly cloned directory. The Push() works fine and I get all commited files, when I clone the remote to a different place. But if I call git status in the first directory then I get:

(sorry for the German, and I don't have the correct translation, it means the branch is at the same position as origin/master, 'gelöscht' means 'deleted' and 'Unbeobachtete dateien' means 'unwatched files')

Auf Branch master
Ihr Branch ist auf dem selben Stand wie 'origin/master'.
zum Commit vorgemerkte Änderungen:
  (benutzen Sie "git reset HEAD <Datei>..." zum Entfernen aus der Staging-Area)

    gelöscht:               .gitignore
    gelöscht:               html/.gitignore
    gelöscht:               images/.gitignore
    gelöscht:               json/.gitignore
    gelöscht:               pjs/.gitignore
    gelöscht:               pjs/test37.pjs
    gelöscht:               pjs/test37.png
    gelöscht:               sounds/.gitignore

Unbeobachtete Dateien:
  (benutzen Sie "git add <Datei>...", um die Änderungen zum Commit vorzumerken)

    .gitignore
    html/
    images/
    json/
    pjs/
    sounds/

What I would expect is the following:

Nothing to commit. No files changed. (or similar)

My code:

 // Add
repo, err := git.OpenRepository(dir)
index, err := repo.Index()
err = index.AddAll([]string{}, git.IndexAddDefault, nil)
treeId, err := index.WriteTreeTo(repo)
tree, err := repo.LookupTree(treeId)

// Commit
sig := &git.Signature{
    Name:  userName,
    Email: userName + "@" + beego.AppConfig.String("userdata::emailserver"),
    When:  time.Now(),
}
commitId, err := repo.CreateCommit("HEAD", sig, sig, message, tree)

// Push
remote, err := repo.LookupRemote("origin")    
err = remote.Push([]string{"refs/heads/master"}, nil, sig, message)

(I took all error handling out, to make it shorter)

  • 写回答

1条回答 默认 最新

  • douzhu5900 2016-05-04 17:09
    关注

    You are opening the current repository's index:

    index, err := repo.Index()
    

    And you are mutating it, and creating a new tree that you subsequently create a commit from:

    err = index.AddAll([]string{}, git.IndexAddDefault, nil)
    treeId, err := index.WriteTreeTo(repo)
    

    Unlike the git add command line, AddAll did not write the index back to disk. This functionality allows you to use the index as a data structure without actually staging the changes immediately. (Many libgit2 functions like merge and rebase use the index as a generic data structure that you can continue to modify or convert to a tree.)

    Thus, at this point, all the changes to the index remain in-memory. You must explicitly write the index back to disk if you want to persist your changes:

    index.Write()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图