Skip to content

Commit

Permalink
feat: ✨ add pull and checkout buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenull committed May 22, 2022
1 parent a13d4f8 commit f950626
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 25 deletions.
3 changes: 3 additions & 0 deletions src/helper/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ ${COMMON_STYLE}
export const BUTTONS = [
{ key: 'status', title: 'Check Status', event: 'check' },
{ key: 'log', title: 'Show Log', event: 'log' },
{ key: 'pull', title: 'Pull', event: 'pull' },
{ key: 'pullRebase', title: 'Pull Rebase', event: 'pullRebase' },
{ key: 'checkout', title: 'Checkout', event: 'checkout' },
{ key: 'commit', title: 'Commit', event: 'commit' },
{ key: 'push', title: 'Push', event: 'push' },
{ key: 'commitAndPush', title: 'Commit & Push', event: 'commitAndPush' },
Expand Down
92 changes: 84 additions & 8 deletions src/helper/git.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,108 @@
// https://logseq.github.io/plugins/interfaces/IAppProxy.html#execGitCommand
import type { IGitResult } from "@logseq/libs/dist/LSPlugin.user"

export const status = async () => {
export const status = async (showRes = true): Promise<IGitResult> => {
// git status --porcelain | awk '{print $2}'
// git status --porcelain | wc -l
const res = await logseq.Git.execCommand(['status', '--porcelain'])
console.log('[faiz:] === status', res)
console.log('[faiz:] === git status', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git status success')
} else {
logseq.App.showMsg(`Git status failed\n${res.stderr}`, 'error')
}
}
// changed files staged files
return res
}

// log with git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
export const log = async (): Promise<IGitResult> => {
export const log = async (showRes = true): Promise<IGitResult> => {
// git log --pretty=format:"%h %s" -n 1
// git log --pretty=format:"%h %ad | %s%d [%an]" --date=short
// return await logseq.App.execGitCommand(['log', '--pretty=format:"%h %s"'])
return await logseq.Git.execCommand(['log', '--pretty=format:"%h %ad | %s [%an]"', '--date=format:"%Y-%m-%d %H:%M:%S"', '--name-status'])
const res = await logseq.Git.execCommand(['log', '--pretty=format:"%h %ad | %s [%an]"', '--date=format:"%Y-%m-%d %H:%M:%S"', '--name-status'])
console.log('[faiz:] === git log', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git log success')
} else {
logseq.App.showMsg(`Git log failed\n${res.stderr}`, 'error')
}
}
return res
}

// git pull
export const pull = async (showRes = true): Promise<IGitResult> => {
const res = await logseq.Git.execCommand(['pull'])
console.log('[faiz:] === git pull', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git pull success')
} else {
logseq.App.showMsg(`Git pull failed\n${res.stderr}`, 'error')
}
}
return res
}

// git pull --rebase
export const pullRebase = async (showRes = true): Promise<IGitResult> => {
const res = await logseq.Git.execCommand(['pull', '--rebase'])
console.log('[faiz:] === git pull --rebase', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git pull --rebase success')
} else {
logseq.App.showMsg(`Git pull --rebase failed\n${res.stderr}`, 'error')
}
}
return res
}

// git checkout .
export const checkout = async (showRes = true): Promise<IGitResult> => {
const res = await logseq.Git.execCommand(['checkout', '.'])
console.log('[faiz:] === git checkout .', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git checkout success')
} else {
logseq.App.showMsg(`Git checkout failed\n${res.stderr}`, 'error')
}
}
return res
}

// git commit
export const commit = async (message: string): Promise<IGitResult> => {
export const commit = async (showRes = true, message: string): Promise<IGitResult> => {
await logseq.Git.execCommand(['add', '.'])
// git commit -m "message"
return await logseq.Git.execCommand(['commit', '-m', message])
const res = await logseq.Git.execCommand(['commit', '-m', message])
console.log('[faiz:] === git commit', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git commit success')
} else {
logseq.App.showMsg(`Git commit failed\n${res.stderr}`, 'error')
}
}
return res
}

// push
export const push = async (): Promise<IGitResult> => {
export const push = async (showRes = true): Promise<IGitResult> => {
// git push
return await logseq.Git.execCommand(['push'])
const res = await logseq.Git.execCommand(['push'])
console.log('[faiz:] === git push', res)
if (showRes) {
if (res.exitCode === 0) {
logseq.App.showMsg('Git push success')
} else {
logseq.App.showMsg(`Git push failed\n${res.stderr}`, 'error')
}
}
return res
}
2 changes: 1 addition & 1 deletion src/helper/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { status } from './git'

export const checkStatus = async () => {
console.log('Checking status...')
const statusRes = await status()
const statusRes = await status(false)
if (statusRes?.stdout === '') {
console.log('No changes', statusRes)
setPluginStyle(INACTIVE_STYLE)
Expand Down
44 changes: 28 additions & 16 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import { BUTTONS, COMMON_STYLE, LOADING_STYLE, SETTINGS_SCHEMA, SHOW_POPUP_STYLE } from './helper/constants'
import { commit, log, push, status } from './helper/git'
import { checkout, commit, log, pull, pullRebase, push, status } from './helper/git'
import { checkStatus, debounce, getPluginStyle, hidePopup, setPluginStyle, showPopup } from './helper/util'
import './index.css'

Expand All @@ -28,42 +28,56 @@ if (isDevelopment) {
}
hidePopup()
}),
pull: debounce(async function() {
console.log('[faiz:] === pull click')
setPluginStyle(LOADING_STYLE)
hidePopup()
await pull(false)
checkStatus()
}),
pullRebase: debounce(async function() {
console.log('[faiz:] === pullRebase click')
setPluginStyle(LOADING_STYLE)
hidePopup()
await pullRebase()
checkStatus()
}),
checkout: debounce(async function() {
console.log('[faiz:] === checkout click')
hidePopup()
checkout()
}),
commit: debounce(async function () {
hidePopup()
console.log('Committing...')
await commit(`[logseq-plugin-git:commit] ${new Date().toISOString()}`)
commit(true, `[logseq-plugin-git:commit] ${new Date().toISOString()}`)
}),
push: debounce(async function () {
setPluginStyle(LOADING_STYLE)
hidePopup()
console.log('Pushing...')
await push()
console.log('Checking status...')
checkStatus()
logseq.App.showMsg('Pushed Successfully!')
}),
commitAndPush: debounce(async function () {
setPluginStyle(LOADING_STYLE)
hidePopup()
console.log('Committing...')
await commit(`[logseq-plugin-git:commit] ${new Date().toISOString()}`)
console.log('Pushing...')
await push()
console.log('Checking status...')
await commit(true, `[logseq-plugin-git:commit] ${new Date().toISOString()}`)
await push(true)
checkStatus()
logseq.App.showMsg('Pushed Successfully!')
}),
log: debounce(async function() {
console.log('[faiz:] === log click')
const res = await log()
const res = await log(false)
logseq.App.showMsg(res?.stdout, 'error')
// logseq.App.showMsg(res?.stdout)
hidePopup()
}),
showPopup: debounce(async function() {
console.log('[faiz:] === showPopup click')
showPopup()
}),
hidePopup: debounce(function() {
console.log('[faiz:] === hidePopup click')
hidePopup()
}),
})

logseq.App.registerUIItem('toolbar', {
Expand All @@ -72,9 +86,7 @@ if (isDevelopment) {
})
logseq.useSettingsSchema(SETTINGS_SCHEMA)
setTimeout(() => {
// setPluginStyle(COMMON_STYLE + SHOW_POPUP_STYLE)
const buttons = (logseq.settings?.buttons as string[])?.map(title => BUTTONS.find(b => b.title === title))
console.log('[faiz:] === buttons', buttons, logseq.settings?.buttons)
if (buttons?.length) {
logseq.provideUI({
key: 'git-popup',
Expand Down

0 comments on commit f950626

Please sign in to comment.