kemayo / sublime-text-git

Plugin for some git integration into sublime text
MIT License
2.83k stars 391 forks source link

Repo Specific Prepare Commit #564

Open Zren opened 5 years ago

Zren commented 5 years ago

I recently found out about the power of .git/hooks/prepare-commit-msg but found out this plugin doesn't support it. So I modified this plugin to look for .git/hooks/SublimeTextGit-prepare-commit-msg use the contents of that file for the commit message. Unlike the other hooks, it's not a bash script which makes it easier to setup.

diff --git a/git/commit.py b/git/commit.py
index 90eb726..0cfcefd 100644
--- a/git/commit.py
+++ b/git/commit.py
@@ -8,6 +8,7 @@ import os
 import sublime
 import sublime_plugin
 from . import GitTextCommand, GitWindowCommand, plugin_file, view_contents, _make_text_safeish
+from . import git_root
 from .add import GitAddSelectedHunkCommand

 history = []
@@ -106,6 +107,19 @@ class GitCommitCommand(GitWindowCommand):
         if not len(self.lines):
             self.lines = ["", ""]

+        if self.get_working_dir():
+            git_root_dir = git_root(self.get_working_dir())
+        else:
+            git_root_dir = self.active_view().settings().get("git_root_dir")
+
+        if git_root_dir:
+            prepareCommitFilepath = os.path.join(git_root_dir, '.git', 'hooks', 'SublimeTextGit-prepare-commit-msg')
+            if os.path.exists(prepareCommitFilepath):
+                with open(prepareCommitFilepath, "r") as fin:
+                    self.lines = fin.read().split('\n')
+                    self.lines.extend([""])
+
+
         self.lines.extend(map(format, history[:historySize]))
         self.lines.extend([
             "# --------------",

My example .git/hooks/SublimeTextGit-prepare-commit-msg


# BUG: 1234

# FIXED-IN: 5.XX.X

# Differential Revision: https://phabricator.kde.org/D1234

I'm not sure if this is a feature you'd consider merging (and I've no idea what the proper filepath should be). I didn't see anyone ask for this feature however so it might be easier to not add it.