Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please help to get git diff into prompt :) #101

Open
dvogt23 opened this issue Jun 17, 2024 · 8 comments
Open

Please help to get git diff into prompt :) #101

dvogt23 opened this issue Jun 17, 2024 · 8 comments

Comments

@dvogt23
Copy link

dvogt23 commented Jun 17, 2024

Hey thanks for this great plugin, I would try to have something like this as a prompt for my commit messages, but have no idea how to get my local git diff into this prompt. Any suggestions? :)

@David-Kunz
Copy link
Owner

David-Kunz commented Jun 27, 2024

Hi @dvogt23 ,

Thanks! You can also modify the prompt templates programmatically and I would do it like that:

Create a lua function to

  1. get the output of git diff
  2. Put that into an appropriate prompt
  3. Put that prompt into a temporary require('gen').prompts['someTmp']
  4. Invoke it with :Gen someTmp

Would that work for you?

@dvogt23
Copy link
Author

dvogt23 commented Jul 2, 2024

Seems strange, did try it like this:

function get_git_diff()
  -- Define the command to get the git diff
  local command = "git diff"

  -- Use io.popen to run the command and capture its output
  local handle = io.popen(command)
  local result = handle:read("*a") -- Read all output
  handle:close()

  return result
end

require("gen").prompts["Commit summary"] = {
  prompt = "You are an expert developer specialist in creating commits. Provide a super concise one sentence overall changes summary of the user "
    .. get_git_diff()
    .. " output following strictly the next rules: - Do not use any code snippets, imports, file routes or bullets points. - Do not mention the route of file that has been change. - Simply describe the MAIN GOAL of the changes. - Output directly the summary in plain text.",
  replace = true,
}

but getting always like this as output:
The user output shows the main goal of the changes as "Added new features to improve the performance and user experience of the application."

I think I have to chain the output of the first prompt, to this:

You are an expert developer specialist in creating commits messages.
	Your only goal is to retrieve a single commit message. 
	Based on the provided user changes, combine them in ONE SINGLE commit message retrieving the global idea, following strictly the next rules:
	- Always use the next format: \`{type}: {commit_message}\` where \`{type}\` is one of \`feat\`, \`fix\`, \`docs\`, \`style\`, \`refactor\`, \`test\`, \`chore\`, \`revert\`.
	- Output directly only one commit message in plain text.
	- Be as concise as possible. 50 characters max.
	- Do not add any issues numeration nor explain your output.

Is this possible somehow? 😆

@Rasmus-Bertell
Copy link

Just wanted to share my solution to this problem if you are still struggling with this. It's missing error handling but should work most of the time.

{
  'David-Kunz/gen.nvim/issues/101',
  opts = { display_mode = 'split' },
  config = function(_, opts)
    local gen = require('gen')

    gen.setup(opts)

    gen.prompts['Commit'] = {
      prompt = function()
        local diff = vim.system({ 'git', 'diff', '--staged' }, { text = true }):wait()

        return [[
          ...prompt...

          Git diff: ]] .. diff.stdout
      end,
      replace = true,
      extract = '```$filetype\n(.-)```',
    }
  end
}

@dvogt23
Copy link
Author

dvogt23 commented Jul 24, 2024

Great, thanks @Rasmus-Bertell now Im able to get the summary. I have to chain the output of summary promt to another one for getting a commit message. Do you have an idea?

@Rasmus-Bertell
Copy link

No, sorry. This covers my needs so I'm not motivated to explore it further.

@David-Kunz
Copy link
Owner

Hi @dvogt23 ,

I'm afraid follow-up prompts are not yet possible. Only manually with :Gen Chat.

I'll chink about supporting prompt chains.

@David-Kunz
Copy link
Owner

For now, would it be possible to put everything in one prompt?

@Rasmus-Bertell
Copy link

The problem is that many models have a hard time generating proper commit messages from just the diff, or at least that's what I have noticed, especially if the diff is long. So I think the solution is to first generate a short summary of changes with one prompt and pipe it to another one that makes the actual commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants