func (root FakeJiriRoot) writeManifest(ctx *tool.Context, manifest *Manifest, dir, path string) error { bytes, err := xml.Marshal(manifest) if err != nil { return fmt.Errorf("Marshal(%v) failed: %v", manifest, err) } if err := ctx.Run().WriteFile(path, bytes, os.FileMode(0600)); err != nil { return err } if err := ctx.Git(tool.RootDirOpt(dir)).Add(path); err != nil { return err } if err := ctx.Git(tool.RootDirOpt(dir)).Commit(); err != nil { return err } return nil }
// DisableRemoteManifestPush disables pushes to the remote manifest // repository. func (root FakeJiriRoot) DisableRemoteManifestPush(ctx *tool.Context) error { dir := tool.RootDirOpt(filepath.Join(root.remote, manifestProject)) if err := ctx.Git(dir).CheckoutBranch("master"); err != nil { return err } return nil }
// EnableRemoteManifestPush enables pushes to the remote manifest // repository. func (root FakeJiriRoot) EnableRemoteManifestPush(ctx *tool.Context) error { dir := tool.RootDirOpt(filepath.Join(root.remote, manifestProject)) if !ctx.Git(dir).BranchExists("non-master") { if err := ctx.Git(dir).CreateBranch("non-master"); err != nil { return err } } if err := ctx.Git(dir).CheckoutBranch("non-master"); err != nil { return err } return nil }
func (op deleteOperation) Run(ctx *tool.Context, _ *Manifest) error { if op.gc { // Never delete the <JiriProject>. if op.project.Name == JiriProject { lines := []string{ fmt.Sprintf("NOTE: project %v was not found in the project manifest", op.project.Name), "however this project is required for correct operation of the jiri", "development tools and will thus not be deleted", } opts := runutil.Opts{Verbose: true} ctx.Run().OutputWithOpts(opts, lines) return nil } // Never delete projects with non-master branches, uncommitted // work, or untracked content. git := ctx.Git(tool.RootDirOpt(op.project.Path)) branches, _, err := git.GetBranches() if err != nil { return err } uncommitted, err := git.HasUncommittedChanges() if err != nil { return err } untracked, err := git.HasUntrackedFiles() if err != nil { return err } if len(branches) != 1 || uncommitted || untracked { lines := []string{ fmt.Sprintf("NOTE: project %v was not found in the project manifest", op.project.Name), "however this project either contains non-master branches, uncommitted", "work, or untracked files and will thus not be deleted", } opts := runutil.Opts{Verbose: true} ctx.Run().OutputWithOpts(opts, lines) return nil } return ctx.Run().RemoveAll(op.source) } lines := []string{ fmt.Sprintf("NOTE: project %v was not found in the project manifest", op.project.Name), "it was not automatically removed to avoid deleting uncommitted work", fmt.Sprintf(`if you no longer need it, invoke "rm -rf %v"`, op.source), `or invoke "jiri update -gc" to remove all such local projects`, } opts := runutil.Opts{Verbose: true} ctx.Run().OutputWithOpts(opts, lines) return nil }
// CreateRemoteProject creates a new remote project. func (root FakeJiriRoot) CreateRemoteProject(ctx *tool.Context, name string) error { projectDir := filepath.Join(root.remote, name) if err := ctx.Run().MkdirAll(projectDir, os.FileMode(0700)); err != nil { return err } if err := ctx.Git().Init(projectDir); err != nil { return err } if err := ctx.Git(tool.RootDirOpt(projectDir)).CommitWithMessage("initial commit"); err != nil { return err } root.Projects[name] = projectDir return nil }
func setProjectState(ctx *tool.Context, state *ProjectState, checkDirty bool, ch chan<- error) { var err error switch state.Project.Protocol { case "git": scm := ctx.Git(tool.RootDirOpt(state.Project.Path)) var branches []string branches, state.CurrentBranch, err = scm.GetBranches() if err != nil { ch <- err return } for _, branch := range branches { file := filepath.Join(state.Project.Path, MetadataDirName(), branch, ".gerrit_commit_message") hasFile := true if _, err := ctx.Run().Stat(file); err != nil { if !os.IsNotExist(err) { ch <- err return } hasFile = false } state.Branches = append(state.Branches, BranchState{ Name: branch, HasGerritMessage: hasFile, }) } if checkDirty { state.HasUncommitted, err = scm.HasUncommittedChanges() if err != nil { ch <- err return } state.HasUntracked, err = scm.HasUntrackedFiles() if err != nil { ch <- err return } } default: ch <- UnsupportedProtocolErr(state.Project.Protocol) return } ch <- nil }
// addProjectToManifest records the information about the given // project in the given manifest. The function is used to create a // manifest that records the current state of jiri projects, which // can be used to restore this state at some later point. // // NOTE: The function assumes that the the given project is on a // master branch. func addProjectToManifest(ctx *tool.Context, manifest *Manifest, project Project) error { // If the project uses relative revision, replace it with an absolute one. switch project.Protocol { case "git": if project.Revision == "HEAD" { revision, err := ctx.Git(tool.RootDirOpt(project.Path)).CurrentRevision() if err != nil { return err } project.Revision = revision } default: return UnsupportedProtocolErr(project.Protocol) } relPath, err := ToRel(project.Path) if err != nil { return err } project.Path = relPath manifest.Projects = append(manifest.Projects, project) return nil }