Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 5.5.0
- Feature: `mob next` uses the file and line you were last looking at in your editor, if an editor plugin (e.g. the IntelliJ mob last file plugin) wrote `<git dir>/mob/last-file.json`. Without a plugin, mob falls back to the last modified file as before. The line is stored as `lastLine:` below `lastFile:` in the wip commit message, which older mob versions ignore.
- Feature: `MOB_OPEN_COMMAND` supports the named placeholders `%{file}` and `%{line}`, e.g. `code -g %{file}:%{line}` or `idea --line %{line} %{file}`. `%s` keeps working.

# 5.4.4
- Feature: Release a Linux arm64 build (`mob_<version>_linux_arm64.tar.gz`). The install script and the AUR package `mobsh-bin` pick it up automatically on arm64 machines.

Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,51 @@ Mob supports you and can automate this step. You just need the configuration opt

For example if you want use IntelliJ the configuration option would look like this: `MOB_OPEN_COMMAND="idea %s"`

`%s` is replaced by the file. If you also want to jump to the line the previous typist was looking at, use the named placeholders `%{file}` and `%{line}`:

```bash
MOB_OPEN_COMMAND="idea --line %{line} %{file}" # IntelliJ / JetBrains IDEs
MOB_OPEN_COMMAND="code -g %{file}:%{line}" # VS Code
```

`%{line}` is `1` if the previous typist's commit contains no line. Named placeholders require mob 5.5.0 or newer; `%s` and commands without placeholders keep working as before.

#### Use the file and line you were looking at in your editor

By default, `mob next` remembers the file you modified last. With an editor plugin installed (e.g. the [IntelliJ mob last file plugin](https://github.com/remotemobprogramming/intellij-mob-last-file-plugin)), `mob next` instead remembers the file and line you were last looking at in your editor. No configuration is needed: if the plugin wrote a current editor state file, mob uses it; otherwise it falls back to the last modified file. The commit message stays readable for every mob version and editor:

```
mob next [ci-skip] [ci skip] [skip ci]

lastFile:src/main/java/Foo.java
lastLine:42
```

Teammates without a plugin, with another editor or with an older mob version can still open `lastFile`; `lastLine` is simply ignored by older versions.

#### Editor plugins: the editor state file

Any editor plugin can provide the last file by writing `<git dir>/mob/last-file.json` (the directory reported by `git rev-parse --absolute-git-dir`, so it is never committed and works per worktree):

```json
{
"version": 1,
"file": "src/main/java/Foo.java",
"line": 42,
"updatedAt": "2026-11-18T10:15:03Z",
"editor": "IntelliJ IDEA 2026.2",
"plugin": "Mob Last File",
"pluginVersion": "0.1.0"
}
```

- `file`: path relative to the git root, using `/` as separator.
- `line`: 1-based line number; optional.
- `updatedAt`: RFC 3339 timestamp of when the file was written.
- `editor`, `plugin`, `pluginVersion`: name and version of the editor and of the plugin which wrote the file; optional and informational only, shown in `mob --debug`.

`mob next` uses the file only if `version` is `1`, `updatedAt` is newer than the last commit, and `file` exists inside the repository. Plugins should update the file when the active file or line changes and when the editor loses focus, and may delete it when the project is closed. If clocks of your team's machines differ a lot, mob may ignore a fresh state file and fall back to the last modified file.

## More on Installation

### Known Issues
Expand Down
79 changes: 79 additions & 0 deletions lastfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"strconv"
"strings"

"github.com/remotemobprogramming/mob/v5/say"
)

const (
lastFileMarker = "lastFile:"
lastLineMarker = "lastLine:"
)

// lastFile describes the file (and optionally the line) which is written into the wip commit message.
type lastFile struct {
Path string // relative to the git root, as written into the commit message (may be quoted)
Line int // 1-based; 0 means unknown
}

// lastFileSource is one way of determining the last file of the current typist.
type lastFileSource interface {
name() string
find() (lastFile, bool)
}

// lastFileSources are tried in order; the first one that finds a file wins.
// Implementations: lastfile_editorstate.go, lastfile_modified.go
var lastFileSources = []lastFileSource{
editorStateFileSource{},
lastModifiedFileSource{},
}

func determineLastFile() lastFile {
for _, source := range lastFileSources {
if result, found := source.find(); found {
say.Debug("Determined last file using " + source.name() + ": " + result.Path)
return result
}
say.Debug("No last file found using " + source.name())
}
return lastFile{}
}

func appendLastFile(commitMessage string, file lastFile) string {
if file.Path == "" {
return commitMessage
}
commitMessage += "\n\n" + lastFileMarker + file.Path
if file.Line > 0 {
commitMessage += "\n" + lastLineMarker + strconv.Itoa(file.Line)
}
return commitMessage
}

// parseLastFileFromCommitMessage extracts the lastFile and the optional lastLine from a wip commit message.
// The returned path is unquoted and relative to the git root.
func parseLastFileFromCommitMessage(message string) (path string, line int, warning string) {
split := strings.Split(message, lastFileMarker)
if len(split) == 1 {
return "", 0, "Couldn't find last modified file in commit message!"
}
if len(split) > 2 {
return "", 0, "Could not determine last modified file from commit message, separator was used multiple times!"
}
path = strings.Split(split[1], "\n")[0]
if strings.HasPrefix(path, "\"") {
path, _ = strconv.Unquote(path)
}
for _, messageLine := range strings.Split(message, "\n") {
if value, found := strings.CutPrefix(strings.TrimSpace(messageLine), lastLineMarker); found {
if parsed, err := strconv.Atoi(strings.TrimSpace(value)); err == nil && parsed > 0 {
line = parsed
}
break
}
}
return path, line, ""
}
100 changes: 100 additions & 0 deletions lastfile_editorstate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package main

import (
"encoding/json"
"os"
"path/filepath"
"strconv"
"strings"
"time"

"github.com/remotemobprogramming/mob/v5/say"
)

const (
// editorStateFile is written by editor plugins (e.g. the IntelliJ mob last file plugin)
// into the git dir of the repository. See README "Editor plugins: the editor state file".
editorStateFile = "mob/last-file.json"
editorStateFormatVersion = 1
)

// editorStateFileSource reads the file an editor plugin wrote into <git dir>/mob/last-file.json.
type editorStateFileSource struct{}

type editorState struct {
Version int `json:"version"`
File string `json:"file"`
Line int `json:"line"`
UpdatedAt time.Time `json:"updatedAt"`
// informational only
Editor string `json:"editor"`
Plugin string `json:"plugin"`
PluginVersion string `json:"pluginVersion"`
}

func (editorStateFileSource) name() string { return "editor state file" }

func (editorStateFileSource) find() (lastFile, bool) {
path := filepath.Join(gitDir(), filepath.FromSlash(editorStateFile))
content, err := os.ReadFile(path)
if err != nil {
say.Debug("No editor state file found (" + path + ")")
return lastFile{}, false
}
var state editorState
if err := json.Unmarshal(content, &state); err != nil {
say.Debug("Ignoring editor state file, it is not valid JSON: " + err.Error())
return lastFile{}, false
}
if reason := validateEditorState(state, gitRootDir(), lastCommitTime()); reason != "" {
say.Debug("Ignoring editor state file: " + reason)
return lastFile{}, false
}
say.Debug("Using editor state file written by plugin '" + state.Plugin + "' " + state.PluginVersion + " in '" + state.Editor + "' at " + state.UpdatedAt.String())
line := state.Line
if line < 1 {
line = 0
}
return lastFile{Path: quotePathIfNeeded(state.File), Line: line}, true
}

// validateEditorState returns an empty string if the state is usable, otherwise the reason why it is not.
func validateEditorState(state editorState, rootDir string, lastCommit time.Time) string {
if state.Version != editorStateFormatVersion {
return "unsupported version " + strconv.Itoa(state.Version)
}
if state.File == "" {
return "no file given"
}
localPath := filepath.FromSlash(state.File)
if !filepath.IsLocal(localPath) {
return "file '" + state.File + "' is not a relative path inside the repository"
}
if !state.UpdatedAt.After(lastCommit) {
return "it was written (" + state.UpdatedAt.String() + ") before the last commit (" + lastCommit.String() + ")"
}
info, err := os.Stat(filepath.Join(rootDir, localPath))
if err != nil || !info.Mode().IsRegular() {
return "file '" + state.File + "' does not exist in the repository"
}
return ""
}

func lastCommitTime() time.Time {
seconds, err := strconv.ParseInt(strings.TrimSpace(silentgit("log", "-1", "--format=%ct")), 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(seconds, 0)
}

// quotePathIfNeeded quotes paths the same way 'git status --porcelain' does for paths containing
// spaces or special characters, so every mob version can read them back via strconv.Unquote.
func quotePathIfNeeded(path string) string {
for _, r := range path {
if r == ' ' || r == '"' || r == '\\' || r < 0x20 || r == 0x7f {
return strconv.Quote(path)
}
}
return path
}
Loading
Loading