Skip to content

Commit

Permalink
add support for Mercurial
Browse files Browse the repository at this point in the history
  • Loading branch information
sonnyp committed Apr 23, 2021
1 parent b8b7582 commit a0d0d7a
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ jobs:
# Requires display server
# Unable to init server: Could not connect: Connection refused
# gtk-builder-tool validate src/window.ui
gjs -m src/git.test.js
gjs -m src/scm.test.js
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ bundle:
test:
./node_modules/.bin/eslint --cache .
flatpak run org.freedesktop.appstream-glib validate data/re.sonny.Commit.appdata.xml
desktop-file-validate data/re.sonny.Commit.desktop
desktop-file-validate --no-hints data/re.sonny.Commit.desktop
gtk-builder-tool validate src/window.ui
gjs -m src/git.test.js
gjs -m src/scm.test.js
30 changes: 25 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<img style="vertical-align: middle;" src="data/icons/re.sonny.Commit.svg" width="120" height="120">

Git commit message editor
Commit message editor

# Commit

Helps you write better Git commit messages.
Commit is an editor that helps you write better Git and Mercurial commit messages.

<!-- <a href='https://flathub.org/apps/details/re.sonny.Commit'><img width='180' height='60' alt='Download on Flathub' src='https://flathub.org/assets/badges/flathub-badge-en.svg'/></a> -->

Expand All @@ -18,9 +18,29 @@ Then run

```sh
flatpak install re.sonny.Commit
```

### Git

To set Commit as default editor for Git run the following command

```sh
git config --global core.editor "flatpak run re.sonny.Commit"
```

See also https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration#_core_editor

### Mercurial

To set commit as default editor for Mercurial, set the following in your `hgrc`

```ini
[ui]
editor=flatpak run re.sonny.Commit
```

See also https://www.mercurial-scm.org/wiki/editor

## Usage

Commit will pop up automatically when you make a commit in one of your projects.
Expand All @@ -34,12 +54,12 @@ To abort and dismiss Commit, press the Cancel button or the _Escape_ key.
- Highlights overflow of subject line when it exceeds 69 characters
- Inserts empty line between subject line and rest of message
- Spell checking
- Select All selects only your commit message, not the Git commit comment
- Comments are readonly and excluded from "Select All"
- Displays project folder and branch in window header
- Git Commit comment is not editable
- Dark theme support: the overflow highlight is adjusted according to your theme
- Supports git commit messages, merge messages, tag messages, add -p messages, and rebase -i messages
- Auto inserts blank line for commit description
- Supports git commit messages, merge messages, tag messages, add -p messages, and rebase -i messages
- Supports Mercurial commit messages

## Development

Expand Down
9 changes: 5 additions & 4 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
1. [x] "#" support in commit message
2. [x] unit tests
3. [ ] Mercurial support
3. [x] Mercurial support
4. [ ] Launch and show instructions
5. [ ] Auto configure
6. [ ] length hint should be a setting
7. [ ] conventional commit? https://www.conventionalcommits.org/en/v1.0.0/
8. [ ] GTK 4 - depends on https://gitlab.gnome.org/GNOME/gspell/-/issues/17
7. [ ] Commit button should say "Save" if type isn't recognize (editing a regular file) and set title to filename
8. [ ] conventional commit? https://www.conventionalcommits.org/en/v1.0.0/
9. [ ] GTK 4 - depends on https://gitlab.gnome.org/GNOME/gspell/-/issues/17
1. [ ] undo/redo support depends on GTK 4 where GTKTextView natively supports it
2. [ ] emoji autocomplete - gitmoji shortcuts? https://gitlab.gnome.org/GNOME/gtk/-/issues/86
9. Cancel / esc does not abort rebase
10. Cancel / esc does not abort rebase or git commit amend
19 changes: 12 additions & 7 deletions data/re.sonny.Commit.appdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
<update_contact>sonny@fastmail.net</update_contact>
<metadata_license>CC0-1.0</metadata_license>
<project_license>GPL-3.0-or-later</project_license>
<summary>Git commit message editor</summary>
<summary>Commit message editor</summary>
<description>
<p>Helps you write better Git commit messages.</p>
<p
>Commit is an editor that helps you write better Git and Mercurial commit messages.</p>
<p>Features:</p>
<ul>
<li>Highlights overflow of subject line when it exceeds 69 characters</li>
<li>Inserts empty line between subject line and rest of message</li>
<li>Spell checking</li>
<li
>Select All selects only your commit message, not the Git commit comment</li>
<li>Comments are readonly and excluded from "Select All"</li>
<li>Displays project folder and branch in window header</li>
<li>Git Commit comment is not editable</li>
<li
>Dark theme support: the overflow highlight is adjusted according to your theme</li>
<li>Auto inserts blank line for commit description</li>
<li
>Supports git commit messages, merge messages, tag messages, git add -p messages, and rebase -i messages</li>
<li>Auto inserts blank line for commit description</li>
<li>Supports Mercurial commit messages</li>
</ul>
</description>
<url type="homepage">https://github.com/sonnyp/Commit</url>
Expand All @@ -42,6 +42,11 @@
</screenshots>
<content_rating type="oars-1.1" />
<releases>
<release version="1.1.0" date="2021-04-23">
<description>
<p>Add support for Mercurial</p>
</description>
</release>
<release version="1.0.0" date="2021-04-06">
<description>
<p>Initial release. Forked from Gnomit 2.0.</p>
Expand All @@ -50,7 +55,7 @@
<li>Properly recognize executable path</li>
</ul>
</description>
</release>
</release>
</releases>
<kudos>
<kudo>HiDpiIcon</kudo>
Expand Down
2 changes: 1 addition & 1 deletion data/re.sonny.Commit.desktop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Desktop Entry]
Name=Commit
Comment=Helps you write better Git commit messages.
Comment=Commit message editor
Exec=re.sonny.Commit
Terminal=false
Type=Application
Expand Down
36 changes: 19 additions & 17 deletions src/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ import Gspell from "gi://Gspell";
import Window from "./window.js";
import showHelp from "./showHelp.js";
import validateCommitButton from "./validateCommitButton.js";
import { getType, parse } from "./git.js";
import { getType, parse } from "./scm.js";

const ByteArray = imports.byteArray;

const SUMMARY = `
Helps you write better Git commit messages.
To use, configure Git to use Commit as the default editor:
Helps you write better commit messages.
To use with Git, set Commit as the default editor:
git config --global core.editor "flatpak run re.sonny.Commit"
To use with Mercurial (hg), set the following in your ~/.hgrc
[ui]
editor=flatpak run re.sonny.Commit
`.trim();

const HIGHLIGHT_BACKGROUND_TAG_NAME = "highlightBackground";
Expand Down Expand Up @@ -52,7 +55,7 @@ export default function Application({ version }) {
// The option context parameter string is displayed next to the
// list of options on the first line of the --help screen.
application.set_option_context_parameter_string(
"<path-to-git-commit-message-file>",
"<path-to-commit-message-file>",
);

// The option context summary is displayed above the set of options
Expand Down Expand Up @@ -86,7 +89,7 @@ export default function Application({ version }) {
});

// Open gets called when a file is passed as a command=line argument.
// We expect Git to pass us one file.
// We expect Git or Mercurial to pass us one file.
application.connect("open", (self, files, hint) => {
if (files.length !== 1) {
// Error: Too many files.
Expand All @@ -99,7 +102,7 @@ export default function Application({ version }) {

// Try to load the commit message contents.
const ERROR_SUMMARY =
"\n\nError: Could not read the Git commit message file.\n\n";
"\n\nError: Could not read the commit message file.\n\n";

let success = false,
commitMessage = "",
Expand All @@ -124,7 +127,7 @@ export default function Application({ version }) {
// This should not happen.
if (!type) {
print(
`Warning: unknown Git commit type encountered in: ${application.commitMessageFilePath}`,
`Warning: unknown commit type encountered in: ${application.commitMessageFilePath}`,
);
}

Expand All @@ -136,17 +139,16 @@ export default function Application({ version }) {
const commitCommentLines = commitComment.split("\n");
application.numberOfLinesInCommitComment = commitCommentLines.length;

// The commit message is always in the .git directory in the
// project directory. Get the project directory’s name by using this.
const pathComponents = application.commitMessageFilePath.split("/");
const projectDirectoryName =
pathComponents[pathComponents.indexOf(".git") - 1];

// Set the title.
application.active_window.set_title(
`Git ${type}: ${projectDirectoryName} (${detail})`,
const projectDirectoryName = GLib.path_get_basename(
GLib.get_current_dir(),
);

if (type) {
application.active_window.set_title(
`${type}: ${projectDirectoryName} (${detail})`,
);
}

// Add Pango markup to make the commented are appear lighter.
commitMessage = `${commitBody}<span foreground="#959595">\n${commitComment}</span>`;

Expand Down
1 change: 1 addition & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default function main(argv, { version }) {
log("argv " + argv.join(" "));
log(`programInvocationName: ${programInvocationName}`);
log(`_: ${GLib.getenv("_")}`);
log(`PWD: ${GLib.get_current_dir()}`);

const restart = new Gio.SimpleAction({
name: "restart",
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pkgdatadir = join_paths(get_option('datadir'), meson.project_name())
gnome = import('gnome')

install_data(
['application.js', 'git.js', 'main.js', 'setup.js', 'showHelp.js', 'validateCommitButton.js', 'window.js', 'window.ui'],
['application.js', 'scm.js', 'main.js', 'setup.js', 'showHelp.js', 'validateCommitButton.js', 'window.js', 'window.ui'],
install_dir : pkgdatadir
)

Expand Down
42 changes: 32 additions & 10 deletions src/git.js → src/scm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export function parse(commit, type) {
let body;
let detail;
let comment_prefix = "#";

if (type === "hg") {
comment_prefix = "HG:";
}

const comment_separator = `\n${comment_prefix}`;

// If this is a git add -p hunk edit message, then we cannot
// split at the first comment as the message starts with a comment.
Expand All @@ -11,23 +18,18 @@ export function parse(commit, type) {
}

// Split the message into the commit body and comment
const firstCommentIndex = commit.indexOf("\n#");
const firstCommentIndex = commit.indexOf(comment_separator);
body = commit.slice(0, firstCommentIndex).trimEnd();
const comment = commit.slice(firstCommentIndex);

// Trim any newlines there may be at the end of the commit body
body = body.trimEnd();

const commentLines = comment.split("\n");
if (type === "commit") {
// Try to get the branch name via a method that relies on
// positional aspect of the branch name so it should work with
// other languages.
// FIXME: I guess we could bundle and use git instead see gitg for flatpak
// or https://wiki.gnome.org/Projects/Libgit2-glib
const wordsOnBranchLine = commentLines[4].split(" ");
const branchName = wordsOnBranchLine[wordsOnBranchLine.length - 1];
detail = branchName;
if (type === "hg") {
detail = getMercurialBranch(commentLines);
} else if (type === "commit") {
detail = getGitBranch(commentLines);
} else if (type === "merge") {
// Display the branch name
detail = `branch ${body.split("'")[1]}`;
Expand All @@ -45,10 +47,30 @@ export function parse(commit, type) {
}

export function getType(filename) {
// Git
if (filename.endsWith("COMMIT_EDITMSG")) return "commit";
if (filename.endsWith("MERGE_MSG")) return "merge";
if (filename.endsWith("TAG_EDITMSG")) return "tag";
if (filename.endsWith("addp-hunk-edit.diff")) return "add -p";
if (filename.endsWith("rebase-merge/git-rebase-todo")) return "rebase";
// Mercurial
if (filename.endsWith(".commit.hg.txt")) return "hg";
// Unknown
return null;
}

export function getMercurialBranch(commentLines) {
// Try to get the branch name via a method that relies on
// positional aspect of the branch name so it should work with
// other languages.
const wordsOnBranchLine = commentLines[5].split(" ");
return wordsOnBranchLine[wordsOnBranchLine.length - 1].split("'")[1];
}

export function getGitBranch(commentLines) {
// Try to get the branch name via a method that relies on
// positional aspect of the branch name so it should work with
// other languages.
const wordsOnBranchLine = commentLines[4].split(" ");
return wordsOnBranchLine[wordsOnBranchLine.length - 1];
}
42 changes: 41 additions & 1 deletion src/git.test.js → src/scm.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Gio from "gi://Gio";
import { parse, getType } from "./git.js";
import { parse, getType } from "./scm.js";

const { byteArray } = imports;

Expand Down Expand Up @@ -27,6 +27,7 @@ is(getType("/foo/bar/COMMIT_EDITMSG"), "commit");
is(getType("/foo/bar/rebase-merge/git-rebase-todo"), "rebase");
is(getType("/foo/bar/MERGE_MSG"), "merge");
is(getType("/foo/bar/TAG_EDITMSG"), "tag");
is(getType("/foo/bar/hg-editor-foo.commit.hg.txt"), "hg");

function readTest(name) {
const file = Gio.File.new_for_uri(import.meta.url);
Expand Down Expand Up @@ -185,3 +186,42 @@ is(
# 1.0.0
# Lines starting with '#' will be ignored.`,
);

is(parse(readTest("hg-editor-without_body.commit.hg.txt"), "hg").body, ``);
is(
parse(readTest("hg-editor-without_body.commit.hg.txt"), "hg").detail,
"default",
);
is(
parse(readTest("hg-editor-without_body.commit.hg.txt"), "hg").comment,
`
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: Sonny Piers <sonny@fastmail.net>
HG: branch 'default'
HG: added foobar
`,
);

is(
parse(readTest("hg-editor-with_body.commit.hg.txt"), "hg").body,
`foo this is great
hello`,
);
is(
parse(readTest("hg-editor-with_body.commit.hg.txt"), "hg").detail,
"default",
);
is(
parse(readTest("hg-editor-with_body.commit.hg.txt"), "hg").comment,
`
HG: Enter commit message. Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: Sonny Piers <sonny@fastmail.net>
HG: branch 'default'
HG: added foobar
`,
);
1 change: 1 addition & 0 deletions src/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<object class="GtkHeaderBar">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="title">Commit</property>
<child>
<object class="GtkButton" id="cancelButton">
<property name="label" translatable="yes">Cancel</property>
Expand Down

0 comments on commit a0d0d7a

Please sign in to comment.