Skip to content

Commit

Permalink
- Fixes and improvements
Browse files Browse the repository at this point in the history
- Improves commit process #1522
  • Loading branch information
glushchenko committed May 5, 2023
1 parent a8cc640 commit a801a99
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
4 changes: 4 additions & 0 deletions FSNotes iOS/Settings/GitViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ class GitViewController: UITableViewController {
if let message = project.gitDo(action, progress: self.progress) {
DispatchQueue.main.async {
self.errorAlert(title: "git error", message: message)

if action == .pullPush {
UIApplication.getVC().checkNew()
}
}
}
})
Expand Down
19 changes: 17 additions & 2 deletions FSNotes iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,7 @@ class ViewController: UIViewController, UISearchBarDelegate, UIGestureRecognizer

viewController.gitQueue.addOperation({
Storage.shared().pullAll(force: force)
self.checkNew()

// if viewController.gitQueueState.operationCount == 0 {
// viewController.gitQueueState.addOperation {
Expand All @@ -725,6 +726,20 @@ class ViewController: UIViewController, UISearchBarDelegate, UIGestureRecognizer
})
}

public func checkNew() {
if let projects = Storage.shared().getGitProjects() {
for project in projects {
if let childProjects = project.getAllChild() {
for childProject in childProjects {
self.checkNotesCacheDiff(for: childProject, isGit: true)
}
}

self.checkNotesCacheDiff(for: project, isGit: true)
}
}
}

public func loadSearchController(query: String? = nil) {
navigationItem.searchController = searchController

Expand Down Expand Up @@ -1592,12 +1607,12 @@ class ViewController: UIViewController, UISearchBarDelegate, UIGestureRecognizer
}
}

public func checkNotesCacheDiff(for project: Project) {
public func checkNotesCacheDiff(for project: Project, isGit: Bool = false) {
let storage = Storage.shared()

// if not cached – load all results for cache
// (not loaded instantly because is resource consumption operation, loaded later in background)
guard project.cacheUsedDiffValidationNeeded else {
guard project.cacheUsedDiffValidationNeeded || isGit else {

_ = storage.noteList
.filter({ $0.project == project })
Expand Down
1 change: 1 addition & 0 deletions FSNotes/Business/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class Storage {

projects.append(project)

assignTree(for: project)
assignTrash(by: project.url)
assignArchive()
assignBookmarks()
Expand Down
3 changes: 3 additions & 0 deletions FSNotes/Git/commons/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum GitError : Error {

case notImplemented(msg: String)
case uncommittedConflict
case noAddedFiles

func associatedValue() -> String {
switch self {
Expand Down Expand Up @@ -52,6 +53,8 @@ public enum GitError : Error {
return "Not implemented \(msg)"
case .uncommittedConflict:
return "Uncommitted conflict"
case .noAddedFiles:
return "New files not found"
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions FSNotes/Git/index/Index+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,29 @@ extension Index {
throw gitUnknownError("Unable to read index", code: error)
}
}

static var count = 0

static let gitIndexCallback: git_index_matched_path_cb = { path, match, payload in
let newPath: String = git_string_converter(path!)

if newPath.startsWith(string: ".Trash") {
return 1
}

count += 1
return 0
}

public func add(path: String) {
public func add(path: String) -> Bool {
var dirPointer = UnsafeMutablePointer<Int8>(mutating: (path as NSString).utf8String)
var paths = withUnsafeMutablePointer(to: &dirPointer) {
git_strarray(strings: $0, count: 1)
}

idx.pointee.flatMap { index in
defer { git_index_free(index) }
let addResult = git_index_add_all(index, &paths, 0, nil, nil)
let addResult = git_index_add_all(index, &paths, 0, Index.gitIndexCallback, nil)
guard addResult == GIT_OK.rawValue else {
print("git_index_add_all \(addResult)")
return
Expand All @@ -141,5 +154,12 @@ extension Index {
return
}
}

let success = Index.count > 0

// reset
Index.count = 0

return success
}
}
16 changes: 12 additions & 4 deletions FSNotesCore/Shared/Extensions/Project+Git.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ extension Project {
}
#else
public func getRepositoryUrl() -> URL {
if !UserDefaultsManagement.iCloudDrive {
return url.appendingPathComponent(".git")
}

let key = settingsKey.md5.prefix(6)
let repoURL = UserDefaultsManagement.gitStorage.appendingPathComponent(key + " - " + label + ".git")

Expand Down Expand Up @@ -205,11 +209,13 @@ extension Project {
if let progress = progress {
progress.log(message: "git add .")
}
head.add(path: ".")
try head.save()

// Check directory is clean
if statuses.workingDirectoryClean == false || lastCommit == nil {
let success = head.add(path: ".")

// No commits yet or added files was found
if success || lastCommit == nil {
try head.save()

do {
progress?.log(message: "git commit")

Expand All @@ -230,6 +236,8 @@ extension Project {
}
} else {
progress?.log(message: "git add: no new data")

throw GitError.noAddedFiles
}
}

Expand Down
11 changes: 5 additions & 6 deletions FSNotesCore/Shared/Note+History.swift
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,13 @@ extension Note {
public func saveRevision(commitMessage: String? = nil, pull: Bool = true) throws {
guard let project = getGitProject() else { return }

try project.commit(message: commitMessage)
print("Successful commit")

do {
try project.commit(message: commitMessage)
try project.pull()
} catch {/*_*/}

try project.push()
try project.push()
} catch {
print(error)
}
}

public func checkout(commit: Commit) {
Expand Down

0 comments on commit a801a99

Please sign in to comment.