Beispiel #1
0
Datei: git.go Projekt: dvln/vcs
// gitUpdateRefs is fired if GitUpdate() gets specific refs to operate
// on... meaning fetch or delete ops (at this point).  Params:
//	u (*GitUpdater): has all the data we need to run the update
// Returns results (vcs cmds run, output) and any error that may have occurred
func gitUpdateRefs(u *GitUpdater) (Resulter, error) {
	var err error
	results := newResults()
	runOpt := "-C"
	runDir := u.LocalRepoPath()
	for ref, refOp := range u.refs {
		var result *Result
		switch refOp {
		case RefDelete:
			result, err = run(gitTool, runOpt, runDir, "update-ref", "-d", ref)
			results.add(result)
		case RefFetch:
			if u.mirror { // request is to mirror refs exactly, do so
				refSpec := fmt.Sprintf("+%s:%s", ref, ref)
				result, err = run(gitTool, runOpt, runDir, "fetch", u.RemoteRepoName(), refSpec)
			} else { // normal fetch requested, heads remapped, all else comes in "as-is"
				m := refsRegex.FindStringSubmatch(ref) // look for refs/heads/<name> refs
				if m[1] != "" {                        // if it was a refs/heads then map it:
					remoteRef := fmt.Sprintf("refs/remotes/%s/%s", u.RemoteRepoName(), m[1])
					refSpec := fmt.Sprintf("+%s:%s", ref, remoteRef)
					result, err = run(gitTool, runOpt, runDir, "fetch", u.RemoteRepoName(), refSpec)
				} else { // bring in tags/etc under the same namespace
					refSpec := fmt.Sprintf("+%s:%s", ref, ref)
					result, err = run(gitTool, runOpt, runDir, "fetch", u.RemoteRepoName(), refSpec)
				}
			}
			results.add(result)
		default:
			err = out.NewErrf(4502, "Update refs: invalid ref operation given \"%v\", clone: %s", refOp, u.LocalRepoPath())
		}
	}
	return results, err
}
Beispiel #2
0
Datei: git.go Projekt: dvln/vcs
// GitHookInstall is used to install a hook into a git clone, params:
//	h (*GitHookMgr): the hook mgr structure (find location of repo/etc)
//	path (string): where is the hook we wish to install?
//	name (string): what is the "git name" for the hook?
//	link (bool): is hook a symlink to hookPath, or full copy/install?
// Returns full path/name to git hook installed along w/any error seen
func GitHookInstall(h *GitHookMgr, path, name string, link bool) (string, error) {
	repoPath, _, err := h.Exists(LocalPath)
	hookInstallPath := ""
	if err == nil && repoPath != "" { // if the local path exists...
		hookInstallPath = filepath.Join(repoPath, ".git", "hooks", name)
		if isBareRepo(repoPath) {
			hookInstallPath = filepath.Join(repoPath, "hooks", name)
		}
		if there, err := file.Exists(hookInstallPath); err == nil && there {
			err = os.Remove(hookInstallPath)
			if err != nil {
				return "", out.WrapErr(err, "Failed to remove previously installed hook", 4510)
			}
		}
		if there, err := file.Exists(path); err != nil || !there {
			if err != nil {
				return "", out.WrapErr(err, "Hook install failed checking source hook existence", 4511)
			}
			return "", out.NewErrf(4512, "Hook install failed, hook source path does not exist:\n  path: %s", path)
		}
		oldUmask := syscall.Umask(0)
		defer syscall.Umask(oldUmask)
		if link { // if symlink desired, try and create that
			err = os.Symlink(path, hookInstallPath)
			if err != nil {
				err = out.WrapErrf(err, 4513, "Hook install failed, failed to set up symlink:\n  linktgt: %s\n  link: %s\n", path, hookInstallPath)
			}
		} else { // otherwise try and copy in the hook file
			_, err = file.CopyFileSetPerms(path, hookInstallPath, 0775)
			if err != nil {
				err = out.WrapErrf(err, 4514, "Hook install failed, failed to copy hook file:\n  hook source path %s\n  hook install path: %s\n", path, hookInstallPath)
			}
		}
	}
	return hookInstallPath, err
}