about summary refs log tree commit diff
path: root/lua/sketchbook/git_sync.lua
blob: d19f02402c222aaa2dae03dfbd94aee52466a0bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local M = {}

function M.is_git_repo(path)
  local git_dir = vim.fn.systemlist("git -C " .. path .. " rev-parse --is-inside-work-tree")[1]
  return git_dir == "true"
end

function M.commit_notes()
  local notes_dir = vim.g.notes_directory or "~/notes/"
  notes_dir = vim.fn.expand(notes_dir)

  if not M.is_git_repo(notes_dir) then
    vim.notify("Note folder is not a Git repository.", vim.log.levels.WARN)
    return
  end

  local commit_message = "Update notes: " .. os.date "%Y-%m-%d %H:%M:%S"
  vim.fn.system("git -C " .. notes_dir .. " add .")
  vim.fn.system("git -C " .. notes_dir .. " commit -m '" .. commit_message .. "'")
  vim.notify("Notes committed to Git repository.", vim.log.levels.INFO)
end

function M.push_notes()
  local notes_dir = vim.g.notes_directory or "~/notes/"
  notes_dir = vim.fn.expand(notes_dir)

  if not M.is_git_repo(notes_dir) then
    vim.notify("Note folder is not a Git repository.", vim.log.levels.WARN)
    return
  end

  vim.fn.system("git -C " .. notes_dir .. " push")
  vim.notify("Notes pushed to remote repository.", vim.log.levels.INFO)
end

return M