File: files.go

package info (click to toggle)
gitbatch 0.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 644 kB
  • sloc: makefile: 5; sh: 1
file content (80 lines) | stat: -rw-r--r-- 2,209 bytes parent folder | download
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
package app

import (
	"io/ioutil"
	"os"
	"path/filepath"
)

// generateDirectories returns possible git repositories to pipe into git pkg
// load function
func generateDirectories(dirs []string, depth int) []string {
	gitDirs := make([]string, 0)
	for i := 0; i < depth; i++ {
		directories, repositories := walkRecursive(dirs, gitDirs)
		dirs = directories
		gitDirs = repositories
	}
	return gitDirs
}

// returns given values, first search directories and second stands for possible
// git repositories. Call this func from a "for i := 0; i<depth; i++" loop
func walkRecursive(search, appendant []string) ([]string, []string) {
	max := len(search)
	for i := 0; i < max; i++ {
		if i >= len(search) {
			continue
		}
		// find possible repositories and remaining ones, b slice is possible ones
		a, b, err := separateDirectories(search[i])
		if err != nil {
			continue
		}
		// since we started to search let's get rid of it and remove from search
		// array
		search[i] = search[len(search)-1]
		search = search[:len(search)-1]
		// lets append what we have found to continue recursion
		search = append(search, a...)
		appendant = append(appendant, b...)
	}
	return search, appendant
}

// separateDirectories is to find all the files in given path. This method
// does not check if the given file is a valid git repositories
func separateDirectories(directory string) ([]string, []string, error) {
	dirs := make([]string, 0)
	gitDirs := make([]string, 0)
	files, err := ioutil.ReadDir(directory)
	// can we read the directory?
	if err != nil {
		return nil, nil, nil
	}
	for _, f := range files {
		repo := directory + string(os.PathSeparator) + f.Name()
		file, err := os.Open(repo)
		// if we cannot open it, simply continue to iteration and don't consider
		if err != nil {
			file.Close()
			continue
		}
		dir, err := filepath.Abs(file.Name())
		if err != nil {
			file.Close()
			continue
		}
		// with this approach, we ignore submodule or sub repositories in a git repository
		ff, err := os.Open(dir + string(os.PathSeparator) + ".git")
		if err != nil {
			dirs = append(dirs, dir)
		} else {
			gitDirs = append(gitDirs, dir)
		}
		ff.Close()
		file.Close()

	}
	return dirs, gitDirs, nil
}