Skip to content

Commit

Permalink
Update prepare-commit-msg +semver: minor (#98)
Browse files Browse the repository at this point in the history
* Update prepare-commit-msg +semver: minor

* CSharpier format

---------

Co-authored-by: gstraccini[bot] <150967461+gstraccini[bot]@users.noreply.github.com>
  • Loading branch information
guibranco and gstraccini[bot] authored Oct 26, 2024
1 parent b1fd8f3 commit ea8ea35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
12 changes: 9 additions & 3 deletions .githooks/prepare-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ if ! command -v dotnet-aicommitmessage &> /dev/null; then
fi

COMMIT_MSG_FILE=$1
GIT_DIFF=$(git diff --staged)
GIT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)
CURRENT_MESSAGE=$(cat "$COMMIT_MSG_FILE")

# From version 0.6.1, this is not needed anymore.
# GIT_DIFF=$(git diff --staged)
# GIT_BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD)

# Run dotnet-aicommitmessage with error handling
if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" -b "$GIT_BRANCH_NAME" -d "$GIT_DIFF" ); then
# From version 0.6.1 branch and diff are now retrieved by the tool and don't need to be passed manually.
# Version 0.6.1 and higher: dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE"
# Version 0.6.0 and lower: dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE" -b "$GIT_BRANCH_NAME" -d "$GIT_DIFF"

if ! AI_MESSAGE=$(dotnet-aicommitmessage generate-message -m "$CURRENT_MESSAGE"); then
echo "Error: Failed to generate AI commit message. Using original message." >&2
exit 0
fi
Expand Down
24 changes: 12 additions & 12 deletions Src/AiCommitMessage/Services/GenerateCommitMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public string GenerateCommitMessage(GenerateCommitMessageOptions options)
: options.Branch;

// Use the provided diff or retrieve it from GIT if not supplied
string diff = string.IsNullOrEmpty(options.Diff)
? GitHelper.GetGitDiff()
: options.Diff;
string diff = string.IsNullOrEmpty(options.Diff) ? GitHelper.GetGitDiff() : options.Diff;

var model = EnvironmentLoader.LoadOpenAiModel();
var url = EnvironmentLoader.LoadOpenAiApiUrl();
Expand All @@ -52,18 +50,20 @@ public string GenerateCommitMessage(GenerateCommitMessageOptions options)

if (string.IsNullOrEmpty(branch) && string.IsNullOrEmpty(diff))
{
throw new InvalidOperationException("Unable to generate commit message: Both branch and diff are empty.");
throw new InvalidOperationException(
"Unable to generate commit message: Both branch and diff are empty."
);
}

var formattedMessage =
"Branch: "
+ (string.IsNullOrEmpty(branch) ? "<unknown>" : branch)
+ "\n\n"
+ "Original message: "
+ message
+ "\n\n"
+ "Git Diff: "
+ (string.IsNullOrEmpty(diff) ? "<no changes>" : diff);
"Branch: "
+ (string.IsNullOrEmpty(branch) ? "<unknown>" : branch)
+ "\n\n"
+ "Original message: "
+ message
+ "\n\n"
+ "Git Diff: "
+ (string.IsNullOrEmpty(diff) ? "<no changes>" : diff);

var chatCompletion = client.CompleteChat(
new SystemChatMessage(Constants.SystemMessage),
Expand Down

0 comments on commit ea8ea35

Please sign in to comment.