1
1
import { App } from "obsidian" ;
2
2
import ObsidianGit from "./main" ;
3
- import { BranchInfo , FileStatusResult , Status , TreeItem , UnstagedFile } from "./types" ;
4
-
3
+ import {
4
+ BranchInfo ,
5
+ FileStatusResult ,
6
+ Status ,
7
+ TreeItem ,
8
+ UnstagedFile ,
9
+ } from "./types" ;
5
10
6
11
export abstract class GitManager {
7
12
readonly plugin : ObsidianGit ;
@@ -13,29 +18,35 @@ export abstract class GitManager {
13
18
14
19
abstract status ( ) : Promise < Status > ;
15
20
16
- abstract commitAll ( _ : { message : string , status ?: Status , unstagedFiles ?: UnstagedFile [ ] ; } ) : Promise < number | undefined > ;
21
+ abstract commitAll ( _ : {
22
+ message : string ;
23
+ status ?: Status ;
24
+ unstagedFiles ?: UnstagedFile [ ] ;
25
+ } ) : Promise < number | undefined > ;
17
26
18
27
abstract commit ( message ?: string ) : Promise < number | undefined > ;
19
28
20
- abstract stageAll ( _ : { dir ?: string , status ?: Status ; } ) : Promise < void > ;
29
+ abstract stageAll ( _ : { dir ?: string ; status ?: Status } ) : Promise < void > ;
21
30
22
- abstract unstageAll ( _ : { dir ?: string , status ?: Status ; } ) : Promise < void > ;
31
+ abstract unstageAll ( _ : { dir ?: string ; status ?: Status } ) : Promise < void > ;
23
32
24
33
abstract stage ( filepath : string , relativeToVault : boolean ) : Promise < void > ;
25
34
26
35
abstract unstage ( filepath : string , relativeToVault : boolean ) : Promise < void > ;
27
36
28
37
abstract discard ( filepath : string ) : Promise < void > ;
29
38
30
- abstract discardAll ( _ : { dir ?: string , status ?: Status ; } ) : Promise < void > ;
39
+ abstract discardAll ( _ : { dir ?: string ; status ?: Status } ) : Promise < void > ;
31
40
32
41
abstract pull ( ) : Promise < FileStatusResult [ ] | undefined > ;
33
42
34
43
abstract push ( ) : Promise < number > ;
35
44
36
45
abstract canPush ( ) : Promise < boolean > ;
37
46
38
- abstract checkRequirements ( ) : Promise < "valid" | "missing-repo" | "missing-git" > ;
47
+ abstract checkRequirements ( ) : Promise <
48
+ "valid" | "missing-repo" | "missing-git"
49
+ > ;
39
50
40
51
abstract branchInfo ( ) : Promise < BranchInfo > ;
41
52
@@ -51,7 +62,10 @@ export abstract class GitManager {
51
62
52
63
abstract clone ( url : string , dir : string , depth ?: number ) : Promise < void > ;
53
64
54
- abstract setConfig ( path : string , value : string | number | boolean | undefined ) : Promise < void > ;
65
+ abstract setConfig (
66
+ path : string ,
67
+ value : string | number | boolean | undefined
68
+ ) : Promise < void > ;
55
69
56
70
abstract getConfig ( path : string ) : Promise < any > ;
57
71
@@ -73,11 +87,13 @@ export abstract class GitManager {
73
87
74
88
abstract updateBasePath ( basePath : string ) : void ;
75
89
76
- abstract getDiffString ( filePath : string , stagedChanges : boolean ) : Promise < string > ;
90
+ abstract getDiffString (
91
+ filePath : string ,
92
+ stagedChanges : boolean
93
+ ) : Promise < string > ;
77
94
78
95
abstract getLastCommitTime ( ) : Promise < Date | undefined > ;
79
96
80
-
81
97
getVaultPath ( path : string ) : string {
82
98
if ( this . plugin . settings . basePath ) {
83
99
return this . plugin . settings . basePath + "/" + path ;
@@ -87,10 +103,15 @@ export abstract class GitManager {
87
103
}
88
104
89
105
getPath ( path : string , relativeToVault : boolean ) : string {
90
- return ( relativeToVault && this . plugin . settings . basePath . length > 0 ) ? path . substring ( this . plugin . settings . basePath . length + 1 ) : path ;
106
+ return relativeToVault && this . plugin . settings . basePath . length > 0
107
+ ? path . substring ( this . plugin . settings . basePath . length + 1 )
108
+ : path ;
91
109
}
92
110
93
- private _getTreeStructure ( children : FileStatusResult [ ] , beginLength = 0 ) : TreeItem [ ] {
111
+ private _getTreeStructure (
112
+ children : FileStatusResult [ ] ,
113
+ beginLength = 0
114
+ ) : TreeItem [ ] {
94
115
const list : TreeItem [ ] = [ ] ;
95
116
children = [ ...children ] ;
96
117
while ( children . length > 0 ) {
@@ -99,22 +120,32 @@ export abstract class GitManager {
99
120
if ( restPath . contains ( "/" ) ) {
100
121
const title = restPath . substring ( 0 , restPath . indexOf ( "/" ) ) ;
101
122
const childrenWithSameTitle = children . filter ( ( item ) => {
102
- return item . path . substring ( beginLength ) . startsWith ( title + "/" ) ;
123
+ return item . path
124
+ . substring ( beginLength )
125
+ . startsWith ( title + "/" ) ;
103
126
} ) ;
104
127
childrenWithSameTitle . forEach ( ( item ) => children . remove ( item ) ) ;
105
- const path = first . path . substring ( 0 , restPath . indexOf ( "/" ) + beginLength ) ;
128
+ const path = first . path . substring (
129
+ 0 ,
130
+ restPath . indexOf ( "/" ) + beginLength
131
+ ) ;
106
132
list . push ( {
107
133
title : title ,
108
134
path : path ,
109
135
vaultPath : this . getVaultPath ( path ) ,
110
- children : this . _getTreeStructure ( childrenWithSameTitle , ( beginLength > 0 ? ( beginLength + title . length ) : title . length ) + 1 )
136
+ children : this . _getTreeStructure (
137
+ childrenWithSameTitle ,
138
+ ( beginLength > 0
139
+ ? beginLength + title . length
140
+ : title . length ) + 1
141
+ ) ,
111
142
} ) ;
112
143
} else {
113
144
list . push ( {
114
145
title : restPath ,
115
146
statusResult : first ,
116
147
path : first . path ,
117
- vaultPath : this . getVaultPath ( first . path )
148
+ vaultPath : this . getVaultPath ( first . path ) ,
118
149
} ) ;
119
150
children . remove ( first ) ;
120
151
}
@@ -123,16 +154,24 @@ export abstract class GitManager {
123
154
}
124
155
125
156
/*
126
- * Sorts the children and simplifies the title
127
- * If a node only contains another subdirectory, that subdirectory is moved up one level and integrated into the parent node
128
- */
157
+ * Sorts the children and simplifies the title
158
+ * If a node only contains another subdirectory, that subdirectory is moved up one level and integrated into the parent node
159
+ */
129
160
private simplify ( tree : TreeItem [ ] ) : TreeItem [ ] {
130
161
for ( const node of tree ) {
131
162
while ( true ) {
132
163
const singleChild = node . children ?. length == 1 ;
133
- const singleChildIsDir = node . children ?. first ( ) ?. statusResult == undefined ;
134
-
135
- if ( ! ( node . children != undefined && singleChild && singleChildIsDir ) ) break ;
164
+ const singleChildIsDir =
165
+ node . children ?. first ( ) ?. statusResult == undefined ;
166
+
167
+ if (
168
+ ! (
169
+ node . children != undefined &&
170
+ singleChild &&
171
+ singleChildIsDir
172
+ )
173
+ )
174
+ break ;
136
175
const child = node . children . first ( ) ! ;
137
176
node . title += "/" + child . title ;
138
177
node . statusResult = child . statusResult ;
@@ -144,7 +183,9 @@ export abstract class GitManager {
144
183
this . simplify ( node . children ) ;
145
184
}
146
185
node . children ?. sort ( ( a , b ) => {
147
- const dirCompare = ( b . statusResult == undefined ? 1 : 0 ) - ( a . statusResult == undefined ? 1 : 0 ) ;
186
+ const dirCompare =
187
+ ( b . statusResult == undefined ? 1 : 0 ) -
188
+ ( a . statusResult == undefined ? 1 : 0 ) ;
148
189
if ( dirCompare != 0 ) {
149
190
return dirCompare ;
150
191
} else {
@@ -153,7 +194,9 @@ export abstract class GitManager {
153
194
} ) ;
154
195
}
155
196
return tree . sort ( ( a , b ) => {
156
- const dirCompare = ( b . statusResult == undefined ? 1 : 0 ) - ( a . statusResult == undefined ? 1 : 0 ) ;
197
+ const dirCompare =
198
+ ( b . statusResult == undefined ? 1 : 0 ) -
199
+ ( a . statusResult == undefined ? 1 : 0 ) ;
157
200
if ( dirCompare != 0 ) {
158
201
return dirCompare ;
159
202
} else {
@@ -182,9 +225,9 @@ export abstract class GitManager {
182
225
}
183
226
184
227
if ( template . includes ( "{{files}}" ) ) {
185
- status = status ?? await this . status ( ) ;
228
+ status = status ?? ( await this . status ( ) ) ;
186
229
187
- const changeset : { [ key : string ] : string [ ] ; } = { } ;
230
+ const changeset : { [ key : string ] : string [ ] } = { } ;
188
231
status . staged . forEach ( ( value : FileStatusResult ) => {
189
232
if ( value . index in changeset ) {
190
233
changeset [ value . index ] . push ( value . path ) ;
@@ -209,7 +252,14 @@ export abstract class GitManager {
209
252
moment ( ) . format ( this . plugin . settings . commitDateFormat )
210
253
) ;
211
254
if ( this . plugin . settings . listChangedFilesInMessageBody ) {
212
- template = template + "\n\n" + "Affected files:" + "\n" + ( status ?? await this . status ( ) ) . staged . map ( ( e ) => e . path ) . join ( "\n" ) ;
255
+ template =
256
+ template +
257
+ "\n\n" +
258
+ "Affected files:" +
259
+ "\n" +
260
+ ( status ?? ( await this . status ( ) ) ) . staged
261
+ . map ( ( e ) => e . path )
262
+ . join ( "\n" ) ;
213
263
}
214
264
return template ;
215
265
}
0 commit comments