1
1
import { Errors } from "isomorphic-git" ;
2
- import * as path from "path" ;
3
2
import type { Debouncer , Menu , TAbstractFile , WorkspaceLeaf } from "obsidian" ;
4
3
import {
5
4
debounce ,
@@ -10,7 +9,9 @@ import {
10
9
Platform ,
11
10
Plugin ,
12
11
TFile ,
12
+ TFolder ,
13
13
} from "obsidian" ;
14
+ import * as path from "path" ;
14
15
import { LineAuthoringFeature } from "src/lineAuthor/lineAuthorIntegration" ;
15
16
import { pluginRef } from "src/pluginGlobalRef" ;
16
17
import { PromiseQueue } from "src/promiseQueue" ;
@@ -24,13 +25,14 @@ import {
24
25
DEFAULT_SETTINGS ,
25
26
DIFF_VIEW_CONFIG ,
26
27
HISTORY_VIEW_CONFIG ,
27
- SPLIT_DIFF_VIEW_CONFIG ,
28
28
SOURCE_CONTROL_VIEW_CONFIG ,
29
+ SPLIT_DIFF_VIEW_CONFIG ,
29
30
} from "./constants" ;
30
31
import type { GitManager } from "./gitManager/gitManager" ;
31
32
import { IsomorphicGit } from "./gitManager/isomorphicGit" ;
32
33
import { SimpleGit } from "./gitManager/simpleGit" ;
33
34
import { LocalStorageSettings } from "./setting/localStorageSettings" ;
35
+ import Tools from "./tools" ;
34
36
import type {
35
37
FileStatusResult ,
36
38
ObsidianGitSettings ,
@@ -39,19 +41,22 @@ import type {
39
41
UnstagedFile ,
40
42
} from "./types" ;
41
43
import {
44
+ CurrentGitAction ,
42
45
mergeSettingsByPriority ,
43
46
NoNetworkError ,
44
- CurrentGitAction ,
45
47
} from "./types" ;
46
48
import DiffView from "./ui/diff/diffView" ;
49
+ import SplitDiffView from "./ui/diff/splitDiffView" ;
47
50
import HistoryView from "./ui/history/historyView" ;
48
51
import { BranchModal } from "./ui/modals/branchModal" ;
49
52
import { GeneralModal } from "./ui/modals/generalModal" ;
50
53
import GitView from "./ui/sourceControl/sourceControl" ;
51
54
import { BranchStatusBar } from "./ui/statusBar/branchStatusBar" ;
52
- import { formatRemoteUrl , splitRemoteBranch } from "./utils" ;
53
- import Tools from "./tools" ;
54
- import SplitDiffView from "./ui/diff/splitDiffView" ;
55
+ import {
56
+ convertPathToAbsoluteGitignoreRule ,
57
+ formatRemoteUrl ,
58
+ splitRemoteBranch ,
59
+ } from "./utils" ;
55
60
56
61
export default class ObsidianGit extends Plugin {
57
62
gitManager : GitManager ;
@@ -319,10 +324,22 @@ export default class ObsidianGit extends Plugin {
319
324
) ;
320
325
}
321
326
322
- async addFileToGitignore ( filePath : string ) : Promise < void > {
327
+ async addFileToGitignore (
328
+ filePath : string ,
329
+ isFolder ?: boolean
330
+ ) : Promise < void > {
331
+ const gitRelativePath = this . gitManager . getRelativeRepoPath (
332
+ filePath ,
333
+ true
334
+ ) ;
335
+ // Define an absolute rule that can apply only for this item.
336
+ const gitignoreRule = convertPathToAbsoluteGitignoreRule ( {
337
+ isFolder,
338
+ gitRelativePath,
339
+ } ) ;
323
340
await this . app . vault . adapter . append (
324
341
this . gitManager . getRelativeVaultPath ( ".gitignore" ) ,
325
- "\n" + this . gitManager . getRelativeRepoPath ( filePath , true )
342
+ "\n" + gitignoreRule
326
343
) ;
327
344
return this . refresh ( ) ;
328
345
}
@@ -389,9 +406,10 @@ export default class ObsidianGit extends Plugin {
389
406
. setIcon ( "file-x" )
390
407
. setSection ( "action" )
391
408
. onClick ( ( _ ) => {
392
- this . addFileToGitignore ( filePath ) . catch ( ( e ) =>
393
- this . displayError ( e )
394
- ) ;
409
+ this . addFileToGitignore (
410
+ filePath ,
411
+ file instanceof TFolder
412
+ ) . catch ( ( e ) => this . displayError ( e ) ) ;
395
413
} ) ;
396
414
} ) ;
397
415
}
@@ -402,9 +420,10 @@ export default class ObsidianGit extends Plugin {
402
420
. setIcon ( "file-x" )
403
421
. setSection ( "action" )
404
422
. onClick ( ( _ ) => {
405
- this . addFileToGitignore ( filePath ) . catch ( ( e ) =>
406
- this . displayError ( e )
407
- ) ;
423
+ this . addFileToGitignore (
424
+ filePath ,
425
+ file instanceof TFolder
426
+ ) . catch ( ( e ) => this . displayError ( e ) ) ;
408
427
} ) ;
409
428
} ) ;
410
429
const gitManager = this . app . vault . adapter ;
0 commit comments