Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
git_borg_linker
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LBMC
Hub
git_borg_linker
Commits
d3a8d2da
Commit
d3a8d2da
authored
May 11, 2022
by
nfontrod
Browse files
Options
Downloads
Patches
Plain Diff
src/checkout.rs: add pre-checkout fonctionality
parent
19231485
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/checkout.rs
+91
-0
91 additions, 0 deletions
src/checkout.rs
with
91 additions
and
0 deletions
src/checkout.rs
0 → 100644
+
91
−
0
View file @
d3a8d2da
use
crate
::
commit
;
use
std
::{
path
::
PathBuf
,
process
::{
exit
,
Command
},
};
/// Check if two commits are differents
pub
fn
is_diff
(
commit1
:
&
str
,
commit2
:
&
str
,
borg_folder
:
&
PathBuf
)
->
bool
{
let
archive1
=
format!
(
"{}::{}"
,
borg_folder
.to_str
()
.unwrap
(),
commit1
);
let
cmd
=
format!
(
"borg diff {} {} | wc -l"
,
archive1
,
commit2
);
let
output
=
Command
::
new
(
"sh"
)
.arg
(
"-c"
)
.arg
(
cmd
)
.output
()
.unwrap
();
match
output
.status
.code
()
.unwrap
()
{
0
=>
(),
num
=>
{
eprintln!
(
"{}"
,
String
::
from_utf8
(
output
.stderr
)
.unwrap
());
exit
(
num
);
}
}
let
mut
strcode
=
String
::
from_utf8
(
output
.stdout
)
.unwrap
();
if
strcode
.ends_with
(
'\n'
)
{
strcode
.pop
();
}
let
res
:
i32
=
strcode
.parse
()
.unwrap
();
res
>
0
}
/// Function that checks if the archive with the name of the current commit id
/// exits in borg repository
pub
fn
check_if_current_commit_exits
(
borg_path
:
&
PathBuf
,
commit_id
:
&
str
)
{
// Trying to create an archive with the current git commit id
let
output
=
Command
::
new
(
"borg"
)
.arg
(
"list"
)
.arg
(
format!
(
"{}::{}"
,
borg_path
.to_str
()
.unwrap
(),
commit_id
))
.output
()
.unwrap
();
match
output
.status
.code
()
.unwrap
()
{
0
=>
(),
num
=>
{
let
msg
=
format!
(
"Archive {} does not exist"
,
commit_id
);
let
mut
output_msg
=
String
::
from_utf8
(
output
.stderr
)
.unwrap
();
if
output_msg
.ends_with
(
"
\n
"
)
{
output_msg
.pop
();
}
if
output_msg
==
msg
{
eprintln!
(
"No archive with the current commit id found !
\n
You should commit it first !"
);
exit
(
3
);
}
else
{
eprintln!
(
"{}"
,
output_msg
);
exit
(
num
);
}
}
}
}
/// Function that prepare checkout. **This function should be used before a
/// git checkout !**
///
/// # Description:
/// This function will check if the archive named as the current commit exits.
/// If it doesn't exists, it will create one. If it exists, it will create a
/// temporary archive of the current result respository and compare it with
/// the archive with the current git commit id.
/// If it is the same as the temporary achive then you can make a
/// checkout without losing data. If not, then you might.
pub
fn
prepare_checkout
()
{
let
(
borg_path
,
_
)
=
commit
::
check_path
();
let
commit_id
=
commit
::
get_current_commit
();
check_if_current_commit_exits
(
&
borg_path
,
&
commit_id
);
// Create an archive with the content of the current commit
let
tmp_name
=
format!
(
"{}-tmp"
,
commit_id
);
commit
::
commit
(
String
::
from
(
"none"
),
tmp_name
.clone
());
let
res
=
is_diff
(
&
commit_id
,
&
tmp_name
,
&
borg_path
);
let
output
=
Command
::
new
(
"borg"
)
.arg
(
"delete"
)
.arg
(
format!
(
"{}::{}"
,
borg_path
.to_str
()
.unwrap
(),
tmp_name
))
.output
()
.unwrap
();
match
output
.status
.code
()
.unwrap
()
{
0
=>
(),
num
=>
{
eprintln!
(
"{}"
,
String
::
from_utf8
(
output
.stderr
)
.unwrap
());
exit
(
num
);
}
}
if
res
{
eprintln!
(
"Your results folder contains unsaved changes!
\n
Please update your current commit with: gbl commit --update"
);
exit
(
4
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment