Skip to content

Instantly share code, notes, and snippets.

@wbhouston
Forked from zefhemel/git.md
Last active May 17, 2025 23:54
Show Gist options
  • Select an option

  • Save wbhouston/6a263383e3290e66f6554e8a9dc44bb3 to your computer and use it in GitHub Desktop.

Select an option

Save wbhouston/6a263383e3290e66f6554e8a9dc44bb3 to your computer and use it in GitHub Desktop.

#meta

git = {}
gitConfig = config.get("git", {})
git.autoSync = gitConfig.autoSync or false
git.autoCommitMinutes = gitConfig.autoCommitMinutes or 5
git.autoSyncDelay = gitConfig.autoSyncDelay or 60
git.skipInitialSync = gitConfig.skipInitialSync or false

function git.commit(message)
  message = message or "Snapshot"
  print "Comitting..."
  local ok, message = pcall(function()
    shell.run("git", {"add", "./*"})
    shell.run("git", {"commit", "-a", "-m", message})
  end)
  if not ok then
    print("Git commit failed: " .. message)
  end
end

function git.sync()
  git.commit()
  print "Pulling..."
  shell.run("git", {"pull"})
  print "Pushing..."
  shell.run("git", {"push"})
end

command.define {
  name = "Git: Commit",
  run = function()
    local message = editor.prompt "Commit message:"
    git.commit(message)
  end
}

command.define {
  name = "Git: Sync",
  run = function()
    git.sync()
    editor.flashNotification "Done!"
  end
}

if git.autoSync then
  if git.skipInitialSync then
    git.previousCommit = os.time()
  end
  
  event.listen {
    name = "cron:secondPassed",
    run = function()
      local currentTime = os.time()
    
      if currentTime - git.previousCheck > git.autoSyncDelay then
        git.previousCheck = currentTime
        local elapsedMinutes = (currentTime - git.previousCommit) / 60
        
        if elapsedMinutes > git.autoCommitMinutes then
          git.previousCommit = os.time()
          editor.flashNotification "Starting git autosync."
          system.invokeCommand('Git: Sync')
        end
      end
    end
  }
else
  print("Git autoSync is not enabled.")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment