Skip to content

Commit

Permalink
fix: support vault in subdirectory of git repo (#722)
Browse files Browse the repository at this point in the history
* fixed getRelativeRepoPath for the case of vault-inside-repo

* fixed import path

* add fallback for mobile app

* fixed lint & format

* correctly translate paths when vault inside repo

* style: fix lints

---------

Co-authored-by: Vinzent <vinzent03@proton.me>
  • Loading branch information
tomoyanonymous and Vinzent03 authored Jun 22, 2024
1 parent ed31553 commit 171693f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/gitManager/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,19 @@ export abstract class GitManager {
// Constructs a path relative to the git repository from a path relative to the vault
//
// @param doConversion - If false, the path is returned as is. This is added because that parameter is often passed on to functions where this method is called.
getRelativeRepoPath(path: string, doConversion: boolean = true): string {
getRelativeRepoPath(
filePath: string,
doConversion: boolean = true
): string {
if (doConversion) {
if (this.plugin.settings.basePath.length > 0) {
return path.substring(this.plugin.settings.basePath.length + 1);
//Expect the case that the git repository is located inside the vault on mobile platform currently.
return filePath.substring(
this.plugin.settings.basePath.length + 1
);
}
}
return path;
return filePath;
}

private _getTreeStructure<T = DiffFile | FileStatusResult>(
Expand Down
54 changes: 48 additions & 6 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { spawnSync } from "child_process";
import debug from "debug";
import { FileSystemAdapter, normalizePath, Notice } from "obsidian";
import { FileSystemAdapter, normalizePath, Notice, Platform } from "obsidian";
import * as path from "path";
import { sep, resolve } from "path";
import simpleGit, * as simple from "simple-git";
Expand All @@ -21,27 +21,32 @@ import { GitManager } from "./gitManager";

export class SimpleGit extends GitManager {
git: simple.SimpleGit;
absoluteRepoPath: string;
constructor(plugin: ObsidianGit) {
super(plugin);
}

async setGitInstance(ignoreError = false): Promise<void> {
if (this.isGitInstalled()) {
const adapter = this.app.vault.adapter as FileSystemAdapter;
const path = adapter.getBasePath();
let basePath = path;
const vaultBasePath = adapter.getBasePath();
let basePath = vaultBasePath;
// Because the basePath setting is a relative path, a leading `/` must
// be appended before concatenating with the path.
if (this.plugin.settings.basePath) {
const exists = await adapter.exists(
normalizePath(this.plugin.settings.basePath)
);
if (exists) {
basePath = path + sep + this.plugin.settings.basePath;
basePath = path.join(
vaultBasePath,
this.plugin.settings.basePath
);
} else if (!ignoreError) {
new Notice("ObsidianGit: Base path does not exist");
}
}
this.absoluteRepoPath = basePath;

this.git = simpleGit({
baseDir: basePath,
Expand Down Expand Up @@ -69,11 +74,48 @@ export class SimpleGit extends GitManager {
// in case git resides in a different filesystem (eg, WSL)
const relativeRoot = await this.git.revparse("--show-cdup");
const absoluteRoot = resolve(basePath + sep + relativeRoot);

this.absoluteRepoPath = absoluteRoot;
await this.git.cwd(absoluteRoot);
}
}
}

// Constructs a path relative to the vault from a path relative to the git repository
getRelativeVaultPath(filePath: string): string {
const adapter = this.app.vault.adapter as FileSystemAdapter;
const from = adapter.getBasePath();

const to = path.join(this.absoluteRepoPath, filePath);

let res = path.relative(from, to);
if (Platform.isWin) {
res = res.replace(/\\/g, "/");
}
return res;
}

// Constructs a path relative to the git repository from a path relative to the vault
//
// @param doConversion - If false, the path is returned as is. This is added because that parameter is often passed on to functions where this method is called.
getRelativeRepoPath(
filePath: string,
doConversion: boolean = true
): string {
if (doConversion) {
const adapter = this.plugin.app.vault.adapter as FileSystemAdapter;
const vaultPath = adapter.getBasePath();
const from = this.absoluteRepoPath;
const to = path.join(vaultPath, filePath);
let res = path.relative(from, to);
if (Platform.isWin) {
res = res.replace(/\\/g, "/");
}
return res;
}
return filePath;
}

async status(): Promise<Status> {
this.plugin.setState(PluginState.status);
const status = await this.git.status((err) => this.onError(err));
Expand Down Expand Up @@ -746,11 +788,11 @@ export class SimpleGit extends GitManager {
}
}

updateGitPath(gitPath: string) {
updateGitPath(_: string) {
this.setGitInstance();
}

updateBasePath(basePath: string) {
updateBasePath(_: string) {
this.setGitInstance(true);
}

Expand Down

0 comments on commit 171693f

Please sign in to comment.