Skip to content

Commit

Permalink
correctly translate paths when vault inside repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Jun 21, 2024
1 parent bbb849e commit d8a2a78
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
18 changes: 4 additions & 14 deletions src/gitManager/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,28 +123,18 @@ export abstract class GitManager {
//
// @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_str: string,
filePath: string,
doConversion: boolean = true
): string {
if (doConversion) {
if (
Platform.isMobileApp &&
this.plugin.settings.basePath.length > 0
) {
if (this.plugin.settings.basePath.length > 0) {
//Expect the case that the git repository is located inside the vault on mobile platform currently.
return path_str.substring(
return filePath.substring(
this.plugin.settings.basePath.length + 1
);
}
const adapter = this.plugin.app.vault.adapter;
if (adapter instanceof FileSystemAdapter) {
const folder = adapter.getBasePath();
const from = path.join(folder, this.plugin.settings.basePath);
const to = path.join(folder, path_str);
return path.relative(from, to);
}
}
return path_str;
return filePath;
}

private _getTreeStructure<T = DiffFile | FileStatusResult>(
Expand Down
50 changes: 46 additions & 4 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

0 comments on commit d8a2a78

Please sign in to comment.