Skip to content

Commit 171693f

Browse files
fix: support vault in subdirectory of git repo (#722)
* 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>
1 parent ed31553 commit 171693f

File tree

2 files changed

+57
-9
lines changed

2 files changed

+57
-9
lines changed

src/gitManager/gitManager.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,19 @@ export abstract class GitManager {
121121
// Constructs a path relative to the git repository from a path relative to the vault
122122
//
123123
// @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.
124-
getRelativeRepoPath(path: string, doConversion: boolean = true): string {
124+
getRelativeRepoPath(
125+
filePath: string,
126+
doConversion: boolean = true
127+
): string {
125128
if (doConversion) {
126129
if (this.plugin.settings.basePath.length > 0) {
127-
return path.substring(this.plugin.settings.basePath.length + 1);
130+
//Expect the case that the git repository is located inside the vault on mobile platform currently.
131+
return filePath.substring(
132+
this.plugin.settings.basePath.length + 1
133+
);
128134
}
129135
}
130-
return path;
136+
return filePath;
131137
}
132138

133139
private _getTreeStructure<T = DiffFile | FileStatusResult>(

src/gitManager/simpleGit.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { spawnSync } from "child_process";
22
import debug from "debug";
3-
import { FileSystemAdapter, normalizePath, Notice } from "obsidian";
3+
import { FileSystemAdapter, normalizePath, Notice, Platform } from "obsidian";
44
import * as path from "path";
55
import { sep, resolve } from "path";
66
import simpleGit, * as simple from "simple-git";
@@ -21,27 +21,32 @@ import { GitManager } from "./gitManager";
2121

2222
export class SimpleGit extends GitManager {
2323
git: simple.SimpleGit;
24+
absoluteRepoPath: string;
2425
constructor(plugin: ObsidianGit) {
2526
super(plugin);
2627
}
2728

2829
async setGitInstance(ignoreError = false): Promise<void> {
2930
if (this.isGitInstalled()) {
3031
const adapter = this.app.vault.adapter as FileSystemAdapter;
31-
const path = adapter.getBasePath();
32-
let basePath = path;
32+
const vaultBasePath = adapter.getBasePath();
33+
let basePath = vaultBasePath;
3334
// Because the basePath setting is a relative path, a leading `/` must
3435
// be appended before concatenating with the path.
3536
if (this.plugin.settings.basePath) {
3637
const exists = await adapter.exists(
3738
normalizePath(this.plugin.settings.basePath)
3839
);
3940
if (exists) {
40-
basePath = path + sep + this.plugin.settings.basePath;
41+
basePath = path.join(
42+
vaultBasePath,
43+
this.plugin.settings.basePath
44+
);
4145
} else if (!ignoreError) {
4246
new Notice("ObsidianGit: Base path does not exist");
4347
}
4448
}
49+
this.absoluteRepoPath = basePath;
4550

4651
this.git = simpleGit({
4752
baseDir: basePath,
@@ -69,11 +74,48 @@ export class SimpleGit extends GitManager {
6974
// in case git resides in a different filesystem (eg, WSL)
7075
const relativeRoot = await this.git.revparse("--show-cdup");
7176
const absoluteRoot = resolve(basePath + sep + relativeRoot);
77+
78+
this.absoluteRepoPath = absoluteRoot;
7279
await this.git.cwd(absoluteRoot);
7380
}
7481
}
7582
}
7683

84+
// Constructs a path relative to the vault from a path relative to the git repository
85+
getRelativeVaultPath(filePath: string): string {
86+
const adapter = this.app.vault.adapter as FileSystemAdapter;
87+
const from = adapter.getBasePath();
88+
89+
const to = path.join(this.absoluteRepoPath, filePath);
90+
91+
let res = path.relative(from, to);
92+
if (Platform.isWin) {
93+
res = res.replace(/\\/g, "/");
94+
}
95+
return res;
96+
}
97+
98+
// Constructs a path relative to the git repository from a path relative to the vault
99+
//
100+
// @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.
101+
getRelativeRepoPath(
102+
filePath: string,
103+
doConversion: boolean = true
104+
): string {
105+
if (doConversion) {
106+
const adapter = this.plugin.app.vault.adapter as FileSystemAdapter;
107+
const vaultPath = adapter.getBasePath();
108+
const from = this.absoluteRepoPath;
109+
const to = path.join(vaultPath, filePath);
110+
let res = path.relative(from, to);
111+
if (Platform.isWin) {
112+
res = res.replace(/\\/g, "/");
113+
}
114+
return res;
115+
}
116+
return filePath;
117+
}
118+
77119
async status(): Promise<Status> {
78120
this.plugin.setState(PluginState.status);
79121
const status = await this.git.status((err) => this.onError(err));
@@ -746,11 +788,11 @@ export class SimpleGit extends GitManager {
746788
}
747789
}
748790

749-
updateGitPath(gitPath: string) {
791+
updateGitPath(_: string) {
750792
this.setGitInstance();
751793
}
752794

753-
updateBasePath(basePath: string) {
795+
updateBasePath(_: string) {
754796
this.setGitInstance(true);
755797
}
756798

0 commit comments

Comments
 (0)