about summary refs log tree commit diff
path: root/tools/git-bootstrap/main.go
blob: 23041acdc33e0d6c39431be1c0f3450c9250b164 (plain) (blame)
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
229
230
231
232
233
234
package main

import (
	"flag"
	"fmt"
	"log"
	"os"
	"os/exec"
	"path"
	"strings"
	"time"
)

const (
	defaultLicense = `Copyright (c) %d %s

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
`
)

const (
	forgeBaseURL = "https://git.fcuny.net"
)

var (
	publicRepo = flag.Bool("public", false, "create a public repository at git.fcuny.net")
	userEmail  = flag.String("email", "franck@fcuny.net", "the author email that will be used for user.email")
	userName   = flag.String("username", "Franck Cuny", "the author name that will be used for user.name")
)

// GitRepo is the repository
type GitRepo struct {
	Name string
	Path string
}

func init() {
	flag.Parse()
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s\n", os.Args[0])
		flag.PrintDefaults()
	}
}

func main() {
	if flag.Arg(0) != "" {
		path := flag.Arg(0)
		if _, err := os.Stat(path); err != nil {
			if os.IsNotExist(err) {
				if err = os.Mkdir(path, 0766); err != nil {
					log.Fatalf("Failed to create directory %s: %v", path, err)
				}
			}
			if err = os.Chdir(path); err != nil {
				log.Fatalf("Failed to change working directory to %s: %v", path, err)
			}
		}
	}
	repo, err := newRepository()

	if err != nil {
		log.Fatalf("Failed to get working directory for the repository: %v", err)
	}

	repo.Create()
	repo.SetUser()
	repo.InitialCommit()
	repo.AddLicense()
	repo.AddReadme()
	repo.Commit()
}

func newRepository() (GitRepo, error) {
	cwd, err := os.Getwd()
	if err != nil {
		return GitRepo{}, err
	}

	baseName := path.Base(cwd)
	repo := GitRepo{
		Name: baseName,
		Path: cwd,
	}
	return repo, nil
}

// Run runs a git command
func (r *GitRepo) Run(args []string) ([]string, error) {
	out, err := exec.Command("git", args...).Output()
	if err != nil {
		return []string{}, err
	}
	return strings.Split(string(out), "\n"), nil
}

// Create creates a git repository
func (r *GitRepo) Create() {
	if _, err := os.Stat(".git"); err != nil {
		if os.IsNotExist(err) {
			_, err := r.Run([]string{"init"})
			if err != nil {
				log.Fatalf("Error while creating the repository: %v", err)
			}
		} else {
			log.Fatalf("Unexpected error: %v", err)
		}
	}
}

// SetUser sets the correct username and email in .git/config
func (r *GitRepo) SetUser() {
	config, err := r.Run([]string{"config", "--local", "user.name"})
	if err != nil || config[0] != *userName {
		if _, err := r.Run([]string{"config", "--local", "user.name", *userName}); err != nil {
			log.Printf("failed to set the username to %s: %v", *userName, err)
		}
	}

	config, err = r.Run([]string{"config", "--local", "user.email"})
	if err != nil || config[0] != *userEmail {
		if _, err := r.Run([]string{"config", "--local", "user.email", *userEmail}); err != nil {
			log.Printf("failed to set the email to %s: %v", *userEmail, err)
		}
	}
}

// InitialCommit creates the initial commit in the new repository
func (r *GitRepo) InitialCommit() {
	if _, err := os.Stat(".git/refs/heads/main"); err != nil {
		if os.IsNotExist(err) {
			_, err := r.Run([]string{"commit", "--allow-empty", "-m", "Initial commit\n\nThis commit is empty on purpose."})
			if err != nil {
				log.Fatalf("Error while creating the initial commit: %v", err)
			}
		}
	}
}

// AddLicense adds a license file to the repository
func (r *GitRepo) AddLicense() {
	licenseFiles := []string{"LICENSE", "license", "LICENSE.txt", "license.txt"}
	licenseExists := skipIfExist(licenseFiles)

	if licenseExists {
		return
	}

	head := []byte(fmt.Sprintf(defaultLicense, time.Now().Year(), *userName))
	f, err := os.Create("LICENSE.txt")
	if err != nil {
		log.Fatalf("Error while creating the license file: %v", err)
	}
	defer f.Close()

	_, err = f.Write(head)
	if err != nil {
		log.Fatalf("Error while writing the license file: %v", err)
	}
}

// AddReadme adds a README to the repository
func (r *GitRepo) AddReadme() {
	readmeFiles := []string{"README", "README.txt", "README.md", "README.org"}
	readmeExists := skipIfExist(readmeFiles)

	if readmeExists {
		return
	}

	head := []byte(fmt.Sprintf("#+TITLE: %s", r.Name))
	f, err := os.Create("README.org")
	if err != nil {
		log.Fatalf("Error while creating the README file: %v", err)
	}
	defer f.Close()
	_, err = f.Write(head)
	if err != nil {
		log.Fatalf("Error while writing the README file: %v", err)
	}
}

//Commit commit the files to the repository
func (r *GitRepo) Commit() {
	filesAdded := []string{}
	for _, file := range []string{"README.org", "LICENSE.txt"} {
		skipFile := false
		if _, err := os.Stat(file); err != nil {
			if os.IsNotExist(err) {
				skipFile = true
			} else {
				log.Fatalf("Error while committing %s: %v", file, err)
			}
		}
		if skipFile {
			continue
		}
		_, err := r.Run([]string{"add", file})
		if err != nil {
			log.Fatalf("Error while adding %s: %v", file, err)
		}
		filesAdded = append(filesAdded, file)
	}
	commitMessage := fmt.Sprintf("Add %s", strings.Join(filesAdded, ", "))
	if _, err := r.Run([]string{"commit", "-m", commitMessage}); err != nil {
		log.Fatalf("Error while committing the base files: %v", err)
	}
}

func skipIfExist(fileNames []string) bool {
	for _, fileName := range fileNames {
		_, err := os.Stat(fileName)
		if err == nil {
			return true
		}
	}
	return false
}