Skip to content

Commit

Permalink
Add initial code for bookmark rm (#2)
Browse files Browse the repository at this point in the history
* Add initial code for bookmark rm

* Fix read_xbel_1 unit test

* Add xbel example

* Clean up Cargo.toml

* Add sanity job in CI

* Cargo fmt pass

* Add Xbel::from_file && Xbel::to_file

* Add git_push function

* Dedup code between bookmark add & rm

* Code cleanup
  • Loading branch information
sydhds authored Oct 14, 2024
1 parent 7075fc3 commit f6be93b
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 223 deletions.
13 changes: 11 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,19 @@ env:
CARGO_TERM_COLOR: always

jobs:

sanity:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Cargo fmt
run: cargo fmt --all --check
- name: Cargo clippy
run: cargo clippy --no-deps --all-targets

build:

needs: sanity
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- name: Build
Expand Down
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ version = "0.1.0"
edition = "2021"

[dependencies]
# quick-xml = { version = "0.36.1", features = ["serde", "serialize"] }
# crossterm = "0.28.1"
clap = { version = "4.5", features = ["derive"] }
serde = { version = "1.0", features = ["derive"] }
git2 = "0.19"
Expand Down
18 changes: 18 additions & 0 deletions ressources/bookmarks_bank_v1.xbel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xbel PUBLIC "+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN//XML" "http://pyxml.sourceforge.net/topics/dtds/xbel.dtd">
<xbel version="1.0">
<!--- highestId :4: for Floccus bookmark sync browser extension -->

<folder id="1">
<title>admin</title>
<folder id="2">
<title>bank</title>
<bookmark href="https://www.bank1.com/" id="3">
<title>Bank 1 - Best bank in the world</title>
</bookmark>
<bookmark href="https://www.bank2.com/" id="4">
<title>Bank 2 because 2 gt 1 !#€</title>
</bookmark>
</folder>
</folder>
</xbel>
43 changes: 43 additions & 0 deletions src/git/git_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,46 @@ pub fn git_merge<'a>(
}
Ok(())
}

pub fn git_push(repo: &Repository, file_to_add: &Path) -> Result<(), git2::Error> {
// Configured author signature
let author = repo.signature()?;

// git add
let status = repo.status_file(file_to_add)?;
println!("status: {:?}", status);
let mut index = repo.index()?;

index.add_path(file_to_add)?;
// the modified in-memory index need to flush back to disk
index.write()?;

// git commit

// returns the object id you can use to look up the actual tree object
let new_tree_oid = index.write_tree()?;
// this is our new tree, i.e. the root directory of the new commit
let new_tree = repo.find_tree(new_tree_oid)?;

// for simple commit, use current head as parent
// TODO: test
// you need more than one parent if the commit is a merge
let head = repo.head()?;
// FIXME: unwrap
let parent = repo.find_commit(head.target().unwrap())?;

let _commit_oid = repo.commit(
Some("HEAD"),
&author,
&author,
"Floccus bookmarks update",
&new_tree,
&[&parent],
)?;

// git push
let mut origin = repo.find_remote("origin")?;
origin.push(&["refs/heads/main:refs/heads/main"], None)?;

Ok(())
}
2 changes: 1 addition & 1 deletion src/git/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod git_command;

pub use git_command::{git_clone, git_fetch, git_merge};
pub use git_command::{git_clone, git_fetch, git_merge, git_push};
Loading

0 comments on commit f6be93b

Please sign in to comment.