From 8cc5df20125f962986efda8e985a7980897e5725 Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sat, 26 Mar 2022 17:23:08 +0100 Subject: [PATCH 1/8] added git branch to pushed data --- plugin/activitywatch.vim | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index c9fdb5f..3d72d49 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -13,6 +13,7 @@ let s:last_heartbeat = localtime() let s:file = '' let s:language = '' let s:project = '' +let s:branch = '' let s:connected = 0 let s:apiurl_host = get(g:, 'aw_apiurl_host', '127.0.0.1') @@ -105,6 +106,16 @@ function! s:CreateBucket() call HTTPPostJson(s:bucket_apiurl, l:body) endfunc +function! s:GetGitBranch() + let l:cmd_result = systemlist('git branch --show-current')[0] + let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 + if cmd_result + return cmd_result + else + return 'N/A' + endif +endfunc + function! s:Heartbeat() " Only send heartbeats if we can connect to aw-server if s:connected < 1 @@ -116,11 +127,13 @@ function! s:Heartbeat() let l:file = expand('%p') let l:language = &filetype let l:project = getcwd() + let l:branch = s:GetGitBranch() " Only send heartbeat if data was changed or more than 1 second has passed " since last heartbeat if s:file != l:file || \ s:language != l:language || \ s:project != l:project || + \ s:branch != l:branch || \ l:localtime - s:last_heartbeat > 1 let l:req_body = { @@ -129,13 +142,15 @@ function! s:Heartbeat() \ 'data': { \ 'file': l:file, \ 'language': l:language, - \ 'project': l:project + \ 'project': l:project, + \ 'branch': l:branch \ } \} call HTTPPostJson(s:heartbeat_apiurl, l:req_body) let s:file = l:file let s:language = l:language let s:project = l:project + let s:branch = l:branch let s:last_heartbeat = l:localtime endif endfunc From 3b5906c97592ecea56384a0b0d00d5519cfe4b9b Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sat, 26 Mar 2022 17:44:29 +0100 Subject: [PATCH 2/8] fixed var --- plugin/activitywatch.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index 3d72d49..d4a50df 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -109,7 +109,7 @@ endfunc function! s:GetGitBranch() let l:cmd_result = systemlist('git branch --show-current')[0] let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 - if cmd_result + if is_git_repo return cmd_result else return 'N/A' From b1b9c3e2b51f13144d3f8ec62a60c074b1c37d5d Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sun, 15 May 2022 18:47:10 +0200 Subject: [PATCH 3/8] moving git branch to only occur on heartbeat --- plugin/activitywatch.vim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index d4a50df..d49b62e 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -127,15 +127,14 @@ function! s:Heartbeat() let l:file = expand('%p') let l:language = &filetype let l:project = getcwd() - let l:branch = s:GetGitBranch() " Only send heartbeat if data was changed or more than 1 second has passed " since last heartbeat if s:file != l:file || \ s:language != l:language || \ s:project != l:project || - \ s:branch != l:branch || \ l:localtime - s:last_heartbeat > 1 - + " Only get branch if heartbeat is to be sent + let l:branch = s:GetGitBranch() let l:req_body = { \ 'duration': 0, \ 'timestamp': l:timestamp, From 943f55db866d98adb162add6d0d1f0edc40aa11d Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sun, 15 May 2022 19:41:26 +0200 Subject: [PATCH 4/8] changed to caching branch name --- plugin/activitywatch.vim | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index d49b62e..c5b12d0 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -10,6 +10,7 @@ set cpo&vim let s:nvim = has('nvim') let s:last_heartbeat = localtime() +let s:last_branch_check = localtime() let s:file = '' let s:language = '' let s:project = '' @@ -106,14 +107,19 @@ function! s:CreateBucket() call HTTPPostJson(s:bucket_apiurl, l:body) endfunc -function! s:GetGitBranch() - let l:cmd_result = systemlist('git branch --show-current')[0] - let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 - if is_git_repo - return cmd_result - else - return 'N/A' +function! s:GetGitBranch(localtime) + if a:localtime - s:last_branch_check > 5 || + \ s:branch == '' + let l:cmd_result = systemlist('git branch --show-current')[0] + let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 + let s:last_branch_check = a:localtime + if is_git_repo + return cmd_result + else + return 'N/A' + endif endif + return s:branch endfunc function! s:Heartbeat() @@ -127,14 +133,15 @@ function! s:Heartbeat() let l:file = expand('%p') let l:language = &filetype let l:project = getcwd() + let l:branch = s:GetGitBranch(l:localtime) " Only send heartbeat if data was changed or more than 1 second has passed " since last heartbeat if s:file != l:file || \ s:language != l:language || \ s:project != l:project || + \ s:branch != l:branch || \ l:localtime - s:last_heartbeat > 1 - " Only get branch if heartbeat is to be sent - let l:branch = s:GetGitBranch() + let l:req_body = { \ 'duration': 0, \ 'timestamp': l:timestamp, From da5e9bd9cb5b485dc4eb502a7d9ae4bd85561360 Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sun, 15 May 2022 19:46:25 +0200 Subject: [PATCH 5/8] changed branch timestamp name, and changed initialization to 0 instead --- plugin/activitywatch.vim | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index c5b12d0..2c99d91 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -10,7 +10,7 @@ set cpo&vim let s:nvim = has('nvim') let s:last_heartbeat = localtime() -let s:last_branch_check = localtime() +let s:last_branch_update = 0 let s:file = '' let s:language = '' let s:project = '' @@ -108,11 +108,10 @@ function! s:CreateBucket() endfunc function! s:GetGitBranch(localtime) - if a:localtime - s:last_branch_check > 5 || - \ s:branch == '' + if a:localtime - s:last_branch_update > 5 let l:cmd_result = systemlist('git branch --show-current')[0] let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 - let s:last_branch_check = a:localtime + let s:last_branch_update = a:localtime if is_git_repo return cmd_result else From e96db58c9e00e92b564004192269c8cb4e1ea380 Mon Sep 17 00:00:00 2001 From: FilipHarald Date: Sat, 21 May 2022 11:19:06 +0200 Subject: [PATCH 6/8] updating cached branch is now async --- plugin/activitywatch.vim | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index 2c99d91..5d690e0 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -10,10 +10,12 @@ set cpo&vim let s:nvim = has('nvim') let s:last_heartbeat = localtime() -let s:last_branch_update = 0 let s:file = '' let s:language = '' let s:project = '' + +let s:last_branch_update = 0 +let s:is_changed_branch = 0 let s:branch = '' let s:connected = 0 @@ -107,18 +109,15 @@ function! s:CreateBucket() call HTTPPostJson(s:bucket_apiurl, l:body) endfunc -function! s:GetGitBranch(localtime) +function! s:RefreshGitBranch(localtime) if a:localtime - s:last_branch_update > 5 - let l:cmd_result = systemlist('git branch --show-current')[0] - let l:is_git_repo = (cmd_result =~ '^fatal: ') ? 0 : 1 let s:last_branch_update = a:localtime - if is_git_repo - return cmd_result - else - return 'N/A' - endif + let l:cmd_result = systemlist('git branch --show-current')[0] + let l:current_branch = (cmd_result =~ '^fatal: ') ? 'N/A' : cmd_result + let s:is_changed_branch = l:current_branch == s:branch ? 0 : 1 + let s:branch = l:current_branch endif - return s:branch + "echo printf('branch %d is_changed_branch %s', s:branch, s:is_changed_branch) endfunc function! s:Heartbeat() @@ -132,13 +131,13 @@ function! s:Heartbeat() let l:file = expand('%p') let l:language = &filetype let l:project = getcwd() - let l:branch = s:GetGitBranch(l:localtime) + call s:RefreshGitBranch(l:localtime) " Only send heartbeat if data was changed or more than 1 second has passed " since last heartbeat if s:file != l:file || \ s:language != l:language || \ s:project != l:project || - \ s:branch != l:branch || + \ s:is_changed_branch == 1 || \ l:localtime - s:last_heartbeat > 1 let l:req_body = { @@ -148,20 +147,20 @@ function! s:Heartbeat() \ 'file': l:file, \ 'language': l:language, \ 'project': l:project, - \ 'branch': l:branch + \ 'branch': s:branch \ } \} call HTTPPostJson(s:heartbeat_apiurl, l:req_body) let s:file = l:file let s:language = l:language let s:project = l:project - let s:branch = l:branch let s:last_heartbeat = l:localtime endif endfunc function! AWStart() call s:CreateBucket() + call s:RefreshGitBranch(localtime()) endfunc function! AWStop() From 3f340f0a3322fb9ed771233ad8975becd49cfd17 Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 17 Jun 2023 17:27:35 +0200 Subject: [PATCH 7/8] fixed it --- plugin/activitywatch.vim | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index e06bfbd..e9b2487 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -109,15 +109,25 @@ function! s:CreateBucket() call HTTPPostJson(s:bucket_apiurl, l:body) endfunc +function! s:GitBranchOnStdout(jobid, data, event) + if a:data != [''] + let l:current_branch = a:data[0] + let s:is_changed_branch = current_branch == s:branch ? 0 : 1 + let s:branch = current_branch + endif +endfunc + +function! s:GitBranchOnExit(jobid, exitcode, eventtype) + if a:exitcode != 0 + let s:branch = 'N/A' + endif +endfunc + function! s:RefreshGitBranch(localtime) if a:localtime - s:last_branch_update > 5 let s:last_branch_update = a:localtime - let l:cmd_result = systemlist('git branch --show-current')[0] - let l:current_branch = (cmd_result =~ '^fatal: ') ? 'N/A' : cmd_result - let s:is_changed_branch = l:current_branch == s:branch ? 0 : 1 - let s:branch = l:current_branch + let l:cmd_result = jobstart('git branch --show-current', {'on_stdout': 's:GitBranchOnStdout', 'on_exit': 's:GitBranchOnExit'}) endif - "echo printf('branch %d is_changed_branch %s', s:branch, s:is_changed_branch) endfunc function! s:Heartbeat() From d41026d05e5326cf7cfa8f7e400fa4d2a8ee596b Mon Sep 17 00:00:00 2001 From: filip Date: Sat, 17 Jun 2023 17:35:39 +0200 Subject: [PATCH 8/8] changed to undo N/A branch --- plugin/activitywatch.vim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/plugin/activitywatch.vim b/plugin/activitywatch.vim index e9b2487..c700a11 100644 --- a/plugin/activitywatch.vim +++ b/plugin/activitywatch.vim @@ -119,7 +119,7 @@ endfunc function! s:GitBranchOnExit(jobid, exitcode, eventtype) if a:exitcode != 0 - let s:branch = 'N/A' + let s:branch = '' endif endfunc @@ -156,10 +156,12 @@ function! s:Heartbeat() \ 'data': { \ 'file': l:file, \ 'language': l:language, - \ 'project': l:project, - \ 'branch': s:branch + \ 'project': l:project \ } \} + if s:branch != '' + let l:req_body['data']['branch'] = s:branch + endif call HTTPPostJson(s:heartbeat_apiurl, l:req_body) let s:file = l:file let s:language = l:language