Skip to content

Instantly share code, notes, and snippets.

@m0o0scar
Created July 29, 2024 23:59
Show Gist options
  • Save m0o0scar/82fcb607aaccd12bcb167cd561153964 to your computer and use it in GitHub Desktop.
Save m0o0scar/82fcb607aaccd12bcb167cd561153964 to your computer and use it in GitHub Desktop.
di-sukharev/opencommit. Continue this conversation at https://readfm.vercel.app?gist=82fcb607aaccd12bcb167cd561153964

[github] di-sukharev/opencommit

Source

JavaScript / 3.1K lines of code. Generate awesome commits with AI in 1 second πŸ€―πŸ”«

URL: https://github.com/di-sukharev/opencommit?tab=readme-ov-file

Conversation

β–² Explain the major function of the project and how it works by referencing the most important and relevant code snippets.

The opencommit project is a command-line tool that uses AI to automatically generate meaningful commit messages for staged changes in a Git repository. It leverages various AI models like OpenAI's GPT-3.5-turbo, GPT-4, Anthropic's Claude, and Azure OpenAI to generate commit messages based on the staged file diffs.

Here's how it works:

  1. Setup:

    • Install opencommit globally using npm install -g opencommit.
    • Obtain an API key from your chosen AI provider (OpenAI, Anthropic, Azure OpenAI, or Gemini).
    • Set the API key in the OpenCommit configuration using oco config set OCO_OPENAI_API_KEY=<your_api_key>.
  2. Usage:

    • Stage your changes using git add <files...>.
    • Run opencommit or its shortcut oco to generate a commit message.
  3. Commit Message Generation:

    • The commit function in src/commands/commit.ts is responsible for generating the commit message.
    • It first retrieves the staged files using getStagedFiles from src/utils/git.ts.
    • Then, it calls generateCommitMessageByDiff in src/generateCommitMessageFromGitDiff.ts to generate the commit message based on the diff of the staged files.
  4. AI Integration:

    • generateCommitMessageByDiff uses the getEngine function from src/utils/engine.ts to select the appropriate AI engine based on the configured provider.
    • The AI engine (e.g., OpenAi in src/engine/openAi.ts) takes the diff and a prompt as input and returns a generated commit message.
  5. Prompt Engineering:

    • The prompt used for the AI model is defined in src/prompts.ts.
    • It includes instructions for the AI model on how to format the commit message, use GitMoji (if enabled), and provide a description (if enabled).
    • The prompt also includes a section for integrating with @commitlint (if enabled) to ensure consistency with local commitlint rules.
  6. Commit Confirmation:

    • The generated commit message is displayed to the user for confirmation.
    • The user can choose to commit the changes with the generated message or regenerate the message.
  7. Git Integration:

    • If the user confirms the commit message, the commit function executes git commit -m "${generatedMessage}" to commit the changes.
    • It also provides options for pushing the changes to a remote repository.
  8. Git Hook Integration:

    • opencommit can be set as a Git prepare-commit-msg hook using oco hook set.
    • This allows the tool to automatically generate a commit message when the user initiates a commit in their IDE.
  9. GitHub Action Integration:

    • opencommit is also available as a GitHub Action, which can be used to automatically improve commit messages on every push to a repository.

Key Code Snippets:

  • src/commands/commit.ts: Contains the main logic for generating and committing the message.
  • src/generateCommitMessageFromGitDiff.ts: Handles the generation of the commit message based on the diff.
  • src/utils/engine.ts: Selects the appropriate AI engine based on the configured provider.
  • src/engine/openAi.ts: Implements the OpenAI engine for generating commit messages.
  • src/prompts.ts: Defines the prompt used for the AI model.
  • src/modules/commitlint/config.ts: Handles the integration with @commitlint to ensure consistency with local rules.

Overall, opencommit streamlines the commit process by automating the generation of meaningful commit messages, leveraging the power of AI to improve code quality and maintain a consistent commit history.

β–² What context except for the diff are given to the ai to generate the commit message?

Besides the diff, opencommit provides the following context to the AI model to generate the commit message:

1. Commit Message Conventions:

  • GitMoji Specification: If enabled, the prompt instructs the AI to use GitMoji conventions to preface the commit message. It provides a list of common GitMoji emojis and their descriptions.
  • Conventional Commits: If GitMoji is not enabled, the prompt instructs the AI to follow the conventional commit convention, using keywords like fix, feat, build, chore, ci, docs, style, refactor, perf, and test.
  • Structure: The prompt explains the structure of a commit message, including the header (type, scope, subject), body, and footer.

2. Additional Instructions:

  • Description: If enabled, the prompt instructs the AI to add a short description of why the changes were made after the commit message.
  • Language: The prompt specifies the language to be used for the commit message, based on the user's configuration.
  • One-Line Commit: If enabled, the prompt instructs the AI to craft a concise commit message that encapsulates all changes in a single line.

3. @commitlint Integration:

  • If @commitlint is enabled, the prompt includes a section that provides the AI with the local @commitlint rules and prompts it to generate a commit message that adheres to those rules. This ensures consistency with the project's commit message style.

4. Example Git Diff:

  • The prompt includes an example Git diff to help the AI understand the context of the changes and generate a more accurate commit message.

5. Identity:

  • The prompt starts with a statement about the AI's role as the author of a commit message in Git. This helps the AI understand the context and generate a more human-like commit message.

In summary, opencommit provides a comprehensive context to the AI model, including commit message conventions, additional instructions, integration with @commitlint, an example Git diff, and an identity statement. This helps the AI generate more accurate, meaningful, and consistent commit messages.

β–² What’s the prompt used to generate the commit message?

The prompt used to generate the commit message in opencommit is dynamically constructed based on the user's configuration and the chosen AI provider. Here's a breakdown of the prompt structure:

1. System Instruction:

You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages as per the [GitMoji specification / conventional commit convention] and explain WHAT were the changes and mainly WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message.
[Use GitMoji convention to preface the commit. Here are some help to choose the right emoji (emoji, description): πŸ›, Fix a bug; ✨, Introduce new features; πŸ“, Add or update documentation; πŸš€, Deploy stuff; βœ…, Add, update, or pass tests; ♻️, Refactor code; ⬆️, Upgrade dependencies; πŸ”§, Add or update configuration files; 🌐, Internationalization and localization; πŸ’‘, Add or update comments in source code; ... ] / [Do not preface the commit with anything. Conventional commit keywords: fix, feat, build, chore, ci, docs, style, refactor, perf, test.]
[Add a short description of WHY the changes are done after the commit message. Don't start it with "This commit", just describe the changes.] / [Don't add any descriptions to the commit, only commit message.]
[Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change.]
Use the present tense. Lines must not be longer than 74 characters. Use [language] for the commit message.
  • GitMoji/Conventional Commits: The prompt specifies whether to use GitMoji or conventional commits based on the user's configuration.
  • Description: The prompt includes instructions on whether to add a description after the commit message.
  • One-Line Commit: The prompt includes instructions on whether to generate a concise one-line commit message.
  • Language: The prompt specifies the language to be used for the commit message.

2. @commitlint Integration:

  • If @commitlint is enabled, the prompt includes a section that provides the AI with the local @commitlint rules and prompts it to generate a commit message that adheres to those rules. This ensures consistency with the project's commit message style.
You will strictly follow the following conventions to generate the content of the commit message:
- [List of @commitlint rules]

The conventions refers to the following structure of commit message:
- Header of commit is composed of type, scope, subject: <type-of-commit>(<scope-of-commit>): <subject-of-commit>
- Description of commit is composed of body and footer (optional): <body-of-commit>\n<footer(s)-of-commit>

3. Example Git Diff:

  • The prompt includes an example Git diff to help the AI understand the context of the changes and generate a more accurate commit message.
diff --git a/src/server.ts b/src/server.ts
index ad4db42..f3b18a9 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -10,7 +10,7 @@
 import {
     initWinstonLogger();
     
     const app = express();
     -const port = 7799;
     +const PORT = 7799;
     
     app.use(express.json());

4. Consistency Prompt (for @commitlint integration):

  • If @commitlint is enabled, the prompt includes a consistency prompt to help the AI generate a commit message that adheres to the local @commitlint rules.
{
  "localLanguage": "english",
  "commitFix": "<Header of commit for bug fix>",
  "commitFeat": "<Header of commit for feature>",
  "commitDescription": "<Description of commit for both the bug fix and the feature>"
}

In summary, the prompt used by opencommit is a dynamic and comprehensive instruction set that guides the AI model to generate a meaningful and consistent commit message based on the user's preferences and the project's commit message style.

{
"url": "https://github.com/di-sukharev/opencommit?tab=readme-ov-file",
"type": "github",
"title": "di-sukharev/opencommit",
"picture": "https://avatars.githubusercontent.com/u/57486732?v=4",
"description": "JavaScript / 3.1K lines of code.\nGenerate awesome commits with AI in 1 second πŸ€―πŸ”«"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment