7
7
# stdin in the form
8
8
# <oldrev> <newrev> <refname>
9
9
# For example:
10
- # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/main
10
+ # aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
11
11
12
+ from typing import Text
12
13
import os
13
- import os .path
14
- import subprocess
15
14
import sys
15
+ import subprocess
16
+ import os .path
16
17
17
18
sys .path .insert (0 , os .path .dirname (__file__ ))
18
19
import zulip_git_config as config
19
20
20
21
VERSION = "0.9"
22
+ EMPTY_SHA = "0000000000000000000000000000000000000000"
23
+ COMMIT_ROW_TEMPLATE = '* {author_name} commited {subject} ([{commit_short_hash}]({commit_url}))\n '
21
24
22
25
if config .ZULIP_API_PATH is not None :
23
26
sys .path .append (config .ZULIP_API_PATH )
24
27
25
28
import zulip
26
-
27
29
client = zulip .Client (
28
30
email = config .ZULIP_USER ,
29
31
site = config .ZULIP_SITE ,
30
32
api_key = config .ZULIP_API_KEY ,
31
- client = "ZulipGit/" + VERSION ,
32
- )
33
-
34
-
35
- def git_repository_name () -> str :
36
- path , name = os .path .split (os .getcwd ())
37
- if name == ".git" :
38
- name = os .path .basename (path )
39
- return name [: - len (".git" )] if name .endswith (".git" ) else name
33
+ client = "ZulipGit/" + VERSION )
40
34
35
+ def git_repository_name () -> Text :
36
+ output = subprocess .check_output (["git" , "rev-parse" , "--is-bare-repository" ])
37
+ if output .strip () == "true" :
38
+ return os .path .basename (os .getcwd ())[:- len (".git" )]
39
+ else :
40
+ return os .path .basename (os .path .dirname (os .getcwd ()))
41
41
42
42
def git_commit_range (oldrev : str , newrev : str ) -> str :
43
- log_cmd = ["git" , "log" , "--reverse" , "--pretty=%aE %H %s" , f"{ oldrev } ..{ newrev } " ]
44
- commits = ""
45
- for ln in subprocess .check_output (log_cmd , universal_newlines = True ).splitlines ():
46
- author_email , commit_id , subject = ln .split (None , 2 )
47
- if hasattr (config , "format_commit_message" ):
48
- commits += config .format_commit_message (author_email , subject , commit_id )
49
- else :
50
- commits += f"!avatar({ author_email } ) { subject } \n "
51
- return commits
43
+ remote_repo_cmd = ["git" , "config" , "--get" , "remote.origin.url" ]
44
+ remote_repo_url = subprocess .check_output (remote_repo_cmd ).strip ().decode ("utf-8" )
45
+
46
+ log_cmd = ["git" , "log" , "--reverse" ,
47
+ "--pretty=%aN%n%H%n%h%n%s" , "%s..%s" % (oldrev , newrev )]
48
+ commits = ''
49
+ output = subprocess .check_output (log_cmd , universal_newlines = True ).splitlines ()
50
+ it = iter (output )
51
+ for _ in range (len (output )// 4 ):
52
+ author_name = next (iter (it ))
53
+ commit_hash = next (iter (it ))
54
+ commit_short_hash = next (iter (it ))
55
+ subject = next (iter (it ))
56
+ commits += COMMIT_ROW_TEMPLATE .format (
57
+ author_name = author_name ,
58
+ commit_short_hash = commit_short_hash ,
59
+ subject = subject ,
60
+ commit_url = "{}/commit/{}" .format (remote_repo_url , commit_hash )
61
+ )
52
62
63
+ return commits
53
64
54
65
def send_bot_message (oldrev : str , newrev : str , refname : str ) -> None :
55
- repo_name = git_repository_name ()
56
- branch = refname .replace (" refs/heads/" , "" )
66
+ repo_name = git_repository_name ()
67
+ branch = refname .replace (' refs/heads/' , '' )
57
68
destination = config .commit_notice_destination (repo_name , branch , newrev )
58
69
if destination is None :
59
70
# Don't forward the notice anywhere
@@ -62,31 +73,28 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
62
73
new_head = newrev [:12 ]
63
74
old_head = oldrev [:12 ]
64
75
65
- if (
66
- oldrev == "0000000000000000000000000000000000000000"
67
- or newrev == "0000000000000000000000000000000000000000"
68
- ):
76
+ if oldrev == EMPTY_SHA or newrev == EMPTY_SHA :
69
77
# New branch pushed or old branch removed
70
- added = ""
71
- removed = ""
78
+ added = ''
79
+ removed = ''
72
80
else :
73
- added = git_commit_range (oldrev , newrev )
81
+ added = git_commit_range (oldrev , newrev )
74
82
removed = git_commit_range (newrev , oldrev )
75
83
76
- if oldrev == "0000000000000000000000000000000000000000" :
77
- message = f"` { new_head } ` was pushed to new branch `{ branch } `"
78
- elif newrev == "0000000000000000000000000000000000000000" :
79
- message = f" branch `{ branch } ` was removed (was `{ old_head } `)"
84
+ if oldrev == EMPTY_SHA :
85
+ message = '`%s ` was pushed to new branch `%s`' % ( new_head , branch )
86
+ elif newrev == EMPTY_SHA :
87
+ message = ' branch `%s ` was removed (was `%s`)' % ( branch , old_head )
80
88
elif removed :
81
- message = f"` { new_head } ` was pushed to `{ branch } `, **REMOVING**:\n \n { removed } "
89
+ message = '`%s ` was pushed to `%s `, **REMOVING**:\n \n %s' % ( new_head , branch , removed )
82
90
if added :
83
- message += " \n **and adding**:\n \n " + added
84
- message += " \n **A HISTORY REWRITE HAS OCCURRED!**"
85
- message += " \n @everyone: Please check your local branches to deal with this."
91
+ message += ' \n **and adding**:\n \n ' + added
92
+ message += ' \n **A HISTORY REWRITE HAS OCCURRED!**'
93
+ message += ' \n @everyone: Please check your local branches to deal with this.'
86
94
elif added :
87
- message = f"` { new_head } ` was deployed to `{ branch } ` with:\n \n { added } "
95
+ message = '`%s ` was deployed to `%s ` with:\n \n %s' % ( new_head , branch , added )
88
96
else :
89
- message = f"` { new_head } ` was pushed to `{ branch } `... but nothing changed?"
97
+ message = '`%s ` was pushed to `%s `... but nothing changed?' % ( new_head , branch )
90
98
91
99
message_data = {
92
100
"type" : "stream" ,
@@ -96,7 +104,6 @@ def send_bot_message(oldrev: str, newrev: str, refname: str) -> None:
96
104
}
97
105
client .send_message (message_data )
98
106
99
-
100
107
for ln in sys .stdin :
101
108
oldrev , newrev , refname = ln .strip ().split ()
102
- send_bot_message (oldrev , newrev , refname )
109
+ send_bot_message (oldrev , newrev , refname )
0 commit comments