Skip to content

Commit 3a095fb

Browse files
authored
Merge pull request #85 from acuarica/submodule
Submodule
2 parents f39c40c + b1dc95c commit 3a095fb

9 files changed

Lines changed: 151 additions & 64 deletions

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
.LSOverride
66

77
# Icon must end with two \r
8-
Icon
8+
Icon
9+
910

1011
# Thumbnails
1112
._*
@@ -72,3 +73,5 @@ Session.vim
7273
tags
7374

7475
.vimrc
76+
77+
/.idea/

command/record.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ func (c RecordCmd) Run(args []string) int {
7878

7979
fileToRecord := ""
8080
if terminal {
81-
projPath, err := scm.RootPath()
81+
projPath, err := scm.GitRepoPath()
8282
if err != nil {
8383
// if not found, ignore error
8484
return 0
8585
}
86+
projPath, _ = scm.Workdir(projPath)
8687
fileToRecord = filepath.Join(projPath, ".gtm", "terminal.app")
8788
} else {
8889
fileToRecord = cmdFlags.Args()[0]

command/report.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ func (c ReportCmd) Run(args []string) int {
136136
}
137137
commits = append(commits, scanner.Text())
138138
}
139-
curProjPath, err := scm.RootPath()
139+
curProjPath, err := scm.GitRepoPath()
140+
curProjPath, _ = scm.Workdir(curProjPath)
140141
if err != nil {
141142
c.Ui.Error(err.Error())
142143
return 1
@@ -152,7 +153,8 @@ func (c ReportCmd) Run(args []string) int {
152153
}
153154
commits = append(commits, a)
154155
}
155-
curProjPath, err := scm.RootPath()
156+
curProjPath, err := scm.GitRepoPath()
157+
curProjPath, _ = scm.Workdir(curProjPath)
156158
if err != nil {
157159
c.Ui.Error(err.Error())
158160
return 1

gtmdebug/gtmdebug.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
package gtmdebug
3+
4+
import (
5+
"log"
6+
"fmt"
7+
)
8+
9+
func Debugf(module string, format string, v ...interface{}) {
10+
if false {
11+
log.Printf(fmt.Sprintf("[%s] ", module)+format, v...)
12+
}
13+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import (
88
"os"
99

1010
"github.com/git-time-metric/gtm/command"
11+
"github.com/git-time-metric/gtm/gtmdebug"
1112
"github.com/mitchellh/cli"
1213
)
1314

1415
// Version is the released version set during the release build process
1516
var Version = "0.0.0"
1617

1718
func main() {
19+
gtmdebug.Debugf("main.go", "Starting main %s", Version)
1820
ui := &cli.ColoredUi{ErrorColor: cli.UiColorRed, Ui: &cli.BasicUi{Writer: os.Stdout, Reader: os.Stdin}}
1921
c := cli.NewCLI("gtm", Version)
2022
c.Args = os.Args[1:]

project/project.go

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919

2020
"github.com/git-time-metric/gtm/scm"
2121
"github.com/git-time-metric/gtm/util"
22-
isatty "github.com/mattn/go-isatty"
22+
"github.com/git-time-metric/gtm/gtmdebug"
23+
"github.com/mattn/go-isatty"
2324
)
2425

2526
var (
@@ -83,23 +84,31 @@ The following items have been removed.
8384
// Initialize initializes a git repo for time tracking
8485
func Initialize(terminal bool, tags []string, clearTags bool) (string, error) {
8586
wd, err := os.Getwd()
87+
gtmdebug.Debugf("[command/project.go] wd=%s", wd)
88+
8689
if err != nil {
8790
return "", err
8891
}
8992

90-
projRoot, err := scm.RootPath(wd)
93+
gitRepoPath, err := scm.GitRepoPath(wd)
94+
gtmdebug.Debugf("[command/project.go::Initialize] gitRepoPath=%s", gitRepoPath)
9195
if err != nil {
9296
return "", fmt.Errorf(
93-
"Unable to intialize Git Time Metric, Git repository not found in %s", projRoot)
97+
"Unable to intialize Git Time Metric, Git repository not found in '%s'", gitRepoPath)
98+
}
99+
if _, err := os.Stat(gitRepoPath); os.IsNotExist(err) {
100+
return "", fmt.Errorf(
101+
"Unable to intialize Git Time Metric, Git repository not found in %s", gitRepoPath)
94102
}
95103

96-
gitPath := filepath.Join(projRoot, ".git")
97-
if _, err := os.Stat(gitPath); os.IsNotExist(err) {
104+
workDirRoot, err := scm.Workdir(gitRepoPath)
105+
gtmdebug.Debugf("[command/project.go::Initialize] workDirRoot=%s", workDirRoot)
106+
if _, err := os.Stat(workDirRoot); os.IsNotExist(err) {
98107
return "", fmt.Errorf(
99-
"Unable to intialize Git Time Metric, Git repository not found in %s", gitPath)
108+
"Unable to intialize Git Time Metric, Git working tree root not found in %s", workDirRoot)
100109
}
101110

102-
gtmPath := filepath.Join(projRoot, GTMDir)
111+
gtmPath := filepath.Join(workDirRoot, GTMDir)
103112
if _, err := os.Stat(gtmPath); os.IsNotExist(err) {
104113
if err := os.MkdirAll(gtmPath, 0700); err != nil {
105114
return "", err
@@ -130,15 +139,15 @@ func Initialize(terminal bool, tags []string, clearTags bool) (string, error) {
130139
os.Remove(filepath.Join(gtmPath, "terminal.app"))
131140
}
132141

133-
if err := scm.SetHooks(GitHooks, projRoot); err != nil {
142+
if err := scm.SetHooks(GitHooks, gitRepoPath); err != nil {
134143
return "", err
135144
}
136145

137-
if err := scm.ConfigSet(GitConfig, projRoot); err != nil {
146+
if err := scm.ConfigSet(GitConfig, gitRepoPath); err != nil {
138147
return "", err
139148
}
140149

141-
if err := scm.IgnoreSet(GitIgnore, projRoot); err != nil {
150+
if err := scm.IgnoreSet(GitIgnore, workDirRoot); err != nil {
142151
return "", err
143152
}
144153

@@ -161,7 +170,7 @@ func Initialize(terminal bool, tags []string, clearTags bool) (string, error) {
161170
}{
162171
strings.Join(tags, " "),
163172
headerFormat,
164-
projRoot,
173+
workDirRoot,
165174
GitHooks,
166175
GitConfig,
167176
GitIgnore,
@@ -177,7 +186,7 @@ func Initialize(terminal bool, tags []string, clearTags bool) (string, error) {
177186
return "", err
178187
}
179188

180-
index.add(projRoot)
189+
index.add(workDirRoot)
181190
err = index.save()
182191
if err != nil {
183192
return "", err
@@ -193,24 +202,25 @@ func Uninitialize() (string, error) {
193202
return "", err
194203
}
195204

196-
projRoot, err := scm.RootPath(wd)
205+
gitRepoPath, err := scm.GitRepoPath(wd)
197206
if err != nil {
198207
return "", fmt.Errorf(
199-
"Unable to unintialize Git Time Metric, Git repository not found in %s", projRoot)
208+
"Unable to unintialize Git Time Metric, Git repository not found in %s", gitRepoPath)
200209
}
201210

202-
gtmPath := filepath.Join(projRoot, GTMDir)
211+
workDir, _ := scm.Workdir(gitRepoPath)
212+
gtmPath := filepath.Join(workDir, GTMDir)
203213
if _, err := os.Stat(gtmPath); os.IsNotExist(err) {
204214
return "", fmt.Errorf(
205215
"Unable to uninitialize Git Time Metric, %s directory not found", gtmPath)
206216
}
207-
if err := scm.RemoveHooks(GitHooks, projRoot); err != nil {
217+
if err := scm.RemoveHooks(GitHooks, gitRepoPath); err != nil {
208218
return "", err
209219
}
210-
if err := scm.ConfigRemove(GitConfig, projRoot); err != nil {
220+
if err := scm.ConfigRemove(GitConfig, gitRepoPath); err != nil {
211221
return "", err
212222
}
213-
if err := scm.IgnoreRemove(GitIgnore, projRoot); err != nil {
223+
if err := scm.IgnoreRemove(GitIgnore, workDir); err != nil {
214224
return "", err
215225
}
216226
if err := os.RemoveAll(gtmPath); err != nil {
@@ -232,7 +242,7 @@ func Uninitialize() (string, error) {
232242
GitIgnore string
233243
}{
234244
headerFormat,
235-
projRoot,
245+
workDir,
236246
GitHooks,
237247
GitConfig,
238248
GitIgnore})
@@ -246,7 +256,7 @@ func Uninitialize() (string, error) {
246256
return "", err
247257
}
248258

249-
index.remove(projRoot)
259+
index.remove(workDir)
250260
err = index.save()
251261
if err != nil {
252262
return "", err
@@ -262,11 +272,13 @@ func Clean(dr util.DateRange, terminalOnly bool) error {
262272
return err
263273
}
264274

265-
projRoot, err := scm.RootPath(wd)
275+
gitRepoPath, err := scm.GitRepoPath(wd)
266276
if err != nil {
267-
return fmt.Errorf("Unable to clean, Git repository not found in %s", projRoot)
277+
return fmt.Errorf("Unable to clean, Git repository not found in %s", gitRepoPath)
268278
}
269279

280+
projRoot, _ := scm.Workdir(gitRepoPath)
281+
270282
gtmPath := filepath.Join(projRoot, GTMDir)
271283
if _, err := os.Stat(gtmPath); os.IsNotExist(err) {
272284
return fmt.Errorf("Unable to clean GTM data, %s directory not found", gtmPath)
@@ -310,17 +322,18 @@ func Paths(wd ...string) (string, string, error) {
310322
util.TimeTrack(time.Now(), "project.Paths")
311323

312324
var (
313-
repoPath string
325+
gitRepoPath string
314326
err error
315327
)
316328
if len(wd) > 0 {
317-
repoPath, err = scm.RootPath(wd[0])
329+
gitRepoPath, err = scm.GitRepoPath(wd[0])
318330
} else {
319-
repoPath, err = scm.RootPath()
331+
gitRepoPath, err = scm.GitRepoPath()
320332
}
321333
if err != nil {
322334
return "", "", ErrNotInitialized
323335
}
336+
repoPath, _ := scm.Workdir(gitRepoPath)
324337

325338
gtmPath := filepath.Join(repoPath, GTMDir)
326339
if _, err := os.Stat(gtmPath); os.IsNotExist(err) {

scm/git.go

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,39 @@ import (
1717
"time"
1818

1919
"github.com/git-time-metric/gtm/util"
20+
"github.com/git-time-metric/gtm/gtmdebug"
2021
"github.com/libgit2/git2go"
2122
)
2223

23-
// RootPath discovers the base directory for a git repo
24-
func RootPath(path ...string) (string, error) {
25-
defer util.TimeTrack(time.Now(), "scm.RootPath")
24+
func Workdir(gitRepoPath string) (string, error) {
25+
gtmdebug.Debugf("scm/git.go::Workdir", "gitRepoPath=%s", gitRepoPath)
26+
27+
var err error
28+
var repo *git.Repository
29+
repo, err = git.OpenRepository(gitRepoPath)
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
var workDir string
35+
workDir = repo.Workdir()
36+
gtmdebug.Debugf("[scm/git.go::Workdir] workDir=%s", workDir)
37+
if !strings.HasSuffix(workDir, "/") {
38+
fmt.Errorf("ASSERT failed: Expecting suffix of work tree to be '/'. workDir=%s", workDir)
39+
}
40+
workDir = filepath.Dir(workDir)
41+
42+
return workDir, nil
43+
}
44+
45+
// GitRepoPath discovers the base directory for a git repo
46+
func GitRepoPath(path ...string) (string, error) {
47+
defer util.TimeTrack(time.Now(), "scm.GitRootPath")
2648
var (
2749
wd string
28-
p string
50+
gitRepoPath string
2951
err error
3052
)
31-
3253
if len(path) > 0 {
3354
wd = path[0]
3455
} else {
@@ -37,19 +58,15 @@ func RootPath(path ...string) (string, error) {
3758
return "", err
3859
}
3960
}
40-
41-
p, err = git.Discover(wd, false, []string{})
61+
//TODO: benchmark the call to git.Discover
62+
//TODO: optionally print result with -debug flag
63+
gitRepoPath, err = git.Discover(wd, false, []string{})
64+
gtmdebug.Debugf("[scm/git.go::GitRepoPath] gitRepoPath=%s", gitRepoPath)
4265
if err != nil {
4366
return "", err
4467
}
4568

46-
x := strings.Index(filepath.ToSlash(p), `/.git/modules/`)
47-
if x != -1 {
48-
// removing the submodule from path and return git parent
49-
p = p[:x]
50-
}
51-
52-
return strings.TrimSuffix(filepath.ToSlash(p), `/.git/`), err
69+
return gitRepoPath, nil
5370
}
5471

5572
// CommitLimiter struct filter commits by criteria
@@ -536,8 +553,6 @@ func ConfigSet(settings map[string]string, wd ...string) error {
536553
} else {
537554
repo, err = openRepository()
538555
}
539-
//FIXME: check err
540-
//FIXME: defer repo.free()
541556

542557
cfg, err = repo.Config()
543558
defer cfg.Free()
@@ -564,8 +579,6 @@ func ConfigRemove(settings map[string]string, wd ...string) error {
564579
} else {
565580
repo, err = openRepository()
566581
}
567-
//FIXME: check err
568-
//FIXME: defer repo.free()
569582

570583
cfg, err = repo.Config()
571584
defer cfg.Free()
@@ -625,13 +638,14 @@ func SetHooks(hooks map[string]GitHook, wd ...string) error {
625638
if len(wd) > 0 {
626639
p = wd[0]
627640
} else {
641+
628642
p, err = os.Getwd()
629643
if err != nil {
630644
return err
631645
}
632646
}
633-
fp := filepath.Join(p, ".git", "hooks", ghfile)
634-
hooksDir := filepath.Join(p, ".git", "hooks")
647+
fp := filepath.Join(p, "hooks", ghfile)
648+
hooksDir := filepath.Join(p, "hooks")
635649

636650
var output string
637651

@@ -682,12 +696,13 @@ func RemoveHooks(hooks map[string]GitHook, wd ...string) error {
682696
if len(wd) > 0 {
683697
p = wd[0]
684698
} else {
699+
return fmt.Errorf("RemoveHooks using getwd not supported")
685700
p, err = os.Getwd()
686701
if err != nil {
687702
return err
688703
}
689704
}
690-
fp := filepath.Join(p, ".git", "hooks", ghfile)
705+
fp := filepath.Join(p, "hooks", ghfile)
691706

692707
if _, err := os.Stat(fp); os.IsNotExist(err) {
693708
continue
@@ -803,9 +818,9 @@ func openRepository(wd ...string) (*git.Repository, error) {
803818
)
804819

805820
if len(wd) > 0 {
806-
p, err = RootPath(wd[0])
821+
p, err = GitRepoPath(wd[0])
807822
} else {
808-
p, err = RootPath()
823+
p, err = GitRepoPath()
809824
}
810825
if err != nil {
811826
return nil, err

0 commit comments

Comments
 (0)