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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。