summaryrefslogtreecommitdiff
path: root/gcl
blob: 17098494f8d3f33b040d0f214ff7c1c98c82df5b (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
# git config linker (gcl) v1.0
# Written by Martin Fischer <martin@push-f.com> and licensed under MIT.
import sys
import os
import configparser
import subprocess
import shutil

GITCONFIGS = 'gitconfigs/'


class colors:
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'


def print_usage():
    print(
        '''git config linker (gcl):

 gcl status            check the status of the directories in .

 gcl link <repo>...    move <repo>/.git/config to gitconfigs/<repo>
                       (leaving a symlink behind)

 gcl clone             list all configs that are not cloned

 gcl clone <config>... clone a repository using gitconfigs/<config>

 gcl mv <src> <dest>   rename a repo and its config and update the symlink

 gcl unlink <repo>...  undo "gcl link <repo>"
                       (only needed if you want to stop using gcl)
'''
    )
    sys.exit()


args = sys.argv[1:]
if len(args) == 0:
    print_usage()

if not os.path.exists(GITCONFIGS + '/.git/config'):
    sys.exit(f'expected {GITCONFIGS} to be a git repository')


def get_link_error(path):
    configpath = path + '/.git/config'
    if not os.path.exists(configpath):
        missing = os.path.relpath(os.path.realpath(configpath))
        return (
            colors.FAIL + 'BROKEN LINK' + colors.ENDC + f' ({missing} does not exist)'
        )
    elif (
        os.path.islink(configpath)
        and os.path.relpath(os.path.realpath(configpath)) != GITCONFIGS + path
    ):
        missing = os.path.relpath(os.path.realpath(configpath))
        return colors.WARNING + 'LINK MISMATCH' + colors.ENDC + f' ({missing})'


def get_repo_status(path):
    errors = []
    link_error = get_link_error(path)
    if link_error:
        errors.append(link_error)

    if hasremote(path + '/.git/config'):
        try:
            if (
                subprocess.check_output(
                    ['git', 'branch', '-r', '--contains', 'HEAD'],
                    cwd=path,
                    stderr=subprocess.DEVNULL,
                )
                == b''
            ):
                errors.append(colors.WARNING + 'UNPUSHED' + colors.ENDC)
        except subprocess.CalledProcessError as err:
            if (
                subprocess.run(
                    ['git', 'rev-parse', 'HEAD'],
                    cwd=path,
                    stderr=subprocess.DEVNULL,
                    stdout=subprocess.DEVNULL,
                )
                == 0
            ):
                # the first command did not just fail because there are no commits yet
                raise err
    else:
        errors.append(colors.FAIL + 'NO REMOTE' + colors.ENDC)

    if subprocess.check_output(['git', 'status', '--porcelain'], cwd=path) != b'':
        errors.append(colors.WARNING + 'DIRTY' + colors.ENDC)
    return ', '.join(errors)


def print_repo_table(reponames):
    maxlen = max([len(s) for s in reponames])
    for reponame in reponames:
        print(f'\t{reponame.ljust(maxlen)} {get_repo_status(reponame)}')
    print()


linked = []
unlinked = []
not_a_repo = []


def hasremote(path):
    config = configparser.ConfigParser()
    config.read(path)
    return any(
        [
            ':' in config[s].get('url', '')
            for s in config.sections()
            if s.startswith('remote')
        ]
    )


def scan():
    for filename in sorted(os.listdir()):
        if not os.path.isdir(filename):
            continue

        configpath = filename + '/.git/config'
        if os.path.islink(configpath):
            linked.append(filename)
        elif not os.path.exists(configpath):
            not_a_repo.append(filename)
        else:
            unlinked.append(filename)


cmd = args.pop(0)

if cmd == 'status':
    scan()
    if not_a_repo:
        print(f"non-repositories:\n")
        print('\t' + '\n\t'.join(not_a_repo))
        print()
    if unlinked:
        print('unlinked repos:\n    (link with "gcl link <name>...")\n')
        print_repo_table(unlinked)
    if linked:
        print('linked repos:\n')
        print_repo_table(linked)

elif cmd == 'link':
    if len(args) == 0:
        sys.exit('Nothing specified, nothing linked.')

    for repo in args:
        configpath = repo + '/.git/config'
        if not os.path.isfile(configpath):
            print(
                f"skipping '{repo}' because {configpath} is not a file (maybe already linked?)"
            )
            continue
        if os.path.exists(GITCONFIGS + repo):
            sys.exit(f"aborting since {GITCONFIGS + repo} already exists")
        os.rename(configpath, GITCONFIGS + repo)
        os.symlink('../../' + GITCONFIGS + repo, configpath)

elif cmd == 'unlink':
    if len(args) == 0:
        sys.exit('Nothing specified, nothing unlinked.')

    for repo in args:
        configpath = repo + '/.git/config'
        if not os.path.islink(configpath):
            continue
        if not os.path.isfile(configpath):
            print(f"skipping '{repo}' because {config} is not a file")
            continue

        os.unlink(configpath)
        shutil.copyfile(GITCONFIGS + repo, configpath)
        os.unlink(GITCONFIGS + repo)

elif cmd == 'clone':
    if len(args) == 0:
        scan()
        available = set(os.listdir(GITCONFIGS)) - set(linked) - set(['.git'])
        if available:
            print('uncloned configs:\n    (clone with "gcl clone <name>...")\n')
            for l in sorted(available):
                print('    ' + l)
            print()
    for arg in args:
        if os.path.exists(arg):
            print(f"skipping '{arg}' because the directory already exists")
            continue
        if not os.path.isfile(GITCONFIGS + arg):
            print(f"skipping '{arg}' because {GITCONFIGS}/{arg} doesn't exist")
            continue
        subprocess.run(['git', 'init', arg])
        os.unlink(arg + '/.git/config')
        os.symlink('../../' + GITCONFIGS + arg, arg + '/.git/config')
        subprocess.run(['git', 'pull'], cwd=arg)

elif cmd == 'mv':
    if len(args) != 2:
        print_usage()
    src, dest = args
    if '/' in src or '/' in dest:
        sys.exit('fatal: src and dest may not contain slashes')
    if src == 'gitconfigs':
        sys.exit('fatal: cannot move gitconfigs')
    link_error = get_link_error(src)
    if link_error:
        sys.exit('aborting because src is not properly linked: ' + link_error)
    if os.path.exists(dest):
        sys.exit(f"fatal: repo destination '{dest}' already exists")
    if os.path.exists(GITCONFIGS + dest):
        sys.exit(f"fatal: config destination '{GITCONFIGS + dest}' already exists")

    os.rename(src, dest)
    os.rename(GITCONFIGS + src, GITCONFIGS + dest)
    os.unlink(dest + '/.git/config')
    os.symlink('../../' + GITCONFIGS + dest, dest + '/.git/config')
else:
    print_usage()