@@ -8,11 +8,12 @@ use liboxen::core::index::{CommitReader, Merger};
8
8
use liboxen:: error:: OxenError ;
9
9
use liboxen:: message:: OxenMessage ;
10
10
use liboxen:: model:: compare:: tabular_compare:: TabularCompareBody ;
11
- use liboxen:: model:: { DataFrameSize , Schema } ;
11
+ use liboxen:: model:: { Commit , DataFrameSize , LocalRepository , Schema } ;
12
12
use liboxen:: opts:: df_opts:: DFOptsView ;
13
13
use liboxen:: opts:: DFOpts ;
14
14
use liboxen:: view:: compare:: {
15
- CompareCommits , CompareCommitsResponse , CompareEntries , CompareResult , CompareTabularResponse ,
15
+ CompareCommits , CompareCommitsResponse , CompareEntries , CompareEntryResponse , CompareResult ,
16
+ CompareTabularResponse ,
16
17
} ;
17
18
use liboxen:: view:: json_data_frame_view:: { DFResourceType , DerivedDFResource , JsonDataFrameSource } ;
18
19
use liboxen:: view:: {
@@ -121,6 +122,52 @@ pub async fn entries(
121
122
Ok ( HttpResponse :: Ok ( ) . json ( view) )
122
123
}
123
124
125
+ pub async fn file (
126
+ req : HttpRequest ,
127
+ query : web:: Query < DFOptsQuery > ,
128
+ ) -> actix_web:: Result < HttpResponse , OxenHttpError > {
129
+ let app_data = app_data ( & req) ?;
130
+ let namespace = path_param ( & req, "namespace" ) ?;
131
+ let name = path_param ( & req, "repo_name" ) ?;
132
+ let base_head = path_param ( & req, "base_head" ) ?;
133
+
134
+ // Get the repository or return error
135
+ let repository = get_repo ( & app_data. path , namespace, name) ?;
136
+
137
+ // Parse the base and head from the base..head/resource string
138
+ // For Example)
139
+ // main..feature/add-data/path/to/file.txt
140
+ let ( base_commit, head_commit, resource) = parse_base_head_resource ( & repository, & base_head) ?;
141
+
142
+ let base_entry = api:: local:: entries:: get_commit_entry ( & repository, & base_commit, & resource) ?;
143
+ let head_entry = api:: local:: entries:: get_commit_entry ( & repository, & head_commit, & resource) ?;
144
+
145
+ let mut opts = DFOpts :: empty ( ) ;
146
+ opts = df_opts_query:: parse_opts ( & query, & mut opts) ;
147
+
148
+ let page_size = query. page_size . unwrap_or ( constants:: DEFAULT_PAGE_SIZE ) ;
149
+ let page = query. page . unwrap_or ( constants:: DEFAULT_PAGE_NUM ) ;
150
+
151
+ let start = if page == 0 { 0 } else { page_size * ( page - 1 ) } ;
152
+ let end = page_size * page;
153
+ opts. slice = Some ( format ! ( "{}..{}" , start, end) ) ;
154
+
155
+ let diff = api:: local:: diff:: diff_entries (
156
+ & repository,
157
+ base_entry,
158
+ & base_commit,
159
+ head_entry,
160
+ & head_commit,
161
+ opts,
162
+ ) ?;
163
+
164
+ let view = CompareEntryResponse {
165
+ status : StatusMessage :: resource_found ( ) ,
166
+ compare : diff,
167
+ } ;
168
+ Ok ( HttpResponse :: Ok ( ) . json ( view) )
169
+ }
170
+
124
171
pub async fn create_df_compare (
125
172
req : HttpRequest ,
126
173
_query : web:: Query < DFOptsQuery > ,
@@ -191,7 +238,7 @@ pub async fn create_df_compare(
191
238
) ?;
192
239
193
240
let view = match result {
194
- CompareResult :: Tabular ( compare) => {
241
+ CompareResult :: Tabular ( ( compare, _ ) ) => {
195
242
let mut messages: Vec < OxenMessage > = vec ! [ ] ;
196
243
197
244
if compare. dupes . left > 0 || compare. dupes . right > 0 {
@@ -297,7 +344,7 @@ pub async fn get_df_compare(
297
344
) ?;
298
345
299
346
match result {
300
- CompareResult :: Tabular ( compare) => {
347
+ CompareResult :: Tabular ( ( compare, _ ) ) => {
301
348
let mut messages: Vec < OxenMessage > = vec ! [ ] ;
302
349
303
350
if compare. dupes . left > 0 || compare. dupes . right > 0 {
@@ -442,3 +489,49 @@ pub async fn get_derived_df(
442
489
}
443
490
}
444
491
}
492
+
493
+ fn parse_base_head_resource (
494
+ repo : & LocalRepository ,
495
+ base_head : & str ,
496
+ ) -> Result < ( Commit , Commit , PathBuf ) , OxenError > {
497
+ log:: debug!( "Parsing base_head_resource: {}" , base_head) ;
498
+
499
+ let mut split = base_head. split ( ".." ) ;
500
+ let base = split
501
+ . next ( )
502
+ . ok_or ( OxenError :: resource_not_found ( base_head) ) ?;
503
+ let head = split
504
+ . next ( )
505
+ . ok_or ( OxenError :: resource_not_found ( base_head) ) ?;
506
+
507
+ let base_commit = api:: local:: revisions:: get ( repo, base) ?
508
+ . ok_or ( OxenError :: revision_not_found ( base. into ( ) ) ) ?;
509
+
510
+ // Split on / and find longest branch name
511
+ let split_head = head. split ( '/' ) ;
512
+ let mut longest_str = String :: from ( "" ) ;
513
+ let mut head_commit: Option < Commit > = None ;
514
+ let mut resource: Option < PathBuf > = None ;
515
+
516
+ for s in split_head {
517
+ let maybe_revision = format ! ( "{}{}" , longest_str, s) ;
518
+ log:: debug!( "Checking maybe head revision: {}" , maybe_revision) ;
519
+ let commit = api:: local:: revisions:: get ( repo, & maybe_revision) ?;
520
+ if commit. is_some ( ) {
521
+ head_commit = commit;
522
+ let mut r_str = head. replace ( & maybe_revision, "" ) ;
523
+ // remove first char from r_str
524
+ r_str. remove ( 0 ) ;
525
+ resource = Some ( PathBuf :: from ( r_str) ) ;
526
+ }
527
+ longest_str = format ! ( "{}/" , maybe_revision) ;
528
+ }
529
+
530
+ log:: debug!( "Got head_commit: {:?}" , head_commit) ;
531
+ log:: debug!( "Got resource: {:?}" , resource) ;
532
+
533
+ let head_commit = head_commit. ok_or ( OxenError :: revision_not_found ( head. into ( ) ) ) ?;
534
+ let resource = resource. ok_or ( OxenError :: revision_not_found ( head. into ( ) ) ) ?;
535
+
536
+ Ok ( ( base_commit, head_commit, resource) )
537
+ }
0 commit comments