Esempio n. 1
0
func runSnapshotCreate(env *cmdline.Env, args []string) error {
	if len(args) != 1 {
		return env.UsageErrorf("unexpected number of arguments")
	}
	label := args[0]
	ctx := tool.NewContextFromEnv(env)
	if err := checkSnapshotDir(ctx); err != nil {
		return err
	}
	snapshotDir, err := getSnapshotDir()
	if err != nil {
		return err
	}
	snapshotFile := filepath.Join(snapshotDir, "labels", label, time.Now().Format(timeFormatFlag))
	// Either atomically create a new snapshot that captures the project
	// state and push the changes to the remote repository (if
	// applicable), or fail with no effect.
	createFn := func() error {
		revision, err := ctx.Git().CurrentRevision()
		if err != nil {
			return err
		}
		if err := createSnapshot(ctx, snapshotDir, snapshotFile, label); err != nil {
			// Clean up on all errors.
			ctx.Git().Reset(revision)
			ctx.Git().RemoveUntrackedFiles()
			return err
		}
		return nil
	}

	// Execute the above function in the snapshot directory.
	p := project.Project{
		Path:     snapshotDir,
		Protocol: "git",
		Revision: "HEAD",
	}
	if err := project.ApplyToLocalMaster(ctx, project.Projects{p.Name: p}, createFn); err != nil {
		return err
	}
	return nil
}
Esempio n. 2
0
func runSnapshotCreate(jirix *jiri.X, args []string) error {
	if len(args) != 1 {
		return jirix.UsageErrorf("unexpected number of arguments")
	}
	label := args[0]
	snapshotDir, err := getSnapshotDir(jirix)
	if err != nil {
		return err
	}
	snapshotFile := filepath.Join(snapshotDir, "labels", label, time.Now().Format(timeFormatFlag))

	if !pushRemoteFlag {
		// No git operations necessary.  Just create the snapshot file.
		return createSnapshot(jirix, snapshotDir, snapshotFile, label)
	}

	// Attempt to create a snapshot on a clean master branch.  If snapshot
	// creation fails, return to the state we were in before.
	createFn := func() error {
		git := gitutil.New(jirix.NewSeq())
		revision, err := git.CurrentRevision()
		if err != nil {
			return err
		}
		if err := createSnapshot(jirix, snapshotDir, snapshotFile, label); err != nil {
			git.Reset(revision)
			git.RemoveUntrackedFiles()
			return err
		}
		return commitAndPushChanges(jirix, snapshotDir, snapshotFile, label)
	}

	// Execute the above function in the snapshot directory on a clean master branch.
	p := project.Project{
		Path:         snapshotDir,
		Protocol:     "git",
		RemoteBranch: "master",
		Revision:     "HEAD",
	}
	return project.ApplyToLocalMaster(jirix, project.Projects{p.Key(): p}, createFn)
}