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
6,948 changes: 6,948 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"svelte-preprocess": "^6.0.3",
"tslib": "^2.8.1",
"typescript": "5.8.3",
"typescript-eslint": "^8.38.0"
"typescript-eslint": "^8.8.1"
},
"dependencies": {
"@codemirror/commands": "^6.8.1",
Expand All @@ -59,6 +59,7 @@
"deep-equal": "^2.2.3",
"diff": "^7.0.0",
"diff2html": "^3.4.52",
"diff3": "^0.0.4",
"isomorphic-git": "^1.32.2",
"js-sha256": "^0.9.0",
"obsidian-community-lib": "github:Vinzent03/obsidian-community-lib#upgrade",
Expand Down
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const DEFAULT_SETTINGS: ObsidianGitSettings = {
showStatusBar: true,
updateSubmodules: false,
syncMethod: "merge",
resolutionMethod: "none",
customMessageOnAutoBackup: false,
autoBackupAfterFileChange: false,
treeStructure: false,
Expand Down
1 change: 1 addition & 0 deletions src/gitManager/gitManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
DiffFile,
FileStatusResult,
LogEntry,
ResolutionMethod,
Status,
TreeItem,
UnstagedFile,
Expand Down
32 changes: 32 additions & 0 deletions src/gitManager/isomorphicGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { GeneralModal } from "../ui/modals/generalModal";
import { splitRemoteBranch, worthWalking } from "../utils";
import { GitManager } from "./gitManager";
import { MyAdapter } from "./myAdapter";
import diff3Merge from "diff3";

export class IsomorphicGit extends GitManager {
private readonly FILE = 0;
Expand Down Expand Up @@ -476,6 +477,37 @@ export class IsomorphicGit extends GitManager {
ours: branchInfo.current,
theirs: branchInfo.tracking!,
abortOnConflict: false,
mergeDriver:
this.plugin.settings.resolutionMethod !== "none"
? ({ contents }) => {
const baseContent = contents[0];
const ourContent = contents[1];
const theirContent = contents[2];

const LINEBREAKS = /^.*(\r?\n|$)/gm;
const ours =
ourContent.match(LINEBREAKS) ?? [];
const base =
baseContent.match(LINEBREAKS) ?? [];
const theirs =
theirContent.match(LINEBREAKS) ?? [];
const result = diff3Merge(ours, base, theirs);
let mergedText = "";
for (const item of result) {
if (item.ok) {
mergedText += item.ok.join("");
}
if (item.conflict) {
mergedText +=
this.plugin.settings
.resolutionMethod === "ours"
? item.conflict.a.join("")
: item.conflict.b.join("");
}
}
return { cleanMerge: true, mergedText };
}
: undefined,
})
);
if (!mergeRes.alreadyMerged) {
Expand Down
16 changes: 14 additions & 2 deletions src/gitManager/simpleGit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,9 @@ export class SimpleGit extends GitManager {
this.plugin.log(
"No tracking branch found. Ignoring pull of main repo and updating submodules only."
);
this.plugin.displayMessage(
"No tracking branch found. Ignoring pull of main repo and updating submodules only."
);
return;
}

Expand All @@ -616,12 +619,20 @@ export class SimpleGit extends GitManager {
this.plugin.settings.syncMethod === "rebase"
) {
try {
const args = [branchInfo.tracking!];

if (this.plugin.settings.resolutionMethod !== "none") {
args.push(
`--strategy-option=${this.plugin.settings.resolutionMethod}`
);
}

switch (this.plugin.settings.syncMethod) {
case "merge":
await this.git.merge([branchInfo.tracking!]);
await this.git.merge(args);
break;
case "rebase":
await this.git.rebase([branchInfo.tracking!]);
await this.git.rebase(args);
}
} catch (err) {
this.plugin.displayError(
Expand All @@ -643,6 +654,7 @@ export class SimpleGit extends GitManager {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
`Sync failed (${this.plugin.settings.syncMethod}): ${"message" in err ? err.message : err}`
);
err = true;
}
}
this.app.workspace.trigger("obsidian-git:head-change");
Expand Down
4 changes: 0 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1074,10 +1074,6 @@ export default class ObsidianGit extends Plugin {
}
}

/** Used for internals
Returns whether the pull added a commit or not.

See {@link pullChangesFromRemote} for the command version. */
async pull(): Promise<false | number> {
if (!(await this.remotesAreSet())) {
return false;
Expand Down
21 changes: 21 additions & 0 deletions src/setting/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
import type ObsidianGit from "src/main";
import type {
ObsidianGitSettings,
ResolutionMethod,
ShowAuthorInHistoryView,
SyncMethod,
} from "src/types";
Expand Down Expand Up @@ -398,6 +399,26 @@ export class ObsidianGitSettingsTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName("Conflict resolution strategy")
.setDesc(
"Decide how to solve conflicts when pulling remote changes"
)
.addDropdown((dropdown) => {
const options: Record<ResolutionMethod, string> = {
none: "None (git default)",
ours: "Our changes",
theirs: "Their changes",
};
dropdown.addOptions(options);
dropdown.setValue(plugin.settings.resolutionMethod);

dropdown.onChange(async (option: ResolutionMethod) => {
plugin.settings.resolutionMethod = option;
await plugin.saveSettings();
});
});

new Setting(containerEl)
.setName("Pull on startup")
.setDesc("Automatically pull commits when Obsidian starts.")
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface ObsidianGitSettings {
autoPullOnBoot: boolean;
autoCommitOnlyStaged: boolean;
syncMethod: SyncMethod;
resolutionMethod: ResolutionMethod;
/**
* Whether to push on commit-and-sync
*/
Expand Down Expand Up @@ -80,6 +81,8 @@ export function mergeSettingsByPriority(

export type SyncMethod = "rebase" | "merge" | "reset";

export type ResolutionMethod = "none" | "ours" | "theirs";

export type ShowAuthorInHistoryView = "full" | "initials" | "hide";

export interface Author {
Expand Down