0

Here is what I have tried: Iterate through git log to find the specific commit I want to modify. Next I modify the commit message. Now I want to amend this message to that specific commit. Code snippet as below:

So my problem is this code does not amend the commit message. Not sure where I am going wrong. Since Amend() takes tree as one of the parameter I am assuming the specific commit message should have been pushed to git.

err = odb.ForEach(func(oid *git.Oid) error {
    obj, err := repo.Lookup(oid)
    if err != nil {
        return err
    }

    if obj.Type() != git.ObjectCommit {
        return nil
    }

    commit, err := obj.AsCommit()
    if err != nil {
        return err
    }
    tree, err := commit.Tree()
    if err != nil {
        return err
    } 
    message := strings.Replace(commit.Message(), "\n", " ", -1)
    if strings.Contains(message, key) {
        message := strings.Replace(commit.Message(), key, new_key, -1)
        repo, err := commit.Amend("", signature, signature, message, tree)
        remote, err = repo.Remotes.Create("origin", repo.Path())
        head, err := repo.Head()
        branchName := head.Name()
        if err := remote.Push([]string{branchName}, &git.PushOptions{}); err != nil {
            return err
        }
  • And what's the problem? – Peter Mar 27 '18 at 22:50
  • [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) go through this and edit your question. – Morse Mar 28 '18 at 00:29
  • I don't know git2go but as far as git is concerned you can only directly amend the *latest* commit. Changing a commit in the middle of a history changes all subsequent commits as well: It is a rebase and must be performed as one. See https://stackoverflow.com/questions/1186535/how-to-modify-a-specified-commit-in-git. – Peter - Reinstate Monica Mar 28 '18 at 00:58
  • @Peter :Edited my post, sorry missed the question part of the post! – user9426119 Mar 28 '18 at 21:15
  • @PeterA.Schneider :I understand git amend amends the latest commit. However, this git2go Amend() API takes tree as one of the parameters which I think is used to identify a specific commit. – user9426119 Mar 28 '18 at 21:35
  • As I said, if `Amend` tries to do what it sounds like it should do (namely amend a commit), it will not be possible without rewriting the history of all commits for which this commit is an ancestor, which amounts to a rebase. I notice that you never check `err` which should be nil when no error orccured (and otherwise presumably should contain a diagnostic message). (As an aside, the utter lack of documentation of git2go is an obvious hurdle to effectively using it, isn't it? Why don't you document for the project what you find out?) – Peter - Reinstate Monica Mar 29 '18 at 05:35

0 Answers0