11using System ;
2+ using System . Collections . Generic ;
23using System . ComponentModel . DataAnnotations ;
34using System . IO ;
45using System . Threading . Tasks ;
@@ -38,8 +39,20 @@ public bool ThreeWayMerge
3839 {
3940 get ;
4041 set ;
42+ } = true ;
43+
44+ public bool CanCreateCommit
45+ {
46+ get => _canCreateCommit ;
47+ set => SetProperty ( ref _canCreateCommit , value ) ;
4148 }
4249
50+ public bool CommitChanges
51+ {
52+ get ;
53+ set ;
54+ } = true ;
55+
4356 public IClipboard Clipboard
4457 {
4558 get ;
@@ -58,14 +71,24 @@ public static ValidationResult ValidatePatchFile(string file, ValidationContext
5871 return new ValidationResult ( "Invalid object instance!!!" ) ;
5972
6073 if ( apply . FromClipboard )
74+ {
75+ apply . CanCreateCommit = false ;
6176 return ValidationResult . Success ;
77+ }
6278
6379 if ( string . IsNullOrEmpty ( file ) )
80+ {
81+ apply . CanCreateCommit = false ;
6482 return new ValidationResult ( "Please select a patch file!!!" ) ;
83+ }
6584
6685 if ( ! File . Exists ( file ) )
86+ {
87+ apply . CanCreateCommit = false ;
6788 return new ValidationResult ( $ "File '{ file } ' can NOT be found!!!") ;
89+ }
6890
91+ apply . CheckPatchFileFormat ( file ) ;
6992 return ValidationResult . Success ;
7093 }
7194
@@ -98,9 +121,21 @@ public override async Task<bool> Sure()
98121 Use ( log ) ;
99122
100123 var extra = ThreeWayMerge ? "--3way" : string . Empty ;
101- var succ = await new Commands . Apply ( _repo . FullPath , finalPatchFile , _ignoreWhiteSpace , SelectedWhiteSpaceMode . Arg , extra )
102- . Use ( log )
103- . ExecAsync ( ) ;
124+ var succ = false ;
125+ if ( ! _fromClipboard && _canCreateCommit && CommitChanges )
126+ {
127+ _repo . ClearCommitMessage ( ) ;
128+
129+ succ = await new Commands . Am ( _repo . FullPath , finalPatchFile , _ignoreWhiteSpace , SelectedWhiteSpaceMode . Arg , extra )
130+ . Use ( log )
131+ . ExecAsync ( ) ;
132+ }
133+ else
134+ {
135+ succ = await new Commands . Apply ( _repo . FullPath , finalPatchFile , _ignoreWhiteSpace , SelectedWhiteSpaceMode . Arg , extra )
136+ . Use ( log )
137+ . ExecAsync ( ) ;
138+ }
104139
105140 log . Complete ( ) ;
106141
@@ -110,9 +145,35 @@ public override async Task<bool> Sure()
110145 return succ ;
111146 }
112147
148+ private void CheckPatchFileFormat ( string file )
149+ {
150+ try
151+ {
152+ using var reader = new StreamReader ( file ) ;
153+ var lines = new List < string > ( ) ;
154+ for ( int i = 0 ; i < 4 ; i ++ )
155+ {
156+ if ( reader . EndOfStream )
157+ break ;
158+ lines . Add ( reader . ReadLine ( ) ) ;
159+ }
160+
161+ CanCreateCommit = lines . Count == 4 &&
162+ lines [ 0 ] . StartsWith ( "From " , StringComparison . Ordinal ) &&
163+ lines [ 1 ] . StartsWith ( "From: " , StringComparison . Ordinal ) &&
164+ lines [ 2 ] . StartsWith ( "Date: " , StringComparison . Ordinal ) &&
165+ lines [ 3 ] . StartsWith ( "Subject: " , StringComparison . Ordinal ) ;
166+ }
167+ catch
168+ {
169+ CanCreateCommit = false ;
170+ }
171+ }
172+
113173 private readonly Repository _repo = null ;
114174 private string _patchFile = string . Empty ;
115175 private bool _fromClipboard = false ;
116176 private bool _ignoreWhiteSpace = true ;
177+ private bool _canCreateCommit = false ;
117178 }
118179}
0 commit comments